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