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