From 01610d88d7a9c7f8289355e8235fa07d7ea4f6e3 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sun, 21 Nov 2021 09:47:59 +0500 Subject: [PATCH] Restart by shortcut --- src/config.def.h | 1 + src/dwm.c | 6 ++++++ src/main.c | 32 +++++++++++++++++++++++++++++--- src/main.h | 4 ++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/config.def.h b/src/config.def.h index 8a18796..42191b6 100644 --- a/src/config.def.h +++ b/src/config.def.h @@ -34,6 +34,7 @@ static const Layout layouts[] = { static Key keys[] = { // WM { MODKEY|ControlMask|ShiftMask, XK_q, quit, {0} }, + { MODKEY|ControlMask|ShiftMask, XK_r, dorestart, {0} }, // Monitor { MODKEY, XK_bracketleft, focusmon, {.i = -1 } }, { MODKEY, XK_bracketright, focusmon, {.i = +1 } }, diff --git a/src/dwm.c b/src/dwm.c index b697c53..9f5102d 100644 --- a/src/dwm.c +++ b/src/dwm.c @@ -164,6 +164,7 @@ static Monitor *createmon(); static void detach(Client *c); static void detachstack(Client *c); static Monitor *dirtomon(int dir); +static void dorestart(const Arg *arg); static void focus(Client *c); static void focusmon(const Arg *arg); static void focusstack(const Arg *arg); @@ -650,6 +651,11 @@ Monitor *dirtomon(int dir) return m; } +void dorestart(__attribute__((unused)) const Arg *const arg) +{ + restart(); +} + void focus(Client *c) { if (!c || !ISVISIBLE(c)) diff --git a/src/main.c b/src/main.c index c71ab96..b3d2a87 100644 --- a/src/main.c +++ b/src/main.c @@ -10,9 +10,12 @@ #include #include #include +#include #define PROGRAM_NAME "polytreewm" +static char *program_exe = NULL; + static void signal_callback(int signo); static void logger(const char *level, const char *fmt, ...); @@ -30,6 +33,10 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } + if (!(program_exe = argv[0])) { + fatal("no program executable name"); + } + if (!setlocale(LC_CTYPE, "") || !dwm_has_locale_support()) { warning("no locale support"); } @@ -44,6 +51,7 @@ int main(int argc, char *argv[]) struct sigaction action; memset(&action, 0, sizeof(action)); action.sa_handler = signal_callback; + if (sigaction(SIGCHLD, &action, NULL) != 0) { fatal_perror("can't install SIGCHLD handler"); } @@ -54,10 +62,20 @@ int main(int argc, char *argv[]) void signal_callback(const int signo) { - if (signo != SIGCHLD) return; + switch (signo) { + case SIGCHLD: + // Clean up any zombies immediately. + while (waitpid(-1, NULL, WNOHANG) > 0); + break; + } +} - // Clean up any zombies immediately. - while (waitpid(-1, NULL, WNOHANG) > 0); +void restart() +{ + info("restarting"); + char *args[] = { program_exe, NULL }; + execvp(program_exe, args); + fatal_perror("restart with `execvp' failed"); } void logger(const char *const level, const char *const fmt, ...) @@ -136,3 +154,11 @@ void warning_perror(const char *const fmt, ...) logger_perror("WARN", fmt, ap); va_end(ap); } + +void info(const char *const fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + logger("INFO", fmt, ap); + va_end(ap); +} diff --git a/src/main.h b/src/main.h index 6cfeb33..d24651d 100644 --- a/src/main.h +++ b/src/main.h @@ -9,5 +9,9 @@ void fatal_nodie(const char *fmt, ...); void fatal_perror_nodie(const char *fmt, ...); void warning(const char *fmt, ...); void warning_perror(const char *fmt, ...); +void info(const char *fmt, ...); + +__attribute__((noreturn)) +void restart(); #endif // _MAIN_H