25#include <freerdp/config.h>
28#include <winpr/cast.h>
29#include <winpr/assert.h>
30#include <winpr/sspicli.h>
39#include <X11/extensions/Xrender.h>
43#include <X11/extensions/XInput.h>
44#include <X11/extensions/XInput2.h>
48#include <X11/Xcursor/Xcursor.h>
52#include <X11/extensions/Xinerama.h>
55#include <X11/XKBlib.h>
67#include <sys/select.h>
71#include <freerdp/freerdp.h>
72#include <freerdp/constants.h>
73#include <freerdp/codec/nsc.h>
74#include <freerdp/codec/rfx.h>
75#include <freerdp/codec/color.h>
76#include <freerdp/codec/bitmap.h>
78#include <freerdp/utils/signal.h>
79#include <freerdp/utils/passphrase.h>
80#include <freerdp/client/cliprdr.h>
81#include <freerdp/client/channels.h>
83#include <freerdp/client/file.h>
84#include <freerdp/client/cmdline.h>
87#include <winpr/synch.h>
88#include <winpr/file.h>
89#include <winpr/print.h>
90#include <winpr/sysinfo.h>
93#if defined(CHANNEL_TSMF_CLIENT)
98#include "xf_cliprdr.h"
101#include "xf_monitor.h"
102#include "xf_graphics.h"
103#include "xf_keyboard.h"
104#include "xf_channels.h"
105#include "xf_client.h"
109#include <freerdp/log.h>
110#define TAG CLIENT_TAG("x11")
112#define MIN_PIXEL_DIFF 0.001
114struct xf_exit_code_map_t
119static const struct xf_exit_code_map_t xf_exit_code_map[] = {
120 { FREERDP_ERROR_SUCCESS, XF_EXIT_SUCCESS },
121 { FREERDP_ERROR_AUTHENTICATION_FAILED, XF_EXIT_AUTH_FAILURE },
122 { FREERDP_ERROR_SECURITY_NEGO_CONNECT_FAILED, XF_EXIT_NEGO_FAILURE },
123 { FREERDP_ERROR_CONNECT_LOGON_FAILURE, XF_EXIT_LOGON_FAILURE },
124 { FREERDP_ERROR_CONNECT_ACCOUNT_LOCKED_OUT, XF_EXIT_ACCOUNT_LOCKED_OUT },
125 { FREERDP_ERROR_PRE_CONNECT_FAILED, XF_EXIT_PRE_CONNECT_FAILED },
126 { FREERDP_ERROR_CONNECT_UNDEFINED, XF_EXIT_CONNECT_UNDEFINED },
127 { FREERDP_ERROR_POST_CONNECT_FAILED, XF_EXIT_POST_CONNECT_FAILED },
128 { FREERDP_ERROR_DNS_ERROR, XF_EXIT_DNS_ERROR },
129 { FREERDP_ERROR_DNS_NAME_NOT_FOUND, XF_EXIT_DNS_NAME_NOT_FOUND },
130 { FREERDP_ERROR_CONNECT_FAILED, XF_EXIT_CONNECT_FAILED },
131 { FREERDP_ERROR_MCS_CONNECT_INITIAL_ERROR, XF_EXIT_MCS_CONNECT_INITIAL_ERROR },
132 { FREERDP_ERROR_TLS_CONNECT_FAILED, XF_EXIT_TLS_CONNECT_FAILED },
133 { FREERDP_ERROR_INSUFFICIENT_PRIVILEGES, XF_EXIT_INSUFFICIENT_PRIVILEGES },
134 { FREERDP_ERROR_CONNECT_CANCELLED, XF_EXIT_CONNECT_CANCELLED },
135 { FREERDP_ERROR_CONNECT_TRANSPORT_FAILED, XF_EXIT_CONNECT_TRANSPORT_FAILED },
136 { FREERDP_ERROR_CONNECT_PASSWORD_EXPIRED, XF_EXIT_CONNECT_PASSWORD_EXPIRED },
137 { FREERDP_ERROR_CONNECT_PASSWORD_MUST_CHANGE, XF_EXIT_CONNECT_PASSWORD_MUST_CHANGE },
138 { FREERDP_ERROR_CONNECT_KDC_UNREACHABLE, XF_EXIT_CONNECT_KDC_UNREACHABLE },
139 { FREERDP_ERROR_CONNECT_ACCOUNT_DISABLED, XF_EXIT_CONNECT_ACCOUNT_DISABLED },
140 { FREERDP_ERROR_CONNECT_PASSWORD_CERTAINLY_EXPIRED,
141 XF_EXIT_CONNECT_PASSWORD_CERTAINLY_EXPIRED },
142 { FREERDP_ERROR_CONNECT_CLIENT_REVOKED, XF_EXIT_CONNECT_CLIENT_REVOKED },
143 { FREERDP_ERROR_CONNECT_WRONG_PASSWORD, XF_EXIT_CONNECT_WRONG_PASSWORD },
144 { FREERDP_ERROR_CONNECT_ACCESS_DENIED, XF_EXIT_CONNECT_ACCESS_DENIED },
145 { FREERDP_ERROR_CONNECT_ACCOUNT_RESTRICTION, XF_EXIT_CONNECT_ACCOUNT_RESTRICTION },
146 { FREERDP_ERROR_CONNECT_ACCOUNT_EXPIRED, XF_EXIT_CONNECT_ACCOUNT_EXPIRED },
147 { FREERDP_ERROR_CONNECT_LOGON_TYPE_NOT_GRANTED, XF_EXIT_CONNECT_LOGON_TYPE_NOT_GRANTED },
148 { FREERDP_ERROR_CONNECT_NO_OR_MISSING_CREDENTIALS, XF_EXIT_CONNECT_NO_OR_MISSING_CREDENTIALS },
149 { FREERDP_ERROR_CONNECT_TARGET_BOOTING, XF_EXIT_CONNECT_TARGET_BOOTING },
150 { FREERDP_ERROR_CONNECT_HYBRID_REQUIRED_BY_SERVER, XF_EXIT_CONNECT_HYBRID_REQUIRED_BY_SERVER }
153static BOOL xf_setup_x11(xfContext* xfc);
154static void xf_teardown_x11(xfContext* xfc);
156static int xf_map_error_to_exit_code(DWORD error)
158 for (
size_t x = 0; x < ARRAYSIZE(xf_exit_code_map); x++)
160 const struct xf_exit_code_map_t* cur = &xf_exit_code_map[x];
161 if (cur->error == error)
165 return XF_EXIT_CONN_FAILED;
168static int (*def_error_handler)(Display*, XErrorEvent*);
169static int xf_error_handler_ex(Display* d, XErrorEvent* ev);
170static void xf_check_extensions(xfContext* context);
171static BOOL xf_get_pixmap_info(xfContext* xfc);
174static void xf_draw_screen_scaled(xfContext* xfc,
int x,
int y,
int w,
int h)
176 XTransform transform = WINPR_C_ARRAY_INIT;
177 Picture windowPicture = 0;
178 Picture primaryPicture = 0;
179 XRenderPictureAttributes pa;
180 XRenderPictFormat* picFormat =
nullptr;
183 const char* filter =
nullptr;
186 rdpSettings* settings = xfc->common.context.settings;
187 WINPR_ASSERT(settings);
189 if (xfc->scaledWidth <= 0 || xfc->scaledHeight <= 0)
191 WLog_ERR(TAG,
"the current window dimensions are invalid");
198 WLog_ERR(TAG,
"the window dimensions are invalid");
202 const double xScalingFactor = 1.0 *
204 (double)xfc->scaledWidth;
205 const double yScalingFactor = 1.0 *
207 (double)xfc->scaledHeight;
208 LogDynAndXSetFillStyle(xfc->log, xfc->display, xfc->gc, FillSolid);
209 LogDynAndXSetForeground(xfc->log, xfc->display, xfc->gc, 0);
212 XRectangle box1 = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, xfc->window->width),
213 WINPR_ASSERTING_INT_CAST(UINT16, xfc->window->height) };
214 XRectangle box2 = { WINPR_ASSERTING_INT_CAST(INT16, xfc->offset_x),
215 WINPR_ASSERTING_INT_CAST(INT16, xfc->offset_y),
216 WINPR_ASSERTING_INT_CAST(UINT16, xfc->scaledWidth),
217 WINPR_ASSERTING_INT_CAST(UINT16, xfc->scaledHeight) };
218 Region reg1 = XCreateRegion();
219 Region reg2 = XCreateRegion();
220 XUnionRectWithRegion(&box1, reg1, reg1);
221 XUnionRectWithRegion(&box2, reg2, reg2);
223 if (XSubtractRegion(reg1, reg2, reg1) && !XEmptyRegion(reg1))
225 LogDynAndXSetRegion(xfc->log, xfc->display, xfc->gc, reg1);
226 LogDynAndXFillRectangle(xfc->log, xfc->display, xfc->window->handle, xfc->gc, 0, 0,
227 WINPR_ASSERTING_INT_CAST(UINT16, xfc->window->width),
228 WINPR_ASSERTING_INT_CAST(UINT16, xfc->window->height));
229 LogDynAndXSetClipMask(xfc->log, xfc->display, xfc->gc, None);
232 XDestroyRegion(reg1);
233 XDestroyRegion(reg2);
235 picFormat = XRenderFindVisualFormat(xfc->display, xfc->visual);
236 pa.subwindow_mode = IncludeInferiors;
238 XRenderCreatePicture(xfc->display, xfc->primary, picFormat, CPSubwindowMode, &pa);
240 XRenderCreatePicture(xfc->display, xfc->window->handle, picFormat, CPSubwindowMode, &pa);
243 filter = FilterBilinear;
244 if (fabs(xScalingFactor - yScalingFactor) < MIN_PIXEL_DIFF)
246 const double inverseX = 1.0 / xScalingFactor;
247 const double inverseRoundedX = round(inverseX);
248 const double absInverse = fabs(inverseX - inverseRoundedX);
250 if (absInverse < MIN_PIXEL_DIFF)
251 filter = FilterNearest;
253 XRenderSetPictureFilter(xfc->display, primaryPicture, filter,
nullptr, 0);
254 transform.matrix[0][0] = XDoubleToFixed(xScalingFactor);
255 transform.matrix[0][1] = XDoubleToFixed(0.0);
256 transform.matrix[0][2] = XDoubleToFixed(0.0);
257 transform.matrix[1][0] = XDoubleToFixed(0.0);
258 transform.matrix[1][1] = XDoubleToFixed(yScalingFactor);
259 transform.matrix[1][2] = XDoubleToFixed(0.0);
260 transform.matrix[2][0] = XDoubleToFixed(0.0);
261 transform.matrix[2][1] = XDoubleToFixed(0.0);
262 transform.matrix[2][2] = XDoubleToFixed(1.0);
267 const double dx1 = floor(x / xScalingFactor);
268 const double dy1 = floor(y / yScalingFactor);
269 const double dx2 = ceil(x2 / xScalingFactor);
270 const double dy2 = ceil(y2 / yScalingFactor);
273 w = ((int)dx2) + 1 - x;
274 h = ((int)dy2) + 1 - y;
275 XRenderSetPictureTransform(xfc->display, primaryPicture, &transform);
276 XRenderComposite(xfc->display, PictOpSrc, primaryPicture, 0, windowPicture, x, y, 0, 0,
277 xfc->offset_x + x, xfc->offset_y + y, WINPR_ASSERTING_INT_CAST(uint32_t, w),
278 WINPR_ASSERTING_INT_CAST(uint32_t, h));
279 XRenderFreePicture(xfc->display, primaryPicture);
280 XRenderFreePicture(xfc->display, windowPicture);
283BOOL xf_picture_transform_required(xfContext* xfc)
285 rdpSettings* settings =
nullptr;
289 settings = xfc->common.context.settings;
290 WINPR_ASSERT(settings);
293 (xfc->offset_x != 0) || (xfc->offset_y != 0) ||
299void xf_draw_screen_(xfContext* xfc,
int x,
int y,
int w,
int h,
const char* fkt,
300 WINPR_ATTR_UNUSED
const char* file, WINPR_ATTR_UNUSED
int line)
304 WLog_DBG(TAG,
"called from [%s] xfc=nullptr", fkt);
308 if (w == 0 || h == 0)
310 WLog_WARN(TAG,
"invalid width and/or height specified: w=%d h=%d", w, h);
316 WLog_WARN(TAG,
"invalid xfc->window=nullptr");
322 if (xf_picture_transform_required(xfc))
324 xf_draw_screen_scaled(xfc, x, y, w, h);
329 LogDynAndXCopyArea(xfc->log, xfc->display, xfc->primary, xfc->window->handle, xfc->gc, x, y,
330 WINPR_ASSERTING_INT_CAST(uint32_t, w), WINPR_ASSERTING_INT_CAST(uint32_t, h),
334static BOOL xf_desktop_resize(rdpContext* context)
336 xfContext* xfc = (xfContext*)context;
340 rdpSettings* settings = context->settings;
341 WINPR_ASSERT(settings);
345 BOOL same = (xfc->primary == xfc->drawing);
346 LogDynAndXFreePixmap(xfc->log, xfc->display, xfc->primary);
348 WINPR_ASSERT(xfc->depth != 0);
349 if (!(xfc->primary = LogDynAndXCreatePixmap(
350 xfc->log, xfc->display, xfc->drawable,
353 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth))))
357 xfc->drawing = xfc->primary;
364 xfc->scaledWidth = WINPR_ASSERTING_INT_CAST(
366 xfc->scaledHeight = WINPR_ASSERTING_INT_CAST(
372 if (!xfc->fullscreen)
374 xf_ResizeDesktopWindow(
376 WINPR_ASSERTING_INT_CAST(
int,
378 WINPR_ASSERTING_INT_CAST(
int,
390 xfc->savedWidth = WINPR_ASSERTING_INT_CAST(
392 xfc->savedHeight = WINPR_ASSERTING_INT_CAST(
396 LogDynAndXSetFunction(xfc->log, xfc->display, xfc->gc, GXcopy);
397 LogDynAndXSetFillStyle(xfc->log, xfc->display, xfc->gc, FillSolid);
398 LogDynAndXSetForeground(xfc->log, xfc->display, xfc->gc, 0);
399 LogDynAndXFillRectangle(xfc->log, xfc->display, xfc->drawable, xfc->gc, 0, 0,
407static BOOL xf_paint(xfContext* xfc,
const GDI_RGN* region)
410 WINPR_ASSERT(region);
414 const RECTANGLE_16 rect = { .left = WINPR_ASSERTING_INT_CAST(UINT16, region->x),
415 .top = WINPR_ASSERTING_INT_CAST(UINT16, region->y),
417 WINPR_ASSERTING_INT_CAST(UINT16, region->x + region->w),
419 WINPR_ASSERTING_INT_CAST(UINT16, region->y + region->h) };
420 return xf_rail_paint(xfc, &rect);
424 LogDynAndXPutImage(xfc->log, xfc->display, xfc->primary, xfc->gc, xfc->image, region->x,
425 region->y, region->x, region->y,
426 WINPR_ASSERTING_INT_CAST(UINT16, region->w),
427 WINPR_ASSERTING_INT_CAST(UINT16, region->h));
428 xf_draw_screen(xfc, region->x, region->y, region->w, region->h);
433static BOOL xf_end_paint(rdpContext* context)
435 xfContext* xfc = (xfContext*)context;
438 rdpGdi* gdi = context->gdi;
441 if (gdi->suppressOutput)
444 HGDI_DC hdc = gdi->primary->hdc;
449 if (!xfc->complex_regions)
451 const GDI_RGN* rgn = hwnd->invalid;
455 if (!xf_paint(xfc, rgn))
461 const INT32 ninvalid = hwnd->ninvalid;
462 const GDI_RGN* cinvalid = hwnd->cinvalid;
464 if (hwnd->ninvalid < 1)
469 for (INT32 i = 0; i < ninvalid; i++)
471 const GDI_RGN* rgn = &cinvalid[i];
472 if (!xf_paint(xfc, rgn))
476 LogDynAndXFlush(xfc->log, xfc->display);
480 hwnd->invalid->null = TRUE;
485static BOOL xf_sw_desktop_resize(rdpContext* context)
487 WINPR_ASSERT(context);
489 rdpGdi* gdi = context->gdi;
492 xfContext* xfc = (xfContext*)context;
493 rdpSettings* settings = context->settings;
494 WINPR_ASSERT(settings);
501 const BOOL suppress = gdi->suppressOutput;
502 gdi->suppressOutput = TRUE;
511 xfc->image->data =
nullptr;
512 XDestroyImage(xfc->image);
515 WINPR_ASSERT(xfc->depth != 0);
516 if (!(xfc->image = LogDynAndXCreateImage(
517 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
518 ZPixmap, 0, (
char*)gdi->primary_buffer,
519 WINPR_ASSERTING_INT_CAST(uint32_t, gdi->width),
520 WINPR_ASSERTING_INT_CAST(uint32_t, gdi->height), xfc->scanline_pad,
521 WINPR_ASSERTING_INT_CAST(
int, gdi->stride))))
526 xfc->image->byte_order = LSBFirst;
527 xfc->image->bitmap_bit_order = LSBFirst;
528 ret = xf_desktop_resize(context);
531 gdi->suppressOutput = suppress;
535static BOOL xf_process_x_events(freerdp* instance)
538 int pending_status = 1;
539 xfContext* xfc = (xfContext*)instance->context;
541 while (pending_status)
544 pending_status = XPending(xfc->display);
548 XEvent xevent = WINPR_C_ARRAY_INIT;
550 XNextEvent(xfc->display, &xevent);
551 status = xf_event_process(instance, &xevent);
556 status = xf_event_update_screen(instance);
564static char* xf_window_get_title(rdpSettings* settings)
567 char* windowTitle =
nullptr;
569 const char* prefix =
"FreeRDP:";
578 return _strdup(title);
582 size = strnlen(name, MAX_PATH) + 16;
583 windowTitle = calloc(size,
sizeof(
char));
589 (void)sprintf_s(windowTitle, size,
"%s %s", prefix, name);
591 (
void)sprintf_s(windowTitle, size,
"%s %s:%" PRIu32, prefix, name,
597BOOL xf_create_window(xfContext* xfc)
599 XGCValues gcv = WINPR_C_ARRAY_INIT;
600 XEvent xevent = WINPR_C_ARRAY_INIT;
601 char* windowTitle =
nullptr;
604 rdpSettings* settings = xfc->common.context.settings;
605 WINPR_ASSERT(settings);
612 const XSetWindowAttributes empty = WINPR_C_ARRAY_INIT;
613 xfc->attribs = empty;
618 xfc->depth = DefaultDepthOfScreen(xfc->screen);
620 XVisualInfo vinfo = WINPR_C_ARRAY_INIT;
621 if (XMatchVisualInfo(xfc->display, xfc->screen_number, xfc->depth, TrueColor, &vinfo))
623 Window root = XDefaultRootWindow(xfc->display);
624 xfc->visual = vinfo.visual;
625 xfc->attribs.colormap = xfc->colormap =
626 XCreateColormap(xfc->display, root, vinfo.visual, AllocNone);
632 WLog_WARN(TAG,
"running in remote app mode, but XServer does not support transparency");
633 WLog_WARN(TAG,
"display of remote applications might be distorted (black frames, ...)");
635 xfc->depth = DefaultDepthOfScreen(xfc->screen);
636 xfc->visual = DefaultVisual(xfc->display, xfc->screen_number);
637 xfc->attribs.colormap = xfc->colormap = DefaultColormap(xfc->display, xfc->screen_number);
644 if (vinfo.red_mask & 0xFF)
649 if (!xfc->remote_app)
651 xfc->attribs.background_pixel = BlackPixelOfScreen(xfc->screen);
652 xfc->attribs.border_pixel = WhitePixelOfScreen(xfc->screen);
653 xfc->attribs.backing_store = xfc->primary ? NotUseful : Always;
654 xfc->attribs.override_redirect = False;
656 xfc->attribs.bit_gravity = NorthWestGravity;
657 xfc->attribs.win_gravity = NorthWestGravity;
658 xfc->attribs_mask = CWBackPixel | CWBackingStore | CWOverrideRedirect | CWColormap |
659 CWBorderPixel | CWWinGravity | CWBitGravity;
665 windowTitle = xf_window_get_title(settings);
675 width = WINPR_ASSERTING_INT_CAST(
679 height = WINPR_ASSERTING_INT_CAST(
682 xfc->scaledWidth = width;
683 xfc->scaledHeight = height;
687 xfc->window = xf_CreateDesktopWindow(xfc, windowTitle, width, height);
693 xf_SetWindowFullscreen(xfc, xfc->window, xfc->fullscreen);
695 xfc->unobscured = (xevent.xvisibility.state == VisibilityUnobscured);
696 XSetWMProtocols(xfc->display, xfc->window->handle, &(xfc->WM_DELETE_WINDOW), 1);
697 xfc->drawable = xfc->window->handle;
701 xfc->attribs.border_pixel = 0;
702 xfc->attribs.background_pixel = 0;
703 xfc->attribs.backing_store = xfc->primary ? NotUseful : Always;
704 xfc->attribs.override_redirect = False;
706 xfc->attribs.bit_gravity = NorthWestGravity;
707 xfc->attribs.win_gravity = NorthWestGravity;
708 xfc->attribs_mask = CWBackPixel | CWBackingStore | CWOverrideRedirect | CWColormap |
709 CWBorderPixel | CWWinGravity | CWBitGravity;
711 xfc->drawable = xf_CreateDummyWindow(xfc);
716 LogDynAndXCreateGC(xfc->log, xfc->display, xfc->drawable, GCGraphicsExposures, &gcv);
718 WINPR_ASSERT(xfc->depth != 0);
721 LogDynAndXCreatePixmap(xfc->log, xfc->display, xfc->drawable,
724 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth));
726 xfc->drawing = xfc->primary;
728 if (!xfc->bitmap_mono)
729 xfc->bitmap_mono = LogDynAndXCreatePixmap(xfc->log, xfc->display, xfc->drawable, 8, 8, 1);
733 LogDynAndXCreateGC(xfc->log, xfc->display, xfc->bitmap_mono, GCGraphicsExposures, &gcv);
735 LogDynAndXSetFunction(xfc->log, xfc->display, xfc->gc, GXcopy);
736 LogDynAndXSetFillStyle(xfc->log, xfc->display, xfc->gc, FillSolid);
737 LogDynAndXSetForeground(xfc->log, xfc->display, xfc->gc, BlackPixelOfScreen(xfc->screen));
738 LogDynAndXFillRectangle(xfc->log, xfc->display, xfc->primary, xfc->gc, 0, 0,
741 LogDynAndXFlush(xfc->log, xfc->display);
746BOOL xf_create_image(xfContext* xfc)
751 const rdpSettings* settings = xfc->common.context.settings;
752 rdpGdi* cgdi = xfc->common.context.gdi;
755 WINPR_ASSERT(xfc->depth != 0);
756 xfc->image = LogDynAndXCreateImage(
757 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
758 ZPixmap, 0, (
char*)cgdi->primary_buffer,
761 WINPR_ASSERTING_INT_CAST(
int, cgdi->stride));
762 xfc->image->byte_order = LSBFirst;
763 xfc->image->bitmap_bit_order = LSBFirst;
768void xf_destroy_window(xfContext* xfc)
772 xf_DestroyDesktopWindow(xfc, xfc->window);
773 xfc->window =
nullptr;
776#if defined(CHANNEL_TSMF_CLIENT)
779 xf_tsmf_uninit(xfc,
nullptr);
780 xfc->xv_context =
nullptr;
786 xfc->image->data =
nullptr;
787 XDestroyImage(xfc->image);
788 xfc->image =
nullptr;
791 if (xfc->bitmap_mono)
793 LogDynAndXFreePixmap(xfc->log, xfc->display, xfc->bitmap_mono);
794 xfc->bitmap_mono = 0;
799 LogDynAndXFreeGC(xfc->log, xfc->display, xfc->gc_mono);
800 xfc->gc_mono =
nullptr;
805 LogDynAndXFreePixmap(xfc->log, xfc->display, xfc->primary);
811 LogDynAndXFreeGC(xfc->log, xfc->display, xfc->gc);
816void xf_toggle_fullscreen(xfContext* xfc)
818 WindowStateChangeEventArgs e = WINPR_C_ARRAY_INIT;
819 rdpContext* context = (rdpContext*)xfc;
820 rdpSettings* settings = context->settings;
829 xfc->fullscreen = !((xfc->fullscreen));
832 xf_SetWindowFullscreen(xfc, xfc->window, xfc->fullscreen);
833 EventArgsInit(&e,
"xfreerdp");
834 e.state = xfc->fullscreen ? FREERDP_WINDOW_STATE_FULLSCREEN : 0;
835 if (PubSub_OnWindowStateChange(context->pubSub, context, &e) < 0)
837 WLog_Print(xfc->log, WLOG_ERROR,
"PubSub_OnWindowStateChange failed");
841void xf_minimize(xfContext* xfc)
843 WindowStateChangeEventArgs e = WINPR_C_ARRAY_INIT;
844 rdpContext* context = (rdpContext*)xfc;
845 WINPR_ASSERT(context);
854 xf_SetWindowMinimized(xfc, xfc->window);
856 e.state = xfc->fullscreen ? FREERDP_WINDOW_STATE_FULLSCREEN : 0;
857 if (PubSub_OnWindowStateChange(context->pubSub, context, &e) < 0)
859 WLog_Print(xfc->log, WLOG_ERROR,
"PubSub_OnWindowStateChange failed");
863void xf_lock_x11_(xfContext* xfc, WINPR_ATTR_UNUSED
const char* fkt)
865 if (!xfc->UseXThreads)
866 (void)WaitForSingleObject(xfc->mutex, INFINITE);
868 XLockDisplay(xfc->display);
873void xf_unlock_x11_(xfContext* xfc, WINPR_ATTR_UNUSED
const char* fkt)
875 if (xfc->locked == 0)
876 WLog_WARN(TAG,
"X11: trying to unlock although not locked!");
880 if (!xfc->UseXThreads)
881 (void)ReleaseMutex(xfc->mutex);
883 XUnlockDisplay(xfc->display);
886static BOOL xf_get_pixmap_info(xfContext* xfc)
889 XPixmapFormatValues* pfs =
nullptr;
891 WINPR_ASSERT(xfc->display);
892 pfs = XListPixmapFormats(xfc->display, &pf_count);
896 WLog_ERR(TAG,
"XListPixmapFormats failed");
900 WINPR_ASSERT(xfc->depth != 0);
901 for (
int i = 0; i < pf_count; i++)
903 const XPixmapFormatValues* pf = &pfs[i];
905 if (pf->depth == xfc->depth)
907 xfc->scanline_pad = pf->scanline_pad;
913 return !((xfc->visual ==
nullptr) || (xfc->scanline_pad == 0));
916static int xf_error_handler(Display* d, XErrorEvent* ev)
918 char buf[256] = WINPR_C_ARRAY_INIT;
919 XGetErrorText(d, ev->error_code, buf,
sizeof(buf));
920 const char* what = request_code_2_str(ev->request_code);
921 WLog_ERR(TAG,
"%s: %s", what, buf);
922 winpr_log_backtrace(TAG, WLOG_ERROR, 20);
926static int xf_error_handler_ex(Display* d, XErrorEvent* ev)
934 XUngrabKeyboard(d, CurrentTime);
935 XUngrabPointer(d, CurrentTime);
937 return xf_error_handler(d, ev);
940static BOOL xf_play_sound(rdpContext* context,
const PLAY_SOUND_UPDATE* play_sound)
942 xfContext* xfc = (xfContext*)context;
943 WINPR_UNUSED(play_sound);
944 XkbBell(xfc->display, None, 100, 0);
948static void xf_check_extensions(xfContext* context)
953 int xkb_major = XkbMajorVersion;
954 int xkb_minor = XkbMinorVersion;
956 if (XkbLibraryVersion(&xkb_major, &xkb_minor) &&
957 XkbQueryExtension(context->display, &xkb_opcode, &xkb_event, &xkb_error, &xkb_major,
960 context->xkbAvailable = TRUE;
965 int xrender_event_base = 0;
966 int xrender_error_base = 0;
968 if (XRenderQueryExtension(context->display, &xrender_event_base, &xrender_error_base))
970 context->xrenderAvailable = TRUE;
979static const char TEST_PTR_STR[] =
"Virtual core XTEST pointer";
980static const size_t TEST_PTR_LEN =
sizeof(TEST_PTR_STR) /
sizeof(
char);
983static void xf_get_x11_button_map(xfContext* xfc,
unsigned char* x11_map)
989 XDevice* ptr_dev =
nullptr;
990 XExtensionVersion* version =
nullptr;
991 XDeviceInfo* devices1 =
nullptr;
992 XIDeviceInfo* devices2 =
nullptr;
995 if (XQueryExtension(xfc->display,
"XInputExtension", &opcode, &event, &error))
997 WLog_DBG(TAG,
"Searching for XInput pointer device");
1000 version = XGetExtensionVersion(xfc->display, INAME);
1002 if (version->major_version >= 2)
1005 devices2 = XIQueryDevice(xfc->display, XIAllDevices, &num_devices);
1009 for (
int i = 0; i < num_devices; ++i)
1011 XIDeviceInfo* dev = &devices2[i];
1012 if ((dev->use == XISlavePointer) &&
1013 (strncmp(dev->name, TEST_PTR_STR, TEST_PTR_LEN) != 0))
1015 ptr_dev = XOpenDevice(xfc->display,
1016 WINPR_ASSERTING_INT_CAST(uint32_t, dev->deviceid));
1022 XIFreeDeviceInfo(devices2);
1028 devices1 = XListInputDevices(xfc->display, &num_devices);
1032 for (
int i = 0; i < num_devices; ++i)
1034 if ((devices1[i].use == IsXExtensionPointer) &&
1035 (strncmp(devices1[i].name, TEST_PTR_STR, TEST_PTR_LEN) != 0))
1037 ptr_dev = XOpenDevice(xfc->display, devices1[i].id);
1043 XFreeDeviceList(devices1);
1053 WLog_DBG(TAG,
"Pointer device: %" PRIu32,
1054 WINPR_CXX_COMPAT_CAST(uint32_t, ptr_dev->device_id));
1055 XGetDeviceButtonMapping(xfc->display, ptr_dev, x11_map, NUM_BUTTONS_MAPPED);
1056 XCloseDevice(xfc->display, ptr_dev);
1060 WLog_DBG(TAG,
"No pointer device found!");
1066 WLog_DBG(TAG,
"Get global pointer mapping (no XInput)");
1067 XGetPointerMapping(xfc->display, x11_map, NUM_BUTTONS_MAPPED);
1073static const button_map xf_button_flags[NUM_BUTTONS_MAPPED] = {
1074 { Button1, PTR_FLAGS_BUTTON1 },
1075 { Button2, PTR_FLAGS_BUTTON3 },
1076 { Button3, PTR_FLAGS_BUTTON2 },
1077 { Button4, PTR_FLAGS_WHEEL | 0x78 },
1079 { Button5, PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | (0x100 - 0x78) },
1080 { 6, PTR_FLAGS_HWHEEL | PTR_FLAGS_WHEEL_NEGATIVE | (0x100 - 0x78) },
1081 { 7, PTR_FLAGS_HWHEEL | 0x78 },
1082 { 8, PTR_XFLAGS_BUTTON1 },
1083 { 9, PTR_XFLAGS_BUTTON2 },
1084 { 97, PTR_XFLAGS_BUTTON1 },
1085 { 112, PTR_XFLAGS_BUTTON2 }
1088static UINT16 get_flags_for_button(
size_t button)
1090 for (
size_t x = 0; x < ARRAYSIZE(xf_button_flags); x++)
1094 if (map->button == button)
1101void xf_button_map_init(xfContext* xfc)
1108 unsigned char x11_map[112] = WINPR_C_ARRAY_INIT;
1111 WINPR_ASSERT(xfc->common.context.settings);
1113 x11_map[0] = Button1;
1114 x11_map[1] = Button2;
1115 x11_map[2] = Button3;
1116 x11_map[3] = Button4;
1117 x11_map[4] = Button5;
1128 xf_get_x11_button_map(xfc, x11_map);
1134 for (
size_t physical = 0; physical < ARRAYSIZE(x11_map); ++physical)
1136 const unsigned char logical = x11_map[physical];
1137 const UINT16 flags = get_flags_for_button(logical);
1139 if ((logical != 0) && (flags != 0))
1141 if (pos >= NUM_BUTTONS_MAPPED)
1143 WLog_ERR(TAG,
"Failed to map mouse button to RDP button, no space");
1148 map->button = logical;
1149 map->flags = get_flags_for_button(physical + Button1);
1166static BOOL xf_pre_connect(freerdp* instance)
1168 UINT32 maxWidth = 0;
1169 UINT32 maxHeight = 0;
1171 WINPR_ASSERT(instance);
1173 rdpContext* context = instance->context;
1174 WINPR_ASSERT(context);
1175 xfContext* xfc = (xfContext*)context;
1177 rdpSettings* settings = context->settings;
1178 WINPR_ASSERT(settings);
1185 if (!xf_setup_x11(xfc))
1193 if (PubSub_SubscribeChannelConnected(context->pubSub, xf_OnChannelConnectedEventHandler) < 0)
1195 if (PubSub_SubscribeChannelDisconnected(context->pubSub, xf_OnChannelDisconnectedEventHandler) <
1203 char login_name[MAX_PATH] = WINPR_C_ARRAY_INIT;
1204 ULONG size =
sizeof(login_name) - 1;
1206 if (GetUserNameExA(NameSamCompatible, login_name, &size))
1211 WLog_INFO(TAG,
"No user name set. - Using login name: %s",
1221 WLog_INFO(TAG,
"auth-only, but no password set. Please provide one.");
1225 WLog_INFO(TAG,
"Authentication only. Don't connect to X.");
1231 xfc->common.context.settings, FreeRDP_KeyboardRemappingList);
1233 xfc->remap_table = freerdp_keyboard_remap_string_to_list(KeyboardRemappingList);
1234 if (!xfc->remap_table)
1236 if (!xf_keyboard_init(xfc))
1238 if (!xf_keyboard_action_script_init(xfc))
1240 if (!xf_detect_monitors(xfc, &maxWidth, &maxHeight))
1264 settings, FreeRDP_DesktopWidth,
1268 settings, FreeRDP_DesktopHeight,
1279 xf_button_map_init(xfc);
1283static BOOL xf_inject_keypress(rdpContext* context,
const char* buffer,
size_t size)
1285 WCHAR wbuffer[64] = WINPR_C_ARRAY_INIT;
1286 const SSIZE_T len = ConvertUtf8NToWChar(buffer, size, wbuffer, ARRAYSIZE(wbuffer));
1290 rdpInput* input = context->input;
1291 WINPR_ASSERT(input);
1293 for (SSIZE_T x = 0; x < len; x++)
1295 const WCHAR code = wbuffer[x];
1296 freerdp_input_send_unicode_keyboard_event(input, 0, code);
1298 freerdp_input_send_unicode_keyboard_event(input, KBD_FLAGS_RELEASE, code);
1304static BOOL xf_process_pipe(rdpContext* context,
const char* pipe)
1306 int fd = open(pipe, O_NONBLOCK | O_RDONLY);
1309 char ebuffer[256] = WINPR_C_ARRAY_INIT;
1310 WLog_ERR(TAG,
"pipe '%s' open returned %s [%d]", pipe,
1311 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)), errno);
1314 while (!freerdp_shall_disconnect_context(context))
1316 char buffer[64] = WINPR_C_ARRAY_INIT;
1318 ssize_t rd = read(fd, buffer,
sizeof(buffer) - 1);
1321 char ebuffer[256] = WINPR_C_ARRAY_INIT;
1322 if ((errno == EAGAIN) || (errno == 0))
1329 WLog_ERR(TAG,
"pipe '%s' read returned %s [%d]", pipe,
1330 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)), errno);
1335 char ebuffer[256] = WINPR_C_ARRAY_INIT;
1336 WLog_ERR(TAG,
"pipe '%s' read returned %s [%d]", pipe,
1337 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)), errno);
1342 if (!xf_inject_keypress(context, buffer, WINPR_ASSERTING_INT_CAST(
size_t, rd)))
1350static void cleanup_pipe(WINPR_ATTR_UNUSED
int signum, WINPR_ATTR_UNUSED
const char* signame,
1353 const char* pipe = context;
1359static DWORD WINAPI xf_handle_pipe(
void* arg)
1361 xfContext* xfc = arg;
1364 rdpContext* context = &xfc->common.context;
1365 WINPR_ASSERT(context);
1367 rdpSettings* settings = context->settings;
1368 WINPR_ASSERT(settings);
1373 const int rc = mkfifo(pipe, S_IWUSR | S_IRUSR);
1376 char ebuffer[256] = WINPR_C_ARRAY_INIT;
1377 WLog_ERR(TAG,
"Failed to create named pipe '%s': %s [%d]", pipe,
1378 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)), errno);
1382 void* ctx = WINPR_CAST_CONST_PTR_AWAY(pipe,
void*);
1383 if (freerdp_add_signal_cleanup_handler(ctx, cleanup_pipe))
1386 xf_process_pipe(context, pipe);
1388 freerdp_del_signal_cleanup_handler(ctx, cleanup_pipe);
1400static BOOL xf_post_connect(freerdp* instance)
1402 ResizeWindowEventArgs e = WINPR_C_ARRAY_INIT;
1404 WINPR_ASSERT(instance);
1405 xfContext* xfc = (xfContext*)instance->context;
1406 rdpContext* context = instance->context;
1407 WINPR_ASSERT(context);
1409 rdpSettings* settings = context->settings;
1410 WINPR_ASSERT(settings);
1412 rdpUpdate* update = context->update;
1413 WINPR_ASSERT(update);
1416 xfc->remote_app = TRUE;
1418 if (!xf_create_window(xfc))
1421 if (!xf_get_pixmap_info(xfc))
1424 if (!gdi_init(instance, xf_get_local_color_format(xfc, TRUE)))
1427 if (!xf_create_image(xfc))
1430 if (!xf_register_pointer(context->graphics))
1442 if (!xfc->xrenderAvailable)
1446 WLog_ERR(TAG,
"XRender not available: disabling smart-sizing");
1453 WLog_ERR(TAG,
"XRender not available: disabling local multi-touch gestures");
1459 update->DesktopResize = xf_sw_desktop_resize;
1460 update->EndPaint = xf_end_paint;
1461 update->PlaySound = xf_play_sound;
1462 update->SetKeyboardIndicators = xf_keyboard_set_indicators;
1463 update->SetKeyboardImeStatus = xf_keyboard_set_ime_status;
1465 const BOOL serverIsWindowsPlatform =
1468 !(xfc->clipboard = xf_clipboard_new(xfc, !serverIsWindowsPlatform)))
1471 if (!(xfc->xfDisp = xf_disp_new(xfc)))
1477 xfc->pipethread = CreateThread(
nullptr, 0, xf_handle_pipe, xfc, 0,
nullptr);
1478 if (!xfc->pipethread)
1482 EventArgsInit(&e,
"xfreerdp");
1487 return PubSub_OnResizeWindow(context->pubSub, xfc, &e) >= 0;
1490static void xf_post_disconnect(freerdp* instance)
1492 xfContext* xfc =
nullptr;
1493 rdpContext* context =
nullptr;
1495 if (!instance || !instance->context)
1498 context = instance->context;
1499 xfc = (xfContext*)context;
1500 PubSub_UnsubscribeChannelConnected(instance->context->pubSub,
1501 xf_OnChannelConnectedEventHandler);
1502 PubSub_UnsubscribeChannelDisconnected(instance->context->pubSub,
1503 xf_OnChannelDisconnectedEventHandler);
1506 if (xfc->pipethread)
1508 (void)WaitForSingleObject(xfc->pipethread, INFINITE);
1509 (void)CloseHandle(xfc->pipethread);
1510 xfc->pipethread =
nullptr;
1514 xf_clipboard_free(xfc->clipboard);
1515 xfc->clipboard =
nullptr;
1520 xf_disp_free(xfc->xfDisp);
1521 xfc->xfDisp =
nullptr;
1524 if ((xfc->window !=
nullptr) && (xfc->drawable == xfc->window->handle))
1527 xf_DestroyDummyWindow(xfc, xfc->drawable);
1529 freerdp_keyboard_remap_free(xfc->remap_table);
1530 xfc->remap_table =
nullptr;
1532 xf_destroy_window(xfc);
1535static void xf_post_final_disconnect(freerdp* instance)
1537 xfContext* xfc =
nullptr;
1538 rdpContext* context =
nullptr;
1540 if (!instance || !instance->context)
1543 context = instance->context;
1544 xfc = (xfContext*)context;
1546 xf_keyboard_free(xfc);
1547 xf_teardown_x11(xfc);
1550static int xf_logon_error_info(freerdp* instance, UINT32 data, UINT32 type)
1552 xfContext* xfc = (xfContext*)instance->context;
1553 const char* str_data = freerdp_get_logon_error_info_data(data);
1554 const char* str_type = freerdp_get_logon_error_info_type(type);
1555 WLog_INFO(TAG,
"Logon Error Info %s [%s]", str_data, str_type);
1556 if (type != LOGON_MSG_SESSION_CONTINUE)
1558 if (!xf_rail_disable_remoteapp_mode(xfc))
1564static BOOL handle_window_events(freerdp* instance)
1566 if (!xf_process_x_events(instance))
1568 WLog_DBG(TAG,
"Closed from X11");
1569 freerdp_abort_connect_context(instance->context);
1584static DWORD WINAPI xf_client_thread(LPVOID param)
1586 DWORD exit_code = 0;
1587 DWORD waitStatus = 0;
1588 HANDLE inputEvent =
nullptr;
1590 freerdp* instance = (freerdp*)param;
1591 WINPR_ASSERT(instance);
1593 const BOOL status = freerdp_connect(instance);
1594 rdpContext* context = instance->context;
1595 WINPR_ASSERT(context);
1596 xfContext* xfc = (xfContext*)instance->context;
1599 rdpSettings* settings = context->settings;
1600 WINPR_ASSERT(settings);
1604 UINT32 error = freerdp_get_last_error(instance->context);
1605 exit_code = (uint32_t)xf_map_error_to_exit_code(error);
1608 exit_code = XF_EXIT_SUCCESS;
1616 WLog_ERR(TAG,
"Authentication only, exit status %" PRId32
"", !status);
1622 WLog_ERR(TAG,
"Freerdp connect error exit status %" PRId32
"", !status);
1623 exit_code = freerdp_error_info(instance);
1625 if (freerdp_get_last_error(instance->context) == FREERDP_ERROR_AUTHENTICATION_FAILED)
1626 exit_code = XF_EXIT_AUTH_FAILURE;
1627 else if (exit_code == ERRINFO_SUCCESS)
1628 exit_code = XF_EXIT_CONN_FAILED;
1633 inputEvent = xfc->x11event;
1635 while (!freerdp_shall_disconnect_context(instance->context))
1637 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
1639 handles[nCount++] = inputEvent;
1646 if (freerdp_focus_required(instance))
1648 xf_keyboard_focus_in(xfc);
1649 xf_keyboard_focus_in(xfc);
1654 freerdp_get_event_handles(context, &handles[nCount], ARRAYSIZE(handles) - nCount);
1658 WLog_ERR(TAG,
"freerdp_get_event_handles failed");
1666 xf_floatbar_hide_and_show(xfc->window->floatbar);
1668 waitStatus = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
1670 if (waitStatus == WAIT_FAILED)
1674 if (!freerdp_check_event_handles(context))
1676 if (client_auto_reconnect_ex(instance, handle_window_events))
1684 const UINT32 error = freerdp_get_last_error(instance->context);
1686 if (freerdp_error_info(instance) == 0)
1687 exit_code = (uint32_t)xf_map_error_to_exit_code(error);
1690 if (freerdp_get_last_error(context) == FREERDP_ERROR_SUCCESS)
1691 WLog_ERR(TAG,
"Failed to check FreeRDP file descriptor");
1697 if (!handle_window_events(instance))
1703 exit_code = freerdp_error_info(instance);
1705 if (exit_code == XF_EXIT_DISCONNECT &&
1706 freerdp_get_disconnect_ultimatum(context) == Disconnect_Ultimatum_user_requested)
1709 WLog_INFO(TAG,
"Error info says user did not initiate but disconnect ultimatum says "
1710 "they did; treat this as a user logoff");
1711 exit_code = XF_EXIT_LOGOFF;
1717 freerdp_disconnect(instance);
1719 ExitThread(exit_code);
1723int xf_exit_code_from_disconnect_reason(DWORD reason)
1725 if ((reason == 0) || ((reason >= XF_EXIT_PARSE_ARGUMENTS) && (reason <= XF_EXIT_CODE_LAST)))
1726 return WINPR_ASSERTING_INT_CAST(
int, reason);
1728 else if (reason >= 0x100 && reason <= 0x10A)
1729 reason -= 0x100 + XF_EXIT_LICENSE_INTERNAL;
1731 else if (reason >= 0x10c9 && reason <= 0x1193)
1732 reason = XF_EXIT_RDP;
1734 else if (!(reason <= 0xC))
1735 reason = XF_EXIT_UNKNOWN;
1737 return WINPR_ASSERTING_INT_CAST(
int, reason);
1740static void xf_TerminateEventHandler(
void* context,
const TerminateEventArgs* e)
1742 rdpContext* ctx = (rdpContext*)context;
1744 freerdp_abort_connect_context(ctx);
1748static void xf_ZoomingChangeEventHandler(
void* context,
const ZoomingChangeEventArgs* e)
1752 rdpSettings* settings =
nullptr;
1753 xfContext* xfc = (xfContext*)context;
1757 settings = xfc->common.context.settings;
1758 WINPR_ASSERT(settings);
1760 w = xfc->scaledWidth + e->dx;
1761 h = xfc->scaledHeight + e->dy;
1763 if (e->dx == 0 && e->dy == 0)
1772 if (w == xfc->scaledWidth && h == xfc->scaledHeight)
1775 xfc->scaledWidth = w;
1776 xfc->scaledHeight = h;
1777 xf_draw_screen(xfc, 0, 0,
1778 WINPR_ASSERTING_INT_CAST(
1780 WINPR_ASSERTING_INT_CAST(
1784static void xf_PanningChangeEventHandler(
void* context,
const PanningChangeEventArgs* e)
1786 xfContext* xfc = (xfContext*)context;
1787 rdpSettings* settings =
nullptr;
1792 settings = xfc->common.context.settings;
1793 WINPR_ASSERT(settings);
1795 if (e->dx == 0 && e->dy == 0)
1798 xfc->offset_x += e->dx;
1799 xfc->offset_y += e->dy;
1800 xf_draw_screen(xfc, 0, 0,
1801 WINPR_ASSERTING_INT_CAST(
1803 WINPR_ASSERTING_INT_CAST(
1812static BOOL xfreerdp_client_global_init(
void)
1815 (void)setlocale(LC_ALL,
"");
1817 return (freerdp_handle_signals() == 0);
1820static void xfreerdp_client_global_uninit(
void)
1824static int xfreerdp_client_start(rdpContext* context)
1826 xfContext* xfc = (xfContext*)context;
1827 rdpSettings* settings = context->settings;
1831 WLog_ERR(TAG,
"error: server hostname was not specified with /v:<server>[:port]");
1835 if (!(xfc->common.thread =
1836 CreateThread(
nullptr, 0, xf_client_thread, context->instance, 0,
nullptr)))
1838 WLog_ERR(TAG,
"failed to create client thread");
1845static Atom get_supported_atom(xfContext* xfc,
const char* atomName)
1847 const Atom atom = Logging_XInternAtom(xfc->log, xfc->display, atomName, False);
1849 for (
unsigned long i = 0; i < xfc->supportedAtomCount; i++)
1851 if (xfc->supportedAtoms[i] == atom)
1858void xf_teardown_x11(xfContext* xfc)
1864 LogDynAndXCloseDisplay(xfc->log, xfc->display);
1865 xfc->display =
nullptr;
1870 (void)CloseHandle(xfc->x11event);
1871 xfc->x11event =
nullptr;
1876 (void)CloseHandle(xfc->mutex);
1877 xfc->mutex =
nullptr;
1880 if (xfc->vscreen.monitors)
1882 free(xfc->vscreen.monitors);
1883 xfc->vscreen.monitors =
nullptr;
1885 xfc->vscreen.nmonitors = 0;
1887 free(xfc->supportedAtoms);
1888 xfc->supportedAtoms =
nullptr;
1889 xfc->supportedAtomCount = 0;
1892BOOL xf_setup_x11(xfContext* xfc)
1896 xfc->UseXThreads = TRUE;
1903 if (xfc->UseXThreads)
1905 if (!XInitThreads())
1907 WLog_WARN(TAG,
"XInitThreads() failure");
1908 xfc->UseXThreads = FALSE;
1912 xfc->display = XOpenDisplay(
nullptr);
1916 WLog_ERR(TAG,
"failed to open display: %s", XDisplayName(
nullptr));
1917 WLog_ERR(TAG,
"Please check that the $DISPLAY environment variable is properly set.");
1922 WLog_INFO(TAG,
"Enabling X11 debug mode.");
1923 XSynchronize(xfc->display, TRUE);
1925 def_error_handler = XSetErrorHandler(xf_error_handler_ex);
1927 xfc->mutex = CreateMutex(
nullptr, FALSE,
nullptr);
1931 WLog_ERR(TAG,
"Could not create mutex!");
1935 xfc->xfds = ConnectionNumber(xfc->display);
1936 xfc->screen_number = DefaultScreen(xfc->display);
1937 xfc->screen = ScreenOfDisplay(xfc->display, xfc->screen_number);
1938 xfc->big_endian = (ImageByteOrder(xfc->display) == MSBFirst);
1940 xfc->complex_regions = TRUE;
1941 xfc->NET_SUPPORTED = Logging_XInternAtom(xfc->log, xfc->display,
"_NET_SUPPORTED", True);
1942 xfc->NET_SUPPORTING_WM_CHECK =
1943 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_SUPPORTING_WM_CHECK", True);
1945 if ((xfc->NET_SUPPORTED != None) && (xfc->NET_SUPPORTING_WM_CHECK != None))
1947 Atom actual_type = 0;
1948 int actual_format = 0;
1949 unsigned long nitems = 0;
1950 unsigned long after = 0;
1951 unsigned char* data =
nullptr;
1952 int status = LogDynAndXGetWindowProperty(
1953 xfc->log, xfc->display, RootWindowOfScreen(xfc->screen), xfc->NET_SUPPORTED, 0, 1024,
1954 False, XA_ATOM, &actual_type, &actual_format, &nitems, &after, &data);
1956 if ((status == Success) && (actual_type == XA_ATOM) && (actual_format == 32))
1958 xfc->supportedAtomCount = nitems;
1959 xfc->supportedAtoms = calloc(xfc->supportedAtomCount,
sizeof(Atom));
1960 WINPR_ASSERT(xfc->supportedAtoms);
1961 memcpy(xfc->supportedAtoms, data, nitems *
sizeof(Atom));
1968 xfc->XWAYLAND_MAY_GRAB_KEYBOARD =
1969 Logging_XInternAtom(xfc->log, xfc->display,
"_XWAYLAND_MAY_GRAB_KEYBOARD", False);
1970 xfc->NET_WM_ICON = Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ICON", False);
1971 xfc->MOTIF_WM_HINTS = Logging_XInternAtom(xfc->log, xfc->display,
"_MOTIF_WM_HINTS", False);
1972 xfc->NET_NUMBER_OF_DESKTOPS =
1973 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_NUMBER_OF_DESKTOPS", False);
1974 xfc->NET_CURRENT_DESKTOP =
1975 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_CURRENT_DESKTOP", False);
1976 xfc->NET_WORKAREA = Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WORKAREA", False);
1977 xfc->NET_WM_STATE = get_supported_atom(xfc,
"_NET_WM_STATE");
1978 xfc->NET_WM_STATE_MODAL = get_supported_atom(xfc,
"_NET_WM_STATE_MODAL");
1979 xfc->NET_WM_STATE_STICKY = get_supported_atom(xfc,
"_NET_WM_STATE_STICKY");
1980 xfc->NET_WM_STATE_MAXIMIZED_HORZ =
1981 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_STATE_MAXIMIZED_HORZ", False);
1982 xfc->NET_WM_STATE_MAXIMIZED_VERT =
1983 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_STATE_MAXIMIZED_VERT", False);
1984 xfc->NET_WM_STATE_SHADED = get_supported_atom(xfc,
"_NET_WM_STATE_SHADED");
1985 xfc->NET_WM_STATE_SKIP_TASKBAR = get_supported_atom(xfc,
"_NET_WM_STATE_SKIP_TASKBAR");
1986 xfc->NET_WM_STATE_SKIP_PAGER = get_supported_atom(xfc,
"_NET_WM_STATE_SKIP_PAGER");
1987 xfc->NET_WM_STATE_HIDDEN = get_supported_atom(xfc,
"_NET_WM_STATE_HIDDEN");
1988 xfc->NET_WM_STATE_FULLSCREEN = get_supported_atom(xfc,
"_NET_WM_STATE_FULLSCREEN");
1989 xfc->NET_WM_STATE_ABOVE = get_supported_atom(xfc,
"_NET_WM_STATE_ABOVE");
1990 xfc->NET_WM_STATE_BELOW = get_supported_atom(xfc,
"_NET_WM_STATE_BELOW");
1991 xfc->NET_WM_STATE_DEMANDS_ATTENTION =
1992 get_supported_atom(xfc,
"_NET_WM_STATE_DEMANDS_ATTENTION");
1993 xfc->NET_WM_FULLSCREEN_MONITORS = get_supported_atom(xfc,
"_NET_WM_FULLSCREEN_MONITORS");
1994 xfc->NET_WM_NAME = Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_NAME", False);
1995 xfc->NET_WM_PID = Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_PID", False);
1996 xfc->NET_WM_WINDOW_TYPE =
1997 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE", False);
1998 xfc->NET_WM_WINDOW_TYPE_NORMAL =
1999 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE_NORMAL", False);
2000 xfc->NET_WM_WINDOW_TYPE_DIALOG =
2001 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE_DIALOG", False);
2002 xfc->NET_WM_WINDOW_TYPE_POPUP =
2003 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE_POPUP", False);
2004 xfc->NET_WM_WINDOW_TYPE_POPUP_MENU =
2005 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE_POPUP_MENU", False);
2006 xfc->NET_WM_WINDOW_TYPE_UTILITY =
2007 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE_UTILITY", False);
2008 xfc->NET_WM_WINDOW_TYPE_DROPDOWN_MENU =
2009 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", False);
2010 xfc->NET_WM_STATE_SKIP_TASKBAR =
2011 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_STATE_SKIP_TASKBAR", False);
2012 xfc->NET_WM_STATE_SKIP_PAGER =
2013 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_STATE_SKIP_PAGER", False);
2014 xfc->NET_WM_MOVERESIZE =
2015 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_MOVERESIZE", False);
2016 xfc->NET_MOVERESIZE_WINDOW =
2017 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_MOVERESIZE_WINDOW", False);
2018 xfc->UTF8_STRING = Logging_XInternAtom(xfc->log, xfc->display,
"UTF8_STRING", FALSE);
2019 xfc->WM_PROTOCOLS = Logging_XInternAtom(xfc->log, xfc->display,
"WM_PROTOCOLS", False);
2020 xfc->WM_DELETE_WINDOW = Logging_XInternAtom(xfc->log, xfc->display,
"WM_DELETE_WINDOW", False);
2021 xfc->WM_STATE = Logging_XInternAtom(xfc->log, xfc->display,
"WM_STATE", False);
2022 xfc->x11event = CreateFileDescriptorEvent(
nullptr, FALSE, FALSE, xfc->xfds, WINPR_FD_READ);
2024 xfc->NET_WM_ALLOWED_ACTIONS =
2025 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ALLOWED_ACTIONS", False);
2027 xfc->NET_WM_ACTION_CLOSE =
2028 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_CLOSE", False);
2029 xfc->NET_WM_ACTION_MINIMIZE =
2030 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_MINIMIZE", False);
2031 xfc->NET_WM_ACTION_MOVE =
2032 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_MOVE", False);
2033 xfc->NET_WM_ACTION_RESIZE =
2034 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_RESIZE", False);
2035 xfc->NET_WM_ACTION_MAXIMIZE_HORZ =
2036 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_MAXIMIZE_HORZ", False);
2037 xfc->NET_WM_ACTION_MAXIMIZE_VERT =
2038 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_MAXIMIZE_VERT", False);
2039 xfc->NET_WM_ACTION_FULLSCREEN =
2040 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_FULLSCREEN", False);
2041 xfc->NET_WM_ACTION_CHANGE_DESKTOP =
2042 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_CHANGE_DESKTOP", False);
2046 WLog_ERR(TAG,
"Could not create xfds event");
2050 xf_check_extensions(xfc);
2052 xfc->vscreen.monitors = calloc(16,
sizeof(MONITOR_INFO));
2054 if (!xfc->vscreen.monitors)
2059 xf_teardown_x11(xfc);
2063static BOOL xfreerdp_client_new(freerdp* instance, rdpContext* context)
2065 xfContext* xfc = (xfContext*)instance->context;
2066 WINPR_ASSERT(context);
2068 WINPR_ASSERT(!xfc->display);
2069 WINPR_ASSERT(!xfc->mutex);
2070 WINPR_ASSERT(!xfc->x11event);
2071 instance->PreConnect = xf_pre_connect;
2072 instance->PostConnect = xf_post_connect;
2073 instance->PostDisconnect = xf_post_disconnect;
2074 instance->PostFinalDisconnect = xf_post_final_disconnect;
2075 instance->LogonErrorInfo = xf_logon_error_info;
2076 instance->GetAccessToken = client_cli_get_access_token;
2077 if (PubSub_SubscribeTerminate(context->pubSub, xf_TerminateEventHandler) < 0)
2080 if (PubSub_SubscribeZoomingChange(context->pubSub, xf_ZoomingChangeEventHandler) < 0)
2082 if (PubSub_SubscribePanningChange(context->pubSub, xf_PanningChangeEventHandler) < 0)
2085 xfc->log = WLog_Get(TAG);
2090static void xfreerdp_client_free(WINPR_ATTR_UNUSED freerdp* instance, rdpContext* context)
2095 if (context->pubSub)
2097 PubSub_UnsubscribeTerminate(context->pubSub, xf_TerminateEventHandler);
2099 PubSub_UnsubscribeZoomingChange(context->pubSub, xf_ZoomingChangeEventHandler);
2100 PubSub_UnsubscribePanningChange(context->pubSub, xf_PanningChangeEventHandler);
2105int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
2107 pEntryPoints->Version = 1;
2108 pEntryPoints->Size =
sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
2109 pEntryPoints->GlobalInit = xfreerdp_client_global_init;
2110 pEntryPoints->GlobalUninit = xfreerdp_client_global_uninit;
2111 pEntryPoints->ContextSize =
sizeof(xfContext);
2112 pEntryPoints->ClientNew = xfreerdp_client_new;
2113 pEntryPoints->ClientFree = xfreerdp_client_free;
2114 pEntryPoints->ClientStart = xfreerdp_client_start;
2115 pEntryPoints->ClientStop = freerdp_client_common_stop;
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL val)
Sets a BOOL settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_string(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *val)
Sets a string settings value. The param is copied.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.