aboutsummaryrefslogtreecommitdiff
path: root/common/mousekey.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/mousekey.c')
-rw-r--r--common/mousekey.c106
1 files changed, 50 insertions, 56 deletions
diff --git a/common/mousekey.c b/common/mousekey.c
index 99e6d34ff..b8af3e59c 100644
--- a/common/mousekey.c
+++ b/common/mousekey.c
@@ -27,40 +27,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
27 27
28 28
29static uint8_t mousekey_repeat = 0; 29static uint8_t mousekey_repeat = 0;
30static uint8_t mousekey_accel = 0;
30 31
31static void mousekey_debug(void); 32static void mousekey_debug(void);
32 33
33 34
34/* max value on report descriptor */
35#define MOUSEKEY_MOVE_MAX 127
36#define MOUSEKEY_WHEEL_MAX 15
37
38#ifndef MOUSEKEY_MOVE_DELTA
39#define MOUSEKEY_MOVE_DELTA 5
40#endif
41#ifndef MOUSEKEY_WHEEL_DELTA
42#define MOUSEKEY_WHEEL_DELTA 1
43#endif
44#ifndef MOUSEKEY_DELAY
45#define MOUSEKEY_DELAY 300
46#endif
47#ifndef MOUSEKEY_INTERVAL
48#define MOUSEKEY_INTERVAL 50
49#endif
50#ifndef MOUSEKEY_MAX_SPEED
51#define MOUSEKEY_MAX_SPEED 10
52#endif
53#ifndef MOUSEKEY_TIME_TO_MAX
54#define MOUSEKEY_TIME_TO_MAX 20
55#endif
56#ifndef MOUSEKEY_WHEEL_MAX_SPEED
57#define MOUSEKEY_WHEEL_MAX_SPEED 8
58#endif
59#ifndef MOUSEKEY_WHEEL_TIME_TO_MAX
60#define MOUSEKEY_WHEEL_TIME_TO_MAX 40
61#endif
62
63
64/* 35/*
65 * Mouse keys acceleration algorithm 36 * Mouse keys acceleration algorithm
66 * http://en.wikipedia.org/wiki/Mouse_keys 37 * http://en.wikipedia.org/wiki/Mouse_keys
@@ -68,18 +39,18 @@ static void mousekey_debug(void);
68 * speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000) 39 * speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000)
69 */ 40 */
70/* milliseconds between the initial key press and first repeated motion event (0-2550) */ 41/* milliseconds between the initial key press and first repeated motion event (0-2550) */
71static uint8_t mk_delay = MOUSEKEY_DELAY/10; 42uint8_t mk_delay = MOUSEKEY_DELAY/10;
72/* milliseconds between repeated motion events (0-255) */ 43/* milliseconds between repeated motion events (0-255) */
73static uint8_t mk_interval = MOUSEKEY_INTERVAL; 44uint8_t mk_interval = MOUSEKEY_INTERVAL;
74/* steady speed (in action_delta units) applied each event (0-255) */ 45/* steady speed (in action_delta units) applied each event (0-255) */
75static uint8_t mk_max_speed = MOUSEKEY_MAX_SPEED; 46uint8_t mk_max_speed = MOUSEKEY_MAX_SPEED;
76/* number of events (count) accelerating to steady speed (0-255) */ 47/* number of events (count) accelerating to steady speed (0-255) */
77static uint8_t mk_time_to_max = MOUSEKEY_TIME_TO_MAX; 48uint8_t mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
78/* ramp used to reach maximum pointer speed (NOT SUPPORTED) */ 49/* ramp used to reach maximum pointer speed (NOT SUPPORTED) */
79//static int8_t mk_curve = 0; 50//int8_t mk_curve = 0;
80 51/* wheel params */
81static uint8_t mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED; 52uint8_t mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
82static uint8_t mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX; 53uint8_t mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
83 54
84 55
85static uint16_t last_timer = 0; 56static uint16_t last_timer = 0;
@@ -88,25 +59,39 @@ static uint16_t last_timer = 0;
88static uint8_t move_unit(void) 59static uint8_t move_unit(void)
89{ 60{
90 uint16_t unit; 61 uint16_t unit;
91 if (mousekey_repeat > mk_time_to_max) { 62 if (mousekey_accel & (1<<0)) {
63 unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed)/4;
64 } else if (mousekey_accel & (1<<1)) {
65 unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed)/2;
66 } else if (mousekey_accel & (1<<2)) {
67 unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed);
68 } else if (mousekey_repeat == 0) {
69 unit = MOUSEKEY_MOVE_DELTA;
70 } else if (mousekey_repeat >= mk_time_to_max) {
92 unit = MOUSEKEY_MOVE_DELTA * mk_max_speed; 71 unit = MOUSEKEY_MOVE_DELTA * mk_max_speed;
93 } else { 72 } else {
94 unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max; 73 unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max;
95 } 74 }
96 if (unit == 0) return 1; 75 return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit));
97 return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : unit);
98} 76}
99 77
100static uint8_t wheel_unit(void) 78static uint8_t wheel_unit(void)
101{ 79{
102 uint16_t unit; 80 uint16_t unit;
103 if (mousekey_repeat > mk_time_to_max) { 81 if (mousekey_accel & (1<<0)) {
82 unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed)/4;
83 } else if (mousekey_accel & (1<<1)) {
84 unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed)/2;
85 } else if (mousekey_accel & (1<<2)) {
86 unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed);
87 } else if (mousekey_repeat == 0) {
88 unit = MOUSEKEY_WHEEL_DELTA;
89 } else if (mousekey_repeat >= mk_time_to_max) {
104 unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed; 90 unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed;
105 } else { 91 } else {
106 unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max; 92 unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max;
107 } 93 }
108 if (unit == 0) return 1; 94 return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit));
109 return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : unit);
110} 95}
111 96
112void mousekey_task(void) 97void mousekey_task(void)
@@ -126,6 +111,7 @@ void mousekey_task(void)
126 if (mouse_report.y > 0) mouse_report.y = move_unit(); 111 if (mouse_report.y > 0) mouse_report.y = move_unit();
127 if (mouse_report.y < 0) mouse_report.y = move_unit() * -1; 112 if (mouse_report.y < 0) mouse_report.y = move_unit() * -1;
128 113
114 /* diagonal move [1/sqrt(2) = 0.7] */
129 if (mouse_report.x && mouse_report.y) { 115 if (mouse_report.x && mouse_report.y) {
130 mouse_report.x *= 0.7; 116 mouse_report.x *= 0.7;
131 mouse_report.y *= 0.7; 117 mouse_report.y *= 0.7;
@@ -141,19 +127,22 @@ void mousekey_task(void)
141 127
142void mousekey_on(uint8_t code) 128void mousekey_on(uint8_t code)
143{ 129{
144 if (code == KC_MS_UP) mouse_report.y = MOUSEKEY_MOVE_DELTA * -1; 130 if (code == KC_MS_UP) mouse_report.y = move_unit() * -1;
145 else if (code == KC_MS_DOWN) mouse_report.y = MOUSEKEY_MOVE_DELTA; 131 else if (code == KC_MS_DOWN) mouse_report.y = move_unit();
146 else if (code == KC_MS_LEFT) mouse_report.x = MOUSEKEY_MOVE_DELTA * -1; 132 else if (code == KC_MS_LEFT) mouse_report.x = move_unit() * -1;
147 else if (code == KC_MS_RIGHT) mouse_report.x = MOUSEKEY_MOVE_DELTA; 133 else if (code == KC_MS_RIGHT) mouse_report.x = move_unit();
148 else if (code == KC_MS_WH_UP) mouse_report.v = MOUSEKEY_WHEEL_DELTA; 134 else if (code == KC_MS_WH_UP) mouse_report.v = wheel_unit();
149 else if (code == KC_MS_WH_DOWN) mouse_report.v = MOUSEKEY_WHEEL_DELTA * -1; 135 else if (code == KC_MS_WH_DOWN) mouse_report.v = wheel_unit() * -1;
150 else if (code == KC_MS_WH_LEFT) mouse_report.h = MOUSEKEY_WHEEL_DELTA * -1; 136 else if (code == KC_MS_WH_LEFT) mouse_report.h = wheel_unit() * -1;
151 else if (code == KC_MS_WH_RIGHT) mouse_report.h = MOUSEKEY_WHEEL_DELTA; 137 else if (code == KC_MS_WH_RIGHT) mouse_report.h = wheel_unit();
152 else if (code == KC_MS_BTN1) mouse_report.buttons |= MOUSE_BTN1; 138 else if (code == KC_MS_BTN1) mouse_report.buttons |= MOUSE_BTN1;
153 else if (code == KC_MS_BTN2) mouse_report.buttons |= MOUSE_BTN2; 139 else if (code == KC_MS_BTN2) mouse_report.buttons |= MOUSE_BTN2;
154 else if (code == KC_MS_BTN3) mouse_report.buttons |= MOUSE_BTN3; 140 else if (code == KC_MS_BTN3) mouse_report.buttons |= MOUSE_BTN3;
155 else if (code == KC_MS_BTN4) mouse_report.buttons |= MOUSE_BTN4; 141 else if (code == KC_MS_BTN4) mouse_report.buttons |= MOUSE_BTN4;
156 else if (code == KC_MS_BTN5) mouse_report.buttons |= MOUSE_BTN5; 142 else if (code == KC_MS_BTN5) mouse_report.buttons |= MOUSE_BTN5;
143 else if (code == KC_MS_ACCEL0) mousekey_accel |= (1<<0);
144 else if (code == KC_MS_ACCEL1) mousekey_accel |= (1<<1);
145 else if (code == KC_MS_ACCEL2) mousekey_accel |= (1<<2);
157} 146}
158 147
159void mousekey_off(uint8_t code) 148void mousekey_off(uint8_t code)
@@ -171,6 +160,9 @@ void mousekey_off(uint8_t code)
171 else if (code == KC_MS_BTN3) mouse_report.buttons &= ~MOUSE_BTN3; 160 else if (code == KC_MS_BTN3) mouse_report.buttons &= ~MOUSE_BTN3;
172 else if (code == KC_MS_BTN4) mouse_report.buttons &= ~MOUSE_BTN4; 161 else if (code == KC_MS_BTN4) mouse_report.buttons &= ~MOUSE_BTN4;
173 else if (code == KC_MS_BTN5) mouse_report.buttons &= ~MOUSE_BTN5; 162 else if (code == KC_MS_BTN5) mouse_report.buttons &= ~MOUSE_BTN5;
163 else if (code == KC_MS_ACCEL0) mousekey_accel &= ~(1<<0);
164 else if (code == KC_MS_ACCEL1) mousekey_accel &= ~(1<<1);
165 else if (code == KC_MS_ACCEL2) mousekey_accel &= ~(1<<2);
174 166
175 if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0) 167 if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0)
176 mousekey_repeat = 0; 168 mousekey_repeat = 0;
@@ -186,17 +178,19 @@ void mousekey_send(void)
186void mousekey_clear(void) 178void mousekey_clear(void)
187{ 179{
188 mouse_report = (report_mouse_t){}; 180 mouse_report = (report_mouse_t){};
181 mousekey_repeat = 0;
182 mousekey_accel = 0;
189} 183}
190 184
191static void mousekey_debug(void) 185static void mousekey_debug(void)
192{ 186{
193 if (!debug_mouse) return; 187 if (!debug_mouse) return;
194 print("mousekey [btn|x y v h]rep: ["); 188 print("mousekey [btn|x y v h](rep/acl): [");
195 phex(mouse_report.buttons); print("|"); 189 phex(mouse_report.buttons); print("|");
196 phex(mouse_report.x); print(" "); 190 phex(mouse_report.x); print(" ");
197 phex(mouse_report.y); print(" "); 191 phex(mouse_report.y); print(" ");
198 phex(mouse_report.v); print(" "); 192 phex(mouse_report.v); print(" ");
199 phex(mouse_report.h); print("]"); 193 phex(mouse_report.h); print("](");
200 phex(mousekey_repeat); 194 phex(mousekey_repeat); print("/");
201 print("\n"); 195 phex(mousekey_accel); print(")\n");
202} 196}