| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- /* Host-side checks for the device-independent touchpad gesture engine. */
- #include <stdio.h>
- #include "touchpad_gesture.h"
- static struct tpg_state tp;
- static int failures;
- static void check(const char *name, int cond)
- {
- printf("%s: %s\n", cond ? "ok" : "FAIL", name);
- if (!cond)
- failures++;
- }
- static void frame(struct tpg_result *r, unsigned int count,
- int x0, int y0, int x1, int y1, int button,
- unsigned int now)
- {
- struct tpg_frame f;
- f.count = count;
- f.mask = 0;
- if (count >= 1)
- f.mask |= 1u;
- if (count >= 2)
- f.mask |= 2u;
- f.contact[0].x = x0;
- f.contact[0].y = y0;
- f.contact[1].x = x1;
- f.contact[1].y = y1;
- f.button = button;
- f.now_ms = now;
- tpg_frame(&tp, &f, r);
- }
- static unsigned int tick(unsigned int now)
- {
- struct tpg_result r;
- tpg_tick(&tp, now, &r);
- return r.buttons;
- }
- static void reset(void)
- {
- tpg_init(&tp, 3000, 2000, 2);
- }
- int main(void)
- {
- struct tpg_result r;
- unsigned int now;
- int scroll;
- reset();
- check("default options include scroll and taps",
- (tpg_options(&tp) & (TPG_TAPCLICK | TPG_RIGHT_TAP |
- TPG_RIGHT_CORNER | TPG_SCROLL)) ==
- (TPG_TAPCLICK | TPG_RIGHT_TAP | TPG_RIGHT_CORNER | TPG_SCROLL));
- /* One-finger tap -> left. */
- now = 1000;
- frame(&r, 1, 1000, 1000, 0, 0, 0, now);
- frame(&r, 1, 1000, 1000, 0, 0, 0, now + 50);
- frame(&r, 0, 0, 0, 0, 0, 0, now + 100);
- check("one-finger tap emits left", r.buttons == 1);
- check("one-finger tap releases", tick(now + 250) == 0);
- /* Two-finger tap -> right. */
- now = 2000;
- frame(&r, 1, 1000, 1000, 0, 0, 0, now);
- frame(&r, 2, 1000, 1000, 1200, 1000, 0, now + 50);
- frame(&r, 2, 1000, 1000, 1200, 1000, 0, now + 100);
- frame(&r, 0, 0, 0, 0, 0, 0, now + 150);
- check("two-finger tap emits right", r.buttons == 2);
- check("two-finger tap releases", tick(now + 300) == 0);
- /* Corner tap -> right; normal tap stays left. */
- now = 3000;
- frame(&r, 1, 2800, 200, 0, 0, 0, now);
- frame(&r, 1, 2800, 200, 0, 0, 0, now + 50);
- frame(&r, 0, 0, 0, 0, 0, 0, now + 100);
- check("corner tap emits right", r.buttons == 2);
- tick(now + 250);
- /* Physical corner press -> right and latched; outside -> left. */
- now = 4000;
- frame(&r, 1, 2800, 200, 0, 0, 1, now);
- check("physical corner press is right", r.buttons == 2);
- frame(&r, 1, 1000, 1000, 0, 0, 1, now + 50);
- check("physical corner latch survives movement", r.buttons == 2);
- frame(&r, 0, 0, 0, 0, 0, 0, now + 100);
- check("physical corner press releases", r.buttons == 0);
- now = 5000;
- frame(&r, 1, 1000, 1000, 0, 0, 1, now);
- check("physical normal press is left", r.buttons == 1);
- frame(&r, 0, 0, 0, 0, 0, 0, now + 50);
- /* A moving one-finger contact is a pointer move, not a click. */
- now = 6000;
- frame(&r, 1, 1000, 1000, 0, 0, 0, now);
- frame(&r, 1, 1200, 1000, 0, 0, 0, now + 50);
- check("one-finger drag moves the pointer", r.dx > 0);
- frame(&r, 0, 0, 0, 0, 0, 0, now + 100);
- check("one-finger drag does not click", r.buttons == 0);
- /* Two-finger scroll: pointer frozen, positive pixel scroll, no click. */
- now = 7000;
- reset();
- frame(&r, 2, 1000, 1000, 1400, 1000, 0, now);
- scroll = 0;
- frame(&r, 2, 1000, 1020, 1400, 1020, 0, now + 25);
- frame(&r, 2, 1000, 1040, 1400, 1040, 0, now + 50);
- frame(&r, 2, 1000, 1060, 1400, 1060, 0, now + 75);
- scroll += r.scroll;
- check("two-finger scroll does not move the pointer", !r.dx && !r.dy);
- frame(&r, 2, 1000, 1100, 1400, 1100, 0, now + 100);
- scroll += r.scroll;
- frame(&r, 2, 1000, 1160, 1400, 1160, 0, now + 125);
- scroll += r.scroll;
- frame(&r, 0, 0, 0, 0, 0, 0, now + 150);
- check("two-finger scroll emits positive pixels", scroll > 0);
- check("two-finger scroll does not click", r.buttons == 0);
- check("two-finger scroll releases cleanly", tick(now + 300) == 0);
- /* Traditional direction inverts the output. */
- reset();
- tpg_set_options(&tp, tpg_options(&tp) | TPG_SCROLL_INVERT);
- now = 8000;
- frame(&r, 2, 1000, 1000, 1400, 1000, 0, now);
- frame(&r, 2, 1000, 1040, 1400, 1040, 0, now + 25);
- frame(&r, 2, 1000, 1120, 1400, 1120, 0, now + 50);
- check("traditional scroll inverts direction", r.scroll < 0);
- /* Two-finger tap with the first slot lifting early still right-clicks. */
- reset();
- now = 9000;
- frame(&r, 1, 1000, 1000, 0, 0, 0, now);
- frame(&r, 2, 1000, 1000, 1200, 1000, 0, now + 50);
- frame(&r, 1, 1200, 1000, 0, 0, 0, now + 100);
- frame(&r, 0, 0, 0, 0, 0, 0, now + 150);
- check("two-finger tap survives first-slot lift", r.buttons == 2);
- /* Pointer re-anchors after the second finger leaves. */
- reset();
- now = 10000;
- frame(&r, 1, 1000, 1000, 0, 0, 0, now);
- frame(&r, 1, 1100, 1000, 0, 0, 0, now + 25);
- frame(&r, 2, 1100, 1000, 1500, 1000, 0, now + 50);
- frame(&r, 1, 1500, 1000, 0, 0, 0, now + 75);
- check("returning to one finger re-anchors", r.dx == 0 && r.dy == 0);
- frame(&r, 1, 1600, 1000, 0, 0, 0, now + 100);
- check("pointer resumes after re-anchor", r.dx > 0);
- /* Scroll disabled: no output and no accidental right-click. */
- reset();
- tpg_set_options(&tp, tpg_options(&tp) & ~TPG_SCROLL);
- now = 11000;
- frame(&r, 2, 1000, 1000, 1400, 1000, 0, now);
- frame(&r, 2, 1000, 1040, 1400, 1040, 0, now + 25);
- frame(&r, 2, 1000, 1120, 1400, 1120, 0, now + 50);
- check("scroll off emits no scroll", r.scroll == 0);
- frame(&r, 0, 0, 0, 0, 0, 0, now + 75);
- check("scroll off still suppresses two-finger click", r.buttons == 0);
- /* A long press is not a tap. */
- reset();
- now = 12000;
- frame(&r, 1, 1000, 1000, 0, 0, 0, now);
- frame(&r, 0, 0, 0, 0, 0, 0, now + 400);
- check("long press does not click", r.buttons == 0);
- printf(failures ? "FAILURES: %d\n" : "all gesture checks passed\n",
- failures);
- return failures ? 1 : 0;
- }
|