meep.c (1743B)
1 #include <libnotify/notify.h> 2 3 #include <X11/XKBlib.h> 4 #include <X11/extensions/XInput2.h> 5 6 #include <string.h> 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <stdbool.h> 10 11 int main(int argc, char * argv[]) { 12 int xiOpcode, queryEvent, queryError; 13 14 notify_init("mehehe meep"); 15 16 Display * disp = XOpenDisplay(":0"); 17 Window root = DefaultRootWindow(disp); 18 if (!XQueryExtension(disp, "XInputExtension", &xiOpcode, &queryEvent, &queryError)) return 2; 19 20 { 21 XIEventMask m; 22 m.deviceid = XIAllMasterDevices; 23 m.mask_len = XIMaskLen(XI_LASTEVENT); 24 m.mask = calloc(m.mask_len, sizeof(char)); 25 XISetMask(m.mask, XI_RawKeyPress); 26 XISelectEvents(disp, root, &m, 1); 27 XSync(disp, false); 28 free(m.mask); 29 } 30 31 XkbSelectEventDetails(disp, XkbUseCoreKbd, XkbStateNotify, XkbGroupStateMask, XkbGroupStateMask); 32 33 char* matches[] = {"mehehe", "meep", "goop"}; 34 int states[sizeof(matches) / sizeof(*matches)] = {0}; 35 36 for (;;) { 37 XEvent event; 38 XGenericEventCookie *cookie = (XGenericEventCookie*)&event.xcookie; 39 XNextEvent(disp, &event); 40 41 if (XGetEventData(disp, cookie)) { 42 if (cookie->extension == xiOpcode && cookie->evtype == XI_RawKeyPress) { 43 XIRawEvent *ev = cookie->data; 44 KeySym s = XkbKeycodeToKeysym(disp, ev->detail, 0, 0); 45 char *str = XKeysymToString(s); 46 if (NULL == str) continue; 47 48 for (int i = 0; i < sizeof(matches) / sizeof(*matches); i++) { 49 if (*str == matches[i][states[i]]) { 50 states[i]++; 51 if (matches[i][states[i]] == 0) { 52 NotifyNotification* notif = notify_notification_new(matches[i], matches[i], NULL); 53 notify_notification_show(notif, NULL); 54 states[i] = 0; 55 } 56 } else { 57 states[i] = 0; 58 } 59 } 60 } 61 XFreeEventData(disp, cookie); 62 } 63 } 64 }