summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pinentry-dmenu.c (renamed from spine.c)164
-rwxr-xr-xtest (renamed from spinetest)0
2 files changed, 104 insertions, 60 deletions
diff --git a/spine.c b/pinentry-dmenu.c
index e1a9f8d..b14ae99 100644
--- a/spine.c
+++ b/pinentry-dmenu.c
@@ -19,11 +19,12 @@
#include "pinentry/memory.h"
/* macros */
-#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)))
-#define LENGTH(X) (sizeof X / sizeof X[0])
-#define TEXTNW(X,N) (drw_font_getexts_width(drw->fonts[0], (X), (N)))
-#define TEXTW(X) (drw_text(drw, 0, 0, 0, 0, (X), 0) + drw->fonts[0]->h)
+#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)))
+#define LENGTH(X) (sizeof X / sizeof X[0])
+#define TEXTNW(X, N) (drw_font_getexts_width(drw->fonts[0], (X), (N)))
+#define TEXTW(X) (drw_text(drw, 0, 0, 0, 0, (X), 0) + drw->fonts[0]->h)
const char *str_OK = "OK\n";
const char *str_ERRUNPARS = "ERR1337 dunno what to do with it\n";
@@ -31,8 +32,8 @@ const char *str_ERRNOTIMP = "ERR4100 not implemented yet\n";
/* enums */
enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
-enum {WinPin, WinConfirm}; /* window modes */
-enum {Ok, NotOk, Cancel}; /* return status */
+enum { WinPin, WinConfirm }; /* window modes */
+enum { Ok, NotOk, Cancel }; /* return status */
static char text[2048] = "";
static int bh, mw, mh;
@@ -63,11 +64,13 @@ void
grabkeyboard(void) {
int i;
- /* 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)
+ /* 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\n");
@@ -78,33 +81,43 @@ nextrune(int cursor, int inc) {
ssize_t n;
/* return location of next utf8 rune in the given direction (+1 or -1) */
- for(n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc);
+ for (n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc);
+
return n;
}
void
insert(const char *str, ssize_t n) {
- if(strlen(text) + n > sizeof text - 1)
+ if (strlen(text) + n > sizeof text - 1) {
return;
+ }
+
/* move existing text out of the way, insert new text, and update cursor */
memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0));
- if(n > 0)
+
+ if (n > 0) {
memcpy(&text[cursor], str, n);
+ }
cursor += n;
}
void
-drawwin(void){
+drawwin(void) {
int curpos;
- int x=0, y=0, h=bh, w;
+ int x = 0, y = 0, bh, w;
+ char *sectext;
+ int i;
+ int seccursor = 0;
+ ssize_t n;
+
drw_setscheme(drw, &scheme[SchemeNorm]);
- drw_rect(drw, 0,0,mw,mh,True,1,1);
+ drw_rect(drw, 0, 0, mw, mh, True, 1, 1);
if ((pinentry->description) && *(pinentry->description)) {
drw_setscheme(drw, &scheme[SchemeSel]);
- drw_text(drw, 0,0,mw,bh,pinentry->description,0);
- y+=bh;
+ drw_text(drw, 0, 0, mw, bh, pinentry->description, 0);
+ y += bh;
}
if ((pinentry->prompt) && *(pinentry->prompt)) {
@@ -116,22 +129,23 @@ drawwin(void){
w = inputw;
drw_setscheme(drw, &scheme[SchemeNorm]);
- char *sectext = malloc (sizeof (char) * 2048);
+ sectext = malloc (sizeof (char) * 2048);
sectext[0] = '\0';
- int i;
- int seccursor=0;
- for (i=0; text[i]!='\0'; i=nextrune(i, +1)){
- strcat(sectext, secchar);
- if (i<cursor){
- ssize_t n;
- for(n = seccursor + 1; n + 1 >= 0 && (sectext[n] & 0xc0) == 0x80; n ++);
+
+ for (i=0; text[i]!='\0'; i=nextrune(i, +1)) {
+ strcat(sectext, secstring);
+ if (i < cursor) {
+ for (n = seccursor + 1; n + 1 >= 0 && (sectext[n] & 0xc0) == 0x80;
+ n ++);
seccursor = n;
}
}
- if (winmode==WinPin) {
+
+ if (winmode == WinPin) {
drw_text(drw, x, y, mw, bh, sectext, 0);
- if((curpos = TEXTNW(sectext, seccursor) + bh/2 - 2) < w) {
- drw_rect(drw, x + curpos + 2, y + 2, 1 , bh-4 , True, 1, 0);
+
+ if ((curpos = TEXTNW(sectext, seccursor) + bh/2 - 2) < w) {
+ drw_rect(drw, x + curpos + 2, y + 2, 1 , bh - 4 , True, 1, 0);
}
} else {
drw_text(drw, x, y, mw, bh, "(y/n)", 0);
@@ -188,17 +202,20 @@ cleanup(void) {
XCloseDisplay(dpy);
}
-
static int
keypress(XKeyEvent *ev) {
char buf[32];
int len;
+
KeySym ksym = NoSymbol;
Status status;
len = XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status);
- if (status == XBufferOverflow)
+
+ if (status == XBufferOverflow) {
return 0;
- if (winmode == WinConfirm){
+ }
+
+ if (winmode == WinConfirm) {
switch(ksym){
case XK_KP_Enter:
case XK_Return:
@@ -219,17 +236,20 @@ keypress(XKeyEvent *ev) {
} else {
switch(ksym){
default:
- if (!iscntrl(*buf))
+ if (!iscntrl(*buf)) {
insert(buf, len);
+ }
break;
case XK_Delete:
- if(text[cursor] == '\0')
+ if (text[cursor] == '\0') {
return 0;
+ }
cursor = nextrune(cursor, +1);
/* fallthrough */
case XK_BackSpace:
- if(cursor == 0)
+ if (cursor == 0) {
return 0;
+ }
insert(NULL, nextrune(cursor, -1) - cursor);
break;
case XK_Escape:
@@ -239,12 +259,12 @@ keypress(XKeyEvent *ev) {
exit(1);*/
break;
case XK_Left:
- if(cursor > 0) {
+ if (cursor > 0) {
cursor = nextrune(cursor, -1);
}
break;
case XK_Right:
- if(text[cursor]!='\0') {
+ if (text[cursor]!='\0') {
cursor = nextrune(cursor, +1);
}
break;
@@ -277,23 +297,29 @@ void
run(void) {
XEvent ev;
while(!XNextEvent(dpy, &ev)) {
- if(XFilterEvent(&ev, win))
- continue; /*what is this I don't even*/
+ if (XFilterEvent(&ev, win)) {
+ continue; /* what is this I don't even */
+ }
switch(ev.type) {
case Expose:
- if(ev.xexpose.count == 0)
+ if (ev.xexpose.count == 0) {
drw_map(drw, win, 0, 0, mw, mh);
+ }
break;
case KeyPress:
- if (keypress(&ev.xkey)) return;
+ if (keypress(&ev.xkey)) {
+ return;
+ }
break;
case SelectionNotify:
- if(ev.xselection.property == utf8)
+ if (ev.xselection.property == utf8) {
paste();
+ }
break;
case VisibilityNotify:
- if(ev.xvisibility.state != VisibilityUnobscured)
+ if (ev.xvisibility.state != VisibilityUnobscured) {
XRaiseWindow(dpy, win);
+ }
break;
}
}
@@ -308,20 +334,27 @@ promptwin(void) {
}
static void
-catchsig(int sig)
-{
- if (sig == SIGALRM)
+catchsig(int sig) {
+ if (sig == SIGALRM) {
timed_out = 1;
+ }
}
static int
password (void) {
+ char *buf;
+
winmode = WinPin;
promptwin();
- if (pinentry->canceled) return -1;
- char *buf = secmem_malloc(strlen(text));
+
+ if (pinentry->canceled) {
+ return -1;
+ }
+
+ buf = secmem_malloc(strlen(text));
strcpy(buf, text);
pinentry_setbuffer_use (pinentry, buf, 0);
+
return 1;
}
@@ -330,50 +363,61 @@ confirm(void) {
winmode = WinConfirm;
confirmed = 0;
promptwin();
+
return confirmed;
}
static int
-spinecmdhandler (pinentry_t recieved_pinentry) {
+cmdhandler (pinentry_t recieved_pinentry) {
+ struct sigaction sa;
+
text[0]='\0';
cursor = 0;
pinentry = recieved_pinentry;
- if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
+ if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) {
fputs("warning: no locale support\n", stderr);
- if(!(dpy = XOpenDisplay(pinentry->display))) /*NULL was here*/
+ }
+ if (!(dpy = XOpenDisplay(pinentry->display))) { /* NULL was here */
die("dmenu: cannot open display\n");
+ }
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
sw = DisplayWidth(dpy, screen);
sh = DisplayHeight(dpy, screen);
drw = drw_create(dpy, screen, root, sw, sh);
drw_load_fonts(drw, fonts, LENGTH(fonts));
- if(!drw->fontcount)
+ if (!drw->fontcount) {
die("No fonts could be loaded.\n");
+ }
drw_setscheme(drw, &scheme[SchemeNorm]);
- if (pinentry->timeout){
- struct sigaction sa;
+ if (pinentry->timeout) {
memset(&sa, 0, sizeof(sa));
sa.sa_handler = catchsig;
sigaction(SIGALRM, &sa, NULL);
alarm(pinentry->timeout);
}
- if (pinentry->pin)
+
+ if (pinentry->pin) {
return password();
- else
+ } else {
return confirm();
+ }
+
return -1;
}
-pinentry_cmd_handler_t pinentry_cmd_handler = spinecmdhandler;
+pinentry_cmd_handler_t pinentry_cmd_handler = cmdhandler;
int
main(int argc, char *argv[]) {
- pinentry_init("spine");
+ pinentry_init("pinentry-dmenu");
pinentry_parse_opts(argc, argv);
- if (pinentry_loop())
+
+ if (pinentry_loop()) {
return 1;
+ }
+
return 0;
}
diff --git a/spinetest b/test
index 35d32bc..35d32bc 100755
--- a/spinetest
+++ b/test