neuswc

patched neuswc library (tracked)
git clone https://git.daat.foo/neuswc.git
Log | Files | Refs | README | LICENSE

commit fe85c08ec5bd659e02f35561e383520fb24cd682
parent 92e1b9a71583216d61a03e7e82b3ecd1b22b559e
Author: cowmonk <cowmonk@based.pt>
Date:   Tue, 24 Nov 2026 09:09:42 -0700

Apply newest MICE patches

These add some more useful compatibility stuff.

Diffstat:
Mlibswc/compositor.c | 106++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
Alibswc/cursor_shape.c | 146+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alibswc/cursor_shape.h | 11+++++++++++
Mlibswc/data.c | 130++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
Mlibswc/data.h | 33+++++++++++++++++++++++++++++++++
Mlibswc/data_device.c | 44+++++++++++++++++++++++++++++++++++++++-----
Mlibswc/data_device.h | 12++++++++++++
Mlibswc/internal.h | 1+
Mlibswc/meson.build | 2++
Mlibswc/pointer.c | 139++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mlibswc/pointer.h | 4++++
Alibswc/selection.c | 100+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mlibswc/swc.c | 4++++
Mlibswc/swc.h | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mlibswc/window.c | 12+++++++++---
Mlibswc/window.h | 5+++++
Apatches/cursorShapes.patch | 477+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/focusFromDecoration.patch | 211+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/selectionHandler.patch | 557+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19 files changed, 2010 insertions(+), 52 deletions(-)

