st-xresources-20260524-688f70a.diff (4764B)
1 From 9ce669ca8d9bfffc4a7626de15da24f5b27ba809 Mon Sep 17 00:00:00 2001 2 From: Justinas Grigas <dev@jstnas.com> 3 Date: Sat, 9 May 2026 14:20:00 +0100 4 Subject: [PATCH] xresources: runtime configuration using X resources 5 6 --- 7 config.def.h | 36 +++++++++++++++++++++++++ 8 x.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 9 2 files changed, 110 insertions(+) 10 11 diff --git a/config.def.h b/config.def.h 12 index 2cd740a..62e88cc 100644 13 --- a/config.def.h 14 +++ b/config.def.h 15 @@ -472,3 +472,39 @@ static char ascii_printable[] = 16 " !\"#$%&'()*+,-./0123456789:;<=>?" 17 "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_" 18 "`abcdefghijklmnopqrstuvwxyz{|}~"; 19 + 20 +/* X resources to update */ 21 +static const XResPref resources[] = { 22 + /* name type address */ 23 + { "st.font", STRING, &font }, 24 + { "st.color0", STRING, &colorname[0] }, 25 + { "st.color1", STRING, &colorname[1] }, 26 + { "st.color2", STRING, &colorname[2] }, 27 + { "st.color3", STRING, &colorname[3] }, 28 + { "st.color4", STRING, &colorname[4] }, 29 + { "st.color5", STRING, &colorname[5] }, 30 + { "st.color6", STRING, &colorname[6] }, 31 + { "st.color7", STRING, &colorname[7] }, 32 + { "st.color8", STRING, &colorname[8] }, 33 + { "st.color9", STRING, &colorname[9] }, 34 + { "st.color10", STRING, &colorname[10] }, 35 + { "st.color11", STRING, &colorname[11] }, 36 + { "st.color12", STRING, &colorname[12] }, 37 + { "st.color13", STRING, &colorname[13] }, 38 + { "st.color14", STRING, &colorname[14] }, 39 + { "st.color15", STRING, &colorname[15] }, 40 + { "st.foreground", STRING, &colorname[258] }, 41 + { "st.background", STRING, &colorname[259] }, 42 + { "st.cursorColor", STRING, &colorname[256] }, 43 + { "st.cursorColorReverse", STRING, &colorname[257] }, 44 + { "st.termname", STRING, &termname }, 45 + { "st.shell", STRING, &shell }, 46 + { "st.minlatency", INTEGER, &minlatency }, 47 + { "st.maxlatency", INTEGER, &maxlatency }, 48 + { "st.blinktimeout", INTEGER, &blinktimeout }, 49 + { "st.bellvolume", INTEGER, &bellvolume }, 50 + { "st.tabspaces", INTEGER, &tabspaces }, 51 + { "st.borderpx", INTEGER, &borderpx }, 52 + { "st.cwscale", FLOAT, &cwscale }, 53 + { "st.chscale", FLOAT, &chscale }, 54 +}; 55 diff --git a/x.c b/x.c 56 index d73152b..5cd6f7c 100644 57 --- a/x.c 58 +++ b/x.c 59 @@ -14,6 +14,7 @@ 60 #include <X11/keysym.h> 61 #include <X11/Xft/Xft.h> 62 #include <X11/XKBlib.h> 63 +#include <X11/Xresource.h> 64 65 char *argv0; 66 #include "arg.h" 67 @@ -45,6 +46,15 @@ typedef struct { 68 signed char appcursor; /* application cursor */ 69 } Key; 70 71 +/* Xresources preferences */ 72 +enum XResType { STRING, INTEGER, FLOAT }; 73 + 74 +typedef struct { 75 + const char *name; 76 + enum XResType type; 77 + void *dst; 78 +} XResPref; 79 + 80 /* X modifiers */ 81 #define XK_ANY_MOD UINT_MAX 82 #define XK_NO_MOD 0 83 @@ -189,6 +199,9 @@ static int match(uint, uint); 84 static void run(void); 85 static void usage(void); 86 87 +static void xresload(const XResPref *); 88 +static void xresupdate(void); 89 + 90 static void (*handler[LASTEvent])(XEvent *) = { 91 [KeyPress] = kpress, 92 [ClientMessage] = cmessage, 93 @@ -220,6 +233,7 @@ static DC dc; 94 static XWindow xw; 95 static XSelection xsel; 96 static TermWindow win; 97 +static XrmDatabase xrdb; 98 99 /* Font Ring Cache */ 100 enum { 101 @@ -2023,6 +2037,63 @@ run(void) 102 } 103 } 104 105 +void 106 +xresload(const XResPref *resource) 107 +{ 108 + char *type; 109 + XrmValue ret; 110 + 111 + if (!XrmGetResource(xrdb, resource->name, NULL, &type, &ret)) 112 + return; 113 + if (ret.addr == NULL || strncmp(type, "String", sizeof("String"))) 114 + return; 115 + 116 + switch (resource->type) { 117 + case STRING: 118 + *(char **)resource->dst = ret.addr; 119 + break; 120 + case INTEGER: 121 + *(int *)resource->dst = strtoul(ret.addr, NULL, 10); 122 + break; 123 + case FLOAT: 124 + *(float *)resource->dst = strtof(ret.addr, NULL); 125 + break; 126 + } 127 +} 128 + 129 +void 130 +xresupdate(void) 131 +{ 132 + Display *display; 133 + char *resm; 134 + const XResPref *p; 135 + 136 + display = XOpenDisplay(NULL); 137 + if (!display) 138 + return; 139 + 140 + resm = XResourceManagerString(display); 141 + if (resm) { 142 + if (xrdb) 143 + XrmDestroyDatabase(xrdb); 144 + xrdb = XrmGetStringDatabase(resm); 145 + if (xrdb) { 146 + for (p = resources; p < resources + LEN(resources); ++p) 147 + xresload(p); 148 + } 149 + } 150 + XCloseDisplay(display); 151 +} 152 + 153 +void 154 +xresreload(int signum) { 155 + xresupdate(); 156 + xloadcols(); 157 + cresize(0, 0); 158 + redraw(); 159 + xhints(); 160 +} 161 + 162 void 163 usage(void) 164 { 165 @@ -2096,6 +2167,9 @@ run: 166 167 setlocale(LC_CTYPE, ""); 168 XSetLocaleModifiers(""); 169 + XrmInitialize(); 170 + xresupdate(); 171 + signal(SIGUSR1, xresreload); 172 cols = MAX(cols, 1); 173 rows = MAX(rows, 1); 174 tnew(cols, rows); 175 -- 176 2.53.0 177