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 = window->width;
277 UINT32 height = 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;
308 width = xfc->savedWidth;
309 height = xfc->savedHeight;
310 startX = xfc->savedPosX;
311 startY = xfc->savedPosY;
318 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, 0);
320 startX = firstMonitor->x;
321 startY = firstMonitor->y;
327 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, i);
328 startX = MIN(startX, monitor->x);
329 startY = MIN(startY, monitor->y);
345 if (xfc->_NET_WM_FULLSCREEN_MONITORS != None ||
348 xf_ResizeDesktopWindow(xfc, window, width, height);
353 XMoveWindow(xfc->display, window->handle, startX, startY);
357 xf_SendClientEvent(xfc, window->handle, xfc->_NET_WM_STATE, 4,
358 fullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE,
359 xfc->_NET_WM_STATE_FULLSCREEN, 0, 0);
367 xf_ResizeDesktopWindow(xfc, window, width, height);
368 XMoveWindow(xfc->display, window->handle, startX, startY);
374 xf_SendClientEvent(xfc, window->handle, xfc->_NET_WM_FULLSCREEN_MONITORS, 5,
375 xfc->fullscreenMonitors.top, xfc->fullscreenMonitors.bottom,
376 xfc->fullscreenMonitors.left, xfc->fullscreenMonitors.right, 1);
383 xf_SetWindowDecorations(xfc, window->handle, FALSE);
385 if (xfc->fullscreenMonitors.top)
387 xf_SendClientEvent(xfc, window->handle, xfc->_NET_WM_STATE, 4, _NET_WM_STATE_ADD,
388 xfc->fullscreenMonitors.top, 0, 0);
392 XSetWindowAttributes xswa;
393 xswa.override_redirect = True;
394 XChangeWindowAttributes(xfc->display, window->handle, CWOverrideRedirect, &xswa);
395 XRaiseWindow(xfc->display, window->handle);
396 xswa.override_redirect = False;
397 XChangeWindowAttributes(xfc->display, window->handle, CWOverrideRedirect, &xswa);
401 if (xfc->_NET_WM_STATE_MAXIMIZED_VERT != None)
404 unsigned long nitems = 0;
405 unsigned long bytes = 0;
408 if (xf_GetWindowProperty(xfc, window->handle, xfc->_NET_WM_STATE, 255, &nitems,
411 const Atom* aprop = (
const Atom*)prop;
414 for (
size_t x = 0; x < nitems; x++)
416 if (aprop[x] == xfc->_NET_WM_STATE_MAXIMIZED_VERT)
419 if (aprop[x] == xfc->_NET_WM_STATE_MAXIMIZED_HORZ)
425 xf_SendClientEvent(xfc, window->handle, xfc->_NET_WM_STATE, 4,
426 _NET_WM_STATE_REMOVE, xfc->_NET_WM_STATE_MAXIMIZED_VERT,
428 xf_SendClientEvent(xfc, window->handle, xfc->_NET_WM_STATE, 4,
429 _NET_WM_STATE_REMOVE, xfc->_NET_WM_STATE_MAXIMIZED_HORZ,
431 xfc->savedMaximizedState = state;
438 width = xfc->vscreen.area.right - xfc->vscreen.area.left + 1;
439 height = xfc->vscreen.area.bottom - xfc->vscreen.area.top + 1;
440 DEBUG_X11(
"X window move and resize %dx%d@%dx%d", startX, startY, width, height);
441 xf_ResizeDesktopWindow(xfc, window, width, height);
442 XMoveWindow(xfc->display, window->handle, startX, startY);
446 xf_SetWindowDecorations(xfc, window->handle, window->decorations);
447 xf_ResizeDesktopWindow(xfc, window, width, height);
448 XMoveWindow(xfc->display, window->handle, startX, startY);
450 if (xfc->fullscreenMonitors.top)
452 xf_SendClientEvent(xfc, window->handle, xfc->_NET_WM_STATE, 4, _NET_WM_STATE_REMOVE,
453 xfc->fullscreenMonitors.top, 0, 0);
457 if (xfc->savedMaximizedState & 0x01)
459 xf_SendClientEvent(xfc, window->handle, xfc->_NET_WM_STATE, 4, _NET_WM_STATE_ADD,
460 xfc->_NET_WM_STATE_MAXIMIZED_VERT, 0, 0);
463 if (xfc->savedMaximizedState & 0x02)
465 xf_SendClientEvent(xfc, window->handle, xfc->_NET_WM_STATE, 4, _NET_WM_STATE_ADD,
466 xfc->_NET_WM_STATE_MAXIMIZED_HORZ, 0, 0);
469 xfc->savedMaximizedState = 0;
476 BOOL xf_GetWindowProperty(xfContext* xfc, Window window, Atom property,
int length,
477 unsigned long* nitems,
unsigned long* bytes, BYTE** prop)
480 Atom actual_type = None;
481 int actual_format = 0;
483 if (property == None)
486 status = LogTagAndXGetWindowProperty(TAG, xfc->display, window, property, 0, length, False,
487 AnyPropertyType, &actual_type, &actual_format, nitems,
490 if (status != Success)
493 if (actual_type == None)
495 WLog_DBG(TAG,
"Property %lu does not exist", (
unsigned long)property);
502 static BOOL xf_GetNumberOfDesktops(xfContext* xfc, Window root,
unsigned* pval)
504 unsigned long nitems = 0;
505 unsigned long bytes = 0;
512 xf_GetWindowProperty(xfc, root, xfc->_NET_NUMBER_OF_DESKTOPS, 1, &nitems, &bytes, &bprop);
514 long* prop = (
long*)bprop;
520 if ((*prop >= 0) && (*prop <= UINT32_MAX))
522 *pval = (UINT32)*prop;
529 static BOOL xf_GetCurrentDesktop(xfContext* xfc, Window root)
531 unsigned long nitems = 0;
532 unsigned long bytes = 0;
536 if (!xf_GetNumberOfDesktops(xfc, root, &max))
542 xf_GetWindowProperty(xfc, root, xfc->_NET_CURRENT_DESKTOP, 1, &nitems, &bytes, &bprop);
544 long* prop = (
long*)bprop;
545 xfc->current_desktop = 0;
549 xfc->current_desktop = (int)MIN(max - 1, *prop);
554 static BOOL xf_GetWorkArea_NET_WORKAREA(xfContext* xfc, Window root)
557 unsigned long nitems = 0;
558 unsigned long bytes = 0;
562 xf_GetWindowProperty(xfc, root, xfc->_NET_WORKAREA, INT_MAX, &nitems, &bytes, &bprop);
563 long* prop = (
long*)bprop;
568 if ((xfc->current_desktop * 4 + 3) >= (INT64)nitems)
571 xfc->workArea.x = (UINT32)MIN(UINT32_MAX, prop[xfc->current_desktop * 4 + 0]);
572 xfc->workArea.y = (UINT32)MIN(UINT32_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]);
582 BOOL xf_GetWorkArea(xfContext* xfc)
586 Window root = DefaultRootWindow(xfc->display);
587 (void)xf_GetCurrentDesktop(xfc, root);
588 return xf_GetWorkArea_NET_WORKAREA(xfc, root);
591 void xf_SetWindowDecorations(xfContext* xfc, Window window, BOOL show)
593 PropMotifWmHints hints = { .decorations = (show) ? MWM_DECOR_ALL : 0,
594 .functions = MWM_FUNC_ALL,
595 .flags = MWM_HINTS_DECORATIONS | MWM_HINTS_FUNCTIONS,
599 LogTagAndXChangeProperty(TAG, xfc->display, window, xfc->_MOTIF_WM_HINTS, xfc->_MOTIF_WM_HINTS,
600 32, PropModeReplace, (BYTE*)&hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
603 void xf_SetWindowUnlisted(xfContext* xfc, Window window)
606 Atom window_state[] = { xfc->_NET_WM_STATE_SKIP_PAGER, xfc->_NET_WM_STATE_SKIP_TASKBAR };
607 LogTagAndXChangeProperty(TAG, xfc->display, window, xfc->_NET_WM_STATE, XA_ATOM, 32,
608 PropModeReplace, (BYTE*)window_state, 2);
611 static void xf_SetWindowPID(xfContext* xfc, Window window, pid_t pid)
619 am_wm_pid = xfc->_NET_WM_PID;
620 LogTagAndXChangeProperty(TAG, xfc->display, window, am_wm_pid, XA_CARDINAL, 32, PropModeReplace,
624 static const char* get_shm_id(
void)
626 static char shm_id[64];
627 (void)sprintf_s(shm_id,
sizeof(shm_id),
"/com.freerdp.xfreerdp.tsmf_%016X",
628 GetCurrentProcessId());
632 Window xf_CreateDummyWindow(xfContext* xfc)
634 return XCreateWindow(xfc->display, RootWindowOfScreen(xfc->screen), xfc->workArea.x,
635 xfc->workArea.y, 1, 1, 0, xfc->depth, InputOutput, xfc->visual,
636 xfc->attribs_mask, &xfc->attribs);
639 void xf_DestroyDummyWindow(xfContext* xfc, Window window)
642 XDestroyWindow(xfc->display, window);
645 xfWindow* 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), xfc->workArea.x,
668 xfc->workArea.y, xfc->workArea.width, xfc->workArea.height, 0, xfc->depth,
669 InputOutput, xfc->visual, xfc->attribs_mask, &xfc->attribs);
670 window->shmid = shm_open(get_shm_id(), (O_CREAT | O_RDWR), (S_IREAD | S_IWRITE));
672 if (window->shmid < 0)
674 DEBUG_X11(
"xf_CreateDesktopWindow: failed to get access to shared memory - shmget()\n");
678 int rc = ftruncate(window->shmid,
sizeof(window->handle));
681 #ifdef WITH_DEBUG_X11
682 char ebuffer[256] = { 0 };
683 DEBUG_X11(
"ftruncate failed with %s [%d]", winpr_strerror(rc, ebuffer,
sizeof(ebuffer)),
689 void* mem = mmap(0,
sizeof(window->handle), PROT_READ | PROT_WRITE, MAP_SHARED,
692 if (mem == MAP_FAILED)
695 "xf_CreateDesktopWindow: failed to assign pointer to the memory address - "
701 *window->xfwin = window->handle;
706 classHints = XAllocClassHint();
710 classHints->res_name =
"xfreerdp";
712 char* res_class = NULL;
715 res_class = _strdup(WmClass);
717 res_class = _strdup(
"xfreerdp");
719 classHints->res_class = res_class;
720 XSetClassHint(xfc->display, window->handle, classHints);
725 xf_ResizeDesktopWindow(xfc, window, width, height);
726 xf_SetWindowDecorations(xfc, window->handle, window->decorations);
727 xf_SetWindowPID(xfc, window->handle, 0);
728 input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
729 VisibilityChangeMask | FocusChangeMask | StructureNotifyMask | PointerMotionMask |
730 ExposureMask | PropertyChangeMask;
732 if (xfc->grab_keyboard)
733 input_mask |= EnterWindowMask | LeaveWindowMask;
735 LogTagAndXChangeProperty(TAG, xfc->display, window->handle, xfc->_NET_WM_ICON, XA_CARDINAL, 32,
736 PropModeReplace, (BYTE*)xf_icon_prop, ARRAYSIZE(xf_icon_prop));
739 XReparentWindow(xfc->display, window->handle, parentWindow, 0, 0);
741 XSelectInput(xfc->display, window->handle, input_mask);
742 XClearWindow(xfc->display, window->handle);
743 xf_SetWindowTitleText(xfc, window->handle, name);
744 XMapWindow(xfc->display, window->handle);
745 xf_input_init(xfc, window->handle);
753 XMaskEvent(xfc->display, VisibilityChangeMask, &xevent);
754 }
while (xevent.type != VisibilityNotify);
763 XMoveWindow(xfc->display, window->handle, 0, 0);
768 XMoveWindow(xfc->display, window->handle,
773 window->floatbar = xf_floatbar_new(xfc, window->handle, name,
776 if (xfc->_XWAYLAND_MAY_GRAB_KEYBOARD)
777 xf_SendClientEvent(xfc, window->handle, xfc->_XWAYLAND_MAY_GRAB_KEYBOARD, 1, 1);
782 void xf_ResizeDesktopWindow(xfContext* xfc, xfWindow* window,
int width,
int height)
784 XSizeHints* size_hints = NULL;
785 rdpSettings* settings = NULL;
790 settings = xfc->common.context.settings;
791 WINPR_ASSERT(settings);
793 if (!(size_hints = XAllocSizeHints()))
796 size_hints->flags = PMinSize | PMaxSize | PWinGravity;
797 size_hints->win_gravity = NorthWestGravity;
798 size_hints->min_width = size_hints->min_height = 1;
799 size_hints->max_width = size_hints->max_height = 16384;
800 XResizeWindow(xfc->display, window->handle, width, height);
807 if (!xfc->fullscreen)
811 size_hints->min_width = size_hints->max_width = width;
812 size_hints->min_height = size_hints->max_height = height;
816 XSetWMNormalHints(xfc->display, window->handle, size_hints);
820 void xf_DestroyDesktopWindow(xfContext* xfc, xfWindow* window)
825 if (xfc->window == window)
828 xf_floatbar_free(window->floatbar);
831 XFreeGC(xfc->display, window->gc);
835 XUnmapWindow(xfc->display, window->handle);
836 XDestroyWindow(xfc->display, window->handle);
840 munmap(0,
sizeof(*window->xfwin));
842 if (window->shmid >= 0)
843 close(window->shmid);
845 shm_unlink(get_shm_id());
846 window->xfwin = (Window*)-1;
851 void xf_SetWindowStyle(xfContext* xfc, xfAppWindow* appWindow, UINT32 style, UINT32 ex_style)
853 Atom window_type = 0;
854 BOOL redirect = FALSE;
856 window_type = xfc->_NET_WM_WINDOW_TYPE_NORMAL;
858 if ((ex_style & WS_EX_NOACTIVATE) || (ex_style & WS_EX_TOOLWINDOW))
861 appWindow->is_transient = TRUE;
862 xf_SetWindowUnlisted(xfc, appWindow->handle);
863 window_type = xfc->_NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
869 else if (ex_style & WS_EX_TOPMOST)
871 window_type = xfc->_NET_WM_WINDOW_TYPE_NORMAL;
874 if (style & WS_POPUP)
876 window_type = xfc->_NET_WM_WINDOW_TYPE_DIALOG;
879 if (!((ex_style & WS_EX_DLGMODALFRAME) || (ex_style & WS_EX_LAYERED) ||
880 (style & WS_SYSMENU)))
882 appWindow->is_transient = TRUE;
885 xf_SetWindowUnlisted(xfc, appWindow->handle);
889 if (!(style == 0 && ex_style == 0))
891 xf_SetWindowActions(xfc, appWindow);
904 XSetWindowAttributes attrs = { 0 };
905 attrs.override_redirect = redirect ? True : False;
906 XChangeWindowAttributes(xfc->display, appWindow->handle, CWOverrideRedirect, &attrs);
909 LogTagAndXChangeProperty(TAG, xfc->display, appWindow->handle, xfc->_NET_WM_WINDOW_TYPE,
910 XA_ATOM, 32, PropModeReplace, (BYTE*)&window_type, 1);
912 if (ex_style & (WS_EX_CONTROLPARENT | WS_EX_TOOLWINDOW | WS_EX_DLGMODALFRAME))
913 xf_XSetTransientForHint(xfc, appWindow);
915 if (((ex_style & WS_EX_TOPMOST) != 0) && ((ex_style & WS_EX_TOOLWINDOW) == 0))
917 xf_SendClientEvent(xfc, appWindow->handle, xfc->_NET_WM_STATE, 4, _NET_WM_STATE_ADD,
918 xfc->_NET_WM_STATE_ABOVE, 0, 0);
922 xf_SendClientEvent(xfc, appWindow->handle, xfc->_NET_WM_STATE, 4, _NET_WM_STATE_REMOVE,
923 xfc->_NET_WM_STATE_ABOVE, 0, 0);
927 void xf_SetWindowActions(xfContext* xfc, xfAppWindow* appWindow)
929 Atom allowed_actions[] = {
930 xfc->NET_WM_ACTION_CLOSE, xfc->NET_WM_ACTION_MINIMIZE,
931 xfc->NET_WM_ACTION_MOVE, xfc->NET_WM_ACTION_RESIZE,
932 xfc->NET_WM_ACTION_MAXIMIZE_HORZ, xfc->NET_WM_ACTION_MAXIMIZE_VERT,
933 xfc->NET_WM_ACTION_FULLSCREEN, xfc->NET_WM_ACTION_CHANGE_DESKTOP
936 if (!(appWindow->dwStyle & WS_SYSMENU))
937 allowed_actions[0] = 0;
939 if (!(appWindow->dwStyle & WS_MINIMIZEBOX))
940 allowed_actions[1] = 0;
942 if (!(appWindow->dwStyle & WS_SIZEBOX))
943 allowed_actions[3] = 0;
945 if (!(appWindow->dwStyle & WS_MAXIMIZEBOX))
947 allowed_actions[4] = 0;
948 allowed_actions[5] = 0;
949 allowed_actions[6] = 0;
952 XChangeProperty(xfc->display, appWindow->handle, xfc->NET_WM_ALLOWED_ACTIONS, XA_ATOM, 32,
953 PropModeReplace, (
unsigned char*)&allowed_actions, 8);
956 void xf_SetWindowText(xfContext* xfc, xfAppWindow* appWindow,
const char* name)
958 xf_SetWindowTitleText(xfc, appWindow->handle, name);
961 static void xf_FixWindowCoordinates(xfContext* xfc,
int* x,
int* y,
int* width,
int* height)
963 int vscreen_width = 0;
964 int vscreen_height = 0;
965 vscreen_width = xfc->vscreen.area.right - xfc->vscreen.area.left + 1;
966 vscreen_height = xfc->vscreen.area.bottom - xfc->vscreen.area.top + 1;
968 if (*x < xfc->vscreen.area.left)
971 *x = xfc->vscreen.area.left;
974 if (*y < xfc->vscreen.area.top)
977 *y = xfc->vscreen.area.top;
980 if (*width > vscreen_width)
982 *width = vscreen_width;
985 if (*height > vscreen_height)
987 *height = vscreen_height;
1001 int xf_AppWindowInit(xfContext* xfc, xfAppWindow* appWindow)
1003 if (!xfc || !appWindow)
1006 xf_SetWindowDecorations(xfc, appWindow->handle, appWindow->decorations);
1007 xf_SetWindowStyle(xfc, appWindow, appWindow->dwStyle, appWindow->dwExStyle);
1008 xf_SetWindowPID(xfc, appWindow->handle, 0);
1009 xf_ShowWindow(xfc, appWindow, WINDOW_SHOW);
1010 XClearWindow(xfc->display, appWindow->handle);
1011 XMapWindow(xfc->display, appWindow->handle);
1013 xf_MoveWindow(xfc, appWindow, appWindow->x, appWindow->y, appWindow->width, appWindow->height);
1014 xf_SetWindowText(xfc, appWindow, appWindow->title);
1018 BOOL xf_AppWindowCreate(xfContext* xfc, xfAppWindow* appWindow)
1020 XGCValues gcv = { 0 };
1022 XWMHints* InputModeHint = NULL;
1023 XClassHint* class_hints = NULL;
1024 const rdpSettings* settings = NULL;
1027 WINPR_ASSERT(appWindow);
1029 settings = xfc->common.context.settings;
1030 WINPR_ASSERT(settings);
1032 xf_FixWindowCoordinates(xfc, &appWindow->x, &appWindow->y, &appWindow->width,
1033 &appWindow->height);
1034 appWindow->shmid = -1;
1035 appWindow->decorations = FALSE;
1036 appWindow->fullscreen = FALSE;
1037 appWindow->local_move.state = LMS_NOT_ACTIVE;
1038 appWindow->is_mapped = FALSE;
1039 appWindow->is_transient = FALSE;
1040 appWindow->rail_state = 0;
1041 appWindow->maxVert = FALSE;
1042 appWindow->maxHorz = FALSE;
1043 appWindow->minimized = FALSE;
1044 appWindow->rail_ignore_configure = FALSE;
1046 WINPR_ASSERT(xfc->depth != 0);
1048 XCreateWindow(xfc->display, RootWindowOfScreen(xfc->screen), appWindow->x, appWindow->y,
1049 appWindow->width, appWindow->height, 0, xfc->depth, InputOutput, xfc->visual,
1050 xfc->attribs_mask, &xfc->attribs);
1052 if (!appWindow->handle)
1055 appWindow->gc = XCreateGC(xfc->display, appWindow->handle, GCGraphicsExposures, &gcv);
1057 if (!xf_AppWindowResize(xfc, appWindow))
1060 class_hints = XAllocClassHint();
1064 char* strclass = NULL;
1068 strclass = _strdup(WmClass);
1072 winpr_asprintf(&strclass, &size,
"RAIL:%08" PRIX64
"", appWindow->windowId);
1074 class_hints->res_class = strclass;
1075 class_hints->res_name =
"RAIL";
1076 XSetClassHint(xfc->display, appWindow->handle, class_hints);
1082 InputModeHint = XAllocWMHints();
1083 InputModeHint->flags = (1L << 0);
1084 InputModeHint->input = True;
1085 XSetWMHints(xfc->display, appWindow->handle, InputModeHint);
1086 XFree(InputModeHint);
1087 XSetWMProtocols(xfc->display, appWindow->handle, &(xfc->WM_DELETE_WINDOW), 1);
1088 input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
1089 EnterWindowMask | LeaveWindowMask | PointerMotionMask | Button1MotionMask |
1090 Button2MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask |
1091 ButtonMotionMask | KeymapStateMask | ExposureMask | VisibilityChangeMask |
1092 StructureNotifyMask | SubstructureNotifyMask | SubstructureRedirectMask |
1093 FocusChangeMask | PropertyChangeMask | ColormapChangeMask | OwnerGrabButtonMask;
1094 XSelectInput(xfc->display, appWindow->handle, input_mask);
1096 if (xfc->_XWAYLAND_MAY_GRAB_KEYBOARD)
1097 xf_SendClientEvent(xfc, appWindow->handle, xfc->_XWAYLAND_MAY_GRAB_KEYBOARD, 1, 1);
1102 void xf_SetWindowMinMaxInfo(xfContext* xfc, xfAppWindow* appWindow,
int maxWidth,
int maxHeight,
1103 int maxPosX,
int maxPosY,
int minTrackWidth,
int minTrackHeight,
1104 int maxTrackWidth,
int maxTrackHeight)
1106 XSizeHints* size_hints = NULL;
1107 size_hints = XAllocSizeHints();
1111 size_hints->flags = PMinSize | PMaxSize | PResizeInc;
1112 size_hints->min_width = minTrackWidth;
1113 size_hints->min_height = minTrackHeight;
1114 size_hints->max_width = maxTrackWidth;
1115 size_hints->max_height = maxTrackHeight;
1117 size_hints->width_inc = size_hints->height_inc = 1;
1118 XSetWMNormalHints(xfc->display, appWindow->handle, size_hints);
1123 void xf_StartLocalMoveSize(xfContext* xfc, xfAppWindow* appWindow,
int direction,
int x,
int y)
1125 if (appWindow->local_move.state != LMS_NOT_ACTIVE)
1132 appWindow->local_move.root_x = x;
1133 appWindow->local_move.root_y = y;
1134 appWindow->local_move.state = LMS_STARTING;
1135 appWindow->local_move.direction = direction;
1140 xfc, appWindow->handle,
1141 xfc->_NET_WM_MOVERESIZE,
1150 void xf_EndLocalMoveSize(xfContext* xfc, xfAppWindow* appWindow)
1152 if (appWindow->local_move.state == LMS_NOT_ACTIVE)
1155 if (appWindow->local_move.state == LMS_STARTING)
1164 xfc, appWindow->handle,
1165 xfc->_NET_WM_MOVERESIZE,
1167 appWindow->local_move.root_x,
1168 appWindow->local_move.root_y,
1169 _NET_WM_MOVERESIZE_CANCEL,
1174 appWindow->local_move.state = LMS_NOT_ACTIVE;
1177 void xf_MoveWindow(xfContext* xfc, xfAppWindow* appWindow,
int x,
int y,
int width,
int height)
1179 BOOL resize = FALSE;
1181 if ((width * height) < 1)
1184 if ((appWindow->width != width) || (appWindow->height != height))
1187 if (appWindow->local_move.state == LMS_STARTING || appWindow->local_move.state == LMS_ACTIVE)
1192 appWindow->width = width;
1193 appWindow->height = height;
1196 XMoveResizeWindow(xfc->display, appWindow->handle, x, y, width, height);
1198 XMoveWindow(xfc->display, appWindow->handle, x, y);
1200 xf_UpdateWindowArea(xfc, appWindow, 0, 0, width, height);
1203 void xf_ShowWindow(xfContext* xfc, xfAppWindow* appWindow, BYTE state)
1206 WINPR_ASSERT(appWindow);
1211 XWithdrawWindow(xfc->display, appWindow->handle, xfc->screen_number);
1214 case WINDOW_SHOW_MINIMIZED:
1215 appWindow->minimized = TRUE;
1216 XIconifyWindow(xfc->display, appWindow->handle, xfc->screen_number);
1219 case WINDOW_SHOW_MAXIMIZED:
1221 appWindow->maxHorz = TRUE;
1222 appWindow->maxVert = TRUE;
1223 xf_SendClientEvent(xfc, appWindow->handle, xfc->_NET_WM_STATE, 4, _NET_WM_STATE_ADD,
1224 xfc->_NET_WM_STATE_MAXIMIZED_VERT, xfc->_NET_WM_STATE_MAXIMIZED_HORZ,
1235 if (appWindow->rail_state == WINDOW_SHOW_MAXIMIZED)
1237 xf_UpdateWindowArea(xfc, appWindow, 0, 0, appWindow->windowWidth,
1238 appWindow->windowHeight);
1245 xf_SendClientEvent(xfc, appWindow->handle, xfc->_NET_WM_STATE, 4, _NET_WM_STATE_REMOVE,
1246 xfc->_NET_WM_STATE_MAXIMIZED_VERT, xfc->_NET_WM_STATE_MAXIMIZED_HORZ,
1255 if (appWindow->rail_state == WINDOW_SHOW_MAXIMIZED)
1256 appWindow->rail_ignore_configure = TRUE;
1258 if (appWindow->is_transient)
1259 xf_SetWindowUnlisted(xfc, appWindow->handle);
1261 XMapWindow(xfc->display, appWindow->handle);
1268 appWindow->rail_state = state;
1269 XFlush(xfc->display);
1272 void xf_SetWindowRects(xfContext* xfc, xfAppWindow* appWindow,
RECTANGLE_16* rects,
int nrects)
1274 XRectangle* xrects = NULL;
1280 xrects = (XRectangle*)calloc(nrects,
sizeof(XRectangle));
1282 for (
int i = 0; i < nrects; i++)
1284 xrects[i].x = rects[i].left;
1285 xrects[i].y = rects[i].top;
1286 xrects[i].width = rects[i].right - rects[i].left;
1287 xrects[i].height = rects[i].bottom - rects[i].top;
1290 XShapeCombineRectangles(xfc->display, appWindow->handle, ShapeBounding, 0, 0, xrects, nrects,
1296 void xf_SetWindowVisibilityRects(xfContext* xfc, xfAppWindow* appWindow, UINT32 rectsOffsetX,
1299 XRectangle* xrects = NULL;
1305 xrects = (XRectangle*)calloc(nrects,
sizeof(XRectangle));
1307 for (
int i = 0; i < nrects; i++)
1309 xrects[i].x = rects[i].left;
1310 xrects[i].y = rects[i].top;
1311 xrects[i].width = rects[i].right - rects[i].left;
1312 xrects[i].height = rects[i].bottom - rects[i].top;
1315 XShapeCombineRectangles(xfc->display, appWindow->handle, ShapeBounding, rectsOffsetX,
1316 rectsOffsetY, xrects, nrects, ShapeSet, 0);
1321 void xf_UpdateWindowArea(xfContext* xfc, xfAppWindow* appWindow,
int x,
int y,
int width,
1326 const rdpSettings* settings = NULL;
1330 settings = xfc->common.context.settings;
1331 WINPR_ASSERT(settings);
1333 if (appWindow == NULL)
1336 if (appWindow->surfaceId < UINT16_MAX)
1339 ax = x + appWindow->windowOffsetX;
1340 ay = y + appWindow->windowOffsetY;
1342 if (ax + width > appWindow->windowOffsetX + appWindow->width)
1343 width = (appWindow->windowOffsetX + appWindow->width - 1) - ax;
1345 if (ay + height > appWindow->windowOffsetY + appWindow->height)
1346 height = (appWindow->windowOffsetY + appWindow->height - 1) - ay;
1352 XPutImage(xfc->display, appWindow->pixmap, appWindow->gc, xfc->image, ax, ay, x, y, width,
1356 XCopyArea(xfc->display, appWindow->pixmap, appWindow->handle, appWindow->gc, x, y, width,
1358 XFlush(xfc->display);
1362 static void xf_AppWindowDestroyImage(xfAppWindow* appWindow)
1364 WINPR_ASSERT(appWindow);
1365 if (appWindow->image)
1367 appWindow->image->data = NULL;
1368 XDestroyImage(appWindow->image);
1369 appWindow->image = NULL;
1373 void xf_DestroyWindow(xfContext* xfc, xfAppWindow* appWindow)
1378 if (xfc->appWindow == appWindow)
1379 xfc->appWindow = NULL;
1382 XFreeGC(xfc->display, appWindow->gc);
1384 if (appWindow->pixmap)
1385 XFreePixmap(xfc->display, appWindow->pixmap);
1387 xf_AppWindowDestroyImage(appWindow);
1389 if (appWindow->handle)
1391 XUnmapWindow(xfc->display, appWindow->handle);
1392 XDestroyWindow(xfc->display, appWindow->handle);
1395 if (appWindow->xfwin)
1396 munmap(0,
sizeof(*appWindow->xfwin));
1398 if (appWindow->shmid >= 0)
1399 close(appWindow->shmid);
1401 shm_unlink(get_shm_id());
1402 appWindow->xfwin = (Window*)-1;
1403 appWindow->shmid = -1;
1404 free(appWindow->title);
1405 free(appWindow->windowRects);
1406 free(appWindow->visibilityRects);
1410 xfAppWindow* xf_AppWindowFromX11Window(xfContext* xfc, Window wnd)
1412 ULONG_PTR* pKeys = NULL;
1415 if (!xfc->railWindows)
1418 size_t count = HashTable_GetKeys(xfc->railWindows, &pKeys);
1420 for (
size_t index = 0; index < count; index++)
1422 xfAppWindow* appWindow = xf_rail_get_window(xfc, *(UINT64*)pKeys[index]);
1430 if (appWindow->handle == wnd)
1441 UINT xf_AppUpdateWindowFromSurface(xfContext* xfc, gdiGfxSurface* surface)
1443 XImage* image = NULL;
1444 UINT rc = ERROR_INTERNAL_ERROR;
1447 WINPR_ASSERT(surface);
1449 xfAppWindow* appWindow = xf_rail_get_window(xfc, surface->windowId);
1452 WLog_VRB(TAG,
"Failed to find a window for id=0x%08" PRIx64, surface->windowId);
1453 return CHANNEL_RC_OK;
1458 const RECTANGLE_16* rects = region16_rects(&surface->invalidRegion, &nrects);
1463 if (appWindow->surfaceId != surface->surfaceId)
1465 xf_AppWindowDestroyImage(appWindow);
1466 appWindow->surfaceId = surface->surfaceId;
1468 if (appWindow->width != (INT64)surface->width)
1469 xf_AppWindowDestroyImage(appWindow);
1470 if (appWindow->height != (INT64)surface->height)
1471 xf_AppWindowDestroyImage(appWindow);
1473 if (!appWindow->image)
1475 WINPR_ASSERT(xfc->depth != 0);
1476 appWindow->image = XCreateImage(xfc->display, xfc->visual, xfc->depth, ZPixmap, 0,
1477 (
char*)surface->data, surface->width, surface->height,
1478 xfc->scanline_pad, surface->scanline);
1479 if (!appWindow->image)
1482 "Failed create a XImage[%" PRIu32
"x%" PRIu32
", scanline=%" PRIu32
1483 ", bpp=%" PRIu32
"] for window id=0x%08" PRIx64,
1484 surface->width, surface->height, surface->scanline, xfc->depth,
1488 appWindow->image->byte_order = LSBFirst;
1489 appWindow->image->bitmap_bit_order = LSBFirst;
1492 image = appWindow->image;
1496 xfGfxSurface* xfSurface = (xfGfxSurface*)surface;
1497 image = xfSurface->image;
1500 for (UINT32 x = 0; x < nrects; x++)
1503 const UINT32 width = rect->right - rect->left;
1504 const UINT32 height = rect->bottom - rect->top;
1506 XPutImage(xfc->display, appWindow->pixmap, appWindow->gc, image, rect->left, rect->top,
1507 rect->left, rect->top, width, height);
1509 XCopyArea(xfc->display, appWindow->pixmap, appWindow->handle, appWindow->gc, rect->left,
1510 rect->top, width, height, rect->left, rect->top);
1515 XFlush(xfc->display);
1520 BOOL xf_AppWindowResize(xfContext* xfc, xfAppWindow* appWindow)
1523 WINPR_ASSERT(appWindow);
1525 if (appWindow->pixmap != 0)
1526 XFreePixmap(xfc->display, appWindow->pixmap);
1528 WINPR_ASSERT(xfc->depth != 0);
1530 XCreatePixmap(xfc->display, xfc->drawable, appWindow->width, appWindow->height, xfc->depth);
1531 xf_AppWindowDestroyImage(appWindow);
1533 return appWindow->pixmap != 0;
1536 void xf_XSetTransientForHint(xfContext* xfc, xfAppWindow* window)
1539 WINPR_ASSERT(window);
1541 if (window->ownerWindowId == 0)
1544 xfAppWindow* parent = xf_rail_get_window(xfc, window->ownerWindowId);
1548 const int rc = XSetTransientForHint(xfc->display, window->handle, parent->handle);
1551 char buffer[128] = { 0 };
1552 WLog_WARN(TAG,
"XSetTransientForHint [%d]{%s}", rc,
1553 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.