aboutsummaryrefslogtreecommitdiffstats
path: root/tmk_core/common/mousekey.c
blob: 42bf231f4c19d600101fa4b0aade55d4fcdb6ff6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight { background: #ffffff; }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#include <xen/config.h>
#include <xen/init.h>
#include <xen/kernel.h>
#include <xen/lib.h>
#include <xen/domain.h>
#include <xen/sched.h>
#include <xen/trace.h>

#ifndef __x86_64__
#undef TRC_PV_64_FLAG
#define TRC_PV_64_FLAG 0
#endif

asmlinkage void trace_hypercall(void)
{
    struct cpu_user_regs *regs = guest_cpu_user_regs();

    if ( !tb_init_done )
        return;

#ifdef __x86_64__
    ifpre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight { background: #ffffff; }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/*
 * Copyright 2011 Jun Wako <wakojun@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdint.h>
#include "keycode.h"
#include "host.h"
#include "timer.h"
#include "print.h"
#include "debug.h"
#include "mousekey.h"

inline int8_t times_inv_sqrt2(int8_t x) {
    // 181/256 is pretty close to 1/sqrt(2)
    // 0.70703125                 0.707106781
    // 1 too small for x=99 and x=198
    // This ends up being a mult and discard lower 8 bits
    return (x * 181) >> 8;
}

static report_mouse_t mouse_report = {0};
static void           mousekey_debug(void);
static uint8_t        mousekey_accel  = 0;
static uint8_t        mousekey_repeat = 0;
static uint16_t       last_timer      = 0;

#ifndef MK_3_SPEED

static uint16_t last_timer_c = 0;
static uint16_t last_timer_w = 0;

/*
 * Mouse keys  acceleration algorithm
 *  http://en.wikipedia.org/wiki/Mouse_keys
 *
 *  speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000)
 */
/* milliseconds between the initial key press and first repeated motion event (0-2550) */
uint8_t mk_delay = MOUSEKEY_DELAY / 10;
/* milliseconds between repeated motion events (0-255) */
uint8_t mk_interval = MOUSEKEY_INTERVAL;
/* steady speed (in action_delta units) applied each event (0-255) */
uint8_t mk_max_speed = MOUSEKEY_MAX_SPEED;
/* number of events (count) accelerating to steady speed (0-255) */
uint8_t mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
/* ramp used to reach maximum pointer speed (NOT SUPPORTED) */
// int8_t mk_curve = 0;
/* wheel params */
/* milliseconds between the initial key press and first repeated motion event (0-2550) */
uint8_t mk_wheel_delay = MOUSEKEY_WHEEL_DELAY / 10;
/* milliseconds between repeated motion events (0-255) */
uint8_t mk_wheel_interval    = MOUSEKEY_WHEEL_INTERVAL;
uint8_t mk_wheel_max_speed   = MOUSEKEY_WHEEL_MAX_SPEED;
uint8_t mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;

#    ifndef MK_COMBINED

static uint8_t move_unit(void) {
    uint16_t unit;
    if (mousekey_accel & (1 << 0)) {
        unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 4;
    } else if (mousekey_accel & (1 << 1)) {
        unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 2;
    } else if (mousekey_accel & (1 << 2)) {
        unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed);
    } else if (mousekey_repeat == 0) {
        unit = MOUSEKEY_MOVE_DELTA;
    } else if (mousekey_repeat >= mk_time_to_max) {
        unit = MOUSEKEY_MOVE_DELTA * mk_max_speed;
    } else {
        unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max;
    }
    return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit));
}

static uint8_t wheel_unit(void) {
    uint16_t unit;
    if (mousekey_accel & (1 << 0)) {
        unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 4;
    } else if (mousekey_accel & (1 << 1)) {
        unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 2;
    } else if (mousekey_accel & (1 << 2)) {
        unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed);
    } else if (mousekey_repeat == 0) {
        unit = MOUSEKEY_WHEEL_DELTA;
    } else if (mousekey_repeat >= mk_wheel_time_to_max) {
        unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed;
    } else {
        unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max;
    }
    return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit));
}

#    else /* #ifndef MK_COMBINED */

static uint8_t move_unit(void) {
    uint16_t unit;
    if (mousekey_accel & (1 << 0)) {
        unit = 1;
    } else if (mousekey_accel & (1 << 1)) {
        unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 2;
    } else if (mousekey_accel & (1 << 2)) {
        unit = MOUSEKEY_MOVE_MAX;
    } else if (mousekey_repeat == 0) {
        unit = MOUSEKEY_MOVE_DELTA;
    } else if (mousekey_repeat >= mk_time_to_max) {
        unit = MOUSEKEY_MOVE_DELTA * mk_max_speed;
    } else {
        unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max;
    }
    return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit));
}

