~kris/suckless

slock

4c905a9c07d1c63e9f9df7187e9e4c5806007920 — bakkeby 6 years ago 2cf8090
Adding xresources patch
8 files changed, 111 insertions(+), 3 deletions(-)

M README.md
M config.def.h
M patch/include.c
M patch/include.h
A patch/xresources.c
A patch/xresources.h
M patches.h
M slock.c
M README.md => README.md +6 -1
@@ 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

M config.def.h => config.def.h +18 -1
@@ 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;

M patch/include.c => patch/include.c +4 -0
@@ 9,4 9,8 @@

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

#if XRESOURCES_PATCH
#include "xresources.c"
#endif
\ No newline at end of file

M patch/include.h => patch/include.h +4 -0
@@ 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

A patch/xresources.c => patch/xresources.c +52 -0
@@ 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);
}

A patch/xresources.h => patch/xresources.h +17 -0
@@ 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;
\ No newline at end of file

M patches.h => patches.h +6 -1
@@ 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

M slock.c => slock.c +4 -0
@@ 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);