-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathspacefn.c
298 lines (251 loc) · 6.62 KB
/
spacefn.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
/*
* spacefn-evdev.c
* James Laird-Wah (abrasive) 2018
* This code is in the public domain.
*/
#include <libevdev/libevdev.h>
#include <libevdev/libevdev-uinput.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
// Key mapping {{{1
unsigned int key_map(unsigned int code) {
switch (code) {
case KEY_BRIGHTNESSDOWN: // my magical escape button
exit(0);
case KEY_J:
return KEY_LEFT;
case KEY_K:
return KEY_DOWN;
case KEY_L:
return KEY_UP;
case KEY_SEMICOLON:
return KEY_RIGHT;
case KEY_M:
return KEY_HOME;
case KEY_COMMA:
return KEY_PAGEDOWN;
case KEY_DOT:
return KEY_PAGEUP;
case KEY_SLASH:
return KEY_END;
case KEY_B:
return KEY_SPACE;
}
return 0;
}
// Blacklist keys for which I have a mapping, to try and train myself out of using them
int blacklist(unsigned int code) {
switch (code) {
case KEY_UP:
case KEY_DOWN:
case KEY_RIGHT:
case KEY_LEFT:
case KEY_HOME:
case KEY_END:
case KEY_PAGEUP:
case KEY_PAGEDOWN:
return 1;
}
return 0;
}
// Global device handles {{{1
struct libevdev *idev;
struct libevdev_uinput *odev;
int fd;
// Ordered unique key buffer {{{1
#define MAX_BUFFER 8
unsigned int buffer[MAX_BUFFER];
unsigned int n_buffer = 0;
static int buffer_contains(unsigned int code) {
for (int i=0; i<n_buffer; i++)
if (buffer[i] == code)
return 1;
return 0;
}
static int buffer_remove(unsigned int code) {
for (int i=0; i<n_buffer; i++)
if (buffer[i] == code) {
memcpy(&buffer[i], &buffer[i+1], (n_buffer - i - 1) * sizeof(*buffer));
n_buffer--;
return 1;
}
return 0;
}
static int buffer_append(unsigned int code) {
if (n_buffer >= MAX_BUFFER)
return 1;
buffer[n_buffer++] = code;
return 0;
}
// Key I/O functions {{{1
// output {{{2
#define V_RELEASE 0
#define V_PRESS 1
#define V_REPEAT 2
static void send_key(unsigned int code, int value) {
libevdev_uinput_write_event(odev, EV_KEY, code, value);
libevdev_uinput_write_event(odev, EV_SYN, SYN_REPORT, 0);
}
static void send_press(unsigned int code) {
send_key(code, 1);
}
static void send_release(unsigned int code) {
send_key(code, 0);
}
static void send_repeat(unsigned int code) {
send_key(code, 2);
}
// input {{{2
static int read_one_key(struct input_event *ev) {
int err = libevdev_next_event(idev, LIBEVDEV_READ_FLAG_NORMAL | LIBEVDEV_READ_FLAG_BLOCKING, ev);
if (err) {
fprintf(stderr, "Failed: (%d) %s\n", -err, strerror(err));
exit(1);
}
if (ev->type != EV_KEY) {
libevdev_uinput_write_event(odev, ev->type, ev->code, ev->value);
return -1;
}
if (blacklist(ev->code))
return -1;
return 0;
}
// State functions {{{1
enum {
IDLE,
DECIDE,
SHIFT,
} state = IDLE;
static void state_idle(void) { // {{{2
struct input_event ev;
for (;;) {
while (read_one_key(&ev));
if (ev.code == KEY_SPACE && ev.value == V_PRESS) {
state = DECIDE;
return;
}
send_key(ev.code, ev.value);
}
}
static void state_decide(void) { // {{{2
n_buffer = 0;
struct input_event ev;
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 200000;
fd_set set;
FD_ZERO(&set);
while (timeout.tv_usec >= 0) {
FD_SET(fd, &set);
int nfds = select(fd+1, &set, NULL, NULL, &timeout);
if (!nfds)
break;
while (read_one_key(&ev));
if (ev.value == V_PRESS) {
buffer_append(ev.code);
continue;
}
if (ev.code == KEY_SPACE && ev.value == V_RELEASE) {
send_key(KEY_SPACE, V_PRESS);
send_key(KEY_SPACE, V_RELEASE);
for (int i=0; i<n_buffer; i++)
send_key(buffer[i], V_PRESS);
state = IDLE;
return;
}
if (ev.value == V_RELEASE && !buffer_contains(ev.code)) {
send_key(ev.code, ev.value);
continue;
}
if (ev.value == V_RELEASE && buffer_remove(ev.code)) {
unsigned int code = key_map(ev.code);
send_key(code, V_PRESS);
send_key(code, V_RELEASE);
state = SHIFT;
return;
}
}
printf("timed out\n");
for (int i=0; i<n_buffer; i++) {
unsigned int code = key_map(buffer[i]);
if (!code)
code = buffer[i];
send_key(code, V_PRESS);
}
state = SHIFT;
}
static void state_shift(void) {
n_buffer = 0;
struct input_event ev;
for (;;) {
while (read_one_key(&ev));
if (ev.code == KEY_SPACE && ev.value == V_RELEASE) {
for (int i=0; i<n_buffer; i++)
send_key(buffer[i], V_RELEASE);
state = IDLE;
return;
}
if (ev.code == KEY_SPACE)
continue;
unsigned int code = key_map(ev.code);
if (code) {
if (ev.value == V_PRESS)
buffer_append(code);
else if (ev.value == V_RELEASE)
buffer_remove(code);
send_key(code, ev.value);
} else {
send_key(ev.code, ev.value);
}
}
}
static void run_state_machine(void) {
for (;;) {
printf("state %d\n", state);
switch (state) {
case IDLE:
state_idle();
break;
case DECIDE:
state_decide();
break;
case SHIFT:
state_shift();
break;
}
}
}
int main(int argc, char **argv) { // {{{1
if (argc < 2) {
printf("usage: %s /dev/input/...", argv[0]);
return 1;
}
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
perror("open input");
return 1;
}
int err = libevdev_new_from_fd(fd, &idev);
if (err) {
fprintf(stderr, "Failed: (%d) %s\n", -err, strerror(err));
return 1;
}
int uifd = open("/dev/uinput", O_RDWR);
if (uifd < 0) {
perror("open /dev/uinput");
return 1;
}
err = libevdev_uinput_create_from_device(idev, uifd, &odev);
if (err) {
fprintf(stderr, "Failed: (%d) %s\n", -err, strerror(err));
return 1;
}
err = libevdev_grab(idev, LIBEVDEV_GRAB);
if (err) {
fprintf(stderr, "Failed: (%d) %s\n", -err, strerror(err));
return 1;
}
run_state_machine();
}