static uint8_t wheel_unit(void) {
    uint16_t unit;
    if (mousekey_accel & (1 << 0)) {
        unit = 1;
    } else if (mousekey_accel & (1 << 1)) {
        unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 2;
    } else if (mousekey_accel & (1 << 2)) {
        unit = 
{ unit = MOUSEKEY_WHEEL_DELTA; } else if (mousekey_repeat >= mk_wheel_time_to_max) { unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed; } else { unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max; } return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit)); } # endif /* #ifndef MK_COMBINED */ void mousekey_task(void) { // report cursor and scroll movement independently report_mouse_t const tmpmr = mouse_report; if ((mouse_report.x || mouse_report.y) && timer_elapsed(last_timer_c) > (mousekey_repeat ? mk_interval : mk_delay * 10)) { if (mousekey_repeat != UINT8_MAX) mousekey_repeat++; mouse_report.v = 0; mouse_report.h = 0; if (mouse_report.x > 0) mouse_report.x = move_unit(); if (mouse_report.x < 0) mouse_report.x = move_unit() * -1; if (mouse_report.y > 0) mouse_report.y = move_unit(); if (mouse_report.y < 0) mouse_report.y = move_unit() * -1; /* diagonal move [1/sqrt(2)] */ if (mouse_report.x && mouse_report.y) { mouse_report.x = times_inv_sqrt2(mouse_report.x); if (mouse_report.x == 0) { mouse_report.x = 1; } mouse_report.y = times_inv_sqrt2(mouse_report.y); if (mouse_report.y == 0) { mouse_report.y = 1; } } mousekey_send(); last_timer_c = last_timer; mouse_report = tmpmr; } if ((mouse_report.v || mouse_report.h) && timer_elapsed(last_timer_w) > (mousekey_repeat ? mk_wheel_interval : mk_wheel_delay * 10)) { if (mousekey_repeat != UINT8_MAX) mousekey_repeat++; mouse_report.x = 0; mouse_report.y = 0; if (mouse_report.v > 0) mouse_report.v = wheel_unit(); if (mouse_report.v < 0) mouse_report.v = wheel_unit() * -1; if (mouse_report.h > 0) mouse_report.h = wheel_unit(); if (mouse_report.h < 0) mouse_report.h = wheel_unit() * -1; /* diagonal move [1/sqrt(2)] */ if (mouse_report.v && mouse_report.h) { mouse_report.v = times_inv_sqrt2(mouse_report.v); if (mouse_report.v == 0) { mouse_report.v = 1; } mouse_report.h = times_inv_sqrt2(mouse_report.h); if (mouse_report.h == 0) { mouse_report.h = 1; } } mousekey_send(); last_timer_w = last_timer; mouse_report = tmpmr; } } void mousekey_on(uint8_t code) { if (code == KC_MS_UP) mouse_report.y = move_unit() * -1; else if (code == KC_MS_DOWN) mouse_report.y = move_unit(); else if (code == KC_MS_LEFT) mouse_report.x = move_unit() * -1; else if (code == KC_MS_RIGHT) mouse_report.x = move_unit(); else if (code == KC_MS_WH_UP) mouse_report.v = wheel_unit(); else if (code == KC_MS_WH_DOWN) mouse_report.v = wheel_unit() * -1; else if (code == KC_MS_WH_LEFT) mouse_report.h = wheel_unit() * -1; else if (code == KC_MS_WH_RIGHT) mouse_report.h = wheel_unit(); else if (code == KC_MS_BTN1) mouse_report.buttons |= MOUSE_BTN1; else if (code == KC_MS_BTN2) mouse_report.buttons |= MOUSE_BTN2; else if (code == KC_MS_BTN3) mouse_report.buttons |= MOUSE_BTN3; else if (code == KC_MS_BTN4) mouse_report.buttons |= MOUSE_BTN4; else if (code == KC_MS_BTN5) mouse_report.buttons |= MOUSE_BTN5; else if (code == KC_MS_ACCEL0) mousekey_accel |= (1 << 0); else if (code == KC_MS_ACCEL1) mousekey_accel |= (1 << 1); else if (code == KC_MS_ACCEL2) mousekey_accel |= (1 << 2); } void mousekey_off(uint8_t code) { if (code == KC_MS_UP && mouse_report.y < 0) mouse_report.y = 0; else if (code == KC_MS_DOWN && mouse_report.y > 0) mouse_report.y = 0; else if (code == KC_MS_LEFT && mouse_report.x < 0) mouse_report.x = 0; else if (code == KC_MS_RIGHT && mouse_report.x > 0) mouse_report.x = 0; else if (code == KC_MS_WH_UP && mouse_report.v > 0) mouse_report.v = 0; else if (code == KC_MS_WH_DOWN && mouse_report.v < 0) mouse_report.v = 0; else if (code == KC_MS_WH_LEFT && mouse_report.h < 0) mouse_report.h = 0; else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0) mouse_report.h = 0; else if (code == KC_MS_BTN1) mouse_report.buttons &= ~MOUSE_BTN1; else if (code == KC_MS_BTN2) mouse_report.buttons &= ~MOUSE_BTN2; else if (code == KC_MS_BTN3) mouse_report.buttons &= ~MOUSE_BTN3; else if (code == KC_MS_BTN4) mouse_report.buttons &= ~MOUSE_BTN4; else if (code == KC_MS_BTN5) mouse_report.buttons &= ~MOUSE_BTN5; else if (code == KC_MS_ACCEL0) mousekey_accel &= ~(1 << 0); else if (code == KC_MS_ACCEL1) mousekey_accel &= ~(1 << 1); else if (code == KC_MS_ACCEL2) mousekey_accel &= ~(1 << 2); if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0) mousekey_repeat = 0; } #else /* #ifndef MK_3_SPEED */ enum { mkspd_unmod, mkspd_0, mkspd_1, mkspd_2, mkspd_COUNT }; # ifndef MK_MOMENTARY_ACCEL static uint8_t mk_speed = mkspd_1; # else static uint8_t mk_speed = mkspd_unmod; static uint8_t mkspd_DEFAULT = mkspd_unmod; # endif static uint16_t last_timer_c = 0; static uint16_t last_timer_w = 0; uint16_t c_offsets[mkspd_COUNT] = {MK_C_OFFSET_UNMOD, MK_C_OFFSET_0, MK_C_OFFSET_1, MK_C_OFFSET_2}; uint16_t c_intervals[mkspd_COUNT] = {MK_C_INTERVAL_UNMOD, MK_C_INTERVAL_0, MK_C_INTERVAL_1, MK_C_INTERVAL_2}; uint16_t w_offsets[mkspd_COUNT] = {MK_W_OFFSET_UNMOD, MK_W_OFFSET_0, MK_W_OFFSET_1, MK_W_OFFSET_2}; uint16_t w_intervals[mkspd_COUNT] = {MK_W_INTERVAL_UNMOD, MK_W_INTERVAL_0, MK_W_INTERVAL_1, MK_W_INTERVAL_2}; void mousekey_task(void) { // report cursor and scroll movement independently report_mouse_t const tmpmr = mouse_report; if ((mouse_report.x || mouse_report.y) && timer_elapsed(last_timer_c) > c_intervals[mk_speed]) { mouse_report.h = 0; mouse_report.v = 0; mousekey_send(); last_timer_c = last_timer; mouse_report = tmpmr; } if ((mouse_report.h || mouse_report.v) && timer_elapsed(last_timer_w) > w_intervals[mk_speed]) { mouse_report.x = 0; mouse_report.y = 0; mousekey_send(); last_timer_w = last_timer; mouse_report = tmpmr; } } void adjust_speed(void) { uint16_t const c_offset = c_offsets[mk_speed]; uint16_t const w_offset = w_offsets[mk_speed]; if (mouse_report.x > 0) mouse_report.x = c_offset; if (mouse_report.x < 0) mouse_report.x = c_offset * -1; if (mouse_report.y > 0) mouse_report.y = c_offset; if (mouse_report.y < 0) mouse_report.y = c_offset * -1; if (mouse_report.h > 0) mouse_report.h = w_offset; if (mouse_report.h < 0) mouse_report.h = w_offset * -1; if (mouse_report.v > 0) mouse_report.v = w_offset; if (mouse_report.v < 0) mouse_report.v = w_offset * -1; // adjust for diagonals if (mouse_report.x && mouse_report.y) { mouse_report.x = times_inv_sqrt2(mouse_report.x); if (mouse_report.x == 0) { mouse_report.x = 1; } mouse_report.y = times_inv_sqrt2(mouse_report.y); if (mouse_report.y == 0) { mouse_report.y = 1; } } if (mouse_report.h && mouse_report.v) { mouse_report.h = times_inv_sqrt2(mouse_report.h); mouse_report.v = times_inv_sqrt2(mouse_report.v); } } void mousekey_on(uint8_t code) { uint16_t const c_offset = c_offsets[mk_speed]; uint16_t const w_offset = w_offsets[mk_speed]; uint8_t const old_speed = mk_speed; if (code == KC_MS_UP) mouse_report.y = c_offset * -1; else if (code == KC_MS_DOWN) mouse_report.y = c_offset; else if (code == KC_MS_LEFT) mouse_report.x = c_offset * -1; else if (code == KC_MS_RIGHT) mouse_report.x = c_offset; else if (code == KC_MS_WH_UP) mouse_report.v = w_offset; else if (code == KC_MS_WH_DOWN) mouse_report.v = w_offset * -1; else if (code == KC_MS_WH_LEFT) mouse_report.h = w_offset * -1; else if (code == KC_MS_WH_RIGHT) mouse_report.h = w_offset; else if (code == KC_MS_BTN1) mouse_report.buttons |= MOUSE_BTN1; else if (code == KC_MS_BTN2) mouse_report.buttons |= MOUSE_BTN2; else if (code == KC_MS_BTN3) mouse_report.buttons |= MOUSE_BTN3; else if (code == KC_MS_BTN4) mouse_report.buttons |= MOUSE_BTN4; else if (code == KC_MS_BTN5) mouse_report.buttons |= MOUSE_BTN5; else if (code == KC_MS_ACCEL0) mk_speed = mkspd_0; else if (code == KC_MS_ACCEL1) mk_speed = mkspd_1; else if (code == KC_MS_ACCEL2) mk_speed = mkspd_2; if (mk_speed != old_speed) adjust_speed(); } void mousekey_off(uint8_t code) { # ifdef MK_MOMENTARY_ACCEL uint8_t const old_speed = mk_speed; # endif if (code == KC_MS_UP && mouse_report.y < 0) mouse_report.y = 0; else if (code == KC_MS_DOWN && mouse_report.y > 0) mouse_report.y = 0; else if (code == KC_MS_LEFT && mouse_report.x < 0) mouse_report.x = 0; else if (code == KC_MS_RIGHT && mouse_report.x > 0) mouse_report.x = 0; else if (code == KC_MS_WH_UP && mouse_report.v > 0) mouse_report.v = 0; else if (code == KC_MS_WH_DOWN && mouse_report.v < 0) mouse_report.v = 0; else if (code == KC_MS_WH_LEFT && mouse_report.h < 0) mouse_report.h = 0; else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0) mouse_report.h = 0; else if (code == KC_MS_BTN1) mouse_report.buttons &= ~MOUSE_BTN1; else if (code == KC_MS_BTN2) mouse_report.buttons &= ~MOUSE_BTN2; else if (code == KC_MS_BTN3) mouse_report.buttons &= ~MOUSE_BTN3; else if (code == KC_MS_BTN4) mouse_report.buttons &= ~MOUSE_BTN4; else if (code == KC_MS_BTN5) mouse_report.buttons &= ~MOUSE_BTN5; # ifdef MK_MOMENTARY_ACCEL else if (code == KC_MS_ACCEL0) mk_speed = mkspd_DEFAULT; else if (code == KC_MS_ACCEL1) mk_speed = mkspd_DEFAULT; else if (code == KC_MS_ACCEL2) mk_speed = mkspd_DEFAULT; if (mk_speed != old_speed) adjust_speed(); # endif } #endif /* #ifndef MK_3_SPEED */ void mousekey_send(void) { mousekey_debug(); host_mouse_send(&mouse_report); last_timer = timer_read(); } void mousekey_clear(void) { mouse_report = (report_mouse_t){}; mousekey_repeat = 0; mousekey_accel = 0; } static void mousekey_debug(void) { if (!debug_mouse) return; print("mousekey [btn|x y v h](rep/acl): ["); phex(mouse_report.buttons); print("|"); print_decs(mouse_report.x); print(" "); print_decs(mouse_report.y); print(" "); print_decs(mouse_report.v); print(" "); print_decs(mouse_report.h); print("]("); print_dec(mousekey_repeat); print("/"); print_dec(mousekey_accel); print(")\n"); }