sites

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

dwm-autodarkmode-20240602-6.5.diff (5913B)


      1 diff --git a/config.def.h b/config.def.h
      2 index 9efa774..8a8d3be 100644
      3 --- a/config.def.h
      4 +++ b/config.def.h
      5 @@ -12,11 +12,16 @@ static const char col_gray2[]       = "#444444";
      6  static const char col_gray3[]       = "#bbbbbb";
      7  static const char col_gray4[]       = "#eeeeee";
      8  static const char col_cyan[]        = "#005577";
      9 -static const char *colors[][3]      = {
     10 +static const char *colorsdark[][3]      = {
     11  	/*               fg         bg         border   */
     12  	[SchemeNorm] = { col_gray3, col_gray1, col_gray2 },
     13  	[SchemeSel]  = { col_gray4, col_cyan,  col_cyan  },
     14  };
     15 +static const char *colorslight[][3]      = {
     16 +	/*               fg         bg         border   */
     17 +	[SchemeNorm] = { col_gray1, col_gray3, col_gray2 },
     18 +	[SchemeSel]  = { col_cyan,  col_gray4, col_cyan  },
     19 +};
     20  
     21  /* tagging */
     22  static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
     23 @@ -56,13 +61,14 @@ static const Layout layouts[] = {
     24  #define SHCMD(cmd) { .v = (const char*[]){ "/bin/sh", "-c", cmd, NULL } }
     25  
     26  /* commands */
     27 -static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn() */
     28 -static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL };
     29 +static char dmenumon[2] = "0"; /* component of dmenu{dark,light}, manipulated in spawndmenu() */
     30 +static const char *dmenudark[] =  { "dmenu_run", "-m", dmenumon, "-i", "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL };
     31 +static const char *dmenulight[] = { "dmenu_run", "-m", dmenumon, "-i", "-fn", dmenufont, "-nb", col_gray3, "-nf", col_gray1, "-sb", col_cyan, "-sf", col_gray4, NULL };
     32  static const char *termcmd[]  = { "st", NULL };
     33  
     34  static const Key keys[] = {
     35  	/* modifier                     key        function        argument */
     36 -	{ MODKEY,                       XK_p,      spawn,          {.v = dmenucmd } },
     37 +	{ MODKEY,                       XK_p,      spawndmenu,     {0} },
     38  	{ MODKEY|ShiftMask,             XK_Return, spawn,          {.v = termcmd } },
     39  	{ MODKEY,                       XK_b,      togglebar,      {0} },
     40  	{ MODKEY,                       XK_j,      focusstack,     {.i = +1 } },
     41 diff --git a/dwm.c b/dwm.c
     42 index f1d86b2..d4e3ddc 100644
     43 --- a/dwm.c
     44 +++ b/dwm.c
     45 @@ -198,6 +198,7 @@ static void scan(void);
     46  static int sendevent(Client *c, Atom proto);
     47  static void sendmon(Client *c, Monitor *m);
     48  static void setclientstate(Client *c, long state);
     49 +static void setcolormode(int sig);
     50  static void setfocus(Client *c);
     51  static void setfullscreen(Client *c, int fullscreen);
     52  static void setlayout(const Arg *arg);
     53 @@ -206,6 +207,7 @@ static void setup(void);
     54  static void seturgent(Client *c, int urg);
     55  static void showhide(Client *c);
     56  static void spawn(const Arg *arg);
     57 +static void spawndmenu(const Arg *arg);
     58  static void tag(const Arg *arg);
     59  static void tagmon(const Arg *arg);
     60  static void tile(Monitor *m);
     61 @@ -262,11 +264,12 @@ static void (*handler[LASTEvent]) (XEvent *) = {
     62  static Atom wmatom[WMLast], netatom[NetLast];
     63  static int running = 1;
     64  static Cur *cursor[CurLast];
     65 -static Clr **scheme;
     66 +static Clr **scheme, **schemedark, **schemelight;
     67  static Display *dpy;
     68  static Drw *drw;
     69  static Monitor *mons, *selmon;
     70  static Window root, wmcheckwin;
     71 +static const char **dmenucmd;
     72  
     73  /* configuration, allows nested code to access above variables */
     74  #include "config.h"
     75 @@ -486,9 +489,12 @@ cleanup(void)
     76  		cleanupmon(mons);
     77  	for (i = 0; i < CurLast; i++)
     78  		drw_cur_free(drw, cursor[i]);
     79 -	for (i = 0; i < LENGTH(colors); i++)
     80 -		free(scheme[i]);
     81 -	free(scheme);
     82 +	for (i = 0; i < LENGTH(colorsdark); i++) {
     83 +		free(schemedark[i]);
     84 +		free(schemelight[i]);
     85 +	}
     86 +	free(schemedark);
     87 +	free(schemelight);
     88  	XDestroyWindow(dpy, wmcheckwin);
     89  	drw_free(drw);
     90  	XSync(dpy, False);
     91 @@ -1442,6 +1448,32 @@ setclientstate(Client *c, long state)
     92  		PropModeReplace, (unsigned char *)data, 2);
     93  }
     94  
     95 +void
     96 +setcolormode(int sig)
     97 +{
     98 +	static const char *file = ".lightmode";
     99 +	static char *path = NULL;
    100 +	const char *home;
    101 +	size_t size;
    102 +
    103 +	if (!path && (home = getenv("HOME"))) {
    104 +		size = strlen(home) + 1 + strlen(file) + 1;
    105 +		path = malloc(size);
    106 +		if (!path)
    107 +			die("dwm: malloc failed");
    108 +
    109 +		snprintf(path, size, "%s/%s", home, file);
    110 +	}
    111 +
    112 +	if (access(path, F_OK) == 0) {
    113 +		scheme = schemelight;
    114 +		dmenucmd = dmenulight;
    115 +	} else {
    116 +		scheme = schemedark;
    117 +		dmenucmd = dmenudark;
    118 +	}
    119 +}
    120 +
    121  int
    122  sendevent(Client *c, Atom proto)
    123  {
    124 @@ -1550,6 +1582,11 @@ setup(void)
    125  	sa.sa_handler = SIG_IGN;
    126  	sigaction(SIGCHLD, &sa, NULL);
    127  
    128 +	/* set color mode on SIGHUP */
    129 +	sigemptyset(&sa.sa_mask);
    130 +	sa.sa_handler = setcolormode;
    131 +	sigaction(SIGHUP, &sa, NULL);
    132 +
    133  	/* clean up any zombies (inherited from .xinitrc etc) immediately */
    134  	while (waitpid(-1, NULL, WNOHANG) > 0);
    135  
    136 @@ -1584,9 +1621,13 @@ setup(void)
    137  	cursor[CurResize] = drw_cur_create(drw, XC_sizing);
    138  	cursor[CurMove] = drw_cur_create(drw, XC_fleur);
    139  	/* init appearance */
    140 -	scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
    141 -	for (i = 0; i < LENGTH(colors); i++)
    142 -		scheme[i] = drw_scm_create(drw, colors[i], 3);
    143 +	schemedark = ecalloc(LENGTH(colorsdark), sizeof(Clr *));
    144 +	schemelight = ecalloc(LENGTH(colorslight), sizeof(Clr *));
    145 +	for (i = 0; i < LENGTH(colorsdark); i++) {
    146 +		schemedark[i] = drw_scm_create(drw, colorsdark[i], 3);
    147 +		schemelight[i] = drw_scm_create(drw, colorslight[i], 3);
    148 +	}
    149 +	setcolormode(0);
    150  	/* init bars */
    151  	updatebars();
    152  	updatestatus();
    153 @@ -1649,8 +1690,6 @@ spawn(const Arg *arg)
    154  {
    155  	struct sigaction sa;
    156  
    157 -	if (arg->v == dmenucmd)
    158 -		dmenumon[0] = '0' + selmon->num;
    159  	if (fork() == 0) {
    160  		if (dpy)
    161  			close(ConnectionNumber(dpy));
    162 @@ -1666,6 +1705,13 @@ spawn(const Arg *arg)
    163  	}
    164  }
    165  
    166 +void
    167 +spawndmenu(const Arg *arg)
    168 +{
    169 +	dmenumon[0] = '0' + selmon->num;
    170 +	spawn(&(const Arg){.v = dmenucmd});
    171 +}
    172 +
    173  void
    174  tag(const Arg *arg)
    175  {