From 50539f2d593f031e5751240e2a1502395fd65290 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= <[EMAIL REDACTED]>
Date: Sat, 17 Oct 2026 19:49:40 +0200
Subject: [PATCH] X11: Use SDL_PremultiplyAlpha when creating XcursorImage
(#16537)
Like the wayland and the KMSDRM backends already do since b528d48 and
71e9df9 respectively.
Problem observed as wrong looking partially transparent cursor on both
X11 and XWayland, while it looked correct with SDL_VIDEO_DRIVER=wayland.
---
src/video/x11/SDL_x11mouse.c | 44 +++++++++++++++++-------------------
1 file changed, 21 insertions(+), 23 deletions(-)
diff --git a/src/video/x11/SDL_x11mouse.c b/src/video/x11/SDL_x11mouse.c
index e2644f70e9c49..a5e268ba2e2cc 100644
--- a/src/video/x11/SDL_x11mouse.c
+++ b/src/video/x11/SDL_x11mouse.c
@@ -91,37 +91,43 @@ static SDL_Cursor *X11_CreateCursorAndData(Cursor x11_cursor)
}
#ifdef SDL_VIDEO_DRIVER_X11_XCURSOR
-static Cursor X11_CreateXCursorCursor(SDL_Surface *surface, int hot_x, int hot_y)
+static XcursorImage *X11_CreateXCursorImage(SDL_Surface *surface, int hot_x, int hot_y, Uint32 delay)
{
- Display *display = GetDisplay();
- Cursor cursor = None;
- XcursorImage *image;
-
- image = X11_XcursorImageCreate(surface->w, surface->h);
+ XcursorImage *image = X11_XcursorImageCreate(surface->w, surface->h);
if (!image) {
SDL_OutOfMemory();
- return None;
+ return NULL;
}
image->xhot = hot_x;
image->yhot = hot_y;
- image->delay = 0;
+ image->delay = delay;
SDL_assert(surface->format == SDL_PIXELFORMAT_ARGB8888);
SDL_assert(surface->pitch == surface->w * 4);
- SDL_memcpy(image->pixels, surface->pixels, (size_t)surface->h * surface->pitch);
+ SDL_PremultiplyAlpha(surface->w, surface->h,
+ surface->format, surface->pixels, surface->pitch,
+ SDL_PIXELFORMAT_ARGB8888, image->pixels,
+ surface->w * 4, false);
- cursor = X11_XcursorImageLoadCursor(display, image);
+ return image;
+}
- X11_XcursorImageDestroy(image);
+static Cursor X11_CreateXCursorCursor(SDL_Surface *surface, int hot_x, int hot_y)
+{
+ Cursor cursor = None;
+ XcursorImage *image = X11_CreateXCursorImage(surface, hot_x, hot_y, 0);
+
+ if (image) {
+ cursor = X11_XcursorImageLoadCursor(GetDisplay(), image);
+ X11_XcursorImageDestroy(image);
+ }
return cursor;
}
static Cursor X11_CreateAnimatedXCursorCursor(SDL_CursorFrameInfo *frames, int num_frames, int hot_x, int hot_y)
{
- Display *display = GetDisplay();
Cursor cursor = None;
- XcursorImage *image;
XcursorImages *images;
images = X11_XcursorImagesCreate(num_frames);
@@ -131,24 +137,16 @@ static Cursor X11_CreateAnimatedXCursorCursor(SDL_CursorFrameInfo *frames, int n
}
for (int i = 0; i < num_frames; ++i) {
- image = X11_XcursorImageCreate(frames[i].surface->w, frames[i].surface->h);
+ XcursorImage *image = X11_CreateXCursorImage(frames[i].surface, hot_x, hot_y, frames[i].duration);
if (!image) {
- SDL_OutOfMemory();
goto cleanup;
}
- image->xhot = hot_x;
- image->yhot = hot_y;
- image->delay = frames[i].duration;
-
- SDL_assert(frames[i].surface->format == SDL_PIXELFORMAT_ARGB8888);
- SDL_assert(frames[i].surface->pitch == frames[i].surface->w * 4);
- SDL_memcpy(image->pixels, frames[i].surface->pixels, (size_t)frames[i].surface->h * frames[i].surface->pitch);
images->images[i] = image;
images->nimage++;
}
- cursor = X11_XcursorImagesLoadCursor(display, images);
+ cursor = X11_XcursorImagesLoadCursor(GetDisplay(), images);
cleanup:
X11_XcursorImagesDestroy(images);