Implement lightmode/darkmode patch

This commit is contained in:
2026-01-14 14:54:10 +01:00
parent 5bbe65082c
commit 0a93955264
2 changed files with 104 additions and 30 deletions
+33 -17
View File
@@ -1,21 +1,36 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#define FONT "FiraCodeNerdFont:size=11:antialias=true:autohint=true"
/* appearance */ /* appearance */
static const unsigned int borderpx = 1; /* border pixel of windows */ static const unsigned int borderpx = 3; /* border pixel of windows */
static const unsigned int snap = 32; /* snap pixel */ static const unsigned int snap = 32; /* snap pixel */
static const int showbar = 1; /* 0 means no bar */ static const int showbar = 1; /* 0 means no bar */
static const int topbar = 1; /* 0 means bottom bar */ static const int topbar = 1; /* 0 means bottom bar */
static const char *fonts[] = { "monospace:size=10" }; static const char *fonts[] = { FONT };
static const char dmenufont[] = "monospace:size=10"; static const char dmenufont[] = FONT;
static const char col_gray1[] = "#222222"; // Dark colors
static const char col_gray2[] = "#444444"; static const char col_dark1[] = "#11111b";
static const char col_gray3[] = "#bbbbbb"; static const char col_dark2[] = "#45475a";
static const char col_gray4[] = "#eeeeee"; static const char col_dark3[] = "#7f849c";
static const char col_cyan[] = "#005577"; static const char col_dark4[] = "#cdd6f4"; //text
static const char *colors[][3] = { static const char col_lavender_dark[] = "#b4befe";
// Light colors
static const char col_light1[] = "#dce0e8";
static const char col_light2[] = "#bcc0cc";
static const char col_light3[] = "#8c8fa1";
static const char col_light4[] = "#4c4f69"; //text
static const char col_lavender_light[] = "#7287fd";
static const char *colorslight[][3] = {
/* fg bg border */ /* fg bg border */
[SchemeNorm] = { col_gray3, col_gray1, col_gray2 }, [SchemeNorm] = { col_light3, col_light1, col_light2 },
[SchemeSel] = { col_gray4, col_cyan, col_cyan }, [SchemeSel] = { col_light4, col_light2, col_lavender_light },
};
static const char *colorsdark[][3] = {
/* fg bg border */
[SchemeNorm] = { col_dark1, col_dark3, col_dark2 },
[SchemeSel] = { col_dark4, col_dark2, col_lavender_dark },
}; };
/* tagging */ /* tagging */
@@ -27,8 +42,8 @@ static const Rule rules[] = {
* WM_NAME(STRING) = title * WM_NAME(STRING) = title
*/ */
/* class instance title tags mask isfloating monitor */ /* class instance title tags mask isfloating monitor */
{ "Gimp", NULL, NULL, 0, 1, -1 }, { "Terminal", NULL, NULL, 0, 1, -1 },
{ "Firefox", NULL, NULL, 1 << 8, 0, -1 }, { "Reader", NULL, NULL, 1 << 8, 0, -1 },
}; };
/* layout(s) */ /* layout(s) */
@@ -36,7 +51,7 @@ static const float mfact = 0.55; /* factor of master area size [0.05..0.95]
static const int nmaster = 1; /* number of clients in master area */ static const int nmaster = 1; /* number of clients in master area */
static const int resizehints = 1; /* 1 means respect size hints in tiled resizals */ static const int resizehints = 1; /* 1 means respect size hints in tiled resizals */
static const int lockfullscreen = 1; /* 1 will force focus on the fullscreen window */ static const int lockfullscreen = 1; /* 1 will force focus on the fullscreen window */
static const int refreshrate = 120; /* refresh rate (per second) for client move/resize */ static const int refreshrate = 60; /* refresh rate (per second) for client move/resize */
static const Layout layouts[] = { static const Layout layouts[] = {
/* symbol arrange function */ /* symbol arrange function */
@@ -57,13 +72,14 @@ static const Layout layouts[] = {
#define SHCMD(cmd) { .v = (const char*[]){ "/bin/sh", "-c", cmd, NULL } } #define SHCMD(cmd) { .v = (const char*[]){ "/bin/sh", "-c", cmd, NULL } }
/* commands */ /* commands */
static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn() */ static char dmenumon[2] = "0"; /* component of dmenu{dark,light}, manipulated in spawndmenu() */
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 *dmenudark[] = { "dmenu_run", "-m", dmenumon, "-i", "-fn", dmenufont, "-nb", col_dark1, "-nf", col_dark3, "-sb", col_lavender_dark, "-sf", col_dark4, NULL };
static const char *dmenulight[] = { "dmenu_run", "-m", dmenumon, "-i", "-fn", dmenufont, "-nb", col_light1, "-nf", col_light3, "-sb", col_lavender_light, "-sf", col_light4, NULL };
static const char *termcmd[] = { "st", NULL }; static const char *termcmd[] = { "st", NULL };
static const Key keys[] = { static const Key keys[] = {
/* modifier key function argument */ /* modifier key function argument */
{ MODKEY, XK_p, spawn, {.v = dmenucmd } }, { MODKEY, XK_p, spawndmenu, {0} },
{ MODKEY|ShiftMask, XK_Return, spawn, {.v = termcmd } }, { MODKEY|ShiftMask, XK_Return, spawn, {.v = termcmd } },
{ MODKEY, XK_b, togglebar, {0} }, { MODKEY, XK_b, togglebar, {0} },
{ MODKEY, XK_j, focusstack, {.i = +1 } }, { MODKEY, XK_j, focusstack, {.i = +1 } },
+67 -9
View File
@@ -152,6 +152,7 @@ static void checkotherwm(void);
static void cleanup(void); static void cleanup(void);
static void cleanupmon(Monitor *mon); static void cleanupmon(Monitor *mon);
static void clientmessage(XEvent *e); static void clientmessage(XEvent *e);
static void colormodehandler(int sig);
static void configure(Client *c); static void configure(Client *c);
static void configurenotify(XEvent *e); static void configurenotify(XEvent *e);
static void configurerequest(XEvent *e); static void configurerequest(XEvent *e);
@@ -197,6 +198,7 @@ static void scan(void);
static int sendevent(Client *c, Atom proto); static int sendevent(Client *c, Atom proto);
static void sendmon(Client *c, Monitor *m); static void sendmon(Client *c, Monitor *m);
static void setclientstate(Client *c, long state); static void setclientstate(Client *c, long state);
static void setcolormode(void);
static void setfocus(Client *c); static void setfocus(Client *c);
static void setfullscreen(Client *c, int fullscreen); static void setfullscreen(Client *c, int fullscreen);
static void setlayout(const Arg *arg); static void setlayout(const Arg *arg);
@@ -205,6 +207,7 @@ static void setup(void);
static void seturgent(Client *c, int urg); static void seturgent(Client *c, int urg);
static void showhide(Client *c); static void showhide(Client *c);
static void spawn(const Arg *arg); static void spawn(const Arg *arg);
static void spawndmenu(const Arg *arg);
static void tag(const Arg *arg); static void tag(const Arg *arg);
static void tagmon(const Arg *arg); static void tagmon(const Arg *arg);
static void tile(Monitor *m); static void tile(Monitor *m);
@@ -261,11 +264,13 @@ static void (*handler[LASTEvent]) (XEvent *) = {
static Atom wmatom[WMLast], netatom[NetLast]; static Atom wmatom[WMLast], netatom[NetLast];
static int running = 1; static int running = 1;
static Cur *cursor[CurLast]; static Cur *cursor[CurLast];
static Clr **scheme; static Clr **scheme, **schemedark, **schemelight;
static Display *dpy; static Display *dpy;
static Drw *drw; static Drw *drw;
static Monitor *mons, *selmon; static Monitor *mons, *selmon;
static Window root, wmcheckwin; static Window root, wmcheckwin;
static const char **dmenucmd;
static int colormodechanged;
/* configuration, allows nested code to access above variables */ /* configuration, allows nested code to access above variables */
#include "config.h" #include "config.h"
@@ -485,9 +490,12 @@ cleanup(void)
cleanupmon(mons); cleanupmon(mons);
for (i = 0; i < CurLast; i++) for (i = 0; i < CurLast; i++)
drw_cur_free(drw, cursor[i]); drw_cur_free(drw, cursor[i]);
for (i = 0; i < LENGTH(colors); i++) for (i = 0; i < LENGTH(colorsdark); i++) {
drw_scm_free(drw, scheme[i], 3); free(schemedark[i]);
free(scheme); free(schemelight[i]);
}
free(schemedark);
free(schemelight);
XDestroyWindow(dpy, wmcheckwin); XDestroyWindow(dpy, wmcheckwin);
drw_free(drw); drw_free(drw);
XSync(dpy, False); XSync(dpy, False);
@@ -530,6 +538,12 @@ clientmessage(XEvent *e)
} }
} }
void
colormodehandler(int sig)
{
colormodechanged = 1;
}
void void
configure(Client *c) configure(Client *c)
{ {
@@ -1224,6 +1238,10 @@ propertynotify(XEvent *e)
Window trans; Window trans;
XPropertyEvent *ev = &e->xproperty; XPropertyEvent *ev = &e->xproperty;
if (colormodechanged) {
setcolormode();
colormodechanged = 0;
}
if ((ev->window == root) && (ev->atom == XA_WM_NAME)) if ((ev->window == root) && (ev->atom == XA_WM_NAME))
updatestatus(); updatestatus();
else if (ev->state == PropertyDelete) else if (ev->state == PropertyDelete)
@@ -1441,6 +1459,32 @@ setclientstate(Client *c, long state)
PropModeReplace, (unsigned char *)data, 2); PropModeReplace, (unsigned char *)data, 2);
} }
void
setcolormode(void)
{
static const char *file = ".lightmode";
static char *path = NULL;
const char *home;
size_t size;
if (!path && (home = getenv("HOME"))) {
size = strlen(home) + 1 + strlen(file) + 1;
path = malloc(size);
if (!path)
die("dwm: malloc failed");
snprintf(path, size, "%s/%s", home, file);
}
if (access(path, F_OK) == 0) {
scheme = schemelight;
dmenucmd = dmenulight;
} else {
scheme = schemedark;
dmenucmd = dmenudark;
}
}
int int
sendevent(Client *c, Atom proto) sendevent(Client *c, Atom proto)
{ {
@@ -1549,6 +1593,11 @@ setup(void)
sa.sa_handler = SIG_IGN; sa.sa_handler = SIG_IGN;
sigaction(SIGCHLD, &sa, NULL); sigaction(SIGCHLD, &sa, NULL);
/* set color mode on SIGHUP */
sigemptyset(&sa.sa_mask);
sa.sa_handler = colormodehandler;
sigaction(SIGHUP, &sa, NULL);
/* clean up any zombies (inherited from .xinitrc etc) immediately */ /* clean up any zombies (inherited from .xinitrc etc) immediately */
while (waitpid(-1, NULL, WNOHANG) > 0); while (waitpid(-1, NULL, WNOHANG) > 0);
@@ -1583,9 +1632,13 @@ setup(void)
cursor[CurResize] = drw_cur_create(drw, XC_sizing); cursor[CurResize] = drw_cur_create(drw, XC_sizing);
cursor[CurMove] = drw_cur_create(drw, XC_fleur); cursor[CurMove] = drw_cur_create(drw, XC_fleur);
/* init appearance */ /* init appearance */
scheme = ecalloc(LENGTH(colors), sizeof(Clr *)); schemedark = ecalloc(LENGTH(colorsdark), sizeof(Clr *));
for (i = 0; i < LENGTH(colors); i++) schemelight = ecalloc(LENGTH(colorslight), sizeof(Clr *));
scheme[i] = drw_scm_create(drw, colors[i], 3); for (i = 0; i < LENGTH(colorsdark); i++) {
schemedark[i] = drw_scm_create(drw, colorsdark[i], 3);
schemelight[i] = drw_scm_create(drw, colorslight[i], 3);
}
setcolormode();
/* init bars */ /* init bars */
updatebars(); updatebars();
updatestatus(); updatestatus();
@@ -1648,8 +1701,6 @@ spawn(const Arg *arg)
{ {
struct sigaction sa; struct sigaction sa;
if (arg->v == dmenucmd)
dmenumon[0] = '0' + selmon->num;
if (fork() == 0) { if (fork() == 0) {
if (dpy) if (dpy)
close(ConnectionNumber(dpy)); close(ConnectionNumber(dpy));
@@ -1665,6 +1716,13 @@ spawn(const Arg *arg)
} }
} }
void
spawndmenu(const Arg *arg)
{
dmenumon[0] = '0' + selmon->num;
spawn(&(const Arg){.v = dmenucmd});
}
void void
tag(const Arg *arg) tag(const Arg *arg)
{ {