dwm-autodarkmode-20250224-6.5.diff (6709B)
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..bd9cf3d 100644 43 --- a/dwm.c 44 +++ b/dwm.c 45 @@ -153,6 +153,7 @@ static void checkotherwm(void); 46 static void cleanup(void); 47 static void cleanupmon(Monitor *mon); 48 static void clientmessage(XEvent *e); 49 +static void colormodehandler(int sig); 50 static void configure(Client *c); 51 static void configurenotify(XEvent *e); 52 static void configurerequest(XEvent *e); 53 @@ -198,6 +199,7 @@ static void scan(void); 54 static int sendevent(Client *c, Atom proto); 55 static void sendmon(Client *c, Monitor *m); 56 static void setclientstate(Client *c, long state); 57 +static void setcolormode(void); 58 static void setfocus(Client *c); 59 static void setfullscreen(Client *c, int fullscreen); 60 static void setlayout(const Arg *arg); 61 @@ -206,6 +208,7 @@ static void setup(void); 62 static void seturgent(Client *c, int urg); 63 static void showhide(Client *c); 64 static void spawn(const Arg *arg); 65 +static void spawndmenu(const Arg *arg); 66 static void tag(const Arg *arg); 67 static void tagmon(const Arg *arg); 68 static void tile(Monitor *m); 69 @@ -262,11 +265,13 @@ static void (*handler[LASTEvent]) (XEvent *) = { 70 static Atom wmatom[WMLast], netatom[NetLast]; 71 static int running = 1; 72 static Cur *cursor[CurLast]; 73 -static Clr **scheme; 74 +static Clr **scheme, **schemedark, **schemelight; 75 static Display *dpy; 76 static Drw *drw; 77 static Monitor *mons, *selmon; 78 static Window root, wmcheckwin; 79 +static const char **dmenucmd; 80 +static int colormodechanged; 81 82 /* configuration, allows nested code to access above variables */ 83 #include "config.h" 84 @@ -486,9 +491,12 @@ cleanup(void) 85 cleanupmon(mons); 86 for (i = 0; i < CurLast; i++) 87 drw_cur_free(drw, cursor[i]); 88 - for (i = 0; i < LENGTH(colors); i++) 89 - free(scheme[i]); 90 - free(scheme); 91 + for (i = 0; i < LENGTH(colorsdark); i++) { 92 + free(schemedark[i]); 93 + free(schemelight[i]); 94 + } 95 + free(schemedark); 96 + free(schemelight); 97 XDestroyWindow(dpy, wmcheckwin); 98 drw_free(drw); 99 XSync(dpy, False); 100 @@ -531,6 +539,12 @@ clientmessage(XEvent *e) 101 } 102 } 103 104 +void 105 +colormodehandler(int sig) 106 +{ 107 + colormodechanged = 1; 108 +} 109 + 110 void 111 configure(Client *c) 112 { 113 @@ -1225,6 +1239,10 @@ propertynotify(XEvent *e) 114 Window trans; 115 XPropertyEvent *ev = &e->xproperty; 116 117 + if (colormodechanged) { 118 + setcolormode(); 119 + colormodechanged = 0; 120 + } 121 if ((ev->window == root) && (ev->atom == XA_WM_NAME)) 122 updatestatus(); 123 else if (ev->state == PropertyDelete) 124 @@ -1442,6 +1460,32 @@ setclientstate(Client *c, long state) 125 PropModeReplace, (unsigned char *)data, 2); 126 } 127 128 +void 129 +setcolormode(void) 130 +{ 131 + static const char *file = ".lightmode"; 132 + static char *path = NULL; 133 + const char *home; 134 + size_t size; 135 + 136 + if (!path && (home = getenv("HOME"))) { 137 + size = strlen(home) + 1 + strlen(file) + 1; 138 + path = malloc(size); 139 + if (!path) 140 + die("dwm: malloc failed"); 141 + 142 + snprintf(path, size, "%s/%s", home, file); 143 + } 144 + 145 + if (access(path, F_OK) == 0) { 146 + scheme = schemelight; 147 + dmenucmd = dmenulight; 148 + } else { 149 + scheme = schemedark; 150 + dmenucmd = dmenudark; 151 + } 152 +} 153 + 154 int 155 sendevent(Client *c, Atom proto) 156 { 157 @@ -1550,6 +1594,11 @@ setup(void) 158 sa.sa_handler = SIG_IGN; 159 sigaction(SIGCHLD, &sa, NULL); 160 161 + /* set color mode on SIGHUP */ 162 + sigemptyset(&sa.sa_mask); 163 + sa.sa_handler = colormodehandler; 164 + sigaction(SIGHUP, &sa, NULL); 165 + 166 /* clean up any zombies (inherited from .xinitrc etc) immediately */ 167 while (waitpid(-1, NULL, WNOHANG) > 0); 168 169 @@ -1584,9 +1633,13 @@ setup(void) 170 cursor[CurResize] = drw_cur_create(drw, XC_sizing); 171 cursor[CurMove] = drw_cur_create(drw, XC_fleur); 172 /* init appearance */ 173 - scheme = ecalloc(LENGTH(colors), sizeof(Clr *)); 174 - for (i = 0; i < LENGTH(colors); i++) 175 - scheme[i] = drw_scm_create(drw, colors[i], 3); 176 + schemedark = ecalloc(LENGTH(colorsdark), sizeof(Clr *)); 177 + schemelight = ecalloc(LENGTH(colorslight), sizeof(Clr *)); 178 + for (i = 0; i < LENGTH(colorsdark); i++) { 179 + schemedark[i] = drw_scm_create(drw, colorsdark[i], 3); 180 + schemelight[i] = drw_scm_create(drw, colorslight[i], 3); 181 + } 182 + setcolormode(); 183 /* init bars */ 184 updatebars(); 185 updatestatus(); 186 @@ -1649,8 +1702,6 @@ spawn(const Arg *arg) 187 { 188 struct sigaction sa; 189 190 - if (arg->v == dmenucmd) 191 - dmenumon[0] = '0' + selmon->num; 192 if (fork() == 0) { 193 if (dpy) 194 close(ConnectionNumber(dpy)); 195 @@ -1666,6 +1717,13 @@ spawn(const Arg *arg) 196 } 197 } 198 199 +void 200 +spawndmenu(const Arg *arg) 201 +{ 202 + dmenumon[0] = '0' + selmon->num; 203 + spawn(&(const Arg){.v = dmenucmd}); 204 +} 205 + 206 void 207 tag(const Arg *arg) 208 {