cursor-shape: use server-side cursors for custom (OSC-22), if possible

Using a lookup table, try to map the user-provided xcursor string to a
cursor-shape-v1 known shape.

If we succeed, set the user’s custom cursor using server side
cursors (i.e. using cursor-shape-v1).

If not, fallback to trying to load the image ourselves (using
wl_cursor_theme_get_cursor()), and set it using the legacy
wl_pointer_set_cursor().
This commit is contained in:
Daniel Eklöf 2023-06-28 13:25:08 +02:00
parent bf83a0b2bd
commit c2baaff3c1
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
6 changed files with 141 additions and 32 deletions

View file

@ -1,4 +1,9 @@
#include <stdlib.h>
#include <string.h>
#define LOG_MODULE "cursor-shape"
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "cursor-shape.h"
#include "debug.h"
@ -30,6 +35,7 @@ cursor_shape_to_string(enum cursor_shape shape)
}
#if defined(HAVE_CURSOR_SHAPE)
enum wp_cursor_shape_device_v1_shape
cursor_shape_to_server_shape(enum cursor_shape shape)
{
@ -51,4 +57,59 @@ cursor_shape_to_server_shape(enum cursor_shape shape)
xassert(table[shape] != 0);
return table[shape];
}
#endif
enum wp_cursor_shape_device_v1_shape
cursor_string_to_server_shape(const char *xcursor)
{
if (xcursor == NULL)
return 0;
static const char *const table[][2] = {
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT] = {"default", "left_ptr"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CONTEXT_MENU] = {"context-menu"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_HELP] = {"help", "question_arrow"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_POINTER] = {"pointer", "hand"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_PROGRESS] = {"progress", "left_ptr_watch"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_WAIT] = {"wait", "watch"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CELL] = {"cell"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CROSSHAIR] = {"crosshair", "cross"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_TEXT] = {"text", "xterm"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_VERTICAL_TEXT] = {"vertical-text"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ALIAS] = {"alias", "dnd-link"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_COPY] = {"copy", "dnd-copy"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_MOVE] = {"move"}, /* dnd-move? */
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NO_DROP] = {"no-drop", "dnd-no-drop"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NOT_ALLOWED] = {"not-allowed", "crossed_circle"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRAB] = {"grab", "hand1"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRABBING] = {"grabbing"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_E_RESIZE] = {"e-resize", "right_side"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_N_RESIZE] = {"n-resize", "top_side"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NE_RESIZE] = {"ne-resize", "top_right_corner"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NW_RESIZE] = {"nw-resize", "top_left_corner"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_S_RESIZE] = {"s-resize", "bottom_side"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SE_RESIZE] = {"se-resize", "bottom_right_corner"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SW_RESIZE] = {"sw-resize", "bottom_left_corner"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_W_RESIZE] = {"w-resize", "left_side"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_EW_RESIZE] = {"ew-resize", "sb_h_double_arrow"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NS_RESIZE] = {"ns-resize", "sb_v_double_arrow"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NESW_RESIZE] = {"nesw-resize", "fd_double_arrow"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NWSE_RESIZE] = {"nwse-resize", "bd_double_arrow"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_COL_RESIZE] = {"col-resize", "sb_h_double_arrow"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ROW_RESIZE] = {"row-resize", "sb_v_double_arrow"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ALL_SCROLL] = {"all-scroll", "fleur"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ZOOM_IN] = {"zoom-in"},
[WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ZOOM_OUT] = {"zoom-out"},
};
for (size_t i = 0; i < ALEN(table); i++) {
for (size_t j = 0; j < ALEN(table[i]); j++) {
if (table[i][j] != NULL && strcmp(xcursor, table[i][j]) == 0) {
return i;
}
}
}
return 0;
}
#endif /* HAVE_CURSOR_SHAPE */

View file

@ -29,4 +29,6 @@ const char *cursor_shape_to_string(enum cursor_shape shape);
#if defined(HAVE_CURSOR_SHAPE)
enum wp_cursor_shape_device_v1_shape cursor_shape_to_server_shape(
enum cursor_shape shape);
enum wp_cursor_shape_device_v1_shape cursor_string_to_server_shape(
const char *xcursor);
#endif

View file

