diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2024-03-04 16:35:56 -0800 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2024-03-04 16:35:56 -0800 |
commit | 10b52c0595dfbde3e504a34b14023620237b39f1 (patch) | |
tree | ebc81b4f6400dae9e496fbd7b1e693a37de3cb03 | |
parent | f91c93e26fe5e891545dec7b2bdeeea2d6bd56e2 (diff) | |
download | xkbdlock-10b52c0595dfbde3e504a34b14023620237b39f1.tar.gz xkbdlock-10b52c0595dfbde3e504a34b14023620237b39f1.tar.xz |
improve error messages and cleanup
-rw-r--r-- | xkbdlock.c | 25 |
1 files changed, 14 insertions, 11 deletions
@@ -1,3 +1,4 @@ +#include <errno.h> #include <stdarg.h> #include <stdbool.h> #include <stdio.h> @@ -12,7 +13,7 @@ struct named_mask { unsigned int mask; }; -static const struct named_mask named_masks[] = { +static const struct named_mask NAMED_MASKS[] = { { "Shift", ShiftMask }, { "Control", ControlMask }, { "Ctrl", ControlMask }, @@ -24,12 +25,12 @@ static const struct named_mask named_masks[] = { { "Mod4", Mod4Mask }, { "Mod5", Mod5Mask } }; -#define len_named_masks (sizeof(named_masks) / sizeof(*named_masks)) +#define NUM_NAMED_MASKS (sizeof(NAMED_MASKS) / sizeof(*NAMED_MASKS)) -static unsigned int str_to_mask(const char *s) +static unsigned int name_to_mask(const char *s) { - for (size_t i = 0; i < len_named_masks; ++i) { - const struct named_mask *m = named_masks + i; + for (const struct named_mask *m = NAMED_MASKS; + m < NAMED_MASKS + NUM_NAMED_MASKS; ++m) { if (!strcmp(s, m->name)) return m->mask; } @@ -46,7 +47,7 @@ static void die(const char *fmt, ...) if (vasprintf(&s, fmt, ap) != -1) fprintf(stderr, "xkbdlock: %s\n", s); else - fprintf(stderr, "xkbdlock: unknown error\n"); + fprintf(stderr, "xkbdlock: %s\n", strerror(errno)); va_end(ap); exit(1); @@ -88,15 +89,15 @@ int main(int argc, char **argv) KeySym key = NoSymbol, arg_key; for (int i = 1; i < argc; ++i) { - if ((arg_mask = str_to_mask(argv[i]))) { + if ((arg_mask = name_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]); + die("unknown key '%s'", argv[i]); if (arg_key >= XK_A && arg_key <= XK_Z) - die("use Shift modifier for uppercase key: %s", + die("use Shift modifier for uppercase key '%s'", argv[i]); key = arg_key; } @@ -105,16 +106,18 @@ int main(int argc, char **argv) if (key == NoSymbol) { if (mask) die("no non-modifier key provided"); + fprintf(stderr, + "xkbdlock: no release key provided, using Escape\n"); key = XK_Escape; } Display *d = XOpenDisplay(NULL); if (!d) - die("cannot open display"); + die("cannot open display '%s'", getenv("DISPLAY")); for (int i = 0; i < ScreenCount(d); ++i) { if (!grab_kbd(d, i)) - die("failed to grab keyboard"); + die("failed to grab keyboard on screen %d", i); } wait_for_release(d, key, mask); |