diff --git a/libswc/compositor.c b/libswc/compositor.c @@ -126,6 +126,8 @@ static struct { uint32_t scheduled_updates; bool updating; + /* The window whose decoration the pointer is over, if one. */ + struct window *chrome_window; struct wl_global *global; bool initialized; @@ -1197,40 +1199,87 @@ restack_view_for_layer(struct compositor_view *view, bool raise) wl_list_insert(insert_after, &view->link); } +/* whether the point is on what the view shows and takes input there */ +static bool +view_content_at(struct compositor_view *view, int32_t x, int32_t y) +{ + struct swc_rectangle *geom = &view->base.geometry; + struct swc_rectangle buffer_geom; + + if (view->window) { + if (!rectangle_contains_point(geom, x, y)) { + return false; + } + } else if (view->base.buffer) { + buffer_geom.x = geom->x - view->buffer_offset_x; + buffer_geom.y = geom->y - view->buffer_offset_y; + buffer_geom.width = view->base.buffer->width; + buffer_geom.height = view->base.buffer->height; + if (!rectangle_contains_point(&buffer_geom, x, y)) { + return false; + } + } else if (!rectangle_contains_point(geom, x, y)) { + return false; + } + + return pixman_region32_contains_point(&view->surface->state.input, + x - geom->x + view->buffer_offset_x, + y - geom->y + view->buffer_offset_y, + NULL); +} + struct compositor_view * compositor_view_at(int32_t x, int32_t y) { struct compositor_view *view; - struct swc_rectangle *geom; - struct swc_rectangle buffer_geom; wl_list_for_each(view, &compositor.views, link) { if (!view->visible || view->no_input) { continue; } + if (view_content_at(view, x, y)) { + return view; + } + } - geom = &view->base.geometry; - if (view->window) { - if (!rectangle_contains_point(geom, x, y)) { - continue; - } - } else if (view->base.buffer) { - buffer_geom.x = geom->x - view->buffer_offset_x; - buffer_geom.y = geom->y - view->buffer_offset_y; - buffer_geom.width = view->base.buffer->width; - buffer_geom.height = view->base.buffer->height; - if (!rectangle_contains_point(&buffer_geom, x, y)) { - continue; - } - } else if (!rectangle_contains_point(geom, x, y)) { + return NULL; +} + +/* whether the point is on the window's decoration or its border, which the + * pointer is not over for the client: the surface is not under it */ +static bool +view_chrome_at(struct compositor_view *view, int32_t x, int32_t y) +{ + struct swc_rectangle *geom = &view->base.geometry; + int64_t border = (int64_t)view->border.outwidth + (int64_t)view->border.inwidth; + + if (!view->window) { + return false; + } + + return x >= geom->x - (int64_t)view->decor.left - border && + x < geom->x + (int64_t)geom->width + (int64_t)view->decor.right + border && + y >= geom->y - (int64_t)view->decor.top - border && + y < geom->y + (int64_t)geom->height + (int64_t)view->decor.bottom + border; +} + +/* the window whose decoration or border is the first thing under the point, + * in stacking order, NULL when it is anything else or nothing */ +static struct compositor_view * +compositor_chrome_at(int32_t x, int32_t y) +{ + struct compositor_view *view; + + wl_list_for_each(view, &compositor.views, link) + { + if (!view->visible || view->no_input) { continue; } - - if (pixman_region32_contains_point(&view->surface->state.input, - x - geom->x + view->buffer_offset_x, - y - geom->y + view->buffer_offset_y, - NULL)) { + if (view_content_at(view, x, y)) { + return NULL; + } + if (view_chrome_at(view, x, y)) { return view; } } @@ -1939,10 +1988,23 @@ handle_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t fx, return false; } - struct compositor_view *view = compositor_view_at(x, y); + pointer_motion_notify(x, y); + + struct compositor_view *chrome = compositor_chrome_at(x, y); + struct compositor_view *view = chrome ? NULL : compositor_view_at(x, y); pointer_set_focus(swc.seat->pointer, view); + /* The pointer over a window's decoration or border is over the window as + * much as over its surface: the window's handler is told as when the + * surface is entered, once until the pointer is anywhere else */ + if (chrome && chrome->window != compositor.chrome_window) { + compositor.chrome_window = chrome->window; + window_pointer_entered(chrome->window); + } else if (!chrome) { + compositor.chrome_window = NULL; + } + return false; } diff --git a/libswc/cursor_shape.c b/libswc/cursor_shape.c @@ -0,0 +1,146 @@ +/* cursor-shape-v1 */ + +#include "cursor_shape.h" +#include "internal.h" +#include "pointer.h" +#include "seat.h" +#include "util.h" + +#include <wayland-server.h> +#include "cursor-shape-v1-server-protocol.h" + +/* the names of the shapes, the ones of CSS: what a window manager that has a theme gives pictures for */ +static const char *const names[] = { + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT] = "default", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CONTEXT_MENU] = "context-menu", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_HELP] = "help", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_POINTER] = "pointer", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_PROGRESS] = "progress", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_WAIT] = "wait", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CELL] = "cell", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CROSSHAIR] = "crosshair", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_TEXT] = "text", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_VERTICAL_TEXT] = "vertical-text", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ALIAS] = "alias", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_COPY] = "copy", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_MOVE] = "move", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NO_DROP] = "no-drop", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NOT_ALLOWED] = "not-allowed", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRAB] = "grab", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRABBING] = "grabbing", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_E_RESIZE] = "e-resize", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_N_RESIZE] = "n-resize", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NE_RESIZE] = "ne-resize", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NW_RESIZE] = "nw-resize", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_S_RESIZE] = "s-resize", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SE_RESIZE] = "se-resize", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SW_RESIZE] = "sw-resize", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_W_RESIZE] = "w-resize", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_EW_RESIZE] = "ew-resize", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NS_RESIZE] = "ns-resize", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NESW_RESIZE] = "nesw-resize", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NWSE_RESIZE] = "nwse-resize", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_COL_RESIZE] = "col-resize", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ROW_RESIZE] = "row-resize", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ALL_SCROLL] = "all-scroll", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ZOOM_IN] = "zoom-in", + [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ZOOM_OUT] = "zoom-out", +}; + +#define NAMES (sizeof names / sizeof names[0]) + +static void +pointer_set_shape_request(struct wl_client *client, struct wl_resource *resource, + uint32_t serial, uint32_t shape) +{ + struct pointer *pointer = wl_resource_get_user_data(resource); + + (void)serial; + if (shape < 1 || shape >= NAMES || !names[shape]) { + wl_resource_post_error(resource, WP_CURSOR_SHAPE_DEVICE_V1_ERROR_INVALID_SHAPE, + "no such shape: %u", shape); + return; + } + /* only what has the pointer says what it looks like */ + if (!pointer || client != pointer->focus.client) { + return; + } + pointer_set_shape(pointer, names[shape]); +} + +static const struct wp_cursor_shape_device_v1_interface pointer_device_impl = { + .destroy = destroy_resource, + .set_shape = pointer_set_shape_request, +}; + +/* a tablet tool has no pointer here: it is answered and does nothing */ +static void +tool_set_shape_request(struct wl_client *client, struct wl_resource *resource, + uint32_t serial, uint32_t shape) +{ + (void)client; + (void)serial; + (void)shape; + (void)resource; +} + +static const struct wp_cursor_shape_device_v1_interface tool_device_impl = { + .destroy = destroy_resource, + .set_shape = tool_set_shape_request, +}; + +static void +get_pointer(struct wl_client *client, struct wl_resource *resource, uint32_t id, + struct wl_resource *pointer_resource) +{ + struct wl_resource *device = wl_resource_create( + client, &wp_cursor_shape_device_v1_interface, wl_resource_get_version(resource), id); + + if (!device) { + wl_resource_post_no_memory(resource); + return; + } + wl_resource_set_implementation(device, &pointer_device_impl, + wl_resource_get_user_data(pointer_resource), NULL); +} + +static void +get_tablet_tool_v2(struct wl_client *client, struct wl_resource *resource, uint32_t id, + struct wl_resource *tool) +{ + struct wl_resource *device = wl_resource_create( + client, &wp_cursor_shape_device_v1_interface, wl_resource_get_version(resource), id); + + (void)tool; + if (!device) { + wl_resource_post_no_memory(resource); + return; + } + wl_resource_set_implementation(device, &tool_device_impl, NULL, NULL); +} + +static const struct wp_cursor_shape_manager_v1_interface manager_impl = { + .destroy = destroy_resource, + .get_pointer = get_pointer, + .get_tablet_tool_v2 = get_tablet_tool_v2, +}; + +static void +bind_manager(struct wl_client *client, void *data, uint32_t version, uint32_t id) +{ + struct wl_resource *resource; + + (void)data; + resource = wl_resource_create(client, &wp_cursor_shape_manager_v1_interface, version, id); + if (!resource) { + wl_client_post_no_memory(client); + return; + } + wl_resource_set_implementation(resource, &manager_impl, NULL, NULL); +} + +struct wl_global * +cursor_shape_manager_create(struct wl_display *display) +{ + return wl_global_create(display, &wp_cursor_shape_manager_v1_interface, 1, NULL, &bind_manager); +} diff --git a/libswc/cursor_shape.h b/libswc/cursor_shape.h @@ -0,0 +1,11 @@ +/* cursor-shape-v1: a client asks for the pointer's picture by name */ + +#ifndef SWC_CURSOR_SHAPE_H +#define SWC_CURSOR_SHAPE_H + +struct wl_display; + +struct wl_global * +cursor_shape_manager_create(struct wl_display *display); + +#endif diff --git a/libswc/data.c b/libswc/data.c @@ -31,7 +31,8 @@ struct data { struct wl_array mime_types; - struct wl_resource *source; + struct wl_resource *source; /* NULL for a source of the compositor's own */ + struct data_host host; struct wl_list offers; /* drag state: source actions, target actions, agreed action */ @@ -74,6 +75,9 @@ offer_accept(struct wl_client *client, struct wl_resource *offer, return; } + if (!data->source) { + return; + } data->accepted = mime_type != NULL; wl_data_source_send_target(data->source, mime_type); } @@ -89,7 +93,7 @@ offer_receive(struct wl_client *client, struct wl_resource *offer, return; } - wl_data_source_send_send(data->source, mime_type, fd); + data_request(data, mime_type, fd); close(fd); } @@ -100,7 +104,7 @@ offer_finish(struct wl_client *client, struct wl_resource *offer) struct data *data = wl_resource_get_user_data(offer); (void)client; - if (data && wl_resource_get_version(data->source) >= WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) { + if (data && data->source && wl_resource_get_version(data->source) >= WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) { wl_data_source_send_dnd_finished(data->source); } } @@ -126,7 +130,7 @@ offer_set_actions(struct wl_client *client, struct wl_resource *offer, uint32_t struct data *data = wl_resource_get_user_data(offer); (void)client; - if (!data) { + if (!data || !data->source) { return; } data->offer_actions = dnd_actions; @@ -181,33 +185,32 @@ static const struct wl_data_source_interface data_source_impl = { .set_actions = source_set_actions, }; +/* after the source is gone each data_offer made from it has a pointer to a + * free'd struct. the resources cannot be destroyed, a client that correctly + * calls data_offer.destroy would crash it, so the user data of each offer is + * cleared, and a client that still calls accept or receive does nothing */ static void -data_destroy(struct wl_resource *source) +release(struct data *data) { - struct data *data = wl_resource_get_user_data(source); struct wl_resource *offer; char **mime_type; wl_array_for_each(mime_type, &data->mime_types) free(*mime_type); wl_array_release(&data->mime_types); - - /* After this data_source is destroyed, each of the data_offer objects - * associated with the data_source has a pointer to a free'd struct. We - * can't destroy the resources because this results in a segfault on the - * client when it correctly tries to call data_source.destroy. However, a - * misbehaving client could still attempt to call accept or receive on the - * data_offer, which would crash the server. - * - * So, we clear the user data on each of the offers to protect us. */ wl_resource_for_each(offer, &data->offers) { wl_resource_set_user_data(offer, NULL); wl_resource_set_destructor(offer, NULL); } - free(data); } +static void +data_destroy(struct wl_resource *source) +{ + release(wl_resource_get_user_data(source)); +} + struct wl_resource * data_source_new(struct wl_client *client, uint32_t version, uint32_t id) { @@ -220,6 +223,7 @@ data_source_new(struct wl_client *client, uint32_t version, uint32_t id) wl_array_init(&data->mime_types); wl_list_init(&data->offers); data->source_actions = data->action = 0; + memset(&data->host, 0, sizeof(data->host)); data->accepted = false; data->drag_offer = NULL; data->offer_actions = data->preferred = data->chosen = 0; @@ -241,10 +245,8 @@ error0: } struct wl_resource * -data_offer_new(struct wl_client *client, struct wl_resource *source, - uint32_t version) +data_offer_for(struct wl_client *client, struct data *data, uint32_t version) { - struct data *data = wl_resource_get_user_data(source); struct wl_resource *offer; offer = wl_resource_create(client, &wl_data_offer_interface, version, 0); @@ -258,10 +260,16 @@ data_offer_new(struct wl_client *client, struct wl_resource *source, return offer; } +struct wl_resource * +data_offer_new(struct wl_client *client, struct wl_resource *source, + uint32_t version) +{ + return data_offer_for(client, wl_resource_get_user_data(source), version); +} + void -data_send_mime_types(struct wl_resource *source, struct wl_resource *offer) +data_send_types(struct data *data, struct wl_resource *offer) { - struct data *data = wl_resource_get_user_data(source); char **mime_type; wl_array_for_each(mime_type, &data->mime_types) @@ -269,6 +277,86 @@ data_send_mime_types(struct wl_resource *source, struct wl_resource *offer) } void +data_send_mime_types(struct wl_resource *source, struct wl_resource *offer) +{ + data_send_types(wl_resource_get_user_data(source), offer); +} + +struct data * +data_of(struct wl_resource *source) +{ + return wl_resource_get_user_data(source); +} + +unsigned +data_type_count(const struct data *data) +{ + return data->mime_types.size / sizeof(char *); +} + +const char * +data_type_at(const struct data *data, unsigned index) +{ + return ((char **)data->mime_types.data)[index]; +} + +void +data_request(struct data *data, const char *mime_type, int fd) +{ + if (data->source) { + wl_data_source_send_send(data->source, mime_type, fd); + } else if (data->host.send) { + data->host.send(data->host.data, mime_type, fd); + } +} + +void +data_cancel(struct data *data) +{ + if (data->source) { + wl_data_source_send_cancelled(data->source); + } else if (data->host.cancelled) { + data->host.cancelled(data->host.data); + } +} + +struct data * +data_host_new(const char *const *types, unsigned count, + const struct data_host *host, void *host_data) +{ + struct data *data = calloc(1, sizeof(*data)); + char **dst; + unsigned i; + + if (!data) { + return NULL; + } + wl_array_init(&data->mime_types); + wl_list_init(&data->offers); + data->host = *host; + data->host.data = host_data; + for (i = 0; i < count; i++) { + dst = wl_array_add(&data->mime_types, sizeof(*dst)); + if (!dst) { + release(data); + return NULL; + } + if (!(*dst = strdup(types[i]))) { + data->mime_types.size -= sizeof(*dst); + release(data); + return NULL; + } + } + return data; +} + +void +data_host_free(struct data *data) +{ + release(data); +} + +void data_drag_enter(struct wl_resource *source, struct wl_resource *offer) { struct data *data = wl_resource_get_user_data(source); diff --git a/libswc/data.h b/libswc/data.h @@ -37,6 +37,39 @@ data_offer_new(struct wl_client *client, struct wl_resource *source, void data_send_mime_types(struct wl_resource *source, struct wl_resource *offer); +struct data; + +/* a source of the compositor's own, for a selection it keeps itself: it + * offers types and answers each ask on its own */ +struct data_host { + void (*send)(void *data, const char *mime_type, int fd); + void (*cancelled)(void *data); + void *data; +}; + +struct data * +data_host_new(const char *const *types, unsigned count, + const struct data_host *host, void *data); +void +data_host_free(struct data *data); +struct data * +data_of(struct wl_resource *source); +/* offers of a selection, host or client */ +struct wl_resource * +data_offer_for(struct wl_client *client, struct data *data, uint32_t version); +void +data_send_types(struct data *data, struct wl_resource *offer); +unsigned +data_type_count(const struct data *data); +const char * +data_type_at(const struct data *data, unsigned index); +/* asks the source for a type, to be written to fd (the caller closes its copy) */ +void +data_request(struct data *data, const char *mime_type, int fd); +/* the selection went to someone else */ +void +data_cancel(struct data *data); + /* a drag offer for source entered a client, nothing accepted yet */ void data_drag_enter(struct wl_resource *source, struct wl_resource *offer); diff --git a/libswc/data_device.c b/libswc/data_device.c @@ -334,6 +334,11 @@ set_selection(struct wl_client *client, struct wl_resource *resource, wl_data_source_send_cancelled(data_device->selection); wl_list_remove(&data_device->selection_destroy_listener.link); } + if (data_device->host) { + data_cancel(data_device->host); + data_host_free(data_device->host); + data_device->host = NULL; + } data_device->selection = data_source; @@ -373,6 +378,7 @@ data_device_create(void) return NULL; } data_device->selection = NULL; + data_device->host = NULL; data_device->selection_destroy_listener.notify = &handle_selection_destroy; wl_signal_init(&data_device->event_signal); wl_list_init(&data_device->resources); @@ -387,6 +393,9 @@ data_device_destroy(struct data_device *data_device) wl_list_for_each_safe(resource, tmp, &data_device->resources, link) wl_resource_destroy(resource); + if (data_device->host) { + data_host_free(data_device->host); + } free(data_device); } @@ -408,18 +417,41 @@ data_device_bind(struct data_device *data_device, struct wl_client *client, return resource; } +struct data * +data_device_client_selection(struct data_device *data_device) +{ + return data_device->selection ? data_of(data_device->selection) : NULL; +} + +void +data_device_set_host(struct data_device *data_device, struct data *host) +{ + if (data_device->selection) { + wl_data_source_send_cancelled(data_device->selection); + wl_list_remove(&data_device->selection_destroy_listener.link); + data_device->selection = NULL; + } + if (data_device->host) { + data_cancel(data_device->host); + data_host_free(data_device->host); + } + data_device->host = host; + send_event(&data_device->event_signal, DATA_DEVICE_EVENT_SELECTION_CHANGED, + host); +} + static struct wl_resource * new_offer(struct wl_resource *resource, struct wl_client *client, - struct wl_resource *source) + struct data *source) { struct wl_resource *offer; - offer = data_offer_new(client, source, wl_resource_get_version(resource)); + offer = data_offer_for(client, source, wl_resource_get_version(resource)); if (!offer) { return NULL; } wl_data_device_send_data_offer(resource, offer); - data_send_mime_types(source, offer); + data_send_types(source, offer); return offer; } @@ -440,8 +472,10 @@ data_device_offer_selection(struct data_device *data_device, } /* If we have a selection, create a new offer for the client. */ - if (data_device->selection) { - offer = new_offer(resource, client, data_device->selection); + if (data_device->host) { + offer = new_offer(resource, client, data_device->host); + } else if (data_device->selection) { + offer = new_offer(resource, client, data_of(data_device->selection)); } wl_data_device_send_selection(resource, offer); diff --git a/libswc/data_device.h b/libswc/data_device.h @@ -29,9 +29,13 @@ enum { DATA_DEVICE_EVENT_SELECTION_CHANGED }; +struct data; + struct data_device { /* The data source corresponding to the current selection. */ struct wl_resource *selection; + /* the selection when the compositor keeps it itself, never with selection */ + struct data *host; struct wl_listener selection_destroy_listener; struct wl_signal event_signal; @@ -45,6 +49,14 @@ data_device_destroy(struct data_device *data_device); struct wl_resource * data_device_bind(struct data_device *data_device, struct wl_client *client, uint32_t version, uint32_t id); +/* the selection a client set, as data, NULL for none */ +struct data * +data_device_client_selection(struct data_device *data_device); +/* takes the selection for the compositor, host owned by the device now. what + * held it is told it is cancelled. a event with data set says it came from + * the compositor and not from a client */ +void +data_device_set_host(struct data_device *data_device, struct data *host); void data_device_offer_selection(struct data_device *data_device, struct wl_client *client); diff --git a/libswc/internal.h b/libswc/internal.h @@ -68,6 +68,7 @@ struct swc { struct wl_global *subcompositor; struct wl_global *xdg_decoration_manager; struct wl_global *xdg_dialog_manager; + struct wl_global *cursor_shape_manager; struct wl_global *xdg_output_manager; struct wl_global *xdg_shell; diff --git a/libswc/meson.build b/libswc/meson.build @@ -3,6 +3,7 @@ install_headers('swc.h') libswc_src = files( 'bindings.c', 'compositor.c', + 'cursor_shape.c', 'data.c', 'data_device.c', 'data_device_manager.c', @@ -21,6 +22,7 @@ libswc_src = files( 'region.c', 'screen.c', 'select.c', + 'selection.c', 'snap.c', 'shell.c', 'shell_surface.c', diff --git a/libswc/pointer.c b/libswc/pointer.c @@ -42,6 +42,19 @@ #include <string.h> #include <wld/wld.h> +/* the pictures of cursors by name, and the one the window manager shows */ +#define SHAPES 64 +struct shape { + char name[32]; + const uint32_t *data; + uint32_t width, height; + int32_t hotspot_x, hotspot_y; +}; +static struct shape shapes[SHAPES]; +static char shape_override[32]; +static void (*motion_handler)(void *data, int32_t x, int32_t y); +static void *motion_data; + static enum swc_cursor_kind cursor_override = SWC_CURSOR_DEFAULT; static enum swc_cursor_mode cursor_mode = SWC_CURSOR_MODE_CLIENT; @@ -367,6 +380,117 @@ swc_clear_cursor_image(enum swc_cursor_kind kind) apply_cursor_override(pointer); } +static const struct shape * +shape_named(const char *name) +{ + if (!name || !*name) { + return NULL; + } + for (int i = 0; i < SHAPES; i++) { + if (shapes[i].data && !strcmp(shapes[i].name, name)) { + return &shapes[i]; + } + } + return NULL; +} + +EXPORT void +swc_set_cursor_shape_image(const char *name, const uint32_t *argb8888, + uint32_t width, uint32_t height, int32_t hotspot_x, + int32_t hotspot_y) +{ + int free_slot = -1; + + if (!name || !*name || strlen(name) >= sizeof shapes[0].name || !argb8888 || + !width || !height) { + return; + } + for (int i = 0; i < SHAPES; i++) { + if (shapes[i].data && !strcmp(shapes[i].name, name)) { + free_slot = i; + break; + } + if (!shapes[i].data && free_slot < 0) { + free_slot = i; + } + } + if (free_slot < 0) { + return; + } + strcpy(shapes[free_slot].name, name); + shapes[free_slot].data = argb8888; + shapes[free_slot].width = width; + shapes[free_slot].height = height; + shapes[free_slot].hotspot_x = hotspot_x; + shapes[free_slot].hotspot_y = hotspot_y; +} + +EXPORT void +swc_set_cursor_shape(const char *name) +{ + struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL; + + if (!name) { + name = ""; + } + if (!strcmp(shape_override, name) || strlen(name) >= sizeof shape_override) { + return; + } + strcpy(shape_override, name); + apply_cursor_override(pointer); +} + +EXPORT void +swc_set_pointer_motion_handler(void (*handler)(void *data, int32_t x, int32_t y), + void *data) +{ + motion_handler = handler; + motion_data = data; +} + +void +pointer_motion_notify(int32_t x, int32_t y) +{ + if (motion_handler) { + motion_handler(motion_data, x, y); + } +} + +/* the cursor a client asked for by name: its picture, drawn by the compositor, where the client's + * own cursor surface would be */ +void +pointer_set_shape(struct pointer *pointer, const char *name) +{ + const struct shape *shape = shape_named(name); + struct cursor shape_cursor; + union wld_object object; + struct wld_buffer *buffer; + + if (!shape && !(shape = shape_named("default"))) { + return; + } + if (pointer->cursor.internal_buffer) { + wld_buffer_unreference(pointer->cursor.internal_buffer); + pointer->cursor.internal_buffer = NULL; + } + drop_client_cursor_surface(pointer); + shape_cursor.width = (int)shape->width; + shape_cursor.height = (int)shape->height; + object.ptr = (void *)shape->data; + buffer = wld_import_buffer(swc.shm->context, WLD_OBJECT_DATA, object, + shape_cursor.width, shape_cursor.height, + WLD_FORMAT_ARGB8888, shape_cursor.width * 4); + if (!buffer) { + WARNING("Failed to create cursor buffer\n"); + return; + } + pointer->cursor.internal_buffer = buffer; + pointer->cursor.hotspot.x = shape->hotspot_x; + pointer->cursor.hotspot.y = shape->hotspot_y; + update_cursor(pointer); + view_attach(&pointer->cursor.view, buffer); +} + void pointer_set_cursor(struct pointer *pointer, uint32_t id) { @@ -375,7 +499,20 @@ pointer_set_cursor(struct pointer *pointer, uint32_t id) union wld_object object = {.ptr = &cursor_data[cursor->offset]}; struct wld_buffer *buffer; - if (id == cursor_left_ptr) { + if (id == cursor_left_ptr && shape_named(shape_override)) { + /* the window manager's, where no client has the pointer */ + static struct cursor shape_cursor; + const struct shape *shape = shape_named(shape_override); + + shape_cursor.width = (int)shape->width; + shape_cursor.height = (int)shape->height; + shape_cursor.hotspot_x = shape->hotspot_x; + shape_cursor.hotspot_y = shape->hotspot_y; + shape_cursor.offset = 0; + cursor = &shape_cursor; + data = shape->data; + object.ptr = (void *)data; + } else if (id == cursor_left_ptr) { enum swc_cursor_kind kind = cursor_override; if (kind < 0 || kind >= (int)ARRAY_LENGTH(cursor_images)) { kind = SWC_CURSOR_DEFAULT; diff --git a/libswc/pointer.h b/libswc/pointer.h @@ -86,6 +86,10 @@ void pointer_set_region(struct pointer *pointer, pixman_region32_t *region); void pointer_set_cursor(struct pointer *pointer, uint32_t id); +void +pointer_set_shape(struct pointer *pointer, const char *name); +void +pointer_motion_notify(int32_t x, int32_t y); struct button * pointer_get_button(struct pointer *pointer, uint32_t serial); diff --git a/libswc/selection.c b/libswc/selection.c @@ -0,0 +1,100 @@ +/* swc: selection.c + * + * the clipboard for a compositor that keeps it itself, see swc.h + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + */ + +#include "swc.h" +#include "data.h" +#include "data_device.h" +#include "event.h" +#include "internal.h" +#include "seat.h" +#include "util.h" + +#include <string.h> + +#define MAX_TYPES 64 + +static struct { + const struct swc_selection_handler *handler; + void *data; + struct wl_listener listener; + bool hooked; +} selection; + +static void +handle_event(struct wl_listener *listener, void *data) +{ + struct event *ev = data; + struct data *source; + const char *types[MAX_TYPES]; + unsigned i, count = 0; + + if (ev->type != DATA_DEVICE_EVENT_SELECTION_CHANGED || ev->data || !selection.handler) { + return; + } + if ((source = data_device_client_selection(swc.seat->data_device))) { + count = data_type_count(source); + if (count > MAX_TYPES) { + count = MAX_TYPES; + } + for (i = 0; i < count; i++) { + types[i] = data_type_at(source, i); + } + } + selection.handler->changed(selection.data, types, count); +} + +EXPORT void +swc_selection_set_handler(const struct swc_selection_handler *handler, void *data) +{ + selection.handler = handler; + selection.data = data; + if (!selection.hooked && swc.seat) { + selection.listener.notify = handle_event; + wl_signal_add(&swc.seat->data_device->event_signal, &selection.listener); + selection.hooked = true; + } +} + +EXPORT bool +swc_selection_request(const char *mime_type, int fd) +{ + struct data *source = data_device_client_selection(swc.seat->data_device); + unsigned i; + + if (!source) { + return false; + } + for (i = 0; i < data_type_count(source); i++) { + if (strcmp(data_type_at(source, i), mime_type) == 0) { + data_request(source, mime_type, fd); + return true; + } + } + return false; +} + +EXPORT bool +swc_selection_offer(const char *const *mime_types, unsigned count, + const struct swc_selection_source *source, void *data) +{ + struct data_host host = { .send = source->send, .cancelled = source->cancelled }; + struct data *offered = data_host_new(mime_types, count, &host, data); + + if (!offered) { + return false; + } + data_device_set_host(swc.seat->data_device, offered); + return true; +} diff --git a/libswc/swc.c b/libswc/swc.c @@ -53,6 +53,7 @@ #include "window.h" #include "xdg_decoration.h" #include "xdg_dialog.h" +#include "cursor_shape.h" #include "xdg_output.h" #include "xdg_shell.h" #ifdef ENABLE_XWAYLAND @@ -374,6 +375,9 @@ swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop, /* optional */ swc.xdg_dialog_manager = xdg_dialog_manager_create(display); + /* optional */ + swc.cursor_shape_manager = cursor_shape_manager_create(display); + swc.xdg_decoration_manager = xdg_decoration_manager_create(display); if (!swc.xdg_decoration_manager) { ERROR("Could not initialize XDG decoration manager\n"); diff --git a/libswc/swc.h b/libswc/swc.h @@ -145,6 +145,74 @@ void swc_clear_cursor_image(enum swc_cursor_kind kind); /** + * set the picture of a cursor by its name: "default", "text", "pointer", + * "wait", "ew-resize" and the like, the names of CSS and of the + * cursor-shape-v1 protocol, which clients ask for by. nothing is drawn + * until a name is asked for, so a window manager with a theme gives + * every name it has. + * + * `argb8888` is `width*height` pixels, kept by the caller as long as it + * may be used + */ +void +swc_set_cursor_shape_image(const char *name, const uint32_t *argb8888, + uint32_t width, uint32_t height, int32_t hotspot_x, + int32_t hotspot_y); + +/** + * show the cursor of this name where no client has the pointer, over a + * window's border for one, NULL for the usual + */ +void +swc_set_cursor_shape(const char *name); + +/** + * be told where the pointer moves, x and y in compositor coordinates, so + * a window manager can say what is under it. at most one, NULL to stop + */ +void +swc_set_pointer_motion_handler(void (*handler)(void *data, int32_t x, int32_t y), + void *data); + +/** + * the clipboard, for a compositor that keeps it itself. a client sets the + * selection as it does in any compositor, and the handler is told which types + * it offers (none when it is gone, the client exited or the selection was + * cleared). what the compositor keeps it offers again as a selection of its + * own, so the clipboard does not end with the client that copied + */ +struct swc_selection_handler { + void (*changed)(void *data, const char *const *mime_types, unsigned count); +}; + +/* what answers a paste of a selection the compositor offers */ +struct swc_selection_source { + /* write the type to fd, which the caller closes */ + void (*send)(void *data, const char *mime_type, int fd); + /* another client took the selection */ + void (*cancelled)(void *data); +}; + +void +swc_selection_set_handler(const struct swc_selection_handler *handler, void *data); + +/** + * asks the client that holds the selection for a type, written to fd. + * false when no client holds it or it does not offer the type. the caller + * closes fd, the client has its own copy + */ +bool +swc_selection_request(const char *mime_type, int fd); + +/** + * takes the selection, offering these types, answered by source. what was + * the selection is cancelled + */ +bool +swc_selection_offer(const char *const *mime_types, unsigned count, + const struct swc_selection_source *source, void *data); + +/** * draw [or update] a simple box overlay * * box is defined by two diagonally opposite corners in compositor-global diff --git a/libswc/window.c b/libswc/window.c @@ -95,6 +95,14 @@ clamp_window_size(const struct window *window, uint32_t *width, window->base.max_height); } +void +window_pointer_entered(struct window *window) +{ + if (window->handler->entered) { + window->handler->entered(window->handler_data); + } +} + static void handle_window_enter(struct wl_listener *listener, void *data) { @@ -110,9 +118,7 @@ handle_window_enter(struct wl_listener *listener, void *data) return; } - if (window->handler->entered) { - window->handler->entered(window->handler_data); - } + window_pointer_entered(window); } struct wl_listener window_enter_listener = { diff --git a/libswc/window.h b/libswc/window.h @@ -96,6 +96,11 @@ struct window_impl { extern struct wl_listener window_enter_listener; +/* The pointer is over the window as much as over its surface: its handler is + * told, as when the surface is entered. */ +void +window_pointer_entered(struct window *window); + bool window_initialize(struct window *window, const struct window_impl *impl, struct surface *surface); diff --git a/patches/cursorShapes.patch b/patches/cursorShapes.patch @@ -0,0 +1,477 @@ +From 58f66a46347a728e9dff4ccc683398afcc4cdeb9 Mon Sep 17 00:00:00 2001 +From: xplshn <anto@xplshn.com.ar> +Date: Tue, 24 Nov 2026 09:19:53 -0300 +Subject: [PATCH] cursors by name: a window manager gives pictures, clients ask + with cursor-shape-v1 + +The pointer's pictures were a few the compositor kept. A window manager +can now hand over a picture for each name a cursor has (the names of +CSS and of cursor-shape-v1), and a client that asks for a shape by +name, as toolkits do, is shown that picture. the window manager can +show a name where no client has the pointer, over a window's border, +and is told where the pointer moves to know which. +--- + libswc/compositor.c | 2 + + libswc/cursor_shape.c | 146 ++++++++++++++++++++++++++++++++++++++++++ + libswc/cursor_shape.h | 11 ++++ + libswc/internal.h | 1 + + libswc/meson.build | 1 + + libswc/pointer.c | 139 +++++++++++++++++++++++++++++++++++++++- + libswc/pointer.h | 4 ++ + libswc/swc.c | 4 ++ + libswc/swc.h | 30 +++++++++ + 9 files changed, 337 insertions(+), 1 deletion(-) + create mode 100644 libswc/cursor_shape.c + create mode 100644 libswc/cursor_shape.h + +diff --git a/libswc/compositor.c b/libswc/compositor.c +index 7c3ec4d..03433ab 100644 +--- a/libswc/compositor.c ++++ b/libswc/compositor.c +@@ -1987,6 +1987,8 @@ handle_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t fx, + return false; + } + ++ pointer_motion_notify(x, y); ++ + struct compositor_view *chrome = compositor_chrome_at(x, y); + struct compositor_view *view = chrome ? NULL : compositor_view_at(x, y); + +diff --git a/libswc/cursor_shape.c b/libswc/cursor_shape.c +new file mode 100644 +index 0000000..28e5a78 +--- /dev/null ++++ b/libswc/cursor_shape.c +@@ -0,0 +1,146 @@ ++/* cursor-shape-v1 */ ++ ++#include "cursor_shape.h" ++#include "internal.h" ++#include "pointer.h" ++#include "seat.h" ++#include "util.h" ++ ++#include <wayland-server.h> ++#include "cursor-shape-v1-server-protocol.h" ++ ++/* the names of the shapes, the ones of CSS: what a window manager that has a theme gives pictures for */ ++static const char *const names[] = { ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT] = "default", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CONTEXT_MENU] = "context-menu", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_HELP] = "help", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_POINTER] = "pointer", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_PROGRESS] = "progress", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_WAIT] = "wait", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CELL] = "cell", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CROSSHAIR] = "crosshair", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_TEXT] = "text", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_VERTICAL_TEXT] = "vertical-text", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ALIAS] = "alias", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_COPY] = "copy", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_MOVE] = "move", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NO_DROP] = "no-drop", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NOT_ALLOWED] = "not-allowed", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRAB] = "grab", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRABBING] = "grabbing", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_E_RESIZE] = "e-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_N_RESIZE] = "n-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NE_RESIZE] = "ne-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NW_RESIZE] = "nw-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_S_RESIZE] = "s-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SE_RESIZE] = "se-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SW_RESIZE] = "sw-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_W_RESIZE] = "w-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_EW_RESIZE] = "ew-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NS_RESIZE] = "ns-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NESW_RESIZE] = "nesw-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NWSE_RESIZE] = "nwse-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_COL_RESIZE] = "col-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ROW_RESIZE] = "row-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ALL_SCROLL] = "all-scroll", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ZOOM_IN] = "zoom-in", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ZOOM_OUT] = "zoom-out", ++}; ++ ++#define NAMES (sizeof names / sizeof names[0]) ++ ++static void ++pointer_set_shape_request(struct wl_client *client, struct wl_resource *resource, ++ uint32_t serial, uint32_t shape) ++{ ++ struct pointer *pointer = wl_resource_get_user_data(resource); ++ ++ (void)serial; ++ if (shape < 1 || shape >= NAMES || !names[shape]) { ++ wl_resource_post_error(resource, WP_CURSOR_SHAPE_DEVICE_V1_ERROR_INVALID_SHAPE, ++ "no such shape: %u", shape); ++ return; ++ } ++ /* only what has the pointer says what it looks like */ ++ if (!pointer || client != pointer->focus.client) { ++ return; ++ } ++ pointer_set_shape(pointer, names[shape]); ++} ++ ++static const struct wp_cursor_shape_device_v1_interface pointer_device_impl = { ++ .destroy = destroy_resource, ++ .set_shape = pointer_set_shape_request, ++}; ++ ++/* a tablet tool has no pointer here: it is answered and does nothing */ ++static void ++tool_set_shape_request(struct wl_client *client, struct wl_resource *resource, ++ uint32_t serial, uint32_t shape) ++{ ++ (void)client; ++ (void)serial; ++ (void)shape; ++ (void)resource; ++} ++ ++static const struct wp_cursor_shape_device_v1_interface tool_device_impl = { ++ .destroy = destroy_resource, ++ .set_shape = tool_set_shape_request, ++}; ++ ++static void ++get_pointer(struct wl_client *client, struct wl_resource *resource, uint32_t id, ++ struct wl_resource *pointer_resource) ++{ ++ struct wl_resource *device = wl_resource_create( ++ client, &wp_cursor_shape_device_v1_interface, wl_resource_get_version(resource), id); ++ ++ if (!device) { ++ wl_resource_post_no_memory(resource); ++ return; ++ } ++ wl_resource_set_implementation(device, &pointer_device_impl, ++ wl_resource_get_user_data(pointer_resource), NULL); ++} ++ ++static void ++get_tablet_tool_v2(struct wl_client *client, struct wl_resource *resource, uint32_t id, ++ struct wl_resource *tool) ++{ ++ struct wl_resource *device = wl_resource_create( ++ client, &wp_cursor_shape_device_v1_interface, wl_resource_get_version(resource), id); ++ ++ (void)tool; ++ if (!device) { ++ wl_resource_post_no_memory(resource); ++ return; ++ } ++ wl_resource_set_implementation(device, &tool_device_impl, NULL, NULL); ++} ++ ++static const struct wp_cursor_shape_manager_v1_interface manager_impl = { ++ .destroy = destroy_resource, ++ .get_pointer = get_pointer, ++ .get_tablet_tool_v2 = get_tablet_tool_v2, ++}; ++ ++static void ++bind_manager(struct wl_client *client, void *data, uint32_t version, uint32_t id) ++{ ++ struct wl_resource *resource; ++ ++ (void)data; ++ resource = wl_resource_create(client, &wp_cursor_shape_manager_v1_interface, version, id); ++ if (!resource) { ++ wl_client_post_no_memory(client); ++ return; ++ } ++ wl_resource_set_implementation(resource, &manager_impl, NULL, NULL); ++} ++ ++struct wl_global * ++cursor_shape_manager_create(struct wl_display *display) ++{ ++ return wl_global_create(display, &wp_cursor_shape_manager_v1_interface, 1, NULL, &bind_manager); ++} +diff --git a/libswc/cursor_shape.h b/libswc/cursor_shape.h +new file mode 100644 +index 0000000..be8ebb4 +--- /dev/null ++++ b/libswc/cursor_shape.h +@@ -0,0 +1,11 @@ ++/* cursor-shape-v1: a client asks for the pointer's picture by name */ ++ ++#ifndef SWC_CURSOR_SHAPE_H ++#define SWC_CURSOR_SHAPE_H ++ ++struct wl_display; ++ ++struct wl_global * ++cursor_shape_manager_create(struct wl_display *display); ++ ++#endif +diff --git a/libswc/internal.h b/libswc/internal.h +index ce62c60..31fdd47 100644 +--- a/libswc/internal.h ++++ b/libswc/internal.h +@@ -68,6 +68,7 @@ struct swc { + struct wl_global *subcompositor; + struct wl_global *xdg_decoration_manager; + struct wl_global *xdg_dialog_manager; ++ struct wl_global *cursor_shape_manager; + struct wl_global *xdg_output_manager; + struct wl_global *xdg_shell; + +diff --git a/libswc/meson.build b/libswc/meson.build +index f506b5c..e1ef27b 100644 +--- a/libswc/meson.build ++++ b/libswc/meson.build +@@ -3,6 +3,7 @@ install_headers('swc.h') + libswc_src = files( + 'bindings.c', + 'compositor.c', ++ 'cursor_shape.c', + 'data.c', + 'data_device.c', + 'data_device_manager.c', +diff --git a/libswc/pointer.c b/libswc/pointer.c +index 662d8d3..9e3fa4b 100644 +--- a/libswc/pointer.c ++++ b/libswc/pointer.c +@@ -42,6 +42,19 @@ + #include <string.h> + #include <wld/wld.h> + ++/* the pictures of cursors by name, and the one the window manager shows */ ++#define SHAPES 64 ++struct shape { ++ char name[32]; ++ const uint32_t *data; ++ uint32_t width, height; ++ int32_t hotspot_x, hotspot_y; ++}; ++static struct shape shapes[SHAPES]; ++static char shape_override[32]; ++static void (*motion_handler)(void *data, int32_t x, int32_t y); ++static void *motion_data; ++ + static enum swc_cursor_kind cursor_override = SWC_CURSOR_DEFAULT; + static enum swc_cursor_mode cursor_mode = SWC_CURSOR_MODE_CLIENT; + +@@ -367,6 +380,117 @@ swc_clear_cursor_image(enum swc_cursor_kind kind) + apply_cursor_override(pointer); + } + ++static const struct shape * ++shape_named(const char *name) ++{ ++ if (!name || !*name) { ++ return NULL; ++ } ++ for (int i = 0; i < SHAPES; i++) { ++ if (shapes[i].data && !strcmp(shapes[i].name, name)) { ++ return &shapes[i]; ++ } ++ } ++ return NULL; ++} ++ ++EXPORT void ++swc_set_cursor_shape_image(const char *name, const uint32_t *argb8888, ++ uint32_t width, uint32_t height, int32_t hotspot_x, ++ int32_t hotspot_y) ++{ ++ int free_slot = -1; ++ ++ if (!name || !*name || strlen(name) >= sizeof shapes[0].name || !argb8888 || ++ !width || !height) { ++ return; ++ } ++ for (int i = 0; i < SHAPES; i++) { ++ if (shapes[i].data && !strcmp(shapes[i].name, name)) { ++ free_slot = i; ++ break; ++ } ++ if (!shapes[i].data && free_slot < 0) { ++ free_slot = i; ++ } ++ } ++ if (free_slot < 0) { ++ return; ++ } ++ strcpy(shapes[free_slot].name, name); ++ shapes[free_slot].data = argb8888; ++ shapes[free_slot].width = width; ++ shapes[free_slot].height = height; ++ shapes[free_slot].hotspot_x = hotspot_x; ++ shapes[free_slot].hotspot_y = hotspot_y; ++} ++ ++EXPORT void ++swc_set_cursor_shape(const char *name) ++{ ++ struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL; ++ ++ if (!name) { ++ name = ""; ++ } ++ if (!strcmp(shape_override, name) || strlen(name) >= sizeof shape_override) { ++ return; ++ } ++ strcpy(shape_override, name); ++ apply_cursor_override(pointer); ++} ++ ++EXPORT void ++swc_set_pointer_motion_handler(void (*handler)(void *data, int32_t x, int32_t y), ++ void *data) ++{ ++ motion_handler = handler; ++ motion_data = data; ++} ++ ++void ++pointer_motion_notify(int32_t x, int32_t y) ++{ ++ if (motion_handler) { ++ motion_handler(motion_data, x, y); ++ } ++} ++ ++/* the cursor a client asked for by name: its picture, drawn by the compositor, where the client's ++ * own cursor surface would be */ ++void ++pointer_set_shape(struct pointer *pointer, const char *name) ++{ ++ const struct shape *shape = shape_named(name); ++ struct cursor shape_cursor; ++ union wld_object object; ++ struct wld_buffer *buffer; ++ ++ if (!shape && !(shape = shape_named("default"))) { ++ return; ++ } ++ if (pointer->cursor.internal_buffer) { ++ wld_buffer_unreference(pointer->cursor.internal_buffer); ++ pointer->cursor.internal_buffer = NULL; ++ } ++ drop_client_cursor_surface(pointer); ++ shape_cursor.width = (int)shape->width; ++ shape_cursor.height = (int)shape->height; ++ object.ptr = (void *)shape->data; ++ buffer = wld_import_buffer(swc.shm->context, WLD_OBJECT_DATA, object, ++ shape_cursor.width, shape_cursor.height, ++ WLD_FORMAT_ARGB8888, shape_cursor.width * 4); ++ if (!buffer) { ++ WARNING("Failed to create cursor buffer\n"); ++ return; ++ } ++ pointer->cursor.internal_buffer = buffer; ++ pointer->cursor.hotspot.x = shape->hotspot_x; ++ pointer->cursor.hotspot.y = shape->hotspot_y; ++ update_cursor(pointer); ++ view_attach(&pointer->cursor.view, buffer); ++} ++ + void + pointer_set_cursor(struct pointer *pointer, uint32_t id) + { +@@ -375,7 +499,20 @@ pointer_set_cursor(struct pointer *pointer, uint32_t id) + union wld_object object = {.ptr = &cursor_data[cursor->offset]}; + struct wld_buffer *buffer; + +- if (id == cursor_left_ptr) { ++ if (id == cursor_left_ptr && shape_named(shape_override)) { ++ /* the window manager's, where no client has the pointer */ ++ static struct cursor shape_cursor; ++ const struct shape *shape = shape_named(shape_override); ++ ++ shape_cursor.width = (int)shape->width; ++ shape_cursor.height = (int)shape->height; ++ shape_cursor.hotspot_x = shape->hotspot_x; ++ shape_cursor.hotspot_y = shape->hotspot_y; ++ shape_cursor.offset = 0; ++ cursor = &shape_cursor; ++ data = shape->data; ++ object.ptr = (void *)data; ++ } else if (id == cursor_left_ptr) { + enum swc_cursor_kind kind = cursor_override; + if (kind < 0 || kind >= (int)ARRAY_LENGTH(cursor_images)) { + kind = SWC_CURSOR_DEFAULT; +diff --git a/libswc/pointer.h b/libswc/pointer.h +index 21a54cd..c6e2126 100644 +--- a/libswc/pointer.h ++++ b/libswc/pointer.h +@@ -86,6 +86,10 @@ void + pointer_set_region(struct pointer *pointer, pixman_region32_t *region); + void + pointer_set_cursor(struct pointer *pointer, uint32_t id); ++void ++pointer_set_shape(struct pointer *pointer, const char *name); ++void ++pointer_motion_notify(int32_t x, int32_t y); + + struct button * + pointer_get_button(struct pointer *pointer, uint32_t serial); +diff --git a/libswc/swc.c b/libswc/swc.c +index d6570b8..ee131d4 100644 +--- a/libswc/swc.c ++++ b/libswc/swc.c +@@ -53,6 +53,7 @@ + #include "window.h" + #include "xdg_decoration.h" + #include "xdg_dialog.h" ++#include "cursor_shape.h" + #include "xdg_output.h" + #include "xdg_shell.h" + #ifdef ENABLE_XWAYLAND +@@ -374,6 +375,9 @@ swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop, + /* optional */ + swc.xdg_dialog_manager = xdg_dialog_manager_create(display); + ++ /* optional */ ++ swc.cursor_shape_manager = cursor_shape_manager_create(display); ++ + swc.xdg_decoration_manager = xdg_decoration_manager_create(display); + if (!swc.xdg_decoration_manager) { + ERROR("Could not initialize XDG decoration manager\n"); +diff --git a/libswc/swc.h b/libswc/swc.h +index 32f6c48..5b7e04e 100644 +--- a/libswc/swc.h ++++ b/libswc/swc.h +@@ -144,6 +144,36 @@ swc_set_cursor_image(enum swc_cursor_kind kind, const uint32_t *argb8888, + void + swc_clear_cursor_image(enum swc_cursor_kind kind); + ++/** ++ * set the picture of a cursor by its name: "default", "text", "pointer", ++ * "wait", "ew-resize" and the like, the names of CSS and of the ++ * cursor-shape-v1 protocol, which clients ask for by. nothing is drawn ++ * until a name is asked for, so a window manager with a theme gives ++ * every name it has. ++ * ++ * `argb8888` is `width*height` pixels, kept by the caller as long as it ++ * may be used ++ */ ++void ++swc_set_cursor_shape_image(const char *name, const uint32_t *argb8888, ++ uint32_t width, uint32_t height, int32_t hotspot_x, ++ int32_t hotspot_y); ++ ++/** ++ * show the cursor of this name where no client has the pointer, over a ++ * window's border for one, NULL for the usual ++ */ ++void ++swc_set_cursor_shape(const char *name); ++ ++/** ++ * be told where the pointer moves, x and y in compositor coordinates, so ++ * a window manager can say what is under it. at most one, NULL to stop ++ */ ++void ++swc_set_pointer_motion_handler(void (*handler)(void *data, int32_t x, int32_t y), ++ void *data); ++ + /** + * the clipboard, for a compositor that keeps it itself. a client sets the + * selection as it does in any compositor, and the handler is told which types +-- +2.56.1 + diff --git a/patches/focusFromDecoration.patch b/patches/focusFromDecoration.patch @@ -0,0 +1,211 @@ +From 4d1bd1573a7baa4b612ea25340379e68817970c7 Mon Sep 17 00:00:00 2001 +From: xplshn <anto@xplshn.com.ar> +Date: Tue, 24 Nov 2026 08:51:51 -0300 +Subject: [PATCH] compositor: the pointer over a window's decoration or border + enters it + +The pointer was over a window only where its surface is, so with focus +following the pointer the title bar did nothing. The window under the +pointer is now the first thing in stacking order that shows its surface, +its decoration or its border there, and its handler is told it was +entered, once, when it is the decoration. +--- + libswc/compositor.c | 104 ++++++++++++++++++++++++++++++++++---------- + libswc/window.c | 12 +++-- + libswc/window.h | 5 +++ + 3 files changed, 96 insertions(+), 25 deletions(-) + +diff --git a/libswc/compositor.c b/libswc/compositor.c +index 16c68ae..7c3ec4d 100644 +--- a/libswc/compositor.c ++++ b/libswc/compositor.c +@@ -126,6 +126,8 @@ static struct { + uint32_t scheduled_updates; + + bool updating; ++ /* The window whose decoration the pointer is over, if one. */ ++ struct window *chrome_window; + struct wl_global *global; + bool initialized; + +@@ -1197,40 +1199,87 @@ restack_view_for_layer(struct compositor_view *view, bool raise) + wl_list_insert(insert_after, &view->link); + } + ++/* whether the point is on what the view shows and takes input there */ ++static bool ++view_content_at(struct compositor_view *view, int32_t x, int32_t y) ++{ ++ struct swc_rectangle *geom = &view->base.geometry; ++ struct swc_rectangle buffer_geom; ++ ++ if (view->window) { ++ if (!rectangle_contains_point(geom, x, y)) { ++ return false; ++ } ++ } else if (view->base.buffer) { ++ buffer_geom.x = geom->x - view->buffer_offset_x; ++ buffer_geom.y = geom->y - view->buffer_offset_y; ++ buffer_geom.width = view->base.buffer->width; ++ buffer_geom.height = view->base.buffer->height; ++ if (!rectangle_contains_point(&buffer_geom, x, y)) { ++ return false; ++ } ++ } else if (!rectangle_contains_point(geom, x, y)) { ++ return false; ++ } ++ ++ return pixman_region32_contains_point(&view->surface->state.input, ++ x - geom->x + view->buffer_offset_x, ++ y - geom->y + view->buffer_offset_y, ++ NULL); ++} ++ + struct compositor_view * + compositor_view_at(int32_t x, int32_t y) + { + struct compositor_view *view; +- struct swc_rectangle *geom; +- struct swc_rectangle buffer_geom; + + wl_list_for_each(view, &compositor.views, link) + { + if (!view->visible || view->no_input) { + continue; + } ++ if (view_content_at(view, x, y)) { ++ return view; ++ } ++ } + +- geom = &view->base.geometry; +- if (view->window) { +- if (!rectangle_contains_point(geom, x, y)) { +- continue; +- } +- } else if (view->base.buffer) { +- buffer_geom.x = geom->x - view->buffer_offset_x; +- buffer_geom.y = geom->y - view->buffer_offset_y; +- buffer_geom.width = view->base.buffer->width; +- buffer_geom.height = view->base.buffer->height; +- if (!rectangle_contains_point(&buffer_geom, x, y)) { +- continue; +- } +- } else if (!rectangle_contains_point(geom, x, y)) { ++ return NULL; ++} ++ ++/* whether the point is on the window's decoration or its border, which the ++ * pointer is not over for the client: the surface is not under it */ ++static bool ++view_chrome_at(struct compositor_view *view, int32_t x, int32_t y) ++{ ++ struct swc_rectangle *geom = &view->base.geometry; ++ int64_t border = (int64_t)view->border.outwidth + (int64_t)view->border.inwidth; ++ ++ if (!view->window) { ++ return false; ++ } ++ ++ return x >= geom->x - (int64_t)view->decor.left - border && ++ x < geom->x + (int64_t)geom->width + (int64_t)view->decor.right + border && ++ y >= geom->y - (int64_t)view->decor.top - border && ++ y < geom->y + (int64_t)geom->height + (int64_t)view->decor.bottom + border; ++} ++ ++/* the window whose decoration or border is the first thing under the point, ++ * in stacking order, NULL when it is anything else or nothing */ ++static struct compositor_view * ++compositor_chrome_at(int32_t x, int32_t y) ++{ ++ struct compositor_view *view; ++ ++ wl_list_for_each(view, &compositor.views, link) ++ { ++ if (!view->visible || view->no_input) { + continue; + } +- +- if (pixman_region32_contains_point(&view->surface->state.input, +- x - geom->x + view->buffer_offset_x, +- y - geom->y + view->buffer_offset_y, +- NULL)) { ++ if (view_content_at(view, x, y)) { ++ return NULL; ++ } ++ if (view_chrome_at(view, x, y)) { + return view; + } + } +@@ -1938,10 +1987,21 @@ handle_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t fx, + return false; + } + +- struct compositor_view *view = compositor_view_at(x, y); ++ struct compositor_view *chrome = compositor_chrome_at(x, y); ++ struct compositor_view *view = chrome ? NULL : compositor_view_at(x, y); + + pointer_set_focus(swc.seat->pointer, view); + ++ /* The pointer over a window's decoration or border is over the window as ++ * much as over its surface: the window's handler is told as when the ++ * surface is entered, once until the pointer is anywhere else */ ++ if (chrome && chrome->window != compositor.chrome_window) { ++ compositor.chrome_window = chrome->window; ++ window_pointer_entered(chrome->window); ++ } else if (!chrome) { ++ compositor.chrome_window = NULL; ++ } ++ + return false; + } + +diff --git a/libswc/window.c b/libswc/window.c +index 80c410e..4bc3054 100644 +--- a/libswc/window.c ++++ b/libswc/window.c +@@ -95,6 +95,14 @@ clamp_window_size(const struct window *window, uint32_t *width, + window->base.max_height); + } + ++void ++window_pointer_entered(struct window *window) ++{ ++ if (window->handler->entered) { ++ window->handler->entered(window->handler_data); ++ } ++} ++ + static void + handle_window_enter(struct wl_listener *listener, void *data) + { +@@ -110,9 +118,7 @@ handle_window_enter(struct wl_listener *listener, void *data) + return; + } + +- if (window->handler->entered) { +- window->handler->entered(window->handler_data); +- } ++ window_pointer_entered(window); + } + + struct wl_listener window_enter_listener = { +diff --git a/libswc/window.h b/libswc/window.h +index bf6baec..d987307 100644 +--- a/libswc/window.h ++++ b/libswc/window.h +@@ -95,6 +95,11 @@ struct window_impl { + + extern struct wl_listener window_enter_listener; + ++/* The pointer is over the window as much as over its surface: its handler is ++ * told, as when the surface is entered. */ ++void ++window_pointer_entered(struct window *window); ++ + bool + window_initialize(struct window *window, const struct window_impl *impl, + struct surface *surface); +-- +2.56.1 + diff --git a/patches/selectionHandler.patch b/patches/selectionHandler.patch @@ -0,0 +1,557 @@ +diff --git a/libswc/data.c b/libswc/data.c +index db91576..d497544 100644 +--- a/libswc/data.c ++++ b/libswc/data.c +@@ -31,7 +31,8 @@ + + struct data { + struct wl_array mime_types; +- struct wl_resource *source; ++ struct wl_resource *source; /* NULL for a source of the compositor's own */ ++ struct data_host host; + struct wl_list offers; + + /* drag state: source actions, target actions, agreed action */ +@@ -74,6 +75,9 @@ offer_accept(struct wl_client *client, struct wl_resource *offer, + return; + } + ++ if (!data->source) { ++ return; ++ } + data->accepted = mime_type != NULL; + wl_data_source_send_target(data->source, mime_type); + } +@@ -89,7 +93,7 @@ offer_receive(struct wl_client *client, struct wl_resource *offer, + return; + } + +- wl_data_source_send_send(data->source, mime_type, fd); ++ data_request(data, mime_type, fd); + close(fd); + } + +@@ -100,7 +104,7 @@ offer_finish(struct wl_client *client, struct wl_resource *offer) + struct data *data = wl_resource_get_user_data(offer); + + (void)client; +- if (data && wl_resource_get_version(data->source) >= WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) { ++ if (data && data->source && wl_resource_get_version(data->source) >= WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) { + wl_data_source_send_dnd_finished(data->source); + } + } +@@ -126,7 +130,7 @@ offer_set_actions(struct wl_client *client, struct wl_resource *offer, uint32_t + struct data *data = wl_resource_get_user_data(offer); + + (void)client; +- if (!data) { ++ if (!data || !data->source) { + return; + } + data->offer_actions = dnd_actions; +@@ -181,33 +185,32 @@ static const struct wl_data_source_interface data_source_impl = { + .set_actions = source_set_actions, + }; + ++/* after the source is gone each data_offer made from it has a pointer to a ++ * free'd struct. the resources cannot be destroyed, a client that correctly ++ * calls data_offer.destroy would crash it, so the user data of each offer is ++ * cleared, and a client that still calls accept or receive does nothing */ + static void +-data_destroy(struct wl_resource *source) ++release(struct data *data) + { +- struct data *data = wl_resource_get_user_data(source); + struct wl_resource *offer; + char **mime_type; + + wl_array_for_each(mime_type, &data->mime_types) free(*mime_type); + wl_array_release(&data->mime_types); +- +- /* After this data_source is destroyed, each of the data_offer objects +- * associated with the data_source has a pointer to a free'd struct. We +- * can't destroy the resources because this results in a segfault on the +- * client when it correctly tries to call data_source.destroy. However, a +- * misbehaving client could still attempt to call accept or receive on the +- * data_offer, which would crash the server. +- * +- * So, we clear the user data on each of the offers to protect us. */ + wl_resource_for_each(offer, &data->offers) + { + wl_resource_set_user_data(offer, NULL); + wl_resource_set_destructor(offer, NULL); + } +- + free(data); + } + ++static void ++data_destroy(struct wl_resource *source) ++{ ++ release(wl_resource_get_user_data(source)); ++} ++ + struct wl_resource * + data_source_new(struct wl_client *client, uint32_t version, uint32_t id) + { +@@ -220,6 +223,7 @@ data_source_new(struct wl_client *client, uint32_t version, uint32_t id) + wl_array_init(&data->mime_types); + wl_list_init(&data->offers); + data->source_actions = data->action = 0; ++ memset(&data->host, 0, sizeof(data->host)); + data->accepted = false; + data->drag_offer = NULL; + data->offer_actions = data->preferred = data->chosen = 0; +@@ -241,10 +245,8 @@ error0: + } + + struct wl_resource * +-data_offer_new(struct wl_client *client, struct wl_resource *source, +- uint32_t version) ++data_offer_for(struct wl_client *client, struct data *data, uint32_t version) + { +- struct data *data = wl_resource_get_user_data(source); + struct wl_resource *offer; + + offer = wl_resource_create(client, &wl_data_offer_interface, version, 0); +@@ -258,16 +260,102 @@ data_offer_new(struct wl_client *client, struct wl_resource *source, + return offer; + } + ++struct wl_resource * ++data_offer_new(struct wl_client *client, struct wl_resource *source, ++ uint32_t version) ++{ ++ return data_offer_for(client, wl_resource_get_user_data(source), version); ++} ++ + void +-data_send_mime_types(struct wl_resource *source, struct wl_resource *offer) ++data_send_types(struct data *data, struct wl_resource *offer) + { +- struct data *data = wl_resource_get_user_data(source); + char **mime_type; + + wl_array_for_each(mime_type, &data->mime_types) + wl_data_offer_send_offer(offer, *mime_type); + } + ++void ++data_send_mime_types(struct wl_resource *source, struct wl_resource *offer) ++{ ++ data_send_types(wl_resource_get_user_data(source), offer); ++} ++ ++struct data * ++data_of(struct wl_resource *source) ++{ ++ return wl_resource_get_user_data(source); ++} ++ ++unsigned ++data_type_count(const struct data *data) ++{ ++ return data->mime_types.size / sizeof(char *); ++} ++ ++const char * ++data_type_at(const struct data *data, unsigned index) ++{ ++ return ((char **)data->mime_types.data)[index]; ++} ++ ++void ++data_request(struct data *data, const char *mime_type, int fd) ++{ ++ if (data->source) { ++ wl_data_source_send_send(data->source, mime_type, fd); ++ } else if (data->host.send) { ++ data->host.send(data->host.data, mime_type, fd); ++ } ++} ++ ++void ++data_cancel(struct data *data) ++{ ++ if (data->source) { ++ wl_data_source_send_cancelled(data->source); ++ } else if (data->host.cancelled) { ++ data->host.cancelled(data->host.data); ++ } ++} ++ ++struct data * ++data_host_new(const char *const *types, unsigned count, ++ const struct data_host *host, void *host_data) ++{ ++ struct data *data = calloc(1, sizeof(*data)); ++ char **dst; ++ unsigned i; ++ ++ if (!data) { ++ return NULL; ++ } ++ wl_array_init(&data->mime_types); ++ wl_list_init(&data->offers); ++ data->host = *host; ++ data->host.data = host_data; ++ for (i = 0; i < count; i++) { ++ dst = wl_array_add(&data->mime_types, sizeof(*dst)); ++ if (!dst) { ++ release(data); ++ return NULL; ++ } ++ if (!(*dst = strdup(types[i]))) { ++ data->mime_types.size -= sizeof(*dst); ++ release(data); ++ return NULL; ++ } ++ } ++ return data; ++} ++ ++void ++data_host_free(struct data *data) ++{ ++ release(data); ++} ++ + void + data_drag_enter(struct wl_resource *source, struct wl_resource *offer) + { +diff --git a/libswc/data.h b/libswc/data.h +index e271dd5..d1c762f 100644 +--- a/libswc/data.h ++++ b/libswc/data.h +@@ -37,6 +37,39 @@ data_offer_new(struct wl_client *client, struct wl_resource *source, + void + data_send_mime_types(struct wl_resource *source, struct wl_resource *offer); + ++struct data; ++ ++/* a source of the compositor's own, for a selection it keeps itself: it ++ * offers types and answers each ask on its own */ ++struct data_host { ++ void (*send)(void *data, const char *mime_type, int fd); ++ void (*cancelled)(void *data); ++ void *data; ++}; ++ ++struct data * ++data_host_new(const char *const *types, unsigned count, ++ const struct data_host *host, void *data); ++void ++data_host_free(struct data *data); ++struct data * ++data_of(struct wl_resource *source); ++/* offers of a selection, host or client */ ++struct wl_resource * ++data_offer_for(struct wl_client *client, struct data *data, uint32_t version); ++void ++data_send_types(struct data *data, struct wl_resource *offer); ++unsigned ++data_type_count(const struct data *data); ++const char * ++data_type_at(const struct data *data, unsigned index); ++/* asks the source for a type, to be written to fd (the caller closes its copy) */ ++void ++data_request(struct data *data, const char *mime_type, int fd); ++/* the selection went to someone else */ ++void ++data_cancel(struct data *data); ++ + /* a drag offer for source entered a client, nothing accepted yet */ + void + data_drag_enter(struct wl_resource *source, struct wl_resource *offer); +diff --git a/libswc/data_device.c b/libswc/data_device.c +index e24cbf6..3255fcc 100644 +--- a/libswc/data_device.c ++++ b/libswc/data_device.c +@@ -334,6 +334,11 @@ set_selection(struct wl_client *client, struct wl_resource *resource, + wl_data_source_send_cancelled(data_device->selection); + wl_list_remove(&data_device->selection_destroy_listener.link); + } ++ if (data_device->host) { ++ data_cancel(data_device->host); ++ data_host_free(data_device->host); ++ data_device->host = NULL; ++ } + + data_device->selection = data_source; + +@@ -373,6 +378,7 @@ data_device_create(void) + return NULL; + } + data_device->selection = NULL; ++ data_device->host = NULL; + data_device->selection_destroy_listener.notify = &handle_selection_destroy; + wl_signal_init(&data_device->event_signal); + wl_list_init(&data_device->resources); +@@ -387,6 +393,9 @@ data_device_destroy(struct data_device *data_device) + + wl_list_for_each_safe(resource, tmp, &data_device->resources, link) + wl_resource_destroy(resource); ++ if (data_device->host) { ++ data_host_free(data_device->host); ++ } + free(data_device); + } + +@@ -408,18 +417,41 @@ data_device_bind(struct data_device *data_device, struct wl_client *client, + return resource; + } + ++struct data * ++data_device_client_selection(struct data_device *data_device) ++{ ++ return data_device->selection ? data_of(data_device->selection) : NULL; ++} ++ ++void ++data_device_set_host(struct data_device *data_device, struct data *host) ++{ ++ if (data_device->selection) { ++ wl_data_source_send_cancelled(data_device->selection); ++ wl_list_remove(&data_device->selection_destroy_listener.link); ++ data_device->selection = NULL; ++ } ++ if (data_device->host) { ++ data_cancel(data_device->host); ++ data_host_free(data_device->host); ++ } ++ data_device->host = host; ++ send_event(&data_device->event_signal, DATA_DEVICE_EVENT_SELECTION_CHANGED, ++ host); ++} ++ + static struct wl_resource * + new_offer(struct wl_resource *resource, struct wl_client *client, +- struct wl_resource *source) ++ struct data *source) + { + struct wl_resource *offer; + +- offer = data_offer_new(client, source, wl_resource_get_version(resource)); ++ offer = data_offer_for(client, source, wl_resource_get_version(resource)); + if (!offer) { + return NULL; + } + wl_data_device_send_data_offer(resource, offer); +- data_send_mime_types(source, offer); ++ data_send_types(source, offer); + + return offer; + } +@@ -440,8 +472,10 @@ data_device_offer_selection(struct data_device *data_device, + } + + /* If we have a selection, create a new offer for the client. */ +- if (data_device->selection) { +- offer = new_offer(resource, client, data_device->selection); ++ if (data_device->host) { ++ offer = new_offer(resource, client, data_device->host); ++ } else if (data_device->selection) { ++ offer = new_offer(resource, client, data_of(data_device->selection)); + } + + wl_data_device_send_selection(resource, offer); +diff --git a/libswc/data_device.h b/libswc/data_device.h +index 6712190..f7242cc 100644 +--- a/libswc/data_device.h ++++ b/libswc/data_device.h +@@ -29,9 +29,13 @@ + + enum { DATA_DEVICE_EVENT_SELECTION_CHANGED }; + ++struct data; ++ + struct data_device { + /* The data source corresponding to the current selection. */ + struct wl_resource *selection; ++ /* the selection when the compositor keeps it itself, never with selection */ ++ struct data *host; + struct wl_listener selection_destroy_listener; + + struct wl_signal event_signal; +@@ -45,6 +49,14 @@ data_device_destroy(struct data_device *data_device); + struct wl_resource * + data_device_bind(struct data_device *data_device, struct wl_client *client, + uint32_t version, uint32_t id); ++/* the selection a client set, as data, NULL for none */ ++struct data * ++data_device_client_selection(struct data_device *data_device); ++/* takes the selection for the compositor, host owned by the device now. what ++ * held it is told it is cancelled. a event with data set says it came from ++ * the compositor and not from a client */ ++void ++data_device_set_host(struct data_device *data_device, struct data *host); + void + data_device_offer_selection(struct data_device *data_device, + struct wl_client *client); +diff --git a/libswc/meson.build b/libswc/meson.build +index b9cdde1..f506b5c 100644 +--- a/libswc/meson.build ++++ b/libswc/meson.build +@@ -21,6 +21,7 @@ libswc_src = files( + 'region.c', + 'screen.c', + 'select.c', ++ 'selection.c', + 'snap.c', + 'shell.c', + 'shell_surface.c', +diff --git a/libswc/selection.c b/libswc/selection.c +new file mode 100644 +index 0000000..b6845e0 +--- /dev/null ++++ b/libswc/selection.c +@@ -0,0 +1,100 @@ ++/* swc: selection.c ++ * ++ * the clipboard for a compositor that keeps it itself, see swc.h ++ * ++ * Permission is hereby granted, free of charge, to any person obtaining a copy ++ * of this software and associated documentation files (the "Software"), to deal ++ * in the Software without restriction, including without limitation the rights ++ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell ++ * copies of the Software, and to permit persons to whom the Software is ++ * furnished to do so, subject to the following conditions: ++ * ++ * The above copyright notice and this permission notice shall be included in ++ * all copies or substantial portions of the Software. ++ */ ++ ++#include "swc.h" ++#include "data.h" ++#include "data_device.h" ++#include "event.h" ++#include "internal.h" ++#include "seat.h" ++#include "util.h" ++ ++#include <string.h> ++ ++#define MAX_TYPES 64 ++ ++static struct { ++ const struct swc_selection_handler *handler; ++ void *data; ++ struct wl_listener listener; ++ bool hooked; ++} selection; ++ ++static void ++handle_event(struct wl_listener *listener, void *data) ++{ ++ struct event *ev = data; ++ struct data *source; ++ const char *types[MAX_TYPES]; ++ unsigned i, count = 0; ++ ++ if (ev->type != DATA_DEVICE_EVENT_SELECTION_CHANGED || ev->data || !selection.handler) { ++ return; ++ } ++ if ((source = data_device_client_selection(swc.seat->data_device))) { ++ count = data_type_count(source); ++ if (count > MAX_TYPES) { ++ count = MAX_TYPES; ++ } ++ for (i = 0; i < count; i++) { ++ types[i] = data_type_at(source, i); ++ } ++ } ++ selection.handler->changed(selection.data, types, count); ++} ++ ++EXPORT void ++swc_selection_set_handler(const struct swc_selection_handler *handler, void *data) ++{ ++ selection.handler = handler; ++ selection.data = data; ++ if (!selection.hooked && swc.seat) { ++ selection.listener.notify = handle_event; ++ wl_signal_add(&swc.seat->data_device->event_signal, &selection.listener); ++ selection.hooked = true; ++ } ++} ++ ++EXPORT bool ++swc_selection_request(const char *mime_type, int fd) ++{ ++ struct data *source = data_device_client_selection(swc.seat->data_device); ++ unsigned i; ++ ++ if (!source) { ++ return false; ++ } ++ for (i = 0; i < data_type_count(source); i++) { ++ if (strcmp(data_type_at(source, i), mime_type) == 0) { ++ data_request(source, mime_type, fd); ++ return true; ++ } ++ } ++ return false; ++} ++ ++EXPORT bool ++swc_selection_offer(const char *const *mime_types, unsigned count, ++ const struct swc_selection_source *source, void *data) ++{ ++ struct data_host host = { .send = source->send, .cancelled = source->cancelled }; ++ struct data *offered = data_host_new(mime_types, count, &host, data); ++ ++ if (!offered) { ++ return false; ++ } ++ data_device_set_host(swc.seat->data_device, offered); ++ return true; ++} +diff --git a/libswc/swc.h b/libswc/swc.h +index 96ea020..32f6c48 100644 +--- a/libswc/swc.h ++++ b/libswc/swc.h +@@ -144,6 +144,44 @@ swc_set_cursor_image(enum swc_cursor_kind kind, const uint32_t *argb8888, + void + swc_clear_cursor_image(enum swc_cursor_kind kind); + ++/** ++ * the clipboard, for a compositor that keeps it itself. a client sets the ++ * selection as it does in any compositor, and the handler is told which types ++ * it offers (none when it is gone, the client exited or the selection was ++ * cleared). what the compositor keeps it offers again as a selection of its ++ * own, so the clipboard does not end with the client that copied ++ */ ++struct swc_selection_handler { ++ void (*changed)(void *data, const char *const *mime_types, unsigned count); ++}; ++ ++/* what answers a paste of a selection the compositor offers */ ++struct swc_selection_source { ++ /* write the type to fd, which the caller closes */ ++ void (*send)(void *data, const char *mime_type, int fd); ++ /* another client took the selection */ ++ void (*cancelled)(void *data); ++}; ++ ++void ++swc_selection_set_handler(const struct swc_selection_handler *handler, void *data); ++ ++/** ++ * asks the client that holds the selection for a type, written to fd. ++ * false when no client holds it or it does not offer the type. the caller ++ * closes fd, the client has its own copy ++ */ ++bool ++swc_selection_request(const char *mime_type, int fd); ++ ++/** ++ * takes the selection, offering these types, answered by source. what was ++ * the selection is cancelled ++ */ ++bool ++swc_selection_offer(const char *const *mime_types, unsigned count, ++ const struct swc_selection_source *source, void *data); ++ + /** + * draw [or update] a simple box overlay + *