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 { FREERDP_ERROR_CONNECT_TARGET_BOOTING, XF_EXIT_CONNECT_TARGET_BOOTING }
150};
151
152static BOOL xf_setup_x11(xfContext* xfc);
153static void xf_teardown_x11(xfContext* xfc);
154
155static int xf_map_error_to_exit_code(DWORD error)
156{
157 for (size_t x = 0; x < ARRAYSIZE(xf_exit_code_map); x++)
158 {
159 const struct xf_exit_code_map_t* cur = &xf_exit_code_map[x];
160 if (cur->error == error)
161 return cur->rc;
162 }
163
164 return XF_EXIT_CONN_FAILED;
165}
166
167static int (*def_error_handler)(Display*, XErrorEvent*);
168static int xf_error_handler_ex(Display* d, XErrorEvent* ev);
169static void xf_check_extensions(xfContext* context);
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 LogDynAndXSetFillStyle(xfc->log, xfc->display, xfc->gc, FillSolid);
208 LogDynAndXSetForeground(xfc->log, 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 LogDynAndXSetRegion(xfc->log, xfc->display, xfc->gc, reg1);
225 LogDynAndXFillRectangle(xfc->log, 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 LogDynAndXSetClipMask(xfc->log, 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=NULL", fkt);
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=NULL");
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 LogDynAndXCopyArea(xfc->log, 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),
334 x, y);
335}
336
337static BOOL xf_desktop_resize(rdpContext* context)
338{
339 xfContext* xfc = (xfContext*)context;
340
341 WINPR_ASSERT(xfc);
342
343 rdpSettings* settings = context->settings;
344 WINPR_ASSERT(settings);
345
346 if (xfc->primary)
347 {
348 BOOL same = (xfc->primary == xfc->drawing) ? TRUE : FALSE;
349 LogDynAndXFreePixmap(xfc->log, xfc->display, xfc->primary);
350
351 WINPR_ASSERT(xfc->depth != 0);
352 if (!(xfc->primary = LogDynAndXCreatePixmap(
353 xfc->log, 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 LogDynAndXSetFunction(xfc->log, xfc->display, xfc->gc, GXcopy);
400 LogDynAndXSetFillStyle(xfc->log, xfc->display, xfc->gc, FillSolid);
401 LogDynAndXSetForeground(xfc->log, xfc->display, xfc->gc, 0);
402 LogDynAndXFillRectangle(xfc->log, 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 LogDynAndXPutImage(xfc->log, xfc->display, xfc->primary, xfc->gc, xfc->image, region->x,
428 region->y, region->x, region->y,
429 WINPR_ASSERTING_INT_CAST(UINT16, region->w),
430 WINPR_ASSERTING_INT_CAST(UINT16, region->h));
431 xf_draw_screen(xfc, region->x, region->y, region->w, region->h);
432 }
433 return TRUE;
434}
435
436static BOOL xf_end_paint(rdpContext* context)
437{
438 xfContext* xfc = (xfContext*)context;
439 WINPR_ASSERT(xfc);
440
441 rdpGdi* gdi = context->gdi;
442 WINPR_ASSERT(gdi);
443
444 if (gdi->suppressOutput)
445 return TRUE;
446
447 HGDI_DC hdc = gdi->primary->hdc;
448 if (!hdc->hwnd)
449 return TRUE;
450
451 HGDI_WND hwnd = hdc->hwnd;
452 if (!xfc->complex_regions)
453 {
454 const GDI_RGN* rgn = hwnd->invalid;
455 if (rgn->null)
456 return TRUE;
457 xf_lock_x11(xfc);
458 if (!xf_paint(xfc, rgn))
459 return FALSE;
460 xf_unlock_x11(xfc);
461 }
462 else
463 {
464 const INT32 ninvalid = hwnd->ninvalid;
465 const GDI_RGN* cinvalid = hwnd->cinvalid;
466
467 if (hwnd->ninvalid < 1)
468 return TRUE;
469
470 xf_lock_x11(xfc);
471
472 for (INT32 i = 0; i < ninvalid; i++)
473 {
474 const GDI_RGN* rgn = &cinvalid[i];
475 if (!xf_paint(xfc, rgn))
476 return FALSE;
477 }
478
479 LogDynAndXFlush(xfc->log, xfc->display);
480 xf_unlock_x11(xfc);
481 }
482
483 hwnd->invalid->null = TRUE;
484 hwnd->ninvalid = 0;
485 return TRUE;
486}
487
488static BOOL xf_sw_desktop_resize(rdpContext* context)
489{
490 WINPR_ASSERT(context);
491
492 rdpGdi* gdi = context->gdi;
493 WINPR_ASSERT(gdi);
494
495 xfContext* xfc = (xfContext*)context;
496 rdpSettings* settings = context->settings;
497 WINPR_ASSERT(settings);
498
499 BOOL ret = FALSE;
500
501 /* There is a possible race here.
502 * Ensure that the drawing thread does not update the screen during a
503 * resize. */
504 const BOOL suppress = gdi->suppressOutput;
505 gdi->suppressOutput = TRUE;
506
507 xf_lock_x11(xfc);
508 if (!gdi_resize(gdi, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
509 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight)))
510 goto out;
511
512 if (xfc->image)
513 {
514 xfc->image->data = NULL;
515 XDestroyImage(xfc->image);
516 }
517
518 WINPR_ASSERT(xfc->depth != 0);
519 if (!(xfc->image = LogDynAndXCreateImage(
520 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
521 ZPixmap, 0, (char*)gdi->primary_buffer,
522 WINPR_ASSERTING_INT_CAST(uint32_t, gdi->width),
523 WINPR_ASSERTING_INT_CAST(uint32_t, gdi->height), xfc->scanline_pad,
524 WINPR_ASSERTING_INT_CAST(int, gdi->stride))))
525 {
526 goto out;
527 }
528
529 xfc->image->byte_order = LSBFirst;
530 xfc->image->bitmap_bit_order = LSBFirst;
531 ret = xf_desktop_resize(context);
532out:
533 xf_unlock_x11(xfc);
534 gdi->suppressOutput = suppress;
535 return ret;
536}
537
538static BOOL xf_process_x_events(freerdp* instance)
539{
540 BOOL status = TRUE;
541 int pending_status = 1;
542 xfContext* xfc = (xfContext*)instance->context;
543
544 while (pending_status)
545 {
546 xf_lock_x11(xfc);
547 pending_status = XPending(xfc->display);
548
549 if (pending_status)
550 {
551 XEvent xevent = { 0 };
552
553 XNextEvent(xfc->display, &xevent);
554 status = xf_event_process(instance, &xevent);
555 }
556 xf_unlock_x11(xfc);
557 if (!status)
558 break;
559 }
560
561 return status;
562}
563
564static char* xf_window_get_title(rdpSettings* settings)
565{
566 BOOL port = 0;
567 char* windowTitle = NULL;
568 size_t size = 0;
569 const char* prefix = "FreeRDP:";
570
571 if (!settings)
572 return NULL;
573
574 const char* name = freerdp_settings_get_string(settings, FreeRDP_ServerHostname);
575 const char* title = freerdp_settings_get_string(settings, FreeRDP_WindowTitle);
576
577 if (title)
578 return _strdup(title);
579
580 port = (freerdp_settings_get_uint32(settings, FreeRDP_ServerPort) != 3389);
581 /* Just assume a window title is never longer than a filename... */
582 size = strnlen(name, MAX_PATH) + 16;
583 windowTitle = calloc(size, sizeof(char));
584
585 if (!windowTitle)
586 return NULL;
587
588 if (!port)
589 (void)sprintf_s(windowTitle, size, "%s %s", prefix, name);
590 else
591 (void)sprintf_s(windowTitle, size, "%s %s:%" PRIu32, prefix, name,
592 freerdp_settings_get_uint32(settings, FreeRDP_ServerPort));
593
594 return windowTitle;
595}
596
597BOOL xf_create_window(xfContext* xfc)
598{
599 XGCValues gcv = { 0 };
600 XEvent xevent = { 0 };
601 char* windowTitle = NULL;
602
603 WINPR_ASSERT(xfc);
604 rdpSettings* settings = xfc->common.context.settings;
605 WINPR_ASSERT(settings);
606
607 int width =
608 WINPR_ASSERTING_INT_CAST(int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth));
609 int height =
610 WINPR_ASSERTING_INT_CAST(int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
611
612 const XSetWindowAttributes empty = { 0 };
613 xfc->attribs = empty;
614
615 if (xfc->remote_app)
616 xfc->depth = 32;
617 else
618 xfc->depth = DefaultDepthOfScreen(xfc->screen);
619
620 XVisualInfo vinfo = { 0 };
621 if (XMatchVisualInfo(xfc->display, xfc->screen_number, xfc->depth, TrueColor, &vinfo))
622 {
623 Window root = XDefaultRootWindow(xfc->display);
624 xfc->visual = vinfo.visual;
625 xfc->attribs.colormap = xfc->colormap =
626 XCreateColormap(xfc->display, root, vinfo.visual, AllocNone);
627 }
628 else
629 {
630 if (xfc->remote_app)
631 {
632 WLog_WARN(TAG, "running in remote app mode, but XServer does not support transparency");
633 WLog_WARN(TAG, "display of remote applications might be distorted (black frames, ...)");
634 }
635 xfc->depth = DefaultDepthOfScreen(xfc->screen);
636 xfc->visual = DefaultVisual(xfc->display, xfc->screen_number);
637 xfc->attribs.colormap = xfc->colormap = DefaultColormap(xfc->display, xfc->screen_number);
638 }
639
640 /*
641 * Detect if the server visual has an inverted colormap
642 * (BGR vs RGB, or red being the least significant byte)
643 */
644 if (vinfo.red_mask & 0xFF)
645 {
646 xfc->invert = FALSE;
647 }
648
649 if (!xfc->remote_app)
650 {
651 xfc->attribs.background_pixel = BlackPixelOfScreen(xfc->screen);
652 xfc->attribs.border_pixel = WhitePixelOfScreen(xfc->screen);
653 xfc->attribs.backing_store = xfc->primary ? NotUseful : Always;
654 xfc->attribs.override_redirect = False;
655
656 xfc->attribs.bit_gravity = NorthWestGravity;
657 xfc->attribs.win_gravity = NorthWestGravity;
658 xfc->attribs_mask = CWBackPixel | CWBackingStore | CWOverrideRedirect | CWColormap |
659 CWBorderPixel | CWWinGravity | CWBitGravity;
660
661#ifdef WITH_XRENDER
662 xfc->offset_x = 0;
663 xfc->offset_y = 0;
664#endif
665 windowTitle = xf_window_get_title(settings);
666
667 if (!windowTitle)
668 return FALSE;
669
670#ifdef WITH_XRENDER
671
672 if (freerdp_settings_get_bool(settings, FreeRDP_SmartSizing) && !xfc->fullscreen)
673 {
674 if (freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingWidth) > 0)
675 width = WINPR_ASSERTING_INT_CAST(
676 int, freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingWidth));
677
678 if (freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingHeight) > 0)
679 height = WINPR_ASSERTING_INT_CAST(
680 int, freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingHeight));
681
682 xfc->scaledWidth = width;
683 xfc->scaledHeight = height;
684 }
685
686#endif
687 xfc->window = xf_CreateDesktopWindow(xfc, windowTitle, width, height);
688 free(windowTitle);
689
690 if (xfc->fullscreen)
691 xf_SetWindowFullscreen(xfc, xfc->window, xfc->fullscreen);
692
693 xfc->unobscured = (xevent.xvisibility.state == VisibilityUnobscured);
694 XSetWMProtocols(xfc->display, xfc->window->handle, &(xfc->WM_DELETE_WINDOW), 1);
695 xfc->drawable = xfc->window->handle;
696 }
697 else
698 {
699 xfc->attribs.border_pixel = 0;
700 xfc->attribs.background_pixel = 0;
701 xfc->attribs.backing_store = xfc->primary ? NotUseful : Always;
702 xfc->attribs.override_redirect = False;
703
704 xfc->attribs.bit_gravity = NorthWestGravity;
705 xfc->attribs.win_gravity = NorthWestGravity;
706 xfc->attribs_mask = CWBackPixel | CWBackingStore | CWOverrideRedirect | CWColormap |
707 CWBorderPixel | CWWinGravity | CWBitGravity;
708
709 xfc->drawable = xf_CreateDummyWindow(xfc);
710 }
711
712 if (!xfc->gc)
713 xfc->gc =
714 LogDynAndXCreateGC(xfc->log, xfc->display, xfc->drawable, GCGraphicsExposures, &gcv);
715
716 WINPR_ASSERT(xfc->depth != 0);
717 if (!xfc->primary)
718 xfc->primary =
719 LogDynAndXCreatePixmap(xfc->log, xfc->display, xfc->drawable,
720 freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
721 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight),
722 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth));
723
724 xfc->drawing = xfc->primary;
725
726 if (!xfc->bitmap_mono)
727 xfc->bitmap_mono = LogDynAndXCreatePixmap(xfc->log, xfc->display, xfc->drawable, 8, 8, 1);
728
729 if (!xfc->gc_mono)
730 xfc->gc_mono =
731 LogDynAndXCreateGC(xfc->log, xfc->display, xfc->bitmap_mono, GCGraphicsExposures, &gcv);
732
733 LogDynAndXSetFunction(xfc->log, xfc->display, xfc->gc, GXcopy);
734 LogDynAndXSetFillStyle(xfc->log, xfc->display, xfc->gc, FillSolid);
735 LogDynAndXSetForeground(xfc->log, xfc->display, xfc->gc, BlackPixelOfScreen(xfc->screen));
736 LogDynAndXFillRectangle(xfc->log, xfc->display, xfc->primary, xfc->gc, 0, 0,
737 freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
738 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
739 LogDynAndXFlush(xfc->log, xfc->display);
740
741 return TRUE;
742}
743
744BOOL xf_create_image(xfContext* xfc)
745{
746 WINPR_ASSERT(xfc);
747 if (!xfc->image)
748 {
749 const rdpSettings* settings = xfc->common.context.settings;
750 rdpGdi* cgdi = xfc->common.context.gdi;
751 WINPR_ASSERT(cgdi);
752
753 WINPR_ASSERT(xfc->depth != 0);
754 xfc->image = LogDynAndXCreateImage(
755 xfc->log, xfc->display, xfc->visual, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->depth),
756 ZPixmap, 0, (char*)cgdi->primary_buffer,
757 freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
758 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight), xfc->scanline_pad,
759 WINPR_ASSERTING_INT_CAST(int, cgdi->stride));
760 xfc->image->byte_order = LSBFirst;
761 xfc->image->bitmap_bit_order = LSBFirst;
762 }
763 return TRUE;
764}
765
766void xf_destroy_window(xfContext* xfc)
767{
768 if (xfc->window)
769 {
770 xf_DestroyDesktopWindow(xfc, xfc->window);
771 xfc->window = NULL;
772 }
773
774#if defined(CHANNEL_TSMF_CLIENT)
775 if (xfc->xv_context)
776 {
777 xf_tsmf_uninit(xfc, NULL);
778 xfc->xv_context = NULL;
779 }
780#endif
781
782 if (xfc->image)
783 {
784 xfc->image->data = NULL;
785 XDestroyImage(xfc->image);
786 xfc->image = NULL;
787 }
788
789 if (xfc->bitmap_mono)
790 {
791 LogDynAndXFreePixmap(xfc->log, xfc->display, xfc->bitmap_mono);
792 xfc->bitmap_mono = 0;
793 }
794
795 if (xfc->gc_mono)
796 {
797 LogDynAndXFreeGC(xfc->log, xfc->display, xfc->gc_mono);
798 xfc->gc_mono = 0;
799 }
800
801 if (xfc->primary)
802 {
803 LogDynAndXFreePixmap(xfc->log, xfc->display, xfc->primary);
804 xfc->primary = 0;
805 }
806
807 if (xfc->gc)
808 {
809 LogDynAndXFreeGC(xfc->log, xfc->display, xfc->gc);
810 xfc->gc = 0;
811 }
812}
813
814void xf_toggle_fullscreen(xfContext* xfc)
815{
816 WindowStateChangeEventArgs e = { 0 };
817 rdpContext* context = (rdpContext*)xfc;
818 rdpSettings* settings = context->settings;
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 xfc->fullscreen = (xfc->fullscreen) ? FALSE : TRUE;
828 xfc->decorations =
829 (xfc->fullscreen) ? FALSE : freerdp_settings_get_bool(settings, FreeRDP_Decorations);
830 xf_SetWindowFullscreen(xfc, xfc->window, xfc->fullscreen);
831 EventArgsInit(&e, "xfreerdp");
832 e.state = xfc->fullscreen ? FREERDP_WINDOW_STATE_FULLSCREEN : 0;
833 PubSub_OnWindowStateChange(context->pubSub, context, &e);
834}
835
836void xf_minimize(xfContext* xfc)
837{
838 WindowStateChangeEventArgs e = { 0 };
839 rdpContext* context = (rdpContext*)xfc;
840 WINPR_ASSERT(context);
841
842 /*
843 when debugging, ungrab keyboard when toggling fullscreen
844 to allow keyboard usage on the debugger
845 */
846 if (xfc->debug)
847 xf_ungrab(xfc);
848
849 xf_SetWindowMinimized(xfc, xfc->window);
850
851 e.state = xfc->fullscreen ? FREERDP_WINDOW_STATE_FULLSCREEN : 0;
852 PubSub_OnWindowStateChange(context->pubSub, context, &e);
853}
854
855void xf_lock_x11_(xfContext* xfc, WINPR_ATTR_UNUSED const char* fkt)
856{
857 if (!xfc->UseXThreads)
858 (void)WaitForSingleObject(xfc->mutex, INFINITE);
859 else
860 XLockDisplay(xfc->display);
861
862 xfc->locked++;
863}
864
865void xf_unlock_x11_(xfContext* xfc, WINPR_ATTR_UNUSED const char* fkt)
866{
867 if (xfc->locked == 0)
868 WLog_WARN(TAG, "X11: trying to unlock although not locked!");
869 else
870 xfc->locked--;
871
872 if (!xfc->UseXThreads)
873 (void)ReleaseMutex(xfc->mutex);
874 else
875 XUnlockDisplay(xfc->display);
876}
877
878static BOOL xf_get_pixmap_info(xfContext* xfc)
879{
880 int pf_count = 0;
881 XPixmapFormatValues* pfs = NULL;
882
883 WINPR_ASSERT(xfc->display);
884 pfs = XListPixmapFormats(xfc->display, &pf_count);
885
886 if (!pfs)
887 {
888 WLog_ERR(TAG, "XListPixmapFormats failed");
889 return 1;
890 }
891
892 WINPR_ASSERT(xfc->depth != 0);
893 for (int i = 0; i < pf_count; i++)
894 {
895 const XPixmapFormatValues* pf = &pfs[i];
896
897 if (pf->depth == xfc->depth)
898 {
899 xfc->scanline_pad = pf->scanline_pad;
900 break;
901 }
902 }
903
904 XFree(pfs);
905 if ((xfc->visual == NULL) || (xfc->scanline_pad == 0))
906 return FALSE;
907
908 return TRUE;
909}
910
911static int xf_error_handler(Display* d, XErrorEvent* ev)
912{
913 char buf[256] = { 0 };
914 XGetErrorText(d, ev->error_code, buf, sizeof(buf));
915 const char* what = request_code_2_str(ev->request_code);
916 WLog_ERR(TAG, "%s: %s", what, buf);
917 winpr_log_backtrace(TAG, WLOG_ERROR, 20);
918 return 0;
919}
920
921static int xf_error_handler_ex(Display* d, XErrorEvent* ev)
922{
923 /*
924 * ungrab the keyboard, in case a debugger is running in
925 * another window. This make xf_error_handler() a potential
926 * debugger breakpoint.
927 */
928#if defined(WITH_DEBUG_X11)
929 XUngrabKeyboard(d, CurrentTime);
930 XUngrabPointer(d, CurrentTime);
931#endif
932 return xf_error_handler(d, ev);
933}
934
935static BOOL xf_play_sound(rdpContext* context, const PLAY_SOUND_UPDATE* play_sound)
936{
937 xfContext* xfc = (xfContext*)context;
938 WINPR_UNUSED(play_sound);
939 XkbBell(xfc->display, None, 100, 0);
940 return TRUE;
941}
942
943static void xf_check_extensions(xfContext* context)
944{
945 int xkb_opcode = 0;
946 int xkb_event = 0;
947 int xkb_error = 0;
948 int xkb_major = XkbMajorVersion;
949 int xkb_minor = XkbMinorVersion;
950
951 if (XkbLibraryVersion(&xkb_major, &xkb_minor) &&
952 XkbQueryExtension(context->display, &xkb_opcode, &xkb_event, &xkb_error, &xkb_major,
953 &xkb_minor))
954 {
955 context->xkbAvailable = TRUE;
956 }
957
958#ifdef WITH_XRENDER
959 {
960 int xrender_event_base = 0;
961 int xrender_error_base = 0;
962
963 if (XRenderQueryExtension(context->display, &xrender_event_base, &xrender_error_base))
964 {
965 context->xrenderAvailable = TRUE;
966 }
967 }
968#endif
969}
970
971#ifdef WITH_XI
972/* Input device which does NOT have the correct mapping. We must disregard */
973/* this device when trying to find the input device which is the pointer. */
974static const char TEST_PTR_STR[] = "Virtual core XTEST pointer";
975static const size_t TEST_PTR_LEN = sizeof(TEST_PTR_STR) / sizeof(char);
976#endif /* WITH_XI */
977
978static void xf_get_x11_button_map(xfContext* xfc, unsigned char* x11_map)
979{
980#ifdef WITH_XI
981 int opcode = 0;
982 int event = 0;
983 int error = 0;
984 XDevice* ptr_dev = NULL;
985 XExtensionVersion* version = NULL;
986 XDeviceInfo* devices1 = NULL;
987 XIDeviceInfo* devices2 = NULL;
988 int num_devices = 0;
989
990 if (XQueryExtension(xfc->display, "XInputExtension", &opcode, &event, &error))
991 {
992 WLog_DBG(TAG, "Searching for XInput pointer device");
993 ptr_dev = NULL;
994 /* loop through every device, looking for a pointer */
995 version = XGetExtensionVersion(xfc->display, INAME);
996
997 if (version->major_version >= 2)
998 {
999 /* XID of pointer device using XInput version 2 */
1000 devices2 = XIQueryDevice(xfc->display, XIAllDevices, &num_devices);
1001
1002 if (devices2)
1003 {
1004 for (int i = 0; i < num_devices; ++i)
1005 {
1006 XIDeviceInfo* dev = &devices2[i];
1007 if ((dev->use == XISlavePointer) &&
1008 (strncmp(dev->name, TEST_PTR_STR, TEST_PTR_LEN) != 0))
1009 {
1010 ptr_dev = XOpenDevice(xfc->display,
1011 WINPR_ASSERTING_INT_CAST(uint32_t, dev->deviceid));
1012 if (ptr_dev)
1013 break;
1014 }
1015 }
1016
1017 XIFreeDeviceInfo(devices2);
1018 }
1019 }
1020 else
1021 {
1022 /* XID of pointer device using XInput version 1 */
1023 devices1 = XListInputDevices(xfc->display, &num_devices);
1024
1025 if (devices1)
1026 {
1027 for (int i = 0; i < num_devices; ++i)
1028 {
1029 if ((devices1[i].use == IsXExtensionPointer) &&
1030 (strncmp(devices1[i].name, TEST_PTR_STR, TEST_PTR_LEN) != 0))
1031 {
1032 ptr_dev = XOpenDevice(xfc->display, devices1[i].id);
1033 if (ptr_dev)
1034 break;
1035 }
1036 }
1037
1038 XFreeDeviceList(devices1);
1039 }
1040 }
1041
1042 XFree(version);
1043
1044 /* get button mapping from input extension if there is a pointer device; */
1045 /* otherwise leave unchanged. */
1046 if (ptr_dev)
1047 {
1048 WLog_DBG(TAG, "Pointer device: %" PRIu32,
1049 WINPR_CXX_COMPAT_CAST(uint32_t, ptr_dev->device_id));
1050 XGetDeviceButtonMapping(xfc->display, ptr_dev, x11_map, NUM_BUTTONS_MAPPED);
1051 XCloseDevice(xfc->display, ptr_dev);
1052 }
1053 else
1054 {
1055 WLog_DBG(TAG, "No pointer device found!");
1056 }
1057 }
1058 else
1059#endif /* WITH_XI */
1060 {
1061 WLog_DBG(TAG, "Get global pointer mapping (no XInput)");
1062 XGetPointerMapping(xfc->display, x11_map, NUM_BUTTONS_MAPPED);
1063 }
1064}
1065
1066/* Assignment of physical (not logical) mouse buttons to wire flags. */
1067/* Notice that the middle button is 2 in X11, but 3 in RDP. */
1068static const button_map xf_button_flags[NUM_BUTTONS_MAPPED] = {
1069 { Button1, PTR_FLAGS_BUTTON1 },
1070 { Button2, PTR_FLAGS_BUTTON3 },
1071 { Button3, PTR_FLAGS_BUTTON2 },
1072 { Button4, PTR_FLAGS_WHEEL | 0x78 },
1073 /* Negative value is 9bit twos complement */
1074 { Button5, PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | (0x100 - 0x78) },
1075 { 6, PTR_FLAGS_HWHEEL | PTR_FLAGS_WHEEL_NEGATIVE | (0x100 - 0x78) },
1076 { 7, PTR_FLAGS_HWHEEL | 0x78 },
1077 { 8, PTR_XFLAGS_BUTTON1 },
1078 { 9, PTR_XFLAGS_BUTTON2 },
1079 { 97, PTR_XFLAGS_BUTTON1 },
1080 { 112, PTR_XFLAGS_BUTTON2 }
1081};
1082
1083static UINT16 get_flags_for_button(size_t button)
1084{
1085 for (size_t x = 0; x < ARRAYSIZE(xf_button_flags); x++)
1086 {
1087 const button_map* map = &xf_button_flags[x];
1088
1089 if (map->button == button)
1090 return map->flags;
1091 }
1092
1093 return 0;
1094}
1095
1096void xf_button_map_init(xfContext* xfc)
1097{
1098 size_t pos = 0;
1099 /* loop counter for array initialization */
1100
1101 /* logical mouse button which is used for each physical mouse */
1102 /* button (indexed from zero). This is the default map. */
1103 unsigned char x11_map[112] = { 0 };
1104
1105 WINPR_ASSERT(xfc);
1106 WINPR_ASSERT(xfc->common.context.settings);
1107
1108 x11_map[0] = Button1;
1109 x11_map[1] = Button2;
1110 x11_map[2] = Button3;
1111 x11_map[3] = Button4;
1112 x11_map[4] = Button5;
1113 x11_map[5] = 6;
1114 x11_map[6] = 7;
1115 x11_map[7] = 8;
1116 x11_map[8] = 9;
1117 x11_map[96] = 97;
1118 x11_map[111] = 112;
1119
1120 /* query system for actual remapping */
1121 if (freerdp_settings_get_bool(xfc->common.context.settings, FreeRDP_UnmapButtons))
1122 {
1123 xf_get_x11_button_map(xfc, x11_map);
1124 }
1125
1126 /* iterate over all (mapped) physical buttons; for each of them */
1127 /* find the logical button in X11, and assign to this the */
1128 /* appropriate value to send over the RDP wire. */
1129 for (size_t physical = 0; physical < ARRAYSIZE(x11_map); ++physical)
1130 {
1131 const unsigned char logical = x11_map[physical];
1132 const UINT16 flags = get_flags_for_button(logical);
1133
1134 if ((logical != 0) && (flags != 0))
1135 {
1136 if (pos >= NUM_BUTTONS_MAPPED)
1137 {
1138 WLog_ERR(TAG, "Failed to map mouse button to RDP button, no space");
1139 }
1140 else
1141 {
1142 button_map* map = &xfc->button_map[pos++];
1143 map->button = logical;
1144 map->flags = get_flags_for_button(physical + Button1);
1145 }
1146 }
1147 }
1148}
1149
1161static BOOL xf_pre_connect(freerdp* instance)
1162{
1163 UINT32 maxWidth = 0;
1164 UINT32 maxHeight = 0;
1165
1166 WINPR_ASSERT(instance);
1167
1168 rdpContext* context = instance->context;
1169 WINPR_ASSERT(context);
1170 xfContext* xfc = (xfContext*)context;
1171
1172 rdpSettings* settings = context->settings;
1173 WINPR_ASSERT(settings);
1174
1175 if (!freerdp_settings_set_bool(settings, FreeRDP_CertificateCallbackPreferPEM, TRUE))
1176 return FALSE;
1177
1178 if (!freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
1179 {
1180 if (!xf_setup_x11(xfc))
1181 return FALSE;
1182 }
1183
1184 if (!freerdp_settings_set_uint32(settings, FreeRDP_OsMajorType, OSMAJORTYPE_UNIX))
1185 return FALSE;
1186 if (!freerdp_settings_set_uint32(settings, FreeRDP_OsMinorType, OSMINORTYPE_NATIVE_XSERVER))
1187 return FALSE;
1188 PubSub_SubscribeChannelConnected(context->pubSub, xf_OnChannelConnectedEventHandler);
1189 PubSub_SubscribeChannelDisconnected(context->pubSub, xf_OnChannelDisconnectedEventHandler);
1190
1191 if (!freerdp_settings_get_string(settings, FreeRDP_Username) &&
1192 !freerdp_settings_get_bool(settings, FreeRDP_CredentialsFromStdin) &&
1193 !freerdp_settings_get_bool(settings, FreeRDP_SmartcardLogon))
1194 {
1195 char login_name[MAX_PATH] = { 0 };
1196 ULONG size = sizeof(login_name) - 1;
1197
1198 if (GetUserNameExA(NameSamCompatible, login_name, &size))
1199 {
1200 if (!freerdp_settings_set_string(settings, FreeRDP_Username, login_name))
1201 return FALSE;
1202
1203 WLog_INFO(TAG, "No user name set. - Using login name: %s",
1204 freerdp_settings_get_string(settings, FreeRDP_Username));
1205 }
1206 }
1207
1208 if (freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
1209 {
1210 /* Check +auth-only has a username and password. */
1211 if (!freerdp_settings_get_string(settings, FreeRDP_Password))
1212 {
1213 WLog_INFO(TAG, "auth-only, but no password set. Please provide one.");
1214 return FALSE;
1215 }
1216
1217 WLog_INFO(TAG, "Authentication only. Don't connect to X.");
1218 }
1219
1220 if (!freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
1221 {
1222 const char* KeyboardRemappingList = freerdp_settings_get_string(
1223 xfc->common.context.settings, FreeRDP_KeyboardRemappingList);
1224
1225 xfc->remap_table = freerdp_keyboard_remap_string_to_list(KeyboardRemappingList);
1226 if (!xfc->remap_table)
1227 return FALSE;
1228 if (!xf_keyboard_init(xfc))
1229 return FALSE;
1230 if (!xf_keyboard_action_script_init(xfc))
1231 return FALSE;
1232 if (!xf_detect_monitors(xfc, &maxWidth, &maxHeight))
1233 return FALSE;
1234 }
1235
1236 if (maxWidth && maxHeight && !freerdp_settings_get_bool(settings, FreeRDP_SmartSizing))
1237 {
1238 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, maxWidth))
1239 return FALSE;
1240 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, maxHeight))
1241 return FALSE;
1242 }
1243
1244#ifdef WITH_XRENDER
1245
1250 if (freerdp_settings_get_bool(settings, FreeRDP_Fullscreen) &&
1251 freerdp_settings_get_bool(settings, FreeRDP_SmartSizing) &&
1252 (freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingWidth) > 0) &&
1253 (freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingHeight) > 0))
1254 {
1256 settings, FreeRDP_DesktopWidth,
1257 freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingWidth)))
1258 return FALSE;
1260 settings, FreeRDP_DesktopHeight,
1261 freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingHeight)))
1262 return FALSE;
1263 }
1264
1265#endif
1266 xfc->fullscreen = freerdp_settings_get_bool(settings, FreeRDP_Fullscreen);
1267 xfc->decorations = freerdp_settings_get_bool(settings, FreeRDP_Decorations);
1268 xfc->grab_keyboard = freerdp_settings_get_bool(settings, FreeRDP_GrabKeyboard);
1269 xfc->fullscreen_toggle = freerdp_settings_get_bool(settings, FreeRDP_ToggleFullscreen);
1270 if (!freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
1271 xf_button_map_init(xfc);
1272 return TRUE;
1273}
1274
1275static BOOL xf_inject_keypress(rdpContext* context, const char* buffer, size_t size)
1276{
1277 WCHAR wbuffer[64] = { 0 };
1278 const SSIZE_T len = ConvertUtf8NToWChar(buffer, size, wbuffer, ARRAYSIZE(wbuffer));
1279 if (len < 0)
1280 return FALSE;
1281
1282 rdpInput* input = context->input;
1283 WINPR_ASSERT(input);
1284
1285 for (SSIZE_T x = 0; x < len; x++)
1286 {
1287 const WCHAR code = wbuffer[x];
1288 freerdp_input_send_unicode_keyboard_event(input, 0, code);
1289 Sleep(5);
1290 freerdp_input_send_unicode_keyboard_event(input, KBD_FLAGS_RELEASE, code);
1291 Sleep(5);
1292 }
1293 return TRUE;
1294}
1295
1296static BOOL xf_process_pipe(rdpContext* context, const char* pipe)
1297{
1298 int fd = open(pipe, O_NONBLOCK | O_RDONLY);
1299 if (fd < 0)
1300 {
1301 char ebuffer[256] = { 0 };
1302 WLog_ERR(TAG, "pipe '%s' open returned %s [%d]", pipe,
1303 winpr_strerror(errno, ebuffer, sizeof(ebuffer)), errno);
1304 return FALSE;
1305 }
1306 while (!freerdp_shall_disconnect_context(context))
1307 {
1308 char buffer[64] = { 0 };
1309 ssize_t rd = read(fd, buffer, sizeof(buffer) - 1);
1310 if (rd == 0)
1311 {
1312 char ebuffer[256] = { 0 };
1313 if ((errno == EAGAIN) || (errno == 0))
1314 {
1315 Sleep(100);
1316 continue;
1317 }
1318
1319 // EOF, abort reading.
1320 WLog_ERR(TAG, "pipe '%s' read returned %s [%d]", pipe,
1321 winpr_strerror(errno, ebuffer, sizeof(ebuffer)), errno);
1322 break;
1323 }
1324 else if (rd < 0)
1325 {
1326 char ebuffer[256] = { 0 };
1327 WLog_ERR(TAG, "pipe '%s' read returned %s [%d]", pipe,
1328 winpr_strerror(errno, ebuffer, sizeof(ebuffer)), errno);
1329 break;
1330 }
1331 else
1332 {
1333 if (!xf_inject_keypress(context, buffer, WINPR_ASSERTING_INT_CAST(size_t, rd)))
1334 break;
1335 }
1336 }
1337 close(fd);
1338 return TRUE;
1339}
1340
1341static void cleanup_pipe(WINPR_ATTR_UNUSED int signum, WINPR_ATTR_UNUSED const char* signame,
1342 void* context)
1343{
1344 const char* pipe = context;
1345 if (!pipe)
1346 return;
1347 unlink(pipe);
1348}
1349
1350static DWORD WINAPI xf_handle_pipe(void* arg)
1351{
1352 xfContext* xfc = arg;
1353 WINPR_ASSERT(xfc);
1354
1355 rdpContext* context = &xfc->common.context;
1356 WINPR_ASSERT(context);
1357
1358 rdpSettings* settings = context->settings;
1359 WINPR_ASSERT(settings);
1360
1361 const char* pipe = freerdp_settings_get_string(settings, FreeRDP_KeyboardPipeName);
1362 WINPR_ASSERT(pipe);
1363
1364 const int rc = mkfifo(pipe, S_IWUSR | S_IRUSR);
1365 if (rc != 0)
1366 {
1367 char ebuffer[256] = { 0 };
1368 WLog_ERR(TAG, "Failed to create named pipe '%s': %s [%d]", pipe,
1369 winpr_strerror(errno, ebuffer, sizeof(ebuffer)), errno);
1370 return 0;
1371 }
1372
1373 void* ctx = WINPR_CAST_CONST_PTR_AWAY(pipe, void*);
1374 freerdp_add_signal_cleanup_handler(ctx, cleanup_pipe);
1375
1376 xf_process_pipe(context, pipe);
1377
1378 freerdp_del_signal_cleanup_handler(ctx, cleanup_pipe);
1379
1380 unlink(pipe);
1381 return 0;
1382}
1383
1389static BOOL xf_post_connect(freerdp* instance)
1390{
1391 ResizeWindowEventArgs e = { 0 };
1392
1393 WINPR_ASSERT(instance);
1394 xfContext* xfc = (xfContext*)instance->context;
1395 rdpContext* context = instance->context;
1396 WINPR_ASSERT(context);
1397
1398 rdpSettings* settings = context->settings;
1399 WINPR_ASSERT(settings);
1400
1401 rdpUpdate* update = context->update;
1402 WINPR_ASSERT(update);
1403
1404 if (freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode))
1405 xfc->remote_app = TRUE;
1406
1407 if (!xf_create_window(xfc))
1408 return FALSE;
1409
1410 if (!xf_get_pixmap_info(xfc))
1411 return FALSE;
1412
1413 if (!gdi_init(instance, xf_get_local_color_format(xfc, TRUE)))
1414 return FALSE;
1415
1416 if (!xf_create_image(xfc))
1417 return FALSE;
1418
1419 if (!xf_register_pointer(context->graphics))
1420 return FALSE;
1421
1422#ifdef WITH_XRENDER
1423 xfc->scaledWidth =
1424 WINPR_ASSERTING_INT_CAST(int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth));
1425 xfc->scaledHeight =
1426 WINPR_ASSERTING_INT_CAST(int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
1427 xfc->offset_x = 0;
1428 xfc->offset_y = 0;
1429#endif
1430
1431 if (!xfc->xrenderAvailable)
1432 {
1433 if (freerdp_settings_get_bool(settings, FreeRDP_SmartSizing))
1434 {
1435 WLog_ERR(TAG, "XRender not available: disabling smart-sizing");
1436 if (!freerdp_settings_set_bool(settings, FreeRDP_SmartSizing, FALSE))
1437 return FALSE;
1438 }
1439
1440 if (freerdp_settings_get_bool(settings, FreeRDP_MultiTouchGestures))
1441 {
1442 WLog_ERR(TAG, "XRender not available: disabling local multi-touch gestures");
1443 if (!freerdp_settings_set_bool(settings, FreeRDP_MultiTouchGestures, FALSE))
1444 return FALSE;
1445 }
1446 }
1447
1448 update->DesktopResize = xf_sw_desktop_resize;
1449 update->EndPaint = xf_end_paint;
1450 update->PlaySound = xf_play_sound;
1451 update->SetKeyboardIndicators = xf_keyboard_set_indicators;
1452 update->SetKeyboardImeStatus = xf_keyboard_set_ime_status;
1453
1454 const BOOL serverIsWindowsPlatform =
1455 (freerdp_settings_get_uint32(settings, FreeRDP_OsMajorType) == OSMAJORTYPE_WINDOWS);
1456 if (freerdp_settings_get_bool(settings, FreeRDP_RedirectClipboard) &&
1457 !(xfc->clipboard = xf_clipboard_new(xfc, !serverIsWindowsPlatform)))
1458 return FALSE;
1459
1460 if (!(xfc->xfDisp = xf_disp_new(xfc)))
1461 return FALSE;
1462
1463 const char* pipe = freerdp_settings_get_string(settings, FreeRDP_KeyboardPipeName);
1464 if (pipe)
1465 {
1466 xfc->pipethread = CreateThread(NULL, 0, xf_handle_pipe, xfc, 0, NULL);
1467 if (!xfc->pipethread)
1468 return FALSE;
1469 }
1470
1471 EventArgsInit(&e, "xfreerdp");
1472 e.width =
1473 WINPR_ASSERTING_INT_CAST(int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth));
1474 e.height =
1475 WINPR_ASSERTING_INT_CAST(int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
1476 PubSub_OnResizeWindow(context->pubSub, xfc, &e);
1477 return TRUE;
1478}
1479
1480static void xf_post_disconnect(freerdp* instance)
1481{
1482 xfContext* xfc = NULL;
1483 rdpContext* context = NULL;
1484
1485 if (!instance || !instance->context)
1486 return;
1487
1488 context = instance->context;
1489 xfc = (xfContext*)context;
1490 PubSub_UnsubscribeChannelConnected(instance->context->pubSub,
1491 xf_OnChannelConnectedEventHandler);
1492 PubSub_UnsubscribeChannelDisconnected(instance->context->pubSub,
1493 xf_OnChannelDisconnectedEventHandler);
1494 gdi_free(instance);
1495
1496 if (xfc->pipethread)
1497 {
1498 (void)WaitForSingleObject(xfc->pipethread, INFINITE);
1499 (void)CloseHandle(xfc->pipethread);
1500 xfc->pipethread = NULL;
1501 }
1502 if (xfc->clipboard)
1503 {
1504 xf_clipboard_free(xfc->clipboard);
1505 xfc->clipboard = NULL;
1506 }
1507
1508 if (xfc->xfDisp)
1509 {
1510 xf_disp_free(xfc->xfDisp);
1511 xfc->xfDisp = NULL;
1512 }
1513
1514 if ((xfc->window != NULL) && (xfc->drawable == xfc->window->handle))
1515 xfc->drawable = 0;
1516 else
1517 xf_DestroyDummyWindow(xfc, xfc->drawable);
1518
1519 freerdp_keyboard_remap_free(xfc->remap_table);
1520 xfc->remap_table = NULL;
1521
1522 xf_destroy_window(xfc);
1523}
1524
1525static void xf_post_final_disconnect(freerdp* instance)
1526{
1527 xfContext* xfc = NULL;
1528 rdpContext* context = NULL;
1529
1530 if (!instance || !instance->context)
1531 return;
1532
1533 context = instance->context;
1534 xfc = (xfContext*)context;
1535
1536 xf_keyboard_free(xfc);
1537 xf_teardown_x11(xfc);
1538}
1539
1540static int xf_logon_error_info(freerdp* instance, UINT32 data, UINT32 type)
1541{
1542 xfContext* xfc = (xfContext*)instance->context;
1543 const char* str_data = freerdp_get_logon_error_info_data(data);
1544 const char* str_type = freerdp_get_logon_error_info_type(type);
1545 WLog_INFO(TAG, "Logon Error Info %s [%s]", str_data, str_type);
1546 if (type != LOGON_MSG_SESSION_CONTINUE)
1547 {
1548 xf_rail_disable_remoteapp_mode(xfc);
1549 }
1550 return 1;
1551}
1552
1553static BOOL handle_window_events(freerdp* instance)
1554{
1555 if (!xf_process_x_events(instance))
1556 {
1557 WLog_DBG(TAG, "Closed from X11");
1558 freerdp_abort_connect_context(instance->context);
1559 return FALSE;
1560 }
1561
1562 return TRUE;
1563}
1564
1573static DWORD WINAPI xf_client_thread(LPVOID param)
1574{
1575 DWORD exit_code = 0;
1576 DWORD waitStatus = 0;
1577 HANDLE inputEvent = NULL;
1578
1579 freerdp* instance = (freerdp*)param;
1580 WINPR_ASSERT(instance);
1581
1582 const BOOL status = freerdp_connect(instance);
1583 rdpContext* context = instance->context;
1584 WINPR_ASSERT(context);
1585 xfContext* xfc = (xfContext*)instance->context;
1586 WINPR_ASSERT(xfc);
1587
1588 rdpSettings* settings = context->settings;
1589 WINPR_ASSERT(settings);
1590
1591 if (!status)
1592 {
1593 UINT32 error = freerdp_get_last_error(instance->context);
1594 exit_code = (uint32_t)xf_map_error_to_exit_code(error);
1595 }
1596 else
1597 exit_code = XF_EXIT_SUCCESS;
1598
1599 if (!status)
1600 goto end;
1601
1602 /* --authonly ? */
1603 if (freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
1604 {
1605 WLog_ERR(TAG, "Authentication only, exit status %" PRId32 "", !status);
1606 goto disconnect;
1607 }
1608
1609 if (!status)
1610 {
1611 WLog_ERR(TAG, "Freerdp connect error exit status %" PRId32 "", !status);
1612 exit_code = freerdp_error_info(instance);
1613
1614 if (freerdp_get_last_error(instance->context) == FREERDP_ERROR_AUTHENTICATION_FAILED)
1615 exit_code = XF_EXIT_AUTH_FAILURE;
1616 else if (exit_code == ERRINFO_SUCCESS)
1617 exit_code = XF_EXIT_CONN_FAILED;
1618
1619 goto disconnect;
1620 }
1621
1622 inputEvent = xfc->x11event;
1623
1624 while (!freerdp_shall_disconnect_context(instance->context))
1625 {
1626 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = { 0 };
1627 DWORD nCount = 0;
1628 handles[nCount++] = inputEvent;
1629
1630 /*
1631 * win8 and server 2k12 seem to have some timing issue/race condition
1632 * when a initial sync request is send to sync the keyboard indicators
1633 * sending the sync event twice fixed this problem
1634 */
1635 if (freerdp_focus_required(instance))
1636 {
1637 xf_keyboard_focus_in(xfc);
1638 xf_keyboard_focus_in(xfc);
1639 }
1640
1641 {
1642 DWORD tmp =
1643 freerdp_get_event_handles(context, &handles[nCount], ARRAYSIZE(handles) - nCount);
1644
1645 if (tmp == 0)
1646 {
1647 WLog_ERR(TAG, "freerdp_get_event_handles failed");
1648 break;
1649 }
1650
1651 nCount += tmp;
1652 }
1653
1654 if (xfc->window)
1655 xf_floatbar_hide_and_show(xfc->window->floatbar);
1656
1657 waitStatus = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
1658
1659 if (waitStatus == WAIT_FAILED)
1660 break;
1661
1662 {
1663 if (!freerdp_check_event_handles(context))
1664 {
1665 if (client_auto_reconnect_ex(instance, handle_window_events))
1666 continue;
1667 else
1668 {
1669 /*
1670 * Indicate an unsuccessful connection attempt if reconnect
1671 * did not succeed and no other error was specified.
1672 */
1673 const UINT32 error = freerdp_get_last_error(instance->context);
1674
1675 if (freerdp_error_info(instance) == 0)
1676 exit_code = (uint32_t)xf_map_error_to_exit_code(error);
1677 }
1678
1679 if (freerdp_get_last_error(context) == FREERDP_ERROR_SUCCESS)
1680 WLog_ERR(TAG, "Failed to check FreeRDP file descriptor");
1681
1682 break;
1683 }
1684 }
1685
1686 if (!handle_window_events(instance))
1687 break;
1688 }
1689
1690 if (!exit_code)
1691 {
1692 exit_code = freerdp_error_info(instance);
1693
1694 if (exit_code == XF_EXIT_DISCONNECT &&
1695 freerdp_get_disconnect_ultimatum(context) == Disconnect_Ultimatum_user_requested)
1696 {
1697 /* This situation might be limited to Windows XP. */
1698 WLog_INFO(TAG, "Error info says user did not initiate but disconnect ultimatum says "
1699 "they did; treat this as a user logoff");
1700 exit_code = XF_EXIT_LOGOFF;
1701 }
1702 }
1703
1704disconnect:
1705
1706 freerdp_disconnect(instance);
1707end:
1708 ExitThread(exit_code);
1709 return exit_code;
1710}
1711
1712int xf_exit_code_from_disconnect_reason(DWORD reason)
1713{
1714 if ((reason == 0) || ((reason >= XF_EXIT_PARSE_ARGUMENTS) && (reason <= XF_EXIT_CODE_LAST)))
1715 return WINPR_ASSERTING_INT_CAST(int, reason);
1716 /* License error set */
1717 else if (reason >= 0x100 && reason <= 0x10A)
1718 reason -= 0x100 + XF_EXIT_LICENSE_INTERNAL;
1719 /* RDP protocol error set */
1720 else if (reason >= 0x10c9 && reason <= 0x1193)
1721 reason = XF_EXIT_RDP;
1722 /* There's no need to test protocol-independent codes: they match */
1723 else if (!(reason <= 0xC))
1724 reason = XF_EXIT_UNKNOWN;
1725
1726 return WINPR_ASSERTING_INT_CAST(int, reason);
1727}
1728
1729static void xf_TerminateEventHandler(void* context, const TerminateEventArgs* e)
1730{
1731 rdpContext* ctx = (rdpContext*)context;
1732 WINPR_UNUSED(e);
1733 freerdp_abort_connect_context(ctx);
1734}
1735
1736#ifdef WITH_XRENDER
1737static void xf_ZoomingChangeEventHandler(void* context, const ZoomingChangeEventArgs* e)
1738{
1739 int w = 0;
1740 int h = 0;
1741 rdpSettings* settings = NULL;
1742 xfContext* xfc = (xfContext*)context;
1743
1744 WINPR_ASSERT(xfc);
1745
1746 settings = xfc->common.context.settings;
1747 WINPR_ASSERT(settings);
1748
1749 w = xfc->scaledWidth + e->dx;
1750 h = xfc->scaledHeight + e->dy;
1751
1752 if (e->dx == 0 && e->dy == 0)
1753 return;
1754
1755 if (w < 10)
1756 w = 10;
1757
1758 if (h < 10)
1759 h = 10;
1760
1761 if (w == xfc->scaledWidth && h == xfc->scaledHeight)
1762 return;
1763
1764 xfc->scaledWidth = w;
1765 xfc->scaledHeight = h;
1766 xf_draw_screen(xfc, 0, 0,
1767 WINPR_ASSERTING_INT_CAST(
1768 int32_t, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth)),
1769 WINPR_ASSERTING_INT_CAST(
1770 int32_t, freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight)));
1771}
1772
1773static void xf_PanningChangeEventHandler(void* context, const PanningChangeEventArgs* e)
1774{
1775 xfContext* xfc = (xfContext*)context;
1776 rdpSettings* settings = NULL;
1777
1778 WINPR_ASSERT(xfc);
1779 WINPR_ASSERT(e);
1780
1781 settings = xfc->common.context.settings;
1782 WINPR_ASSERT(settings);
1783
1784 if (e->dx == 0 && e->dy == 0)
1785 return;
1786
1787 xfc->offset_x += e->dx;
1788 xfc->offset_y += e->dy;
1789 xf_draw_screen(xfc, 0, 0,
1790 WINPR_ASSERTING_INT_CAST(
1791 int32_t, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth)),
1792 WINPR_ASSERTING_INT_CAST(
1793 int32_t, freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight)));
1794}
1795#endif
1796
1801static BOOL xfreerdp_client_global_init(void)
1802{
1803 // NOLINTNEXTLINE(concurrency-mt-unsafe)
1804 (void)setlocale(LC_ALL, "");
1805
1806 if (freerdp_handle_signals() != 0)
1807 return FALSE;
1808
1809 return TRUE;
1810}
1811
1812static void xfreerdp_client_global_uninit(void)
1813{
1814}
1815
1816static int xfreerdp_client_start(rdpContext* context)
1817{
1818 xfContext* xfc = (xfContext*)context;
1819 rdpSettings* settings = context->settings;
1820
1821 if (!freerdp_settings_get_string(settings, FreeRDP_ServerHostname))
1822 {
1823 WLog_ERR(TAG, "error: server hostname was not specified with /v:<server>[:port]");
1824 return -1;
1825 }
1826
1827 if (!(xfc->common.thread = CreateThread(NULL, 0, xf_client_thread, context->instance, 0, NULL)))
1828 {
1829 WLog_ERR(TAG, "failed to create client thread");
1830 return -1;
1831 }
1832
1833 return 0;
1834}
1835
1836static Atom get_supported_atom(xfContext* xfc, const char* atomName)
1837{
1838 const Atom atom = Logging_XInternAtom(xfc->log, xfc->display, atomName, False);
1839
1840 for (unsigned long i = 0; i < xfc->supportedAtomCount; i++)
1841 {
1842 if (xfc->supportedAtoms[i] == atom)
1843 return atom;
1844 }
1845
1846 return None;
1847}
1848
1849void xf_teardown_x11(xfContext* xfc)
1850{
1851 WINPR_ASSERT(xfc);
1852
1853 if (xfc->display)
1854 {
1855 LogDynAndXCloseDisplay(xfc->log, xfc->display);
1856 xfc->display = NULL;
1857 }
1858
1859 if (xfc->x11event)
1860 {
1861 (void)CloseHandle(xfc->x11event);
1862 xfc->x11event = NULL;
1863 }
1864
1865 if (xfc->mutex)
1866 {
1867 (void)CloseHandle(xfc->mutex);
1868 xfc->mutex = NULL;
1869 }
1870
1871 if (xfc->vscreen.monitors)
1872 {
1873 free(xfc->vscreen.monitors);
1874 xfc->vscreen.monitors = NULL;
1875 }
1876 xfc->vscreen.nmonitors = 0;
1877
1878 free(xfc->supportedAtoms);
1879 xfc->supportedAtoms = NULL;
1880 xfc->supportedAtomCount = 0;
1881}
1882
1883BOOL xf_setup_x11(xfContext* xfc)
1884{
1885
1886 WINPR_ASSERT(xfc);
1887 xfc->UseXThreads = TRUE;
1888
1889#if !defined(NDEBUG)
1890 /* uncomment below if debugging to prevent keyboard grab */
1891 xfc->debug = TRUE;
1892#endif
1893
1894 if (xfc->UseXThreads)
1895 {
1896 if (!XInitThreads())
1897 {
1898 WLog_WARN(TAG, "XInitThreads() failure");
1899 xfc->UseXThreads = FALSE;
1900 }
1901 }
1902
1903 xfc->display = XOpenDisplay(NULL);
1904
1905 if (!xfc->display)
1906 {
1907 WLog_ERR(TAG, "failed to open display: %s", XDisplayName(NULL));
1908 WLog_ERR(TAG, "Please check that the $DISPLAY environment variable is properly set.");
1909 goto fail;
1910 }
1911 if (xfc->debug)
1912 {
1913 WLog_INFO(TAG, "Enabling X11 debug mode.");
1914 XSynchronize(xfc->display, TRUE);
1915 }
1916 def_error_handler = XSetErrorHandler(xf_error_handler_ex);
1917
1918 xfc->mutex = CreateMutex(NULL, FALSE, NULL);
1919
1920 if (!xfc->mutex)
1921 {
1922 WLog_ERR(TAG, "Could not create mutex!");
1923 goto fail;
1924 }
1925
1926 xfc->xfds = ConnectionNumber(xfc->display);
1927 xfc->screen_number = DefaultScreen(xfc->display);
1928 xfc->screen = ScreenOfDisplay(xfc->display, xfc->screen_number);
1929 xfc->big_endian = (ImageByteOrder(xfc->display) == MSBFirst);
1930 xfc->invert = TRUE;
1931 xfc->complex_regions = TRUE;
1932 xfc->NET_SUPPORTED = Logging_XInternAtom(xfc->log, xfc->display, "_NET_SUPPORTED", True);
1933 xfc->NET_SUPPORTING_WM_CHECK =
1934 Logging_XInternAtom(xfc->log, xfc->display, "_NET_SUPPORTING_WM_CHECK", True);
1935
1936 if ((xfc->NET_SUPPORTED != None) && (xfc->NET_SUPPORTING_WM_CHECK != None))
1937 {
1938 Atom actual_type = 0;
1939 int actual_format = 0;
1940 unsigned long nitems = 0;
1941 unsigned long after = 0;
1942 unsigned char* data = NULL;
1943 int status = LogDynAndXGetWindowProperty(
1944 xfc->log, xfc->display, RootWindowOfScreen(xfc->screen), xfc->NET_SUPPORTED, 0, 1024,
1945 False, XA_ATOM, &actual_type, &actual_format, &nitems, &after, &data);
1946
1947 if ((status == Success) && (actual_type == XA_ATOM) && (actual_format == 32))
1948 {
1949 xfc->supportedAtomCount = nitems;
1950 xfc->supportedAtoms = calloc(xfc->supportedAtomCount, sizeof(Atom));
1951 WINPR_ASSERT(xfc->supportedAtoms);
1952 memcpy(xfc->supportedAtoms, data, nitems * sizeof(Atom));
1953 }
1954
1955 if (data)
1956 XFree(data);
1957 }
1958
1959 xfc->XWAYLAND_MAY_GRAB_KEYBOARD =
1960 Logging_XInternAtom(xfc->log, xfc->display, "_XWAYLAND_MAY_GRAB_KEYBOARD", False);
1961 xfc->NET_WM_ICON = Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ICON", False);
1962 xfc->MOTIF_WM_HINTS = Logging_XInternAtom(xfc->log, xfc->display, "_MOTIF_WM_HINTS", False);
1963 xfc->NET_NUMBER_OF_DESKTOPS =
1964 Logging_XInternAtom(xfc->log, xfc->display, "_NET_NUMBER_OF_DESKTOPS", False);
1965 xfc->NET_CURRENT_DESKTOP =
1966 Logging_XInternAtom(xfc->log, xfc->display, "_NET_CURRENT_DESKTOP", False);
1967 xfc->NET_WORKAREA = Logging_XInternAtom(xfc->log, xfc->display, "_NET_WORKAREA", False);
1968 xfc->NET_WM_STATE = get_supported_atom(xfc, "_NET_WM_STATE");
1969 xfc->NET_WM_STATE_MODAL = get_supported_atom(xfc, "_NET_WM_STATE_MODAL");
1970 xfc->NET_WM_STATE_STICKY = get_supported_atom(xfc, "_NET_WM_STATE_STICKY");
1971 xfc->NET_WM_STATE_MAXIMIZED_HORZ =
1972 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
1973 xfc->NET_WM_STATE_MAXIMIZED_VERT =
1974 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
1975 xfc->NET_WM_STATE_SHADED = get_supported_atom(xfc, "_NET_WM_STATE_SHADED");
1976 xfc->NET_WM_STATE_SKIP_TASKBAR = get_supported_atom(xfc, "_NET_WM_STATE_SKIP_TASKBAR");
1977 xfc->NET_WM_STATE_SKIP_PAGER = get_supported_atom(xfc, "_NET_WM_STATE_SKIP_PAGER");
1978 xfc->NET_WM_STATE_HIDDEN = get_supported_atom(xfc, "_NET_WM_STATE_HIDDEN");
1979 xfc->NET_WM_STATE_FULLSCREEN = get_supported_atom(xfc, "_NET_WM_STATE_FULLSCREEN");
1980 xfc->NET_WM_STATE_ABOVE = get_supported_atom(xfc, "_NET_WM_STATE_ABOVE");
1981 xfc->NET_WM_STATE_BELOW = get_supported_atom(xfc, "_NET_WM_STATE_BELOW");
1982 xfc->NET_WM_STATE_DEMANDS_ATTENTION =
1983 get_supported_atom(xfc, "_NET_WM_STATE_DEMANDS_ATTENTION");
1984 xfc->NET_WM_FULLSCREEN_MONITORS = get_supported_atom(xfc, "_NET_WM_FULLSCREEN_MONITORS");
1985 xfc->NET_WM_NAME = Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_NAME", False);
1986 xfc->NET_WM_PID = Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_PID", False);
1987 xfc->NET_WM_WINDOW_TYPE =
1988 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_WINDOW_TYPE", False);
1989 xfc->NET_WM_WINDOW_TYPE_NORMAL =
1990 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_WINDOW_TYPE_NORMAL", False);
1991 xfc->NET_WM_WINDOW_TYPE_DIALOG =
1992 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1993 xfc->NET_WM_WINDOW_TYPE_POPUP =
1994 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_WINDOW_TYPE_POPUP", False);
1995 xfc->NET_WM_WINDOW_TYPE_POPUP_MENU =
1996 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_WINDOW_TYPE_POPUP_MENU", False);
1997 xfc->NET_WM_WINDOW_TYPE_UTILITY =
1998 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_WINDOW_TYPE_UTILITY", False);
1999 xfc->NET_WM_WINDOW_TYPE_DROPDOWN_MENU =
2000 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", False);
2001 xfc->NET_WM_STATE_SKIP_TASKBAR =
2002 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_STATE_SKIP_TASKBAR", False);
2003 xfc->NET_WM_STATE_SKIP_PAGER =
2004 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_STATE_SKIP_PAGER", False);
2005 xfc->NET_WM_MOVERESIZE =
2006 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_MOVERESIZE", False);
2007 xfc->NET_MOVERESIZE_WINDOW =
2008 Logging_XInternAtom(xfc->log, xfc->display, "_NET_MOVERESIZE_WINDOW", False);
2009 xfc->UTF8_STRING = Logging_XInternAtom(xfc->log, xfc->display, "UTF8_STRING", FALSE);
2010 xfc->WM_PROTOCOLS = Logging_XInternAtom(xfc->log, xfc->display, "WM_PROTOCOLS", False);
2011 xfc->WM_DELETE_WINDOW = Logging_XInternAtom(xfc->log, xfc->display, "WM_DELETE_WINDOW", False);
2012 xfc->WM_STATE = Logging_XInternAtom(xfc->log, xfc->display, "WM_STATE", False);
2013 xfc->x11event = CreateFileDescriptorEvent(NULL, FALSE, FALSE, xfc->xfds, WINPR_FD_READ);
2014
2015 xfc->NET_WM_ALLOWED_ACTIONS =
2016 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ALLOWED_ACTIONS", False);
2017
2018 xfc->NET_WM_ACTION_CLOSE =
2019 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ACTION_CLOSE", False);
2020 xfc->NET_WM_ACTION_MINIMIZE =
2021 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ACTION_MINIMIZE", False);
2022 xfc->NET_WM_ACTION_MOVE =
2023 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ACTION_MOVE", False);
2024 xfc->NET_WM_ACTION_RESIZE =
2025 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ACTION_RESIZE", False);
2026 xfc->NET_WM_ACTION_MAXIMIZE_HORZ =
2027 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ACTION_MAXIMIZE_HORZ", False);
2028 xfc->NET_WM_ACTION_MAXIMIZE_VERT =
2029 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ACTION_MAXIMIZE_VERT", False);
2030 xfc->NET_WM_ACTION_FULLSCREEN =
2031 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ACTION_FULLSCREEN", False);
2032 xfc->NET_WM_ACTION_CHANGE_DESKTOP =
2033 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_ACTION_CHANGE_DESKTOP", False);
2034
2035 if (!xfc->x11event)
2036 {
2037 WLog_ERR(TAG, "Could not create xfds event");
2038 goto fail;
2039 }
2040
2041 xf_check_extensions(xfc);
2042
2043 xfc->vscreen.monitors = calloc(16, sizeof(MONITOR_INFO));
2044
2045 if (!xfc->vscreen.monitors)
2046 goto fail;
2047 return TRUE;
2048
2049fail:
2050 xf_teardown_x11(xfc);
2051 return FALSE;
2052}
2053
2054static BOOL xfreerdp_client_new(freerdp* instance, rdpContext* context)
2055{
2056 xfContext* xfc = (xfContext*)instance->context;
2057 WINPR_ASSERT(context);
2058 WINPR_ASSERT(xfc);
2059 WINPR_ASSERT(!xfc->display);
2060 WINPR_ASSERT(!xfc->mutex);
2061 WINPR_ASSERT(!xfc->x11event);
2062 instance->PreConnect = xf_pre_connect;
2063 instance->PostConnect = xf_post_connect;
2064 instance->PostDisconnect = xf_post_disconnect;
2065 instance->PostFinalDisconnect = xf_post_final_disconnect;
2066 instance->LogonErrorInfo = xf_logon_error_info;
2067 instance->GetAccessToken = client_cli_get_access_token;
2068 PubSub_SubscribeTerminate(context->pubSub, xf_TerminateEventHandler);
2069#ifdef WITH_XRENDER
2070 PubSub_SubscribeZoomingChange(context->pubSub, xf_ZoomingChangeEventHandler);
2071 PubSub_SubscribePanningChange(context->pubSub, xf_PanningChangeEventHandler);
2072#endif
2073 xfc->log = WLog_Get(TAG);
2074
2075 return TRUE;
2076}
2077
2078static void xfreerdp_client_free(WINPR_ATTR_UNUSED freerdp* instance, rdpContext* context)
2079{
2080 if (!context)
2081 return;
2082
2083 if (context->pubSub)
2084 {
2085 PubSub_UnsubscribeTerminate(context->pubSub, xf_TerminateEventHandler);
2086#ifdef WITH_XRENDER
2087 PubSub_UnsubscribeZoomingChange(context->pubSub, xf_ZoomingChangeEventHandler);
2088 PubSub_UnsubscribePanningChange(context->pubSub, xf_PanningChangeEventHandler);
2089#endif
2090 }
2091}
2092
2093int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
2094{
2095 pEntryPoints->Version = 1;
2096 pEntryPoints->Size = sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
2097 pEntryPoints->GlobalInit = xfreerdp_client_global_init;
2098 pEntryPoints->GlobalUninit = xfreerdp_client_global_uninit;
2099 pEntryPoints->ContextSize = sizeof(xfContext);
2100 pEntryPoints->ClientNew = xfreerdp_client_new;
2101 pEntryPoints->ClientFree = xfreerdp_client_free;
2102 pEntryPoints->ClientStart = xfreerdp_client_start;
2103 pEntryPoints->ClientStop = freerdp_client_common_stop;
2104 return 0;
2105}
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.