22 #include <freerdp/config.h>
25 #include <X11/Xutil.h>
28 #include <X11/Xcursor/Xcursor.h>
34 #include <winpr/crt.h>
35 #include <winpr/assert.h>
37 #include <freerdp/codec/bitmap.h>
38 #include <freerdp/codec/rfx.h>
40 #include "xf_graphics.h"
43 #include <freerdp/log.h>
44 #define TAG CLIENT_TAG("x11")
46 static BOOL xf_Pointer_Set(rdpContext* context, rdpPointer* pointer);
48 BOOL xf_decode_color(xfContext* xfc,
const UINT32 srcColor, XColor* color)
59 rdpGdi* gdi = xfc->common.context.gdi;
64 rdpSettings* settings = xfc->common.context.settings;
73 SrcFormat = PIXEL_FORMAT_BGR24;
77 SrcFormat = PIXEL_FORMAT_RGB16;
81 SrcFormat = PIXEL_FORMAT_RGB15;
85 SrcFormat = PIXEL_FORMAT_RGB8;
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;
98 if (XAllocColor(xfc->display, xfc->colormap, color) == 0)
104 static BOOL xf_Pointer_GetCursorForCurrentScale(rdpContext* context, rdpPointer* pointer,
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;
113 if (!context || !pointer || !context->gdi)
116 rdpSettings* settings = xfc->common.context.settings;
122 const double xscale =
126 const double yscale =
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));
132 WLog_DBG(TAG,
"scaled: %" PRIu32
"x%" PRIu32
", desktop: %" PRIu32
"x%" PRIu32,
133 xfc->scaledWidth, xfc->scaledHeight,
136 for (UINT32 i = 0; i < xpointer->nCursors; i++)
138 if ((xpointer->cursorWidths[i] == xTargetSize) &&
139 (xpointer->cursorHeights[i] == yTargetSize))
145 if (cursorIndex == -1)
147 UINT32 CursorFormat = 0;
151 CursorFormat = (!xfc->big_endian) ? PIXEL_FORMAT_RGBA32 : PIXEL_FORMAT_ABGR32;
153 CursorFormat = (!xfc->big_endian) ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_ARGB32;
155 if (xpointer->nCursors == xpointer->mCursors)
158 xpointer->mCursors = (xpointer->mCursors == 0 ? 1 : xpointer->mCursors * 2);
160 tmp2 = realloc(xpointer->cursorWidths,
sizeof(UINT32) * xpointer->mCursors);
166 xpointer->cursorWidths = tmp2;
168 tmp2 = realloc(xpointer->cursorHeights,
sizeof(UINT32) * xpointer->mCursors);
174 xpointer->cursorHeights = (UINT32*)tmp2;
176 tmp2 = realloc(xpointer->cursors,
sizeof(Cursor) * xpointer->mCursors);
182 xpointer->cursors = (Cursor*)tmp2;
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);
193 void* tmp = winpr_aligned_malloc(size, 16);
199 ci.pixels = (XcursorPixel*)tmp;
201 const double xs = fabs(fabs(xscale) - 1.0);
202 const double ys = fabs(fabs(yscale) - 1.0);
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))
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))
214 winpr_aligned_free(tmp);
221 ci.pixels = xpointer->cursorPixels;
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;
230 winpr_aligned_free(tmp);
236 WLog_DBG(TAG,
"using cached cursor %" PRId32, cursorIndex);
239 cursor[0] = xpointer->cursors[cursorIndex];
245 static Window xf_Pointer_get_window(xfContext* xfc)
249 WLog_WARN(TAG,
"xf_Pointer: Invalid context");
256 WLog_WARN(TAG,
"xf_Pointer: Invalid appWindow");
259 return xfc->appWindow->handle;
265 WLog_WARN(TAG,
"xf_Pointer: Invalid window");
268 return xfc->window->handle;
272 BOOL xf_pointer_update_scale(xfContext* xfc)
274 xfPointer* pointer = NULL;
277 pointer = xfc->pointer;
281 return xf_Pointer_Set(&xfc->common.context, &xfc->pointer->pointer);
284 static BOOL xf_Pointer_New(rdpContext* context, rdpPointer* pointer)
289 UINT32 CursorFormat = 0;
291 xfContext* xfc = (xfContext*)context;
292 xfPointer* xpointer = (xfPointer*)pointer;
294 if (!context || !pointer || !context->gdi)
298 CursorFormat = (!xfc->big_endian) ? PIXEL_FORMAT_RGBA32 : PIXEL_FORMAT_ABGR32;
300 CursorFormat = (!xfc->big_endian) ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_ARGB32;
302 xpointer->nCursors = 0;
303 xpointer->mCursors = 0;
305 size = 1ull * pointer->height * pointer->width * FreeRDPGetBytesPerPixel(CursorFormat);
307 if (!(xpointer->cursorPixels = (XcursorPixel*)winpr_aligned_malloc(size, 16)))
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))
315 winpr_aligned_free(xpointer->cursorPixels);
324 WLog_DBG(TAG,
"%p", rc ? pointer : NULL);
328 static void xf_Pointer_Free(rdpContext* context, rdpPointer* pointer)
330 WLog_DBG(TAG,
"%p", pointer);
333 xfContext* xfc = (xfContext*)context;
334 xfPointer* xpointer = (xfPointer*)pointer;
338 winpr_aligned_free(xpointer->cursorPixels);
339 free(xpointer->cursorWidths);
340 free(xpointer->cursorHeights);
342 for (UINT32 i = 0; i < xpointer->nCursors; i++)
344 XFreeCursor(xfc->display, xpointer->cursors[i]);
347 free(xpointer->cursors);
348 xpointer->nCursors = 0;
349 xpointer->mCursors = 0;
355 static BOOL xf_Pointer_Set(rdpContext* context, rdpPointer* pointer)
357 WLog_DBG(TAG,
"%p", pointer);
359 xfContext* xfc = (xfContext*)context;
360 Window handle = xf_Pointer_get_window(xfc);
363 WINPR_ASSERT(pointer);
365 xfc->pointer = (xfPointer*)pointer;
371 if (!xf_Pointer_GetCursorForCurrentScale(context, pointer, &(xfc->pointer->cursor)))
374 XDefineCursor(xfc->display, handle, xfc->pointer->cursor);
379 WLog_WARN(TAG,
"handle=%ld", handle);
385 static BOOL xf_Pointer_SetNull(rdpContext* context)
387 WLog_DBG(TAG,
"called");
389 xfContext* xfc = (xfContext*)context;
390 static Cursor nullcursor = None;
391 Window handle = xf_Pointer_get_window(xfc);
394 if (nullcursor == None)
396 XcursorImage ci = { 0 };
399 ci.version = XCURSOR_IMAGE_VERSION;
400 ci.size =
sizeof(ci);
401 ci.width = ci.height = 1;
402 ci.xhot = ci.yhot = 0;
404 nullcursor = XcursorImageLoadCursor(xfc->display, &ci);
409 if ((handle) && (nullcursor != None))
410 XDefineCursor(xfc->display, handle, nullcursor);
417 static BOOL xf_Pointer_SetDefault(rdpContext* context)
419 WLog_DBG(TAG,
"called");
421 xfContext* xfc = (xfContext*)context;
422 Window handle = xf_Pointer_get_window(xfc);
427 XUndefineCursor(xfc->display, handle);
434 static BOOL xf_Pointer_SetPosition(rdpContext* context, UINT32 x, UINT32 y)
436 xfContext* xfc = (xfContext*)context;
437 XWindowAttributes current = { 0 };
438 XSetWindowAttributes tmp = { 0 };
441 Window handle = xf_Pointer_get_window(xfc);
445 WLog_WARN(TAG,
"focus %d, handle%lu", xfc->focused, handle);
449 WLog_DBG(TAG,
"%" PRIu32
"x%" PRIu32, x, y);
453 xf_adjust_coordinates_to_screen(xfc, &x, &y);
457 rc = XGetWindowAttributes(xfc->display, handle, ¤t);
460 WLog_WARN(TAG,
"XGetWindowAttributes==%d", rc);
464 tmp.event_mask = (current.your_event_mask & ~(PointerMotionMask));
466 rc = XChangeWindowAttributes(xfc->display, handle, CWEventMask, &tmp);
469 WLog_WARN(TAG,
"XChangeWindowAttributes==%d", rc);
473 rc = XWarpPointer(xfc->display, handle, handle, 0, 0, 0, 0, x, y);
475 WLog_WARN(TAG,
"XWarpPointer==%d", rc);
476 tmp.event_mask = current.your_event_mask;
477 rc = XChangeWindowAttributes(xfc->display, handle, CWEventMask, &tmp);
479 WLog_WARN(TAG,
"2.try XChangeWindowAttributes==%d", rc);
487 BOOL xf_register_pointer(rdpGraphics* graphics)
489 rdpPointer pointer = { 0 };
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);
502 UINT32 xf_get_local_color_format(xfContext* xfc, BOOL aligned)
504 UINT32 DstFormat = 0;
510 invert = xfc->invert;
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)
520 DstFormat = (!invert) ? PIXEL_FORMAT_RGBX32 : PIXEL_FORMAT_BGRX32;
522 DstFormat = (!invert) ? PIXEL_FORMAT_RGB24 : PIXEL_FORMAT_BGR24;
524 else if (xfc->depth == 16)
525 DstFormat = PIXEL_FORMAT_RGB16;
526 else if (xfc->depth == 15)
527 DstFormat = PIXEL_FORMAT_RGB15;
529 DstFormat = (!invert) ? PIXEL_FORMAT_RGBX32 : PIXEL_FORMAT_BGRX32;
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.