1
0
mirror of https://github.com/alrayyes/dwm synced 2023-11-14 15:56:31 +00:00
dwm/local-r1615-selfrestart.diff
2020-12-01 10:52:26 +01:00

90 lines
2.5 KiB
Diff

diff -up -N a/config.def.h b/config.def.h
--- a/config.def.h 2020-12-01 10:21:09.920859006 +0100
+++ b/config.def.h 2020-12-01 10:22:27.167535028 +0100
@@ -66,6 +66,8 @@ static char dmenumon[2] = "0"; /* compon
static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL };
static const char *termcmd[] = { "st", NULL };
+#include "selfrestart.c"
+
static Key keys[] = {
/* modifier key function argument */
{ MODKEY, XK_p, spawn, {.v = dmenucmd } },
@@ -103,6 +105,7 @@ static Key keys[] = {
TAGKEYS( XK_7, 6)
TAGKEYS( XK_8, 7)
TAGKEYS( XK_9, 8)
+ { MODKEY|ShiftMask, XK_r, self_restart, {0} },
{ MODKEY|ShiftMask, XK_q, quit, {0} },
};
diff -up -N a/selfrestart.c b/selfrestart.c
--- a/selfrestart.c 1970-01-01 01:00:00.000000000 +0100
+++ b/selfrestart.c 2020-12-01 10:24:06.714211651 +0100
@@ -0,0 +1,65 @@
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+/**
+ * Magically finds the current's executable path
+ *
+ * I'm doing the do{}while(); trick because Linux (what I'm running) is not
+ * POSIX compilant and so lstat() cannot be trusted on /proc entries
+ *
+ * @return char* the path of the current executable
+ */
+char *get_dwm_path(){
+ struct stat s;
+ int r, length, rate = 42;
+ char *path = NULL;
+
+ if(lstat("/proc/self/exe", &s) == -1){
+ perror("lstat:");
+ return NULL;
+ }
+
+ length = s.st_size + 1 - rate;
+
+ do{
+ length+=rate;
+
+ free(path);
+ path = malloc(sizeof(char) * length);
+
+ if(path == NULL){
+ perror("malloc:");
+ return NULL;
+ }
+
+ r = readlink("/proc/self/exe", path, length);
+
+ if(r == -1){
+ perror("readlink:");
+ return NULL;
+ }
+ }while(r >= length);
+
+ path[r] = '\0';
+
+ return path;
+}
+
+/**
+ * self-restart
+ *
+ * Initially inspired by: Yu-Jie Lin
+ * https://sites.google.com/site/yjlnotes/notes/dwm
+ */
+void self_restart(const Arg *arg) {
+ char *const argv[] = {get_dwm_path(), NULL};
+
+ if(argv[0] == NULL){
+ return;
+ }
+
+ execv(argv[0], argv);
+}