aboutsummaryrefslogtreecommitdiff
path: root/common/mousekey.c
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2012-06-07 02:25:15 +0900
committertmk <nobody@nowhere>2012-06-07 02:47:33 +0900
commitf4125707399d11a7d80587659c464b9bcddb8c56 (patch)
tree1d2a02e30f8cd103e8f4dc36629c09f6a3d44fef /common/mousekey.c
parent225de7a847a511d004bf909b1334e19497cf2f9d (diff)
downloadqmk_firmware-f4125707399d11a7d80587659c464b9bcddb8c56.tar.gz
qmk_firmware-f4125707399d11a7d80587659c464b9bcddb8c56.zip
Moved files to common, protocol and doc directory
Diffstat (limited to 'common/mousekey.c')
-rwxr-xr-xcommon/mousekey.c132
1 files changed, 132 insertions, 0 deletions
diff --git a/common/mousekey.c b/common/mousekey.c
new file mode 100755
index 000000000..76bd0fd36
--- /dev/null
+++ b/common/mousekey.c
@@ -0,0 +1,132 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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
28static report_mouse_t report;
29static report_mouse_t report_prev;
30
31static uint8_t mousekey_repeat = 0;
32
33static 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
45uint8_t mousekey_move_unit = 2;
46uint8_t mousekey_resolution = 5;
47
48
49static inline uint8_t move_unit(void)
50{
51 uint16_t unit = 5 + mousekey_repeat*2;
52 return (unit > 63 ? 63 : unit);
53}
54
55void 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
72bool mousekey_changed(void)
73{
74 return (report.buttons != report_prev.buttons ||
75 report.x || report.y || report.v || report.h);
76}
77
78void 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
112void 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
121static void mousekey_debug(void)
122{
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(" ");
129 phex(report.h);
130 phex(mousekey_repeat);
131 print("\n");
132}