summaryrefslogtreecommitdiff
path: root/xlockkbd.c
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2024-03-04 03:41:46 -0800
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2024-03-04 03:41:46 -0800
commit33163fb30686fdac5891ec860c19996ebec7683a (patch)
tree6b78f0523e9c5fc5ccba3ab3b0910d4cda37c1de /xlockkbd.c
downloadxkbdlock-33163fb30686fdac5891ec860c19996ebec7683a.tar.gz
xkbdlock-33163fb30686fdac5891ec860c19996ebec7683a.tar.xz
initial import
Diffstat (limited to 'xlockkbd.c')
-rw-r--r--xlockkbd.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/xlockkbd.c b/xlockkbd.c
new file mode 100644
index 0000000..fd9e659
--- /dev/null
+++ b/xlockkbd.c
@@ -0,0 +1,43 @@
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <X11/Xlib.h>
+#include <X11/keysym.h>
+
+static void die(const char *s)
+{
+ fprintf(stderr, "xlockkbd: %s\n", s);
+ exit(1);
+}
+
+static bool grab_kbd(Display *d, int screen)
+{
+ return (XGrabKeyboard(d, RootWindow(d, screen), True, GrabModeAsync,
+ GrabModeAsync, CurrentTime)
+ == GrabSuccess);
+}
+
+static void wait_for_key(Display *d)
+{
+ XEvent ev;
+ while (!XNextEvent(d, &ev)) {
+ if (ev.type == KeyPress &&
+ XLookupKeysym(&ev.xkey, 0) == XK_Escape)
+ break;
+ }
+}
+
+int main()
+{
+ Display *d = XOpenDisplay(NULL);
+ if (!d)
+ die("cannot open display");
+
+ for (int i = 0; i < ScreenCount(d); ++i) {
+ if (!grab_kbd(d, i))
+ die("failed to grab keyboard");
+ }
+
+ wait_for_key(d);
+ return 0;
+}