-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyevent.showkey.c
427 lines (346 loc) · 7.89 KB
/
keyevent.showkey.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
/*
this code based on `kbd` package, showkey.c
GPL v2
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <getopt.h>
#include <fcntl.h>
#include <signal.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
#include <linux/keyboard.h>
#include <string.h>
#include <stdarg.h>
#include <locale.h>
#include <libintl.h>
//------------------------ keyevent
#include <linux/input.h>
#define KEY_CTRL 29
#define KEY_ALT 56
#define KEY_LEFT 105
#define KEY_RIGHT 106
// #define K_REPT 2
// #define K_DOWN 1
// #define K_UP 0
int st_ctrl = 0;
int st_alt = 0;
char cmd_l[256];
char cmd_r[256];
void init_cmd(){
const char *cond = "xdotool getwindowfocus getwindowname | grep --color -- ' - Oracle VM VirtualBox$' && ";
snprintf(cmd_l, 255, "%s c=`xdotool get_desktop` && xdotool set_desktop -- $((c-1))", cond);
snprintf(cmd_r, 255, "%s c=`xdotool get_desktop` && xdotool set_desktop -- $((c+1))", cond);
printf("cmd_l: %s\n", cmd_l);
printf("cmd_r: %s\n", cmd_r);
}
int key_event(struct input_event *ev) {
switch(ev->code){
case KEY_CTRL:
st_ctrl = ev->value; break;
case KEY_ALT:
st_alt = ev->value; break;
case KEY_LEFT:
if(st_alt & st_ctrl){
if(ev->value){
printf("\e[033m key-combine: ctrl+alt+left! \e[0m\n");
system(cmd_l);
}
}
break;
case KEY_RIGHT:
if(st_alt & st_ctrl){
if(ev->value){
printf("\e[033m key-combine: ctrl+alt+right! \e[0m\n");
system(cmd_r);
}
}
break;
}
return 0;
}
/*
struct input_event {
struct timeval time;
__u16 type;
__u16 code;
__s32 value;
};
*/
struct input_event ev;
void key_process(int code, int val){
ev.code = code;
ev.value = val;
key_event(&ev);
}
//------------------------ vars
int fd;
int oldkbmode;
struct termios old;
// #define _(Text) gettext(Text)
#define _(Text) (Text)
// #define LC_ALL C
#define PACKAGE_NAME "kbd"
#define LOCALEDIR "/usr/share/locale"
//------------------------
//------------------------
//------------------------
//------------------------ getfd
static char *conspath[] = {
"/proc/self/fd/0",
"/dev/tty",
"/dev/tty0",
"/dev/vc/0",
"/dev/systty",
"/dev/console",
NULL
};
/*
* getfd.c
*
* Get an fd for use with kbd/console ioctls.
* We try several things because opening /dev/console will fail
* if someone else used X (which does a chown on /dev/console).
*/
static int
is_a_console(int fd)
{
char arg;
arg = 0;
return (isatty(fd) && ioctl(fd, KDGKBTYPE, &arg) == 0 && ((arg == KB_101) || (arg == KB_84)));
}
static int
open_a_console(const char *fnam)
{
int fd;
/*
* For ioctl purposes we only need some fd and permissions
* do not matter. But setfont:activatemap() does a write.
*/
fd = open(fnam, O_RDWR);
if (fd < 0)
fd = open(fnam, O_WRONLY);
if (fd < 0)
fd = open(fnam, O_RDONLY);
if (fd < 0)
return -1;
return fd;
}
int getfd(const char *fnam)
{
int fd, i;
if (fnam) {
if ((fd = open_a_console(fnam)) >= 0) {
if (is_a_console(fd))
return fd;
close(fd);
}
fprintf(stderr, _("Couldn't open %s\n"), fnam);
exit(1);
}
for (i = 0; conspath[i]; i++) {
if ((fd = open_a_console(conspath[i])) >= 0) {
if (is_a_console(fd))
return fd;
close(fd);
}
}
for (fd = 0; fd < 3; fd++)
if (is_a_console(fd))
return fd;
fprintf(stderr,
_("Couldn't get a file descriptor referring to the console\n"));
/* total failure */
exit(1);
}
static int
kbmode(int fd) {
int mode;
if (ioctl(fd, KDGKBMODE, &mode) < 0)
return -1;
else
return mode;
}
//------------------------ kbd_error
static const char *progname = NULL;
static inline const char *
set_progname(const char *name)
{
char *p;
p = strrchr(name, '/');
return (p && p + 1 ? p + 1 : name);
}
void
__attribute__((format(printf, 2, 3)))
kbd_warning(const int errnum, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "%s: ", progname);
vfprintf(stderr, fmt, ap);
if (errnum > 0)
fprintf(stderr, ": %s\n", strerror(errnum));
va_end(ap);
return;
}
void
__attribute__((noreturn))
__attribute__((format(printf, 3, 4)))
kbd_error(const int exitnum, const int errnum, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "%s: ", progname);
vfprintf(stderr, fmt, ap);
va_end(ap);
if (errnum > 0)
fprintf(stderr, ": %s\n", strerror(errnum));
exit(exitnum);
}
//------------------------
/*
* version 0.81 of showkey would restore kbmode unconditially to XLATE,
* thus making the console unusable when it was called under X.
*/
static void
get_mode(void)
{
char *m;
if (ioctl(fd, KDGKBMODE, &oldkbmode)) {
kbd_error(EXIT_FAILURE, errno, "ioctl KDGKBMODE");
}
switch (oldkbmode) {
case K_RAW:
m = "RAW";
break;
case K_XLATE:
m = "XLATE";
break;
case K_MEDIUMRAW:
m = "MEDIUMRAW";
break;
case K_UNICODE:
m = "UNICODE";
break;
default:
m = _("?UNKNOWN?");
break;
}
printf(_("kb mode was %s\n"), m);
if (oldkbmode != K_XLATE) {
printf(_("[ if you are trying this under X, it might not work\n"
"since the X server is also reading /dev/console ]\n"));
}
printf("\n");
}
static void
clean_up(void)
{
if (ioctl(fd, KDSKBMODE, oldkbmode)) {
kbd_error(EXIT_FAILURE, errno, "ioctl KDSKBMODE");
}
if (tcsetattr(fd, 0, &old) == -1)
kbd_warning(errno, "tcsetattr");
close(fd);
}
static void __attribute__((noreturn))
die(int x)
{
printf(_("caught signal %d, cleaning up...\n"), x);
clean_up();
exit(EXIT_FAILURE);
}
static void __attribute__((noreturn))
watch_dog(int x __attribute__((unused)))
{
clean_up();
exit(EXIT_SUCCESS);
}
//------------------------ main
int main(int argc, char *argv[])
{
int show_keycodes = 1;
struct termios new;
unsigned char buf[18]; /* divisible by 3 */
int i, n;
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE_NAME, LOCALEDIR);
textdomain(PACKAGE_NAME);
init_cmd();
//------------------------
if ((fd = getfd(NULL)) < 0)
kbd_error(EXIT_FAILURE, 0, _("Couldn't get a file descriptor referring to the console"));
/* the program terminates when there is no input for 10 secs */
signal(SIGALRM, watch_dog);
/*
if we receive a signal, we want to exit nicely, in
order not to leave the keyboard in an unusable mode
*/
signal(SIGHUP, die);
signal(SIGINT, die);
signal(SIGQUIT, die);
signal(SIGILL, die);
signal(SIGTRAP, die);
signal(SIGABRT, die);
signal(SIGIOT, die);
signal(SIGFPE, die);
signal(SIGKILL, die);
signal(SIGUSR1, die);
signal(SIGSEGV, die);
signal(SIGUSR2, die);
signal(SIGPIPE, die);
signal(SIGTERM, die);
#ifdef SIGSTKFLT
signal(SIGSTKFLT, die);
#endif
// signal(SIGCHLD, die); sig 17
signal(SIGCONT, die);
signal(SIGSTOP, die);
signal(SIGTSTP, die);
signal(SIGTTIN, die);
signal(SIGTTOU, die);
get_mode();
if (tcgetattr(fd, &old) == -1)
kbd_warning(errno, "tcgetattr");
if (tcgetattr(fd, &new) == -1)
kbd_warning(errno, "tcgetattr");
new.c_lflag &= ~(ICANON | ECHO | ISIG);
new.c_iflag = 0;
new.c_cc[VMIN] = sizeof(buf);
new.c_cc[VTIME] = 1; /* 0.1 sec intercharacter timeout */
if (tcsetattr(fd, TCSAFLUSH, &new) == -1)
kbd_warning(errno, "tcsetattr");
if (ioctl(fd, KDSKBMODE, show_keycodes ? K_MEDIUMRAW : K_RAW)) {
kbd_error(EXIT_FAILURE, errno, "ioctl KDSKBMODE");
}
// printf(_("press any key (program terminates 10s after last keypress)...\n"));
//------------------------
/* show keycodes - 2.6 allows 3-byte reports */
// has repeat keys
while (1) {
// alarm(10);
n = read(fd, buf, sizeof(buf));
i = 0;
while (i < n) {
int kc;
char *s;
s = (buf[i] & 0x80) ? _("up") : _("down");
if (i + 2 < n && (buf[i] & 0x7f) == 0 && (buf[i + 1] & 0x80) != 0 && (buf[i + 2] & 0x80) != 0) {
kc = ((buf[i + 1] & 0x7f) << 7) |
(buf[i + 2] & 0x7f);
i += 3;
} else {
kc = (buf[i] & 0x7f);
i++;
}
printf(_("\rkey %3d %s\n"), kc, s);
int key_down = !(buf[i] & 0x80);
key_process(kc, key_down);
}
}
clean_up();
return EXIT_SUCCESS;
}