FreeRDP/client/SDL/SDL3/sdl_disp.cpp

472 lines
12 KiB
C++
Raw Normal View History

2022-12-30 13:25:28 +03:00
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* SDL Display Control Channel
*
* Copyright 2023 Armin Novak <armin.novak@thincast.com>
*
* 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 <vector>
2022-12-30 13:25:28 +03:00
#include <winpr/sysinfo.h>
#include <winpr/assert.h>
#include <freerdp/gdi/gdi.h>
#include <SDL3/SDL.h>
2022-12-30 13:25:28 +03:00
2023-04-14 09:36:05 +03:00
#include "sdl_disp.hpp"
#include "sdl_kbd.hpp"
#include "sdl_utils.hpp"
2023-04-14 10:16:50 +03:00
#include "sdl_freerdp.hpp"
2022-12-30 13:25:28 +03:00
#include <freerdp/log.h>
#define TAG CLIENT_TAG("sdl.disp")
#define RESIZE_MIN_DELAY 200 /* minimum delay in ms between two resizes */
#define MAX_RETRIES 5
2022-12-30 13:25:28 +03:00
BOOL sdlDispContext::settings_changed()
2022-12-30 13:25:28 +03:00
{
auto settings = _sdl->context()->settings;
2022-12-30 13:25:28 +03:00
WINPR_ASSERT(settings);
if (_lastSentWidth != _targetWidth)
2022-12-30 13:25:28 +03:00
return TRUE;
if (_lastSentHeight != _targetHeight)
2022-12-30 13:25:28 +03:00
return TRUE;
2023-10-13 10:48:44 +03:00
if (_lastSentDesktopOrientation !=
freerdp_settings_get_uint16(settings, FreeRDP_DesktopOrientation))
2022-12-30 13:25:28 +03:00
return TRUE;
2023-10-13 10:48:44 +03:00
if (_lastSentDesktopScaleFactor !=
freerdp_settings_get_uint32(settings, FreeRDP_DesktopScaleFactor))
2022-12-30 13:25:28 +03:00
return TRUE;
2023-10-13 10:48:44 +03:00
if (_lastSentDeviceScaleFactor !=
freerdp_settings_get_uint32(settings, FreeRDP_DeviceScaleFactor))
2022-12-30 13:25:28 +03:00
return TRUE;
/* TODO
if (_fullscreen != _sdl->fullscreen)
2022-12-30 13:25:28 +03:00
return TRUE;
*/
return FALSE;
}
BOOL sdlDispContext::update_last_sent()
2022-12-30 13:25:28 +03:00
{
WINPR_ASSERT(_sdl);
2022-12-30 13:25:28 +03:00
auto settings = _sdl->context()->settings;
2022-12-30 13:25:28 +03:00
WINPR_ASSERT(settings);
_lastSentWidth = _targetWidth;
_lastSentHeight = _targetHeight;
2023-10-13 10:48:44 +03:00
_lastSentDesktopOrientation = freerdp_settings_get_uint16(settings, FreeRDP_DesktopOrientation);
_lastSentDesktopScaleFactor = freerdp_settings_get_uint32(settings, FreeRDP_DesktopScaleFactor);
_lastSentDeviceScaleFactor = freerdp_settings_get_uint32(settings, FreeRDP_DeviceScaleFactor);
// TODO _fullscreen = _sdl->fullscreen;
2022-12-30 13:25:28 +03:00
return TRUE;
}
BOOL sdlDispContext::sendResize()
2022-12-30 13:25:28 +03:00
{
DISPLAY_CONTROL_MONITOR_LAYOUT layout = {};
auto settings = _sdl->context()->settings;
2022-12-30 13:25:28 +03:00
if (!settings)
return FALSE;
if (!_activated || !_disp)
2022-12-30 13:25:28 +03:00
return TRUE;
if (GetTickCount64() - _lastSentDate < RESIZE_MIN_DELAY)
2022-12-30 13:25:28 +03:00
return TRUE;
_lastSentDate = GetTickCount64();
2022-12-30 13:25:28 +03:00
if (!settings_changed())
2022-12-30 13:25:28 +03:00
return TRUE;
2023-10-13 10:48:44 +03:00
const UINT32 mcount = freerdp_settings_get_uint32(settings, FreeRDP_MonitorCount);
if (_sdl->fullscreen && (mcount > 0))
2022-12-30 13:25:28 +03:00
{
2023-10-13 10:48:44 +03:00
auto monitors = static_cast<const rdpMonitor*>(
freerdp_settings_get_pointer(settings, FreeRDP_MonitorDefArray));
if (sendLayout(monitors, mcount) != CHANNEL_RC_OK)
return FALSE;
2022-12-30 13:25:28 +03:00
}
else
{
_waitingResize = TRUE;
2022-12-30 13:25:28 +03:00
layout.Flags = DISPLAY_CONTROL_MONITOR_PRIMARY;
layout.Top = layout.Left = 0;
layout.Width = _targetWidth;
layout.Height = _targetHeight;
2023-10-13 10:48:44 +03:00
layout.Orientation = freerdp_settings_get_uint16(settings, FreeRDP_DesktopOrientation);
layout.DesktopScaleFactor =
freerdp_settings_get_uint32(settings, FreeRDP_DesktopScaleFactor);
layout.DeviceScaleFactor = freerdp_settings_get_uint32(settings, FreeRDP_DeviceScaleFactor);
layout.PhysicalWidth = _targetWidth;
layout.PhysicalHeight = _targetHeight;
2022-12-30 13:25:28 +03:00
if (IFCALLRESULT(CHANNEL_RC_OK, _disp->SendMonitorLayout, _disp, 1, &layout) !=
CHANNEL_RC_OK)
2022-12-30 13:25:28 +03:00
return FALSE;
}
return update_last_sent();
2022-12-30 13:25:28 +03:00
}
BOOL sdlDispContext::set_window_resizable()
2022-12-30 13:25:28 +03:00
{
2023-04-14 12:55:29 +03:00
_sdl->update_resizeable(TRUE);
2022-12-30 13:25:28 +03:00
return TRUE;
}
static BOOL sdl_disp_check_context(void* context, SdlContext** ppsdl, sdlDispContext** ppsdlDisp,
2022-12-30 13:25:28 +03:00
rdpSettings** ppSettings)
{
if (!context)
return FALSE;
auto sdl = get_context(context);
2024-04-19 10:13:36 +03:00
if (!sdl)
return FALSE;
2022-12-30 13:25:28 +03:00
if (!sdl->context()->settings)
2022-12-30 13:25:28 +03:00
return FALSE;
*ppsdl = sdl;
*ppsdlDisp = &sdl->disp;
*ppSettings = sdl->context()->settings;
2022-12-30 13:25:28 +03:00
return TRUE;
}
void sdlDispContext::OnActivated(void* context, const ActivatedEventArgs* e)
2022-12-30 13:25:28 +03:00
{
SdlContext* sdl = nullptr;
sdlDispContext* sdlDisp = nullptr;
rdpSettings* settings = nullptr;
2022-12-30 13:25:28 +03:00
if (!sdl_disp_check_context(context, &sdl, &sdlDisp, &settings))
return;
sdlDisp->_waitingResize = FALSE;
2022-12-30 13:25:28 +03:00
2023-10-13 10:48:44 +03:00
if (sdlDisp->_activated && !freerdp_settings_get_bool(settings, FreeRDP_Fullscreen))
2022-12-30 13:25:28 +03:00
{
sdlDisp->set_window_resizable();
2022-12-30 13:25:28 +03:00
if (e->firstActivation)
return;
sdlDisp->addTimer();
2022-12-30 13:25:28 +03:00
}
}
void sdlDispContext::OnGraphicsReset(void* context, const GraphicsResetEventArgs* e)
2022-12-30 13:25:28 +03:00
{
SdlContext* sdl = nullptr;
sdlDispContext* sdlDisp = nullptr;
rdpSettings* settings = nullptr;
2022-12-30 13:25:28 +03:00
WINPR_UNUSED(e);
if (!sdl_disp_check_context(context, &sdl, &sdlDisp, &settings))
return;
sdlDisp->_waitingResize = FALSE;
2022-12-30 13:25:28 +03:00
2023-10-13 10:48:44 +03:00
if (sdlDisp->_activated && !freerdp_settings_get_bool(settings, FreeRDP_Fullscreen))
2022-12-30 13:25:28 +03:00
{
sdlDisp->set_window_resizable();
sdlDisp->addTimer();
2022-12-30 13:25:28 +03:00
}
}
2024-08-12 12:50:02 +03:00
Uint32 sdlDispContext::OnTimer(void* param, SDL_TimerID timerID, Uint32 interval)
2022-12-30 13:25:28 +03:00
{
auto ctx = static_cast<sdlDispContext*>(param);
if (!ctx)
return 0;
SdlContext* sdl = ctx->_sdl;
2024-04-19 10:13:36 +03:00
if (!sdl)
return 0;
sdlDispContext* sdlDisp = nullptr;
rdpSettings* settings = nullptr;
2022-12-30 13:25:28 +03:00
if (!sdl_disp_check_context(sdl->context(), &sdl, &sdlDisp, &settings))
return 0;
2022-12-30 13:25:28 +03:00
WLog_Print(sdl->log, WLOG_TRACE, "checking for display changes...");
2023-10-13 10:48:44 +03:00
if (!sdlDisp->_activated || freerdp_settings_get_bool(settings, FreeRDP_Fullscreen))
return 0;
else
{
auto rc = sdlDisp->sendResize();
if (!rc)
WLog_Print(sdl->log, WLOG_TRACE, "sent new display layout, result %d", rc);
}
if (sdlDisp->_timer_retries++ >= MAX_RETRIES)
{
WLog_Print(sdl->log, WLOG_TRACE, "deactivate timer, retries exceeded");
return 0;
}
2022-12-30 13:25:28 +03:00
WLog_Print(sdl->log, WLOG_TRACE, "fire timer one more time");
return interval;
2022-12-30 13:25:28 +03:00
}
UINT sdlDispContext::sendLayout(const rdpMonitor* monitors, size_t nmonitors)
2022-12-30 13:25:28 +03:00
{
UINT ret = CHANNEL_RC_OK;
WINPR_ASSERT(monitors);
WINPR_ASSERT(nmonitors > 0);
auto settings = _sdl->context()->settings;
2022-12-30 13:25:28 +03:00
WINPR_ASSERT(settings);
std::vector<DISPLAY_CONTROL_MONITOR_LAYOUT> layouts;
layouts.resize(nmonitors);
2022-12-30 13:25:28 +03:00
for (size_t i = 0; i < nmonitors; i++)
2022-12-30 13:25:28 +03:00
{
auto monitor = &monitors[i];
auto layout = &layouts[i];
2022-12-30 13:25:28 +03:00
layout->Flags = (monitor->is_primary ? DISPLAY_CONTROL_MONITOR_PRIMARY : 0);
layout->Left = monitor->x;
layout->Top = monitor->y;
layout->Width = monitor->width;
layout->Height = monitor->height;
layout->Orientation = ORIENTATION_LANDSCAPE;
layout->PhysicalWidth = monitor->attributes.physicalWidth;
layout->PhysicalHeight = monitor->attributes.physicalHeight;
switch (monitor->attributes.orientation)
{
case 90:
layout->Orientation = ORIENTATION_PORTRAIT;
break;
case 180:
layout->Orientation = ORIENTATION_LANDSCAPE_FLIPPED;
break;
case 270:
layout->Orientation = ORIENTATION_PORTRAIT_FLIPPED;
break;
case 0:
default:
/* MS-RDPEDISP - 2.2.2.2.1:
* Orientation (4 bytes): A 32-bit unsigned integer that specifies the
* orientation of the monitor in degrees. Valid values are 0, 90, 180
* or 270
*
* So we default to ORIENTATION_LANDSCAPE
*/
layout->Orientation = ORIENTATION_LANDSCAPE;
break;
}
2023-10-13 10:48:44 +03:00
layout->DesktopScaleFactor =
freerdp_settings_get_uint32(settings, FreeRDP_DesktopScaleFactor);
layout->DeviceScaleFactor =
freerdp_settings_get_uint32(settings, FreeRDP_DeviceScaleFactor);
2022-12-30 13:25:28 +03:00
}
WINPR_ASSERT(_disp);
ret = IFCALLRESULT(CHANNEL_RC_OK, _disp->SendMonitorLayout, _disp, layouts.size(),
layouts.data());
2022-12-30 13:25:28 +03:00
return ret;
}
BOOL sdlDispContext::addTimer()
{
if (SDL_WasInit(SDL_INIT_TIMER) == 0)
return FALSE;
SDL_RemoveTimer(_timer);
WLog_Print(_sdl->log, WLOG_TRACE, "adding new display check timer");
_timer_retries = 0;
sendResize();
_timer = SDL_AddTimer(1000, sdlDispContext::OnTimer, this);
return TRUE;
}
BOOL sdlDispContext::handle_display_event(const SDL_DisplayEvent* ev)
2022-12-30 13:25:28 +03:00
{
WINPR_ASSERT(ev);
switch (ev->type)
2022-12-30 13:25:28 +03:00
{
case SDL_EVENT_DISPLAY_ADDED:
SDL_Log("A new display with id %d was connected", ev->displayID);
2022-12-30 13:25:28 +03:00
return TRUE;
case SDL_EVENT_DISPLAY_REMOVED:
SDL_Log("The display with id %d was disconnected", ev->displayID);
2022-12-30 13:25:28 +03:00
return TRUE;
case SDL_EVENT_DISPLAY_ORIENTATION:
SDL_Log("The orientation of display with id %d was changed", ev->displayID);
2022-12-30 13:25:28 +03:00
return TRUE;
default:
return TRUE;
}
}
BOOL sdlDispContext::handle_window_event(const SDL_WindowEvent* ev)
2022-12-30 13:25:28 +03:00
{
WINPR_ASSERT(ev);
auto bordered = freerdp_settings_get_bool(_sdl->context()->settings, FreeRDP_Decorations)
? SDL_TRUE
: SDL_FALSE;
auto it = _sdl->windows.find(ev->windowID);
if (it != _sdl->windows.end())
it->second.setBordered(bordered);
switch (ev->type)
2022-12-30 13:25:28 +03:00
{
case SDL_EVENT_WINDOW_HIDDEN:
case SDL_EVENT_WINDOW_MINIMIZED:
gdi_send_suppress_output(_sdl->context()->gdi, TRUE);
2022-12-30 13:25:28 +03:00
return TRUE;
case SDL_EVENT_WINDOW_EXPOSED:
case SDL_EVENT_WINDOW_SHOWN:
case SDL_EVENT_WINDOW_MAXIMIZED:
case SDL_EVENT_WINDOW_RESTORED:
gdi_send_suppress_output(_sdl->context()->gdi, FALSE);
2022-12-30 13:25:28 +03:00
return TRUE;
case SDL_EVENT_WINDOW_RESIZED:
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
_targetWidth = ev->data1;
_targetHeight = ev->data2;
return addTimer();
2022-12-30 13:25:28 +03:00
case SDL_EVENT_WINDOW_MOUSE_LEAVE:
2023-04-14 12:47:30 +03:00
WINPR_ASSERT(_sdl);
_sdl->input.keyboard_grab(ev->windowID, SDL_FALSE);
2022-12-30 13:25:28 +03:00
return TRUE;
case SDL_EVENT_WINDOW_MOUSE_ENTER:
2023-04-14 12:47:30 +03:00
WINPR_ASSERT(_sdl);
_sdl->input.keyboard_grab(ev->windowID, SDL_TRUE);
return _sdl->input.keyboard_focus_in();
case SDL_EVENT_WINDOW_FOCUS_GAINED:
return _sdl->input.keyboard_focus_in();
2022-12-30 13:25:28 +03:00
default:
return TRUE;
}
}
UINT sdlDispContext::DisplayControlCaps(DispClientContext* disp, UINT32 maxNumMonitors,
UINT32 maxMonitorAreaFactorA, UINT32 maxMonitorAreaFactorB)
2022-12-30 13:25:28 +03:00
{
/* we're called only if dynamic resolution update is activated */
WINPR_ASSERT(disp);
auto sdlDisp = reinterpret_cast<sdlDispContext*>(disp->custom);
return sdlDisp->DisplayControlCaps(maxNumMonitors, maxMonitorAreaFactorA,
maxMonitorAreaFactorB);
}
2022-12-30 13:25:28 +03:00
UINT sdlDispContext::DisplayControlCaps(UINT32 maxNumMonitors, UINT32 maxMonitorAreaFactorA,
UINT32 maxMonitorAreaFactorB)
{
auto settings = _sdl->context()->settings;
2022-12-30 13:25:28 +03:00
WINPR_ASSERT(settings);
WLog_DBG(TAG,
"DisplayControlCapsPdu: MaxNumMonitors: %" PRIu32 " MaxMonitorAreaFactorA: %" PRIu32
" MaxMonitorAreaFactorB: %" PRIu32 "",
maxNumMonitors, maxMonitorAreaFactorA, maxMonitorAreaFactorB);
_activated = TRUE;
2022-12-30 13:25:28 +03:00
2023-10-13 10:48:44 +03:00
if (freerdp_settings_get_bool(settings, FreeRDP_Fullscreen))
2022-12-30 13:25:28 +03:00
return CHANNEL_RC_OK;
WLog_DBG(TAG, "DisplayControlCapsPdu: setting the window as resizable");
return set_window_resizable() ? CHANNEL_RC_OK : CHANNEL_RC_NO_MEMORY;
2022-12-30 13:25:28 +03:00
}
BOOL sdlDispContext::init(DispClientContext* disp)
2022-12-30 13:25:28 +03:00
{
if (!disp)
2022-12-30 13:25:28 +03:00
return FALSE;
auto settings = _sdl->context()->settings;
2022-12-30 13:25:28 +03:00
if (!settings)
return FALSE;
_disp = disp;
disp->custom = this;
2022-12-30 13:25:28 +03:00
2023-10-13 10:48:44 +03:00
if (freerdp_settings_get_bool(settings, FreeRDP_DynamicResolutionUpdate))
2022-12-30 13:25:28 +03:00
{
disp->DisplayControlCaps = sdlDispContext::DisplayControlCaps;
2022-12-30 13:25:28 +03:00
}
2023-04-14 12:55:29 +03:00
_sdl->update_resizeable(TRUE);
2022-12-30 13:25:28 +03:00
return TRUE;
}
BOOL sdlDispContext::uninit(DispClientContext* disp)
2022-12-30 13:25:28 +03:00
{
if (!disp)
2022-12-30 13:25:28 +03:00
return FALSE;
_disp = nullptr;
2023-04-14 12:55:29 +03:00
_sdl->update_resizeable(FALSE);
2022-12-30 13:25:28 +03:00
return TRUE;
}
sdlDispContext::sdlDispContext(SdlContext* sdl) : _sdl(sdl), _timer(0)
{
SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
WINPR_ASSERT(_sdl);
WINPR_ASSERT(_sdl->context()->settings);
WINPR_ASSERT(_sdl->context()->pubSub);
auto settings = _sdl->context()->settings;
auto pubSub = _sdl->context()->pubSub;
2023-10-13 10:48:44 +03:00
_lastSentWidth = _targetWidth = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
_lastSentHeight = _targetHeight = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
PubSub_SubscribeActivated(pubSub, sdlDispContext::OnActivated);
PubSub_SubscribeGraphicsReset(pubSub, sdlDispContext::OnGraphicsReset);
addTimer();
}
sdlDispContext::~sdlDispContext()
{
wPubSub* pubSub = _sdl->context()->pubSub;
WINPR_ASSERT(pubSub);
PubSub_UnsubscribeActivated(pubSub, sdlDispContext::OnActivated);
PubSub_UnsubscribeGraphicsReset(pubSub, sdlDispContext::OnGraphicsReset);
SDL_RemoveTimer(_timer);
SDL_Quit();
}