From f91c93e26fe5e891545dec7b2bdeeea2d6bd56e2 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 4 Mar 2024 15:23:11 -0800 Subject: rename --- Makefile | 2 +- xkbdlock.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ xlockkbd.c | 122 ------------------------------------------------------------- 3 files changed, 123 insertions(+), 123 deletions(-) create mode 100644 xkbdlock.c delete mode 100644 xlockkbd.c diff --git a/Makefile b/Makefile index 066d2ac..8278faf 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CFLAGS += -O2 -Wall -Wextra -Wpedantic LDFLAGS += -lX11 -TARGET := xlockkbd +TARGET := xkbdlock SRC := $(wildcard *.c) OBJ := $(SRC:.c=.o) diff --git a/xkbdlock.c b/xkbdlock.c new file mode 100644 index 0000000..0cdaedf --- /dev/null +++ b/xkbdlock.c @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +struct named_mask { + const char *name; + unsigned int mask; +}; + +static const struct named_mask named_masks[] = { + { "Shift", ShiftMask }, + { "Control", ControlMask }, + { "Ctrl", ControlMask }, + { "Alt", Mod1Mask }, + { "Super", Mod4Mask }, + { "Mod1", Mod1Mask }, + { "Mod2", Mod2Mask }, + { "Mod3", Mod3Mask }, + { "Mod4", Mod4Mask }, + { "Mod5", Mod5Mask } +}; +#define len_named_masks (sizeof(named_masks) / sizeof(*named_masks)) + +static unsigned int str_to_mask(const char *s) +{ + for (size_t i = 0; i < len_named_masks; ++i) { + const struct named_mask *m = named_masks + i; + if (!strcmp(s, m->name)) + return m->mask; + } + + return 0; +} + +static void die(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + + char *s; + if (vasprintf(&s, fmt, ap) != -1) + fprintf(stderr, "xkbdlock: %s\n", s); + else + fprintf(stderr, "xkbdlock: unknown error\n"); + + va_end(ap); + exit(1); +} + +static bool grab_kbd(Display *d, int screen) +{ + Window w = RootWindow(d, screen); + int res; + + // attempt to grab keyboard for 500ms if already grabbed. + // thanks to slock code for the pointer. + for (int i = 0;;) { + res = XGrabKeyboard(d, w, True, GrabModeAsync, GrabModeAsync, + CurrentTime); + if (res != AlreadyGrabbed || ++i > 5) + break; + + usleep(100000); + } + + return (res == GrabSuccess); +} + +static void wait_for_release(Display *d, KeySym key, unsigned int mask) +{ + XEvent ev; + while (!XNextEvent(d, &ev)) { + if (ev.type == KeyPress && + XLookupKeysym(&ev.xkey, 0) == key && + (mask & ev.xkey.state) == mask) + break; + } +} + +int main(int argc, char **argv) +{ + unsigned int mask = 0, arg_mask; + KeySym key = NoSymbol, arg_key; + + for (int i = 1; i < argc; ++i) { + if ((arg_mask = str_to_mask(argv[i]))) { + mask = mask | arg_mask; + } else if (key != NoSymbol) { + die("more than one non-modifier key provided"); + } else { + if ((arg_key = XStringToKeysym(argv[i])) == NoSymbol) + die("unknown key: %s", argv[i]); + if (arg_key >= XK_A && arg_key <= XK_Z) + die("use Shift modifier for uppercase key: %s", + argv[i]); + key = arg_key; + } + } + + if (key == NoSymbol) { + if (mask) + die("no non-modifier key provided"); + key = XK_Escape; + } + + 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_release(d, key, mask); + return 0; +} diff --git a/xlockkbd.c b/xlockkbd.c deleted file mode 100644 index 2e970c5..0000000 --- a/xlockkbd.c +++ /dev/null @@ -1,122 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -struct named_mask { - const char *name; - unsigned int mask; -}; - -static const struct named_mask named_masks[] = { - { "Shift", ShiftMask }, - { "Control", ControlMask }, - { "Ctrl", ControlMask }, - { "Alt", Mod1Mask }, - { "Super", Mod4Mask }, - { "Mod1", Mod1Mask }, - { "Mod2", Mod2Mask }, - { "Mod3", Mod3Mask }, - { "Mod4", Mod4Mask }, - { "Mod5", Mod5Mask } -}; -#define len_named_masks (sizeof(named_masks) / sizeof(*named_masks)) - -static unsigned int str_to_mask(const char *s) -{ - for (size_t i = 0; i < len_named_masks; ++i) { - const struct named_mask *m = named_masks + i; - if (!strcmp(s, m->name)) - return m->mask; - } - - return 0; -} - -static void die(const char *fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - - char *s; - if (vasprintf(&s, fmt, ap) != -1) - fprintf(stderr, "xlockkbd: %s\n", s); - else - fprintf(stderr, "xlockkbd: unknown error\n"); - - va_end(ap); - exit(1); -} - -static bool grab_kbd(Display *d, int screen) -{ - Window w = RootWindow(d, screen); - int res; - - // attempt to grab keyboard for 500ms if already grabbed. - // thanks to slock code for the pointer. - for (int i = 0;;) { - res = XGrabKeyboard(d, w, True, GrabModeAsync, GrabModeAsync, - CurrentTime); - if (res != AlreadyGrabbed || ++i > 5) - break; - - usleep(100000); - } - - return (res == GrabSuccess); -} - -static void wait_for_release(Display *d, KeySym key, unsigned int mask) -{ - XEvent ev; - while (!XNextEvent(d, &ev)) { - if (ev.type == KeyPress && - XLookupKeysym(&ev.xkey, 0) == key && - (mask & ev.xkey.state) == mask) - break; - } -} - -int main(int argc, char **argv) -{ - unsigned int mask = 0, arg_mask; - KeySym key = NoSymbol, arg_key; - - for (int i = 1; i < argc; ++i) { - if ((arg_mask = str_to_mask(argv[i]))) { - mask = mask | arg_mask; - } else if (key != NoSymbol) { - die("more than one non-modifier key provided"); - } else { - if ((arg_key = XStringToKeysym(argv[i])) == NoSymbol) - die("unknown key: %s", argv[i]); - if (arg_key >= XK_A && arg_key <= XK_Z) - die("use Shift modifier for uppercase key: %s", - argv[i]); - key = arg_key; - } - } - - if (key == NoSymbol) { - if (mask) - die("no non-modifier key provided"); - key = XK_Escape; - } - - 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_release(d, key, mask); - return 0; -} -- cgit v1.2.3-70-g09d2