AtlatestRepositorysigil-ui
1
/* Cursor protocol objects belong to the window connection. Theme decoding2
* uses optional libwayland-cursor; no toolkit or display connection is added.3
* The vendored public ABI is Wayland 1.23.0 cursor/wayland-cursor.h (MIT). */4
#include "wayland/cursor.h"6
typedef struct {7
Wl *owner;8
struct wl_buffer *buffer;9
void *mapped;10
size_t bytes;11
int width, height, busy;12
} CursorBuffer;14
struct CursorState {15
void *library;16
struct wl_cursor_theme *(*load)(const char *, int, struct wl_shm *);17
void (*destroy)(struct wl_cursor_theme *);18
struct wl_cursor *(*get)(struct wl_cursor_theme *, const char *);19
struct wl_buffer *(*buffer)(struct wl_cursor_image *);20
struct wl_cursor_theme *theme;21
struct wl_surface *surface;22
CursorBuffer image_buffers[2];23
char theme_name[256], name[64];24
int size, theme_scale, backend, explicit_configuration;25
int image_width, image_height, image_x, image_y, image_scale;26
uint8_t *image;27
int image_pending, current_image;28
};30
typedef struct { const char *name, *alias; uint32_t shape; } CursorName;31
#define CURSOR(name, alias, shape) { name, alias, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_##shape }32
static const CursorName cursor_names[] = {33
CURSOR("default", "left_ptr", DEFAULT), CURSOR("context-menu", "context-menu", CONTEXT_MENU),34
CURSOR("help", "question_arrow", HELP), CURSOR("pointer", "hand2", POINTER),35
CURSOR("progress", "left_ptr_watch", PROGRESS), CURSOR("wait", "watch", WAIT),36
CURSOR("cell", "plus", CELL), CURSOR("crosshair", "crosshair", CROSSHAIR),37
CURSOR("text", "xterm", TEXT), CURSOR("vertical-text", "vertical-text", VERTICAL_TEXT),38
CURSOR("alias", "dnd-link", ALIAS), CURSOR("copy", "dnd-copy", COPY),39
CURSOR("move", "fleur", MOVE), CURSOR("no-drop", "dnd-none", NO_DROP),40
CURSOR("not-allowed", "crossed_circle", NOT_ALLOWED), CURSOR("grab", "openhand", GRAB),41
CURSOR("grabbing", "closedhand", GRABBING), CURSOR("e-resize", "right_side", E_RESIZE),42
CURSOR("n-resize", "top_side", N_RESIZE), CURSOR("ne-resize", "top_right_corner", NE_RESIZE),43
CURSOR("nw-resize", "top_left_corner", NW_RESIZE), CURSOR("s-resize", "bottom_side", S_RESIZE),44
CURSOR("se-resize", "bottom_right_corner", SE_RESIZE), CURSOR("sw-resize", "bottom_left_corner", SW_RESIZE),45
CURSOR("w-resize", "left_side", W_RESIZE), CURSOR("ew-resize", "sb_h_double_arrow", EW_RESIZE),46
CURSOR("ns-resize", "sb_v_double_arrow", NS_RESIZE), CURSOR("nesw-resize", "fd_double_arrow", NESW_RESIZE),47
CURSOR("nwse-resize", "bd_double_arrow", NWSE_RESIZE), CURSOR("col-resize", "h_double_arrow", COL_RESIZE),48
CURSOR("row-resize", "v_double_arrow", ROW_RESIZE), CURSOR("all-scroll", "fleur", ALL_SCROLL),49
CURSOR("zoom-in", "zoom-in", ZOOM_IN), CURSOR("zoom-out", "zoom-out", ZOOM_OUT)50
};51
#undef CURSOR53
static const CursorName *cursor_name(const char *name)54
{55
for (size_t i = 0; i < sizeof cursor_names / sizeof *cursor_names; i++)56
if (!strcmp(name, cursor_names[i].name) || !strcmp(name, cursor_names[i].alias)) return &cursor_names[i];57
return NULL;58
}60
static Wl *cursor_owner(Wl *w)61
{62
if (!w || !w->display || w->dead || w->closed) return NULL;63
while (w->parent) w = w->parent;64
return !w->dead && !w->closed && pthread_equal(w->service_thread, pthread_self()) ? w : NULL;65
}67
static int cursor_environment_size(void)68
{69
const char *text = getenv("XCURSOR_SIZE");70
if (!text || !*text) return 24;71
char *end;72
errno = 0;73
long size = strtol(text, &end, 10);74
return !errno && !*end && size > 0 && size <= 128 ? (int)size : 24;75
}77
static CursorState *cursor_state(Wl *w)78
{79
if (w->cursor) return w->cursor;80
CursorState *cursor = calloc(1, sizeof *cursor);81
if (!cursor) return NULL;82
strcpy(cursor->theme_name, "default");83
strcpy(cursor->name, "default");84
cursor->size = cursor_environment_size();85
cursor->current_image = -1;86
for (int i = 0; i < 2; i++) cursor->image_buffers[i].owner = w;87
const char *library = getenv("SIGIL_UI_LIBWAYLAND_CURSOR");88
cursor->library = dlopen(library && *library ? library : "libwayland-cursor.so.0", RTLD_NOW | RTLD_LOCAL);89
if (cursor->library) {90
cursor->load = dlsym(cursor->library, "wl_cursor_theme_load");91
cursor->destroy = dlsym(cursor->library, "wl_cursor_theme_destroy");92
cursor->get = dlsym(cursor->library, "wl_cursor_theme_get_cursor");93
cursor->buffer = dlsym(cursor->library, "wl_cursor_image_get_buffer");94
if (!cursor->load || !cursor->destroy || !cursor->get || !cursor->buffer) {95
dlclose(cursor->library);96
cursor->library = NULL;97
}98
}99
w->cursor = cursor;100
return cursor;101
}103
static int cursor_show(Wl *w, struct wl_buffer *buffer, int width, int height, int x, int y, int scale)104
{105
CursorState *cursor = w->cursor;106
/* Theme sizes need not divide the output scale (including builtins).107
* Never submit a buffer that would raise invalid_size at commit. */108
if (width % scale || height % scale) scale = 1;109
if (!cursor->surface) cursor->surface = wl_compositor_create_surface(w->compositor);110
if (!cursor->surface) return 0;111
wl_pointer_set_cursor(w->pointer, w->pointer_serial, cursor->surface, x / scale, y / scale);112
wl_surface_set_buffer_scale(cursor->surface, scale);113
wl_surface_attach(cursor->surface, buffer, 0, 0);114
wl_surface_damage(cursor->surface, 0, 0, (width + scale - 1) / scale, (height + scale - 1) / scale);115
wl_surface_commit(cursor->surface);116
return 1;117
}119
static void cursor_buffer_close(CursorBuffer *buffer)120
{121
if (buffer->buffer) wl_buffer_destroy(buffer->buffer);122
if (buffer->mapped) munmap(buffer->mapped, buffer->bytes);123
buffer->buffer = NULL;124
buffer->mapped = NULL;125
buffer->bytes = 0;126
buffer->busy = 0;127
}129
static void cursor_buffer_released(void *data, struct wl_buffer *proxy)130
{131
(void)proxy;132
CursorBuffer *buffer = data;133
buffer->busy = 0;134
Wl *w = buffer->owner;135
if (w->cursor && w->cursor->image_pending) cursor_apply(w);136
}137
static const struct wl_buffer_listener cursor_buffer_listener = {cursor_buffer_released};139
static int cursor_buffer_open(Wl *w, CursorBuffer *buffer, int width, int height)140
{141
size_t size = (size_t)width * height * 4;142
int fd = (int)syscall(SYS_memfd_create, "sigil-ui-cursor", 1u);143
if (fd < 0) return 0;144
if (ftruncate(fd, (off_t)size) < 0) { close(fd); return 0; }145
void *mapped = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);146
if (mapped == MAP_FAILED) { close(fd); return 0; }147
struct wl_shm_pool *pool = wl_shm_create_pool(w->shm, fd, (int32_t)size);148
if (pool) {149
buffer->buffer = wl_shm_pool_create_buffer(pool, 0, width, height, width * 4, WL_SHM_FORMAT_ARGB8888);150
wl_shm_pool_destroy(pool);151
}152
close(fd);153
if (!buffer->buffer) { munmap(mapped, size); return 0; }154
buffer->mapped = mapped;155
buffer->bytes = size;156
buffer->width = width;157
buffer->height = height;158
if (wl_buffer_add_listener(buffer->buffer, &cursor_buffer_listener, buffer) < 0) {159
cursor_buffer_close(buffer);160
return 0;161
}162
return 1;163
}165
/* At most two protocol buffers and one latest pending copy. Never rewrite a166
* busy buffer or allocate another slot while the compositor holds both. */167
static int cursor_image_buffer(Wl *w)168
{169
CursorState *cursor = w->cursor;170
if (!cursor->image_pending && cursor->current_image >= 0 &&171
!cursor->image_buffers[cursor->current_image].busy) return cursor->current_image;172
/* An older release may already be queued. A busy slot gets exactly one173
* submission until that release is dispatched, even for unchanged data. */174
cursor->image_pending = 1;175
for (int i = 0; i < 2; i++) {176
CursorBuffer *buffer = &cursor->image_buffers[i];177
if (buffer->busy) continue;178
if (buffer->buffer && (buffer->width != cursor->image_width || buffer->height != cursor->image_height))179
cursor_buffer_close(buffer);180
if (!buffer->buffer && !cursor_buffer_open(w, buffer, cursor->image_width, cursor->image_height)) return -2;181
memcpy(buffer->mapped, cursor->image, buffer->bytes);182
cursor->image_pending = 0;183
cursor->current_image = i;184
return i;185
}186
return -1;187
}189
static int cursor_apply(Wl *w)190
{191
if (!(w = cursor_owner(w))) return 0;192
CursorState *cursor = w->cursor;193
if (!cursor) return 1;194
cursor->backend = SUI_CURSOR_UNAVAILABLE;195
/* Serial zero is valid after the protocol counter wraps. Focus is the196
* validity bit; never infer it from the serial's numeric value. */197
if (!w->pointer_inside || !w->pointer) return 1;198
if (!strcmp(cursor->name, "none")) {199
wl_pointer_set_cursor(w->pointer, w->pointer_serial, NULL, 0, 0);200
cursor->backend = SUI_CURSOR_HIDDEN;201
} else if (cursor->image) {202
int slot = cursor_image_buffer(w);203
if (slot == -1) {204
cursor->backend = cursor->current_image >= 0 ? SUI_CURSOR_IMAGE : SUI_CURSOR_UNAVAILABLE;205
return 1;206
}207
if (slot < 0 || !cursor_show(w, cursor->image_buffers[slot].buffer,208
cursor->image_width, cursor->image_height, cursor->image_x, cursor->image_y, cursor->image_scale)) return 0;209
cursor->image_buffers[slot].busy = 1;210
cursor->backend = SUI_CURSOR_IMAGE;211
} else {212
const CursorName *name = cursor_name(cursor->name);213
/* A name alone lets the compositor apply desktop preferences. Only214
* explicit theme/size configuration gives library images priority. */215
if (cursor->explicit_configuration || !name || !w->cursor_device) {216
int scale = w->scale > 0 && w->scale <= 8 ? w->scale : 1;217
if (cursor->theme && cursor->theme_scale != scale) {218
cursor->destroy(cursor->theme);219
cursor->theme = NULL;220
}221
if (!cursor->theme && cursor->library && w->shm) {222
cursor->theme = cursor->load(cursor->explicit_configuration ? cursor->theme_name : "default",223
cursor->size * scale, w->shm);224
cursor->theme_scale = scale;225
}226
if (cursor->theme) {227
struct wl_cursor *image = cursor->get(cursor->theme, name ? name->name : cursor->name);228
if (!image && name) image = cursor->get(cursor->theme, name->alias);229
if (image && image->image_count && image->images && image->images[0]) {230
struct wl_cursor_image *first = image->images[0];231
if (first->width && first->height && first->width <= 1024 && first->height <= 1024 &&232
first->hotspot_x < first->width && first->hotspot_y < first->height) {233
struct wl_buffer *buffer = cursor->buffer(first);234
if (buffer && cursor_show(w, buffer, first->width, first->height,235
first->hotspot_x, first->hotspot_y, scale))236
cursor->backend = SUI_CURSOR_THEME;237
}238
}239
}240
}241
if (cursor->backend == SUI_CURSOR_UNAVAILABLE && name && w->cursor_device) {242
wp_cursor_shape_device_v1_set_shape(w->cursor_device, w->pointer_serial, name->shape);243
cursor->backend = SUI_CURSOR_SHAPE;244
}245
if (cursor->backend == SUI_CURSOR_UNAVAILABLE) return 0;246
}247
return wl_display_flush(w->display) >= 0 || errno == EAGAIN;248
}250
static void cursor_drop_image(CursorState *cursor)251
{252
free(cursor->image);253
cursor->image = NULL;254
cursor->image_pending = 0;255
}257
int sui_wayland_cursor_configure(Wl *w, const char *theme, int size)258
{259
if (!(w = cursor_owner(w)) || size < 0 || size > 128 ||260
(theme && (!*theme || strlen(theme) >= 256))) return 0;261
CursorState *cursor = cursor_state(w);262
if (!cursor) return 0;263
int explicit_configuration = theme != NULL || size > 0;264
if (!theme) {265
theme = getenv("XCURSOR_THEME");266
if (!theme || !*theme || strlen(theme) >= sizeof cursor->theme_name) theme = "default";267
}268
if (!size) size = cursor_environment_size();269
if (size != cursor->size || strcmp(theme, cursor->theme_name) ||270
explicit_configuration != cursor->explicit_configuration) {271
if (cursor->theme) cursor->destroy(cursor->theme);272
cursor->theme = NULL;273
cursor->size = size;274
if (strcmp(theme, cursor->theme_name)) strcpy(cursor->theme_name, theme);275
}276
cursor->explicit_configuration = explicit_configuration;277
return cursor_apply(w);278
}280
int sui_wayland_cursor_name(Wl *w, const char *name)281
{282
if (!(w = cursor_owner(w)) || !name || !*name || strlen(name) >= 64) return 0;283
CursorState *cursor = cursor_state(w);284
if (!cursor) return 0;285
cursor_drop_image(cursor);286
cursor->current_image = -1;287
strcpy(cursor->name, name);288
return cursor_apply(w);289
}291
int sui_wayland_cursor_image(Wl *w, const uint8_t *bytes, size_t length,292
int width, int height, size_t stride, int x, int y, int scale)293
{294
if (!(w = cursor_owner(w)) || !bytes || width < 1 || height < 1 || width > 512 || height > 512 ||295
stride < (size_t)width * 4 || stride > 2048 || stride * height > length ||296
x < 0 || x >= width || y < 0 || y >= height || scale < 1 || scale > 8 ||297
width % scale || height % scale) return 0;298
size_t row = (size_t)width * 4;299
uint8_t *copy = malloc(row * height);300
if (!copy) return 0;301
for (int y = 0; y < height; y++) memcpy(copy + row * y, bytes + stride * y, row);302
CursorState *cursor = cursor_state(w);303
if (!cursor) { free(copy); return 0; }304
cursor_drop_image(cursor);305
strcpy(cursor->name, "image");306
cursor->image = copy;307
cursor->image_pending = 1;308
cursor->image_width = width;309
cursor->image_height = height;310
cursor->image_x = x;311
cursor->image_y = y;312
cursor->image_scale = scale;313
return cursor_apply(w);314
}316
int sui_wayland_cursor_backend(Wl *w)317
{318
return (w = cursor_owner(w)) && w->cursor ? w->cursor->backend : SUI_CURSOR_UNAVAILABLE;319
}321
static void cursor_close(Wl *w)322
{323
CursorState *cursor = w->cursor;324
if (!cursor) return;325
cursor_drop_image(cursor);326
for (int i = 0; i < 2; i++) cursor_buffer_close(&cursor->image_buffers[i]);327
if (cursor->theme) cursor->destroy(cursor->theme);328
if (cursor->surface) wl_surface_destroy(cursor->surface);329
if (cursor->library) dlclose(cursor->library);330
free(cursor);331
w->cursor = NULL;332
}