sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

dwm-r1615-selfrestart.diff (2817B)


      1 # HG changeset patch
      2 # User Barbu Paul - Gheorghe <barbu.paul.gheorghe@gmail.com>
      3 # Date 1354650884 -7200
      4 # Node ID 6c472a21a5887c5295a331c48c4da188ec2c8413
      5 # Parent  aaab44133a6830c9a00263731d098c01cc1d6fb5
      6 selfrestart now magically locates the current dwm (no need to hardcode a path)
      7 
      8 diff -r aaab44133a68 -r 6c472a21a588 config.def.h
      9 --- a/config.def.h	Tue Dec 04 21:54:44 2012 +0200
     10 +++ b/config.def.h	Tue Dec 04 21:54:44 2012 +0200
     11 @@ -54,6 +54,8 @@
     12  static const char *termcmd[]  = { "urxvtc", NULL };
     13  static const char *filemancmd[]  = { "thunar", NULL };
     14  
     15 +#include "selfrestart.c"
     16 +
     17  static Key keys[] = {
     18  	/* modifier                     key        function        argument */
     19  	{ MODKEY,                       XK_r,      spawn,          {.v = dmenucmd } },
     20 @@ -89,6 +91,7 @@
     21  	TAGKEYS(                        XK_7,                      6)
     22  	TAGKEYS(                        XK_8,                      7)
     23  	TAGKEYS(                        XK_9,                      8)
     24 +    { MODKEY|ShiftMask,             XK_r,      self_restart,   {0} },
     25  	{ MODKEY|ShiftMask,             XK_q,      quit,           {0} },
     26  };
     27  
     28 @@ -108,4 +111,3 @@
     29  	{ ClkTagBar,            MODKEY,         Button1,        tag,            {0} },
     30  	{ ClkTagBar,            MODKEY,         Button3,        toggletag,      {0} },
     31  };
     32 -
     33 diff -r aaab44133a68 -r 6c472a21a588 selfrestart.c
     34 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     35 +++ b/selfrestart.c	Tue Dec 04 21:54:44 2012 +0200
     36 @@ -0,0 +1,65 @@
     37 +#include <unistd.h>
     38 +#include <sys/types.h>
     39 +#include <sys/stat.h>
     40 +#include <stdio.h>
     41 +#include <stdlib.h>
     42 +
     43 +/**
     44 + * Magically finds the current's executable path
     45 + *
     46 + * I'm doing the do{}while(); trick because Linux (what I'm running) is not
     47 + * POSIX compilant and so lstat() cannot be trusted on /proc entries
     48 + *
     49 + * @return char* the path of the current executable
     50 + */
     51 +char *get_dwm_path(){
     52 +    struct stat s;
     53 +    int r, length, rate = 42;
     54 +    char *path = NULL;
     55 +
     56 +    if(lstat("/proc/self/exe", &s) == -1){
     57 +        perror("lstat:");
     58 +        return NULL;
     59 +    }
     60 +
     61 +    length = s.st_size + 1 - rate;
     62 +
     63 +    do{
     64 +        length+=rate;
     65 +
     66 +        free(path);
     67 +        path = malloc(sizeof(char) * length);
     68 +
     69 +        if(path == NULL){
     70 +            perror("malloc:");
     71 +            return NULL;
     72 +        }
     73 +
     74 +        r = readlink("/proc/self/exe", path, length);
     75 +
     76 +        if(r == -1){
     77 +            perror("readlink:");
     78 +            return NULL;
     79 +        }
     80 +    }while(r >= length);
     81 +
     82 +    path[r] = '\0';
     83 +
     84 +    return path;
     85 +}
     86 +
     87 +/**
     88 + * self-restart
     89 + *
     90 + * Initially inspired by: Yu-Jie Lin
     91 + * https://sites.google.com/site/yjlnotes/notes/dwm
     92 + */
     93 +void self_restart(const Arg *arg) {
     94 +    char *const argv[] = {get_dwm_path(), NULL};
     95 +
     96 +    if(argv[0] == NULL){
     97 +        return;
     98 +    }
     99 +
    100 +    execv(argv[0], argv);
    101 +}