/* Device-independent touchpad contact gestures. See touchpad_gesture.h. */ #include "touchpad_gesture.h" #define TPG_TAP_MIN_MS 40 #define TPG_TAP_MAX_MS 300 #define TPG_CLICK_HOLD_MS 120 #define TPG_POINTER_MAX 64 static int abs_int(int v) { return v < 0 ? -v : v; } static void set_limits(struct tpg_state *s) { s->tap_move = s->max_x / 50; if (s->tap_move < 16) s->tap_move = 16; s->scroll_start = s->max_x / 100; if (s->scroll_start < 8) s->scroll_start = 8; } static int in_corner(const struct tpg_state *s, int x, int y) { return x >= s->max_x * TPG_CORNER_X_PCT / 100 && y <= s->max_y * TPG_CORNER_Y_PCT / 100; } void tpg_init(struct tpg_state *s, int max_x, int max_y, int units_per_px) { s->max_x = max_x > 0 ? max_x : 4095; s->max_y = max_y > 0 ? max_y : 4095; s->units_per_px = units_per_px > 0 ? units_per_px : 1; s->opts = TPG_DEFAULT_OPTIONS; set_limits(s); s->btn_raw = 0; s->btn_latched = 0; s->synth_btn = 0; s->synth_release_ms = 0; s->taps_left = 0; s->taps_right = 0; s->active = 0; s->start_ms = 0; s->start_x = s->start_y = 0; s->started = 0; s->moved = 0; s->two = 0; s->physical = 0; s->scrolled = 0; s->last_count = 0; s->last_x = s->last_y = 0; s->have_last = 0; s->acc_x = s->acc_y = 0; s->scroll_have = 0; s->scroll_last_y = 0; s->scroll_total = 0; s->scroll_acc = 0; } void tpg_set_limits(struct tpg_state *s, int max_x, int max_y) { s->max_x = max_x > 0 ? max_x : 4095; s->max_y = max_y > 0 ? max_y : 4095; set_limits(s); } void tpg_set_options(struct tpg_state *s, unsigned int opts) { s->opts = opts; } unsigned int tpg_options(const struct tpg_state *s) { return s->opts; } void tpg_tick(struct tpg_state *s, unsigned int now_ms, struct tpg_result *r) { r->dx = r->dy = r->scroll = 0; if (s->synth_btn && (int)(now_ms - s->synth_release_ms) >= 0) s->synth_btn = 0; r->buttons = (unsigned int)s->btn_latched | s->synth_btn; } void tpg_frame(struct tpg_state *s, const struct tpg_frame *f, struct tpg_result *r) { unsigned int count = f->count; unsigned int mask = f->mask; unsigned int now = f->now_ms; int has0 = (mask & 1u) && count >= 1; int has1 = (mask & 2u) && count >= 2; int c0x = has0 ? f->contact[0].x : 0; int c0y = has0 ? f->contact[0].y : 0; r->dx = r->dy = r->scroll = 0; if (s->synth_btn && (int)(now - s->synth_release_ms) >= 0) s->synth_btn = 0; /* Latch the physical clickpad button at its leading edge. A press * whose finger is in the right-click zone reports the right button * for the whole press; everything else is the left button. */ if (f->button) { if (!s->btn_raw) { s->btn_latched = 0x01; if ((s->opts & TPG_RIGHT_CORNER) && has0 && in_corner(s, c0x, c0y)) s->btn_latched = 0x02; } s->physical = 1; } else { s->btn_latched = 0; } s->btn_raw = f->button != 0; if (count) { if (!s->active) { s->active = 1; s->start_ms = now; s->started = 0; s->moved = 0; s->two = 0; s->physical = f->button != 0; s->scrolled = 0; s->scroll_have = 0; s->scroll_total = 0; s->scroll_acc = 0; s->have_last = 0; s->acc_x = s->acc_y = 0; } if (count >= 2) s->two = 1; /* The active finger packed into contact[0] can change when a * finger lifts. Re-anchor the tap movement check so a * two-finger tap followed by an early first-finger lift is * not mistaken for a drag. */ if (s->last_count && count != s->last_count) s->started = 0; if (has0) { if (!s->started) { s->started = 1; s->start_x = c0x; s->start_y = c0y; } else if (abs_int(c0x - s->start_x) > s->tap_move || abs_int(c0y - s->start_y) > s->tap_move) { s->moved = 1; } } } /* Pointer motion only with exactly one contact; two fingers are * either a tap or a scroll, so the pointer must not drift. */ if (count == 1 && has0) { if (!s->have_last) { s->last_x = c0x; s->last_y = c0y; s->have_last = 1; } else { int dx, dy; s->acc_x += c0x - s->last_x; s->acc_y += c0y - s->last_y; s->last_x = c0x; s->last_y = c0y; dx = s->acc_x / s->units_per_px; s->acc_x -= dx * s->units_per_px; dy = s->acc_y / s->units_per_px; s->acc_y -= dy * s->units_per_px; if (dx > TPG_POINTER_MAX) dx = TPG_POINTER_MAX; if (dx < -TPG_POINTER_MAX) dx = -TPG_POINTER_MAX; if (dy > TPG_POINTER_MAX) dy = TPG_POINTER_MAX; if (dy < -TPG_POINTER_MAX) dy = -TPG_POINTER_MAX; r->dx = dx; r->dy = dy; } } else { s->have_last = 0; s->acc_x = s->acc_y = 0; } /* Two-finger scroll. The centroid movement is accumulated until it * clears the start threshold, which keeps a stationary two-finger tap * from scrolling. */ if (count == 2 && has0 && has1) { int cy = (f->contact[0].y + f->contact[1].y) / 2; if (!s->scroll_have) { s->scroll_have = 1; s->scroll_last_y = cy; s->scroll_total = 0; } else { int d = cy - s->scroll_last_y; int out; s->scroll_last_y = cy; if (!s->scrolled) { s->scroll_total += d; if (abs_int(s->scroll_total) >= s->scroll_start) { s->scrolled = 1; s->scroll_acc += s->scroll_total; } } else { s->scroll_acc += d; } if (s->scrolled && (s->opts & TPG_SCROLL)) { out = s->scroll_acc / s->units_per_px; s->scroll_acc -= out * s->units_per_px; if (s->opts & TPG_SCROLL_INVERT) out = -out; r->scroll = out; } } } else { s->scroll_have = 0; s->scroll_total = 0; s->scroll_acc = 0; } /* Last finger up: finish the contact sequence and synthesize a tap. */ if (!count && s->active) { unsigned int duration = now - s->start_ms; unsigned int click = 0; s->active = 0; if (!s->moved && !s->physical && !s->scrolled && duration >= TPG_TAP_MIN_MS && duration <= TPG_TAP_MAX_MS) { if (s->two) { if (s->opts & TPG_RIGHT_TAP) click = 0x02; } else if (s->started && (s->opts & TPG_RIGHT_CORNER) && in_corner(s, s->start_x, s->start_y)) { click = 0x02; } else if (s->opts & TPG_TAPCLICK) { click = 0x01; } } if (click) { s->synth_btn = click; s->synth_release_ms = now + TPG_CLICK_HOLD_MS; if (click == 0x02) s->taps_right++; else s->taps_left++; } s->started = 0; s->moved = 0; s->two = 0; s->physical = 0; s->scrolled = 0; s->scroll_have = 0; s->scroll_total = 0; s->have_last = 0; s->acc_x = s->acc_y = 0; } s->last_count = count; r->buttons = (unsigned int)s->btn_latched | s->synth_btn; }