applied patch gestures

This commit is contained in:
2025-10-26 00:24:31 +02:00
parent 887eaab90f
commit 485feecc1c

69
dwl.c
View File

@@ -87,6 +87,8 @@ enum { CurNormal, CurPressed, CurMove, CurResize }; /* cursor */
enum { XDGShell, LayerShell, X11 }; /* client types */ enum { XDGShell, LayerShell, X11 }; /* client types */
enum { LyrBg, LyrBottom, LyrTile, LyrFloat, LyrTop, LyrFS, LyrOverlay, LyrBlock, NUM_LAYERS }; /* scene layers */ enum { LyrBg, LyrBottom, LyrTile, LyrFloat, LyrTop, LyrFS, LyrOverlay, LyrBlock, NUM_LAYERS }; /* scene layers */
enum { SWIPE_LEFT, SWIPE_RIGHT, SWIPE_DOWN, SWIPE_UP };
typedef union { typedef union {
int i; int i;
uint32_t ui; uint32_t ui;
@@ -101,6 +103,14 @@ typedef struct {
const Arg arg; const Arg arg;
} Button; } Button;
typedef struct {
unsigned int mod;
unsigned int motion;
unsigned int fingers_count;
void (*func)(const Arg *);
const Arg arg;
} Gesture;
typedef struct Monitor Monitor; typedef struct Monitor Monitor;
typedef struct { typedef struct {
/* Must keep this field first */ /* Must keep this field first */
@@ -249,6 +259,7 @@ static void arrangelayer(Monitor *m, struct wl_list *list,
static void arrangelayers(Monitor *m); static void arrangelayers(Monitor *m);
static void axisnotify(struct wl_listener *listener, void *data); static void axisnotify(struct wl_listener *listener, void *data);
static void buttonpress(struct wl_listener *listener, void *data); static void buttonpress(struct wl_listener *listener, void *data);
static int ongesture(struct wlr_pointer_swipe_end_event *event);
static void swipe_begin(struct wl_listener *listener, void *data); static void swipe_begin(struct wl_listener *listener, void *data);
static void swipe_update(struct wl_listener *listener, void *data); static void swipe_update(struct wl_listener *listener, void *data);
static void swipe_end(struct wl_listener *listener, void *data); static void swipe_end(struct wl_listener *listener, void *data);
@@ -416,6 +427,10 @@ static struct wlr_box sgeom;
static struct wl_list mons; static struct wl_list mons;
static Monitor *selmon; static Monitor *selmon;
static uint32_t swipe_fingers = 0;
static double swipe_dx = 0;
static double swipe_dy = 0;
/* global event handlers */ /* global event handlers */
static struct wl_listener cursor_axis = {.notify = axisnotify}; static struct wl_listener cursor_axis = {.notify = axisnotify};
static struct wl_listener cursor_button = {.notify = buttonpress}; static struct wl_listener cursor_button = {.notify = buttonpress};
@@ -465,6 +480,8 @@ static struct wlr_xwayland *xwayland;
/* attempt to encapsulate suck into one file */ /* attempt to encapsulate suck into one file */
#include "client.h" #include "client.h"
static const unsigned int abzsquare = swipe_min_threshold * swipe_min_threshold;
/* function implementations */ /* function implementations */
void void
applybounds(Client *c, struct wlr_box *bbox) applybounds(Client *c, struct wlr_box *bbox)
@@ -686,6 +703,11 @@ swipe_begin(struct wl_listener *listener, void *data)
{ {
struct wlr_pointer_swipe_begin_event *event = data; struct wlr_pointer_swipe_begin_event *event = data;
swipe_fingers = event->fingers;
// Reset swipe distance at the beginning of a swipe
swipe_dx = 0;
swipe_dy = 0;
// Forward swipe begin event to client // Forward swipe begin event to client
wlr_pointer_gestures_v1_send_swipe_begin( wlr_pointer_gestures_v1_send_swipe_begin(
pointer_gestures, pointer_gestures,
@@ -700,6 +722,11 @@ swipe_update(struct wl_listener *listener, void *data)
{ {
struct wlr_pointer_swipe_update_event *event = data; struct wlr_pointer_swipe_update_event *event = data;
swipe_fingers = event->fingers;
// Accumulate swipe distance
swipe_dx += event->dx;
swipe_dy += event->dy;
// Forward swipe update event to client // Forward swipe update event to client
wlr_pointer_gestures_v1_send_swipe_update( wlr_pointer_gestures_v1_send_swipe_update(
pointer_gestures, pointer_gestures,
@@ -710,11 +737,53 @@ swipe_update(struct wl_listener *listener, void *data)
); );
} }
int
ongesture(struct wlr_pointer_swipe_end_event *event)
{
struct wlr_keyboard *keyboard;
uint32_t mods;
const Gesture *g;
unsigned int motion;
unsigned int adx = (int)round(fabs(swipe_dx));
unsigned int ady = (int)round(fabs(swipe_dy));
int handled = 0;
if (event->cancelled) {
return handled;
}
// Require absolute distance movement beyond a small thresh-hold
if (adx * adx + ady * ady < abzsquare) {
return handled;
}
if (adx > ady) {
motion = swipe_dx < 0 ? SWIPE_LEFT : SWIPE_RIGHT;
} else {
motion = swipe_dy < 0 ? SWIPE_UP : SWIPE_DOWN;
}
keyboard = wlr_seat_get_keyboard(seat);
mods = keyboard ? wlr_keyboard_get_modifiers(keyboard) : 0;
for (g = gestures; g < END(gestures); g++) {
if (CLEANMASK(mods) == CLEANMASK(g->mod) &&
swipe_fingers == g->fingers_count &&
motion == g->motion && g->func) {
g->func(&g->arg);
handled = 1;
}
}
return handled;
}
void void
swipe_end(struct wl_listener *listener, void *data) swipe_end(struct wl_listener *listener, void *data)
{ {
struct wlr_pointer_swipe_end_event *event = data; struct wlr_pointer_swipe_end_event *event = data;
// TODO: should we stop here if the event has been handled?
ongesture(event);
// Forward swipe end event to client // Forward swipe end event to client
wlr_pointer_gestures_v1_send_swipe_end( wlr_pointer_gestures_v1_send_swipe_end(
pointer_gestures, pointer_gestures,