i have been wanting and trying to create a patch that checks my charger status and if its plugged in the color-theme of dwm will be blue but if it is unplugged then it will turn the theme of dwm to red, heres the "patch" i made
------------------- config.def.h
------------------- dwm.c
In the code I have set the default theme to be blue and that works just fine and dwm gets colored correctly on build, even if I change the theme manually, but the theme doesn't change on status change.
This is a very specific purpose and I'm pretty sure that there isn't a patch for this, but if there's someone who knows of a patch that does this or can help me out with this I would really appreciate it. I tried doing this myself but I just can't figure it out.
I'm pretty new to C and don't have any experience working with linux or xorg utils, this is just how far I could get with some AI help.
------------------- config.def.h
Code:
--- original.h 2025-03-27 15:14:53.097312042 +0500
+++ config.def.h 2025-06-15 14:51:21.240599830 +0500
@@ -1,100 +1,150 @@
/* See LICENSE file for copyright and license details. */
/* appearance */
-static const unsigned int borderpx = 1; /* border pixel of windows */
-static const unsigned int snap = 32; /* snap pixel */
-static const int showbar = 1; /* 0 means no bar */
-static const int topbar = 1; /* 0 means bottom bar */
-static const char *fonts[] = { "monospace:size=10" };
-static const char dmenufont[] = "monospace:size=10";
-static const char col_gray1[] = "#222222";
-static const char col_gray2[] = "#444444";
-static const char col_gray3[] = "#bbbbbb";
-static const char col_gray4[] = "#eeeeee";
-static const char col_cyan[] = "#005577";
-static const char *colors[][3] = {
- /* fg bg border */
- [SchemeNorm] = { col_gray3, col_gray1, col_gray2 },
- [SchemeSel] = { col_gray4, col_cyan, col_cyan },
+static const unsigned int borderpx = 6; /* border pixel of windows */
+static const unsigned int snap = 32; /* snap pixel */
+static const int showbar = 1; /* 0 means no bar */
+static const int topbar = 1; /* 0 means bottom bar */
+static const char *fonts[] = { "Liberation Mono:bold:size=14" };
+static const char dmenufont[] = "Liberation Mono:bold:size=14";
+
+static const char *blue[] = {
+// text dark light
+ "#FFFFFF", "#0E1C4A", "#3E54BD"
};
+static const char *red[] = {
+// text dark light
+ "#FFFFFF", "#430B07", "#73493D"
+};
+
+static const char **current_theme = blue;
+/*
+static const char *green[] = {
+ "#1A2C12", "#020C02", "#193A0D", "#385C17", "#1B331D"
+};
+*/
------------------- dwm.c
Code:
--- original.c 2025-06-15 15:17:35.492684035 +0500
+++ dwm.c 2025-05-25 10:10:31.564985125 +0500
@@ -271,6 +272,49 @@
/* configuration, allows nested code to access above variables */
#include "config.h"
+extern const char **current_theme;
+const char *colors[2][3];
+
+
+void
+update_colors(void) {
+ colors[SchemeNorm][0] = (char *)current_theme[0];
+ colors[SchemeNorm][1] = (char *)current_theme[1];
+ colors[SchemeNorm][2] = (char *)current_theme[2];
+
+ colors[SchemeSel][0] = (char *)current_theme[0];
+ colors[SchemeSel][1] = (char *)current_theme[2];
+ colors[SchemeSel][2] = (char *)current_theme[2];
+}
+
+void
+check_theme(Display *dpy) {
+ char *name = NULL;
+ Window root = DefaultRootWindow(dpy);
+
+ if (XFetchName(dpy, root, &name)) {
+ if (name && strstr(name, "change")) {
+ current_theme = red;
+ update_colors();
+ }
+ XFree(name);
+ }
+}
+
+void *
+theme_checker(void *arg) {
+ Display *dpy_thread = XOpenDisplay(NULL);
+ if (!dpy_thread) return NULL;
+
+ for (;;) {
+ check_theme(dpy_thread);
+ usleep(100000);
+ }
+
+ XCloseDisplay(dpy_thread);
+ return NULL;
+}
+
/* compile-time check if all tags fit into an unsigned int bit array. */
struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
@@ -1851,7 +1895,7 @@
}
void
-updateclientlist()
+updateclientlist(void)
{
Client *c;
Monitor *m;
@@ -2152,12 +2198,15 @@
if (!(dpy = XOpenDisplay(NULL)))
die("dwm: cannot open display");
checkotherwm();
+ update_colors();
setup();
#ifdef __OpenBSD__
if (pledge("stdio rpath proc exec", NULL) == -1)
die("pledge");
#endif /* __OpenBSD__ */
scan();
+ pthread_t theme_thread;
+ pthread_create(&theme_thread, NULL, theme_checker, NULL);
run();
cleanup();
XCloseDisplay(dpy);
In the code I have set the default theme to be blue and that works just fine and dwm gets colored correctly on build, even if I change the theme manually, but the theme doesn't change on status change.
This is a very specific purpose and I'm pretty sure that there isn't a patch for this, but if there's someone who knows of a patch that does this or can help me out with this I would really appreciate it. I tried doing this myself but I just can't figure it out.
I'm pretty new to C and don't have any experience working with linux or xorg utils, this is just how far I could get with some AI help.