25#include <freerdp/config.h>
26#include "xf_reconnect.h"
29#include <winpr/cast.h>
30#include <winpr/assert.h>
31#include <winpr/sspicli.h>
40#include <X11/extensions/Xrender.h>
44#include <X11/extensions/XInput.h>
45#include <X11/extensions/XInput2.h>
49#include <X11/Xcursor/Xcursor.h>
53#include <X11/extensions/Xinerama.h>
56#include <X11/XKBlib.h>
68#include <sys/select.h>
72#include <freerdp/freerdp.h>
73#include <freerdp/constants.h>
74#include <freerdp/codec/nsc.h>
75#include <freerdp/codec/rfx.h>
76#include <freerdp/codec/color.h>
77#include <freerdp/codec/bitmap.h>
79#include <freerdp/utils/signal.h>
80#include <freerdp/utils/passphrase.h>
81#include <freerdp/client/cliprdr.h>
82#include <freerdp/client/channels.h>
84#include <freerdp/client/file.h>
85#include <freerdp/client/cmdline.h>
86#include <freerdp/client/aad_helper.h>
89#include <winpr/synch.h>
90#include <winpr/file.h>
91#include <winpr/print.h>
92#include <winpr/sysinfo.h>
95#if defined(CHANNEL_TSMF_CLIENT)
100#include "xf_cliprdr.h"
103#include "xf_monitor.h"
104#include "xf_graphics.h"
105#include "xf_keyboard.h"
106#include "xf_channels.h"
107#include "xf_client.h"
111#include <freerdp/log.h>
112#define TAG CLIENT_TAG("x11")
114#define MIN_PIXEL_DIFF 0.001
116struct xf_exit_code_map_t
121static const struct xf_exit_code_map_t xf_exit_code_map[] = {
122 { FREERDP_ERROR_SUCCESS, XF_EXIT_SUCCESS },
123 { FREERDP_ERROR_AUTHENTICATION_FAILED, XF_EXIT_AUTH_FAILURE },
124 { FREERDP_ERROR_SECURITY_NEGO_CONNECT_FAILED, XF_EXIT_NEGO_FAILURE },
125 { FREERDP_ERROR_CONNECT_LOGON_FAILURE, XF_EXIT_LOGON_FAILURE },
126 { FREERDP_ERROR_CONNECT_ACCOUNT_LOCKED_OUT, XF_EXIT_ACCOUNT_LOCKED_OUT },
127 { FREERDP_ERROR_PRE_CONNECT_FAILED, XF_EXIT_PRE_CONNECT_FAILED },
128 { FREERDP_ERROR_CONNECT_UNDEFINED, XF_EXIT_CONNECT_UNDEFINED },
129 { FREERDP_ERROR_POST_CONNECT_FAILED, XF_EXIT_POST_CONNECT_FAILED },
130 { FREERDP_ERROR_DNS_ERROR, XF_EXIT_DNS_ERROR },
131 { FREERDP_ERROR_DNS_NAME_NOT_FOUND, XF_EXIT_DNS_NAME_NOT_FOUND },
132 { FREERDP_ERROR_CONNECT_FAILED, XF_EXIT_CONNECT_FAILED },
133 { FREERDP_ERROR_MCS_CONNECT_INITIAL_ERROR, XF_EXIT_MCS_CONNECT_INITIAL_ERROR },
134 { FREERDP_ERROR_TLS_CONNECT_FAILED, XF_EXIT_TLS_CONNECT_FAILED },
135 { FREERDP_ERROR_INSUFFICIENT_PRIVILEGES, XF_EXIT_INSUFFICIENT_PRIVILEGES },
136 { FREERDP_ERROR_CONNECT_CANCELLED, XF_EXIT_CONNECT_CANCELLED },
137 { FREERDP_ERROR_CONNECT_TRANSPORT_FAILED, XF_EXIT_CONNECT_TRANSPORT_FAILED },
138 { FREERDP_ERROR_CONNECT_PASSWORD_EXPIRED, XF_EXIT_CONNECT_PASSWORD_EXPIRED },
139 { FREERDP_ERROR_CONNECT_PASSWORD_MUST_CHANGE, XF_EXIT_CONNECT_PASSWORD_MUST_CHANGE },
140 { FREERDP_ERROR_CONNECT_KDC_UNREACHABLE, XF_EXIT_CONNECT_KDC_UNREACHABLE },
141 { FREERDP_ERROR_CONNECT_ACCOUNT_DISABLED, XF_EXIT_CONNECT_ACCOUNT_DISABLED },
142 { FREERDP_ERROR_CONNECT_PASSWORD_CERTAINLY_EXPIRED,
143 XF_EXIT_CONNECT_PASSWORD_CERTAINLY_EXPIRED },
144 { FREERDP_ERROR_CONNECT_CLIENT_REVOKED, XF_EXIT_CONNECT_CLIENT_REVOKED },
145 { FREERDP_ERROR_CONNECT_WRONG_PASSWORD, XF_EXIT_CONNECT_WRONG_PASSWORD },
146 { FREERDP_ERROR_CONNECT_ACCESS_DENIED, XF_EXIT_CONNECT_ACCESS_DENIED },
147 { FREERDP_ERROR_CONNECT_ACCOUNT_RESTRICTION, XF_EXIT_CONNECT_ACCOUNT_RESTRICTION },
148 { FREERDP_ERROR_CONNECT_ACCOUNT_EXPIRED, XF_EXIT_CONNECT_ACCOUNT_EXPIRED },
149 { FREERDP_ERROR_CONNECT_LOGON_TYPE_NOT_GRANTED, XF_EXIT_CONNECT_LOGON_TYPE_NOT_GRANTED },
150 { FREERDP_ERROR_CONNECT_NO_OR_MISSING_CREDENTIALS, XF_EXIT_CONNECT_NO_OR_MISSING_CREDENTIALS },
151 { FREERDP_ERROR_CONNECT_TARGET_BOOTING, XF_EXIT_CONNECT_TARGET_BOOTING },
152 { FREERDP_ERROR_CONNECT_HYBRID_REQUIRED_BY_SERVER, XF_EXIT_CONNECT_HYBRID_REQUIRED_BY_SERVER }
155static BOOL xf_setup_x11(xfContext* xfc);
156static void xf_teardown_x11(xfContext* xfc);
158static int xf_map_error_to_exit_code(DWORD error)
160 for (
size_t x = 0; x < ARRAYSIZE(xf_exit_code_map); x++)
162 const struct xf_exit_code_map_t* cur = &xf_exit_code_map[x];
163 if (cur->error == error)
167 return XF_EXIT_CONN_FAILED;
170static int (*def_error_handler)(Display*, XErrorEvent*);
171static int xf_error_handler_ex(Display* d, XErrorEvent* ev);
172static void xf_check_extensions(xfContext* context);
173static BOOL xf_get_pixmap_info(xfContext* xfc);
176static void xf_draw_screen_scaled(xfContext* xfc,
int x,
int y,
int w,
int h)
178 XTransform transform = WINPR_C_ARRAY_INIT;
179 Picture windowPicture = 0;
180 Picture primaryPicture = 0;
181 XRenderPictureAttributes pa;
182 XRenderPictFormat* picFormat =
nullptr;
185 const char* filter =
nullptr;
188 rdpSettings* settings = xfc->common.context.settings;
189 WINPR_ASSERT(settings);
191 if (xfc->scaledWidth <= 0 || xfc->scaledHeight <= 0)
193 WLog_ERR(TAG,
"the current window dimensions are invalid");
200 WLog_ERR(TAG,
"the window dimensions are invalid");
204 const double xScalingFactor = 1.0 *
206 (double)xfc->scaledWidth;
207 const double yScalingFactor = 1.0 *
209 (double)xfc->scaledHeight;
210 LogDynAndXSetFillStyle(xfc->log, xfc->display, xfc->gc, FillSolid);
211 LogDynAndXSetForeground(xfc->log, xfc->display, xfc->gc, 0);
214 XRectangle box1 = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, xfc->window->width),
215 WINPR_ASSERTING_INT_CAST(UINT16, xfc->window->height) };
216 XRectangle box2 = { WINPR_ASSERTING_INT_CAST(INT16, xfc->offset_x),
217 WINPR_ASSERTING_INT_CAST(INT16, xfc->offset_y),
218 WINPR_ASSERTING_INT_CAST(UINT16, xfc->scaledWidth),
219 WINPR_ASSERTING_INT_CAST(UINT16, xfc->scaledHeight) };
220 Region reg1 = XCreateRegion();
221 Region reg2 = XCreateRegion();
222 XUnionRectWithRegion(&box1, reg1, reg1);
223 XUnionRectWithRegion(&box2, reg2, reg2);
225 if (XSubtractRegion(reg1, reg2, reg1) && !XEmptyRegion(reg1))
227 LogDynAndXSetRegion(xfc->log, xfc->display, xfc->gc, reg1);
228 LogDynAndXFillRectangle(xfc->log, xfc->display, xfc->window->handle, xfc->gc, 0, 0,
229 WINPR_ASSERTING_INT_CAST(UINT16, xfc->window->width),
230 WINPR_ASSERTING_INT_CAST(UINT16, xfc->window->height));
231 LogDynAndXSetClipMask(xfc->log, xfc->display, xfc->gc, None);
234 XDestroyRegion(reg1);
235 XDestroyRegion(reg2);
237 picFormat = XRenderFindVisualFormat(xfc->display, xfc->visual);
238 pa.subwindow_mode = IncludeInferiors;
240 XRenderCreatePicture(xfc->display, xfc->primary, picFormat, CPSubwindowMode, &pa);
242 XRenderCreatePicture(xfc->display, xfc->window->handle, picFormat, CPSubwindowMode, &pa);
245 filter = FilterBilinear;
246 if (fabs(xScalingFactor - yScalingFactor) < MIN_PIXEL_DIFF)
248 const double inverseX = 1.0 / xScalingFactor;
249 const double inverseRoundedX = round(inverseX);
250 const double absInverse = fabs(inverseX - inverseRoundedX);
252 if (absInverse < MIN_PIXEL_DIFF)
253 filter = FilterNearest;
255 XRenderSetPictureFilter(xfc->display, primaryPicture, filter,
nullptr, 0);
256 transform.matrix[0][0] = XDoubleToFixed(xScalingFactor);
257 transform.matrix[0][1] = XDoubleToFixed(0.0);
258 transform.matrix[0][2] = XDoubleToFixed(0.0);
259 transform.matrix[1][0] = XDoubleToFixed(0.0);
260 transform.matrix[1][1] = XDoubleToFixed(yScalingFactor);
261 transform.matrix[1][2] = XDoubleToFixed(0.0);
262 transform.matrix[2][0] = XDoubleToFixed(0.0);
263 transform.matrix[2][1] = XDoubleToFixed(0.0);
264 transform.matrix[2][2] = XDoubleToFixed(1.0);
269 const double dx1 = floor(x / xScalingFactor);
270 const double dy1 = floor(y / yScalingFactor);
271 const double dx2 = ceil(x2 / xScalingFactor);
272 const double dy2 = ceil(y2 / yScalingFactor);
275 w = ((int)dx2) + 1 - x;
276 h = ((int)dy2) + 1 - y;
277 XRenderSetPictureTransform(xfc->display, primaryPicture, &transform);
278 XRenderComposite(xfc->display, PictOpSrc, primaryPicture, 0, windowPicture, x, y, 0, 0,
279 xfc->offset_x + x, xfc->offset_y + y, WINPR_ASSERTING_INT_CAST(uint32_t, w),
280 WINPR_ASSERTING_INT_CAST(uint32_t, h));
281 XRenderFreePicture(xfc->display, primaryPicture);
282 XRenderFreePicture(xfc->display, windowPicture);
285BOOL xf_picture_transform_required(xfContext* xfc)
287 rdpSettings* settings =
nullptr;
291 settings = xfc->common.context.settings;
292 WINPR_ASSERT(settings);
295 (xfc->offset_x != 0) || (xfc->offset_y != 0) ||
301void xf_draw_screen_(xfContext* xfc,
int x,
int y,
int w,
int h,
const char* fkt,
302 WINPR_ATTR_UNUSED
const char* file, WINPR_ATTR_UNUSED
int line)
306 WLog_DBG(TAG,
"called from [%s] xfc=nullptr", fkt);
310 if (w == 0 || h == 0)
312 WLog_WARN(TAG,
"invalid width and/or height specified: w=%d h=%d", w, h);
318 WLog_WARN(TAG,
"invalid xfc->window=nullptr");
324 if (xf_picture_transform_required(xfc))
326 xf_draw_screen_scaled(xfc, x, y, w, h);
331 LogDynAndXCopyArea(xfc->log, xfc->display, xfc->primary, xfc->window->handle, xfc->gc, x, y,
332 WINPR_ASSERTING_INT_CAST(uint32_t, w), WINPR_ASSERTING_INT_CAST(uint32_t, h),
336static BOOL xf_desktop_resize(rdpContext* context)
338 xfContext* xfc = (xfContext*)context;
342 rdpSettings* settings = context->settings;
343 WINPR_ASSERT(settings);
347 BOOL same = (xfc->primary == xfc->drawing);
348 LogDynAndXFreePixmap(xfc->log, xfc->display, xfc->primary);
350 WINPR_ASSERT(xfc->depth != 0);
351 if (!(xfc->primary = LogDynAndXCreatePixmap(
352 xfc->log, xfc->display, xfc->drawable,
355 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth))))
359 xfc->drawing = xfc->primary;
366 xfc->scaledWidth = WINPR_ASSERTING_INT_CAST(
368 xfc->scaledHeight = WINPR_ASSERTING_INT_CAST(
374 if (!xfc->fullscreen)
376 xf_ResizeDesktopWindow(
378 WINPR_ASSERTING_INT_CAST(
int,
380 WINPR_ASSERTING_INT_CAST(
int,
392 xfc->savedWidth = WINPR_ASSERTING_INT_CAST(
394 xfc->savedHeight = WINPR_ASSERTING_INT_CAST(
398 LogDynAndXSetFunction(xfc->log, xfc->display, xfc->gc, GXcopy);
399 LogDynAndXSetFillStyle(xfc->log, xfc->display, xfc->gc, FillSolid);
400 LogDynAndXSetForeground(xfc->log, xfc->display, xfc->gc, 0);
401 LogDynAndXFillRectangle(xfc->log, xfc->display, xfc->drawable, xfc->gc, 0, 0,
409static BOOL xf_paint(xfContext* xfc,
const GDI_RGN* region)
412 WINPR_ASSERT(region);
416 const RECTANGLE_16 rect = { .left = WINPR_ASSERTING_INT_CAST(UINT16, region->x),
417 .top = WINPR_ASSERTING_INT_CAST(UINT16, region->y),
419 WINPR_ASSERTING_INT_CAST(UINT16, region->x + region->w),
421 WINPR_ASSERTING_INT_CAST(UINT16, region->y + region->h) };
422 return xf_rail_paint(xfc, &rect);
426 LogDynAndXPutImage(xfc->log, xfc->display, xfc->primary, xfc->gc, xfc->image, region->x,
427 region->y, region->x, region->y,
428 WINPR_ASSERTING_INT_CAST(UINT16, region->w),
429 WINPR_ASSERTING_INT_CAST(UINT16, region->h));
430 xf_draw_screen(xfc, region->x, region->y, region->w, region->h);
435static BOOL xf_end_paint(rdpContext* context)
437 xfContext* xfc = (xfContext*)context;
440 rdpGdi* gdi = context->gdi;
443 if (gdi->suppressOutput)
446 HGDI_DC hdc = gdi->primary->hdc;
451 if (!xfc->complex_regions)
453 const GDI_RGN* rgn = hwnd->invalid;
457 if (!xf_paint(xfc, rgn))
463 const INT32 ninvalid = hwnd->ninvalid;
464 const GDI_RGN* cinvalid = hwnd->cinvalid;
466 if (hwnd->ninvalid < 1)
471 for (INT32 i = 0; i < ninvalid; i++)
473 const GDI_RGN* rgn = &cinvalid[i];
474 if (!xf_paint(xfc, rgn))
478 LogDynAndXFlush(xfc->log, xfc->display);
482 hwnd->invalid->null = TRUE;
487static BOOL xf_sw_desktop_resize(rdpContext* context)
489 WINPR_ASSERT(context);
491 rdpGdi* gdi = context->gdi;
494 xfContext* xfc = (xfContext*)context;
495 rdpSettings* settings = context->settings;
496 WINPR_ASSERT(settings);
503 const BOOL suppress = gdi->suppressOutput;
504 gdi->suppressOutput = TRUE;
513 xfc->image->data =
nullptr;
514 XDestroyImage(xfc->image);
517 WINPR_ASSERT(xfc->depth != 0);
518 if (!(xfc->image = LogDynAndXCreateImage(
519 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
520 ZPixmap, 0, (
char*)gdi->primary_buffer,
521 WINPR_ASSERTING_INT_CAST(uint32_t, gdi->width),
522 WINPR_ASSERTING_INT_CAST(uint32_t, gdi->height), xfc->scanline_pad,
523 WINPR_ASSERTING_INT_CAST(
int, gdi->stride))))
528 xfc->image->byte_order = LSBFirst;
529 xfc->image->bitmap_bit_order = LSBFirst;
530 ret = xf_desktop_resize(context);
533 gdi->suppressOutput = suppress;
537static BOOL xf_process_x_events(freerdp* instance)
540 int pending_status = 1;
541 xfContext* xfc = (xfContext*)instance->context;
543 while (pending_status)
546 pending_status = XPending(xfc->display);
550 XEvent xevent = WINPR_C_ARRAY_INIT;
552 XNextEvent(xfc->display, &xevent);
553 status = xf_event_process(instance, &xevent);
558 status = xf_event_update_screen(instance);
566static char* xf_window_get_title(rdpSettings* settings)
569 char* windowTitle =
nullptr;
571 const char* prefix =
"FreeRDP:";
580 return _strdup(title);
584 size = strnlen(name, MAX_PATH) + 16;
585 windowTitle = calloc(size,
sizeof(
char));
591 (void)sprintf_s(windowTitle, size,
"%s %s", prefix, name);
593 (
void)sprintf_s(windowTitle, size,
"%s %s:%" PRIu32, prefix, name,
599BOOL xf_create_window(xfContext* xfc)
601 XGCValues gcv = WINPR_C_ARRAY_INIT;
602 XEvent xevent = WINPR_C_ARRAY_INIT;
603 char* windowTitle =
nullptr;
606 rdpSettings* settings = xfc->common.context.settings;
607 WINPR_ASSERT(settings);
614 const XSetWindowAttributes empty = WINPR_C_ARRAY_INIT;
615 xfc->attribs = empty;
620 xfc->depth = DefaultDepthOfScreen(xfc->screen);
622 XVisualInfo vinfo = WINPR_C_ARRAY_INIT;
623 if (XMatchVisualInfo(xfc->display, xfc->screen_number, xfc->depth, TrueColor, &vinfo))
625 Window root = XDefaultRootWindow(xfc->display);
626 xfc->visual = vinfo.visual;
627 xfc->attribs.colormap = xfc->colormap =
628 XCreateColormap(xfc->display, root, vinfo.visual, AllocNone);
634 WLog_WARN(TAG,
"running in remote app mode, but XServer does not support transparency");
635 WLog_WARN(TAG,
"display of remote applications might be distorted (black frames, ...)");
637 xfc->depth = DefaultDepthOfScreen(xfc->screen);
638 xfc->visual = DefaultVisual(xfc->display, xfc->screen_number);
639 xfc->attribs.colormap = xfc->colormap = DefaultColormap(xfc->display, xfc->screen_number);
646 if (vinfo.red_mask & 0xFF)
651 if (!xfc->remote_app)
653 xfc->attribs.background_pixel = BlackPixelOfScreen(xfc->screen);
654 xfc->attribs.border_pixel = WhitePixelOfScreen(xfc->screen);
655 xfc->attribs.backing_store = xfc->primary ? NotUseful : Always;
656 xfc->attribs.override_redirect = False;
658 xfc->attribs.bit_gravity = NorthWestGravity;
659 xfc->attribs.win_gravity = NorthWestGravity;
660 xfc->attribs_mask = CWBackPixel | CWBackingStore | CWOverrideRedirect | CWColormap |
661 CWBorderPixel | CWWinGravity | CWBitGravity;
667 windowTitle = xf_window_get_title(settings);
677 width = WINPR_ASSERTING_INT_CAST(
681 height = WINPR_ASSERTING_INT_CAST(
684 xfc->scaledWidth = width;
685 xfc->scaledHeight = height;
689 xfc->window = xf_CreateDesktopWindow(xfc, windowTitle, width, height);
695 xf_SetWindowFullscreen(xfc, xfc->window, xfc->fullscreen);
697 xfc->unobscured = (xevent.xvisibility.state == VisibilityUnobscured);
698 XSetWMProtocols(xfc->display, xfc->window->handle, &(xfc->WM_DELETE_WINDOW), 1);
699 xfc->drawable = xfc->window->handle;
703 xfc->attribs.border_pixel = 0;
704 xfc->attribs.background_pixel = 0;
705 xfc->attribs.backing_store = xfc->primary ? NotUseful : Always;
706 xfc->attribs.override_redirect = False;
708 xfc->attribs.bit_gravity = NorthWestGravity;
709 xfc->attribs.win_gravity = NorthWestGravity;
710 xfc->attribs_mask = CWBackPixel | CWBackingStore | CWOverrideRedirect | CWColormap |
711 CWBorderPixel | CWWinGravity | CWBitGravity;
713 xfc->drawable = xf_CreateDummyWindow(xfc);
718 LogDynAndXCreateGC(xfc->log, xfc->display, xfc->drawable, GCGraphicsExposures, &gcv);
720 WINPR_ASSERT(xfc->depth != 0);
723 LogDynAndXCreatePixmap(xfc->log, xfc->display, xfc->drawable,
726 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth));
728 xfc->drawing = xfc->primary;
730 if (!xfc->bitmap_mono)
731 xfc->bitmap_mono = LogDynAndXCreatePixmap(xfc->log, xfc->display, xfc->drawable, 8, 8, 1);
735 LogDynAndXCreateGC(xfc->log, xfc->display, xfc->bitmap_mono, GCGraphicsExposures, &gcv);
737 LogDynAndXSetFunction(xfc->log, xfc->display, xfc->gc, GXcopy);
738 LogDynAndXSetFillStyle(xfc->log, xfc->display, xfc->gc, FillSolid);
739 LogDynAndXSetForeground(xfc->log, xfc->display, xfc->gc, BlackPixelOfScreen(xfc->screen));
740 LogDynAndXFillRectangle(xfc->log, xfc->display, xfc->primary, xfc->gc, 0, 0,
743 LogDynAndXFlush(xfc->log, xfc->display);
748BOOL xf_create_image(xfContext* xfc)
753 const rdpSettings* settings = xfc->common.context.settings;
754 rdpGdi* cgdi = xfc->common.context.gdi;
757 WINPR_ASSERT(xfc->depth != 0);
758 xfc->image = LogDynAndXCreateImage(
759 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
760 ZPixmap, 0, (
char*)cgdi->primary_buffer,
763 WINPR_ASSERTING_INT_CAST(
int, cgdi->stride));
764 xfc->image->byte_order = LSBFirst;
765 xfc->image->bitmap_bit_order = LSBFirst;
770void xf_destroy_window(xfContext* xfc)
774 xf_DestroyDesktopWindow(xfc, xfc->window);
775 xfc->window =
nullptr;
778#if defined(CHANNEL_TSMF_CLIENT)
781 xf_tsmf_uninit(xfc,
nullptr);
782 xfc->xv_context =
nullptr;
788 xfc->image->data =
nullptr;
789 XDestroyImage(xfc->image);
790 xfc->image =
nullptr;
793 if (xfc->bitmap_mono)
795 LogDynAndXFreePixmap(xfc->log, xfc->display, xfc->bitmap_mono);
796 xfc->bitmap_mono = 0;
801 LogDynAndXFreeGC(xfc->log, xfc->display, xfc->gc_mono);
802 xfc->gc_mono =
nullptr;
807 LogDynAndXFreePixmap(xfc->log, xfc->display, xfc->primary);
813 LogDynAndXFreeGC(xfc->log, xfc->display, xfc->gc);
818void xf_toggle_fullscreen(xfContext* xfc)
820 WindowStateChangeEventArgs e = WINPR_C_ARRAY_INIT;
821 rdpContext* context = (rdpContext*)xfc;
822 rdpSettings* settings = context->settings;
831 xfc->fullscreen = !((xfc->fullscreen));
834 xf_SetWindowFullscreen(xfc, xfc->window, xfc->fullscreen);
835 EventArgsInit(&e,
"xfreerdp");
836 e.state = xfc->fullscreen ? FREERDP_WINDOW_STATE_FULLSCREEN : 0;
837 if (PubSub_OnWindowStateChange(context->pubSub, context, &e) < 0)
839 WLog_Print(xfc->log, WLOG_ERROR,
"PubSub_OnWindowStateChange failed");
843void xf_minimize(xfContext* xfc)
845 WindowStateChangeEventArgs e = WINPR_C_ARRAY_INIT;
846 rdpContext* context = (rdpContext*)xfc;
847 WINPR_ASSERT(context);
856 xf_SetWindowMinimized(xfc, xfc->window);
858 e.state = xfc->fullscreen ? FREERDP_WINDOW_STATE_FULLSCREEN : 0;
859 if (PubSub_OnWindowStateChange(context->pubSub, context, &e) < 0)
861 WLog_Print(xfc->log, WLOG_ERROR,
"PubSub_OnWindowStateChange failed");
865void xf_lock_x11_(xfContext* xfc, WINPR_ATTR_UNUSED
const char* fkt)
867 if (!xfc->UseXThreads)
868 (void)WaitForSingleObject(xfc->mutex, INFINITE);
870 XLockDisplay(xfc->display);
875void xf_unlock_x11_(xfContext* xfc, WINPR_ATTR_UNUSED
const char* fkt)
877 if (xfc->locked == 0)
878 WLog_WARN(TAG,
"X11: trying to unlock although not locked!");
882 if (!xfc->UseXThreads)
883 (void)ReleaseMutex(xfc->mutex);
885 XUnlockDisplay(xfc->display);
888static BOOL xf_get_pixmap_info(xfContext* xfc)
891 XPixmapFormatValues* pfs =
nullptr;
893 WINPR_ASSERT(xfc->display);
894 pfs = XListPixmapFormats(xfc->display, &pf_count);
898 WLog_ERR(TAG,
"XListPixmapFormats failed");
902 WINPR_ASSERT(xfc->depth != 0);
903 for (
int i = 0; i < pf_count; i++)
905 const XPixmapFormatValues* pf = &pfs[i];
907 if (pf->depth == xfc->depth)
909 xfc->scanline_pad = pf->scanline_pad;
915 return !((xfc->visual ==
nullptr) || (xfc->scanline_pad == 0));
918static int xf_error_handler(Display* d, XErrorEvent* ev)
920 char buf[256] = WINPR_C_ARRAY_INIT;
921 XGetErrorText(d, ev->error_code, buf,
sizeof(buf));
922 const char* what = request_code_2_str(ev->request_code);
923 WLog_ERR(TAG,
"%s: %s", what, buf);
924 winpr_log_backtrace(TAG, WLOG_ERROR, 20);
928static int xf_error_handler_ex(Display* d, XErrorEvent* ev)
936 XUngrabKeyboard(d, CurrentTime);
937 XUngrabPointer(d, CurrentTime);
939 return xf_error_handler(d, ev);
942static BOOL xf_play_sound(rdpContext* context,
const PLAY_SOUND_UPDATE* play_sound)
944 xfContext* xfc = (xfContext*)context;
945 WINPR_UNUSED(play_sound);
946 XkbBell(xfc->display, None, 100, 0);
950static void xf_check_extensions(xfContext* context)
955 int xkb_major = XkbMajorVersion;
956 int xkb_minor = XkbMinorVersion;
958 if (XkbLibraryVersion(&xkb_major, &xkb_minor) &&
959 XkbQueryExtension(context->display, &xkb_opcode, &xkb_event, &xkb_error, &xkb_major,
962 context->xkbAvailable = TRUE;
967 int xrender_event_base = 0;
968 int xrender_error_base = 0;
970 if (XRenderQueryExtension(context->display, &xrender_event_base, &xrender_error_base))
972 context->xrenderAvailable = TRUE;
981static const char TEST_PTR_STR[] =
"Virtual core XTEST pointer";
982static const size_t TEST_PTR_LEN =
sizeof(TEST_PTR_STR) /
sizeof(
char);
985static void xf_get_x11_button_map(xfContext* xfc,
unsigned char* x11_map)
991 XDevice* ptr_dev =
nullptr;
992 XExtensionVersion* version =
nullptr;
993 XDeviceInfo* devices1 =
nullptr;
994 XIDeviceInfo* devices2 =
nullptr;
997 if (XQueryExtension(xfc->display,
"XInputExtension", &opcode, &event, &error))
999 WLog_DBG(TAG,
"Searching for XInput pointer device");
1002 version = XGetExtensionVersion(xfc->display, INAME);
1004 if (version->major_version >= 2)
1007 devices2 = XIQueryDevice(xfc->display, XIAllDevices, &num_devices);
1011 for (
int i = 0; i < num_devices; ++i)
1013 XIDeviceInfo* dev = &devices2[i];
1014 if ((dev->use == XISlavePointer) &&
1015 (strncmp(dev->name, TEST_PTR_STR, TEST_PTR_LEN) != 0))
1017 ptr_dev = XOpenDevice(xfc->display,
1018 WINPR_ASSERTING_INT_CAST(uint32_t, dev->deviceid));
1024 XIFreeDeviceInfo(devices2);
1030 devices1 = XListInputDevices(xfc->display, &num_devices);
1034 for (
int i = 0; i < num_devices; ++i)
1036 if ((devices1[i].use == IsXExtensionPointer) &&
1037 (strncmp(devices1[i].name, TEST_PTR_STR, TEST_PTR_LEN) != 0))
1039 ptr_dev = XOpenDevice(xfc->display, devices1[i].id);
1045 XFreeDeviceList(devices1);
1055 WLog_DBG(TAG,
"Pointer device: %" PRIu32,
1056 WINPR_CXX_COMPAT_CAST(uint32_t, ptr_dev->device_id));
1057 XGetDeviceButtonMapping(xfc->display, ptr_dev, x11_map, NUM_BUTTONS_MAPPED);
1058 XCloseDevice(xfc->display, ptr_dev);
1062 WLog_DBG(TAG,
"No pointer device found!");
1068 WLog_DBG(TAG,
"Get global pointer mapping (no XInput)");
1069 XGetPointerMapping(xfc->display, x11_map, NUM_BUTTONS_MAPPED);
1075static const button_map xf_button_flags[NUM_BUTTONS_MAPPED] = {
1076 { Button1, PTR_FLAGS_BUTTON1 },
1077 { Button2, PTR_FLAGS_BUTTON3 },
1078 { Button3, PTR_FLAGS_BUTTON2 },
1079 { Button4, PTR_FLAGS_WHEEL | 0x78 },
1081 { Button5, PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | (0x100 - 0x78) },
1082 { 6, PTR_FLAGS_HWHEEL | PTR_FLAGS_WHEEL_NEGATIVE | (0x100 - 0x78) },
1083 { 7, PTR_FLAGS_HWHEEL | 0x78 },
1084 { 8, PTR_XFLAGS_BUTTON1 },
1085 { 9, PTR_XFLAGS_BUTTON2 },
1086 { 97, PTR_XFLAGS_BUTTON1 },
1087 { 112, PTR_XFLAGS_BUTTON2 }
1090static UINT16 get_flags_for_button(
size_t button)
1092 for (
size_t x = 0; x < ARRAYSIZE(xf_button_flags); x++)
1096 if (map->button == button)
1103void xf_button_map_init(xfContext* xfc)
1110 unsigned char x11_map[112] = WINPR_C_ARRAY_INIT;
1113 WINPR_ASSERT(xfc->common.context.settings);
1115 x11_map[0] = Button1;
1116 x11_map[1] = Button2;
1117 x11_map[2] = Button3;
1118 x11_map[3] = Button4;
1119 x11_map[4] = Button5;
1130 xf_get_x11_button_map(xfc, x11_map);
1136 for (
size_t physical = 0; physical < ARRAYSIZE(x11_map); ++physical)
1138 const unsigned char logical = x11_map[physical];
1139 const UINT16 flags = get_flags_for_button(logical);
1141 if ((logical != 0) && (flags != 0))
1143 if (pos >= NUM_BUTTONS_MAPPED)
1145 WLog_ERR(TAG,
"Failed to map mouse button to RDP button, no space");
1150 map->button = logical;
1151 map->flags = get_flags_for_button(physical + Button1);
1168static BOOL xf_pre_connect(freerdp* instance)
1170 UINT32 maxWidth = 0;
1171 UINT32 maxHeight = 0;
1173 WINPR_ASSERT(instance);
1175 rdpContext* context = instance->context;
1176 WINPR_ASSERT(context);
1177 xfContext* xfc = (xfContext*)context;
1179 rdpSettings* settings = context->settings;
1180 WINPR_ASSERT(settings);
1187 if (!xf_setup_x11(xfc))
1195 if (PubSub_SubscribeChannelConnected(context->pubSub, xf_OnChannelConnectedEventHandler) < 0)
1197 if (PubSub_SubscribeChannelDisconnected(context->pubSub, xf_OnChannelDisconnectedEventHandler) <
1205 char login_name[MAX_PATH] = WINPR_C_ARRAY_INIT;
1206 ULONG size =
sizeof(login_name) - 1;
1208 if (GetUserNameExA(NameSamCompatible, login_name, &size))
1213 WLog_INFO(TAG,
"No user name set. - Using login name: %s",
1223 WLog_INFO(TAG,
"auth-only, but no password set. Please provide one.");
1227 WLog_INFO(TAG,
"Authentication only. Don't connect to X.");
1233 xfc->common.context.settings, FreeRDP_KeyboardRemappingList);
1235 xfc->remap_table = freerdp_keyboard_remap_string_to_list(KeyboardRemappingList);
1236 if (!xfc->remap_table)
1238 if (!xf_keyboard_init(xfc))
1240 if (!xf_keyboard_action_script_init(xfc))
1242 if (!xf_detect_monitors(xfc, &maxWidth, &maxHeight))
1266 settings, FreeRDP_DesktopWidth,
1270 settings, FreeRDP_DesktopHeight,
1281 xf_button_map_init(xfc);
1285static BOOL xf_inject_keypress(rdpContext* context,
const char* buffer,
size_t size)
1287 WCHAR wbuffer[64] = WINPR_C_ARRAY_INIT;
1288 const SSIZE_T len = ConvertUtf8NToWChar(buffer, size, wbuffer, ARRAYSIZE(wbuffer));
1292 rdpInput* input = context->input;
1293 WINPR_ASSERT(input);
1295 for (SSIZE_T x = 0; x < len; x++)
1297 const WCHAR code = wbuffer[x];
1298 freerdp_input_send_unicode_keyboard_event(input, 0, code);
1300 freerdp_input_send_unicode_keyboard_event(input, KBD_FLAGS_RELEASE, code);
1306static BOOL xf_process_pipe(rdpContext* context,
const char* pipe)
1308 int fd = open(pipe, O_NONBLOCK | O_RDONLY);
1311 char ebuffer[256] = WINPR_C_ARRAY_INIT;
1312 WLog_ERR(TAG,
"pipe '%s' open returned %s [%d]", pipe,
1313 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)), errno);
1316 while (!freerdp_shall_disconnect_context(context))
1318 char buffer[64] = WINPR_C_ARRAY_INIT;
1320 ssize_t rd = read(fd, buffer,
sizeof(buffer) - 1);
1323 char ebuffer[256] = WINPR_C_ARRAY_INIT;
1324 if ((errno == EAGAIN) || (errno == 0))
1331 WLog_ERR(TAG,
"pipe '%s' read returned %s [%d]", pipe,
1332 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)), errno);
1337 char ebuffer[256] = WINPR_C_ARRAY_INIT;
1338 WLog_ERR(TAG,
"pipe '%s' read returned %s [%d]", pipe,
1339 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)), errno);
1344 if (!xf_inject_keypress(context, buffer, WINPR_ASSERTING_INT_CAST(
size_t, rd)))
1352static void cleanup_pipe(WINPR_ATTR_UNUSED
int signum, WINPR_ATTR_UNUSED
const char* signame,
1355 const char* pipe = context;
1361static DWORD WINAPI xf_handle_pipe(
void* arg)
1363 xfContext* xfc = arg;
1366 rdpContext* context = &xfc->common.context;
1367 WINPR_ASSERT(context);
1369 rdpSettings* settings = context->settings;
1370 WINPR_ASSERT(settings);
1375 const int rc = mkfifo(pipe, S_IWUSR | S_IRUSR);
1378 char ebuffer[256] = WINPR_C_ARRAY_INIT;
1379 WLog_ERR(TAG,
"Failed to create named pipe '%s': %s [%d]", pipe,
1380 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)), errno);
1384 void* ctx = WINPR_CAST_CONST_PTR_AWAY(pipe,
void*);
1385 if (freerdp_add_signal_cleanup_handler(ctx, cleanup_pipe))
1388 xf_process_pipe(context, pipe);
1390 freerdp_del_signal_cleanup_handler(ctx, cleanup_pipe);
1402static BOOL xf_post_connect(freerdp* instance)
1404 ResizeWindowEventArgs e = WINPR_C_ARRAY_INIT;
1406 WINPR_ASSERT(instance);
1407 xfContext* xfc = (xfContext*)instance->context;
1408 rdpContext* context = instance->context;
1409 WINPR_ASSERT(context);
1411 rdpSettings* settings = context->settings;
1412 WINPR_ASSERT(settings);
1414 rdpUpdate* update = context->update;
1415 WINPR_ASSERT(update);
1418 xfc->remote_app = TRUE;
1420 if (!xf_create_window(xfc))
1423 if (!xf_get_pixmap_info(xfc))
1426 if (!gdi_init(instance, xf_get_local_color_format(xfc, TRUE)))
1429 if (!xf_create_image(xfc))
1432 if (!xf_register_pointer(context->graphics))
1444 if (!xfc->xrenderAvailable)
1448 WLog_ERR(TAG,
"XRender not available: disabling smart-sizing");
1455 WLog_ERR(TAG,
"XRender not available: disabling local multi-touch gestures");
1461 update->DesktopResize = xf_sw_desktop_resize;
1462 update->EndPaint = xf_end_paint;
1463 update->PlaySound = xf_play_sound;
1464 update->SetKeyboardIndicators = xf_keyboard_set_indicators;
1465 update->SetKeyboardImeStatus = xf_keyboard_set_ime_status;
1467 const BOOL serverIsWindowsPlatform =
1470 !(xfc->clipboard = xf_clipboard_new(xfc, !serverIsWindowsPlatform)))
1473 if (!(xfc->xfDisp = xf_disp_new(xfc)))
1479 xfc->pipethread = CreateThread(
nullptr, 0, xf_handle_pipe, xfc, 0,
nullptr);
1480 if (!xfc->pipethread)
1484 EventArgsInit(&e,
"xfreerdp");
1489 return PubSub_OnResizeWindow(context->pubSub, xfc, &e) >= 0;
1492static void xf_post_disconnect(freerdp* instance)
1494 xfContext* xfc =
nullptr;
1495 rdpContext* context =
nullptr;
1497 if (!instance || !instance->context)
1500 context = instance->context;
1501 xfc = (xfContext*)context;
1502 PubSub_UnsubscribeChannelConnected(instance->context->pubSub,
1503 xf_OnChannelConnectedEventHandler);
1504 PubSub_UnsubscribeChannelDisconnected(instance->context->pubSub,
1505 xf_OnChannelDisconnectedEventHandler);
1508 if (xfc->pipethread)
1510 (void)WaitForSingleObject(xfc->pipethread, INFINITE);
1511 (void)CloseHandle(xfc->pipethread);
1512 xfc->pipethread =
nullptr;
1516 xf_clipboard_free(xfc->clipboard);
1517 xfc->clipboard =
nullptr;
1522 xf_disp_free(xfc->xfDisp);
1523 xfc->xfDisp =
nullptr;
1526 if ((xfc->window !=
nullptr) && (xfc->drawable == xfc->window->handle))
1529 xf_DestroyDummyWindow(xfc, xfc->drawable);
1531 freerdp_keyboard_remap_free(xfc->remap_table);
1532 xfc->remap_table =
nullptr;
1534 xf_destroy_window(xfc);
1537static void xf_post_final_disconnect(freerdp* instance)
1539 xfContext* xfc =
nullptr;
1540 rdpContext* context =
nullptr;
1542 if (!instance || !instance->context)
1545 context = instance->context;
1546 xfc = (xfContext*)context;
1548 xf_keyboard_free(xfc);
1549 xf_teardown_x11(xfc);
1552static int xf_logon_error_info(freerdp* instance, UINT32 data, UINT32 type)
1554 xfContext* xfc = (xfContext*)instance->context;
1555 const char* str_data = freerdp_get_logon_error_info_data(data);
1556 const char* str_type = freerdp_get_logon_error_info_type(type);
1557 WLog_INFO(TAG,
"Logon Error Info %s [%s]", str_data, str_type);
1558 if (type != LOGON_MSG_SESSION_CONTINUE)
1560 if (!xf_rail_disable_remoteapp_mode(xfc))
1566static BOOL handle_window_events(freerdp* instance)
1568 if (!xf_process_x_events(instance))
1570 WLog_DBG(TAG,
"Closed from X11");
1571 freerdp_abort_connect_context(instance->context);
1586static DWORD WINAPI xf_client_thread(LPVOID param)
1588 DWORD exit_code = 0;
1589 DWORD waitStatus = 0;
1590 HANDLE inputEvent =
nullptr;
1592 freerdp* instance = (freerdp*)param;
1593 WINPR_ASSERT(instance);
1595 const BOOL status = freerdp_connect(instance);
1596 rdpContext* context = instance->context;
1597 WINPR_ASSERT(context);
1598 xfContext* xfc = (xfContext*)instance->context;
1601 rdpSettings* settings = context->settings;
1602 WINPR_ASSERT(settings);
1606 UINT32 error = freerdp_get_last_error(instance->context);
1607 exit_code = (uint32_t)xf_map_error_to_exit_code(error);
1610 exit_code = XF_EXIT_SUCCESS;
1618 WLog_ERR(TAG,
"Authentication only, exit status %" PRId32
"", !status);
1624 WLog_ERR(TAG,
"Freerdp connect error exit status %" PRId32
"", !status);
1625 exit_code = freerdp_error_info(instance);
1627 if (freerdp_get_last_error(instance->context) == FREERDP_ERROR_AUTHENTICATION_FAILED)
1628 exit_code = XF_EXIT_AUTH_FAILURE;
1629 else if (exit_code == ERRINFO_SUCCESS)
1630 exit_code = XF_EXIT_CONN_FAILED;
1635 inputEvent = xfc->x11event;
1637 while (!freerdp_shall_disconnect_context(instance->context))
1639 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
1641 handles[nCount++] = inputEvent;
1648 if (freerdp_focus_required(instance))
1650 xf_keyboard_focus_in(xfc);
1651 xf_keyboard_focus_in(xfc);
1656 freerdp_get_event_handles(context, &handles[nCount], ARRAYSIZE(handles) - nCount);
1660 WLog_ERR(TAG,
"freerdp_get_event_handles failed");
1668 xf_floatbar_hide_and_show(xfc->window->floatbar);
1670 waitStatus = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
1672 if (waitStatus == WAIT_FAILED)
1676 if (!freerdp_check_event_handles(context))
1678 const BOOL reconnected = client_auto_reconnect_ex(instance, handle_window_events);
1679 xf_reconnect_close(xfc);
1688 const UINT32 error = freerdp_get_last_error(instance->context);
1690 if (freerdp_error_info(instance) == 0)
1691 exit_code = (uint32_t)xf_map_error_to_exit_code(error);
1694 if (freerdp_get_last_error(context) == FREERDP_ERROR_SUCCESS)
1695 WLog_ERR(TAG,
"Failed to check FreeRDP file descriptor");
1701 if (!handle_window_events(instance))
1707 exit_code = freerdp_error_info(instance);
1709 if (exit_code == XF_EXIT_DISCONNECT &&
1710 freerdp_get_disconnect_ultimatum(context) == Disconnect_Ultimatum_user_requested)
1713 WLog_INFO(TAG,
"Error info says user did not initiate but disconnect ultimatum says "
1714 "they did; treat this as a user logoff");
1715 exit_code = XF_EXIT_LOGOFF;
1721 freerdp_disconnect(instance);
1723 ExitThread(exit_code);
1727int xf_exit_code_from_disconnect_reason(DWORD reason)
1729 if ((reason == 0) || ((reason >= XF_EXIT_PARSE_ARGUMENTS) && (reason <= XF_EXIT_CODE_LAST)))
1730 return WINPR_ASSERTING_INT_CAST(
int, reason);
1732 else if (reason >= 0x100 && reason <= 0x10A)
1733 reason -= 0x100 + XF_EXIT_LICENSE_INTERNAL;
1735 else if (reason >= 0x10c9 && reason <= 0x1193)
1736 reason = XF_EXIT_RDP;
1738 else if (!(reason <= 0xC))
1739 reason = XF_EXIT_UNKNOWN;
1741 return WINPR_ASSERTING_INT_CAST(
int, reason);
1744static void xf_TerminateEventHandler(
void* context,
const TerminateEventArgs* e)
1746 rdpContext* ctx = (rdpContext*)context;
1748 freerdp_abort_connect_context(ctx);
1752static void xf_ZoomingChangeEventHandler(
void* context,
const ZoomingChangeEventArgs* e)
1756 rdpSettings* settings =
nullptr;
1757 xfContext* xfc = (xfContext*)context;
1761 settings = xfc->common.context.settings;
1762 WINPR_ASSERT(settings);
1764 w = xfc->scaledWidth + e->dx;
1765 h = xfc->scaledHeight + e->dy;
1767 if (e->dx == 0 && e->dy == 0)
1776 if (w == xfc->scaledWidth && h == xfc->scaledHeight)
1779 xfc->scaledWidth = w;
1780 xfc->scaledHeight = h;
1781 xf_draw_screen(xfc, 0, 0,
1782 WINPR_ASSERTING_INT_CAST(
1784 WINPR_ASSERTING_INT_CAST(
1788static void xf_PanningChangeEventHandler(
void* context,
const PanningChangeEventArgs* e)
1790 xfContext* xfc = (xfContext*)context;
1791 rdpSettings* settings =
nullptr;
1796 settings = xfc->common.context.settings;
1797 WINPR_ASSERT(settings);
1799 if (e->dx == 0 && e->dy == 0)
1802 xfc->offset_x += e->dx;
1803 xfc->offset_y += e->dy;
1804 xf_draw_screen(xfc, 0, 0,
1805 WINPR_ASSERTING_INT_CAST(
1807 WINPR_ASSERTING_INT_CAST(
1816static BOOL xfreerdp_client_global_init(
void)
1819 (void)setlocale(LC_ALL,
"");
1821 return (freerdp_handle_signals() == 0);
1824static void xfreerdp_client_global_uninit(
void)
1828static int xfreerdp_client_start(rdpContext* context)
1830 xfContext* xfc = (xfContext*)context;
1831 rdpSettings* settings = context->settings;
1835 WLog_ERR(TAG,
"error: server hostname was not specified with /v:<server>[:port]");
1839 if (!(xfc->common.thread =
1840 CreateThread(
nullptr, 0, xf_client_thread, context->instance, 0,
nullptr)))
1842 WLog_ERR(TAG,
"failed to create client thread");
1849static Atom get_supported_atom(xfContext* xfc,
const char* atomName)
1851 const Atom atom = Logging_XInternAtom(xfc->log, xfc->display, atomName, False);
1853 for (
unsigned long i = 0; i < xfc->supportedAtomCount; i++)
1855 if (xfc->supportedAtoms[i] == atom)
1862void xf_teardown_x11(xfContext* xfc)
1865 xf_reconnect_close(xfc);
1869 LogDynAndXCloseDisplay(xfc->log, xfc->display);
1870 xfc->display =
nullptr;
1875 (void)CloseHandle(xfc->x11event);
1876 xfc->x11event =
nullptr;
1881 (void)CloseHandle(xfc->mutex);
1882 xfc->mutex =
nullptr;
1885 if (xfc->vscreen.monitors)
1887 free(xfc->vscreen.monitors);
1888 xfc->vscreen.monitors =
nullptr;
1890 xfc->vscreen.nmonitors = 0;
1892 free(xfc->supportedAtoms);
1893 xfc->supportedAtoms =
nullptr;
1894 xfc->supportedAtomCount = 0;
1897BOOL xf_setup_x11(xfContext* xfc)
1901 xfc->UseXThreads = TRUE;
1908 if (xfc->UseXThreads)
1910 if (!XInitThreads())
1912 WLog_WARN(TAG,
"XInitThreads() failure");
1913 xfc->UseXThreads = FALSE;
1917 xfc->display = XOpenDisplay(
nullptr);
1921 WLog_ERR(TAG,
"failed to open display: %s", XDisplayName(
nullptr));
1922 WLog_ERR(TAG,
"Please check that the $DISPLAY environment variable is properly set.");
1927 WLog_INFO(TAG,
"Enabling X11 debug mode.");
1928 XSynchronize(xfc->display, TRUE);
1930 def_error_handler = XSetErrorHandler(xf_error_handler_ex);
1932 xfc->mutex = CreateMutex(
nullptr, FALSE,
nullptr);
1936 WLog_ERR(TAG,
"Could not create mutex!");
1940 xfc->xfds = ConnectionNumber(xfc->display);
1941 xfc->screen_number = DefaultScreen(xfc->display);
1942 xfc->screen = ScreenOfDisplay(xfc->display, xfc->screen_number);
1943 xfc->big_endian = (ImageByteOrder(xfc->display) == MSBFirst);
1945 xfc->complex_regions = TRUE;
1946 xfc->NET_SUPPORTED = Logging_XInternAtom(xfc->log, xfc->display,
"_NET_SUPPORTED", True);
1947 xfc->NET_SUPPORTING_WM_CHECK =
1948 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_SUPPORTING_WM_CHECK", True);
1950 if ((xfc->NET_SUPPORTED != None) && (xfc->NET_SUPPORTING_WM_CHECK != None))
1952 Atom actual_type = 0;
1953 int actual_format = 0;
1954 unsigned long nitems = 0;
1955 unsigned long after = 0;
1956 unsigned char* data =
nullptr;
1957 int status = LogDynAndXGetWindowProperty(
1958 xfc->log, xfc->display, RootWindowOfScreen(xfc->screen), xfc->NET_SUPPORTED, 0, 1024,
1959 False, XA_ATOM, &actual_type, &actual_format, &nitems, &after, &data);
1961 if ((status == Success) && (actual_type == XA_ATOM) && (actual_format == 32))
1963 xfc->supportedAtomCount = nitems;
1964 xfc->supportedAtoms = calloc(xfc->supportedAtomCount,
sizeof(Atom));
1965 WINPR_ASSERT(xfc->supportedAtoms);
1966 memcpy(xfc->supportedAtoms, data, nitems *
sizeof(Atom));
1973 xfc->XWAYLAND_MAY_GRAB_KEYBOARD =
1974 Logging_XInternAtom(xfc->log, xfc->display,
"_XWAYLAND_MAY_GRAB_KEYBOARD", False);
1975 xfc->NET_WM_ICON = Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ICON", False);
1976 xfc->MOTIF_WM_HINTS = Logging_XInternAtom(xfc->log, xfc->display,
"_MOTIF_WM_HINTS", False);
1977 xfc->NET_NUMBER_OF_DESKTOPS =
1978 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_NUMBER_OF_DESKTOPS", False);
1979 xfc->NET_CURRENT_DESKTOP =
1980 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_CURRENT_DESKTOP", False);
1981 xfc->NET_WORKAREA = Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WORKAREA", False);
1982 xfc->NET_WM_STATE = get_supported_atom(xfc,
"_NET_WM_STATE");
1983 xfc->NET_WM_STATE_MODAL = get_supported_atom(xfc,
"_NET_WM_STATE_MODAL");
1984 xfc->NET_WM_STATE_STICKY = get_supported_atom(xfc,
"_NET_WM_STATE_STICKY");
1985 xfc->NET_WM_STATE_MAXIMIZED_HORZ =
1986 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_STATE_MAXIMIZED_HORZ", False);
1987 xfc->NET_WM_STATE_MAXIMIZED_VERT =
1988 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_STATE_MAXIMIZED_VERT", False);
1989 xfc->NET_WM_STATE_SHADED = get_supported_atom(xfc,
"_NET_WM_STATE_SHADED");
1990 xfc->NET_WM_STATE_SKIP_TASKBAR = get_supported_atom(xfc,
"_NET_WM_STATE_SKIP_TASKBAR");
1991 xfc->NET_WM_STATE_SKIP_PAGER = get_supported_atom(xfc,
"_NET_WM_STATE_SKIP_PAGER");
1992 xfc->NET_WM_STATE_HIDDEN = get_supported_atom(xfc,
"_NET_WM_STATE_HIDDEN");
1993 xfc->NET_WM_STATE_FULLSCREEN = get_supported_atom(xfc,
"_NET_WM_STATE_FULLSCREEN");
1994 xfc->NET_WM_STATE_ABOVE = get_supported_atom(xfc,
"_NET_WM_STATE_ABOVE");
1995 xfc->NET_WM_STATE_BELOW = get_supported_atom(xfc,
"_NET_WM_STATE_BELOW");
1996 xfc->NET_WM_STATE_DEMANDS_ATTENTION =
1997 get_supported_atom(xfc,
"_NET_WM_STATE_DEMANDS_ATTENTION");
1998 xfc->NET_WM_FULLSCREEN_MONITORS = get_supported_atom(xfc,
"_NET_WM_FULLSCREEN_MONITORS");
1999 xfc->NET_WM_NAME = Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_NAME", False);
2000 xfc->NET_WM_PID = Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_PID", False);
2001 xfc->NET_WM_WINDOW_TYPE =
2002 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE", False);
2003 xfc->NET_WM_WINDOW_TYPE_NORMAL =
2004 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE_NORMAL", False);
2005 xfc->NET_WM_WINDOW_TYPE_DIALOG =
2006 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE_DIALOG", False);
2007 xfc->NET_WM_WINDOW_TYPE_POPUP =
2008 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE_POPUP", False);
2009 xfc->NET_WM_WINDOW_TYPE_POPUP_MENU =
2010 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE_POPUP_MENU", False);
2011 xfc->NET_WM_WINDOW_TYPE_UTILITY =
2012 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE_UTILITY", False);
2013 xfc->NET_WM_WINDOW_TYPE_DROPDOWN_MENU =
2014 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", False);
2015 xfc->NET_WM_STATE_SKIP_TASKBAR =
2016 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_STATE_SKIP_TASKBAR", False);
2017 xfc->NET_WM_STATE_SKIP_PAGER =
2018 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_STATE_SKIP_PAGER", False);
2019 xfc->NET_WM_MOVERESIZE =
2020 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_MOVERESIZE", False);
2021 xfc->NET_MOVERESIZE_WINDOW =
2022 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_MOVERESIZE_WINDOW", False);
2023 xfc->UTF8_STRING = Logging_XInternAtom(xfc->log, xfc->display,
"UTF8_STRING", FALSE);
2024 xfc->WM_PROTOCOLS = Logging_XInternAtom(xfc->log, xfc->display,
"WM_PROTOCOLS", False);
2025 xfc->WM_DELETE_WINDOW = Logging_XInternAtom(xfc->log, xfc->display,
"WM_DELETE_WINDOW", False);
2026 xfc->WM_STATE = Logging_XInternAtom(xfc->log, xfc->display,
"WM_STATE", False);
2027 xfc->x11event = CreateFileDescriptorEvent(
nullptr, FALSE, FALSE, xfc->xfds, WINPR_FD_READ);
2029 xfc->NET_WM_ALLOWED_ACTIONS =
2030 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ALLOWED_ACTIONS", False);
2032 xfc->NET_WM_ACTION_CLOSE =
2033 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_CLOSE", False);
2034 xfc->NET_WM_ACTION_MINIMIZE =
2035 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_MINIMIZE", False);
2036 xfc->NET_WM_ACTION_MOVE =
2037 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_MOVE", False);
2038 xfc->NET_WM_ACTION_RESIZE =
2039 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_RESIZE", False);
2040 xfc->NET_WM_ACTION_MAXIMIZE_HORZ =
2041 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_MAXIMIZE_HORZ", False);
2042 xfc->NET_WM_ACTION_MAXIMIZE_VERT =
2043 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_MAXIMIZE_VERT", False);
2044 xfc->NET_WM_ACTION_FULLSCREEN =
2045 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_FULLSCREEN", False);
2046 xfc->NET_WM_ACTION_CHANGE_DESKTOP =
2047 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_CHANGE_DESKTOP", False);
2051 WLog_ERR(TAG,
"Could not create xfds event");
2055 xf_check_extensions(xfc);
2057 xfc->vscreen.monitors = calloc(16,
sizeof(MONITOR_INFO));
2059 if (!xfc->vscreen.monitors)
2064 xf_teardown_x11(xfc);
2068static BOOL xfreerdp_client_new(freerdp* instance, rdpContext* context)
2070 xfContext* xfc = (xfContext*)instance->context;
2071 WINPR_ASSERT(context);
2073 WINPR_ASSERT(!xfc->display);
2074 WINPR_ASSERT(!xfc->mutex);
2075 WINPR_ASSERT(!xfc->x11event);
2076 instance->PreConnect = xf_pre_connect;
2077 instance->PostConnect = xf_post_connect;
2078 instance->RetryDialog = xf_retry_dialog;
2079 instance->PostDisconnect = xf_post_disconnect;
2080 instance->PostFinalDisconnect = xf_post_final_disconnect;
2081 instance->LogonErrorInfo = xf_logon_error_info;
2082 instance->GetAccessToken = client_failsafe_get_access_token;
2084 if (PubSub_SubscribeTerminate(context->pubSub, xf_TerminateEventHandler) < 0)
2087 if (PubSub_SubscribeZoomingChange(context->pubSub, xf_ZoomingChangeEventHandler) < 0)
2089 if (PubSub_SubscribePanningChange(context->pubSub, xf_PanningChangeEventHandler) < 0)
2092 xfc->log = WLog_Get(TAG);
2097static void xfreerdp_client_free(WINPR_ATTR_UNUSED freerdp* instance, rdpContext* context)
2102 if (context->pubSub)
2104 PubSub_UnsubscribeTerminate(context->pubSub, xf_TerminateEventHandler);
2106 PubSub_UnsubscribeZoomingChange(context->pubSub, xf_ZoomingChangeEventHandler);
2107 PubSub_UnsubscribePanningChange(context->pubSub, xf_PanningChangeEventHandler);
2112int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
2114 pEntryPoints->Version = 1;
2115 pEntryPoints->Size =
sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
2116 pEntryPoints->GlobalInit = xfreerdp_client_global_init;
2117 pEntryPoints->GlobalUninit = xfreerdp_client_global_uninit;
2118 pEntryPoints->ContextSize =
sizeof(xfContext);
2119 pEntryPoints->ClientNew = xfreerdp_client_new;
2120 pEntryPoints->ClientFree = xfreerdp_client_free;
2121 pEntryPoints->ClientStart = xfreerdp_client_start;
2122 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.