FreeRDP
Loading...
Searching...
No Matches
xf_client.c
1
25#include <freerdp/config.h>
26
27#include <math.h>
28#include <winpr/cast.h>
29#include <winpr/assert.h>
30#include <winpr/sspicli.h>
31
32#include <float.h>
33
34#include <X11/Xlib.h>
35#include <X11/Xutil.h>
36#include <X11/Xatom.h>
37
38#ifdef WITH_XRENDER
39#include <X11/extensions/Xrender.h>
40#endif
41
42#ifdef WITH_XI
43#include <X11/extensions/XInput.h>
44#include <X11/extensions/XInput2.h>
45#endif
46
47#ifdef WITH_XCURSOR
48#include <X11/Xcursor/Xcursor.h>
49#endif
50
51#ifdef WITH_XINERAMA
52#include <X11/extensions/Xinerama.h>
53#endif
54
55#include <X11/XKBlib.h>
56
57#include <errno.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <locale.h>
61#include <unistd.h>
62#include <string.h>
63#include <termios.h>
64#include <pthread.h>
65#include <sys/wait.h>
66#include <sys/types.h>
67#include <sys/select.h>
68#include <fcntl.h>
69#include <sys/stat.h>
70
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>
77
78#include <freerdp/utils/signal.h>
79#include <freerdp/utils/passphrase.h>
80#include <freerdp/client/cliprdr.h>
81#include <freerdp/client/channels.h>
82
83#include <freerdp/client/file.h>
84#include <freerdp/client/cmdline.h>
85
86#include <winpr/crt.h>
87#include <winpr/synch.h>
88#include <winpr/file.h>
89#include <winpr/print.h>
90#include <winpr/sysinfo.h>
91
92#include "xf_rail.h"
93#if defined(CHANNEL_TSMF_CLIENT)
94#include "xf_tsmf.h"
95#endif
96#include "xf_event.h"
97#include "xf_input.h"
98#include "xf_cliprdr.h"
99#include "xf_disp.h"
100#include "xf_video.h"
101#include "xf_monitor.h"
102#include "xf_graphics.h"
103#include "xf_keyboard.h"
104#include "xf_channels.h"
105#include "xf_client.h"
106#include "xfreerdp.h"
107#include "xf_utils.h"
108
109#include <freerdp/log.h>
110#define TAG CLIENT_TAG("x11")
111
112#define MIN_PIXEL_DIFF 0.001
113
114struct xf_exit_code_map_t
115{
116 DWORD error;
117 int rc;
118};
119static const struct xf_exit_code_map_t xf_exit_code_map[] = {
120 { FREERDP_ERROR_SUCCESS, XF_EXIT_SUCCESS },
121 { FREERDP_ERROR_AUTHENTICATION_FAILED, XF_EXIT_AUTH_FAILURE },
122 { FREERDP_ERROR_SECURITY_NEGO_CONNECT_FAILED, XF_EXIT_NEGO_FAILURE },
123 { FREERDP_ERROR_CONNECT_LOGON_FAILURE, XF_EXIT_LOGON_FAILURE },
124 { FREERDP_ERROR_CONNECT_ACCOUNT_LOCKED_OUT, XF_EXIT_ACCOUNT_LOCKED_OUT },
125 { FREERDP_ERROR_PRE_CONNECT_FAILED, XF_EXIT_PRE_CONNECT_FAILED },
126 { FREERDP_ERROR_CONNECT_UNDEFINED, XF_EXIT_CONNECT_UNDEFINED },
127 { FREERDP_ERROR_POST_CONNECT_FAILED, XF_EXIT_POST_CONNECT_FAILED },
128 { FREERDP_ERROR_DNS_ERROR, XF_EXIT_DNS_ERROR },
129 { FREERDP_ERROR_DNS_NAME_NOT_FOUND, XF_EXIT_DNS_NAME_NOT_FOUND },
130 { FREERDP_ERROR_CONNECT_FAILED, XF_EXIT_CONNECT_FAILED },
131 { FREERDP_ERROR_MCS_CONNECT_INITIAL_ERROR, XF_EXIT_MCS_CONNECT_INITIAL_ERROR },
132 { FREERDP_ERROR_TLS_CONNECT_FAILED, XF_EXIT_TLS_CONNECT_FAILED },
133 { FREERDP_ERROR_INSUFFICIENT_PRIVILEGES, XF_EXIT_INSUFFICIENT_PRIVILEGES },
134 { FREERDP_ERROR_CONNECT_CANCELLED, XF_EXIT_CONNECT_CANCELLED },
135 { FREERDP_ERROR_CONNECT_TRANSPORT_FAILED, XF_EXIT_CONNECT_TRANSPORT_FAILED },
136 { FREERDP_ERROR_CONNECT_PASSWORD_EXPIRED, XF_EXIT_CONNECT_PASSWORD_EXPIRED },
137 { FREERDP_ERROR_CONNECT_PASSWORD_MUST_CHANGE, XF_EXIT_CONNECT_PASSWORD_MUST_CHANGE },
138 { FREERDP_ERROR_CONNECT_KDC_UNREACHABLE, XF_EXIT_CONNECT_KDC_UNREACHABLE },
139 { FREERDP_ERROR_CONNECT_ACCOUNT_DISABLED, XF_EXIT_CONNECT_ACCOUNT_DISABLED },
140 { FREERDP_ERROR_CONNECT_PASSWORD_CERTAINLY_EXPIRED,
141 XF_EXIT_CONNECT_PASSWORD_CERTAINLY_EXPIRED },
142 { FREERDP_ERROR_CONNECT_CLIENT_REVOKED, XF_EXIT_CONNECT_CLIENT_REVOKED },
143 { FREERDP_ERROR_CONNECT_WRONG_PASSWORD, XF_EXIT_CONNECT_WRONG_PASSWORD },
144 { FREERDP_ERROR_CONNECT_ACCESS_DENIED, XF_EXIT_CONNECT_ACCESS_DENIED },
145 { FREERDP_ERROR_CONNECT_ACCOUNT_RESTRICTION, XF_EXIT_CONNECT_ACCOUNT_RESTRICTION },
146 { FREERDP_ERROR_CONNECT_ACCOUNT_EXPIRED, XF_EXIT_CONNECT_ACCOUNT_EXPIRED },
147 { FREERDP_ERROR_CONNECT_LOGON_TYPE_NOT_GRANTED, XF_EXIT_CONNECT_LOGON_TYPE_NOT_GRANTED },
148 { FREERDP_ERROR_CONNECT_NO_OR_MISSING_CREDENTIALS, XF_EXIT_CONNECT_NO_OR_MISSING_CREDENTIALS }
149};
150
151static BOOL xf_setup_x11(xfContext* xfc);
152static void xf_teardown_x11(xfContext* xfc);
153
154static int xf_map_error_to_exit_code(DWORD error)
155{
156 for (size_t x = 0; x < ARRAYSIZE(xf_exit_code_map); x++)
157 {
158 const struct xf_exit_code_map_t* cur = &xf_exit_code_map[x];
159 if (cur->error == error)
160 return cur->rc;
161 }
162
163 return XF_EXIT_CONN_FAILED;
164}
165
166static int (*def_error_handler)(Display*, XErrorEvent*);
167static int xf_error_handler_ex(Display* d, XErrorEvent* ev);
168static void xf_check_extensions(xfContext* context);
169static void xf_window_free(xfContext* xfc);
170static BOOL xf_get_pixmap_info(xfContext* xfc);
171
172#ifdef WITH_XRENDER
173static void xf_draw_screen_scaled(xfContext* xfc, int x, int y, int w, int h)
174{
175 XTransform transform = { 0 };
176 Picture windowPicture = 0;
177 Picture primaryPicture = 0;
178 XRenderPictureAttributes pa;
179 XRenderPictFormat* picFormat = NULL;
180 int x2 = 0;
181 int y2 = 0;
182 const char* filter = NULL;
183 WINPR_ASSERT(xfc);
184
185 rdpSettings* settings = xfc->common.context.settings;
186 WINPR_ASSERT(settings);
187
188 if (xfc->scaledWidth <= 0 || xfc->scaledHeight <= 0)
189 {
190 WLog_ERR(TAG, "the current window dimensions are invalid");
191 return;
192 }
193
194 if (freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) <= 0 ||
195 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight) <= 0)
196 {
197 WLog_ERR(TAG, "the window dimensions are invalid");
198 return;
199 }
200
201 const double xScalingFactor = 1.0 *
202 freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) /
203 (double)xfc->scaledWidth;
204 const double yScalingFactor = 1.0 *
205 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight) /
206 (double)xfc->scaledHeight;
207 XSetFillStyle(xfc->display, xfc->gc, FillSolid);
208 XSetForeground(xfc->display, xfc->gc, 0);
209 /* Black out possible space between desktop and window borders */
210 {
211 XRectangle box1 = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, xfc->window->width),
212 WINPR_ASSERTING_INT_CAST(UINT16, xfc->window->height) };
213 XRectangle box2 = { WINPR_ASSERTING_INT_CAST(INT16, xfc->offset_x),
214 WINPR_ASSERTING_INT_CAST(INT16, xfc->offset_y),
215 WINPR_ASSERTING_INT_CAST(UINT16, xfc->scaledWidth),
216 WINPR_ASSERTING_INT_CAST(UINT16, xfc->scaledHeight) };
217 Region reg1 = XCreateRegion();
218 Region reg2 = XCreateRegion();
219 XUnionRectWithRegion(&box1, reg1, reg1);
220 XUnionRectWithRegion(&box2, reg2, reg2);
221
222 if (XSubtractRegion(reg1, reg2, reg1) && !XEmptyRegion(reg1))
223 {
224 XSetRegion(xfc->display, xfc->gc, reg1);
225 XFillRectangle(xfc->display, xfc->window->handle, xfc->gc, 0, 0,
226 WINPR_ASSERTING_INT_CAST(UINT16, xfc->window->width),
227 WINPR_ASSERTING_INT_CAST(UINT16, xfc->window->height));
228 XSetClipMask(xfc->display, xfc->gc, None);
229 }
230
231 XDestroyRegion(reg1);
232 XDestroyRegion(reg2);
233 }
234 picFormat = XRenderFindVisualFormat(xfc->display, xfc->visual);
235 pa.subwindow_mode = IncludeInferiors;
236 primaryPicture =
237 XRenderCreatePicture(xfc->display, xfc->primary, picFormat, CPSubwindowMode, &pa);
238 windowPicture =
239 XRenderCreatePicture(xfc->display, xfc->window->handle, picFormat, CPSubwindowMode, &pa);
240 /* avoid blurry filter when scaling factor is 2x, 3x, etc
241 * useful when the client has high-dpi monitor */
242 filter = FilterBilinear;
243 if (fabs(xScalingFactor - yScalingFactor) < MIN_PIXEL_DIFF)
244 {
245 const double inverseX = 1.0 / xScalingFactor;
246 const double inverseRoundedX = round(inverseX);
247 const double absInverse = fabs(inverseX - inverseRoundedX);
248
249 if (absInverse < MIN_PIXEL_DIFF)
250 filter = FilterNearest;
251 }
252 XRenderSetPictureFilter(xfc->display, primaryPicture, filter, 0, 0);
253 transform.matrix[0][0] = XDoubleToFixed(xScalingFactor);
254 transform.matrix[0][1] = XDoubleToFixed(0.0);
255 transform.matrix[0][2] = XDoubleToFixed(0.0);
256 transform.matrix[1][0] = XDoubleToFixed(0.0);
257 transform.matrix[1][1] = XDoubleToFixed(yScalingFactor);
258 transform.matrix[1][2] = XDoubleToFixed(0.0);
259 transform.matrix[2][0] = XDoubleToFixed(0.0);
260 transform.matrix[2][1] = XDoubleToFixed(0.0);
261 transform.matrix[2][2] = XDoubleToFixed(1.0);
262 /* calculate and fix up scaled coordinates */
263 x2 = x + w;
264 y2 = y + h;
265
266 const double dx1 = floor(x / xScalingFactor);
267 const double dy1 = floor(y / yScalingFactor);
268 const double dx2 = ceil(x2 / xScalingFactor);
269 const double dy2 = ceil(y2 / yScalingFactor);
270 x = ((int)dx1) - 1;
271 y = ((int)dy1) - 1;
272 w = ((int)dx2) + 1 - x;
273 h = ((int)dy2) + 1 - y;
274 XRenderSetPictureTransform(xfc->display, primaryPicture, &transform);
275 XRenderComposite(xfc->display, PictOpSrc, primaryPicture, 0, windowPicture, x, y, 0, 0,
276 xfc->offset_x + x, xfc->offset_y + y, WINPR_ASSERTING_INT_CAST(uint32_t, w),
277 WINPR_ASSERTING_INT_CAST(uint32_t, h));
278 XRenderFreePicture(xfc->display, primaryPicture);
279 XRenderFreePicture(xfc->display, windowPicture);
280}
281
282BOOL xf_picture_transform_required(xfContext* xfc)
283{
284 rdpSettings* settings = NULL;
285
286 WINPR_ASSERT(xfc);
287
288 settings = xfc->common.context.settings;
289 WINPR_ASSERT(settings);
290
291 if ((xfc->offset_x != 0) || (xfc->offset_y != 0) ||
292 (xfc->scaledWidth != (INT64)freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth)) ||
293 (xfc->scaledHeight != (INT64)freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight)))
294 {
295 return TRUE;
296 }
297
298 return FALSE;
299}
300#endif /* WITH_XRENDER defined */
301
302void xf_draw_screen_(xfContext* xfc, int x, int y, int w, int h, const char* fkt,
303 WINPR_ATTR_UNUSED const char* file, WINPR_ATTR_UNUSED int line)
304{
305 if (!xfc)
306 {
307 WLog_DBG(TAG, "called from [%s] xfc=%p", fkt, xfc);
308 return;
309 }
310
311 if (w == 0 || h == 0)
312 {
313 WLog_WARN(TAG, "invalid width and/or height specified: w=%d h=%d", w, h);
314 return;
315 }
316
317 if (!xfc->window)
318 {
319 WLog_WARN(TAG, "invalid xfc->window=%p", xfc->window);
320 return;
321 }
322
323#ifdef WITH_XRENDER
324
325 if (xf_picture_transform_required(xfc))
326 {
327 xf_draw_screen_scaled(xfc, x, y, w, h);
328 return;
329 }
330
331#endif
332 XCopyArea(xfc->display, xfc->primary, xfc->window->handle, xfc->gc, x, y,
333 WINPR_ASSERTING_INT_CAST(uint32_t, w), WINPR_ASSERTING_INT_CAST(uint32_t, h), x, y);
334}
335
336static BOOL xf_desktop_resize(rdpContext* context)
337{
338 rdpSettings* settings = NULL;
339 xfContext* xfc = (xfContext*)context;
340
341 WINPR_ASSERT(xfc);
342
343 settings = context->settings;
344 WINPR_ASSERT(settings);
345
346 if (xfc->primary)
347 {
348 BOOL same = (xfc->primary == xfc->drawing) ? TRUE : FALSE;
349 XFreePixmap(xfc->display, xfc->primary);
350
351 WINPR_ASSERT(xfc->depth != 0);
352 if (!(xfc->primary =
353 XCreatePixmap(xfc->display, xfc->drawable,
354 freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
355 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight),
356 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth))))
357 return FALSE;
358
359 if (same)
360 xfc->drawing = xfc->primary;
361 }
362
363#ifdef WITH_XRENDER
364
365 if (!freerdp_settings_get_bool(settings, FreeRDP_SmartSizing))
366 {
367 xfc->scaledWidth = WINPR_ASSERTING_INT_CAST(
368 int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth));
369 xfc->scaledHeight = WINPR_ASSERTING_INT_CAST(
370 int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
371 }
372
373#endif
374
375 if (!xfc->fullscreen)
376 {
377 xf_ResizeDesktopWindow(
378 xfc, xfc->window,
379 WINPR_ASSERTING_INT_CAST(int,
380 freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth)),
381 WINPR_ASSERTING_INT_CAST(int,
382 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight)));
383 }
384 else
385 {
386#ifdef WITH_XRENDER
387
388 if (!freerdp_settings_get_bool(settings, FreeRDP_SmartSizing))
389#endif
390 {
391 /* Update the saved width and height values the window will be
392 * resized to when toggling out of fullscreen */
393 xfc->savedWidth = WINPR_ASSERTING_INT_CAST(
394 int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth));
395 xfc->savedHeight = WINPR_ASSERTING_INT_CAST(
396 int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
397 }
398
399 XSetFunction(xfc->display, xfc->gc, GXcopy);
400 XSetFillStyle(xfc->display, xfc->gc, FillSolid);
401 XSetForeground(xfc->display, xfc->gc, 0);
402 XFillRectangle(xfc->display, xfc->drawable, xfc->gc, 0, 0,
403 freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
404 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
405 }
406
407 return TRUE;
408}
409
410static BOOL xf_paint(xfContext* xfc, const GDI_RGN* region)
411{
412 WINPR_ASSERT(xfc);
413 WINPR_ASSERT(region);
414
415 if (xfc->remote_app)
416 {
417 const RECTANGLE_16 rect = { .left = WINPR_ASSERTING_INT_CAST(UINT16, region->x),
418 .top = WINPR_ASSERTING_INT_CAST(UINT16, region->y),
419 .right =
420 WINPR_ASSERTING_INT_CAST(UINT16, region->x + region->w),
421 .bottom =
422 WINPR_ASSERTING_INT_CAST(UINT16, region->y + region->h) };
423 xf_rail_paint(xfc, &rect);
424 }
425 else
426 {
427 XPutImage(xfc->display, xfc->primary, xfc->gc, xfc->image, region->x, region->y, region->x,
428 region->y, 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);
431 }
432 return TRUE;
433}
434
435static BOOL xf_end_paint(rdpContext* context)
436{
437 xfContext* xfc = (xfContext*)context;
438 rdpGdi* gdi = context->gdi;
439
440 if (gdi->suppressOutput)
441 return TRUE;
442
443 HGDI_DC hdc = gdi->primary->hdc;
444
445 if (!xfc->complex_regions)
446 {
447 const GDI_RGN* rgn = hdc->hwnd->invalid;
448 if (rgn->null)
449 return TRUE;
450 xf_lock_x11(xfc);
451 if (!xf_paint(xfc, rgn))
452 return FALSE;
453 xf_unlock_x11(xfc);
454 }
455 else
456 {
457 const INT32 ninvalid = hdc->hwnd->ninvalid;
458 const GDI_RGN* cinvalid = hdc->hwnd->cinvalid;
459
460 if (hdc->hwnd->ninvalid < 1)
461 return TRUE;
462
463 xf_lock_x11(xfc);
464
465 for (INT32 i = 0; i < ninvalid; i++)
466 {
467 const GDI_RGN* rgn = &cinvalid[i];
468 if (!xf_paint(xfc, rgn))
469 return FALSE;
470 }
471
472 XFlush(xfc->display);
473 xf_unlock_x11(xfc);
474 }
475
476 hdc->hwnd->invalid->null = TRUE;
477 hdc->hwnd->ninvalid = 0;
478 return TRUE;
479}
480
481static BOOL xf_sw_desktop_resize(rdpContext* context)
482{
483 rdpGdi* gdi = context->gdi;
484 xfContext* xfc = (xfContext*)context;
485 rdpSettings* settings = context->settings;
486 BOOL ret = FALSE;
487
488 if (!gdi_resize(gdi, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
489 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight)))
490 return FALSE;
491
492 /* Do not lock during gdi_resize, there might still be drawing operations in progress.
493 * locking will deadlock. */
494 xf_lock_x11(xfc);
495 if (xfc->image)
496 {
497 xfc->image->data = NULL;
498 XDestroyImage(xfc->image);
499 }
500
501 WINPR_ASSERT(xfc->depth != 0);
502 if (!(xfc->image = XCreateImage(
503 xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth), ZPixmap, 0,
504 (char*)gdi->primary_buffer, WINPR_ASSERTING_INT_CAST(uint32_t, gdi->width),
505 WINPR_ASSERTING_INT_CAST(uint32_t, gdi->height), xfc->scanline_pad,
506 WINPR_ASSERTING_INT_CAST(int, gdi->stride))))
507 {
508 goto out;
509 }
510
511 xfc->image->byte_order = LSBFirst;
512 xfc->image->bitmap_bit_order = LSBFirst;
513 ret = xf_desktop_resize(context);
514out:
515 xf_unlock_x11(xfc);
516 return ret;
517}
518
519static BOOL xf_process_x_events(freerdp* instance)
520{
521 BOOL status = TRUE;
522 int pending_status = 1;
523 xfContext* xfc = (xfContext*)instance->context;
524
525 while (pending_status)
526 {
527 xf_lock_x11(xfc);
528 pending_status = XPending(xfc->display);
529
530 if (pending_status)
531 {
532 XEvent xevent = { 0 };
533
534 XNextEvent(xfc->display, &xevent);
535 status = xf_event_process(instance, &xevent);
536 }
537 xf_unlock_x11(xfc);
538 if (!status)
539 break;
540 }
541
542 return status;
543}
544
545static char* xf_window_get_title(rdpSettings* settings)
546{
547 BOOL port = 0;
548 char* windowTitle = NULL;
549 size_t size = 0;
550 const char* prefix = "FreeRDP:";
551
552 if (!settings)
553 return NULL;
554
555 const char* name = freerdp_settings_get_string(settings, FreeRDP_ServerHostname);
556 const char* title = freerdp_settings_get_string(settings, FreeRDP_WindowTitle);
557
558 if (title)
559 return _strdup(title);
560
561 port = (freerdp_settings_get_uint32(settings, FreeRDP_ServerPort) != 3389);
562 /* Just assume a window title is never longer than a filename... */
563 size = strnlen(name, MAX_PATH) + 16;
564 windowTitle = calloc(size, sizeof(char));
565
566 if (!windowTitle)
567 return NULL;
568
569 if (!port)
570 (void)sprintf_s(windowTitle, size, "%s %s", prefix, name);
571 else
572 (void)sprintf_s(windowTitle, size, "%s %s:%" PRIu32, prefix, name,
573 freerdp_settings_get_uint32(settings, FreeRDP_ServerPort));
574
575 return windowTitle;
576}
577
578BOOL xf_create_window(xfContext* xfc)
579{
580 XGCValues gcv = { 0 };
581 XEvent xevent = { 0 };
582 char* windowTitle = NULL;
583
584 WINPR_ASSERT(xfc);
585 rdpSettings* settings = xfc->common.context.settings;
586 WINPR_ASSERT(settings);
587
588 int width =
589 WINPR_ASSERTING_INT_CAST(int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth));
590 int height =
591 WINPR_ASSERTING_INT_CAST(int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
592
593 const XSetWindowAttributes empty = { 0 };
594 xfc->attribs = empty;
595
596 if (xfc->remote_app)
597 xfc->depth = 32;
598 else
599 xfc->depth = DefaultDepthOfScreen(xfc->screen);
600
601 XVisualInfo vinfo = { 0 };
602 if (XMatchVisualInfo(xfc->display, xfc->screen_number, xfc->depth, TrueColor, &vinfo))
603 {
604 Window root = XDefaultRootWindow(xfc->display);
605 xfc->visual = vinfo.visual;
606 xfc->attribs.colormap = xfc->colormap =
607 XCreateColormap(xfc->display, root, vinfo.visual, AllocNone);
608 }
609 else
610 {
611 if (xfc->remote_app)
612 {
613 WLog_WARN(TAG, "running in remote app mode, but XServer does not support transparency");
614 WLog_WARN(TAG, "display of remote applications might be distorted (black frames, ...)");
615 }
616 xfc->depth = DefaultDepthOfScreen(xfc->screen);
617 xfc->visual = DefaultVisual(xfc->display, xfc->screen_number);
618 xfc->attribs.colormap = xfc->colormap = DefaultColormap(xfc->display, xfc->screen_number);
619 }
620
621 /*
622 * Detect if the server visual has an inverted colormap
623 * (BGR vs RGB, or red being the least significant byte)
624 */
625 if (vinfo.red_mask & 0xFF)
626 {
627 xfc->invert = FALSE;
628 }
629
630 if (!xfc->remote_app)
631 {
632 xfc->attribs.background_pixel = BlackPixelOfScreen(xfc->screen);
633 xfc->attribs.border_pixel = WhitePixelOfScreen(xfc->screen);
634 xfc->attribs.backing_store = xfc->primary ? NotUseful : Always;
635 xfc->attribs.override_redirect = False;
636
637 xfc->attribs.bit_gravity = NorthWestGravity;
638 xfc->attribs.win_gravity = NorthWestGravity;
639 xfc->attribs_mask = CWBackPixel | CWBackingStore | CWOverrideRedirect | CWColormap |
640 CWBorderPixel | CWWinGravity | CWBitGravity;
641
642#ifdef WITH_XRENDER
643 xfc->offset_x = 0;
644 xfc->offset_y = 0;
645#endif
646 windowTitle = xf_window_get_title(settings);
647
648 if (!windowTitle)
649 return FALSE;
650
651#ifdef WITH_XRENDER
652
653 if (freerdp_settings_get_bool(settings, FreeRDP_SmartSizing) && !xfc->fullscreen)
654 {
655 if (freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingWidth) > 0)
656 width = WINPR_ASSERTING_INT_CAST(
657 int, freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingWidth));
658
659 if (freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingHeight) > 0)
660 height = WINPR_ASSERTING_INT_CAST(
661 int, freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingHeight));
662
663 xfc->scaledWidth = width;
664 xfc->scaledHeight = height;
665 }
666
667#endif
668 xfc->window = xf_CreateDesktopWindow(xfc, windowTitle, width, height);
669 free(windowTitle);
670
671 if (xfc->fullscreen)
672 xf_SetWindowFullscreen(xfc, xfc->window, xfc->fullscreen);
673
674 xfc->unobscured = (xevent.xvisibility.state == VisibilityUnobscured);
675 XSetWMProtocols(xfc->display, xfc->window->handle, &(xfc->WM_DELETE_WINDOW), 1);
676 xfc->drawable = xfc->window->handle;
677 }
678 else
679 {
680 xfc->attribs.border_pixel = 0;
681 xfc->attribs.background_pixel = 0;
682 xfc->attribs.backing_store = xfc->primary ? NotUseful : Always;
683 xfc->attribs.override_redirect = False;
684
685 xfc->attribs.bit_gravity = NorthWestGravity;
686 xfc->attribs.win_gravity = NorthWestGravity;
687 xfc->attribs_mask = CWBackPixel | CWBackingStore | CWOverrideRedirect | CWColormap |
688 CWBorderPixel | CWWinGravity | CWBitGravity;
689
690 xfc->drawable = xf_CreateDummyWindow(xfc);
691 }
692
693 if (!xfc->gc)
694 xfc->gc = XCreateGC(xfc->display, xfc->drawable, GCGraphicsExposures, &gcv);
695
696 WINPR_ASSERT(xfc->depth != 0);
697 if (!xfc->primary)
698 xfc->primary = XCreatePixmap(xfc->display, xfc->drawable,
699 freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
700 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight),
701 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth));
702
703 xfc->drawing = xfc->primary;
704
705 if (!xfc->bitmap_mono)
706 xfc->bitmap_mono = XCreatePixmap(xfc->display, xfc->drawable, 8, 8, 1);
707
708 if (!xfc->gc_mono)
709 xfc->gc_mono = XCreateGC(xfc->display, xfc->bitmap_mono, GCGraphicsExposures, &gcv);
710
711 XSetFunction(xfc->display, xfc->gc, GXcopy);
712 XSetFillStyle(xfc->display, xfc->gc, FillSolid);
713 XSetForeground(xfc->display, xfc->gc, BlackPixelOfScreen(xfc->screen));
714 XFillRectangle(xfc->display, xfc->primary, xfc->gc, 0, 0,
715 freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
716 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
717 XFlush(xfc->display);
718
719 return TRUE;
720}
721
722BOOL xf_create_image(xfContext* xfc)
723{
724 WINPR_ASSERT(xfc);
725 if (!xfc->image)
726 {
727 const rdpSettings* settings = xfc->common.context.settings;
728 rdpGdi* cgdi = xfc->common.context.gdi;
729 WINPR_ASSERT(cgdi);
730
731 WINPR_ASSERT(xfc->depth != 0);
732 xfc->image =
733 XCreateImage(xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
734 ZPixmap, 0, (char*)cgdi->primary_buffer,
735 freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
736 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight),
737 xfc->scanline_pad, WINPR_ASSERTING_INT_CAST(int, cgdi->stride));
738 xfc->image->byte_order = LSBFirst;
739 xfc->image->bitmap_bit_order = LSBFirst;
740 }
741 return TRUE;
742}
743
744static void xf_window_free(xfContext* xfc)
745{
746 if (xfc->window)
747 {
748 xf_DestroyDesktopWindow(xfc, xfc->window);
749 xfc->window = NULL;
750 }
751
752#if defined(CHANNEL_TSMF_CLIENT)
753 if (xfc->xv_context)
754 {
755 xf_tsmf_uninit(xfc, NULL);
756 xfc->xv_context = NULL;
757 }
758#endif
759
760 if (xfc->image)
761 {
762 xfc->image->data = NULL;
763 XDestroyImage(xfc->image);
764 xfc->image = NULL;
765 }
766
767 if (xfc->bitmap_mono)
768 {
769 XFreePixmap(xfc->display, xfc->bitmap_mono);
770 xfc->bitmap_mono = 0;
771 }
772
773 if (xfc->gc_mono)
774 {
775 XFreeGC(xfc->display, xfc->gc_mono);
776 xfc->gc_mono = 0;
777 }
778
779 if (xfc->primary)
780 {
781 XFreePixmap(xfc->display, xfc->primary);
782 xfc->primary = 0;
783 }
784
785 if (xfc->gc)
786 {
787 XFreeGC(xfc->display, xfc->gc);
788 xfc->gc = 0;
789 }
790}
791
792void xf_toggle_fullscreen(xfContext* xfc)
793{
794 WindowStateChangeEventArgs e = { 0 };
795 rdpContext* context = (rdpContext*)xfc;
796 rdpSettings* settings = context->settings;
797
798 /*
799 when debugging, ungrab keyboard when toggling fullscreen
800 to allow keyboard usage on the debugger
801 */
802 if (xfc->debug)
803 xf_ungrab(xfc);
804
805 xfc->fullscreen = (xfc->fullscreen) ? FALSE : TRUE;
806 xfc->decorations =
807 (xfc->fullscreen) ? FALSE : freerdp_settings_get_bool(settings, FreeRDP_Decorations);
808 xf_SetWindowFullscreen(xfc, xfc->window, xfc->fullscreen);
809 EventArgsInit(&e, "xfreerdp");
810 e.state = xfc->fullscreen ? FREERDP_WINDOW_STATE_FULLSCREEN : 0;
811 PubSub_OnWindowStateChange(context->pubSub, context, &e);
812}
813
814void xf_minimize(xfContext* xfc)
815{
816 WindowStateChangeEventArgs e = { 0 };
817 rdpContext* context = (rdpContext*)xfc;
818 WINPR_ASSERT(context);
819
820 /*
821 when debugging, ungrab keyboard when toggling fullscreen
822 to allow keyboard usage on the debugger
823 */
824 if (xfc->debug)
825 xf_ungrab(xfc);
826
827 xf_SetWindowMinimized(xfc, xfc->window);
828
829 e.state = xfc->fullscreen ? FREERDP_WINDOW_STATE_FULLSCREEN : 0;
830 PubSub_OnWindowStateChange(context->pubSub, context, &e);
831}
832
833void xf_lock_x11_(xfContext* xfc, WINPR_ATTR_UNUSED const char* fkt)
834{
835 if (!xfc->UseXThreads)
836 (void)WaitForSingleObject(xfc->mutex, INFINITE);
837 else
838 XLockDisplay(xfc->display);
839
840 xfc->locked++;
841}
842
843void xf_unlock_x11_(xfContext* xfc, WINPR_ATTR_UNUSED const char* fkt)
844{
845 if (xfc->locked == 0)
846 WLog_WARN(TAG, "X11: trying to unlock although not locked!");
847 else
848 xfc->locked--;
849
850 if (!xfc->UseXThreads)
851 (void)ReleaseMutex(xfc->mutex);
852 else
853 XUnlockDisplay(xfc->display);
854}
855
856static BOOL xf_get_pixmap_info(xfContext* xfc)
857{
858 int pf_count = 0;
859 XPixmapFormatValues* pfs = NULL;
860
861 WINPR_ASSERT(xfc->display);
862 pfs = XListPixmapFormats(xfc->display, &pf_count);
863
864 if (!pfs)
865 {
866 WLog_ERR(TAG, "XListPixmapFormats failed");
867 return 1;
868 }
869
870 WINPR_ASSERT(xfc->depth != 0);
871 for (int i = 0; i < pf_count; i++)
872 {
873 const XPixmapFormatValues* pf = &pfs[i];
874
875 if (pf->depth == xfc->depth)
876 {
877 xfc->scanline_pad = pf->scanline_pad;
878 break;
879 }
880 }
881
882 XFree(pfs);
883 if ((xfc->visual == NULL) || (xfc->scanline_pad == 0))
884 return FALSE;
885
886 return TRUE;
887}
888
889static int xf_error_handler(Display* d, XErrorEvent* ev)
890{
891 char buf[256] = { 0 };
892 XGetErrorText(d, ev->error_code, buf, sizeof(buf));
893 WLog_ERR(TAG, "%s", buf);
894 winpr_log_backtrace(TAG, WLOG_ERROR, 20);
895
896 if (def_error_handler)
897 return def_error_handler(d, ev);
898
899 return 0;
900}
901
902static int xf_error_handler_ex(Display* d, XErrorEvent* ev)
903{
904 /*
905 * ungrab the keyboard, in case a debugger is running in
906 * another window. This make xf_error_handler() a potential
907 * debugger breakpoint.
908 */
909
910 XUngrabKeyboard(d, CurrentTime);
911 XUngrabPointer(d, CurrentTime);
912 return xf_error_handler(d, ev);
913}
914
915static BOOL xf_play_sound(rdpContext* context, const PLAY_SOUND_UPDATE* play_sound)
916{
917 xfContext* xfc = (xfContext*)context;
918 WINPR_UNUSED(play_sound);
919 XkbBell(xfc->display, None, 100, 0);
920 return TRUE;
921}
922
923static void xf_check_extensions(xfContext* context)
924{
925 int xkb_opcode = 0;
926 int xkb_event = 0;
927 int xkb_error = 0;
928 int xkb_major = XkbMajorVersion;
929 int xkb_minor = XkbMinorVersion;
930
931 if (XkbLibraryVersion(&xkb_major, &xkb_minor) &&
932 XkbQueryExtension(context->display, &xkb_opcode, &xkb_event, &xkb_error, &xkb_major,
933 &xkb_minor))
934 {
935 context->xkbAvailable = TRUE;
936 }
937
938#ifdef WITH_XRENDER
939 {
940 int xrender_event_base = 0;
941 int xrender_error_base = 0;
942
943 if (XRenderQueryExtension(context->display, &xrender_event_base, &xrender_error_base))
944 {
945 context->xrenderAvailable = TRUE;
946 }
947 }
948#endif
949}
950
951#ifdef WITH_XI
952/* Input device which does NOT have the correct mapping. We must disregard */
953/* this device when trying to find the input device which is the pointer. */
954static const char TEST_PTR_STR[] = "Virtual core XTEST pointer";
955static const size_t TEST_PTR_LEN = sizeof(TEST_PTR_STR) / sizeof(char);
956#endif /* WITH_XI */
957
958static void xf_get_x11_button_map(xfContext* xfc, unsigned char* x11_map)
959{
960#ifdef WITH_XI
961 int opcode = 0;
962 int event = 0;
963 int error = 0;
964 XDevice* ptr_dev = NULL;
965 XExtensionVersion* version = NULL;
966 XDeviceInfo* devices1 = NULL;
967 XIDeviceInfo* devices2 = NULL;
968 int num_devices = 0;
969
970 if (XQueryExtension(xfc->display, "XInputExtension", &opcode, &event, &error))
971 {
972 WLog_DBG(TAG, "Searching for XInput pointer device");
973 ptr_dev = NULL;
974 /* loop through every device, looking for a pointer */
975 version = XGetExtensionVersion(xfc->display, INAME);
976
977 if (version->major_version >= 2)
978 {
979 /* XID of pointer device using XInput version 2 */
980 devices2 = XIQueryDevice(xfc->display, XIAllDevices, &num_devices);
981
982 if (devices2)
983 {
984 for (int i = 0; i < num_devices; ++i)
985 {
986 XIDeviceInfo* dev = &devices2[i];
987 if ((dev->use == XISlavePointer) &&
988 (strncmp(dev->name, TEST_PTR_STR, TEST_PTR_LEN) != 0))
989 {
990 ptr_dev = XOpenDevice(xfc->display,
991 WINPR_ASSERTING_INT_CAST(uint32_t, dev->deviceid));
992 if (ptr_dev)
993 break;
994 }
995 }
996
997 XIFreeDeviceInfo(devices2);
998 }
999 }
1000 else
1001 {
1002 /* XID of pointer device using XInput version 1 */
1003 devices1 = XListInputDevices(xfc->display, &num_devices);
1004
1005 if (devices1)
1006 {
1007 for (int i = 0; i < num_devices; ++i)
1008 {
1009 if ((devices1[i].use == IsXExtensionPointer) &&
1010 (strncmp(devices1[i].name, TEST_PTR_STR, TEST_PTR_LEN) != 0))
1011 {
1012 ptr_dev = XOpenDevice(xfc->display, devices1[i].id);
1013 if (ptr_dev)
1014 break;
1015 }
1016 }
1017
1018 XFreeDeviceList(devices1);
1019 }
1020 }
1021
1022 XFree(version);
1023
1024 /* get button mapping from input extension if there is a pointer device; */
1025 /* otherwise leave unchanged. */
1026 if (ptr_dev)
1027 {
1028 WLog_DBG(TAG, "Pointer device: %d", ptr_dev->device_id);
1029 XGetDeviceButtonMapping(xfc->display, ptr_dev, x11_map, NUM_BUTTONS_MAPPED);
1030 XCloseDevice(xfc->display, ptr_dev);
1031 }
1032 else
1033 {
1034 WLog_DBG(TAG, "No pointer device found!");
1035 }
1036 }
1037 else
1038#endif /* WITH_XI */
1039 {
1040 WLog_DBG(TAG, "Get global pointer mapping (no XInput)");
1041 XGetPointerMapping(xfc->display, x11_map, NUM_BUTTONS_MAPPED);
1042 }
1043}
1044
1045/* Assignment of physical (not logical) mouse buttons to wire flags. */
1046/* Notice that the middle button is 2 in X11, but 3 in RDP. */
1047static const button_map xf_button_flags[NUM_BUTTONS_MAPPED] = {
1048 { Button1, PTR_FLAGS_BUTTON1 },
1049 { Button2, PTR_FLAGS_BUTTON3 },
1050 { Button3, PTR_FLAGS_BUTTON2 },
1051 { Button4, PTR_FLAGS_WHEEL | 0x78 },
1052 /* Negative value is 9bit twos complement */
1053 { Button5, PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | (0x100 - 0x78) },
1054 { 6, PTR_FLAGS_HWHEEL | PTR_FLAGS_WHEEL_NEGATIVE | (0x100 - 0x78) },
1055 { 7, PTR_FLAGS_HWHEEL | 0x78 },
1056 { 8, PTR_XFLAGS_BUTTON1 },
1057 { 9, PTR_XFLAGS_BUTTON2 },
1058 { 97, PTR_XFLAGS_BUTTON1 },
1059 { 112, PTR_XFLAGS_BUTTON2 }
1060};
1061
1062static UINT16 get_flags_for_button(size_t button)
1063{
1064 for (size_t x = 0; x < ARRAYSIZE(xf_button_flags); x++)
1065 {
1066 const button_map* map = &xf_button_flags[x];
1067
1068 if (map->button == button)
1069 return map->flags;
1070 }
1071
1072 return 0;
1073}
1074
1075void xf_button_map_init(xfContext* xfc)
1076{
1077 size_t pos = 0;
1078 /* loop counter for array initialization */
1079
1080 /* logical mouse button which is used for each physical mouse */
1081 /* button (indexed from zero). This is the default map. */
1082 unsigned char x11_map[112] = { 0 };
1083
1084 WINPR_ASSERT(xfc);
1085 WINPR_ASSERT(xfc->common.context.settings);
1086
1087 x11_map[0] = Button1;
1088 x11_map[1] = Button2;
1089 x11_map[2] = Button3;
1090 x11_map[3] = Button4;
1091 x11_map[4] = Button5;
1092 x11_map[5] = 6;
1093 x11_map[6] = 7;
1094 x11_map[7] = 8;
1095 x11_map[8] = 9;
1096 x11_map[96] = 97;
1097 x11_map[111] = 112;
1098
1099 /* query system for actual remapping */
1100 if (freerdp_settings_get_bool(xfc->common.context.settings, FreeRDP_UnmapButtons))
1101 {
1102 xf_get_x11_button_map(xfc, x11_map);
1103 }
1104
1105 /* iterate over all (mapped) physical buttons; for each of them */
1106 /* find the logical button in X11, and assign to this the */
1107 /* appropriate value to send over the RDP wire. */
1108 for (size_t physical = 0; physical < ARRAYSIZE(x11_map); ++physical)
1109 {
1110 const unsigned char logical = x11_map[physical];
1111 const UINT16 flags = get_flags_for_button(logical);
1112
1113 if ((logical != 0) && (flags != 0))
1114 {
1115 if (pos >= NUM_BUTTONS_MAPPED)
1116 {
1117 WLog_ERR(TAG, "Failed to map mouse button to RDP button, no space");
1118 }
1119 else
1120 {
1121 button_map* map = &xfc->button_map[pos++];
1122 map->button = logical;
1123 map->flags = get_flags_for_button(physical + Button1);
1124 }
1125 }
1126 }
1127}
1128
1140static BOOL xf_pre_connect(freerdp* instance)
1141{
1142 UINT32 maxWidth = 0;
1143 UINT32 maxHeight = 0;
1144
1145 WINPR_ASSERT(instance);
1146
1147 rdpContext* context = instance->context;
1148 WINPR_ASSERT(context);
1149 xfContext* xfc = (xfContext*)context;
1150
1151 rdpSettings* settings = context->settings;
1152 WINPR_ASSERT(settings);
1153
1154 if (!freerdp_settings_set_bool(settings, FreeRDP_CertificateCallbackPreferPEM, TRUE))
1155 return FALSE;
1156
1157 if (!freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
1158 {
1159 if (!xf_setup_x11(xfc))
1160 return FALSE;
1161 }
1162
1163 if (!freerdp_settings_set_uint32(settings, FreeRDP_OsMajorType, OSMAJORTYPE_UNIX))
1164 return FALSE;
1165 if (!freerdp_settings_set_uint32(settings, FreeRDP_OsMinorType, OSMINORTYPE_NATIVE_XSERVER))
1166 return FALSE;
1167 PubSub_SubscribeChannelConnected(context->pubSub, xf_OnChannelConnectedEventHandler);
1168 PubSub_SubscribeChannelDisconnected(context->pubSub, xf_OnChannelDisconnectedEventHandler);
1169
1170 if (!freerdp_settings_get_string(settings, FreeRDP_Username) &&
1171 !freerdp_settings_get_bool(settings, FreeRDP_CredentialsFromStdin) &&
1172 !freerdp_settings_get_bool(settings, FreeRDP_SmartcardLogon))
1173 {
1174 char login_name[MAX_PATH] = { 0 };
1175 ULONG size = sizeof(login_name) - 1;
1176
1177 if (GetUserNameExA(NameSamCompatible, login_name, &size))
1178 {
1179 if (!freerdp_settings_set_string(settings, FreeRDP_Username, login_name))
1180 return FALSE;
1181
1182 WLog_INFO(TAG, "No user name set. - Using login name: %s",
1183 freerdp_settings_get_string(settings, FreeRDP_Username));
1184 }
1185 }
1186
1187 if (freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
1188 {
1189 /* Check +auth-only has a username and password. */
1190 if (!freerdp_settings_get_string(settings, FreeRDP_Password))
1191 {
1192 WLog_INFO(TAG, "auth-only, but no password set. Please provide one.");
1193 return FALSE;
1194 }
1195
1196 WLog_INFO(TAG, "Authentication only. Don't connect to X.");
1197 }
1198
1199 if (!freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
1200 {
1201 const char* KeyboardRemappingList = freerdp_settings_get_string(
1202 xfc->common.context.settings, FreeRDP_KeyboardRemappingList);
1203
1204 xfc->remap_table = freerdp_keyboard_remap_string_to_list(KeyboardRemappingList);
1205 if (!xfc->remap_table)
1206 return FALSE;
1207 if (!xf_keyboard_init(xfc))
1208 return FALSE;
1209 if (!xf_keyboard_action_script_init(xfc))
1210 return FALSE;
1211 if (!xf_detect_monitors(xfc, &maxWidth, &maxHeight))
1212 return FALSE;
1213 }
1214
1215 if (maxWidth && maxHeight && !freerdp_settings_get_bool(settings, FreeRDP_SmartSizing))
1216 {
1217 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, maxWidth))
1218 return FALSE;
1219 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, maxHeight))
1220 return FALSE;
1221 }
1222
1223#ifdef WITH_XRENDER
1224
1229 if (freerdp_settings_get_bool(settings, FreeRDP_Fullscreen) &&
1230 freerdp_settings_get_bool(settings, FreeRDP_SmartSizing) &&
1231 (freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingWidth) > 0) &&
1232 (freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingHeight) > 0))
1233 {
1235 settings, FreeRDP_DesktopWidth,
1236 freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingWidth)))
1237 return FALSE;
1239 settings, FreeRDP_DesktopHeight,
1240 freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingHeight)))
1241 return FALSE;
1242 }
1243
1244#endif
1245 xfc->fullscreen = freerdp_settings_get_bool(settings, FreeRDP_Fullscreen);
1246 xfc->decorations = freerdp_settings_get_bool(settings, FreeRDP_Decorations);
1247 xfc->grab_keyboard = freerdp_settings_get_bool(settings, FreeRDP_GrabKeyboard);
1248 xfc->fullscreen_toggle = freerdp_settings_get_bool(settings, FreeRDP_ToggleFullscreen);
1249 if (!freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
1250 xf_button_map_init(xfc);
1251 return TRUE;
1252}
1253
1254static BOOL xf_inject_keypress(rdpContext* context, const char* buffer, size_t size)
1255{
1256 WCHAR wbuffer[64] = { 0 };
1257 const SSIZE_T len = ConvertUtf8NToWChar(buffer, size, wbuffer, ARRAYSIZE(wbuffer));
1258 if (len < 0)
1259 return FALSE;
1260
1261 rdpInput* input = context->input;
1262 WINPR_ASSERT(input);
1263
1264 for (SSIZE_T x = 0; x < len; x++)
1265 {
1266 const WCHAR code = wbuffer[x];
1267 freerdp_input_send_unicode_keyboard_event(input, 0, code);
1268 Sleep(5);
1269 freerdp_input_send_unicode_keyboard_event(input, KBD_FLAGS_RELEASE, code);
1270 Sleep(5);
1271 }
1272 return TRUE;
1273}
1274
1275static BOOL xf_process_pipe(rdpContext* context, const char* pipe)
1276{
1277 int fd = open(pipe, O_NONBLOCK | O_RDONLY);
1278 if (fd < 0)
1279 {
1280 char ebuffer[256] = { 0 };
1281 WLog_ERR(TAG, "pipe '%s' open returned %s [%d]", pipe,
1282 winpr_strerror(errno, ebuffer, sizeof(ebuffer)), errno);
1283 return FALSE;
1284 }
1285 while (!freerdp_shall_disconnect_context(context))
1286 {
1287 char buffer[64] = { 0 };
1288 ssize_t rd = read(fd, buffer, sizeof(buffer) - 1);
1289 if (rd == 0)
1290 {
1291 char ebuffer[256] = { 0 };
1292 if ((errno == EAGAIN) || (errno == 0))
1293 {
1294 Sleep(100);
1295 continue;
1296 }
1297
1298 // EOF, abort reading.
1299 WLog_ERR(TAG, "pipe '%s' read returned %s [%d]", pipe,
1300 winpr_strerror(errno, ebuffer, sizeof(ebuffer)), errno);
1301 break;
1302 }
1303 else if (rd < 0)
1304 {
1305 char ebuffer[256] = { 0 };
1306 WLog_ERR(TAG, "pipe '%s' read returned %s [%d]", pipe,
1307 winpr_strerror(errno, ebuffer, sizeof(ebuffer)), errno);
1308 break;
1309 }
1310 else
1311 {
1312 if (!xf_inject_keypress(context, buffer, WINPR_ASSERTING_INT_CAST(size_t, rd)))
1313 break;
1314 }
1315 }
1316 close(fd);
1317 return TRUE;
1318}
1319
1320static void cleanup_pipe(WINPR_ATTR_UNUSED int signum, WINPR_ATTR_UNUSED const char* signame,
1321 void* context)
1322{
1323 const char* pipe = context;
1324 if (!pipe)
1325 return;
1326 unlink(pipe);
1327}
1328
1329static DWORD WINAPI xf_handle_pipe(void* arg)
1330{
1331 xfContext* xfc = arg;
1332 WINPR_ASSERT(xfc);
1333
1334 rdpContext* context = &xfc->common.context;
1335 WINPR_ASSERT(context);
1336
1337 rdpSettings* settings = context->settings;
1338 WINPR_ASSERT(settings);
1339
1340 const char* pipe = freerdp_settings_get_string(settings, FreeRDP_KeyboardPipeName);
1341 WINPR_ASSERT(pipe);
1342
1343 const int rc = mkfifo(pipe, S_IWUSR | S_IRUSR);
1344 if (rc != 0)
1345 {
1346 char ebuffer[256] = { 0 };
1347 WLog_ERR(TAG, "Failed to create named pipe '%s': %s [%d]", pipe,
1348 winpr_strerror(errno, ebuffer, sizeof(ebuffer)), errno);
1349 return 0;
1350 }
1351
1352 void* ctx = WINPR_CAST_CONST_PTR_AWAY(pipe, void*);
1353 freerdp_add_signal_cleanup_handler(ctx, cleanup_pipe);
1354
1355 xf_process_pipe(context, pipe);
1356
1357 freerdp_del_signal_cleanup_handler(ctx, cleanup_pipe);
1358
1359 unlink(pipe);
1360 return 0;
1361}
1362
1368static BOOL xf_post_connect(freerdp* instance)
1369{
1370 ResizeWindowEventArgs e = { 0 };
1371
1372 WINPR_ASSERT(instance);
1373 xfContext* xfc = (xfContext*)instance->context;
1374 rdpContext* context = instance->context;
1375 WINPR_ASSERT(context);
1376
1377 rdpSettings* settings = context->settings;
1378 WINPR_ASSERT(settings);
1379
1380 rdpUpdate* update = context->update;
1381 WINPR_ASSERT(update);
1382
1383 if (freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode))
1384 xfc->remote_app = TRUE;
1385
1386 if (!xf_create_window(xfc))
1387 return FALSE;
1388
1389 if (!xf_get_pixmap_info(xfc))
1390 return FALSE;
1391
1392 if (!gdi_init(instance, xf_get_local_color_format(xfc, TRUE)))
1393 return FALSE;
1394
1395 if (!xf_create_image(xfc))
1396 return FALSE;
1397
1398 if (!xf_register_pointer(context->graphics))
1399 return FALSE;
1400
1401#ifdef WITH_XRENDER
1402 xfc->scaledWidth =
1403 WINPR_ASSERTING_INT_CAST(int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth));
1404 xfc->scaledHeight =
1405 WINPR_ASSERTING_INT_CAST(int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
1406 xfc->offset_x = 0;
1407 xfc->offset_y = 0;
1408#endif
1409
1410 if (!xfc->xrenderAvailable)
1411 {
1412 if (freerdp_settings_get_bool(settings, FreeRDP_SmartSizing))
1413 {
1414 WLog_ERR(TAG, "XRender not available: disabling smart-sizing");
1415 if (!freerdp_settings_set_bool(settings, FreeRDP_SmartSizing, FALSE))
1416 return FALSE;
1417 }
1418
1419 if (freerdp_settings_get_bool(settings, FreeRDP_MultiTouchGestures))
1420 {
1421 WLog_ERR(TAG, "XRender not available: disabling local multi-touch gestures");
1422 if (!freerdp_settings_set_bool(settings, FreeRDP_MultiTouchGestures, FALSE))
1423 return FALSE;
1424 }
1425 }
1426
1427 update->DesktopResize = xf_sw_desktop_resize;
1428 update->EndPaint = xf_end_paint;
1429 update->PlaySound = xf_play_sound;
1430 update->SetKeyboardIndicators = xf_keyboard_set_indicators;
1431 update->SetKeyboardImeStatus = xf_keyboard_set_ime_status;
1432
1433 const BOOL serverIsWindowsPlatform =
1434 (freerdp_settings_get_uint32(settings, FreeRDP_OsMajorType) == OSMAJORTYPE_WINDOWS);
1435 if (freerdp_settings_get_bool(settings, FreeRDP_RedirectClipboard) &&
1436 !(xfc->clipboard = xf_clipboard_new(xfc, !serverIsWindowsPlatform)))
1437 return FALSE;
1438
1439 if (!(xfc->xfDisp = xf_disp_new(xfc)))
1440 return FALSE;
1441
1442 const char* pipe = freerdp_settings_get_string(settings, FreeRDP_KeyboardPipeName);
1443 if (pipe)
1444 {
1445 xfc->pipethread = CreateThread(NULL, 0, xf_handle_pipe, xfc, 0, NULL);
1446 if (!xfc->pipethread)
1447 return FALSE;
1448 }
1449
1450 EventArgsInit(&e, "xfreerdp");
1451 e.width =
1452 WINPR_ASSERTING_INT_CAST(int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth));
1453 e.height =
1454 WINPR_ASSERTING_INT_CAST(int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
1455 PubSub_OnResizeWindow(context->pubSub, xfc, &e);
1456 return TRUE;
1457}
1458
1459static void xf_post_disconnect(freerdp* instance)
1460{
1461 xfContext* xfc = NULL;
1462 rdpContext* context = NULL;
1463
1464 if (!instance || !instance->context)
1465 return;
1466
1467 context = instance->context;
1468 xfc = (xfContext*)context;
1469 PubSub_UnsubscribeChannelConnected(instance->context->pubSub,
1470 xf_OnChannelConnectedEventHandler);
1471 PubSub_UnsubscribeChannelDisconnected(instance->context->pubSub,
1472 xf_OnChannelDisconnectedEventHandler);
1473 gdi_free(instance);
1474
1475 if (xfc->pipethread)
1476 {
1477 (void)WaitForSingleObject(xfc->pipethread, INFINITE);
1478 (void)CloseHandle(xfc->pipethread);
1479 xfc->pipethread = NULL;
1480 }
1481 if (xfc->clipboard)
1482 {
1483 xf_clipboard_free(xfc->clipboard);
1484 xfc->clipboard = NULL;
1485 }
1486
1487 if (xfc->xfDisp)
1488 {
1489 xf_disp_free(xfc->xfDisp);
1490 xfc->xfDisp = NULL;
1491 }
1492
1493 if ((xfc->window != NULL) && (xfc->drawable == xfc->window->handle))
1494 xfc->drawable = 0;
1495 else
1496 xf_DestroyDummyWindow(xfc, xfc->drawable);
1497
1498 freerdp_keyboard_remap_free(xfc->remap_table);
1499 xfc->remap_table = NULL;
1500
1501 xf_window_free(xfc);
1502}
1503
1504static void xf_post_final_disconnect(freerdp* instance)
1505{
1506 xfContext* xfc = NULL;
1507 rdpContext* context = NULL;
1508
1509 if (!instance || !instance->context)
1510 return;
1511
1512 context = instance->context;
1513 xfc = (xfContext*)context;
1514
1515 xf_keyboard_free(xfc);
1516 xf_teardown_x11(xfc);
1517}
1518
1519static int xf_logon_error_info(freerdp* instance, UINT32 data, UINT32 type)
1520{
1521 xfContext* xfc = (xfContext*)instance->context;
1522 const char* str_data = freerdp_get_logon_error_info_data(data);
1523 const char* str_type = freerdp_get_logon_error_info_type(type);
1524 WLog_INFO(TAG, "Logon Error Info %s [%s]", str_data, str_type);
1525 if (type != LOGON_MSG_SESSION_CONTINUE)
1526 {
1527 xf_rail_disable_remoteapp_mode(xfc);
1528 }
1529 return 1;
1530}
1531
1532static BOOL handle_window_events(freerdp* instance)
1533{
1534 if (!xf_process_x_events(instance))
1535 {
1536 WLog_DBG(TAG, "Closed from X11");
1537 return FALSE;
1538 }
1539
1540 return TRUE;
1541}
1542
1551static DWORD WINAPI xf_client_thread(LPVOID param)
1552{
1553 DWORD exit_code = 0;
1554 DWORD waitStatus = 0;
1555 HANDLE inputEvent = NULL;
1556 HANDLE timer = NULL;
1557 LARGE_INTEGER due = { 0 };
1558 TimerEventArgs timerEvent = { 0 };
1559
1560 EventArgsInit(&timerEvent, "xfreerdp");
1561 freerdp* instance = (freerdp*)param;
1562 WINPR_ASSERT(instance);
1563
1564 const BOOL status = freerdp_connect(instance);
1565 rdpContext* context = instance->context;
1566 WINPR_ASSERT(context);
1567 xfContext* xfc = (xfContext*)instance->context;
1568 WINPR_ASSERT(xfc);
1569
1570 rdpSettings* settings = context->settings;
1571 WINPR_ASSERT(settings);
1572
1573 if (!status)
1574 {
1575 UINT32 error = freerdp_get_last_error(instance->context);
1576 exit_code = (uint32_t)xf_map_error_to_exit_code(error);
1577 }
1578 else
1579 exit_code = XF_EXIT_SUCCESS;
1580
1581 if (!status)
1582 goto end;
1583
1584 /* --authonly ? */
1585 if (freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
1586 {
1587 WLog_ERR(TAG, "Authentication only, exit status %" PRId32 "", !status);
1588 goto disconnect;
1589 }
1590
1591 if (!status)
1592 {
1593 WLog_ERR(TAG, "Freerdp connect error exit status %" PRId32 "", !status);
1594 exit_code = freerdp_error_info(instance);
1595
1596 if (freerdp_get_last_error(instance->context) == FREERDP_ERROR_AUTHENTICATION_FAILED)
1597 exit_code = XF_EXIT_AUTH_FAILURE;
1598 else if (exit_code == ERRINFO_SUCCESS)
1599 exit_code = XF_EXIT_CONN_FAILED;
1600
1601 goto disconnect;
1602 }
1603
1604 timer = CreateWaitableTimerA(NULL, FALSE, "mainloop-periodic-timer");
1605
1606 if (!timer)
1607 {
1608 WLog_ERR(TAG, "failed to create timer");
1609 goto disconnect;
1610 }
1611
1612 due.QuadPart = 0;
1613
1614 if (!SetWaitableTimer(timer, &due, 20, NULL, NULL, FALSE))
1615 {
1616 goto disconnect;
1617 }
1618 inputEvent = xfc->x11event;
1619
1620 while (!freerdp_shall_disconnect_context(instance->context))
1621 {
1622 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = { 0 };
1623 DWORD nCount = 0;
1624 handles[nCount++] = timer;
1625 handles[nCount++] = inputEvent;
1626
1627 /*
1628 * win8 and server 2k12 seem to have some timing issue/race condition
1629 * when a initial sync request is send to sync the keyboard indicators
1630 * sending the sync event twice fixed this problem
1631 */
1632 if (freerdp_focus_required(instance))
1633 {
1634 xf_keyboard_focus_in(xfc);
1635 xf_keyboard_focus_in(xfc);
1636 }
1637
1638 {
1639 DWORD tmp =
1640 freerdp_get_event_handles(context, &handles[nCount], ARRAYSIZE(handles) - nCount);
1641
1642 if (tmp == 0)
1643 {
1644 WLog_ERR(TAG, "freerdp_get_event_handles failed");
1645 break;
1646 }
1647
1648 nCount += tmp;
1649 }
1650
1651 if (xfc->window)
1652 xf_floatbar_hide_and_show(xfc->window->floatbar);
1653
1654 waitStatus = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
1655
1656 if (waitStatus == WAIT_FAILED)
1657 break;
1658
1659 {
1660 if (!freerdp_check_event_handles(context))
1661 {
1662 if (client_auto_reconnect_ex(instance, handle_window_events))
1663 continue;
1664 else
1665 {
1666 /*
1667 * Indicate an unsuccessful connection attempt if reconnect
1668 * did not succeed and no other error was specified.
1669 */
1670 const UINT32 error = freerdp_get_last_error(instance->context);
1671
1672 if (freerdp_error_info(instance) == 0)
1673 exit_code = (uint32_t)xf_map_error_to_exit_code(error);
1674 }
1675
1676 if (freerdp_get_last_error(context) == FREERDP_ERROR_SUCCESS)
1677 WLog_ERR(TAG, "Failed to check FreeRDP file descriptor");
1678
1679 break;
1680 }
1681 }
1682
1683 if (!handle_window_events(instance))
1684 break;
1685
1686 if ((waitStatus != WAIT_TIMEOUT) && (waitStatus == WAIT_OBJECT_0))
1687 {
1688 timerEvent.now = GetTickCount64();
1689 PubSub_OnTimer(context->pubSub, context, &timerEvent);
1690 }
1691 }
1692
1693 if (!exit_code)
1694 {
1695 exit_code = freerdp_error_info(instance);
1696
1697 if (exit_code == XF_EXIT_DISCONNECT &&
1698 freerdp_get_disconnect_ultimatum(context) == Disconnect_Ultimatum_user_requested)
1699 {
1700 /* This situation might be limited to Windows XP. */
1701 WLog_INFO(TAG, "Error info says user did not initiate but disconnect ultimatum says "
1702 "they did; treat this as a user logoff");
1703 exit_code = XF_EXIT_LOGOFF;
1704 }
1705 }
1706
1707disconnect:
1708
1709 if (timer)
1710 (void)CloseHandle(timer);
1711
1712 freerdp_disconnect(instance);
1713end:
1714 ExitThread(exit_code);
1715 return exit_code;
1716}
1717
1718int xf_exit_code_from_disconnect_reason(DWORD reason)
1719{
1720 if (reason == 0 ||
1721 (reason >= XF_EXIT_PARSE_ARGUMENTS && reason <= XF_EXIT_CONNECT_NO_OR_MISSING_CREDENTIALS))
1722 return WINPR_ASSERTING_INT_CAST(int, reason);
1723 /* License error set */
1724 else if (reason >= 0x100 && reason <= 0x10A)
1725 reason -= 0x100 + XF_EXIT_LICENSE_INTERNAL;
1726 /* RDP protocol error set */
1727 else if (reason >= 0x10c9 && reason <= 0x1193)
1728 reason = XF_EXIT_RDP;
1729 /* There's no need to test protocol-independent codes: they match */
1730 else if (!(reason <= 0xC))
1731 reason = XF_EXIT_UNKNOWN;
1732
1733 return WINPR_ASSERTING_INT_CAST(int, reason);
1734}
1735
1736static void xf_TerminateEventHandler(void* context, const TerminateEventArgs* e)
1737{
1738 rdpContext* ctx = (rdpContext*)context;
1739 WINPR_UNUSED(e);
1740 freerdp_abort_connect_context(ctx);
1741}
1742
1743#ifdef WITH_XRENDER
1744static void xf_ZoomingChangeEventHandler(void* context, const ZoomingChangeEventArgs* e)
1745{
1746 int w = 0;
1747 int h = 0;
1748 rdpSettings* settings = NULL;
1749 xfContext* xfc = (xfContext*)context;
1750
1751 WINPR_ASSERT(xfc);
1752
1753 settings = xfc->common.context.settings;
1754 WINPR_ASSERT(settings);
1755
1756 w = xfc->scaledWidth + e->dx;
1757 h = xfc->scaledHeight + e->dy;
1758
1759 if (e->dx == 0 && e->dy == 0)
1760 return;
1761
1762 if (w < 10)
1763 w = 10;
1764
1765 if (h < 10)
1766 h = 10;
1767
1768 if (w == xfc->scaledWidth && h == xfc->scaledHeight)
1769 return;
1770
1771 xfc->scaledWidth = w;
1772 xfc->scaledHeight = h;
1773 xf_draw_screen(xfc, 0, 0,
1774 WINPR_ASSERTING_INT_CAST(
1775 int32_t, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth)),
1776 WINPR_ASSERTING_INT_CAST(
1777 int32_t, freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight)));
1778}
1779
1780static void xf_PanningChangeEventHandler(void* context, const PanningChangeEventArgs* e)
1781{
1782 xfContext* xfc = (xfContext*)context;
1783 rdpSettings* settings = NULL;
1784
1785 WINPR_ASSERT(xfc);
1786 WINPR_ASSERT(e);
1787
1788 settings = xfc->common.context.settings;
1789 WINPR_ASSERT(settings);
1790
1791 if (e->dx == 0 && e->dy == 0)
1792 return;
1793
1794 xfc->offset_x += e->dx;
1795 xfc->offset_y += e->dy;
1796 xf_draw_screen(xfc, 0, 0,
1797 WINPR_ASSERTING_INT_CAST(
1798 int32_t, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth)),
1799 WINPR_ASSERTING_INT_CAST(
1800 int32_t, freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight)));
1801}
1802#endif
1803
1808static BOOL xfreerdp_client_global_init(void)
1809{
1810 // NOLINTNEXTLINE(concurrency-mt-unsafe)
1811 (void)setlocale(LC_ALL, "");
1812
1813 if (freerdp_handle_signals() != 0)
1814 return FALSE;
1815
1816 return TRUE;
1817}
1818
1819static void xfreerdp_client_global_uninit(void)
1820{
1821}
1822
1823static int xfreerdp_client_start(rdpContext* context)
1824{
1825 xfContext* xfc = (xfContext*)context;
1826 rdpSettings* settings = context->settings;
1827
1828 if (!freerdp_settings_get_string(settings, FreeRDP_ServerHostname))
1829 {
1830 WLog_ERR(TAG, "error: server hostname was not specified with /v:<server>[:port]");
1831 return -1;
1832 }
1833
1834 if (!(xfc->common.thread = CreateThread(NULL, 0, xf_client_thread, context->instance, 0, NULL)))
1835 {
1836 WLog_ERR(TAG, "failed to create client thread");
1837 return -1;
1838 }
1839
1840 return 0;
1841}
1842
1843static Atom get_supported_atom(xfContext* xfc, const char* atomName)
1844{
1845 const Atom atom = Logging_XInternAtom(xfc->log, xfc->display, atomName, False);
1846
1847 for (unsigned long i = 0; i < xfc->supportedAtomCount; i++)
1848 {
1849 if (xfc->supportedAtoms[i] == atom)
1850 return atom;
1851 }
1852
1853 return None;
1854}
1855
1856void xf_teardown_x11(xfContext* xfc)
1857{
1858 WINPR_ASSERT(xfc);
1859
1860 if (xfc->display)
1861 {
1862 XCloseDisplay(xfc->display);
1863 xfc->display = NULL;
1864 }
1865
1866 if (xfc->x11event)
1867 {
1868 (void)CloseHandle(xfc->x11event);
1869 xfc->x11event = NULL;
1870 }
1871
1872 if (xfc->mutex)
1873 {
1874 (void)CloseHandle(xfc->mutex);
1875 xfc->mutex = NULL;
1876 }
1877
1878 if (xfc->vscreen.monitors)
1879 {
1880 free(xfc->vscreen.monitors);
1881 xfc->vscreen.monitors = NULL;
1882 }
1883 xfc->vscreen.nmonitors = 0;
1884
1885 free(xfc->supportedAtoms);
1886 xfc->supportedAtoms = NULL;
1887 xfc->supportedAtomCount = 0;
1888}
1889
1890BOOL xf_setup_x11(xfContext* xfc)
1891{
1892
1893 WINPR_ASSERT(xfc);
1894 xfc->UseXThreads = TRUE;
1895
1896#if !defined(NDEBUG)
1897 /* uncomment below if debugging to prevent keyboard grab */
1898 xfc->debug = TRUE;
1899#endif
1900
1901 if (xfc->UseXThreads)
1902 {
1903 if (!XInitThreads())
1904 {
1905 WLog_WARN(TAG, "XInitThreads() failure");
1906 xfc->UseXThreads = FALSE;
1907 }
1908 }
1909
1910 xfc->display = XOpenDisplay(NULL);
1911
1912 if (!xfc->display)
1913 {
1914 WLog_ERR(TAG, "failed to open display: %s", XDisplayName(NULL));
1915 WLog_ERR(TAG, "Please check that the $DISPLAY environment variable is properly set.");
1916 goto fail;
1917 }
1918 if (xfc->debug)
1919 {
1920 WLog_INFO(TAG, "Enabling X11 debug mode.");
1921 XSynchronize(xfc->display, TRUE);
1922 }
1923 def_error_handler = XSetErrorHandler(xf_error_handler_ex);
1924
1925 xfc->mutex = CreateMutex(NULL, FALSE, NULL);
1926
1927 if (!xfc->mutex)
1928 {
1929 WLog_ERR(TAG, "Could not create mutex!");
1930 goto fail;
1931 }
1932
1933 xfc->xfds = ConnectionNumber(xfc->display);
1934 xfc->screen_number = DefaultScreen(xfc->display);
1935 xfc->screen = ScreenOfDisplay(xfc->display, xfc->screen_number);
1936 xfc->big_endian = (ImageByteOrder(xfc->display) == MSBFirst);
1937 xfc->invert = TRUE;
1938 xfc->complex_regions = TRUE;
1939 xfc->NET_SUPPORTED = Logging_XInternAtom(xfc->log, xfc->display, "_NET_SUPPORTED", True);
1940 xfc->NET_SUPPORTING_WM_CHECK =
1941 Logging_XInternAtom(xfc->log, xfc->display, "_NET_SUPPORTING_WM_CHECK", True);
1942
1943 if ((xfc->NET_SUPPORTED != None) && (xfc->NET_SUPPORTING_WM_CHECK != None))
1944 {
1945 Atom actual_type = 0;
1946 int actual_format = 0;
1947 unsigned long nitems = 0;
1948 unsigned long after = 0;
1949 unsigned char* data = NULL;
1950 int status = LogTagAndXGetWindowProperty(
1951 TAG, xfc->display, RootWindowOfScreen(xfc->screen), xfc->NET_SUPPORTED, 0, 1024, False,
1952 XA_ATOM, &actual_type, &actual_format, &nitems, &after, &data);
1953
1954 if ((status == Success) && (actual_type == XA_ATOM) && (actual_format == 32))
1955 {
1956 xfc->supportedAtomCount = nitems;
1957 xfc->supportedAtoms = calloc(xfc->supportedAtomCount, sizeof(Atom));
1958 WINPR_ASSERT(xfc->supportedAtoms);
1959 memcpy(xfc->supportedAtoms, data, nitems * sizeof(Atom));
1960 }
1961
1962 if (data)
1963 XFree(data);
1964 }
1965
1966 xfc->XWAYLAND_MAY_GRAB_KEYBOARD =
1967 Logging_XInternAtom(xfc->log, xfc->display, "_XWAYLAND_MAY_GRAB_KEYBOARD", False);
1968 xfc->NET_WM_ICON = Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ICON", False);
1969 xfc->MOTIF_WM_HINTS = Logging_XInternAtom(xfc->log, xfc->display, "_MOTIF_WM_HINTS", False);
1970 xfc->NET_NUMBER_OF_DESKTOPS =
1971 Logging_XInternAtom(xfc->log, xfc->display, "_NET_NUMBER_OF_DESKTOPS", False);
1972 xfc->NET_CURRENT_DESKTOP =
1973 Logging_XInternAtom(xfc->log, xfc->display, "_NET_CURRENT_DESKTOP", False);
1974 xfc->NET_WORKAREA = Logging_XInternAtom(xfc->log, xfc->display, "_NET_WORKAREA", False);
1975 xfc->NET_WM_STATE = get_supported_atom(xfc, "_NET_WM_STATE");
1976 xfc->NET_WM_STATE_MODAL = get_supported_atom(xfc, "_NET_WM_STATE_MODAL");
1977 xfc->NET_WM_STATE_STICKY = get_supported_atom(xfc, "_NET_WM_STATE_STICKY");
1978 xfc->NET_WM_STATE_MAXIMIZED_HORZ =
1979 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
1980 xfc->NET_WM_STATE_MAXIMIZED_VERT =
1981 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
1982 xfc->NET_WM_STATE_SHADED = get_supported_atom(xfc, "_NET_WM_STATE_SHADED");
1983 xfc->NET_WM_STATE_SKIP_TASKBAR = get_supported_atom(xfc, "_NET_WM_STATE_SKIP_TASKBAR");
1984 xfc->NET_WM_STATE_SKIP_PAGER = get_supported_atom(xfc, "_NET_WM_STATE_SKIP_PAGER");
1985 xfc->NET_WM_STATE_HIDDEN = get_supported_atom(xfc, "_NET_WM_STATE_HIDDEN");
1986 xfc->NET_WM_STATE_FULLSCREEN = get_supported_atom(xfc, "_NET_WM_STATE_FULLSCREEN");
1987 xfc->NET_WM_STATE_ABOVE = get_supported_atom(xfc, "_NET_WM_STATE_ABOVE");
1988 xfc->NET_WM_STATE_BELOW = get_supported_atom(xfc, "_NET_WM_STATE_BELOW");
1989 xfc->NET_WM_STATE_DEMANDS_ATTENTION =
1990 get_supported_atom(xfc, "_NET_WM_STATE_DEMANDS_ATTENTION");
1991 xfc->NET_WM_FULLSCREEN_MONITORS = get_supported_atom(xfc, "_NET_WM_FULLSCREEN_MONITORS");
1992 xfc->NET_WM_NAME = Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_NAME", False);
1993 xfc->NET_WM_PID = Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_PID", False);
1994 xfc->NET_WM_WINDOW_TYPE =
1995 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_WINDOW_TYPE", False);
1996 xfc->NET_WM_WINDOW_TYPE_NORMAL =
1997 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_WINDOW_TYPE_NORMAL", False);
1998 xfc->NET_WM_WINDOW_TYPE_DIALOG =
1999 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
2000 xfc->NET_WM_WINDOW_TYPE_POPUP =
2001 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_WINDOW_TYPE_POPUP", False);
2002 xfc->NET_WM_WINDOW_TYPE_POPUP_MENU =
2003 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_WINDOW_TYPE_POPUP_MENU", False);
2004 xfc->NET_WM_WINDOW_TYPE_UTILITY =
2005 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_WINDOW_TYPE_UTILITY", False);
2006 xfc->NET_WM_WINDOW_TYPE_DROPDOWN_MENU =
2007 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", False);
2008 xfc->NET_WM_STATE_SKIP_TASKBAR =
2009 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_STATE_SKIP_TASKBAR", False);
2010 xfc->NET_WM_STATE_SKIP_PAGER =
2011 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_STATE_SKIP_PAGER", False);
2012 xfc->NET_WM_MOVERESIZE =
2013 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_MOVERESIZE", False);
2014 xfc->NET_MOVERESIZE_WINDOW =
2015 Logging_XInternAtom(xfc->log, xfc->display, "_NET_MOVERESIZE_WINDOW", False);
2016 xfc->UTF8_STRING = Logging_XInternAtom(xfc->log, xfc->display, "UTF8_STRING", FALSE);
2017 xfc->WM_PROTOCOLS = Logging_XInternAtom(xfc->log, xfc->display, "WM_PROTOCOLS", False);
2018 xfc->WM_DELETE_WINDOW = Logging_XInternAtom(xfc->log, xfc->display, "WM_DELETE_WINDOW", False);
2019 xfc->WM_STATE = Logging_XInternAtom(xfc->log, xfc->display, "WM_STATE", False);
2020 xfc->x11event = CreateFileDescriptorEvent(NULL, FALSE, FALSE, xfc->xfds, WINPR_FD_READ);
2021
2022 xfc->NET_WM_ALLOWED_ACTIONS =
2023 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ALLOWED_ACTIONS", False);
2024
2025 xfc->NET_WM_ACTION_CLOSE =
2026 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ACTION_CLOSE", False);
2027 xfc->NET_WM_ACTION_MINIMIZE =
2028 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ACTION_MINIMIZE", False);
2029 xfc->NET_WM_ACTION_MOVE =
2030 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ACTION_MOVE", False);
2031 xfc->NET_WM_ACTION_RESIZE =
2032 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ACTION_RESIZE", False);
2033 xfc->NET_WM_ACTION_MAXIMIZE_HORZ =
2034 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ACTION_MAXIMIZE_HORZ", False);
2035 xfc->NET_WM_ACTION_MAXIMIZE_VERT =
2036 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ACTION_MAXIMIZE_VERT", False);
2037 xfc->NET_WM_ACTION_FULLSCREEN =
2038 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ACTION_FULLSCREEN", False);
2039 xfc->NET_WM_ACTION_CHANGE_DESKTOP =
2040 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ACTION_CHANGE_DESKTOP", False);
2041
2042 if (!xfc->x11event)
2043 {
2044 WLog_ERR(TAG, "Could not create xfds event");
2045 goto fail;
2046 }
2047
2048 xf_check_extensions(xfc);
2049
2050 xfc->vscreen.monitors = calloc(16, sizeof(MONITOR_INFO));
2051
2052 if (!xfc->vscreen.monitors)
2053 goto fail;
2054 return TRUE;
2055
2056fail:
2057 xf_teardown_x11(xfc);
2058 return FALSE;
2059}
2060
2061static BOOL xfreerdp_client_new(freerdp* instance, rdpContext* context)
2062{
2063 xfContext* xfc = (xfContext*)instance->context;
2064 WINPR_ASSERT(context);
2065 WINPR_ASSERT(xfc);
2066 WINPR_ASSERT(!xfc->display);
2067 WINPR_ASSERT(!xfc->mutex);
2068 WINPR_ASSERT(!xfc->x11event);
2069 instance->PreConnect = xf_pre_connect;
2070 instance->PostConnect = xf_post_connect;
2071 instance->PostDisconnect = xf_post_disconnect;
2072 instance->PostFinalDisconnect = xf_post_final_disconnect;
2073 instance->LogonErrorInfo = xf_logon_error_info;
2074 instance->GetAccessToken = client_cli_get_access_token;
2075 PubSub_SubscribeTerminate(context->pubSub, xf_TerminateEventHandler);
2076#ifdef WITH_XRENDER
2077 PubSub_SubscribeZoomingChange(context->pubSub, xf_ZoomingChangeEventHandler);
2078 PubSub_SubscribePanningChange(context->pubSub, xf_PanningChangeEventHandler);
2079#endif
2080 xfc->log = WLog_Get(TAG);
2081
2082 return TRUE;
2083}
2084
2085static void xfreerdp_client_free(WINPR_ATTR_UNUSED freerdp* instance, rdpContext* context)
2086{
2087 if (!context)
2088 return;
2089
2090 if (context->pubSub)
2091 {
2092 PubSub_UnsubscribeTerminate(context->pubSub, xf_TerminateEventHandler);
2093#ifdef WITH_XRENDER
2094 PubSub_UnsubscribeZoomingChange(context->pubSub, xf_ZoomingChangeEventHandler);
2095 PubSub_UnsubscribePanningChange(context->pubSub, xf_PanningChangeEventHandler);
2096#endif
2097 }
2098}
2099
2100int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
2101{
2102 pEntryPoints->Version = 1;
2103 pEntryPoints->Size = sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
2104 pEntryPoints->GlobalInit = xfreerdp_client_global_init;
2105 pEntryPoints->GlobalUninit = xfreerdp_client_global_uninit;
2106 pEntryPoints->ContextSize = sizeof(xfContext);
2107 pEntryPoints->ClientNew = xfreerdp_client_new;
2108 pEntryPoints->ClientFree = xfreerdp_client_free;
2109 pEntryPoints->ClientStart = xfreerdp_client_start;
2110 pEntryPoints->ClientStop = freerdp_client_common_stop;
2111 return 0;
2112}
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_set_string(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *param)
Sets a string settings value. The param is copied.
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL param)
Sets a BOOL settings value.