]> git.donarmstrong.com Git - tmk_firmware.git/blob - common/mousekey.c
Fix build option MOUSEKEY_ENABLE.
[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 "keycode.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
30 static uint8_t mousekey_repeat =  0;
31
32 static void mousekey_debug(void);
33
34
35 /*
36  * TODO: fix acceleration algorithm
37  * see wikipedia http://en.wikipedia.org/wiki/Mouse_keys
38  */
39 #ifndef MOUSEKEY_DELAY_TIME
40 #   define MOUSEKEY_DELAY_TIME 100
41 #endif
42
43 #define MOUSEKEY_MOVE_INIT      5
44 #define MOUSEKEY_WHEEL_INIT     1
45 #define MOUSEKEY_MOVE_ACCEL     5
46 #define MOUSEKEY_WHEEL_ACCEL    1
47
48 static uint16_t last_timer = 0;
49
50 // acceleration parameters
51 //uint8_t mousekey_move_unit = 2;
52 //uint8_t mousekey_resolution = 5;
53
54
55 static inline uint8_t move_unit(void)
56 {
57     uint16_t unit = MOUSEKEY_MOVE_INIT + MOUSEKEY_MOVE_ACCEL * mousekey_repeat;
58     return (unit > 63 ? 63 : unit);
59 }
60
61 static inline uint8_t wheel_unit(void)
62 {
63     uint16_t unit = MOUSEKEY_WHEEL_INIT + MOUSEKEY_WHEEL_ACCEL * mousekey_repeat;
64     return (unit > 15 ? 15 : unit);
65 }
66
67 void mousekey_task(void)
68 {
69     if (timer_elapsed(last_timer) < MOUSEKEY_DELAY_TIME)
70         return;
71
72     if (report.x == 0 && report.y == 0 && report.v == 0 && report.h == 0)
73         return;
74
75     if (mousekey_repeat != UINT8_MAX)
76         mousekey_repeat++;
77
78
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;
83
84     if (report.x && report.y) {
85         report.x *= 0.7;
86         report.y *= 0.7;
87     }
88
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;
93
94     mousekey_send();
95 }
96
97 void mousekey_on(uint8_t code)
98 {
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;
112 }
113
114 void mousekey_off(uint8_t code)
115 {
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;
129
130     if (report.x == 0 && report.y == 0 && report.v == 0 && report.h == 0)
131         mousekey_repeat = 0;
132 }
133
134 void mousekey_send(void)
135 {
136     mousekey_debug();
137     host_mouse_send(&report);
138     last_timer = timer_read();
139 }
140
141 void mousekey_clear(void)
142 {
143     report = (report_mouse_t){};
144 }
145
146 static void mousekey_debug(void)
147 {
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);
156     print("\n");
157 }