forked from maricn/interception-vimproved
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterception-vimproved.cpp
More file actions
432 lines (358 loc) · 12.6 KB
/
interception-vimproved.cpp
File metadata and controls
432 lines (358 loc) · 12.6 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <cstdlib>
#include <iostream>
#include <linux/input.h>
#include <set>
#include <unistd.h>
#include <vector>
using namespace std;
/**
* Global constants
**/
const int KEY_STROKE_UP = 0, KEY_STROKE_DOWN = 1, KEY_STROKE_REPEAT = 2;
const int input_event_struct_size = sizeof(struct input_event);
/**
* Only very rare keys are above 0x151, not found on most of keyboards, probably
* you don't need to mimic them. We cover above 0x100 which includes mouse BTNs.
* Check what else is there at:
* https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h
**/
const unsigned short MAX_KEY = 0x151;
typedef struct input_event Event;
// clang-format off
const Event
_syn = {.time = { .tv_sec = 0, .tv_usec = 0}, .type = EV_SYN, .code = SYN_REPORT, .value = KEY_STROKE_UP};
const Event
*syn = &_syn;
// clang-format on
int readEvent(Event *e) {
return fread(e, input_event_struct_size, 1, stdin) == 1;
}
void writeEvent(const Event *e) {
if (fwrite(e, input_event_struct_size, 1, stdout) != 1)
exit(EXIT_FAILURE);
}
void writeEvents(const vector<input_event> *events) {
const unsigned long size = events->size();
if (fwrite(events->data(), input_event_struct_size, size, stdout) != size)
exit(EXIT_FAILURE);
}
// @TODO: convert to a map cache to reduce amount of memory allocations
Event buildEvent(int direction, unsigned short keycode) {
input_event ev = {.time = {.tv_sec = 0, .tv_usec = 0},
.type = EV_KEY,
.code = keycode,
.value = direction};
return ev;
}
Event buildEventDown(unsigned short keycode) {
return buildEvent(KEY_STROKE_DOWN, keycode);
}
Event buildEventUp(unsigned short keycode) {
return buildEvent(KEY_STROKE_UP, keycode);
}
void writeCombo(unsigned short keycode) {
input_event key_down = {.time = {.tv_sec = 0, .tv_usec = 0},
.type = EV_KEY,
.code = keycode,
.value = KEY_STROKE_DOWN};
input_event key_up = {.time = {.tv_sec = 0, .tv_usec = 0},
.type = EV_KEY,
.code = keycode,
.value = KEY_STROKE_UP};
vector<input_event> *combo = new vector<input_event>();
combo->push_back(key_down);
combo->push_back(*syn);
combo->push_back(key_up);
writeEvents(combo);
delete combo;
}
/***************************************************************
*********************** Global classes ************************
**************************************************************/
/**
* Intercepted key specification
**/
class InterceptedKey {
public:
enum State { START = 0, INTERCEPTED_KEY_HELD = 1, OTHER_KEY_HELD = 2 };
static int isModifier(int key) {
switch (key) {
default:
return 0;
// clang-format off
case KEY_LEFTSHIFT: case KEY_RIGHTSHIFT:
case KEY_LEFTCTRL: case KEY_RIGHTCTRL:
case KEY_LEFTALT: case KEY_RIGHTALT:
case KEY_LEFTMETA: case KEY_RIGHTMETA:
// clang-format on
// @TODO: handle capslock as not modifier?
case KEY_CAPSLOCK:
return 1;
}
}
InterceptedKey(unsigned short intercepted, unsigned short tapped) {
this->_intercepted = intercepted;
this->_tapped = tapped;
}
bool matches(unsigned short code) { return this->_intercepted == code; }
State getState() { return this->_state; }
bool process(Event *input) {
switch (this->_state) {
case START:
return this->processStart(input);
case INTERCEPTED_KEY_HELD:
return this->processInterceptedHeld(input);
case OTHER_KEY_HELD:
return this->processOtherKeyHeld(input);
}
throw std::exception();
}
protected:
bool processStart(Event *input) {
if (this->matches(input->code) && input->value == KEY_STROKE_DOWN) {
this->_shouldEmitTapped = true;
this->_state = INTERCEPTED_KEY_HELD;
return false;
}
return true;
};
virtual bool processInterceptedHeld(Event *input) = 0;
virtual bool processOtherKeyHeld(Event *input) = 0;
unsigned short _intercepted;
unsigned short _tapped;
State _state;
bool _shouldEmitTapped = true;
};
class InterceptedKeyLayer : public InterceptedKey {
private:
unsigned short *_map;
set<unsigned short> *_heldKeys = new set<unsigned short>();
protected:
Event map(Event *input) {
Event result(*input);
result.code = this->_map[input->code];
return result;
}
bool processInterceptedHeld(Event *input) override {
if (this->matches(input->code) && input->value != KEY_STROKE_UP) {
return false; // don't emit anything
}
if (this->matches(input->code)) { // && stroke up
// TODO: find a way to have a mouse click mark the key as intercepted
// or just make it time based
// this->_shouldEmitTapped &= mouse clicked || timeout;
if (this->_shouldEmitTapped) {
writeCombo(this->_tapped);
this->_shouldEmitTapped = false;
}
this->_state = START;
return false;
}
if (input->value == KEY_STROKE_DOWN) { // any other key
// @NOTE: if we don't blindly set _shouldEmitTapped to false on any
// keypress, we can type faster because only in case of mapped key down,
// the intercepted key will not be emitted - useful for scenario:
// L_DOWN, SPACE_DOWN, A_DOWN, L_UP, A_UP, SPACE_UP
this->_shouldEmitTapped &= !this->hasMapped(input->code) &&
!InterceptedKey::isModifier(input->code);
if (this->hasMapped(input->code)) {
this->_heldKeys->insert(input->code);
Event mapped = this->map(input);
writeEvent(&mapped);
this->_state = InterceptedKey::OTHER_KEY_HELD;
return false;
}
}
return true;
}
bool processOtherKeyHeld(Event *input) override {
if (input->code == this->_intercepted && input->value != KEY_STROKE_UP)
return false;
if (input->value == KEY_STROKE_DOWN &&
this->_heldKeys->find(input->code) != this->_heldKeys->end()) {
return false;
}
bool shouldEmitInput = true;
if (input->value == KEY_STROKE_UP) {
if (this->_heldKeys->find(input->code) !=
this->_heldKeys->end()) { // one of mapped held keys goes up
Event mapped = this->map(input);
writeEvent(&mapped);
this->_heldKeys->erase(input->code);
if (this->_heldKeys->empty()) {
this->_state = InterceptedKey::INTERCEPTED_KEY_HELD;
}
shouldEmitInput = false;
} else { // key that was not mapped & held goes up
if (this->matches(input->code)) {
vector<Event> *held_keys_up = new vector<Event>();
for (auto held_key_code : *this->_heldKeys) {
Event held_key_up = buildEventUp(this->_map[held_key_code]);
held_keys_up->push_back(held_key_up);
held_keys_up->push_back(*syn);
}
writeEvents(held_keys_up);
delete held_keys_up;
this->_heldKeys->clear();
this->_state = InterceptedKey::START;
shouldEmitInput = false;
}
}
} else { // KEY_STROKE_DOWN or KEY_STROKE_REPEAT
if (this->hasMapped(input->code)) {
auto mapped = this->map(input);
writeEvent(&mapped);
if (input->value == KEY_STROKE_DOWN) {
this->_heldKeys->insert(input->code);
}
shouldEmitInput = false;
}
}
return shouldEmitInput;
}
public:
InterceptedKeyLayer(unsigned short intercepted, unsigned short tapped)
: InterceptedKey(intercepted, tapped) {
this->_map = new unsigned short[MAX_KEY]{0};
}
~InterceptedKeyLayer() { delete this->_map; }
InterceptedKey *addMapping(unsigned short from, unsigned short to) {
this->_map[from] = to;
return this;
}
bool hasMapped(unsigned short from) { return this->_map[from] != 0; }
};
class InterceptedKeyModifier : public InterceptedKey {
protected:
unsigned short _modifier;
bool processInterceptedHeld(Event *input) override {
if (this->matches(input->code) && input->value != KEY_STROKE_UP) {
return false;
}
bool shouldEmitInput = true;
if (this->matches(input->code)) { // && stroke up
if (this->_shouldEmitTapped) {
writeCombo(this->_tapped);
} else { // intercepted is mapped to modifier and key stroke up
Event *modifier_up = new Event(*input);
modifier_up->code = this->_modifier;
writeEvent(modifier_up);
delete modifier_up;
}
this->_state = START;
return false;
}
if (input->value == KEY_STROKE_DOWN) { // any other than intercepted
if (this->_shouldEmitTapped) { // on first non-matching input after a
// matching input
Event *modifier_down = new Event(*input);
modifier_down->code = this->_modifier;
// for some reason, need to push "syn" after modifier here
vector<Event> *modifier_and_input = new vector<Event>();
modifier_and_input->push_back(*modifier_down);
modifier_and_input->push_back(*syn);
writeEvents(modifier_and_input);
this->_shouldEmitTapped = false;
delete modifier_and_input;
return true; // gotta emit input event independently so we can process layer+modifier+input together
}
}
return shouldEmitInput;
}
bool processOtherKeyHeld(Event *input) override { return true; }
public:
InterceptedKeyModifier(unsigned short intercepted, unsigned short tapped,
unsigned short modifier)
: InterceptedKey(intercepted, tapped) {
if (!InterceptedKey::isModifier(modifier))
throw invalid_argument("Specified wrong modifier key");
this->_modifier = modifier;
}
};
vector<InterceptedKey *> *initInterceptedKeys() {
// tap space for space, hold for layer mapping
InterceptedKeyLayer *space = new InterceptedKeyLayer(KEY_SPACE, KEY_SPACE);
// special chars
/*
space->addMapping(KEY_E, KEY_ESC);
space->addMapping(KEY_D, KEY_DELETE);
space->addMapping(KEY_B, KEY_BACKSPACE);
*/
// vim home row
space->addMapping(KEY_H, KEY_LEFT);
space->addMapping(KEY_J, KEY_DOWN);
space->addMapping(KEY_K, KEY_UP);
space->addMapping(KEY_L, KEY_RIGHT);
/*
// vim above home row
space->addMapping(KEY_Y, KEY_HOME);
space->addMapping(KEY_U, KEY_PAGEDOWN);
space->addMapping(KEY_I, KEY_PAGEUP);
space->addMapping(KEY_O, KEY_END);
// number row to F keys
space->addMapping(KEY_1, KEY_F1);
space->addMapping(KEY_2, KEY_F2);
space->addMapping(KEY_3, KEY_F3);
space->addMapping(KEY_4, KEY_F4);
space->addMapping(KEY_5, KEY_F5);
space->addMapping(KEY_6, KEY_F6);
space->addMapping(KEY_7, KEY_F7);
space->addMapping(KEY_8, KEY_F8);
space->addMapping(KEY_9, KEY_F9);
space->addMapping(KEY_0, KEY_F10);
space->addMapping(KEY_MINUS, KEY_F11);
space->addMapping(KEY_EQUAL, KEY_F12);
// xf86 audio
space->addMapping(KEY_M, KEY_MUTE);
space->addMapping(KEY_COMMA, KEY_VOLUMEDOWN);
space->addMapping(KEY_DOT, KEY_VOLUMEUP);
// mouse navigation
space->addMapping(BTN_LEFT, BTN_BACK);
space->addMapping(BTN_RIGHT, BTN_FORWARD);
// @FIXME: this is not working, even though `wev` says keycode 99 is Print
PrtSc -> Context Menu
space->addMapping(KEY_SYSRQ, KEY_CONTEXT_MENU);
*/
/*
// tap caps for esc, hold for ctrl
InterceptedKeyModifier *caps =
new InterceptedKeyModifier(KEY_CAPSLOCK, KEY_ESC, KEY_LEFTCTRL);
// tap enter for enter, hold for ctrl
InterceptedKeyModifier *enter =
new InterceptedKeyModifier(KEY_ENTER, KEY_ENTER, KEY_RIGHTCTRL);
*/
// @NOTE: modifier keys must go first because layerKey.processInterceptedHeld
// emits mapped key as soon as the for loop calls layerKey.process..
// if that process is run before modifierKey.process, the modifier key will
// not be emitted
vector<InterceptedKey *> *interceptedKeys = new vector<InterceptedKey *>();
//interceptedKeys->push_back(caps);
//interceptedKeys->push_back(enter);
interceptedKeys->push_back(space);
return interceptedKeys;
}
int main() {
auto interceptedKeys = initInterceptedKeys();
setbuf(stdin, NULL), setbuf(stdout, NULL);
/* event *input = (event*) malloc(input_event_struct_size); */
Event *input = new Event();
while (readEvent(input)) {
if (input->type == EV_MSC && input->code == MSC_SCAN)
continue;
if (input->type != EV_KEY) {
writeEvent(input);
continue;
}
/* cerr << input->type << "," << input->code << " "; */
bool shouldEmitInput = true;
for (auto key : *interceptedKeys) {
shouldEmitInput &= key->process(input);
}
if (shouldEmitInput) {
writeEvent(input);
}
}
/* free(input); */
delete input;
delete interceptedKeys;
}