Skip to content

Commit

Permalink
Adding xresources patch
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkeby committed Nov 27, 2019
1 parent 2cf8090 commit 4c905a9
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 3 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
- 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
19 changes: 18 additions & 1 deletion config.def.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,25 @@ 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;
Expand Down
4 changes: 4 additions & 0 deletions patch/include.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@

#if PAMAUTH_PATCH
#include "pamauth.c"
#endif

#if XRESOURCES_PATCH
#include "xresources.c"
#endif
4 changes: 4 additions & 0 deletions patch/include.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/* Patches */
#if PAMAUTH_PATCH
#include "pamauth.h"
#endif

#if XRESOURCES_PATCH
#include "xresources.h"
#endif
52 changes: 52 additions & 0 deletions patch/xresources.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <math.h>

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);
}
17 changes: 17 additions & 0 deletions patch/xresources.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <X11/Xresource.h>

/* 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;
7 changes: 6 additions & 1 deletion patches.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,9 @@
* unless they enter the correct password.
* https://tools.suckless.org/slock/patches/unlock_screen/
*/
#define UNLOCKSCREEN_PATCH 0
#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
4 changes: 4 additions & 0 deletions slock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 4c905a9

Please sign in to comment.