diff --git a/README b/README index b324940..e3c6efc 100644 --- a/README +++ b/README @@ -18,7 +18,7 @@ the /usr/local namespace by default). Afterwards enter the following command to build and install dmenu (if necessary as root): - make clean install + make clean install Running pinentry-dmenu ---------------------- @@ -28,7 +28,7 @@ Config ------ To use pinentry-dmenu add in `~/.gnupg/gpg-agent.conf`: - pinentry-program + pinentry-program The config is located in `~/.gnupg/pinentry-dmenu.conf`. diff --git a/config.def.h b/config.def.h index 01fefc8..ec08d0b 100644 --- a/config.def.h +++ b/config.def.h @@ -8,24 +8,24 @@ static unsigned int lineheight = 22; static unsigned int borderwidth = 2; static unsigned int minpwlen = 32; static unsigned int alphas[SchemeLast][2] = { - /* values between 0 (transparent) and 255 (opaque) */ - /* foreground, background */ - [SchemePrompt] = { 255, 255 }, - [SchemeNormal] = { 255, 230 }, - [SchemeSelect] = { 255, 255 }, - [SchemeDesc] = { 255, 230 }, + /* values between 0 (transparent) and 255 (opaque) */ + /* foreground, background */ + [SchemePrompt] = { 255, 255 }, + [SchemeNormal] = { 255, 230 }, + [SchemeSelect] = { 255, 255 }, + [SchemeDesc] = { 255, 230 }, }; static const char *position = "center"; static const char *fonts[] = { - "DejaVuSansMono Nerd Font:pixelsize=16:antialias=true:autohint=true" + "DejaVuSansMono Nerd Font:pixelsize=16:antialias=true:autohint=true" }; static const char *prompt = NULL; static const char *asterisk = "*"; static const char *colors[SchemeLast][2] = { - /* foreground, background */ - [SchemePrompt] = { "#cccccc", "#4185d7" }, - [SchemeNormal] = { "#cccccc", "#000000" }, - [SchemeSelect] = { "#ffffff", "#4185d7" }, - [SchemeDesc] = { "#cccccc", "#000000" }, + /* foreground, background */ + [SchemePrompt] = { "#cccccc", "#4185d7" }, + [SchemeNormal] = { "#cccccc", "#000000" }, + [SchemeSelect] = { "#ffffff", "#4185d7" }, + [SchemeDesc] = { "#cccccc", "#000000" }, }; diff --git a/drw.c b/drw.c index 6624f95..a930d1e 100644 --- a/drw.c +++ b/drw.c @@ -19,86 +19,86 @@ static const long utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF static long utf8decodebyte(const char c, size_t *i) { - for (*i = 0; *i < (UTF_SIZ + 1); ++(*i)) - if (((unsigned char)c & utfmask[*i]) == utfbyte[*i]) - return (unsigned char)c & ~utfmask[*i]; - return 0; + for (*i = 0; *i < (UTF_SIZ + 1); ++(*i)) + if (((unsigned char)c & utfmask[*i]) == utfbyte[*i]) + return (unsigned char)c & ~utfmask[*i]; + return 0; } static size_t utf8validate(long *u, size_t i) { - if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF)) - *u = UTF_INVALID; - for (i = 1; *u > utfmax[i]; ++i) - ; - return i; + if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF)) + *u = UTF_INVALID; + for (i = 1; *u > utfmax[i]; ++i) + ; + return i; } static size_t utf8decode(const char *c, long *u, size_t clen) { - size_t i, j, len, type; - long udecoded; + size_t i, j, len, type; + long udecoded; - *u = UTF_INVALID; - if (!clen) - return 0; - udecoded = utf8decodebyte(c[0], &len); - if (!BETWEEN(len, 1, UTF_SIZ)) - return 1; - for (i = 1, j = 1; i < clen && j < len; ++i, ++j) { - udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type); - if (type) - return j; - } - if (j < len) - return 0; - *u = udecoded; - utf8validate(u, len); + *u = UTF_INVALID; + if (!clen) + return 0; + udecoded = utf8decodebyte(c[0], &len); + if (!BETWEEN(len, 1, UTF_SIZ)) + return 1; + for (i = 1, j = 1; i < clen && j < len; ++i, ++j) { + udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type); + if (type) + return j; + } + if (j < len) + return 0; + *u = udecoded; + utf8validate(u, len); - return len; + return len; } Drw * drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h, Visual *visual, unsigned int depth, Colormap cmap) { - Drw *drw = ecalloc(1, sizeof(Drw)); + Drw *drw = ecalloc(1, sizeof(Drw)); - drw->dpy = dpy; - drw->screen = screen; - drw->root = root; - drw->w = w; - drw->h = h; - drw->visual = visual; - drw->depth = depth; - drw->cmap = cmap; - drw->drawable = XCreatePixmap(dpy, root, w, h, depth); - drw->gc = XCreateGC(dpy, drw->drawable, 0, NULL); - XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter); + drw->dpy = dpy; + drw->screen = screen; + drw->root = root; + drw->w = w; + drw->h = h; + drw->visual = visual; + drw->depth = depth; + drw->cmap = cmap; + drw->drawable = XCreatePixmap(dpy, root, w, h, depth); + drw->gc = XCreateGC(dpy, drw->drawable, 0, NULL); + XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter); - return drw; + return drw; } void drw_resize(Drw *drw, unsigned int w, unsigned int h) { - if (!drw) - return; + if (!drw) + return; - drw->w = w; - drw->h = h; - if (drw->drawable) - XFreePixmap(drw->dpy, drw->drawable); - drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, drw->depth); + drw->w = w; + drw->h = h; + if (drw->drawable) + XFreePixmap(drw->dpy, drw->drawable); + drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, drw->depth); } void drw_free(Drw *drw) { - XFreePixmap(drw->dpy, drw->drawable); - XFreeGC(drw->dpy, drw->gc); - free(drw); + XFreePixmap(drw->dpy, drw->drawable); + XFreeGC(drw->dpy, drw->gc); + free(drw); } /* This function is an implementation detail. Library users should use @@ -107,92 +107,92 @@ drw_free(Drw *drw) static Fnt * xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern) { - Fnt *font; - XftFont *xfont = NULL; - FcPattern *pattern = NULL; + Fnt *font; + XftFont *xfont = NULL; + FcPattern *pattern = NULL; - if (fontname) { - /* Using the pattern found at font->xfont->pattern does not yield the - * same substitution results as using the pattern returned by - * FcNameParse; using the latter results in the desired fallback - * behaviour whereas the former just results in missing-character - * rectangles being drawn, at least with some fonts. */ - if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) { - fprintf(stderr, "error, cannot load font from name: '%s'\n", fontname); - return NULL; - } - if (!(pattern = FcNameParse((FcChar8 *) fontname))) { - fprintf(stderr, "error, cannot parse font name to pattern: '%s'\n", fontname); - XftFontClose(drw->dpy, xfont); - return NULL; - } - } else if (fontpattern) { - if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) { - fprintf(stderr, "error, cannot load font from pattern.\n"); - return NULL; - } - } else { - die("no font specified."); - } + if (fontname) { + /* Using the pattern found at font->xfont->pattern does not yield the + * same substitution results as using the pattern returned by + * FcNameParse; using the latter results in the desired fallback + * behaviour whereas the former just results in missing-character + * rectangles being drawn, at least with some fonts. */ + if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) { + fprintf(stderr, "error, cannot load font from name: '%s'\n", fontname); + return NULL; + } + if (!(pattern = FcNameParse((FcChar8 *) fontname))) { + fprintf(stderr, "error, cannot parse font name to pattern: '%s'\n", fontname); + XftFontClose(drw->dpy, xfont); + return NULL; + } + } else if (fontpattern) { + if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) { + fprintf(stderr, "error, cannot load font from pattern.\n"); + return NULL; + } + } else { + die("no font specified."); + } - font = ecalloc(1, sizeof(Fnt)); - font->xfont = xfont; - font->pattern = pattern; - font->h = xfont->ascent + xfont->descent; - font->dpy = drw->dpy; + font = ecalloc(1, sizeof(Fnt)); + font->xfont = xfont; + font->pattern = pattern; + font->h = xfont->ascent + xfont->descent; + font->dpy = drw->dpy; - return font; + return font; } static void xfont_free(Fnt *font) { - if (!font) - return; - if (font->pattern) - FcPatternDestroy(font->pattern); - XftFontClose(font->dpy, font->xfont); - free(font); + if (!font) + return; + if (font->pattern) + FcPatternDestroy(font->pattern); + XftFontClose(font->dpy, font->xfont); + free(font); } Fnt* drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount) { - Fnt *cur, *ret = NULL; - size_t i; + Fnt *cur, *ret = NULL; + size_t i; - if (!drw || !fonts) - return NULL; + if (!drw || !fonts) + return NULL; - for (i = 1; i <= fontcount; i++) { - if ((cur = xfont_create(drw, fonts[fontcount - i], NULL))) { - cur->next = ret; - ret = cur; - } - } - return (drw->fonts = ret); + for (i = 1; i <= fontcount; i++) { + if ((cur = xfont_create(drw, fonts[fontcount - i], NULL))) { + cur->next = ret; + ret = cur; + } + } + return (drw->fonts = ret); } void drw_fontset_free(Fnt *font) { - if (font) { - drw_fontset_free(font->next); - xfont_free(font); - } + if (font) { + drw_fontset_free(font->next); + xfont_free(font); + } } void drw_clr_create(Drw *drw, Clr *dest, const char *clrname, unsigned int alpha) { - if (!drw || !dest || !clrname) - return; + if (!drw || !dest || !clrname) + return; - if (!XftColorAllocName(drw->dpy, drw->visual, drw->cmap, - clrname, dest)) - die("error, cannot allocate color '%s'", clrname); + if (!XftColorAllocName(drw->dpy, drw->visual, drw->cmap, + clrname, dest)) + die("error, cannot allocate color '%s'", clrname); - dest->pixel = (dest->pixel & 0x00ffffffU) | (alpha << 24); + dest->pixel = (dest->pixel & 0x00ffffffU) | (alpha << 24); } /* Wrapper to create color schemes. The caller has to call free(3) on the @@ -200,224 +200,224 @@ drw_clr_create(Drw *drw, Clr *dest, const char *clrname, unsigned int alpha) Clr * drw_scm_create(Drw *drw, const char *clrnames[], unsigned int alphas[], size_t clrcount) { - size_t i; - Clr *ret; + size_t i; + Clr *ret; - /* need at least two colors for a scheme */ - if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor)))) - return NULL; + /* need at least two colors for a scheme */ + if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor)))) + return NULL; - for (i = 0; i < clrcount; i++) - drw_clr_create(drw, &ret[i], clrnames[i], alphas[i]); - return ret; + for (i = 0; i < clrcount; i++) + drw_clr_create(drw, &ret[i], clrnames[i], alphas[i]); + return ret; } void drw_setfontset(Drw *drw, Fnt *set) { - if (drw) - drw->fonts = set; + if (drw) + drw->fonts = set; } void drw_setscheme(Drw *drw, Clr *scm) { - if (drw) - drw->scheme = scm; + if (drw) + drw->scheme = scm; } void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert) { - if (!drw || !drw->scheme) - return; - XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel); - if (filled) - XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); - else - XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1); + if (!drw || !drw->scheme) + return; + XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel); + if (filled) + XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); + else + XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1); } int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert) { - char buf[1024]; - int ty; - unsigned int ew; - XftDraw *d = NULL; - Fnt *usedfont, *curfont, *nextfont; - size_t i, len; - int utf8strlen, utf8charlen, render = x || y || w || h; - long utf8codepoint = 0; - const char *utf8str; - FcCharSet *fccharset; - FcPattern *fcpattern; - FcPattern *match; - XftResult result; - int charexists = 0; + char buf[1024]; + int ty; + unsigned int ew; + XftDraw *d = NULL; + Fnt *usedfont, *curfont, *nextfont; + size_t i, len; + int utf8strlen, utf8charlen, render = x || y || w || h; + long utf8codepoint = 0; + const char *utf8str; + FcCharSet *fccharset; + FcPattern *fcpattern; + FcPattern *match; + XftResult result; + int charexists = 0; - if (!drw || (render && !drw->scheme) || !text || !drw->fonts) - return 0; + if (!drw || (render && !drw->scheme) || !text || !drw->fonts) + return 0; - if (!render) { - w = ~w; - } else { - XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel); - XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); - d = XftDrawCreate(drw->dpy, drw->drawable, drw->visual, drw->cmap); - x += lpad; - w -= lpad; - } + if (!render) { + w = ~w; + } else { + XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel); + XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); + d = XftDrawCreate(drw->dpy, drw->drawable, drw->visual, drw->cmap); + x += lpad; + w -= lpad; + } - usedfont = drw->fonts; - while (1) { - utf8strlen = 0; - utf8str = text; - nextfont = NULL; - while (*text) { - utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ); - for (curfont = drw->fonts; curfont; curfont = curfont->next) { - charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint); - if (charexists) { - if (curfont == usedfont) { - utf8strlen += utf8charlen; - text += utf8charlen; - } else { - nextfont = curfont; - } - break; - } - } + usedfont = drw->fonts; + while (1) { + utf8strlen = 0; + utf8str = text; + nextfont = NULL; + while (*text) { + utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ); + for (curfont = drw->fonts; curfont; curfont = curfont->next) { + charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint); + if (charexists) { + if (curfont == usedfont) { + utf8strlen += utf8charlen; + text += utf8charlen; + } else { + nextfont = curfont; + } + break; + } + } - if (!charexists || nextfont) - break; - else - charexists = 0; - } + if (!charexists || nextfont) + break; + else + charexists = 0; + } - if (utf8strlen) { - drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL); - /* shorten text if necessary */ - for (len = MIN(utf8strlen, sizeof(buf) - 1); len && ew > w; len--) - drw_font_getexts(usedfont, utf8str, len, &ew, NULL); + if (utf8strlen) { + drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL); + /* shorten text if necessary */ + for (len = MIN(utf8strlen, sizeof(buf) - 1); len && ew > w; len--) + drw_font_getexts(usedfont, utf8str, len, &ew, NULL); - if (len) { - memcpy(buf, utf8str, len); - buf[len] = '\0'; - if (len < utf8strlen) - for (i = len; i && i > len - 3; buf[--i] = '.') - ; /* NOP */ + if (len) { + memcpy(buf, utf8str, len); + buf[len] = '\0'; + if (len < utf8strlen) + for (i = len; i && i > len - 3; buf[--i] = '.') + ; /* NOP */ - if (render) { - ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent; - XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg], - usedfont->xfont, x, ty, (XftChar8 *)buf, len); - } - x += ew; - w -= ew; - } - } + if (render) { + ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent; + XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg], + usedfont->xfont, x, ty, (XftChar8 *)buf, len); + } + x += ew; + w -= ew; + } + } - if (!*text) { - break; - } else if (nextfont) { - charexists = 0; - usedfont = nextfont; - } else { - /* Regardless of whether or not a fallback font is found, the - * character must be drawn. */ - charexists = 1; + if (!*text) { + break; + } else if (nextfont) { + charexists = 0; + usedfont = nextfont; + } else { + /* Regardless of whether or not a fallback font is found, the + * character must be drawn. */ + charexists = 1; - fccharset = FcCharSetCreate(); - FcCharSetAddChar(fccharset, utf8codepoint); + fccharset = FcCharSetCreate(); + FcCharSetAddChar(fccharset, utf8codepoint); - if (!drw->fonts->pattern) { - /* Refer to the comment in xfont_create for more information. */ - die("the first font in the cache must be loaded from a font string."); - } + if (!drw->fonts->pattern) { + /* Refer to the comment in xfont_create for more information. */ + die("the first font in the cache must be loaded from a font string."); + } - fcpattern = FcPatternDuplicate(drw->fonts->pattern); - FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset); - FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue); + fcpattern = FcPatternDuplicate(drw->fonts->pattern); + FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset); + FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue); - FcConfigSubstitute(NULL, fcpattern, FcMatchPattern); - FcDefaultSubstitute(fcpattern); - match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result); + FcConfigSubstitute(NULL, fcpattern, FcMatchPattern); + FcDefaultSubstitute(fcpattern); + match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result); - FcCharSetDestroy(fccharset); - FcPatternDestroy(fcpattern); + FcCharSetDestroy(fccharset); + FcPatternDestroy(fcpattern); - if (match) { - usedfont = xfont_create(drw, NULL, match); - if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) { - for (curfont = drw->fonts; curfont->next; curfont = curfont->next) - ; /* NOP */ - curfont->next = usedfont; - } else { - xfont_free(usedfont); - usedfont = drw->fonts; - } - } - } - } - if (d) - XftDrawDestroy(d); + if (match) { + usedfont = xfont_create(drw, NULL, match); + if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) { + for (curfont = drw->fonts; curfont->next; curfont = curfont->next) + ; /* NOP */ + curfont->next = usedfont; + } else { + xfont_free(usedfont); + usedfont = drw->fonts; + } + } + } + } + if (d) + XftDrawDestroy(d); - return x + (render ? w : 0); + return x + (render ? w : 0); } void drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h) { - if (!drw) - return; + if (!drw) + return; - XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y); - XSync(drw->dpy, False); + XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y); + XSync(drw->dpy, False); } unsigned int drw_fontset_getwidth(Drw *drw, const char *text) { - if (!drw || !drw->fonts || !text) - return 0; - return drw_text(drw, 0, 0, 0, 0, 0, text, 0); + if (!drw || !drw->fonts || !text) + return 0; + return drw_text(drw, 0, 0, 0, 0, 0, text, 0); } void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h) { - XGlyphInfo ext; + XGlyphInfo ext; - if (!font || !text) - return; + if (!font || !text) + return; - XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext); - if (w) - *w = ext.xOff; - if (h) - *h = font->h; + XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext); + if (w) + *w = ext.xOff; + if (h) + *h = font->h; } Cur * drw_cur_create(Drw *drw, int shape) { - Cur *cur; + Cur *cur; - if (!drw || !(cur = ecalloc(1, sizeof(Cur)))) - return NULL; + if (!drw || !(cur = ecalloc(1, sizeof(Cur)))) + return NULL; - cur->cursor = XCreateFontCursor(drw->dpy, shape); + cur->cursor = XCreateFontCursor(drw->dpy, shape); - return cur; + return cur; } void drw_cur_free(Drw *drw, Cur *cursor) { - if (!cursor) - return; + if (!cursor) + return; - XFreeCursor(drw->dpy, cursor->cursor); - free(cursor); + XFreeCursor(drw->dpy, cursor->cursor); + free(cursor); } diff --git a/drw.h b/drw.h index b91a753..0c86110 100644 --- a/drw.h +++ b/drw.h @@ -1,32 +1,32 @@ /* See LICENSE file for copyright and license details. */ typedef struct { - Cursor cursor; + Cursor cursor; } Cur; typedef struct Fnt { - Display *dpy; - unsigned int h; - XftFont *xfont; - FcPattern *pattern; - struct Fnt *next; + Display *dpy; + unsigned int h; + XftFont *xfont; + FcPattern *pattern; + struct Fnt *next; } Fnt; enum { ColFg, ColBg }; /* Clr scheme index */ typedef XftColor Clr; typedef struct { - unsigned int w, h; - Display *dpy; - int screen; - Window root; - Visual *visual; - unsigned int depth; - Colormap cmap; - Drawable drawable; - GC gc; - Clr *scheme; - Fnt *fonts; + unsigned int w, h; + Display *dpy; + int screen; + Window root; + Visual *visual; + unsigned int depth; + Colormap cmap; + Drawable drawable; + GC gc; + Clr *scheme; + Fnt *fonts; } Drw; /* Drawable abstraction */ diff --git a/pinentry-dmenu.c b/pinentry-dmenu.c index 815888e..e409a9b 100644 --- a/pinentry-dmenu.c +++ b/pinentry-dmenu.c @@ -29,8 +29,8 @@ #define CONFIG_DIR "/.gnupg" #define CONFIG_FILE "/pinentry-dmenu.conf" #define INTERSECT(x, y, w, h, r) \ - (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \ - && MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org))) + (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \ + && MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org))) #define LENGTH(X) (sizeof(X) / sizeof(X[0])) #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) #define MINDESCLEN 8 @@ -77,835 +77,835 @@ pinentry_t pinentry_info; static void xinitvisual(void) { - XVisualInfo *infos; - XRenderPictFormat *fmt; - int nitems; - int i; + XVisualInfo *infos; + XRenderPictFormat *fmt; + int nitems; + int i; - XVisualInfo tpl = { - .screen = screen, - .depth = 32, - .class = TrueColor - }; - long masks = VisualScreenMask | VisualDepthMask | VisualClassMask; + XVisualInfo tpl = { + .screen = screen, + .depth = 32, + .class = TrueColor + }; + long masks = VisualScreenMask | VisualDepthMask | VisualClassMask; - infos = XGetVisualInfo(dpy, masks, &tpl, &nitems); - visual = NULL; - for(i = 0; i < nitems; i ++) { - fmt = XRenderFindVisualFormat(dpy, infos[i].visual); - if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) { - visual = infos[i].visual; - depth = infos[i].depth; - cmap = XCreateColormap(dpy, root, visual, AllocNone); - useargb = 1; - break; - } - } + infos = XGetVisualInfo(dpy, masks, &tpl, &nitems); + visual = NULL; + for(i = 0; i < nitems; i ++) { + fmt = XRenderFindVisualFormat(dpy, infos[i].visual); + if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) { + visual = infos[i].visual; + depth = infos[i].depth; + cmap = XCreateColormap(dpy, root, visual, AllocNone); + useargb = 1; + break; + } + } - XFree(infos); + XFree(infos); - if (! visual) { - visual = DefaultVisual(dpy, screen); - depth = DefaultDepth(dpy, screen); - cmap = DefaultColormap(dpy, screen); - } + if (! visual) { + visual = DefaultVisual(dpy, screen); + depth = DefaultDepth(dpy, screen); + cmap = DefaultColormap(dpy, screen); + } } static int drawitem(const char* text, Bool sel, int x, int y, int w) { - unsigned int i = (sel) ? SchemeSelect : SchemeNormal; + unsigned int i = (sel) ? SchemeSelect : SchemeNormal; - drw_setscheme(drw, scheme[i]); + drw_setscheme(drw, scheme[i]); - return drw_text(drw, x, y, w, bh, lrpad / 2, text, 0); + return drw_text(drw, x, y, w, bh, lrpad / 2, text, 0); } static void grabfocus(void) { - Window focuswin; - int i, revertwin; + Window focuswin; + int i, revertwin; - for (i = 0; i < 100; ++i) { - XGetInputFocus(dpy, &focuswin, &revertwin); - if (focuswin == win) { - return; - } - XSetInputFocus(dpy, win, RevertToParent, CurrentTime); - usleep(1000); - } + for (i = 0; i < 100; ++i) { + XGetInputFocus(dpy, &focuswin, &revertwin); + if (focuswin == win) { + return; + } + XSetInputFocus(dpy, win, RevertToParent, CurrentTime); + usleep(1000); + } - die("cannot grab focus"); + die("cannot grab focus"); } static void grabkeyboard(void) { - int i; + int i; - if (embedded) { - return; - } + if (embedded) { + return; + } - /* Try to grab keyboard, - * we may have to wait for another process to ungrab */ - for (i = 0; i < 1000; i++) { - if (XGrabKeyboard(dpy, DefaultRootWindow(dpy), True, GrabModeAsync, - GrabModeAsync, CurrentTime) == GrabSuccess) { - return; - } - usleep(1000); - } + /* Try to grab keyboard, + * we may have to wait for another process to ungrab */ + for (i = 0; i < 1000; i++) { + if (XGrabKeyboard(dpy, DefaultRootWindow(dpy), True, GrabModeAsync, + GrabModeAsync, CurrentTime) == GrabSuccess) { + return; + } + usleep(1000); + } - die("cannot grab keyboard"); + die("cannot grab keyboard"); } static size_t nextrune(int cursor, int inc) { - ssize_t n; + ssize_t n; - /* Return location of next utf8 rune in the given direction (+1 or -1) */ - for (n = cursor + inc; - n + inc >= 0 && (pin[n] & 0xc0) == 0x80; - n += inc); + /* Return location of next utf8 rune in the given direction (+1 or -1) */ + for (n = cursor + inc; + n + inc >= 0 && (pin[n] & 0xc0) == 0x80; + n += inc); - return n; + return n; } static void setup_pin(char* pin_ptr, int len, int reset) { - pin = pin_ptr; - pin_len = len; + pin = pin_ptr; + pin_len = len; - if (reset) { - promptw = (prompt) ? TEXTW(prompt) - lrpad / 4 : 0; - cursor = 0; + if (reset) { + promptw = (prompt) ? TEXTW(prompt) - lrpad / 4 : 0; + cursor = 0; - if (pin) { - pin[0] = '\0'; - } - } + if (pin) { + pin[0] = '\0'; + } + } } static void insert(const char *str, ssize_t n) { - size_t len = strlen(pin); + size_t len = strlen(pin); - // FIXME: Pinentry crashes when increasing the pin buffer the second time. - // Other pinentry programs has a limited password field length. - if (len + n > pin_len - 1) { - if (repeat) { - pin_repeat_len = 2 * pin_repeat_len; - pin_repeat = secmem_realloc(pin_repeat, pin_repeat_len); - setup_pin(pin_repeat, pin_repeat_len, 0); - if (!pin_repeat) { - pin_len = 0; - } - } else { - if (!pinentry_setbufferlen(pinentry_info, 2 * pinentry_info->pin_len)) { - pin_len = 0; - } else { - setup_pin(pinentry_info->pin, pinentry_info->pin_len, 0); - } - } - if (pin_len == 0) { - printf("Error: Couldn't allocate secure memory\n"); - return; - } - } + // FIXME: Pinentry crashes when increasing the pin buffer the second time. + // Other pinentry programs has a limited password field length. + if (len + n > pin_len - 1) { + if (repeat) { + pin_repeat_len = 2 * pin_repeat_len; + pin_repeat = secmem_realloc(pin_repeat, pin_repeat_len); + setup_pin(pin_repeat, pin_repeat_len, 0); + if (!pin_repeat) { + pin_len = 0; + } + } else { + if (!pinentry_setbufferlen(pinentry_info, 2 * pinentry_info->pin_len)) { + pin_len = 0; + } else { + setup_pin(pinentry_info->pin, pinentry_info->pin_len, 0); + } + } + if (pin_len == 0) { + printf("Error: Couldn't allocate secure memory\n"); + return; + } + } - /* Move existing text out of the way, insert new text, and update cursor */ - memmove(&pin[cursor + n], &pin[cursor], pin_len - cursor - MAX(n, 0)); + /* Move existing text out of the way, insert new text, and update cursor */ + memmove(&pin[cursor + n], &pin[cursor], pin_len - cursor - MAX(n, 0)); - if (n > 0) { - memcpy(&pin[cursor], str, n); - } + if (n > 0) { + memcpy(&pin[cursor], str, n); + } - cursor += n; - pin[len + n] = '\0'; + cursor += n; + pin[len + n] = '\0'; } static void drawwin(void) { - unsigned int curpos; - int x = 0, pb, pbw = 0, fh = drw->fonts->h, i; - size_t asterlen = strlen(asterisk); - size_t pdesclen; - int leftinput; - char* censort; + unsigned int curpos; + int x = 0, pb, pbw = 0, fh = drw->fonts->h, i; + size_t asterlen = strlen(asterisk); + size_t pdesclen; + int leftinput; + char* censort; - char* pprompt = (repeat) ? pinentry_info->repeat_passphrase : pinentry_info->prompt; - int ppromptw = (pprompt) ? TEXTW(pprompt) : 0; + char* pprompt = (repeat) ? pinentry_info->repeat_passphrase : pinentry_info->prompt; + int ppromptw = (pprompt) ? TEXTW(pprompt) : 0; - unsigned int censortl = (center) ? 0 : minpwlen * TEXTW(asterisk) / strlen(asterisk); - unsigned int confirml = TEXTW(" YesNo ") + 3 * lrpad; + unsigned int censortl = (center) ? 0 : minpwlen * TEXTW(asterisk) / strlen(asterisk); + unsigned int confirml = TEXTW(" YesNo ") + 3 * lrpad; - drw_setscheme(drw, scheme[SchemeNormal]); - drw_rect(drw, 0, 0, mw, mh, 1, 1); + drw_setscheme(drw, scheme[SchemeNormal]); + drw_rect(drw, 0, 0, mw, mh, 1, 1); - if (prompt) { - drw_setscheme(drw, scheme[SchemePrompt]); - x = drw_text(drw, x, 0, promptw, bh, lrpad / 2, prompt, 0); - } + if (prompt) { + drw_setscheme(drw, scheme[SchemePrompt]); + x = drw_text(drw, x, 0, promptw, bh, lrpad / 2, prompt, 0); + } - if (pprompt) { - drw_setscheme(drw, scheme[SchemePrompt]); - drw_text(drw, x, 0, ppromptw, bh, lrpad / 2, pprompt, 0); - x += ppromptw; - } + if (pprompt) { + drw_setscheme(drw, scheme[SchemePrompt]); + drw_text(drw, x, 0, ppromptw, bh, lrpad / 2, pprompt, 0); + x += ppromptw; + } - if (pinentry_info->description) { - pb = mw - x; - pdesclen = strlen(pinentry_info->description); + if (pinentry_info->description) { + pb = mw - x; + pdesclen = strlen(pinentry_info->description); - if (pb > 0) { - pb -= (winmode == WinPin) ? censortl : confirml; - pbw = MINDESCLEN * pdescw / pdesclen; - pbw = MIN(pbw, pdescw); + if (pb > 0) { + pb -= (winmode == WinPin) ? censortl : confirml; + pbw = MINDESCLEN * pdescw / pdesclen; + pbw = MIN(pbw, pdescw); - if (pb >= pbw) { - pbw = MAX(pbw, pdescw); - pbw = MIN(pbw, pb); - pb = mw - pbw; + if (pb >= pbw) { + pbw = MAX(pbw, pdescw); + pbw = MIN(pbw, pb); + pb = mw - pbw; - for (i = 0; i < pdesclen; i++) { - if (pinentry_info->description[i] == '\n') { - pinentry_info->description[i] = ' '; - } - } + for (i = 0; i < pdesclen; i++) { + if (pinentry_info->description[i] == '\n') { + pinentry_info->description[i] = ' '; + } + } - drw_setscheme(drw, scheme[SchemeDesc]); - if (center) { - drw_text(drw, promptw + ppromptw, lineheight, centerwidth, - bh + borderwidth, lrpad / 2, - pinentry_info->description, 0); - } else { - drw_text(drw, pb, 0, pbw, bh, lrpad / 2, - pinentry_info->description, 0); - } - } else { - pbw = 0; - } - } - } + drw_setscheme(drw, scheme[SchemeDesc]); + if (center) { + drw_text(drw, promptw + ppromptw, lineheight, centerwidth, + bh + borderwidth, lrpad / 2, + pinentry_info->description, 0); + } else { + drw_text(drw, pb, 0, pbw, bh, lrpad / 2, + pinentry_info->description, 0); + } + } else { + pbw = 0; + } + } + } - /* Draw input field */ - drw_setscheme(drw, scheme[SchemeNormal]); + /* Draw input field */ + drw_setscheme(drw, scheme[SchemeNormal]); - if (winmode == WinPin) { - censort = ecalloc(1, asterlen * pin_len); + if (winmode == WinPin) { + censort = ecalloc(1, asterlen * pin_len); - for (i = 0; i < asterlen * strlen(pin); i += asterlen) { - memcpy(&censort[i], asterisk, asterlen); - } + for (i = 0; i < asterlen * strlen(pin); i += asterlen) { + memcpy(&censort[i], asterisk, asterlen); + } - censort[i+1] = '\n'; - leftinput = (center) ? mw - x : mw - x - pbw; - drw_text(drw, x, 0, leftinput, bh, lrpad / 2, censort, 0); - drw_font_getexts(drw->fonts, censort, cursor * asterlen, &curpos, NULL); + censort[i+1] = '\n'; + leftinput = (center) ? mw - x : mw - x - pbw; + drw_text(drw, x, 0, leftinput, bh, lrpad / 2, censort, 0); + drw_font_getexts(drw->fonts, censort, cursor * asterlen, &curpos, NULL); - if ((curpos += lrpad / 2 - 1) < leftinput) { - drw_setscheme(drw, scheme[SchemeNormal]); - drw_rect(drw, x + curpos, 2 + (bh - fh) / 2, 2, fh - 4, 1, 0); - } + if ((curpos += lrpad / 2 - 1) < leftinput) { + drw_setscheme(drw, scheme[SchemeNormal]); + drw_rect(drw, x + curpos, 2 + (bh - fh) / 2, 2, fh - 4, 1, 0); + } - free(censort); - } else { - x = drawitem("No", (sel == No), x, 0, TEXTW("No")); - x = drawitem("Yes", (sel == Yes), x, 0, TEXTW("Yes")); - } + free(censort); + } else { + x = drawitem("No", (sel == No), x, 0, TEXTW("No")); + x = drawitem("Yes", (sel == Yes), x, 0, TEXTW("Yes")); + } - drw_map(drw, win, 0, 0, mw, mh); + drw_map(drw, win, 0, 0, mw, mh); } static void setup(void) { - int x, y, i = 0, j; - unsigned int du; - XSetWindowAttributes swa; - XIM xim; - Window w, dw, *dws; - XWindowAttributes wa; - XClassHint ch = {"pinentry-dmenu", "prinentry-dmenu"}; + int x, y, i = 0, j; + unsigned int du; + XSetWindowAttributes swa; + XIM xim; + Window w, dw, *dws; + XWindowAttributes wa; + XClassHint ch = {"pinentry-dmenu", "prinentry-dmenu"}; #ifdef XINERAMA - XineramaScreenInfo *info; - Window pw; - int a, di, n, area = 0; + XineramaScreenInfo *info; + Window pw; + int a, di, n, area = 0; #endif - /* Init appearance */ - for (j = 0; j < SchemeLast; j++) - scheme[j] = drw_scm_create(drw, colors[j], alphas[j], 2); + /* Init appearance */ + for (j = 0; j < SchemeLast; j++) + scheme[j] = drw_scm_create(drw, colors[j], alphas[j], 2); - clip = XInternAtom(dpy, "CLIPBOARD", False); - utf8 = XInternAtom(dpy, "UTF8_STRING", False); + clip = XInternAtom(dpy, "CLIPBOARD", False); + utf8 = XInternAtom(dpy, "UTF8_STRING", False); - /* Calculate menu geometry */ - bh = drw->fonts->h + 2; - bh = MAX(bh,lineheight); /* make a menu line AT LEAST 'lineheight' tall */ - mh = (center) ? bh * 2 : bh; + /* Calculate menu geometry */ + bh = drw->fonts->h + 2; + bh = MAX(bh,lineheight); /* make a menu line AT LEAST 'lineheight' tall */ + mh = (center) ? bh * 2 : bh; #ifdef XINERAMA - info = XineramaQueryScreens(dpy, &n); + info = XineramaQueryScreens(dpy, &n); - if (parentwin == root && info) { - XGetInputFocus(dpy, &w, &di); - if (mon >= 0 && mon < n) { - i = mon; - } else if (w != root && w != PointerRoot && w != None) { - /* Find top-level window containing current input focus */ - do { - if (XQueryTree(dpy, (pw = w), &dw, &w, &dws, &du) && dws) { - XFree(dws); - } - } while (w != root && w != pw); - /* Find xinerama screen with which the window intersects most */ - if (XGetWindowAttributes(dpy, pw, &wa)) { - for (j = 0; j < n; j++) { - a = INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j]); - if (a > area) { - area = a; - i = j; - } - } - } - } - /* No focused window is on screen, so use pointer location instead */ - if (mon < 0 && !area - && XQueryPointer(dpy, root, &dw, &dw, &x, &y, &di, &di, &du)) { - for (i = 0; i < n; i++) { - if (INTERSECT(x, y, 1, 1, info[i])) { - break; - } - } - } + if (parentwin == root && info) { + XGetInputFocus(dpy, &w, &di); + if (mon >= 0 && mon < n) { + i = mon; + } else if (w != root && w != PointerRoot && w != None) { + /* Find top-level window containing current input focus */ + do { + if (XQueryTree(dpy, (pw = w), &dw, &w, &dws, &du) && dws) { + XFree(dws); + } + } while (w != root && w != pw); + /* Find xinerama screen with which the window intersects most */ + if (XGetWindowAttributes(dpy, pw, &wa)) { + for (j = 0; j < n; j++) { + a = INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j]); + if (a > area) { + area = a; + i = j; + } + } + } + } + /* No focused window is on screen, so use pointer location instead */ + if (mon < 0 && !area + && XQueryPointer(dpy, root, &dw, &dw, &x, &y, &di, &di, &du)) { + for (i = 0; i < n; i++) { + if (INTERSECT(x, y, 1, 1, info[i])) { + break; + } + } + } - if (center) { - mw = MIN(centerwidth, info[i].width); - x = info[i].x_org + ((info[i].width - mw) / 2); - y = info[i].y_org + ((info[i].height - mh) / 2); - } else { - x = info[i].x_org; - y = info[i].y_org + (bottom ? info[i].height - mh - (borderwidth * 2) : 0); - mw = info[i].width - (borderwidth * 2); - } + if (center) { + mw = MIN(centerwidth, info[i].width); + x = info[i].x_org + ((info[i].width - mw) / 2); + y = info[i].y_org + ((info[i].height - mh) / 2); + } else { + x = info[i].x_org; + y = info[i].y_org + (bottom ? info[i].height - mh - (borderwidth * 2) : 0); + mw = info[i].width - (borderwidth * 2); + } - XFree(info); - } else + XFree(info); + } else #endif - { - if (!XGetWindowAttributes(dpy, parentwin, &wa)) { - die("could not get embedding window attributes: 0x%lx", parentwin); - } + { + if (!XGetWindowAttributes(dpy, parentwin, &wa)) { + die("could not get embedding window attributes: 0x%lx", parentwin); + } - if (center) { - mw = MIN(centerwidth, wa.width); - x = (wa.width - mw) / 2; - y = (wa.height - mh) / 2; - } else { - x = 0; - y = bottom ? wa.height - mh - (borderwidth * 2) : 0; - mw = wa.width - (borderwidth * 2); - } - } + if (center) { + mw = MIN(centerwidth, wa.width); + x = (wa.width - mw) / 2; + y = (wa.height - mh) / 2; + } else { + x = 0; + y = bottom ? wa.height - mh - (borderwidth * 2) : 0; + mw = wa.width - (borderwidth * 2); + } + } - pdescw = (pinentry_info->description) ? TEXTW(pinentry_info->description) : 0; + pdescw = (pinentry_info->description) ? TEXTW(pinentry_info->description) : 0; - /* Create menu window */ - swa.override_redirect = True; - swa.background_pixel = 0; - swa.border_pixel = 0; - swa.colormap = cmap; - swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask; - win = XCreateWindow(dpy, parentwin, x, y, mw, mh, borderwidth, - depth, CopyFromParent, visual, - CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWColormap | CWEventMask, &swa); - if (borderwidth) - XSetWindowBorder(dpy, win, scheme[SchemeSelect][ColBg].pixel); - XSetClassHint(dpy, win, &ch); + /* Create menu window */ + swa.override_redirect = True; + swa.background_pixel = 0; + swa.border_pixel = 0; + swa.colormap = cmap; + swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask; + win = XCreateWindow(dpy, parentwin, x, y, mw, mh, borderwidth, + depth, CopyFromParent, visual, + CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWColormap | CWEventMask, &swa); + if (borderwidth) + XSetWindowBorder(dpy, win, scheme[SchemeSelect][ColBg].pixel); + XSetClassHint(dpy, win, &ch); - /* Open input methods */ - xim = XOpenIM(dpy, NULL, NULL, NULL); - xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, - XNClientWindow, win, XNFocusWindow, win, NULL); - XMapRaised(dpy, win); + /* Open input methods */ + xim = XOpenIM(dpy, NULL, NULL, NULL); + xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, + XNClientWindow, win, XNFocusWindow, win, NULL); + XMapRaised(dpy, win); - if (embedded) { - XSelectInput(dpy, parentwin, FocusChangeMask); + if (embedded) { + XSelectInput(dpy, parentwin, FocusChangeMask); - if (XQueryTree(dpy, parentwin, &dw, &w, &dws, &du) && dws) { - for (i = 0; i < du && dws[i] != win; ++i) { - XSelectInput(dpy, dws[i], FocusChangeMask); - } + if (XQueryTree(dpy, parentwin, &dw, &w, &dws, &du) && dws) { + for (i = 0; i < du && dws[i] != win; ++i) { + XSelectInput(dpy, dws[i], FocusChangeMask); + } - XFree(dws); - } - grabfocus(); - } + XFree(dws); + } + grabfocus(); + } - drw_resize(drw, mw, mh); + drw_resize(drw, mw, mh); } static void cleanup(void) { - size_t i; + size_t i; - XUngrabKey(dpy, AnyKey, AnyModifier, root); - for (i = 0; i < SchemeLast; i++) - free(scheme[i]); - drw_free(drw); - XSync(dpy, False); - XCloseDisplay(dpy); + XUngrabKey(dpy, AnyKey, AnyModifier, root); + for (i = 0; i < SchemeLast; i++) + free(scheme[i]); + drw_free(drw); + XSync(dpy, False); + XCloseDisplay(dpy); } static int keypress_confirm(XKeyEvent *ev, KeySym ksym) { - if (ev->state & ControlMask) { - switch(ksym) { - case XK_c: - pinentry_info->canceled = 1; - sel = No; - return 1; - default: - return 1; - } - } + if (ev->state & ControlMask) { + switch(ksym) { + case XK_c: + pinentry_info->canceled = 1; + sel = No; + return 1; + default: + return 1; + } + } - switch(ksym) { - case XK_KP_Enter: - case XK_Return: - if (sel != Nothing) { - return 1; - } - break; - case XK_y: - case XK_Y: - sel = Yes; - return 1; - case XK_n: - case XK_N: - sel = No; - return 1; - case XK_g: - case XK_G: - case XK_Escape: - pinentry_info->canceled = 1; - sel = No; - return 1; - case XK_h: - case XK_j: - case XK_Home: - case XK_Left: - case XK_Prior: - case XK_Up: - sel = No; - break; - case XK_k: - case XK_l: - case XK_Down: - case XK_End: - case XK_Next: - case XK_Right: - sel = Yes; - break; - } + switch(ksym) { + case XK_KP_Enter: + case XK_Return: + if (sel != Nothing) { + return 1; + } + break; + case XK_y: + case XK_Y: + sel = Yes; + return 1; + case XK_n: + case XK_N: + sel = No; + return 1; + case XK_g: + case XK_G: + case XK_Escape: + pinentry_info->canceled = 1; + sel = No; + return 1; + case XK_h: + case XK_j: + case XK_Home: + case XK_Left: + case XK_Prior: + case XK_Up: + sel = No; + break; + case XK_k: + case XK_l: + case XK_Down: + case XK_End: + case XK_Next: + case XK_Right: + sel = Yes; + break; + } - return 0; + return 0; } static int keypress_pin(XKeyEvent *ev, KeySym ksym, char* buf, int len) { - int old; + int old; - if (ev->state & ControlMask) { - switch(ksym) { - case XK_a: ksym = XK_Home; break; - case XK_b: ksym = XK_Left; break; - case XK_c: ksym = XK_Escape; break; - case XK_d: ksym = XK_Delete; break; - case XK_e: ksym = XK_End; break; - case XK_f: ksym = XK_Right; break; - case XK_g: ksym = XK_Escape; break; - case XK_h: ksym = XK_BackSpace; break; - case XK_k: - old = cursor; - cursor = strlen(pin); - insert(NULL, old - cursor); - break; - case XK_u: - insert(NULL, -cursor); - break; - case XK_v: - XConvertSelection(dpy, (ev->state & ShiftMask) ? clip : XA_PRIMARY, - utf8, utf8, win, CurrentTime); - return 0; - case XK_Return: - case XK_KP_Enter: - break; - case XK_bracketleft: - pinentry_info->canceled = 1; - return 1; - default: - return 1; - } - } + if (ev->state & ControlMask) { + switch(ksym) { + case XK_a: ksym = XK_Home; break; + case XK_b: ksym = XK_Left; break; + case XK_c: ksym = XK_Escape; break; + case XK_d: ksym = XK_Delete; break; + case XK_e: ksym = XK_End; break; + case XK_f: ksym = XK_Right; break; + case XK_g: ksym = XK_Escape; break; + case XK_h: ksym = XK_BackSpace; break; + case XK_k: + old = cursor; + cursor = strlen(pin); + insert(NULL, old - cursor); + break; + case XK_u: + insert(NULL, -cursor); + break; + case XK_v: + XConvertSelection(dpy, (ev->state & ShiftMask) ? clip : XA_PRIMARY, + utf8, utf8, win, CurrentTime); + return 0; + case XK_Return: + case XK_KP_Enter: + break; + case XK_bracketleft: + pinentry_info->canceled = 1; + return 1; + default: + return 1; + } + } - switch(ksym) { - case XK_Delete: - if (pin[cursor] == '\0') { - return 0; - } - cursor = nextrune(cursor, +1); - /* Fallthrough */ - case XK_BackSpace: - if (cursor == 0) { - return 0; - } - insert(NULL, nextrune(cursor, -1) - cursor); - break; - case XK_Escape: - pinentry_info->canceled = 1; - return 1; - case XK_Left: - if (cursor > 0) { - cursor = nextrune(cursor, -1); - } - break; - case XK_Right: - if (pin[cursor] != '\0') { - cursor = nextrune(cursor, +1); - } - break; - case XK_Home: - cursor = 0; - break; - case XK_End: - cursor = strlen(pin); - break; - case XK_Return: - case XK_KP_Enter: - return 1; - break; - default: - if (!iscntrl(*buf)) { - insert(buf, len); - } - } + switch(ksym) { + case XK_Delete: + if (pin[cursor] == '\0') { + return 0; + } + cursor = nextrune(cursor, +1); + /* Fallthrough */ + case XK_BackSpace: + if (cursor == 0) { + return 0; + } + insert(NULL, nextrune(cursor, -1) - cursor); + break; + case XK_Escape: + pinentry_info->canceled = 1; + return 1; + case XK_Left: + if (cursor > 0) { + cursor = nextrune(cursor, -1); + } + break; + case XK_Right: + if (pin[cursor] != '\0') { + cursor = nextrune(cursor, +1); + } + break; + case XK_Home: + cursor = 0; + break; + case XK_End: + cursor = strlen(pin); + break; + case XK_Return: + case XK_KP_Enter: + return 1; + break; + default: + if (!iscntrl(*buf)) { + insert(buf, len); + } + } - return 0; + return 0; } static int keypress(XKeyEvent *ev) { - char buf[32]; - int len; - int ret = 1; + char buf[32]; + int len; + int ret = 1; - KeySym ksym = NoSymbol; - Status status; - len = XmbLookupString(xic, ev, buf, sizeof(buf), &ksym, &status); + KeySym ksym = NoSymbol; + Status status; + len = XmbLookupString(xic, ev, buf, sizeof(buf), &ksym, &status); - if (status != XBufferOverflow) { - if (winmode == WinConfirm) { - ret = keypress_confirm(ev, ksym); - } else { - ret = keypress_pin(ev, ksym, buf, len); - } + if (status != XBufferOverflow) { + if (winmode == WinConfirm) { + ret = keypress_confirm(ev, ksym); + } else { + ret = keypress_pin(ev, ksym, buf, len); + } - if (ret == 0) { - drawwin(); - } - } + if (ret == 0) { + drawwin(); + } + } - return ret; + return ret; } static void paste(void) { - char *p, *q; - int di; - unsigned long dl; - Atom da; + char *p, *q; + int di; + unsigned long dl; + Atom da; - /* We have been given the current selection, now insert it into input */ - XGetWindowProperty(dpy, win, utf8, 0, pin_len / 4, False, utf8, &da, &di, - &dl, &dl, (unsigned char **)&p); - insert(p, (q = strchr(p, '\n')) ? q - p : (ssize_t) strlen(p)); - XFree(p); - drawwin(); + /* We have been given the current selection, now insert it into input */ + XGetWindowProperty(dpy, win, utf8, 0, pin_len / 4, False, utf8, &da, &di, + &dl, &dl, (unsigned char **)&p); + insert(p, (q = strchr(p, '\n')) ? q - p : (ssize_t) strlen(p)); + XFree(p); + drawwin(); } void run(void) { - XEvent ev; + XEvent ev; - drawwin(); + drawwin(); - while (!XNextEvent(dpy, &ev)) { - if (XFilterEvent(&ev, win)) { - continue; - } - switch(ev.type) { - case Expose: - if (ev.xexpose.count == 0) { - drw_map(drw, win, 0, 0, mw, mh); - } - break; - case KeyPress: - if (keypress(&ev.xkey)) { - return; - } - break; - case SelectionNotify: - if (ev.xselection.property == utf8) { - paste(); - } - break; - case VisibilityNotify: - if (ev.xvisibility.state != VisibilityUnobscured) { - XRaiseWindow(dpy, win); - } - break; - } - } + while (!XNextEvent(dpy, &ev)) { + if (XFilterEvent(&ev, win)) { + continue; + } + switch(ev.type) { + case Expose: + if (ev.xexpose.count == 0) { + drw_map(drw, win, 0, 0, mw, mh); + } + break; + case KeyPress: + if (keypress(&ev.xkey)) { + return; + } + break; + case SelectionNotify: + if (ev.xselection.property == utf8) { + paste(); + } + break; + case VisibilityNotify: + if (ev.xvisibility.state != VisibilityUnobscured) { + XRaiseWindow(dpy, win); + } + break; + } + } } static void catchsig(int sig) { - if (sig == SIGALRM) { - timed_out = 1; - } + if (sig == SIGALRM) { + timed_out = 1; + } } static void password(void) { - winmode = WinPin; - repeat = 0; - setup_pin(pinentry_info->pin, pinentry_info->pin_len, 1); - run(); + winmode = WinPin; + repeat = 0; + setup_pin(pinentry_info->pin, pinentry_info->pin_len, 1); + run(); - if (!pinentry_info->canceled && pinentry_info->repeat_passphrase) { - repeat = 1; - pin_repeat_len = pinentry_info->pin_len; - pin_repeat = secmem_malloc(pinentry_info->pin_len); - setup_pin(pin_repeat, pin_repeat_len, 1); - run(); + if (!pinentry_info->canceled && pinentry_info->repeat_passphrase) { + repeat = 1; + pin_repeat_len = pinentry_info->pin_len; + pin_repeat = secmem_malloc(pinentry_info->pin_len); + setup_pin(pin_repeat, pin_repeat_len, 1); + run(); - pinentry_info->repeat_okay = (strcmp(pinentry_info->pin, pin_repeat) == 0)? 1 : 0; - secmem_free(pin_repeat); + pinentry_info->repeat_okay = (strcmp(pinentry_info->pin, pin_repeat) == 0)? 1 : 0; + secmem_free(pin_repeat); - if (!pinentry_info->repeat_okay) { - pinentry_info->result = -1; - return; - } - } + if (!pinentry_info->repeat_okay) { + pinentry_info->result = -1; + return; + } + } - if (pinentry_info->canceled) { - pinentry_info->result = -1; - return; - } + if (pinentry_info->canceled) { + pinentry_info->result = -1; + return; + } - pinentry_info->result = strlen(pinentry_info->pin); + pinentry_info->result = strlen(pinentry_info->pin); } static void confirm(void) { - winmode = WinConfirm; - sel = Nothing; - run(); - pinentry_info->result = sel != No; + winmode = WinConfirm; + sel = Nothing; + run(); + pinentry_info->result = sel != No; } static int cmdhandler(pinentry_t received_pinentry) { - struct sigaction sa; - XWindowAttributes wa; + struct sigaction sa; + XWindowAttributes wa; - pinentry_info = received_pinentry; + pinentry_info = received_pinentry; - if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) { - fputs("warning: no locale support\n", stderr); - } - if (!(dpy = XOpenDisplay(pinentry_info->display))) { - die("cannot open display"); - } - screen = DefaultScreen(dpy); - root = RootWindow(dpy, screen); - embedded = (pinentry_info->parent_wid) ? embedded : 0; - parentwin = (embedded) ? pinentry_info->parent_wid : root; - if (!XGetWindowAttributes(dpy, parentwin, &wa)) { - die("could not get embedding window attributes: 0x%lx", parentwin); - } - xinitvisual(); - drw = drw_create(dpy, screen, root, wa.width, wa.height, visual, depth, cmap); - if (!drw_fontset_create(drw, fonts, LENGTH(fonts))) { - die("no fonts could be loaded."); - } - lrpad = drw->fonts->h; - drw_setscheme(drw, scheme[SchemePrompt]); + if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) { + fputs("warning: no locale support\n", stderr); + } + if (!(dpy = XOpenDisplay(pinentry_info->display))) { + die("cannot open display"); + } + screen = DefaultScreen(dpy); + root = RootWindow(dpy, screen); + embedded = (pinentry_info->parent_wid) ? embedded : 0; + parentwin = (embedded) ? pinentry_info->parent_wid : root; + if (!XGetWindowAttributes(dpy, parentwin, &wa)) { + die("could not get embedding window attributes: 0x%lx", parentwin); + } + xinitvisual(); + drw = drw_create(dpy, screen, root, wa.width, wa.height, visual, depth, cmap); + if (!drw_fontset_create(drw, fonts, LENGTH(fonts))) { + die("no fonts could be loaded."); + } + lrpad = drw->fonts->h; + drw_setscheme(drw, scheme[SchemePrompt]); - if (pinentry_info->timeout) { - memset(&sa, 0, sizeof(sa)); - sa.sa_handler = catchsig; - sigaction(SIGALRM, &sa, NULL); - alarm(pinentry_info->timeout); - } + if (pinentry_info->timeout) { + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = catchsig; + sigaction(SIGALRM, &sa, NULL); + alarm(pinentry_info->timeout); + } - grabkeyboard(); - setup(); + grabkeyboard(); + setup(); - if (pinentry_info->pin) { - do { - password(); - } while (!pinentry_info->canceled && pinentry_info->repeat_passphrase - && !pinentry_info->repeat_okay); - } else { - confirm(); - } + if (pinentry_info->pin) { + do { + password(); + } while (!pinentry_info->canceled && pinentry_info->repeat_passphrase + && !pinentry_info->repeat_okay); + } else { + confirm(); + } - cleanup(); + cleanup(); - return pinentry_info->result; + return pinentry_info->result; } pinentry_cmd_handler_t pinentry_cmd_handler = cmdhandler; int main(int argc, char *argv[]) { - Bool bval; - int i, val; - const char *str; - struct passwd *pw; - char path[PATH_MAX]; - char *sudo_uid = getenv("SUDO_UID"); - char *home = getenv("HOME"); - char *gnupghome = getenv("GNUPGHOME"); - config_t cfg; + Bool bval; + int i, val; + const char *str; + struct passwd *pw; + char path[PATH_MAX]; + char *sudo_uid = getenv("SUDO_UID"); + char *home = getenv("HOME"); + char *gnupghome = getenv("GNUPGHOME"); + config_t cfg; - if (gnupghome) { - i = strlen(gnupghome); - strcpy(path, gnupghome); - } else { - /* Get the home dir even if the user used sudo or logged in as root */ - if (sudo_uid) { - i = atoi(sudo_uid); - pw = getpwuid(i); - home = pw->pw_dir; - } + if (gnupghome) { + i = strlen(gnupghome); + strcpy(path, gnupghome); + } else { + /* Get the home dir even if the user used sudo or logged in as root */ + if (sudo_uid) { + i = atoi(sudo_uid); + pw = getpwuid(i); + home = pw->pw_dir; + } - i = strlen(home); - strcpy(path, home); - strcpy(&path[i], CONFIG_DIR); - i += strlen(CONFIG_DIR); - } + i = strlen(home); + strcpy(path, home); + strcpy(&path[i], CONFIG_DIR); + i += strlen(CONFIG_DIR); + } - strcpy(&path[i], CONFIG_FILE); - endpwent(); + strcpy(&path[i], CONFIG_FILE); + endpwent(); - config_init(&cfg); + config_init(&cfg); - /* Read the file. If there is an error, report it and exit. */ - if (config_read_file(&cfg, path)) { - if (config_lookup_int(&cfg, "monitor", &val)) { - mon = val; - } - if (config_lookup_int(&cfg, "center_width", &val)) { - centerwidth = val; - } - if (config_lookup_bool(&cfg, "embedded", &bval)) { - embedded = bval; - } - if (config_lookup_int(&cfg, "line_height", &val)) { - lineheight = val; - } - if (config_lookup_int(&cfg, "border_width", &val)) { - borderwidth = val; - } - if (config_lookup_int(&cfg, "prompt_fg_alpha", &val)) { - alphas[SchemePrompt][0] = val; - } - if (config_lookup_int(&cfg, "prompt_bg_alpha", &val)) { - alphas[SchemePrompt][1] = val; - } - if (config_lookup_int(&cfg, "normal_fg_alpha", &val)) { - alphas[SchemeNormal][0] = val; - } - if (config_lookup_int(&cfg, "normal_bg_alpha", &val)) { - alphas[SchemeNormal][1] = val; - } - if (config_lookup_int(&cfg, "select_fg_alpha", &val)) { - alphas[SchemeSelect][0] = val; - } - if (config_lookup_int(&cfg, "select_bg_alpha", &val)) { - alphas[SchemeSelect][1] = val; - } - if (config_lookup_int(&cfg, "desc_fg_alpha", &val)) { - alphas[SchemeDesc][0] = val; - } - if (config_lookup_int(&cfg, "desc_bg_alpha", &val)) { - alphas[SchemeDesc][1] = val; - } - if (config_lookup_string(&cfg, "position", &str)) { - position = str; - } - if (config_lookup_string(&cfg, "font", &str)) { - fonts[0] = str; - } - if (config_lookup_int(&cfg, "min_password_length", &val)) { - minpwlen = val; - } - if (config_lookup_string(&cfg, "asterisk", &str)) { - asterisk = str; - } - if (config_lookup_string(&cfg, "prompt", &str)) { - prompt = str; - } - if (config_lookup_string(&cfg, "prompt_fg", &str)) { - colors[SchemePrompt][ColFg] = str; - } - if (config_lookup_string(&cfg, "prompt_bg", &str)) { - colors[SchemePrompt][ColBg] = str; - } - if (config_lookup_string(&cfg, "normal_fg", &str)) { - colors[SchemeNormal][ColFg] = str; - } - if (config_lookup_string(&cfg, "normal_bg", &str)) { - colors[SchemeNormal][ColBg] = str; - } - if (config_lookup_string(&cfg, "select_fg", &str)) { - colors[SchemeSelect][ColFg] = str; - } - if (config_lookup_string(&cfg, "select_bg", &str)) { - colors[SchemeSelect][ColBg] = str; - } - if (config_lookup_string(&cfg, "desc_fg", &str)) { - colors[SchemeDesc][ColFg] = str; - } - if (config_lookup_string(&cfg, "desc_bg", &str)) { - colors[SchemeDesc][ColBg] = str; - } - } else if ((str = config_error_file(&cfg))) { - fprintf(stderr, "%s:%d: %s\n", config_error_file(&cfg), - config_error_line(&cfg), config_error_text(&cfg)); - return(EXIT_FAILURE); - } else { - printf("No config file found. Use defaults.\n"); - } + /* Read the file. If there is an error, report it and exit. */ + if (config_read_file(&cfg, path)) { + if (config_lookup_int(&cfg, "monitor", &val)) { + mon = val; + } + if (config_lookup_int(&cfg, "center_width", &val)) { + centerwidth = val; + } + if (config_lookup_bool(&cfg, "embedded", &bval)) { + embedded = bval; + } + if (config_lookup_int(&cfg, "line_height", &val)) { + lineheight = val; + } + if (config_lookup_int(&cfg, "border_width", &val)) { + borderwidth = val; + } + if (config_lookup_int(&cfg, "prompt_fg_alpha", &val)) { + alphas[SchemePrompt][0] = val; + } + if (config_lookup_int(&cfg, "prompt_bg_alpha", &val)) { + alphas[SchemePrompt][1] = val; + } + if (config_lookup_int(&cfg, "normal_fg_alpha", &val)) { + alphas[SchemeNormal][0] = val; + } + if (config_lookup_int(&cfg, "normal_bg_alpha", &val)) { + alphas[SchemeNormal][1] = val; + } + if (config_lookup_int(&cfg, "select_fg_alpha", &val)) { + alphas[SchemeSelect][0] = val; + } + if (config_lookup_int(&cfg, "select_bg_alpha", &val)) { + alphas[SchemeSelect][1] = val; + } + if (config_lookup_int(&cfg, "desc_fg_alpha", &val)) { + alphas[SchemeDesc][0] = val; + } + if (config_lookup_int(&cfg, "desc_bg_alpha", &val)) { + alphas[SchemeDesc][1] = val; + } + if (config_lookup_string(&cfg, "position", &str)) { + position = str; + } + if (config_lookup_string(&cfg, "font", &str)) { + fonts[0] = str; + } + if (config_lookup_int(&cfg, "min_password_length", &val)) { + minpwlen = val; + } + if (config_lookup_string(&cfg, "asterisk", &str)) { + asterisk = str; + } + if (config_lookup_string(&cfg, "prompt", &str)) { + prompt = str; + } + if (config_lookup_string(&cfg, "prompt_fg", &str)) { + colors[SchemePrompt][ColFg] = str; + } + if (config_lookup_string(&cfg, "prompt_bg", &str)) { + colors[SchemePrompt][ColBg] = str; + } + if (config_lookup_string(&cfg, "normal_fg", &str)) { + colors[SchemeNormal][ColFg] = str; + } + if (config_lookup_string(&cfg, "normal_bg", &str)) { + colors[SchemeNormal][ColBg] = str; + } + if (config_lookup_string(&cfg, "select_fg", &str)) { + colors[SchemeSelect][ColFg] = str; + } + if (config_lookup_string(&cfg, "select_bg", &str)) { + colors[SchemeSelect][ColBg] = str; + } + if (config_lookup_string(&cfg, "desc_fg", &str)) { + colors[SchemeDesc][ColFg] = str; + } + if (config_lookup_string(&cfg, "desc_bg", &str)) { + colors[SchemeDesc][ColBg] = str; + } + } else if ((str = config_error_file(&cfg))) { + fprintf(stderr, "%s:%d: %s\n", config_error_file(&cfg), + config_error_line(&cfg), config_error_text(&cfg)); + return(EXIT_FAILURE); + } else { + printf("No config file found. Use defaults.\n"); + } - if (0 == strcmp(position, "center")) { - center = 1; - bottom = 0; - } - if (0 == strcmp(position, "bottom")) { - center = 0; - bottom = 1; - } + if (0 == strcmp(position, "center")) { + center = 1; + bottom = 0; + } + if (0 == strcmp(position, "bottom")) { + center = 0; + bottom = 1; + } - pinentry_init("pinentry-dmenu"); - pinentry_parse_opts(argc, argv); + pinentry_init("pinentry-dmenu"); + pinentry_parse_opts(argc, argv); - if (pinentry_loop()) { - return 1; - } + if (pinentry_loop()) { + return 1; + } - config_destroy(&cfg); + config_destroy(&cfg); - return 0; + return 0; } diff --git a/test.sh b/test.sh index 95b278d..05fed5b 100755 --- a/test.sh +++ b/test.sh @@ -17,20 +17,20 @@ SETREPEAT repeat GETPIN" case $1 in - 1) - printf "%s\nBYE" "$prompt" \ - | ./pinentry-dmenu - ;; - 2) - printf "%s\nBYE" "$confirm" \ - | ./pinentry-dmenu - ;; - 3) - printf "%s\nBYE" "$repeat" \ - | ./pinentry-dmenu - ;; - *) - printf "%s\n%s\n%s\nBYE" "$prompt" "$confirm" "$repeat" \ - | ./pinentry-dmenu - ;; + 1) + printf "%s\nBYE" "$prompt" \ + | ./pinentry-dmenu + ;; + 2) + printf "%s\nBYE" "$confirm" \ + | ./pinentry-dmenu + ;; + 3) + printf "%s\nBYE" "$repeat" \ + | ./pinentry-dmenu + ;; + *) + printf "%s\n%s\n%s\nBYE" "$prompt" "$confirm" "$repeat" \ + | ./pinentry-dmenu + ;; esac diff --git a/util.c b/util.c index fe044fc..ab40cc7 100644 --- a/util.c +++ b/util.c @@ -9,27 +9,27 @@ void * ecalloc(size_t nmemb, size_t size) { - void *p; + void *p; - if (!(p = calloc(nmemb, size))) - die("calloc:"); - return p; + if (!(p = calloc(nmemb, size))) + die("calloc:"); + return p; } void die(const char *fmt, ...) { - va_list ap; + va_list ap; - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); - if (fmt[0] && fmt[strlen(fmt)-1] == ':') { - fputc(' ', stderr); - perror(NULL); - } else { - fputc('\n', stderr); - } + if (fmt[0] && fmt[strlen(fmt)-1] == ':') { + fputc(' ', stderr); + perror(NULL); + } else { + fputc('\n', stderr); + } - exit(1); + exit(1); }