diff --git a/MANUAL.md b/MANUAL.md index 2dfe618..3a1dae3 100644 --- a/MANUAL.md +++ b/MANUAL.md @@ -32,6 +32,18 @@ In [src/dwm/config.h](file:///home/lux/proj/geminux/src/dwm/config.h), `MODKEY` Touchscreen input is supported directly across XLibre (`libinput`), DWM tag/layout/close buttons (`>= 68px` top bar), `dmenu` items (`68px` rows), `st`, and all nine `libgemui` applications (scaled for `180 DPI` / `>= 1.85x`). +### Touchscreen multi-touch gestures + +`geminux-touchd` intercepts raw XInput 2.2 touch sequences on the root window and provides universal X11 right-click, middle-click, and two-finger scroll without interfering with 1-finger tapping or dragging: + +| Gesture | X11 action | Notes | +| --- | --- | --- | +| 1-finger tap or drag | Left-click (`Button1`) / drag | Native passthrough for taps, text selection, sliders, and `Mod4 + drag` window move | +| 2-finger tap | Right-click (`Button3`) | Quick context menus without moving the cursor first | +| 1-finger stationary hold (`0.5 s`) | Right-click (`Button3`) | Alternative single-handed right-click | +| 3-finger tap | Middle-click (`Button2`) | Pastes X11 primary selection in `st` and opens tabs/links in apps | +| 2-finger drag | Scroll wheel (`Button4`–`Button7`) | Natural vertical and horizontal scroll across X11 apps and `st` scrollback (set `GEMINUX_NATURAL_SCROLL=0` for classic wheel direction) | + ## Unified theming via `.Xresources` Geminux includes 21 unified `.xth` color themes under `~/.xthemes/` (copied from `/etc/skel/.xthemes/`). Switch themes either interactively with `geminux-themes` or from the terminal: diff --git a/overlay/rootfs/usr/bin/geminux-session b/overlay/rootfs/usr/bin/geminux-session index 3cd2a45..1ce9548 100755 --- a/overlay/rootfs/usr/bin/geminux-session +++ b/overlay/rootfs/usr/bin/geminux-session @@ -75,6 +75,7 @@ start_bg pipewire-pulse start_bg wireplumber start_bg dunst start_bg geminux-statusd +start_bg geminux-touchd start_bg geminux-modem-ctl start_bg geminux-bt-obex diff --git a/prebuilt/geminux/usr/bin/geminux-touchd b/prebuilt/geminux/usr/bin/geminux-touchd new file mode 100755 index 0000000..e15c6b4 Binary files /dev/null and b/prebuilt/geminux/usr/bin/geminux-touchd differ diff --git a/prebuilt/suckless/usr/bin/st b/prebuilt/suckless/usr/bin/st index d6c9095..f5c4356 100755 Binary files a/prebuilt/suckless/usr/bin/st and b/prebuilt/suckless/usr/bin/st differ diff --git a/scripts/build-geminux-ui.sh b/scripts/build-geminux-ui.sh index c0fa004..dc80ad2 100755 --- a/scripts/build-geminux-ui.sh +++ b/scripts/build-geminux-ui.sh @@ -1,6 +1,6 @@ #!/bin/sh # Cross-compile Geminux UI SDK (libgemui.so), 9 landscape HiDPI apps, -# and 7 zero-poll C11 daemons (+ power-ctl & checkpw) for aarch64. +# and 8 zero-poll C11 daemons (+ power-ctl & checkpw) for aarch64. set -eu BASE_DIR="$(cd "$(dirname "$0")/.." && pwd)" @@ -51,6 +51,8 @@ EOF libxft-dev \ libxext-dev \ libxrandr-dev \ + libxi-dev \ + libxtst-dev \ cairo-dev \ pango-dev \ harfbuzz-dev \ diff --git a/scripts/rootfs-packages.txt b/scripts/rootfs-packages.txt index bbf6215..2d520cc 100644 --- a/scripts/rootfs-packages.txt +++ b/scripts/rootfs-packages.txt @@ -61,6 +61,7 @@ libva libva-utils libxcvt libxft +libxi libxinerama libxrandr libxscrnsaver diff --git a/scripts/test-power-policy.py b/scripts/test-power-policy.py index 41553e6..4040371 100755 --- a/scripts/test-power-policy.py +++ b/scripts/test-power-policy.py @@ -11,10 +11,12 @@ import tempfile BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DAEMON_DIR = os.path.join(BASE_DIR, "src", "geminux-daemons") POLICY_C = os.path.join(DAEMON_DIR, "geminux-power-policy.c") +GESTURE_C = os.path.join(DAEMON_DIR, "geminux-touch-gesture.c") DAEMON_FILES = { "geminux-powerd.c": "epoll_wait", "geminux-statusd.c": "epoll_wait", + "geminux-touchd.c": "epoll_wait", "geminux-ccci-daemon.c": "epoll_wait", "geminux-modem-ctl.c": "epoll_wait", "geminux-bt-obex.c": "g_main_loop_run", @@ -25,6 +27,104 @@ C_TEST = r""" #include #include #include "geminux-power-policy.h" +#include "geminux-touch-gesture.h" + +static int count_decision(const geminux_gesture_action_t *a, int mode) { + int c = 0; + for (int i = 0; i < a->decision_count; i++) { + if (a->decisions[i].allow_mode == mode) c++; + } + return c; +} + +static void test_touch_gestures(void) { + geminux_gesture_state_t gs; + geminux_gesture_action_t act; + + /* 1. 1-finger tap -> REJECT passthrough, no synthetic click */ + geminux_gesture_init(&gs, true); + geminux_gesture_on_begin(&gs, 2, 100, 500.0, 400.0, 0, &act); + assert(gs.phase == GESTURE_PENDING_1F); + assert(act.decision_count == 0); + assert(act.next_timer_ms == GEMINUX_LONGPRESS_MS); + geminux_gesture_on_end(&gs, 2, 100, 504.0, 403.0, 90, &act); + assert(act.decision_count == 1); + assert(act.decisions[0].devid == 2 && act.decisions[0].touchid == 100); + assert(act.decisions[0].allow_mode == GEMINUX_ALLOW_REJECT); + assert(act.click_button == 0); + assert(gs.phase == GESTURE_IDLE); + + /* 2. 1-finger drag -> held during 45ms grace, REJECTed on timer */ + geminux_gesture_init(&gs, true); + geminux_gesture_on_begin(&gs, 2, 101, 500.0, 400.0, 1000, &act); + geminux_gesture_on_update(&gs, 2, 101, 550.0, 400.0, 1020, &act); + assert(gs.phase == GESTURE_PENDING_1F); + assert(act.decision_count == 0); + assert(act.next_timer_ms == 25); + geminux_gesture_on_timer(&gs, 1045, &act); + assert(gs.phase == GESTURE_PASSTHROUGH_1F); + assert(count_decision(&act, GEMINUX_ALLOW_REJECT) == 1); + geminux_gesture_on_end(&gs, 2, 101, 600.0, 400.0, 1150, &act); + assert(act.click_button == 0); + assert(gs.phase == GESTURE_IDLE); + + /* 3. 1-finger 500ms stationary hold -> ACCEPT + Button3 right-click */ + geminux_gesture_init(&gs, true); + geminux_gesture_on_begin(&gs, 2, 102, 700.0, 300.0, 2000, &act); + geminux_gesture_on_timer(&gs, 2500, &act); + assert(gs.phase == GESTURE_ACCEPTED_1F_HOLD); + assert(count_decision(&act, GEMINUX_ALLOW_ACCEPT) == 1); + assert(act.click_button == 3); + geminux_gesture_on_end(&gs, 2, 102, 705.0, 302.0, 2600, &act); + assert(act.click_button == 0); + assert(gs.phase == GESTURE_IDLE); + + /* 4. 2-finger tap -> ACCEPT both + Button3 right-click on release */ + geminux_gesture_init(&gs, true); + geminux_gesture_on_begin(&gs, 2, 201, 600.0, 400.0, 3000, &act); + geminux_gesture_on_begin(&gs, 2, 202, 680.0, 410.0, 3015, &act); + assert(gs.phase == GESTURE_ACCEPTED_MULTI); + assert(count_decision(&act, GEMINUX_ALLOW_ACCEPT) == 2); + assert(act.next_timer_ms == 0); + geminux_gesture_on_end(&gs, 2, 201, 604.0, 402.0, 3110, &act); + assert(act.click_button == 0); + geminux_gesture_on_end(&gs, 2, 202, 684.0, 412.0, 3120, &act); + assert(act.click_button == 3); + assert(gs.phase == GESTURE_IDLE); + + /* 5. 3-finger tap -> ACCEPT all 3 + Button2 middle-click on release */ + geminux_gesture_init(&gs, true); + geminux_gesture_on_begin(&gs, 2, 301, 500.0, 400.0, 4000, &act); + geminux_gesture_on_begin(&gs, 2, 302, 600.0, 400.0, 4010, &act); + assert(count_decision(&act, GEMINUX_ALLOW_ACCEPT) == 2); + geminux_gesture_on_begin(&gs, 2, 303, 700.0, 400.0, 4025, &act); + assert(count_decision(&act, GEMINUX_ALLOW_ACCEPT) == 1); + geminux_gesture_on_end(&gs, 2, 301, 505.0, 402.0, 4120, &act); + geminux_gesture_on_end(&gs, 2, 302, 605.0, 402.0, 4135, &act); + assert(act.click_button == 0); + geminux_gesture_on_end(&gs, 2, 303, 705.0, 402.0, 4150, &act); + assert(act.click_button == 2); + assert(gs.phase == GESTURE_IDLE); + + /* 6. 2-finger drag -> natural vertical & horizontal scroll ticks, no tap click */ + int total_v = 0, total_h = 0; + geminux_gesture_init(&gs, true); + geminux_gesture_on_begin(&gs, 2, 401, 600.0, 300.0, 5000, &act); + geminux_gesture_on_begin(&gs, 2, 402, 680.0, 300.0, 5010, &act); + geminux_gesture_on_update(&gs, 2, 401, 730.0, 410.0, 5080, &act); + total_v += act.wheel_v; + total_h += act.wheel_h; + geminux_gesture_on_update(&gs, 2, 402, 810.0, 410.0, 5081, &act); + total_v += act.wheel_v; + total_h += act.wheel_h; + assert(gs.scrolled); + assert(total_v == 2); + assert(total_h == 2); + geminux_gesture_on_end(&gs, 2, 401, 730.0, 410.0, 5140, &act); + geminux_gesture_on_end(&gs, 2, 402, 810.0, 410.0, 5150, &act); + assert(act.click_button == 0); + assert(gs.phase == GESTURE_IDLE); +} int main(void) { geminux_power_policy_t pol; @@ -63,6 +163,8 @@ int main(void) { assert(strcmp(perf.governor, "schedutil") == 0); assert(perf.a72_online); assert(perf.suspend_timeout_s == 0); + + test_touch_gestures(); return 0; } """ @@ -89,7 +191,7 @@ def main(): if gcc: subprocess.run( [gcc, "-std=c11", "-Wall", "-Wextra", "-Werror", - "-I", DAEMON_DIR, src, POLICY_C, "-o", bin_path], + "-I", DAEMON_DIR, src, POLICY_C, GESTURE_C, "-lm", "-o", bin_path], check=True, ) subprocess.run([bin_path], check=True) @@ -100,11 +202,12 @@ def main(): "apk add --no-cache build-base >/dev/null && " "gcc -std=c11 -Wall -Wextra -Werror -I/geminux/src/geminux-daemons " "/work/test_power.c /geminux/src/geminux-daemons/geminux-power-policy.c " + "/geminux/src/geminux-daemons/geminux-touch-gesture.c -lm " "-o /work/test_power && /work/test_power"], check=True, ) - print("[PASS] Zero-poll daemon loops and power/lid policy verified") + print("[PASS] Zero-poll daemon loops, power/lid policy, and touch gestures verified") return 0 diff --git a/src/geminux-daemons/Makefile b/src/geminux-daemons/Makefile index 6080e0d..ed888c7 100644 --- a/src/geminux-daemons/Makefile +++ b/src/geminux-daemons/Makefile @@ -7,6 +7,8 @@ KERNEL_INC ?= ../../prebuilt/kernel/include CFLAGS += -std=c11 -Wall -Wextra -O2 -I. -I$(KERNEL_INC) X11_CFLAGS = $(shell $(PKG_CONFIG) --cflags x11) X11_LIBS = $(shell $(PKG_CONFIG) --libs x11) +XTOUCH_CFLAGS = $(shell $(PKG_CONFIG) --cflags x11 xi xtst) +XTOUCH_LIBS = $(shell $(PKG_CONFIG) --libs x11 xi xtst) -lm GIO_CFLAGS = $(shell $(PKG_CONFIG) --cflags gio-2.0 glib-2.0 gobject-2.0) GIO_LIBS = $(shell $(PKG_CONFIG) --libs gio-2.0 glib-2.0 gobject-2.0) @@ -14,6 +16,7 @@ BINS = \ geminux-powerd \ geminux-power-ctl \ geminux-statusd \ + geminux-touchd \ geminux-ccci-daemon \ geminux-modem-ctl \ geminux-bt-obex \ @@ -32,6 +35,9 @@ geminux-power-ctl: geminux-power-ctl.c geminux-statusd: geminux-statusd.c $(CC) $(CFLAGS) $(X11_CFLAGS) -o $@ $< $(X11_LIBS) +geminux-touchd: geminux-touchd.c geminux-touch-gesture.c geminux-touch-gesture.h + $(CC) $(CFLAGS) $(XTOUCH_CFLAGS) -o $@ geminux-touchd.c geminux-touch-gesture.c $(XTOUCH_LIBS) + geminux-ccci-daemon: geminux-ccci-daemon.c geminux-ccci-proto.c geminux-ccci-proto.h $(CC) $(CFLAGS) -o $@ geminux-ccci-daemon.c geminux-ccci-proto.c @@ -52,7 +58,7 @@ geminux-checkpw: geminux-checkpw.c install: all mkdir -p $(DESTDIR)$(PREFIX)/bin $(DESTDIR)$(PREFIX)/sbin - cp -f geminux-power-ctl geminux-statusd geminux-modem-ctl \ + cp -f geminux-power-ctl geminux-statusd geminux-touchd geminux-modem-ctl \ geminux-bt-obex geminux-power-audit $(DESTDIR)$(PREFIX)/bin/ cp -f geminux-powerd geminux-ccci-daemon geminux-leds \ geminux-checkpw $(DESTDIR)$(PREFIX)/sbin/ diff --git a/src/geminux-daemons/geminux-touch-gesture.c b/src/geminux-daemons/geminux-touch-gesture.c new file mode 100644 index 0000000..baa9aeb --- /dev/null +++ b/src/geminux-daemons/geminux-touch-gesture.c @@ -0,0 +1,350 @@ +#include "geminux-touch-gesture.h" + +#include +#include + +static void action_reset(geminux_gesture_action_t *out) { + memset(out, 0, sizeof(*out)); + out->next_timer_ms = -1; +} + +static void decide_slot(geminux_touch_slot_t *slot, int mode, + geminux_gesture_action_t *out) { + if (!slot->active || slot->decided || mode == GEMINUX_ALLOW_NONE) return; + if (out->decision_count < GEMINUX_MAX_TOUCHES) { + out->decisions[out->decision_count].devid = slot->devid; + out->decisions[out->decision_count].touchid = slot->touchid; + out->decisions[out->decision_count].allow_mode = mode; + out->decision_count++; + } + slot->decided = true; +} + +static void decide_all_active(geminux_gesture_state_t *st, int mode, + geminux_gesture_action_t *out) { + for (int i = 0; i < GEMINUX_MAX_TOUCHES; i++) { + if (st->slots[i].active) { + decide_slot(&st->slots[i], mode, out); + } + } +} + +static geminux_touch_slot_t *find_slot(geminux_gesture_state_t *st, int devid, + unsigned int touchid) { + for (int i = 0; i < GEMINUX_MAX_TOUCHES; i++) { + if (st->slots[i].active && + st->slots[i].devid == devid && + st->slots[i].touchid == touchid) { + return &st->slots[i]; + } + } + return NULL; +} + +static geminux_touch_slot_t *alloc_slot(geminux_gesture_state_t *st, int devid, + unsigned int touchid) { + geminux_touch_slot_t *existing = find_slot(st, devid, touchid); + if (existing) return existing; + for (int i = 0; i < GEMINUX_MAX_TOUCHES; i++) { + if (!st->slots[i].active) return &st->slots[i]; + } + return NULL; +} + +static int recount_active(const geminux_gesture_state_t *st) { + int cnt = 0; + for (int i = 0; i < GEMINUX_MAX_TOUCHES; i++) { + if (st->slots[i].active) cnt++; + } + return cnt; +} + +static bool compute_centroid(const geminux_gesture_state_t *st, + double *cx_out, double *cy_out) { + double sx = 0.0, sy = 0.0; + int cnt = 0; + for (int i = 0; i < GEMINUX_MAX_TOUCHES; i++) { + if (st->slots[i].active) { + sx += st->slots[i].x; + sy += st->slots[i].y; + cnt++; + } + } + if (cnt <= 0) return false; + *cx_out = sx / (double)cnt; + *cy_out = sy / (double)cnt; + return true; +} + +static int compute_timer_pending_1f(const geminux_gesture_state_t *st, + const geminux_touch_slot_t *slot, + uint64_t now_ms) { + uint64_t elapsed = (now_ms >= st->seq_start_ms) ? (now_ms - st->seq_start_ms) : 0; + double dist = hypot(slot->x - slot->x0, slot->y - slot->y0); + if (dist > GEMINUX_MOVE_THRESH_PX) { + if (elapsed < GEMINUX_MULTITOUCH_GRACE_MS) { + int rem = (int)(GEMINUX_MULTITOUCH_GRACE_MS - elapsed); + return rem > 0 ? rem : 1; + } + return 1; + } + if (elapsed < GEMINUX_LONGPRESS_MS) { + int rem = (int)(GEMINUX_LONGPRESS_MS - elapsed); + return rem > 0 ? rem : 1; + } + return 1; +} + +void geminux_gesture_init(geminux_gesture_state_t *st, bool natural_scroll) { + memset(st, 0, sizeof(*st)); + st->phase = GESTURE_IDLE; + st->natural_scroll = natural_scroll; +} + +void geminux_gesture_on_begin(geminux_gesture_state_t *st, int devid, + unsigned int touchid, double x, double y, + uint64_t now_ms, geminux_gesture_action_t *out) { + action_reset(out); + + geminux_touch_slot_t *slot = alloc_slot(st, devid, touchid); + if (!slot) return; + + slot->active = true; + slot->decided = false; + slot->devid = devid; + slot->touchid = touchid; + slot->x0 = x; + slot->y0 = y; + slot->x = x; + slot->y = y; + + st->active_count = recount_active(st); + + if (st->phase == GESTURE_IDLE) { + st->phase = GESTURE_PENDING_1F; + st->seq_start_ms = now_ms; + st->max_fingers = 1; + st->scrolled = false; + st->centroid_valid = false; + st->max_disp = 0.0; + st->anchor_x = x; + st->anchor_y = y; + st->accum_dx = 0.0; + st->accum_dy = 0.0; + out->next_timer_ms = GEMINUX_LONGPRESS_MS; + } else if (st->phase == GESTURE_PASSTHROUGH_1F) { + decide_slot(slot, GEMINUX_ALLOW_REJECT, out); + out->next_timer_ms = 0; + } else if (st->phase == GESTURE_ACCEPTED_1F_HOLD) { + decide_slot(slot, GEMINUX_ALLOW_ACCEPT, out); + out->next_timer_ms = 0; + } else { + st->phase = GESTURE_ACCEPTED_MULTI; + decide_all_active(st, GEMINUX_ALLOW_ACCEPT, out); + + double cx = x, cy = y; + compute_centroid(st, &cx, &cy); + + if (st->active_count > st->max_fingers) { + st->max_fingers = st->active_count; + if (!st->scrolled) { + st->anchor_x = cx; + st->anchor_y = cy; + } + } + + if (st->active_count == 2 && st->max_fingers == 2) { + st->centroid_valid = true; + st->start_cx = cx; + st->start_cy = cy; + st->last_cx = cx; + st->last_cy = cy; + if (!st->scrolled) { + st->anchor_x = cx; + st->anchor_y = cy; + } + } else { + st->centroid_valid = false; + } + + out->next_timer_ms = 0; + } +} + +void geminux_gesture_on_update(geminux_gesture_state_t *st, int devid, + unsigned int touchid, double x, double y, + uint64_t now_ms, geminux_gesture_action_t *out) { + action_reset(out); + + geminux_touch_slot_t *slot = find_slot(st, devid, touchid); + if (!slot) { + if (st->phase == GESTURE_PENDING_1F) { + for (int i = 0; i < GEMINUX_MAX_TOUCHES; i++) { + if (st->slots[i].active) { + out->next_timer_ms = compute_timer_pending_1f(st, &st->slots[i], now_ms); + break; + } + } + } + return; + } + + slot->x = x; + slot->y = y; + + double dist = hypot(x - slot->x0, y - slot->y0); + if (dist > st->max_disp) st->max_disp = dist; + + if (st->phase == GESTURE_PENDING_1F) { + uint64_t elapsed = (now_ms >= st->seq_start_ms) ? (now_ms - st->seq_start_ms) : 0; + if (dist > GEMINUX_MOVE_THRESH_PX && elapsed >= GEMINUX_MULTITOUCH_GRACE_MS) { + st->phase = GESTURE_PASSTHROUGH_1F; + decide_all_active(st, GEMINUX_ALLOW_REJECT, out); + out->next_timer_ms = 0; + } else if (dist <= GEMINUX_MOVE_THRESH_PX && elapsed >= GEMINUX_LONGPRESS_MS) { + st->phase = GESTURE_ACCEPTED_1F_HOLD; + decide_all_active(st, GEMINUX_ALLOW_ACCEPT, out); + out->click_button = 3; + out->pointer_x = slot->x0; + out->pointer_y = slot->y0; + out->next_timer_ms = 0; + } else { + out->next_timer_ms = compute_timer_pending_1f(st, slot, now_ms); + } + } else if (st->phase == GESTURE_ACCEPTED_MULTI) { + if (st->active_count == 2 && st->max_fingers == 2) { + double cx = 0.0, cy = 0.0; + if (!compute_centroid(st, &cx, &cy)) return; + + if (!st->centroid_valid) { + st->centroid_valid = true; + st->start_cx = cx; + st->start_cy = cy; + st->last_cx = cx; + st->last_cy = cy; + } + + if (!st->scrolled) { + if (hypot(cx - st->start_cx, cy - st->start_cy) >= GEMINUX_SCROLL_DEADZONE_PX) { + st->scrolled = true; + } + } + + if (st->scrolled) { + st->accum_dx += (cx - st->last_cx); + st->accum_dy += (cy - st->last_cy); + st->last_cx = cx; + st->last_cy = cy; + + int sign = st->natural_scroll ? 1 : -1; + while (st->accum_dy >= GEMINUX_SCROLL_STEP_Y_PX) { + st->accum_dy -= GEMINUX_SCROLL_STEP_Y_PX; + out->wheel_v += sign; + } + while (st->accum_dy <= -GEMINUX_SCROLL_STEP_Y_PX) { + st->accum_dy += GEMINUX_SCROLL_STEP_Y_PX; + out->wheel_v -= sign; + } + while (st->accum_dx >= GEMINUX_SCROLL_STEP_X_PX) { + st->accum_dx -= GEMINUX_SCROLL_STEP_X_PX; + out->wheel_h += sign; + } + while (st->accum_dx <= -GEMINUX_SCROLL_STEP_X_PX) { + st->accum_dx += GEMINUX_SCROLL_STEP_X_PX; + out->wheel_h -= sign; + } + if (out->wheel_v != 0 || out->wheel_h != 0) { + out->pointer_x = st->anchor_x; + out->pointer_y = st->anchor_y; + } + } + } + } +} + +void geminux_gesture_on_end(geminux_gesture_state_t *st, int devid, + unsigned int touchid, double x, double y, + uint64_t now_ms, geminux_gesture_action_t *out) { + action_reset(out); + + geminux_touch_slot_t *slot = find_slot(st, devid, touchid); + if (!slot) return; + + slot->x = x; + slot->y = y; + + double dist = hypot(x - slot->x0, y - slot->y0); + if (dist > st->max_disp) st->max_disp = dist; + + if (st->phase == GESTURE_PENDING_1F || st->phase == GESTURE_PASSTHROUGH_1F) { + decide_slot(slot, GEMINUX_ALLOW_REJECT, out); + } else { + decide_slot(slot, GEMINUX_ALLOW_ACCEPT, out); + } + + slot->active = false; + st->active_count = recount_active(st); + st->centroid_valid = false; + + if (st->active_count == 0) { + uint64_t elapsed = (now_ms >= st->seq_start_ms) ? (now_ms - st->seq_start_ms) : 0; + if (st->phase == GESTURE_ACCEPTED_MULTI && !st->scrolled) { + if (st->max_fingers == 2 && + elapsed <= GEMINUX_TAP_2F_MS && + st->max_disp <= GEMINUX_TAP_2F_THRESH_PX) { + out->click_button = 3; + out->pointer_x = st->anchor_x; + out->pointer_y = st->anchor_y; + } else if (st->max_fingers == 3 && + elapsed <= GEMINUX_TAP_3F_MS && + st->max_disp <= GEMINUX_TAP_3F_THRESH_PX) { + out->click_button = 2; + out->pointer_x = st->anchor_x; + out->pointer_y = st->anchor_y; + } + } + st->phase = GESTURE_IDLE; + out->next_timer_ms = 0; + } +} + +void geminux_gesture_on_timer(geminux_gesture_state_t *st, uint64_t now_ms, + geminux_gesture_action_t *out) { + action_reset(out); + + if (st->phase != GESTURE_PENDING_1F) { + out->next_timer_ms = 0; + return; + } + + geminux_touch_slot_t *slot = NULL; + for (int i = 0; i < GEMINUX_MAX_TOUCHES; i++) { + if (st->slots[i].active) { + slot = &st->slots[i]; + break; + } + } + if (!slot) { + st->phase = GESTURE_IDLE; + out->next_timer_ms = 0; + return; + } + + uint64_t elapsed = (now_ms >= st->seq_start_ms) ? (now_ms - st->seq_start_ms) : 0; + double dist = hypot(slot->x - slot->x0, slot->y - slot->y0); + + if (dist > GEMINUX_MOVE_THRESH_PX && elapsed >= GEMINUX_MULTITOUCH_GRACE_MS) { + st->phase = GESTURE_PASSTHROUGH_1F; + decide_all_active(st, GEMINUX_ALLOW_REJECT, out); + out->next_timer_ms = 0; + } else if (elapsed >= GEMINUX_LONGPRESS_MS) { + st->phase = GESTURE_ACCEPTED_1F_HOLD; + decide_all_active(st, GEMINUX_ALLOW_ACCEPT, out); + out->click_button = 3; + out->pointer_x = slot->x0; + out->pointer_y = slot->y0; + out->next_timer_ms = 0; + } else { + out->next_timer_ms = compute_timer_pending_1f(st, slot, now_ms); + } +} diff --git a/src/geminux-daemons/geminux-touch-gesture.h b/src/geminux-daemons/geminux-touch-gesture.h new file mode 100644 index 0000000..081acb6 --- /dev/null +++ b/src/geminux-daemons/geminux-touch-gesture.h @@ -0,0 +1,96 @@ +#ifndef GEMINUX_TOUCH_GESTURE_H +#define GEMINUX_TOUCH_GESTURE_H + +#include +#include + +#define GEMINUX_MAX_TOUCHES 10 + +#define GEMINUX_MULTITOUCH_GRACE_MS 45 +#define GEMINUX_LONGPRESS_MS 500 +#define GEMINUX_TAP_2F_MS 320 +#define GEMINUX_TAP_3F_MS 360 + +#define GEMINUX_MOVE_THRESH_PX 32.0 +#define GEMINUX_TAP_2F_THRESH_PX 44.0 +#define GEMINUX_TAP_3F_THRESH_PX 56.0 +#define GEMINUX_SCROLL_DEADZONE_PX 18.0 +#define GEMINUX_SCROLL_STEP_Y_PX 44.0 +#define GEMINUX_SCROLL_STEP_X_PX 56.0 + +enum { + GEMINUX_ALLOW_NONE = 0, + GEMINUX_ALLOW_REJECT = 1, + GEMINUX_ALLOW_ACCEPT = 2 +}; + +typedef enum { + GESTURE_IDLE = 0, + GESTURE_PENDING_1F, + GESTURE_PASSTHROUGH_1F, + GESTURE_ACCEPTED_1F_HOLD, + GESTURE_ACCEPTED_MULTI +} geminux_gesture_phase_t; + +typedef struct { + bool active; + bool decided; + int devid; + unsigned int touchid; + double x0; + double y0; + double x; + double y; +} geminux_touch_slot_t; + +typedef struct { + int devid; + unsigned int touchid; + int allow_mode; +} geminux_touch_decision_t; + +typedef struct { + geminux_touch_decision_t decisions[GEMINUX_MAX_TOUCHES]; + int decision_count; + int click_button; + int wheel_v; + int wheel_h; + double pointer_x; + double pointer_y; + int next_timer_ms; +} geminux_gesture_action_t; + +typedef struct { + geminux_gesture_phase_t phase; + bool natural_scroll; + geminux_touch_slot_t slots[GEMINUX_MAX_TOUCHES]; + int active_count; + int max_fingers; + uint64_t seq_start_ms; + bool scrolled; + bool centroid_valid; + double max_disp; + double anchor_x; + double anchor_y; + double start_cx; + double start_cy; + double last_cx; + double last_cy; + double accum_dx; + double accum_dy; +} geminux_gesture_state_t; + +void geminux_gesture_init(geminux_gesture_state_t *st, bool natural_scroll); +void geminux_gesture_on_begin(geminux_gesture_state_t *st, int devid, + unsigned int touchid, double x, double y, + uint64_t now_ms, geminux_gesture_action_t *out); +void geminux_gesture_on_update(geminux_gesture_state_t *st, int devid, + unsigned int touchid, double x, double y, + uint64_t now_ms, geminux_gesture_action_t *out); +void geminux_gesture_on_end(geminux_gesture_state_t *st, int devid, + unsigned int touchid, double x, double y, + uint64_t now_ms, geminux_gesture_action_t *out); +void geminux_gesture_on_timer(geminux_gesture_state_t *st, uint64_t now_ms, + geminux_gesture_action_t *out); + +#endif diff --git a/src/geminux-daemons/geminux-touchd.c b/src/geminux-daemons/geminux-touchd.c new file mode 100644 index 0000000..28a1a43 --- /dev/null +++ b/src/geminux-daemons/geminux-touchd.c @@ -0,0 +1,266 @@ +#define _POSIX_C_SOURCE 200809L +#include "geminux-touch-gesture.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static uint64_t now_monotonic_ms(void) { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (uint64_t)ts.tv_sec * 1000ULL + (uint64_t)(ts.tv_nsec / 1000000L); +} + +static int x_err_handler(Display *dpy, XErrorEvent *ev) { + (void)dpy; + (void)ev; + return 0; +} + +static void arm_timer(int tfd, int ms) { + if (tfd < 0) return; + struct itimerspec its; + memset(&its, 0, sizeof(its)); + if (ms > 0) { + its.it_value.tv_sec = ms / 1000; + its.it_value.tv_nsec = (long)(ms % 1000) * 1000000L; + } + timerfd_settime(tfd, 0, &its, NULL); +} + +static void warp_pointer(Display *dpy, int screen, double x, double y) { + int ix = (int)(x + 0.5); + int iy = (int)(y + 0.5); + if (ix < 0) ix = 0; + if (iy < 0) iy = 0; + XTestFakeMotionEvent(dpy, screen, ix, iy, CurrentTime); +} + +static void emit_button_ticks(Display *dpy, unsigned int button, int count) { + for (int i = 0; i < count; i++) { + XTestFakeButtonEvent(dpy, button, True, CurrentTime); + XTestFakeButtonEvent(dpy, button, False, CurrentTime); + } +} + +static void apply_action(Display *dpy, Window root, int screen, int tfd, + const geminux_gesture_action_t *act) { + bool need_flush = false; + + for (int i = 0; i < act->decision_count; i++) { + int mode = (act->decisions[i].allow_mode == GEMINUX_ALLOW_ACCEPT) + ? XIAcceptTouch + : XIRejectTouch; + XIAllowTouchEvents(dpy, act->decisions[i].devid, + act->decisions[i].touchid, root, mode); + need_flush = true; + } + + if (act->click_button > 0) { + warp_pointer(dpy, screen, act->pointer_x, act->pointer_y); + emit_button_ticks(dpy, (unsigned int)act->click_button, 1); + need_flush = true; + } + + if (act->wheel_v != 0 || act->wheel_h != 0) { + warp_pointer(dpy, screen, act->pointer_x, act->pointer_y); + if (act->wheel_v > 0) { + emit_button_ticks(dpy, 4U, act->wheel_v); + } else if (act->wheel_v < 0) { + emit_button_ticks(dpy, 5U, -act->wheel_v); + } + if (act->wheel_h > 0) { + emit_button_ticks(dpy, 6U, act->wheel_h); + } else if (act->wheel_h < 0) { + emit_button_ticks(dpy, 7U, -act->wheel_h); + } + need_flush = true; + } + + if (need_flush) { + XFlush(dpy); + } + + if (act->next_timer_ms >= 0) { + arm_timer(tfd, act->next_timer_ms); + } +} + +static void drain_x_events(Display *dpy, int xi_opcode, Window root, int screen, + int tfd, geminux_gesture_state_t *gst) { + while (XPending(dpy) > 0) { + XEvent ev; + XNextEvent(dpy, &ev); + if (ev.type != GenericEvent || ev.xcookie.extension != xi_opcode) { + continue; + } + if (!XGetEventData(dpy, &ev.xcookie)) { + continue; + } + + int evtype = ev.xcookie.evtype; + if (evtype == XI_TouchBegin || evtype == XI_TouchUpdate || evtype == XI_TouchEnd) { + XIDeviceEvent *de = (XIDeviceEvent *)ev.xcookie.data; + uint64_t now_ms = now_monotonic_ms(); + geminux_gesture_action_t act; + if (evtype == XI_TouchBegin) { + geminux_gesture_on_begin(gst, de->deviceid, (unsigned int)de->detail, + de->root_x, de->root_y, now_ms, &act); + } else if (evtype == XI_TouchUpdate) { + geminux_gesture_on_update(gst, de->deviceid, (unsigned int)de->detail, + de->root_x, de->root_y, now_ms, &act); + } else { + geminux_gesture_on_end(gst, de->deviceid, (unsigned int)de->detail, + de->root_x, de->root_y, now_ms, &act); + } + apply_action(dpy, root, screen, tfd, &act); + } + + XFreeEventData(dpy, &ev.xcookie); + } +} + +int main(void) { + const char *env_nat = getenv("GEMINUX_NATURAL_SCROLL"); + bool natural_scroll = !(env_nat && strcmp(env_nat, "0") == 0); + + Display *dpy = XOpenDisplay(NULL); + if (!dpy) { + fprintf(stderr, "geminux-touchd: cannot open X11 display\n"); + return 1; + } + + XSetErrorHandler(x_err_handler); + + int xi_opcode = 0, xi_ev = 0, xi_err = 0; + if (!XQueryExtension(dpy, "XInputExtension", &xi_opcode, &xi_ev, &xi_err)) { + fprintf(stderr, "geminux-touchd: XInputExtension unavailable\n"); + XCloseDisplay(dpy); + return 1; + } + + int xi_maj = 2, xi_min = 2; + if (XIQueryVersion(dpy, &xi_maj, &xi_min) != Success || + xi_maj < 2 || (xi_maj == 2 && xi_min < 2)) { + fprintf(stderr, "geminux-touchd: XInput 2.2+ required (got %d.%d)\n", + xi_maj, xi_min); + XCloseDisplay(dpy); + return 1; + } + + int xt_ev = 0, xt_err = 0, xt_maj = 0, xt_min = 0; + if (!XTestQueryExtension(dpy, &xt_ev, &xt_err, &xt_maj, &xt_min)) { + fprintf(stderr, "geminux-touchd: XTEST extension unavailable\n"); + XCloseDisplay(dpy); + return 1; + } + + Window root = DefaultRootWindow(dpy); + int screen = DefaultScreen(dpy); + + unsigned char mask[XIMaskLen(XI_LASTEVENT)]; + memset(mask, 0, sizeof(mask)); + XISetMask(mask, XI_TouchBegin); + XISetMask(mask, XI_TouchUpdate); + XISetMask(mask, XI_TouchEnd); + + XIEventMask evmask; + memset(&evmask, 0, sizeof(evmask)); + evmask.deviceid = XIAllMasterDevices; + evmask.mask_len = (int)sizeof(mask); + evmask.mask = mask; + + XIGrabModifiers mods; + memset(&mods, 0, sizeof(mods)); + mods.modifiers = XIAnyModifier; + + int nfailed = XIGrabTouchBegin(dpy, XIAllMasterDevices, root, + XINoOwnerEvents, &evmask, 1, &mods); + XSync(dpy, False); + if (nfailed != 0) { + fprintf(stderr, "geminux-touchd: XIGrabTouchBegin failed (%d)\n", nfailed); + XCloseDisplay(dpy); + return 1; + } + + sigset_t sigmask; + sigemptyset(&sigmask); + sigaddset(&sigmask, SIGINT); + sigaddset(&sigmask, SIGTERM); + sigprocmask(SIG_BLOCK, &sigmask, NULL); + int sfd = signalfd(-1, &sigmask, SFD_NONBLOCK | SFD_CLOEXEC); + + int tfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); + int xfd = ConnectionNumber(dpy); + int epfd = epoll_create1(EPOLL_CLOEXEC); + + if (sfd < 0 || tfd < 0 || xfd < 0 || epfd < 0) { + fprintf(stderr, "geminux-touchd: failed to initialize epoll fds\n"); + if (epfd >= 0) close(epfd); + if (tfd >= 0) close(tfd); + if (sfd >= 0) close(sfd); + XIUngrabTouchBegin(dpy, XIAllMasterDevices, root, 1, &mods); + XCloseDisplay(dpy); + return 1; + } + + int fds[3] = { xfd, tfd, sfd }; + for (int i = 0; i < 3; i++) { + struct epoll_event ev; + memset(&ev, 0, sizeof(ev)); + ev.events = EPOLLIN; + ev.data.fd = fds[i]; + epoll_ctl(epfd, EPOLL_CTL_ADD, fds[i], &ev); + } + + geminux_gesture_state_t gst; + geminux_gesture_init(&gst, natural_scroll); + + bool running = true; + struct epoll_event events[8]; + while (running) { + drain_x_events(dpy, xi_opcode, root, screen, tfd, &gst); + + int n = epoll_wait(epfd, events, 8, -1); + if (n < 0) { + if (errno == EINTR) continue; + break; + } + + for (int i = 0; i < n; i++) { + int fd = events[i].data.fd; + if (fd == sfd) { + struct signalfd_siginfo si; + while (read(sfd, &si, sizeof(si)) > 0) {} + running = false; + } else if (fd == tfd) { + uint64_t expirations = 0; + while (read(tfd, &expirations, sizeof(expirations)) > 0) {} + geminux_gesture_action_t act; + geminux_gesture_on_timer(&gst, now_monotonic_ms(), &act); + apply_action(dpy, root, screen, tfd, &act); + } + } + } + + close(epfd); + close(tfd); + close(sfd); + XIUngrabTouchBegin(dpy, XIAllMasterDevices, root, 1, &mods); + XSync(dpy, False); + XCloseDisplay(dpy); + return 0; +} diff --git a/src/st/config.def.h b/src/st/config.def.h index cf5e106..8423d24 100644 --- a/src/st/config.def.h +++ b/src/st/config.def.h @@ -211,10 +211,10 @@ ResourcePref resources[] = { static MouseShortcut mshortcuts[] = { /* mask button function argument release */ { XK_ANY_MOD, Button2, selpaste, {.i = 0}, 1 }, - { ShiftMask, Button4, ttysend, {.s = "\033[5;2~"} }, - { XK_ANY_MOD, Button4, ttysend, {.s = "\031"} }, - { ShiftMask, Button5, ttysend, {.s = "\033[6;2~"} }, - { XK_ANY_MOD, Button5, ttysend, {.s = "\005"} }, + { ShiftMask, Button4, kscrollup, {.i = 3} }, + { XK_ANY_MOD, Button4, wheelscrollup, {.i = 3} }, + { ShiftMask, Button5, kscrolldown, {.i = 3} }, + { XK_ANY_MOD, Button5, wheelscrolldown, {.i = 3} }, }; /* Internal keyboard shortcuts. */ diff --git a/src/st/config.h b/src/st/config.h index cf5e106..8423d24 100644 --- a/src/st/config.h +++ b/src/st/config.h @@ -211,10 +211,10 @@ ResourcePref resources[] = { static MouseShortcut mshortcuts[] = { /* mask button function argument release */ { XK_ANY_MOD, Button2, selpaste, {.i = 0}, 1 }, - { ShiftMask, Button4, ttysend, {.s = "\033[5;2~"} }, - { XK_ANY_MOD, Button4, ttysend, {.s = "\031"} }, - { ShiftMask, Button5, ttysend, {.s = "\033[6;2~"} }, - { XK_ANY_MOD, Button5, ttysend, {.s = "\005"} }, + { ShiftMask, Button4, kscrollup, {.i = 3} }, + { XK_ANY_MOD, Button4, wheelscrollup, {.i = 3} }, + { ShiftMask, Button5, kscrolldown, {.i = 3} }, + { XK_ANY_MOD, Button5, wheelscrolldown, {.i = 3} }, }; /* Internal keyboard shortcuts. */ diff --git a/src/st/st.c b/src/st/st.c index 38e41d3..0f1a739 100644 --- a/src/st/st.c +++ b/src/st/st.c @@ -1115,6 +1115,12 @@ kscrollup(const Arg* a) } } +int +tisaltscr(void) +{ + return IS_SET(MODE_ALTSCREEN); +} + void tscrolldown(int orig, int n, int copyhist) { diff --git a/src/st/st.h b/src/st/st.h index 5489ac5..cd5d997 100644 --- a/src/st/st.h +++ b/src/st/st.h @@ -83,6 +83,7 @@ void draw(void); void kscrolldown(const Arg *); void kscrollup(const Arg *); +int tisaltscr(void); void printscreen(const Arg *); void printsel(const Arg *); void sendbreak(const Arg *); diff --git a/src/st/x.c b/src/st/x.c index 2b0845f..2f14e88 100644 --- a/src/st/x.c +++ b/src/st/x.c @@ -74,6 +74,8 @@ static void zoom(const Arg *); static void zoomabs(const Arg *); static void zoomreset(const Arg *); static void ttysend(const Arg *); +static void wheelscrollup(const Arg *); +static void wheelscrolldown(const Arg *); static void invert(const Arg *); static void toggleselectionmode(const Arg *); @@ -272,7 +274,7 @@ static char *opt_title = NULL; static int invertcolors = 0; static int oldbutton = 3; /* button event on startup: 3 = release */ -static int selectionmodeenabled = 0; +static int selectionmodeenabled = 1; void invert(const Arg *dummy) @@ -375,6 +377,24 @@ ttysend(const Arg *arg) ttywrite(arg->s, strlen(arg->s), 1); } +void +wheelscrollup(const Arg *a) +{ + if (tisaltscr()) + ttysend(&(Arg){ .s = "\031" }); + else + kscrollup(a); +} + +void +wheelscrolldown(const Arg *a) +{ + if (tisaltscr()) + ttysend(&(Arg){ .s = "\005" }); + else + kscrolldown(a); +} + int evcol(XEvent *e) {