You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
8.7 KiB

  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. }
  38. }
  39. tap.timer = timer_read();
  40. }
  41. if (timer_elapsed(tap.timer) > (CIRQUE_PINNACLE_TOUCH_DEBOUNCE)) {
  42. tap.timer = 0;
  43. }
  44. return mouse_report;
  45. }
  46. void cirque_pinnacle_enable_tap(bool enable) {
  47. features.tap_enable = enable;
  48. }
  49. #endif
  50. #ifdef CIRQUE_PINNACLE_CIRCULAR_SCROLL_ENABLE
  51. # if !CIRQUE_PINNACLE_POSITION_MODE
  52. # error "Circular scroll is not supported in relative mode"
  53. # endif
  54. /* To set a trackpad exclusively as scroll wheel: outer_ring_pct = 100, trigger_px = 0, trigger_ang = 0 */
  55. static circular_scroll_context_t scroll = {.config = {.outer_ring_pct = 33,
  56. .trigger_px = 16,
  57. .trigger_ang = 9102, /* 50 degrees */
  58. .wheel_clicks = 18}};
  59. static inline uint16_t atan2_16(int32_t dy, int32_t dx) {
  60. if (dy == 0) {
  61. if (dx >= 0) {
  62. return 0;
  63. } else {
  64. return 32768;
  65. }
  66. }
  67. int32_t abs_y = dy > 0 ? dy : -dy;
  68. int16_t a;
  69. if (dx >= 0) {
  70. a = 8192 - (8192 * (dx - abs_y) / (dx + abs_y));
  71. } else {
  72. a = 24576 - (8192 * (dx + abs_y) / (abs_y - dx));
  73. }
  74. if (dy < 0) {
  75. return -a; // negate if in quad III or IV
  76. }
  77. return a;
  78. }
  79. static circular_scroll_t circular_scroll(pinnacle_data_t touchData) {
  80. circular_scroll_t report = {0, 0, false};
  81. int8_t x, y, wheel_clicks;
  82. uint8_t center = INT8_MAX, mag;
  83. int16_t ang, dot, det, opposite_side, adjacent_side;
  84. uint16_t scale = cirque_pinnacle_get_scale();
  85. if (touchData.zValue) {
  86. /*
  87. * Place origin at center of trackpad, treat coordinates as vectors.
  88. * Scale to +/-INT8_MAX; angles are independent of resolution.
  89. */
  90. if (scale) {
  91. /* Rotate coordinates into a consistent orientation */
  92. 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)};
  93. # if defined(SPLIT_POINTING_ENABLE) && defined(POINTING_DEVICE_COMBINED)
  94. if (!is_keyboard_left()) {
  95. rot = pointing_device_adjust_by_defines_right(rot);
  96. } else
  97. # endif
  98. {
  99. rot = pointing_device_adjust_by_defines(rot);
  100. }
  101. x = rot.x;
  102. y = rot.y;
  103. } else {
  104. x = 0;
  105. y = 0;
  106. }
  107. /* Check if first touch */
  108. if (!scroll.z) {
  109. report.suppress_touch = false;
  110. /* Check if touch falls within outer ring */
  111. mag = sqrt16(x * x + y * y);
  112. if (mag * 100 / center >= 100 - scroll.config.outer_ring_pct) {
  113. scroll.state = SCROLL_DETECTING;
  114. scroll.x = x;
  115. scroll.y = y;
  116. scroll.mag = mag;
  117. /*
  118. * Decide scroll axis:
  119. * Vertical if started from righ half
  120. * Horizontal if started from left half
  121. * Flipped for left-handed
  122. */
  123. scroll.axis = x < 0;
  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. # if !CIRQUE_PINNACLE_POSITION_MODE
  193. # error "Circular scroll is not supported in relative mode"
  194. # endif
  195. circular_scroll_t scroll_report;
  196. if (features.circular_scroll_enable) {
  197. scroll_report = circular_scroll(touchData);
  198. mouse_report->v = scroll_report.v;
  199. mouse_report->h = scroll_report.h;
  200. suppress_mouse_update = scroll_report.suppress_touch;
  201. }
  202. #endif
  203. #if defined(CIRQUE_PINNACLE_TAP_ENABLE) && CIRQUE_PINNACLE_POSITION_MODE
  204. if (features.tap_enable) {
  205. *mouse_report = trackpad_tap(*mouse_report, touchData);
  206. }
  207. #endif
  208. return suppress_mouse_update;
  209. }