]> git.gir.st - tmk_keyboard.git/blob - common/mousekey.c
Add keycode.h and remove usb_keycodes.h.
[tmk_keyboard.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 20
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 = 5 + mousekey_repeat*4;
58 return (unit > 63 ? 63 : unit);
59 }
60
61 void mousekey_task(void)
62 {
63 if (timer_elapsed(last_timer) < MOUSEKEY_DELAY_TIME)
64 return;
65
66 if (report.x == 0 && report.y == 0 && report.v == 0 && report.h == 0)
67 return;
68
69 if (mousekey_repeat != UINT8_MAX)
70 mousekey_repeat++;
71
72
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;
77
78 if (report.x && report.y) {
79 report.x *= 0.7;
80 report.y *= 0.7;
81 }
82
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;
87
88 mousekey_send();
89 }
90
91 void mousekey_on(uint8_t code)
92 {
93 if (code == KC_MS_UP) report.y = MOUSEKEY_MOVE_INIT * -1;
94 else if (code == KC_MS_DOWN) report.y = MOUSEKEY_MOVE_INIT;
95 else if (code == KC_MS_LEFT) report.x = MOUSEKEY_MOVE_INIT * -1;
96 else if (code == KC_MS_RIGHT) report.x = MOUSEKEY_MOVE_INIT;
97 else if (code == KC_MS_WH_UP) report.v = MOUSEKEY_WHEEL_INIT;
98 else if (code == KC_MS_WH_DOWN) report.v = MOUSEKEY_WHEEL_INIT * -1;
99 else if (code == KC_MS_WH_LEFT) report.h = MOUSEKEY_WHEEL_INIT * -1;
100 else if (code == KC_MS_WH_RIGHT) report.h = MOUSEKEY_WHEEL_INIT;
101 else if (code == KC_MS_BTN1) report.buttons |= MOUSE_BTN1;
102 else if (code == KC_MS_BTN2) report.buttons |= MOUSE_BTN2;
103 else if (code == KC_MS_BTN3) report.buttons |= MOUSE_BTN3;
104 else if (code == KC_MS_BTN4) report.buttons |= MOUSE_BTN4;
105 else if (code == KC_MS_BTN5) report.buttons |= MOUSE_BTN5;
106 }
107
108 void mousekey_off(uint8_t code)
109 {
110 if (code == KC_MS_UP && report.y < 0) report.y = 0;
111 else if (code == KC_MS_DOWN && report.y > 0) report.y = 0;
112 else if (code == KC_MS_LEFT && report.x < 0) report.x = 0;
113 else if (code == KC_MS_RIGHT && report.x > 0) report.x = 0;
114 else if (code == KC_MS_WH_UP && report.v > 0) report.v = 0;
115 else if (code == KC_MS_WH_DOWN && report.v < 0) report.v = 0;
116 else if (code == KC_MS_WH_LEFT && report.h < 0) report.h = 0;
117 else if (code == KC_MS_WH_RIGHT && report.h > 0) report.h = 0;
118 else if (code == KC_MS_BTN1) report.buttons &= ~MOUSE_BTN1;
119 else if (code == KC_MS_BTN2) report.buttons &= ~MOUSE_BTN2;
120 else if (code == KC_MS_BTN3) report.buttons &= ~MOUSE_BTN3;
121 else if (code == KC_MS_BTN4) report.buttons &= ~MOUSE_BTN4;
122 else if (code == KC_MS_BTN5) report.buttons &= ~MOUSE_BTN5;
123
124 if (report.x == 0 && report.y == 0 && report.v == 0 && report.h == 0)
125 mousekey_repeat = 0;
126 }
127
128 void mousekey_send(void)
129 {
130 mousekey_debug();
131 host_mouse_send(&report);
132 last_timer = timer_read();
133 }
134
135 void mousekey_clear(void)
136 {
137 report = (report_mouse_t){};
138 }
139
140 static void mousekey_debug(void)
141 {
142 if (!debug_mouse) return;
143 print("mousekey [btn|x y v h]rep: [");
144 phex(report.buttons); print("|");
145 phex(report.x); print(" ");
146 phex(report.y); print(" ");
147 phex(report.v); print(" ");
148 phex(report.h); print("]");
149 phex(mousekey_repeat);
150 print("\n");
151 }
Imprint / Impressum