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;
29 static report_mouse_t report_prev;
31 static uint8_t mousekey_repeat = 0;
33 static void mousekey_debug(void);
37 * TODO: fix acceleration algorithm
38 * see wikipedia http://en.wikipedia.org/wiki/Mouse_keys
40 #ifndef MOUSEKEY_DELAY_TIME
41 # define MOUSEKEY_DELAY_TIME 255
44 // acceleration parameters
45 uint8_t mousekey_move_unit = 2;
46 uint8_t mousekey_resolution = 5;
49 static inline uint8_t move_unit(void)
51 uint16_t unit = 5 + mousekey_repeat*2;
52 return (unit > 63 ? 63 : unit);
55 void mousekey_decode(uint8_t code)
57 if (code == KB_MS_UP) report.y = -move_unit();
58 else if (code == KB_MS_DOWN) report.y = move_unit();
59 else if (code == KB_MS_LEFT) report.x = -move_unit();
60 else if (code == KB_MS_RIGHT) report.x = move_unit();
61 else if (code == KB_MS_BTN1) report.buttons |= MOUSE_BTN1;
62 else if (code == KB_MS_BTN2) report.buttons |= MOUSE_BTN2;
63 else if (code == KB_MS_BTN3) report.buttons |= MOUSE_BTN3;
64 else if (code == KB_MS_BTN4) report.buttons |= MOUSE_BTN4;
65 else if (code == KB_MS_BTN5) report.buttons |= MOUSE_BTN5;
66 else if (code == KB_MS_WH_UP) report.v += move_unit()/4;
67 else if (code == KB_MS_WH_DOWN) report.v -= move_unit()/4;
68 else if (code == KB_MS_WH_LEFT) report.h -= move_unit()/4;
69 else if (code == KB_MS_WH_RIGHT)report.h += move_unit()/4;
72 bool mousekey_changed(void)
74 return (report.buttons != report_prev.buttons ||
75 report.x || report.y || report.v || report.h);
78 void mousekey_send(void)
80 static uint16_t last_timer = 0;
82 if (!mousekey_changed()) {
84 mousekey_clear_report();
88 // send immediately when buttun state is changed
89 if (report.buttons == report_prev.buttons) {
90 if (timer_elapsed(last_timer) < 100) {
91 mousekey_clear_report();
96 if (mousekey_repeat != 0xFF) {
100 if (report.x && report.y) {
106 host_mouse_send(&report);
107 report_prev = report;
108 last_timer = timer_read();
109 mousekey_clear_report();
112 void mousekey_clear_report(void)
121 static void mousekey_debug(void)
123 if (!debug_mouse) return;
124 print("mousekey[btn|x y v h]: ");
125 phex(report.buttons); print("|");
126 phex(report.x); print(" ");
127 phex(report.y); print(" ");
128 phex(report.v); print(" ");
130 phex(mousekey_repeat);