touchpad_gesture.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Device-independent touchpad contact gestures.
  2. *
  3. * A hardware driver decodes its reports into up to two contact positions plus
  4. * the total contact count and the physical clickpad bit, and calls
  5. * tpg_frame() once per report. The engine owns pointer delta scaling, tap
  6. * detection, the synthesized button press, the physical-button latch and
  7. * two-finger scrolling, so a PS/2, I2C or USB touchpad backend only has to
  8. * decode its own protocol.
  9. *
  10. * Scroll output is in pixels: positive means "toward later content" (the page
  11. * moves up), matching a wheel-down event under natural scrolling. The Elan
  12. * and pointer delta scale defaults to 2 device units per pixel; a backend
  13. * with a different resolution passes its own units_per_px to tpg_init().
  14. */
  15. #ifndef TOUCHPAD_GESTURE_H
  16. #define TOUCHPAD_GESTURE_H
  17. #define TPG_CONTACTS 2
  18. /* Option bits. */
  19. #define TPG_TAPCLICK 0x01 /* one-finger tap = left click */
  20. #define TPG_RIGHT_TAP 0x02 /* two-finger tap = right click */
  21. #define TPG_RIGHT_CORNER 0x04 /* corner tap/press = right click */
  22. #define TPG_SCROLL 0x08 /* two-finger scroll */
  23. #define TPG_SCROLL_INVERT 0x10 /* traditional instead of natural */
  24. #define TPG_DEFAULT_OPTIONS (TPG_TAPCLICK | TPG_RIGHT_TAP | \
  25. TPG_RIGHT_CORNER | TPG_SCROLL)
  26. /* Software right-button zone: right 25% x bottom 25% of the pad. */
  27. #define TPG_CORNER_X_PCT 75
  28. #define TPG_CORNER_Y_PCT 25
  29. struct tpg_contact {
  30. int x, y; /* absolute device coordinates */
  31. };
  32. struct tpg_frame {
  33. unsigned int count; /* total active contacts, 0..5 */
  34. unsigned int mask; /* bit 0/1: contact[] entry is present */
  35. struct tpg_contact contact[TPG_CONTACTS];
  36. int button; /* physical clickpad/left button */
  37. unsigned int now_ms;
  38. };
  39. struct tpg_result {
  40. int dx, dy; /* pointer motion, PS/2 convention (+dy up) */
  41. int scroll; /* pixels, positive = later content */
  42. unsigned int buttons; /* bit0 left, bit1 right, bit2 middle */
  43. };
  44. struct tpg_state {
  45. int max_x, max_y;
  46. int units_per_px;
  47. unsigned int opts;
  48. int tap_move; /* max movement for a tap, device units */
  49. int scroll_start; /* movement before scrolling starts */
  50. int btn_raw;
  51. int btn_latched; /* physical button: 0, 0x01 or 0x02 */
  52. unsigned int synth_btn;
  53. unsigned int synth_release_ms;
  54. unsigned int taps_left; /* synthesized tap counters (status) */
  55. unsigned int taps_right;
  56. /* One contact sequence, from first finger down to last finger up. */
  57. int active;
  58. unsigned int start_ms;
  59. int start_x, start_y;
  60. int started;
  61. int moved;
  62. int two;
  63. int physical;
  64. int scrolled;
  65. unsigned int last_count;
  66. /* Pointer delta accumulation. */
  67. int last_x, last_y;
  68. int have_last;
  69. int acc_x, acc_y;
  70. /* Two-finger scroll tracking. */
  71. int scroll_have;
  72. int scroll_last_y;
  73. int scroll_total;
  74. int scroll_acc;
  75. };
  76. void tpg_init(struct tpg_state *s, int max_x, int max_y, int units_per_px);
  77. void tpg_set_limits(struct tpg_state *s, int max_x, int max_y);
  78. void tpg_set_options(struct tpg_state *s, unsigned int opts);
  79. unsigned int tpg_options(const struct tpg_state *s);
  80. /* Feed one report. r is fully overwritten. */
  81. void tpg_frame(struct tpg_state *s, const struct tpg_frame *f,
  82. struct tpg_result *r);
  83. /* Time-only poll: releases a synthesized tap press after its hold. Call it
  84. * between reports (and before the report throttle) so the release is
  85. * delivered even though no new contact report is coming. */
  86. void tpg_tick(struct tpg_state *s, unsigned int now_ms,
  87. struct tpg_result *r);
  88. #endif