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