diff options
author | Joel Challis <git@zvecr.com> | 2021-02-07 13:56:08 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-07 13:56:08 +0000 |
commit | 02881427692b682287d19fe533c3b84aa7b42a26 (patch) | |
tree | b64dfc778cf652a04b337f8067dac1cd2a1cfe05 /quantum | |
parent | 0c44aa950334a4956dbe1d3f1e4830c13eb38b45 (diff) | |
download | qmk_firmware-02881427692b682287d19fe533c3b84aa7b42a26.tar.gz qmk_firmware-02881427692b682287d19fe533c3b84aa7b42a26.zip |
Migrate mousekey to quantum (#11804)
Diffstat (limited to 'quantum')
-rw-r--r-- | quantum/mousekey.c | 488 | ||||
-rw-r--r-- | quantum/mousekey.h | 179 |
2 files changed, 667 insertions, 0 deletions
diff --git a/quantum/mousekey.c b/quantum/mousekey.c new file mode 100644 index 000000000..63e74baa9 --- /dev/null +++ b/quantum/mousekey.c | |||
@@ -0,0 +1,488 @@ | |||
1 | /* | ||
2 | * Copyright 2011 Jun Wako <wakojun@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 | |||
18 | #include <stdint.h> | ||
19 | #include "keycode.h" | ||
20 | #include "host.h" | ||
21 | #include "timer.h" | ||
22 | #include "print.h" | ||
23 | #include "debug.h" | ||
24 | #include "mousekey.h" | ||
25 | |||
26 | inline int8_t times_inv_sqrt2(int8_t x) { | ||
27 | // 181/256 is pretty close to 1/sqrt(2) | ||
28 | // 0.70703125 0.707106781 | ||
29 | // 1 too small for x=99 and x=198 | ||
30 | // This ends up being a mult and discard lower 8 bits | ||
31 | return (x * 181) >> 8; | ||
32 | } | ||
33 | |||
34 | static report_mouse_t mouse_report = {0}; | ||
35 | static void mousekey_debug(void); | ||
36 | static uint8_t mousekey_accel = 0; | ||
37 | static uint8_t mousekey_repeat = 0; | ||
38 | static uint8_t mousekey_wheel_repeat = 0; | ||
39 | #ifdef MK_KINETIC_SPEED | ||
40 | static uint16_t mouse_timer = 0; | ||
41 | #endif | ||
42 | |||
43 | #ifndef MK_3_SPEED | ||
44 | |||
45 | static uint16_t last_timer_c = 0; | ||
46 | static uint16_t last_timer_w = 0; | ||
47 | |||
48 | /* | ||
49 | * Mouse keys acceleration algorithm | ||
50 | * http://en.wikipedia.org/wiki/Mouse_keys | ||
51 | * | ||
52 | * speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000) | ||
53 | */ | ||
54 | /* milliseconds between the initial key press and first repeated motion event (0-2550) */ | ||
55 | uint8_t mk_delay = MOUSEKEY_DELAY / 10; | ||
56 | /* milliseconds between repeated motion events (0-255) */ | ||
57 | uint8_t mk_interval = MOUSEKEY_INTERVAL; | ||
58 | /* steady speed (in action_delta units) applied each event (0-255) */ | ||
59 | uint8_t mk_max_speed = MOUSEKEY_MAX_SPEED; | ||
60 | /* number of events (count) accelerating to steady speed (0-255) */ | ||
61 | uint8_t mk_time_to_max = MOUSEKEY_TIME_TO_MAX; | ||
62 | /* ramp used to reach maximum pointer speed (NOT SUPPORTED) */ | ||
63 | // int8_t mk_curve = 0; | ||
64 | /* wheel params */ | ||
65 | /* milliseconds between the initial key press and first repeated motion event (0-2550) */ | ||
66 | uint8_t mk_wheel_delay = MOUSEKEY_WHEEL_DELAY / 10; | ||
67 | /* milliseconds between repeated motion events (0-255) */ | ||
68 | uint8_t mk_wheel_interval = MOUSEKEY_WHEEL_INTERVAL; | ||
69 | uint8_t mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED; | ||
70 | uint8_t mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX; | ||
71 | |||
72 | # ifndef MK_COMBINED | ||
73 | |||
74 | static uint8_t move_unit(void) { | ||
75 | uint16_t unit; | ||
76 | if (mousekey_accel & (1 << 0)) { | ||
77 | unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 4; | ||
78 | } else if (mousekey_accel & (1 << 1)) { | ||
79 | unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 2; | ||
80 | } else if (mousekey_accel & (1 << 2)) { | ||
81 | unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed); | ||
82 | } else if (mousekey_repeat == 0) { | ||
83 | unit = MOUSEKEY_MOVE_DELTA; | ||
84 | } else if (mousekey_repeat >= mk_time_to_max) { | ||
85 | unit = MOUSEKEY_MOVE_DELTA * mk_max_speed; | ||
86 | } else { | ||
87 | unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max; | ||
88 | } | ||
89 | return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit)); | ||
90 | } | ||
91 | |||
92 | static uint8_t wheel_unit(void) { | ||
93 | uint16_t unit; | ||
94 | if (mousekey_accel & (1 << 0)) { | ||
95 | unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 4; | ||
96 | } else if (mousekey_accel & (1 << 1)) { | ||
97 | unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 2; | ||
98 | } else if (mousekey_accel & (1 << 2)) { | ||
99 | unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed); | ||
100 | } else if (mousekey_wheel_repeat == 0) { | ||
101 | unit = MOUSEKEY_WHEEL_DELTA; | ||
102 | } else if (mousekey_wheel_repeat >= mk_wheel_time_to_max) { | ||
103 | unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed; | ||
104 | } else { | ||
105 | unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_wheel_repeat) / mk_wheel_time_to_max; | ||
106 | } | ||
107 | return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit)); | ||
108 | } | ||
109 | |||
110 | # else /* #ifndef MK_COMBINED */ | ||
111 | # ifndef MK_KINETIC_SPEED | ||
112 | |||
113 | /* | ||
114 | * Kinetic movement acceleration algorithm | ||
115 | * | ||
116 | * current speed = I + A * T/50 + A * 0.5 * T^2 | maximum B | ||
117 | * | ||
118 | * T: time since the mouse movement started | ||
119 | * E: mouse events per second (set through MOUSEKEY_INTERVAL, UHK sends 250, the | ||
120 | * pro micro on my Signum 3.0 sends only 125!) | ||
121 | * I: initial speed at time 0 | ||
122 | * A: acceleration | ||
123 | * B: base mouse travel speed | ||
124 | */ | ||
125 | const uint16_t mk_accelerated_speed = MOUSEKEY_ACCELERATED_SPEED; | ||
126 | const uint16_t mk_base_speed = MOUSEKEY_BASE_SPEED; | ||
127 | const uint16_t mk_decelerated_speed = MOUSEKEY_DECELERATED_SPEED; | ||
128 | const uint16_t mk_initial_speed = MOUSEKEY_INITIAL_SPEED; | ||
129 | |||
130 | static uint8_t move_unit(void) { | ||
131 | float speed = mk_initial_speed; | ||
132 | |||
133 | if (mousekey_accel & ((1 << 0) | (1 << 2))) { | ||
134 | speed = mousekey_accel & (1 << 2) ? mk_accelerated_speed : mk_decelerated_speed; | ||
135 | } else if (mousekey_repeat && mouse_timer) { | ||
136 | const float time_elapsed = timer_elapsed(mouse_timer) / 50; | ||
137 | speed = mk_initial_speed + MOUSEKEY_MOVE_DELTA * time_elapsed + MOUSEKEY_MOVE_DELTA * 0.5 * time_elapsed * time_elapsed; | ||
138 | |||
139 | speed = speed > mk_base_speed ? mk_base_speed : speed; | ||
140 | } | ||
141 | |||
142 | /* convert speed to USB mouse speed 1 to 127 */ | ||
143 | speed = (uint8_t)(speed / (1000.0f / mk_interval)); | ||
144 | speed = speed < 1 ? 1 : speed; | ||
145 | |||
146 | return speed > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : speed; | ||
147 | } | ||
148 | |||
149 | float mk_wheel_interval = 1000.0f / MOUSEKEY_WHEEL_INITIAL_MOVEMENTS; | ||
150 | |||
151 | static uint8_t wheel_unit(void) { | ||
152 | float speed = MOUSEKEY_WHEEL_INITIAL_MOVEMENTS; | ||
153 | |||
154 | if (mousekey_accel & ((1 << 0) | (1 << 2))) { | ||
155 | speed = mousekey_accel & (1 << 2) ? MOUSEKEY_WHEEL_ACCELERATED_MOVEMENTS : MOUSEKEY_WHEEL_DECELERATED_MOVEMENTS; | ||
156 | } else if (mousekey_repeat && mouse_timer) { | ||
157 | if (mk_wheel_interval != MOUSEKEY_WHEEL_BASE_MOVEMENTS) { | ||
158 | const float time_elapsed = timer_elapsed(mouse_timer) / 50; | ||
159 | speed = MOUSEKEY_WHEEL_INITIAL_MOVEMENTS + 1 * time_elapsed + 1 * 0.5 * time_elapsed * time_elapsed; | ||
160 | } | ||
161 | speed = speed > MOUSEKEY_WHEEL_BASE_MOVEMENTS ? MOUSEKEY_WHEEL_BASE_MOVEMENTS : speed; | ||
162 | } | ||
163 | |||
164 | mk_wheel_interval = 1000.0f / speed; | ||
165 | |||
166 | return 1; | ||
167 | } | ||
168 | |||
169 | # else /* #ifndef MK_KINETIC_SPEED */ | ||
170 | |||
171 | static uint8_t move_unit(void) { | ||
172 | uint16_t unit; | ||
173 | if (mousekey_accel & (1 << 0)) { | ||
174 | unit = 1; | ||
175 | } else if (mousekey_accel & (1 << 1)) { | ||
176 | unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 2; | ||
177 | } else if (mousekey_accel & (1 << 2)) { | ||
178 | unit = MOUSEKEY_MOVE_MAX; | ||
179 | } else if (mousekey_repeat == 0) { | ||
180 | unit = MOUSEKEY_MOVE_DELTA; | ||
181 | } else if (mousekey_repeat >= mk_time_to_max) { | ||
182 | unit = MOUSEKEY_MOVE_DELTA * mk_max_speed; | ||
183 | } else { | ||
184 | unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max; | ||
185 | } | ||
186 | return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit)); | ||
187 | } | ||
188 | |||
189 | static uint8_t wheel_unit(void) { | ||
190 | uint16_t unit; | ||
191 | if (mousekey_accel & (1 << 0)) { | ||
192 | unit = 1; | ||
193 | } else if (mousekey_accel & (1 << 1)) { | ||
194 | unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 2; | ||
195 | } else if (mousekey_accel & (1 << 2)) { | ||
196 | unit = MOUSEKEY_WHEEL_MAX; | ||
197 | } else if (mousekey_repeat == 0) { | ||
198 | unit = MOUSEKEY_WHEEL_DELTA; | ||
199 | } else if (mousekey_repeat >= mk_wheel_time_to_max) { | ||
200 | unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed; | ||
201 | } else { | ||
202 | unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max; | ||
203 | } | ||
204 | return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit)); | ||
205 | } | ||
206 | |||
207 | # endif /* #ifndef MK_KINETIC_SPEED */ | ||
208 | # endif /* #ifndef MK_COMBINED */ | ||
209 | |||
210 | void mousekey_task(void) { | ||
211 | // report cursor and scroll movement independently | ||
212 | report_mouse_t const tmpmr = mouse_report; | ||
213 | |||
214 | mouse_report.x = 0; | ||
215 | mouse_report.y = 0; | ||
216 | mouse_report.v = 0; | ||
217 | mouse_report.h = 0; | ||
218 | |||
219 | if ((tmpmr.x || tmpmr.y) && timer_elapsed(last_timer_c) > (mousekey_repeat ? mk_interval : mk_delay * 10)) { | ||
220 | if (mousekey_repeat != UINT8_MAX) mousekey_repeat++; | ||
221 | if (tmpmr.x != 0) mouse_report.x = move_unit() * ((tmpmr.x > 0) ? 1 : -1); | ||
222 | if (tmpmr.y != 0) mouse_report.y = move_unit() * ((tmpmr.y > 0) ? 1 : -1); | ||
223 | |||
224 | /* diagonal move [1/sqrt(2)] */ | ||
225 | if (mouse_report.x && mouse_report.y) { | ||
226 | mouse_report.x = times_inv_sqrt2(mouse_report.x); | ||
227 | if (mouse_report.x == 0) { | ||
228 | mouse_report.x = 1; | ||
229 | } | ||
230 | mouse_report.y = times_inv_sqrt2(mouse_report.y); | ||
231 | if (mouse_report.y == 0) { | ||
232 | mouse_report.y = 1; | ||
233 | } | ||
234 | } | ||
235 | } | ||
236 | if ((tmpmr.v || tmpmr.h) && timer_elapsed(last_timer_w) > (mousekey_wheel_repeat ? mk_wheel_interval : mk_wheel_delay * 10)) { | ||
237 | if (mousekey_wheel_repeat != UINT8_MAX) mousekey_wheel_repeat++; | ||
238 | if (tmpmr.v != 0) mouse_report.v = wheel_unit() * ((tmpmr.v > 0) ? 1 : -1); | ||
239 | if (tmpmr.h != 0) mouse_report.h = wheel_unit() * ((tmpmr.h > 0) ? 1 : -1); | ||
240 | |||
241 | /* diagonal move [1/sqrt(2)] */ | ||
242 | if (mouse_report.v && mouse_report.h) { | ||
243 | mouse_report.v = times_inv_sqrt2(mouse_report.v); | ||
244 | if (mouse_report.v == 0) { | ||
245 | mouse_report.v = 1; | ||
246 | } | ||
247 | mouse_report.h = times_inv_sqrt2(mouse_report.h); | ||
248 | if (mouse_report.h == 0) { | ||
249 | mouse_report.h = 1; | ||
250 | } | ||
251 | } | ||
252 | } | ||
253 | |||
254 | if (mouse_report.x || mouse_report.y || mouse_report.v || mouse_report.h) mousekey_send(); | ||
255 | mouse_report = tmpmr; | ||
256 | } | ||
257 | |||
258 | void mousekey_on(uint8_t code) { | ||
259 | # ifdef MK_KINETIC_SPEED | ||
260 | if (mouse_timer == 0) { | ||
261 | mouse_timer = timer_read(); | ||
262 | } | ||
263 | # endif /* #ifdef MK_KINETIC_SPEED */ | ||
264 | |||
265 | if (code == KC_MS_UP) | ||
266 | mouse_report.y = move_unit() * -1; | ||
267 | else if (code == KC_MS_DOWN) | ||
268 | mouse_report.y = move_unit(); | ||
269 | else if (code == KC_MS_LEFT) | ||
270 | mouse_report.x = move_unit() * -1; | ||
271 | else if (code == KC_MS_RIGHT) | ||
272 | mouse_report.x = move_unit(); | ||
273 | else if (code == KC_MS_WH_UP) | ||
274 | mouse_report.v = wheel_unit(); | ||
275 | else if (code == KC_MS_WH_DOWN) | ||
276 | mouse_report.v = wheel_unit() * -1; | ||
277 | else if (code == KC_MS_WH_LEFT) | ||
278 | mouse_report.h = wheel_unit() * -1; | ||
279 | else if (code == KC_MS_WH_RIGHT) | ||
280 | mouse_report.h = wheel_unit(); | ||
281 | else if (IS_MOUSEKEY_BUTTON(code)) | ||
282 | mouse_report.buttons |= 1 << (code - KC_MS_BTN1); | ||
283 | else if (code == KC_MS_ACCEL0) | ||
284 | mousekey_accel |= (1 << 0); | ||
285 | else if (code == KC_MS_ACCEL1) | ||
286 | mousekey_accel |= (1 << 1); | ||
287 | else if (code == KC_MS_ACCEL2) | ||
288 | mousekey_accel |= (1 << 2); | ||
289 | } | ||
290 | |||
291 | void mousekey_off(uint8_t code) { | ||
292 | if (code == KC_MS_UP && mouse_report.y < 0) | ||
293 | mouse_report.y = 0; | ||
294 | else if (code == KC_MS_DOWN && mouse_report.y > 0) | ||
295 | mouse_report.y = 0; | ||
296 | else if (code == KC_MS_LEFT && mouse_report.x < 0) | ||
297 | mouse_report.x = 0; | ||
298 | else if (code == KC_MS_RIGHT && mouse_report.x > 0) | ||
299 | mouse_report.x = 0; | ||
300 | else if (code == KC_MS_WH_UP && mouse_report.v > 0) | ||
301 | mouse_report.v = 0; | ||
302 | else if (code == KC_MS_WH_DOWN && mouse_report.v < 0) | ||
303 | mouse_report.v = 0; | ||
304 | else if (code == KC_MS_WH_LEFT && mouse_report.h < 0) | ||
305 | mouse_report.h = 0; | ||
306 | else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0) | ||
307 | mouse_report.h = 0; | ||
308 | else if (IS_MOUSEKEY_BUTTON(code)) | ||
309 | mouse_report.buttons &= ~(1 << (code - KC_MS_BTN1)); | ||
310 | else if (code == KC_MS_ACCEL0) | ||
311 | mousekey_accel &= ~(1 << 0); | ||
312 | else if (code == KC_MS_ACCEL1) | ||
313 | mousekey_accel &= ~(1 << 1); | ||
314 | else if (code == KC_MS_ACCEL2) | ||
315 | mousekey_accel &= ~(1 << 2); | ||
316 | if (mouse_report.x == 0 && mouse_report.y == 0) { | ||
317 | mousekey_repeat = 0; | ||
318 | # ifdef MK_KINETIC_SPEED | ||
319 | mouse_timer = 0; | ||
320 | # endif /* #ifdef MK_KINETIC_SPEED */ | ||
321 | } | ||
322 | if (mouse_report.v == 0 && mouse_report.h == 0) mousekey_wheel_repeat = 0; | ||
323 | } | ||
324 | |||
325 | #else /* #ifndef MK_3_SPEED */ | ||
326 | |||
327 | enum { mkspd_unmod, mkspd_0, mkspd_1, mkspd_2, mkspd_COUNT }; | ||
328 | # ifndef MK_MOMENTARY_ACCEL | ||
329 | static uint8_t mk_speed = mkspd_1; | ||
330 | # else | ||
331 | static uint8_t mk_speed = mkspd_unmod; | ||
332 | static uint8_t mkspd_DEFAULT = mkspd_unmod; | ||
333 | # endif | ||
334 | static uint16_t last_timer_c = 0; | ||
335 | static uint16_t last_timer_w = 0; | ||
336 | uint16_t c_offsets[mkspd_COUNT] = {MK_C_OFFSET_UNMOD, MK_C_OFFSET_0, MK_C_OFFSET_1, MK_C_OFFSET_2}; | ||
337 | uint16_t c_intervals[mkspd_COUNT] = {MK_C_INTERVAL_UNMOD, MK_C_INTERVAL_0, MK_C_INTERVAL_1, MK_C_INTERVAL_2}; | ||
338 | uint16_t w_offsets[mkspd_COUNT] = {MK_W_OFFSET_UNMOD, MK_W_OFFSET_0, MK_W_OFFSET_1, MK_W_OFFSET_2}; | ||
339 | uint16_t w_intervals[mkspd_COUNT] = {MK_W_INTERVAL_UNMOD, MK_W_INTERVAL_0, MK_W_INTERVAL_1, MK_W_INTERVAL_2}; | ||
340 | |||
341 | void mousekey_task(void) { | ||
342 | // report cursor and scroll movement independently | ||
343 | report_mouse_t const tmpmr = mouse_report; | ||
344 | mouse_report.x = 0; | ||
345 | mouse_report.y = 0; | ||
346 | mouse_report.v = 0; | ||
347 | mouse_report.h = 0; | ||
348 | |||
349 | if ((tmpmr.x || tmpmr.y) && timer_elapsed(last_timer_c) > c_intervals[mk_speed]) { | ||
350 | mouse_report.x = tmpmr.x; | ||
351 | mouse_report.y = tmpmr.y; | ||
352 | } | ||
353 | if ((tmpmr.h || tmpmr.v) && timer_elapsed(last_timer_w) > w_intervals[mk_speed]) { | ||
354 | mouse_report.v = tmpmr.v; | ||
355 | mouse_report.h = tmpmr.h; | ||
356 | } | ||
357 | |||
358 | if (mouse_report.x || mouse_report.y || mouse_report.v || mouse_report.h) mousekey_send(); | ||
359 | mouse_report = tmpmr; | ||
360 | } | ||
361 | |||
362 | void adjust_speed(void) { | ||
363 | uint16_t const c_offset = c_offsets[mk_speed]; | ||
364 | uint16_t const w_offset = w_offsets[mk_speed]; | ||
365 | if (mouse_report.x > 0) mouse_report.x = c_offset; | ||
366 | if (mouse_report.x < 0) mouse_report.x = c_offset * -1; | ||
367 | if (mouse_report.y > 0) mouse_report.y = c_offset; | ||
368 | if (mouse_report.y < 0) mouse_report.y = c_offset * -1; | ||
369 | if (mouse_report.h > 0) mouse_report.h = w_offset; | ||
370 | if (mouse_report.h < 0) mouse_report.h = w_offset * -1; | ||
371 | if (mouse_report.v > 0) mouse_report.v = w_offset; | ||
372 | if (mouse_report.v < 0) mouse_report.v = w_offset * -1; | ||
373 | // adjust for diagonals | ||
374 | if (mouse_report.x && mouse_report.y) { | ||
375 | mouse_report.x = times_inv_sqrt2(mouse_report.x); | ||
376 | if (mouse_report.x == 0) { | ||
377 | mouse_report.x = 1; | ||
378 | } | ||
379 | mouse_report.y = times_inv_sqrt2(mouse_report.y); | ||
380 | if (mouse_report.y == 0) { | ||
381 | mouse_report.y = 1; | ||
382 | } | ||
383 | } | ||
384 | if (mouse_report.h && mouse_report.v) { | ||
385 | mouse_report.h = times_inv_sqrt2(mouse_report.h); | ||
386 | mouse_report.v = times_inv_sqrt2(mouse_report.v); | ||
387 | } | ||
388 | } | ||
389 | |||
390 | void mousekey_on(uint8_t code) { | ||
391 | uint16_t const c_offset = c_offsets[mk_speed]; | ||
392 | uint16_t const w_offset = w_offsets[mk_speed]; | ||
393 | uint8_t const old_speed = mk_speed; | ||
394 | if (code == KC_MS_UP) | ||
395 | mouse_report.y = c_offset * -1; | ||
396 | else if (code == KC_MS_DOWN) | ||
397 | mouse_report.y = c_offset; | ||
398 | else if (code == KC_MS_LEFT) | ||
399 | mouse_report.x = c_offset * -1; | ||
400 | else if (code == KC_MS_RIGHT) | ||
401 | mouse_report.x = c_offset; | ||
402 | else if (code == KC_MS_WH_UP) | ||
403 | mouse_report.v = w_offset; | ||
404 | else if (code == KC_MS_WH_DOWN) | ||
405 | mouse_report.v = w_offset * -1; | ||
406 | else if (code == KC_MS_WH_LEFT) | ||
407 | mouse_report.h = w_offset * -1; | ||
408 | else if (code == KC_MS_WH_RIGHT) | ||
409 | mouse_report.h = w_offset; | ||
410 | else if (IS_MOUSEKEY_BUTTON(code)) | ||
411 | mouse_report.buttons |= 1 << (code - KC_MS_BTN1); | ||
412 | else if (code == KC_MS_ACCEL0) | ||
413 | mk_speed = mkspd_0; | ||
414 | else if (code == KC_MS_ACCEL1) | ||
415 | mk_speed = mkspd_1; | ||
416 | else if (code == KC_MS_ACCEL2) | ||
417 | mk_speed = mkspd_2; | ||
418 | if (mk_speed != old_speed) adjust_speed(); | ||
419 | } | ||
420 | |||
421 | void mousekey_off(uint8_t code) { | ||
422 | # ifdef MK_MOMENTARY_ACCEL | ||
423 | uint8_t const old_speed = mk_speed; | ||
424 | # endif | ||
425 | if (code == KC_MS_UP && mouse_report.y < 0) | ||
426 | mouse_report.y = 0; | ||
427 | else if (code == KC_MS_DOWN && mouse_report.y > 0) | ||
428 | mouse_report.y = 0; | ||
429 | else if (code == KC_MS_LEFT && mouse_report.x < 0) | ||
430 | mouse_report.x = 0; | ||
431 | else if (code == KC_MS_RIGHT && mouse_report.x > 0) | ||
432 | mouse_report.x = 0; | ||
433 | else if (code == KC_MS_WH_UP && mouse_report.v > 0) | ||
434 | mouse_report.v = 0; | ||
435 | else if (code == KC_MS_WH_DOWN && mouse_report.v < 0) | ||
436 | mouse_report.v = 0; | ||
437 | else if (code == KC_MS_WH_LEFT && mouse_report.h < 0) | ||
438 | mouse_report.h = 0; | ||
439 | else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0) | ||
440 | mouse_report.h = 0; | ||
441 | else if (IS_MOUSEKEY_BUTTON(code)) | ||
442 | mouse_report.buttons &= ~(1 << (code - KC_MS_BTN1)); | ||
443 | # ifdef MK_MOMENTARY_ACCEL | ||
444 | else if (code == KC_MS_ACCEL0) | ||
445 | mk_speed = mkspd_DEFAULT; | ||
446 | else if (code == KC_MS_ACCEL1) | ||
447 | mk_speed = mkspd_DEFAULT; | ||
448 | else if (code == KC_MS_ACCEL2) | ||
449 | mk_speed = mkspd_DEFAULT; | ||
450 | if (mk_speed != old_speed) adjust_speed(); | ||
451 | # endif | ||
452 | } | ||
453 | |||
454 | #endif /* #ifndef MK_3_SPEED */ | ||
455 | |||
456 | void mousekey_send(void) { | ||
457 | mousekey_debug(); | ||
458 | uint16_t time = timer_read(); | ||
459 | if (mouse_report.x || mouse_report.y) last_timer_c = time; | ||
460 | if (mouse_report.v || mouse_report.h) last_timer_w = time; | ||
461 | host_mouse_send(&mouse_report); | ||
462 | } | ||
463 | |||
464 | void mousekey_clear(void) { | ||
465 | mouse_report = (report_mouse_t){}; | ||
466 | mousekey_repeat = 0; | ||
467 | mousekey_wheel_repeat = 0; | ||
468 | mousekey_accel = 0; | ||
469 | } | ||
470 | |||
471 | static void mousekey_debug(void) { | ||
472 | if (!debug_mouse) return; | ||
473 | print("mousekey [btn|x y v h](rep/acl): ["); | ||
474 | print_hex8(mouse_report.buttons); | ||
475 | print("|"); | ||
476 | print_decs(mouse_report.x); | ||
477 | print(" "); | ||
478 | print_decs(mouse_report.y); | ||
479 | print(" "); | ||
480 | print_decs(mouse_report.v); | ||
481 | print(" "); | ||
482 | print_decs(mouse_report.h); | ||
483 | print("]("); | ||
484 | print_dec(mousekey_repeat); | ||
485 | print("/"); | ||
486 | print_dec(mousekey_accel); | ||
487 | print(")\n"); | ||
488 | } | ||
diff --git a/quantum/mousekey.h b/quantum/mousekey.h new file mode 100644 index 000000000..70dc4bb5c --- /dev/null +++ b/quantum/mousekey.h | |||
@@ -0,0 +1,179 @@ | |||
1 | /* | ||
2 | Copyright 2011 Jun Wako <wakojun@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 | |||
18 | #pragma once | ||
19 | |||
20 | #include <stdint.h> | ||
21 | #include "host.h" | ||
22 | |||
23 | #ifndef MK_3_SPEED | ||
24 | |||
25 | /* max value on report descriptor */ | ||
26 | # ifndef MOUSEKEY_MOVE_MAX | ||
27 | # define MOUSEKEY_MOVE_MAX 127 | ||
28 | # elif MOUSEKEY_MOVE_MAX > 127 | ||
29 | # error MOUSEKEY_MOVE_MAX needs to be smaller than 127 | ||
30 | # endif | ||
31 | |||
32 | # ifndef MOUSEKEY_WHEEL_MAX | ||
33 | # define MOUSEKEY_WHEEL_MAX 127 | ||
34 | # elif MOUSEKEY_WHEEL_MAX > 127 | ||
35 | # error MOUSEKEY_WHEEL_MAX needs to be smaller than 127 | ||
36 | # endif | ||
37 | |||
38 | # ifndef MOUSEKEY_MOVE_DELTA | ||
39 | # ifndef MK_KINETIC_SPEED | ||
40 | # define MOUSEKEY_MOVE_DELTA 5 | ||
41 | # else | ||
42 | # define MOUSEKEY_MOVE_DELTA 25 | ||
43 | # endif | ||
44 | # endif | ||
45 | # ifndef MOUSEKEY_WHEEL_DELTA | ||
46 | # define MOUSEKEY_WHEEL_DELTA 1 | ||
47 | # endif | ||
48 | # ifndef MOUSEKEY_DELAY | ||
49 | # ifndef MK_KINETIC_SPEED | ||
50 | # define MOUSEKEY_DELAY 300 | ||
51 | # else | ||
52 | # define MOUSEKEY_DELAY 8 | ||
53 | # endif | ||
54 | # endif | ||
55 | # ifndef MOUSEKEY_INTERVAL | ||
56 | # ifndef MK_KINETIC_SPEED | ||
57 | # define MOUSEKEY_INTERVAL 50 | ||
58 | # else | ||
59 | # define MOUSEKEY_INTERVAL 8 | ||
60 | # endif | ||
61 | # endif | ||
62 | # ifndef MOUSEKEY_MAX_SPEED | ||
63 | # define MOUSEKEY_MAX_SPEED 10 | ||
64 | # endif | ||
65 | # ifndef MOUSEKEY_TIME_TO_MAX | ||
66 | # define MOUSEKEY_TIME_TO_MAX 20 | ||
67 | # endif | ||
68 | # ifndef MOUSEKEY_WHEEL_DELAY | ||
69 | # define MOUSEKEY_WHEEL_DELAY 300 | ||
70 | # endif | ||
71 | # ifndef MOUSEKEY_WHEEL_INTERVAL | ||
72 | # define MOUSEKEY_WHEEL_INTERVAL 100 | ||
73 | # endif | ||
74 | # ifndef MOUSEKEY_WHEEL_MAX_SPEED | ||
75 | # define MOUSEKEY_WHEEL_MAX_SPEED 8 | ||
76 | # endif | ||
77 | # ifndef MOUSEKEY_WHEEL_TIME_TO_MAX | ||
78 | # define MOUSEKEY_WHEEL_TIME_TO_MAX 40 | ||
79 | # endif | ||
80 | |||
81 | # ifndef MOUSEKEY_INITIAL_SPEED | ||
82 | # define MOUSEKEY_INITIAL_SPEED 100 | ||
83 | # endif | ||
84 | # ifndef MOUSEKEY_BASE_SPEED | ||
85 | # define MOUSEKEY_BASE_SPEED 1000 | ||
86 | # endif | ||
87 | # ifndef MOUSEKEY_DECELERATED_SPEED | ||
88 | # define MOUSEKEY_DECELERATED_SPEED 400 | ||
89 | # endif | ||
90 | # ifndef MOUSEKEY_ACCELERATED_SPEED | ||
91 | # define MOUSEKEY_ACCELERATED_SPEED 3000 | ||
92 | # endif | ||
93 | # ifndef MOUSEKEY_WHEEL_INITIAL_MOVEMENTS | ||
94 | # define MOUSEKEY_WHEEL_INITIAL_MOVEMENTS 16 | ||
95 | # endif | ||
96 | # ifndef MOUSEKEY_WHEEL_BASE_MOVEMENTS | ||
97 | # define MOUSEKEY_WHEEL_BASE_MOVEMENTS 32 | ||
98 | # endif | ||
99 | # ifndef MOUSEKEY_WHEEL_ACCELERATED_MOVEMENTS | ||
100 | # define MOUSEKEY_WHEEL_ACCELERATED_MOVEMENTS 48 | ||
101 | # endif | ||
102 | # ifndef MOUSEKEY_WHEEL_DECELERATED_MOVEMENTS | ||
103 | # define MOUSEKEY_WHEEL_DECELERATED_MOVEMENTS 8 | ||
104 | # endif | ||
105 | |||
106 | #else /* #ifndef MK_3_SPEED */ | ||
107 | |||
108 | # ifndef MK_C_OFFSET_UNMOD | ||
109 | # define MK_C_OFFSET_UNMOD 16 | ||
110 | # endif | ||
111 | # ifndef MK_C_INTERVAL_UNMOD | ||
112 | # define MK_C_INTERVAL_UNMOD 16 | ||
113 | # endif | ||
114 | # ifndef MK_C_OFFSET_0 | ||
115 | # define MK_C_OFFSET_0 1 | ||
116 | # endif | ||
117 | # ifndef MK_C_INTERVAL_0 | ||
118 | # define MK_C_INTERVAL_0 32 | ||
119 | # endif | ||
120 | # ifndef MK_C_OFFSET_1 | ||
121 | # define MK_C_OFFSET_1 4 | ||
122 | # endif | ||
123 | # ifndef MK_C_INTERVAL_1 | ||
124 | # define MK_C_INTERVAL_1 16 | ||
125 | # endif | ||
126 | # ifndef MK_C_OFFSET_2 | ||
127 | # define MK_C_OFFSET_2 32 | ||
128 | # endif | ||
129 | # ifndef MK_C_INTERVAL_2 | ||
130 | # define MK_C_INTERVAL_2 16 | ||
131 | # endif | ||
132 | |||
133 | # ifndef MK_W_OFFSET_UNMOD | ||
134 | # define MK_W_OFFSET_UNMOD 1 | ||
135 | # endif | ||
136 | # ifndef MK_W_INTERVAL_UNMOD | ||
137 | # define MK_W_INTERVAL_UNMOD 40 | ||
138 | # endif | ||
139 | # ifndef MK_W_OFFSET_0 | ||
140 | # define MK_W_OFFSET_0 1 | ||
141 | # endif | ||
142 | # ifndef MK_W_INTERVAL_0 | ||
143 | # define MK_W_INTERVAL_0 360 | ||
144 | # endif | ||
145 | # ifndef MK_W_OFFSET_1 | ||
146 | # define MK_W_OFFSET_1 1 | ||
147 | # endif | ||
148 | # ifndef MK_W_INTERVAL_1 | ||
149 | # define MK_W_INTERVAL_1 120 | ||
150 | # endif | ||
151 | # ifndef MK_W_OFFSET_2 | ||
152 | # define MK_W_OFFSET_2 1 | ||
153 | # endif | ||
154 | # ifndef MK_W_INTERVAL_2 | ||
155 | # define MK_W_INTERVAL_2 20 | ||
156 | # endif | ||
157 | |||
158 | #endif /* #ifndef MK_3_SPEED */ | ||
159 | |||
160 | #ifdef __cplusplus | ||
161 | extern "C" { | ||
162 | #endif | ||
163 | |||
164 | extern uint8_t mk_delay; | ||
165 | extern uint8_t mk_interval; | ||
166 | extern uint8_t mk_max_speed; | ||
167 | extern uint8_t mk_time_to_max; | ||
168 | extern uint8_t mk_wheel_max_speed; | ||
169 | extern uint8_t mk_wheel_time_to_max; | ||
170 | |||
171 | void mousekey_task(void); | ||
172 | void mousekey_on(uint8_t code); | ||
173 | void mousekey_off(uint8_t code); | ||
174 | void mousekey_clear(void); | ||
175 | void mousekey_send(void); | ||
176 | |||
177 | #ifdef __cplusplus | ||
178 | } | ||
179 | #endif | ||