wayland: scale cursors to the right size

When loading cursors at scale, we expect the
cursor images to have a size of scale * size.
If we don't find such images, load them at their
unscaled size and scale them up ourselves.

Without this, cursors will appear in unexpected
sizes depending on scales and themes.

Related: #4746
This commit is contained in:
Matthias Clasen 2022-05-27 10:10:27 -04:00
parent 00d45c6743
commit 5507b3f8c1

View File

@ -200,27 +200,51 @@ wl_cursor_destroy(struct wl_cursor *cursor)
} }
static struct wl_cursor * static struct wl_cursor *
wl_cursor_create_from_xcursor_images(XcursorImages *images, wl_cursor_create_from_xcursor_images(struct wl_cursor_theme *theme,
struct wl_cursor_theme *theme,
const char *name, const char *name,
unsigned int size, unsigned int size,
unsigned int scale) unsigned int scale)
{ {
char *path;
XcursorImages *images;
struct cursor *cursor; struct cursor *cursor;
struct cursor_image *image; struct cursor_image *image;
int i, nbytes; int i, nbytes;
unsigned int load_size; unsigned int load_size;
int width, height; int load_scale = 1;
load_size = size * scale; load_size = size * scale;
cursor = malloc(sizeof *cursor);
if (!cursor) path = g_strconcat (theme->path, "/", name, NULL);
images = xcursor_load_images (path, load_size);
if (!images)
{
g_free (path);
return NULL; return NULL;
}
if (images->images[0]->width != load_size ||
images->images[0]->height != load_size)
{
xcursor_images_destroy (images);
images = xcursor_load_images (path, size);
load_scale = scale;
}
g_free (path);
cursor = malloc(sizeof *cursor);
if (!cursor) {
xcursor_images_destroy (images);
return NULL;
}
cursor->cursor.images = cursor->cursor.images =
malloc(images->nimage * sizeof cursor->cursor.images[0]); malloc(images->nimage * sizeof cursor->cursor.images[0]);
if (!cursor->cursor.images) { if (!cursor->cursor.images) {
free(cursor); free(cursor);
xcursor_images_destroy (images);
return NULL; return NULL;
} }
@ -236,19 +260,10 @@ wl_cursor_create_from_xcursor_images(XcursorImages *images,
image->theme = theme; image->theme = theme;
image->buffer = NULL; image->buffer = NULL;
/* ensure that width and height are multiples of scale */ image->image.width = images->images[i]->width * load_scale;
width = images->images[i]->width; image->image.height = images->images[i]->height * load_scale;
if ((width % scale) != 0) image->image.hotspot_x = images->images[i]->xhot * load_scale;
width = (width / scale + 1) * scale; image->image.hotspot_y = images->images[i]->yhot * load_scale;
height = images->images[i]->height;
if ((height % scale) != 0)
height = (height / scale + 1) * scale;
image->image.width = width;
image->image.height = height;
image->image.hotspot_x = images->images[i]->xhot;
image->image.hotspot_y = images->images[i]->yhot;
image->image.delay = images->images[i]->delay; image->image.delay = images->images[i]->delay;
nbytes = image->image.width * image->image.height * 4; nbytes = image->image.width * image->image.height * 4;
@ -258,14 +273,23 @@ wl_cursor_create_from_xcursor_images(XcursorImages *images,
break; break;
} }
if (load_scale == 1) {
/* copy pixels to shm pool */ /* copy pixels to shm pool */
/* pad at the right and bottom with transparent pixels */ memcpy(theme->pool->data + image->offset,
memset (theme->pool->data + image->offset, 0, nbytes); images->images[i]->pixels, nbytes);
for (int y = 0; y < height; y++) }
{ else {
memcpy(theme->pool->data + image->offset + y * width * 4, /* scale image up while copying it */
images->images[i]->pixels + y * images->images[i]->width * 4, for (int y = 0; y < image->image.height; y++) {
images->images[i]->width * 4); char *p = theme->pool->data + image->offset + y * image->image.width * 4;
char *q = ((char *)images->images[i]->pixels) + (y / load_scale) * images->images[i]->width * 4;
for (int x = 0; x < image->image.width; x++) {
p[4 * x] = q[4 * (x/load_scale)];
p[4 * x + 1] = q[4 * (x/load_scale) + 1];
p[4 * x + 2] = q[4 * (x/load_scale) + 2];
p[4 * x + 3] = q[4 * (x/load_scale) + 3];
}
}
} }
cursor->total_delay += image->image.delay; cursor->total_delay += image->image.delay;
cursor->cursor.images[i] = (struct wl_cursor_image *) image; cursor->cursor.images[i] = (struct wl_cursor_image *) image;
@ -276,9 +300,12 @@ wl_cursor_create_from_xcursor_images(XcursorImages *images,
free(cursor->cursor.name); free(cursor->cursor.name);
free(cursor->cursor.images); free(cursor->cursor.images);
free(cursor); free(cursor);
xcursor_images_destroy (images);
return NULL; return NULL;
} }
xcursor_images_destroy (images);
return &cursor->cursor; return &cursor->cursor;
} }
@ -288,18 +315,9 @@ load_cursor(struct wl_cursor_theme *theme,
unsigned int size, unsigned int size,
unsigned int scale) unsigned int scale)
{ {
XcursorImages *images;
struct wl_cursor *cursor; struct wl_cursor *cursor;
char *path;
path = g_strconcat (theme->path, "/", name, NULL); cursor = wl_cursor_create_from_xcursor_images(theme, name, size, scale);
images = xcursor_load_images (path, size * scale);
g_free (path);
if (!images)
return;
cursor = wl_cursor_create_from_xcursor_images(images, theme, name, size, scale);
if (cursor) { if (cursor) {
theme->cursor_count++; theme->cursor_count++;
@ -314,8 +332,6 @@ load_cursor(struct wl_cursor_theme *theme,
theme->cursors[theme->cursor_count - 1] = cursor; theme->cursors[theme->cursor_count - 1] = cursor;
} }
} }
xcursor_images_destroy (images);
} }
/** Load a cursor theme to memory shared with the compositor /** Load a cursor theme to memory shared with the compositor