2 Copyright 2011 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
19 #include <util/delay.h>
29 static uint8_t mousekey_repeat
= 0;
31 static void mousekey_debug(void);
34 /* max value on report descriptor */
35 #define MOUSEKEY_MOVE_MAX 127
36 #define MOUSEKEY_WHEEL_MAX 15
38 #ifndef MOUSEKEY_MOVE_DELTA
39 #define MOUSEKEY_MOVE_DELTA 5
41 #ifndef MOUSEKEY_WHEEL_DELTA
42 #define MOUSEKEY_WHEEL_DELTA 1
44 #ifndef MOUSEKEY_DELAY
45 #define MOUSEKEY_DELAY 300
47 #ifndef MOUSEKEY_INTERVAL
48 #define MOUSEKEY_INTERVAL 50
50 #ifndef MOUSEKEY_MAX_SPEED
51 #define MOUSEKEY_MAX_SPEED 10
53 #ifndef MOUSEKEY_TIME_TO_MAX
54 #define MOUSEKEY_TIME_TO_MAX 20
56 #ifndef MOUSEKEY_WHEEL_MAX_SPEED
57 #define MOUSEKEY_WHEEL_MAX_SPEED 8
59 #ifndef MOUSEKEY_WHEEL_TIME_TO_MAX
60 #define MOUSEKEY_WHEEL_TIME_TO_MAX 40
65 * Mouse keys acceleration algorithm
66 * http://en.wikipedia.org/wiki/Mouse_keys
68 * speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000)
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;
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
;
85 static uint16_t last_timer
= 0;
88 static uint8_t move_unit(void)
91 if (mousekey_repeat
> mk_time_to_max
) {
92 unit
= MOUSEKEY_MOVE_DELTA
* mk_max_speed
;
94 unit
= (MOUSEKEY_MOVE_DELTA
* mk_max_speed
* mousekey_repeat
) / mk_time_to_max
;
96 if (unit
== 0) return 1;
97 return (unit
> MOUSEKEY_MOVE_MAX
? MOUSEKEY_MOVE_MAX
: unit
);
100 static uint8_t wheel_unit(void)
103 if (mousekey_repeat
> mk_time_to_max
) {
104 unit
= MOUSEKEY_WHEEL_DELTA
* mk_wheel_max_speed
;
106 unit
= (MOUSEKEY_WHEEL_DELTA
* mk_wheel_max_speed
* mousekey_repeat
) / mk_wheel_time_to_max
;
108 if (unit
== 0) return 1;
109 return (unit
> MOUSEKEY_WHEEL_MAX
? MOUSEKEY_WHEEL_MAX
: unit
);
112 void mousekey_task(void)
114 if (timer_elapsed(last_timer
) < (mousekey_repeat
? mk_interval
: mk_delay
*10))
117 if (mouse_report
.x
== 0 && mouse_report
.y
== 0 && mouse_report
.v
== 0 && mouse_report
.h
== 0)
120 if (mousekey_repeat
!= UINT8_MAX
)
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;
129 if (mouse_report
.x
&& mouse_report
.y
) {
130 mouse_report
.x
*= 0.7;
131 mouse_report
.y
*= 0.7;
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;
142 void mousekey_on(uint8_t code
)
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
;
159 void mousekey_off(uint8_t code
)
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
;
175 if (mouse_report
.x
== 0 && mouse_report
.y
== 0 && mouse_report
.v
== 0 && mouse_report
.h
== 0)
179 void mousekey_send(void)
182 host_mouse_send(&mouse_report
);
183 last_timer
= timer_read();
186 void mousekey_clear(void)
188 mouse_report
= (report_mouse_t
){};
191 static void mousekey_debug(void)
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
);