Added Sway-pointer-constraints patch

This commit is contained in:
Erwinjitsu 2024-01-24 22:57:00 +02:00
parent 4a339aa239
commit 4db1819a09
2 changed files with 537 additions and 9 deletions

View File

@ -0,0 +1,537 @@
From 084ad6ff6b5db9935e58d22713383055f09496cb Mon Sep 17 00:00:00 2001
From: Erwinjitsu <erwinjitsu@proton.me>
Date: Wed, 24 Jan 2024 22:48:37 +0200
Subject: [PATCH] Implement pointer constraints
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Mostly copied from sway implementation
Original: Leonardo Hernández Hernández <leohdz172@proton.me>
Modified-by: A Frederick Christensen <dwl@ivories.org>
Modified-by: pm4rcin <37148802+pm4rcin@users.noreply.github.com>
Modified-by: migue07juegos <104018233+migue07juegos@users.noreply.github.com>
---
Makefile | 9 +-
dwl.c | 272 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 253 insertions(+), 28 deletions(-)
diff --git a/Makefile b/Makefile
index 0822ddc..0d2fb97 100644
--- a/Makefile
+++ b/Makefile
@@ -9,14 +9,14 @@ DWLDEVCFLAGS = -g -pedantic -Wall -Wextra -Wdeclaration-after-statement -Wno-unu
-Werror=strict-prototypes -Werror=implicit -Werror=return-type -Werror=incompatible-pointer-types -Wfloat-conversion
# CFLAGS / LDFLAGS
-PKGS = wlroots wayland-server xkbcommon libinput $(XLIBS)
+PKGS = wlroots wayland-server xkbcommon libinput pixman-1 $(XLIBS)
DWLCFLAGS = `$(PKG_CONFIG) --cflags $(PKGS)` $(DWLCPPFLAGS) $(DWLDEVCFLAGS) $(CFLAGS)
-LDLIBS = `$(PKG_CONFIG) --libs $(PKGS)` $(LIBS)
+LDLIBS = `$(PKG_CONFIG) --libs $(PKGS)` -lm $(LIBS)
all: dwl
dwl: dwl.o util.o
$(CC) dwl.o util.o $(LDLIBS) $(LDFLAGS) $(DWLCFLAGS) -o $@
-dwl.o: dwl.c config.mk config.h client.h cursor-shape-v1-protocol.h xdg-shell-protocol.h wlr-layer-shell-unstable-v1-protocol.h
+dwl.o: dwl.c config.mk config.h client.h cursor-shape-v1-protocol.h xdg-shell-protocol.h wlr-layer-shell-unstable-v1-protocol.h pointer-constraints-unstable-v1-protocol.h
util.o: util.c util.h
# wayland-scanner is a tool which generates C headers and rigging for Wayland
@@ -31,6 +31,9 @@ xdg-shell-protocol.h:
wlr-layer-shell-unstable-v1-protocol.h:
$(WAYLAND_SCANNER) server-header \
protocols/wlr-layer-shell-unstable-v1.xml $@
+pointer-constraints-unstable-v1-protocol.h:
+ $(WAYLAND_SCANNER) server-header \
+ $(WAYLAND_PROTOCOLS)/unstable/pointer-constraints/pointer-constraints-unstable-v1.xml $@
cursor-shape-v1-protocol.h:
$(WAYLAND_SCANNER) server-header \
$(WAYLAND_PROTOCOLS)/staging/cursor-shape/cursor-shape-v1.xml $@
diff --git a/dwl.c b/dwl.c
index bf02a6d..402d991 100644
--- a/dwl.c
+++ b/dwl.c
@@ -4,6 +4,7 @@
#include <getopt.h>
#include <libinput.h>
#include <linux/input-event-codes.h>
+#include <pixman-1/pixman.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
@@ -35,9 +36,11 @@
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_output_management_v1.h>
#include <wlr/types/wlr_pointer.h>
+#include <wlr/types/wlr_pointer_constraints_v1.h>
#include <wlr/types/wlr_presentation_time.h>
#include <wlr/types/wlr_primary_selection.h>
#include <wlr/types/wlr_primary_selection_v1.h>
+#include <wlr/types/wlr_relative_pointer_v1.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/types/wlr_screencopy_v1.h>
#include <wlr/types/wlr_seat.h>
@@ -53,6 +56,7 @@
#include <wlr/types/wlr_xdg_output_v1.h>
#include <wlr/types/wlr_xdg_shell.h>
#include <wlr/util/log.h>
+#include <wlr/util/region.h>
#include <xkbcommon/xkbcommon.h>
#ifdef XWAYLAND
#include <wlr/xwayland.h>
@@ -213,6 +217,14 @@ typedef struct {
int x, y;
} MonitorRule;
+typedef struct {
+ struct wlr_pointer_constraint_v1 *constraint;
+ Client *focused;
+
+ struct wl_listener set_region;
+ struct wl_listener destroy;
+} PointerConstraint;
+
typedef struct {
const char *id;
const char *title;
@@ -239,6 +251,7 @@ static void arrangelayer(Monitor *m, struct wl_list *list,
static void arrangelayers(Monitor *m);
static void axisnotify(struct wl_listener *listener, void *data);
static void buttonpress(struct wl_listener *listener, void *data);
+static void checkconstraintregion(void);
static void chvt(const Arg *arg);
static void checkidleinhibitor(struct wlr_surface *exclude);
static void cleanup(void);
@@ -246,6 +259,7 @@ static void cleanupmon(struct wl_listener *listener, void *data);
static void closemon(Monitor *m);
static void commitlayersurfacenotify(struct wl_listener *listener, void *data);
static void commitnotify(struct wl_listener *listener, void *data);
+static void commitpointerconstraint(struct wl_listener *listener, void *data);
static void createdecoration(struct wl_listener *listener, void *data);
static void createidleinhibitor(struct wl_listener *listener, void *data);
static void createkeyboard(struct wlr_keyboard *keyboard);
@@ -254,7 +268,10 @@ static void createlocksurface(struct wl_listener *listener, void *data);
static void createmon(struct wl_listener *listener, void *data);
static void createnotify(struct wl_listener *listener, void *data);
static void createpointer(struct wlr_pointer *pointer);
+static void createpointerconstraint(struct wl_listener *listener, void *data);
+static void cursorconstrain(struct wlr_pointer_constraint_v1 *constraint);
static void cursorframe(struct wl_listener *listener, void *data);
+static void cursorwarptoconstrainthint(void);
static void destroydecoration(struct wl_listener *listener, void *data);
static void destroydragicon(struct wl_listener *listener, void *data);
static void destroyidleinhibitor(struct wl_listener *listener, void *data);
@@ -262,6 +279,7 @@ static void destroylayersurfacenotify(struct wl_listener *listener, void *data);
static void destroylock(SessionLock *lock, int unlocked);
static void destroylocksurface(struct wl_listener *listener, void *data);
static void destroynotify(struct wl_listener *listener, void *data);
+static void destroypointerconstraint(struct wl_listener *listener, void *data);
static void destroysessionlock(struct wl_listener *listener, void *data);
static void destroysessionmgr(struct wl_listener *listener, void *data);
static Monitor *dirtomon(enum wlr_direction dir);
@@ -284,12 +302,14 @@ static void mapnotify(struct wl_listener *listener, void *data);
static void maximizenotify(struct wl_listener *listener, void *data);
static void monocle(Monitor *m);
static void motionabsolute(struct wl_listener *listener, void *data);
-static void motionnotify(uint32_t time);
+static void motionnotify(uint32_t time, struct wlr_input_device *device, double sx,
+ double sy, double sx_unaccel, double sy_unaccel);
static void motionrelative(struct wl_listener *listener, void *data);
static void moveresize(const Arg *arg);
static void outputmgrapply(struct wl_listener *listener, void *data);
static void outputmgrapplyortest(struct wlr_output_configuration_v1 *config, int test);
static void outputmgrtest(struct wl_listener *listener, void *data);
+static void pointerconstraintsetregion(struct wl_listener *listener, void *data);
static void pointerfocus(Client *c, struct wlr_surface *surface,
double sx, double sy, uint32_t time);
static void printstatus(void);
@@ -363,6 +383,13 @@ static struct wlr_gamma_control_manager_v1 *gamma_control_mgr;
static struct wlr_virtual_keyboard_manager_v1 *virtual_keyboard_mgr;
static struct wlr_cursor_shape_manager_v1 *cursor_shape_mgr;
+static struct wlr_relative_pointer_manager_v1 *relative_pointer_mgr;
+static struct wlr_pointer_constraints_v1 *pointer_constraints;
+static struct wl_listener pointer_constraint_commit;
+static PointerConstraint *active_constraint;
+static pixman_region32_t active_confine;
+static int active_confine_requires_warp;
+
static struct wlr_cursor *cursor;
static struct wlr_xcursor_manager *cursor_mgr;
@@ -472,8 +499,7 @@ arrange(Monitor *m)
if (m->lt[m->sellt]->arrange)
m->lt[m->sellt]->arrange(m);
- motionnotify(0);
- checkidleinhibitor(NULL);
+ motionnotify(0, NULL, 0, 0, 0, 0);
}
void
@@ -605,6 +631,42 @@ buttonpress(struct wl_listener *listener, void *data)
event->time_msec, event->button, event->state);
}
+void
+checkconstraintregion(void)
+{
+ struct wlr_pointer_constraint_v1 *constraint = active_constraint->constraint;
+ pixman_region32_t *region = &constraint->region;
+ Client *c = NULL;
+ double sx, sy;
+ toplevel_from_wlr_surface(constraint->surface, &c, NULL);
+ if (active_confine_requires_warp && c) {
+ active_confine_requires_warp = 0;
+
+ sx = cursor->x + c->geom.x;
+ sy = cursor->y + c->geom.y;
+
+ if (!pixman_region32_contains_point(region,
+ ROUND(sx), ROUND(sy), NULL)) {
+ int nboxes;
+ pixman_box32_t *boxes = pixman_region32_rectangles(region, &nboxes);
+ if (nboxes > 0) {
+ sx = (boxes[0].x1 + boxes[0].x2) / 2.;
+ sy = (boxes[0].y1 + boxes[0].y2) / 2.;
+
+ wlr_cursor_warp_closest(cursor, NULL,
+ c->geom.x - sx, sy - c->geom.y);
+ }
+ }
+ }
+
+ /* A locked pointer will result in an empty region, thus disallowing all movement. */
+ if (constraint->type == WLR_POINTER_CONSTRAINT_V1_CONFINED) {
+ pixman_region32_copy(&active_confine, region);
+ } else {
+ pixman_region32_clear(&active_confine);
+ }
+}
+
void
chvt(const Arg *arg)
{
@@ -754,6 +816,12 @@ createdecoration(struct wl_listener *listener, void *data)
requestdecorationmode(&c->set_decoration_mode, deco);
}
+void
+commitpointerconstraint(struct wl_listener *listener, void *data)
+{
+ checkconstraintregion();
+}
+
void
createidleinhibitor(struct wl_listener *listener, void *data)
{
@@ -1008,6 +1076,74 @@ createpointer(struct wlr_pointer *pointer)
wlr_cursor_attach_input_device(cursor, &pointer->base);
}
+void
+createpointerconstraint(struct wl_listener *listener, void *data)
+{
+ struct wlr_pointer_constraint_v1 *wlr_constraint = data;
+ PointerConstraint *constraint = ecalloc(1, sizeof(*constraint));
+ Client *c = NULL, *sel = focustop(selmon);
+ toplevel_from_wlr_surface(wlr_constraint->surface, &c, NULL);
+ constraint->constraint = wlr_constraint;
+ wlr_constraint->data = constraint;
+
+ LISTEN(&wlr_constraint->events.set_region, &constraint->set_region,
+ pointerconstraintsetregion);
+ LISTEN(&wlr_constraint->events.destroy, &constraint->destroy,
+ destroypointerconstraint);
+
+ if (c == sel)
+ cursorconstrain(wlr_constraint);
+}
+
+void
+cursorconstrain(struct wlr_pointer_constraint_v1 *wlr_constraint)
+{
+ PointerConstraint *constraint = wlr_constraint->data;
+
+ if (active_constraint == constraint)
+ return;
+
+ wl_list_remove(&pointer_constraint_commit.link);
+ if (active_constraint) {
+ if (!wlr_constraint)
+ cursorwarptoconstrainthint();
+
+ wlr_pointer_constraint_v1_send_deactivated(active_constraint->constraint);
+ }
+
+ active_constraint = constraint;
+
+ if (!wlr_constraint) {
+ wl_list_init(&pointer_constraint_commit.link);
+ return;
+ }
+
+ active_confine_requires_warp = 1;
+
+ /* Stolen from sway/input/cursor.c:1435
+ *
+ * FIXME: Big hack, stolen from wlr_pointer_constraints_v1.c:121.
+ * This is necessary because the focus may be set before the surface
+ * has finished committing, which means that warping won't work properly,
+ * since this code will be run *after* the focus has been set.
+ * That is why we duplicate the code here.
+ */
+ if (pixman_region32_not_empty(&wlr_constraint->current.region)) {
+ pixman_region32_intersect(&wlr_constraint->region,
+ &wlr_constraint->surface->input_region, &wlr_constraint->current.region);
+ } else {
+ pixman_region32_copy(&wlr_constraint->region,
+ &wlr_constraint->surface->input_region);
+ }
+
+ checkconstraintregion();
+
+ wlr_pointer_constraint_v1_send_activated(wlr_constraint);
+
+ LISTEN(&wlr_constraint->surface->events.commit, &pointer_constraint_commit,
+ commitpointerconstraint);
+}
+
void
cursorframe(struct wl_listener *listener, void *data)
{
@@ -1028,12 +1164,38 @@ destroydecoration(struct wl_listener *listener, void *data)
wl_list_remove(&c->set_decoration_mode.link);
}
+void
+cursorwarptoconstrainthint(void)
+{
+ struct wlr_pointer_constraint_v1 *constraint = active_constraint->constraint;
+
+ if (constraint->current.committed &
+ WLR_POINTER_CONSTRAINT_V1_STATE_CURSOR_HINT) {
+ double lx, ly;
+ double sx = lx = constraint->current.cursor_hint.x;
+ double sy = ly = constraint->current.cursor_hint.y;
+
+ Client *c = NULL;
+ toplevel_from_wlr_surface(constraint->surface, &c, NULL);
+ if (c) {
+ lx -= c->geom.x;
+ ly -= c->geom.y;
+ }
+
+ wlr_cursor_warp(cursor, NULL, lx, ly);
+
+ /* Warp the pointer as well, so that on the next pointer rebase we don't
+ * send an unexpected synthetic motion event to clients. */
+ wlr_seat_pointer_warp(seat, sx, sy);
+ }
+}
+
void
destroydragicon(struct wl_listener *listener, void *data)
{
/* Focus enter isn't sent during drag, so refocus the focused node. */
focusclient(focustop(selmon), 1);
- motionnotify(0);
+ motionnotify(0, NULL, 0, 0, 0, 0);
}
void
@@ -1069,7 +1231,7 @@ destroylock(SessionLock *lock, int unlock)
wlr_scene_node_set_enabled(&locked_bg->node, 0);
focusclient(focustop(selmon), 0);
- motionnotify(0);
+ motionnotify(0, NULL, 0, 0, 0, 0);
destroy:
wl_list_remove(&lock->new_surface.link);
@@ -1128,6 +1290,26 @@ destroynotify(struct wl_listener *listener, void *data)
free(c);
}
+void
+destroypointerconstraint(struct wl_listener *listener, void *data)
+{
+ PointerConstraint *constraint = wl_container_of(listener, constraint, destroy);
+ wl_list_remove(&constraint->set_region.link);
+ wl_list_remove(&constraint->destroy.link);
+
+ if (active_constraint == constraint) {
+ cursorwarptoconstrainthint();
+
+ if (pointer_constraint_commit.link.next)
+ wl_list_remove(&pointer_constraint_commit.link);
+
+ wl_list_init(&pointer_constraint_commit.link);
+ active_constraint = NULL;
+ }
+
+ free(constraint);
+}
+
void
destroysessionlock(struct wl_listener *listener, void *data)
{
@@ -1224,7 +1406,7 @@ focusclient(Client *c, int lift)
}
/* Change cursor surface */
- motionnotify(0);
+ motionnotify(0, NULL, 0, 0, 0, 0);
/* Have a client, so focus its top-level wlr_surface */
client_notify_enter(client_surface(c), wlr_seat_get_keyboard(seat));
@@ -1483,7 +1665,7 @@ locksession(struct wl_listener *listener, void *data)
void
maplayersurfacenotify(struct wl_listener *listener, void *data)
{
- motionnotify(0);
+ motionnotify(0, NULL, 0, 0, 0, 0);
}
void
@@ -1599,20 +1781,58 @@ motionabsolute(struct wl_listener *listener, void *data)
* so we have to warp the mouse there. There is also some hardware which
* emits these events. */
struct wlr_pointer_motion_absolute_event *event = data;
- wlr_cursor_warp_absolute(cursor, &event->pointer->base, event->x, event->y);
- motionnotify(event->time_msec);
+ double lx, ly, dx, dy;
+ wlr_cursor_absolute_to_layout_coords(cursor, &event->pointer->base, event->x, event->y, &lx, &ly);
+ dx = lx - cursor->x;
+ dy = ly - cursor->y;
+
+ motionnotify(event->time_msec, &event->pointer->base, dx, dy, dx, dy);
}
void
-motionnotify(uint32_t time)
+motionnotify(uint32_t time, struct wlr_input_device *device, double dx, double dy,
+ double dx_unaccel, double dy_unaccel)
{
double sx = 0, sy = 0;
+ double sx_confined, sy_confined;
Client *c = NULL, *w = NULL;
LayerSurface *l = NULL;
struct wlr_surface *surface = NULL;
+ struct wlr_pointer_constraint_v1 *constraint = NULL;
+
+ /* Find the client under the pointer and send the event along. */
+ xytonode(cursor->x, cursor->y, &surface, &c, NULL, &sx, &sy);
+
+ if (cursor_mode == CurPressed && !seat->drag && surface != held_grab
+ && toplevel_from_wlr_surface(held_grab, &w, &l) >= 0) {
+ c = w;
+ surface = held_grab;
+ sx = cursor->x - (l ? l->geom.x : w->geom.x);
+ sy = cursor->y - (l ? l->geom.y : w->geom.y);
+ }
/* time is 0 in internal calls meant to restore pointer focus. */
if (time) {
+ wlr_relative_pointer_manager_v1_send_relative_motion(
+ relative_pointer_mgr, seat, (uint64_t)time * 1000,
+ dx, dy, dx_unaccel, dy_unaccel);
+
+ wl_list_for_each(constraint, &pointer_constraints->constraints, link)
+ cursorconstrain(constraint);
+
+ if (active_constraint) {
+ constraint = active_constraint->constraint;
+ if (constraint->surface == surface
+ && wlr_region_confine(&active_confine, sx, sy, sx + dx,
+ sy + dy, &sx_confined, &sy_confined)) {
+ dx = sx_confined - sx;
+ dy = sy_confined - sy;
+ } else {
+ return;
+ }
+ }
+ wlr_cursor_move(cursor, device, dx, dy);
+
wlr_idle_notifier_v1_notify_activity(idle_notifier, seat);
/* Update selmon (even while dragging a window) */
@@ -1635,17 +1855,6 @@ motionnotify(uint32_t time)
return;
}
- /* Find the client under the pointer and send the event along. */
- xytonode(cursor->x, cursor->y, &surface, &c, NULL, &sx, &sy);
-
- if (cursor_mode == CurPressed && !seat->drag && surface != held_grab
- && toplevel_from_wlr_surface(held_grab, &w, &l) >= 0) {
- c = w;
- surface = held_grab;
- sx = cursor->x - (l ? l->geom.x : w->geom.x);
- sy = cursor->y - (l ? l->geom.y : w->geom.y);
- }
-
/* If there's no client surface under the cursor, set the cursor image to a
* default. This is what makes the cursor image appear when you move it
* off of a client or over its border. */
@@ -1666,8 +1875,8 @@ motionrelative(struct wl_listener *listener, void *data)
* special configuration applied for the specific input device which
* generated the event. You can pass NULL for the device if you want to move
* the cursor around without any input. */
- wlr_cursor_move(cursor, &event->pointer->base, event->delta_x, event->delta_y);
- motionnotify(event->time_msec);
+ motionnotify(event->time_msec, &event->pointer->base, event->delta_x, event->delta_y,
+ event->unaccel_dx, event->unaccel_dy);
}
void
@@ -1766,6 +1975,14 @@ outputmgrtest(struct wl_listener *listener, void *data)
outputmgrapplyortest(config, 1);
}
+void
+pointerconstraintsetregion(struct wl_listener *listener, void *data)
+{
+ PointerConstraint *constraint = wl_container_of(listener, constraint, set_region);
+ active_confine_requires_warp = 1;
+ constraint->constraint->surface->data = NULL;
+}
+
void
pointerfocus(Client *c, struct wlr_surface *surface, double sx, double sy,
uint32_t time)
@@ -2384,6 +2601,11 @@ setup(void)
LISTEN_STATIC(&output_mgr->events.apply, outputmgrapply);
LISTEN_STATIC(&output_mgr->events.test, outputmgrtest);
+ relative_pointer_mgr = wlr_relative_pointer_manager_v1_create(dpy);
+ pointer_constraints = wlr_pointer_constraints_v1_create(dpy);
+ LISTEN_STATIC(&pointer_constraints->events.new_constraint, createpointerconstraint);
+ wl_list_init(&pointer_constraint_commit.link);
+
wlr_scene_set_presentation(scene, wlr_presentation_create(dpy, backend));
/* Make sure XWayland clients don't connect to the parent X server,
@@ -2547,7 +2769,7 @@ unmaplayersurfacenotify(struct wl_listener *listener, void *data)
arrangelayers(l->mon);
if (l->layer_surface->surface == seat->keyboard_state.focused_surface)
focusclient(focustop(selmon), 1);
- motionnotify(0);
+ motionnotify(0, NULL, 0, 0, 0, 0);
}
void
@@ -2573,7 +2795,7 @@ unmapnotify(struct wl_listener *listener, void *data)
wlr_scene_node_destroy(&c->scene->node);
printstatus();
- motionnotify(0);
+ motionnotify(0, NULL, 0, 0, 0, 0);
}
void
--
2.43.0

View File

@ -1,9 +0,0 @@
### Description
Implement pointer constraint and relative pointer protocols, useful for games
**NOTE**: this patch is currently semi-broken and needs you to resize/move clients to work properly (unsetting and then setting fullscreen should be enough)
### Download
- [2023-08-22](https://github.com/djpohly/dwl/compare/main...sevz17:pointer-constraints.patch)
### Authors
- [sevz](https://github.com/sevz17)