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 xf_input_init(xfc, window->handle);
758
759 /*
760 * NOTE: This must be done here to handle reparenting the window,
761 * so that we don't miss the event and hang waiting for the next one
762 */
763 do
764 {
765 XMaskEvent(xfc->display, VisibilityChangeMask, &xevent);
766 } while (xevent.type != VisibilityNotify);
767
768 /*
769 * The XCreateWindow call will start the window in the upper-left corner of our current
770 * monitor instead of the upper-left monitor for remote app mode (which uses all monitors).
771 * This extra call after the window is mapped will position the login window correctly
772 */
773 if (freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode))
774 {
775 LogDynAndXMoveWindow(xfc->log, xfc->display, window->handle, 0, 0);
776 }
777 else if ((freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosX) != UINT32_MAX) &&
778 (freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosY) != UINT32_MAX))
779 {
780 LogDynAndXMoveWindow(xfc->log, xfc->display, window->handle,
781 WINPR_ASSERTING_INT_CAST(
782 int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosX)),
783 WINPR_ASSERTING_INT_CAST(
784 int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopPosY)));
785 }
786
787 window->floatbar = xf_floatbar_new(xfc, window->handle, name,
788 freerdp_settings_get_uint32(settings, FreeRDP_Floatbar));
789
790 if (xfc->XWAYLAND_MAY_GRAB_KEYBOARD)
791 xf_SendClientEvent(xfc, window->handle, xfc->XWAYLAND_MAY_GRAB_KEYBOARD, 1, 1);
792
793 return window;
794}
795
796void xf_ResizeDesktopWindow(xfContext* xfc, xfWindow* window, int width, int height)
797{
798 XSizeHints* size_hints = nullptr;
799 rdpSettings* settings = nullptr;
800
801 if (!xfc || !window)
802 return;
803
804 settings = xfc->common.context.settings;
805 WINPR_ASSERT(settings);
806
807 if (!(size_hints = XAllocSizeHints()))
808 return;
809
810 size_hints->flags = PMinSize | PMaxSize | PWinGravity;
811 size_hints->win_gravity = NorthWestGravity;
812 size_hints->min_width = size_hints->min_height = 1;
813 size_hints->max_width = size_hints->max_height = 16384;
814 LogDynAndXResizeWindow(xfc->log, xfc->display, window->handle,
815 WINPR_ASSERTING_INT_CAST(uint32_t, width),
816 WINPR_ASSERTING_INT_CAST(uint32_t, height));
817#ifdef WITH_XRENDER
818
819 if (!freerdp_settings_get_bool(settings, FreeRDP_SmartSizing) &&
820 !freerdp_settings_get_bool(settings, FreeRDP_DynamicResolutionUpdate))
821#endif
822 {
823 if (!xfc->fullscreen)
824 {
825 /* min == max is an hint for the WM to indicate that the window should
826 * not be resizable */
827 size_hints->min_width = size_hints->max_width = width;
828 size_hints->min_height = size_hints->max_height = height;
829 }
830 }
831
832 XSetWMNormalHints(xfc->display, window->handle, size_hints);
833 XFree(size_hints);
834}
835
836void xf_DestroyDesktopWindow(xfContext* xfc, xfWindow* window)
837{
838 if (!window)
839 return;
840
841 if (xfc->window == window)
842 xfc->window = nullptr;
843
844 xf_floatbar_free(window->floatbar);
845
846 if (window->gc)
847 LogDynAndXFreeGC(xfc->log, xfc->display, window->gc);
848
849 if (window->handle)
850 {
851 LogDynAndXUnmapWindow(xfc->log, xfc->display, window->handle);
852 LogDynAndXDestroyWindow(xfc->log, xfc->display, window->handle);
853 }
854
855 if (window->xfwin)
856 munmap(nullptr, sizeof(*window->xfwin));
857
858 if (window->shmid >= 0)
859 close(window->shmid);
860
861 shm_unlink(get_shm_id());
862 window->xfwin = (Window*)-1;
863 window->shmid = -1;
864 free(window);
865}
866
867void xf_SetWindowStyle(xfContext* xfc, xfAppWindow* appWindow, UINT32 style, UINT32 ex_style)
868{
869 Atom window_type = 0;
870 BOOL redirect = FALSE;
871
872 window_type = xfc->NET_WM_WINDOW_TYPE_NORMAL;
873
874 if ((ex_style & WS_EX_NOACTIVATE) || (ex_style & WS_EX_TOOLWINDOW))
875 {
876 redirect = TRUE;
877 appWindow->is_transient = TRUE;
878 xf_SetWindowUnlisted(xfc, appWindow->handle);
879 window_type = xfc->NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
880 }
881 /*
882 * TOPMOST window that is not a tool window is treated like a regular window (i.e. task
883 * manager). Want to do this here, since the window may have type WS_POPUP
884 */
885 else if (ex_style & WS_EX_TOPMOST)
886 {
887 window_type = xfc->NET_WM_WINDOW_TYPE_NORMAL;
888 }
889
890 if (style & WS_POPUP)
891 {
892 window_type = xfc->NET_WM_WINDOW_TYPE_DIALOG;
893 /* this includes dialogs, popups, etc, that need to be full-fledged windows */
894
895 if (!((ex_style & WS_EX_DLGMODALFRAME) || (ex_style & WS_EX_LAYERED) ||
896 (style & WS_SYSMENU)))
897 {
898 appWindow->is_transient = TRUE;
899 redirect = TRUE;
900
901 xf_SetWindowUnlisted(xfc, appWindow->handle);
902 }
903 }
904
905 if (!(style == 0 && ex_style == 0))
906 {
907 xf_SetWindowActions(xfc, appWindow);
908 }
909
910 {
911 /*
912 * Tooltips and menu items should be unmanaged windows
913 * (called "override redirect" in X windows parlance)
914 * If they are managed, there are issues with window focus that
915 * cause the windows to behave improperly. For example, a mouse
916 * press will dismiss a drop-down menu because the RDP server
917 * sees that as a focus out event from the window owning the
918 * dropdown.
919 */
920 XSetWindowAttributes attrs = WINPR_C_ARRAY_INIT;
921 attrs.override_redirect = redirect ? True : False;
922 LogDynAndXChangeWindowAttributes(xfc->log, xfc->display, appWindow->handle,
923 CWOverrideRedirect, &attrs);
924 }
925
926 LogDynAndXChangeProperty(xfc->log, xfc->display, appWindow->handle, xfc->NET_WM_WINDOW_TYPE,
927 XA_ATOM, 32, PropModeReplace, (BYTE*)&window_type, 1);
928
929 const BOOL above = (ex_style & WS_EX_TOPMOST) != 0;
930 const BOOL transient = (style & WS_CHILD) == 0;
931
932 if (transient)
933 xf_XSetTransientForHint(
934 xfc, appWindow); // xf_XSetTransientForHint only sets the hint if there is a parent
935
936 xf_SendClientEvent(xfc, appWindow->handle, xfc->NET_WM_STATE, 4,
937 above ? NET_WM_STATE_ADD : NET_WM_STATE_REMOVE, xfc->NET_WM_STATE_ABOVE, 0,
938 0);
939}
940
941void xf_SetWindowActions(xfContext* xfc, xfAppWindow* appWindow)
942{
943 Atom allowed_actions[] = {
944 xfc->NET_WM_ACTION_CLOSE, xfc->NET_WM_ACTION_MINIMIZE,
945 xfc->NET_WM_ACTION_MOVE, xfc->NET_WM_ACTION_RESIZE,
946 xfc->NET_WM_ACTION_MAXIMIZE_HORZ, xfc->NET_WM_ACTION_MAXIMIZE_VERT,
947 xfc->NET_WM_ACTION_FULLSCREEN, xfc->NET_WM_ACTION_CHANGE_DESKTOP
948 };
949
950 if (!(appWindow->dwStyle & WS_SYSMENU))
951 allowed_actions[0] = 0;
952
953 if (!(appWindow->dwStyle & WS_MINIMIZEBOX))
954 allowed_actions[1] = 0;
955
956 if (!(appWindow->dwStyle & WS_SIZEBOX))
957 allowed_actions[3] = 0;
958
959 if (!(appWindow->dwStyle & WS_MAXIMIZEBOX))
960 {
961 allowed_actions[4] = 0;
962 allowed_actions[5] = 0;
963 allowed_actions[6] = 0;
964 }
965
966 LogDynAndXChangeProperty(xfc->log, xfc->display, appWindow->handle, xfc->NET_WM_ALLOWED_ACTIONS,
967 XA_ATOM, 32, PropModeReplace, (unsigned char*)&allowed_actions, 8);
968}
969
970void xf_SetWindowText(xfContext* xfc, xfAppWindow* appWindow, const char* name)
971{
972 xf_SetWindowTitleText(xfc, appWindow->handle, name);
973}
974
975static void xf_FixWindowCoordinates(xfContext* xfc, int* x, int* y, int* width, int* height)
976{
977 int vscreen_width = 0;
978 int vscreen_height = 0;
979 vscreen_width = xfc->vscreen.area.right - xfc->vscreen.area.left + 1;
980 vscreen_height = xfc->vscreen.area.bottom - xfc->vscreen.area.top + 1;
981
982 if (*x < xfc->vscreen.area.left)
983 {
984 *width += *x;
985 *x = xfc->vscreen.area.left;
986 }
987
988 if (*y < xfc->vscreen.area.top)
989 {
990 *height += *y;
991 *y = xfc->vscreen.area.top;
992 }
993
994 if (*width > vscreen_width)
995 {
996 *width = vscreen_width;
997 }
998
999 if (*height > vscreen_height)
1000 {
1001 *height = vscreen_height;
1002 }
1003
1004 if (*width < 1)
1005 {
1006 *width = 1;
1007 }
1008
1009 if (*height < 1)
1010 {
1011 *height = 1;
1012 }
1013}
1014
1015int xf_AppWindowInit(xfContext* xfc, xfAppWindow* appWindow)
1016{
1017 if (!xfc || !appWindow)
1018 return -1;
1019
1020 xf_SetWindowDecorations(xfc, appWindow->handle, appWindow->decorations);
1021 xf_SetWindowStyle(xfc, appWindow, appWindow->dwStyle, appWindow->dwExStyle);
1022 xf_SetWindowPID(xfc, appWindow->handle, 0);
1023 xf_ShowWindow(xfc, appWindow, WINDOW_SHOW);
1024 LogDynAndXClearWindow(xfc->log, xfc->display, appWindow->handle);
1025 LogDynAndXMapWindow(xfc->log, xfc->display, appWindow->handle);
1026 /* Move doesn't seem to work until window is mapped. */
1027 xf_MoveWindow(xfc, appWindow, appWindow->x, appWindow->y, appWindow->width, appWindow->height);
1028 xf_SetWindowText(xfc, appWindow, appWindow->title);
1029 return 1;
1030}
1031
1032BOOL xf_AppWindowCreate(xfContext* xfc, xfAppWindow* appWindow)
1033{
1034 XGCValues gcv = WINPR_C_ARRAY_INIT;
1035 int input_mask = 0;
1036 XWMHints* InputModeHint = nullptr;
1037 XClassHint* class_hints = nullptr;
1038 const rdpSettings* settings = nullptr;
1039
1040 WINPR_ASSERT(xfc);
1041 WINPR_ASSERT(appWindow);
1042
1043 settings = xfc->common.context.settings;
1044 WINPR_ASSERT(settings);
1045
1046 xf_FixWindowCoordinates(xfc, &appWindow->x, &appWindow->y, &appWindow->width,
1047 &appWindow->height);
1048 appWindow->shmid = -1;
1049 appWindow->decorations = FALSE;
1050 appWindow->fullscreen = FALSE;
1051 appWindow->local_move.state = LMS_NOT_ACTIVE;
1052 appWindow->is_mapped = FALSE;
1053 appWindow->is_transient = FALSE;
1054 appWindow->rail_state = 0;
1055 appWindow->maxVert = FALSE;
1056 appWindow->maxHorz = FALSE;
1057 appWindow->minimized = FALSE;
1058 appWindow->rail_ignore_configure = FALSE;
1059
1060 WINPR_ASSERT(xfc->depth != 0);
1061 appWindow->handle = LogDynAndXCreateWindow(
1062 xfc->log, xfc->display, RootWindowOfScreen(xfc->screen), appWindow->x, appWindow->y,
1063 WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->width),
1064 WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->height), 0, xfc->depth, InputOutput,
1065 xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->attribs_mask), &xfc->attribs);
1066
1067 if (!appWindow->handle)
1068 return FALSE;
1069
1070 appWindow->gc =
1071 LogDynAndXCreateGC(xfc->log, xfc->display, appWindow->handle, GCGraphicsExposures, &gcv);
1072
1073 if (!xf_AppWindowResize(xfc, appWindow))
1074 return FALSE;
1075
1076 class_hints = XAllocClassHint();
1077
1078 if (class_hints)
1079 {
1080 char* strclass = nullptr;
1081
1082 const char* WmClass = freerdp_settings_get_string(settings, FreeRDP_WmClass);
1083 if (WmClass)
1084 strclass = _strdup(WmClass);
1085 else
1086 {
1087 size_t size = 0;
1088 winpr_asprintf(&strclass, &size, "RAIL:%08" PRIX64 "", appWindow->windowId);
1089 }
1090 class_hints->res_class = strclass;
1091 class_hints->res_name = "RAIL";
1092 XSetClassHint(xfc->display, appWindow->handle, class_hints);
1093 XFree(class_hints);
1094 free(strclass);
1095 }
1096
1097 /* Set the input mode hint for the WM */
1098 InputModeHint = XAllocWMHints();
1099 InputModeHint->flags = (1L << 0);
1100 InputModeHint->input = True;
1101 XSetWMHints(xfc->display, appWindow->handle, InputModeHint);
1102 XFree(InputModeHint);
1103 XSetWMProtocols(xfc->display, appWindow->handle, &(xfc->WM_DELETE_WINDOW), 1);
1104 input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
1105 EnterWindowMask | LeaveWindowMask | PointerMotionMask | Button1MotionMask |
1106 Button2MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask |
1107 ButtonMotionMask | KeymapStateMask | ExposureMask | VisibilityChangeMask |
1108 StructureNotifyMask | SubstructureNotifyMask | SubstructureRedirectMask |
1109 FocusChangeMask | PropertyChangeMask | ColormapChangeMask | OwnerGrabButtonMask;
1110 XSelectInput(xfc->display, appWindow->handle, input_mask);
1111
1112 if (xfc->XWAYLAND_MAY_GRAB_KEYBOARD)
1113 xf_SendClientEvent(xfc, appWindow->handle, xfc->XWAYLAND_MAY_GRAB_KEYBOARD, 1, 1);
1114
1115 return TRUE;
1116}
1117
1118void xf_SetWindowMinMaxInfo(xfContext* xfc, xfAppWindow* appWindow, WINPR_ATTR_UNUSED int maxWidth,
1119 WINPR_ATTR_UNUSED int maxHeight, WINPR_ATTR_UNUSED int maxPosX,
1120 WINPR_ATTR_UNUSED int maxPosY, int minTrackWidth, int minTrackHeight,
1121 int maxTrackWidth, int maxTrackHeight)
1122{
1123 XSizeHints* size_hints = nullptr;
1124 size_hints = XAllocSizeHints();
1125
1126 if (size_hints)
1127 {
1128 size_hints->flags = PMinSize | PMaxSize | PResizeInc;
1129 size_hints->min_width = minTrackWidth;
1130 size_hints->min_height = minTrackHeight;
1131 size_hints->max_width = maxTrackWidth;
1132 size_hints->max_height = maxTrackHeight;
1133 /* to speedup window drawing we need to select optimal value for sizing step. */
1134 size_hints->width_inc = size_hints->height_inc = 1;
1135 XSetWMNormalHints(xfc->display, appWindow->handle, size_hints);
1136 XFree(size_hints);
1137 }
1138}
1139
1140void xf_StartLocalMoveSize(xfContext* xfc, xfAppWindow* appWindow, int direction, int x, int y)
1141{
1142 if (appWindow->local_move.state != LMS_NOT_ACTIVE)
1143 return;
1144
1145 /*
1146 * Save original mouse location relative to root. This will be needed
1147 * to end local move to RDP server and/or X server
1148 */
1149 appWindow->local_move.root_x = x;
1150 appWindow->local_move.root_y = y;
1151 appWindow->local_move.state = LMS_STARTING;
1152 appWindow->local_move.direction = direction;
1153
1154 xf_ungrab(xfc);
1155
1156 xf_SendClientEvent(
1157 xfc, appWindow->handle,
1158 xfc->NET_WM_MOVERESIZE, /* request X window manager to initiate a local move */
1159 5, /* 5 arguments to follow */
1160 x, /* x relative to root window */
1161 y, /* y relative to root window */
1162 direction, /* extended ICCM direction flag */
1163 1, /* simulated mouse button 1 */
1164 1); /* 1 == application request per extended ICCM */
1165}
1166
1167void xf_EndLocalMoveSize(xfContext* xfc, xfAppWindow* appWindow)
1168{
1169 if (appWindow->local_move.state == LMS_NOT_ACTIVE)
1170 return;
1171
1172 if (appWindow->local_move.state == LMS_STARTING)
1173 {
1174 /*
1175 * The move never was property started. This can happen due to race
1176 * conditions between the mouse button up and the communications to the
1177 * RDP server for local moves. We must cancel the X window manager move.
1178 * Per ICCM, the X client can ask to cancel an active move.
1179 */
1180 xf_SendClientEvent(
1181 xfc, appWindow->handle,
1182 xfc->NET_WM_MOVERESIZE, /* request X window manager to abort a local move */
1183 5, /* 5 arguments to follow */
1184 appWindow->local_move.root_x, /* x relative to root window */
1185 appWindow->local_move.root_y, /* y relative to root window */
1186 NET_WM_MOVERESIZE_CANCEL, /* extended ICCM direction flag */
1187 1, /* simulated mouse button 1 */
1188 1); /* 1 == application request per extended ICCM */
1189 }
1190
1191 appWindow->local_move.state = LMS_NOT_ACTIVE;
1192}
1193
1194void xf_MoveWindow(xfContext* xfc, xfAppWindow* appWindow, int x, int y, int width, int height)
1195{
1196 BOOL resize = FALSE;
1197
1198 if ((width * height) < 1)
1199 return;
1200
1201 if ((appWindow->width != width) || (appWindow->height != height))
1202 resize = TRUE;
1203
1204 if (appWindow->local_move.state == LMS_STARTING || appWindow->local_move.state == LMS_ACTIVE)
1205 return;
1206
1207 appWindow->x = x;
1208 appWindow->y = y;
1209 appWindow->width = width;
1210 appWindow->height = height;
1211
1212 if (resize)
1213 {
1214 if (!xf_AppWindowResize(xfc, appWindow))
1215 return;
1216
1217 LogDynAndXMoveResizeWindow(xfc->log, xfc->display, appWindow->handle, x, y,
1218 WINPR_ASSERTING_INT_CAST(uint32_t, width),
1219 WINPR_ASSERTING_INT_CAST(uint32_t, height));
1220 }
1221 else
1222 LogDynAndXMoveWindow(xfc->log, xfc->display, appWindow->handle, x, y);
1223
1224 xf_UpdateWindowArea(xfc, appWindow, 0, 0, width, height);
1225}
1226
1227void xf_ShowWindow(xfContext* xfc, xfAppWindow* appWindow, BYTE state)
1228{
1229 WINPR_ASSERT(xfc);
1230 WINPR_ASSERT(appWindow);
1231
1232 switch (state)
1233 {
1234 case WINDOW_HIDE:
1235 LogDynAndXWithdrawWindow(xfc->log, xfc->display, appWindow->handle, xfc->screen_number);
1236 break;
1237
1238 case WINDOW_SHOW_MINIMIZED:
1239 appWindow->minimized = TRUE;
1240 XIconifyWindow(xfc->display, appWindow->handle, xfc->screen_number);
1241 break;
1242
1243 case WINDOW_SHOW_MAXIMIZED:
1244 /* Set the window as maximized */
1245 appWindow->maxHorz = TRUE;
1246 appWindow->maxVert = TRUE;
1247 xf_SendClientEvent(xfc, appWindow->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_ADD,
1248 xfc->NET_WM_STATE_MAXIMIZED_VERT, xfc->NET_WM_STATE_MAXIMIZED_HORZ,
1249 0);
1250
1251 /*
1252 * This is a workaround for the case where the window is maximized locally before the
1253 * rail server is told to maximize the window, this appears to be a race condition where
1254 * the local window with incomplete data and once the window is actually maximized on
1255 * the server
1256 * - an update of the new areas may not happen. So, we simply to do a full update of the
1257 * entire window once the rail server notifies us that the window is now maximized.
1258 */
1259 if (appWindow->rail_state == WINDOW_SHOW_MAXIMIZED)
1260 {
1261 xf_UpdateWindowArea(xfc, appWindow, 0, 0,
1262 WINPR_ASSERTING_INT_CAST(int, appWindow->windowWidth),
1263 WINPR_ASSERTING_INT_CAST(int, appWindow->windowHeight));
1264 }
1265
1266 break;
1267
1268 case WINDOW_SHOW:
1269 /* Ensure the window is not maximized */
1270 xf_SendClientEvent(xfc, appWindow->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_REMOVE,
1271 xfc->NET_WM_STATE_MAXIMIZED_VERT, xfc->NET_WM_STATE_MAXIMIZED_HORZ,
1272 0);
1273
1274 /*
1275 * Ignore configure requests until both the Maximized properties have been processed
1276 * to prevent condition where WM overrides size of request due to one or both of these
1277 * properties still being set - which causes a position adjustment to be sent back to
1278 * the server thus causing the window to not return to its original size
1279 */
1280 if (appWindow->rail_state == WINDOW_SHOW_MAXIMIZED)
1281 appWindow->rail_ignore_configure = TRUE;
1282
1283 if (appWindow->is_transient)
1284 xf_SetWindowUnlisted(xfc, appWindow->handle);
1285
1286 LogDynAndXMapWindow(xfc->log, xfc->display, appWindow->handle);
1287 break;
1288 default:
1289 break;
1290 }
1291
1292 /* Save the current rail state of this window */
1293 appWindow->rail_state = state;
1294 LogDynAndXFlush(xfc->log, xfc->display);
1295}
1296
1297void xf_SetWindowRects(xfContext* xfc, xfAppWindow* appWindow, RECTANGLE_16* rects, int nrects)
1298{
1299 XRectangle* xrects = nullptr;
1300
1301 if (nrects < 1)
1302 return;
1303
1304#ifdef WITH_XEXT
1305 xrects = (XRectangle*)calloc(WINPR_ASSERTING_INT_CAST(uint32_t, nrects), sizeof(XRectangle));
1306
1307 for (int i = 0; i < nrects; i++)
1308 {
1309 xrects[i].x = WINPR_ASSERTING_INT_CAST(short, rects[i].left);
1310 xrects[i].y = WINPR_ASSERTING_INT_CAST(short, rects[i].top);
1311 xrects[i].width = WINPR_ASSERTING_INT_CAST(unsigned short, rects[i].right - rects[i].left);
1312 xrects[i].height = WINPR_ASSERTING_INT_CAST(unsigned short, rects[i].bottom - rects[i].top);
1313 }
1314
1315 XShapeCombineRectangles(xfc->display, appWindow->handle, ShapeBounding, 0, 0, xrects, nrects,
1316 ShapeSet, 0);
1317 free(xrects);
1318#endif
1319}
1320
1321void xf_SetWindowVisibilityRects(xfContext* xfc, xfAppWindow* appWindow, UINT32 rectsOffsetX,
1322 UINT32 rectsOffsetY, RECTANGLE_16* rects, int nrects)
1323{
1324 XRectangle* xrects = nullptr;
1325
1326 if (nrects < 1)
1327 return;
1328
1329#ifdef WITH_XEXT
1330 xrects = (XRectangle*)calloc(WINPR_ASSERTING_INT_CAST(uint32_t, nrects), sizeof(XRectangle));
1331
1332 for (int i = 0; i < nrects; i++)
1333 {
1334 xrects[i].x = WINPR_ASSERTING_INT_CAST(short, rects[i].left);
1335 xrects[i].y = WINPR_ASSERTING_INT_CAST(short, rects[i].top);
1336 xrects[i].width = WINPR_ASSERTING_INT_CAST(unsigned short, rects[i].right - rects[i].left);
1337 xrects[i].height = WINPR_ASSERTING_INT_CAST(unsigned short, rects[i].bottom - rects[i].top);
1338 }
1339
1340 XShapeCombineRectangles(
1341 xfc->display, appWindow->handle, ShapeBounding, WINPR_ASSERTING_INT_CAST(int, rectsOffsetX),
1342 WINPR_ASSERTING_INT_CAST(int, rectsOffsetY), xrects, nrects, ShapeSet, 0);
1343 free(xrects);
1344#endif
1345}
1346
1347void xf_UpdateWindowArea(xfContext* xfc, xfAppWindow* appWindow, int x, int y, int width,
1348 int height)
1349{
1350 int ax = 0;
1351 int ay = 0;
1352 const rdpSettings* settings = nullptr;
1353
1354 WINPR_ASSERT(xfc);
1355
1356 settings = xfc->common.context.settings;
1357 WINPR_ASSERT(settings);
1358
1359 if (appWindow == nullptr)
1360 return;
1361
1362 if (appWindow->surfaceId < UINT16_MAX)
1363 return;
1364
1365 ax = x + appWindow->windowOffsetX;
1366 ay = y + appWindow->windowOffsetY;
1367
1368 if (ax + width > appWindow->windowOffsetX + appWindow->width)
1369 width = (appWindow->windowOffsetX + appWindow->width - 1) - ax;
1370
1371 if (ay + height > appWindow->windowOffsetY + appWindow->height)
1372 height = (appWindow->windowOffsetY + appWindow->height - 1) - ay;
1373
1374 xf_lock_x11(xfc);
1375
1376 if (freerdp_settings_get_bool(settings, FreeRDP_SoftwareGdi))
1377 {
1378 LogDynAndXPutImage(xfc->log, xfc->display, appWindow->pixmap, appWindow->gc, xfc->image, ax,
1379 ay, x, y, WINPR_ASSERTING_INT_CAST(uint32_t, width),
1380 WINPR_ASSERTING_INT_CAST(uint32_t, height));
1381 }
1382
1383 LogDynAndXCopyArea(xfc->log, xfc->display, appWindow->pixmap, appWindow->handle, appWindow->gc,
1384 x, y, WINPR_ASSERTING_INT_CAST(uint32_t, width),
1385 WINPR_ASSERTING_INT_CAST(uint32_t, height), x, y);
1386 LogDynAndXFlush(xfc->log, xfc->display);
1387 xf_unlock_x11(xfc);
1388}
1389
1390void xf_AppWindowDestroyImage(xfAppWindow* appWindow)
1391{
1392 WINPR_ASSERT(appWindow);
1393 if (appWindow->image)
1394 {
1395 appWindow->image->data = nullptr;
1396 XDestroyImage(appWindow->image);
1397 appWindow->image = nullptr;
1398 }
1399}
1400
1401void xf_DestroyWindow(xfContext* xfc, xfAppWindow* appWindow)
1402{
1403 if (!appWindow)
1404 return;
1405
1406 if (xfc->appWindow == appWindow)
1407 xfc->appWindow = nullptr;
1408
1409 if (appWindow->gc)
1410 LogDynAndXFreeGC(xfc->log, xfc->display, appWindow->gc);
1411
1412 if (appWindow->pixmap)
1413 LogDynAndXFreePixmap(xfc->log, xfc->display, appWindow->pixmap);
1414
1415 xf_AppWindowDestroyImage(appWindow);
1416
1417 if (appWindow->handle)
1418 {
1419 LogDynAndXUnmapWindow(xfc->log, xfc->display, appWindow->handle);
1420 LogDynAndXDestroyWindow(xfc->log, xfc->display, appWindow->handle);
1421 }
1422
1423 if (appWindow->xfwin)
1424 munmap(nullptr, sizeof(*appWindow->xfwin));
1425
1426 if (appWindow->shmid >= 0)
1427 close(appWindow->shmid);
1428
1429 shm_unlink(get_shm_id());
1430 appWindow->xfwin = (Window*)-1;
1431 appWindow->shmid = -1;
1432 free(appWindow->title);
1433 free(appWindow->windowRects);
1434 free(appWindow->visibilityRects);
1435 free(appWindow);
1436}
1437
1438static xfAppWindow* get_windowUnlocked(xfContext* xfc, UINT64 id)
1439{
1440 WINPR_ASSERT(xfc);
1441 return HashTable_GetItemValue(xfc->railWindows, &id);
1442}
1443
1444xfAppWindow* xf_rail_get_windowFrom(xfContext* xfc, UINT64 id, const char* file, const char* fkt,
1445 size_t line)
1446{
1447 if (!xfc)
1448 return nullptr;
1449
1450 if (!xfc->railWindows)
1451 return nullptr;
1452
1453 xfAppWindowsLockFrom(xfc, file, fkt, line);
1454 xfAppWindow* window = get_windowUnlocked(xfc, id);
1455 if (!window)
1456 xfAppWindowsUnlockFrom(xfc, file, fkt, line);
1457
1458 return window;
1459}
1460
1461xfAppWindow* xf_AppWindowFromX11WindowFrom(xfContext* xfc, Window wnd, const char* file,
1462 const char* fkt, size_t line)
1463{
1464 ULONG_PTR* pKeys = nullptr;
1465
1466 WINPR_ASSERT(xfc);
1467 if (!xfc->railWindows)
1468 return nullptr;
1469
1470 xfAppWindowsLockFrom(xfc, file, fkt, line);
1471 size_t count = HashTable_GetKeys(xfc->railWindows, &pKeys);
1472
1473 for (size_t index = 0; index < count; index++)
1474 {
1475 xfAppWindow* appWindow = get_windowUnlocked(xfc, *(UINT64*)pKeys[index]);
1476
1477 if (!appWindow)
1478 {
1479 xfAppWindowsUnlockFrom(xfc, file, fkt, line);
1480 free(pKeys);
1481 return nullptr;
1482 }
1483
1484 if (appWindow->handle == wnd)
1485 {
1486 free(pKeys);
1487 return appWindow;
1488 }
1489 }
1490
1491 xfAppWindowsUnlockFrom(xfc, file, fkt, line);
1492 free(pKeys);
1493 return nullptr;
1494}
1495
1496UINT xf_AppUpdateWindowFromSurface(xfContext* xfc, gdiGfxSurface* surface)
1497{
1498 XImage* image = nullptr;
1499 UINT rc = ERROR_INTERNAL_ERROR;
1500
1501 WINPR_ASSERT(xfc);
1502 WINPR_ASSERT(surface);
1503
1504 xfAppWindow* appWindow = xf_rail_get_window(xfc, surface->windowId);
1505 if (!appWindow)
1506 {
1507 WLog_VRB(TAG, "Failed to find a window for id=0x%08" PRIx64, surface->windowId);
1508 return CHANNEL_RC_OK;
1509 }
1510
1511 const BOOL swGdi = freerdp_settings_get_bool(xfc->common.context.settings, FreeRDP_SoftwareGdi);
1512 UINT32 nrects = 0;
1513 const RECTANGLE_16* rects = region16_rects(&surface->invalidRegion, &nrects);
1514
1515 xf_lock_x11(xfc);
1516 if (swGdi)
1517 {
1518 if (appWindow->surfaceId != surface->surfaceId)
1519 {
1520 xf_AppWindowDestroyImage(appWindow);
1521 appWindow->surfaceId = surface->surfaceId;
1522 }
1523 if (appWindow->width != (INT64)surface->width)
1524 xf_AppWindowDestroyImage(appWindow);
1525 if (appWindow->height != (INT64)surface->height)
1526 xf_AppWindowDestroyImage(appWindow);
1527
1528 if (!appWindow->image)
1529 {
1530 WINPR_ASSERT(xfc->depth != 0);
1531 appWindow->image = LogDynAndXCreateImage(
1532 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
1533 ZPixmap, 0, (char*)surface->data, surface->width, surface->height,
1534 xfc->scanline_pad, WINPR_ASSERTING_INT_CAST(int, surface->scanline));
1535 if (!appWindow->image)
1536 {
1537 WLog_WARN(TAG,
1538 "Failed create a XImage[%" PRIu32 "x%" PRIu32 ", scanline=%" PRIu32
1539 ", bpp=%" PRId32 "] for window id=0x%08" PRIx64,
1540 surface->width, surface->height, surface->scanline, xfc->depth,
1541 surface->windowId);
1542 goto fail;
1543 }
1544 appWindow->image->byte_order = LSBFirst;
1545 appWindow->image->bitmap_bit_order = LSBFirst;
1546 }
1547
1548 image = appWindow->image;
1549 }
1550 else
1551 {
1552 xfGfxSurface* xfSurface = (xfGfxSurface*)surface;
1553 image = xfSurface->image;
1554 }
1555
1556 for (UINT32 x = 0; x < nrects; x++)
1557 {
1558 const RECTANGLE_16* rect = &rects[x];
1559 const UINT32 width = rect->right - rect->left;
1560 const UINT32 height = rect->bottom - rect->top;
1561
1562 LogDynAndXPutImage(xfc->log, xfc->display, appWindow->pixmap, appWindow->gc, image,
1563 rect->left, rect->top, rect->left, rect->top, width, height);
1564
1565 LogDynAndXCopyArea(xfc->log, xfc->display, appWindow->pixmap, appWindow->handle,
1566 appWindow->gc, rect->left, rect->top, width, height, rect->left,
1567 rect->top);
1568 }
1569
1570 rc = CHANNEL_RC_OK;
1571fail:
1572 xf_rail_return_window(appWindow);
1573 LogDynAndXFlush(xfc->log, xfc->display);
1574 xf_unlock_x11(xfc);
1575 return rc;
1576}
1577
1578BOOL xf_AppWindowResize(xfContext* xfc, xfAppWindow* appWindow)
1579{
1580 WINPR_ASSERT(xfc);
1581 WINPR_ASSERT(appWindow);
1582
1583 if (appWindow->pixmap != 0)
1584 LogDynAndXFreePixmap(xfc->log, xfc->display, appWindow->pixmap);
1585
1586 WINPR_ASSERT(xfc->depth != 0);
1587 appWindow->pixmap = LogDynAndXCreatePixmap(
1588 xfc->log, xfc->display, xfc->drawable, WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->width),
1589 WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->height),
1590 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth));
1591 xf_AppWindowDestroyImage(appWindow);
1592
1593 return appWindow->pixmap != 0;
1594}
1595
1596void xf_XSetTransientForHint(xfContext* xfc, xfAppWindow* window)
1597{
1598 WINPR_ASSERT(xfc);
1599 WINPR_ASSERT(window);
1600
1601 if (window->ownerWindowId == 0)
1602 return;
1603
1604 xfAppWindow* parent = xf_rail_get_window(xfc, window->ownerWindowId);
1605 if (!parent)
1606 return;
1607
1608 (void)LogDynAndXSetTransientForHint(xfc->log, xfc->display, window->handle, parent->handle);
1609 xf_rail_return_window(parent);
1610}
1611
1612void xfAppWindowsLockFrom(xfContext* xfc, WINPR_ATTR_UNUSED const char* file,
1613 WINPR_ATTR_UNUSED const char* fkt, WINPR_ATTR_UNUSED size_t line)
1614{
1615 WINPR_ASSERT(xfc);
1616
1617#if defined(WITH_VERBOSE_WINPR_ASSERT)
1618 const DWORD level = WLOG_TRACE;
1619 if (WLog_IsLevelActive(xfc->log, level))
1620 WLog_PrintTextMessage(xfc->log, level, line, file, fkt, "[rails] locking [%s]", fkt);
1621#endif
1622
1623 HashTable_Lock(xfc->railWindows);
1624
1625#if defined(WITH_VERBOSE_WINPR_ASSERT)
1626 WINPR_ASSERT(!xfc->isRailWindowsLocked);
1627 xfc->isRailWindowsLocked = TRUE;
1628#endif
1629}
1630
1631void xfAppWindowsUnlockFrom(xfContext* xfc, WINPR_ATTR_UNUSED const char* file,
1632 WINPR_ATTR_UNUSED const char* fkt, WINPR_ATTR_UNUSED size_t line)
1633{
1634 WINPR_ASSERT(xfc);
1635
1636#if defined(WITH_VERBOSE_WINPR_ASSERT)
1637 const DWORD level = WLOG_TRACE;
1638 if (WLog_IsLevelActive(xfc->log, level))
1639 WLog_PrintTextMessage(xfc->log, level, line, file, fkt, "[rails] unocking [%s]", fkt);
1640
1641 WINPR_ASSERT(xfc->isRailWindowsLocked);
1642 xfc->isRailWindowsLocked = FALSE;
1643#endif
1644
1645 HashTable_Unlock(xfc->railWindows);
1646}
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.