aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/common/mousekey.c
diff options
context:
space:
mode:
authorstein3 <stein3@gmail.com>2020-10-06 07:15:41 -0700
committerstein3 <stein3@gmail.com>2020-10-06 07:15:41 -0700
commit2e402741a89c5eec8cf30c966ce6f36d6ec9249b (patch)
tree3592e8c5e6bd19943ae55db7fc02a5f755afbb51 /tmk_core/common/mousekey.c
parent3e5e4f74272c610bb9fa737f674f8e65ed6100ca (diff)
parent2013f6313430b977e557e482d30daa279a46e75d (diff)
downloadqmk_firmware-2e402741a89c5eec8cf30c966ce6f36d6ec9249b.tar.gz
qmk_firmware-2e402741a89c5eec8cf30c966ce6f36d6ec9249b.zip
Merge branch 'master' into meteor
Diffstat (limited to 'tmk_core/common/mousekey.c')
-rw-r--r--tmk_core/common/mousekey.c91
1 files changed, 47 insertions, 44 deletions
diff --git a/tmk_core/common/mousekey.c b/tmk_core/common/mousekey.c
index 42bf231f4..ef18bcf1a 100644
--- a/tmk_core/common/mousekey.c
+++ b/tmk_core/common/mousekey.c
@@ -33,9 +33,9 @@ inline int8_t times_inv_sqrt2(int8_t x) {
33 33
34static report_mouse_t mouse_report = {0}; 34static report_mouse_t mouse_report = {0};
35static void mousekey_debug(void); 35static void mousekey_debug(void);
36static uint8_t mousekey_accel = 0; 36static uint8_t mousekey_accel = 0;
37static uint8_t mousekey_repeat = 0; 37static uint8_t mousekey_repeat = 0;
38static uint16_t last_timer = 0; 38static uint8_t mousekey_wheel_repeat = 0;
39 39
40#ifndef MK_3_SPEED 40#ifndef MK_3_SPEED
41 41
@@ -94,12 +94,12 @@ static uint8_t wheel_unit(void) {
94 unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 2; 94 unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 2;
95 } else if (mousekey_accel & (1 << 2)) { 95 } else if (mousekey_accel & (1 << 2)) {
96 unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed); 96 unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed);
97 } else if (mousekey_repeat == 0) { 97 } else if (mousekey_wheel_repeat == 0) {
98 unit = MOUSEKEY_WHEEL_DELTA; 98 unit = MOUSEKEY_WHEEL_DELTA;
99 } else if (mousekey_repeat >= mk_wheel_time_to_max) { 99 } else if (mousekey_wheel_repeat >= mk_wheel_time_to_max) {
100 unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed; 100 unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed;
101 } else { 101 } else {
102 unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max; 102 unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_wheel_repeat) / mk_wheel_time_to_max;
103 } 103 }
104 return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit)); 104 return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit));
105} 105}
@@ -147,14 +147,17 @@ static uint8_t wheel_unit(void) {
147void mousekey_task(void) { 147void mousekey_task(void) {
148 // report cursor and scroll movement independently 148 // report cursor and scroll movement independently
149 report_mouse_t const tmpmr = mouse_report; 149 report_mouse_t const tmpmr = mouse_report;
150 if ((mouse_report.x || mouse_report.y) && timer_elapsed(last_timer_c) > (mousekey_repeat ? mk_interval : mk_delay * 10)) { 150
151 mouse_report.x = 0;
152 mouse_report.y = 0;
153 mouse_report.v = 0;
154 mouse_report.h = 0;
155
156 if ((tmpmr.x || tmpmr.y) && timer_elapsed(last_timer_c) > (mousekey_repeat ? mk_interval : mk_delay * 10)) {
151 if (mousekey_repeat != UINT8_MAX) mousekey_repeat++; 157 if (mousekey_repeat != UINT8_MAX) mousekey_repeat++;
152 mouse_report.v = 0; 158 if (tmpmr.x != 0) mouse_report.x = move_unit() * ((tmpmr.x > 0) ? 1 : -1);
153 mouse_report.h = 0; 159 if (tmpmr.y != 0) mouse_report.y = move_unit() * ((tmpmr.y > 0) ? 1 : -1);
154 if (mouse_report.x > 0) mouse_report.x = move_unit(); 160
155 if (mouse_report.x < 0) mouse_report.x = move_unit() * -1;
156 if (mouse_report.y > 0) mouse_report.y = move_unit();
157 if (mouse_report.y < 0) mouse_report.y = move_unit() * -1;
158 /* diagonal move [1/sqrt(2)] */ 161 /* diagonal move [1/sqrt(2)] */
159 if (mouse_report.x && mouse_report.y) { 162 if (mouse_report.x && mouse_report.y) {
160 mouse_report.x = times_inv_sqrt2(mouse_report.x); 163 mouse_report.x = times_inv_sqrt2(mouse_report.x);
@@ -166,18 +169,12 @@ void mousekey_task(void) {
166 mouse_report.y = 1; 169 mouse_report.y = 1;
167 } 170 }
168 } 171 }
169 mousekey_send();
170 last_timer_c = last_timer;
171 mouse_report = tmpmr;
172 } 172 }
173 if ((mouse_report.v || mouse_report.h) && timer_elapsed(last_timer_w) > (mousekey_repeat ? mk_wheel_interval : mk_wheel_delay * 10)) { 173 if ((tmpmr.v || tmpmr.h) && timer_elapsed(last_timer_w) > (mousekey_wheel_repeat ? mk_wheel_interval : mk_wheel_delay * 10)) {
174 if (mousekey_repeat != UINT8_MAX) mousekey_repeat++; 174 if (mousekey_wheel_repeat != UINT8_MAX) mousekey_wheel_repeat++;
175 mouse_report.x = 0; 175 if (tmpmr.v != 0) mouse_report.v = wheel_unit() * ((tmpmr.v > 0) ? 1 : -1);
176 mouse_report.y = 0; 176 if (tmpmr.h != 0) mouse_report.h = wheel_unit() * ((tmpmr.h > 0) ? 1 : -1);
177 if (mouse_report.v > 0) mouse_report.v = wheel_unit(); 177
178 if (mouse_report.v < 0) mouse_report.v = wheel_unit() * -1;
179 if (mouse_report.h > 0) mouse_report.h = wheel_unit();
180 if (mouse_report.h < 0) mouse_report.h = wheel_unit() * -1;
181 /* diagonal move [1/sqrt(2)] */ 178 /* diagonal move [1/sqrt(2)] */
182 if (mouse_report.v && mouse_report.h) { 179 if (mouse_report.v && mouse_report.h) {
183 mouse_report.v = times_inv_sqrt2(mouse_report.v); 180 mouse_report.v = times_inv_sqrt2(mouse_report.v);
@@ -189,10 +186,10 @@ void mousekey_task(void) {
189 mouse_report.h = 1; 186 mouse_report.h = 1;
190 } 187 }
191 } 188 }
192 mousekey_send();
193 last_timer_w = last_timer;
194 mouse_report = tmpmr;
195 } 189 }
190
191 if (mouse_report.x || mouse_report.y || mouse_report.v || mouse_report.h) mousekey_send();
192 mouse_report = tmpmr;
196} 193}
197 194
198void mousekey_on(uint8_t code) { 195void mousekey_on(uint8_t code) {
@@ -263,7 +260,8 @@ void mousekey_off(uint8_t code) {
263 mousekey_accel &= ~(1 << 1); 260 mousekey_accel &= ~(1 << 1);
264 else if (code == KC_MS_ACCEL2) 261 else if (code == KC_MS_ACCEL2)
265 mousekey_accel &= ~(1 << 2); 262 mousekey_accel &= ~(1 << 2);
266 if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0) mousekey_repeat = 0; 263 if (mouse_report.x == 0 && mouse_report.y == 0) mousekey_repeat = 0;
264 if (mouse_report.v == 0 && mouse_report.h == 0) mousekey_wheel_repeat = 0;
267} 265}
268 266
269#else /* #ifndef MK_3_SPEED */ 267#else /* #ifndef MK_3_SPEED */
@@ -285,20 +283,22 @@ uint16_t w_intervals[mkspd_COUNT] = {MK_W_INTERVAL_UNMOD, MK_W_INTERVAL_0
285void mousekey_task(void) { 283void mousekey_task(void) {
286 // report cursor and scroll movement independently 284 // report cursor and scroll movement independently
287 report_mouse_t const tmpmr = mouse_report; 285 report_mouse_t const tmpmr = mouse_report;
288 if ((mouse_report.x || mouse_report.y) && timer_elapsed(last_timer_c) > c_intervals[mk_speed]) { 286 mouse_report.x = 0;
289 mouse_report.h = 0; 287 mouse_report.y = 0;
290 mouse_report.v = 0; 288 mouse_report.v = 0;
291 mousekey_send(); 289 mouse_report.h = 0;
292 last_timer_c = last_timer; 290
293 mouse_report = tmpmr; 291 if ((tmpmr.x || tmpmr.y) && timer_elapsed(last_timer_c) > c_intervals[mk_speed]) {
292 mouse_report.x = tmpmr.x;
293 mouse_report.y = tmpmr.y;
294 } 294 }
295 if ((mouse_report.h || mouse_report.v) && timer_elapsed(last_timer_w) > w_intervals[mk_speed]) { 295 if ((tmpmr.h || tmpmr.v) && timer_elapsed(last_timer_w) > w_intervals[mk_speed]) {
296 mouse_report.x = 0; 296 mouse_report.v = tmpmr.v;
297 mouse_report.y = 0; 297 mouse_report.h = tmpmr.h;
298 mousekey_send();
299 last_timer_w = last_timer;
300 mouse_report = tmpmr;
301 } 298 }
299
300 if (mouse_report.x || mouse_report.y || mouse_report.v || mouse_report.h) mousekey_send();
301 mouse_report = tmpmr;
302} 302}
303 303
304void adjust_speed(void) { 304void adjust_speed(void) {
@@ -413,14 +413,17 @@ void mousekey_off(uint8_t code) {
413 413
414void mousekey_send(void) { 414void mousekey_send(void) {
415 mousekey_debug(); 415 mousekey_debug();
416 uint16_t time = timer_read();
417 if (mouse_report.x || mouse_report.y) last_timer_c = time;
418 if (mouse_report.v || mouse_report.h) last_timer_w = time;
416 host_mouse_send(&mouse_report); 419 host_mouse_send(&mouse_report);
417 last_timer = timer_read();
418} 420}
419 421
420void mousekey_clear(void) { 422void mousekey_clear(void) {
421 mouse_report = (report_mouse_t){}; 423 mouse_report = (report_mouse_t){};
422 mousekey_repeat = 0; 424 mousekey_repeat = 0;
423 mousekey_accel = 0; 425 mousekey_wheel_repeat = 0;
426 mousekey_accel = 0;
424} 427}
425 428
426static void mousekey_debug(void) { 429static void mousekey_debug(void) {