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>
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 100
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 = MOUSEKEY_MOVE_INIT + MOUSEKEY_MOVE_ACCEL * mousekey_repeat;
58 return (unit > 63 ? 63 : unit);
61 static inline uint8_t wheel_unit(void)
63 uint16_t unit = MOUSEKEY_WHEEL_INIT + MOUSEKEY_WHEEL_ACCEL * mousekey_repeat;
64 return (unit > 15 ? 15 : unit);
67 void mousekey_task(void)
69 if (timer_elapsed(last_timer) < MOUSEKEY_DELAY_TIME)
72 if (report.x == 0 && report.y == 0 && report.v == 0 && report.h == 0)
75 if (mousekey_repeat != UINT8_MAX)
79 if (report.x > 0) report.x = move_unit();
80 if (report.x < 0) report.x = move_unit() * -1;
81 if (report.y > 0) report.y = move_unit();
82 if (report.y < 0) report.y = move_unit() * -1;
84 if (report.x && report.y) {
89 if (report.v > 0) report.v = wheel_unit();
90 if (report.v < 0) report.v = wheel_unit() * -1;
91 if (report.h > 0) report.h = wheel_unit();
92 if (report.h < 0) report.h = wheel_unit() * -1;
97 void mousekey_on(uint8_t code)
99 if (code == KC_MS_UP) report.y = MOUSEKEY_MOVE_INIT * -1;
100 else if (code == KC_MS_DOWN) report.y = MOUSEKEY_MOVE_INIT;
101 else if (code == KC_MS_LEFT) report.x = MOUSEKEY_MOVE_INIT * -1;
102 else if (code == KC_MS_RIGHT) report.x = MOUSEKEY_MOVE_INIT;
103 else if (code == KC_MS_WH_UP) report.v = MOUSEKEY_WHEEL_INIT;
104 else if (code == KC_MS_WH_DOWN) report.v = MOUSEKEY_WHEEL_INIT * -1;
105 else if (code == KC_MS_WH_LEFT) report.h = MOUSEKEY_WHEEL_INIT * -1;
106 else if (code == KC_MS_WH_RIGHT) report.h = MOUSEKEY_WHEEL_INIT;
107 else if (code == KC_MS_BTN1) report.buttons |= MOUSE_BTN1;
108 else if (code == KC_MS_BTN2) report.buttons |= MOUSE_BTN2;
109 else if (code == KC_MS_BTN3) report.buttons |= MOUSE_BTN3;
110 else if (code == KC_MS_BTN4) report.buttons |= MOUSE_BTN4;
111 else if (code == KC_MS_BTN5) report.buttons |= MOUSE_BTN5;
114 void mousekey_off(uint8_t code)
116 if (code == KC_MS_UP && report.y < 0) report.y = 0;
117 else if (code == KC_MS_DOWN && report.y > 0) report.y = 0;
118 else if (code == KC_MS_LEFT && report.x < 0) report.x = 0;
119 else if (code == KC_MS_RIGHT && report.x > 0) report.x = 0;
120 else if (code == KC_MS_WH_UP && report.v > 0) report.v = 0;
121 else if (code == KC_MS_WH_DOWN && report.v < 0) report.v = 0;
122 else if (code == KC_MS_WH_LEFT && report.h < 0) report.h = 0;
123 else if (code == KC_MS_WH_RIGHT && report.h > 0) report.h = 0;
124 else if (code == KC_MS_BTN1) report.buttons &= ~MOUSE_BTN1;
125 else if (code == KC_MS_BTN2) report.buttons &= ~MOUSE_BTN2;
126 else if (code == KC_MS_BTN3) report.buttons &= ~MOUSE_BTN3;
127 else if (code == KC_MS_BTN4) report.buttons &= ~MOUSE_BTN4;
128 else if (code == KC_MS_BTN5) report.buttons &= ~MOUSE_BTN5;
130 if (report.x == 0 && report.y == 0 && report.v == 0 && report.h == 0)
134 void mousekey_send(void)
137 host_mouse_send(&report);
138 last_timer = timer_read();
141 void mousekey_clear(void)
143 report = (report_mouse_t){};
146 static void mousekey_debug(void)
148 if (!debug_mouse) return;
149 print("mousekey [btn|x y v h]rep: [");
150 phex(report.buttons); print("|");
151 phex(report.x); print(" ");
152 phex(report.y); print(" ");
153 phex(report.v); print(" ");
154 phex(report.h); print("]");
155 phex(mousekey_repeat);