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/>.
28 static report_mouse_t mouse_report
= {};
29 static uint8_t mousekey_repeat
= 0;
30 static uint8_t mousekey_accel
= 0;
32 static void mousekey_debug(void);
36 * Mouse keys acceleration algorithm
37 * http://en.wikipedia.org/wiki/Mouse_keys
39 * speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000)
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;
52 uint8_t mk_wheel_max_speed
= MOUSEKEY_WHEEL_MAX_SPEED
;
53 uint8_t mk_wheel_time_to_max
= MOUSEKEY_WHEEL_TIME_TO_MAX
;
56 static uint16_t last_timer
= 0;
59 static uint8_t move_unit(void)
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
;
73 unit
= (MOUSEKEY_MOVE_DELTA
* mk_max_speed
* mousekey_repeat
) / mk_time_to_max
;
75 return (unit
> MOUSEKEY_MOVE_MAX
? MOUSEKEY_MOVE_MAX
: (unit
== 0 ? 1 : unit
));
78 static uint8_t wheel_unit(void)
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
;
92 unit
= (MOUSEKEY_WHEEL_DELTA
* mk_wheel_max_speed
* mousekey_repeat
) / mk_wheel_time_to_max
;
94 return (unit
> MOUSEKEY_WHEEL_MAX
? MOUSEKEY_WHEEL_MAX
: (unit
== 0 ? 1 : unit
));
97 void mousekey_task(void)
99 if (timer_elapsed(last_timer
) < (mousekey_repeat
? mk_interval
: mk_delay
*10))
102 if (mouse_report
.x
== 0 && mouse_report
.y
== 0 && mouse_report
.v
== 0 && mouse_report
.h
== 0)
105 if (mousekey_repeat
!= UINT8_MAX
)
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;
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;
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;
128 void mousekey_on(uint8_t code
)
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);
148 void mousekey_off(uint8_t code
)
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);
167 if (mouse_report
.x
== 0 && mouse_report
.y
== 0 && mouse_report
.v
== 0 && mouse_report
.h
== 0)
171 void mousekey_send(void)
174 host_mouse_send(&mouse_report
);
175 last_timer
= timer_read();
178 void mousekey_clear(void)
180 mouse_report
= (report_mouse_t
){};
185 static void mousekey_debug(void)
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");