forked from dzhu/keytap
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resolver.c
358 lines (336 loc) · 12 KB
/
resolver.c
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "time_util.h"
#include "resolver.h"
#include "names.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
void resolver_init(struct resolver *r, key_action_t *root_keymap,
send_t send, void *send_data){
*r = (struct resolver){0};
r->send = send;
r->send_data = send_data;
r->root_keymap = root_keymap;
r->current_keymap = root_keymap;
}
// call on each "natural" TAP resolution (which happens upon key release)
static void track_last_tap(struct resolver *r, struct input_event release_ev){
r->last_tap_code = release_ev.code;
r->last_tap_time = release_ev.time;
}
// call on each non-natural-TAP dual-key resolution
static void invalidate_last_tap(struct resolver *r){
r->last_tap_code = -1;
}
// call on each incoming keypress
static void maybe_invalidate_last_tap(struct resolver *r, int code){
if(code != r->last_tap_code){
r->last_tap_code = -1;
}
}
// returns bool ok
bool resolve_dedup_input(struct resolver *r, struct input_event ev){
if(ev.type != EV_KEY) return true;
if(ev.code > KEY_MAX) return true;
if(ev.value == 0){
// ignore non-final release of a key
if(r->input_counts[ev.code] == 0){
fprintf(stderr,
"invalid key release of %s in resolve_dedup_input\n",
get_input_name(ev.code)
);
return false;
}
return --r->input_counts[ev.code] == 0;
}
if(ev.value == 1){
// ignore non-first press of a key;
return r->input_counts[ev.code]++ == 0;
}
return true;
}
/* Given a pressed dual-key X, check unresolved events to decide tap or hold.
The first condition met from the list below indicates the correct mode:
- X has been double-tapped (tap mode)
- X has timed out (hold mode)
- X has been released (tap mode)
- another key has been pressed and released (hold mode)
- actually, neither has happened yet (resolvable time will be set) */
enum waveform {
WAVEFORM_TAP,
WAVEFORM_HOLD,
WAVEFORM_NONE_YET,
};
enum waveform check_waveform(struct resolver *r, struct input_event ev,
key_dual_t dual){
// is the keypress old enough to be a hold?
if(msec_diff(timeval_now(), ev.time) > dual.hold_ms){
// only on time-based holds do we check doubletap behavior.
long dtms = dual.double_tap_ms;
if(dtms > -1){
// check for double tap conditions
if(r->last_tap_code == ev.code){
if(dtms == 0 || msec_diff(ev.time, r->last_tap_time) < dtms){
// not a natural tap
invalidate_last_tap(r);
return WAVEFORM_TAP;
}
}
}
invalidate_last_tap(r);
return WAVEFORM_HOLD;
}
// in TIMEOUT_ONLY, we don't have to check any further
if(dual.mode == DUAL_MODE_TIMEOUT_ONLY){
return WAVEFORM_NONE_YET;
}
int keys_pressed[KEY_MAX] = {0};
for(size_t i = 1; i < r->ur_len; i++){
struct input_event ev2 = r->unresolved[(r->ur_start + i) % URMAX];
// only consider key events
if(ev2.type != EV_KEY) continue;
// was the main key released?
if(ev2.value == 0 && ev2.code == ev.code){
track_last_tap(r, ev2);
return WAVEFORM_TAP;
}
// in HOLD_ON_ROLLOVER mode, any other keypress is HOLD
if(dual.mode == DUAL_MODE_HOLD_ON_ROLLOVER
&& ev2.type == EV_KEY && ev2.value == 1){
invalidate_last_tap(r);
return WAVEFORM_HOLD;
}
// on press, record the pressed state of the key
if(ev2.value == 1 && ev2.code < KEY_MAX){
keys_pressed[ev2.code] = 1;
// any secondary press prevents a doubletap
invalidate_last_tap(r);
}
// some other key was pressed and released, main key is a HOLD
if(ev2.value == 0 && ev2.code < KEY_MAX && keys_pressed[ev2.code]){
invalidate_last_tap(r);
return WAVEFORM_HOLD;
}
}
return WAVEFORM_NONE_YET;
}
void do_keypress(struct resolver *r, struct input_event ev, key_action_t *ka){
switch(ka->type){
case KT_DUAL:
fprintf(stderr, "can't call do_keypress() on a dual-mode key\n");
exit(1);
break;
case KT_NONE:
fprintf(stderr, "can't call do_keypress() on a none-type key\n");
exit(1);
break;
case KT_SIMPLE:
// remember how to release the key
r->release_map[ev.code] = ka->key.simple;
// send the modified key
ev.code = ka->key.simple;
r->send(r->send_data, ev);
break;
case KT_MACRO:
// send the whole chain of macros
for(key_macro_t *m = ka->key.macro; m; m = m->next){
struct input_event ev = {
.type = EV_KEY,
.value = m->press,
.code = m->code,
};
r->send(r->send_data, ev);
struct input_event syn_ev = {
.type = EV_SYN,
.code = SYN_REPORT,
.value = 0,
};
r->send(r->send_data, syn_ev);
}
break;
case KT_MAP:
// set the keymap
r->current_keymap = ka;
// reset the keymap on release
r->release_map[ev.code] = RESET_KEYMAP;
// send nothing
break;
}
}
bool resolve_press(struct resolver *r, struct input_event ev){
maybe_invalidate_last_tap(r, ev.code);
// get the key action from the map
key_action_t *ka = key_action_get(r->current_keymap, ev.code);
switch(ka->type){
case KT_MAP:
case KT_MACRO:
case KT_SIMPLE:
do_keypress(r, ev, ka);
return true;
case KT_DUAL:
switch(check_waveform(r, ev, ka->key.dual)){
// .tap and .hold must not be KT_DUALs
case WAVEFORM_TAP:
do_keypress(r, ev, ka->key.dual.tap);
return true;
case WAVEFORM_HOLD:
do_keypress(r, ev, ka->key.dual.hold);
return true;
case WAVEFORM_NONE_YET:
// wait for a timeout to resolve this event
r->resolvable_time = msec_after(
ev.time, ka->key.dual.hold_ms
);
r->use_resolvable_time=true;
return false;
}
case KT_NONE:
default:
fprintf(stderr, "invalid key action in resolver\n");
exit(1);
}
}
bool resolve_release(struct resolver *r, struct input_event ev){
/* make the code look like whatever we mapped it to when we
resolved the initial keypress */
int initial_code = ev.code;
ev.code = r->release_map[initial_code];
r->release_map[initial_code] = 0;
switch(ev.code){
case RESET_KEYMAP:
r->current_keymap = r->root_keymap;
break;
case 0:
// we must have sent this key release early; do nothing.
break;
default:
r->send(r->send_data, ev);
}
return true;
}
/* helper function which tries to resolve the oldest key event. Returns false
if it deems the event unresolvable, setting the resolver.resolve_time as
appropriate. */
bool resolve(struct resolver *r){
if(r->ur_len == 0){
return false;
}
// grab the oldest event
struct input_event ev = r->unresolved[r->ur_start % URMAX];
bool resolved = false;
r->use_resolvable_time = false;
if(ev.type == EV_KEY){
// invalid key code
if(ev.code > KEY_MAX){
fprintf(stderr, "Dropping too-high keycode %d\n", ev.code);
resolved = true;
}
// key released
else if(ev.value == 0){
// printf("%.10s of %.10s\n", "release", get_input_name(ev.code));
resolved = resolve_release(r, ev);
}
// key pressed
else if(ev.value == 1){
// printf("%.10s of %.10s\n", "press", get_input_name(ev.code));
resolved = resolve_press(r, ev);
}
// key repeated
else if(ev.value == 2){
// printf("%.10s of %.10s\n", "repeat", get_input_name(ev.code));
/* make the code look like whatever we mapped it to when we
resolved the initial keypress */
ev.code = r->release_map[ev.code];
if(ev.code != 0 && ev.code != RESET_KEYMAP){
/* TODO: generate fake repeats to avoid the situation where
where a key is pressed twice and we can't tell which
repeat is from the first press and which one is
irrelevant. For now, X handles its own key repeats
so these don't really even matter. */
r->send(r->send_data, ev);
}
resolved = true;
}else{
fprintf(stderr,
"dropping invalid ev.value %d in resolver\n", ev.value
);
resolved = true;
}
}else if(ev.type == EV_SYN){
// printf("EV_SYN\n");
r->send(r->send_data, ev);
resolved = true;
}else{
// other ev.types are passed through unchanged
r->send(r->send_data, ev);
resolved = true;
}
if(resolved){
// one less element
r->ur_len--;
// but we start one later
r->ur_start = (r->ur_start + 1) % URMAX;
}else{
/* if we decided we couldn't resolve the oldest event, but the newest
event is a key release, emit the key release immediately. We know
that the initial press must have been resolved because if the
initial press had come after the unresolvable key, then now that
we have the release the unresolvable key would be resolvable. */
ev = r->unresolved[(r->ur_start + r->ur_len - 1) % URMAX];
if(ev.value == 0 && ev.code < KEY_MAX){
/* make the code look like whatever we mapped it to when we
resolved the initial keypress */
ev.code = r->release_map[ev.code];
switch(ev.code){
case RESET_KEYMAP:
// return to root keymap
r->current_keymap = r->root_keymap;
// one less element
r->ur_len--;
break;
case KEY_LEFTALT:
case KEY_RIGHTALT:
case KEY_LEFTCTRL:
case KEY_RIGHTCTRL:
case KEY_LEFTMETA:
case KEY_RIGHTMETA:
case KEY_LEFTSHIFT:
case KEY_RIGHTSHIFT:
// modifier keys don't get resolved early
break;
default:
r->send(r->send_data, ev);
// send a sync event for this generated key event
struct input_event syn_ev = {
.type = EV_SYN,
.code = SYN_REPORT,
.value = 0,
};
r->send(r->send_data, syn_ev);
// one less element
r->ur_len--;
}
}
}
return resolved;
}
// returns the timeout to be used for select(), which is either out or NULL
struct timeval *select_timeout(struct resolver *r, struct timeval *out){
// don't pass a timeout if none is valid.
if(!r->use_resolvable_time){
return NULL;
}
struct timespec now_timespec;
clock_gettime(CLOCK_REALTIME, &now_timespec);
struct timeval now;
TIMESPEC_TO_TIMEVAL(&now, &now_timespec);
long mdiff = msec_diff(r->resolvable_time, now);
if(mdiff < 0){
// somehow the timeout has expired, zero length timeout
out->tv_sec = 0;
out->tv_usec = 0;
}else{
out->tv_sec = mdiff / 1000;
out->tv_usec = (mdiff % 1000) * 1000;
}
return out;
}