-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxsk.c
106 lines (81 loc) · 2.38 KB
/
xsk.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
/* Adapted from https://www.cs.bgu.ac.il/~orlovm/code/realkey.C */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
int cont = 1; // flag for while loop
int result = 0;
int check = 0;
const char *const *checkChord;
Display *display = 0; // X server connection
const char* key_map[32768]; // key names
void quitter(int sig) {
cont = 0;
}
int main(int argc, const char *const *argv) {
if(signal(SIGINT, &quitter) == SIG_ERR || signal(SIGTERM, &quitter) == SIG_ERR) {
fprintf(stderr, "Could not register signal handlers.");
exit(1);
}
if(argc > 1) {
check = 1;
checkChord = argv;
}
/*
* open the display
*/
if (! (display = XOpenDisplay(getenv("DISPLAY")))) {
fprintf(stderr, "Can't open DISPLAY=%s", getenv("DISPLAY"));
exit(1);
}
/*
* fill up keyboard mapping
*/
int min_key_code, max_key_code; // key codes range
XDisplayKeycodes(display, &min_key_code, &max_key_code);
for (int code = min_key_code; code <= max_key_code; ++code) {
const char *keysym = XKeysymToString(XKeycodeToKeysym(display, code, 0));
key_map[code] = keysym ? keysym : "NoSymbol";
}
/*
* fill relevant key buffers
*/
#define KEYS_LEN 32
char keys[KEYS_LEN]; // buffer for reading keys in
char lastkeys[KEYS_LEN]; // previous keys state
memset(lastkeys, 0, KEYS_LEN);
while (cont) {
int check_success = 0;
usleep(10000);
XQueryKeymap(display, keys);
// print changed keys
for (unsigned int i = 0; i < KEYS_LEN; ++i) {
// check which key got changed
for (unsigned int j = 0, test = 1; j < 8; ++j, test <<= 1) {
int code = i*8+j;
if(code < min_key_code || code > max_key_code) continue;
const char* key = key_map[code];
int key_down = keys[i] & test ? 1 : 0;
if(check) {
for(int i=1; i<argc; i++) {
if(key_down && !strcmp(key, argv[i])) check_success = 1;
}
}
else if ((keys[i] & test) != (lastkeys[i] & test)) {
printf("%s %d\n", key, key_down);
fflush(stdout);
}
}
lastkeys[i] = keys[i];
}
if(check) {
result = !check_success;
cont = 0;
}
}
XCloseDisplay(display);
exit(result);
}