Restart by shortcut

This commit is contained in:
Alex Kotov 2021-11-21 09:47:59 +05:00
parent 6b8d87d1ae
commit 01610d88d7
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
4 changed files with 40 additions and 3 deletions

View File

@ -34,6 +34,7 @@ static const Layout layouts[] = {
static Key keys[] = { static Key keys[] = {
// WM // WM
{ MODKEY|ControlMask|ShiftMask, XK_q, quit, {0} }, { MODKEY|ControlMask|ShiftMask, XK_q, quit, {0} },
{ MODKEY|ControlMask|ShiftMask, XK_r, dorestart, {0} },
// Monitor // Monitor
{ MODKEY, XK_bracketleft, focusmon, {.i = -1 } }, { MODKEY, XK_bracketleft, focusmon, {.i = -1 } },
{ MODKEY, XK_bracketright, focusmon, {.i = +1 } }, { MODKEY, XK_bracketright, focusmon, {.i = +1 } },

View File

@ -164,6 +164,7 @@ static Monitor *createmon();
static void detach(Client *c); static void detach(Client *c);
static void detachstack(Client *c); static void detachstack(Client *c);
static Monitor *dirtomon(int dir); static Monitor *dirtomon(int dir);
static void dorestart(const Arg *arg);
static void focus(Client *c); static void focus(Client *c);
static void focusmon(const Arg *arg); static void focusmon(const Arg *arg);
static void focusstack(const Arg *arg); static void focusstack(const Arg *arg);
@ -650,6 +651,11 @@ Monitor *dirtomon(int dir)
return m; return m;
} }
void dorestart(__attribute__((unused)) const Arg *const arg)
{
restart();
}
void focus(Client *c) void focus(Client *c)
{ {
if (!c || !ISVISIBLE(c)) if (!c || !ISVISIBLE(c))

View File

@ -10,9 +10,12 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h>
#define PROGRAM_NAME "polytreewm" #define PROGRAM_NAME "polytreewm"
static char *program_exe = NULL;
static void signal_callback(int signo); static void signal_callback(int signo);
static void logger(const char *level, const char *fmt, ...); static void logger(const char *level, const char *fmt, ...);
@ -30,6 +33,10 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (!(program_exe = argv[0])) {
fatal("no program executable name");
}
if (!setlocale(LC_CTYPE, "") || !dwm_has_locale_support()) { if (!setlocale(LC_CTYPE, "") || !dwm_has_locale_support()) {
warning("no locale support"); warning("no locale support");
} }
@ -44,6 +51,7 @@ int main(int argc, char *argv[])
struct sigaction action; struct sigaction action;
memset(&action, 0, sizeof(action)); memset(&action, 0, sizeof(action));
action.sa_handler = signal_callback; action.sa_handler = signal_callback;
if (sigaction(SIGCHLD, &action, NULL) != 0) { if (sigaction(SIGCHLD, &action, NULL) != 0) {
fatal_perror("can't install SIGCHLD handler"); fatal_perror("can't install SIGCHLD handler");
} }
@ -54,10 +62,20 @@ int main(int argc, char *argv[])
void signal_callback(const int signo) 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. void restart()
while (waitpid(-1, NULL, WNOHANG) > 0); {
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, ...) 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); logger_perror("WARN", fmt, ap);
va_end(ap); va_end(ap);
} }
void info(const char *const fmt, ...)
{
va_list ap;
va_start(ap, fmt);
logger("INFO", fmt, ap);
va_end(ap);
}

View File

@ -9,5 +9,9 @@ void fatal_nodie(const char *fmt, ...);
void fatal_perror_nodie(const char *fmt, ...); void fatal_perror_nodie(const char *fmt, ...);
void warning(const char *fmt, ...); void warning(const char *fmt, ...);
void warning_perror(const char *fmt, ...); void warning_perror(const char *fmt, ...);
void info(const char *fmt, ...);
__attribute__((noreturn))
void restart();
#endif // _MAIN_H #endif // _MAIN_H