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