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>
85#include <freerdp/client/aad_helper.h>
88#include <winpr/synch.h>
89#include <winpr/file.h>
90#include <winpr/print.h>
91#include <winpr/sysinfo.h>
94#if defined(CHANNEL_TSMF_CLIENT)
99#include "xf_cliprdr.h"
102#include "xf_monitor.h"
103#include "xf_graphics.h"
104#include "xf_keyboard.h"
105#include "xf_channels.h"
106#include "xf_client.h"
110#include <freerdp/log.h>
111#define TAG CLIENT_TAG("x11")
113#define MIN_PIXEL_DIFF 0.001
115struct xf_exit_code_map_t
120static const struct xf_exit_code_map_t xf_exit_code_map[] = {
121 { FREERDP_ERROR_SUCCESS, XF_EXIT_SUCCESS },
122 { FREERDP_ERROR_AUTHENTICATION_FAILED, XF_EXIT_AUTH_FAILURE },
123 { FREERDP_ERROR_SECURITY_NEGO_CONNECT_FAILED, XF_EXIT_NEGO_FAILURE },
124 { FREERDP_ERROR_CONNECT_LOGON_FAILURE, XF_EXIT_LOGON_FAILURE },
125 { FREERDP_ERROR_CONNECT_ACCOUNT_LOCKED_OUT, XF_EXIT_ACCOUNT_LOCKED_OUT },
126 { FREERDP_ERROR_PRE_CONNECT_FAILED, XF_EXIT_PRE_CONNECT_FAILED },
127 { FREERDP_ERROR_CONNECT_UNDEFINED, XF_EXIT_CONNECT_UNDEFINED },
128 { FREERDP_ERROR_POST_CONNECT_FAILED, XF_EXIT_POST_CONNECT_FAILED },
129 { FREERDP_ERROR_DNS_ERROR, XF_EXIT_DNS_ERROR },
130 { FREERDP_ERROR_DNS_NAME_NOT_FOUND, XF_EXIT_DNS_NAME_NOT_FOUND },
131 { FREERDP_ERROR_CONNECT_FAILED, XF_EXIT_CONNECT_FAILED },
132 { FREERDP_ERROR_MCS_CONNECT_INITIAL_ERROR, XF_EXIT_MCS_CONNECT_INITIAL_ERROR },
133 { FREERDP_ERROR_TLS_CONNECT_FAILED, XF_EXIT_TLS_CONNECT_FAILED },
134 { FREERDP_ERROR_INSUFFICIENT_PRIVILEGES, XF_EXIT_INSUFFICIENT_PRIVILEGES },
135 { FREERDP_ERROR_CONNECT_CANCELLED, XF_EXIT_CONNECT_CANCELLED },
136 { FREERDP_ERROR_CONNECT_TRANSPORT_FAILED, XF_EXIT_CONNECT_TRANSPORT_FAILED },
137 { FREERDP_ERROR_CONNECT_PASSWORD_EXPIRED, XF_EXIT_CONNECT_PASSWORD_EXPIRED },
138 { FREERDP_ERROR_CONNECT_PASSWORD_MUST_CHANGE, XF_EXIT_CONNECT_PASSWORD_MUST_CHANGE },
139 { FREERDP_ERROR_CONNECT_KDC_UNREACHABLE, XF_EXIT_CONNECT_KDC_UNREACHABLE },
140 { FREERDP_ERROR_CONNECT_ACCOUNT_DISABLED, XF_EXIT_CONNECT_ACCOUNT_DISABLED },
141 { FREERDP_ERROR_CONNECT_PASSWORD_CERTAINLY_EXPIRED,
142 XF_EXIT_CONNECT_PASSWORD_CERTAINLY_EXPIRED },
143 { FREERDP_ERROR_CONNECT_CLIENT_REVOKED, XF_EXIT_CONNECT_CLIENT_REVOKED },
144 { FREERDP_ERROR_CONNECT_WRONG_PASSWORD, XF_EXIT_CONNECT_WRONG_PASSWORD },
145 { FREERDP_ERROR_CONNECT_ACCESS_DENIED, XF_EXIT_CONNECT_ACCESS_DENIED },
146 { FREERDP_ERROR_CONNECT_ACCOUNT_RESTRICTION, XF_EXIT_CONNECT_ACCOUNT_RESTRICTION },
147 { FREERDP_ERROR_CONNECT_ACCOUNT_EXPIRED, XF_EXIT_CONNECT_ACCOUNT_EXPIRED },
148 { FREERDP_ERROR_CONNECT_LOGON_TYPE_NOT_GRANTED, XF_EXIT_CONNECT_LOGON_TYPE_NOT_GRANTED },
149 { FREERDP_ERROR_CONNECT_NO_OR_MISSING_CREDENTIALS, XF_EXIT_CONNECT_NO_OR_MISSING_CREDENTIALS },
150 { FREERDP_ERROR_CONNECT_TARGET_BOOTING, XF_EXIT_CONNECT_TARGET_BOOTING },
151 { FREERDP_ERROR_CONNECT_HYBRID_REQUIRED_BY_SERVER, XF_EXIT_CONNECT_HYBRID_REQUIRED_BY_SERVER }
154static BOOL xf_setup_x11(xfContext* xfc);
155static void xf_teardown_x11(xfContext* xfc);
157static int xf_map_error_to_exit_code(DWORD error)
159 for (
size_t x = 0; x < ARRAYSIZE(xf_exit_code_map); x++)
161 const struct xf_exit_code_map_t* cur = &xf_exit_code_map[x];
162 if (cur->error == error)
166 return XF_EXIT_CONN_FAILED;
169static int (*def_error_handler)(Display*, XErrorEvent*);
170static int xf_error_handler_ex(Display* d, XErrorEvent* ev);
171static void xf_check_extensions(xfContext* context);
172static BOOL xf_get_pixmap_info(xfContext* xfc);
175static void xf_draw_screen_scaled(xfContext* xfc,
int x,
int y,
int w,
int h)
177 XTransform transform = WINPR_C_ARRAY_INIT;
178 Picture windowPicture = 0;
179 Picture primaryPicture = 0;
180 XRenderPictureAttributes pa;
181 XRenderPictFormat* picFormat =
nullptr;
184 const char* filter =
nullptr;
187 rdpSettings* settings = xfc->common.context.settings;
188 WINPR_ASSERT(settings);
190 if (xfc->scaledWidth <= 0 || xfc->scaledHeight <= 0)
192 WLog_ERR(TAG,
"the current window dimensions are invalid");
199 WLog_ERR(TAG,
"the window dimensions are invalid");
203 const double xScalingFactor = 1.0 *
205 (double)xfc->scaledWidth;
206 const double yScalingFactor = 1.0 *
208 (double)xfc->scaledHeight;
209 LogDynAndXSetFillStyle(xfc->log, xfc->display, xfc->gc, FillSolid);
210 LogDynAndXSetForeground(xfc->log, xfc->display, xfc->gc, 0);
213 XRectangle box1 = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, xfc->window->width),
214 WINPR_ASSERTING_INT_CAST(UINT16, xfc->window->height) };
215 XRectangle box2 = { WINPR_ASSERTING_INT_CAST(INT16, xfc->offset_x),
216 WINPR_ASSERTING_INT_CAST(INT16, xfc->offset_y),
217 WINPR_ASSERTING_INT_CAST(UINT16, xfc->scaledWidth),
218 WINPR_ASSERTING_INT_CAST(UINT16, xfc->scaledHeight) };
219 Region reg1 = XCreateRegion();
220 Region reg2 = XCreateRegion();
221 XUnionRectWithRegion(&box1, reg1, reg1);
222 XUnionRectWithRegion(&box2, reg2, reg2);
224 if (XSubtractRegion(reg1, reg2, reg1) && !XEmptyRegion(reg1))
226 LogDynAndXSetRegion(xfc->log, xfc->display, xfc->gc, reg1);
227 LogDynAndXFillRectangle(xfc->log, xfc->display, xfc->window->handle, xfc->gc, 0, 0,
228 WINPR_ASSERTING_INT_CAST(UINT16, xfc->window->width),
229 WINPR_ASSERTING_INT_CAST(UINT16, xfc->window->height));
230 LogDynAndXSetClipMask(xfc->log, xfc->display, xfc->gc, None);
233 XDestroyRegion(reg1);
234 XDestroyRegion(reg2);
236 picFormat = XRenderFindVisualFormat(xfc->display, xfc->visual);
237 pa.subwindow_mode = IncludeInferiors;
239 XRenderCreatePicture(xfc->display, xfc->primary, picFormat, CPSubwindowMode, &pa);
241 XRenderCreatePicture(xfc->display, xfc->window->handle, picFormat, CPSubwindowMode, &pa);
244 filter = FilterBilinear;
245 if (fabs(xScalingFactor - yScalingFactor) < MIN_PIXEL_DIFF)
247 const double inverseX = 1.0 / xScalingFactor;
248 const double inverseRoundedX = round(inverseX);
249 const double absInverse = fabs(inverseX - inverseRoundedX);
251 if (absInverse < MIN_PIXEL_DIFF)
252 filter = FilterNearest;
254 XRenderSetPictureFilter(xfc->display, primaryPicture, filter,
nullptr, 0);
255 transform.matrix[0][0] = XDoubleToFixed(xScalingFactor);
256 transform.matrix[0][1] = XDoubleToFixed(0.0);
257 transform.matrix[0][2] = XDoubleToFixed(0.0);
258 transform.matrix[1][0] = XDoubleToFixed(0.0);
259 transform.matrix[1][1] = XDoubleToFixed(yScalingFactor);
260 transform.matrix[1][2] = XDoubleToFixed(0.0);
261 transform.matrix[2][0] = XDoubleToFixed(0.0);
262 transform.matrix[2][1] = XDoubleToFixed(0.0);
263 transform.matrix[2][2] = XDoubleToFixed(1.0);
268 const double dx1 = floor(x / xScalingFactor);
269 const double dy1 = floor(y / yScalingFactor);
270 const double dx2 = ceil(x2 / xScalingFactor);
271 const double dy2 = ceil(y2 / yScalingFactor);
274 w = ((int)dx2) + 1 - x;
275 h = ((int)dy2) + 1 - y;
276 XRenderSetPictureTransform(xfc->display, primaryPicture, &transform);
277 XRenderComposite(xfc->display, PictOpSrc, primaryPicture, 0, windowPicture, x, y, 0, 0,
278 xfc->offset_x + x, xfc->offset_y + y, WINPR_ASSERTING_INT_CAST(uint32_t, w),
279 WINPR_ASSERTING_INT_CAST(uint32_t, h));
280 XRenderFreePicture(xfc->display, primaryPicture);
281 XRenderFreePicture(xfc->display, windowPicture);
284BOOL xf_picture_transform_required(xfContext* xfc)
286 rdpSettings* settings =
nullptr;
290 settings = xfc->common.context.settings;
291 WINPR_ASSERT(settings);
294 (xfc->offset_x != 0) || (xfc->offset_y != 0) ||
300void xf_draw_screen_(xfContext* xfc,
int x,
int y,
int w,
int h,
const char* fkt,
301 WINPR_ATTR_UNUSED
const char* file, WINPR_ATTR_UNUSED
int line)
305 WLog_DBG(TAG,
"called from [%s] xfc=nullptr", fkt);
309 if (w == 0 || h == 0)
311 WLog_WARN(TAG,
"invalid width and/or height specified: w=%d h=%d", w, h);
317 WLog_WARN(TAG,
"invalid xfc->window=nullptr");
323 if (xf_picture_transform_required(xfc))
325 xf_draw_screen_scaled(xfc, x, y, w, h);
330 LogDynAndXCopyArea(xfc->log, xfc->display, xfc->primary, xfc->window->handle, xfc->gc, x, y,
331 WINPR_ASSERTING_INT_CAST(uint32_t, w), WINPR_ASSERTING_INT_CAST(uint32_t, h),
335static BOOL xf_desktop_resize(rdpContext* context)
337 xfContext* xfc = (xfContext*)context;
341 rdpSettings* settings = context->settings;
342 WINPR_ASSERT(settings);
346 BOOL same = (xfc->primary == xfc->drawing);
347 LogDynAndXFreePixmap(xfc->log, xfc->display, xfc->primary);
349 WINPR_ASSERT(xfc->depth != 0);
350 if (!(xfc->primary = LogDynAndXCreatePixmap(
351 xfc->log, xfc->display, xfc->drawable,
354 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth))))
358 xfc->drawing = xfc->primary;
365 xfc->scaledWidth = WINPR_ASSERTING_INT_CAST(
367 xfc->scaledHeight = WINPR_ASSERTING_INT_CAST(
373 if (!xfc->fullscreen)
375 xf_ResizeDesktopWindow(
377 WINPR_ASSERTING_INT_CAST(
int,
379 WINPR_ASSERTING_INT_CAST(
int,
391 xfc->savedWidth = WINPR_ASSERTING_INT_CAST(
393 xfc->savedHeight = WINPR_ASSERTING_INT_CAST(
397 LogDynAndXSetFunction(xfc->log, xfc->display, xfc->gc, GXcopy);
398 LogDynAndXSetFillStyle(xfc->log, xfc->display, xfc->gc, FillSolid);
399 LogDynAndXSetForeground(xfc->log, xfc->display, xfc->gc, 0);
400 LogDynAndXFillRectangle(xfc->log, xfc->display, xfc->drawable, xfc->gc, 0, 0,
408static BOOL xf_paint(xfContext* xfc,
const GDI_RGN* region)
411 WINPR_ASSERT(region);
415 const RECTANGLE_16 rect = { .left = WINPR_ASSERTING_INT_CAST(UINT16, region->x),
416 .top = WINPR_ASSERTING_INT_CAST(UINT16, region->y),
418 WINPR_ASSERTING_INT_CAST(UINT16, region->x + region->w),
420 WINPR_ASSERTING_INT_CAST(UINT16, region->y + region->h) };
421 return xf_rail_paint(xfc, &rect);
425 LogDynAndXPutImage(xfc->log, xfc->display, xfc->primary, xfc->gc, xfc->image, region->x,
426 region->y, region->x, region->y,
427 WINPR_ASSERTING_INT_CAST(UINT16, region->w),
428 WINPR_ASSERTING_INT_CAST(UINT16, region->h));
429 xf_draw_screen(xfc, region->x, region->y, region->w, region->h);
434static BOOL xf_end_paint(rdpContext* context)
436 xfContext* xfc = (xfContext*)context;
439 rdpGdi* gdi = context->gdi;
442 if (gdi->suppressOutput)
445 HGDI_DC hdc = gdi->primary->hdc;
450 if (!xfc->complex_regions)
452 const GDI_RGN* rgn = hwnd->invalid;
456 if (!xf_paint(xfc, rgn))
462 const INT32 ninvalid = hwnd->ninvalid;
463 const GDI_RGN* cinvalid = hwnd->cinvalid;
465 if (hwnd->ninvalid < 1)
470 for (INT32 i = 0; i < ninvalid; i++)
472 const GDI_RGN* rgn = &cinvalid[i];
473 if (!xf_paint(xfc, rgn))
477 LogDynAndXFlush(xfc->log, xfc->display);
481 hwnd->invalid->null = TRUE;
486static BOOL xf_sw_desktop_resize(rdpContext* context)
488 WINPR_ASSERT(context);
490 rdpGdi* gdi = context->gdi;
493 xfContext* xfc = (xfContext*)context;
494 rdpSettings* settings = context->settings;
495 WINPR_ASSERT(settings);
502 const BOOL suppress = gdi->suppressOutput;
503 gdi->suppressOutput = TRUE;
512 xfc->image->data =
nullptr;
513 XDestroyImage(xfc->image);
516 WINPR_ASSERT(xfc->depth != 0);
517 if (!(xfc->image = LogDynAndXCreateImage(
518 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
519 ZPixmap, 0, (
char*)gdi->primary_buffer,
520 WINPR_ASSERTING_INT_CAST(uint32_t, gdi->width),
521 WINPR_ASSERTING_INT_CAST(uint32_t, gdi->height), xfc->scanline_pad,
522 WINPR_ASSERTING_INT_CAST(
int, gdi->stride))))
527 xfc->image->byte_order = LSBFirst;
528 xfc->image->bitmap_bit_order = LSBFirst;
529 ret = xf_desktop_resize(context);
532 gdi->suppressOutput = suppress;
536static BOOL xf_process_x_events(freerdp* instance)
539 int pending_status = 1;
540 xfContext* xfc = (xfContext*)instance->context;
542 while (pending_status)
545 pending_status = XPending(xfc->display);
549 XEvent xevent = WINPR_C_ARRAY_INIT;
551 XNextEvent(xfc->display, &xevent);
552 status = xf_event_process(instance, &xevent);
557 status = xf_event_update_screen(instance);
565static char* xf_window_get_title(rdpSettings* settings)
568 char* windowTitle =
nullptr;
570 const char* prefix =
"FreeRDP:";
579 return _strdup(title);
583 size = strnlen(name, MAX_PATH) + 16;
584 windowTitle = calloc(size,
sizeof(
char));
590 (void)sprintf_s(windowTitle, size,
"%s %s", prefix, name);
592 (
void)sprintf_s(windowTitle, size,
"%s %s:%" PRIu32, prefix, name,
598BOOL xf_create_window(xfContext* xfc)
600 XGCValues gcv = WINPR_C_ARRAY_INIT;
601 XEvent xevent = WINPR_C_ARRAY_INIT;
602 char* windowTitle =
nullptr;
605 rdpSettings* settings = xfc->common.context.settings;
606 WINPR_ASSERT(settings);
613 const XSetWindowAttributes empty = WINPR_C_ARRAY_INIT;
614 xfc->attribs = empty;
619 xfc->depth = DefaultDepthOfScreen(xfc->screen);
621 XVisualInfo vinfo = WINPR_C_ARRAY_INIT;
622 if (XMatchVisualInfo(xfc->display, xfc->screen_number, xfc->depth, TrueColor, &vinfo))
624 Window root = XDefaultRootWindow(xfc->display);
625 xfc->visual = vinfo.visual;
626 xfc->attribs.colormap = xfc->colormap =
627 XCreateColormap(xfc->display, root, vinfo.visual, AllocNone);
633 WLog_WARN(TAG,
"running in remote app mode, but XServer does not support transparency");
634 WLog_WARN(TAG,
"display of remote applications might be distorted (black frames, ...)");
636 xfc->depth = DefaultDepthOfScreen(xfc->screen);
637 xfc->visual = DefaultVisual(xfc->display, xfc->screen_number);
638 xfc->attribs.colormap = xfc->colormap = DefaultColormap(xfc->display, xfc->screen_number);
645 if (vinfo.red_mask & 0xFF)
650 if (!xfc->remote_app)
652 xfc->attribs.background_pixel = BlackPixelOfScreen(xfc->screen);
653 xfc->attribs.border_pixel = WhitePixelOfScreen(xfc->screen);
654 xfc->attribs.backing_store = xfc->primary ? NotUseful : Always;
655 xfc->attribs.override_redirect = False;
657 xfc->attribs.bit_gravity = NorthWestGravity;
658 xfc->attribs.win_gravity = NorthWestGravity;
659 xfc->attribs_mask = CWBackPixel | CWBackingStore | CWOverrideRedirect | CWColormap |
660 CWBorderPixel | CWWinGravity | CWBitGravity;
666 windowTitle = xf_window_get_title(settings);
676 width = WINPR_ASSERTING_INT_CAST(
680 height = WINPR_ASSERTING_INT_CAST(
683 xfc->scaledWidth = width;
684 xfc->scaledHeight = height;
688 xfc->window = xf_CreateDesktopWindow(xfc, windowTitle, width, height);
694 xf_SetWindowFullscreen(xfc, xfc->window, xfc->fullscreen);
696 xfc->unobscured = (xevent.xvisibility.state == VisibilityUnobscured);
697 XSetWMProtocols(xfc->display, xfc->window->handle, &(xfc->WM_DELETE_WINDOW), 1);
698 xfc->drawable = xfc->window->handle;
702 xfc->attribs.border_pixel = 0;
703 xfc->attribs.background_pixel = 0;
704 xfc->attribs.backing_store = xfc->primary ? NotUseful : Always;
705 xfc->attribs.override_redirect = False;
707 xfc->attribs.bit_gravity = NorthWestGravity;
708 xfc->attribs.win_gravity = NorthWestGravity;
709 xfc->attribs_mask = CWBackPixel | CWBackingStore | CWOverrideRedirect | CWColormap |
710 CWBorderPixel | CWWinGravity | CWBitGravity;
712 xfc->drawable = xf_CreateDummyWindow(xfc);
717 LogDynAndXCreateGC(xfc->log, xfc->display, xfc->drawable, GCGraphicsExposures, &gcv);
719 WINPR_ASSERT(xfc->depth != 0);
722 LogDynAndXCreatePixmap(xfc->log, xfc->display, xfc->drawable,
725 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth));
727 xfc->drawing = xfc->primary;
729 if (!xfc->bitmap_mono)
730 xfc->bitmap_mono = LogDynAndXCreatePixmap(xfc->log, xfc->display, xfc->drawable, 8, 8, 1);
734 LogDynAndXCreateGC(xfc->log, xfc->display, xfc->bitmap_mono, GCGraphicsExposures, &gcv);
736 LogDynAndXSetFunction(xfc->log, xfc->display, xfc->gc, GXcopy);
737 LogDynAndXSetFillStyle(xfc->log, xfc->display, xfc->gc, FillSolid);
738 LogDynAndXSetForeground(xfc->log, xfc->display, xfc->gc, BlackPixelOfScreen(xfc->screen));
739 LogDynAndXFillRectangle(xfc->log, xfc->display, xfc->primary, xfc->gc, 0, 0,
742 LogDynAndXFlush(xfc->log, xfc->display);
747BOOL xf_create_image(xfContext* xfc)
752 const rdpSettings* settings = xfc->common.context.settings;
753 rdpGdi* cgdi = xfc->common.context.gdi;
756 WINPR_ASSERT(xfc->depth != 0);
757 xfc->image = LogDynAndXCreateImage(
758 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
759 ZPixmap, 0, (
char*)cgdi->primary_buffer,
762 WINPR_ASSERTING_INT_CAST(
int, cgdi->stride));
763 xfc->image->byte_order = LSBFirst;
764 xfc->image->bitmap_bit_order = LSBFirst;
769void xf_destroy_window(xfContext* xfc)
773 xf_DestroyDesktopWindow(xfc, xfc->window);
774 xfc->window =
nullptr;
777#if defined(CHANNEL_TSMF_CLIENT)
780 xf_tsmf_uninit(xfc,
nullptr);
781 xfc->xv_context =
nullptr;
787 xfc->image->data =
nullptr;
788 XDestroyImage(xfc->image);
789 xfc->image =
nullptr;
792 if (xfc->bitmap_mono)
794 LogDynAndXFreePixmap(xfc->log, xfc->display, xfc->bitmap_mono);
795 xfc->bitmap_mono = 0;
800 LogDynAndXFreeGC(xfc->log, xfc->display, xfc->gc_mono);
801 xfc->gc_mono =
nullptr;
806 LogDynAndXFreePixmap(xfc->log, xfc->display, xfc->primary);
812 LogDynAndXFreeGC(xfc->log, xfc->display, xfc->gc);
817void xf_toggle_fullscreen(xfContext* xfc)
819 WindowStateChangeEventArgs e = WINPR_C_ARRAY_INIT;
820 rdpContext* context = (rdpContext*)xfc;
821 rdpSettings* settings = context->settings;
830 xfc->fullscreen = !((xfc->fullscreen));
833 xf_SetWindowFullscreen(xfc, xfc->window, xfc->fullscreen);
834 EventArgsInit(&e,
"xfreerdp");
835 e.state = xfc->fullscreen ? FREERDP_WINDOW_STATE_FULLSCREEN : 0;
836 if (PubSub_OnWindowStateChange(context->pubSub, context, &e) < 0)
838 WLog_Print(xfc->log, WLOG_ERROR,
"PubSub_OnWindowStateChange failed");
842void xf_minimize(xfContext* xfc)
844 WindowStateChangeEventArgs e = WINPR_C_ARRAY_INIT;
845 rdpContext* context = (rdpContext*)xfc;
846 WINPR_ASSERT(context);
855 xf_SetWindowMinimized(xfc, xfc->window);
857 e.state = xfc->fullscreen ? FREERDP_WINDOW_STATE_FULLSCREEN : 0;
858 if (PubSub_OnWindowStateChange(context->pubSub, context, &e) < 0)
860 WLog_Print(xfc->log, WLOG_ERROR,
"PubSub_OnWindowStateChange failed");
864void xf_lock_x11_(xfContext* xfc, WINPR_ATTR_UNUSED
const char* fkt)
866 if (!xfc->UseXThreads)
867 (void)WaitForSingleObject(xfc->mutex, INFINITE);
869 XLockDisplay(xfc->display);
874void xf_unlock_x11_(xfContext* xfc, WINPR_ATTR_UNUSED
const char* fkt)
876 if (xfc->locked == 0)
877 WLog_WARN(TAG,
"X11: trying to unlock although not locked!");
881 if (!xfc->UseXThreads)
882 (void)ReleaseMutex(xfc->mutex);
884 XUnlockDisplay(xfc->display);
887static BOOL xf_get_pixmap_info(xfContext* xfc)
890 XPixmapFormatValues* pfs =
nullptr;
892 WINPR_ASSERT(xfc->display);
893 pfs = XListPixmapFormats(xfc->display, &pf_count);
897 WLog_ERR(TAG,
"XListPixmapFormats failed");
901 WINPR_ASSERT(xfc->depth != 0);
902 for (
int i = 0; i < pf_count; i++)
904 const XPixmapFormatValues* pf = &pfs[i];
906 if (pf->depth == xfc->depth)
908 xfc->scanline_pad = pf->scanline_pad;
914 return !((xfc->visual ==
nullptr) || (xfc->scanline_pad == 0));
917static int xf_error_handler(Display* d, XErrorEvent* ev)
919 char buf[256] = WINPR_C_ARRAY_INIT;
920 XGetErrorText(d, ev->error_code, buf,
sizeof(buf));
921 const char* what = request_code_2_str(ev->request_code);
922 WLog_ERR(TAG,
"%s: %s", what, buf);
923 winpr_log_backtrace(TAG, WLOG_ERROR, 20);
927static int xf_error_handler_ex(Display* d, XErrorEvent* ev)
935 XUngrabKeyboard(d, CurrentTime);
936 XUngrabPointer(d, CurrentTime);
938 return xf_error_handler(d, ev);
941static BOOL xf_play_sound(rdpContext* context,
const PLAY_SOUND_UPDATE* play_sound)
943 xfContext* xfc = (xfContext*)context;
944 WINPR_UNUSED(play_sound);
945 XkbBell(xfc->display, None, 100, 0);
949static void xf_check_extensions(xfContext* context)
954 int xkb_major = XkbMajorVersion;
955 int xkb_minor = XkbMinorVersion;
957 if (XkbLibraryVersion(&xkb_major, &xkb_minor) &&
958 XkbQueryExtension(context->display, &xkb_opcode, &xkb_event, &xkb_error, &xkb_major,
961 context->xkbAvailable = TRUE;
966 int xrender_event_base = 0;
967 int xrender_error_base = 0;
969 if (XRenderQueryExtension(context->display, &xrender_event_base, &xrender_error_base))
971 context->xrenderAvailable = TRUE;
980static const char TEST_PTR_STR[] =
"Virtual core XTEST pointer";
981static const size_t TEST_PTR_LEN =
sizeof(TEST_PTR_STR) /
sizeof(
char);
984static void xf_get_x11_button_map(xfContext* xfc,
unsigned char* x11_map)
990 XDevice* ptr_dev =
nullptr;
991 XExtensionVersion* version =
nullptr;
992 XDeviceInfo* devices1 =
nullptr;
993 XIDeviceInfo* devices2 =
nullptr;
996 if (XQueryExtension(xfc->display,
"XInputExtension", &opcode, &event, &error))
998 WLog_DBG(TAG,
"Searching for XInput pointer device");
1001 version = XGetExtensionVersion(xfc->display, INAME);
1003 if (version->major_version >= 2)
1006 devices2 = XIQueryDevice(xfc->display, XIAllDevices, &num_devices);
1010 for (
int i = 0; i < num_devices; ++i)
1012 XIDeviceInfo* dev = &devices2[i];
1013 if ((dev->use == XISlavePointer) &&
1014 (strncmp(dev->name, TEST_PTR_STR, TEST_PTR_LEN) != 0))
1016 ptr_dev = XOpenDevice(xfc->display,
1017 WINPR_ASSERTING_INT_CAST(uint32_t, dev->deviceid));
1023 XIFreeDeviceInfo(devices2);
1029 devices1 = XListInputDevices(xfc->display, &num_devices);
1033 for (
int i = 0; i < num_devices; ++i)
1035 if ((devices1[i].use == IsXExtensionPointer) &&
1036 (strncmp(devices1[i].name, TEST_PTR_STR, TEST_PTR_LEN) != 0))
1038 ptr_dev = XOpenDevice(xfc->display, devices1[i].id);
1044 XFreeDeviceList(devices1);
1054 WLog_DBG(TAG,
"Pointer device: %" PRIu32,
1055 WINPR_CXX_COMPAT_CAST(uint32_t, ptr_dev->device_id));
1056 XGetDeviceButtonMapping(xfc->display, ptr_dev, x11_map, NUM_BUTTONS_MAPPED);
1057 XCloseDevice(xfc->display, ptr_dev);
1061 WLog_DBG(TAG,
"No pointer device found!");
1067 WLog_DBG(TAG,
"Get global pointer mapping (no XInput)");
1068 XGetPointerMapping(xfc->display, x11_map, NUM_BUTTONS_MAPPED);
1074static const button_map xf_button_flags[NUM_BUTTONS_MAPPED] = {
1075 { Button1, PTR_FLAGS_BUTTON1 },
1076 { Button2, PTR_FLAGS_BUTTON3 },
1077 { Button3, PTR_FLAGS_BUTTON2 },
1078 { Button4, PTR_FLAGS_WHEEL | 0x78 },
1080 { Button5, PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | (0x100 - 0x78) },
1081 { 6, PTR_FLAGS_HWHEEL | PTR_FLAGS_WHEEL_NEGATIVE | (0x100 - 0x78) },
1082 { 7, PTR_FLAGS_HWHEEL | 0x78 },
1083 { 8, PTR_XFLAGS_BUTTON1 },
1084 { 9, PTR_XFLAGS_BUTTON2 },
1085 { 97, PTR_XFLAGS_BUTTON1 },
1086 { 112, PTR_XFLAGS_BUTTON2 }
1089static UINT16 get_flags_for_button(
size_t button)
1091 for (
size_t x = 0; x < ARRAYSIZE(xf_button_flags); x++)
1095 if (map->button == button)
1102void xf_button_map_init(xfContext* xfc)
1109 unsigned char x11_map[112] = WINPR_C_ARRAY_INIT;
1112 WINPR_ASSERT(xfc->common.context.settings);
1114 x11_map[0] = Button1;
1115 x11_map[1] = Button2;
1116 x11_map[2] = Button3;
1117 x11_map[3] = Button4;
1118 x11_map[4] = Button5;
1129 xf_get_x11_button_map(xfc, x11_map);
1135 for (
size_t physical = 0; physical < ARRAYSIZE(x11_map); ++physical)
1137 const unsigned char logical = x11_map[physical];
1138 const UINT16 flags = get_flags_for_button(logical);
1140 if ((logical != 0) && (flags != 0))
1142 if (pos >= NUM_BUTTONS_MAPPED)
1144 WLog_ERR(TAG,
"Failed to map mouse button to RDP button, no space");
1149 map->button = logical;
1150 map->flags = get_flags_for_button(physical + Button1);
1167static BOOL xf_pre_connect(freerdp* instance)
1169 UINT32 maxWidth = 0;
1170 UINT32 maxHeight = 0;
1172 WINPR_ASSERT(instance);
1174 rdpContext* context = instance->context;
1175 WINPR_ASSERT(context);
1176 xfContext* xfc = (xfContext*)context;
1178 rdpSettings* settings = context->settings;
1179 WINPR_ASSERT(settings);
1186 if (!xf_setup_x11(xfc))
1194 if (PubSub_SubscribeChannelConnected(context->pubSub, xf_OnChannelConnectedEventHandler) < 0)
1196 if (PubSub_SubscribeChannelDisconnected(context->pubSub, xf_OnChannelDisconnectedEventHandler) <
1204 char login_name[MAX_PATH] = WINPR_C_ARRAY_INIT;
1205 ULONG size =
sizeof(login_name) - 1;
1207 if (GetUserNameExA(NameSamCompatible, login_name, &size))
1212 WLog_INFO(TAG,
"No user name set. - Using login name: %s",
1222 WLog_INFO(TAG,
"auth-only, but no password set. Please provide one.");
1226 WLog_INFO(TAG,
"Authentication only. Don't connect to X.");
1232 xfc->common.context.settings, FreeRDP_KeyboardRemappingList);
1234 xfc->remap_table = freerdp_keyboard_remap_string_to_list(KeyboardRemappingList);
1235 if (!xfc->remap_table)
1237 if (!xf_keyboard_init(xfc))
1239 if (!xf_keyboard_action_script_init(xfc))
1241 if (!xf_detect_monitors(xfc, &maxWidth, &maxHeight))
1265 settings, FreeRDP_DesktopWidth,
1269 settings, FreeRDP_DesktopHeight,
1280 xf_button_map_init(xfc);
1284static BOOL xf_inject_keypress(rdpContext* context,
const char* buffer,
size_t size)
1286 WCHAR wbuffer[64] = WINPR_C_ARRAY_INIT;
1287 const SSIZE_T len = ConvertUtf8NToWChar(buffer, size, wbuffer, ARRAYSIZE(wbuffer));
1291 rdpInput* input = context->input;
1292 WINPR_ASSERT(input);
1294 for (SSIZE_T x = 0; x < len; x++)
1296 const WCHAR code = wbuffer[x];
1297 freerdp_input_send_unicode_keyboard_event(input, 0, code);
1299 freerdp_input_send_unicode_keyboard_event(input, KBD_FLAGS_RELEASE, code);
1305static BOOL xf_process_pipe(rdpContext* context,
const char* pipe)
1307 int fd = open(pipe, O_NONBLOCK | O_RDONLY);
1310 char ebuffer[256] = WINPR_C_ARRAY_INIT;
1311 WLog_ERR(TAG,
"pipe '%s' open returned %s [%d]", pipe,
1312 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)), errno);
1315 while (!freerdp_shall_disconnect_context(context))
1317 char buffer[64] = WINPR_C_ARRAY_INIT;
1319 ssize_t rd = read(fd, buffer,
sizeof(buffer) - 1);
1322 char ebuffer[256] = WINPR_C_ARRAY_INIT;
1323 if ((errno == EAGAIN) || (errno == 0))
1330 WLog_ERR(TAG,
"pipe '%s' read returned %s [%d]", pipe,
1331 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)), errno);
1336 char ebuffer[256] = WINPR_C_ARRAY_INIT;
1337 WLog_ERR(TAG,
"pipe '%s' read returned %s [%d]", pipe,
1338 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)), errno);
1343 if (!xf_inject_keypress(context, buffer, WINPR_ASSERTING_INT_CAST(
size_t, rd)))
1351static void cleanup_pipe(WINPR_ATTR_UNUSED
int signum, WINPR_ATTR_UNUSED
const char* signame,
1354 const char* pipe = context;
1360static DWORD WINAPI xf_handle_pipe(
void* arg)
1362 xfContext* xfc = arg;
1365 rdpContext* context = &xfc->common.context;
1366 WINPR_ASSERT(context);
1368 rdpSettings* settings = context->settings;
1369 WINPR_ASSERT(settings);
1374 const int rc = mkfifo(pipe, S_IWUSR | S_IRUSR);
1377 char ebuffer[256] = WINPR_C_ARRAY_INIT;
1378 WLog_ERR(TAG,
"Failed to create named pipe '%s': %s [%d]", pipe,
1379 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)), errno);
1383 void* ctx = WINPR_CAST_CONST_PTR_AWAY(pipe,
void*);
1384 if (freerdp_add_signal_cleanup_handler(ctx, cleanup_pipe))
1387 xf_process_pipe(context, pipe);
1389 freerdp_del_signal_cleanup_handler(ctx, cleanup_pipe);
1401static BOOL xf_post_connect(freerdp* instance)
1403 ResizeWindowEventArgs e = WINPR_C_ARRAY_INIT;
1405 WINPR_ASSERT(instance);
1406 xfContext* xfc = (xfContext*)instance->context;
1407 rdpContext* context = instance->context;
1408 WINPR_ASSERT(context);
1410 rdpSettings* settings = context->settings;
1411 WINPR_ASSERT(settings);
1413 rdpUpdate* update = context->update;
1414 WINPR_ASSERT(update);
1417 xfc->remote_app = TRUE;
1419 if (!xf_create_window(xfc))
1422 if (!xf_get_pixmap_info(xfc))
1425 if (!gdi_init(instance, xf_get_local_color_format(xfc, TRUE)))
1428 if (!xf_create_image(xfc))
1431 if (!xf_register_pointer(context->graphics))
1443 if (!xfc->xrenderAvailable)
1447 WLog_ERR(TAG,
"XRender not available: disabling smart-sizing");
1454 WLog_ERR(TAG,
"XRender not available: disabling local multi-touch gestures");
1460 update->DesktopResize = xf_sw_desktop_resize;
1461 update->EndPaint = xf_end_paint;
1462 update->PlaySound = xf_play_sound;
1463 update->SetKeyboardIndicators = xf_keyboard_set_indicators;
1464 update->SetKeyboardImeStatus = xf_keyboard_set_ime_status;
1466 const BOOL serverIsWindowsPlatform =
1469 !(xfc->clipboard = xf_clipboard_new(xfc, !serverIsWindowsPlatform)))
1472 if (!(xfc->xfDisp = xf_disp_new(xfc)))
1478 xfc->pipethread = CreateThread(
nullptr, 0, xf_handle_pipe, xfc, 0,
nullptr);
1479 if (!xfc->pipethread)
1483 EventArgsInit(&e,
"xfreerdp");
1488 return PubSub_OnResizeWindow(context->pubSub, xfc, &e) >= 0;
1491static void xf_post_disconnect(freerdp* instance)
1493 xfContext* xfc =
nullptr;
1494 rdpContext* context =
nullptr;
1496 if (!instance || !instance->context)
1499 context = instance->context;
1500 xfc = (xfContext*)context;
1501 PubSub_UnsubscribeChannelConnected(instance->context->pubSub,
1502 xf_OnChannelConnectedEventHandler);
1503 PubSub_UnsubscribeChannelDisconnected(instance->context->pubSub,
1504 xf_OnChannelDisconnectedEventHandler);
1507 if (xfc->pipethread)
1509 (void)WaitForSingleObject(xfc->pipethread, INFINITE);
1510 (void)CloseHandle(xfc->pipethread);
1511 xfc->pipethread =
nullptr;
1515 xf_clipboard_free(xfc->clipboard);
1516 xfc->clipboard =
nullptr;
1521 xf_disp_free(xfc->xfDisp);
1522 xfc->xfDisp =
nullptr;
1525 if ((xfc->window !=
nullptr) && (xfc->drawable == xfc->window->handle))
1528 xf_DestroyDummyWindow(xfc, xfc->drawable);
1530 freerdp_keyboard_remap_free(xfc->remap_table);
1531 xfc->remap_table =
nullptr;
1533 xf_destroy_window(xfc);
1536static void xf_post_final_disconnect(freerdp* instance)
1538 xfContext* xfc =
nullptr;
1539 rdpContext* context =
nullptr;
1541 if (!instance || !instance->context)
1544 context = instance->context;
1545 xfc = (xfContext*)context;
1547 xf_keyboard_free(xfc);
1548 xf_teardown_x11(xfc);
1551static int xf_logon_error_info(freerdp* instance, UINT32 data, UINT32 type)
1553 xfContext* xfc = (xfContext*)instance->context;
1554 const char* str_data = freerdp_get_logon_error_info_data(data);
1555 const char* str_type = freerdp_get_logon_error_info_type(type);
1556 WLog_INFO(TAG,
"Logon Error Info %s [%s]", str_data, str_type);
1557 if (type != LOGON_MSG_SESSION_CONTINUE)
1559 if (!xf_rail_disable_remoteapp_mode(xfc))
1565static BOOL handle_window_events(freerdp* instance)
1567 if (!xf_process_x_events(instance))
1569 WLog_DBG(TAG,
"Closed from X11");
1570 freerdp_abort_connect_context(instance->context);
1585static DWORD WINAPI xf_client_thread(LPVOID param)
1587 DWORD exit_code = 0;
1588 DWORD waitStatus = 0;
1589 HANDLE inputEvent =
nullptr;
1591 freerdp* instance = (freerdp*)param;
1592 WINPR_ASSERT(instance);
1594 const BOOL status = freerdp_connect(instance);
1595 rdpContext* context = instance->context;
1596 WINPR_ASSERT(context);
1597 xfContext* xfc = (xfContext*)instance->context;
1600 rdpSettings* settings = context->settings;
1601 WINPR_ASSERT(settings);
1605 UINT32 error = freerdp_get_last_error(instance->context);
1606 exit_code = (uint32_t)xf_map_error_to_exit_code(error);
1609 exit_code = XF_EXIT_SUCCESS;
1617 WLog_ERR(TAG,
"Authentication only, exit status %" PRId32
"", !status);
1623 WLog_ERR(TAG,
"Freerdp connect error exit status %" PRId32
"", !status);
1624 exit_code = freerdp_error_info(instance);
1626 if (freerdp_get_last_error(instance->context) == FREERDP_ERROR_AUTHENTICATION_FAILED)
1627 exit_code = XF_EXIT_AUTH_FAILURE;
1628 else if (exit_code == ERRINFO_SUCCESS)
1629 exit_code = XF_EXIT_CONN_FAILED;
1634 inputEvent = xfc->x11event;
1636 while (!freerdp_shall_disconnect_context(instance->context))
1638 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
1640 handles[nCount++] = inputEvent;
1647 if (freerdp_focus_required(instance))
1649 xf_keyboard_focus_in(xfc);
1650 xf_keyboard_focus_in(xfc);
1655 freerdp_get_event_handles(context, &handles[nCount], ARRAYSIZE(handles) - nCount);
1659 WLog_ERR(TAG,
"freerdp_get_event_handles failed");
1667 xf_floatbar_hide_and_show(xfc->window->floatbar);
1669 waitStatus = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
1671 if (waitStatus == WAIT_FAILED)
1675 if (!freerdp_check_event_handles(context))
1677 if (client_auto_reconnect_ex(instance, handle_window_events))
1685 const UINT32 error = freerdp_get_last_error(instance->context);
1687 if (freerdp_error_info(instance) == 0)
1688 exit_code = (uint32_t)xf_map_error_to_exit_code(error);
1691 if (freerdp_get_last_error(context) == FREERDP_ERROR_SUCCESS)
1692 WLog_ERR(TAG,
"Failed to check FreeRDP file descriptor");
1698 if (!handle_window_events(instance))
1704 exit_code = freerdp_error_info(instance);
1706 if (exit_code == XF_EXIT_DISCONNECT &&
1707 freerdp_get_disconnect_ultimatum(context) == Disconnect_Ultimatum_user_requested)
1710 WLog_INFO(TAG,
"Error info says user did not initiate but disconnect ultimatum says "
1711 "they did; treat this as a user logoff");
1712 exit_code = XF_EXIT_LOGOFF;
1718 freerdp_disconnect(instance);
1720 ExitThread(exit_code);
1724int xf_exit_code_from_disconnect_reason(DWORD reason)
1726 if ((reason == 0) || ((reason >= XF_EXIT_PARSE_ARGUMENTS) && (reason <= XF_EXIT_CODE_LAST)))
1727 return WINPR_ASSERTING_INT_CAST(
int, reason);
1729 else if (reason >= 0x100 && reason <= 0x10A)
1730 reason -= 0x100 + XF_EXIT_LICENSE_INTERNAL;
1732 else if (reason >= 0x10c9 && reason <= 0x1193)
1733 reason = XF_EXIT_RDP;
1735 else if (!(reason <= 0xC))
1736 reason = XF_EXIT_UNKNOWN;
1738 return WINPR_ASSERTING_INT_CAST(
int, reason);
1741static void xf_TerminateEventHandler(
void* context,
const TerminateEventArgs* e)
1743 rdpContext* ctx = (rdpContext*)context;
1745 freerdp_abort_connect_context(ctx);
1749static void xf_ZoomingChangeEventHandler(
void* context,
const ZoomingChangeEventArgs* e)
1753 rdpSettings* settings =
nullptr;
1754 xfContext* xfc = (xfContext*)context;
1758 settings = xfc->common.context.settings;
1759 WINPR_ASSERT(settings);
1761 w = xfc->scaledWidth + e->dx;
1762 h = xfc->scaledHeight + e->dy;
1764 if (e->dx == 0 && e->dy == 0)
1773 if (w == xfc->scaledWidth && h == xfc->scaledHeight)
1776 xfc->scaledWidth = w;
1777 xfc->scaledHeight = h;
1778 xf_draw_screen(xfc, 0, 0,
1779 WINPR_ASSERTING_INT_CAST(
1781 WINPR_ASSERTING_INT_CAST(
1785static void xf_PanningChangeEventHandler(
void* context,
const PanningChangeEventArgs* e)
1787 xfContext* xfc = (xfContext*)context;
1788 rdpSettings* settings =
nullptr;
1793 settings = xfc->common.context.settings;
1794 WINPR_ASSERT(settings);
1796 if (e->dx == 0 && e->dy == 0)
1799 xfc->offset_x += e->dx;
1800 xfc->offset_y += e->dy;
1801 xf_draw_screen(xfc, 0, 0,
1802 WINPR_ASSERTING_INT_CAST(
1804 WINPR_ASSERTING_INT_CAST(
1813static BOOL xfreerdp_client_global_init(
void)
1816 (void)setlocale(LC_ALL,
"");
1818 return (freerdp_handle_signals() == 0);
1821static void xfreerdp_client_global_uninit(
void)
1825static int xfreerdp_client_start(rdpContext* context)
1827 xfContext* xfc = (xfContext*)context;
1828 rdpSettings* settings = context->settings;
1832 WLog_ERR(TAG,
"error: server hostname was not specified with /v:<server>[:port]");
1836 if (!(xfc->common.thread =
1837 CreateThread(
nullptr, 0, xf_client_thread, context->instance, 0,
nullptr)))
1839 WLog_ERR(TAG,
"failed to create client thread");
1846static Atom get_supported_atom(xfContext* xfc,
const char* atomName)
1848 const Atom atom = Logging_XInternAtom(xfc->log, xfc->display, atomName, False);
1850 for (
unsigned long i = 0; i < xfc->supportedAtomCount; i++)
1852 if (xfc->supportedAtoms[i] == atom)
1859void xf_teardown_x11(xfContext* xfc)
1865 LogDynAndXCloseDisplay(xfc->log, xfc->display);
1866 xfc->display =
nullptr;
1871 (void)CloseHandle(xfc->x11event);
1872 xfc->x11event =
nullptr;
1877 (void)CloseHandle(xfc->mutex);
1878 xfc->mutex =
nullptr;
1881 if (xfc->vscreen.monitors)
1883 free(xfc->vscreen.monitors);
1884 xfc->vscreen.monitors =
nullptr;
1886 xfc->vscreen.nmonitors = 0;
1888 free(xfc->supportedAtoms);
1889 xfc->supportedAtoms =
nullptr;
1890 xfc->supportedAtomCount = 0;
1893BOOL xf_setup_x11(xfContext* xfc)
1897 xfc->UseXThreads = TRUE;
1904 if (xfc->UseXThreads)
1906 if (!XInitThreads())
1908 WLog_WARN(TAG,
"XInitThreads() failure");
1909 xfc->UseXThreads = FALSE;
1913 xfc->display = XOpenDisplay(
nullptr);
1917 WLog_ERR(TAG,
"failed to open display: %s", XDisplayName(
nullptr));
1918 WLog_ERR(TAG,
"Please check that the $DISPLAY environment variable is properly set.");
1923 WLog_INFO(TAG,
"Enabling X11 debug mode.");
1924 XSynchronize(xfc->display, TRUE);
1926 def_error_handler = XSetErrorHandler(xf_error_handler_ex);
1928 xfc->mutex = CreateMutex(
nullptr, FALSE,
nullptr);
1932 WLog_ERR(TAG,
"Could not create mutex!");
1936 xfc->xfds = ConnectionNumber(xfc->display);
1937 xfc->screen_number = DefaultScreen(xfc->display);
1938 xfc->screen = ScreenOfDisplay(xfc->display, xfc->screen_number);
1939 xfc->big_endian = (ImageByteOrder(xfc->display) == MSBFirst);
1941 xfc->complex_regions = TRUE;
1942 xfc->NET_SUPPORTED = Logging_XInternAtom(xfc->log, xfc->display,
"_NET_SUPPORTED", True);
1943 xfc->NET_SUPPORTING_WM_CHECK =
1944 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_SUPPORTING_WM_CHECK", True);
1946 if ((xfc->NET_SUPPORTED != None) && (xfc->NET_SUPPORTING_WM_CHECK != None))
1948 Atom actual_type = 0;
1949 int actual_format = 0;
1950 unsigned long nitems = 0;
1951 unsigned long after = 0;
1952 unsigned char* data =
nullptr;
1953 int status = LogDynAndXGetWindowProperty(
1954 xfc->log, xfc->display, RootWindowOfScreen(xfc->screen), xfc->NET_SUPPORTED, 0, 1024,
1955 False, XA_ATOM, &actual_type, &actual_format, &nitems, &after, &data);
1957 if ((status == Success) && (actual_type == XA_ATOM) && (actual_format == 32))
1959 xfc->supportedAtomCount = nitems;
1960 xfc->supportedAtoms = calloc(xfc->supportedAtomCount,
sizeof(Atom));
1961 WINPR_ASSERT(xfc->supportedAtoms);
1962 memcpy(xfc->supportedAtoms, data, nitems *
sizeof(Atom));
1969 xfc->XWAYLAND_MAY_GRAB_KEYBOARD =
1970 Logging_XInternAtom(xfc->log, xfc->display,
"_XWAYLAND_MAY_GRAB_KEYBOARD", False);
1971 xfc->NET_WM_ICON = Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ICON", False);
1972 xfc->MOTIF_WM_HINTS = Logging_XInternAtom(xfc->log, xfc->display,
"_MOTIF_WM_HINTS", False);
1973 xfc->NET_NUMBER_OF_DESKTOPS =
1974 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_NUMBER_OF_DESKTOPS", False);
1975 xfc->NET_CURRENT_DESKTOP =
1976 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_CURRENT_DESKTOP", False);
1977 xfc->NET_WORKAREA = Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WORKAREA", False);
1978 xfc->NET_WM_STATE = get_supported_atom(xfc,
"_NET_WM_STATE");
1979 xfc->NET_WM_STATE_MODAL = get_supported_atom(xfc,
"_NET_WM_STATE_MODAL");
1980 xfc->NET_WM_STATE_STICKY = get_supported_atom(xfc,
"_NET_WM_STATE_STICKY");
1981 xfc->NET_WM_STATE_MAXIMIZED_HORZ =
1982 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_STATE_MAXIMIZED_HORZ", False);
1983 xfc->NET_WM_STATE_MAXIMIZED_VERT =
1984 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_STATE_MAXIMIZED_VERT", False);
1985 xfc->NET_WM_STATE_SHADED = get_supported_atom(xfc,
"_NET_WM_STATE_SHADED");
1986 xfc->NET_WM_STATE_SKIP_TASKBAR = get_supported_atom(xfc,
"_NET_WM_STATE_SKIP_TASKBAR");
1987 xfc->NET_WM_STATE_SKIP_PAGER = get_supported_atom(xfc,
"_NET_WM_STATE_SKIP_PAGER");
1988 xfc->NET_WM_STATE_HIDDEN = get_supported_atom(xfc,
"_NET_WM_STATE_HIDDEN");
1989 xfc->NET_WM_STATE_FULLSCREEN = get_supported_atom(xfc,
"_NET_WM_STATE_FULLSCREEN");
1990 xfc->NET_WM_STATE_ABOVE = get_supported_atom(xfc,
"_NET_WM_STATE_ABOVE");
1991 xfc->NET_WM_STATE_BELOW = get_supported_atom(xfc,
"_NET_WM_STATE_BELOW");
1992 xfc->NET_WM_STATE_DEMANDS_ATTENTION =
1993 get_supported_atom(xfc,
"_NET_WM_STATE_DEMANDS_ATTENTION");
1994 xfc->NET_WM_FULLSCREEN_MONITORS = get_supported_atom(xfc,
"_NET_WM_FULLSCREEN_MONITORS");
1995 xfc->NET_WM_NAME = Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_NAME", False);
1996 xfc->NET_WM_PID = Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_PID", False);
1997 xfc->NET_WM_WINDOW_TYPE =
1998 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE", False);
1999 xfc->NET_WM_WINDOW_TYPE_NORMAL =
2000 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE_NORMAL", False);
2001 xfc->NET_WM_WINDOW_TYPE_DIALOG =
2002 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE_DIALOG", False);
2003 xfc->NET_WM_WINDOW_TYPE_POPUP =
2004 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE_POPUP", False);
2005 xfc->NET_WM_WINDOW_TYPE_POPUP_MENU =
2006 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE_POPUP_MENU", False);
2007 xfc->NET_WM_WINDOW_TYPE_UTILITY =
2008 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE_UTILITY", False);
2009 xfc->NET_WM_WINDOW_TYPE_DROPDOWN_MENU =
2010 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", False);
2011 xfc->NET_WM_STATE_SKIP_TASKBAR =
2012 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_STATE_SKIP_TASKBAR", False);
2013 xfc->NET_WM_STATE_SKIP_PAGER =
2014 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_STATE_SKIP_PAGER", False);
2015 xfc->NET_WM_MOVERESIZE =
2016 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_MOVERESIZE", False);
2017 xfc->NET_MOVERESIZE_WINDOW =
2018 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_MOVERESIZE_WINDOW", False);
2019 xfc->UTF8_STRING = Logging_XInternAtom(xfc->log, xfc->display,
"UTF8_STRING", FALSE);
2020 xfc->WM_PROTOCOLS = Logging_XInternAtom(xfc->log, xfc->display,
"WM_PROTOCOLS", False);
2021 xfc->WM_DELETE_WINDOW = Logging_XInternAtom(xfc->log, xfc->display,
"WM_DELETE_WINDOW", False);
2022 xfc->WM_STATE = Logging_XInternAtom(xfc->log, xfc->display,
"WM_STATE", False);
2023 xfc->x11event = CreateFileDescriptorEvent(
nullptr, FALSE, FALSE, xfc->xfds, WINPR_FD_READ);
2025 xfc->NET_WM_ALLOWED_ACTIONS =
2026 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ALLOWED_ACTIONS", False);
2028 xfc->NET_WM_ACTION_CLOSE =
2029 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_CLOSE", False);
2030 xfc->NET_WM_ACTION_MINIMIZE =
2031 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_MINIMIZE", False);
2032 xfc->NET_WM_ACTION_MOVE =
2033 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_MOVE", False);
2034 xfc->NET_WM_ACTION_RESIZE =
2035 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_RESIZE", False);
2036 xfc->NET_WM_ACTION_MAXIMIZE_HORZ =
2037 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_MAXIMIZE_HORZ", False);
2038 xfc->NET_WM_ACTION_MAXIMIZE_VERT =
2039 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_MAXIMIZE_VERT", False);
2040 xfc->NET_WM_ACTION_FULLSCREEN =
2041 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_FULLSCREEN", False);
2042 xfc->NET_WM_ACTION_CHANGE_DESKTOP =
2043 Logging_XInternAtom(xfc->log, xfc->display,
"_NET_WM_ACTION_CHANGE_DESKTOP", False);
2047 WLog_ERR(TAG,
"Could not create xfds event");
2051 xf_check_extensions(xfc);
2053 xfc->vscreen.monitors = calloc(16,
sizeof(MONITOR_INFO));
2055 if (!xfc->vscreen.monitors)
2060 xf_teardown_x11(xfc);
2064static BOOL xfreerdp_client_new(freerdp* instance, rdpContext* context)
2066 xfContext* xfc = (xfContext*)instance->context;
2067 WINPR_ASSERT(context);
2069 WINPR_ASSERT(!xfc->display);
2070 WINPR_ASSERT(!xfc->mutex);
2071 WINPR_ASSERT(!xfc->x11event);
2072 instance->PreConnect = xf_pre_connect;
2073 instance->PostConnect = xf_post_connect;
2074 instance->PostDisconnect = xf_post_disconnect;
2075 instance->PostFinalDisconnect = xf_post_final_disconnect;
2076 instance->LogonErrorInfo = xf_logon_error_info;
2077 instance->GetAccessToken = client_failsafe_get_access_token;
2079 if (PubSub_SubscribeTerminate(context->pubSub, xf_TerminateEventHandler) < 0)
2082 if (PubSub_SubscribeZoomingChange(context->pubSub, xf_ZoomingChangeEventHandler) < 0)
2084 if (PubSub_SubscribePanningChange(context->pubSub, xf_PanningChangeEventHandler) < 0)
2087 xfc->log = WLog_Get(TAG);
2092static void xfreerdp_client_free(WINPR_ATTR_UNUSED freerdp* instance, rdpContext* context)
2097 if (context->pubSub)
2099 PubSub_UnsubscribeTerminate(context->pubSub, xf_TerminateEventHandler);
2101 PubSub_UnsubscribeZoomingChange(context->pubSub, xf_ZoomingChangeEventHandler);
2102 PubSub_UnsubscribePanningChange(context->pubSub, xf_PanningChangeEventHandler);
2107int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
2109 pEntryPoints->Version = 1;
2110 pEntryPoints->Size =
sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
2111 pEntryPoints->GlobalInit = xfreerdp_client_global_init;
2112 pEntryPoints->GlobalUninit = xfreerdp_client_global_uninit;
2113 pEntryPoints->ContextSize =
sizeof(xfContext);
2114 pEntryPoints->ClientNew = xfreerdp_client_new;
2115 pEntryPoints->ClientFree = xfreerdp_client_free;
2116 pEntryPoints->ClientStart = xfreerdp_client_start;
2117 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.