21#include <winpr/sysinfo.h>
22#include <winpr/cast.h>
24#include <freerdp/timer.h>
28#define TAG CLIENT_TAG("wayland.disp")
30#define RESIZE_MIN_DELAY_NS 200000UL
32struct s_wlfDispContext
35 DispClientContext* disp;
37 int eventBase, errorBase;
38 int lastSentWidth, lastSentHeight;
40 int targetWidth, targetHeight;
44 UINT16 lastSentDesktopOrientation;
45 UINT32 lastSentDesktopScaleFactor;
46 UINT32 lastSentDeviceScaleFactor;
47 FreeRDP_TimerID timerID;
50static BOOL wlf_disp_sendResize(wlfDispContext* wlfDisp, BOOL fromTimer);
51static BOOL wlf_disp_check_context(
void* context,
wlfContext** ppwlc, wlfDispContext** ppwlfDisp,
52 rdpSettings** ppSettings);
53static UINT wlf_disp_sendLayout(DispClientContext* disp,
const rdpMonitor* monitors,
56static BOOL wlf_disp_settings_changed(wlfDispContext* wlfDisp)
58 rdpSettings* settings = NULL;
60 WINPR_ASSERT(wlfDisp);
61 WINPR_ASSERT(wlfDisp->wlc);
63 settings = wlfDisp->wlc->common.context.settings;
64 WINPR_ASSERT(settings);
66 if (wlfDisp->lastSentWidth != wlfDisp->targetWidth)
69 if (wlfDisp->lastSentHeight != wlfDisp->targetHeight)
72 if (wlfDisp->lastSentDesktopOrientation !=
76 if (wlfDisp->lastSentDesktopScaleFactor !=
80 if (wlfDisp->lastSentDeviceScaleFactor !=
84 if (wlfDisp->fullscreen != wlfDisp->wlc->fullscreen)
90static BOOL wlf_update_last_sent(wlfDispContext* wlfDisp)
92 rdpSettings* settings = NULL;
94 WINPR_ASSERT(wlfDisp);
95 WINPR_ASSERT(wlfDisp->wlc);
97 settings = wlfDisp->wlc->common.context.settings;
98 WINPR_ASSERT(settings);
100 wlfDisp->lastSentDate = winpr_GetTickCount64NS();
101 wlfDisp->lastSentWidth = wlfDisp->targetWidth;
102 wlfDisp->lastSentHeight = wlfDisp->targetHeight;
103 wlfDisp->lastSentDesktopOrientation =
105 wlfDisp->lastSentDesktopScaleFactor =
107 wlfDisp->lastSentDeviceScaleFactor =
109 wlfDisp->fullscreen = wlfDisp->wlc->fullscreen;
113static uint64_t wlf_disp_OnTimer(rdpContext* context, WINPR_ATTR_UNUSED
void* userdata,
114 WINPR_ATTR_UNUSED FreeRDP_TimerID timerID,
115 WINPR_ATTR_UNUSED uint64_t timestamp, uint64_t interval)
118 wlfDispContext* wlfDisp = NULL;
119 rdpSettings* settings = NULL;
121 if (!wlf_disp_check_context(context, &wlc, &wlfDisp, &settings))
127 wlf_disp_sendResize(wlfDisp, TRUE);
128 wlfDisp->timerID = 0;
132static BOOL update_timer(wlfDispContext* wlfDisp, uint64_t intervalNS)
134 WINPR_ASSERT(wlfDisp);
136 if (wlfDisp->timerID == 0)
138 rdpContext* context = &wlfDisp->wlc->common.context;
140 wlfDisp->timerID = freerdp_timer_add(context, intervalNS, wlf_disp_OnTimer, NULL,
true);
145BOOL wlf_disp_sendResize(wlfDispContext* wlfDisp, BOOL fromTimer)
149 rdpSettings* settings = NULL;
151 if (!wlfDisp || !wlfDisp->wlc)
155 settings = wlc->common.context.settings;
160 if (!wlfDisp->activated || !wlfDisp->disp)
161 return update_timer(wlfDisp, RESIZE_MIN_DELAY_NS);
163 const uint64_t now = winpr_GetTickCount64NS();
164 const uint64_t diff = now - wlfDisp->lastSentDate;
165 if (diff < RESIZE_MIN_DELAY_NS)
166 return update_timer(wlfDisp, RESIZE_MIN_DELAY_NS);
168 if (!fromTimer && (wlfDisp->timerID != 0))
171 if (!wlf_disp_settings_changed(wlfDisp))
179 if (wlf_disp_sendLayout(wlfDisp->disp, monitors, nmonitors) != CHANNEL_RC_OK)
184 wlfDisp->waitingResize = TRUE;
185 layout.Flags = DISPLAY_CONTROL_MONITOR_PRIMARY;
186 layout.Top = layout.Left = 0;
187 layout.Width = WINPR_ASSERTING_INT_CAST(uint32_t, wlfDisp->targetWidth);
188 layout.Height = WINPR_ASSERTING_INT_CAST(uint32_t, wlfDisp->targetHeight);
190 layout.DesktopScaleFactor =
193 layout.PhysicalWidth = WINPR_ASSERTING_INT_CAST(uint32_t, wlfDisp->targetWidth);
194 layout.PhysicalHeight = WINPR_ASSERTING_INT_CAST(uint32_t, wlfDisp->targetHeight);
196 if (IFCALLRESULT(CHANNEL_RC_OK, wlfDisp->disp->SendMonitorLayout, wlfDisp->disp, 1,
197 &layout) != CHANNEL_RC_OK)
200 return wlf_update_last_sent(wlfDisp);
203static BOOL wlf_disp_set_window_resizable(WINPR_ATTR_UNUSED wlfDispContext* wlfDisp)
205 WLog_ERR(
"TODO",
"TODO: implement");
209BOOL wlf_disp_check_context(
void* context,
wlfContext** ppwlc, wlfDispContext** ppwlfDisp,
210 rdpSettings** ppSettings)
222 if (!wlc->common.context.settings)
226 *ppwlfDisp = wlc->disp;
227 *ppSettings = wlc->common.context.settings;
231static void wlf_disp_OnActivated(
void* context,
const ActivatedEventArgs* e)
234 wlfDispContext* wlfDisp = NULL;
235 rdpSettings* settings = NULL;
237 if (!wlf_disp_check_context(context, &wlc, &wlfDisp, &settings))
240 wlfDisp->waitingResize = FALSE;
244 wlf_disp_set_window_resizable(wlfDisp);
246 if (e->firstActivation)
249 wlf_disp_sendResize(wlfDisp, FALSE);
253static void wlf_disp_OnGraphicsReset(
void* context,
const GraphicsResetEventArgs* e)
256 wlfDispContext* wlfDisp = NULL;
257 rdpSettings* settings = NULL;
260 if (!wlf_disp_check_context(context, &wlc, &wlfDisp, &settings))
263 wlfDisp->waitingResize = FALSE;
267 wlf_disp_set_window_resizable(wlfDisp);
268 wlf_disp_sendResize(wlfDisp, FALSE);
274 wlfDispContext* ret = NULL;
275 wPubSub* pubSub = NULL;
276 rdpSettings* settings = NULL;
278 if (!wlc || !wlc->common.context.settings || !wlc->common.context.pubSub)
281 settings = wlc->common.context.settings;
282 pubSub = wlc->common.context.pubSub;
283 ret = calloc(1,
sizeof(wlfDispContext));
289 ret->lastSentWidth = ret->targetWidth =
291 ret->lastSentHeight = ret->targetHeight =
293 PubSub_SubscribeActivated(pubSub, wlf_disp_OnActivated);
294 PubSub_SubscribeGraphicsReset(pubSub, wlf_disp_OnGraphicsReset);
298void wlf_disp_free(wlfDispContext* disp)
305 wPubSub* pubSub = disp->wlc->common.context.pubSub;
306 PubSub_UnsubscribeActivated(pubSub, wlf_disp_OnActivated);
307 PubSub_UnsubscribeGraphicsReset(pubSub, wlf_disp_OnGraphicsReset);
313UINT wlf_disp_sendLayout(DispClientContext* disp,
const rdpMonitor* monitors,
size_t nmonitors)
315 UINT ret = CHANNEL_RC_OK;
317 wlfDispContext* wlfDisp = NULL;
318 rdpSettings* settings = NULL;
321 WINPR_ASSERT(monitors);
322 WINPR_ASSERT(nmonitors > 0);
323 WINPR_ASSERT(nmonitors <= UINT32_MAX);
325 wlfDisp = (wlfDispContext*)disp->custom;
326 WINPR_ASSERT(wlfDisp);
327 WINPR_ASSERT(wlfDisp->wlc);
329 settings = wlfDisp->wlc->common.context.settings;
330 WINPR_ASSERT(settings);
335 return CHANNEL_RC_NO_MEMORY;
337 for (
size_t i = 0; i < nmonitors; i++)
342 layout->Flags = (monitor->is_primary ? DISPLAY_CONTROL_MONITOR_PRIMARY : 0);
343 layout->Left = monitor->x;
344 layout->Top = monitor->y;
345 layout->Width = WINPR_ASSERTING_INT_CAST(UINT32, monitor->width);
346 layout->Height = WINPR_ASSERTING_INT_CAST(UINT32, monitor->height);
347 layout->Orientation = ORIENTATION_LANDSCAPE;
348 layout->PhysicalWidth = monitor->attributes.physicalWidth;
349 layout->PhysicalHeight = monitor->attributes.physicalHeight;
351 switch (monitor->attributes.orientation)
354 layout->Orientation = ORIENTATION_PORTRAIT;
358 layout->Orientation = ORIENTATION_LANDSCAPE_FLIPPED;
362 layout->Orientation = ORIENTATION_PORTRAIT_FLIPPED;
374 layout->Orientation = ORIENTATION_LANDSCAPE;
378 layout->DesktopScaleFactor =
380 layout->DeviceScaleFactor =
384 ret = IFCALLRESULT(CHANNEL_RC_OK, disp->SendMonitorLayout, disp, (UINT32)nmonitors, layouts);
389BOOL wlf_disp_handle_configure(wlfDispContext* disp, int32_t width, int32_t height)
394 disp->targetWidth = width;
395 disp->targetHeight = height;
396 return wlf_disp_sendResize(disp, FALSE);
399static UINT wlf_DisplayControlCaps(DispClientContext* disp, UINT32 maxNumMonitors,
400 UINT32 maxMonitorAreaFactorA, UINT32 maxMonitorAreaFactorB)
403 wlfDispContext* wlfDisp = NULL;
404 rdpSettings* settings = NULL;
408 wlfDisp = (wlfDispContext*)disp->custom;
409 WINPR_ASSERT(wlfDisp);
410 WINPR_ASSERT(wlfDisp->wlc);
412 settings = wlfDisp->wlc->common.context.settings;
413 WINPR_ASSERT(settings);
416 "DisplayControlCapsPdu: MaxNumMonitors: %" PRIu32
" MaxMonitorAreaFactorA: %" PRIu32
417 " MaxMonitorAreaFactorB: %" PRIu32
"",
418 maxNumMonitors, maxMonitorAreaFactorA, maxMonitorAreaFactorB);
419 wlfDisp->activated = TRUE;
422 return CHANNEL_RC_OK;
424 WLog_DBG(TAG,
"DisplayControlCapsPdu: setting the window as resizable");
425 return wlf_disp_set_window_resizable(wlfDisp) ? CHANNEL_RC_OK : CHANNEL_RC_NO_MEMORY;
428BOOL wlf_disp_init(wlfDispContext* wlfDisp, DispClientContext* disp)
430 rdpSettings* settings = NULL;
432 if (!wlfDisp || !wlfDisp->wlc || !disp)
435 settings = wlfDisp->wlc->common.context.settings;
440 wlfDisp->disp = disp;
441 disp->custom = (
void*)wlfDisp;
445 disp->DisplayControlCaps = wlf_DisplayControlCaps;
451BOOL wlf_disp_uninit(wlfDispContext* wlfDisp, DispClientContext* disp)
453 if (!wlfDisp || !disp)
456 wlfDisp->disp = NULL;
462 uint32_t nmonitors = UwacDisplayGetNbOutputs(wlc->display);
464 for (uint32_t i = 0; i < nmonitors; i++)
466 const UwacOutput* monitor =
467 UwacDisplayGetOutput(wlc->display, WINPR_ASSERTING_INT_CAST(
int, i));
473 UwacOutputGetPosition(monitor, &pos);
474 UwacOutputGetResolution(monitor, &resolution);
476 printf(
" %s [%u] %dx%d\t+%d+%d\n", (i == 0) ?
"*" :
" ", i, resolution.width,
477 resolution.height, pos.x, pos.y);
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API UINT16 freerdp_settings_get_uint16(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt16 id)
Returns a UINT16 settings value.
FREERDP_API const void * freerdp_settings_get_pointer(const rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id)
Returns a immutable pointer settings value.