touchpad_gesture.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* Device-independent touchpad contact gestures. See touchpad_gesture.h. */
  2. #include "touchpad_gesture.h"
  3. #define TPG_TAP_MIN_MS 40
  4. #define TPG_TAP_MAX_MS 300
  5. #define TPG_CLICK_HOLD_MS 120
  6. #define TPG_POINTER_MAX 64
  7. static int abs_int(int v)
  8. {
  9. return v < 0 ? -v : v;
  10. }
  11. static void set_limits(struct tpg_state *s)
  12. {
  13. s->tap_move = s->max_x / 50;
  14. if (s->tap_move < 16)
  15. s->tap_move = 16;
  16. s->scroll_start = s->max_x / 100;
  17. if (s->scroll_start < 8)
  18. s->scroll_start = 8;
  19. }
  20. static int in_corner(const struct tpg_state *s, int x, int y)
  21. {
  22. return x >= s->max_x * TPG_CORNER_X_PCT / 100 &&
  23. y <= s->max_y * TPG_CORNER_Y_PCT / 100;
  24. }
  25. void tpg_init(struct tpg_state *s, int max_x, int max_y, int units_per_px)
  26. {
  27. s->max_x = max_x > 0 ? max_x : 4095;
  28. s->max_y = max_y > 0 ? max_y : 4095;
  29. s->units_per_px = units_per_px > 0 ? units_per_px : 1;
  30. s->opts = TPG_DEFAULT_OPTIONS;
  31. set_limits(s);
  32. s->btn_raw = 0;
  33. s->btn_latched = 0;
  34. s->synth_btn = 0;
  35. s->synth_release_ms = 0;
  36. s->taps_left = 0;
  37. s->taps_right = 0;
  38. s->active = 0;
  39. s->start_ms = 0;
  40. s->start_x = s->start_y = 0;
  41. s->started = 0;
  42. s->moved = 0;
  43. s->two = 0;
  44. s->physical = 0;
  45. s->scrolled = 0;
  46. s->last_count = 0;
  47. s->last_x = s->last_y = 0;
  48. s->have_last = 0;
  49. s->acc_x = s->acc_y = 0;
  50. s->scroll_have = 0;
  51. s->scroll_last_y = 0;
  52. s->scroll_total = 0;
  53. s->scroll_acc = 0;
  54. }
  55. void tpg_set_limits(struct tpg_state *s, int max_x, int max_y)
  56. {
  57. s->max_x = max_x > 0 ? max_x : 4095;
  58. s->max_y = max_y > 0 ? max_y : 4095;
  59. set_limits(s);
  60. }
  61. void tpg_set_options(struct tpg_state *s, unsigned int opts)
  62. {
  63. s->opts = opts;
  64. }
  65. unsigned int tpg_options(const struct tpg_state *s)
  66. {
  67. return s->opts;
  68. }
  69. void tpg_tick(struct tpg_state *s, unsigned int now_ms,
  70. struct tpg_result *r)
  71. {
  72. r->dx = r->dy = r->scroll = 0;
  73. if (s->synth_btn && (int)(now_ms - s->synth_release_ms) >= 0)
  74. s->synth_btn = 0;
  75. r->buttons = (unsigned int)s->btn_latched | s->synth_btn;
  76. }
  77. void tpg_frame(struct tpg_state *s, const struct tpg_frame *f,
  78. struct tpg_result *r)
  79. {
  80. unsigned int count = f->count;
  81. unsigned int mask = f->mask;
  82. unsigned int now = f->now_ms;
  83. int has0 = (mask & 1u) && count >= 1;
  84. int has1 = (mask & 2u) && count >= 2;
  85. int c0x = has0 ? f->contact[0].x : 0;
  86. int c0y = has0 ? f->contact[0].y : 0;
  87. r->dx = r->dy = r->scroll = 0;
  88. if (s->synth_btn && (int)(now - s->synth_release_ms) >= 0)
  89. s->synth_btn = 0;
  90. /* Latch the physical clickpad button at its leading edge. A press
  91. * whose finger is in the right-click zone reports the right button
  92. * for the whole press; everything else is the left button. */
  93. if (f->button) {
  94. if (!s->btn_raw) {
  95. s->btn_latched = 0x01;
  96. if ((s->opts & TPG_RIGHT_CORNER) && has0 &&
  97. in_corner(s, c0x, c0y))
  98. s->btn_latched = 0x02;
  99. }
  100. s->physical = 1;
  101. } else {
  102. s->btn_latched = 0;
  103. }
  104. s->btn_raw = f->button != 0;
  105. if (count) {
  106. if (!s->active) {
  107. s->active = 1;
  108. s->start_ms = now;
  109. s->started = 0;
  110. s->moved = 0;
  111. s->two = 0;
  112. s->physical = f->button != 0;
  113. s->scrolled = 0;
  114. s->scroll_have = 0;
  115. s->scroll_total = 0;
  116. s->scroll_acc = 0;
  117. s->have_last = 0;
  118. s->acc_x = s->acc_y = 0;
  119. }
  120. if (count >= 2)
  121. s->two = 1;
  122. /* The active finger packed into contact[0] can change when a
  123. * finger lifts. Re-anchor the tap movement check so a
  124. * two-finger tap followed by an early first-finger lift is
  125. * not mistaken for a drag. */
  126. if (s->last_count && count != s->last_count)
  127. s->started = 0;
  128. if (has0) {
  129. if (!s->started) {
  130. s->started = 1;
  131. s->start_x = c0x;
  132. s->start_y = c0y;
  133. } else if (abs_int(c0x - s->start_x) > s->tap_move ||
  134. abs_int(c0y - s->start_y) > s->tap_move) {
  135. s->moved = 1;
  136. }
  137. }
  138. }
  139. /* Pointer motion only with exactly one contact; two fingers are
  140. * either a tap or a scroll, so the pointer must not drift. */
  141. if (count == 1 && has0) {
  142. if (!s->have_last) {
  143. s->last_x = c0x;
  144. s->last_y = c0y;
  145. s->have_last = 1;
  146. } else {
  147. int dx, dy;
  148. s->acc_x += c0x - s->last_x;
  149. s->acc_y += c0y - s->last_y;
  150. s->last_x = c0x;
  151. s->last_y = c0y;
  152. dx = s->acc_x / s->units_per_px;
  153. s->acc_x -= dx * s->units_per_px;
  154. dy = s->acc_y / s->units_per_px;
  155. s->acc_y -= dy * s->units_per_px;
  156. if (dx > TPG_POINTER_MAX)
  157. dx = TPG_POINTER_MAX;
  158. if (dx < -TPG_POINTER_MAX)
  159. dx = -TPG_POINTER_MAX;
  160. if (dy > TPG_POINTER_MAX)
  161. dy = TPG_POINTER_MAX;
  162. if (dy < -TPG_POINTER_MAX)
  163. dy = -TPG_POINTER_MAX;
  164. r->dx = dx;
  165. r->dy = dy;
  166. }
  167. } else {
  168. s->have_last = 0;
  169. s->acc_x = s->acc_y = 0;
  170. }
  171. /* Two-finger scroll. The centroid movement is accumulated until it
  172. * clears the start threshold, which keeps a stationary two-finger tap
  173. * from scrolling. */
  174. if (count == 2 && has0 && has1) {
  175. int cy = (f->contact[0].y + f->contact[1].y) / 2;
  176. if (!s->scroll_have) {
  177. s->scroll_have = 1;
  178. s->scroll_last_y = cy;
  179. s->scroll_total = 0;
  180. } else {
  181. int d = cy - s->scroll_last_y;
  182. int out;
  183. s->scroll_last_y = cy;
  184. if (!s->scrolled) {
  185. s->scroll_total += d;
  186. if (abs_int(s->scroll_total) >= s->scroll_start) {
  187. s->scrolled = 1;
  188. s->scroll_acc += s->scroll_total;
  189. }
  190. } else {
  191. s->scroll_acc += d;
  192. }
  193. if (s->scrolled && (s->opts & TPG_SCROLL)) {
  194. out = s->scroll_acc / s->units_per_px;
  195. s->scroll_acc -= out * s->units_per_px;
  196. if (s->opts & TPG_SCROLL_INVERT)
  197. out = -out;
  198. r->scroll = out;
  199. }
  200. }
  201. } else {
  202. s->scroll_have = 0;
  203. s->scroll_total = 0;
  204. s->scroll_acc = 0;
  205. }
  206. /* Last finger up: finish the contact sequence and synthesize a tap. */
  207. if (!count && s->active) {
  208. unsigned int duration = now - s->start_ms;
  209. unsigned int click = 0;
  210. s->active = 0;
  211. if (!s->moved && !s->physical && !s->scrolled &&
  212. duration >= TPG_TAP_MIN_MS && duration <= TPG_TAP_MAX_MS) {
  213. if (s->two) {
  214. if (s->opts & TPG_RIGHT_TAP)
  215. click = 0x02;
  216. } else if (s->started &&
  217. (s->opts & TPG_RIGHT_CORNER) &&
  218. in_corner(s, s->start_x, s->start_y)) {
  219. click = 0x02;
  220. } else if (s->opts & TPG_TAPCLICK) {
  221. click = 0x01;
  222. }
  223. }
  224. if (click) {
  225. s->synth_btn = click;
  226. s->synth_release_ms = now + TPG_CLICK_HOLD_MS;
  227. if (click == 0x02)
  228. s->taps_right++;
  229. else
  230. s->taps_left++;
  231. }
  232. s->started = 0;
  233. s->moved = 0;
  234. s->two = 0;
  235. s->physical = 0;
  236. s->scrolled = 0;
  237. s->scroll_have = 0;
  238. s->scroll_total = 0;
  239. s->have_last = 0;
  240. s->acc_x = s->acc_y = 0;
  241. }
  242. s->last_count = count;
  243. r->buttons = (unsigned int)s->btn_latched | s->synth_btn;
  244. }