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>
20 #include "usb_keycodes.h"
28 static report_mouse_t report;
30 static uint8_t mousekey_repeat = 0;
32 static void mousekey_debug(void);
36 * TODO: fix acceleration algorithm
37 * see wikipedia http://en.wikipedia.org/wiki/Mouse_keys
39 #ifndef MOUSEKEY_DELAY_TIME
40 # define MOUSEKEY_DELAY_TIME 20
43 #define MOUSEKEY_MOVE_INIT 5
44 #define MOUSEKEY_WHEEL_INIT 1
45 #define MOUSEKEY_MOVE_ACCEL 5
46 #define MOUSEKEY_WHEEL_ACCEL 1
48 static uint16_t last_timer = 0;
50 // acceleration parameters
51 //uint8_t mousekey_move_unit = 2;
52 //uint8_t mousekey_resolution = 5;
55 static inline uint8_t move_unit(void)
57 uint16_t unit = 5 + mousekey_repeat*4;
58 return (unit > 63 ? 63 : unit);
61 void mousekey_task(void)
63 if (timer_elapsed(last_timer) < MOUSEKEY_DELAY_TIME)
66 if (report.x == 0 && report.y == 0 && report.v == 0 && report.h == 0)
69 if (mousekey_repeat != UINT8_MAX)
73 if (report.x > 0) report.x = move_unit();
74 if (report.x < 0) report.x = move_unit() * -1;
75 if (report.y > 0) report.y = move_unit();
76 if (report.y < 0) report.y = move_unit() * -1;
78 if (report.x && report.y) {
83 if (report.v > 0) report.v = move_unit();
84 if (report.v < 0) report.v = move_unit() * -1;
85 if (report.h > 0) report.h = move_unit();
86 if (report.h < 0) report.h = move_unit() * -1;
91 void mousekey_on(uint8_t code)
93 if (code == KB_MS_UP) report.y = MOUSEKEY_MOVE_INIT * -1;
94 else if (code == KB_MS_DOWN) report.y = MOUSEKEY_MOVE_INIT;
95 else if (code == KB_MS_LEFT) report.x = MOUSEKEY_MOVE_INIT * -1;
96 else if (code == KB_MS_RIGHT) report.x = MOUSEKEY_MOVE_INIT;
97 else if (code == KB_MS_WH_UP) report.v = MOUSEKEY_WHEEL_INIT;
98 else if (code == KB_MS_WH_DOWN) report.v = MOUSEKEY_WHEEL_INIT * -1;
99 else if (code == KB_MS_WH_LEFT) report.h = MOUSEKEY_WHEEL_INIT * -1;
100 else if (code == KB_MS_WH_RIGHT) report.h = MOUSEKEY_WHEEL_INIT;
101 else if (code == KB_MS_BTN1) report.buttons |= MOUSE_BTN1;
102 else if (code == KB_MS_BTN2) report.buttons |= MOUSE_BTN2;
103 else if (code == KB_MS_BTN3) report.buttons |= MOUSE_BTN3;
104 else if (code == KB_MS_BTN4) report.buttons |= MOUSE_BTN4;
105 else if (code == KB_MS_BTN5) report.buttons |= MOUSE_BTN5;
108 void mousekey_off(uint8_t code)
110 if (code == KB_MS_UP && report.y < 0) report.y = 0;
111 else if (code == KB_MS_DOWN && report.y > 0) report.y = 0;
112 else if (code == KB_MS_LEFT && report.x < 0) report.x = 0;
113 else if (code == KB_MS_RIGHT && report.x > 0) report.x = 0;
114 else if (code == KB_MS_WH_UP && report.v > 0) report.v = 0;
115 else if (code == KB_MS_WH_DOWN && report.v < 0) report.v = 0;
116 else if (code == KB_MS_WH_LEFT && report.h < 0) report.h = 0;
117 else if (code == KB_MS_WH_RIGHT && report.h > 0) report.h = 0;
118 else if (code == KB_MS_BTN1) report.buttons &= ~MOUSE_BTN1;
119 else if (code == KB_MS_BTN2) report.buttons &= ~MOUSE_BTN2;
120 else if (code == KB_MS_BTN3) report.buttons &= ~MOUSE_BTN3;
121 else if (code == KB_MS_BTN4) report.buttons &= ~MOUSE_BTN4;
122 else if (code == KB_MS_BTN5) report.buttons &= ~MOUSE_BTN5;
124 if (report.x == 0 && report.y == 0 && report.v == 0 && report.h == 0)
128 void mousekey_send(void)
131 host_mouse_send(&report);
132 last_timer = timer_read();
135 void mousekey_clear(void)
137 report = (report_mouse_t){};
147 static void mousekey_debug(void)
149 if (!debug_mouse) return;
150 print("mousekey [btn|x y v h]rep: [");
151 phex(report.buttons); print("|");
152 phex(report.x); print(" ");
153 phex(report.y); print(" ");
154 phex(report.v); print(" ");
155 phex(report.h); print("]");
156 phex(mousekey_repeat);