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