svkbd

simple virtual keyboard
git clone git://git.suckless.org/svkbd
Log | Files | Refs | README | LICENSE

commit 8c28fd15f33d37cbb424cae3e0c306f55380bee6
parent 869a4328c970b4cfbfcf86ab535f68caaa8a9e00
Author: tetrakist <tetrakist@mutandum.com>
Date:   Mon,  8 Mar 2021 23:10:59 -0800

Add Xresources support.

Signed-off-by: Maarten van Gompel <proycon@anaproy.nl>

Diffstat:
Mconfig.def.h | 4++--
Msvkbd.c | 95++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 96 insertions(+), 3 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -4,10 +4,10 @@ static double overlay_delay = 1.0; //in seconds static double repeat_delay = 0.75; //in seconds, will not work on keys with overlays static int scan_rate = 50; //scan rate in microseconds, affects key repetition rate static int heightfactor = 14; //one row of keys takes up 1/x of the screen height -static const char *fonts[] = { +static const char *defaultfonts[] = { "DejaVu Sans:bold:size=22" }; -static const char *colors[SchemeLast][2] = { +static const char *defaultcolors[SchemeLast][2] = { /* fg bg */ [SchemeNorm] = { "#bbbbbb", "#132a33" }, [SchemeNormShift] = { "#008ac0", "#132a33" }, diff --git a/svkbd.c b/svkbd.c @@ -21,6 +21,7 @@ #include <X11/Xproto.h> #include <X11/extensions/XTest.h> #include <X11/Xft/Xft.h> +#include <X11/Xresource.h> #ifdef XINERAMA #include <X11/extensions/Xinerama.h> #endif @@ -117,6 +118,9 @@ static int debug = 0; static int numlayers = 0; static int numkeys = 0; +static char *colors[10][2]; /* 10 schemes, 2 colors each */ +static char *fonts[] = {0}; + static KeySym ispressingkeysym; Bool ispressing = False; @@ -691,6 +695,75 @@ run(void) } void +readxresources(void) { + XrmInitialize(); + + char* xrm; + if ((xrm = XResourceManagerString(drw->dpy))) { + char *type; + XrmDatabase xdb = XrmGetStringDatabase(xrm); + XrmValue xval; + + if (XrmGetResource(xdb, "svkbd.font", "*", &type, &xval) && !fonts[0]) + fonts[0] = strdup(xval.addr); + + if (XrmGetResource(xdb, "svkbd.background", "*", &type, &xval) && !colors[SchemeNorm][ColBg] ) + colors[SchemeNorm][ColBg] = strdup(xval.addr); + if (XrmGetResource(xdb, "svkbd.foreground", "*", &type, &xval) && !colors[SchemeNorm][ColFg] ) + colors[SchemeNorm][ColFg] = strdup(xval.addr); + + if (XrmGetResource(xdb, "svkbd.shiftforeground", "*", &type, &xval) && !colors[SchemeNormShift][ColFg] ) + colors[SchemeNormShift][ColFg] = strdup(xval.addr); + if (XrmGetResource(xdb, "svkbd.shiftbackground", "*", &type, &xval) && !colors[SchemeNormShift][ColBg] ) + colors[SchemeNormShift][ColBg] = strdup(xval.addr); + + if (XrmGetResource(xdb, "svkbd.ABCforeground", "*", &type, &xval) && !colors[SchemeNormABC][ColFg] ) + colors[SchemeNormABC][ColFg] = strdup(xval.addr); + if (XrmGetResource(xdb, "svkbd.ABCbackground", "*", &type, &xval) && !colors[SchemeNormABC][ColBg] ) + colors[SchemeNormABC][ColBg] = strdup(xval.addr); + + if (XrmGetResource(xdb, "svkbd.ABCshiftforeground", "*", &type, &xval) && !colors[SchemeNormShift][ColFg] ) + colors[SchemeNormShift][ColFg] = strdup(xval.addr); + if (XrmGetResource(xdb, "svkbd.ABCshiftbackground", "*", &type, &xval) && !colors[SchemeNormShift][ColBg] ) + colors[SchemeNormShift][ColBg] = strdup(xval.addr); + + if (XrmGetResource(xdb, "svkbd.pressbackground", "*", &type, &xval) && !colors[SchemePress][ColBg] ) + colors[SchemePress][ColBg] = strdup(xval.addr); + if (XrmGetResource(xdb, "svkbd.pressforeground", "*", &type, &xval) && !colors[SchemePress][ColFg] ) + colors[SchemePress][ColFg] = strdup(xval.addr); + + if (XrmGetResource(xdb, "svkbd.pressshiftbackground", "*", &type, &xval) && !colors[SchemePressShift][ColBg] ) + colors[SchemePressShift][ColBg] = strdup(xval.addr); + if (XrmGetResource(xdb, "svkbd.pressshiftforeground", "*", &type, &xval) && !colors[SchemePressShift][ColFg] ) + colors[SchemePressShift][ColFg] = strdup(xval.addr); + + if (XrmGetResource(xdb, "svkbd.highlightbackground", "*", &type, &xval) && !colors[SchemeHighlight][ColBg] ) + colors[SchemeHighlight][ColBg] = strdup(xval.addr); + if (XrmGetResource(xdb, "svkbd.highlightforeground", "*", &type, &xval) && !colors[SchemeHighlight][ColFg] ) + colors[SchemeHighlight][ColFg] = strdup(xval.addr); + + if (XrmGetResource(xdb, "svkbd.highlightshiftbackground", "*", &type, &xval) && !colors[SchemeHighlightShift][ColBg] ) + colors[SchemeHighlightShift][ColBg] = strdup(xval.addr); + if (XrmGetResource(xdb, "svkbd.highlightshiftforeground", "*", &type, &xval) && !colors[SchemeHighlightShift][ColFg] ) + colors[SchemeHighlightShift][ColFg] = strdup(xval.addr); + + if (XrmGetResource(xdb, "svkbd.overlaybackground", "*", &type, &xval) && !colors[SchemeOverlay][ColBg] ) + colors[SchemeOverlay][ColBg] = strdup(xval.addr); + if (XrmGetResource(xdb, "svkbd.overlayforeground", "*", &type, &xval) && !colors[SchemeOverlay][ColFg] ) + colors[SchemeOverlay][ColFg] = strdup(xval.addr); + + if (XrmGetResource(xdb, "svkbd.overlayshiftbackground", "*", &type, &xval) && !colors[SchemeOverlayShift][ColBg] ) + colors[SchemeOverlayShift][ColBg] = strdup(xval.addr); + if (XrmGetResource(xdb, "svkbd.overlayshiftforeground", "*", &type, &xval) && !colors[SchemeOverlayShift][ColFg] ) + colors[SchemeOverlayShift][ColFg] = strdup(xval.addr); + + + XrmDestroyDatabase(xdb); + } +} + + +void setup(void) { XSetWindowAttributes wa; @@ -721,8 +794,23 @@ setup(void) sh = DisplayHeight(dpy, screen); } drw = drw_create(dpy, screen, root, sw, sh); + + readxresources(); + + /* Apply defaults to font and colors*/ + if ( !fonts[0] ) + fonts[0] = strdup(defaultfonts[0]); + for (i = 0; i < SchemeLast; ++i){ + for (j = 0; j < 2; ++j){ + if ( !colors[i][j] ) + colors[i][j] = strdup(defaultcolors[i][j]); + } + } + if (!drw_fontset_create(drw, fonts, LENGTH(fonts))) die("no fonts could be loaded"); + free(fonts[0]); + drw_setscheme(drw, scheme[SchemeNorm]); /* find an unused keycode to use as a temporary keycode (derived from source: @@ -755,6 +843,11 @@ setup(void) for (j = 0; j < SchemeLast; j++) scheme[j] = drw_scm_create(drw, colors[j], 2); + for (j = 0; j < SchemeLast; ++j) { + free(colors[j][ColFg]); + free(colors[j][ColBg]); + } + /* init atoms */ if (isdock) { netatom[NetWMWindowType] = XInternAtom(dpy, @@ -1067,7 +1160,7 @@ main(int argc, char *argv[]) wy = -1; i++; } else if (!strcmp(argv[i], "-fn")) { /* font or font set */ - fonts[0] = argv[++i]; + fonts[0] = strdup(argv[++i]); } else if (!strcmp(argv[i], "-D")) { debug = 1; } else if (!strcmp(argv[i], "-h")) {