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 = 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  cursorIndex = xpointer->nCursors;
225  xpointer->cursorWidths[cursorIndex] = ci.width;
226  xpointer->cursorHeights[cursorIndex] = ci.height;
227  xpointer->cursors[cursorIndex] = XcursorImageLoadCursor(xfc->display, &ci);
228  xpointer->nCursors += 1;
229 
230  winpr_aligned_free(tmp);
231 
232  xf_unlock_x11(xfc);
233  }
234  else
235  {
236  WLog_DBG(TAG, "using cached cursor %" PRId32, cursorIndex);
237  }
238 
239  cursor[0] = xpointer->cursors[cursorIndex];
240 #endif
241  return TRUE;
242 }
243 
244 /* Pointer Class */
245 static Window xf_Pointer_get_window(xfContext* xfc)
246 {
247  if (!xfc)
248  {
249  WLog_WARN(TAG, "xf_Pointer: Invalid context");
250  return 0;
251  }
252  if (xfc->remote_app)
253  {
254  if (!xfc->appWindow)
255  {
256  WLog_WARN(TAG, "xf_Pointer: Invalid appWindow");
257  return 0;
258  }
259  return xfc->appWindow->handle;
260  }
261  else
262  {
263  if (!xfc->window)
264  {
265  WLog_WARN(TAG, "xf_Pointer: Invalid window");
266  return 0;
267  }
268  return xfc->window->handle;
269  }
270 }
271 
272 BOOL xf_pointer_update_scale(xfContext* xfc)
273 {
274  xfPointer* pointer = NULL;
275  WINPR_ASSERT(xfc);
276 
277  pointer = xfc->pointer;
278  if (!pointer)
279  return TRUE;
280 
281  return xf_Pointer_Set(&xfc->common.context, &xfc->pointer->pointer);
282 }
283 
284 static BOOL xf_Pointer_New(rdpContext* context, rdpPointer* pointer)
285 {
286  BOOL rc = FALSE;
287 
288 #ifdef WITH_XCURSOR
289  UINT32 CursorFormat = 0;
290  size_t size = 0;
291  xfContext* xfc = (xfContext*)context;
292  xfPointer* xpointer = (xfPointer*)pointer;
293 
294  if (!context || !pointer || !context->gdi)
295  goto fail;
296 
297  if (!xfc->invert)
298  CursorFormat = (!xfc->big_endian) ? PIXEL_FORMAT_RGBA32 : PIXEL_FORMAT_ABGR32;
299  else
300  CursorFormat = (!xfc->big_endian) ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_ARGB32;
301 
302  xpointer->nCursors = 0;
303  xpointer->mCursors = 0;
304 
305  size = 1ull * pointer->height * pointer->width * FreeRDPGetBytesPerPixel(CursorFormat);
306 
307  if (!(xpointer->cursorPixels = (XcursorPixel*)winpr_aligned_malloc(size, 16)))
308  goto fail;
309 
310  if (!freerdp_image_copy_from_pointer_data(
311  (BYTE*)xpointer->cursorPixels, CursorFormat, 0, 0, 0, pointer->width, pointer->height,
312  pointer->xorMaskData, pointer->lengthXorMask, pointer->andMaskData,
313  pointer->lengthAndMask, pointer->xorBpp, &context->gdi->palette))
314  {
315  winpr_aligned_free(xpointer->cursorPixels);
316  goto fail;
317  }
318 
319 #endif
320 
321  rc = TRUE;
322 
323 fail:
324  WLog_DBG(TAG, "%p", rc ? pointer : NULL);
325  return rc;
326 }
327 
328 static void xf_Pointer_Free(rdpContext* context, rdpPointer* pointer)
329 {
330  WLog_DBG(TAG, "%p", pointer);
331 
332 #ifdef WITH_XCURSOR
333  xfContext* xfc = (xfContext*)context;
334  xfPointer* xpointer = (xfPointer*)pointer;
335 
336  xf_lock_x11(xfc);
337 
338  winpr_aligned_free(xpointer->cursorPixels);
339  free(xpointer->cursorWidths);
340  free(xpointer->cursorHeights);
341 
342  for (UINT32 i = 0; i < xpointer->nCursors; i++)
343  {
344  XFreeCursor(xfc->display, xpointer->cursors[i]);
345  }
346 
347  free(xpointer->cursors);
348  xpointer->nCursors = 0;
349  xpointer->mCursors = 0;
350 
351  xf_unlock_x11(xfc);
352 #endif
353 }
354 
355 static BOOL xf_Pointer_Set(rdpContext* context, rdpPointer* pointer)
356 {
357  WLog_DBG(TAG, "%p", pointer);
358 #ifdef WITH_XCURSOR
359  xfContext* xfc = (xfContext*)context;
360  Window handle = xf_Pointer_get_window(xfc);
361 
362  WINPR_ASSERT(xfc);
363  WINPR_ASSERT(pointer);
364 
365  xfc->pointer = (xfPointer*)pointer;
366 
367  /* in RemoteApp mode, window can be null if none has had focus */
368 
369  if (handle)
370  {
371  if (!xf_Pointer_GetCursorForCurrentScale(context, pointer, &(xfc->pointer->cursor)))
372  return FALSE;
373  xf_lock_x11(xfc);
374  XDefineCursor(xfc->display, handle, xfc->pointer->cursor);
375  xf_unlock_x11(xfc);
376  }
377  else
378  {
379  WLog_WARN(TAG, "handle=%ld", handle);
380  }
381 #endif
382  return TRUE;
383 }
384 
385 static BOOL xf_Pointer_SetNull(rdpContext* context)
386 {
387  WLog_DBG(TAG, "called");
388 #ifdef WITH_XCURSOR
389  xfContext* xfc = (xfContext*)context;
390  static Cursor nullcursor = None;
391  Window handle = xf_Pointer_get_window(xfc);
392  xf_lock_x11(xfc);
393 
394  if (nullcursor == None)
395  {
396  XcursorImage ci = { 0 };
397  XcursorPixel xp = 0;
398 
399  ci.version = XCURSOR_IMAGE_VERSION;
400  ci.size = sizeof(ci);
401  ci.width = ci.height = 1;
402  ci.xhot = ci.yhot = 0;
403  ci.pixels = &xp;
404  nullcursor = XcursorImageLoadCursor(xfc->display, &ci);
405  }
406 
407  xfc->pointer = NULL;
408 
409  if ((handle) && (nullcursor != None))
410  XDefineCursor(xfc->display, handle, nullcursor);
411 
412  xf_unlock_x11(xfc);
413 #endif
414  return TRUE;
415 }
416 
417 static BOOL xf_Pointer_SetDefault(rdpContext* context)
418 {
419  WLog_DBG(TAG, "called");
420 #ifdef WITH_XCURSOR
421  xfContext* xfc = (xfContext*)context;
422  Window handle = xf_Pointer_get_window(xfc);
423  xf_lock_x11(xfc);
424  xfc->pointer = NULL;
425 
426  if (handle)
427  XUndefineCursor(xfc->display, handle);
428 
429  xf_unlock_x11(xfc);
430 #endif
431  return TRUE;
432 }
433 
434 static BOOL xf_Pointer_SetPosition(rdpContext* context, UINT32 x, UINT32 y)
435 {
436  xfContext* xfc = (xfContext*)context;
437  XWindowAttributes current = { 0 };
438  XSetWindowAttributes tmp = { 0 };
439  BOOL ret = FALSE;
440  Status rc = 0;
441  Window handle = xf_Pointer_get_window(xfc);
442 
443  if (!handle)
444  {
445  WLog_WARN(TAG, "focus %d, handle%lu", xfc->focused, handle);
446  return TRUE;
447  }
448 
449  WLog_DBG(TAG, "%" PRIu32 "x%" PRIu32, x, y);
450  if (!xfc->focused)
451  return TRUE;
452 
453  xf_adjust_coordinates_to_screen(xfc, &x, &y);
454 
455  xf_lock_x11(xfc);
456 
457  rc = XGetWindowAttributes(xfc->display, handle, &current);
458  if (rc == 0)
459  {
460  WLog_WARN(TAG, "XGetWindowAttributes==%d", rc);
461  goto out;
462  }
463 
464  tmp.event_mask = (current.your_event_mask & ~(PointerMotionMask));
465 
466  rc = XChangeWindowAttributes(xfc->display, handle, CWEventMask, &tmp);
467  if (rc == 0)
468  {
469  WLog_WARN(TAG, "XChangeWindowAttributes==%d", rc);
470  goto out;
471  }
472 
473  rc = XWarpPointer(xfc->display, handle, handle, 0, 0, 0, 0, x, y);
474  if (rc == 0)
475  WLog_WARN(TAG, "XWarpPointer==%d", rc);
476  tmp.event_mask = current.your_event_mask;
477  rc = XChangeWindowAttributes(xfc->display, handle, CWEventMask, &tmp);
478  if (rc == 0)
479  WLog_WARN(TAG, "2.try XChangeWindowAttributes==%d", rc);
480  ret = TRUE;
481 out:
482  xf_unlock_x11(xfc);
483  return ret;
484 }
485 
486 /* Graphics Module */
487 BOOL xf_register_pointer(rdpGraphics* graphics)
488 {
489  rdpPointer pointer = { 0 };
490 
491  pointer.size = sizeof(xfPointer);
492  pointer.New = xf_Pointer_New;
493  pointer.Free = xf_Pointer_Free;
494  pointer.Set = xf_Pointer_Set;
495  pointer.SetNull = xf_Pointer_SetNull;
496  pointer.SetDefault = xf_Pointer_SetDefault;
497  pointer.SetPosition = xf_Pointer_SetPosition;
498  graphics_register_pointer(graphics, &pointer);
499  return TRUE;
500 }
501 
502 UINT32 xf_get_local_color_format(xfContext* xfc, BOOL aligned)
503 {
504  UINT32 DstFormat = 0;
505  BOOL invert = FALSE;
506 
507  if (!xfc)
508  return 0;
509 
510  invert = xfc->invert;
511 
512  WINPR_ASSERT(xfc->depth != 0);
513  if (xfc->depth == 32)
514  DstFormat = (!invert) ? PIXEL_FORMAT_RGBA32 : PIXEL_FORMAT_BGRA32;
515  else if (xfc->depth == 30)
516  DstFormat = (!invert) ? PIXEL_FORMAT_RGBX32_DEPTH30 : PIXEL_FORMAT_BGRX32_DEPTH30;
517  else if (xfc->depth == 24)
518  {
519  if (aligned)
520  DstFormat = (!invert) ? PIXEL_FORMAT_RGBX32 : PIXEL_FORMAT_BGRX32;
521  else
522  DstFormat = (!invert) ? PIXEL_FORMAT_RGB24 : PIXEL_FORMAT_BGR24;
523  }
524  else if (xfc->depth == 16)
525  DstFormat = PIXEL_FORMAT_RGB16;
526  else if (xfc->depth == 15)
527  DstFormat = PIXEL_FORMAT_RGB15;
528  else
529  DstFormat = (!invert) ? PIXEL_FORMAT_RGBX32 : PIXEL_FORMAT_BGRX32;
530 
531  return DstFormat;
532 }
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.