cirque_pinnacle_gestures.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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(SPLIT_POINTING_ENABLE) && defined(POINTING_DEVICE_COMBINED)
  24. # include "keyboard.h"
  25. #endif
  26. #if (defined(CIRQUE_PINNACLE_TAP_ENABLE) || defined(CIRQUE_PINNACLE_CIRCULAR_SCROLL_ENABLE)) && CIRQUE_PINNACLE_POSITION_MODE
  27. static cirque_pinnacle_features_t features = {.tap_enable = true, .circular_scroll_enable = true};
  28. #endif
  29. #if defined(CIRQUE_PINNACLE_TAP_ENABLE) && CIRQUE_PINNACLE_POSITION_MODE
  30. static trackpad_tap_context_t tap;
  31. static report_mouse_t trackpad_tap(report_mouse_t mouse_report, pinnacle_data_t touchData) {
  32. if (touchData.touchDown != tap.touchDown) {
  33. tap.touchDown = touchData.touchDown;
  34. if (!touchData.zValue) {
  35. if (timer_elapsed(tap.timer) < CIRQUE_PINNACLE_TAPPING_TERM && tap.timer != 0) {
  36. mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, true, POINTING_DEVICE_BUTTON1);
  37. pointing_device_set_report(mouse_report);
  38. pointing_device_send();
  39. # if TAP_CODE_DELAY > 0
  40. wait_ms(TAP_CODE_DELAY);
  41. # endif
  42. mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, false, POINTING_DEVICE_BUTTON1);
  43. pointing_device_set_report(mouse_report);
  44. pointing_device_send();
  45. }
  46. }
  47. tap.timer = timer_read();
  48. }
  49. if (timer_elapsed(tap.timer) > (CIRQUE_PINNACLE_TOUCH_DEBOUNCE)) {
  50. tap.timer = 0;
  51. }
  52. return mouse_report;
  53. }
  54. void cirque_pinnacle_enable_tap(bool enable) {
  55. features.tap_enable = enable;
  56. }
  57. #endif
  58. #ifdef CIRQUE_PINNACLE_CIRCULAR_SCROLL_ENABLE
  59. # if !CIRQUE_PINNACLE_POSITION_MODE
  60. # error "Circular scroll is not supported in relative mode"
  61. # endif
  62. /* To set a trackpad exclusively as scroll wheel: outer_ring_pct = 100, trigger_px = 0, trigger_ang = 0 */
  63. static circular_scroll_context_t scroll = {.config = {.outer_ring_pct = 33,
  64. .trigger_px = 16,
  65. .trigger_ang = 9102, /* 50 degrees */
  66. .wheel_clicks = 18}};
  67. static inline uint16_t atan2_16(int32_t dy, int32_t dx) {
  68. if (dy == 0) {
  69. if (dx >= 0) {
  70. return 0;
  71. } else {
  72. return 32768;
  73. }
  74. }
  75. int32_t abs_y = dy > 0 ? dy : -dy;
  76. int16_t a;
  77. if (dx >= 0) {
  78. a = 8192 - (8192 * (dx - abs_y) / (dx + abs_y));
  79. } else {
  80. a = 24576 - (8192 * (dx + abs_y) / (abs_y - dx));
  81. }
  82. if (dy < 0) {
  83. return -a; // negate if in quad III or IV
  84. }
  85. return a;
  86. }
  87. static circular_scroll_t circular_scroll(pinnacle_data_t touchData) {
  88. circular_scroll_t report = {0, 0, false};
  89. int8_t x, y, wheel_clicks;
  90. uint8_t center = INT8_MAX, mag;
  91. int16_t ang, dot, det, opposite_side, adjacent_side;
  92. uint16_t scale = cirque_pinnacle_get_scale();
  93. if (touchData.zValue) {
  94. /*
  95. * Place origin at center of trackpad, treat coordinates as vectors.
  96. * Scale to +/-INT8_MAX; angles are independent of resolution.
  97. */
  98. if (scale) {
  99. /* Rotate coordinates into a consistent orientation */
  100. report_mouse_t rot = {.x = (int8_t)((int32_t)touchData.xValue * INT8_MAX * 2 / scale - center), .y = (int8_t)((int32_t)touchData.yValue * INT8_MAX * 2 / scale - center)};
  101. # if defined(SPLIT_POINTING_ENABLE) && defined(POINTING_DEVICE_COMBINED)
  102. if (!is_keyboard_left()) {
  103. rot = pointing_device_adjust_by_defines_right(rot);
  104. } else
  105. # endif
  106. {
  107. rot = pointing_device_adjust_by_defines(rot);
  108. }
  109. x = rot.x;
  110. y = rot.y;
  111. } else {
  112. x = 0;
  113. y = 0;
  114. }
  115. /* Check if first touch */
  116. if (!scroll.z) {
  117. report.suppress_touch = false;
  118. /* Check if touch falls within outer ring */
  119. mag = sqrt16(x * x + y * y);
  120. if (mag * 100 / center >= 100 - scroll.config.outer_ring_pct) {
  121. scroll.state = SCROLL_DETECTING;
  122. scroll.x = x;
  123. scroll.y = y;
  124. scroll.mag = mag;
  125. /*
  126. * Decide scroll axis:
  127. * Vertical if started from righ half
  128. * Horizontal if started from left half
  129. * Flipped for left-handed
  130. */
  131. scroll.axis = x < 0;
  132. }
  133. } else if (scroll.state == SCROLL_DETECTING) {
  134. report.suppress_touch = true;
  135. /* Already detecting scroll, check movement from touchdown location */
  136. mag = sqrt16((x - scroll.x) * (x - scroll.x) + (y - scroll.y) * (y - scroll.y));
  137. if (mag >= scroll.config.trigger_px) {
  138. /*
  139. * Find angle of movement.
  140. * 0 degrees here means movement towards center of circle
  141. */
  142. dot = scroll.x * x + scroll.y * y;
  143. det = scroll.x * y - scroll.y * x;
  144. opposite_side = abs(det); /* Based on scalar rejection */
  145. adjacent_side = abs(scroll.mag * scroll.mag - abs(dot)); /* Based on scalar projection */
  146. ang = (int16_t)atan2_16(opposite_side, adjacent_side);
  147. if (ang < scroll.config.trigger_ang) {
  148. /* Not a scroll, release coordinates */
  149. report.suppress_touch = false;
  150. scroll.state = NOT_SCROLL;
  151. } else {
  152. /* Scroll detected */
  153. scroll.state = SCROLL_VALID;
  154. }
  155. }
  156. }
  157. if (scroll.state == SCROLL_VALID) {
  158. report.suppress_touch = true;
  159. dot = scroll.x * x + scroll.y * y;
  160. det = scroll.x * y - scroll.y * x;
  161. ang = (int16_t)atan2_16(det, dot);
  162. wheel_clicks = ((int32_t)ang * scroll.config.wheel_clicks) / 65536;
  163. if (wheel_clicks >= 1 || wheel_clicks <= -1) {
  164. if (scroll.config.left_handed) {
  165. if (scroll.axis == 0) {
  166. report.h = -wheel_clicks;
  167. } else {
  168. report.v = wheel_clicks;
  169. }
  170. } else {
  171. if (scroll.axis == 0) {
  172. report.v = -wheel_clicks;
  173. } else {
  174. report.h = wheel_clicks;
  175. }
  176. }
  177. scroll.x = x;
  178. scroll.y = y;
  179. }
  180. }
  181. }
  182. scroll.z = touchData.zValue;
  183. if (!scroll.z) scroll.state = SCROLL_UNINITIALIZED;
  184. return report;
  185. }
  186. void cirque_pinnacle_enable_circular_scroll(bool enable) {
  187. features.circular_scroll_enable = enable;
  188. }
  189. 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) {
  190. scroll.config.outer_ring_pct = outer_ring_pct;
  191. scroll.config.trigger_px = trigger_px;
  192. scroll.config.trigger_ang = trigger_ang;
  193. scroll.config.wheel_clicks = wheel_clicks;
  194. scroll.config.left_handed = left_handed;
  195. }
  196. #endif
  197. bool cirque_pinnacle_gestures(report_mouse_t* mouse_report, pinnacle_data_t touchData) {
  198. bool suppress_mouse_update = false;
  199. #ifdef CIRQUE_PINNACLE_CIRCULAR_SCROLL_ENABLE
  200. # if !CIRQUE_PINNACLE_POSITION_MODE
  201. # error "Circular scroll is not supported in relative mode"
  202. # endif
  203. circular_scroll_t scroll_report;
  204. if (features.circular_scroll_enable) {
  205. scroll_report = circular_scroll(touchData);
  206. mouse_report->v = scroll_report.v;
  207. mouse_report->h = scroll_report.h;
  208. suppress_mouse_update = scroll_report.suppress_touch;
  209. }
  210. #endif
  211. #if defined(CIRQUE_PINNACLE_TAP_ENABLE) && CIRQUE_PINNACLE_POSITION_MODE
  212. if (features.tap_enable) {
  213. *mouse_report = trackpad_tap(*mouse_report, touchData);
  214. }
  215. #endif
  216. return suppress_mouse_update;
  217. }