@ -4270,41 +4270,62 @@ render_xcursor_update(struct seat *seat)
xassert(seat->pointer.cursor != NULL);
const enum cursor_shape shape = seat->pointer.shape;
const char *const xcursor = seat->pointer.last_custom_xcursor;
#if defined(HAVE_CURSOR_SHAPE)
if (seat->pointer.shape_device != NULL &&
seat->pointer.shape != CURSOR_SHAPE_CUSTOM)
{
LOG_DBG("setting cursor shape using cursor-shape-v1");
wp_cursor_shape_device_v1_set_shape(
seat->pointer.shape_device,
seat->pointer.serial,
cursor_shape_to_server_shape(seat->pointer.shape));
} else
#endif
{
LOG_DBG("setting cursor shape using a client-side cursor surface");
const int scale = seat->pointer.scale;
struct wl_cursor_image *image = seat->pointer.cursor->images[0];
if (seat->pointer.shape_device != NULL) {
xassert(shape != CURSOR_SHAPE_CUSTOM || xcursor != NULL);
wl_surface_attach(
seat->pointer.surface.surf, wl_cursor_image_get_buffer(image), 0, 0);
const enum wp_cursor_shape_device_v1_shape custom_shape =
(shape == CURSOR_SHAPE_CUSTOM && xcursor != NULL
? cursor_string_to_server_shape(xcursor)
: 0);
wl_pointer_set_cursor(
seat->wl_pointer, seat->pointer.serial,
seat->pointer.surface.surf,
image->hotspot_x / scale, image->hotspot_y / scale);
if (shape != CURSOR_SHAPE_CUSTOM || custom_shape != 0) {
xassert(custom_shape == 0 || shape == CURSOR_SHAPE_CUSTOM);
wl_surface_damage_buffer(
seat->pointer.surface.surf, 0, 0, INT32_MAX, INT32_MAX);
const enum wp_cursor_shape_device_v1_shape wp_shape = custom_shape != 0
? custom_shape
: cursor_shape_to_server_shape(shape);
wl_surface_set_buffer_scale(seat->pointer.surface.surf, scale);
LOG_DBG("setting %scursor shape using cursor-shape-v1",
custom_shape != 0 ? "custom " : "");
xassert(seat->pointer.xcursor_callback == NULL);
seat->pointer.xcursor_callback = wl_surface_frame(seat->pointer.surface.surf);
wl_callback_add_listener(seat->pointer.xcursor_callback, &xcursor_listener, seat);
wp_cursor_shape_device_v1_set_shape(
seat->pointer.shape_device,
seat->pointer.serial,
wp_shape);
wl_surface_commit(seat->pointer.surface.surf);
return;
}
}
#endif
LOG_DBG("setting %scursor shape using a client-side cursor surface",
shape == CURSOR_SHAPE_CUSTOM ? "custom " : "");
const int scale = seat->pointer.scale;
struct wl_cursor_image *image = seat->pointer.cursor->images[0];
wl_surface_attach(
seat->pointer.surface.surf, wl_cursor_image_get_buffer(image), 0, 0);
wl_pointer_set_cursor(
seat->wl_pointer, seat->pointer.serial,
seat->pointer.surface.surf,
image->hotspot_x / scale, image->hotspot_y / scale);
wl_surface_damage_buffer(
seat->pointer.surface.surf, 0, 0, INT32_MAX, INT32_MAX);
wl_surface_set_buffer_scale(seat->pointer.surface.surf, scale);
xassert(seat->pointer.xcursor_callback == NULL);
seat->pointer.xcursor_callback = wl_surface_frame(seat->pointer.surface.surf);
wl_callback_add_listener(seat->pointer.xcursor_callback, &xcursor_listener, seat);
wl_surface_commit(seat->pointer.surface.surf);
}
static void
@ -4467,8 +4488,13 @@ render_xcursor_set(struct seat *seat, struct terminal *term,
return true;
}
if (seat->pointer.shape == shape)
if (seat->pointer.shape == shape &&
!(shape == CURSOR_SHAPE_CUSTOM &&
strcmp(seat->pointer.last_custom_xcursor,
term->mouse_user_cursor) != 0))
{
return true;
}
/* TODO: skip this when using server-side cursors */
if (shape != CURSOR_SHAPE_HIDDEN) {
@ -4491,8 +4517,16 @@ render_xcursor_set(struct seat *seat, struct terminal *term,
return false;
}
}
} else
if (shape == CURSOR_SHAPE_CUSTOM) {
free(seat->pointer.last_custom_xcursor);
seat->pointer.last_custom_xcursor = xstrdup(term->mouse_user_cursor);
}
} else {
seat->pointer.cursor = NULL;
free(seat->pointer.last_custom_xcursor);
seat->pointer.last_custom_xcursor = NULL;
}
/* FDM hook takes care of actual rendering */
seat->pointer.shape = shape;

View file

@ -3130,8 +3130,15 @@ term_xcursor_update_for_seat(struct terminal *term, struct seat *seat)
if (seat->pointer.hidden)
shape = CURSOR_SHAPE_HIDDEN;
else if (render_xcursor_is_valid(seat, term->mouse_user_cursor))
#if defined(HAVE_CURSOR_SHAPE)
else if (cursor_string_to_server_shape(term->mouse_user_cursor) != 0
#elif
else if (true
#endif
|| render_xcursor_is_valid(seat, term->mouse_user_cursor))
{
shape = CURSOR_SHAPE_CUSTOM;
}
else if (seat->mouse.col >= 0 &&
seat->mouse.row >= 0 &&
@ -3716,6 +3723,8 @@ void
term_set_user_mouse_cursor(struct terminal *term, const char *cursor)
{
free(term->mouse_user_cursor);
term->mouse_user_cursor = cursor != NULL ? xstrdup(cursor) : NULL;
term->mouse_user_cursor = cursor != NULL && strlen(cursor) > 0
? xstrdup(cursor)
: NULL;
term_xcursor_update(term);
}

View file

@ -234,6 +234,7 @@ seat_destroy(struct seat *seat)
ime_reset_pending(seat);
free(seat->clipboard.text);
free(seat->primary.text);
free(seat->pointer.last_custom_xcursor);
free(seat->name);
}

View file

@ -159,6 +159,8 @@ struct seat {
float scale;
bool hidden;
enum cursor_shape shape;
char *last_custom_xcursor;
struct wl_callback *xcursor_callback;
bool xcursor_pending;
} pointer;