23#include <freerdp/config.h>
38#include <winpr/assert.h>
39#include <winpr/thread.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"
61#define TAG CLIENT_TAG("x11")
63#include <FreeRDP_Icon_256px.h>
64#define xf_icon_prop FreeRDP_Icon_256px_prop
71#define MWM_HINTS_FUNCTIONS (1L << 0)
72#define MWM_HINTS_DECORATIONS (1L << 1)
77#define MWM_FUNC_ALL (1L << 0)
85#define MWM_DECOR_ALL (1L << 0)
93#define PROP_MOTIF_WM_HINTS_ELEMENTS 5
102 unsigned long functions;
103 unsigned long decorations;
105 unsigned long status;
108static void xf_XSetTransientForHint(xfContext* xfc, xfAppWindow* window);
110static const char* window_style_to_string(UINT32 style)
118 ENTRY(WS_CLIPCHILDREN);
119 ENTRY(WS_CLIPSIBLINGS);
125 ENTRY(WS_MAXIMIZEBOX);
127 ENTRY(WS_OVERLAPPEDWINDOW);
129 ENTRY(WS_POPUPWINDOW);
139const char* window_styles_to_string(UINT32 style,
char* buffer,
size_t length)
141 (void)_snprintf(buffer, length,
"style[0x%08" PRIx32
"] {", style);
142 const char* sep =
"";
143 for (
size_t x = 0; x < 32; x++)
145 const UINT32 val = 1 << x;
146 if ((style & val) != 0)
148 const char* str = window_style_to_string(val);
151 winpr_str_append(str, buffer, length, sep);
156 winpr_str_append(
"}", buffer, length,
"");
161static const char* window_style_ex_to_string(UINT32 styleEx)
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);
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);
194const char* window_styles_ex_to_string(UINT32 styleEx,
char* buffer,
size_t length)
196 (void)_snprintf(buffer, length,
"styleEx[0x%08" PRIx32
"] {", styleEx);
197 const char* sep =
"";
198 for (
size_t x = 0; x < 32; x++)
200 const UINT32 val = (UINT32)(1UL << x);
201 if ((styleEx & val) != 0)
203 const char* str = window_style_ex_to_string(val);
206 winpr_str_append(str, buffer, length, sep);
211 winpr_str_append(
"}", buffer, length,
"");
216static void xf_SetWindowTitleText(xfContext* xfc, Window window,
const char* name)
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);
232void xf_SendClientEvent(xfContext* xfc, Window window, Atom atom,
unsigned int numArgs, ...)
234 XEvent xevent = { 0 };
236 va_start(argp, numArgs);
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;
246 for (
size_t i = 0; i < numArgs; i++)
248 xevent.xclient.data.l[i] = va_arg(argp,
int);
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);
258void xf_SetWindowMinimized(xfContext* xfc, xfWindow* window)
260 XIconifyWindow(xfc->display, window->handle, xfc->screen_number);
263void xf_SetWindowFullscreen(xfContext* xfc, xfWindow* window, BOOL fullscreen)
265 const rdpSettings* settings = NULL;
268 UINT32 width = WINPR_ASSERTING_INT_CAST(uint32_t, window->width);
269 UINT32 height = WINPR_ASSERTING_INT_CAST(uint32_t, window->height);
273 settings = xfc->common.context.settings;
274 WINPR_ASSERT(settings);
278 window->decorations = 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);
286 xfc->savedWidth = xfc->window->width;
287 xfc->savedHeight = xfc->window->height;
288 xfc->savedPosX = xfc->window->left;
289 xfc->savedPosY = xfc->window->top;
292 ? WINPR_ASSERTING_INT_CAST(
296 ? WINPR_ASSERTING_INT_CAST(
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;
312 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, 0);
314 startX = firstMonitor->x;
315 startY = firstMonitor->y;
321 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, i);
322 startX = MIN(startX, monitor->x);
323 startY = MIN(startY, monitor->y);
339 if (xfc->NET_WM_FULLSCREEN_MONITORS != None ||
342 xf_ResizeDesktopWindow(xfc, window, WINPR_ASSERTING_INT_CAST(
int, width),
343 WINPR_ASSERTING_INT_CAST(
int, height));
348 XMoveWindow(xfc->display, window->handle, startX, startY);
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);
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);
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);
379 xf_SetWindowDecorations(xfc, window->handle, FALSE);
381 if (xfc->fullscreenMonitors.top)
383 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_ADD,
384 xfc->fullscreenMonitors.top, 0, 0);
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);
397 if (xfc->NET_WM_STATE_MAXIMIZED_VERT != None)
400 unsigned long nitems = 0;
401 unsigned long bytes = 0;
404 if (xf_GetWindowProperty(xfc, window->handle, xfc->NET_WM_STATE, 255, &nitems,
407 const Atom* aprop = (
const Atom*)prop;
410 for (
size_t x = 0; x < nitems; x++)
412 if (aprop[x] == xfc->NET_WM_STATE_MAXIMIZED_VERT)
415 if (aprop[x] == xfc->NET_WM_STATE_MAXIMIZED_HORZ)
421 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4,
422 NET_WM_STATE_REMOVE, xfc->NET_WM_STATE_MAXIMIZED_VERT, 0,
424 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4,
425 NET_WM_STATE_REMOVE, xfc->NET_WM_STATE_MAXIMIZED_HORZ, 0,
427 xfc->savedMaximizedState = state;
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);
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);
448 if (xfc->fullscreenMonitors.top)
450 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_REMOVE,
451 xfc->fullscreenMonitors.top, 0, 0);
455 if (xfc->savedMaximizedState & 0x01)
457 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_ADD,
458 xfc->NET_WM_STATE_MAXIMIZED_VERT, 0, 0);
461 if (xfc->savedMaximizedState & 0x02)
463 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_ADD,
464 xfc->NET_WM_STATE_MAXIMIZED_HORZ, 0, 0);
467 xfc->savedMaximizedState = 0;
474BOOL xf_GetWindowProperty(xfContext* xfc, Window window, Atom property,
int length,
475 unsigned long* nitems,
unsigned long* bytes, BYTE** prop)
478 Atom actual_type = None;
479 int actual_format = 0;
481 if (property == None)
484 status = LogTagAndXGetWindowProperty(TAG, xfc->display, window, property, 0, length, False,
485 AnyPropertyType, &actual_type, &actual_format, nitems,
488 if (status != Success)
491 if (actual_type == None)
493 WLog_DBG(TAG,
"Property %lu does not exist", (
unsigned long)property);
500static BOOL xf_GetNumberOfDesktops(xfContext* xfc, Window root,
unsigned* pval)
502 unsigned long nitems = 0;
503 unsigned long bytes = 0;
510 xf_GetWindowProperty(xfc, root, xfc->NET_NUMBER_OF_DESKTOPS, 1, &nitems, &bytes, &bprop);
512 long* prop = (
long*)bprop;
518 if ((*prop >= 0) && (*prop <= UINT32_MAX))
520 *pval = (UINT32)*prop;
527static BOOL xf_GetCurrentDesktop(xfContext* xfc, Window root)
529 unsigned long nitems = 0;
530 unsigned long bytes = 0;
534 if (!xf_GetNumberOfDesktops(xfc, root, &max))
540 xf_GetWindowProperty(xfc, root, xfc->NET_CURRENT_DESKTOP, 1, &nitems, &bytes, &bprop);
542 long* prop = (
long*)bprop;
543 xfc->current_desktop = 0;
547 xfc->current_desktop = (int)MIN(max - 1, *prop);
552static BOOL xf_GetWorkArea_NET_WORKAREA(xfContext* xfc, Window root)
555 unsigned long nitems = 0;
556 unsigned long bytes = 0;
560 xf_GetWindowProperty(xfc, root, xfc->NET_WORKAREA, INT_MAX, &nitems, &bytes, &bprop);
561 long* prop = (
long*)bprop;
566 if ((xfc->current_desktop * 4 + 3) >= (INT64)nitems)
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]);
580BOOL xf_GetWorkArea(xfContext* xfc)
584 Window root = DefaultRootWindow(xfc->display);
585 (void)xf_GetCurrentDesktop(xfc, root);
586 return xf_GetWorkArea_NET_WORKAREA(xfc, root);
589void xf_SetWindowDecorations(xfContext* xfc, Window window, BOOL show)
591 PropMotifWmHints hints = { .decorations = (show) ? MWM_DECOR_ALL : 0,
592 .functions = MWM_FUNC_ALL,
593 .flags = MWM_HINTS_DECORATIONS | MWM_HINTS_FUNCTIONS,
597 LogTagAndXChangeProperty(TAG, xfc->display, window, xfc->MOTIF_WM_HINTS, xfc->MOTIF_WM_HINTS,
598 32, PropModeReplace, (BYTE*)&hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
601void xf_SetWindowUnlisted(xfContext* xfc, Window window)
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);
609static void xf_SetWindowPID(xfContext* xfc, Window window, pid_t pid)
617 am_wm_pid = xfc->NET_WM_PID;
618 LogTagAndXChangeProperty(TAG, xfc->display, window, am_wm_pid, XA_CARDINAL, 32, PropModeReplace,
622static const char* get_shm_id(
void)
624 static char shm_id[64];
625 (void)sprintf_s(shm_id,
sizeof(shm_id),
"/com.freerdp.xfreerdp.tsmf_%016X",
626 GetCurrentProcessId());
630Window xf_CreateDummyWindow(xfContext* xfc)
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);
639void xf_DestroyDummyWindow(xfContext* xfc, Window window)
642 XDestroyWindow(xfc->display, window);
645xfWindow* xf_CreateDesktopWindow(xfContext* xfc,
char* name,
int width,
int height)
647 XEvent xevent = { 0 };
649 XClassHint* classHints = NULL;
650 xfWindow* window = (xfWindow*)calloc(1,
sizeof(xfWindow));
655 rdpSettings* settings = xfc->common.context.settings;
656 WINPR_ASSERT(settings);
659 window->width = width;
660 window->height = height;
661 window->decorations = xfc->decorations;
662 window->is_mapped = FALSE;
663 window->is_transient = FALSE;
665 WINPR_ASSERT(xfc->depth != 0);
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));
674 if (window->shmid < 0)
676 DEBUG_X11(
"xf_CreateDesktopWindow: failed to get access to shared memory - shmget()\n");
680 int rc = ftruncate(window->shmid,
sizeof(window->handle));
684 char ebuffer[256] = { 0 };
685 DEBUG_X11(
"ftruncate failed with %s [%d]", winpr_strerror(rc, ebuffer,
sizeof(ebuffer)),
691 void* mem = mmap(0,
sizeof(window->handle), PROT_READ | PROT_WRITE, MAP_SHARED,
694 if (mem == MAP_FAILED)
697 "xf_CreateDesktopWindow: failed to assign pointer to the memory address - "
703 *window->xfwin = window->handle;
708 classHints = XAllocClassHint();
712 classHints->res_name =
"xfreerdp";
714 char* res_class = NULL;
717 res_class = _strdup(WmClass);
719 res_class = _strdup(
"xfreerdp");
721 classHints->res_class = res_class;
722 XSetClassHint(xfc->display, window->handle, classHints);
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;
734 if (xfc->grab_keyboard)
735 input_mask |= EnterWindowMask | LeaveWindowMask;
737 LogTagAndXChangeProperty(TAG, xfc->display, window->handle, xfc->NET_WM_ICON, XA_CARDINAL, 32,
738 PropModeReplace, (BYTE*)xf_icon_prop, ARRAYSIZE(xf_icon_prop));
741 XReparentWindow(xfc->display, window->handle, parentWindow, 0, 0);
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);
755 XMaskEvent(xfc->display, VisibilityChangeMask, &xevent);
756 }
while (xevent.type != VisibilityNotify);
765 XMoveWindow(xfc->display, window->handle, 0, 0);
770 XMoveWindow(xfc->display, window->handle,
771 WINPR_ASSERTING_INT_CAST(
773 WINPR_ASSERTING_INT_CAST(
777 window->floatbar = xf_floatbar_new(xfc, window->handle, name,
780 if (xfc->XWAYLAND_MAY_GRAB_KEYBOARD)
781 xf_SendClientEvent(xfc, window->handle, xfc->XWAYLAND_MAY_GRAB_KEYBOARD, 1, 1);
786void xf_ResizeDesktopWindow(xfContext* xfc, xfWindow* window,
int width,
int height)
788 XSizeHints* size_hints = NULL;
789 rdpSettings* settings = NULL;
794 settings = xfc->common.context.settings;
795 WINPR_ASSERT(settings);
797 if (!(size_hints = XAllocSizeHints()))
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));
812 if (!xfc->fullscreen)
816 size_hints->min_width = size_hints->max_width = width;
817 size_hints->min_height = size_hints->max_height = height;
821 XSetWMNormalHints(xfc->display, window->handle, size_hints);
825void xf_DestroyDesktopWindow(xfContext* xfc, xfWindow* window)
830 if (xfc->window == window)
833 xf_floatbar_free(window->floatbar);
836 XFreeGC(xfc->display, window->gc);
840 XUnmapWindow(xfc->display, window->handle);
841 XDestroyWindow(xfc->display, window->handle);
845 munmap(0,
sizeof(*window->xfwin));
847 if (window->shmid >= 0)
848 close(window->shmid);
850 shm_unlink(get_shm_id());
851 window->xfwin = (Window*)-1;
856void xf_SetWindowStyle(xfContext* xfc, xfAppWindow* appWindow, UINT32 style, UINT32 ex_style)
858 Atom window_type = 0;
859 BOOL redirect = FALSE;
861 window_type = xfc->NET_WM_WINDOW_TYPE_NORMAL;
863 if ((ex_style & WS_EX_NOACTIVATE) || (ex_style & WS_EX_TOOLWINDOW))
866 appWindow->is_transient = TRUE;
867 xf_SetWindowUnlisted(xfc, appWindow->handle);
868 window_type = xfc->NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
874 else if (ex_style & WS_EX_TOPMOST)
876 window_type = xfc->NET_WM_WINDOW_TYPE_NORMAL;
879 if (style & WS_POPUP)
881 window_type = xfc->NET_WM_WINDOW_TYPE_DIALOG;
884 if (!((ex_style & WS_EX_DLGMODALFRAME) || (ex_style & WS_EX_LAYERED) ||
885 (style & WS_SYSMENU)))
887 appWindow->is_transient = TRUE;
890 xf_SetWindowUnlisted(xfc, appWindow->handle);
894 if (!(style == 0 && ex_style == 0))
896 xf_SetWindowActions(xfc, appWindow);
909 XSetWindowAttributes attrs = { 0 };
910 attrs.override_redirect = redirect ? True : False;
911 XChangeWindowAttributes(xfc->display, appWindow->handle, CWOverrideRedirect, &attrs);
914 LogTagAndXChangeProperty(TAG, xfc->display, appWindow->handle, xfc->NET_WM_WINDOW_TYPE, XA_ATOM,
915 32, PropModeReplace, (BYTE*)&window_type, 1);
917 const BOOL above = (ex_style & WS_EX_TOPMOST) != 0;
918 const BOOL transient = (style & WS_CHILD) == 0;
921 xf_XSetTransientForHint(
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,
929void xf_SetWindowActions(xfContext* xfc, xfAppWindow* appWindow)
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
938 if (!(appWindow->dwStyle & WS_SYSMENU))
939 allowed_actions[0] = 0;
941 if (!(appWindow->dwStyle & WS_MINIMIZEBOX))
942 allowed_actions[1] = 0;
944 if (!(appWindow->dwStyle & WS_SIZEBOX))
945 allowed_actions[3] = 0;
947 if (!(appWindow->dwStyle & WS_MAXIMIZEBOX))
949 allowed_actions[4] = 0;
950 allowed_actions[5] = 0;
951 allowed_actions[6] = 0;
954 XChangeProperty(xfc->display, appWindow->handle, xfc->NET_WM_ALLOWED_ACTIONS, XA_ATOM, 32,
955 PropModeReplace, (
unsigned char*)&allowed_actions, 8);
958void xf_SetWindowText(xfContext* xfc, xfAppWindow* appWindow,
const char* name)
960 xf_SetWindowTitleText(xfc, appWindow->handle, name);
963static void xf_FixWindowCoordinates(xfContext* xfc,
int* x,
int* y,
int* width,
int* height)
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;
970 if (*x < xfc->vscreen.area.left)
973 *x = xfc->vscreen.area.left;
976 if (*y < xfc->vscreen.area.top)
979 *y = xfc->vscreen.area.top;
982 if (*width > vscreen_width)
984 *width = vscreen_width;
987 if (*height > vscreen_height)
989 *height = vscreen_height;
1003int xf_AppWindowInit(xfContext* xfc, xfAppWindow* appWindow)
1005 if (!xfc || !appWindow)
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);
1015 xf_MoveWindow(xfc, appWindow, appWindow->x, appWindow->y, appWindow->width, appWindow->height);
1016 xf_SetWindowText(xfc, appWindow, appWindow->title);
1020BOOL xf_AppWindowCreate(xfContext* xfc, xfAppWindow* appWindow)
1022 XGCValues gcv = { 0 };
1024 XWMHints* InputModeHint = NULL;
1025 XClassHint* class_hints = NULL;
1026 const rdpSettings* settings = NULL;
1029 WINPR_ASSERT(appWindow);
1031 settings = xfc->common.context.settings;
1032 WINPR_ASSERT(settings);
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;
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);
1055 if (!appWindow->handle)
1058 appWindow->gc = XCreateGC(xfc->display, appWindow->handle, GCGraphicsExposures, &gcv);
1060 if (!xf_AppWindowResize(xfc, appWindow))
1063 class_hints = XAllocClassHint();
1067 char* strclass = NULL;
1071 strclass = _strdup(WmClass);
1075 winpr_asprintf(&strclass, &size,
"RAIL:%08" PRIX64
"", appWindow->windowId);
1077 class_hints->res_class = strclass;
1078 class_hints->res_name =
"RAIL";
1079 XSetClassHint(xfc->display, appWindow->handle, class_hints);
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);
1099 if (xfc->XWAYLAND_MAY_GRAB_KEYBOARD)
1100 xf_SendClientEvent(xfc, appWindow->handle, xfc->XWAYLAND_MAY_GRAB_KEYBOARD, 1, 1);
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)
1110 XSizeHints* size_hints = NULL;
1111 size_hints = XAllocSizeHints();
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;
1121 size_hints->width_inc = size_hints->height_inc = 1;
1122 XSetWMNormalHints(xfc->display, appWindow->handle, size_hints);
1127void xf_StartLocalMoveSize(xfContext* xfc, xfAppWindow* appWindow,
int direction,
int x,
int y)
1129 if (appWindow->local_move.state != LMS_NOT_ACTIVE)
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;
1144 xfc, appWindow->handle,
1145 xfc->NET_WM_MOVERESIZE,
1154void xf_EndLocalMoveSize(xfContext* xfc, xfAppWindow* appWindow)
1156 if (appWindow->local_move.state == LMS_NOT_ACTIVE)
1159 if (appWindow->local_move.state == LMS_STARTING)
1168 xfc, appWindow->handle,
1169 xfc->NET_WM_MOVERESIZE,
1171 appWindow->local_move.root_x,
1172 appWindow->local_move.root_y,
1173 NET_WM_MOVERESIZE_CANCEL,
1178 appWindow->local_move.state = LMS_NOT_ACTIVE;
1181void xf_MoveWindow(xfContext* xfc, xfAppWindow* appWindow,
int x,
int y,
int width,
int height)
1183 BOOL resize = FALSE;
1185 if ((width * height) < 1)
1188 if ((appWindow->width != width) || (appWindow->height != height))
1191 if (appWindow->local_move.state == LMS_STARTING || appWindow->local_move.state == LMS_ACTIVE)
1196 appWindow->width = width;
1197 appWindow->height = height;
1200 XMoveResizeWindow(xfc->display, appWindow->handle, x, y,
1201 WINPR_ASSERTING_INT_CAST(uint32_t, width),
1202 WINPR_ASSERTING_INT_CAST(uint32_t, height));
1204 XMoveWindow(xfc->display, appWindow->handle, x, y);
1206 xf_UpdateWindowArea(xfc, appWindow, 0, 0, width, height);
1209void xf_ShowWindow(xfContext* xfc, xfAppWindow* appWindow, BYTE state)
1212 WINPR_ASSERT(appWindow);
1217 XWithdrawWindow(xfc->display, appWindow->handle, xfc->screen_number);
1220 case WINDOW_SHOW_MINIMIZED:
1221 appWindow->minimized = TRUE;
1222 XIconifyWindow(xfc->display, appWindow->handle, xfc->screen_number);
1225 case WINDOW_SHOW_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,
1241 if (appWindow->rail_state == WINDOW_SHOW_MAXIMIZED)
1243 xf_UpdateWindowArea(xfc, appWindow, 0, 0,
1244 WINPR_ASSERTING_INT_CAST(
int, appWindow->windowWidth),
1245 WINPR_ASSERTING_INT_CAST(
int, appWindow->windowHeight));
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,
1262 if (appWindow->rail_state == WINDOW_SHOW_MAXIMIZED)
1263 appWindow->rail_ignore_configure = TRUE;
1265 if (appWindow->is_transient)
1266 xf_SetWindowUnlisted(xfc, appWindow->handle);
1268 XMapWindow(xfc->display, appWindow->handle);
1275 appWindow->rail_state = state;
1276 XFlush(xfc->display);
1279void xf_SetWindowRects(xfContext* xfc, xfAppWindow* appWindow,
RECTANGLE_16* rects,
int nrects)
1281 XRectangle* xrects = NULL;
1287 xrects = (XRectangle*)calloc(WINPR_ASSERTING_INT_CAST(uint32_t, nrects),
sizeof(XRectangle));
1289 for (
int i = 0; i < nrects; i++)
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);
1297 XShapeCombineRectangles(xfc->display, appWindow->handle, ShapeBounding, 0, 0, xrects, nrects,
1303void xf_SetWindowVisibilityRects(xfContext* xfc, xfAppWindow* appWindow, UINT32 rectsOffsetX,
1306 XRectangle* xrects = NULL;
1312 xrects = (XRectangle*)calloc(WINPR_ASSERTING_INT_CAST(uint32_t, nrects),
sizeof(XRectangle));
1314 for (
int i = 0; i < nrects; i++)
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);
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);
1329void xf_UpdateWindowArea(xfContext* xfc, xfAppWindow* appWindow,
int x,
int y,
int width,
1334 const rdpSettings* settings = NULL;
1338 settings = xfc->common.context.settings;
1339 WINPR_ASSERT(settings);
1341 if (appWindow == NULL)
1344 if (appWindow->surfaceId < UINT16_MAX)
1347 ax = x + appWindow->windowOffsetX;
1348 ay = y + appWindow->windowOffsetY;
1350 if (ax + width > appWindow->windowOffsetX + appWindow->width)
1351 width = (appWindow->windowOffsetX + appWindow->width - 1) - ax;
1353 if (ay + height > appWindow->windowOffsetY + appWindow->height)
1354 height = (appWindow->windowOffsetY + appWindow->height - 1) - ay;
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));
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),
1368 XFlush(xfc->display);
1372static void xf_AppWindowDestroyImage(xfAppWindow* appWindow)
1374 WINPR_ASSERT(appWindow);
1375 if (appWindow->image)
1377 appWindow->image->data = NULL;
1378 XDestroyImage(appWindow->image);
1379 appWindow->image = NULL;
1383void xf_DestroyWindow(xfContext* xfc, xfAppWindow* appWindow)
1388 if (xfc->appWindow == appWindow)
1389 xfc->appWindow = NULL;
1392 XFreeGC(xfc->display, appWindow->gc);
1394 if (appWindow->pixmap)
1395 XFreePixmap(xfc->display, appWindow->pixmap);
1397 xf_AppWindowDestroyImage(appWindow);
1399 if (appWindow->handle)
1401 XUnmapWindow(xfc->display, appWindow->handle);
1402 XDestroyWindow(xfc->display, appWindow->handle);
1405 if (appWindow->xfwin)
1406 munmap(0,
sizeof(*appWindow->xfwin));
1408 if (appWindow->shmid >= 0)
1409 close(appWindow->shmid);
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);
1420xfAppWindow* xf_AppWindowFromX11Window(xfContext* xfc, Window wnd)
1422 ULONG_PTR* pKeys = NULL;
1425 if (!xfc->railWindows)
1428 size_t count = HashTable_GetKeys(xfc->railWindows, &pKeys);
1430 for (
size_t index = 0; index < count; index++)
1432 xfAppWindow* appWindow = xf_rail_get_window(xfc, *(UINT64*)pKeys[index]);
1440 if (appWindow->handle == wnd)
1451UINT xf_AppUpdateWindowFromSurface(xfContext* xfc, gdiGfxSurface* surface)
1453 XImage* image = NULL;
1454 UINT rc = ERROR_INTERNAL_ERROR;
1457 WINPR_ASSERT(surface);
1459 xfAppWindow* appWindow = xf_rail_get_window(xfc, surface->windowId);
1462 WLog_VRB(TAG,
"Failed to find a window for id=0x%08" PRIx64, surface->windowId);
1463 return CHANNEL_RC_OK;
1468 const RECTANGLE_16* rects = region16_rects(&surface->invalidRegion, &nrects);
1473 if (appWindow->surfaceId != surface->surfaceId)
1475 xf_AppWindowDestroyImage(appWindow);
1476 appWindow->surfaceId = surface->surfaceId;
1478 if (appWindow->width != (INT64)surface->width)
1479 xf_AppWindowDestroyImage(appWindow);
1480 if (appWindow->height != (INT64)surface->height)
1481 xf_AppWindowDestroyImage(appWindow);
1483 if (!appWindow->image)
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)
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,
1499 appWindow->image->byte_order = LSBFirst;
1500 appWindow->image->bitmap_bit_order = LSBFirst;
1503 image = appWindow->image;
1507 xfGfxSurface* xfSurface = (xfGfxSurface*)surface;
1508 image = xfSurface->image;
1511 for (UINT32 x = 0; x < nrects; x++)
1514 const UINT32 width = rect->right - rect->left;
1515 const UINT32 height = rect->bottom - rect->top;
1517 XPutImage(xfc->display, appWindow->pixmap, appWindow->gc, image, rect->left, rect->top,
1518 rect->left, rect->top, width, height);
1520 XCopyArea(xfc->display, appWindow->pixmap, appWindow->handle, appWindow->gc, rect->left,
1521 rect->top, width, height, rect->left, rect->top);
1526 XFlush(xfc->display);
1531BOOL xf_AppWindowResize(xfContext* xfc, xfAppWindow* appWindow)
1534 WINPR_ASSERT(appWindow);
1536 if (appWindow->pixmap != 0)
1537 XFreePixmap(xfc->display, appWindow->pixmap);
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);
1546 return appWindow->pixmap != 0;
1549void xf_XSetTransientForHint(xfContext* xfc, xfAppWindow* window)
1552 WINPR_ASSERT(window);
1554 if (window->ownerWindowId == 0)
1557 xfAppWindow* parent = xf_rail_get_window(xfc, window->ownerWindowId);
1561 const int rc = XSetTransientForHint(xfc->display, window->handle, parent->handle);
1564 char buffer[128] = { 0 };
1565 WLog_WARN(TAG,
"XSetTransientForHint [%d]{%s}", rc,
1566 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 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.