From 4c905a9c07d1c63e9f9df7187e9e4c5806007920 Mon Sep 17 00:00:00 2001 From: bakkeby Date: Wed, 27 Nov 2019 08:42:00 +0100 Subject: [PATCH] Adding xresources patch --- README.md | 7 ++++++- config.def.h | 19 ++++++++++++++++- patch/include.c | 4 ++++ patch/include.h | 4 ++++ patch/xresources.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++ patch/xresources.h | 17 +++++++++++++++ patches.h | 7 ++++++- slock.c | 4 ++++ 8 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 patch/xresources.c create mode 100644 patch/xresources.h diff --git a/README.md b/README.md index 89bd10a..03e4bcd 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ Refer to [https://tools.suckless.org/slock/](https://tools.suckless.org/slock/) ### Changelog: +2019-11-27 - Added xresources patch + 2019-10-17 - Added capscolor, control clear, dpms, mediakeys, message, pam auth, quickcancel patches 2019-10-16 - Introduced [flexipatch-finalizer](https://github.com/bakkeby/flexipatch-finalizer) @@ -51,4 +53,7 @@ Refer to [https://tools.suckless.org/slock/](https://tools.suckless.org/slock/) - [unlockscreen](https://tools.suckless.org/slock/patches/unlock_screen/) - this patch keeps the screen unlocked, but keeps the input locked - - that is, the screen is not affected by slock, but users will not be able to interact with the X session unless they enter the correct password \ No newline at end of file + - that is, the screen is not affected by slock, but users will not be able to interact with the X session unless they enter the correct password + + - [xresources](https://tools.suckless.org/slock/patches/xresources/) + - this patch adds the ability to get colors via Xresources \ No newline at end of file diff --git a/config.def.h b/config.def.h index a7dd03f..992c523 100644 --- a/config.def.h +++ b/config.def.h @@ -11,9 +11,26 @@ static const char *colorname[NUMCOLS] = { #endif // CAPSCOLOR_PATCH #if PAMAUTH_PATCH [PAM] = "#9400D3", /* waiting for PAM */ - #endif // CAPSCOLOR_PATCH + #endif // PAMAUTH_PATCH }; +#if XRESOURCES_PATCH +/* + * Xresources preferences to load at startup + */ +ResourcePref resources[] = { + { "color0", STRING, &colorname[INIT] }, + { "color4", STRING, &colorname[INPUT] }, + { "color1", STRING, &colorname[FAILED] }, + #if CAPSCOLOR_PATCH + { "color3", STRING, &colorname[CAPS] }, + #endif // CAPSCOLOR_PATCH + #if PAMAUTH_PATCH + { "color5", STRING, &colorname[PAM] }, + #endif // PAMAUTH_PATCH +}; +#endif // XRESOURCES_PATCH + /* treat a cleared input like a wrong password (color) */ static const int failonclear = 1; diff --git a/patch/include.c b/patch/include.c index 8b20688..760b3ac 100644 --- a/patch/include.c +++ b/patch/include.c @@ -9,4 +9,8 @@ #if PAMAUTH_PATCH #include "pamauth.c" +#endif + +#if XRESOURCES_PATCH +#include "xresources.c" #endif \ No newline at end of file diff --git a/patch/include.h b/patch/include.h index 2e0f2e4..e3422e6 100644 --- a/patch/include.h +++ b/patch/include.h @@ -1,4 +1,8 @@ /* Patches */ #if PAMAUTH_PATCH #include "pamauth.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..c4b299f --- /dev/null +++ b/patch/xresources.c @@ -0,0 +1,52 @@ +#include + +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", "slock", name); + snprintf(fullclass, sizeof(fullclass), "%s.%s", "Slock", 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(Display *dpy) +{ + char *resm; + XrmDatabase db; + ResourcePref *p; + + XrmInitialize(); + resm = XResourceManagerString(dpy); + if (!resm) + return; + + db = XrmGetStringDatabase(resm); + for (p = resources; p < resources + LEN(resources); p++) + resource_load(db, p->name, p->type, p->dst); +} diff --git a/patch/xresources.h b/patch/xresources.h new file mode 100644 index 0000000..971d264 --- /dev/null +++ b/patch/xresources.h @@ -0,0 +1,17 @@ +#include + +/* macros */ +#define LEN(a) (sizeof(a) / sizeof(a)[0]) + +/* Xresources preferences */ +enum resource_type { + STRING = 0, + INTEGER = 1, + FLOAT = 2 +}; + +typedef struct { + char *name; + enum resource_type type; + void *dst; +} ResourcePref; \ No newline at end of file diff --git a/patches.h b/patches.h index 7afc3e0..4809d49 100644 --- a/patches.h +++ b/patches.h @@ -72,4 +72,9 @@ * unless they enter the correct password. * https://tools.suckless.org/slock/patches/unlock_screen/ */ -#define UNLOCKSCREEN_PATCH 0 \ No newline at end of file +#define UNLOCKSCREEN_PATCH 0 + +/* This patch adds the ability to get colors via Xresources. + * https://tools.suckless.org/slock/patches/xresources/ + */ +#define XRESOURCES_PATCH 0 diff --git a/slock.c b/slock.c index b07dcde..c0f8251 100644 --- a/slock.c +++ b/slock.c @@ -514,6 +514,10 @@ main(int argc, char **argv) { if (setuid(duid) < 0) die("slock: setuid: %s\n", strerror(errno)); + #if XRESOURCES_PATCH + config_init(dpy); + #endif // XRESOURCES_PATCH + /* check for Xrandr support */ rr.active = XRRQueryExtension(dpy, &rr.evbase, &rr.errbase);