commit 2b8fb38f372c49c3fd47f16dd2ae143e09b4eab6
parent 1536be8f233685ceff2688724c8063430b3c677a
Author: dalem <lain@dalem.foo>
Date: Mon, 23 Feb 2026 23:09:52 +0100
Add a protocol for interactive area selection
This adds a new protocol "swc_select" to expose the internal overlay set box function to clients
Behavior wise it sets appropriate cursors for the state of the operation, and starts the overlay box drawing operation after a click
Diffstat:
6 files changed, 155 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -97,6 +97,7 @@ PROTO_EXTENSIONS= \
protocol/server-decoration.xml \
protocol/swc.xml \
protocol/swc_snap.xml \
+ protocol/swc_select.xml \
protocol/swc_wallpaper.xml \
protocol/wayland-drm.xml \
${WAYLAND_PROTOCOLS_DATADIR}/stable/xdg-shell/xdg-shell.xml \
@@ -125,6 +126,7 @@ PROTO_GEN_C= \
protocol/server-decoration-protocol.c \
protocol/swc-protocol.c \
protocol/swc_snap-protocol.c \
+ protocol/swc_select-protocol.c \
protocol/swc_wallpaper-protocol.c \
protocol/wayland-drm-protocol.c \
protocol/xdg-decoration-unstable-v1-protocol.c \
@@ -139,6 +141,8 @@ PROTO_GEN_H= \
protocol/swc-client-protocol.h \
protocol/swc_snap-server-protocol.h \
protocol/swc_snap-client-protocol.h \
+ protocol/swc_select-server-protocol.h \
+ protocol/swc_select-client-protocol.h \
protocol/swc_wallpaper-server-protocol.h \
protocol/swc_wallpaper-client-protocol.h \
protocol/wayland-drm-server-protocol.h \
@@ -205,6 +209,7 @@ SWC_SOURCES= \
libswc/subcompositor.c \
libswc/subsurface.c \
libswc/surface.c \
+ libswc/select.c \
libswc/swc.c \
libswc/util.c \
libswc/view.c \
@@ -217,6 +222,7 @@ SWC_SOURCES= \
protocol/server-decoration-protocol.c \
protocol/swc-protocol.c \
protocol/swc_snap-protocol.c \
+ protocol/swc_select-protocol.c \
protocol/swc_wallpaper-protocol.c \
protocol/wayland-drm-protocol.c \
protocol/xdg-decoration-unstable-v1-protocol.c \
diff --git a/libswc/internal.h b/libswc/internal.h
@@ -50,6 +50,7 @@ struct swc {
struct wl_global *panel_manager;
struct wl_global *shell;
struct wl_global *snap_manager;
+ struct wl_global *select_manager;
struct wl_global *subcompositor;
struct wl_global *wallpaper_manager;
struct wl_global *xdg_decoration_manager;
diff --git a/libswc/select.c b/libswc/select.c
@@ -0,0 +1,109 @@
+#include <wayland-server.h>
+
+#include "select.h"
+#include "swc_select-server-protocol.h"
+
+#include "compositor.h"
+#include "pointer.h"
+#include "seat.h"
+#include "util.h"
+
+static struct wl_list select_resources;
+static int32_t start_x, start_y;
+static struct pointer_handler select_pointer_handler;
+
+enum select_state { STATE_WAIT, STATE_DRAG };
+static enum select_state select_state;
+
+static bool
+handle_motion(struct pointer_handler *h, uint32_t time, wl_fixed_t fx,
+ wl_fixed_t fy)
+{
+ int32_t x = wl_fixed_to_int(fx);
+ int32_t y = wl_fixed_to_int(fy);
+ struct wl_resource *resource;
+
+ if (select_state != STATE_DRAG) {
+ return false;
+ }
+
+ /* TODO: customizable color, maybeee, idk idc */
+ swc_overlay_set_box(start_x, start_y, x, y, 0xffffffff, 2);
+
+ wl_resource_for_each(resource, &select_resources)
+ swc_select_send_update(resource, start_x, start_y, x, y);
+
+ return true;
+}
+
+static bool
+handle_button(struct pointer_handler *h, uint32_t time, struct button *button,
+ uint32_t state)
+{
+ int32_t x = wl_fixed_to_int(swc.seat->pointer->x);
+ int32_t y = wl_fixed_to_int(swc.seat->pointer->y);
+ struct wl_resource *resource;
+
+ if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
+ select_state == STATE_WAIT) {
+ start_x = x;
+ start_y = y;
+ select_state = STATE_DRAG;
+ return true;
+ }
+
+ if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
+ select_state == STATE_DRAG) {
+ swc_overlay_clear();
+ wl_list_remove(&select_pointer_handler.link);
+ select_state = STATE_WAIT;
+ swc_set_cursor(SWC_CURSOR_DEFAULT);
+
+ wl_resource_for_each(resource, &select_resources)
+ swc_select_send_done(resource, start_x, start_y, x, y);
+
+ return true;
+ }
+
+ return false;
+}
+
+static void
+handle_grab(struct wl_client *client, struct wl_resource *resource)
+{
+ select_state = STATE_WAIT;
+ select_pointer_handler.motion = handle_motion;
+ select_pointer_handler.button = handle_button;
+ wl_list_insert(&swc.seat->pointer->handlers, &select_pointer_handler.link);
+ swc_set_cursor(SWC_CURSOR_CROSS);
+}
+
+static const struct swc_select_interface select_impl = {
+ .grab = handle_grab,
+};
+
+static void
+bind_select(struct wl_client *client, void *data, uint32_t version, uint32_t id)
+{
+ struct wl_resource *resource;
+
+ resource = wl_resource_create(client, &swc_select_interface, 1, id);
+ if (!resource) {
+ wl_client_post_no_memory(client);
+ return;
+ }
+
+ wl_resource_set_implementation(resource, &select_impl, NULL,
+ remove_resource);
+ wl_list_insert(&select_resources, wl_resource_get_link(resource));
+}
+
+struct wl_global *
+select_manager_create(struct wl_display *display)
+{
+ wl_list_init(&select_resources);
+ select_pointer_handler.motion = handle_motion;
+ select_pointer_handler.button = handle_button;
+ return wl_global_create(display, &swc_select_interface, 1, NULL,
+ &bind_select);
+}
diff --git a/libswc/select.h b/libswc/select.h
@@ -0,0 +1,4 @@
+#include <wayland-server.h>
+
+struct wl_global *
+select_manager_create(struct wl_display *display);
diff --git a/libswc/swc.c b/libswc/swc.c
@@ -35,6 +35,7 @@
#include "pointer.h"
#include "screen.h"
#include "seat.h"
+#include "select.h"
#include "shell.h"
#include "shm.h"
#include "snap.h"
@@ -251,10 +252,18 @@ swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop,
}
#endif
+ swc.select_manager = select_manager_create(display);
+ if (!swc.select_manager) {
+ ERROR("Could not initialize select manager\n");
+ goto error17;
+ }
+
setup_compositor();
return true;
+error17:
+ wl_global_destroy(swc.select_manager);
#ifdef ENABLE_XWAYLAND
error16:
wl_global_destroy(swc.wallpaper_manager);
diff --git a/protocol/swc_select.xml b/protocol/swc_select.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<protocol name="swc_select">
+ <interface name="swc_select" version="1">
+ <description summary="Get a selection box">
+ Allows clients to get an interactive selction box.
+ </description>
+
+ <request name="grab" />
+ <event name="update" >
+ <arg name="x1" type="int" />
+ <arg name="y1" type="int" />
+ <arg name="x2" type="int" />
+ <arg name="y2" type="int" />
+ </event>
+
+ <event name="done" >
+ <arg name="x1" type="int" />
+ <arg name="y1" type="int" />
+ <arg name="x2" type="int" />
+ <arg name="y2" type="int" />
+ </event>
+
+ <event name="cancelled" />
+ <request name="destroy" type="destructor" />
+ </interface>
+</protocol>