cirque_pinnacle_gestures.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  2. * Copyright 2022 Daniel Kao <daniel.m.kao@gmail.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdlib.h>
  18. #include <lib/lib8tion/lib8tion.h>
  19. #include "cirque_pinnacle_gestures.h"
  20. #include "pointing_device.h"
  21. #include "timer.h"
  22. #include "wait.h"
  23. #if defined(CIRQUE_PINNACLE_TAP_ENABLE) || defined(CIRQUE_PINNACLE_CIRCULAR_SCROLL_ENABLE)
  24. static cirque_pinnacle_features_t features = {.tap_enable = true, .circular_scroll_enable = true};
  25. #endif
  26. #ifdef CIRQUE_PINNACLE_TAP_ENABLE
  27. static trackpad_tap_context_t tap;
  28. static report_mouse_t trackpad_tap(report_mouse_t mouse_report, pinnacle_data_t touchData) {
  29. if (touchData.touchDown != tap.touchDown) {
  30. tap.touchDown = touchData.touchDown;
  31. if (!touchData.zValue) {
  32. if (timer_elapsed(tap.timer) < CIRQUE_PINNACLE_TAPPING_TERM && tap.timer != 0) {
  33. mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, true, POINTING_DEVICE_BUTTON1);
  34. pointing_device_set_report(mouse_report);
  35. pointing_device_send();
  36. # if TAP_CODE_DELAY > 0
  37. wait_ms(TAP_CODE_DELAY);
  38. # endif
  39. mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, false, POINTING_DEVICE_BUTTON1);
  40. pointing_device_set_report(mouse_report);
  41. pointing_device_send();
  42. }
  43. }
  44. tap.timer = timer_read();
  45. }
  46. if (timer_elapsed(tap.timer) > (CIRQUE_PINNACLE_TOUCH_DEBOUNCE)) {
  47. tap.timer = 0;
  48. }
  49. return mouse_report;
  50. }
  51. void cirque_pinnacle_enable_tap(bool enable) {
  52. features.tap_enable = enable;
  53. }
  54. #endif
  55. #ifdef CIRQUE_PINNACLE_CIRCULAR_SCROLL_ENABLE
  56. /* To set a trackpad exclusively as scroll wheel: outer_ring_pct = 100, trigger_px = 0, trigger_ang = 0 */
  57. static circular_scroll_context_t scroll = {.config = {.outer_ring_pct = 33,
  58. .trigger_px = 16,
  59. .trigger_ang = 9102, /* 50 degrees */
  60. .wheel_clicks = 18}};
  61. static inline uint16_t atan2_16(int32_t dy, int32_t dx) {
  62. if (dy == 0) {
  63. if (dx >= 0) {
  64. return 0;
  65. } else {
  66. return 32768;
  67. }
  68. }
  69. int32_t abs_y = dy > 0 ? dy : -dy;
  70. int16_t a;
  71. if (dx >= 0) {
  72. a = 8192 - (8192 * (dx - abs_y) / (dx + abs_y));
  73. } else {
  74. a = 24576 - (8192 * (dx + abs_y) / (abs_y - dx));
  75. }
  76. if (dy < 0) {
  77. return -a; // negate if in quad III or IV
  78. }
  79. return a;
  80. }
  81. static circular_scroll_t circular_scroll(pinnacle_data_t touchData) {
  82. circular_scroll_t report = {0, 0, false};
  83. int8_t x, y, wheel_clicks;
  84. uint8_t center = 256 / 2, mag;
  85. int16_t ang, dot, det, opposite_side, adjacent_side;
  86. uint16_t scale = cirque_pinnacle_get_scale();
  87. if (touchData.zValue) {
  88. /*
  89. * Place origin at center of trackpad, treat coordinates as vectors.
  90. * Scale to fixed int8_t size; angles are independent of resolution.
  91. */
  92. if (scale) {
  93. x = (int8_t)((int32_t)touchData.xValue * 256 / scale - center);
  94. y = (int8_t)((int32_t)touchData.yValue * 256 / scale - center);
  95. } else {
  96. x = 0;
  97. y = 0;
  98. }
  99. /* Check if first touch */
  100. if (!scroll.z) {
  101. report.suppress_touch = false;
  102. /* Check if touch falls within outer ring */
  103. mag = sqrt16(x * x + y * y);
  104. if (mag * 100 / center >= 100 - scroll.config.outer_ring_pct) {
  105. scroll.state = SCROLL_DETECTING;
  106. scroll.x = x;
  107. scroll.y = y;
  108. scroll.mag = mag;
  109. /*
  110. * Decide scroll axis:
  111. * Vertical if started from righ half
  112. * Horizontal if started from left half
  113. * Flipped for left-handed
  114. */
  115. # if defined(POINTING_DEVICE_ROTATION_90)
  116. scroll.axis = y < 0;
  117. # elif defined(POINTING_DEVICE_ROTATION_180)
  118. scroll.axis = x > 0;
  119. # elif defined(POINTING_DEVICE_ROTATION_270)
  120. scroll.axis = y > 0;
  121. # else
  122. scroll.axis = x < 0;
  123. # endif
  124. }
  125. } else if (scroll.state == SCROLL_DETECTING) {
  126. report.suppress_touch = true;
  127. /* Already detecting scroll, check movement from touchdown location */
  128. mag = sqrt16((x - scroll.x) * (x - scroll.x) + (y - scroll.y) * (y - scroll.y));
  129. if (mag >= scroll.config.trigger_px) {
  130. /*
  131. * Find angle of movement.
  132. * 0 degrees here means movement towards center of circle
  133. */
  134. dot = scroll.x * x + scroll.y * y;
  135. det = scroll.x * y - scroll.y * x;
  136. opposite_side = abs(det); /* Based on scalar rejection */
  137. adjacent_side = abs(scroll.mag * scroll.mag - abs(dot)); /* Based on scalar projection */
  138. ang = (int16_t)atan2_16(opposite_side, adjacent_side);
  139. if (ang < scroll.config.trigger_ang) {
  140. /* Not a scroll, release coordinates */
  141. report.suppress_touch = false;
  142. scroll.state = NOT_SCROLL;
  143. } else {
  144. /* Scroll detected */
  145. scroll.state = SCROLL_VALID;
  146. }
  147. }
  148. }
  149. if (scroll.state == SCROLL_VALID) {
  150. report.suppress_touch = true;
  151. dot = scroll.x * x + scroll.y * y;
  152. det = scroll.x * y - scroll.y * x;
  153. ang = (int16_t)atan2_16(det, dot);
  154. wheel_clicks = ((int32_t)ang * scroll.config.wheel_clicks) / 65536;
  155. if (wheel_clicks >= 1 || wheel_clicks <= -1) {
  156. if (scroll.config.left_handed) {
  157. if (scroll.axis == 0) {
  158. report.h = -wheel_clicks;
  159. } else {
  160. report.v = wheel_clicks;
  161. }
  162. } else {
  163. if (scroll.axis == 0) {
  164. report.v = -wheel_clicks;
  165. } else {
  166. report.h = wheel_clicks;
  167. }
  168. }
  169. scroll.x = x;
  170. scroll.y = y;
  171. }
  172. }
  173. }
  174. scroll.z = touchData.zValue;
  175. if (!scroll.z) scroll.state = SCROLL_UNINITIALIZED;
  176. return report;
  177. }
  178. void cirque_pinnacle_enable_circular_scroll(bool enable) {
  179. features.circular_scroll_enable = enable;
  180. }
  181. void cirque_pinnacle_configure_circular_scroll(uint8_t outer_ring_pct, uint8_t trigger_px, uint16_t trigger_ang, uint8_t wheel_clicks, bool left_handed) {
  182. scroll.config.outer_ring_pct = outer_ring_pct;
  183. scroll.config.trigger_px = trigger_px;
  184. scroll.config.trigger_ang = trigger_ang;
  185. scroll.config.wheel_clicks = wheel_clicks;
  186. scroll.config.left_handed = left_handed;
  187. }
  188. #endif
  189. bool cirque_pinnacle_gestures(report_mouse_t* mouse_report, pinnacle_data_t touchData) {
  190. bool suppress_mouse_update = false;
  191. #ifdef CIRQUE_PINNACLE_CIRCULAR_SCROLL_ENABLE
  192. circular_scroll_t scroll_report;
  193. if (features.circular_scroll_enable) {
  194. scroll_report = circular_scroll(touchData);
  195. mouse_report->v = scroll_report.v;
  196. mouse_report->h = scroll_report.h;
  197. suppress_mouse_update = scroll_report.suppress_touch;
  198. }
  199. #endif
  200. #ifdef CIRQUE_PINNACLE_TAP_ENABLE
  201. if (features.tap_enable) {
  202. *mouse_report = trackpad_tap(*mouse_report, touchData);
  203. }
  204. #endif
  205. return suppress_mouse_update;
  206. }