23 #include <freerdp/config.h>
28 #include <sys/types.h>
31 #include <X11/Xutil.h>
32 #include <X11/Xatom.h>
38 #include <winpr/assert.h>
39 #include <winpr/thread.h>
40 #include <winpr/crt.h>
41 #include <winpr/string.h>
43 #include <freerdp/rail.h>
44 #include <freerdp/log.h>
47 #include <X11/extensions/shape.h>
51 #include <X11/extensions/XInput2.h>
57 #include "xf_keyboard.h"
60 #define TAG CLIENT_TAG("x11")
63 #define DEBUG_X11(...) WLog_DBG(TAG, __VA_ARGS__)
65 #define DEBUG_X11(...) \
71 #include <FreeRDP_Icon_256px.h>
72 #define xf_icon_prop FreeRDP_Icon_256px_prop
74 #include "xf_window.h"
79 #define MWM_HINTS_FUNCTIONS (1L << 0)
80 #define MWM_HINTS_DECORATIONS (1L << 1)
85 #define MWM_FUNC_ALL (1L << 0)
93 #define MWM_DECOR_ALL (1L << 0)
101 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
110 unsigned long functions;
111 unsigned long decorations;
113 unsigned long status;
116 static void xf_XSetTransientForHint(xfContext* xfc, xfAppWindow* window);
118 static const char* window_style_to_string(UINT32 style)
126 ENTRY(WS_CLIPCHILDREN);
127 ENTRY(WS_CLIPSIBLINGS);
133 ENTRY(WS_MAXIMIZEBOX);
135 ENTRY(WS_OVERLAPPEDWINDOW);
137 ENTRY(WS_POPUPWINDOW);
147 const char* window_styles_to_string(UINT32 style,
char* buffer,
size_t length)
149 (void)_snprintf(buffer, length,
"style[0x%08" PRIx32
"] {", style);
150 const char* sep =
"";
151 for (
size_t x = 0; x < 32; x++)
153 const UINT32 val = 1 << x;
154 if ((style & val) != 0)
156 const char* str = window_style_to_string(val);
159 winpr_str_append(str, buffer, length, sep);
164 winpr_str_append(
"}", buffer, length,
"");
169 static const char* window_style_ex_to_string(UINT32 styleEx)
173 ENTRY(WS_EX_ACCEPTFILES);
174 ENTRY(WS_EX_APPWINDOW);
175 ENTRY(WS_EX_CLIENTEDGE);
176 ENTRY(WS_EX_COMPOSITED);
177 ENTRY(WS_EX_CONTEXTHELP);
178 ENTRY(WS_EX_CONTROLPARENT);
179 ENTRY(WS_EX_DLGMODALFRAME);
180 ENTRY(WS_EX_LAYERED);
181 ENTRY(WS_EX_LAYOUTRTL);
182 ENTRY(WS_EX_LEFTSCROLLBAR);
183 ENTRY(WS_EX_MDICHILD);
184 ENTRY(WS_EX_NOACTIVATE);
185 ENTRY(WS_EX_NOINHERITLAYOUT);
186 ENTRY(WS_EX_NOPARENTNOTIFY);
187 ENTRY(WS_EX_OVERLAPPEDWINDOW);
188 ENTRY(WS_EX_PALETTEWINDOW);
190 ENTRY(WS_EX_RIGHTSCROLLBAR);
191 ENTRY(WS_EX_RTLREADING);
192 ENTRY(WS_EX_STATICEDGE);
193 ENTRY(WS_EX_TOOLWINDOW);
194 ENTRY(WS_EX_TOPMOST);
195 ENTRY(WS_EX_TRANSPARENT);
196 ENTRY(WS_EX_WINDOWEDGE);
202 const char* window_styles_ex_to_string(UINT32 styleEx,
char* buffer,
size_t length)
204 (void)_snprintf(buffer, length,
"styleEx[0x%08" PRIx32
"] {", styleEx);
205 const char* sep =
"";
206 for (
size_t x = 0; x < 32; x++)
208 const UINT32 val = (UINT32)(1UL << x);
209 if ((styleEx & val) != 0)
211 const char* str = window_style_ex_to_string(val);
214 winpr_str_append(str, buffer, length, sep);
219 winpr_str_append(
"}", buffer, length,
"");
224 static void xf_SetWindowTitleText(xfContext* xfc, Window window,
const char* name)
229 const size_t i = strnlen(name, MAX_PATH);
230 XStoreName(xfc->display, window, name);
231 Atom wm_Name = xfc->_NET_WM_NAME;
232 Atom utf8Str = xfc->UTF8_STRING;
233 LogTagAndXChangeProperty(TAG, xfc->display, window, wm_Name, utf8Str, 8, PropModeReplace,
234 (
const unsigned char*)name, (
int)i);
240 void xf_SendClientEvent(xfContext* xfc, Window window, Atom atom,
unsigned int numArgs, ...)
242 XEvent xevent = { 0 };
244 va_start(argp, numArgs);
246 xevent.xclient.type = ClientMessage;
247 xevent.xclient.serial = 0;
248 xevent.xclient.send_event = False;
249 xevent.xclient.display = xfc->display;
250 xevent.xclient.window = window;
251 xevent.xclient.message_type = atom;
252 xevent.xclient.format = 32;
254 for (
size_t i = 0; i < numArgs; i++)
256 xevent.xclient.data.l[i] = va_arg(argp,
int);
259 DEBUG_X11(
"Send ClientMessage Event: wnd=0x%04lX", (
unsigned long)xevent.xclient.window);
260 XSendEvent(xfc->display, RootWindowOfScreen(xfc->screen), False,
261 SubstructureRedirectMask | SubstructureNotifyMask, &xevent);
262 XSync(xfc->display, False);
266 void xf_SetWindowMinimized(xfContext* xfc, xfWindow* window)
268 XIconifyWindow(xfc->display, window->handle, xfc->screen_number);
271 void xf_SetWindowFullscreen(xfContext* xfc, xfWindow* window, BOOL fullscreen)
273 const rdpSettings* settings = NULL;
276 UINT32 width = WINPR_ASSERTING_INT_CAST(uint32_t, window->width);
277 UINT32 height = WINPR_ASSERTING_INT_CAST(uint32_t, window->height);
281 settings = xfc->common.context.settings;
282 WINPR_ASSERT(settings);
286 window->decorations = xfc->decorations;
288 xf_SetWindowDecorations(xfc, window->handle, window->decorations);
289 DEBUG_X11(TAG,
"X window decoration set to %d", (
int)window->decorations);
290 xf_floatbar_toggle_fullscreen(xfc->window->floatbar, fullscreen);
294 xfc->savedWidth = xfc->window->width;
295 xfc->savedHeight = xfc->window->height;
296 xfc->savedPosX = xfc->window->left;
297 xfc->savedPosY = xfc->window->top;
300 ? WINPR_ASSERTING_INT_CAST(
304 ? WINPR_ASSERTING_INT_CAST(
310 width = WINPR_ASSERTING_INT_CAST(uint32_t, xfc->savedWidth);
311 height = WINPR_ASSERTING_INT_CAST(uint32_t, xfc->savedHeight);
312 startX = xfc->savedPosX;
313 startY = xfc->savedPosY;
320 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, 0);
322 startX = firstMonitor->x;
323 startY = firstMonitor->y;
329 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, i);
330 startX = MIN(startX, monitor->x);
331 startY = MIN(startY, monitor->y);
337 startX += WINPR_ASSERTING_INT_CAST(
339 startY += WINPR_ASSERTING_INT_CAST(
349 if (xfc->_NET_WM_FULLSCREEN_MONITORS != None ||
352 xf_ResizeDesktopWindow(xfc, window, WINPR_ASSERTING_INT_CAST(
int, width),
353 WINPR_ASSERTING_INT_CAST(
int, height));
358 XMoveWindow(xfc->display, window->handle, startX, startY);
362 xf_SendClientEvent(xfc, window->handle, xfc->_NET_WM_STATE, 4,
363 fullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE,
364 xfc->_NET_WM_STATE_FULLSCREEN, 0, 0);
372 xf_ResizeDesktopWindow(xfc, window, WINPR_ASSERTING_INT_CAST(
int, width),
373 WINPR_ASSERTING_INT_CAST(
int, height));
374 XMoveWindow(xfc->display, window->handle, startX, startY);
380 xf_SendClientEvent(xfc, window->handle, xfc->_NET_WM_FULLSCREEN_MONITORS, 5,
381 xfc->fullscreenMonitors.top, xfc->fullscreenMonitors.bottom,
382 xfc->fullscreenMonitors.left, xfc->fullscreenMonitors.right, 1);
389 xf_SetWindowDecorations(xfc, window->handle, FALSE);
391 if (xfc->fullscreenMonitors.top)
393 xf_SendClientEvent(xfc, window->handle, xfc->_NET_WM_STATE, 4, _NET_WM_STATE_ADD,
394 xfc->fullscreenMonitors.top, 0, 0);
398 XSetWindowAttributes xswa;
399 xswa.override_redirect = True;
400 XChangeWindowAttributes(xfc->display, window->handle, CWOverrideRedirect, &xswa);
401 XRaiseWindow(xfc->display, window->handle);
402 xswa.override_redirect = False;
403 XChangeWindowAttributes(xfc->display, window->handle, CWOverrideRedirect, &xswa);
407 if (xfc->_NET_WM_STATE_MAXIMIZED_VERT != None)
410 unsigned long nitems = 0;
411 unsigned long bytes = 0;
414 if (xf_GetWindowProperty(xfc, window->handle, xfc->_NET_WM_STATE, 255, &nitems,
417 const Atom* aprop = (
const Atom*)prop;
420 for (
size_t x = 0; x < nitems; x++)
422 if (aprop[x] == xfc->_NET_WM_STATE_MAXIMIZED_VERT)
425 if (aprop[x] == xfc->_NET_WM_STATE_MAXIMIZED_HORZ)
431 xf_SendClientEvent(xfc, window->handle, xfc->_NET_WM_STATE, 4,
432 _NET_WM_STATE_REMOVE, xfc->_NET_WM_STATE_MAXIMIZED_VERT,
434 xf_SendClientEvent(xfc, window->handle, xfc->_NET_WM_STATE, 4,
435 _NET_WM_STATE_REMOVE, xfc->_NET_WM_STATE_MAXIMIZED_HORZ,
437 xfc->savedMaximizedState = state;
444 width = xfc->vscreen.area.right - xfc->vscreen.area.left + 1;
445 height = xfc->vscreen.area.bottom - xfc->vscreen.area.top + 1;
446 DEBUG_X11(
"X window move and resize %dx%d@%dx%d", startX, startY, width, height);
447 xf_ResizeDesktopWindow(xfc, window, WINPR_ASSERTING_INT_CAST(
int, width),
448 WINPR_ASSERTING_INT_CAST(
int, height));
449 XMoveWindow(xfc->display, window->handle, startX, startY);
453 xf_SetWindowDecorations(xfc, window->handle, window->decorations);
454 xf_ResizeDesktopWindow(xfc, window, WINPR_ASSERTING_INT_CAST(
int, width),
455 WINPR_ASSERTING_INT_CAST(
int, height));
456 XMoveWindow(xfc->display, window->handle, startX, startY);
458 if (xfc->fullscreenMonitors.top)
460 xf_SendClientEvent(xfc, window->handle, xfc->_NET_WM_STATE, 4, _NET_WM_STATE_REMOVE,
461 xfc->fullscreenMonitors.top, 0, 0);
465 if (xfc->savedMaximizedState & 0x01)
467 xf_SendClientEvent(xfc, window->handle, xfc->_NET_WM_STATE, 4, _NET_WM_STATE_ADD,
468 xfc->_NET_WM_STATE_MAXIMIZED_VERT, 0, 0);
471 if (xfc->savedMaximizedState & 0x02)
473 xf_SendClientEvent(xfc, window->handle, xfc->_NET_WM_STATE, 4, _NET_WM_STATE_ADD,
474 xfc->_NET_WM_STATE_MAXIMIZED_HORZ, 0, 0);
477 xfc->savedMaximizedState = 0;
484 BOOL xf_GetWindowProperty(xfContext* xfc, Window window, Atom property,
int length,
485 unsigned long* nitems,
unsigned long* bytes, BYTE** prop)
488 Atom actual_type = None;
489 int actual_format = 0;
491 if (property == None)
494 status = LogTagAndXGetWindowProperty(TAG, xfc->display, window, property, 0, length, False,
495 AnyPropertyType, &actual_type, &actual_format, nitems,
498 if (status != Success)
501 if (actual_type == None)
503 WLog_DBG(TAG,
"Property %lu does not exist", (
unsigned long)property);
510 static BOOL xf_GetNumberOfDesktops(xfContext* xfc, Window root,
unsigned* pval)
512 unsigned long nitems = 0;
513 unsigned long bytes = 0;
520 xf_GetWindowProperty(xfc, root, xfc->_NET_NUMBER_OF_DESKTOPS, 1, &nitems, &bytes, &bprop);
522 long* prop = (
long*)bprop;
528 if ((*prop >= 0) && (*prop <= UINT32_MAX))
530 *pval = (UINT32)*prop;
537 static BOOL xf_GetCurrentDesktop(xfContext* xfc, Window root)
539 unsigned long nitems = 0;
540 unsigned long bytes = 0;
544 if (!xf_GetNumberOfDesktops(xfc, root, &max))
550 xf_GetWindowProperty(xfc, root, xfc->_NET_CURRENT_DESKTOP, 1, &nitems, &bytes, &bprop);
552 long* prop = (
long*)bprop;
553 xfc->current_desktop = 0;
557 xfc->current_desktop = (int)MIN(max - 1, *prop);
562 static BOOL xf_GetWorkArea_NET_WORKAREA(xfContext* xfc, Window root)
565 unsigned long nitems = 0;
566 unsigned long bytes = 0;
570 xf_GetWindowProperty(xfc, root, xfc->_NET_WORKAREA, INT_MAX, &nitems, &bytes, &bprop);
571 long* prop = (
long*)bprop;
576 if ((xfc->current_desktop * 4 + 3) >= (INT64)nitems)
579 xfc->workArea.x = (UINT32)MIN(UINT32_MAX, prop[xfc->current_desktop * 4 + 0]);
580 xfc->workArea.y = (UINT32)MIN(UINT32_MAX, prop[xfc->current_desktop * 4 + 1]);
581 xfc->workArea.width = (UINT32)MIN(UINT32_MAX, prop[xfc->current_desktop * 4 + 2]);
582 xfc->workArea.height = (UINT32)MIN(UINT32_MAX, prop[xfc->current_desktop * 4 + 3]);
590 BOOL xf_GetWorkArea(xfContext* xfc)
594 Window root = DefaultRootWindow(xfc->display);
595 (void)xf_GetCurrentDesktop(xfc, root);
596 return xf_GetWorkArea_NET_WORKAREA(xfc, root);
599 void xf_SetWindowDecorations(xfContext* xfc, Window window, BOOL show)
601 PropMotifWmHints hints = { .decorations = (show) ? MWM_DECOR_ALL : 0,
602 .functions = MWM_FUNC_ALL,
603 .flags = MWM_HINTS_DECORATIONS | MWM_HINTS_FUNCTIONS,
607 LogTagAndXChangeProperty(TAG, xfc->display, window, xfc->_MOTIF_WM_HINTS, xfc->_MOTIF_WM_HINTS,
608 32, PropModeReplace, (BYTE*)&hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
611 void xf_SetWindowUnlisted(xfContext* xfc, Window window)
614 Atom window_state[] = { xfc->_NET_WM_STATE_SKIP_PAGER, xfc->_NET_WM_STATE_SKIP_TASKBAR };
615 LogTagAndXChangeProperty(TAG, xfc->display, window, xfc->_NET_WM_STATE, XA_ATOM, 32,
616 PropModeReplace, (BYTE*)window_state, 2);
619 static void xf_SetWindowPID(xfContext* xfc, Window window, pid_t pid)
627 am_wm_pid = xfc->_NET_WM_PID;
628 LogTagAndXChangeProperty(TAG, xfc->display, window, am_wm_pid, XA_CARDINAL, 32, PropModeReplace,
632 static const char* get_shm_id(
void)
634 static char shm_id[64];
635 (void)sprintf_s(shm_id,
sizeof(shm_id),
"/com.freerdp.xfreerdp.tsmf_%016X",
636 GetCurrentProcessId());
640 Window xf_CreateDummyWindow(xfContext* xfc)
642 return XCreateWindow(xfc->display, RootWindowOfScreen(xfc->screen),
643 WINPR_ASSERTING_INT_CAST(
int, xfc->workArea.x),
644 WINPR_ASSERTING_INT_CAST(
int, xfc->workArea.y), 1, 1, 0, xfc->depth,
645 InputOutput, xfc->visual,
646 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->attribs_mask), &xfc->attribs);
649 void xf_DestroyDummyWindow(xfContext* xfc, Window window)
652 XDestroyWindow(xfc->display, window);
655 xfWindow* xf_CreateDesktopWindow(xfContext* xfc,
char* name,
int width,
int height)
657 XEvent xevent = { 0 };
659 XClassHint* classHints = NULL;
660 xfWindow* window = (xfWindow*)calloc(1,
sizeof(xfWindow));
665 rdpSettings* settings = xfc->common.context.settings;
666 WINPR_ASSERT(settings);
669 window->width = width;
670 window->height = height;
671 window->decorations = xfc->decorations;
672 window->is_mapped = FALSE;
673 window->is_transient = FALSE;
675 WINPR_ASSERT(xfc->depth != 0);
677 XCreateWindow(xfc->display, RootWindowOfScreen(xfc->screen),
678 WINPR_ASSERTING_INT_CAST(
int, xfc->workArea.x),
679 WINPR_ASSERTING_INT_CAST(
int, xfc->workArea.y), xfc->workArea.width,
680 xfc->workArea.height, 0, xfc->depth, InputOutput, xfc->visual,
681 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->attribs_mask), &xfc->attribs);
682 window->shmid = shm_open(get_shm_id(), (O_CREAT | O_RDWR), (S_IREAD | S_IWRITE));
684 if (window->shmid < 0)
686 DEBUG_X11(
"xf_CreateDesktopWindow: failed to get access to shared memory - shmget()\n");
690 int rc = ftruncate(window->shmid,
sizeof(window->handle));
693 #ifdef WITH_DEBUG_X11
694 char ebuffer[256] = { 0 };
695 DEBUG_X11(
"ftruncate failed with %s [%d]", winpr_strerror(rc, ebuffer,
sizeof(ebuffer)),
701 void* mem = mmap(0,
sizeof(window->handle), PROT_READ | PROT_WRITE, MAP_SHARED,
704 if (mem == MAP_FAILED)
707 "xf_CreateDesktopWindow: failed to assign pointer to the memory address - "
713 *window->xfwin = window->handle;
718 classHints = XAllocClassHint();
722 classHints->res_name =
"xfreerdp";
724 char* res_class = NULL;
727 res_class = _strdup(WmClass);
729 res_class = _strdup(
"xfreerdp");
731 classHints->res_class = res_class;
732 XSetClassHint(xfc->display, window->handle, classHints);
737 xf_ResizeDesktopWindow(xfc, window, width, height);
738 xf_SetWindowDecorations(xfc, window->handle, window->decorations);
739 xf_SetWindowPID(xfc, window->handle, 0);
740 input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
741 VisibilityChangeMask | FocusChangeMask | StructureNotifyMask | PointerMotionMask |
742 ExposureMask | PropertyChangeMask;
744 if (xfc->grab_keyboard)
745 input_mask |= EnterWindowMask | LeaveWindowMask;
747 LogTagAndXChangeProperty(TAG, xfc->display, window->handle, xfc->_NET_WM_ICON, XA_CARDINAL, 32,
748 PropModeReplace, (BYTE*)xf_icon_prop, ARRAYSIZE(xf_icon_prop));
751 XReparentWindow(xfc->display, window->handle, parentWindow, 0, 0);
753 XSelectInput(xfc->display, window->handle, input_mask);
754 XClearWindow(xfc->display, window->handle);
755 xf_SetWindowTitleText(xfc, window->handle, name);
756 XMapWindow(xfc->display, window->handle);
757 xf_input_init(xfc, window->handle);
765 XMaskEvent(xfc->display, VisibilityChangeMask, &xevent);
766 }
while (xevent.type != VisibilityNotify);
775 XMoveWindow(xfc->display, window->handle, 0, 0);
780 XMoveWindow(xfc->display, window->handle,
781 WINPR_ASSERTING_INT_CAST(
783 WINPR_ASSERTING_INT_CAST(
787 window->floatbar = xf_floatbar_new(xfc, window->handle, name,
790 if (xfc->_XWAYLAND_MAY_GRAB_KEYBOARD)
791 xf_SendClientEvent(xfc, window->handle, xfc->_XWAYLAND_MAY_GRAB_KEYBOARD, 1, 1);
796 void xf_ResizeDesktopWindow(xfContext* xfc, xfWindow* window,
int width,
int height)
798 XSizeHints* size_hints = NULL;
799 rdpSettings* settings = NULL;
804 settings = xfc->common.context.settings;
805 WINPR_ASSERT(settings);
807 if (!(size_hints = XAllocSizeHints()))
810 size_hints->flags = PMinSize | PMaxSize | PWinGravity;
811 size_hints->win_gravity = NorthWestGravity;
812 size_hints->min_width = size_hints->min_height = 1;
813 size_hints->max_width = size_hints->max_height = 16384;
814 XResizeWindow(xfc->display, window->handle, WINPR_ASSERTING_INT_CAST(uint32_t, width),
815 WINPR_ASSERTING_INT_CAST(uint32_t, height));
822 if (!xfc->fullscreen)
826 size_hints->min_width = size_hints->max_width = width;
827 size_hints->min_height = size_hints->max_height = height;
831 XSetWMNormalHints(xfc->display, window->handle, size_hints);
835 void xf_DestroyDesktopWindow(xfContext* xfc, xfWindow* window)
840 if (xfc->window == window)
843 xf_floatbar_free(window->floatbar);
846 XFreeGC(xfc->display, window->gc);
850 XUnmapWindow(xfc->display, window->handle);
851 XDestroyWindow(xfc->display, window->handle);
855 munmap(0,
sizeof(*window->xfwin));
857 if (window->shmid >= 0)
858 close(window->shmid);
860 shm_unlink(get_shm_id());
861 window->xfwin = (Window*)-1;
866 void xf_SetWindowStyle(xfContext* xfc, xfAppWindow* appWindow, UINT32 style, UINT32 ex_style)
868 Atom window_type = 0;
869 BOOL redirect = FALSE;
871 window_type = xfc->_NET_WM_WINDOW_TYPE_NORMAL;
873 if ((ex_style & WS_EX_NOACTIVATE) || (ex_style & WS_EX_TOOLWINDOW))
876 appWindow->is_transient = TRUE;
877 xf_SetWindowUnlisted(xfc, appWindow->handle);
878 window_type = xfc->_NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
884 else if (ex_style & WS_EX_TOPMOST)
886 window_type = xfc->_NET_WM_WINDOW_TYPE_NORMAL;
889 if (style & WS_POPUP)
891 window_type = xfc->_NET_WM_WINDOW_TYPE_DIALOG;
894 if (!((ex_style & WS_EX_DLGMODALFRAME) || (ex_style & WS_EX_LAYERED) ||
895 (style & WS_SYSMENU)))
897 appWindow->is_transient = TRUE;
900 xf_SetWindowUnlisted(xfc, appWindow->handle);
904 if (!(style == 0 && ex_style == 0))
906 xf_SetWindowActions(xfc, appWindow);
919 XSetWindowAttributes attrs = { 0 };
920 attrs.override_redirect = redirect ? True : False;
921 XChangeWindowAttributes(xfc->display, appWindow->handle, CWOverrideRedirect, &attrs);
924 LogTagAndXChangeProperty(TAG, xfc->display, appWindow->handle, xfc->_NET_WM_WINDOW_TYPE,
925 XA_ATOM, 32, PropModeReplace, (BYTE*)&window_type, 1);
927 const BOOL above = (ex_style & WS_EX_TOPMOST) != 0;
928 const BOOL
transient = (style & WS_CHILD) == 0;
931 xf_XSetTransientForHint(
934 xf_SendClientEvent(xfc, appWindow->handle, xfc->_NET_WM_STATE, 4,
935 above ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE, xfc->_NET_WM_STATE_ABOVE,
939 void xf_SetWindowActions(xfContext* xfc, xfAppWindow* appWindow)
941 Atom allowed_actions[] = {
942 xfc->NET_WM_ACTION_CLOSE, xfc->NET_WM_ACTION_MINIMIZE,
943 xfc->NET_WM_ACTION_MOVE, xfc->NET_WM_ACTION_RESIZE,
944 xfc->NET_WM_ACTION_MAXIMIZE_HORZ, xfc->NET_WM_ACTION_MAXIMIZE_VERT,
945 xfc->NET_WM_ACTION_FULLSCREEN, xfc->NET_WM_ACTION_CHANGE_DESKTOP
948 if (!(appWindow->dwStyle & WS_SYSMENU))
949 allowed_actions[0] = 0;
951 if (!(appWindow->dwStyle & WS_MINIMIZEBOX))
952 allowed_actions[1] = 0;
954 if (!(appWindow->dwStyle & WS_SIZEBOX))
955 allowed_actions[3] = 0;
957 if (!(appWindow->dwStyle & WS_MAXIMIZEBOX))
959 allowed_actions[4] = 0;
960 allowed_actions[5] = 0;
961 allowed_actions[6] = 0;
964 XChangeProperty(xfc->display, appWindow->handle, xfc->NET_WM_ALLOWED_ACTIONS, XA_ATOM, 32,
965 PropModeReplace, (
unsigned char*)&allowed_actions, 8);
968 void xf_SetWindowText(xfContext* xfc, xfAppWindow* appWindow,
const char* name)
970 xf_SetWindowTitleText(xfc, appWindow->handle, name);
973 static void xf_FixWindowCoordinates(xfContext* xfc,
int* x,
int* y,
int* width,
int* height)
975 int vscreen_width = 0;
976 int vscreen_height = 0;
977 vscreen_width = xfc->vscreen.area.right - xfc->vscreen.area.left + 1;
978 vscreen_height = xfc->vscreen.area.bottom - xfc->vscreen.area.top + 1;
980 if (*x < xfc->vscreen.area.left)
983 *x = xfc->vscreen.area.left;
986 if (*y < xfc->vscreen.area.top)
989 *y = xfc->vscreen.area.top;
992 if (*width > vscreen_width)
994 *width = vscreen_width;
997 if (*height > vscreen_height)
999 *height = vscreen_height;
1013 int xf_AppWindowInit(xfContext* xfc, xfAppWindow* appWindow)
1015 if (!xfc || !appWindow)
1018 xf_SetWindowDecorations(xfc, appWindow->handle, appWindow->decorations);
1019 xf_SetWindowStyle(xfc, appWindow, appWindow->dwStyle, appWindow->dwExStyle);
1020 xf_SetWindowPID(xfc, appWindow->handle, 0);
1021 xf_ShowWindow(xfc, appWindow, WINDOW_SHOW);
1022 XClearWindow(xfc->display, appWindow->handle);
1023 XMapWindow(xfc->display, appWindow->handle);
1025 xf_MoveWindow(xfc, appWindow, appWindow->x, appWindow->y, appWindow->width, appWindow->height);
1026 xf_SetWindowText(xfc, appWindow, appWindow->title);
1030 BOOL xf_AppWindowCreate(xfContext* xfc, xfAppWindow* appWindow)
1032 XGCValues gcv = { 0 };
1034 XWMHints* InputModeHint = NULL;
1035 XClassHint* class_hints = NULL;
1036 const rdpSettings* settings = NULL;
1039 WINPR_ASSERT(appWindow);
1041 settings = xfc->common.context.settings;
1042 WINPR_ASSERT(settings);
1044 xf_FixWindowCoordinates(xfc, &appWindow->x, &appWindow->y, &appWindow->width,
1045 &appWindow->height);
1046 appWindow->shmid = -1;
1047 appWindow->decorations = FALSE;
1048 appWindow->fullscreen = FALSE;
1049 appWindow->local_move.state = LMS_NOT_ACTIVE;
1050 appWindow->is_mapped = FALSE;
1051 appWindow->is_transient = FALSE;
1052 appWindow->rail_state = 0;
1053 appWindow->maxVert = FALSE;
1054 appWindow->maxHorz = FALSE;
1055 appWindow->minimized = FALSE;
1056 appWindow->rail_ignore_configure = FALSE;
1058 WINPR_ASSERT(xfc->depth != 0);
1059 appWindow->handle = XCreateWindow(
1060 xfc->display, RootWindowOfScreen(xfc->screen), appWindow->x, appWindow->y,
1061 WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->width),
1062 WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->height), 0, xfc->depth, InputOutput,
1063 xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->attribs_mask), &xfc->attribs);
1065 if (!appWindow->handle)
1068 appWindow->gc = XCreateGC(xfc->display, appWindow->handle, GCGraphicsExposures, &gcv);
1070 if (!xf_AppWindowResize(xfc, appWindow))
1073 class_hints = XAllocClassHint();
1077 char* strclass = NULL;
1081 strclass = _strdup(WmClass);
1085 winpr_asprintf(&strclass, &size,
"RAIL:%08" PRIX64
"", appWindow->windowId);
1087 class_hints->res_class = strclass;
1088 class_hints->res_name =
"RAIL";
1089 XSetClassHint(xfc->display, appWindow->handle, class_hints);
1095 InputModeHint = XAllocWMHints();
1096 InputModeHint->flags = (1L << 0);
1097 InputModeHint->input = True;
1098 XSetWMHints(xfc->display, appWindow->handle, InputModeHint);
1099 XFree(InputModeHint);
1100 XSetWMProtocols(xfc->display, appWindow->handle, &(xfc->WM_DELETE_WINDOW), 1);
1101 input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
1102 EnterWindowMask | LeaveWindowMask | PointerMotionMask | Button1MotionMask |
1103 Button2MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask |
1104 ButtonMotionMask | KeymapStateMask | ExposureMask | VisibilityChangeMask |
1105 StructureNotifyMask | SubstructureNotifyMask | SubstructureRedirectMask |
1106 FocusChangeMask | PropertyChangeMask | ColormapChangeMask | OwnerGrabButtonMask;
1107 XSelectInput(xfc->display, appWindow->handle, input_mask);
1109 if (xfc->_XWAYLAND_MAY_GRAB_KEYBOARD)
1110 xf_SendClientEvent(xfc, appWindow->handle, xfc->_XWAYLAND_MAY_GRAB_KEYBOARD, 1, 1);
1115 void xf_SetWindowMinMaxInfo(xfContext* xfc, xfAppWindow* appWindow,
int maxWidth,
int maxHeight,
1116 int maxPosX,
int maxPosY,
int minTrackWidth,
int minTrackHeight,
1117 int maxTrackWidth,
int maxTrackHeight)
1119 XSizeHints* size_hints = NULL;
1120 size_hints = XAllocSizeHints();
1124 size_hints->flags = PMinSize | PMaxSize | PResizeInc;
1125 size_hints->min_width = minTrackWidth;
1126 size_hints->min_height = minTrackHeight;
1127 size_hints->max_width = maxTrackWidth;
1128 size_hints->max_height = maxTrackHeight;
1130 size_hints->width_inc = size_hints->height_inc = 1;
1131 XSetWMNormalHints(xfc->display, appWindow->handle, size_hints);
1136 void xf_StartLocalMoveSize(xfContext* xfc, xfAppWindow* appWindow,
int direction,
int x,
int y)
1138 if (appWindow->local_move.state != LMS_NOT_ACTIVE)
1145 appWindow->local_move.root_x = x;
1146 appWindow->local_move.root_y = y;
1147 appWindow->local_move.state = LMS_STARTING;
1148 appWindow->local_move.direction = direction;
1153 xfc, appWindow->handle,
1154 xfc->_NET_WM_MOVERESIZE,
1163 void xf_EndLocalMoveSize(xfContext* xfc, xfAppWindow* appWindow)
1165 if (appWindow->local_move.state == LMS_NOT_ACTIVE)
1168 if (appWindow->local_move.state == LMS_STARTING)
1177 xfc, appWindow->handle,
1178 xfc->_NET_WM_MOVERESIZE,
1180 appWindow->local_move.root_x,
1181 appWindow->local_move.root_y,
1182 _NET_WM_MOVERESIZE_CANCEL,
1187 appWindow->local_move.state = LMS_NOT_ACTIVE;
1190 void xf_MoveWindow(xfContext* xfc, xfAppWindow* appWindow,
int x,
int y,
int width,
int height)
1192 BOOL resize = FALSE;
1194 if ((width * height) < 1)
1197 if ((appWindow->width != width) || (appWindow->height != height))
1200 if (appWindow->local_move.state == LMS_STARTING || appWindow->local_move.state == LMS_ACTIVE)
1205 appWindow->width = width;
1206 appWindow->height = height;
1209 XMoveResizeWindow(xfc->display, appWindow->handle, x, y,
1210 WINPR_ASSERTING_INT_CAST(uint32_t, width),
1211 WINPR_ASSERTING_INT_CAST(uint32_t, height));
1213 XMoveWindow(xfc->display, appWindow->handle, x, y);
1215 xf_UpdateWindowArea(xfc, appWindow, 0, 0, width, height);
1218 void xf_ShowWindow(xfContext* xfc, xfAppWindow* appWindow, BYTE state)
1221 WINPR_ASSERT(appWindow);
1226 XWithdrawWindow(xfc->display, appWindow->handle, xfc->screen_number);
1229 case WINDOW_SHOW_MINIMIZED:
1230 appWindow->minimized = TRUE;
1231 XIconifyWindow(xfc->display, appWindow->handle, xfc->screen_number);
1234 case WINDOW_SHOW_MAXIMIZED:
1236 appWindow->maxHorz = TRUE;
1237 appWindow->maxVert = TRUE;
1238 xf_SendClientEvent(xfc, appWindow->handle, xfc->_NET_WM_STATE, 4, _NET_WM_STATE_ADD,
1239 xfc->_NET_WM_STATE_MAXIMIZED_VERT, xfc->_NET_WM_STATE_MAXIMIZED_HORZ,
1250 if (appWindow->rail_state == WINDOW_SHOW_MAXIMIZED)
1252 xf_UpdateWindowArea(xfc, appWindow, 0, 0,
1253 WINPR_ASSERTING_INT_CAST(
int, appWindow->windowWidth),
1254 WINPR_ASSERTING_INT_CAST(
int, appWindow->windowHeight));
1261 xf_SendClientEvent(xfc, appWindow->handle, xfc->_NET_WM_STATE, 4, _NET_WM_STATE_REMOVE,
1262 xfc->_NET_WM_STATE_MAXIMIZED_VERT, xfc->_NET_WM_STATE_MAXIMIZED_HORZ,
1271 if (appWindow->rail_state == WINDOW_SHOW_MAXIMIZED)
1272 appWindow->rail_ignore_configure = TRUE;
1274 if (appWindow->is_transient)
1275 xf_SetWindowUnlisted(xfc, appWindow->handle);
1277 XMapWindow(xfc->display, appWindow->handle);
1284 appWindow->rail_state = state;
1285 XFlush(xfc->display);
1288 void xf_SetWindowRects(xfContext* xfc, xfAppWindow* appWindow,
RECTANGLE_16* rects,
int nrects)
1290 XRectangle* xrects = NULL;
1296 xrects = (XRectangle*)calloc(WINPR_ASSERTING_INT_CAST(uint32_t, nrects),
sizeof(XRectangle));
1298 for (
int i = 0; i < nrects; i++)
1300 xrects[i].x = WINPR_ASSERTING_INT_CAST(
short, rects[i].left);
1301 xrects[i].y = WINPR_ASSERTING_INT_CAST(
short, rects[i].top);
1302 xrects[i].width = WINPR_ASSERTING_INT_CAST(
unsigned short, rects[i].right - rects[i].left);
1303 xrects[i].height = WINPR_ASSERTING_INT_CAST(
unsigned short, rects[i].bottom - rects[i].top);
1306 XShapeCombineRectangles(xfc->display, appWindow->handle, ShapeBounding, 0, 0, xrects, nrects,
1312 void xf_SetWindowVisibilityRects(xfContext* xfc, xfAppWindow* appWindow, UINT32 rectsOffsetX,
1315 XRectangle* xrects = NULL;
1321 xrects = (XRectangle*)calloc(WINPR_ASSERTING_INT_CAST(uint32_t, nrects),
sizeof(XRectangle));
1323 for (
int i = 0; i < nrects; i++)
1325 xrects[i].x = WINPR_ASSERTING_INT_CAST(
short, rects[i].left);
1326 xrects[i].y = WINPR_ASSERTING_INT_CAST(
short, rects[i].top);
1327 xrects[i].width = WINPR_ASSERTING_INT_CAST(
unsigned short, rects[i].right - rects[i].left);
1328 xrects[i].height = WINPR_ASSERTING_INT_CAST(
unsigned short, rects[i].bottom - rects[i].top);
1331 XShapeCombineRectangles(
1332 xfc->display, appWindow->handle, ShapeBounding, WINPR_ASSERTING_INT_CAST(
int, rectsOffsetX),
1333 WINPR_ASSERTING_INT_CAST(
int, rectsOffsetY), xrects, nrects, ShapeSet, 0);
1338 void xf_UpdateWindowArea(xfContext* xfc, xfAppWindow* appWindow,
int x,
int y,
int width,
1343 const rdpSettings* settings = NULL;
1347 settings = xfc->common.context.settings;
1348 WINPR_ASSERT(settings);
1350 if (appWindow == NULL)
1353 if (appWindow->surfaceId < UINT16_MAX)
1356 ax = x + appWindow->windowOffsetX;
1357 ay = y + appWindow->windowOffsetY;
1359 if (ax + width > appWindow->windowOffsetX + appWindow->width)
1360 width = (appWindow->windowOffsetX + appWindow->width - 1) - ax;
1362 if (ay + height > appWindow->windowOffsetY + appWindow->height)
1363 height = (appWindow->windowOffsetY + appWindow->height - 1) - ay;
1369 XPutImage(xfc->display, appWindow->pixmap, appWindow->gc, xfc->image, ax, ay, x, y,
1370 WINPR_ASSERTING_INT_CAST(uint32_t, width),
1371 WINPR_ASSERTING_INT_CAST(uint32_t, height));
1374 XCopyArea(xfc->display, appWindow->pixmap, appWindow->handle, appWindow->gc, x, y,
1375 WINPR_ASSERTING_INT_CAST(uint32_t, width), WINPR_ASSERTING_INT_CAST(uint32_t, height),
1377 XFlush(xfc->display);
1381 static void xf_AppWindowDestroyImage(xfAppWindow* appWindow)
1383 WINPR_ASSERT(appWindow);
1384 if (appWindow->image)
1386 appWindow->image->data = NULL;
1387 XDestroyImage(appWindow->image);
1388 appWindow->image = NULL;
1392 void xf_DestroyWindow(xfContext* xfc, xfAppWindow* appWindow)
1397 if (xfc->appWindow == appWindow)
1398 xfc->appWindow = NULL;
1401 XFreeGC(xfc->display, appWindow->gc);
1403 if (appWindow->pixmap)
1404 XFreePixmap(xfc->display, appWindow->pixmap);
1406 xf_AppWindowDestroyImage(appWindow);
1408 if (appWindow->handle)
1410 XUnmapWindow(xfc->display, appWindow->handle);
1411 XDestroyWindow(xfc->display, appWindow->handle);
1414 if (appWindow->xfwin)
1415 munmap(0,
sizeof(*appWindow->xfwin));
1417 if (appWindow->shmid >= 0)
1418 close(appWindow->shmid);
1420 shm_unlink(get_shm_id());
1421 appWindow->xfwin = (Window*)-1;
1422 appWindow->shmid = -1;
1423 free(appWindow->title);
1424 free(appWindow->windowRects);
1425 free(appWindow->visibilityRects);
1429 xfAppWindow* xf_AppWindowFromX11Window(xfContext* xfc, Window wnd)
1431 ULONG_PTR* pKeys = NULL;
1434 if (!xfc->railWindows)
1437 size_t count = HashTable_GetKeys(xfc->railWindows, &pKeys);
1439 for (
size_t index = 0; index < count; index++)
1441 xfAppWindow* appWindow = xf_rail_get_window(xfc, *(UINT64*)pKeys[index]);
1449 if (appWindow->handle == wnd)
1460 UINT xf_AppUpdateWindowFromSurface(xfContext* xfc, gdiGfxSurface* surface)
1462 XImage* image = NULL;
1463 UINT rc = ERROR_INTERNAL_ERROR;
1466 WINPR_ASSERT(surface);
1468 xfAppWindow* appWindow = xf_rail_get_window(xfc, surface->windowId);
1471 WLog_VRB(TAG,
"Failed to find a window for id=0x%08" PRIx64, surface->windowId);
1472 return CHANNEL_RC_OK;
1477 const RECTANGLE_16* rects = region16_rects(&surface->invalidRegion, &nrects);
1482 if (appWindow->surfaceId != surface->surfaceId)
1484 xf_AppWindowDestroyImage(appWindow);
1485 appWindow->surfaceId = surface->surfaceId;
1487 if (appWindow->width != (INT64)surface->width)
1488 xf_AppWindowDestroyImage(appWindow);
1489 if (appWindow->height != (INT64)surface->height)
1490 xf_AppWindowDestroyImage(appWindow);
1492 if (!appWindow->image)
1494 WINPR_ASSERT(xfc->depth != 0);
1495 appWindow->image = XCreateImage(
1496 xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth), ZPixmap,
1497 0, (
char*)surface->data, surface->width, surface->height, xfc->scanline_pad,
1498 WINPR_ASSERTING_INT_CAST(
int, surface->scanline));
1499 if (!appWindow->image)
1502 "Failed create a XImage[%" PRIu32
"x%" PRIu32
", scanline=%" PRIu32
1503 ", bpp=%" PRIu32
"] for window id=0x%08" PRIx64,
1504 surface->width, surface->height, surface->scanline, xfc->depth,
1508 appWindow->image->byte_order = LSBFirst;
1509 appWindow->image->bitmap_bit_order = LSBFirst;
1512 image = appWindow->image;
1516 xfGfxSurface* xfSurface = (xfGfxSurface*)surface;
1517 image = xfSurface->image;
1520 for (UINT32 x = 0; x < nrects; x++)
1523 const UINT32 width = rect->right - rect->left;
1524 const UINT32 height = rect->bottom - rect->top;
1526 XPutImage(xfc->display, appWindow->pixmap, appWindow->gc, image, rect->left, rect->top,
1527 rect->left, rect->top, width, height);
1529 XCopyArea(xfc->display, appWindow->pixmap, appWindow->handle, appWindow->gc, rect->left,
1530 rect->top, width, height, rect->left, rect->top);
1535 XFlush(xfc->display);
1540 BOOL xf_AppWindowResize(xfContext* xfc, xfAppWindow* appWindow)
1543 WINPR_ASSERT(appWindow);
1545 if (appWindow->pixmap != 0)
1546 XFreePixmap(xfc->display, appWindow->pixmap);
1548 WINPR_ASSERT(xfc->depth != 0);
1549 appWindow->pixmap = XCreatePixmap(xfc->display, xfc->drawable,
1550 WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->width),
1551 WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->height),
1552 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth));
1553 xf_AppWindowDestroyImage(appWindow);
1555 return appWindow->pixmap != 0;
1558 void xf_XSetTransientForHint(xfContext* xfc, xfAppWindow* window)
1561 WINPR_ASSERT(window);
1563 if (window->ownerWindowId == 0)
1566 xfAppWindow* parent = xf_rail_get_window(xfc, window->ownerWindowId);
1570 const int rc = XSetTransientForHint(xfc->display, window->handle, parent->handle);
1573 char buffer[128] = { 0 };
1574 WLog_WARN(TAG,
"XSetTransientForHint [%d]{%s}", rc,
1575 x11_error_to_string(xfc, rc, buffer,
sizeof(buffer)));
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 const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.