FreeRDP
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 
46 static BOOL xf_Pointer_Set(rdpContext* context, rdpPointer* pointer);
47 
48 BOOL 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 
104 static 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 */
246 static 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 
273 BOOL 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 
285 static 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 
324 fail:
325  WLog_DBG(TAG, "%p", rc ? pointer : NULL);
326  return rc;
327 }
328 
329 static 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 
356 static 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  return TRUE;
384 }
385 
386 static BOOL xf_Pointer_SetNull(rdpContext* context)
387 {
388  WLog_DBG(TAG, "called");
389 #ifdef WITH_XCURSOR
390  xfContext* xfc = (xfContext*)context;
391  static Cursor nullcursor = None;
392  Window handle = xf_Pointer_get_window(xfc);
393  xf_lock_x11(xfc);
394 
395  if (nullcursor == None)
396  {
397  XcursorImage ci = { 0 };
398  XcursorPixel xp = 0;
399 
400  ci.version = XCURSOR_IMAGE_VERSION;
401  ci.size = sizeof(ci);
402  ci.width = ci.height = 1;
403  ci.xhot = ci.yhot = 0;
404  ci.pixels = &xp;
405  nullcursor = XcursorImageLoadCursor(xfc->display, &ci);
406  }
407 
408  xfc->pointer = NULL;
409 
410  if ((handle) && (nullcursor != None))
411  XDefineCursor(xfc->display, handle, nullcursor);
412 
413  xf_unlock_x11(xfc);
414 #endif
415  return TRUE;
416 }
417 
418 static BOOL xf_Pointer_SetDefault(rdpContext* context)
419 {
420  WLog_DBG(TAG, "called");
421 #ifdef WITH_XCURSOR
422  xfContext* xfc = (xfContext*)context;
423  Window handle = xf_Pointer_get_window(xfc);
424  xf_lock_x11(xfc);
425  xfc->pointer = NULL;
426 
427  if (handle)
428  XUndefineCursor(xfc->display, handle);
429 
430  xf_unlock_x11(xfc);
431 #endif
432  return TRUE;
433 }
434 
435 static BOOL xf_Pointer_SetPosition(rdpContext* context, UINT32 x, UINT32 y)
436 {
437  xfContext* xfc = (xfContext*)context;
438  XWindowAttributes current = { 0 };
439  XSetWindowAttributes tmp = { 0 };
440  BOOL ret = FALSE;
441  Status rc = 0;
442  Window handle = xf_Pointer_get_window(xfc);
443 
444  if (!handle)
445  {
446  WLog_WARN(TAG, "focus %d, handle%lu", xfc->focused, handle);
447  return TRUE;
448  }
449 
450  WLog_DBG(TAG, "%" PRIu32 "x%" PRIu32, x, y);
451  if (!xfc->focused)
452  return TRUE;
453 
454  xf_adjust_coordinates_to_screen(xfc, &x, &y);
455 
456  xf_lock_x11(xfc);
457 
458  rc = XGetWindowAttributes(xfc->display, handle, &current);
459  if (rc == 0)
460  {
461  WLog_WARN(TAG, "XGetWindowAttributes==%d", rc);
462  goto out;
463  }
464 
465  tmp.event_mask = (current.your_event_mask & ~(PointerMotionMask));
466 
467  rc = XChangeWindowAttributes(xfc->display, handle, CWEventMask, &tmp);
468  if (rc == 0)
469  {
470  WLog_WARN(TAG, "XChangeWindowAttributes==%d", rc);
471  goto out;
472  }
473 
474  rc = XWarpPointer(xfc->display, handle, handle, 0, 0, 0, 0, WINPR_ASSERTING_INT_CAST(int, x),
475  WINPR_ASSERTING_INT_CAST(int, y));
476  if (rc == 0)
477  WLog_WARN(TAG, "XWarpPointer==%d", rc);
478  tmp.event_mask = current.your_event_mask;
479  rc = XChangeWindowAttributes(xfc->display, handle, CWEventMask, &tmp);
480  if (rc == 0)
481  WLog_WARN(TAG, "2.try XChangeWindowAttributes==%d", rc);
482  ret = TRUE;
483 out:
484  xf_unlock_x11(xfc);
485  return ret;
486 }
487 
488 /* Graphics Module */
489 BOOL xf_register_pointer(rdpGraphics* graphics)
490 {
491  rdpPointer pointer = { 0 };
492 
493  pointer.size = sizeof(xfPointer);
494  pointer.New = xf_Pointer_New;
495  pointer.Free = xf_Pointer_Free;
496  pointer.Set = xf_Pointer_Set;
497  pointer.SetNull = xf_Pointer_SetNull;
498  pointer.SetDefault = xf_Pointer_SetDefault;
499  pointer.SetPosition = xf_Pointer_SetPosition;
500  graphics_register_pointer(graphics, &pointer);
501  return TRUE;
502 }
503 
504 UINT32 xf_get_local_color_format(xfContext* xfc, BOOL aligned)
505 {
506  UINT32 DstFormat = 0;
507  BOOL invert = FALSE;
508 
509  if (!xfc)
510  return 0;
511 
512  invert = xfc->invert;
513 
514  WINPR_ASSERT(xfc->depth != 0);
515  if (xfc->depth == 32)
516  DstFormat = (!invert) ? PIXEL_FORMAT_RGBA32 : PIXEL_FORMAT_BGRA32;
517  else if (xfc->depth == 30)
518  DstFormat = (!invert) ? PIXEL_FORMAT_RGBX32_DEPTH30 : PIXEL_FORMAT_BGRX32_DEPTH30;
519  else if (xfc->depth == 24)
520  {
521  if (aligned)
522  DstFormat = (!invert) ? PIXEL_FORMAT_RGBX32 : PIXEL_FORMAT_BGRX32;
523  else
524  DstFormat = (!invert) ? PIXEL_FORMAT_RGB24 : PIXEL_FORMAT_BGR24;
525  }
526  else if (xfc->depth == 16)
527  DstFormat = PIXEL_FORMAT_RGB16;
528  else if (xfc->depth == 15)
529  DstFormat = PIXEL_FORMAT_RGB15;
530  else
531  DstFormat = (!invert) ? PIXEL_FORMAT_RGBX32 : PIXEL_FORMAT_BGRX32;
532 
533  return DstFormat;
534 }
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.