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 = 1 << 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 DEBUG_X11("Send ClientMessage Event: wnd=0x%04lX", (unsigned long)xevent.xclient.window);
252 LogDynAndXSendEvent(xfc->log, xfc->display, RootWindowOfScreen(xfc->screen), False,
253 SubstructureRedirectMask | SubstructureNotifyMask, &xevent);
254 LogDynAndXSync(xfc->log, xfc->display, False);
255 va_end(argp);
256}
257
258void xf_SetWindowMinimized(xfContext* xfc, xfWindow* window)
259{
260 XIconifyWindow(xfc->display, window->handle, xfc->screen_number);
261}
262
263void xf_SetWindowFullscreen(xfContext* xfc, xfWindow* window, BOOL fullscreen)
264{
265 const rdpSettings* settings = nullptr;
266 int startX = 0;
267 int startY = 0;
268 UINT32 width = WINPR_ASSERTING_INT_CAST(uint32_t, window->width);
269 UINT32 height = WINPR_ASSERTING_INT_CAST(uint32_t, window->height);
270
271 WINPR_ASSERT(xfc);
272
273 settings = xfc->common.context.settings;
274 WINPR_ASSERT(settings);
275
276 /* xfc->decorations is set by caller depending on settings and whether it is fullscreen or not
277 */
278 window->decorations = xfc->decorations;
279 /* show/hide decorations (e.g. title bar) as guided by xfc->decorations */
280 xf_SetWindowDecorations(xfc, window->handle, window->decorations);
281 DEBUG_X11("X window decoration set to %d", (int)window->decorations);
282 xf_floatbar_toggle_fullscreen(xfc->window->floatbar, fullscreen);
283
284 if (fullscreen)
285 {
286 xfc->savedWidth = xfc->window->width;
287 xfc->savedHeight = xfc->window->height;
288 xfc->savedPosX = xfc->window->left;
289 xfc->savedPosY = xfc->window->top;
290
291 startX = (freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosX) != UINT32_MAX)
292 ? WINPR_ASSERTING_INT_CAST(
293 int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosX))
294 : 0;
295 startY = (freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosY) != UINT32_MAX)
296 ? WINPR_ASSERTING_INT_CAST(
297 int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosY))
298 : 0;
299 }
300 else
301 {
302 width = WINPR_ASSERTING_INT_CAST(uint32_t, xfc->savedWidth);
303 height = WINPR_ASSERTING_INT_CAST(uint32_t, xfc->savedHeight);
304 startX = xfc->savedPosX;
305 startY = xfc->savedPosY;
306 }
307
308 /* Determine the x,y starting location for the fullscreen window */
309 if (fullscreen)
310 {
311 const rdpMonitor* firstMonitor =
312 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, 0);
313 /* Initialize startX and startY with reasonable values */
314 startX = firstMonitor->x;
315 startY = firstMonitor->y;
316
317 /* Search all monitors to find the lowest startX and startY values */
318 for (size_t i = 0; i < freerdp_settings_get_uint32(settings, FreeRDP_MonitorCount); i++)
319 {
320 const rdpMonitor* monitor =
321 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, i);
322 startX = MIN(startX, monitor->x);
323 startY = MIN(startY, monitor->y);
324 }
325
326 /* Lastly apply any monitor shift(translation from remote to local coordinate system)
327 * to startX and startY values
328 */
329 startX += freerdp_settings_get_int32(settings, FreeRDP_MonitorLocalShiftX);
330 startY += freerdp_settings_get_int32(settings, FreeRDP_MonitorLocalShiftY);
331 }
332
333 /*
334 It is safe to proceed with simply toggling _NET_WM_STATE_FULLSCREEN window state on the
335 following conditions:
336 - The window manager supports multiple monitor full screen
337 - The user requested to use a single monitor to render the remote desktop
338 */
339 if (xfc->NET_WM_FULLSCREEN_MONITORS != None ||
340 freerdp_settings_get_uint32(settings, FreeRDP_MonitorCount) == 1)
341 {
342 xf_ResizeDesktopWindow(xfc, window, WINPR_ASSERTING_INT_CAST(int, width),
343 WINPR_ASSERTING_INT_CAST(int, height));
344
345 if (fullscreen)
346 {
347 /* enter full screen: move the window before adding NET_WM_STATE_FULLSCREEN */
348 LogDynAndXMoveWindow(xfc->log, xfc->display, window->handle, startX, startY);
349 }
350
351 /* Set the fullscreen state */
352 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4,
353 fullscreen ? NET_WM_STATE_ADD : NET_WM_STATE_REMOVE,
354 xfc->NET_WM_STATE_FULLSCREEN, 0, 0);
355
356 if (!fullscreen)
357 {
358 /* leave full screen: move the window after removing NET_WM_STATE_FULLSCREEN
359 * Resize the window again, the previous call to xf_SendClientEvent might have
360 * changed the window size (borders, ...)
361 */
362 xf_ResizeDesktopWindow(xfc, window, WINPR_ASSERTING_INT_CAST(int, width),
363 WINPR_ASSERTING_INT_CAST(int, height));
364 LogDynAndXMoveWindow(xfc->log, xfc->display, window->handle, startX, startY);
365 }
366
367 /* Set monitor bounds */
368 if (freerdp_settings_get_uint32(settings, FreeRDP_MonitorCount) > 1)
369 {
370 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_FULLSCREEN_MONITORS, 5,
371 xfc->fullscreenMonitors.top, xfc->fullscreenMonitors.bottom,
372 xfc->fullscreenMonitors.left, xfc->fullscreenMonitors.right, 1);
373 }
374 }
375 else
376 {
377 if (fullscreen)
378 {
379 xf_SetWindowDecorations(xfc, window->handle, FALSE);
380
381 if (xfc->fullscreenMonitors.top)
382 {
383 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_ADD,
384 xfc->fullscreenMonitors.top, 0, 0);
385 }
386 else
387 {
388 XSetWindowAttributes xswa = WINPR_C_ARRAY_INIT;
389 xswa.override_redirect = True;
390 LogDynAndXChangeWindowAttributes(xfc->log, xfc->display, window->handle,
391 CWOverrideRedirect, &xswa);
392 LogDynAndXRaiseWindow(xfc->log, xfc->display, window->handle);
393 xswa.override_redirect = False;
394 LogDynAndXChangeWindowAttributes(xfc->log, xfc->display, window->handle,
395 CWOverrideRedirect, &xswa);
396 }
397
398 /* if window is in maximized state, save and remove */
399 if (xfc->NET_WM_STATE_MAXIMIZED_VERT != None)
400 {
401 BYTE state = 0;
402 unsigned long nitems = 0;
403 unsigned long bytes = 0;
404 BYTE* prop = nullptr;
405
406 if (xf_GetWindowProperty(xfc, window->handle, xfc->NET_WM_STATE, 255, &nitems,
407 &bytes, &prop))
408 {
409 const Atom* aprop = (const Atom*)prop;
410 state = 0;
411
412 for (size_t x = 0; x < nitems; x++)
413 {
414 if (aprop[x] == xfc->NET_WM_STATE_MAXIMIZED_VERT)
415 state |= 0x01;
416
417 if (aprop[x] == xfc->NET_WM_STATE_MAXIMIZED_HORZ)
418 state |= 0x02;
419 }
420
421 if (state)
422 {
423 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4,
424 NET_WM_STATE_REMOVE, xfc->NET_WM_STATE_MAXIMIZED_VERT, 0,
425 0);
426 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4,
427 NET_WM_STATE_REMOVE, xfc->NET_WM_STATE_MAXIMIZED_HORZ, 0,
428 0);
429 xfc->savedMaximizedState = state;
430 }
431
432 XFree(prop);
433 }
434 }
435
436 width = xfc->vscreen.area.right - xfc->vscreen.area.left + 1;
437 height = xfc->vscreen.area.bottom - xfc->vscreen.area.top + 1;
438 DEBUG_X11("X window move and resize %dx%d@%" PRIu32 "x%" PRIu32 "", startX, startY,
439 width, height);
440 xf_ResizeDesktopWindow(xfc, window, WINPR_ASSERTING_INT_CAST(int, width),
441 WINPR_ASSERTING_INT_CAST(int, height));
442 LogDynAndXMoveWindow(xfc->log, xfc->display, window->handle, startX, startY);
443 }
444 else
445 {
446 xf_SetWindowDecorations(xfc, window->handle, window->decorations);
447 xf_ResizeDesktopWindow(xfc, window, WINPR_ASSERTING_INT_CAST(int, width),
448 WINPR_ASSERTING_INT_CAST(int, height));
449 LogDynAndXMoveWindow(xfc->log, xfc->display, window->handle, startX, startY);
450
451 if (xfc->fullscreenMonitors.top)
452 {
453 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_REMOVE,
454 xfc->fullscreenMonitors.top, 0, 0);
455 }
456
457 /* restore maximized state, if the window was maximized before setting fullscreen */
458 if (xfc->savedMaximizedState & 0x01)
459 {
460 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_ADD,
461 xfc->NET_WM_STATE_MAXIMIZED_VERT, 0, 0);
462 }
463
464 if (xfc->savedMaximizedState & 0x02)
465 {
466 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_ADD,
467 xfc->NET_WM_STATE_MAXIMIZED_HORZ, 0, 0);
468 }
469
470 xfc->savedMaximizedState = 0;
471 }
472 }
473}
474
475/* http://tronche.com/gui/x/xlib/window-information/XGetWindowProperty.html */
476
477BOOL xf_GetWindowProperty(xfContext* xfc, Window window, Atom property, int length,
478 unsigned long* nitems, unsigned long* bytes, BYTE** prop)
479{
480 int status = 0;
481 Atom actual_type = None;
482 int actual_format = 0;
483
484 WINPR_ASSERT(prop);
485
486 if (property == None)
487 return FALSE;
488
489 status = LogDynAndXGetWindowProperty(xfc->log, xfc->display, window, property, 0, length, False,
490 AnyPropertyType, &actual_type, &actual_format, nitems,
491 bytes, prop);
492
493 if (status != Success)
494 return FALSE;
495
496 if (actual_type == None)
497 {
498 WLog_DBG(TAG, "Property %lu does not exist", (unsigned long)property);
499 if (*prop)
500 XFree(*prop);
501 *prop = nullptr;
502 return FALSE;
503 }
504
505 return TRUE;
506}
507
508static BOOL xf_GetNumberOfDesktops(xfContext* xfc, Window root, unsigned* pval)
509{
510 unsigned long nitems = 0;
511 unsigned long bytes = 0;
512 BYTE* bprop = nullptr;
513
514 WINPR_ASSERT(xfc);
515 WINPR_ASSERT(pval);
516
517 const BOOL rc =
518 xf_GetWindowProperty(xfc, root, xfc->NET_NUMBER_OF_DESKTOPS, 1, &nitems, &bytes, &bprop);
519
520 long* prop = (long*)bprop;
521 *pval = 0;
522
523 BOOL res = FALSE;
524 if (rc)
525 {
526 if ((*prop >= 0) && (*prop <= UINT32_MAX))
527 {
528 *pval = (UINT32)*prop;
529 res = TRUE;
530 }
531 }
532 if (prop)
533 XFree(prop);
534 return res;
535}
536
537static BOOL xf_GetCurrentDesktop(xfContext* xfc, Window root)
538{
539 unsigned long nitems = 0;
540 unsigned long bytes = 0;
541 BYTE* bprop = nullptr;
542 unsigned max = 0;
543
544 if (!xf_GetNumberOfDesktops(xfc, root, &max))
545 return FALSE;
546 if (max < 1)
547 return FALSE;
548
549 const BOOL rc =
550 xf_GetWindowProperty(xfc, root, xfc->NET_CURRENT_DESKTOP, 1, &nitems, &bytes, &bprop);
551
552 long* prop = (long*)bprop;
553 xfc->current_desktop = 0;
554 if (rc)
555 xfc->current_desktop = (int)MIN(max - 1, *prop);
556 if (prop)
557 XFree(prop);
558 return rc;
559}
560
561static BOOL xf_GetWorkArea_NET_WORKAREA(xfContext* xfc, Window root)
562{
563 BOOL rc = FALSE;
564 unsigned long nitems = 0;
565 unsigned long bytes = 0;
566 BYTE* bprop = nullptr;
567
568 const BOOL status =
569 xf_GetWindowProperty(xfc, root, xfc->NET_WORKAREA, INT_MAX, &nitems, &bytes, &bprop);
570 long* prop = (long*)bprop;
571
572 if (!status)
573 goto fail;
574
575 if ((xfc->current_desktop * 4 + 3) >= (INT64)nitems)
576 goto fail;
577
578 xfc->workArea.x = (INT32)MIN(INT32_MAX, prop[xfc->current_desktop * 4 + 0]);
579 xfc->workArea.y = (INT32)MIN(INT32_MAX, prop[xfc->current_desktop * 4 + 1]);
580 xfc->workArea.width = (UINT32)MIN(UINT32_MAX, prop[xfc->current_desktop * 4 + 2]);
581 xfc->workArea.height = (UINT32)MIN(UINT32_MAX, prop[xfc->current_desktop * 4 + 3]);
582
583 rc = TRUE;
584fail:
585 XFree(prop);
586 return rc;
587}
588
589BOOL xf_GetWorkArea(xfContext* xfc)
590{
591 WINPR_ASSERT(xfc);
592
593 Window root = DefaultRootWindow(xfc->display);
594 (void)xf_GetCurrentDesktop(xfc, root);
595 return xf_GetWorkArea_NET_WORKAREA(xfc, root);
596}
597
598void xf_SetWindowDecorations(xfContext* xfc, Window window, BOOL show)
599{
600 PropMotifWmHints hints = { .decorations = (show) ? MWM_DECOR_ALL : 0,
601 .functions = MWM_FUNC_ALL,
602 .flags = MWM_HINTS_DECORATIONS | MWM_HINTS_FUNCTIONS,
603 .inputMode = 0,
604 .status = 0 };
605 WINPR_ASSERT(xfc);
606 LogDynAndXChangeProperty(xfc->log, xfc->display, window, xfc->MOTIF_WM_HINTS,
607 xfc->MOTIF_WM_HINTS, 32, PropModeReplace, (BYTE*)&hints,
608 PROP_MOTIF_WM_HINTS_ELEMENTS);
609}
610
611void xf_SetWindowUnlisted(xfContext* xfc, Window window)
612{
613 WINPR_ASSERT(xfc);
614 Atom window_state[] = { xfc->NET_WM_STATE_SKIP_PAGER, xfc->NET_WM_STATE_SKIP_TASKBAR };
615 LogDynAndXChangeProperty(xfc->log, xfc->display, window, xfc->NET_WM_STATE, XA_ATOM, 32,
616 PropModeReplace, (BYTE*)window_state, 2);
617}
618
619static void xf_SetWindowPID(xfContext* xfc, Window window, pid_t pid)
620{
621 Atom am_wm_pid = 0;
622
623 WINPR_ASSERT(xfc);
624 if (!pid)
625 pid = getpid();
626
627 am_wm_pid = xfc->NET_WM_PID;
628 LogDynAndXChangeProperty(xfc->log, xfc->display, window, am_wm_pid, XA_CARDINAL, 32,
629 PropModeReplace, (BYTE*)&pid, 1);
630}
631
632static const char* get_shm_id(void)
633{
634 static char shm_id[64];
635 (void)sprintf_s(shm_id, sizeof(shm_id), "/com.freerdp.xfreerdp.tsmf_%016X",
636 GetCurrentProcessId());
637 return shm_id;
638}
639
640Window xf_CreateDummyWindow(xfContext* xfc)
641{
642 return LogDynAndXCreateWindow(
643 xfc->log, xfc->display, RootWindowOfScreen(xfc->screen),
644 WINPR_ASSERTING_INT_CAST(int, xfc->workArea.x),
645 WINPR_ASSERTING_INT_CAST(int, xfc->workArea.y), 1, 1, 0, xfc->depth, InputOutput,
646 xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->attribs_mask), &xfc->attribs);
647}
648
649void xf_DestroyDummyWindow(xfContext* xfc, Window window)
650{
651 if (window)
652 LogDynAndXDestroyWindow(xfc->log, xfc->display, window);
653}
654
655xfWindow* xf_CreateDesktopWindow(xfContext* xfc, char* name, int width, int height)
656{
657 XEvent xevent = WINPR_C_ARRAY_INIT;
658 int input_mask = 0;
659 XClassHint* classHints = nullptr;
660 xfWindow* window = (xfWindow*)calloc(1, sizeof(xfWindow));
661
662 if (!window)
663 return nullptr;
664
665 rdpSettings* settings = xfc->common.context.settings;
666 WINPR_ASSERT(settings);
667
668 Window parentWindow = (Window)freerdp_settings_get_uint64(settings, FreeRDP_ParentWindowId);
669 window->width = width;
670 window->height = height;
671 window->decorations = xfc->decorations;
672 window->is_mapped = FALSE;
673 window->is_transient = FALSE;
674
675 WINPR_ASSERT(xfc->depth != 0);
676 window->handle = LogDynAndXCreateWindow(
677 xfc->log, xfc->display, RootWindowOfScreen(xfc->screen),
678 WINPR_ASSERTING_INT_CAST(int, xfc->workArea.x),
679 WINPR_ASSERTING_INT_CAST(int, xfc->workArea.y), xfc->workArea.width, xfc->workArea.height,
680 0, xfc->depth, InputOutput, xfc->visual,
681 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->attribs_mask), &xfc->attribs);
682 window->shmid = shm_open(get_shm_id(), (O_CREAT | O_RDWR), (S_IREAD | S_IWRITE));
683
684 if (window->shmid < 0)
685 {
686 DEBUG_X11("xf_CreateDesktopWindow: failed to get access to shared memory - shmget()\n");
687 }
688 else
689 {
690 int rc = ftruncate(window->shmid, sizeof(window->handle));
691 if (rc != 0)
692 {
693#ifdef WITH_DEBUG_X11
694 char ebuffer[256] = WINPR_C_ARRAY_INIT;
695 DEBUG_X11("ftruncate failed with %s [%d]", winpr_strerror(rc, ebuffer, sizeof(ebuffer)),
696 rc);
697#endif
698 }
699 else
700 {
701 void* mem = mmap(nullptr, sizeof(window->handle), PROT_READ | PROT_WRITE, MAP_SHARED,
702 window->shmid, 0);
703
704 if (mem == MAP_FAILED)
705 {
706 DEBUG_X11(
707 "xf_CreateDesktopWindow: failed to assign pointer to the memory address - "
708 "shmat()\n");
709 }
710 else
711 {
712 window->xfwin = mem;
713 *window->xfwin = window->handle;
714 }
715 }
716 }
717
718 classHints = XAllocClassHint();
719
720 if (classHints)
721 {
722 classHints->res_name = "xfreerdp";
723
724 char* res_class = nullptr;
725 const char* WmClass = freerdp_settings_get_string(settings, FreeRDP_WmClass);
726 if (WmClass)
727 res_class = _strdup(WmClass);
728 else
729 res_class = _strdup("xfreerdp");
730
731 classHints->res_class = res_class;
732 XSetClassHint(xfc->display, window->handle, classHints);
733 XFree(classHints);
734 free(res_class);
735 }
736
737 xf_ResizeDesktopWindow(xfc, window, width, height);
738 xf_SetWindowDecorations(xfc, window->handle, window->decorations);
739 xf_SetWindowPID(xfc, window->handle, 0);
740 input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
741 VisibilityChangeMask | FocusChangeMask | StructureNotifyMask | PointerMotionMask |
742 ExposureMask | PropertyChangeMask;
743
744 if (xfc->grab_keyboard)
745 input_mask |= EnterWindowMask | LeaveWindowMask;
746
747 LogDynAndXChangeProperty(xfc->log, xfc->display, window->handle, xfc->NET_WM_ICON, XA_CARDINAL,
748 32, PropModeReplace, (BYTE*)xf_icon_prop, ARRAYSIZE(xf_icon_prop));
749
750 if (parentWindow)
751 LogDynAndXReparentWindow(xfc->log, xfc->display, window->handle, parentWindow, 0, 0);
752
753 XSelectInput(xfc->display, window->handle, input_mask);
754 LogDynAndXClearWindow(xfc->log, xfc->display, window->handle);
755 xf_SetWindowTitleText(xfc, window->handle, name);
756 LogDynAndXMapWindow(xfc->log, xfc->display, window->handle);
757 if (xf_input_init(xfc, window->handle) < 0)
758 {
759 xf_DestroyDesktopWindow(xfc, window);
760 return nullptr;
761 }
762
763 /*
764 * NOTE: This must be done here to handle reparenting the window,
765 * so that we don't miss the event and hang waiting for the next one
766 */
767 do
768 {
769 XMaskEvent(xfc->display, VisibilityChangeMask, &xevent);
770 } while (xevent.type != VisibilityNotify);
771
772 /*
773 * The XCreateWindow call will start the window in the upper-left corner of our current
774 * monitor instead of the upper-left monitor for remote app mode (which uses all monitors).
775 * This extra call after the window is mapped will position the login window correctly
776 */
777 if (freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode))
778 {
779 LogDynAndXMoveWindow(xfc->log, xfc->display, window->handle, 0, 0);
780 }
781 else if ((freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosX) != UINT32_MAX) &&
782 (freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosY) != UINT32_MAX))
783 {
784 LogDynAndXMoveWindow(xfc->log, xfc->display, window->handle,
785 WINPR_ASSERTING_INT_CAST(
786 int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosX)),
787 WINPR_ASSERTING_INT_CAST(
788 int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosY)));
789 }
790
791 window->floatbar = xf_floatbar_new(xfc, window->handle, name,
792 freerdp_settings_get_uint32(settings, FreeRDP_Floatbar));
793
794 if (xfc->XWAYLAND_MAY_GRAB_KEYBOARD)
795 xf_SendClientEvent(xfc, window->handle, xfc->XWAYLAND_MAY_GRAB_KEYBOARD, 1, 1);
796
797 return window;
798}
799
800void xf_ResizeDesktopWindow(xfContext* xfc, xfWindow* window, int width, int height)
801{
802 XSizeHints* size_hints = nullptr;
803 rdpSettings* settings = nullptr;
804
805 if (!xfc || !window)
806 return;
807
808 settings = xfc->common.context.settings;
809 WINPR_ASSERT(settings);
810
811 if (!(size_hints = XAllocSizeHints()))
812 return;
813
814 size_hints->flags = PMinSize | PMaxSize | PWinGravity;
815 size_hints->win_gravity = NorthWestGravity;
816 size_hints->min_width = size_hints->min_height = 1;
817 size_hints->max_width = size_hints->max_height = 16384;
818 LogDynAndXResizeWindow(xfc->log, xfc->display, window->handle,
819 WINPR_ASSERTING_INT_CAST(uint32_t, width),
820 WINPR_ASSERTING_INT_CAST(uint32_t, height));
821#ifdef WITH_XRENDER
822
823 if (!freerdp_settings_get_bool(settings, FreeRDP_SmartSizing) &&
824 !freerdp_settings_get_bool(settings, FreeRDP_DynamicResolutionUpdate))
825#endif
826 {
827 if (!xfc->fullscreen)
828 {
829 /* min == max is an hint for the WM to indicate that the window should
830 * not be resizable */
831 size_hints->min_width = size_hints->max_width = width;
832 size_hints->min_height = size_hints->max_height = height;
833 }
834 }
835
836 XSetWMNormalHints(xfc->display, window->handle, size_hints);
837 XFree(size_hints);
838}
839
840void xf_DestroyDesktopWindow(xfContext* xfc, xfWindow* window)
841{
842 WINPR_ASSERT(xfc);
843
844 if (!window)
845 return;
846
847 if (xfc->window == window)
848 xfc->window = nullptr;
849
850 xf_floatbar_free(window->floatbar);
851
852 if (window->gc)
853 LogDynAndXFreeGC(xfc->log, xfc->display, window->gc);
854
855 if (window->handle)
856 {
857 LogDynAndXUnmapWindow(xfc->log, xfc->display, window->handle);
858 LogDynAndXDestroyWindow(xfc->log, xfc->display, window->handle);
859 }
860
861 if (window->xfwin)
862 munmap(window->xfwin, sizeof(*window->xfwin));
863
864 if (window->shmid >= 0)
865 close(window->shmid);
866
867 shm_unlink(get_shm_id());
868 window->xfwin = (Window*)-1;
869 window->shmid = -1;
870 free(window);
871}
872
873void xf_SetWindowStyle(xfContext* xfc, xfAppWindow* appWindow, UINT32 style, UINT32 ex_style)
874{
875 Atom window_type = 0;
876 BOOL redirect = FALSE;
877
878 window_type = xfc->NET_WM_WINDOW_TYPE_NORMAL;
879
880 if ((ex_style & WS_EX_NOACTIVATE) || (ex_style & WS_EX_TOOLWINDOW))
881 {
882 redirect = TRUE;
883 appWindow->is_transient = TRUE;
884 xf_SetWindowUnlisted(xfc, appWindow->handle);
885 window_type = xfc->NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
886 }
887 /*
888 * TOPMOST window that is not a tool window is treated like a regular window (i.e. task
889 * manager). Want to do this here, since the window may have type WS_POPUP
890 */
891 else if (ex_style & WS_EX_TOPMOST)
892 {
893 window_type = xfc->NET_WM_WINDOW_TYPE_NORMAL;
894 }
895
896 if (style & WS_POPUP)
897 {
898 window_type = xfc->NET_WM_WINDOW_TYPE_DIALOG;
899 /* this includes dialogs, popups, etc, that need to be full-fledged windows */
900
901 if (!((ex_style & WS_EX_DLGMODALFRAME) || (ex_style & WS_EX_LAYERED) ||
902 (style & WS_SYSMENU)))
903 {
904 appWindow->is_transient = TRUE;
905 redirect = TRUE;
906
907 xf_SetWindowUnlisted(xfc, appWindow->handle);
908 }
909 }
910
911 if (!(style == 0 && ex_style == 0))
912 {
913 xf_SetWindowActions(xfc, appWindow);
914 }
915
916 {
917 /*
918 * Tooltips and menu items should be unmanaged windows
919 * (called "override redirect" in X windows parlance)
920 * If they are managed, there are issues with window focus that
921 * cause the windows to behave improperly. For example, a mouse
922 * press will dismiss a drop-down menu because the RDP server
923 * sees that as a focus out event from the window owning the
924 * dropdown.
925 */
926 XSetWindowAttributes attrs = WINPR_C_ARRAY_INIT;
927 attrs.override_redirect = redirect ? True : False;
928 LogDynAndXChangeWindowAttributes(xfc->log, xfc->display, appWindow->handle,
929 CWOverrideRedirect, &attrs);
930 }
931
932 LogDynAndXChangeProperty(xfc->log, xfc->display, appWindow->handle, xfc->NET_WM_WINDOW_TYPE,
933 XA_ATOM, 32, PropModeReplace, (BYTE*)&window_type, 1);
934
935 const BOOL above = (ex_style & WS_EX_TOPMOST) != 0;
936 const BOOL transient = (style & WS_CHILD) == 0;
937
938 if (transient)
939 xf_XSetTransientForHint(
940 xfc, appWindow); // xf_XSetTransientForHint only sets the hint if there is a parent
941
942 xf_SendClientEvent(xfc, appWindow->handle, xfc->NET_WM_STATE, 4,
943 above ? NET_WM_STATE_ADD : NET_WM_STATE_REMOVE, xfc->NET_WM_STATE_ABOVE, 0,
944 0);
945}
946
947void xf_SetWindowActions(xfContext* xfc, xfAppWindow* appWindow)
948{
949 Atom allowed_actions[] = {
950 xfc->NET_WM_ACTION_CLOSE, xfc->NET_WM_ACTION_MINIMIZE,
951 xfc->NET_WM_ACTION_MOVE, xfc->NET_WM_ACTION_RESIZE,
952 xfc->NET_WM_ACTION_MAXIMIZE_HORZ, xfc->NET_WM_ACTION_MAXIMIZE_VERT,
953 xfc->NET_WM_ACTION_FULLSCREEN, xfc->NET_WM_ACTION_CHANGE_DESKTOP
954 };
955
956 if (!(appWindow->dwStyle & WS_SYSMENU))
957 allowed_actions[0] = 0;
958
959 if (!(appWindow->dwStyle & WS_MINIMIZEBOX))
960 allowed_actions[1] = 0;
961
962 if (!(appWindow->dwStyle & WS_SIZEBOX))
963 allowed_actions[3] = 0;
964
965 if (!(appWindow->dwStyle & WS_MAXIMIZEBOX))
966 {
967 allowed_actions[4] = 0;
968 allowed_actions[5] = 0;
969 allowed_actions[6] = 0;
970 }
971
972 LogDynAndXChangeProperty(xfc->log, xfc->display, appWindow->handle, xfc->NET_WM_ALLOWED_ACTIONS,
973 XA_ATOM, 32, PropModeReplace, (unsigned char*)&allowed_actions, 8);
974}
975
976void xf_SetWindowText(xfContext* xfc, xfAppWindow* appWindow, const char* name)
977{
978 xf_SetWindowTitleText(xfc, appWindow->handle, name);
979}
980
981static void xf_FixWindowCoordinates(xfContext* xfc, int* x, int* y, int* width, int* height)
982{
983 int vscreen_width = 0;
984 int vscreen_height = 0;
985 vscreen_width = xfc->vscreen.area.right - xfc->vscreen.area.left + 1;
986 vscreen_height = xfc->vscreen.area.bottom - xfc->vscreen.area.top + 1;
987
988 if (*x < xfc->vscreen.area.left)
989 {
990 *width += *x;
991 *x = xfc->vscreen.area.left;
992 }
993
994 if (*y < xfc->vscreen.area.top)
995 {
996 *height += *y;
997 *y = xfc->vscreen.area.top;
998 }
999
1000 if (*width > vscreen_width)
1001 {
1002 *width = vscreen_width;
1003 }
1004
1005 if (*height > vscreen_height)
1006 {
1007 *height = vscreen_height;
1008 }
1009
1010 if (*width < 1)
1011 {
1012 *width = 1;
1013 }
1014
1015 if (*height < 1)
1016 {
1017 *height = 1;
1018 }
1019}
1020
1021int xf_AppWindowInit(xfContext* xfc, xfAppWindow* appWindow)
1022{
1023 if (!xfc || !appWindow)
1024 return -1;
1025
1026 xf_SetWindowDecorations(xfc, appWindow->handle, appWindow->decorations);
1027 xf_SetWindowStyle(xfc, appWindow, appWindow->dwStyle, appWindow->dwExStyle);
1028 xf_SetWindowPID(xfc, appWindow->handle, 0);
1029 xf_ShowWindow(xfc, appWindow, WINDOW_SHOW);
1030 LogDynAndXClearWindow(xfc->log, xfc->display, appWindow->handle);
1031 LogDynAndXMapWindow(xfc->log, xfc->display, appWindow->handle);
1032 /* Move doesn't seem to work until window is mapped. */
1033 xf_MoveWindow(xfc, appWindow, appWindow->x, appWindow->y, appWindow->width, appWindow->height);
1034 xf_SetWindowText(xfc, appWindow, appWindow->title);
1035 return 1;
1036}
1037
1038BOOL xf_AppWindowCreate(xfContext* xfc, xfAppWindow* appWindow)
1039{
1040 XGCValues gcv = WINPR_C_ARRAY_INIT;
1041 int input_mask = 0;
1042 XWMHints* InputModeHint = nullptr;
1043 XClassHint* class_hints = nullptr;
1044 const rdpSettings* settings = nullptr;
1045
1046 WINPR_ASSERT(xfc);
1047 WINPR_ASSERT(appWindow);
1048
1049 settings = xfc->common.context.settings;
1050 WINPR_ASSERT(settings);
1051
1052 xf_FixWindowCoordinates(xfc, &appWindow->x, &appWindow->y, &appWindow->width,
1053 &appWindow->height);
1054 appWindow->shmid = -1;
1055 appWindow->decorations = FALSE;
1056 appWindow->fullscreen = FALSE;
1057 appWindow->local_move.state = LMS_NOT_ACTIVE;
1058 appWindow->is_mapped = FALSE;
1059 appWindow->is_transient = FALSE;
1060 appWindow->rail_state = 0;
1061 appWindow->maxVert = FALSE;
1062 appWindow->maxHorz = FALSE;
1063 appWindow->minimized = FALSE;
1064 appWindow->rail_ignore_configure = FALSE;
1065
1066 WINPR_ASSERT(xfc->depth != 0);
1067 appWindow->handle = LogDynAndXCreateWindow(
1068 xfc->log, xfc->display, RootWindowOfScreen(xfc->screen), appWindow->x, appWindow->y,
1069 WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->width),
1070 WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->height), 0, xfc->depth, InputOutput,
1071 xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->attribs_mask), &xfc->attribs);
1072
1073 if (!appWindow->handle)
1074 return FALSE;
1075
1076 appWindow->gc =
1077 LogDynAndXCreateGC(xfc->log, xfc->display, appWindow->handle, GCGraphicsExposures, &gcv);
1078
1079 if (!xf_AppWindowResize(xfc, appWindow))
1080 return FALSE;
1081
1082 class_hints = XAllocClassHint();
1083
1084 if (class_hints)
1085 {
1086 char* strclass = nullptr;
1087
1088 const char* WmClass = freerdp_settings_get_string(settings, FreeRDP_WmClass);
1089 if (WmClass)
1090 strclass = _strdup(WmClass);
1091 else
1092 {
1093 size_t size = 0;
1094 winpr_asprintf(&strclass, &size, "RAIL:%08" PRIX64 "", appWindow->windowId);
1095 }
1096 class_hints->res_class = strclass;
1097 class_hints->res_name = "RAIL";
1098 XSetClassHint(xfc->display, appWindow->handle, class_hints);
1099 XFree(class_hints);
1100 free(strclass);
1101 }
1102
1103 /* Set the input mode hint for the WM */
1104 InputModeHint = XAllocWMHints();
1105 InputModeHint->flags = (1L << 0);
1106 InputModeHint->input = True;
1107 XSetWMHints(xfc->display, appWindow->handle, InputModeHint);
1108 XFree(InputModeHint);
1109 XSetWMProtocols(xfc->display, appWindow->handle, &(xfc->WM_DELETE_WINDOW), 1);
1110 input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
1111 EnterWindowMask | LeaveWindowMask | PointerMotionMask | Button1MotionMask |
1112 Button2MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask |
1113 ButtonMotionMask | KeymapStateMask | ExposureMask | VisibilityChangeMask |
1114 StructureNotifyMask | SubstructureNotifyMask | SubstructureRedirectMask |
1115 FocusChangeMask | PropertyChangeMask | ColormapChangeMask | OwnerGrabButtonMask;
1116 XSelectInput(xfc->display, appWindow->handle, input_mask);
1117
1118 if (xfc->XWAYLAND_MAY_GRAB_KEYBOARD)
1119 xf_SendClientEvent(xfc, appWindow->handle, xfc->XWAYLAND_MAY_GRAB_KEYBOARD, 1, 1);
1120
1121 return TRUE;
1122}
1123
1124void xf_SetWindowMinMaxInfo(xfContext* xfc, xfAppWindow* appWindow, WINPR_ATTR_UNUSED int maxWidth,
1125 WINPR_ATTR_UNUSED int maxHeight, WINPR_ATTR_UNUSED int maxPosX,
1126 WINPR_ATTR_UNUSED int maxPosY, int minTrackWidth, int minTrackHeight,
1127 int maxTrackWidth, int maxTrackHeight)
1128{
1129 XSizeHints* size_hints = nullptr;
1130 size_hints = XAllocSizeHints();
1131
1132 if (size_hints)
1133 {
1134 size_hints->flags = PMinSize | PMaxSize | PResizeInc;
1135 size_hints->min_width = minTrackWidth;
1136 size_hints->min_height = minTrackHeight;
1137 size_hints->max_width = maxTrackWidth;
1138 size_hints->max_height = maxTrackHeight;
1139 /* to speedup window drawing we need to select optimal value for sizing step. */
1140 size_hints->width_inc = size_hints->height_inc = 1;
1141 XSetWMNormalHints(xfc->display, appWindow->handle, size_hints);
1142 XFree(size_hints);
1143 }
1144}
1145
1146void xf_StartLocalMoveSize(xfContext* xfc, xfAppWindow* appWindow, int direction, int x, int y)
1147{
1148 if (appWindow->local_move.state != LMS_NOT_ACTIVE)
1149 return;
1150
1151 /*
1152 * Save original mouse location relative to root. This will be needed
1153 * to end local move to RDP server and/or X server
1154 */
1155 appWindow->local_move.root_x = x;
1156 appWindow->local_move.root_y = y;
1157 appWindow->local_move.state = LMS_STARTING;
1158 appWindow->local_move.direction = direction;
1159
1160 xf_ungrab(xfc);
1161
1162 xf_SendClientEvent(
1163 xfc, appWindow->handle,
1164 xfc->NET_WM_MOVERESIZE, /* request X window manager to initiate a local move */
1165 5, /* 5 arguments to follow */
1166 x, /* x relative to root window */
1167 y, /* y relative to root window */
1168 direction, /* extended ICCM direction flag */
1169 1, /* simulated mouse button 1 */
1170 1); /* 1 == application request per extended ICCM */
1171}
1172
1173void xf_EndLocalMoveSize(xfContext* xfc, xfAppWindow* appWindow)
1174{
1175 if (appWindow->local_move.state == LMS_NOT_ACTIVE)
1176 return;
1177
1178 if (appWindow->local_move.state == LMS_STARTING)
1179 {
1180 /*
1181 * The move never was property started. This can happen due to race
1182 * conditions between the mouse button up and the communications to the
1183 * RDP server for local moves. We must cancel the X window manager move.
1184 * Per ICCM, the X client can ask to cancel an active move.
1185 */
1186 xf_SendClientEvent(
1187 xfc, appWindow->handle,
1188 xfc->NET_WM_MOVERESIZE, /* request X window manager to abort a local move */
1189 5, /* 5 arguments to follow */
1190 appWindow->local_move.root_x, /* x relative to root window */
1191 appWindow->local_move.root_y, /* y relative to root window */
1192 NET_WM_MOVERESIZE_CANCEL, /* extended ICCM direction flag */
1193 1, /* simulated mouse button 1 */
1194 1); /* 1 == application request per extended ICCM */
1195 }
1196
1197 appWindow->local_move.state = LMS_NOT_ACTIVE;
1198}
1199
1200void xf_MoveWindow(xfContext* xfc, xfAppWindow* appWindow, int x, int y, int width, int height)
1201{
1202 BOOL resize = FALSE;
1203
1204 if ((width * height) < 1)
1205 return;
1206
1207 if ((appWindow->width != width) || (appWindow->height != height))
1208 resize = TRUE;
1209
1210 if (appWindow->local_move.state == LMS_STARTING || appWindow->local_move.state == LMS_ACTIVE)
1211 return;
1212
1213 appWindow->x = x;
1214 appWindow->y = y;
1215 appWindow->width = width;
1216 appWindow->height = height;
1217
1218 if (resize)
1219 {
1220 if (!xf_AppWindowResize(xfc, appWindow))
1221 return;
1222
1223 LogDynAndXMoveResizeWindow(xfc->log, xfc->display, appWindow->handle, x, y,
1224 WINPR_ASSERTING_INT_CAST(uint32_t, width),
1225 WINPR_ASSERTING_INT_CAST(uint32_t, height));
1226 }
1227 else
1228 LogDynAndXMoveWindow(xfc->log, xfc->display, appWindow->handle, x, y);
1229
1230 xf_UpdateWindowArea(xfc, appWindow, 0, 0, width, height);
1231}
1232
1233void xf_ShowWindow(xfContext* xfc, xfAppWindow* appWindow, BYTE state)
1234{
1235 WINPR_ASSERT(xfc);
1236 WINPR_ASSERT(appWindow);
1237
1238 switch (state)
1239 {
1240 case WINDOW_HIDE:
1241 LogDynAndXWithdrawWindow(xfc->log, xfc->display, appWindow->handle, xfc->screen_number);
1242 break;
1243
1244 case WINDOW_SHOW_MINIMIZED:
1245 appWindow->minimized = TRUE;
1246 XIconifyWindow(xfc->display, appWindow->handle, xfc->screen_number);
1247 break;
1248
1249 case WINDOW_SHOW_MAXIMIZED:
1250 /* Set the window as maximized */
1251 appWindow->maxHorz = TRUE;
1252 appWindow->maxVert = TRUE;
1253 xf_SendClientEvent(xfc, appWindow->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_ADD,
1254 xfc->NET_WM_STATE_MAXIMIZED_VERT, xfc->NET_WM_STATE_MAXIMIZED_HORZ,
1255 0);
1256
1257 /*
1258 * This is a workaround for the case where the window is maximized locally before the
1259 * rail server is told to maximize the window, this appears to be a race condition where
1260 * the local window with incomplete data and once the window is actually maximized on
1261 * the server
1262 * - an update of the new areas may not happen. So, we simply to do a full update of the
1263 * entire window once the rail server notifies us that the window is now maximized.
1264 */
1265 if (appWindow->rail_state == WINDOW_SHOW_MAXIMIZED)
1266 {
1267 xf_UpdateWindowArea(xfc, appWindow, 0, 0,
1268 WINPR_ASSERTING_INT_CAST(int, appWindow->windowWidth),
1269 WINPR_ASSERTING_INT_CAST(int, appWindow->windowHeight));
1270 }
1271
1272 break;
1273
1274 case WINDOW_SHOW:
1275 /* Ensure the window is not maximized */
1276 xf_SendClientEvent(xfc, appWindow->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_REMOVE,
1277 xfc->NET_WM_STATE_MAXIMIZED_VERT, xfc->NET_WM_STATE_MAXIMIZED_HORZ,
1278 0);
1279
1280 /*
1281 * Ignore configure requests until both the Maximized properties have been processed
1282 * to prevent condition where WM overrides size of request due to one or both of these
1283 * properties still being set - which causes a position adjustment to be sent back to
1284 * the server thus causing the window to not return to its original size
1285 */
1286 if (appWindow->rail_state == WINDOW_SHOW_MAXIMIZED)
1287 appWindow->rail_ignore_configure = TRUE;
1288
1289 if (appWindow->is_transient)
1290 xf_SetWindowUnlisted(xfc, appWindow->handle);
1291
1292 LogDynAndXMapWindow(xfc->log, xfc->display, appWindow->handle);
1293 break;
1294 default:
1295 break;
1296 }
1297
1298 /* Save the current rail state of this window */
1299 appWindow->rail_state = state;
1300 LogDynAndXFlush(xfc->log, xfc->display);
1301}
1302
1303void xf_SetWindowRects(xfContext* xfc, xfAppWindow* appWindow, RECTANGLE_16* rects, int nrects)
1304{
1305 XRectangle* xrects = nullptr;
1306
1307 if (nrects < 1)
1308 return;
1309
1310#ifdef WITH_XEXT
1311 xrects = (XRectangle*)calloc(WINPR_ASSERTING_INT_CAST(uint32_t, nrects), sizeof(XRectangle));
1312
1313 for (int i = 0; i < nrects; i++)
1314 {
1315 xrects[i].x = WINPR_ASSERTING_INT_CAST(short, rects[i].left);
1316 xrects[i].y = WINPR_ASSERTING_INT_CAST(short, rects[i].top);
1317 xrects[i].width = WINPR_ASSERTING_INT_CAST(unsigned short, rects[i].right - rects[i].left);
1318 xrects[i].height = WINPR_ASSERTING_INT_CAST(unsigned short, rects[i].bottom - rects[i].top);
1319 }
1320
1321 XShapeCombineRectangles(xfc->display, appWindow->handle, ShapeBounding, 0, 0, xrects, nrects,
1322 ShapeSet, 0);
1323 free(xrects);
1324#endif
1325}
1326
1327void xf_SetWindowVisibilityRects(xfContext* xfc, xfAppWindow* appWindow, UINT32 rectsOffsetX,
1328 UINT32 rectsOffsetY, RECTANGLE_16* rects, int nrects)
1329{
1330 XRectangle* xrects = nullptr;
1331
1332 if (nrects < 1)
1333 return;
1334
1335#ifdef WITH_XEXT
1336 xrects = (XRectangle*)calloc(WINPR_ASSERTING_INT_CAST(uint32_t, nrects), sizeof(XRectangle));
1337
1338 for (int i = 0; i < nrects; i++)
1339 {
1340 xrects[i].x = WINPR_ASSERTING_INT_CAST(short, rects[i].left);
1341 xrects[i].y = WINPR_ASSERTING_INT_CAST(short, rects[i].top);
1342 xrects[i].width = WINPR_ASSERTING_INT_CAST(unsigned short, rects[i].right - rects[i].left);
1343 xrects[i].height = WINPR_ASSERTING_INT_CAST(unsigned short, rects[i].bottom - rects[i].top);
1344 }
1345
1346 XShapeCombineRectangles(
1347 xfc->display, appWindow->handle, ShapeBounding, WINPR_ASSERTING_INT_CAST(int, rectsOffsetX),
1348 WINPR_ASSERTING_INT_CAST(int, rectsOffsetY), xrects, nrects, ShapeSet, 0);
1349 free(xrects);
1350#endif
1351}
1352
1353void xf_UpdateWindowArea(xfContext* xfc, xfAppWindow* appWindow, int x, int y, int width,
1354 int height)
1355{
1356 int ax = 0;
1357 int ay = 0;
1358 const rdpSettings* settings = nullptr;
1359
1360 WINPR_ASSERT(xfc);
1361
1362 settings = xfc->common.context.settings;
1363 WINPR_ASSERT(settings);
1364
1365 if (appWindow == nullptr)
1366 return;
1367
1368 if (appWindow->surfaceId < UINT16_MAX)
1369 return;
1370
1371 ax = x + appWindow->windowOffsetX;
1372 ay = y + appWindow->windowOffsetY;
1373
1374 if (ax + width > appWindow->windowOffsetX + appWindow->width)
1375 width = (appWindow->windowOffsetX + appWindow->width - 1) - ax;
1376
1377 if (ay + height > appWindow->windowOffsetY + appWindow->height)
1378 height = (appWindow->windowOffsetY + appWindow->height - 1) - ay;
1379
1380 if (freerdp_settings_get_bool(settings, FreeRDP_SoftwareGdi))
1381 {
1382 LogDynAndXPutImage(xfc->log, xfc->display, appWindow->pixmap, appWindow->gc, xfc->image, ax,
1383 ay, x, y, WINPR_ASSERTING_INT_CAST(uint32_t, width),
1384 WINPR_ASSERTING_INT_CAST(uint32_t, height));
1385 }
1386
1387 LogDynAndXCopyArea(xfc->log, xfc->display, appWindow->pixmap, appWindow->handle, appWindow->gc,
1388 x, y, WINPR_ASSERTING_INT_CAST(uint32_t, width),
1389 WINPR_ASSERTING_INT_CAST(uint32_t, height), x, y);
1390 LogDynAndXFlush(xfc->log, xfc->display);
1391}
1392
1393void xf_AppWindowDestroyImage(xfAppWindow* appWindow)
1394{
1395 WINPR_ASSERT(appWindow);
1396 if (appWindow->image)
1397 {
1398 appWindow->image->data = nullptr;
1399 XDestroyImage(appWindow->image);
1400 appWindow->image = nullptr;
1401 }
1402}
1403
1404void xf_DestroyWindow(xfContext* xfc, xfAppWindow* appWindow)
1405{
1406 if (!appWindow)
1407 return;
1408
1409 if (xfc->appWindow == appWindow)
1410 xfc->appWindow = nullptr;
1411
1412 if (appWindow->gc)
1413 LogDynAndXFreeGC(xfc->log, xfc->display, appWindow->gc);
1414
1415 if (appWindow->pixmap)
1416 LogDynAndXFreePixmap(xfc->log, xfc->display, appWindow->pixmap);
1417
1418 xf_AppWindowDestroyImage(appWindow);
1419
1420 if (appWindow->handle)
1421 {
1422 LogDynAndXUnmapWindow(xfc->log, xfc->display, appWindow->handle);
1423 LogDynAndXDestroyWindow(xfc->log, xfc->display, appWindow->handle);
1424 }
1425
1426 if (appWindow->xfwin)
1427 munmap(nullptr, sizeof(*appWindow->xfwin));
1428
1429 if (appWindow->shmid >= 0)
1430 close(appWindow->shmid);
1431
1432 shm_unlink(get_shm_id());
1433 appWindow->xfwin = (Window*)-1;
1434 appWindow->shmid = -1;
1435 free(appWindow->title);
1436 free(appWindow->windowRects);
1437 free(appWindow->visibilityRects);
1438 free(appWindow);
1439}
1440
1441static xfAppWindow* get_windowUnlocked(xfContext* xfc, UINT64 id)
1442{
1443 WINPR_ASSERT(xfc);
1444 return HashTable_GetItemValue(xfc->railWindows, &id);
1445}
1446
1447xfAppWindow* xf_rail_get_windowFrom(xfContext* xfc, UINT64 id, BOOL alreadyLocked, const char* file,
1448 const char* fkt, size_t line)
1449{
1450 if (!xfc)
1451 return nullptr;
1452
1453 if (!xfc->railWindows)
1454 return nullptr;
1455
1456 if (!alreadyLocked)
1457 xfAppWindowsLockFrom(xfc, file, fkt, line);
1458
1459 xfAppWindow* window = get_windowUnlocked(xfc, id);
1460
1461 if (!window && !alreadyLocked)
1462 xfAppWindowsUnlockFrom(xfc, file, fkt, line);
1463
1464 return window;
1465}
1466
1467xfAppWindow* xf_AppWindowFromX11WindowFrom(xfContext* xfc, Window wnd, const char* file,
1468 const char* fkt, size_t line)
1469{
1470 ULONG_PTR* pKeys = nullptr;
1471
1472 WINPR_ASSERT(xfc);
1473 if (!xfc->railWindows)
1474 return nullptr;
1475
1476 xfAppWindowsLockFrom(xfc, file, fkt, line);
1477 size_t count = HashTable_GetKeys(xfc->railWindows, &pKeys);
1478
1479 for (size_t index = 0; index < count; index++)
1480 {
1481 xfAppWindow* appWindow = get_windowUnlocked(xfc, *(UINT64*)pKeys[index]);
1482
1483 if (!appWindow)
1484 {
1485 xfAppWindowsUnlockFrom(xfc, file, fkt, line);
1486 free(pKeys);
1487 return nullptr;
1488 }
1489
1490 if (appWindow->handle == wnd)
1491 {
1492 free(pKeys);
1493 return appWindow;
1494 }
1495 }
1496
1497 xfAppWindowsUnlockFrom(xfc, file, fkt, line);
1498 free(pKeys);
1499 return nullptr;
1500}
1501
1502UINT xf_AppUpdateWindowFromSurface(xfContext* xfc, gdiGfxSurface* surface)
1503{
1504 XImage* image = nullptr;
1505 UINT rc = ERROR_INTERNAL_ERROR;
1506
1507 WINPR_ASSERT(xfc);
1508 WINPR_ASSERT(surface);
1509
1510 xfAppWindow* appWindow = xf_rail_get_window(xfc, surface->windowId, FALSE);
1511 if (!appWindow)
1512 {
1513 WLog_VRB(TAG, "Failed to find a window for id=0x%08" PRIx64, surface->windowId);
1514 return CHANNEL_RC_OK;
1515 }
1516
1517 const BOOL swGdi = freerdp_settings_get_bool(xfc->common.context.settings, FreeRDP_SoftwareGdi);
1518 UINT32 nrects = 0;
1519 const RECTANGLE_16* rects = region16_rects(&surface->invalidRegion, &nrects);
1520
1521 if (swGdi)
1522 {
1523 if (appWindow->surfaceId != surface->surfaceId)
1524 {
1525 xf_AppWindowDestroyImage(appWindow);
1526 appWindow->surfaceId = surface->surfaceId;
1527 }
1528 if (appWindow->width != (INT64)surface->width)
1529 xf_AppWindowDestroyImage(appWindow);
1530 if (appWindow->height != (INT64)surface->height)
1531 xf_AppWindowDestroyImage(appWindow);
1532
1533 if (!appWindow->image)
1534 {
1535 WINPR_ASSERT(xfc->depth != 0);
1536 appWindow->image = LogDynAndXCreateImage(
1537 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
1538 ZPixmap, 0, (char*)surface->data, surface->width, surface->height,
1539 xfc->scanline_pad, WINPR_ASSERTING_INT_CAST(int, surface->scanline));
1540 if (!appWindow->image)
1541 {
1542 WLog_WARN(TAG,
1543 "Failed create a XImage[%" PRIu32 "x%" PRIu32 ", scanline=%" PRIu32
1544 ", bpp=%" PRId32 "] for window id=0x%08" PRIx64,
1545 surface->width, surface->height, surface->scanline, xfc->depth,
1546 surface->windowId);
1547 goto fail;
1548 }
1549 appWindow->image->byte_order = LSBFirst;
1550 appWindow->image->bitmap_bit_order = LSBFirst;
1551 }
1552
1553 image = appWindow->image;
1554 }
1555 else
1556 {
1557 xfGfxSurface* xfSurface = (xfGfxSurface*)surface;
1558 image = xfSurface->image;
1559 }
1560
1561 for (UINT32 x = 0; x < nrects; x++)
1562 {
1563 const RECTANGLE_16* rect = &rects[x];
1564 const UINT32 width = rect->right - rect->left;
1565 const UINT32 height = rect->bottom - rect->top;
1566
1567 LogDynAndXPutImage(xfc->log, xfc->display, appWindow->pixmap, appWindow->gc, image,
1568 rect->left, rect->top, rect->left, rect->top, width, height);
1569
1570 LogDynAndXCopyArea(xfc->log, xfc->display, appWindow->pixmap, appWindow->handle,
1571 appWindow->gc, rect->left, rect->top, width, height, rect->left,
1572 rect->top);
1573 }
1574
1575 rc = CHANNEL_RC_OK;
1576fail:
1577 xf_rail_return_window(appWindow, FALSE);
1578 LogDynAndXFlush(xfc->log, xfc->display);
1579
1580 return rc;
1581}
1582
1583BOOL xf_AppWindowResize(xfContext* xfc, xfAppWindow* appWindow)
1584{
1585 WINPR_ASSERT(xfc);
1586 WINPR_ASSERT(appWindow);
1587
1588 if (appWindow->pixmap != 0)
1589 LogDynAndXFreePixmap(xfc->log, xfc->display, appWindow->pixmap);
1590
1591 WINPR_ASSERT(xfc->depth != 0);
1592 appWindow->pixmap = LogDynAndXCreatePixmap(
1593 xfc->log, xfc->display, xfc->drawable, WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->width),
1594 WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->height),
1595 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth));
1596 xf_AppWindowDestroyImage(appWindow);
1597
1598 return appWindow->pixmap != 0;
1599}
1600
1601void xf_XSetTransientForHint(xfContext* xfc, xfAppWindow* window)
1602{
1603 WINPR_ASSERT(xfc);
1604 WINPR_ASSERT(window);
1605
1606 if (window->ownerWindowId == 0)
1607 return;
1608
1609 xfAppWindow* parent = xf_rail_get_window(xfc, window->ownerWindowId, TRUE);
1610 if (!parent)
1611 return;
1612
1613 (void)LogDynAndXSetTransientForHint(xfc->log, xfc->display, window->handle, parent->handle);
1614 xf_rail_return_window(parent, TRUE);
1615}
1616
1617void xfAppWindowsLockFrom(xfContext* xfc, WINPR_ATTR_UNUSED const char* file,
1618 WINPR_ATTR_UNUSED const char* fkt, WINPR_ATTR_UNUSED size_t line)
1619{
1620 WINPR_ASSERT(xfc);
1621
1622#if defined(WITH_VERBOSE_WINPR_ASSERT)
1623 const DWORD level = WLOG_TRACE;
1624 if (WLog_IsLevelActive(xfc->log, level))
1625 WLog_PrintTextMessage(xfc->log, level, line, file, fkt, "[rails] locking [%s]", fkt);
1626#endif
1627
1628 xf_lock_x11(xfc);
1629 HashTable_Lock(xfc->railWindows);
1630
1631#if defined(WITH_VERBOSE_WINPR_ASSERT)
1632 WINPR_ASSERT(!xfc->isRailWindowsLocked);
1633 xfc->isRailWindowsLocked = TRUE;
1634#endif
1635}
1636
1637void xfAppWindowsUnlockFrom(xfContext* xfc, WINPR_ATTR_UNUSED const char* file,
1638 WINPR_ATTR_UNUSED const char* fkt, WINPR_ATTR_UNUSED size_t line)
1639{
1640 WINPR_ASSERT(xfc);
1641
1642#if defined(WITH_VERBOSE_WINPR_ASSERT)
1643 const DWORD level = WLOG_TRACE;
1644 if (WLog_IsLevelActive(xfc->log, level))
1645 WLog_PrintTextMessage(xfc->log, level, line, file, fkt, "[rails] unocking [%s]", fkt);
1646
1647 WINPR_ASSERT(xfc->isRailWindowsLocked);
1648 xfc->isRailWindowsLocked = FALSE;
1649#endif
1650
1651 HashTable_Unlock(xfc->railWindows);
1652 xf_unlock_x11(xfc);
1653}
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.