dmenu

git clone git://mattcarlson.org/repos/dmenu.git
Log | Files | Refs

commit 2586ec31a4b0b4503f758977b5ab48d24253ce56
parent f03e55649ffe0b6e549894c4be2bf4986ceb77aa
Author: Matthew Carlson <matt@mcarlson.xyz>
Date:   Thu,  1 Jul 2021 21:43:24 +0000

Merge branch 'xresources' into dmenu

Diffstat:
Mconfig.h | 33+++++++++++++++++++++++++++------
Mdmenu.c | 69++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mdrw.c | 2+-
Mdrw.h | 3+--
4 files changed, 97 insertions(+), 10 deletions(-)

diff --git a/config.h b/config.h @@ -4,16 +4,25 @@ static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */ static int centered = 0; /* -c option; centers dmenu on screen */ static int min_width = 500; /* minimum width when centered */ + /* -fn option overrides fonts[0]; default X11 font or font set */ +static char font[] = "monospace:size=10"; static const char *fonts[] = { - "monospace:size=10" + font, + "monospace:size=10", }; -static const char *prompt = NULL; /* -p option; prompt to the left of input field */ -static const char *colors[SchemeLast][2] = { + +static char *prompt = NULL; /* -p option; prompt to the left of input field */ + +static char normfgcolor[] = "#bbbbbb"; +static char normbgcolor[] = "#222222"; +static char selfgcolor[] = "#eeeeee"; +static char selbgcolor[] = "#005577"; +static char *colors[SchemeLast][2] = { /* fg bg */ - [SchemeNorm] = { "#bbbbbb", "#222222" }, - [SchemeSel] = { "#eeeeee", "#005577" }, - [SchemeOut] = { "#000000", "#00ffff" }, + [SchemeNorm] = { normfgcolor, normbgcolor }, + [SchemeSel] = { selfgcolor, selbgcolor }, + [SchemeOut] = { "#000000", "#00ffff" }, }; /* -l option; if nonzero, dmenu uses vertical list with given number of lines */ static unsigned int lines = 0; @@ -26,3 +35,15 @@ static const char worddelimiters[] = " "; /* Size of the window border */ static const unsigned int border_width = 5; + +/* + * Xresources preferences to load at startup + */ +ResourcePref resources[] = { + { "font", STRING, &font }, + { "normfgcolor", STRING, &normfgcolor }, + { "normbgcolor", STRING, &normbgcolor }, + { "selfgcolor", STRING, &selfgcolor }, + { "selbgcolor", STRING, &selbgcolor }, + { "prompt", STRING, &prompt }, +}; diff --git a/dmenu.c b/dmenu.c @@ -11,6 +11,7 @@ #include <X11/Xlib.h> #include <X11/Xatom.h> #include <X11/Xutil.h> +#include <X11/Xresource.h> #ifdef XINERAMA #include <X11/extensions/Xinerama.h> #endif @@ -56,6 +57,21 @@ static XIC xic; static Drw *drw; static Clr *scheme[SchemeLast]; +/* Xresources preferences */ +enum resource_type { + STRING = 0, + INTEGER = 1, + FLOAT = 2 +}; +typedef struct { + char *name; + enum resource_type type; + void *dst; +} ResourcePref; + +static void load_xresources(void); +static void resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst); + #include "config.h" static int (*fstrncmp)(const char *, const char *, size_t) = strncmp; @@ -430,7 +446,7 @@ keypress(XKeyEvent *ev) switch(ksym) { default: -insert: + insert: if (!iscntrl(*buf)) insert(buf, len); break; @@ -736,6 +752,54 @@ readstdin(void) lines = MIN(lines, i); } +void +resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst) +{ + char *sdst = NULL; + int *idst = NULL; + float *fdst = NULL; + sdst = dst; + idst = dst; + fdst = dst; + char fullname[256]; + char *type; + XrmValue ret; + snprintf(fullname, sizeof(fullname), "%s.%s", "dmenu", name); + fullname[sizeof(fullname) - 1] = '\0'; + XrmGetResource(db, fullname, "*", &type, &ret); + if (!(ret.addr == NULL || strncmp("String", type, 64))) + { + switch (rtype) { + case STRING: + strcpy(sdst, ret.addr); + break; + case INTEGER: + *idst = strtoul(ret.addr, NULL, 10); + break; + case FLOAT: + *fdst = strtof(ret.addr, NULL); + break; + } + } +} + +void +load_xresources(void) +{ + Display *display; + char *resm; + XrmDatabase db; + ResourcePref *p; + display = XOpenDisplay(NULL); + resm = XResourceManagerString(display); + if (!resm) + return; + db = XrmGetStringDatabase(resm); + for (p = resources; p < resources + LENGTH(resources); p++) + resource_load(db, p->name, p->type, p->dst); + XCloseDisplay(display); +} + static void run(void) { @@ -912,6 +976,9 @@ main(int argc, char *argv[]) XWindowAttributes wa; int i, fast = 0; + XrmInitialize(); + load_xresources(); + for (i = 1; i < argc; i++) /* these options take no arguments */ if (!strcmp(argv[i], "-v")) { /* prints version information */ diff --git a/drw.c b/drw.c @@ -208,7 +208,7 @@ drw_clr_create(Drw *drw, Clr *dest, const char *clrname) /* Wrapper to create color schemes. The caller has to call free(3) on the * returned color scheme when done using it. */ Clr * -drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount) +drw_scm_create(Drw *drw, char *clrnames[], size_t clrcount) { size_t i; Clr *ret; diff --git a/drw.h b/drw.h @@ -39,8 +39,7 @@ void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned in /* Colorscheme abstraction */ void drw_clr_create(Drw *drw, Clr *dest, const char *clrname); -Clr *drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount); - +Clr *drw_scm_create(Drw *drw, char *clrnames[], size_t clrcount); /* Cursor abstraction */ Cur *drw_cur_create(Drw *drw, int shape); void drw_cur_free(Drw *drw, Cur *cursor);