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