diff options
| author | Akshay Nair <phenax5@gmail.com> | 2020-06-16 20:08:25 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2020-06-16 20:09:14 +0530 |
| commit | 7bc460eb28b829537ba9b117eba238d4eaf7b059 (patch) | |
| tree | 19244b8b5d43d0e0ccb0060d5d2df3736b096202 /daemonic.c | |
| download | shotkey-7bc460eb28b829537ba9b117eba238d4eaf7b059.tar.gz shotkey-7bc460eb28b829537ba9b117eba238d4eaf7b059.zip | |
Init commit with all the shit
Diffstat (limited to 'daemonic.c')
| -rw-r--r-- | daemonic.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/daemonic.c b/daemonic.c new file mode 100644 index 0000000..b025224 --- /dev/null +++ b/daemonic.c @@ -0,0 +1,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); +} + |
