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 LogDynAndXChangeProperty(xfc->log, 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 LogDynAndXSendEvent(xfc->log, xfc->display, RootWindowOfScreen(xfc->screen), False,
253 SubstructureRedirectMask | SubstructureNotifyMask, &xevent);
254 LogDynAndXSync(xfc->log, 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 LogDynAndXMoveWindow(xfc->log, 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 LogDynAndXMoveWindow(xfc->log, 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 LogDynAndXChangeWindowAttributes(xfc->log, xfc->display, window->handle,
391 CWOverrideRedirect, &xswa);
392 LogDynAndXRaiseWindow(xfc->log, xfc->display, window->handle);
393 xswa.override_redirect = False;
394 LogDynAndXChangeWindowAttributes(xfc->log, xfc->display, window->handle,
395 CWOverrideRedirect, &xswa);
399 if (xfc->NET_WM_STATE_MAXIMIZED_VERT != None)
402 unsigned long nitems = 0;
403 unsigned long bytes = 0;
406 if (xf_GetWindowProperty(xfc, window->handle, xfc->NET_WM_STATE, 255, &nitems,
409 const Atom* aprop = (
const Atom*)prop;
412 for (
size_t x = 0; x < nitems; x++)
414 if (aprop[x] == xfc->NET_WM_STATE_MAXIMIZED_VERT)
417 if (aprop[x] == xfc->NET_WM_STATE_MAXIMIZED_HORZ)
423 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4,
424 NET_WM_STATE_REMOVE, xfc->NET_WM_STATE_MAXIMIZED_VERT, 0,
426 xf_SendClientEvent(xfc, window->handle, xfc->NET_WM_STATE, 4,
427 NET_WM_STATE_REMOVE, xfc->NET_WM_STATE_MAXIMIZED_HORZ, 0,
429 xfc->savedMaximizedState = state;
436 width = xfc->vscreen.area.right - xfc->vscreen.area.left + 1;
437 height = xfc->vscreen.area.bottom - xfc->vscreen.area.top + 1;
438 DEBUG_X11(
"X window move and resize %dx%d@%dx%d", startX, startY, width, height);
439 xf_ResizeDesktopWindow(xfc, window, WINPR_ASSERTING_INT_CAST(
int, width),
440 WINPR_ASSERTING_INT_CAST(
int, height));
441 LogDynAndXMoveWindow(xfc->log, xfc->display, window->handle, startX, startY);
445 xf_SetWindowDecorations(xfc, window->handle, window->decorations);
446 xf_ResizeDesktopWindow(xfc, window, WINPR_ASSERTING_INT_CAST(
int, width),
447 WINPR_ASSERTING_INT_CAST(
int, height));
448 LogDynAndXMoveWindow(xfc->log, xfc->display, window->handle, startX, startY);
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;
476BOOL 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 = LogDynAndXGetWindowProperty(xfc->log, 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);
502static 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;
529static 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);
554static 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 = (INT32)MIN(INT32_MAX, prop[xfc->current_desktop * 4 + 0]);
572 xfc->workArea.y = (INT32)MIN(INT32_MAX, prop[xfc->current_desktop * 4 + 1]);
573 xfc->workArea.width = (UINT32)MIN(UINT32_MAX, prop[xfc->current_desktop * 4 + 2]);
574 xfc->workArea.height = (UINT32)MIN(UINT32_MAX, prop[xfc->current_desktop * 4 + 3]);
582BOOL xf_GetWorkArea(xfContext* xfc)
586 Window root = DefaultRootWindow(xfc->display);
587 (void)xf_GetCurrentDesktop(xfc, root);
588 return xf_GetWorkArea_NET_WORKAREA(xfc, root);
591void 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 LogDynAndXChangeProperty(xfc->log, xfc->display, window, xfc->MOTIF_WM_HINTS,
600 xfc->MOTIF_WM_HINTS, 32, PropModeReplace, (BYTE*)&hints,
601 PROP_MOTIF_WM_HINTS_ELEMENTS);
604void xf_SetWindowUnlisted(xfContext* xfc, Window window)
607 Atom window_state[] = { xfc->NET_WM_STATE_SKIP_PAGER, xfc->NET_WM_STATE_SKIP_TASKBAR };
608 LogDynAndXChangeProperty(xfc->log, xfc->display, window, xfc->NET_WM_STATE, XA_ATOM, 32,
609 PropModeReplace, (BYTE*)window_state, 2);
612static void xf_SetWindowPID(xfContext* xfc, Window window, pid_t pid)
620 am_wm_pid = xfc->NET_WM_PID;
621 LogDynAndXChangeProperty(xfc->log, xfc->display, window, am_wm_pid, XA_CARDINAL, 32,
622 PropModeReplace, (BYTE*)&pid, 1);
625static const char* get_shm_id(
void)
627 static char shm_id[64];
628 (void)sprintf_s(shm_id,
sizeof(shm_id),
"/com.freerdp.xfreerdp.tsmf_%016X",
629 GetCurrentProcessId());
633Window xf_CreateDummyWindow(xfContext* xfc)
635 return LogDynAndXCreateWindow(
636 xfc->log, xfc->display, RootWindowOfScreen(xfc->screen),
637 WINPR_ASSERTING_INT_CAST(
int, xfc->workArea.x),
638 WINPR_ASSERTING_INT_CAST(
int, xfc->workArea.y), 1, 1, 0, xfc->depth, InputOutput,
639 xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->attribs_mask), &xfc->attribs);
642void xf_DestroyDummyWindow(xfContext* xfc, Window window)
645 LogDynAndXDestroyWindow(xfc->log, xfc->display, window);
648xfWindow* xf_CreateDesktopWindow(xfContext* xfc,
char* name,
int width,
int height)
650 XEvent xevent = { 0 };
652 XClassHint* classHints = NULL;
653 xfWindow* window = (xfWindow*)calloc(1,
sizeof(xfWindow));
658 rdpSettings* settings = xfc->common.context.settings;
659 WINPR_ASSERT(settings);
662 window->width = width;
663 window->height = height;
664 window->decorations = xfc->decorations;
665 window->is_mapped = FALSE;
666 window->is_transient = FALSE;
668 WINPR_ASSERT(xfc->depth != 0);
669 window->handle = LogDynAndXCreateWindow(
670 xfc->log, xfc->display, RootWindowOfScreen(xfc->screen),
671 WINPR_ASSERTING_INT_CAST(
int, xfc->workArea.x),
672 WINPR_ASSERTING_INT_CAST(
int, xfc->workArea.y), xfc->workArea.width, xfc->workArea.height,
673 0, xfc->depth, InputOutput, xfc->visual,
674 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->attribs_mask), &xfc->attribs);
675 window->shmid = shm_open(get_shm_id(), (O_CREAT | O_RDWR), (S_IREAD | S_IWRITE));
677 if (window->shmid < 0)
679 DEBUG_X11(
"xf_CreateDesktopWindow: failed to get access to shared memory - shmget()\n");
683 int rc = ftruncate(window->shmid,
sizeof(window->handle));
687 char ebuffer[256] = { 0 };
688 DEBUG_X11(
"ftruncate failed with %s [%d]", winpr_strerror(rc, ebuffer,
sizeof(ebuffer)),
694 void* mem = mmap(0,
sizeof(window->handle), PROT_READ | PROT_WRITE, MAP_SHARED,
697 if (mem == MAP_FAILED)
700 "xf_CreateDesktopWindow: failed to assign pointer to the memory address - "
706 *window->xfwin = window->handle;
711 classHints = XAllocClassHint();
715 classHints->res_name =
"xfreerdp";
717 char* res_class = NULL;
720 res_class = _strdup(WmClass);
722 res_class = _strdup(
"xfreerdp");
724 classHints->res_class = res_class;
725 XSetClassHint(xfc->display, window->handle, classHints);
730 xf_ResizeDesktopWindow(xfc, window, width, height);
731 xf_SetWindowDecorations(xfc, window->handle, window->decorations);
732 xf_SetWindowPID(xfc, window->handle, 0);
733 input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
734 VisibilityChangeMask | FocusChangeMask | StructureNotifyMask | PointerMotionMask |
735 ExposureMask | PropertyChangeMask;
737 if (xfc->grab_keyboard)
738 input_mask |= EnterWindowMask | LeaveWindowMask;
740 LogDynAndXChangeProperty(xfc->log, xfc->display, window->handle, xfc->NET_WM_ICON, XA_CARDINAL,
741 32, PropModeReplace, (BYTE*)xf_icon_prop, ARRAYSIZE(xf_icon_prop));
744 LogDynAndXReparentWindow(xfc->log, xfc->display, window->handle, parentWindow, 0, 0);
746 XSelectInput(xfc->display, window->handle, input_mask);
747 LogDynAndXClearWindow(xfc->log, xfc->display, window->handle);
748 xf_SetWindowTitleText(xfc, window->handle, name);
749 LogDynAndXMapWindow(xfc->log, xfc->display, window->handle);
750 xf_input_init(xfc, window->handle);
758 XMaskEvent(xfc->display, VisibilityChangeMask, &xevent);
759 }
while (xevent.type != VisibilityNotify);
768 LogDynAndXMoveWindow(xfc->log, xfc->display, window->handle, 0, 0);
773 LogDynAndXMoveWindow(xfc->log, xfc->display, window->handle,
774 WINPR_ASSERTING_INT_CAST(
776 WINPR_ASSERTING_INT_CAST(
780 window->floatbar = xf_floatbar_new(xfc, window->handle, name,
783 if (xfc->XWAYLAND_MAY_GRAB_KEYBOARD)
784 xf_SendClientEvent(xfc, window->handle, xfc->XWAYLAND_MAY_GRAB_KEYBOARD, 1, 1);
789void xf_ResizeDesktopWindow(xfContext* xfc, xfWindow* window,
int width,
int height)
791 XSizeHints* size_hints = NULL;
792 rdpSettings* settings = NULL;
797 settings = xfc->common.context.settings;
798 WINPR_ASSERT(settings);
800 if (!(size_hints = XAllocSizeHints()))
803 size_hints->flags = PMinSize | PMaxSize | PWinGravity;
804 size_hints->win_gravity = NorthWestGravity;
805 size_hints->min_width = size_hints->min_height = 1;
806 size_hints->max_width = size_hints->max_height = 16384;
807 LogDynAndXResizeWindow(xfc->log, xfc->display, window->handle,
808 WINPR_ASSERTING_INT_CAST(uint32_t, width),
809 WINPR_ASSERTING_INT_CAST(uint32_t, height));
816 if (!xfc->fullscreen)
820 size_hints->min_width = size_hints->max_width = width;
821 size_hints->min_height = size_hints->max_height = height;
825 XSetWMNormalHints(xfc->display, window->handle, size_hints);
829void xf_DestroyDesktopWindow(xfContext* xfc, xfWindow* window)
834 if (xfc->window == window)
837 xf_floatbar_free(window->floatbar);
840 LogDynAndXFreeGC(xfc->log, xfc->display, window->gc);
844 LogDynAndXUnmapWindow(xfc->log, xfc->display, window->handle);
845 LogDynAndXDestroyWindow(xfc->log, xfc->display, window->handle);
849 munmap(0,
sizeof(*window->xfwin));
851 if (window->shmid >= 0)
852 close(window->shmid);
854 shm_unlink(get_shm_id());
855 window->xfwin = (Window*)-1;
860void xf_SetWindowStyle(xfContext* xfc, xfAppWindow* appWindow, UINT32 style, UINT32 ex_style)
862 Atom window_type = 0;
863 BOOL redirect = FALSE;
865 window_type = xfc->NET_WM_WINDOW_TYPE_NORMAL;
867 if ((ex_style & WS_EX_NOACTIVATE) || (ex_style & WS_EX_TOOLWINDOW))
870 appWindow->is_transient = TRUE;
871 xf_SetWindowUnlisted(xfc, appWindow->handle);
872 window_type = xfc->NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
878 else if (ex_style & WS_EX_TOPMOST)
880 window_type = xfc->NET_WM_WINDOW_TYPE_NORMAL;
883 if (style & WS_POPUP)
885 window_type = xfc->NET_WM_WINDOW_TYPE_DIALOG;
888 if (!((ex_style & WS_EX_DLGMODALFRAME) || (ex_style & WS_EX_LAYERED) ||
889 (style & WS_SYSMENU)))
891 appWindow->is_transient = TRUE;
894 xf_SetWindowUnlisted(xfc, appWindow->handle);
898 if (!(style == 0 && ex_style == 0))
900 xf_SetWindowActions(xfc, appWindow);
913 XSetWindowAttributes attrs = { 0 };
914 attrs.override_redirect = redirect ? True : False;
915 LogDynAndXChangeWindowAttributes(xfc->log, xfc->display, appWindow->handle,
916 CWOverrideRedirect, &attrs);
919 LogDynAndXChangeProperty(xfc->log, xfc->display, appWindow->handle, xfc->NET_WM_WINDOW_TYPE,
920 XA_ATOM, 32, PropModeReplace, (BYTE*)&window_type, 1);
922 const BOOL above = (ex_style & WS_EX_TOPMOST) != 0;
923 const BOOL transient = (style & WS_CHILD) == 0;
926 xf_XSetTransientForHint(
929 xf_SendClientEvent(xfc, appWindow->handle, xfc->NET_WM_STATE, 4,
930 above ? NET_WM_STATE_ADD : NET_WM_STATE_REMOVE, xfc->NET_WM_STATE_ABOVE, 0,
934void xf_SetWindowActions(xfContext* xfc, xfAppWindow* appWindow)
936 Atom allowed_actions[] = {
937 xfc->NET_WM_ACTION_CLOSE, xfc->NET_WM_ACTION_MINIMIZE,
938 xfc->NET_WM_ACTION_MOVE, xfc->NET_WM_ACTION_RESIZE,
939 xfc->NET_WM_ACTION_MAXIMIZE_HORZ, xfc->NET_WM_ACTION_MAXIMIZE_VERT,
940 xfc->NET_WM_ACTION_FULLSCREEN, xfc->NET_WM_ACTION_CHANGE_DESKTOP
943 if (!(appWindow->dwStyle & WS_SYSMENU))
944 allowed_actions[0] = 0;
946 if (!(appWindow->dwStyle & WS_MINIMIZEBOX))
947 allowed_actions[1] = 0;
949 if (!(appWindow->dwStyle & WS_SIZEBOX))
950 allowed_actions[3] = 0;
952 if (!(appWindow->dwStyle & WS_MAXIMIZEBOX))
954 allowed_actions[4] = 0;
955 allowed_actions[5] = 0;
956 allowed_actions[6] = 0;
959 LogDynAndXChangeProperty(xfc->log, xfc->display, appWindow->handle, xfc->NET_WM_ALLOWED_ACTIONS,
960 XA_ATOM, 32, PropModeReplace, (
unsigned char*)&allowed_actions, 8);
963void xf_SetWindowText(xfContext* xfc, xfAppWindow* appWindow,
const char* name)
965 xf_SetWindowTitleText(xfc, appWindow->handle, name);
968static void xf_FixWindowCoordinates(xfContext* xfc,
int* x,
int* y,
int* width,
int* height)
970 int vscreen_width = 0;
971 int vscreen_height = 0;
972 vscreen_width = xfc->vscreen.area.right - xfc->vscreen.area.left + 1;
973 vscreen_height = xfc->vscreen.area.bottom - xfc->vscreen.area.top + 1;
975 if (*x < xfc->vscreen.area.left)
978 *x = xfc->vscreen.area.left;
981 if (*y < xfc->vscreen.area.top)
984 *y = xfc->vscreen.area.top;
987 if (*width > vscreen_width)
989 *width = vscreen_width;
992 if (*height > vscreen_height)
994 *height = vscreen_height;
1008int xf_AppWindowInit(xfContext* xfc, xfAppWindow* appWindow)
1010 if (!xfc || !appWindow)
1013 xf_SetWindowDecorations(xfc, appWindow->handle, appWindow->decorations);
1014 xf_SetWindowStyle(xfc, appWindow, appWindow->dwStyle, appWindow->dwExStyle);
1015 xf_SetWindowPID(xfc, appWindow->handle, 0);
1016 xf_ShowWindow(xfc, appWindow, WINDOW_SHOW);
1017 LogDynAndXClearWindow(xfc->log, xfc->display, appWindow->handle);
1018 LogDynAndXMapWindow(xfc->log, xfc->display, appWindow->handle);
1020 xf_MoveWindow(xfc, appWindow, appWindow->x, appWindow->y, appWindow->width, appWindow->height);
1021 xf_SetWindowText(xfc, appWindow, appWindow->title);
1025BOOL xf_AppWindowCreate(xfContext* xfc, xfAppWindow* appWindow)
1027 XGCValues gcv = { 0 };
1029 XWMHints* InputModeHint = NULL;
1030 XClassHint* class_hints = NULL;
1031 const rdpSettings* settings = NULL;
1034 WINPR_ASSERT(appWindow);
1036 settings = xfc->common.context.settings;
1037 WINPR_ASSERT(settings);
1039 xf_FixWindowCoordinates(xfc, &appWindow->x, &appWindow->y, &appWindow->width,
1040 &appWindow->height);
1041 appWindow->shmid = -1;
1042 appWindow->decorations = FALSE;
1043 appWindow->fullscreen = FALSE;
1044 appWindow->local_move.state = LMS_NOT_ACTIVE;
1045 appWindow->is_mapped = FALSE;
1046 appWindow->is_transient = FALSE;
1047 appWindow->rail_state = 0;
1048 appWindow->maxVert = FALSE;
1049 appWindow->maxHorz = FALSE;
1050 appWindow->minimized = FALSE;
1051 appWindow->rail_ignore_configure = FALSE;
1053 WINPR_ASSERT(xfc->depth != 0);
1054 appWindow->handle = LogDynAndXCreateWindow(
1055 xfc->log, xfc->display, RootWindowOfScreen(xfc->screen), appWindow->x, appWindow->y,
1056 WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->width),
1057 WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->height), 0, xfc->depth, InputOutput,
1058 xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->attribs_mask), &xfc->attribs);
1060 if (!appWindow->handle)
1064 LogDynAndXCreateGC(xfc->log, xfc->display, appWindow->handle, GCGraphicsExposures, &gcv);
1066 if (!xf_AppWindowResize(xfc, appWindow))
1069 class_hints = XAllocClassHint();
1073 char* strclass = NULL;
1077 strclass = _strdup(WmClass);
1081 winpr_asprintf(&strclass, &size,
"RAIL:%08" PRIX64
"", appWindow->windowId);
1083 class_hints->res_class = strclass;
1084 class_hints->res_name =
"RAIL";
1085 XSetClassHint(xfc->display, appWindow->handle, class_hints);
1091 InputModeHint = XAllocWMHints();
1092 InputModeHint->flags = (1L << 0);
1093 InputModeHint->input = True;
1094 XSetWMHints(xfc->display, appWindow->handle, InputModeHint);
1095 XFree(InputModeHint);
1096 XSetWMProtocols(xfc->display, appWindow->handle, &(xfc->WM_DELETE_WINDOW), 1);
1097 input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
1098 EnterWindowMask | LeaveWindowMask | PointerMotionMask | Button1MotionMask |
1099 Button2MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask |
1100 ButtonMotionMask | KeymapStateMask | ExposureMask | VisibilityChangeMask |
1101 StructureNotifyMask | SubstructureNotifyMask | SubstructureRedirectMask |
1102 FocusChangeMask | PropertyChangeMask | ColormapChangeMask | OwnerGrabButtonMask;
1103 XSelectInput(xfc->display, appWindow->handle, input_mask);
1105 if (xfc->XWAYLAND_MAY_GRAB_KEYBOARD)
1106 xf_SendClientEvent(xfc, appWindow->handle, xfc->XWAYLAND_MAY_GRAB_KEYBOARD, 1, 1);
1111void xf_SetWindowMinMaxInfo(xfContext* xfc, xfAppWindow* appWindow, WINPR_ATTR_UNUSED
int maxWidth,
1112 WINPR_ATTR_UNUSED
int maxHeight, WINPR_ATTR_UNUSED
int maxPosX,
1113 WINPR_ATTR_UNUSED
int maxPosY,
int minTrackWidth,
int minTrackHeight,
1114 int maxTrackWidth,
int maxTrackHeight)
1116 XSizeHints* size_hints = NULL;
1117 size_hints = XAllocSizeHints();
1121 size_hints->flags = PMinSize | PMaxSize | PResizeInc;
1122 size_hints->min_width = minTrackWidth;
1123 size_hints->min_height = minTrackHeight;
1124 size_hints->max_width = maxTrackWidth;
1125 size_hints->max_height = maxTrackHeight;
1127 size_hints->width_inc = size_hints->height_inc = 1;
1128 XSetWMNormalHints(xfc->display, appWindow->handle, size_hints);
1133void xf_StartLocalMoveSize(xfContext* xfc, xfAppWindow* appWindow,
int direction,
int x,
int y)
1135 if (appWindow->local_move.state != LMS_NOT_ACTIVE)
1142 appWindow->local_move.root_x = x;
1143 appWindow->local_move.root_y = y;
1144 appWindow->local_move.state = LMS_STARTING;
1145 appWindow->local_move.direction = direction;
1150 xfc, appWindow->handle,
1151 xfc->NET_WM_MOVERESIZE,
1160void xf_EndLocalMoveSize(xfContext* xfc, xfAppWindow* appWindow)
1162 if (appWindow->local_move.state == LMS_NOT_ACTIVE)
1165 if (appWindow->local_move.state == LMS_STARTING)
1174 xfc, appWindow->handle,
1175 xfc->NET_WM_MOVERESIZE,
1177 appWindow->local_move.root_x,
1178 appWindow->local_move.root_y,
1179 NET_WM_MOVERESIZE_CANCEL,
1184 appWindow->local_move.state = LMS_NOT_ACTIVE;
1187void xf_MoveWindow(xfContext* xfc, xfAppWindow* appWindow,
int x,
int y,
int width,
int height)
1189 BOOL resize = FALSE;
1191 if ((width * height) < 1)
1194 if ((appWindow->width != width) || (appWindow->height != height))
1197 if (appWindow->local_move.state == LMS_STARTING || appWindow->local_move.state == LMS_ACTIVE)
1202 appWindow->width = width;
1203 appWindow->height = height;
1207 if (!xf_AppWindowResize(xfc, appWindow))
1210 LogDynAndXMoveResizeWindow(xfc->log, xfc->display, appWindow->handle, x, y,
1211 WINPR_ASSERTING_INT_CAST(uint32_t, width),
1212 WINPR_ASSERTING_INT_CAST(uint32_t, height));
1215 LogDynAndXMoveWindow(xfc->log, xfc->display, appWindow->handle, x, y);
1217 xf_UpdateWindowArea(xfc, appWindow, 0, 0, width, height);
1220void xf_ShowWindow(xfContext* xfc, xfAppWindow* appWindow, BYTE state)
1223 WINPR_ASSERT(appWindow);
1228 LogDynAndXWithdrawWindow(xfc->log, xfc->display, appWindow->handle, xfc->screen_number);
1231 case WINDOW_SHOW_MINIMIZED:
1232 appWindow->minimized = TRUE;
1233 XIconifyWindow(xfc->display, appWindow->handle, xfc->screen_number);
1236 case WINDOW_SHOW_MAXIMIZED:
1238 appWindow->maxHorz = TRUE;
1239 appWindow->maxVert = TRUE;
1240 xf_SendClientEvent(xfc, appWindow->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_ADD,
1241 xfc->NET_WM_STATE_MAXIMIZED_VERT, xfc->NET_WM_STATE_MAXIMIZED_HORZ,
1252 if (appWindow->rail_state == WINDOW_SHOW_MAXIMIZED)
1254 xf_UpdateWindowArea(xfc, appWindow, 0, 0,
1255 WINPR_ASSERTING_INT_CAST(
int, appWindow->windowWidth),
1256 WINPR_ASSERTING_INT_CAST(
int, appWindow->windowHeight));
1263 xf_SendClientEvent(xfc, appWindow->handle, xfc->NET_WM_STATE, 4, NET_WM_STATE_REMOVE,
1264 xfc->NET_WM_STATE_MAXIMIZED_VERT, xfc->NET_WM_STATE_MAXIMIZED_HORZ,
1273 if (appWindow->rail_state == WINDOW_SHOW_MAXIMIZED)
1274 appWindow->rail_ignore_configure = TRUE;
1276 if (appWindow->is_transient)
1277 xf_SetWindowUnlisted(xfc, appWindow->handle);
1279 LogDynAndXMapWindow(xfc->log, xfc->display, appWindow->handle);
1286 appWindow->rail_state = state;
1287 LogDynAndXFlush(xfc->log, xfc->display);
1290void xf_SetWindowRects(xfContext* xfc, xfAppWindow* appWindow,
RECTANGLE_16* rects,
int nrects)
1292 XRectangle* xrects = NULL;
1298 xrects = (XRectangle*)calloc(WINPR_ASSERTING_INT_CAST(uint32_t, nrects),
sizeof(XRectangle));
1300 for (
int i = 0; i < nrects; i++)
1302 xrects[i].x = WINPR_ASSERTING_INT_CAST(
short, rects[i].left);
1303 xrects[i].y = WINPR_ASSERTING_INT_CAST(
short, rects[i].top);
1304 xrects[i].width = WINPR_ASSERTING_INT_CAST(
unsigned short, rects[i].right - rects[i].left);
1305 xrects[i].height = WINPR_ASSERTING_INT_CAST(
unsigned short, rects[i].bottom - rects[i].top);
1308 XShapeCombineRectangles(xfc->display, appWindow->handle, ShapeBounding, 0, 0, xrects, nrects,
1314void xf_SetWindowVisibilityRects(xfContext* xfc, xfAppWindow* appWindow, UINT32 rectsOffsetX,
1317 XRectangle* xrects = NULL;
1323 xrects = (XRectangle*)calloc(WINPR_ASSERTING_INT_CAST(uint32_t, nrects),
sizeof(XRectangle));
1325 for (
int i = 0; i < nrects; i++)
1327 xrects[i].x = WINPR_ASSERTING_INT_CAST(
short, rects[i].left);
1328 xrects[i].y = WINPR_ASSERTING_INT_CAST(
short, rects[i].top);
1329 xrects[i].width = WINPR_ASSERTING_INT_CAST(
unsigned short, rects[i].right - rects[i].left);
1330 xrects[i].height = WINPR_ASSERTING_INT_CAST(
unsigned short, rects[i].bottom - rects[i].top);
1333 XShapeCombineRectangles(
1334 xfc->display, appWindow->handle, ShapeBounding, WINPR_ASSERTING_INT_CAST(
int, rectsOffsetX),
1335 WINPR_ASSERTING_INT_CAST(
int, rectsOffsetY), xrects, nrects, ShapeSet, 0);
1340void xf_UpdateWindowArea(xfContext* xfc, xfAppWindow* appWindow,
int x,
int y,
int width,
1345 const rdpSettings* settings = NULL;
1349 settings = xfc->common.context.settings;
1350 WINPR_ASSERT(settings);
1352 if (appWindow == NULL)
1355 if (appWindow->surfaceId < UINT16_MAX)
1358 ax = x + appWindow->windowOffsetX;
1359 ay = y + appWindow->windowOffsetY;
1361 if (ax + width > appWindow->windowOffsetX + appWindow->width)
1362 width = (appWindow->windowOffsetX + appWindow->width - 1) - ax;
1364 if (ay + height > appWindow->windowOffsetY + appWindow->height)
1365 height = (appWindow->windowOffsetY + appWindow->height - 1) - ay;
1371 LogDynAndXPutImage(xfc->log, xfc->display, appWindow->pixmap, appWindow->gc, xfc->image, ax,
1372 ay, x, y, WINPR_ASSERTING_INT_CAST(uint32_t, width),
1373 WINPR_ASSERTING_INT_CAST(uint32_t, height));
1376 LogDynAndXCopyArea(xfc->log, xfc->display, appWindow->pixmap, appWindow->handle, appWindow->gc,
1377 x, y, WINPR_ASSERTING_INT_CAST(uint32_t, width),
1378 WINPR_ASSERTING_INT_CAST(uint32_t, height), x, y);
1379 LogDynAndXFlush(xfc->log, xfc->display);
1383static void xf_AppWindowDestroyImage(xfAppWindow* appWindow)
1385 WINPR_ASSERT(appWindow);
1386 if (appWindow->image)
1388 appWindow->image->data = NULL;
1389 XDestroyImage(appWindow->image);
1390 appWindow->image = NULL;
1394void xf_DestroyWindow(xfContext* xfc, xfAppWindow* appWindow)
1399 if (xfc->appWindow == appWindow)
1400 xfc->appWindow = NULL;
1403 LogDynAndXFreeGC(xfc->log, xfc->display, appWindow->gc);
1405 if (appWindow->pixmap)
1406 LogDynAndXFreePixmap(xfc->log, xfc->display, appWindow->pixmap);
1408 xf_AppWindowDestroyImage(appWindow);
1410 if (appWindow->handle)
1412 LogDynAndXUnmapWindow(xfc->log, xfc->display, appWindow->handle);
1413 LogDynAndXDestroyWindow(xfc->log, xfc->display, appWindow->handle);
1416 if (appWindow->xfwin)
1417 munmap(0,
sizeof(*appWindow->xfwin));
1419 if (appWindow->shmid >= 0)
1420 close(appWindow->shmid);
1422 shm_unlink(get_shm_id());
1423 appWindow->xfwin = (Window*)-1;
1424 appWindow->shmid = -1;
1425 free(appWindow->title);
1426 free(appWindow->windowRects);
1427 free(appWindow->visibilityRects);
1431xfAppWindow* xf_AppWindowFromX11Window(xfContext* xfc, Window wnd)
1433 ULONG_PTR* pKeys = NULL;
1436 if (!xfc->railWindows)
1439 size_t count = HashTable_GetKeys(xfc->railWindows, &pKeys);
1441 for (
size_t index = 0; index < count; index++)
1443 xfAppWindow* appWindow = xf_rail_get_window(xfc, *(UINT64*)pKeys[index]);
1451 if (appWindow->handle == wnd)
1462UINT xf_AppUpdateWindowFromSurface(xfContext* xfc, gdiGfxSurface* surface)
1464 XImage* image = NULL;
1465 UINT rc = ERROR_INTERNAL_ERROR;
1468 WINPR_ASSERT(surface);
1470 xfAppWindow* appWindow = xf_rail_get_window(xfc, surface->windowId);
1473 WLog_VRB(TAG,
"Failed to find a window for id=0x%08" PRIx64, surface->windowId);
1474 return CHANNEL_RC_OK;
1479 const RECTANGLE_16* rects = region16_rects(&surface->invalidRegion, &nrects);
1484 if (appWindow->surfaceId != surface->surfaceId)
1486 xf_AppWindowDestroyImage(appWindow);
1487 appWindow->surfaceId = surface->surfaceId;
1489 if (appWindow->width != (INT64)surface->width)
1490 xf_AppWindowDestroyImage(appWindow);
1491 if (appWindow->height != (INT64)surface->height)
1492 xf_AppWindowDestroyImage(appWindow);
1494 if (!appWindow->image)
1496 WINPR_ASSERT(xfc->depth != 0);
1497 appWindow->image = LogDynAndXCreateImage(
1498 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
1499 ZPixmap, 0, (
char*)surface->data, surface->width, surface->height,
1500 xfc->scanline_pad, WINPR_ASSERTING_INT_CAST(
int, surface->scanline));
1501 if (!appWindow->image)
1504 "Failed create a XImage[%" PRIu32
"x%" PRIu32
", scanline=%" PRIu32
1505 ", bpp=%" PRIu32
"] for window id=0x%08" PRIx64,
1506 surface->width, surface->height, surface->scanline, xfc->depth,
1510 appWindow->image->byte_order = LSBFirst;
1511 appWindow->image->bitmap_bit_order = LSBFirst;
1514 image = appWindow->image;
1518 xfGfxSurface* xfSurface = (xfGfxSurface*)surface;
1519 image = xfSurface->image;
1522 for (UINT32 x = 0; x < nrects; x++)
1525 const UINT32 width = rect->right - rect->left;
1526 const UINT32 height = rect->bottom - rect->top;
1528 LogDynAndXPutImage(xfc->log, xfc->display, appWindow->pixmap, appWindow->gc, image,
1529 rect->left, rect->top, rect->left, rect->top, width, height);
1531 LogDynAndXCopyArea(xfc->log, xfc->display, appWindow->pixmap, appWindow->handle,
1532 appWindow->gc, rect->left, rect->top, width, height, rect->left,
1538 LogDynAndXFlush(xfc->log, xfc->display);
1543BOOL xf_AppWindowResize(xfContext* xfc, xfAppWindow* appWindow)
1546 WINPR_ASSERT(appWindow);
1548 if (appWindow->pixmap != 0)
1549 LogDynAndXFreePixmap(xfc->log, xfc->display, appWindow->pixmap);
1551 WINPR_ASSERT(xfc->depth != 0);
1552 appWindow->pixmap = LogDynAndXCreatePixmap(
1553 xfc->log, xfc->display, xfc->drawable, WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->width),
1554 WINPR_ASSERTING_INT_CAST(uint32_t, appWindow->height),
1555 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth));
1556 xf_AppWindowDestroyImage(appWindow);
1558 return appWindow->pixmap != 0;
1561void xf_XSetTransientForHint(xfContext* xfc, xfAppWindow* window)
1564 WINPR_ASSERT(window);
1566 if (window->ownerWindowId == 0)
1569 xfAppWindow* parent = xf_rail_get_window(xfc, window->ownerWindowId);
1573 (void)LogDynAndXSetTransientForHint(xfc->log, xfc->display, window->handle, parent->handle);
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.