load configuration from xresources

Originally started as the xresources patch, but I ended up bringing
over the more configurable logic from the other suckless projects.
master
Ryan 2023-09-22 01:53:05 -04:00
parent dec89e4d66
commit 91de526006
Signed by: ryan
GPG Key ID: 7D7E2E94267DAD95
2 changed files with 93 additions and 10 deletions

View File

@ -2,18 +2,34 @@ static const char *background_color = "#3e3e3e";
static const char *border_color = "#ececec"; static const char *border_color = "#ececec";
static const char *font_color = "#ececec"; static const char *font_color = "#ececec";
static const char *font_pattern = "monospace:size=10"; static const char *font_pattern = "monospace:size=10";
static const unsigned line_spacing = 5; static unsigned line_spacing = 5;
static const unsigned int padding = 15; static unsigned int padding = 15;
static const unsigned int width = 450; static unsigned int width = 450;
static const unsigned int border_size = 2; static unsigned int border_size = 2;
static const unsigned int pos_x = 30; static unsigned int pos_x = 30;
static const unsigned int pos_y = 60; static unsigned int pos_y = 60;
enum corners { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT }; enum corners { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT };
enum corners corner = TOP_RIGHT; enum corners corner = TOP_RIGHT;
static const unsigned int duration = 5; /* in seconds */ static unsigned int duration = 5; /* in seconds */
#define DISMISS_BUTTON Button1 #define DISMISS_BUTTON Button1
#define ACTION_BUTTON Button3 #define ACTION_BUTTON Button3
/* Xresources preferences to load at startup */
ResourcePref resources[] = {
{ "bgcolor", STRING, &background_color },
{ "bordercolor", STRING, &border_color },
{ "fontcolor", STRING, &font_color },
{ "fontpattern", STRING, &font_pattern },
{ "line_spacing", INTEGER, &padding },
{ "padding", INTEGER, &padding },
{ "width", INTEGER, &width },
{ "border_size", INTEGER, &border_size },
{ "pos_x", INTEGER, &pos_x },
{ "pos_y", INTEGER, &pos_y },
{ "corner", INTEGER, &corner },
{ "duration", INTEGER, &duration },
};

71
herbe.c
View File

@ -1,5 +1,6 @@
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/Xft/Xft.h> #include <X11/Xft/Xft.h>
#include <X11/Xresource.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <signal.h> #include <signal.h>
@ -9,12 +10,27 @@
#include <fcntl.h> #include <fcntl.h>
#include <semaphore.h> #include <semaphore.h>
#include "config.h"
#define EXIT_ACTION 0 #define EXIT_ACTION 0
#define EXIT_FAIL 1 #define EXIT_FAIL 1
#define EXIT_DISMISS 2 #define EXIT_DISMISS 2
#define LEN(X) (sizeof X / sizeof X[0])
/* Xresources preferences */
enum resource_type {
STRING = 0,
INTEGER = 1,
FLOAT = 2
};
typedef struct {
char *name;
enum resource_type type;
void *dst;
} ResourcePref;
#include "config.h"
Display *display; Display *display;
Window window; Window window;
int exit_code = EXIT_DISMISS; int exit_code = EXIT_DISMISS;
@ -79,6 +95,55 @@ void expire(int sig)
XFlush(display); XFlush(display);
} }
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", "herbe", name);
snprintf(fullclass, sizeof(fullclass), "%s.%s", "Herbe", 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);
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc == 1) if (argc == 1)
@ -107,6 +172,8 @@ int main(int argc, char *argv[])
if (!(display = XOpenDisplay(0))) if (!(display = XOpenDisplay(0)))
die("Cannot open display"); die("Cannot open display");
config_init(display);
int screen = DefaultScreen(display); int screen = DefaultScreen(display);
Visual *visual = DefaultVisual(display, screen); Visual *visual = DefaultVisual(display, screen);
Colormap colormap = DefaultColormap(display, screen); Colormap colormap = DefaultColormap(display, screen);