]> git.donarmstrong.com Git - tmk_firmware.git/blob - common/mousekey.c
Add consumer/system control feature to LUFA.
[tmk_firmware.git] / common / mousekey.c
1 /*
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
3
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.
8
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.
13
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/>.
16 */
17
18 #include <stdint.h>
19 #include <util/delay.h>
20 #include "usb_keycodes.h"
21 #include "host.h"
22 #include "timer.h"
23 #include "print.h"
24 #include "debug.h"
25 #include "mousekey.h"
26
27
28 static report_mouse_t report;
29 static report_mouse_t report_prev;
30
31 static uint8_t mousekey_repeat =  0;
32
33 static void mousekey_debug(void);
34
35
36 /*
37  * TODO: fix acceleration algorithm
38  * see wikipedia http://en.wikipedia.org/wiki/Mouse_keys
39  */
40 #ifndef MOUSEKEY_DELAY_TIME
41 #   define MOUSEKEY_DELAY_TIME 255
42 #endif
43
44 // acceleration parameters
45 uint8_t mousekey_move_unit = 2;
46 uint8_t mousekey_resolution = 5;
47
48
49 static inline uint8_t move_unit(void)
50 {
51     uint16_t unit = 5 + mousekey_repeat*2;
52     return (unit > 63 ? 63 : unit);
53 }
54
55 void mousekey_decode(uint8_t code)
56 {
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;
70 }
71
72 bool mousekey_changed(void)
73 {
74     return (report.buttons != report_prev.buttons ||
75             report.x || report.y || report.v || report.h);
76 }
77
78 void mousekey_send(void)
79 {
80     static uint16_t last_timer = 0;
81
82     if (!mousekey_changed()) {
83         mousekey_repeat = 0;
84         mousekey_clear_report();
85         return;
86     }
87
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();
92             return;
93         }
94     }
95
96     if (mousekey_repeat != 0xFF) {
97         mousekey_repeat++;
98     }
99
100     if (report.x && report.y) {
101         report.x *= 0.7;
102         report.y *= 0.7;
103     }
104
105     mousekey_debug();
106     host_mouse_send(&report);
107     report_prev = report;
108     last_timer = timer_read();
109     mousekey_clear_report();
110 }
111
112 void mousekey_clear_report(void)
113 {
114     report.buttons = 0;
115     report.x = 0;
116     report.y = 0;
117     report.v = 0;
118     report.h = 0;
119 }
120
121 static void mousekey_debug(void)
122 {
123     if (!debug_mouse) return;
124     print("mousekey [btn|x y v h]rep: [");
125     phex(report.buttons); print("|");
126     phex(report.x); print(" ");
127     phex(report.y); print(" ");
128     phex(report.v); print(" ");
129     phex(report.h); print("]");
130     phex(mousekey_repeat);
131     print("\n");
132 }