Get settings from config file and extend color scheme

pull/4/head
Moritz Luedecke 7 years ago
parent 99bab97ea7
commit 47d89812c2

@ -1,5 +1,6 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <ctype.h> #include <ctype.h>
#include <libconfig.h>
#include <locale.h> #include <locale.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
@ -30,14 +31,16 @@
#define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
#define MINDESCLEN 8 #define MINDESCLEN 8
#define CONFIGPATH $HOME/.gnupg/pinentry-dmenu.conf
/* enums */ /* enums */
enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */ enum { SchemePrompt, SchemeNormal, SchemeSelect, SchemeDesc, SchemeLast }; /* color schemes */
enum { WinPin, WinConfirm }; /* window modes */ enum { WinPin, WinConfirm }; /* window modes */
enum { Ok, NotOk, Cancel }; /* return status */ enum { Ok, NotOk, Cancel }; /* return status */
enum { Nothing, Yes, No }; /* confirm dialog */ enum { Nothing, Yes, No }; /* confirm dialog */
static char text[BUFSIZ] = ""; static char text[BUFSIZ] = "";
//static char *text;
static char *embed; static char *embed;
static int bh, mw, mh; static int bh, mw, mh;
static int sel; static int sel;
@ -62,7 +65,7 @@ pinentry_t pinentry;
static int static int
drawitem(const char* text, Bool sel, int x, int y, int w) { drawitem(const char* text, Bool sel, int x, int y, int w) {
unsigned int i = (sel) ? SchemeSel : SchemeNorm; unsigned int i = (sel) ? SchemeSelect : SchemeNormal;
drw_setscheme(drw, scheme[i]); drw_setscheme(drw, scheme[i]);
@ -138,21 +141,22 @@ drawwin(void) {
int x = 0, pb = 0, pbw = 0, i; int x = 0, pb = 0, pbw = 0, i;
size_t asterlen = strlen(asterisk); size_t asterlen = strlen(asterisk);
size_t pdesclen; size_t pdesclen;
char* desc;
char* censort = ecalloc(1, asterlen * sizeof(text)); char* censort = ecalloc(1, asterlen * sizeof(text));
unsigned int censortl = minpwlen * TEXTW(asterisk) / strlen(asterisk); unsigned int censortl = minpwlen * TEXTW(asterisk) / strlen(asterisk);
unsigned int confirml = TEXTW(" YesNo ") + 3 * lrpad; unsigned int confirml = TEXTW(" YesNo ") + 3 * lrpad;
drw_setscheme(drw, scheme[SchemeNorm]); drw_setscheme(drw, scheme[SchemeNormal]);
drw_rect(drw, 0, 0, mw, mh, 1, 1); drw_rect(drw, 0, 0, mw, mh, 1, 1);
if (prompt && *prompt) { if (prompt && *prompt) {
drw_setscheme(drw, scheme[SchemeSel]); drw_setscheme(drw, scheme[SchemePrompt]);
x = drw_text(drw, x, 0, promptw, bh, lrpad / 2, prompt, 0); x = drw_text(drw, x, 0, promptw, bh, lrpad / 2, prompt, 0);
} }
if (pinentry->prompt && *pinentry->prompt) { if (pinentry->prompt && *pinentry->prompt) {
drw_setscheme(drw, scheme[SchemeSel]); drw_setscheme(drw, scheme[SchemePrompt]);
drw_text(drw, x, 0, ppromptw, bh, lrpad / 2, pinentry->prompt, 0); drw_text(drw, x, 0, ppromptw, bh, lrpad / 2, pinentry->prompt, 0);
x += ppromptw; x += ppromptw;
} }
@ -170,7 +174,9 @@ drawwin(void) {
pbw = MAX(pbw, pdescw); pbw = MAX(pbw, pdescw);
pbw = MIN(pbw, pb); pbw = MIN(pbw, pb);
pb = mw - pbw; pb = mw - pbw;
drawitem(pinentry->description, True, pb, 0, pbw); desc = pinentry->description;
drw_setscheme(drw, scheme[SchemeDesc]);
drw_text(drw, pb, 0, pbw, bh, lrpad / 2, desc, 0);
} else { } else {
pb = 0; pb = 0;
} }
@ -180,7 +186,7 @@ drawwin(void) {
} }
/* Draw input field */ /* Draw input field */
drw_setscheme(drw, scheme[SchemeNorm]); drw_setscheme(drw, scheme[SchemeNormal]);
if (winmode == WinPin) { if (winmode == WinPin) {
for (i = 0; i < asterlen * strlen(text); i += asterlen) { for (i = 0; i < asterlen * strlen(text); i += asterlen) {
@ -217,9 +223,12 @@ setup(void) {
#endif #endif
/* Init appearance */ /* Init appearance */
scheme[SchemeNorm] = drw_scm_create(drw, colors[SchemeNorm], 2); scheme[SchemePrompt] = drw_scm_create(drw, colors[SchemePrompt], 2);
scheme[SchemeSel] = drw_scm_create(drw, colors[SchemeSel], 2); scheme[SchemeNormal] = drw_scm_create(drw, colors[SchemeNormal], 2);
scheme[SchemeSelect] = drw_scm_create(drw, colors[SchemeSelect], 2);
scheme[SchemeDesc] = drw_scm_create(drw, colors[SchemeDesc], 2);
text[0] = '\0';
clip = XInternAtom(dpy, "CLIPBOARD", False); clip = XInternAtom(dpy, "CLIPBOARD", False);
utf8 = XInternAtom(dpy, "UTF8_STRING", False); utf8 = XInternAtom(dpy, "UTF8_STRING", False);
@ -282,7 +291,7 @@ setup(void) {
/* create menu window */ /* create menu window */
swa.override_redirect = True; swa.override_redirect = True;
swa.background_pixel = scheme[SchemeNorm][ColBg].pixel; swa.background_pixel = scheme[SchemePrompt][ColBg].pixel;
swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask; swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
win = XCreateWindow(dpy, parentwin, x, y, mw, mh, 0, win = XCreateWindow(dpy, parentwin, x, y, mw, mh, 0,
CopyFromParent, CopyFromParent, CopyFromParent, CopyFromParent, CopyFromParent, CopyFromParent,
@ -314,8 +323,10 @@ setup(void) {
static void static void
cleanup(void) { cleanup(void) {
XUngrabKey(dpy, AnyKey, AnyModifier, root); XUngrabKey(dpy, AnyKey, AnyModifier, root);
free(scheme[SchemeNorm]); free(scheme[SchemeDesc]);
free(scheme[SchemeSel]); free(scheme[SchemeSelect]);
free(scheme[SchemeNormal]);
free(scheme[SchemePrompt]);
drw_free(drw); drw_free(drw);
XSync(dpy, False); XSync(dpy, False);
XCloseDisplay(dpy); XCloseDisplay(dpy);
@ -392,7 +403,7 @@ keypress(XKeyEvent *ev) {
} }
break; break;
case XK_Right: case XK_Right:
if (text[cursor]!='\0') { if (text[cursor] != '\0') {
cursor = nextrune(cursor, +1); cursor = nextrune(cursor, +1);
} }
break; break;
@ -471,19 +482,25 @@ catchsig(int sig) {
} }
static int static int
password (void) { password(void) {
char *buf; char *buf;
// text = secmem_malloc(BUFSIZ);
winmode = WinPin; winmode = WinPin;
promptwin(); promptwin();
if (pinentry->canceled) { if (pinentry->canceled) {
// secmem_free(text);
return -1; return -1;
} }
//printf("text = %s\n", text);
buf = secmem_malloc(strlen(text));
strcpy(buf, text);
buf = secmem_malloc(strlen(text)); // pinentry_setbuffer_use(pinentry, text, 0);
strcpy(buf, text); pinentry_setbuffer_use(pinentry, buf, 0);
pinentry_setbuffer_use(pinentry, buf, 0); //secmem_free(buf);
// secmem_free(text);
return 1; return 1;
} }
@ -502,7 +519,6 @@ cmdhandler(pinentry_t received_pinentry) {
struct sigaction sa; struct sigaction sa;
XWindowAttributes wa; XWindowAttributes wa;
text[0]='\0';
cursor = 0; cursor = 0;
pinentry = received_pinentry; pinentry = received_pinentry;
@ -525,7 +541,7 @@ cmdhandler(pinentry_t received_pinentry) {
die("no fonts could be loaded."); die("no fonts could be loaded.");
} }
lrpad = drw->fonts->h; lrpad = drw->fonts->h;
drw_setscheme(drw, scheme[SchemeNorm]); drw_setscheme(drw, scheme[SchemePrompt]);
if (pinentry->timeout) { if (pinentry->timeout) {
memset(&sa, 0, sizeof(sa)); memset(&sa, 0, sizeof(sa));
@ -543,59 +559,74 @@ cmdhandler(pinentry_t received_pinentry) {
return -1; return -1;
} }
static void
usage(void) {
fputs("usage: dmenu [-biv] [-P symbol] [-p prompt] [-fn font] [-m monitor]\n"
" [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr);
exit(1);
}
pinentry_cmd_handler_t pinentry_cmd_handler = cmdhandler; pinentry_cmd_handler_t pinentry_cmd_handler = cmdhandler;
int int
main(int argc, char *argv[]) { main(int argc, char *argv[]) {
#if 0 Bool bval;
/* TODO: Add argparse for dmenu parameters */ int val;
int i = 0; const char *str;
for (i = 1; i < argc; i++) config_t cfg;
/* these options take no arguments */ config_setting_t *setting;
if (!strcmp(argv[i], "-v")) { /* prints version information */
puts("dmenu-"VERSION); config_init(&cfg);
exit(0);
} else if (!strcmp(argv[i], "-b")) { /* appears at the bottom of the screen */ /* Read the file. If there is an error, report it and exit. */
topbar = 0; if (!config_read_file(&cfg, "example.cfg")) {
} else if (!strcmp(argv[i], "-f")) { /* ignore this parameter */ fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
; config_error_line(&cfg), config_error_text(&cfg));
} else if (!strcmp(argv[i], "-i")) { /* ignore this parameter */ config_destroy(&cfg);
; return(EXIT_FAILURE);
} else if (!strcmp(argv[i], "-P")) { /* sets the minimum length of pw field */ }
minpwlen = argv[++i];
} else if (i + 1 == argc) { if (config_lookup_string(&cfg, "asterisk", &str)) {
usage(); asterisk = str;
/* these options take one argument */ }
} else if (!strcmp(argv[i], "-l")) { /* ignore this parameter */ if (config_lookup_bool(&cfg, "buttom", &bval)) {
++i; topbar = !bval;
} else if (!strcmp(argv[i], "-m")) { }
mon = atoi(argv[++i]); if (config_lookup_int(&cfg, "min_password_length", &val)) {
} else if (!strcmp(argv[i], "-p")) { /* adds prompt to left of input field */ minpwlen = val;
prompt = argv[++i]; }
} else if (!strcmp(argv[i], "-fn")) { /* font or font set */ if (config_lookup_int(&cfg, "monitor", &val)) {
fonts[0] = argv[++i]; mon = val;
} else if (!strcmp(argv[i], "-nb")) { /* normal background color */ }
colors[SchemeNorm][ColBg] = argv[++i]; if (config_lookup_string(&cfg, "prompt", &str)) {
} else if (!strcmp(argv[i], "-nf")) { /* normal foreground color */ prompt = str;
colors[SchemeNorm][ColFg] = argv[++i]; }
} else if (!strcmp(argv[i], "-sb")) { /* selected background color */
colors[SchemeSel][ColBg] = argv[++i]; if (config_lookup_string(&cfg, "font", &str)) {
} else if (!strcmp(argv[i], "-sf")) { /* selected foreground color */ fonts[0] = str;
colors[SchemeSel][ColFg] = argv[++i]; }
} else if (!strcmp(argv[i], "-w")) { /* embedding window id */ if (config_lookup_string(&cfg, "prompt_bg", &str)) {
embed = argv[++i]; colors[SchemePrompt][ColBg] = str;
} else { }
usage(); if (config_lookup_string(&cfg, "prompt_fg", &str)) {
} colors[SchemePrompt][ColFg] = str;
#endif }
if (config_lookup_string(&cfg, "normal_bg", &str)) {
colors[SchemeNormal][ColBg] = str;
}
if (config_lookup_string(&cfg, "normal_fg", &str)) {
colors[SchemeNormal][ColFg] = str;
}
if (config_lookup_string(&cfg, "select_bg", &str)) {
colors[SchemeSelect][ColBg] = str;
}
if (config_lookup_string(&cfg, "select_fg", &str)) {
colors[SchemeSelect][ColFg] = str;
}
if (config_lookup_string(&cfg, "desc_bg", &str)) {
colors[SchemeDesc][ColBg] = str;
}
if (config_lookup_string(&cfg, "desc_fg", &str)) {
colors[SchemeDesc][ColFg] = str;
}
if (config_lookup_string(&cfg, "window_id", &str)) {
embed = str;
}
pinentry_init("pinentry-dmenu"); pinentry_init("pinentry-dmenu");
pinentry_parse_opts(argc, argv); pinentry_parse_opts(argc, argv);
@ -603,5 +634,7 @@ main(int argc, char *argv[]) {
return 1; return 1;
} }
config_destroy(&cfg);
return 0; return 0;
} }

Loading…
Cancel
Save