2023-12-20 12:02:40 +03:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* SDL Client
|
|
|
|
*
|
|
|
|
* Copyright 2023 Armin Novak <armin.novak@thincast.com>
|
|
|
|
* Copyright 2023 Thincast Technologies GmbH
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
#include "sdl_window.hpp"
|
2024-02-13 13:02:33 +03:00
|
|
|
#include "sdl_utils.hpp"
|
2023-12-20 12:02:40 +03:00
|
|
|
|
|
|
|
SdlWindow::SdlWindow(const std::string& title, Sint32 startupX, Sint32 startupY, Sint32 width,
|
|
|
|
Sint32 height, Uint32 flags)
|
|
|
|
{
|
2024-05-07 11:16:02 +03:00
|
|
|
auto props = SDL_CreateProperties();
|
|
|
|
SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, title.c_str());
|
|
|
|
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, startupX);
|
|
|
|
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, startupY);
|
|
|
|
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, width);
|
|
|
|
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, height);
|
|
|
|
// SDL_SetProperty(props, SDL_PROP_WINDOW_CREATE_FL);
|
|
|
|
_window = SDL_CreateWindowWithProperties(props);
|
|
|
|
SDL_DestroyProperties(props);
|
2023-12-20 12:02:40 +03:00
|
|
|
}
|
|
|
|
|
2024-08-29 17:00:42 +03:00
|
|
|
SdlWindow::SdlWindow(SdlWindow&& other) noexcept
|
2023-12-20 12:02:40 +03:00
|
|
|
: _window(other._window), _offset_x(other._offset_x), _offset_y(other._offset_y)
|
|
|
|
{
|
|
|
|
other._window = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SdlWindow::~SdlWindow()
|
|
|
|
{
|
|
|
|
SDL_DestroyWindow(_window);
|
|
|
|
}
|
|
|
|
|
2024-02-13 13:02:33 +03:00
|
|
|
Uint32 SdlWindow::id() const
|
|
|
|
{
|
|
|
|
if (!_window)
|
|
|
|
return 0;
|
|
|
|
return SDL_GetWindowID(_window);
|
|
|
|
}
|
|
|
|
|
|
|
|
int SdlWindow::displayIndex() const
|
|
|
|
{
|
|
|
|
if (!_window)
|
|
|
|
return 0;
|
2024-05-07 11:16:02 +03:00
|
|
|
return SDL_GetDisplayForWindow(_window);
|
2024-02-13 13:02:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Rect SdlWindow::rect() const
|
|
|
|
{
|
|
|
|
SDL_Rect rect = {};
|
|
|
|
if (_window)
|
|
|
|
{
|
|
|
|
SDL_GetWindowPosition(_window, &rect.x, &rect.y);
|
|
|
|
SDL_GetWindowSize(_window, &rect.w, &rect.h);
|
|
|
|
}
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2023-12-20 12:02:40 +03:00
|
|
|
SDL_Window* SdlWindow::window() const
|
|
|
|
{
|
|
|
|
return _window;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sint32 SdlWindow::offsetX() const
|
|
|
|
{
|
|
|
|
return _offset_x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SdlWindow::setOffsetX(Sint32 x)
|
|
|
|
{
|
|
|
|
_offset_x = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SdlWindow::setOffsetY(Sint32 y)
|
|
|
|
{
|
|
|
|
_offset_y = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sint32 SdlWindow::offsetY() const
|
|
|
|
{
|
|
|
|
return _offset_y;
|
|
|
|
}
|
2024-02-13 13:02:33 +03:00
|
|
|
|
|
|
|
bool SdlWindow::grabKeyboard(bool enable)
|
|
|
|
{
|
|
|
|
if (!_window)
|
|
|
|
return false;
|
|
|
|
SDL_SetWindowKeyboardGrab(_window, enable ? SDL_TRUE : SDL_FALSE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SdlWindow::grabMouse(bool enable)
|
|
|
|
{
|
|
|
|
if (!_window)
|
|
|
|
return false;
|
|
|
|
SDL_SetWindowMouseGrab(_window, enable ? SDL_TRUE : SDL_FALSE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SdlWindow::setBordered(bool bordered)
|
|
|
|
{
|
|
|
|
if (_window)
|
|
|
|
SDL_SetWindowBordered(_window, bordered ? SDL_TRUE : SDL_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SdlWindow::raise()
|
|
|
|
{
|
|
|
|
SDL_RaiseWindow(_window);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SdlWindow::resizeable(bool use)
|
|
|
|
{
|
|
|
|
SDL_SetWindowResizable(_window, use ? SDL_TRUE : SDL_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SdlWindow::fullscreen(bool enter)
|
|
|
|
{
|
|
|
|
auto curFlags = SDL_GetWindowFlags(_window);
|
|
|
|
|
|
|
|
if (enter)
|
|
|
|
{
|
|
|
|
if (!(curFlags & SDL_WINDOW_BORDERLESS))
|
|
|
|
{
|
2024-05-07 11:16:02 +03:00
|
|
|
auto idx = SDL_GetDisplayForWindow(_window);
|
|
|
|
auto mode = SDL_GetCurrentDisplayMode(idx);
|
2024-02-13 13:02:33 +03:00
|
|
|
|
|
|
|
SDL_RestoreWindow(_window); // Maximize so we can see the caption and
|
|
|
|
// bits
|
|
|
|
SDL_SetWindowBordered(_window, SDL_FALSE);
|
|
|
|
SDL_SetWindowPosition(_window, 0, 0);
|
|
|
|
SDL_SetWindowAlwaysOnTop(_window, SDL_TRUE);
|
|
|
|
SDL_RaiseWindow(_window);
|
2024-05-07 11:16:02 +03:00
|
|
|
if (mode)
|
|
|
|
SDL_SetWindowSize(_window, mode->w, mode->h);
|
2024-02-13 13:02:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (curFlags & SDL_WINDOW_BORDERLESS)
|
|
|
|
{
|
|
|
|
|
|
|
|
SDL_SetWindowBordered(_window, SDL_TRUE);
|
|
|
|
SDL_SetWindowAlwaysOnTop(_window, SDL_FALSE);
|
|
|
|
SDL_RaiseWindow(_window);
|
|
|
|
SDL_MinimizeWindow(_window); // Maximize so we can see the caption and bits
|
|
|
|
SDL_MaximizeWindow(_window); // Maximize so we can see the caption and bits
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-10 13:03:37 +03:00
|
|
|
void SdlWindow::minimize()
|
|
|
|
{
|
|
|
|
SDL_MinimizeWindow(_window);
|
|
|
|
}
|
|
|
|
|
2024-02-13 13:02:33 +03:00
|
|
|
bool SdlWindow::fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
|
|
|
|
{
|
|
|
|
auto surface = SDL_GetWindowSurface(_window);
|
|
|
|
if (!surface)
|
|
|
|
return false;
|
|
|
|
SDL_Rect rect = { 0, 0, surface->w, surface->h };
|
2024-08-12 12:50:02 +03:00
|
|
|
auto color = SDL_MapSurfaceRGBA(surface, r, g, b, a);
|
2024-02-13 13:02:33 +03:00
|
|
|
|
2024-05-07 11:16:02 +03:00
|
|
|
SDL_FillSurfaceRect(surface, &rect, color);
|
2024-02-13 13:02:33 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SdlWindow::blit(SDL_Surface* surface, const SDL_Rect& srcRect, SDL_Rect& dstRect)
|
|
|
|
{
|
|
|
|
auto screen = SDL_GetWindowSurface(_window);
|
|
|
|
if (!screen || !surface)
|
|
|
|
return false;
|
2024-05-07 11:16:02 +03:00
|
|
|
if (!SDL_SetSurfaceClipRect(surface, &srcRect))
|
2024-03-11 14:18:49 +03:00
|
|
|
return true;
|
2024-05-07 11:16:02 +03:00
|
|
|
if (!SDL_SetSurfaceClipRect(screen, &dstRect))
|
2024-03-11 14:18:49 +03:00
|
|
|
return true;
|
2024-09-05 15:48:40 +03:00
|
|
|
if (!SDL_BlitSurfaceScaled(surface, &srcRect, screen, &dstRect, SDL_SCALEMODE_LINEAR))
|
2024-02-13 13:02:33 +03:00
|
|
|
{
|
2024-09-05 15:48:40 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL_BlitScaled: %s", SDL_GetError());
|
|
|
|
return false;
|
2024-02-13 13:02:33 +03:00
|
|
|
}
|
2024-09-05 15:48:40 +03:00
|
|
|
return true;
|
2024-02-13 13:02:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SdlWindow::updateSurface()
|
|
|
|
{
|
|
|
|
SDL_UpdateWindowSurface(_window);
|
|
|
|
}
|