diff options
author | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-04-05 21:04:57 +0200 |
---|---|---|
committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-04-05 21:04:57 +0200 |
commit | 48170858f68f8bd39f5901eaddee840d772521f7 (patch) | |
tree | 7bc520728f71ec28faa66c4a95b59518ff7eb4b7 | |
parent | 8b0902af0ba4bbd0241150ec52cc1c77686a8605 (diff) |
add pointer-constraints protocol for mouse captures
-rw-r--r-- | Makefile | 9 | ||||
-rw-r--r-- | config.h | 63 | ||||
-rw-r--r-- | dwl.c | 281 | ||||
-rw-r--r-- | patches/pointer-constraints.patch | 563 |
4 files changed, 857 insertions, 59 deletions
@@ -9,14 +9,14 @@ DWLDEVCFLAGS = -pedantic -Wall -Wextra -Wdeclaration-after-statement -Wno-unused -Werror=strict-prototypes -Werror=implicit -Werror=return-type -Werror=incompatible-pointer-types # 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 dwl-bar-ipc-unstable-v1-protocol.o $(CC) dwl.o util.o dwl-bar-ipc-unstable-v1-protocol.o $(LDLIBS) $(LDFLAGS) $(DWLCFLAGS) -o $@ -dwl.o: dwl.c config.mk config.h client.h xdg-shell-protocol.h wlr-layer-shell-unstable-v1-protocol.h dwl-bar-ipc-unstable-v1-protocol.h +dwl.o: dwl.c config.mk config.h client.h xdg-shell-protocol.h wlr-layer-shell-unstable-v1-protocol.h dwl-bar-ipc-unstable-v1-protocol.h pointer-constraints-unstable-v1-protocol.h util.o: util.c util.h dwl-bar-ipc-unstable-v1-protocol.o: dwl-bar-ipc-unstable-v1-protocol.c dwl-bar-ipc-unstable-v1-protocol.h @@ -32,6 +32,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 $@ dwl-bar-ipc-unstable-v1-protocol.h: $(WAYLAND_SCANNER) server-header \ protocols/dwl-bar-ipc-unstable-v1.xml $@ @@ -113,32 +113,41 @@ static const char *menucmd[] = { "dmenu-wl_run", "-p", "Run", NULL }; static const Key keys[] = { /* Note that Shift changes certain key codes: c -> C, 2 -> at, etc. */ /* modifier key function argument */ - { MODKEY, XKB_KEY_p, spawn, {.v = menucmd} }, - { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_Return, spawn, {.v = termcmd} }, - { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_B, spawn, SHCMD("$BROWSER") }, - { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_L, spawn, SHCMD("wlock") }, - { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_P, spawn, SHCMD("passmenu --type") }, - { MODKEY, XKB_KEY_j, focusstack, {.i = +1} }, - { MODKEY, XKB_KEY_k, focusstack, {.i = -1} }, - { MODKEY, XKB_KEY_i, incnmaster, {.i = +1} }, - { MODKEY, XKB_KEY_d, incnmaster, {.i = -1} }, - { MODKEY, XKB_KEY_h, setmfact, {.f = -0.05} }, - { MODKEY, XKB_KEY_l, setmfact, {.f = +0.05} }, - { MODKEY, XKB_KEY_Return, zoom, {0} }, - { MODKEY, XKB_KEY_Tab, view, {0} }, - { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_C, killclient, {0} }, - { MODKEY, XKB_KEY_t, setlayout, {.v = &layouts[0]} }, - { MODKEY, XKB_KEY_f, setlayout, {.v = &layouts[1]} }, - { MODKEY, XKB_KEY_m, setlayout, {.v = &layouts[2]} }, - { MODKEY, XKB_KEY_space, setlayout, {0} }, - { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_space, togglefloating, {0} }, - { MODKEY, XKB_KEY_e, togglefullscreen, {0} }, - { MODKEY, XKB_KEY_0, view, {.ui = ~0} }, - { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_parenright, tag, {.ui = ~0} }, - { MODKEY, XKB_KEY_comma, focusmon, {.i = WLR_DIRECTION_LEFT} }, - { MODKEY, XKB_KEY_period, focusmon, {.i = WLR_DIRECTION_RIGHT} }, - { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_less, tagmon, {.i = WLR_DIRECTION_LEFT} }, - { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_greater, tagmon, {.i = WLR_DIRECTION_RIGHT} }, + { MODKEY, XKB_KEY_p, spawn, {.v = menucmd} }, + { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_Return, spawn, {.v = termcmd} }, + { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_B, spawn, SHCMD("$BROWSER") }, + { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_L, spawn, SHCMD("wlock") }, + { 0, XKB_KEY_Scroll_Lock, spawn, SHCMD("wlock") }, + { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_P, spawn, SHCMD("passmenu --type") }, + { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_E, spawn, SHCMD("st -e neomutt") }, + { MODKEY, XKB_KEY_F12, spawn, SHCMD("keyusbctl -t && status -u")}, + { 0, XKB_KEY_XF86AudioLowerVolume, spawn, SHCMD("pactl set-sink-volume 0 -3% && status -u")}, + { 0, XKB_KEY_XF86AudioRaiseVolume, spawn, SHCMD("pactl set-sink-volume 0 +3% && status -u")}, + { 0, XKB_KEY_XF86AudioMute, spawn, SHCMD("pactl set-sink-mute 0 toggle && status -u")}, + { 0, XKB_KEY_XF86AudioPlay, spawn, SHCMD("playerctl play-pause && status -u")}, + { 0, XKB_KEY_XF86AudioNext, spawn, SHCMD("playerctl next && status -u")}, + { 0, XKB_KEY_XF86AudioPrev, spawn, SHCMD("playerctl previous && status -u")}, + { MODKEY, XKB_KEY_j, focusstack, {.i = +1} }, + { MODKEY, XKB_KEY_k, focusstack, {.i = -1} }, + { MODKEY, XKB_KEY_i, incnmaster, {.i = +1} }, + { MODKEY, XKB_KEY_d, incnmaster, {.i = -1} }, + { MODKEY, XKB_KEY_h, setmfact, {.f = -0.05} }, + { MODKEY, XKB_KEY_l, setmfact, {.f = +0.05} }, + { MODKEY, XKB_KEY_Return, zoom, {0} }, + { MODKEY, XKB_KEY_Tab, view, {0} }, + { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_C, killclient, {0} }, + { MODKEY, XKB_KEY_t, setlayout, {.v = &layouts[0]} }, + { MODKEY, XKB_KEY_f, setlayout, {.v = &layouts[1]} }, + { MODKEY, XKB_KEY_m, setlayout, {.v = &layouts[2]} }, + { MODKEY, XKB_KEY_space, setlayout, {0} }, + { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_space, togglefloating, {0} }, + { MODKEY, XKB_KEY_e, togglefullscreen, {0} }, + { MODKEY, XKB_KEY_0, view, {.ui = ~0} }, + { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_parenright, tag, {.ui = ~0} }, + { MODKEY, XKB_KEY_comma, focusmon, {.i = WLR_DIRECTION_LEFT} }, + { MODKEY, XKB_KEY_period, focusmon, {.i = WLR_DIRECTION_RIGHT} }, + { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_less, tagmon, {.i = WLR_DIRECTION_LEFT} }, + { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_greater, tagmon, {.i = WLR_DIRECTION_RIGHT} }, TAGKEYS( XKB_KEY_1, XKB_KEY_exclam, 0), TAGKEYS( XKB_KEY_2, XKB_KEY_at, 1), TAGKEYS( XKB_KEY_3, XKB_KEY_numbersign, 2), @@ -148,7 +157,7 @@ static const Key keys[] = { TAGKEYS( XKB_KEY_7, XKB_KEY_ampersand, 6), TAGKEYS( XKB_KEY_8, XKB_KEY_asterisk, 7), TAGKEYS( XKB_KEY_9, XKB_KEY_parenleft, 8), - { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_Q, quit, {0} }, + { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_Q, quit, {0} }, /* Ctrl-Alt-Backspace and Ctrl-Alt-Fx used to be handled by X server */ { WLR_MODIFIER_CTRL|WLR_MODIFIER_ALT,XKB_KEY_Terminate_Server, quit, {0} }, @@ -5,6 +5,7 @@ #include <libinput.h> #include <limits.h> #include <linux/input-event-codes.h> +#include <pixman-1/pixman.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> @@ -34,9 +35,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> @@ -52,6 +55,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> #include "dwl-bar-ipc-unstable-v1-protocol.h" #ifdef XWAYLAND @@ -213,6 +217,14 @@ typedef struct { } 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; unsigned int tags; @@ -238,6 +250,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); @@ -247,6 +260,7 @@ 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 createdecoration(struct wl_listener *listener, void *data); +static void commitpointerconstraint(struct wl_listener *listener, void *data); static void createidleinhibitor(struct wl_listener *listener, void *data); static void createkeyboard(struct wlr_keyboard *keyboard); static void createlayersurface(struct wl_listener *listener, void *data); @@ -254,13 +268,17 @@ 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 destroydragicon(struct wl_listener *listener, void *data); static void destroyidleinhibitor(struct wl_listener *listener, void *data); 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); @@ -293,12 +311,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); @@ -367,6 +387,13 @@ static struct wlr_layer_shell_v1 *layer_shell; static struct wlr_output_manager_v1 *output_mgr; static struct wlr_virtual_keyboard_manager_v1 *virtual_keyboard_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; @@ -400,6 +427,7 @@ static struct wl_listener layout_change = {.notify = updatemons}; static struct wl_listener new_input = {.notify = inputdevice}; static struct wl_listener new_virtual_keyboard = {.notify = virtualkeyboard}; static struct wl_listener new_output = {.notify = createmon}; +static struct wl_listener new_pointer_constraint = {.notify = createpointerconstraint}; static struct wl_listener new_xdg_surface = {.notify = createnotify}; static struct wl_listener new_xdg_decoration = {.notify = createdecoration}; static struct wl_listener new_layer_shell_surface = {.notify = createlayersurface}; @@ -510,8 +538,7 @@ arrange(Monitor *m) strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, LENGTH(m->ltsymbol)); if (m && m->lt[m->sellt]->arrange) m->lt[m->sellt]->arrange(m); - motionnotify(0); - checkidleinhibitor(NULL); + motionnotify(0, NULL, 0, 0, 0, 0); } void @@ -632,7 +659,7 @@ buttonpress(struct wl_listener *listener, void *data) * we will send an enter event after which the client will provide us * a cursor surface */ wlr_seat_pointer_clear_focus(seat); - motionnotify(0); + motionnotify(0, NULL, 0, 0, 0, 0); /* Drop the window off on its new monitor */ selmon = xytomon(cursor->x, cursor->y); setmon(grabc, selmon, 0); @@ -649,6 +676,42 @@ buttonpress(struct wl_listener *listener, void *data) } 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, + floor(sx), floor(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, + sx - c->geom.x, 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) { wlr_session_change_vt(wlr_backend_get_session(backend), arg->ui); @@ -818,6 +881,12 @@ createdecoration(struct wl_listener *listener, void *data) } void +commitpointerconstraint(struct wl_listener *listener, void *data) +{ + checkconstraintregion(); +} + +void createidleinhibitor(struct wl_listener *listener, void *data) { struct wlr_idle_inhibitor_v1 *idle_inhibitor = data; @@ -1095,6 +1164,74 @@ createpointer(struct wlr_pointer *pointer) } 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) { /* This event is forwarded by the cursor when a pointer emits an frame @@ -1106,13 +1243,39 @@ cursorframe(struct wl_listener *listener, void *data) } 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) { struct wlr_drag_icon *icon = data; wlr_scene_node_destroy(icon->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 @@ -1147,7 +1310,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); @@ -1201,6 +1364,26 @@ destroynotify(struct wl_listener *listener, void *data) } 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) { SessionLock *lock = wl_container_of(listener, lock, destroy); @@ -1482,7 +1665,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)); @@ -1725,7 +1908,7 @@ maplayersurfacenotify(struct wl_listener *listener, void *data) { LayerSurface *l = wl_container_of(listener, l, map); wlr_surface_send_enter(l->layer_surface->surface, l->mon->wlr_output); - motionnotify(0); + motionnotify(0, NULL, 0, 0, 0, 0); } void @@ -1840,22 +2023,62 @@ 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; int type; struct wlr_surface *surface = NULL; struct wlr_drag_icon *icon; + 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) { + if ((type = toplevel_from_wlr_surface( + seat->pointer_state.focused_surface, &w, &l)) >= 0) { + c = w; + surface = seat->pointer_state.focused_surface; + sx = cursor->x - (type == LayerShell ? l->geom.x : w->geom.x); + sy = cursor->y - (type == LayerShell ? 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); + IDLE_NOTIFY_ACTIVITY; /* Update selmon (even while dragging a window) */ @@ -1879,19 +2102,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) { - if ((type = toplevel_from_wlr_surface( - seat->pointer_state.focused_surface, &w, &l)) >= 0) { - c = w; - surface = seat->pointer_state.focused_surface; - sx = cursor->x - (type == LayerShell ? l->geom.x : w->geom.x); - sy = cursor->y - (type == LayerShell ? 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. */ @@ -1912,8 +2122,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 @@ -2016,6 +2226,14 @@ outputmgrtest(struct wl_listener *listener, void *data) } 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) { @@ -2517,6 +2735,11 @@ setup(void) wl_signal_add(&output_mgr->events.apply, &output_mgr_apply); wl_signal_add(&output_mgr->events.test, &output_mgr_test); + relative_pointer_mgr = wlr_relative_pointer_manager_v1_create(dpy); + pointer_constraints = wlr_pointer_constraints_v1_create(dpy); + wl_signal_add(&pointer_constraints->events.new_constraint, &new_pointer_constraint); + wl_list_init(&pointer_constraint_commit.link); + wlr_scene_set_presentation(scene, wlr_presentation_create(dpy, backend)); wl_global_create(dpy, &zdwl_manager_v1_interface, 2, NULL, dwl_manager_bind); @@ -2557,7 +2780,7 @@ startdrag(struct wl_listener *listener, void *data) return; drag->icon->data = wlr_scene_subsurface_tree_create(layers[LyrDragIcon], drag->icon->surface); - motionnotify(0); + motionnotify(0, NULL, 0, 0, 0, 0); wl_signal_add(&drag->icon->events.destroy, &drag_icon_destroy); } @@ -2691,7 +2914,7 @@ unmaplayersurfacenotify(struct wl_listener *listener, void *data) if (layersurface->layer_surface->surface == seat->keyboard_state.focused_surface) focusclient(focustop(selmon), 1); - motionnotify(0); + motionnotify(0, NULL, 0, 0, 0, 0); } void @@ -2718,7 +2941,7 @@ unmapnotify(struct wl_listener *listener, void *data) wl_list_remove(&c->commit.link); wlr_scene_node_destroy(&c->scene->node); printstatus(); - motionnotify(0); + motionnotify(0, NULL, 0, 0, 0, 0); } void diff --git a/patches/pointer-constraints.patch b/patches/pointer-constraints.patch new file mode 100644 index 0000000..23d1ea1 --- /dev/null +++ b/patches/pointer-constraints.patch @@ -0,0 +1,563 @@ +From a18dab86893714b4103d9965ec0e143e53ffbfd8 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Leonardo=20Hern=C3=A1ndez=20Hern=C3=A1ndez?= + <leohdz172@protonmail.com> +Date: Thu, 26 May 2022 01:26:21 -0500 +Subject: [PATCH] implement pointer constraints + +mostly copied from sway implementation + +Co-authored-by: A Frederick Christenesn <dwl@ivories.org> +Co-authored-by: pm4rcin <37148802+pm4rcin@users.noreply.github.com> +--- + Makefile | 9 +- + dwl.c | 281 +++++++++++++++++++++++++++++++++++++++++++++++++------ + 2 files changed, 258 insertions(+), 32 deletions(-) + +diff --git a/Makefile b/Makefile +index ccca0794..28c57728 100644 +--- a/Makefile ++++ b/Makefile +@@ -9,14 +9,14 @@ DWLDEVCFLAGS = -pedantic -Wall -Wextra -Wdeclaration-after-statement -Wno-unused + -Werror=strict-prototypes -Werror=implicit -Werror=return-type -Werror=incompatible-pointer-types + + # 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 dwl-bar-ipc-unstable-v1-protocol.o + $(CC) dwl.o util.o dwl-bar-ipc-unstable-v1-protocol.o $(LDLIBS) $(LDFLAGS) $(DWLCFLAGS) -o $@ +-dwl.o: dwl.c config.mk config.h client.h xdg-shell-protocol.h wlr-layer-shell-unstable-v1-protocol.h dwl-bar-ipc-unstable-v1-protocol.h ++dwl.o: dwl.c config.mk config.h client.h xdg-shell-protocol.h wlr-layer-shell-unstable-v1-protocol.h dwl-bar-ipc-unstable-v1-protocol.h pointer-constraints-unstable-v1-protocol.h + util.o: util.c util.h + dwl-bar-ipc-unstable-v1-protocol.o: dwl-bar-ipc-unstable-v1-protocol.c dwl-bar-ipc-unstable-v1-protocol.h + +@@ -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 $@ + dwl-bar-ipc-unstable-v1-protocol.h: + $(WAYLAND_SCANNER) server-header \ + protocols/dwl-bar-ipc-unstable-v1.xml $@ +diff --git a/dwl.c b/dwl.c +index 8043bf98..5c018f55 100644 +--- a/dwl.c ++++ b/dwl.c +@@ -5,6 +5,7 @@ + #include <libinput.h> + #include <limits.h> + #include <linux/input-event-codes.h> ++#include <pixman-1/pixman.h> + #include <signal.h> + #include <stdio.h> + #include <stdlib.h> +@@ -33,9 +34,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> +@@ -51,6 +54,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> + #include "dwl-bar-ipc-unstable-v1-protocol.h" + #ifdef XWAYLAND +@@ -202,6 +206,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; +@@ -228,6 +240,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); +@@ -237,6 +250,7 @@ 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 createdecoration(struct wl_listener *listener, void *data); ++static void commitpointerconstraint(struct wl_listener *listener, void *data); + static void createidleinhibitor(struct wl_listener *listener, void *data); + static void createkeyboard(struct wlr_keyboard *keyboard); + static void createlayersurface(struct wl_listener *listener, void *data); +@@ -244,13 +258,17 @@ 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 destroydragicon(struct wl_listener *listener, void *data); + static void destroyidleinhibitor(struct wl_listener *listener, void *data); + 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); +@@ -272,12 +290,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); +@@ -345,6 +365,13 @@ static struct wlr_layer_shell_v1 *layer_shell; + static struct wlr_output_manager_v1 *output_mgr; + static struct wlr_virtual_keyboard_manager_v1 *virtual_keyboard_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; + +@@ -376,6 +403,7 @@ static struct wl_listener layout_change = {.notify = updatemons}; + static struct wl_listener new_input = {.notify = inputdevice}; + static struct wl_listener new_virtual_keyboard = {.notify = virtualkeyboard}; + static struct wl_listener new_output = {.notify = createmon}; ++static struct wl_listener new_pointer_constraint = {.notify = createpointerconstraint}; + static struct wl_listener new_xdg_surface = {.notify = createnotify}; + static struct wl_listener new_xdg_decoration = {.notify = createdecoration}; + static struct wl_listener new_layer_shell_surface = {.notify = createlayersurface}; +@@ -484,8 +512,7 @@ arrange(Monitor *m) + strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, LENGTH(m->ltsymbol)); + if (m && m->lt[m->sellt]->arrange) + m->lt[m->sellt]->arrange(m); +- motionnotify(0); +- checkidleinhibitor(NULL); ++ motionnotify(0, NULL, 0, 0, 0, 0); + } + + void +@@ -606,7 +633,7 @@ buttonpress(struct wl_listener *listener, void *data) + * we will send an enter event after which the client will provide us + * a cursor surface */ + wlr_seat_pointer_clear_focus(seat); +- motionnotify(0); ++ motionnotify(0, NULL, 0, 0, 0, 0); + /* Drop the window off on its new monitor */ + selmon = xytomon(cursor->x, cursor->y); + setmon(grabc, selmon, 0); +@@ -622,6 +649,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, ++ floor(sx), floor(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, ++ sx - c->geom.x, 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) + { +@@ -785,6 +848,12 @@ createdecoration(struct wl_listener *listener, void *data) + wlr_xdg_toplevel_decoration_v1_set_mode(dec, WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); + } + ++void ++commitpointerconstraint(struct wl_listener *listener, void *data) ++{ ++ checkconstraintregion(); ++} ++ + void + createidleinhibitor(struct wl_listener *listener, void *data) + { +@@ -1060,6 +1129,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) + { +@@ -1071,6 +1208,32 @@ cursorframe(struct wl_listener *listener, void *data) + wlr_seat_pointer_notify_frame(seat); + } + ++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) + { +@@ -1078,7 +1241,7 @@ destroydragicon(struct wl_listener *listener, void *data) + wlr_scene_node_destroy(icon->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 +@@ -1113,7 +1276,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); +@@ -1166,6 +1329,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) + { +@@ -1258,7 +1441,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)); +@@ -1501,7 +1684,7 @@ maplayersurfacenotify(struct wl_listener *listener, void *data) + { + LayerSurface *l = wl_container_of(listener, l, map); + wlr_surface_send_enter(l->layer_surface->surface, l->mon->wlr_output); +- motionnotify(0); ++ motionnotify(0, NULL, 0, 0, 0, 0); + } + + void +@@ -1612,22 +1795,62 @@ 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; + int type; + struct wlr_surface *surface = NULL; + struct wlr_drag_icon *icon; ++ 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) { ++ if ((type = toplevel_from_wlr_surface( ++ seat->pointer_state.focused_surface, &w, &l)) >= 0) { ++ c = w; ++ surface = seat->pointer_state.focused_surface; ++ sx = cursor->x - (type == LayerShell ? l->geom.x : w->geom.x); ++ sy = cursor->y - (type == LayerShell ? 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); ++ + IDLE_NOTIFY_ACTIVITY; + + /* Update selmon (even while dragging a window) */ +@@ -1651,19 +1874,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) { +- if ((type = toplevel_from_wlr_surface( +- seat->pointer_state.focused_surface, &w, &l)) >= 0) { +- c = w; +- surface = seat->pointer_state.focused_surface; +- sx = cursor->x - (type == LayerShell ? l->geom.x : w->geom.x); +- sy = cursor->y - (type == LayerShell ? 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. */ +@@ -1684,8 +1894,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 +@@ -1787,6 +1997,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) +@@ -2288,6 +2506,11 @@ setup(void) + wl_signal_add(&output_mgr->events.apply, &output_mgr_apply); + wl_signal_add(&output_mgr->events.test, &output_mgr_test); + ++ relative_pointer_mgr = wlr_relative_pointer_manager_v1_create(dpy); ++ pointer_constraints = wlr_pointer_constraints_v1_create(dpy); ++ wl_signal_add(&pointer_constraints->events.new_constraint, &new_pointer_constraint); ++ wl_list_init(&pointer_constraint_commit.link); ++ + wlr_scene_set_presentation(scene, wlr_presentation_create(dpy, backend)); + wl_global_create(dpy, &zdwl_manager_v1_interface, 2, NULL, dwl_manager_bind); + +@@ -2327,7 +2550,7 @@ startdrag(struct wl_listener *listener, void *data) + return; + + drag->icon->data = wlr_scene_subsurface_tree_create(layers[LyrDragIcon], drag->icon->surface); +- motionnotify(0); ++ motionnotify(0, NULL, 0, 0, 0, 0); + wl_signal_add(&drag->icon->events.destroy, &drag_icon_destroy); + } + +@@ -2452,7 +2675,7 @@ unmaplayersurfacenotify(struct wl_listener *listener, void *data) + if (layersurface->layer_surface->surface == + seat->keyboard_state.focused_surface) + focusclient(focustop(selmon), 1); +- motionnotify(0); ++ motionnotify(0, NULL, 0, 0, 0, 0); + } + + void +@@ -2479,7 +2702,7 @@ unmapnotify(struct wl_listener *listener, void *data) + wl_list_remove(&c->commit.link); + wlr_scene_node_destroy(&c->scene->node); + printstatus(); +- motionnotify(0); ++ motionnotify(0, NULL, 0, 0, 0, 0); + } + + void |