diff options
Diffstat (limited to 'tmk_core/common/mousekey.c')
-rw-r--r-- | tmk_core/common/mousekey.c | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/tmk_core/common/mousekey.c b/tmk_core/common/mousekey.c new file mode 100644 index 000000000..23469476e --- /dev/null +++ b/tmk_core/common/mousekey.c | |||
@@ -0,0 +1,196 @@ | |||
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 | |||
59 | static uint8_t move_unit(void) | ||
60 | { | ||
61 | uint16_t unit; | ||
62 | if (mousekey_accel & (1<<0)) { | ||
63 | unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed)/4; | ||
64 | } else if (mousekey_accel & (1<<1)) { | ||
65 | unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed)/2; | ||
66 | } else if (mousekey_accel & (1<<2)) { | ||
67 | unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed); | ||
68 | } else if (mousekey_repeat == 0) { | ||
69 | unit = MOUSEKEY_MOVE_DELTA; | ||
70 | } else if (mousekey_repeat >= mk_time_to_max) { | ||
71 | unit = MOUSEKEY_MOVE_DELTA * mk_max_speed; | ||
72 | } else { | ||
73 | unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max; | ||
74 | } | ||
75 | return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit)); | ||
76 | } | ||
77 | |||
78 | static uint8_t wheel_unit(void) | ||
79 | { | ||
80 | uint16_t unit; | ||
81 | if (mousekey_accel & (1<<0)) { | ||
82 | unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed)/4; | ||
83 | } else if (mousekey_accel & (1<<1)) { | ||
84 | unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed)/2; | ||
85 | } else if (mousekey_accel & (1<<2)) { | ||
86 | unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed); | ||
87 | } else if (mousekey_repeat == 0) { | ||
88 | unit = MOUSEKEY_WHEEL_DELTA; | ||
89 | } else if (mousekey_repeat >= mk_wheel_time_to_max) { | ||
90 | unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed; | ||
91 | } else { | ||
92 | unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max; | ||
93 | } | ||
94 | return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit)); | ||
95 | } | ||
96 | |||
97 | void mousekey_task(void) | ||
98 | { | ||
99 | if (timer_elapsed(last_timer) < (mousekey_repeat ? mk_interval : mk_delay*10)) | ||
100 | return; | ||
101 | |||
102 | if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0) | ||
103 | return; | ||
104 | |||
105 | if (mousekey_repeat != UINT8_MAX) | ||
106 | mousekey_repeat++; | ||
107 | |||
108 | |||
109 | if (mouse_report.x > 0) mouse_report.x = move_unit(); | ||
110 | if (mouse_report.x < 0) mouse_report.x = move_unit() * -1; | ||
111 | if (mouse_report.y > 0) mouse_report.y = move_unit(); | ||
112 | if (mouse_report.y < 0) mouse_report.y = move_unit() * -1; | ||
113 | |||
114 | /* diagonal move [1/sqrt(2) = 0.7] */ | ||
115 | if (mouse_report.x && mouse_report.y) { | ||
116 | mouse_report.x *= 0.7; | ||
117 | mouse_report.y *= 0.7; | ||
118 | } | ||
119 | |||
120 | if (mouse_report.v > 0) mouse_report.v = wheel_unit(); | ||
121 | if (mouse_report.v < 0) mouse_report.v = wheel_unit() * -1; | ||
122 | if (mouse_report.h > 0) mouse_report.h = wheel_unit(); | ||
123 | if (mouse_report.h < 0) mouse_report.h = wheel_unit() * -1; | ||
124 | |||
125 | mousekey_send(); | ||
126 | } | ||
127 | |||
128 | void mousekey_on(uint8_t code) | ||
129 | { | ||
130 | if (code == KC_MS_UP) mouse_report.y = move_unit() * -1; | ||
131 | else if (code == KC_MS_DOWN) mouse_report.y = move_unit(); | ||
132 | else if (code == KC_MS_LEFT) mouse_report.x = move_unit() * -1; | ||
133 | else if (code == KC_MS_RIGHT) mouse_report.x = move_unit(); | ||
134 | else if (code == KC_MS_WH_UP) mouse_report.v = wheel_unit(); | ||
135 | else if (code == KC_MS_WH_DOWN) mouse_report.v = wheel_unit() * -1; | ||
136 | else if (code == KC_MS_WH_LEFT) mouse_report.h = wheel_unit() * -1; | ||
137 | else if (code == KC_MS_WH_RIGHT) mouse_report.h = wheel_unit(); | ||
138 | else if (code == KC_MS_BTN1) mouse_report.buttons |= MOUSE_BTN1; | ||
139 | else if (code == KC_MS_BTN2) mouse_report.buttons |= MOUSE_BTN2; | ||
140 | else if (code == KC_MS_BTN3) mouse_report.buttons |= MOUSE_BTN3; | ||
141 | else if (code == KC_MS_BTN4) mouse_report.buttons |= MOUSE_BTN4; | ||
142 | else if (code == KC_MS_BTN5) mouse_report.buttons |= MOUSE_BTN5; | ||
143 | else if (code == KC_MS_ACCEL0) mousekey_accel |= (1<<0); | ||
144 | else if (code == KC_MS_ACCEL1) mousekey_accel |= (1<<1); | ||
145 | else if (code == KC_MS_ACCEL2) mousekey_accel |= (1<<2); | ||
146 | } | ||
147 | |||
148 | void mousekey_off(uint8_t code) | ||
149 | { | ||
150 | if (code == KC_MS_UP && mouse_report.y < 0) mouse_report.y = 0; | ||
151 | else if (code == KC_MS_DOWN && mouse_report.y > 0) mouse_report.y = 0; | ||
152 | else if (code == KC_MS_LEFT && mouse_report.x < 0) mouse_report.x = 0; | ||
153 | else if (code == KC_MS_RIGHT && mouse_report.x > 0) mouse_report.x = 0; | ||
154 | else if (code == KC_MS_WH_UP && mouse_report.v > 0) mouse_report.v = 0; | ||
155 | else if (code == KC_MS_WH_DOWN && mouse_report.v < 0) mouse_report.v = 0; | ||
156 | else if (code == KC_MS_WH_LEFT && mouse_report.h < 0) mouse_report.h = 0; | ||
157 | else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0) mouse_report.h = 0; | ||
158 | else if (code == KC_MS_BTN1) mouse_report.buttons &= ~MOUSE_BTN1; | ||
159 | else if (code == KC_MS_BTN2) mouse_report.buttons &= ~MOUSE_BTN2; | ||
160 | else if (code == KC_MS_BTN3) mouse_report.buttons &= ~MOUSE_BTN3; | ||
161 | else if (code == KC_MS_BTN4) mouse_report.buttons &= ~MOUSE_BTN4; | ||
162 | else if (code == KC_MS_BTN5) mouse_report.buttons &= ~MOUSE_BTN5; | ||
163 | else if (code == KC_MS_ACCEL0) mousekey_accel &= ~(1<<0); | ||
164 | else if (code == KC_MS_ACCEL1) mousekey_accel &= ~(1<<1); | ||
165 | else if (code == KC_MS_ACCEL2) mousekey_accel &= ~(1<<2); | ||
166 | |||
167 | if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0) | ||
168 | mousekey_repeat = 0; | ||
169 | } | ||
170 | |||
171 | void mousekey_send(void) | ||
172 | { | ||
173 | mousekey_debug(); | ||
174 | host_mouse_send(&mouse_report); | ||
175 | last_timer = timer_read(); | ||
176 | } | ||
177 | |||
178 | void mousekey_clear(void) | ||
179 | { | ||
180 | mouse_report = (report_mouse_t){}; | ||
181 | mousekey_repeat = 0; | ||
182 | mousekey_accel = 0; | ||
183 | } | ||
184 | |||
185 | static void mousekey_debug(void) | ||
186 | { | ||
187 | if (!debug_mouse) return; | ||
188 | print("mousekey [btn|x y v h](rep/acl): ["); | ||
189 | phex(mouse_report.buttons); print("|"); | ||
190 | print_decs(mouse_report.x); print(" "); | ||
191 | print_decs(mouse_report.y); print(" "); | ||
192 | print_decs(mouse_report.v); print(" "); | ||
193 | print_decs(mouse_report.h); print("]("); | ||
194 | print_dec(mousekey_repeat); print("/"); | ||
195 | print_dec(mousekey_accel); print(")\n"); | ||
196 | } | ||