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
|
#define _DEFAULT_SOURCE /* for getopt() and optarg */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdarg.h>
#include <linux/input.h>
#include <linux/uinput.h>
#define VERSION "0.0.1"
#define DEFAULT_DELAY_MS 30
#define DEFAULT_KBD "/dev/input/by-id/usb-Marson_Marson_Keyboard_and_Mouse_Link_Ver:ps2120L-event-kbd"
const char *self;
int debugging = 0; /* -v */
const char *keyboard_dev = DEFAULT_KBD; /* -k */
int delay_ms = DEFAULT_DELAY_MS; /* -d */
/* variadic macros are apparently C99 and not just a GNU extension */
void logmsg(const char *prefix, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "%s: %s: ", self, prefix);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
}
#define die(fmt, ...) { logmsg("fatal", fmt, ##__VA_ARGS__); exit(1); };
#define warn(fmt, ...) { logmsg("warning", fmt, ##__VA_ARGS__); };
#define info(fmt, ...) { logmsg("info", fmt, ##__VA_ARGS__); };
#define debug(fmt, ...) { if(debugging) logmsg("debug", fmt, ##__VA_ARGS__); };
void set_self(const char *argv0) {
char *p;
self = argv0;
p = strrchr(self, '/');
if(p) self = p + 1;
}
void print_help(void) {
/* helptext comes from usage.c, which gets generated by mkusage.pl,
from the content in the the .rst file. */
extern const char *helptext[];
const char **helpline;
puts("marsond " VERSION ", by B. Watson <urchlay@slackware.uk>, WTFPL");
puts("Usage:");
for(helpline = helptext; *helpline; helpline++)
puts(*helpline);
}
void version(void) {
puts(VERSION);
}
void parse_args(int argc, char **argv) {
int opt;
opterr = 0;
if(argc >= 2) {
if(strcmp(argv[1], "--help") == 0) {
print_help();
exit(0);
} else if(strcmp(argv[1], "--version") == 0) {
version();
exit(0);
}
}
while( (opt = getopt(argc, argv, "d:hk:vV")) != -1) {
switch(opt) {
case 'v': debugging++; break;
case 'd': delay_ms = atoi(optarg); break;
case 'k': keyboard_dev = optarg; break;
case 'h': print_help(); exit(0); break;
case 'V': version(); exit(0); break;
case '?': die("invalid option (try --help)"); break;
default: print_help(); exit(1); break;
}
}
if(delay_ms < 1) die("invalid -d argument");
}
void daemonize(void) {
int pid;
pid = fork();
if(pid < 0) {
die("fork() failed");
} else if(pid) {
/* parent */
exit(0);
}
/* we are the child here */
setsid();
pid = fork();
if(pid < 0) {
die("fork() failed");
} else if(pid) {
/* 2nd generation parent */
info("forked to background, PID %d\n", pid);
exit(0);
}
/* we are the grandchild here */
chdir("/");
close(0);
close(1);
close(2);
}
int main(int argc, char **argv) {
int infd, outfd, i;
struct uinput_user_dev dev;
struct input_event ev;
set_self(argv[0]);
debug("starting up");
parse_args(argc, argv);
if((outfd = open("/dev/uinput", O_WRONLY | O_NONBLOCK)) >= 0) {
debug("opened /dev/uinput");
} else {
die("failed to open /dev/uinput: %s", strerror(errno));
}
if((infd = open(keyboard_dev, O_RDONLY)) >= 0) {
debug("opened %s", keyboard_dev);
} else {
die("failed to open %s: %s", keyboard_dev, strerror(errno));
}
if(ioctl(infd, EVIOCGRAB, 1) >= 0) {
debug("grabbed %s", keyboard_dev);
} else {
die("failed to grab %s: %s", keyboard_dev, strerror(errno));
}
if(ioctl(outfd, UI_SET_EVBIT, EV_KEY) >= 0) {
debug("UI_SET_EVBIT OK");
} else {
die("UI_SET_EVBIT failed: %s", strerror(errno));
}
for(i = 0; i < KEY_MAX; i++) {
if(ioctl(outfd, UI_SET_KEYBIT, i) >= 0) {
/* we don't wanna be *that* verbose */
} else {
die("UI_SET_EVBIT failed: %s", strerror(errno));
}
}
debug("UI_SET_KEYBIT OK");
memset(&dev, 0, sizeof(dev));
snprintf(dev.name, UINPUT_MAX_NAME_SIZE, "marsond virtual keyboard");
dev.id.bustype = BUS_USB;
dev.id.vendor = 0x69;
dev.id.product = 0x666;
dev.id.version = 1;
if(write(outfd, &dev, sizeof(dev)) >= 0) {
debug("wrote dev structure to /dev/uinput");
} else {
die("write to /dev/uinput failed: %s", strerror(errno));
}
if(ioctl(outfd, UI_DEV_CREATE) >= 0) {
debug("created virtual keyboard device");
} else {
die("UI_DEV_CREATE failed: %s", strerror(errno));
}
if(!debugging) daemonize();
/* note: don't call die() unless debugging is true, since daemonize()
closes our stderr (there's nowhere for the error message to print). */
while(1) {
if(read(infd, &ev, sizeof(ev)) < 0) {
if(debugging)
die("read from %s failed: %s", keyboard_dev, strerror(errno));
}
debug("event: type %d, code %d, value %d\n",
ev.type, ev.code, ev.value);
if(ev.type == EV_KEY && ev.code == KEY_ENTER && ev.value == 0) {
debug("got Enter key release, delaying %d ms", delay_ms);
usleep(delay_ms * 1000);
}
if(write(outfd, &ev, sizeof(ev)) < 0) {
if(debugging)
die("write to /dev/uinput failed: %s", strerror(errno));
}
}
exit(1);
}
/*
for the A key:
event: type 4, code 4, value 458756
event: type 1, code 30, value 1
event: type 0, code 0, value 0
event: type 4, code 4, value 458756
event: type 1, code 30, value 0
event: type 0, code 0, value 0
for Enter:
event: type 4, code 4, value 458792
event: type 1, code 28, value 1
event: type 0, code 0, value 0
event: type 4, code 4, value 458792
event: type 1, code 28, value 0
event: type 0, code 0, value 0
*/
|