-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.c
548 lines (468 loc) · 13.5 KB
/
main.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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#include <fcntl.h>
#include <locale.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wayland-client.h>
#include <wchar.h>
#include <xkbcommon/xkbcommon.h>
#include "virtual-keyboard-unstable-v1-client-protocol.h"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
enum wtype_command_type {
WTYPE_COMMAND_TEXT = 0,
WTYPE_COMMAND_MOD_PRESS = 1,
WTYPE_COMMAND_MOD_RELEASE = 2,
WTYPE_COMMAND_KEY_PRESS = 3,
WTYPE_COMMAND_KEY_RELEASE = 4,
WTYPE_COMMAND_SLEEP = 5,
WTYPE_COMMAND_TEXT_STDIN = 6
};
enum wtype_mod {
WTYPE_MOD_NONE = 0,
WTYPE_MOD_SHIFT = 1,
WTYPE_MOD_CAPSLOCK = 2,
WTYPE_MOD_CTRL = 4,
WTYPE_MOD_ALT = 8,
WTYPE_MOD_LOGO = 64,
WTYPE_MOD_ALTGR = 128
};
struct wtype_command {
enum wtype_command_type type;
union {
struct {
unsigned int *key_codes;
size_t key_codes_len;
unsigned int delay_ms;
};
unsigned int single_key_code;
enum wtype_mod mod;
unsigned int sleep_ms;
};
};
struct keymap_entry {
xkb_keysym_t xkb;
wchar_t wchr;
};
struct wtype {
struct wl_display *display;
struct wl_registry *registry;
struct wl_seat *seat;
struct zwp_virtual_keyboard_manager_v1 *manager;
struct zwp_virtual_keyboard_v1 *keyboard;
// Stores a keycode -> (xkb_keysym_t, wchar_t) mapping
// Beware that this is one-indexed (as zero-keycodes are sometimes
// not handled well by clients).
// That is, keymap[0] is a (xkb_keysym_t, wchar_t) pair which we can type by sending
// the keycode 1.
// Beware that the wchar_t does not have to be valid (and is 0 in such cases)
// This is since some keysyms need not have a unicode representation
// (such as the arrow keys and similar)
size_t keymap_len;
struct keymap_entry *keymap;
uint32_t mod_status;
size_t command_count;
struct wtype_command *commands;
};
void fail(const char *format, ...)
{
va_list vas;
va_start(vas, format);
vfprintf(stderr, format, vas);
va_end(vas);
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}
static void upload_keymap(struct wtype *wtype);
static void handle_wl_event(void *data, struct wl_registry *registry,
uint32_t name, const char *interface,
uint32_t version)
{
struct wtype *wtype = data;
if (!strcmp(interface, wl_seat_interface.name)) {
wtype->seat = wl_registry_bind(
registry, name, &wl_seat_interface, version <= 7 ? version : 7
);
} else if (!strcmp(interface, zwp_virtual_keyboard_manager_v1_interface.name)) {
wtype->manager = wl_registry_bind(
registry, name, &zwp_virtual_keyboard_manager_v1_interface, 1
);
}
}
static void handle_wl_event_remove(void *data, struct wl_registry *registry,
uint32_t name)
{
}
static const struct wl_registry_listener registry_listener = {
.global = handle_wl_event,
.global_remove = handle_wl_event_remove,
};
static const struct { const char *name; enum wtype_mod mod; } mod_names[] = {
{"shift", WTYPE_MOD_SHIFT},
{"capslock", WTYPE_MOD_CAPSLOCK},
{"ctrl", WTYPE_MOD_CTRL},
{"logo", WTYPE_MOD_LOGO},
{"win", WTYPE_MOD_LOGO},
{"alt", WTYPE_MOD_ALT},
{"altgr", WTYPE_MOD_ALTGR},
};
enum wtype_mod name_to_mod(const char *name)
{
for (unsigned int i = 0; i < ARRAY_SIZE(mod_names); i++) {
if (!strcasecmp(mod_names[i].name, name)) {
return mod_names[i].mod;
}
}
return WTYPE_MOD_NONE;
}
static unsigned int append_keymap_entry(struct wtype *wtype, wchar_t ch, xkb_keysym_t xkb)
{
wtype->keymap = realloc(
wtype->keymap, ++wtype->keymap_len * sizeof(wtype->keymap[0])
);
wtype->keymap[wtype->keymap_len - 1].wchr = ch;
wtype->keymap[wtype->keymap_len - 1].xkb = xkb;
return wtype->keymap_len;
}
unsigned int get_key_code_by_wchar(struct wtype *wtype, wchar_t ch)
{
const struct {
wchar_t from;
xkb_keysym_t to;
} remap_table[] = {
{ L'\n', XKB_KEY_Return },
{ L'\t', XKB_KEY_Tab },
{ L'\e', XKB_KEY_Escape },
};
for (unsigned int i = 0; i < wtype->keymap_len; i++) {
if (wtype->keymap[i].wchr == ch) {
return i + 1;
}
}
// TODO: Maybe warn if this actually ends up being XKB_KEY_NoSymbol or something?
xkb_keysym_t xkb = xkb_utf32_to_keysym(ch);
for (size_t i = 0; i < ARRAY_SIZE(remap_table); i++) {
if (remap_table[i].from == ch) {
// This overwrites whatever xkb gave us before.
xkb = remap_table[i].to;
break;
}
}
return append_keymap_entry(wtype, ch, xkb);
}
unsigned int get_key_code_by_xkb(struct wtype *wtype, xkb_keysym_t xkb)
{
for (unsigned int i = 0; i < wtype->keymap_len; i++) {
if (wtype->keymap[i].xkb == xkb) {
return i + 1;
}
}
return append_keymap_entry(wtype, 0, xkb);
}
static void parse_args(struct wtype *wtype, int argc, const char *argv[])
{
wtype->commands = calloc(argc, sizeof(wtype->commands[0])); // Upper bound
wtype->command_count = 0;
bool raw_text = false;
bool prefix_with_space = false;
bool use_stdin = false;
unsigned int delay_ms = 0;
for (int i = 1; i < argc; i++) {
struct wtype_command *cmd = &wtype->commands[wtype->command_count];
if (!raw_text && !strcmp("--", argv[i])) {
raw_text = true;
} else if (!raw_text && !strcmp("-", argv[i])) {
// Output text from stdin
if (use_stdin) {
fail("Stdin place-holder can only appear once");
}
use_stdin = true;
cmd->type = WTYPE_COMMAND_TEXT_STDIN;
cmd->delay_ms = delay_ms;
wtype->command_count++;
} else if (!raw_text && argv[i][0] == '-'){
if (i == argc - 1) {
fail("Missing argument to %s", argv[i]);
}
if (!strcmp("-M", argv[i])) {
// Press modifier
cmd->type = WTYPE_COMMAND_MOD_PRESS;
cmd->mod = name_to_mod(argv[i + 1]);
if (cmd->mod == WTYPE_MOD_NONE) {
fail("Invalid modifier name '%s'", argv[i + 1]);
}
} else if (!strcmp("-m", argv[i])) {
// Release modifier
cmd->type = WTYPE_COMMAND_MOD_RELEASE;
cmd->mod = name_to_mod(argv[i + 1]);
if (cmd->mod == WTYPE_MOD_NONE) {
fail("Invalid modifier name '%s'", argv[i + 1]);
}
} else if (!strcmp("-s", argv[i])) {
// Sleep
cmd->type = WTYPE_COMMAND_SLEEP;
cmd->sleep_ms = atoi(argv[i + 1]);
if (cmd->sleep_ms <= 0) {
fail("Invalid sleep time");
}
} else if (!strcmp("-d", argv[i])) {
// Set delay between type keystrokes
delay_ms = atoi(argv[i + 1]);
if (delay_ms <= 0) {
fail("Invalid sleep time");
}
} else if (!strcmp("-k", argv[i])) {
//size_t k;
xkb_keysym_t ks = xkb_keysym_from_name(argv[i + 1], XKB_KEYSYM_CASE_INSENSITIVE);
if (ks == XKB_KEY_NoSymbol) {
fail("Unknown key '%s'", argv[i + 1]);
}
cmd->type = WTYPE_COMMAND_TEXT;
cmd->key_codes = malloc(sizeof(cmd->key_codes[0]));
cmd->key_codes_len = 1;
cmd->key_codes[0] = get_key_code_by_xkb(wtype, ks);
cmd->delay_ms = delay_ms;
} else if (!strcmp("-P", argv[i]) || !strcmp("-p", argv[i])) {
// Press/release a key
xkb_keysym_t ks = xkb_keysym_from_name(argv[i + 1], XKB_KEYSYM_CASE_INSENSITIVE);
if (ks == XKB_KEY_NoSymbol) {
fail("Unknown key '%s'", argv[i + 1]);
}
cmd->type = argv[i][1] == 'P' ? WTYPE_COMMAND_KEY_PRESS : WTYPE_COMMAND_KEY_RELEASE;
cmd->single_key_code = get_key_code_by_xkb(wtype, ks);
} else {
fail("Unknown parameter %s", argv[i]);
}
prefix_with_space = false;
i++;
wtype->command_count++;
} else {
// Text
cmd->type = WTYPE_COMMAND_TEXT;
cmd->delay_ms = delay_ms;
wtype->command_count++;
size_t raw_len = strlen(argv[i]) + 2; // NULL byte and the potential space
wchar_t text[raw_len]; // Upper bound on size
memset(text, 0, sizeof(text));
setlocale(LC_CTYPE, "");
ssize_t ret = mbstowcs(
prefix_with_space ? &text[1] : text,
argv[i],
prefix_with_space ? ARRAY_SIZE(text) - 1 : ARRAY_SIZE(text)
);
if (ret < 0) {
fail("Failed to deencode input argv");
}
if (prefix_with_space) {
text[0] = L' ';
ret++;
}
cmd->key_codes = calloc(ret, sizeof(cmd->key_codes[0]));
cmd->key_codes_len = ret;
for (ssize_t k = 0; k < ret; k++) {
cmd->key_codes[k] = get_key_code_by_wchar(wtype, text[k]);
}
prefix_with_space = true;
}
}
}
static void run_sleep(struct wtype *wtype, struct wtype_command *cmd)
{
usleep(cmd->sleep_ms * 1000);
}
static void run_mod(struct wtype *wtype, struct wtype_command *cmd)
{
if (cmd->type == WTYPE_COMMAND_MOD_PRESS) {
wtype->mod_status |= cmd->mod;
} else {
wtype->mod_status &= ~cmd->mod;
}
zwp_virtual_keyboard_v1_modifiers(
wtype->keyboard, wtype->mod_status & ~WTYPE_MOD_CAPSLOCK, 0,
wtype->mod_status & WTYPE_MOD_CAPSLOCK, 0
);
wl_display_roundtrip(wtype->display);
}
static void run_key(struct wtype *wtype, struct wtype_command *cmd)
{
zwp_virtual_keyboard_v1_key(
wtype->keyboard, 0, cmd->single_key_code,
cmd->type == WTYPE_COMMAND_KEY_PRESS ?
WL_KEYBOARD_KEY_STATE_PRESSED : WL_KEYBOARD_KEY_STATE_RELEASED
);
wl_display_roundtrip(wtype->display);
}
static void type_keycode(struct wtype *wtype, unsigned int key_code)
{
zwp_virtual_keyboard_v1_key(
wtype->keyboard, 0, key_code, WL_KEYBOARD_KEY_STATE_PRESSED
);
wl_display_roundtrip(wtype->display);
usleep(2000);
zwp_virtual_keyboard_v1_key(
wtype->keyboard, 0, key_code, WL_KEYBOARD_KEY_STATE_RELEASED
);
wl_display_roundtrip(wtype->display);
usleep(2000);
}
static void run_text(struct wtype *wtype, struct wtype_command *cmd)
{
for (size_t i = 0; i < cmd->key_codes_len; i++) {
type_keycode(wtype, cmd->key_codes[i]);
usleep(cmd->delay_ms * 1000);
}
}
static void run_text_stdin(struct wtype *wtype, struct wtype_command *cmd)
{
char kbuf[8];
size_t k = 0;
wchar_t text_char;
const size_t buf_size = 100;
cmd->key_codes = calloc(buf_size, sizeof(cmd->key_codes[0]));
cmd->key_codes_len = 0;
setlocale(LC_CTYPE, "");
while (1) {
if ((kbuf[k] = fgetc(stdin)) == EOF) {
break;
}
kbuf[k+1] = '\0';
size_t ret = mbstowcs(&text_char, kbuf, 1);
if (ret == -1) {
k++;
if (k >= ARRAY_SIZE(kbuf) - 1) {
break;
}
continue;
} else if (ret == 0) {
k = 0;
continue;
}
k = 0;
unsigned int key_code = get_key_code_by_wchar(wtype, text_char);
cmd->key_codes[cmd->key_codes_len++] = key_code;
if (cmd->key_codes_len == buf_size) {
upload_keymap(wtype);
for (size_t i = 0; i < cmd->key_codes_len; i++) {
type_keycode(wtype, cmd->key_codes[i]);
usleep(cmd->delay_ms * 1000);
}
cmd->key_codes_len = 0;
}
}
if (cmd->key_codes_len != 0) {
upload_keymap(wtype);
for (size_t i = 0; i < cmd->key_codes_len; i++) {
type_keycode(wtype, cmd->key_codes[i]);
usleep(cmd->delay_ms * 1000);
}
}
free(cmd->key_codes);
}
static void run_commands(struct wtype *wtype)
{
void (*handlers[])(struct wtype *, struct wtype_command *) = {
[WTYPE_COMMAND_SLEEP] = run_sleep,
[WTYPE_COMMAND_MOD_PRESS] = run_mod,
[WTYPE_COMMAND_MOD_RELEASE] = run_mod,
[WTYPE_COMMAND_KEY_PRESS] = run_key,
[WTYPE_COMMAND_KEY_RELEASE] = run_key,
[WTYPE_COMMAND_TEXT] = run_text,
[WTYPE_COMMAND_TEXT_STDIN] = run_text_stdin,
};
for (unsigned int i = 0; i < wtype->command_count; i++) {
handlers[wtype->commands[i].type](wtype, &wtype->commands[i]);
}
}
static void print_keysym_name(xkb_keysym_t keysym, FILE *f)
{
char sym_name[256];
int ret = xkb_keysym_get_name(keysym, sym_name, sizeof(sym_name));
if (ret <= 0) {
fail("Unable to get XKB symbol name for keysym %04x\n", keysym);
return;
}
fprintf(f, "%s", sym_name);
}
static void upload_keymap(struct wtype *wtype)
{
char filename[] = "/tmp/wtype-XXXXXX";
int fd = mkstemp(filename);
if (fd < 0) {
fail("Failed to create the temporary keymap file");
}
unlink(filename);
FILE *f = fdopen(fd, "w");
fprintf(f, "xkb_keymap {\n");
fprintf(
f,
"xkb_keycodes \"(unnamed)\" {\n"
"minimum = 8;\n"
"maximum = %ld;\n",
wtype->keymap_len + 8 + 1
);
for (size_t i = 0; i < wtype->keymap_len; i++) {
fprintf(f, "<K%ld> = %ld;\n", i + 1, i + 8 + 1);
}
fprintf(f, "};\n");
// TODO: Is including "complete" here really a good idea?
fprintf(f, "xkb_types \"(unnamed)\" { include \"complete\" };\n");
fprintf(f, "xkb_compatibility \"(unnamed)\" { include \"complete\" };\n");
fprintf(f, "xkb_symbols \"(unnamed)\" {\n");
for (size_t i = 0; i < wtype->keymap_len; i++) {
fprintf(f, "key <K%ld> {[", i + 1);
print_keysym_name(wtype->keymap[i].xkb, f);
fprintf(f, "]};\n");
}
fprintf(f, "};\n");
fprintf(f, "};\n");
fputc('\0', f);
fflush(f);
size_t keymap_size = ftell(f);
zwp_virtual_keyboard_v1_keymap(
wtype->keyboard, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, fileno(f), keymap_size
);
wl_display_roundtrip(wtype->display);
fclose(f);
}
int main(int argc, const char *argv[])
{
if (argc < 2) {
fail("Usage: %s <text-to-type>", argv[0]);
}
struct wtype wtype;
memset(&wtype, 0, sizeof(wtype));
parse_args(&wtype, argc, argv);
wtype.display = wl_display_connect(NULL);
if (wtype.display == NULL) {
fail("Wayland connection failed");
}
wtype.registry = wl_display_get_registry(wtype.display);
wl_registry_add_listener(wtype.registry, ®istry_listener, &wtype);
wl_display_dispatch(wtype.display);
wl_display_roundtrip(wtype.display);
if (wtype.manager == NULL) {
fail("Compositor does not support the virtual keyboard protocol");
}
if (wtype.seat == NULL) {
fail("No seat found");
}
wtype.keyboard = zwp_virtual_keyboard_manager_v1_create_virtual_keyboard(
wtype.manager, wtype.seat
);
upload_keymap(&wtype);
run_commands(&wtype);
for (unsigned int i = 0; i < wtype.command_count; i++) {
if (wtype.commands[i].type == WTYPE_COMMAND_TEXT) {
free(wtype.commands[i].key_codes);
}
}
free(wtype.commands);
free(wtype.keymap);
zwp_virtual_keyboard_v1_destroy(wtype.keyboard);
zwp_virtual_keyboard_manager_v1_destroy(wtype.manager);
wl_registry_destroy(wtype.registry);
wl_display_disconnect(wtype.display);
}