From 4bd0ed3327ab43549ae178114bd1e34448505037 Mon Sep 17 00:00:00 2001 From: bakkeby Date: Mon, 16 Sep 2019 14:21:09 +0200 Subject: [PATCH] Adding xresources patch --- config.def.h | 38 +++++++++++++++++++++++++++++++++ patch/x_include.c | 4 ++++ patch/x_include.h | 4 ++++ patch/xresources.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++ patch/xresources.h | 17 +++++++++++++++ patches.h | 5 +++++ x.c | 11 ++++++++++ 7 files changed, 131 insertions(+) create mode 100644 patch/xresources.c create mode 100644 patch/xresources.h diff --git a/config.def.h b/config.def.h index 2b33860..fa1d4ea 100644 --- a/config.def.h +++ b/config.def.h @@ -162,6 +162,44 @@ static unsigned int mousebg = 0; */ static unsigned int defaultattr = 11; +#if XRESOURCES_PATCH +/* + * Xresources preferences to load at startup + */ +ResourcePref resources[] = { + { "font", STRING, &font }, + { "color0", STRING, &colorname[0] }, + { "color1", STRING, &colorname[1] }, + { "color2", STRING, &colorname[2] }, + { "color3", STRING, &colorname[3] }, + { "color4", STRING, &colorname[4] }, + { "color5", STRING, &colorname[5] }, + { "color6", STRING, &colorname[6] }, + { "color7", STRING, &colorname[7] }, + { "color8", STRING, &colorname[8] }, + { "color9", STRING, &colorname[9] }, + { "color10", STRING, &colorname[10] }, + { "color11", STRING, &colorname[11] }, + { "color12", STRING, &colorname[12] }, + { "color13", STRING, &colorname[13] }, + { "color14", STRING, &colorname[14] }, + { "color15", STRING, &colorname[15] }, + { "background", STRING, &colorname[256] }, + { "foreground", STRING, &colorname[257] }, + { "cursorColor", STRING, &colorname[258] }, + { "termname", STRING, &termname }, + { "shell", STRING, &shell }, + { "xfps", INTEGER, &xfps }, + { "actionfps", INTEGER, &actionfps }, + { "blinktimeout", INTEGER, &blinktimeout }, + { "bellvolume", INTEGER, &bellvolume }, + { "tabspaces", INTEGER, &tabspaces }, + { "borderpx", INTEGER, &borderpx }, + { "cwscale", FLOAT, &cwscale }, + { "chscale", FLOAT, &chscale }, +}; +#endif // XRESOURCES_PATCH + /* * Internal mouse shortcuts. * Beware that overloading Button1 will disable the selection. diff --git a/patch/x_include.c b/patch/x_include.c index 9b2e512..1f09c89 100644 --- a/patch/x_include.c +++ b/patch/x_include.c @@ -6,4 +6,8 @@ #if FIXIME_PATCH #include "fixime.c" +#endif + +#if XRESOURCES_PATCH +#include "xresources.c" #endif \ No newline at end of file diff --git a/patch/x_include.h b/patch/x_include.h index f5dca48..fa163b2 100644 --- a/patch/x_include.h +++ b/patch/x_include.h @@ -6,4 +6,8 @@ #if FIXIME_PATCH #include "fixime.h" +#endif + +#if XRESOURCES_PATCH +#include "xresources.h" #endif \ No newline at end of file diff --git a/patch/xresources.c b/patch/xresources.c new file mode 100644 index 0000000..b7808b2 --- /dev/null +++ b/patch/xresources.c @@ -0,0 +1,52 @@ +int +resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst) +{ + char **sdst = dst; + int *idst = dst; + float *fdst = dst; + + char fullname[256]; + char fullclass[256]; + char *type; + XrmValue ret; + + snprintf(fullname, sizeof(fullname), "%s.%s", + opt_name ? opt_name : "st", name); + snprintf(fullclass, sizeof(fullclass), "%s.%s", + opt_class ? opt_class : "St", name); + fullname[sizeof(fullname) - 1] = fullclass[sizeof(fullclass) - 1] = '\0'; + + XrmGetResource(db, fullname, fullclass, &type, &ret); + if (ret.addr == NULL || strncmp("String", type, 64)) + return 1; + + switch (rtype) { + case STRING: + *sdst = ret.addr; + break; + case INTEGER: + *idst = strtoul(ret.addr, NULL, 10); + break; + case FLOAT: + *fdst = strtof(ret.addr, NULL); + break; + } + return 0; +} + +void +config_init(void) +{ + char *resm; + XrmDatabase db; + ResourcePref *p; + + XrmInitialize(); + resm = XResourceManagerString(xw.dpy); + if (!resm) + return; + + db = XrmGetStringDatabase(resm); + for (p = resources; p < resources + LEN(resources); p++) + resource_load(db, p->name, p->type, p->dst); +} \ No newline at end of file diff --git a/patch/xresources.h b/patch/xresources.h new file mode 100644 index 0000000..a8da8a1 --- /dev/null +++ b/patch/xresources.h @@ -0,0 +1,17 @@ +#include + +/* Xresources preferences */ +enum resource_type { + STRING = 0, + INTEGER = 1, + FLOAT = 2 +}; + +typedef struct { + char *name; + enum resource_type type; + void *dst; +} ResourcePref; + +int resource_load(XrmDatabase, char *, enum resource_type, void *); +void config_init(void); \ No newline at end of file diff --git a/patches.h b/patches.h index b3b6722..861a53a 100644 --- a/patches.h +++ b/patches.h @@ -80,3 +80,8 @@ */ #define OPENCOPIED_PATCH 1 +/* This patch adds the ability to configure st via Xresources. At startup, st will read and + * apply the resources named in the resources[] array in config.h. + * https://st.suckless.org/patches/xresources/ + */ +#define XRESOURCES_PATCH 1 diff --git a/x.c b/x.c index a6ea20e..9c8492f 100644 --- a/x.c +++ b/x.c @@ -847,8 +847,13 @@ xclear(int x1, int y1, int x2, int y2) void xhints(void) { + #if XRESOURCES_PATCH + XClassHint class = {opt_name ? opt_name : "st", + opt_class ? opt_class : "St"}; + #else XClassHint class = {opt_name ? opt_name : termname, opt_class ? opt_class : termname}; + #endif // XRESOURCES_PATCH XWMHints wm = {.flags = InputHint, .input = 1}; XSizeHints *sizeh; @@ -2157,6 +2162,12 @@ run: setlocale(LC_CTYPE, ""); XSetLocaleModifiers(""); + #if XRESOURCES_PATCH + if (!(xw.dpy = XOpenDisplay(NULL))) + die("Can't open display\n"); + + config_init(); + #endif // XRESOURCES_PATCH cols = MAX(cols, 1); rows = MAX(rows, 1); tnew(cols, rows);