FreeRDP
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
xf_graphics.c
1
22#include <freerdp/config.h>
23
24#include <X11/Xlib.h>
25#include <X11/Xutil.h>
26
27#ifdef WITH_XCURSOR
28#include <X11/Xcursor/Xcursor.h>
29#endif
30
31#include <float.h>
32#include <math.h>
33
34#include <winpr/crt.h>
35#include <winpr/assert.h>
36
37#include <freerdp/codec/bitmap.h>
38#include <freerdp/codec/rfx.h>
39
40#include "xf_graphics.h"
41#include "xf_event.h"
42#include "xf_utils.h"
43
44#include <freerdp/log.h>
45#define TAG CLIENT_TAG("x11")
46
47static BOOL xf_Pointer_Set(rdpContext* context, rdpPointer* pointer);
48
49BOOL xf_decode_color(xfContext* xfc, const UINT32 srcColor, XColor* color)
50{
51 UINT32 SrcFormat = 0;
52 BYTE r = 0;
53 BYTE g = 0;
54 BYTE b = 0;
55 BYTE a = 0;
56
57 if (!xfc || !color)
58 return FALSE;
59
60 rdpGdi* gdi = xfc->common.context.gdi;
61
62 if (!gdi)
63 return FALSE;
64
65 rdpSettings* settings = xfc->common.context.settings;
66
67 if (!settings)
68 return FALSE;
69
70 switch (freerdp_settings_get_uint32(settings, FreeRDP_ColorDepth))
71 {
72 case 32:
73 case 24:
74 SrcFormat = PIXEL_FORMAT_BGR24;
75 break;
76
77 case 16:
78 SrcFormat = PIXEL_FORMAT_RGB16;
79 break;
80
81 case 15:
82 SrcFormat = PIXEL_FORMAT_RGB15;
83 break;
84
85 case 8:
86 SrcFormat = PIXEL_FORMAT_RGB8;
87 break;
88
89 default:
90 return FALSE;
91 }
92
93 FreeRDPSplitColor(srcColor, SrcFormat, &r, &g, &b, &a, &gdi->palette);
94 color->blue = (unsigned short)(b << 8);
95 color->green = (unsigned short)(g << 8);
96 color->red = (unsigned short)(r << 8);
97 color->flags = DoRed | DoGreen | DoBlue;
98
99 if (XAllocColor(xfc->display, xfc->colormap, color) == 0)
100 return FALSE;
101
102 return TRUE;
103}
104
105static BOOL xf_Pointer_GetCursorForCurrentScale(rdpContext* context, rdpPointer* pointer,
106 Cursor* cursor)
107{
108#if defined(WITH_XCURSOR) && defined(WITH_XRENDER)
109 xfContext* xfc = (xfContext*)context;
110 xfPointer* xpointer = (xfPointer*)pointer;
111 XcursorImage ci = { 0 };
112 int cursorIndex = -1;
113
114 if (!context || !pointer || !context->gdi)
115 return FALSE;
116
117 rdpSettings* settings = xfc->common.context.settings;
118
119 if (!settings)
120 return FALSE;
121
122 const double dw = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
123 const double xscale =
124 (freerdp_settings_get_bool(settings, FreeRDP_SmartSizing) ? 1.0 * xfc->scaledWidth / dw
125 : 1);
126 const double dh = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
127 const double yscale =
128 (freerdp_settings_get_bool(settings, FreeRDP_SmartSizing) ? 1.0 * xfc->scaledHeight / dh
129 : 1);
130 const UINT32 xTargetSize = MAX(1, (UINT32)lround(1.0 * pointer->width * xscale));
131 const UINT32 yTargetSize = MAX(1, (UINT32)lround(1.0 * pointer->height * yscale));
132
133 WLog_DBG(TAG, "scaled: %" PRIu32 "x%" PRIu32 ", desktop: %" PRIu32 "x%" PRIu32,
134 xfc->scaledWidth, xfc->scaledHeight,
135 freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
136 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
137 for (UINT32 i = 0; i < xpointer->nCursors; i++)
138 {
139 if ((xpointer->cursorWidths[i] == xTargetSize) &&
140 (xpointer->cursorHeights[i] == yTargetSize))
141 {
142 cursorIndex = WINPR_ASSERTING_INT_CAST(int, i);
143 }
144 }
145
146 if (cursorIndex == -1)
147 {
148 UINT32 CursorFormat = 0;
149 xf_lock_x11(xfc);
150
151 if (!xfc->invert)
152 CursorFormat = (!xfc->big_endian) ? PIXEL_FORMAT_RGBA32 : PIXEL_FORMAT_ABGR32;
153 else
154 CursorFormat = (!xfc->big_endian) ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_ARGB32;
155
156 if (xpointer->nCursors == xpointer->mCursors)
157 {
158 void* tmp2 = NULL;
159 xpointer->mCursors = (xpointer->mCursors == 0 ? 1 : xpointer->mCursors * 2);
160
161 tmp2 = realloc(xpointer->cursorWidths, sizeof(UINT32) * xpointer->mCursors);
162 if (!tmp2)
163 {
164 xf_unlock_x11(xfc);
165 return FALSE;
166 }
167 xpointer->cursorWidths = tmp2;
168
169 tmp2 = realloc(xpointer->cursorHeights, sizeof(UINT32) * xpointer->mCursors);
170 if (!tmp2)
171 {
172 xf_unlock_x11(xfc);
173 return FALSE;
174 }
175 xpointer->cursorHeights = (UINT32*)tmp2;
176
177 tmp2 = realloc(xpointer->cursors, sizeof(Cursor) * xpointer->mCursors);
178 if (!tmp2)
179 {
180 xf_unlock_x11(xfc);
181 return FALSE;
182 }
183 xpointer->cursors = (Cursor*)tmp2;
184 }
185
186 ci.version = XCURSOR_IMAGE_VERSION;
187 ci.size = sizeof(ci);
188 ci.width = xTargetSize;
189 ci.height = yTargetSize;
190 ci.xhot = (XcursorDim)lround(1.0 * pointer->xPos * xscale);
191 ci.yhot = (XcursorDim)lround(1.0 * pointer->yPos * yscale);
192 const size_t size = 1ull * ci.height * ci.width * FreeRDPGetBytesPerPixel(CursorFormat);
193
194 void* tmp = winpr_aligned_malloc(size, 16);
195 if (!tmp)
196 {
197 xf_unlock_x11(xfc);
198 return FALSE;
199 }
200 ci.pixels = (XcursorPixel*)tmp;
201
202 const double xs = fabs(fabs(xscale) - 1.0);
203 const double ys = fabs(fabs(yscale) - 1.0);
204
205 WLog_DBG(TAG,
206 "cursorIndex %" PRId32 " scaling pointer %" PRIu32 "x%" PRIu32 " --> %" PRIu32
207 "x%" PRIu32 " [%lfx%lf]",
208 cursorIndex, pointer->width, pointer->height, ci.width, ci.height, xscale, yscale);
209 if ((xs > DBL_EPSILON) || (ys > DBL_EPSILON))
210 {
211 if (!freerdp_image_scale((BYTE*)ci.pixels, CursorFormat, 0, 0, 0, ci.width, ci.height,
212 (BYTE*)xpointer->cursorPixels, CursorFormat, 0, 0, 0,
213 pointer->width, pointer->height))
214 {
215 winpr_aligned_free(tmp);
216 xf_unlock_x11(xfc);
217 return FALSE;
218 }
219 }
220 else
221 {
222 ci.pixels = xpointer->cursorPixels;
223 }
224
225 const size_t idx = xpointer->nCursors;
226 xpointer->cursorWidths[idx] = ci.width;
227 xpointer->cursorHeights[idx] = ci.height;
228 xpointer->cursors[idx] = XcursorImageLoadCursor(xfc->display, &ci);
229 cursorIndex = WINPR_ASSERTING_INT_CAST(int, idx);
230 xpointer->nCursors += 1;
231
232 winpr_aligned_free(tmp);
233
234 xf_unlock_x11(xfc);
235 }
236 else
237 {
238 WLog_DBG(TAG, "using cached cursor %" PRId32, cursorIndex);
239 }
240
241 cursor[0] = xpointer->cursors[cursorIndex];
242#endif
243 return TRUE;
244}
245
246/* Pointer Class */
247static Window xf_Pointer_get_window(xfContext* xfc)
248{
249 if (!xfc)
250 {
251 WLog_WARN(TAG, "xf_Pointer: Invalid context");
252 return 0;
253 }
254 if (xfc->remote_app)
255 {
256 if (!xfc->appWindow)
257 {
258 WLog_WARN(TAG, "xf_Pointer: Invalid appWindow");
259 return 0;
260 }
261 return xfc->appWindow->handle;
262 }
263 else
264 {
265 if (!xfc->window)
266 {
267 WLog_WARN(TAG, "xf_Pointer: Invalid window");
268 return 0;
269 }
270 return xfc->window->handle;
271 }
272}
273
274BOOL xf_pointer_update_scale(xfContext* xfc)
275{
276 xfPointer* pointer = NULL;
277 WINPR_ASSERT(xfc);
278
279 pointer = xfc->pointer;
280 if (!pointer)
281 return TRUE;
282
283 return xf_Pointer_Set(&xfc->common.context, &xfc->pointer->pointer);
284}
285
286static BOOL xf_Pointer_New(rdpContext* context, rdpPointer* pointer)
287{
288 BOOL rc = FALSE;
289
290#ifdef WITH_XCURSOR
291 UINT32 CursorFormat = 0;
292 size_t size = 0;
293 xfContext* xfc = (xfContext*)context;
294 xfPointer* xpointer = (xfPointer*)pointer;
295
296 if (!context || !pointer || !context->gdi)
297 goto fail;
298
299 if (!xfc->invert)
300 CursorFormat = (!xfc->big_endian) ? PIXEL_FORMAT_RGBA32 : PIXEL_FORMAT_ABGR32;
301 else
302 CursorFormat = (!xfc->big_endian) ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_ARGB32;
303
304 xpointer->nCursors = 0;
305 xpointer->mCursors = 0;
306
307 size = 1ull * pointer->height * pointer->width * FreeRDPGetBytesPerPixel(CursorFormat);
308
309 if (!(xpointer->cursorPixels = (XcursorPixel*)winpr_aligned_malloc(size, 16)))
310 goto fail;
311
312 if (!freerdp_image_copy_from_pointer_data(
313 (BYTE*)xpointer->cursorPixels, CursorFormat, 0, 0, 0, pointer->width, pointer->height,
314 pointer->xorMaskData, pointer->lengthXorMask, pointer->andMaskData,
315 pointer->lengthAndMask, pointer->xorBpp, &context->gdi->palette))
316 {
317 winpr_aligned_free(xpointer->cursorPixels);
318 goto fail;
319 }
320
321#endif
322
323 rc = TRUE;
324
325fail:
326 WLog_DBG(TAG, "%p", rc ? pointer : NULL);
327 return rc;
328}
329
330static void xf_Pointer_Free(rdpContext* context, rdpPointer* pointer)
331{
332 WLog_DBG(TAG, "%p", pointer);
333
334#ifdef WITH_XCURSOR
335 xfContext* xfc = (xfContext*)context;
336 xfPointer* xpointer = (xfPointer*)pointer;
337
338 xf_lock_x11(xfc);
339
340 winpr_aligned_free(xpointer->cursorPixels);
341 free(xpointer->cursorWidths);
342 free(xpointer->cursorHeights);
343
344 for (UINT32 i = 0; i < xpointer->nCursors; i++)
345 {
346 XFreeCursor(xfc->display, xpointer->cursors[i]);
347 }
348
349 free(xpointer->cursors);
350 xpointer->nCursors = 0;
351 xpointer->mCursors = 0;
352
353 xf_unlock_x11(xfc);
354#endif
355}
356
357static BOOL xf_Pointer_Set(rdpContext* context, rdpPointer* pointer)
358{
359 WLog_DBG(TAG, "%p", pointer);
360#ifdef WITH_XCURSOR
361 xfContext* xfc = (xfContext*)context;
362 Window handle = xf_Pointer_get_window(xfc);
363
364 WINPR_ASSERT(xfc);
365 WINPR_ASSERT(pointer);
366
367 xfc->pointer = (xfPointer*)pointer;
368
369 /* in RemoteApp mode, window can be null if none has had focus */
370
371 if (handle)
372 {
373 if (!xf_Pointer_GetCursorForCurrentScale(context, pointer, &(xfc->pointer->cursor)))
374 return FALSE;
375 xf_lock_x11(xfc);
376 XDefineCursor(xfc->display, handle, xfc->pointer->cursor);
377 xf_unlock_x11(xfc);
378 }
379 else
380 {
381 WLog_WARN(TAG, "handle=%ld", handle);
382 }
383 xfc->isCursorHidden = false;
384#endif
385 return TRUE;
386}
387
388static BOOL xf_Pointer_SetNull(rdpContext* context)
389{
390 WLog_DBG(TAG, "called");
391#ifdef WITH_XCURSOR
392 xfContext* xfc = (xfContext*)context;
393 static Cursor nullcursor = None;
394 Window handle = xf_Pointer_get_window(xfc);
395 xf_lock_x11(xfc);
396
397 if (nullcursor == None)
398 {
399 XcursorImage ci = { 0 };
400 XcursorPixel xp = 0;
401
402 ci.version = XCURSOR_IMAGE_VERSION;
403 ci.size = sizeof(ci);
404 ci.width = ci.height = 1;
405 ci.xhot = ci.yhot = 0;
406 ci.pixels = &xp;
407 nullcursor = XcursorImageLoadCursor(xfc->display, &ci);
408 }
409
410 xfc->pointer = NULL;
411
412 if ((handle) && (nullcursor != None))
413 XDefineCursor(xfc->display, handle, nullcursor);
414
415 xf_unlock_x11(xfc);
416 xfc->isCursorHidden = true;
417#endif
418 return TRUE;
419}
420
421static BOOL xf_Pointer_SetDefault(rdpContext* context)
422{
423 WLog_DBG(TAG, "called");
424#ifdef WITH_XCURSOR
425 xfContext* xfc = (xfContext*)context;
426 Window handle = xf_Pointer_get_window(xfc);
427 xf_lock_x11(xfc);
428 xfc->pointer = NULL;
429
430 if (handle)
431 XUndefineCursor(xfc->display, handle);
432
433 xf_unlock_x11(xfc);
434 xfc->isCursorHidden = false;
435#endif
436 return TRUE;
437}
438
439static BOOL xf_Pointer_SetPosition(rdpContext* context, UINT32 x, UINT32 y)
440{
441 xfContext* xfc = (xfContext*)context;
442 XWindowAttributes current = { 0 };
443 XSetWindowAttributes tmp = { 0 };
444 BOOL ret = FALSE;
445 Status rc = 0;
446 Window handle = xf_Pointer_get_window(xfc);
447
448 if (!handle)
449 {
450 WLog_WARN(TAG, "focus %d, handle%lu", xfc->focused, handle);
451 return TRUE;
452 }
453
454 WLog_DBG(TAG, "%" PRIu32 "x%" PRIu32, x, y);
455 if (!xfc->focused)
456 return TRUE;
457
458 xf_adjust_coordinates_to_screen(xfc, &x, &y);
459
460 xf_lock_x11(xfc);
461
462 rc = XGetWindowAttributes(xfc->display, handle, &current);
463 if (rc == 0)
464 {
465 WLog_WARN(TAG, "XGetWindowAttributes==%d", rc);
466 goto out;
467 }
468
469 tmp.event_mask = (current.your_event_mask & ~(PointerMotionMask));
470
471 rc = LogDynAndXChangeWindowAttributes(xfc->log, xfc->display, handle, CWEventMask, &tmp);
472 if (rc == 0)
473 goto out;
474
475 rc = XWarpPointer(xfc->display, handle, handle, 0, 0, 0, 0, WINPR_ASSERTING_INT_CAST(int, x),
476 WINPR_ASSERTING_INT_CAST(int, y));
477 if (rc == 0)
478 WLog_WARN(TAG, "XWarpPointer==%d", rc);
479 tmp.event_mask = current.your_event_mask;
480 LogDynAndXChangeWindowAttributes(xfc->log, xfc->display, handle, CWEventMask, &tmp);
481 ret = TRUE;
482out:
483 xf_unlock_x11(xfc);
484 return ret;
485}
486
487/* Graphics Module */
488BOOL xf_register_pointer(rdpGraphics* graphics)
489{
490 rdpPointer pointer = { 0 };
491
492 pointer.size = sizeof(xfPointer);
493 pointer.New = xf_Pointer_New;
494 pointer.Free = xf_Pointer_Free;
495 pointer.Set = xf_Pointer_Set;
496 pointer.SetNull = xf_Pointer_SetNull;
497 pointer.SetDefault = xf_Pointer_SetDefault;
498 pointer.SetPosition = xf_Pointer_SetPosition;
499 graphics_register_pointer(graphics, &pointer);
500 return TRUE;
501}
502
503UINT32 xf_get_local_color_format(xfContext* xfc, BOOL aligned)
504{
505 UINT32 DstFormat = 0;
506 BOOL invert = FALSE;
507
508 if (!xfc)
509 return 0;
510
511 invert = xfc->invert;
512
513 WINPR_ASSERT(xfc->depth != 0);
514 if (xfc->depth == 32)
515 DstFormat = (!invert) ? PIXEL_FORMAT_RGBA32 : PIXEL_FORMAT_BGRA32;
516 else if (xfc->depth == 30)
517 DstFormat = (!invert) ? PIXEL_FORMAT_RGBX32_DEPTH30 : PIXEL_FORMAT_BGRX32_DEPTH30;
518 else if (xfc->depth == 24)
519 {
520 if (aligned)
521 DstFormat = (!invert) ? PIXEL_FORMAT_RGBX32 : PIXEL_FORMAT_BGRX32;
522 else
523 DstFormat = (!invert) ? PIXEL_FORMAT_RGB24 : PIXEL_FORMAT_BGR24;
524 }
525 else if (xfc->depth == 16)
526 DstFormat = PIXEL_FORMAT_RGB16;
527 else if (xfc->depth == 15)
528 DstFormat = PIXEL_FORMAT_RGB15;
529 else
530 DstFormat = (!invert) ? PIXEL_FORMAT_RGBX32 : PIXEL_FORMAT_BGRX32;
531
532 return DstFormat;
533}
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_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.