-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuinput-inject.c
146 lines (122 loc) · 3.33 KB
/
uinput-inject.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
#define _DEFAULT_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdbool.h>
#include <fcntl.h>
#include <unistd.h>
#include <err.h>
#include <sys/ioctl.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include "uinput-key.h"
static void check_posix(intmax_t rc, const char *fmt, ...)
{
if (rc < 0) {
va_list args;
va_start(args, fmt);
verr(EXIT_FAILURE, fmt, args);
va_end(args);
}
}
static int ev_emit(int fd, int type, int code, int value)
{
struct input_event ev = {
.type = type,
.code = code,
.value = value
};
gettimeofday(&ev.time, 0);
return write(fd, &ev, sizeof(ev));
}
static int ev_syn(int fd)
{
return ev_emit(fd, EV_SYN, SYN_REPORT, 0);
}
static int ev_key(int fd, int key, int value)
{
int ret;
if (value && (SHIFT & key)) {
if (ev_key(fd, KEY_LEFTSHIFT, 1) < 0)
return -1;
}
ret = ev_emit(fd, EV_KEY, key & KEYMASK, value);
if (ev_syn(fd) < 0)
return -1;
if (!value && (SHIFT & key)) {
if (ev_key(fd, KEY_LEFTSHIFT, 0) < 0)
return -1;
}
return ret;
}
static int ev_key_click(int fd, int key)
{
if (ev_key(fd, key, 1) < 0)
return -1;
return ev_key(fd, key, 0);
}
static int ev_inject_keypress(int fd, const char c)
{
const int key = printable_to_key(c);
if (ev_key_click(fd, key) < 0)
return -1;
if (key == KEY_ENTER)
usleep(500000);
return 0;
}
static int ev_inject_keypresses(int fd, const char *msg)
{
bool escaped = false;
for (const char *c = msg; *c; ++c) {
char key = *c;
if (escaped) {
escaped = false;
if (key == 'n') {
if (ev_inject_keypress(fd, '\n') < 0)
return -1;
} else if (key == 't') {
if (ev_inject_keypress(fd, '\t') < 0)
return -1;
} else {
if (ev_inject_keypress(fd, '\\') < 0)
return -1;
if (ev_inject_keypress(fd, key) < 0)
return -1;
}
} else if (key == '\\') {
escaped = true;
} else if (ev_inject_keypress(fd, key) < 0) {
return -1;
}
}
return 0;
}
int main(int argc, char *argv[])
{
if (argc < 2)
return -1;
int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
check_posix(fd, "failed to open uinput");
check_posix(ioctl(fd, UI_SET_EVBIT, EV_KEY), "failed to set EV_KEY");
check_posix(ioctl(fd, UI_SET_KEYBIT, KEY_LEFTSHIFT), "failed to set UI_SET_KEYBIT");
for (int key = KEY_1; key <= KEY_SPACE; ++key) {
check_posix(ioctl(fd, UI_SET_KEYBIT, key & KEYMASK),
"failed to set UI_SET_KEYBIT");
}
struct uinput_user_dev uidev = {
.id.bustype = BUS_VIRTUAL,
.id.vendor = 0x1,
.id.product = 0x1,
.id.version = 1
};
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "Virtual Injector Keyboard");
check_posix(write(fd, &uidev, sizeof(uidev)),
"failed to write uinput_user_dev struct");
check_posix(ioctl(fd, UI_DEV_CREATE),
"failed to create uinput device");
usleep(500000);
ev_inject_keypresses(fd, argv[1]);
ioctl(fd, UI_DEV_DESTROY);
close(fd);
}