commit d3cbf80c83fcfe278fc82b7d0ee16307f08b2a19 from: Brett Fisher date: Fri May 22 20:57:55 2026 UTC bxhkd.c: Reorder functions and add comments - Edit existing comments for brevity and readability - Organize forward function declarations based on function purpose - Reorder functions to match order in forward declarations commit - e79c0991d924dd894c2c5d070736e1df9198beef commit + d3cbf80c83fcfe278fc82b7d0ee16307f08b2a19 blob - c255f98aa4bb90f06a24b677db3ed73ad4ac32a8 blob + a24563451bd6456a4376fedc96f4a636764a0d9a --- bxhkd.c +++ bxhkd.c @@ -1,6 +1,5 @@ /* - * bxhkd.c - * + * bxhkd * A very basic X hotkey daemon, a.k.a Brett's X hotkey daemon. * See LICENSE.md and README.md for details. */ @@ -18,25 +17,28 @@ #include "config.h" -/* Set macros */ +/* Macros */ #define LENGTH(arr) (sizeof(arr) / sizeof(arr[0])) #define SOCKET_SUFFIX "/.local/tmp/bxwm.sock" - -/* Set global variables */ +/* Global Variables */ static Display *dpy; static Window root; static volatile sig_atomic_t running = 1; -/* Set forward function declarations */ +/* Forward Declarations */ +/* Utilities */ +static void sigchld(int sig); +static void spawn(const char *cmd); +/* Setup & Teardown */ +static void cleanup(void); +static void sig_term(int sig); +/* Key Management */ static void grabkeys(void); static void keypress(XKeyEvent *ev); -static void spawn(const char *cmd); static void mappingnotify(XMappingEvent *ev); +/* Inter-Process Communication */ static void send_cmd(const char *cmd); -static void sigchld(int sig); -static void sig_term(int sig); -static void cleanup(void); int main(void) @@ -68,6 +70,55 @@ main(void) } static void +sigchld(int sig) +{ + (void)sig; + while (waitpid(-1, NULL, WNOHANG) > 0); +} + +static void +spawn(const char *cmd) +{ + char buf[256]; + char *argv[64]; + char *tok; + int argc = 0; + + strncpy(buf, cmd, sizeof(buf) - 1); + buf[sizeof(buf) - 1] = '\0'; + + tok = strtok(buf, " "); + while (tok && argc < 63) { + argv[argc++] = tok; + tok = strtok(NULL, " "); + } + argv[argc] = NULL; + + if (fork() == 0) { + if (dpy) + close(ConnectionNumber(dpy)); + setsid(); + execvp(argv[0], argv); + fprintf(stderr, "bxhkd: execvp %s failed\n", argv[0]); + _exit(EXIT_FAILURE); + } +} + +static void +cleanup(void) +{ + XUngrabKey(dpy, AnyKey, AnyModifier, root); + XCloseDisplay(dpy); +} + +static void +sig_term(int sig) +{ + (void)sig; + running = 0; +} + +static void grabkeys(void) { unsigned int i, j; @@ -104,34 +155,6 @@ keypress(XKeyEvent *ev) } static void -spawn(const char *cmd) -{ - char buf[256]; - char *argv[64]; - char *tok; - int argc = 0; - - strncpy(buf, cmd, sizeof(buf) - 1); - buf[sizeof(buf) - 1] = '\0'; - - tok = strtok(buf, " "); - while (tok && argc < 63) { - argv[argc++] = tok; - tok = strtok(NULL, " "); - } - argv[argc] = NULL; - - if (fork() == 0) { - if (dpy) - close(ConnectionNumber(dpy)); - setsid(); - execvp(argv[0], argv); - fprintf(stderr, "bxhkd: execvp %s failed\n", argv[0]); - _exit(EXIT_FAILURE); - } -} - -static void mappingnotify(XMappingEvent *ev) { XRefreshKeyboardMapping(ev); @@ -162,24 +185,3 @@ send_cmd(const char *cmd) { close(sock); } -static void -sigchld(int sig) -{ - (void)sig; - while (waitpid(-1, NULL, WNOHANG) > 0); -} - -static void -sig_term(int sig) -{ - (void)sig; - running = 0; -} - -static void -cleanup(void) -{ - XUngrabKey(dpy, AnyKey, AnyModifier, root); - XCloseDisplay(dpy); -} -