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