sites

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

dwm-autostart-20200609-cb3f58a.diff (3740B)


      1 From 63ed18e9ee236bb88426fddee2d311e090aee71d Mon Sep 17 00:00:00 2001
      2 From: Gan Ainm <gan.ainm.riomhphost@gmail.com>
      3 Date: Tue, 9 Jun 2020 16:03:30 +0000
      4 Subject: [PATCH] Make autostart patch XDG conformant
      5 
      6 ---
      7  dwm.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      8  1 file changed, 92 insertions(+)
      9 
     10 diff --git a/dwm.c b/dwm.c
     11 index 4465af1..ac6ed5e 100644
     12 --- a/dwm.c
     13 +++ b/dwm.c
     14 @@ -29,6 +29,7 @@
     15  #include <string.h>
     16  #include <unistd.h>
     17  #include <sys/types.h>
     18 +#include <sys/stat.h>
     19  #include <sys/wait.h>
     20  #include <X11/cursorfont.h>
     21  #include <X11/keysym.h>
     22 @@ -193,6 +194,7 @@ static void resizeclient(Client *c, int x, int y, int w, int h);
     23  static void resizemouse(const Arg *arg);
     24  static void restack(Monitor *m);
     25  static void run(void);
     26 +static void runautostart(void);
     27  static void scan(void);
     28  static int sendevent(Client *c, Atom proto);
     29  static void sendmon(Client *c, Monitor *m);
     30 @@ -235,7 +237,11 @@ static int xerrorstart(Display *dpy, XErrorEvent *ee);
     31  static void zoom(const Arg *arg);
     32  
     33  /* variables */
     34 +static const char autostartblocksh[] = "autostart_blocking.sh";
     35 +static const char autostartsh[] = "autostart.sh";
     36  static const char broken[] = "broken";
     37 +static const char dwmdir[] = "dwm";
     38 +static const char localshare[] = ".local/share";
     39  static char stext[256];
     40  static int screen;
     41  static int sw, sh;           /* X display screen geometry width, height */
     42 @@ -1380,6 +1386,91 @@ run(void)
     43  			handler[ev.type](&ev); /* call handler */
     44  }
     45  
     46 +void
     47 +runautostart(void)
     48 +{
     49 +	char *pathpfx;
     50 +	char *path;
     51 +	char *xdgdatahome;
     52 +	char *home;
     53 +
     54 +	if ((home = getenv("HOME")) == NULL)
     55 +		/* this is almost impossible */
     56 +		return;
     57 +
     58 +	/* if $XDG_DATA_HOME is defined, use $XDG_DATA_HOME/dwm,
     59 +	 * otherwise use ~/.local/share/dwm as autostart script directory
     60 +	 */
     61 +	if ((xdgdatahome = getenv("XDG_DATA_HOME")) != NULL) {
     62 +		/* space for path segments, separators and nul */
     63 +		if ((pathpfx = malloc(strlen(xdgdatahome) + strlen(dwmdir) + 2)) == NULL)
     64 +			return;
     65 +
     66 +		if (sprintf(pathpfx, "%s/%s", xdgdatahome, dwmdir) <= 0) {
     67 +			free(pathpfx);
     68 +			return;
     69 +		}
     70 +	} else {
     71 +		/* space for path segments, separators and nul */
     72 +		if ((pathpfx = malloc(strlen(home) + strlen(localshare) + strlen(dwmdir) + 3)) == NULL)
     73 +			return;
     74 +
     75 +		if (sprintf(pathpfx, "%s/%s/%s", home, localshare, dwmdir) < 0) {
     76 +			free(pathpfx);
     77 +			return;
     78 +		}
     79 +	}
     80 +
     81 +	/* check if the autostart script directory exists */
     82 +	struct stat sb;
     83 +
     84 +	if (! (stat(pathpfx, &sb) == 0 && S_ISDIR(sb.st_mode))) {
     85 +		/* the XDG conformant path does not exist or are not directories
     86 +		 * so we try ~/.dwm instead
     87 +		 */
     88 +		if (realloc(pathpfx, strlen(home) + strlen(dwmdir) + 3) == NULL) {
     89 +			free(pathpfx);
     90 +			return;
     91 +		}
     92 +
     93 +		if (sprintf(pathpfx, "%s/.%s", home, dwmdir) <= 0) {
     94 +			free(pathpfx);
     95 +			return;
     96 +		}
     97 +	}
     98 +
     99 +	/* try the blocking script first */
    100 +	if ((path = malloc(strlen(pathpfx) + strlen(autostartblocksh) + 2)) == NULL) {
    101 +		free(pathpfx);
    102 +		return;
    103 +	} else
    104 +		if (sprintf(path, "%s/%s", pathpfx, autostartblocksh) <= 0) {
    105 +			free(path);
    106 +			free(pathpfx);
    107 +		}
    108 +
    109 +	if (access(path, X_OK) == 0) {
    110 +		system(path);
    111 +	}
    112 +
    113 +	/* now the non-blocking script */
    114 +	if ((path = realloc(path, strlen(pathpfx) + strlen(autostartsh) + 4)) == NULL) {
    115 +		free(pathpfx);
    116 +		free(path);
    117 +		return;
    118 +	} else
    119 +		if (sprintf(path, "%s/%s", pathpfx, autostartsh) <= 0) {
    120 +			free(path);
    121 +			free(pathpfx);
    122 +		}
    123 +
    124 +	if (access(path, X_OK) == 0) {
    125 +		system(strcat(path, " &"));
    126 +		free(pathpfx);
    127 +		free(path);
    128 +	}
    129 +}
    130 +
    131  void
    132  scan(void)
    133  {
    134 @@ -2142,6 +2233,7 @@ main(int argc, char *argv[])
    135  		die("pledge");
    136  #endif /* __OpenBSD__ */
    137  	scan();
    138 +	runautostart();
    139  	run();
    140  	cleanup();
    141  	XCloseDisplay(dpy);
    142 -- 
    143 2.27.0
    144