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
|
#include<stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
typedef struct Key {
unsigned int mod;
KeySym key;
char* command;
} Key;
#include "config.h"
#define LENGTH(X) (sizeof X / sizeof X[0])
void
bind_key(Display *dpy, Window win, unsigned int mod, KeySym key)
{
int keycode = XKeysymToKeycode(dpy, key);
XGrabKey(dpy, keycode, mod, win, False, GrabModeAsync, GrabModeAsync);
}
int
error_handler(Display *disp, XErrorEvent *xe)
{
if (xe->error_code == BadAccess) {
printf("daemonic: [BadAccess] Cant grab key binding. Already grabbed\n");
return 0;
}
printf("daemonic: Something went wrong");
return 1;
}
int
main()
{
int running = 10;
XSetErrorHandler(error_handler);
Display *dpy = XOpenDisplay(0);
Window root = DefaultRootWindow(dpy);
// Grab keys
int i;
for (i = 0; i < LENGTH(keys); i++) {
bind_key(dpy, root, keys[i].mod, keys[i].key);
}
XSelectInput(dpy, root, KeyPressMask);
/* main event loop */
XEvent ev;
XSync(dpy, False);
while (running && !XNextEvent(dpy, &ev)) {
if (ev.type == KeyPress) {
printf("Key: %d\n", ev.xkey.keycode);
running--;
}
}
XCloseDisplay(dpy);
}
|