FreeRDP
Loading...
Searching...
No Matches
xf_window.c
1
23#include <freerdp/config.h>
24
25#include <stdarg.h>
26#include <stdint.h>
27#include <unistd.h>
28#include <sys/types.h>
29
30#include <X11/Xlib.h>
31#include <X11/Xutil.h>
32#include <X11/Xatom.h>
33
34#include <sys/mman.h>
35#include <sys/stat.h>
36#include <fcntl.h>
37
38#include <winpr/assert.h>
39#include <winpr/thread.h>
40#include <winpr/crt.h>
41#include <winpr/string.h>
42
43#include <freerdp/rail.h>
44#include <freerdp/log.h>
45
46#ifdef WITH_XEXT
47#include <X11/extensions/shape.h>
48#endif
49
50#ifdef WITH_XI
51#include <X11/extensions/XInput2.h>
52#endif
53
54#include "xf_gfx.h"
55#include "xf_rail.h"
56#include "xf_input.h"
57#include "xf_keyboard.h"
58#include "xf_utils.h"
59#include "xf_debug.h"
60
61#define TAG CLIENT_TAG("x11")
62
63#include <FreeRDP_Icon_256px.h>
64#define xf_icon_prop FreeRDP_Icon_256px_prop
65
66#include "xf_window.h"
67
68/* Extended Window Manager Hints: http://standards.freedesktop.org/wm-spec/wm-spec-1.3.html */
69
70/* bit definitions for MwmHints.flags */
71#define MWM_HINTS_FUNCTIONS (1L << 0)
72#define MWM_HINTS_DECORATIONS (1L << 1)
73// #define MWM_HINTS_INPUT_MODE (1L << 2)
74// #define MWM_HINTS_STATUS (1L << 3)
75
76/* bit definitions for MwmHints.functions */
77#define MWM_FUNC_ALL (1L << 0)
78// #define MWM_FUNC_RESIZE (1L << 1)
79// #define MWM_FUNC_MOVE (1L << 2)
80// #define MWM_FUNC_MINIMIZE (1L << 3)
81// #define MWM_FUNC_MAXIMIZE (1L << 4)
82// #define MWM_FUNC_CLOSE (1L << 5)
83
84/* bit definitions for MwmHints.decorations */
85#define MWM_DECOR_ALL (1L << 0)
86// #define MWM_DECOR_BORDER (1L << 1)
87// #define MWM_DECOR_RESIZEH (1L << 2)
88// #define MWM_DECOR_TITLE (1L << 3)
89// #define MWM_DECOR_MENU (1L << 4)
90// #define MWM_DECOR_MINIMIZE (1L << 5)
91// #define MWM_DECOR_MAXIMIZE (1L << 6)
92
93#define PROP_MOTIF_WM_HINTS_ELEMENTS 5
94
95#define ENTRY(x) \
96 case x: \
97 return #x
98
99typedef struct
100{
101 unsigned long flags;
102 unsigned long functions;
103 unsigned long decorations;
104 long inputMode;
105 unsigned long status;
106} PropMotifWmHints;
107
108static void xf_XSetTransientForHint(xfContext* xfc, xfAppWindow* window);
109
110static const char* window_style_to_string(UINT32 style)
111{
112 switch (style)
113 {
114 ENTRY(WS_NONE);
115 ENTRY(WS_BORDER);
116 ENTRY(WS_CAPTION);
117 ENTRY(WS_CHILD);
118 ENTRY(WS_CLIPCHILDREN);
119 ENTRY(WS_CLIPSIBLINGS);
120 ENTRY(WS_DISABLED);
121 ENTRY(WS_DLGFRAME);
122 ENTRY(WS_GROUP);
123 ENTRY(WS_HSCROLL);
124 ENTRY(WS_MAXIMIZE);
125 ENTRY(WS_MAXIMIZEBOX);
126 ENTRY(WS_MINIMIZE);
127 ENTRY(WS_OVERLAPPEDWINDOW);
128 ENTRY(WS_POPUP);
129 ENTRY(WS_POPUPWINDOW);
130 ENTRY(WS_SIZEBOX);
131 ENTRY(WS_SYSMENU);
132 ENTRY(WS_VISIBLE);
133 ENTRY(WS_VSCROLL);
134 default:
135 return nullptr;
136 }
137}
138
139const char* window_styles_to_string(UINT32 style, char* buffer, size_t length)
140{
141 (void)_snprintf(buffer, length, "style[0x%08" PRIx32 "] {", style);
142 const char* sep = "";
143 for (size_t x = 0; x < 32; x++)
144 {
145 const UINT32 val = 1u << x;
146 if ((style & val) != 0)
147 {
148 const char* str = window_style_to_string(val);
149 if (str)
150 {
151 winpr_str_append(str, buffer, length, sep);
152 sep = "|";
153 }
154 }
155 }
156 winpr_str_append("}", buffer, length, "");
157
158 return buffer;
159}
160
161static const char* window_style_ex_to_string(UINT32 styleEx)
162{
163 switch (styleEx)
164 {
165 ENTRY(WS_EX_ACCEPTFILES);
166 ENTRY(WS_EX_APPWINDOW);
167 ENTRY(WS_EX_CLIENTEDGE);
168 ENTRY(WS_EX_COMPOSITED);
169 ENTRY(WS_EX_CONTEXTHELP);
170 ENTRY(WS_EX_CONTROLPARENT);
171 ENTRY(WS_EX_DLGMODALFRAME);
172 ENTRY(WS_EX_LAYERED);
173 ENTRY(WS_EX_LAYOUTRTL);
174 ENTRY(WS_EX_LEFTSCROLLBAR);
175 ENTRY(WS_EX_MDICHILD);
176 ENTRY(WS_EX_NOACTIVATE);
177 ENTRY(WS_EX_NOINHERITLAYOUT);
178 ENTRY(WS_EX_NOPARENTNOTIFY);
179 ENTRY(WS_EX_OVERLAPPEDWINDOW);
180 ENTRY(WS_EX_PALETTEWINDOW);
181 ENTRY(WS_EX_RIGHT);
182 ENTRY(WS_EX_RIGHTSCROLLBAR);
183 ENTRY(WS_EX_RTLREADING);
184 ENTRY(WS_EX_STATICEDGE);
185 ENTRY(WS_EX_TOOLWINDOW);
186 ENTRY(WS_EX_TOPMOST);
187 ENTRY(WS_EX_TRANSPARENT);
188 ENTRY(WS_EX_WINDOWEDGE);
189 default:
190 return nullptr;
191 }
192}
193
194const char* window_styles_ex_to_string(UINT32 styleEx, char* buffer, size_t length)
195{
196 (void)_snprintf(buffer, length, "styleEx[0x%08" PRIx32 "] {", styleEx);
197 const char* sep = "";
198 for (size_t x = 0; x < 32; x++)
199 {
200 const UINT32 val = (UINT32)(1UL << x);
201 if ((styleEx & val) != 0)
202 {
203 const char* str = window_style_ex_to_string(val);
204 if (str)
205 {
206 winpr_str_append(str, buffer, length, sep);
207 sep = "|";
208 }
209 }
210 }
211 winpr_str_append("}", buffer, length, "");
212
213 return buffer;
214}
215
216static void xf_SetWindowTitleText(xfContext* xfc, Window window, const char* name)
217{
218 WINPR_ASSERT(xfc);
219 WINPR_ASSERT(name);
220
221 const size_t i = strnlen(name, MAX_PATH);
222 XStoreName(xfc->display, window, name);
223 Atom wm_Name = xfc->NET_WM_NAME;
224 Atom utf8Str = xfc->UTF8_STRING;
225 LogDynAndXChangeProperty(xfc->log, xfc->display, window, wm_Name, utf8Str, 8, PropModeReplace,
226 (const unsigned char*)name, (int)i);
227}
228
232void xf_SendClientEvent(xfContext* xfc, Window window, Atom atom, unsigned int numArgs, ...)
233{
234 XEvent xevent = WINPR_C_ARRAY_INIT;
235 va_list argp = WINPR_C_ARRAY_INIT;
236 va_start(argp, numArgs);
237
238 xevent.xclient.type = ClientMessage;
239 xevent.xclient.serial = 0;
240 xevent.xclient.send_event = False;
241 xevent.xclient.display = xfc->display;
242 xevent.xclient.window = window;
243 xevent.xclient.message_type = atom;
244 xevent.xclient.format = 32;
245
246 for (size_t i = 0; i < numArgs; i++)
247 {
248 xevent.xclient.data.l[i] = va_arg(argp, int);
249 }
250
251 WLog_Print(xfc->log, WLOG_TRACE, "Send ClientMessage Event: wnd=0x%04lX",
252 (unsigned long)xevent.xclient.window);
253 LogDynAndXSendEvent(xfc->log, xfc->display, RootWindowOfScreen(xfc->screen), False,
254 SubstructureRedirectMask | SubstructureNotifyMask, &xevent);
255 LogDynAndXSync(xfc->log, xfc->display, False);
256 va_end(argp);
257}
258
259void xf_SetWindowMinimized(xfContext* xfc, xfWindow* window)
260{
261 XIconifyWindow(xfc->display, window->handle, xfc->screen_number);
262}
263
264void xf_SetWindowFullscreen(xfContext* xfc, xfWindow* window, BOOL fullscreen)
265{
266 const rdpSettings* settings = nullptr;
267 int startX = 0;
268 int startY = 0;
269 UINT32 width = WINPR_ASSERTING_INT_CAST(uint32_t, window->width);
270 UINT32 height = WINPR_ASSERTING_INT_CAST(uint32_t, window->height);
271
272 WINPR_ASSERT(xfc);
273
274 settings = xfc->common.context.settings;
275 WINPR_ASSERT(settings);
276
277 /* xfc->decorations is set by caller depending on settings and whether it is fullscreen or not
278 */
279 window->decorations = xfc->decorations;
280 /* show/hide decorations (e.g. title bar) as guided by xfc->decorations */
281 xf_SetWindowDecorations(xfc, window->handle, window->decorations);
282 WLog_Print(xfc->log, WLOG_TRACE, "X window decoration set to %d", (int)window->decorations);
283 xf_floatbar_toggle_fullscreen(xfc->window->floatbar, fullscreen);
284
285 if (fullscreen)
286 {
287 xfc->savedWidth = xfc->window->width;
288 xfc->savedHeight = xfc->window->height;
289 xfc->savedPosX = xfc->window->left;
290 xfc->savedPosY = xfc->window->top;
291
292 startX = (freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosX) != UINT32_MAX)
293 ? WINPR_ASSERTING_INT_CAST(
294 int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosX))
295 : 0;
296 startY = (freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosY) != UINT32_MAX)
297 ? WINPR_ASSERTING_INT_CAST(
298 int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosY))
299 : 0;
300 }
301 else
302 {
303 width = WINPR_ASSERTING_INT_CAST(uint32_t, xfc->savedWidth);
304 height = WINPR_ASSERTING_INT_CAST(uint32_t, xfc->savedHeight);
305 startX = xfc->savedPosX;
306 startY = xfc->savedPosY;
307 }
308
309 /* Determine the x,y starting location for the fullscreen window */
310 if (fullscreen)
311 {
312 const rdpMonitor* firstMonitor =
313 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, 0);
314 /* Initialize startX and startY with reasonable values */
315 startX = firstMonitor->x;
316 startY = firstMonitor->y;
317
318 /* Search all monitors to find the lowest startX and startY values */
319 for (size_t i = 0; i < freerdp_settings_get_uint32(settings, FreeRDP_MonitorCount); i++)
320 {
321 const rdpMonitor* monitor =
322 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, i);
323 startX = MIN(startX, monitor->x);
324 startY = MIN(startY, monitor->y);
325 }
326
327 /* Lastly apply any monitor shift(translation from remote to local coordinate system)
328 * to startX and startY values
329 */
330 startX += freerdp_settings_get_int32(settings, FreeRDP_MonitorLocalShiftX);
331 startY += freerdp_settings_get_int32(settings, FreeRDP_MonitorLocalShiftY);
332 }
333
334 /*
335 It is safe to proceed with simply toggling _NET_WM_STATE_FULLSCREEN window state on the
336 following conditions:
337 - The window manager supports multiple monitor full screen
338 - The user requested to use a single monitor to render the remote desktop
339 */
340 if (xfc->NET_WM_FULLSCREEN_MONITORS != None ||
341 freerdp_settings_get_uint32(settings, FreeRDP_MonitorCount) == 1)
342 {
343 xf_ResizeDesktopWindow(xfc, window, WINPR_ASSERTING_INT_CAST(int, width),
344 WINPR_ASSERTING_INT_CAST(int, height));
345
346 if (fullscreen)
347 {
348 /* enter full screen: move the window before adding NET_WM_STATE_FULLSCREEN */
349 LogDynAndXMoveWindow(xfc->log, xfc->display, window->handle, startX, startY);
350 }
351
352 /* Set the fullscreen state */
353 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4,
354 fullscreen ? NET_WM_STATE_ADD : NET_WM_STATE_REMOVE,
355 xfc->NET_WM_STATE_FULLSCREEN, 0, 0);
356
357 if (!fullscreen)
358 {
359 /* leave full screen: move the window after removing NET_WM_STATE_FULLSCREEN
360 * Resize the window again, the previous call to xf_SendClientEvent might have
361 * changed the window size (borders, ...)
362 */
363 xf_ResizeDesktopWindow(xfc, window, WINPR_ASSERTING_INT_CAST(int, width),
364 WINPR_ASSERTING_INT_CAST(int, height));
365 LogDynAndXMoveWindow(xfc->log, xfc->display, window->handle, startX, startY);
366 }
367
368 /* Set monitor bounds */
369 if (freerdp_settings_get_uint32(settings, FreeRDP_MonitorCount) > 1)
370 {
371 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_FULLSCREEN_MONITORS, 5,
372 xfc->fullscreenMonitors.top, xfc->fullscreenMonitors.bottom,
373 xfc->fullscreenMonitors.left, xfc->fullscreenMonitors.right, 1);
374 }
375 }
376 else
377 {
378 if (fullscreen)
379 {
380 xf_SetWindowDecorations(xfc, window->handle, FALSE);
381
382 if (xfc->fullscreenMonitors.top)
383 {
384 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_ADD,
385 xfc->fullscreenMonitors.top, 0, 0);
386 }
387 else
388 {
389 XSetWindowAttributes xswa = WINPR_C_ARRAY_INIT;
390 xswa.override_redirect = True;
391 LogDynAndXChangeWindowAttributes(xfc->log, xfc->display, window->handle,
392 CWOverrideRedirect, &xswa);
393 LogDynAndXRaiseWindow(xfc->log, xfc->display, window->handle);
394 xswa.override_redirect = False;
395 LogDynAndXChangeWindowAttributes(xfc->log, xfc->display, window->handle,
396 CWOverrideRedirect, &xswa);
397 }
398
399 /* if window is in maximized state, save and remove */
400 if (xfc->NET_WM_STATE_MAXIMIZED_VERT != None)
401 {
402 BYTE state = 0;
403 unsigned long nitems = 0;
404 unsigned long bytes = 0;
405 BYTE* prop = nullptr;
406
407 if (xf_GetWindowProperty(xfc, window->handle, xfc->NET_WM_STATE, 255, &nitems,
408 &bytes, &prop))
409 {
410 const Atom* aprop = WINPR_PACKED_ALIGN_CAST(const Atom*, prop);
411 state = 0;
412
413 for (size_t x = 0; x < nitems; x++)
414 {
415 if (aprop[x] == xfc->NET_WM_STATE_MAXIMIZED_VERT)
416 state |= 0x01;
417
418 if (aprop[x] == xfc->NET_WM_STATE_MAXIMIZED_HORZ)
419 state |= 0x02;
420 }
421
422 if (state)
423 {
424 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4,
425 NET_WM_STATE_REMOVE, xfc->NET_WM_STATE_MAXIMIZED_VERT, 0,
426 0);
427 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4,
428 NET_WM_STATE_REMOVE, xfc->NET_WM_STATE_MAXIMIZED_HORZ, 0,
429 0);
430 xfc->savedMaximizedState = state;
431 }
432
433 XFree(prop);
434 }
435 }
436
437 width = xfc->vscreen.area.right - xfc->vscreen.area.left + 1;
438 height = xfc->vscreen.area.bottom - xfc->vscreen.area.top + 1;
439 WLog_Print(xfc->log, WLOG_TRACE,
440 "X window move and resize %dx%d@%" PRIu32 "x%" PRIu32 "", startX, startY,
441 width, height);
442 xf_ResizeDesktopWindow(xfc, window, WINPR_ASSERTING_INT_CAST(int, width),
443 WINPR_ASSERTING_INT_CAST(int, height));
444 LogDynAndXMoveWindow(xfc->log, xfc->display, window->handle, startX, startY);
445 }
446 else
447 {
448 xf_SetWindowDecorations(xfc, window->handle, window->decorations);
449 xf_ResizeDesktopWindow(xfc, window, WINPR_ASSERTING_INT_CAST(int, width),
450 WINPR_ASSERTING_INT_CAST(int, height));
451 LogDynAndXMoveWindow(xfc->log, xfc->display, window->handle, startX, startY);
452
453 if (xfc->fullscreenMonitors.top)
454 {
455 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_REMOVE,
456 xfc->fullscreenMonitors.top, 0, 0);
457 }
458
459 /* restore maximized state, if the window was maximized before setting fullscreen */
460 if (xfc->savedMaximizedState & 0x01)
461 {
462 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_ADD,
463 xfc->NET_WM_STATE_MAXIMIZED_VERT, 0, 0);
464 }
465
466 if (xfc->savedMaximizedState & 0x02)
467 {
468 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_ADD,
469 xfc->NET_WM_STATE_MAXIMIZED_HORZ, 0, 0);
470 }
471
472 xfc->savedMaximizedState = 0;
473 }
474 }
475}
476
477/* http://tronche.com/gui/x/xlib/window-information/XGetWindowProperty.html */
478
479BOOL xf_GetWindowProperty(xfContext* xfc, Window window, Atom property, int length,
480 unsigned long* nitems, unsigned long* bytes, BYTE** prop)
481{
482 int status = 0;
483 Atom actual_type = None;
484 int actual_format = 0;
485
486 WINPR_ASSERT(prop);
487
488 if (property == None)
489 return FALSE;
490
491 status = LogDynAndXGetWindowProperty(xfc->log, xfc->display, window, property, 0, length, False,
492 AnyPropertyType, &actual_type, &actual_format, nitems,
493 bytes, prop);
494
495 if (status != Success)
496 return FALSE;
497
498 if (actual_type == None)
499 {
500 WLog_DBG(TAG, "Property %lu does not exist", (unsigned long)property);
501 if (*prop)
502 XFree(*prop);
503 *prop = nullptr;
504 return FALSE;
505 }
506
507 return TRUE;
508}
509
510static BOOL xf_GetNumberOfDesktops(xfContext* xfc, Window root, unsigned* pval)
511{
512 unsigned long nitems = 0;
513 unsigned long bytes = 0;
514 BYTE* bprop = nullptr;
515
516 WINPR_ASSERT(xfc);
517 WINPR_ASSERT(pval);
518
519 const BOOL rc =
520 xf_GetWindowProperty(xfc, root, xfc->NET_NUMBER_OF_DESKTOPS, 1, &nitems, &bytes, &bprop);
521
522 long* prop = WINPR_PACKED_ALIGN_CAST(long*, bprop);
523 *pval = 0;
524
525 BOOL res = FALSE;
526 if (rc)
527 {
528 if ((*prop >= 0) && (*prop <= UINT32_MAX))
529 {
530 *pval = (UINT32)*prop;
531 res = TRUE;
532 }
533 }
534 if (prop)
535 XFree(prop);
536 return res;
537}
538
539static BOOL xf_GetCurrentDesktop(xfContext* xfc, Window root)
540{
541 unsigned long nitems = 0;
542 unsigned long bytes = 0;
543 BYTE* bprop = nullptr;
544 unsigned max = 0;
545
546 if (!xf_GetNumberOfDesktops(xfc, root, &max))
547 return FALSE;
548 if (max < 1)
549 return FALSE;
550
551 const BOOL rc =
552 xf_GetWindowProperty(xfc, root, xfc->NET_CURRENT_DESKTOP, 1, &nitems, &bytes, &bprop);
553
554 long* prop = WINPR_PACKED_ALIGN_CAST(long*, bprop);
555 xfc->current_desktop = 0;
556 if (rc)
557 xfc->current_desktop = (int)MIN(max - 1, *prop);
558 if (prop)
559 XFree(prop);
560 return rc;
561}
562
563static BOOL xf_GetWorkArea_NET_WORKAREA(xfContext* xfc, Window root)
564{
565 BOOL rc = FALSE;
566 unsigned long nitems = 0;
567 unsigned long bytes = 0;
568 BYTE* bprop = nullptr;
569
570 const BOOL status =
571 xf_GetWindowProperty(xfc, root, xfc->NET_WORKAREA, INT_MAX, &nitems, &bytes, &bprop);
572 long* prop = WINPR_PACKED_ALIGN_CAST(long*, bprop);
573
574 if (!status)
575 goto fail;
576
577 if ((xfc->current_desktop * 4 + 3) >= (INT64)nitems)
578 goto fail;
579
580 xfc->workArea.x = (INT32)MIN(INT32_MAX, prop[xfc->current_desktop * 4 + 0]);
581 xfc->workArea.y = (INT32)MIN(INT32_MAX, prop[xfc->current_desktop * 4 + 1]);
582 xfc->workArea.width = (UINT32)MIN(UINT32_MAX, prop[xfc->current_desktop * 4 + 2]);
583 xfc->workArea.height = (UINT32)MIN(UINT32_MAX, prop[xfc->current_desktop * 4 + 3]);
584
585 rc = TRUE;
586fail:
587 XFree(prop);
588 return rc;
589}
590
591BOOL xf_GetWorkArea(xfContext* xfc)
592{
593 WINPR_ASSERT(xfc);
594
595 Window root = DefaultRootWindow(xfc->display);
596 (void)xf_GetCurrentDesktop(xfc, root);
597 return xf_GetWorkArea_NET_WORKAREA(xfc, root);
598}
599
600void xf_SetWindowDecorations(xfContext* xfc, Window window, BOOL show)
601{
602 PropMotifWmHints hints = { .decorations = (show) ? MWM_DECOR_ALL : 0,
603 .functions = MWM_FUNC_ALL,
604 .flags = MWM_HINTS_DECORATIONS | MWM_HINTS_FUNCTIONS,
605 .inputMode = 0,
606 .status = 0 };
607 WINPR_ASSERT(xfc);
608 LogDynAndXChangeProperty(xfc->log, xfc->display, window, xfc->MOTIF_WM_HINTS,
609 xfc->MOTIF_WM_HINTS, 32, PropModeReplace, (BYTE*)&hints,
610 PROP_MOTIF_WM_HINTS_ELEMENTS);
611}
612
613void xf_SetWindowUnlisted(xfContext* xfc, Window window)
614{
615 WINPR_ASSERT(xfc);
616 Atom window_state[] = { xfc->NET_WM_STATE_SKIP_PAGER, xfc->NET_WM_STATE_SKIP_TASKBAR };
617 LogDynAndXChangeProperty(xfc->log, xfc->display, window, xfc->NET_WM_STATE, XA_ATOM, 32,
618 PropModeReplace, (BYTE*)window_state, 2);
619}
620
621static void xf_SetWindowPID(xfContext* xfc, Window window, pid_t pid)
622{
623 Atom am_wm_pid = 0;
624
625 WINPR_ASSERT(xfc);
626 if (!pid)
627 pid = getpid();
628
629 am_wm_pid = xfc->NET_WM_PID;
630 LogDynAndXChangeProperty(xfc->log, xfc->display, window, am_wm_pid, XA_CARDINAL, 32,
631 PropModeReplace, (BYTE*)&pid, 1);
632}
633
634static const char* get_shm_id(void)
635{
636 static char shm_id[64];
637 (void)sprintf_s(shm_id, sizeof(shm_id), "/com.freerdp.xfreerdp.tsmf_%016X",
638 GetCurrentProcessId());
639 return shm_id;
640}
641
642Window xf_CreateDummyWindow(xfContext* xfc)
643{
644 return LogDynAndXCreateWindow(
645 xfc->log, xfc->display, RootWindowOfScreen(xfc->screen),
646 WINPR_ASSERTING_INT_CAST(int, xfc->workArea.x),
647 WINPR_ASSERTING_INT_CAST(int, xfc->workArea.y), 1, 1, 0, xfc->depth, InputOutput,
648 xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->attribs_mask), &xfc->attribs);
649}
650
651void xf_DestroyDummyWindow(xfContext* xfc, Window window)
652{
653 if (window)
654 LogDynAndXDestroyWindow(xfc->log, xfc->display, window);
655}
656
657xfWindow* xf_CreateDesktopWindow(xfContext* xfc, char* name, int width, int height)
658{
659 XEvent xevent = WINPR_C_ARRAY_INIT;
660 int input_mask = 0;
661 XClassHint* classHints = nullptr;
662 xfWindow* window = (xfWindow*)calloc(1, sizeof(xfWindow));
663
664 if (!window)
665 return nullptr;
666
667 rdpSettings* settings = xfc->common.context.settings;
668 WINPR_ASSERT(settings);
669
670 Window parentWindow = (Window)freerdp_settings_get_uint64(settings, FreeRDP_ParentWindowId);
671 window->width = width;
672 window->height = height;
673 window->decorations = xfc->decorations;
674 window->is_mapped = FALSE;
675 window->is_transient = FALSE;
676
677 WINPR_ASSERT(xfc->depth != 0);
678 window->handle = LogDynAndXCreateWindow(
679 xfc->log, xfc->display, RootWindowOfScreen(xfc->screen),
680 WINPR_ASSERTING_INT_CAST(int, xfc->workArea.x),
681 WINPR_ASSERTING_INT_CAST(int, xfc->workArea.y), xfc->workArea.width, xfc->workArea.height,
682 0, xfc->depth, InputOutput, xfc->visual,
683 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->attribs_mask), &xfc->attribs);
684 window->shmid = shm_open(get_shm_id(), (O_CREAT | O_RDWR), (S_IREAD | S_IWRITE));
685
686 if (window->shmid < 0)
687 {
688 WLog_Print(xfc->log, WLOG_TRACE,
689 "xf_CreateDesktopWindow: failed to get access to shared memory - shmget()\n");
690 }
691 else
692 {
693 int rc = ftruncate(window->shmid, sizeof(window->handle));
694 if (rc != 0)
695 {
696 char ebuffer[256] = WINPR_C_ARRAY_INIT;
697 WLog_Print(xfc->log, WLOG_TRACE, "ftruncate failed with %s [%d]",
698 winpr_strerror(rc, ebuffer, sizeof(ebuffer)), rc);
699 }
700 else
701 {
702 void* mem = mmap(nullptr, sizeof(window->handle), PROT_READ | PROT_WRITE, MAP_SHARED,
703 window->shmid, 0);
704
705 if (mem == MAP_FAILED)
706 {
707 WLog_Print(
708 xfc->log, WLOG_TRACE,
709 "xf_CreateDesktopWindow: failed to assign pointer to the memory address - "
710 "shmat()\n");
711 }
712 else
713 {
714 window->xfwin = mem;
715 *window->xfwin = window->handle;
716 }
717 }
718 }
719
720 classHints = XAllocClassHint();
721
722 if (classHints)
723 {
724 classHints->res_name = "xfreerdp";
725
726 char* res_class = nullptr;
727 const char* WmClass = freerdp_settings_get_string(settings, FreeRDP_WmClass);
728 if (WmClass)
729 res_class = _strdup(WmClass);
730 else
731 res_class = _strdup("xfreerdp");
732
733 classHints->res_class = res_class;
734 XSetClassHint(xfc->display, window->handle, classHints);
735 XFree(classHints);
736 free(res_class);
737 }
738
739 xf_ResizeDesktopWindow(xfc, window, width, height);
740 xf_SetWindowDecorations(xfc, window->handle, window->decorations);
741 xf_SetWindowPID(xfc, window->handle, 0);
742 input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
743 VisibilityChangeMask | FocusChangeMask | StructureNotifyMask | PointerMotionMask |
744 ExposureMask | PropertyChangeMask;
745
746 if (xfc->grab_keyboard)
747 input_mask |= EnterWindowMask | LeaveWindowMask;
748
749 LogDynAndXChangeProperty(xfc->log, xfc->display, window->handle, xfc->NET_WM_ICON, XA_CARDINAL,
750 32, PropModeReplace, (BYTE*)xf_icon_prop, ARRAYSIZE(xf_icon_prop));
751
752 if (parentWindow)
753 LogDynAndXReparentWindow(xfc->log, xfc->display, window->handle, parentWindow, 0, 0);
754
755 XSelectInput(xfc->display, window->handle, input_mask);
756 LogDynAndXClearWindow(xfc->log, xfc->display, window->handle);
757 xf_SetWindowTitleText(xfc, window->handle, name);
758 LogDynAndXMapWindow(xfc->log, xfc->display, window->handle);
759 if (xf_input_init(xfc, window->handle) < 0)
760 {
761 xf_DestroyDesktopWindow(xfc, window);
762 return nullptr;
763 }
764
765 /*
766 * NOTE: This must be done here to handle reparenting the window,
767 * so that we don't miss the event and hang waiting for the next one
768 */
769 do
770 {
771 XMaskEvent(xfc->display, VisibilityChangeMask, &xevent);
772 } while (xevent.type != VisibilityNotify);
773
774 /*
775 * The XCreateWindow call will start the window in the upper-left corner of our current
776 * monitor instead of the upper-left monitor for remote app mode (which uses all monitors).
777 * This extra call after the window is mapped will position the login window correctly
778 */
779 if (freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode))
780 {
781 LogDynAndXMoveWindow(xfc->log, xfc->display, window->handle, 0, 0);
782 }
783 else if ((freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosX) != UINT32_MAX) &&
784 (freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosY) != UINT32_MAX))
785 {
786 LogDynAndXMoveWindow(xfc->log, xfc->display, window->handle,
787 WINPR_ASSERTING_INT_CAST(
788 int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosX)),
789 WINPR_ASSERTING_INT_CAST(
790 int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosY)));
791 }
792
793 window->floatbar = xf_floatbar_new(xfc, window->handle, name,
794 freerdp_settings_get_uint32(settings, FreeRDP_Floatbar));
795
796 if (xfc->XWAYLAND_MAY_GRAB_KEYBOARD)
797 xf_SendClientEvent(xfc, window->handle, xfc->XWAYLAND_MAY_GRAB_KEYBOARD, 1, 1);
798
799 return window;
800}
801
802void xf_ResizeDesktopWindow(xfContext* xfc, xfWindow* window, int width, int height)
803{
804 XSizeHints* size_hints = nullptr;
805 rdpSettings* settings = nullptr;
806
807 if (!xfc || !window)
808 return;
809
810 settings = xfc->common.context.settings;
811 WINPR_ASSERT(settings);
812
813 if (!(size_hints = XAllocSizeHints()))
814 return;
815
816 size_hints->flags = PMinSize | PMaxSize | PWinGravity;
817 size_hints->win_gravity = NorthWestGravity;
818 size_hints->min_width = size_hints->min_height = 1;
819 size_hints->max_width = size_hints->max_height = 16384;
820 LogDynAndXResizeWindow(xfc->log, xfc->display, window->handle,
821 WINPR_ASSERTING_INT_CAST(uint32_t, width),
822 WINPR_ASSERTING_INT_CAST(uint32_t, height));
823#ifdef WITH_XRENDER
824
825 if (!freerdp_settings_get_bool(settings, FreeRDP_SmartSizing) &&
826 !freerdp_settings_get_bool(settings, FreeRDP_DynamicResolutionUpdate))
827#endif
828 {
829 if (!xfc->fullscreen)
830 {
831 /* min == max is an hint for the WM to indicate that the window should
832 * not be resizable */
833 size_hints->min_width = size_hints->max_width = width;
834 size_hints->min_height = size_hints->max_height = height;
835 }
836 }
837
838 XSetWMNormalHints(xfc->display, window->handle, size_hints);
839 XFree(size_hints);
840}
841
842void xf_DestroyDesktopWindow(xfContext* xfc, xfWindow* window)
843{
844 WINPR_ASSERT(xfc);
845
846 if (!window)
847 return;
848
849 if (xfc->window == window)
850 xfc->window = nullptr;
851
852 /* Reset XI2 event flags so standard X11 events are processed for RAIL
853 * windows. The per-window XI2 registrations were on the now-destroyed
854 * desktop window and will no longer fire. */
855 xfc->xi_event = false;
856 xfc->xi_rawevent = false;
857 xf_ungrab(xfc);
858
859 xf_floatbar_free(window->floatbar);
860
861 if (window->gc)
862 LogDynAndXFreeGC(xfc->log, xfc->display, window->gc);
863
864 if (window->handle)
865 {
866 LogDynAndXUnmapWindow(xfc->log, xfc->display, window->handle);
867 LogDynAndXDestroyWindow(xfc->log, xfc->display, window->handle);
868 }
869
870 if (window->xfwin)
871 munmap(window->xfwin, sizeof(*window->xfwin));
872
873 if (window->shmid >= 0)
874 close(window->shmid);
875
876 shm_unlink(get_shm_id());
877 window->xfwin = (Window*)-1;
878 window->shmid = -1;
879 free(window);
880}
881
882void xf_SetWindowStyle(xfContext* xfc, xfAppWindow* appWindow, UINT32 style, UINT32 ex_style)
883{
884 Atom window_type = 0;
885 BOOL redirect = FALSE;
886
887 window_type = xfc->NET_WM_WINDOW_TYPE_NORMAL;
888
889 if ((ex_style & WS_EX_NOACTIVATE) || (ex_style & WS_EX_TOOLWINDOW))
890 {
891 redirect = TRUE;
892 appWindow->is_transient = TRUE;
893 xf_SetWindowUnlisted(xfc, appWindow->handle);
894 window_type = xfc->NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
895 }
896 /*
897 * TOPMOST window that is not a tool window is treated like a regular window (i.e. task
898 * manager). Want to do this here, since the window may have type WS_POPUP
899 */
900 else if (ex_style & WS_EX_TOPMOST)
901 {
902 window_type = xfc->NET_WM_WINDOW_TYPE_NORMAL;
903 }
904
905 if (style & WS_POPUP)
906 {
907 /* this includes dialogs, popups, etc, that need to be full-fledged windows */
908 if (!redirect)
909 window_type = xfc->NET_WM_WINDOW_TYPE_DIALOG;
910
911 if (!((ex_style & WS_EX_DLGMODALFRAME) || (ex_style & WS_EX_LAYERED) ||
912 (style & WS_SYSMENU)))
913 {
914 appWindow->is_transient = TRUE;
915 redirect = TRUE;
916
917 xf_SetWindowUnlisted(xfc, appWindow->handle);
918 }
919 }
920
921 if (!(style == 0 && ex_style == 0))
922 {
923 xf_SetWindowActions(xfc, appWindow);
924 }
925
926 {
927 /*
928 * Tooltips and menu items should be unmanaged windows
929 * (called "override redirect" in X windows parlance)
930 * If they are managed, there are issues with window focus that
931 * cause the windows to behave improperly. For example, a mouse
932 * press will dismiss a drop-down menu because the RDP server
933 * sees that as a focus out event from the window owning the
934 * dropdown.
935 */
936 XSetWindowAttributes attrs = WINPR_C_ARRAY_INIT;
937 attrs.override_redirect = redirect ? True : False;
938 appWindow->override_redirect = redirect;
939 LogDynAndXChangeWindowAttributes(xfc->log, xfc->display, appWindow->handle,
940 CWOverrideRedirect, &attrs);
941 }
942
943 LogDynAndXChangeProperty(xfc->log, xfc->display, appWindow->handle, xfc->NET_WM_WINDOW_TYPE,
944 XA_ATOM, 32, PropModeReplace, (BYTE*)&window_type, 1);
945
946 const BOOL above = (ex_style & WS_EX_TOPMOST) != 0;
947 const BOOL transient = (style & WS_CHILD) == 0;
948
949 if (transient)
950 xf_XSetTransientForHint(
951 xfc, appWindow); // xf_XSetTransientForHint only sets the hint if there is a parent
952
953 xf_SendClientEvent(xfc, appWindow->handle, xfc->NET_WM_STATE, 4,
954 above ? NET_WM_STATE_ADD : NET_WM_STATE_REMOVE, xfc->NET_WM_STATE_ABOVE, 0,
955 0);
956}
957
958void xf_SetWindowActions(xfContext* xfc, xfAppWindow* appWindow)
959{
960 Atom allowed_actions[] = {
961 xfc->NET_WM_ACTION_CLOSE, xfc->NET_WM_ACTION_MINIMIZE,
962 xfc->NET_WM_ACTION_MOVE, xfc->NET_WM_ACTION_RESIZE,
963 xfc->NET_WM_ACTION_MAXIMIZE_HORZ, xfc->NET_WM_ACTION_MAXIMIZE_VERT,
964 xfc->NET_WM_ACTION_FULLSCREEN, xfc->NET_WM_ACTION_CHANGE_DESKTOP
965 };
966
967 if (!(appWindow->dwStyle & WS_SYSMENU))
968 allowed_actions[0] = 0;
969
970 if (!(appWindow->dwStyle & WS_MINIMIZEBOX))
971 allowed_actions[1] = 0;
972
973 if (!(appWindow->dwStyle & WS_SIZEBOX))
974 allowed_actions[3] = 0;
975
976 if (!(appWindow->dwStyle & WS_MAXIMIZEBOX))
977 {
978 allowed_actions[4] = 0;
979 allowed_actions[5] = 0;
980 allowed_actions[6] = 0;
981 }
982
983 LogDynAndXChangeProperty(xfc->log, xfc->display, appWindow->handle, xfc->NET_WM_ALLOWED_ACTIONS,
984 XA_ATOM, 32, PropModeReplace, (unsigned char*)&allowed_actions, 8);
985}
986
987void xf_SetWindowText(xfContext* xfc, xfAppWindow* appWindow, const char* name)
988{
989 xf_SetWindowTitleText(xfc, appWindow->handle, name);
990}
991
992static void xf_FixWindowCoordinates(xfContext* xfc, int* x, int* y, int* width, int* height)
993{
994 int vscreen_width = 0;
995 int vscreen_height = 0;
996 vscreen_width = xfc->vscreen.area.right - xfc->vscreen.area.left + 1;
997 vscreen_height = xfc->vscreen.area.bottom - xfc->vscreen.area.top + 1;
998
999 if (*x < xfc->vscreen.area.left)
1000 {
1001 /* Only trim the window when a usable width is left. The original code
1002 * unconditionally did `*width += *x`, which turns the width negative
1003 * (and later clamps it to 1px) when the server reports an x far left of
1004 * the virtual desktop - which happens for RemoteApp dialogs when the
1005 * window manager places them. */
1006 const int shift = xfc->vscreen.area.left - *x;
1007
1008 if (*width - shift >= 64)
1009 *width -= shift;
1010
1011 *x = xfc->vscreen.area.left;
1012 }
1013
1014 if (*y < xfc->vscreen.area.top)
1015 {
1016 /* See above. */
1017 const int shift = xfc->vscreen.area.top - *y;
1018
1019 if (*height - shift >= 64)
1020 *height -= shift;
1021
1022 *y = xfc->vscreen.area.top;
1023 }
1024
1025 if (*width > vscreen_width)
1026 {
1027 *width = vscreen_width;
1028 }
1029
1030 if (*height > vscreen_height)
1031 {
1032 *height = vscreen_height;
1033 }
1034
1035 if (*width < 1)
1036 {
1037 *width = 1;
1038 }
1039
1040 if (*height < 1)
1041 {
1042 *height = 1;
1043 }
1044}
1045
1046BOOL xf_AppWindowInit(xfContext* xfc, xfAppWindow* appWindow, BOOL hidden)
1047{
1048 BOOL redraw = FALSE;
1049 if (!xfc || !appWindow)
1050 return FALSE;
1051
1052 xf_SetWindowDecorations(xfc, appWindow->handle, appWindow->decorations);
1053 xf_SetWindowStyle(xfc, appWindow, appWindow->dwStyle, appWindow->dwExStyle);
1054 xf_SetWindowPID(xfc, appWindow->handle, 0);
1055 LogDynAndXClearWindow(xfc->log, xfc->display, appWindow->handle);
1056
1057 /* A window the server creates hidden stays unmapped until the server shows it, see
1058 * xf_ShowWindow(). Mapping it here and withdrawing it right after races with the window
1059 * manager: the later map request can be lost, leaving a withdrawn window that is never
1060 * shown while the application waits on it, e.g. a modal dialog. */
1061 if (hidden)
1062 {
1063 xf_SetWindowText(xfc, appWindow, appWindow->title);
1064 return FALSE;
1065 }
1066
1067 if (xf_ShowWindow(xfc, appWindow, WINDOW_SHOW))
1068 redraw = TRUE;
1069 LogDynAndXMapWindow(xfc->log, xfc->display, appWindow->handle);
1070 /* Move doesn't seem to work until window is mapped. */
1071 if (xf_MoveWindow(xfc, appWindow, appWindow->x, appWindow->y, appWindow->width,
1072 appWindow->height))
1073 redraw = TRUE;
1074 xf_SetWindowText(xfc, appWindow, appWindow->title);
1075 return redraw;
1076}
1077
1078BOOL xf_AppWindowCreate(xfContext* xfc, xfAppWindow* appWindow)
1079{
1080 XGCValues gcv = WINPR_C_ARRAY_INIT;
1081 int input_mask = 0;
1082 XWMHints* InputModeHint = nullptr;
1083 XClassHint* class_hints = nullptr;
1084 const rdpSettings* settings = nullptr;
1085
1086 WINPR_ASSERT(xfc);
1087 WINPR_ASSERT(appWindow);
1088
1089 settings = xfc->common.context.settings;
1090 WINPR_ASSERT(settings);
1091
1092 xf_FixWindowCoordinates(xfc, &appWindow->x, &appWindow->y, &appWindow->width,
1093 &appWindow->height);
1094 appWindow->shmid = -1;
1095 appWindow->decorations = xfc->decorations;
1096 appWindow->fullscreen = FALSE;
1097 appWindow->local_move.state = LMS_NOT_ACTIVE;
1098 appWindow->is_mapped = FALSE;
1099 appWindow->is_transient = FALSE;
1100 appWindow->override_redirect = FALSE;
1101 appWindow->rail_state = 0;
1102 appWindow->maxVert = FALSE;
1103 appWindow->maxHorz = FALSE;
1104 appWindow->minimized = FALSE;
1105 appWindow->rail_ignore_configure = FALSE;
1106 appWindow->rail_fullscreen_normalizing = FALSE;
1107 appWindow->lastWndSizeUpdate = 0;
1108
1109 WINPR_ASSERT(xfc->depth != 0);
1110 appWindow->handle = LogDynAndXCreateWindow(
1111 xfc->log, xfc->display, RootWindowOfScreen(xfc->screen), appWindow->x, appWindow->y,
1112 WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->width),
1113 WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->height), 0, xfc->depth, InputOutput,
1114 xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->attribs_mask), &xfc->attribs);
1115
1116 if (!appWindow->handle)
1117 return FALSE;
1118
1119 appWindow->gc =
1120 LogDynAndXCreateGC(xfc->log, xfc->display, appWindow->handle, GCGraphicsExposures, &gcv);
1121
1122 if (!xf_AppWindowResize(xfc, appWindow))
1123 return FALSE;
1124
1125 class_hints = XAllocClassHint();
1126
1127 if (class_hints)
1128 {
1129 char* strclass = nullptr;
1130
1131 const char* WmClass = freerdp_settings_get_string(settings, FreeRDP_WmClass);
1132 if (WmClass)
1133 strclass = _strdup(WmClass);
1134 else
1135 {
1136 size_t size = 0;
1137 winpr_asprintf(&strclass, &size, "RAIL:%08" PRIX64 "", appWindow->windowId);
1138 }
1139 class_hints->res_class = strclass;
1140 class_hints->res_name = "RAIL";
1141 XSetClassHint(xfc->display, appWindow->handle, class_hints);
1142 XFree(class_hints);
1143 free(strclass);
1144 }
1145
1146 /* Set the input mode hint for the WM */
1147 InputModeHint = XAllocWMHints();
1148 InputModeHint->flags = (1L << 0);
1149 InputModeHint->input = True;
1150 XSetWMHints(xfc->display, appWindow->handle, InputModeHint);
1151 XFree(InputModeHint);
1152 XSetWMProtocols(xfc->display, appWindow->handle, &(xfc->WM_DELETE_WINDOW), 1);
1153 input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
1154 EnterWindowMask | LeaveWindowMask | PointerMotionMask | Button1MotionMask |
1155 Button2MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask |
1156 ButtonMotionMask | KeymapStateMask | ExposureMask | VisibilityChangeMask |
1157 StructureNotifyMask | SubstructureNotifyMask | SubstructureRedirectMask |
1158 FocusChangeMask | PropertyChangeMask | ColormapChangeMask | OwnerGrabButtonMask;
1159 XSelectInput(xfc->display, appWindow->handle, input_mask);
1160
1161 if (xfc->XWAYLAND_MAY_GRAB_KEYBOARD)
1162 xf_SendClientEvent(xfc, appWindow->handle, xfc->XWAYLAND_MAY_GRAB_KEYBOARD, 1, 1);
1163
1164 return TRUE;
1165}
1166
1167void xf_SetWindowMinMaxInfo(xfContext* xfc, xfAppWindow* appWindow, WINPR_ATTR_UNUSED int maxWidth,
1168 WINPR_ATTR_UNUSED int maxHeight, WINPR_ATTR_UNUSED int maxPosX,
1169 WINPR_ATTR_UNUSED int maxPosY, int minTrackWidth, int minTrackHeight,
1170 int maxTrackWidth, int maxTrackHeight)
1171{
1172 XSizeHints* size_hints = nullptr;
1173 size_hints = XAllocSizeHints();
1174
1175 if (size_hints)
1176 {
1177 size_hints->flags = PMinSize | PMaxSize | PResizeInc;
1178 size_hints->min_width = minTrackWidth;
1179 size_hints->min_height = minTrackHeight;
1180 size_hints->max_width = maxTrackWidth;
1181 size_hints->max_height = maxTrackHeight;
1182 /* to speedup window drawing we need to select optimal value for sizing step. */
1183 size_hints->width_inc = size_hints->height_inc = 1;
1184 XSetWMNormalHints(xfc->display, appWindow->handle, size_hints);
1185 XFree(size_hints);
1186 }
1187}
1188
1189void xf_StartLocalMoveSize(xfContext* xfc, xfAppWindow* appWindow, int direction, int x, int y)
1190{
1191 if (appWindow->local_move.state != LMS_NOT_ACTIVE)
1192 return;
1193
1194 /*
1195 * Save original mouse location relative to root. This will be needed
1196 * to end local move to RDP server and/or X server
1197 */
1198 appWindow->local_move.root_x = x;
1199 appWindow->local_move.root_y = y;
1200 appWindow->local_move.state = LMS_STARTING;
1201 appWindow->local_move.direction = direction;
1202
1203 xf_ungrab(xfc);
1204
1205 xf_SendClientEvent(
1206 xfc, appWindow->handle,
1207 xfc->NET_WM_MOVERESIZE, /* request X window manager to initiate a local move */
1208 5, /* 5 arguments to follow */
1209 x, /* x relative to root window */
1210 y, /* y relative to root window */
1211 direction, /* extended ICCM direction flag */
1212 1, /* simulated mouse button 1 */
1213 1); /* 1 == application request per extended ICCM */
1214}
1215
1216void xf_EndLocalMoveSize(xfContext* xfc, xfAppWindow* appWindow)
1217{
1218 if (appWindow->local_move.state == LMS_NOT_ACTIVE)
1219 return;
1220
1221 if (appWindow->local_move.state == LMS_STARTING)
1222 {
1223 /*
1224 * The move never was property started. This can happen due to race
1225 * conditions between the mouse button up and the communications to the
1226 * RDP server for local moves. We must cancel the X window manager move.
1227 * Per ICCM, the X client can ask to cancel an active move.
1228 */
1229 xf_SendClientEvent(
1230 xfc, appWindow->handle,
1231 xfc->NET_WM_MOVERESIZE, /* request X window manager to abort a local move */
1232 5, /* 5 arguments to follow */
1233 appWindow->local_move.root_x, /* x relative to root window */
1234 appWindow->local_move.root_y, /* y relative to root window */
1235 NET_WM_MOVERESIZE_CANCEL, /* extended ICCM direction flag */
1236 1, /* simulated mouse button 1 */
1237 1); /* 1 == application request per extended ICCM */
1238 }
1239
1240 appWindow->local_move.state = LMS_NOT_ACTIVE;
1241}
1242
1243BOOL xf_MoveWindow(xfContext* xfc, xfAppWindow* appWindow, int x, int y, int width, int height)
1244{
1245 BOOL resize = FALSE;
1246
1247 if ((width * height) < 1)
1248 return FALSE;
1249
1250 if ((appWindow->width != width) || (appWindow->height != height))
1251 resize = TRUE;
1252
1253 if (appWindow->local_move.state == LMS_STARTING || appWindow->local_move.state == LMS_ACTIVE)
1254 return FALSE;
1255
1256 appWindow->x = x;
1257 appWindow->y = y;
1258 appWindow->width = width;
1259 appWindow->height = height;
1260
1261 if (resize)
1262 {
1263 if (!xf_AppWindowResize(xfc, appWindow))
1264 return FALSE;
1265
1266 LogDynAndXMoveResizeWindow(xfc->log, xfc->display, appWindow->handle, x, y,
1267 WINPR_ASSERTING_INT_CAST(uint32_t, width),
1268 WINPR_ASSERTING_INT_CAST(uint32_t, height));
1269 }
1270 else
1271 LogDynAndXMoveWindow(xfc->log, xfc->display, appWindow->handle, x, y);
1272
1273 return xf_UpdateWindowArea(xfc, appWindow, 0, 0, width, height);
1274}
1275
1276BOOL xf_ShowWindow(xfContext* xfc, xfAppWindow* appWindow, BYTE state)
1277{
1278 BOOL redraw = FALSE;
1279 WINPR_ASSERT(xfc);
1280 WINPR_ASSERT(appWindow);
1281
1282 switch (state)
1283 {
1284 case WINDOW_HIDE:
1285 /* Nothing to withdraw if the window was never shown, see xf_AppWindowInit(). */
1286 if (appWindow->rail_state != WINDOW_HIDE)
1287 LogDynAndXWithdrawWindow(xfc->log, xfc->display, appWindow->handle,
1288 xfc->screen_number);
1289 break;
1290
1291 case WINDOW_SHOW_MINIMIZED:
1292 appWindow->minimized = TRUE;
1293 XIconifyWindow(xfc->display, appWindow->handle, xfc->screen_number);
1294 break;
1295
1296 case WINDOW_SHOW_MAXIMIZED:
1297 /* Set the window as maximized */
1298 appWindow->maxHorz = TRUE;
1299 appWindow->maxVert = TRUE;
1300 xf_SendClientEvent(xfc, appWindow->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_ADD,
1301 xfc->NET_WM_STATE_MAXIMIZED_VERT, xfc->NET_WM_STATE_MAXIMIZED_HORZ,
1302 0);
1303
1304 /*
1305 * This is a workaround for the case where the window is maximized locally before the
1306 * rail server is told to maximize the window, this appears to be a race condition where
1307 * the local window with incomplete data and once the window is actually maximized on
1308 * the server
1309 * - an update of the new areas may not happen. So, we simply to do a full update of the
1310 * entire window once the rail server notifies us that the window is now maximized.
1311 */
1312 if (appWindow->rail_state == WINDOW_SHOW_MAXIMIZED)
1313 {
1314 if (xf_UpdateWindowArea(xfc, appWindow, 0, 0,
1315 WINPR_ASSERTING_INT_CAST(int, appWindow->windowWidth),
1316 WINPR_ASSERTING_INT_CAST(int, appWindow->windowHeight)))
1317 redraw = TRUE;
1318 }
1319
1320 break;
1321
1322 case WINDOW_SHOW:
1323 /* Ensure the window is not maximized */
1324 xf_SendClientEvent(xfc, appWindow->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_REMOVE,
1325 xfc->NET_WM_STATE_MAXIMIZED_VERT, xfc->NET_WM_STATE_MAXIMIZED_HORZ,
1326 0);
1327
1328 /*
1329 * Ignore configure requests until both the Maximized properties have been processed
1330 * to prevent condition where WM overrides size of request due to one or both of these
1331 * properties still being set - which causes a position adjustment to be sent back to
1332 * the server thus causing the window to not return to its original size
1333 */
1334 if (appWindow->rail_state == WINDOW_SHOW_MAXIMIZED)
1335 appWindow->rail_ignore_configure = TRUE;
1336
1337 if (appWindow->is_transient)
1338 xf_SetWindowUnlisted(xfc, appWindow->handle);
1339
1340 LogDynAndXMapWindow(xfc->log, xfc->display, appWindow->handle);
1341 /* Move doesn't work until the window is mapped, see xf_AppWindowInit(). */
1342 if ((appWindow->rail_state == WINDOW_HIDE) && (appWindow->width > 0) &&
1343 (appWindow->height > 0))
1344 LogDynAndXMoveResizeWindow(xfc->log, xfc->display, appWindow->handle,
1345 WINPR_ASSERTING_INT_CAST(int, appWindow->x),
1346 WINPR_ASSERTING_INT_CAST(int, appWindow->y),
1347 WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->width),
1348 WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->height));
1349 break;
1350 default:
1351 break;
1352 }
1353
1354 /* Save the current rail state of this window */
1355 appWindow->rail_state = state;
1356 LogDynAndXFlush(xfc->log, xfc->display);
1357 return redraw;
1358}
1359
1360void xf_SetWindowRects(xfContext* xfc, xfAppWindow* appWindow, RECTANGLE_16* rects, int nrects)
1361{
1362 XRectangle* xrects = nullptr;
1363
1364 if (nrects < 1)
1365 return;
1366
1367#ifdef WITH_XEXT
1368 xrects = (XRectangle*)calloc(WINPR_ASSERTING_INT_CAST(uint32_t, nrects), sizeof(XRectangle));
1369
1370 for (int i = 0; i < nrects; i++)
1371 {
1372 /* Coords may be negative (INT16 in UINT16 fields); cast modularly, no assert. */
1373 xrects[i].x = WINPR_CXX_COMPAT_CAST(short, rects[i].left);
1374 xrects[i].y = WINPR_CXX_COMPAT_CAST(short, rects[i].top);
1375 xrects[i].width = WINPR_CXX_COMPAT_CAST(unsigned short, rects[i].right - rects[i].left);
1376 xrects[i].height = WINPR_CXX_COMPAT_CAST(unsigned short, rects[i].bottom - rects[i].top);
1377 }
1378
1379 XShapeCombineRectangles(xfc->display, appWindow->handle, ShapeBounding, 0, 0, xrects, nrects,
1380 ShapeSet, 0);
1381 free(xrects);
1382#endif
1383}
1384
1385void xf_SetWindowVisibilityRects(xfContext* xfc, xfAppWindow* appWindow, UINT32 rectsOffsetX,
1386 UINT32 rectsOffsetY, RECTANGLE_16* rects, int nrects)
1387{
1388 XRectangle* xrects = nullptr;
1389
1390 if (nrects < 1)
1391 return;
1392
1393#ifdef WITH_XEXT
1394 xrects = (XRectangle*)calloc(WINPR_ASSERTING_INT_CAST(uint32_t, nrects), sizeof(XRectangle));
1395
1396 for (int i = 0; i < nrects; i++)
1397 {
1398 /* Coords may be negative (INT16 in UINT16 fields); cast modularly, no assert. */
1399 xrects[i].x = WINPR_CXX_COMPAT_CAST(short, rects[i].left);
1400 xrects[i].y = WINPR_CXX_COMPAT_CAST(short, rects[i].top);
1401 xrects[i].width = WINPR_CXX_COMPAT_CAST(unsigned short, rects[i].right - rects[i].left);
1402 xrects[i].height = WINPR_CXX_COMPAT_CAST(unsigned short, rects[i].bottom - rects[i].top);
1403 }
1404
1405 XShapeCombineRectangles(
1406 xfc->display, appWindow->handle, ShapeBounding, WINPR_ASSERTING_INT_CAST(int, rectsOffsetX),
1407 WINPR_ASSERTING_INT_CAST(int, rectsOffsetY), xrects, nrects, ShapeSet, 0);
1408 free(xrects);
1409#endif
1410}
1411
1412void xf_ClearWindowVisibilityRects(xfContext* xfc, xfAppWindow* appWindow)
1413{
1414 WINPR_ASSERT(xfc);
1415 WINPR_ASSERT(appWindow);
1416#ifdef WITH_XEXT
1417 /* Drop the bounding shape so the whole window is visible. */
1418 XShapeCombineMask(xfc->display, appWindow->handle, ShapeBounding, 0, 0, None, ShapeSet);
1419#endif
1420}
1421
1422BOOL xf_UpdateWindowArea(xfContext* xfc, xfAppWindow* appWindow, int x, int y, int width,
1423 int height)
1424{
1425 int ax = 0;
1426 int ay = 0;
1427 const rdpSettings* settings = nullptr;
1428
1429 WINPR_ASSERT(xfc);
1430
1431 settings = xfc->common.context.settings;
1432 WINPR_ASSERT(settings);
1433
1434 if (appWindow == nullptr)
1435 return FALSE;
1436
1437 if (appWindow->surfaceId < UINT16_MAX)
1438 {
1439 /* need a full window update */
1440 if (appWindow->is_mapped && appWindow->surfaceStale)
1441 return TRUE;
1442 /* The pixmap holds what xf_AppUpdateWindowFromSurface() painted last. */
1443 else if (!appWindow->surfaceStale && (appWindow->pixmap != 0) && (x >= 0) && (y >= 0) &&
1444 (width > 0) && (height > 0))
1445 {
1446 LogDynAndXCopyArea(xfc->log, xfc->display, appWindow->pixmap, appWindow->handle,
1447 appWindow->gc, x, y, WINPR_ASSERTING_INT_CAST(uint32_t, width),
1448 WINPR_ASSERTING_INT_CAST(uint32_t, height), x, y);
1449 LogDynAndXFlush(xfc->log, xfc->display);
1450 }
1451 return FALSE;
1452 }
1453
1454 /*
1455 * With GFX a RemoteApp window shows its own surface, see xf_AppUpdateWindowFromSurface().
1456 * Until the server maps a surface to the window it has no content. xfc->image contains the
1457 * hidden server desktop (black, wallpaper), painting from it flashes that in a new window,
1458 * e.g. the Snap bar when dragging a window. Leave the window empty until its surface is
1459 * mapped.
1460 */
1461 const rdpGdi* gdi = xfc->common.context.gdi;
1462 if (gdi && gdi->gfx)
1463 return FALSE;
1464
1465 ax = x + appWindow->windowOffsetX;
1466 ay = y + appWindow->windowOffsetY;
1467
1468 if (ax + width > appWindow->windowOffsetX + appWindow->width)
1469 width = (appWindow->windowOffsetX + appWindow->width - 1) - ax;
1470
1471 if (ay + height > appWindow->windowOffsetY + appWindow->height)
1472 height = (appWindow->windowOffsetY + appWindow->height - 1) - ay;
1473
1474 if (freerdp_settings_get_bool(settings, FreeRDP_SoftwareGdi))
1475 {
1476 LogDynAndXPutImage(xfc->log, xfc->display, appWindow->pixmap, appWindow->gc, xfc->image, ax,
1477 ay, x, y, WINPR_ASSERTING_INT_CAST(uint32_t, width),
1478 WINPR_ASSERTING_INT_CAST(uint32_t, height));
1479 }
1480
1481 LogDynAndXCopyArea(xfc->log, xfc->display, appWindow->pixmap, appWindow->handle, appWindow->gc,
1482 x, y, WINPR_ASSERTING_INT_CAST(uint32_t, width),
1483 WINPR_ASSERTING_INT_CAST(uint32_t, height), x, y);
1484 LogDynAndXFlush(xfc->log, xfc->display);
1485 return FALSE;
1486}
1487
1488void xf_AppWindowDestroyImage(xfAppWindow* appWindow)
1489{
1490 WINPR_ASSERT(appWindow);
1491 if (appWindow->image)
1492 {
1493 appWindow->image->data = nullptr;
1494 XDestroyImage(appWindow->image);
1495 appWindow->image = nullptr;
1496 }
1497}
1498
1499void xf_DestroyWindow(xfContext* xfc, xfAppWindow* appWindow)
1500{
1501 if (!appWindow)
1502 return;
1503
1504 if (xfc->appWindow == appWindow)
1505 xfc->appWindow = nullptr;
1506
1507 if (appWindow->gc)
1508 LogDynAndXFreeGC(xfc->log, xfc->display, appWindow->gc);
1509
1510 if (appWindow->pixmap)
1511 LogDynAndXFreePixmap(xfc->log, xfc->display, appWindow->pixmap);
1512
1513 xf_AppWindowDestroyImage(appWindow);
1514
1515 if (appWindow->handle)
1516 {
1517 LogDynAndXUnmapWindow(xfc->log, xfc->display, appWindow->handle);
1518 LogDynAndXDestroyWindow(xfc->log, xfc->display, appWindow->handle);
1519 }
1520
1521 if (appWindow->xfwin)
1522 munmap(nullptr, sizeof(*appWindow->xfwin));
1523
1524 if (appWindow->shmid >= 0)
1525 close(appWindow->shmid);
1526
1527 shm_unlink(get_shm_id());
1528 appWindow->xfwin = (Window*)-1;
1529 appWindow->shmid = -1;
1530 free(appWindow->title);
1531 free(appWindow->windowRects);
1532 free(appWindow->visibilityRects);
1533 free(appWindow);
1534}
1535
1536static xfAppWindow* get_windowUnlocked(xfContext* xfc, UINT64 id)
1537{
1538 WINPR_ASSERT(xfc);
1539 return HashTable_GetItemValue(xfc->railWindows, &id);
1540}
1541
1542xfAppWindow* xf_rail_get_windowFrom(xfContext* xfc, UINT64 id, BOOL alreadyLocked, const char* file,
1543 const char* fkt, size_t line)
1544{
1545 if (!xfc)
1546 return nullptr;
1547
1548 if (!xfc->railWindows)
1549 return nullptr;
1550
1551 if (!alreadyLocked)
1552 xfAppWindowsLockFrom(xfc, file, fkt, line);
1553
1554 xfAppWindow* window = get_windowUnlocked(xfc, id);
1555
1556 if (!window && !alreadyLocked)
1557 xfAppWindowsUnlockFrom(xfc, file, fkt, line);
1558
1559 return window;
1560}
1561
1562xfAppWindow* xf_AppWindowFromX11WindowFrom(xfContext* xfc, Window wnd, const char* file,
1563 const char* fkt, size_t line)
1564{
1565 ULONG_PTR* pKeys = nullptr;
1566
1567 WINPR_ASSERT(xfc);
1568 if (!xfc->railWindows)
1569 return nullptr;
1570
1571 xfAppWindowsLockFrom(xfc, file, fkt, line);
1572 size_t count = HashTable_GetKeys(xfc->railWindows, &pKeys);
1573
1574 for (size_t index = 0; index < count; index++)
1575 {
1576 xfAppWindow* appWindow = get_windowUnlocked(xfc, *(UINT64*)pKeys[index]);
1577
1578 if (!appWindow)
1579 {
1580 xfAppWindowsUnlockFrom(xfc, file, fkt, line);
1581 free(pKeys);
1582 return nullptr;
1583 }
1584
1585 if (appWindow->handle == wnd)
1586 {
1587 free(pKeys);
1588 return appWindow;
1589 }
1590 }
1591
1592 xfAppWindowsUnlockFrom(xfc, file, fkt, line);
1593 free(pKeys);
1594 return nullptr;
1595}
1596
1597UINT xf_AppUpdateWindowFromSurface(xfContext* xfc, gdiGfxSurface* surface)
1598{
1599 XImage* image = nullptr;
1600 UINT rc = ERROR_INTERNAL_ERROR;
1601
1602 WINPR_ASSERT(xfc);
1603 WINPR_ASSERT(surface);
1604
1605 xfAppWindow* appWindow = xf_rail_get_window(xfc, surface->windowId, FALSE);
1606 if (!appWindow)
1607 {
1608 WLog_VRB(TAG, "Failed to find a window for id=0x%08" PRIx64, surface->windowId);
1609 region16_clear(&surface->invalidRegion);
1610 return CHANNEL_RC_OK;
1611 }
1612
1613 BOOL surfaceChanged = (appWindow->surfaceId != surface->surfaceId);
1614 if (surfaceChanged)
1615 appWindow->surfaceId = surface->surfaceId;
1616
1617 /* The server keeps sending updates for windows that are not visible on the
1618 * client, e.g. minimized ones or the hidden desktop window ("Program
1619 * Manager") that spans the whole virtual screen. Uploading those pixels into
1620 * an unmapped X window is wasted work (~100 MB per frame on a large
1621 * multi-monitor setup), so skip it and repaint the whole window once it is
1622 * mapped again, see xf_AppWindowRepaintFromSurface(). */
1623 if (!appWindow->is_mapped)
1624 {
1625 appWindow->surfaceStale = TRUE;
1626 region16_clear(&surface->invalidRegion);
1627 xf_rail_return_window(appWindow, FALSE);
1628 return CHANNEL_RC_OK;
1629 }
1630
1631 if (appWindow->surfaceStale)
1632 {
1633 appWindow->surfaceStale = FALSE;
1634 surfaceChanged = TRUE;
1635 }
1636
1637 const BOOL maximized = (appWindow->dwStyle & WS_MAXIMIZE) != 0;
1638 const UINT32 winW = WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->width);
1639 const UINT32 winH = WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->height);
1640
1641 const BOOL swGdi = freerdp_settings_get_bool(xfc->common.context.settings, FreeRDP_SoftwareGdi);
1642 UINT32 nrects = 0;
1643 const RECTANGLE_16* rects = region16_rects(&surface->invalidRegion, &nrects);
1644
1645 RECTANGLE_16 fullRect = WINPR_C_ARRAY_INIT;
1646 if (surfaceChanged)
1647 {
1648 fullRect.right = WINPR_ASSERTING_INT_CAST(UINT16, MIN(winW, surface->width));
1649 fullRect.bottom = WINPR_ASSERTING_INT_CAST(UINT16, MIN(winH, surface->height));
1650 if ((fullRect.right > 0) && (fullRect.bottom > 0))
1651 {
1652 rects = &fullRect;
1653 nrects = 1;
1654 }
1655 }
1656
1657 if (swGdi)
1658 {
1659 if (surfaceChanged || (appWindow->width != (INT64)surface->width) ||
1660 (appWindow->height != (INT64)surface->height))
1661 xf_AppWindowDestroyImage(appWindow);
1662
1663 if (!appWindow->image)
1664 {
1665 WINPR_ASSERT(xfc->depth != 0);
1666 appWindow->image = LogDynAndXCreateImage(
1667 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
1668 ZPixmap, 0, (char*)surface->data, surface->width, surface->height,
1669 xfc->scanline_pad, WINPR_ASSERTING_INT_CAST(int, surface->scanline));
1670 if (!appWindow->image)
1671 {
1672 WLog_WARN(TAG,
1673 "Failed create a XImage[%" PRIu32 "x%" PRIu32 ", scanline=%" PRIu32
1674 ", bpp=%" PRId32 "] for window id=0x%08" PRIx64,
1675 surface->width, surface->height, surface->scanline, xfc->depth,
1676 surface->windowId);
1677 goto fail;
1678 }
1679 appWindow->image->byte_order = LSBFirst;
1680 appWindow->image->bitmap_bit_order = LSBFirst;
1681 }
1682
1683 image = appWindow->image;
1684 }
1685 else
1686 {
1687 xfGfxSurface* xfSurface = (xfGfxSurface*)surface;
1688 image = xfSurface->image;
1689 }
1690
1691 /* Skip the off-screen resize-margin frame a maximized surface carries: shift the blit by the
1692 * real left/top margin so content fills from (0,0). */
1693 const int insetX =
1694 (maximized && (surface->mappedWidth > winW)) ? (int)appWindow->resizeMarginLeft : 0;
1695 const int insetY =
1696 (maximized && (surface->mappedHeight > winH)) ? (int)appWindow->resizeMarginTop : 0;
1697
1698 for (UINT32 x = 0; x < nrects; x++)
1699 {
1700 const RECTANGLE_16* rect = &rects[x];
1701 int srcX = rect->left;
1702 int srcY = rect->top;
1703 int dstX = (int)rect->left - insetX;
1704 int dstY = (int)rect->top - insetY;
1705 int w = (int)rect->right - rect->left;
1706 int h = (int)rect->bottom - rect->top;
1707
1708 if (dstX < 0)
1709 {
1710 srcX -= dstX;
1711 w += dstX;
1712 dstX = 0;
1713 }
1714 if (dstY < 0)
1715 {
1716 srcY -= dstY;
1717 h += dstY;
1718 dstY = 0;
1719 }
1720 if ((w <= 0) || (h <= 0))
1721 continue;
1722
1723 LogDynAndXPutImage(xfc->log, xfc->display, appWindow->pixmap, appWindow->gc, image, srcX,
1724 srcY, dstX, dstY, WINPR_ASSERTING_INT_CAST(uint32_t, w),
1725 WINPR_ASSERTING_INT_CAST(uint32_t, h));
1726
1727 LogDynAndXCopyArea(xfc->log, xfc->display, appWindow->pixmap, appWindow->handle,
1728 appWindow->gc, dstX, dstY, WINPR_ASSERTING_INT_CAST(uint32_t, w),
1729 WINPR_ASSERTING_INT_CAST(uint32_t, h), dstX, dstY);
1730 }
1731
1732 rc = CHANNEL_RC_OK;
1733fail:
1734 /* The invalid region has been painted, reset it like xf_OutputUpdate does for
1735 * output mapped surfaces. Otherwise the damage accumulates across frames until
1736 * the whole window is uploaded again on every single update. */
1737 region16_clear(&surface->invalidRegion);
1738 xf_rail_return_window(appWindow, FALSE);
1739 LogDynAndXFlush(xfc->log, xfc->display);
1740
1741 return rc;
1742}
1743
1744BOOL xf_AppWindowRepaintFromSurface(xfContext* xfc, UINT64 windowId, UINT32 surfaceId)
1745{
1746 WINPR_ASSERT(xfc);
1747
1748 if (surfaceId >= UINT16_MAX)
1749 return TRUE;
1750
1751 rdpGdi* gdi = xfc->common.context.gdi;
1752 if (!gdi || !gdi->gfx)
1753 return TRUE;
1754
1755 RdpgfxClientContext* gfx = gdi->gfx;
1756 UINT rc = CHANNEL_RC_OK;
1757
1758 /* Called on the X11 event thread with the X11 lock held. The GFX channel thread takes
1759 * gfx->mux first and the X11 lock second (UpdateSurfaces and DeleteSurface reach
1760 * xf_rail_get_window()), so blocking here would deadlock. Like xf_OutputExpose, repaint
1761 * on a best-effort basis: if the mux is busy, the window stays surfaceStale and the next
1762 * UpdateSurfaces pass repaints it fully. */
1763 if (!TryEnterCriticalSection(&gfx->mux))
1764 {
1765 WLog_VRB(TAG, "gfx mux busy, deferring repaint of window 0x%08" PRIx64, windowId);
1766 return TRUE;
1767 }
1768
1769 gdiGfxSurface* surface = gfx->GetSurfaceData(gfx, (UINT16)surfaceId);
1770 if (surface && surface->windowMapped && (surface->windowId == windowId))
1771 rc = xf_AppUpdateWindowFromSurface(xfc, surface);
1772 LeaveCriticalSection(&gfx->mux);
1773 return rc == CHANNEL_RC_OK;
1774}
1775
1776BOOL xf_AppWindowResize(xfContext* xfc, xfAppWindow* appWindow)
1777{
1778 WINPR_ASSERT(xfc);
1779 WINPR_ASSERT(appWindow);
1780
1781 const UINT32 width = WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->width);
1782 const UINT32 height = WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->height);
1783
1784 /* Called on every ConfigureNotify, also for plain moves: keep the pixmap and its content
1785 * when the size did not change, it is what an Expose is repainted from. */
1786 if ((appWindow->pixmap != 0) && (appWindow->pixmapWidth == width) &&
1787 (appWindow->pixmapHeight == height))
1788 return TRUE;
1789
1790 if (appWindow->pixmap != 0)
1791 LogDynAndXFreePixmap(xfc->log, xfc->display, appWindow->pixmap);
1792
1793 WINPR_ASSERT(xfc->depth != 0);
1794 appWindow->pixmap = LogDynAndXCreatePixmap(xfc->log, xfc->display, xfc->drawable, width, height,
1795 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth));
1796 appWindow->pixmapWidth = width;
1797 appWindow->pixmapHeight = height;
1798 xf_AppWindowDestroyImage(appWindow);
1799
1800 /* The new pixmap has undefined content, the next paint from the surface has to cover the
1801 * whole window. */
1802 if (appWindow->surfaceId < UINT16_MAX)
1803 appWindow->surfaceStale = TRUE;
1804
1805 return appWindow->pixmap != 0;
1806}
1807
1808void xf_XSetTransientForHint(xfContext* xfc, xfAppWindow* window)
1809{
1810 WINPR_ASSERT(xfc);
1811 WINPR_ASSERT(window);
1812
1813 if (window->ownerWindowId == 0)
1814 return;
1815
1816 xfAppWindow* parent = xf_rail_get_window(xfc, window->ownerWindowId, TRUE);
1817 if (!parent)
1818 return;
1819
1820 (void)LogDynAndXSetTransientForHint(xfc->log, xfc->display, window->handle, parent->handle);
1821 xf_rail_return_window(parent, TRUE);
1822}
1823
1824void xfAppWindowsLockFrom(xfContext* xfc, WINPR_ATTR_UNUSED const char* file,
1825 WINPR_ATTR_UNUSED const char* fkt, WINPR_ATTR_UNUSED size_t line)
1826{
1827 WINPR_ASSERT(xfc);
1828
1829#if defined(WITH_VERBOSE_WINPR_ASSERT)
1830 const DWORD level = WLOG_TRACE;
1831 if (WLog_IsLevelActive(xfc->log, level))
1832 WLog_PrintTextMessage(xfc->log, level, line, file, fkt, "[rails] locking [%s]", fkt);
1833#endif
1834
1835 xf_lock_x11(xfc);
1836 if (xfc->railWindows)
1837 HashTable_Lock(xfc->railWindows);
1838
1839#if defined(WITH_VERBOSE_WINPR_ASSERT)
1840 WINPR_ASSERT(!xfc->isRailWindowsLocked);
1841 xfc->isRailWindowsLocked = TRUE;
1842 xfc->whereFile = file;
1843 xfc->whereFkt = fkt;
1844 xfc->whereLine = line;
1845#endif
1846}
1847
1848void xfAppWindowsUnlockFrom(xfContext* xfc, WINPR_ATTR_UNUSED const char* file,
1849 WINPR_ATTR_UNUSED const char* fkt, WINPR_ATTR_UNUSED size_t line)
1850{
1851 WINPR_ASSERT(xfc);
1852
1853#if defined(WITH_VERBOSE_WINPR_ASSERT)
1854 const DWORD level = WLOG_TRACE;
1855 if (WLog_IsLevelActive(xfc->log, level))
1856 WLog_PrintTextMessage(xfc->log, level, line, file, fkt, "[rails] unocking [%s]", fkt);
1857
1858 WINPR_ASSERT(xfc->isRailWindowsLocked);
1859 xfc->isRailWindowsLocked = FALSE;
1860#endif
1861
1862 if (xfc->railWindows)
1863 HashTable_Unlock(xfc->railWindows);
1864 xf_unlock_x11(xfc);
1865}
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
WINPR_ATTR_NODISCARD FREERDP_API INT32 freerdp_settings_get_int32(const rdpSettings *settings, FreeRDP_Settings_Keys_Int32 id)
Returns a INT32 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.