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))
141 cursorIndex = WINPR_ASSERTING_INT_CAST(
int, i);
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 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;
231 winpr_aligned_free(tmp);
237 WLog_DBG(TAG,
"using cached cursor %" PRId32, cursorIndex);
240 cursor[0] = xpointer->cursors[cursorIndex];
246 static Window xf_Pointer_get_window(xfContext* xfc)
250 WLog_WARN(TAG,
"xf_Pointer: Invalid context");
257 WLog_WARN(TAG,
"xf_Pointer: Invalid appWindow");
260 return xfc->appWindow->handle;
266 WLog_WARN(TAG,
"xf_Pointer: Invalid window");
269 return xfc->window->handle;
273 BOOL xf_pointer_update_scale(xfContext* xfc)
275 xfPointer* pointer = NULL;
278 pointer = xfc->pointer;
282 return xf_Pointer_Set(&xfc->common.context, &xfc->pointer->pointer);
285 static BOOL xf_Pointer_New(rdpContext* context, rdpPointer* pointer)
290 UINT32 CursorFormat = 0;
292 xfContext* xfc = (xfContext*)context;
293 xfPointer* xpointer = (xfPointer*)pointer;
295 if (!context || !pointer || !context->gdi)
299 CursorFormat = (!xfc->big_endian) ? PIXEL_FORMAT_RGBA32 : PIXEL_FORMAT_ABGR32;
301 CursorFormat = (!xfc->big_endian) ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_ARGB32;
303 xpointer->nCursors = 0;
304 xpointer->mCursors = 0;
306 size = 1ull * pointer->height * pointer->width * FreeRDPGetBytesPerPixel(CursorFormat);
308 if (!(xpointer->cursorPixels = (XcursorPixel*)winpr_aligned_malloc(size, 16)))
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))
316 winpr_aligned_free(xpointer->cursorPixels);
325 WLog_DBG(TAG,
"%p", rc ? pointer : NULL);
329 static void xf_Pointer_Free(rdpContext* context, rdpPointer* pointer)
331 WLog_DBG(TAG,
"%p", pointer);
334 xfContext* xfc = (xfContext*)context;
335 xfPointer* xpointer = (xfPointer*)pointer;
339 winpr_aligned_free(xpointer->cursorPixels);
340 free(xpointer->cursorWidths);
341 free(xpointer->cursorHeights);
343 for (UINT32 i = 0; i < xpointer->nCursors; i++)
345 XFreeCursor(xfc->display, xpointer->cursors[i]);
348 free(xpointer->cursors);
349 xpointer->nCursors = 0;
350 xpointer->mCursors = 0;
356 static BOOL xf_Pointer_Set(rdpContext* context, rdpPointer* pointer)
358 WLog_DBG(TAG,
"%p", pointer);
360 xfContext* xfc = (xfContext*)context;
361 Window handle = xf_Pointer_get_window(xfc);
364 WINPR_ASSERT(pointer);
366 xfc->pointer = (xfPointer*)pointer;
372 if (!xf_Pointer_GetCursorForCurrentScale(context, pointer, &(xfc->pointer->cursor)))
375 XDefineCursor(xfc->display, handle, xfc->pointer->cursor);
380 WLog_WARN(TAG,
"handle=%ld", handle);
386 static BOOL xf_Pointer_SetNull(rdpContext* context)
388 WLog_DBG(TAG,
"called");
390 xfContext* xfc = (xfContext*)context;
391 static Cursor nullcursor = None;
392 Window handle = xf_Pointer_get_window(xfc);
395 if (nullcursor == None)
397 XcursorImage ci = { 0 };
400 ci.version = XCURSOR_IMAGE_VERSION;
401 ci.size =
sizeof(ci);
402 ci.width = ci.height = 1;
403 ci.xhot = ci.yhot = 0;
405 nullcursor = XcursorImageLoadCursor(xfc->display, &ci);
410 if ((handle) && (nullcursor != None))
411 XDefineCursor(xfc->display, handle, nullcursor);
418 static BOOL xf_Pointer_SetDefault(rdpContext* context)
420 WLog_DBG(TAG,
"called");
422 xfContext* xfc = (xfContext*)context;
423 Window handle = xf_Pointer_get_window(xfc);
428 XUndefineCursor(xfc->display, handle);
435 static BOOL xf_Pointer_SetPosition(rdpContext* context, UINT32 x, UINT32 y)
437 xfContext* xfc = (xfContext*)context;
438 XWindowAttributes current = { 0 };
439 XSetWindowAttributes tmp = { 0 };
442 Window handle = xf_Pointer_get_window(xfc);
446 WLog_WARN(TAG,
"focus %d, handle%lu", xfc->focused, handle);
450 WLog_DBG(TAG,
"%" PRIu32
"x%" PRIu32, x, y);
454 xf_adjust_coordinates_to_screen(xfc, &x, &y);
458 rc = XGetWindowAttributes(xfc->display, handle, ¤t);
461 WLog_WARN(TAG,
"XGetWindowAttributes==%d", rc);
465 tmp.event_mask = (current.your_event_mask & ~(PointerMotionMask));
467 rc = XChangeWindowAttributes(xfc->display, handle, CWEventMask, &tmp);
470 WLog_WARN(TAG,
"XChangeWindowAttributes==%d", rc);
474 rc = XWarpPointer(xfc->display, handle, handle, 0, 0, 0, 0, WINPR_ASSERTING_INT_CAST(
int, x),
475 WINPR_ASSERTING_INT_CAST(
int, y));
477 WLog_WARN(TAG,
"XWarpPointer==%d", rc);
478 tmp.event_mask = current.your_event_mask;
479 rc = XChangeWindowAttributes(xfc->display, handle, CWEventMask, &tmp);
481 WLog_WARN(TAG,
"2.try XChangeWindowAttributes==%d", rc);
489 BOOL xf_register_pointer(rdpGraphics* graphics)
491 rdpPointer pointer = { 0 };
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);
504 UINT32 xf_get_local_color_format(xfContext* xfc, BOOL aligned)
506 UINT32 DstFormat = 0;
512 invert = xfc->invert;
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)
522 DstFormat = (!invert) ? PIXEL_FORMAT_RGBX32 : PIXEL_FORMAT_BGRX32;
524 DstFormat = (!invert) ? PIXEL_FORMAT_RGB24 : PIXEL_FORMAT_BGR24;
526 else if (xfc->depth == 16)
527 DstFormat = PIXEL_FORMAT_RGB16;
528 else if (xfc->depth == 15)
529 DstFormat = PIXEL_FORMAT_RGB15;
531 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.