Add geminux-touchd multi-touch gesture daemon and st wheel scrollback
This commit is contained in:
1 parent
9db15e30ce
commit
ba7b86e570
16 files changed
+877
-13
No files matched your search
@@ -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/
|
||||
|
||||
@@ -0,0 +1,350 @@
|
||||
#include "geminux-touch-gesture.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
#ifndef GEMINUX_TOUCH_GESTURE_H
|
||||
#define GEMINUX_TOUCH_GESTURE_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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
|
||||
@@ -0,0 +1,266 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include "geminux-touch-gesture.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <sys/signalfd.h>
|
||||
#include <sys/timerfd.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/extensions/XI2.h>
|
||||
#include <X11/extensions/XInput2.h>
|
||||
#include <X11/extensions/XTest.h>
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in new issue
Block a user