dwm-noxz

[fork] suckless dwm - personal fork
git clone git://git.noxz.tech/dwm-noxz
Log | Files | Refs | README | LICENSE

commit 1358c4f2b3b99d91a120d1463689160bcfcc48fb
parent e543008324dc7d4e391ad5860f2b9d073c5a33ff
Author: Chris Noxz <chris@noxz.tech>
Date:   Thu, 18 Mar 2021 13:29:31 +0100

Replace vtcolors patch with Xresources

Diffstat:
Mconfig.def.h | 111+++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
Mdrw.c | 6+++---
Mdrw.h | 6+++---
Mdwm.c | 148++++++++++++++++++++++++++++++++++++++-----------------------------------------
4 files changed, 152 insertions(+), 119 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -3,42 +3,47 @@ #define MODKEY Mod4Mask /* appearance */ -static const unsigned int borderpx = 2; /* border pixel of windows */ -static const unsigned int snap = 32; /* snap pixel */ -static const int showbar = 1; /* 0 means no bar */ -static const int topbar = 1; /* 0 means bottom bar */ -static const int viewontag = 1; -static const int focusonwheel = 0; -static const char *fonts[] = { "dnoxz_numerics:pixelsize=12:autohint=false" }; -static const char title_bg_dark[] = "#030303"; -static const char title_bg_light[] = "#fdfdfd"; -static const int color_ptrs[][4] = { - /* fg bg border float */ - [SchemeNorm] = { -1, -1, 5, -1 }, - [SchemeSel] = { -1, -1, 6, 1 }, - [SchemeTagsNorm] = { 2, 0, 0, -1 }, - [SchemeTagsSel] = { 5, 6, 6, -1 }, - [SchemeTitleNorm] = { 6, -1, -1, -1 }, - [SchemeTitleSel] = { 6, -1, -1, -1 }, +static unsigned int borderpx = 2; /* border pixel of windows */ +static unsigned int snap = 32; /* snap pixel */ +static int showbar = 1; /* 0 means no bar */ +static int topbar = 1; /* 0 means bottom bar */ +static int viewontag = 1; +static int focusonwheel = 0; +static char font[256] = "monospace:size=10"; +static char *fonts[] = { font }; - [SchemeStatusNorm] = { 6, -1, -1, -1 }, - [SchemeStatusAct] = { 5, 6, -1, -1 }, - [SchemeStatusDist] = { 11, 0, -1, -1 }, - [SchemeStatusNoti] = { 0, 6, -1, -1 }, -}; -static char colors[][4][8] = { - /* fg bg border float */ - [SchemeNorm] = { COL_DEF, COL_DEF, COL_DEF, COL_DEF }, - [SchemeSel] = { COL_DEF, COL_DEF, COL_DEF, COL_DEF }, - [SchemeTagsNorm] = { COL_DEF, COL_DEF, COL_DEF, COL_DEF }, - [SchemeTagsSel] = { COL_DEF, COL_DEF, COL_DEF, COL_DEF }, - [SchemeTitleNorm] = { COL_DEF, COL_DEF, COL_DEF, COL_DEF }, - [SchemeTitleSel] = { COL_DEF, COL_DEF, COL_DEF, COL_DEF }, +static char col_none[] = COL_DEF; +static char col_nbr[] = COL_DEF; +static char col_sbr[] = COL_DEF; +static char col_sfl[] = COL_DEF; +static char col_tanfg[] = COL_DEF; +static char col_tanbg[] = COL_DEF; +static char col_tanbr[] = COL_DEF; +static char col_tasfg[] = COL_DEF; +static char col_tasbg[] = COL_DEF; +static char col_tasbr[] = COL_DEF; +static char col_tinfg[] = COL_DEF; +static char col_tisfg[] = COL_DEF; +static char col_snfg[] = COL_DEF; +static char col_safg[] = COL_DEF; +static char col_sabg[] = COL_DEF; +static char col_sdfg[] = COL_DEF; +static char col_sdbg[] = COL_DEF; +static char col_snofg[] = COL_DEF; +static char col_snobg[] = COL_DEF; +static char *colors[][4] = { + /* fg bg border float */ + [SchemeNorm] = { col_none, col_none, col_nbr, col_none }, + [SchemeSel] = { col_none, col_none, col_sbr, col_sfl }, + [SchemeTagsNorm] = { col_tanfg, col_tanbg, col_tanbr, col_none }, + [SchemeTagsSel] = { col_tasfg, col_tasbg, col_tasbr, col_none }, + [SchemeTitleNorm] = { col_tinfg, col_none, col_none, col_none }, + [SchemeTitleSel] = { col_tisfg, col_none, col_none, col_none }, - [SchemeStatusNorm] = { COL_DEF, COL_DEF, COL_DEF, COL_DEF }, - [SchemeStatusAct] = { COL_DEF, COL_DEF, COL_DEF, COL_DEF }, - [SchemeStatusDist] = { COL_DEF, COL_DEF, COL_DEF, COL_DEF }, - [SchemeStatusNoti] = { COL_DEF, COL_DEF, COL_DEF, COL_DEF }, + [SchemeStatusNorm] = { col_snfg, col_none, col_none, col_none }, + [SchemeStatusAct] = { col_safg, col_sabg, col_none, col_none }, + [SchemeStatusDist] = { col_sdfg, col_sdbg, col_none, col_none }, + [SchemeStatusNoti] = { col_snofg, col_snobg, col_none, col_none }, }; /* tagging */ @@ -53,9 +58,9 @@ static const Rule rules[] = { NULL }; /* layout(s) */ -static const float mfact = 0.55; /* factor of master area size [0.05..0.95] */ -static const int nmaster = 0; /* number of clients in master area */ -static const int resizehints = 0; /* 1 means respect size hints in tiled resizals */ +static float mfact = 0.55; /* factor of master area size [0.05..0.95] */ +static int nmaster = 0; /* number of clients in master area */ +static int resizehints = 0; /* 1 means respect size hints in tiled resizals */ static const Layout layouts[] = { /* symbol arrange function */ @@ -83,6 +88,38 @@ static Button buttons[] = { { ClkTagBar, Mod4Mask, Button3, toggletag, {0} }, }; +/* Xresources preferences to load at startup */ +ResourcePref resources[] = { + { "normalBorderColor", STRING, &col_nbr }, + { "selectedBorderColor", STRING, &col_sbr }, + { "selectedFloatColor", STRING, &col_sfl }, + { "tagsNormalForeground", STRING, &col_tanfg }, + { "tagsNormalBackground", STRING, &col_tanbg }, + { "tagsNormalBorderColor", STRING, &col_tanbr }, + { "tagsSelectedForeground", STRING, &col_tasfg }, + { "tagsSelectedBackground", STRING, &col_tasbg }, + { "tagsSelectedBorderColor", STRING, &col_tasbr }, + { "titleNormalForeground", STRING, &col_tinfg }, + { "titleSelectedForeground", STRING, &col_tisfg }, + { "statusNormalForeground", STRING, &col_snfg }, + { "statusActForeground", STRING, &col_safg }, + { "statusActBackground", STRING, &col_sabg }, + { "statusDistForeground", STRING, &col_sdfg }, + { "statusDistBackground", STRING, &col_sdbg }, + { "statusNotiForeground", STRING, &col_snofg }, + { "statusNotiBackground", STRING, &col_snobg }, + { "font", STRING, &font }, + { "borderpx", INTEGER, &borderpx }, + { "snap", INTEGER, &snap }, + { "showbar", INTEGER, &showbar }, + { "topbar", INTEGER, &topbar }, + { "viewontag", INTEGER, &viewontag }, + { "focusonwheel", INTEGER, &focusonwheel }, + { "nmaster", INTEGER, &nmaster }, + { "resizehints", INTEGER, &resizehints }, + { "mfact", FLOAT, &mfact }, +}; + static const char *dwmfifo = "/tmp/dwm.fifo"; static Command commands[] = { { "spawn ...", spawn, {.i = DispCmdLine} }, diff --git a/drw.c b/drw.c @@ -167,7 +167,7 @@ xfont_free(Fnt *font) } Fnt* -drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount) +drw_fontset_create(Drw* drw, char *fonts[], size_t fontcount) { Fnt *cur, *ret = NULL; size_t i; @@ -194,7 +194,7 @@ drw_fontset_free(Fnt *font) } void -drw_clr_create(Drw *drw, Clr *dest, const char clrname[]) +drw_clr_create(Drw *drw, Clr *dest, const char *clrname) { if (!drw || !dest || !clrname) return; @@ -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, char clrnames[][8], 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 @@ -32,14 +32,14 @@ void drw_resize(Drw *drw, unsigned int w, unsigned int h); void drw_free(Drw *drw); /* Fnt abstraction */ -Fnt *drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount); +Fnt *drw_fontset_create(Drw* drw, char *fonts[], size_t fontcount); void drw_fontset_free(Fnt* set); unsigned int drw_fontset_getwidth(Drw *drw, const char *text); void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h); /* Colorscheme abstraction */ -void drw_clr_create(Drw *drw, Clr *dest, const char clrname[]); -Clr *drw_scm_create(Drw *drw, char clrnames[][8], size_t clrcount); +void drw_clr_create(Drw *drw, Clr *dest, const char *clrname); +Clr *drw_scm_create(Drw *drw, char *clrnames[], size_t clrcount); /* Cursor abstraction */ Cur *drw_cur_create(Drw *drw, int shape); diff --git a/dwm.c b/dwm.c @@ -38,6 +38,7 @@ #include <X11/Xlib.h> #include <X11/Xproto.h> #include <X11/Xutil.h> +#include <X11/Xresource.h> #ifdef XINERAMA #include <X11/extensions/Xinerama.h> #endif /* XINERAMA */ @@ -141,6 +142,19 @@ typedef struct { int monitor; } Rule; +/* Xresources preferences */ +enum resource_type { + STRING = 0, + INTEGER = 1, + FLOAT = 2 +}; + +typedef struct { + char *name; + enum resource_type type; + void *dst; +} ResourcePref; + typedef struct { const char *name; void (*func)(const Arg *arg); @@ -187,6 +201,7 @@ static int gettextprop(Window w, Atom atom, char *text, unsigned int size); static void grabbuttons(Client *c, int focused); static void incnmaster(const Arg *arg); static void killclient(const Arg *arg); +static void load_xresources(void); static void manage(Window w, XWindowAttributes *wa); static void maprequest(XEvent *e); static void monocle(Monitor *m); @@ -199,6 +214,7 @@ static Monitor *recttomon(int x, int y, int w, int h); static void resize(Client *c, int x, int y, int w, int h, int interact); static void resizeclient(Client *c, int x, int y, int w, int h); static void resizemouse(const Arg *arg); +static void resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst); static void restack(Monitor *m); static void movestack(const Arg *arg); static void rotatestack(const Arg *arg); @@ -213,8 +229,6 @@ static void setlayout(const Arg *arg); static void togglelayout(const Arg *arg); static void rotatelayout(const Arg *arg); static void setmfact(const Arg *arg); -static void get_vt_colors(void); -static int get_luminance(char *rgb); static void setup(void); static void seturgent(Client *c, int urg); static void showhide(Client *c); @@ -1801,72 +1815,6 @@ setmfact(const Arg *arg) } void -get_vt_colors(void) -{ - char *cfs[3] = { - "/sys/module/vt/parameters/default_red", - "/sys/module/vt/parameters/default_grn", - "/sys/module/vt/parameters/default_blu", - }; - char vtcs[16][8]; - char tk[] = ","; - char cl[64]; - char *tp = NULL; - FILE *fp; - size_t r; - int i, c, n; - - for (i = 0; i < 16; i++) - strcpy(vtcs[i], "#000000"); - - for (i = 0, r = 0; i < 3; i++) { - if ((fp = fopen(cfs[i], "r")) == NULL) - continue; - while ((cl[r] = fgetc(fp)) != EOF && cl[r] != '\n') - r++; - cl[r] = '\0'; - for (c = 0, tp = cl, n = 0; c < 16; c++, tp++) { - if ((r = strcspn(tp, tk)) == -1) - break; - for (n = 0; r && *tp >= 48 && *tp < 58; r--, tp++) - n = n * 10 - 48 + *tp; - vtcs[c][i * 2 + 1] = n / 16 < 10 ? n / 16 + 48 : n / 16 + 87; - vtcs[c][i * 2 + 2] = n % 16 < 10 ? n % 16 + 48 : n % 16 + 87; - } - fclose(fp); - } - for (i = 0; i < LENGTH(colors); i++) { - for (c = 0; c < 4; c++) { - n = color_ptrs[i][c]; - if (n > -1 && strlen(colors[i][c]) >= strlen(vtcs[n])) - memcpy(colors[i][c], vtcs[n], 7); - } - } -} - -int get_luminance(char *r) -{ - char *c = r; - int n[3] = {0}; - int i = 0; - - while (*c) { - if (*c >= 48 && *c < 58) - n[i / 2] = n[i / 2] * 16 - 48 + *c; - else if (*c >= 65 && *c < 71) - n[i / 2] = n[i / 2] * 16 - 55 + *c; - else if (*c >= 97 && *c < 103) - n[i / 2] = n[i / 2] * 16 - 87 + *c; - else - i--; - i++; - c++; - } - - return (0.299 * n[0] + 0.587 * n[1] + 0.114 * n[2]) / 2.55; -} - -void setup(void) { int i; @@ -1907,14 +1855,6 @@ setup(void) cursor[CurResize] = drw_cur_create(drw, XC_sizing); cursor[CurMove] = drw_cur_create(drw, XC_fleur); /* init appearance */ - get_vt_colors(); - if (get_luminance(colors[SchemeTagsNorm][ColBg]) > 50) { - strcpy(colors[SchemeTitleNorm][ColBg], title_bg_light); - strcpy(colors[SchemeTitleSel][ColBg], title_bg_light); - } else { - strcpy(colors[SchemeTitleNorm][ColBg], title_bg_dark); - strcpy(colors[SchemeTitleSel][ColBg], title_bg_dark); - } scheme = ecalloc(LENGTH(colors), sizeof(Clr *)); for (i = 0; i < LENGTH(colors); i++) scheme[i] = drw_scm_create(drw, colors[i], 4); @@ -2584,6 +2524,60 @@ zoom(const Arg *arg) pop(c); } +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", "dwm", 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); +} + int main(int argc, char *argv[]) { @@ -2596,6 +2590,8 @@ main(int argc, char *argv[]) if (!(dpy = XOpenDisplay(NULL))) die("dwm: cannot open display"); checkotherwm(); + XrmInitialize(); + load_xresources(); setup(); #ifdef __OpenBSD__ if (pledge("stdio rpath proc exec", NULL) == -1)