FreeRDP
Loading...
Searching...
No Matches
SDL3/sdl_disp.cpp
1
20#include <vector>
21#include <winpr/sysinfo.h>
22#include <winpr/assert.h>
23
24#include <freerdp/gdi/gdi.h>
25
26#include <SDL3/SDL.h>
27
28#include "sdl_disp.hpp"
29#include "sdl_input.hpp"
30#include "sdl_context.hpp"
31
32#include <freerdp/log.h>
33#define TAG CLIENT_TAG("sdl.disp")
34
35static constexpr UINT64 RESIZE_MIN_DELAY = 500; /* minimum delay in ms between two resizes */
36static constexpr unsigned MAX_RETRIES = 5;
37
38static auto operator==(const DISPLAY_CONTROL_MONITOR_LAYOUT& a,
40{
41 if (a.Flags != b.Flags)
42 return false;
43 if (a.Left != b.Left)
44 return false;
45 if (a.Top != b.Top)
46 return false;
47 if (a.Width != b.Width)
48 return false;
49 if (a.Height != b.Height)
50 return false;
51 if (a.PhysicalWidth != b.PhysicalWidth)
52 return false;
53 if (a.PhysicalHeight != b.PhysicalHeight)
54 return false;
55 if (a.Orientation != b.Orientation)
56 return false;
57 if (a.DesktopScaleFactor != b.DesktopScaleFactor)
58 return false;
59 if (a.DeviceScaleFactor != b.DeviceScaleFactor)
60 return false;
61 return true;
62}
63
64bool sdlDispContext::settings_changed(const std::vector<DISPLAY_CONTROL_MONITOR_LAYOUT>& layout)
65{
66 return (layout != _last_sent_layout);
67}
68
69bool sdlDispContext::sendResize()
70{
71 auto settings = _sdl->context()->settings;
72
73 if (!settings)
74 return false;
75
76 if (!_activated || !_disp)
77 return true;
78
79 if (GetTickCount64() - _lastSentDate < RESIZE_MIN_DELAY)
80 return true;
81
82 _lastSentDate = GetTickCount64();
83
84 const UINT32 mcount = freerdp_settings_get_uint32(settings, FreeRDP_MonitorCount);
85 auto monitors = static_cast<const rdpMonitor*>(
86 freerdp_settings_get_pointer(settings, FreeRDP_MonitorDefArray));
87 return sendLayout(monitors, mcount);
88}
89
90bool sdlDispContext::setWindowResizeable()
91{
92 return _sdl->setResizeable(true);
93}
94
95static bool sdl_disp_check_context(void* context, SdlContext** ppsdl, sdlDispContext** ppsdlDisp,
96 rdpSettings** ppSettings)
97{
98 if (!context)
99 return false;
100
101 auto sdl = get_context(context);
102 if (!sdl)
103 return false;
104
105 if (!sdl->context()->settings)
106 return false;
107
108 *ppsdl = sdl;
109 *ppsdlDisp = &sdl->getDisplayChannelContext();
110 *ppSettings = sdl->context()->settings;
111 return true;
112}
113
114void sdlDispContext::OnActivated(void* context, const ActivatedEventArgs* e)
115{
116 SdlContext* sdl = nullptr;
117 sdlDispContext* sdlDisp = nullptr;
118 rdpSettings* settings = nullptr;
119
120 if (!sdl_disp_check_context(context, &sdl, &sdlDisp, &settings))
121 return;
122
123 sdlDisp->_waitingResize = false;
124
125 if (!sdlDisp->_activated)
126 return;
127
128 /* The desktop size agreed on at connect time is capped by the protocol (4096x2048,
129 * MS-RDPBCGR 2.2.1.3.2), so a larger window starts out with a smaller session. Only the
130 * display control channel can raise it, and in fullscreen nothing else triggers a layout:
131 * the window is never resized by hand. */
132 if (!freerdp_settings_get_bool(settings, FreeRDP_Fullscreen))
133 {
134 if (!sdlDisp->setWindowResizeable())
135 return;
136
137 if (e->firstActivation)
138 return;
139 }
140
141 std::ignore = sdlDisp->addTimer();
142}
143
144void sdlDispContext::OnGraphicsReset(void* context, const GraphicsResetEventArgs* e)
145{
146 SdlContext* sdl = nullptr;
147 sdlDispContext* sdlDisp = nullptr;
148 rdpSettings* settings = nullptr;
149
150 WINPR_UNUSED(e);
151 if (!sdl_disp_check_context(context, &sdl, &sdlDisp, &settings))
152 return;
153
154 sdlDisp->_waitingResize = false;
155
156 if (!sdlDisp->_activated)
157 return;
158
159 if (freerdp_settings_get_bool(settings, FreeRDP_Fullscreen))
160 std::ignore = sdlDisp->addTimer();
161 else if (sdlDisp->setWindowResizeable())
162 std::ignore = sdlDisp->addTimer();
163}
164
165Uint32 sdlDispContext::OnTimer(void* param, [[maybe_unused]] SDL_TimerID timerID, Uint32 interval)
166{
167 auto ctx = static_cast<sdlDispContext*>(param);
168 if (!ctx)
169 return 0;
170
171 SdlContext* sdl = ctx->_sdl;
172 if (!sdl)
173 return 0;
174
175 sdlDispContext* sdlDisp = nullptr;
176 rdpSettings* settings = nullptr;
177
178 if (!sdl_disp_check_context(sdl->context(), &sdl, &sdlDisp, &settings))
179 return 0;
180
181 WLog_Print(sdl->getWLog(), WLOG_TRACE, "checking for display changes...");
182
183 if (!sdlDisp->sendResize())
184 WLog_Print(sdl->getWLog(), WLOG_TRACE, "failed to send the display layout");
185
186 if (sdlDisp->_timer_retries++ >= MAX_RETRIES)
187 {
188 WLog_Print(sdl->getWLog(), WLOG_TRACE, "deactivate timer, retries exceeded");
189 return 0;
190 }
191
192 WLog_Print(sdl->getWLog(), WLOG_TRACE, "fire timer one more time");
193 return interval;
194}
195
196bool sdlDispContext::sendLayout(const rdpMonitor* monitors, size_t nmonitors)
197{
198 WINPR_ASSERT(monitors);
199 WINPR_ASSERT(nmonitors > 0);
200
201 auto settings = _sdl->context()->settings;
202 WINPR_ASSERT(settings);
203
204 std::vector<DISPLAY_CONTROL_MONITOR_LAYOUT> layouts;
205 layouts.reserve(nmonitors);
206
207 for (size_t i = 0; i < nmonitors; i++)
208 {
209 auto monitor = &monitors[i];
211
212 layout.Flags = (monitor->is_primary ? DISPLAY_CONTROL_MONITOR_PRIMARY : 0);
213 layout.Left = monitor->x;
214 layout.Top = monitor->y;
215 layout.Width = WINPR_ASSERTING_INT_CAST(uint32_t, monitor->width);
216 layout.Height = WINPR_ASSERTING_INT_CAST(uint32_t, monitor->height);
217 layout.Orientation = ORIENTATION_LANDSCAPE;
218 layout.PhysicalWidth = monitor->attributes.physicalWidth;
219 layout.PhysicalHeight = monitor->attributes.physicalHeight;
220
221 switch (monitor->attributes.orientation)
222 {
223 case ORIENTATION_PORTRAIT:
224 layout.Orientation = ORIENTATION_PORTRAIT;
225 break;
226
227 case ORIENTATION_LANDSCAPE_FLIPPED:
228 layout.Orientation = ORIENTATION_LANDSCAPE_FLIPPED;
229 break;
230
231 case ORIENTATION_PORTRAIT_FLIPPED:
232 layout.Orientation = ORIENTATION_PORTRAIT_FLIPPED;
233 break;
234
235 case ORIENTATION_LANDSCAPE:
236 default:
237 /* MS-RDPEDISP - 2.2.2.2.1:
238 * Orientation (4 bytes): A 32-bit unsigned integer that specifies the
239 * orientation of the monitor in degrees. Valid values are 0, 90, 180
240 * or 270
241 *
242 * So we default to ORIENTATION_LANDSCAPE
243 */
244 layout.Orientation = ORIENTATION_LANDSCAPE;
245 break;
246 }
247
248 layout.DesktopScaleFactor = monitor->attributes.desktopScaleFactor;
249 layout.DeviceScaleFactor = monitor->attributes.deviceScaleFactor;
250
251 auto mask = freerdp_settings_get_uint64(settings, FreeRDP_MonitorOverrideFlags);
252 if ((mask & FREERDP_MONITOR_OVERRIDE_ORIENTATION) != 0)
253 layout.Orientation = freerdp_settings_get_uint16(settings, FreeRDP_DesktopOrientation);
254 if ((mask & FREERDP_MONITOR_OVERRIDE_DESKTOP_SCALE) != 0)
255 layout.DesktopScaleFactor =
256 freerdp_settings_get_uint32(settings, FreeRDP_DesktopScaleFactor);
257 if ((mask & FREERDP_MONITOR_OVERRIDE_DEVICE_SCALE) != 0)
258 layout.DeviceScaleFactor =
259 freerdp_settings_get_uint32(settings, FreeRDP_DeviceScaleFactor);
260 layouts.emplace_back(layout);
261 }
262
263 if (!settings_changed(layouts))
264 return true;
265
266 WINPR_ASSERT(_disp);
267 const size_t len = layouts.size();
268 WINPR_ASSERT(len <= UINT32_MAX);
269 const auto ret = IFCALLRESULT(CHANNEL_RC_OK, _disp->SendMonitorLayout, _disp,
270 static_cast<UINT32>(len), layouts.data());
271 if (ret != CHANNEL_RC_OK)
272 return false;
273 _last_sent_layout = layouts;
274 return true;
275}
276
277bool sdlDispContext::addTimer()
278{
279 if (SDL_WasInit(SDL_INIT_EVENTS) == 0)
280 return false;
281
282 SDL_RemoveTimer(_timer);
283 WLog_Print(_sdl->getWLog(), WLOG_TRACE, "adding new display check timer");
284
285 _timer_retries = 0;
286 if (!sendResize())
287 return false;
288 _timer = SDL_AddTimer(1000, sdlDispContext::OnTimer, this);
289 return true;
290}
291
292bool sdlDispContext::updateMonitor(SDL_WindowID id)
293{
294 if (!freerdp_settings_get_bool(_sdl->context()->settings, FreeRDP_DynamicResolutionUpdate))
295 return true;
296
297 if (!_sdl->updateWindow(id))
298 return false;
299
300 if (!_sdl->updateWindowList())
301 return false;
302
303 return addTimer();
304}
305
306bool sdlDispContext::updateMonitors(SDL_EventType type, SDL_DisplayID displayID)
307{
308 auto settings = _sdl->context()->settings;
309 if (!freerdp_settings_get_bool(settings, FreeRDP_UseMultimon))
310 return true;
311
312 if (!freerdp_settings_get_bool(settings, FreeRDP_DynamicResolutionUpdate))
313 return true;
314
315 switch (type)
316 {
317 case SDL_EVENT_DISPLAY_ADDED:
318 if (!_sdl->addDisplayWindow(displayID))
319 return false;
320 break;
321 case SDL_EVENT_DISPLAY_REMOVED:
322 if (!_sdl->removeDisplayWindow(displayID))
323 return false;
324 break;
325 default:
326 break;
327 }
328
329 if (!_sdl->updateWindowList())
330 return false;
331 return addTimer();
332}
333
334bool sdlDispContext::handleEvent(const SDL_DisplayEvent& ev)
335{
336 const auto cat = SDL_LOG_CATEGORY_APPLICATION;
337 switch (ev.type)
338 {
339 case SDL_EVENT_DISPLAY_ADDED:
340 SDL_LogDebug(cat, "A new display with id %u was connected", ev.displayID);
341 return updateMonitors(ev.type, ev.displayID);
342 case SDL_EVENT_DISPLAY_REMOVED:
343 SDL_LogDebug(cat, "The display with id %u was disconnected", ev.displayID);
344 return updateMonitors(ev.type, ev.displayID);
345 case SDL_EVENT_DISPLAY_ORIENTATION:
346 SDL_LogDebug(cat, "The orientation of display with id %u was changed", ev.displayID);
347 return updateMonitors(ev.type, ev.displayID);
348 case SDL_EVENT_DISPLAY_MOVED:
349 SDL_LogDebug(cat, "The display with id %u was moved", ev.displayID);
350 return updateMonitors(ev.type, ev.displayID);
351 case SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED:
352 SDL_LogDebug(cat, "The display with id %u changed scale", ev.displayID);
353 return updateMonitors(ev.type, ev.displayID);
354#if SDL_VERSION_ATLEAST(3, 3, 2)
355 case SDL_EVENT_DISPLAY_USABLE_BOUNDS_CHANGED:
356 SDL_LogDebug(cat, "The display with id %u changed usable bounds", ev.displayID);
357 return updateMonitors(ev.type, ev.displayID);
358#endif
359 case SDL_EVENT_DISPLAY_CURRENT_MODE_CHANGED:
360 SDL_LogDebug(cat, "The display with id %u changed mode", ev.displayID);
361 return updateMonitors(ev.type, ev.displayID);
362 case SDL_EVENT_DISPLAY_DESKTOP_MODE_CHANGED:
363 SDL_LogDebug(cat, "The display with id %u changed desktop mode", ev.displayID);
364 return updateMonitors(ev.type, ev.displayID);
365 default:
366 return true;
367 }
368}
369
370bool sdlDispContext::handleEvent(const SDL_WindowEvent& ev)
371{
372 auto window = _sdl->getWindowForId(ev.windowID);
373 if (!window)
374 return true;
375
376 auto bordered = freerdp_settings_get_bool(_sdl->context()->settings, FreeRDP_Decorations);
377 window->setBordered(bordered);
378
379 switch (ev.type)
380 {
381 case SDL_EVENT_WINDOW_HIDDEN:
382 case SDL_EVENT_WINDOW_MINIMIZED:
383 return _sdl->redraw(true);
384 case SDL_EVENT_WINDOW_ENTER_FULLSCREEN:
385 return updateMonitor(ev.windowID);
386 case SDL_EVENT_WINDOW_LEAVE_FULLSCREEN:
387 return updateMonitor(ev.windowID);
388
389 case SDL_EVENT_WINDOW_EXPOSED:
390 case SDL_EVENT_WINDOW_SHOWN:
391 case SDL_EVENT_WINDOW_MAXIMIZED:
392 case SDL_EVENT_WINDOW_RESTORED:
393 if (!_sdl->redraw())
394 return false;
395
396 /* fallthrough */
397 WINPR_FALLTHROUGH
398 case SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED:
399 case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
400 case SDL_EVENT_WINDOW_RESIZED:
401 return updateMonitor(ev.windowID);
402 case SDL_EVENT_WINDOW_MOUSE_LEAVE:
403 WINPR_ASSERT(_sdl);
404 return _sdl->getInputChannelContext().keyboard_grab(ev.windowID, false);
405 case SDL_EVENT_WINDOW_MOUSE_ENTER:
406 WINPR_ASSERT(_sdl);
407 if (!_sdl->getInputChannelContext().keyboard_grab(ev.windowID, true))
408 return false;
409 return _sdl->getInputChannelContext().keyboard_focus_in();
410 case SDL_EVENT_WINDOW_FOCUS_GAINED:
411 return _sdl->getInputChannelContext().keyboard_focus_in();
412
413 default:
414 return true;
415 }
416}
417
418UINT sdlDispContext::DisplayControlCaps(DispClientContext* disp, UINT32 maxNumMonitors,
419 UINT32 maxMonitorAreaFactorA, UINT32 maxMonitorAreaFactorB)
420{
421 /* we're called only if dynamic resolution update is activated */
422 WINPR_ASSERT(disp);
423
424 auto sdlDisp = reinterpret_cast<sdlDispContext*>(disp->custom);
425 return sdlDisp->DisplayControlCaps(maxNumMonitors, maxMonitorAreaFactorA,
426 maxMonitorAreaFactorB);
427}
428
429UINT sdlDispContext::DisplayControlCaps(UINT32 maxNumMonitors, UINT32 maxMonitorAreaFactorA,
430 UINT32 maxMonitorAreaFactorB)
431{
432 auto settings = _sdl->context()->settings;
433 WINPR_ASSERT(settings);
434
435 WLog_DBG(TAG,
436 "DisplayControlCapsPdu: MaxNumMonitors: %" PRIu32 " MaxMonitorAreaFactorA: %" PRIu32
437 " MaxMonitorAreaFactorB: %" PRIu32 "",
438 maxNumMonitors, maxMonitorAreaFactorA, maxMonitorAreaFactorB);
439 _activated = true;
440
441 /* A new display channel means a new server instance, e.g. after a server redirection (GNOME
442 * Remote Desktop hands the connection over from the login screen to the user session). It
443 * does not know the layout sent to the previous instance, and the window size did not change,
444 * so settings_changed() would suppress it: forget it and send the current layout again. */
445 _last_sent_layout.clear();
446
447 if (freerdp_settings_get_bool(settings, FreeRDP_Fullscreen))
448 {
449 /* Window events have long passed by the time the channel comes up, and a fullscreen
450 * window produces no further ones, so send the layout now. */
451 std::ignore = addTimer();
452 return CHANNEL_RC_OK;
453 }
454
455 WLog_DBG(TAG, "DisplayControlCapsPdu: setting the window as resizable");
456 if (!setWindowResizeable())
457 return CHANNEL_RC_NO_MEMORY;
458 std::ignore = addTimer();
459 return CHANNEL_RC_OK;
460}
461
462bool sdlDispContext::init(DispClientContext* disp)
463{
464 if (!disp)
465 return false;
466
467 auto settings = _sdl->context()->settings;
468
469 if (!settings)
470 return false;
471
472 _disp = disp;
473 disp->custom = this;
474
475 if (freerdp_settings_get_bool(settings, FreeRDP_DynamicResolutionUpdate))
476 {
477 disp->DisplayControlCaps = sdlDispContext::DisplayControlCaps;
478 }
479
480 return _sdl->setResizeable(true);
481}
482
483bool sdlDispContext::uninit(DispClientContext* disp)
484{
485 if (!disp)
486 return false;
487
488 _disp = nullptr;
489 return _sdl->setResizeable(false);
490}
491
492sdlDispContext::sdlDispContext(SdlContext* sdl) : _sdl(sdl)
493{
494 WINPR_ASSERT(_sdl);
495 WINPR_ASSERT(_sdl->context()->settings);
496 WINPR_ASSERT(_sdl->context()->pubSub);
497
498 auto pubSub = _sdl->context()->pubSub;
499
500 if (PubSub_SubscribeActivated(pubSub, sdlDispContext::OnActivated) < 0)
501 throw std::exception();
502 if (PubSub_SubscribeGraphicsReset(pubSub, sdlDispContext::OnGraphicsReset) < 0)
503 throw std::exception();
504 std::ignore = addTimer();
505}
506
507sdlDispContext::~sdlDispContext()
508{
509 wPubSub* pubSub = _sdl->context()->pubSub;
510 WINPR_ASSERT(pubSub);
511
512 PubSub_UnsubscribeActivated(pubSub, sdlDispContext::OnActivated);
513 PubSub_UnsubscribeGraphicsReset(pubSub, sdlDispContext::OnGraphicsReset);
514 SDL_RemoveTimer(_timer);
515}
WINPR_ATTR_NODISCARD FREERDP_API const void * freerdp_settings_get_pointer(const rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id)
Returns a immutable pointer settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT16 freerdp_settings_get_uint16(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt16 id)
Returns a UINT16 settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT64 freerdp_settings_get_uint64(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt64 id)
Returns a UINT64 settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.