Fix code formation

pull/4/head
Moritz Luedecke 7 years ago
parent c601f2c319
commit 900aa41620

@ -19,8 +19,9 @@
#include "pinentry/memory.h" #include "pinentry/memory.h"
/* macros */ /* macros */
#define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \ #define INTERSECT(x, y, w, h, r) \
* 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 LENGTH(X) (sizeof X / sizeof X[0])
#define TEXTNW(X, N) (drw_font_getexts_width(drw->fonts[0], (X), (N))) #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 TEXTW(X) (drw_text(drw, 0, 0, 0, 0, (X), 0) + drw->fonts[0]->h)
@ -63,11 +64,13 @@ void
grabkeyboard(void) { grabkeyboard(void) {
int i; int i;
/* try to grab keyboard, we may have to wait for another process to ungrab */ /* try to grab keyboard,
* we may have to wait for another process to ungrab */
for (i = 0; i < 1000; i++) { for (i = 0; i < 1000; i++) {
if(XGrabKeyboard(dpy, DefaultRootWindow(dpy), True, if (XGrabKeyboard(dpy, DefaultRootWindow(dpy), True, GrabModeAsync,
GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess) GrabModeAsync, CurrentTime) == GrabSuccess) {
return; return;
}
usleep(1000); usleep(1000);
} }
die("cannot grab keyboard\n"); die("cannot grab keyboard\n");
@ -79,25 +82,35 @@ nextrune(int cursor, int inc) {
/* return location of next utf8 rune in the given direction (+1 or -1) */ /* 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; return n;
} }
void void
insert(const char *str, ssize_t n) { insert(const char *str, ssize_t n) {
if(strlen(text) + n > sizeof text - 1) if (strlen(text) + n > sizeof text - 1) {
return; return;
}
/* move existing text out of the way, insert new text, and update cursor */ /* 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)); memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0));
if(n > 0)
if (n > 0) {
memcpy(&text[cursor], str, n); memcpy(&text[cursor], str, n);
}
cursor += n; cursor += n;
} }
void void
drawwin(void) { drawwin(void) {
int curpos; 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_setscheme(drw, &scheme[SchemeNorm]);
drw_rect(drw, 0, 0, mw, mh, True, 1, 1); drw_rect(drw, 0, 0, mw, mh, True, 1, 1);
@ -116,20 +129,21 @@ drawwin(void){
w = inputw; w = inputw;
drw_setscheme(drw, &scheme[SchemeNorm]); drw_setscheme(drw, &scheme[SchemeNorm]);
char *sectext = malloc (sizeof (char) * 2048); sectext = malloc (sizeof (char) * 2048);
sectext[0] = '\0'; sectext[0] = '\0';
int i;
int seccursor=0;
for (i=0; text[i]!='\0'; i=nextrune(i, +1)) { for (i=0; text[i]!='\0'; i=nextrune(i, +1)) {
strcat(sectext, secchar); strcat(sectext, secstring);
if (i < cursor) { if (i < cursor) {
ssize_t n; for (n = seccursor + 1; n + 1 >= 0 && (sectext[n] & 0xc0) == 0x80;
for(n = seccursor + 1; n + 1 >= 0 && (sectext[n] & 0xc0) == 0x80; n ++); n ++);
seccursor = n; seccursor = n;
} }
} }
if (winmode == WinPin) { if (winmode == WinPin) {
drw_text(drw, x, y, mw, bh, sectext, 0); drw_text(drw, x, y, mw, bh, sectext, 0);
if ((curpos = TEXTNW(sectext, seccursor) + bh/2 - 2) < w) { if ((curpos = TEXTNW(sectext, seccursor) + bh/2 - 2) < w) {
drw_rect(drw, x + curpos + 2, y + 2, 1 , bh - 4 , True, 1, 0); drw_rect(drw, x + curpos + 2, y + 2, 1 , bh - 4 , True, 1, 0);
} }
@ -188,16 +202,19 @@ cleanup(void) {
XCloseDisplay(dpy); XCloseDisplay(dpy);
} }
static int static int
keypress(XKeyEvent *ev) { keypress(XKeyEvent *ev) {
char buf[32]; char buf[32];
int len; int len;
KeySym ksym = NoSymbol; KeySym ksym = NoSymbol;
Status status; Status status;
len = XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status); len = XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status);
if (status == XBufferOverflow)
if (status == XBufferOverflow) {
return 0; return 0;
}
if (winmode == WinConfirm) { if (winmode == WinConfirm) {
switch(ksym){ switch(ksym){
case XK_KP_Enter: case XK_KP_Enter:
@ -219,17 +236,20 @@ keypress(XKeyEvent *ev) {
} else { } else {
switch(ksym){ switch(ksym){
default: default:
if (!iscntrl(*buf)) if (!iscntrl(*buf)) {
insert(buf, len); insert(buf, len);
}
break; break;
case XK_Delete: case XK_Delete:
if(text[cursor] == '\0') if (text[cursor] == '\0') {
return 0; return 0;
}
cursor = nextrune(cursor, +1); cursor = nextrune(cursor, +1);
/* fallthrough */ /* fallthrough */
case XK_BackSpace: case XK_BackSpace:
if(cursor == 0) if (cursor == 0) {
return 0; return 0;
}
insert(NULL, nextrune(cursor, -1) - cursor); insert(NULL, nextrune(cursor, -1) - cursor);
break; break;
case XK_Escape: case XK_Escape:
@ -277,23 +297,29 @@ void
run(void) { run(void) {
XEvent ev; XEvent ev;
while(!XNextEvent(dpy, &ev)) { while(!XNextEvent(dpy, &ev)) {
if(XFilterEvent(&ev, win)) if (XFilterEvent(&ev, win)) {
continue; /* what is this I don't even */ continue; /* what is this I don't even */
}
switch(ev.type) { switch(ev.type) {
case Expose: case Expose:
if(ev.xexpose.count == 0) if (ev.xexpose.count == 0) {
drw_map(drw, win, 0, 0, mw, mh); drw_map(drw, win, 0, 0, mw, mh);
}
break; break;
case KeyPress: case KeyPress:
if (keypress(&ev.xkey)) return; if (keypress(&ev.xkey)) {
return;
}
break; break;
case SelectionNotify: case SelectionNotify:
if(ev.xselection.property == utf8) if (ev.xselection.property == utf8) {
paste(); paste();
}
break; break;
case VisibilityNotify: case VisibilityNotify:
if(ev.xvisibility.state != VisibilityUnobscured) if (ev.xvisibility.state != VisibilityUnobscured) {
XRaiseWindow(dpy, win); XRaiseWindow(dpy, win);
}
break; break;
} }
} }
@ -308,20 +334,27 @@ promptwin(void) {
} }
static void static void
catchsig(int sig) catchsig(int sig) {
{ if (sig == SIGALRM) {
if (sig == SIGALRM)
timed_out = 1; timed_out = 1;
} }
}
static int static int
password (void) { password (void) {
char *buf;
winmode = WinPin; winmode = WinPin;
promptwin(); 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); strcpy(buf, text);
pinentry_setbuffer_use (pinentry, buf, 0); pinentry_setbuffer_use (pinentry, buf, 0);
return 1; return 1;
} }
@ -330,50 +363,61 @@ confirm(void) {
winmode = WinConfirm; winmode = WinConfirm;
confirmed = 0; confirmed = 0;
promptwin(); promptwin();
return confirmed; return confirmed;
} }
static int static int
spinecmdhandler (pinentry_t recieved_pinentry) { cmdhandler (pinentry_t recieved_pinentry) {
struct sigaction sa;
text[0]='\0'; text[0]='\0';
cursor = 0; cursor = 0;
pinentry = recieved_pinentry; pinentry = recieved_pinentry;
if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) {
fputs("warning: no locale support\n", stderr); 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"); die("dmenu: cannot open display\n");
}
screen = DefaultScreen(dpy); screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen); root = RootWindow(dpy, screen);
sw = DisplayWidth(dpy, screen); sw = DisplayWidth(dpy, screen);
sh = DisplayHeight(dpy, screen); sh = DisplayHeight(dpy, screen);
drw = drw_create(dpy, screen, root, sw, sh); drw = drw_create(dpy, screen, root, sw, sh);
drw_load_fonts(drw, fonts, LENGTH(fonts)); drw_load_fonts(drw, fonts, LENGTH(fonts));
if(!drw->fontcount) if (!drw->fontcount) {
die("No fonts could be loaded.\n"); die("No fonts could be loaded.\n");
}
drw_setscheme(drw, &scheme[SchemeNorm]); drw_setscheme(drw, &scheme[SchemeNorm]);
if (pinentry->timeout) { if (pinentry->timeout) {
struct sigaction sa;
memset(&sa, 0, sizeof(sa)); memset(&sa, 0, sizeof(sa));
sa.sa_handler = catchsig; sa.sa_handler = catchsig;
sigaction(SIGALRM, &sa, NULL); sigaction(SIGALRM, &sa, NULL);
alarm(pinentry->timeout); alarm(pinentry->timeout);
} }
if (pinentry->pin)
if (pinentry->pin) {
return password(); return password();
else } else {
return confirm(); return confirm();
}
return -1; return -1;
} }
pinentry_cmd_handler_t pinentry_cmd_handler = spinecmdhandler; pinentry_cmd_handler_t pinentry_cmd_handler = cmdhandler;
int int
main(int argc, char *argv[]) { main(int argc, char *argv[]) {
pinentry_init("spine"); pinentry_init("pinentry-dmenu");
pinentry_parse_opts(argc, argv); pinentry_parse_opts(argc, argv);
if (pinentry_loop())
if (pinentry_loop()) {
return 1; return 1;
}
return 0; return 0;
} }
Loading…
Cancel
Save