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