]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/mousekey.c
Do some cleanup for the API
[qmk_firmware.git] / tmk_core / common / mousekey.c
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
27
28 static report_mouse_t mouse_report = {};
29 static uint8_t mousekey_repeat =  0;
30 static uint8_t mousekey_accel = 0;
31
32 static void mousekey_debug(void);
33
34
35 /*
36  * Mouse keys  acceleration algorithm
37  *  http://en.wikipedia.org/wiki/Mouse_keys
38  *
39  *  speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000)
40  */
41 /* milliseconds between the initial key press and first repeated motion event (0-2550) */
42 uint8_t mk_delay = MOUSEKEY_DELAY/10;
43 /* milliseconds between repeated motion events (0-255) */
44 uint8_t mk_interval = MOUSEKEY_INTERVAL;
45 /* steady speed (in action_delta units) applied each event (0-255) */
46 uint8_t mk_max_speed = MOUSEKEY_MAX_SPEED;
47 /* number of events (count) accelerating to steady speed (0-255) */
48 uint8_t mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
49 /* ramp used to reach maximum pointer speed (NOT SUPPORTED) */
50 //int8_t mk_curve = 0;
51 /* wheel params */
52 uint8_t mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
53 uint8_t mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
54
55
56 static uint16_t last_timer = 0;
57
58 inline int8_t times_inv_sqrt2(int8_t x)
59 {
60     // 181/256 is pretty close to 1/sqrt(2)
61     // 0.70703125                 0.707106781
62     // 1 too small for x=99 and x=198
63     // This ends up being a mult and discard lower 8 bits
64     return (x * 181) >> 8;
65 }
66
67 static uint8_t move_unit(void)
68 {
69     uint16_t unit;
70     if (mousekey_accel & (1<<0)) {
71         unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed)/4;
72     } else if (mousekey_accel & (1<<1)) {
73         unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed)/2;
74     } else if (mousekey_accel & (1<<2)) {
75         unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed);
76     } else if (mousekey_repeat == 0) {
77         unit = MOUSEKEY_MOVE_DELTA;
78     } else if (mousekey_repeat >= mk_time_to_max) {
79         unit = MOUSEKEY_MOVE_DELTA * mk_max_speed;
80     } else {
81         unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max;
82     }
83     return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit));
84 }
85
86 static uint8_t wheel_unit(void)
87 {
88     uint16_t unit;
89     if (mousekey_accel & (1<<0)) {
90         unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed)/4;
91     } else if (mousekey_accel & (1<<1)) {
92         unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed)/2;
93     } else if (mousekey_accel & (1<<2)) {
94         unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed);
95     } else if (mousekey_repeat == 0) {
96         unit = MOUSEKEY_WHEEL_DELTA;
97     } else if (mousekey_repeat >= mk_wheel_time_to_max) {
98         unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed;
99     } else {
100         unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max;
101     }
102     return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit));
103 }
104
105 void mousekey_task(void)
106 {
107     if (timer_elapsed(last_timer) < (mousekey_repeat ? mk_interval : mk_delay*10))
108         return;
109
110     if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0)
111         return;
112
113     if (mousekey_repeat != UINT8_MAX)
114         mousekey_repeat++;
115
116
117     if (mouse_report.x > 0) mouse_report.x = move_unit();
118     if (mouse_report.x < 0) mouse_report.x = move_unit() * -1;
119     if (mouse_report.y > 0) mouse_report.y = move_unit();
120     if (mouse_report.y < 0) mouse_report.y = move_unit() * -1;
121
122     /* diagonal move [1/sqrt(2)] */
123     if (mouse_report.x && mouse_report.y) {
124         mouse_report.x = times_inv_sqrt2(mouse_report.x);
125         mouse_report.y = times_inv_sqrt2(mouse_report.y);
126     }
127
128     if (mouse_report.v > 0) mouse_report.v = wheel_unit();
129     if (mouse_report.v < 0) mouse_report.v = wheel_unit() * -1;
130     if (mouse_report.h > 0) mouse_report.h = wheel_unit();
131     if (mouse_report.h < 0) mouse_report.h = wheel_unit() * -1;
132
133     mousekey_send();
134 }
135
136 void mousekey_on(uint8_t code)
137 {
138     if      (code == KC_MS_UP)       mouse_report.y = move_unit() * -1;
139     else if (code == KC_MS_DOWN)     mouse_report.y = move_unit();
140     else if (code == KC_MS_LEFT)     mouse_report.x = move_unit() * -1;
141     else if (code == KC_MS_RIGHT)    mouse_report.x = move_unit();
142     else if (code == KC_MS_WH_UP)    mouse_report.v = wheel_unit();
143     else if (code == KC_MS_WH_DOWN)  mouse_report.v = wheel_unit() * -1;
144     else if (code == KC_MS_WH_LEFT)  mouse_report.h = wheel_unit() * -1;
145     else if (code == KC_MS_WH_RIGHT) mouse_report.h = wheel_unit();
146     else if (code == KC_MS_BTN1)     mouse_report.buttons |= MOUSE_BTN1;
147     else if (code == KC_MS_BTN2)     mouse_report.buttons |= MOUSE_BTN2;
148     else if (code == KC_MS_BTN3)     mouse_report.buttons |= MOUSE_BTN3;
149     else if (code == KC_MS_BTN4)     mouse_report.buttons |= MOUSE_BTN4;
150     else if (code == KC_MS_BTN5)     mouse_report.buttons |= MOUSE_BTN5;
151     else if (code == KC_MS_ACCEL0)   mousekey_accel |= (1<<0);
152     else if (code == KC_MS_ACCEL1)   mousekey_accel |= (1<<1);
153     else if (code == KC_MS_ACCEL2)   mousekey_accel |= (1<<2);
154 }
155
156 void mousekey_off(uint8_t code)
157 {
158     if      (code == KC_MS_UP       && mouse_report.y < 0) mouse_report.y = 0;
159     else if (code == KC_MS_DOWN     && mouse_report.y > 0) mouse_report.y = 0;
160     else if (code == KC_MS_LEFT     && mouse_report.x < 0) mouse_report.x = 0;
161     else if (code == KC_MS_RIGHT    && mouse_report.x > 0) mouse_report.x = 0;
162     else if (code == KC_MS_WH_UP    && mouse_report.v > 0) mouse_report.v = 0;
163     else if (code == KC_MS_WH_DOWN  && mouse_report.v < 0) mouse_report.v = 0;
164     else if (code == KC_MS_WH_LEFT  && mouse_report.h < 0) mouse_report.h = 0;
165     else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0) mouse_report.h = 0;
166     else if (code == KC_MS_BTN1) mouse_report.buttons &= ~MOUSE_BTN1;
167     else if (code == KC_MS_BTN2) mouse_report.buttons &= ~MOUSE_BTN2;
168     else if (code == KC_MS_BTN3) mouse_report.buttons &= ~MOUSE_BTN3;
169     else if (code == KC_MS_BTN4) mouse_report.buttons &= ~MOUSE_BTN4;
170     else if (code == KC_MS_BTN5) mouse_report.buttons &= ~MOUSE_BTN5;
171     else if (code == KC_MS_ACCEL0) mousekey_accel &= ~(1<<0);
172     else if (code == KC_MS_ACCEL1) mousekey_accel &= ~(1<<1);
173     else if (code == KC_MS_ACCEL2) mousekey_accel &= ~(1<<2);
174
175     if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0)
176         mousekey_repeat = 0;
177 }
178
179 void mousekey_send(void)
180 {
181     mousekey_debug();
182     host_mouse_send(&mouse_report);
183     last_timer = timer_read();
184 }
185
186 void mousekey_clear(void)
187 {
188     mouse_report = (report_mouse_t){};
189     mousekey_repeat = 0;
190     mousekey_accel = 0;
191 }
192
193 static void mousekey_debug(void)
194 {
195     if (!debug_mouse) return;
196     print("mousekey [btn|x y v h](rep/acl): [");
197     phex(mouse_report.buttons); print("|");
198     print_decs(mouse_report.x); print(" ");
199     print_decs(mouse_report.y); print(" ");
200     print_decs(mouse_report.v); print(" ");
201     print_decs(mouse_report.h); print("](");
202     print_dec(mousekey_repeat); print("/");
203     print_dec(mousekey_accel); print(")\n");
204 }