Fix code formation
This commit is contained in:
parent
c601f2c319
commit
900aa41620
2 changed files with 104 additions and 60 deletions
|
@ -19,11 +19,12 @@
|
||||||
#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)) \
|
||||||
#define LENGTH(X) (sizeof X / sizeof X[0])
|
&& MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org)))
|
||||||
#define TEXTNW(X,N) (drw_font_getexts_width(drw->fonts[0], (X), (N)))
|
#define LENGTH(X) (sizeof X / sizeof X[0])
|
||||||
#define TEXTW(X) (drw_text(drw, 0, 0, 0, 0, (X), 0) + drw->fonts[0]->h)
|
#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_OK = "OK\n";
|
||||||
const char *str_ERRUNPARS = "ERR1337 dunno what to do with it\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 */
|
/* enums */
|
||||||
enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
|
enum { SchemeNorm, SchemeSel, 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 */
|
||||||
|
|
||||||
static char text[2048] = "";
|
static char text[2048] = "";
|
||||||
static int bh, mw, mh;
|
static int bh, mw, mh;
|
||||||
|
@ -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,
|
||||||
for(i = 0; i < 1000; i++) {
|
* we may have to wait for another process to ungrab */
|
||||||
if(XGrabKeyboard(dpy, DefaultRootWindow(dpy), True,
|
for (i = 0; i < 1000; i++) {
|
||||||
GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
|
if (XGrabKeyboard(dpy, DefaultRootWindow(dpy), True, GrabModeAsync,
|
||||||
|
GrabModeAsync, CurrentTime) == GrabSuccess) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
usleep(1000);
|
usleep(1000);
|
||||||
}
|
}
|
||||||
die("cannot grab keyboard\n");
|
die("cannot grab keyboard\n");
|
||||||
|
@ -78,33 +81,43 @@ nextrune(int cursor, int inc) {
|
||||||
ssize_t n;
|
ssize_t n;
|
||||||
|
|
||||||
/* 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);
|
||||||
|
|
||||||
if ((pinentry->description) && *(pinentry->description)) {
|
if ((pinentry->description) && *(pinentry->description)) {
|
||||||
drw_setscheme(drw, &scheme[SchemeSel]);
|
drw_setscheme(drw, &scheme[SchemeSel]);
|
||||||
drw_text(drw, 0,0,mw,bh,pinentry->description,0);
|
drw_text(drw, 0, 0, mw, bh, pinentry->description, 0);
|
||||||
y+=bh;
|
y += bh;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((pinentry->prompt) && *(pinentry->prompt)) {
|
if ((pinentry->prompt) && *(pinentry->prompt)) {
|
||||||
|
@ -116,22 +129,23 @@ 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, secstring);
|
||||||
strcat(sectext, secchar);
|
if (i < cursor) {
|
||||||
if (i<cursor){
|
for (n = seccursor + 1; n + 1 >= 0 && (sectext[n] & 0xc0) == 0x80;
|
||||||
ssize_t n;
|
n ++);
|
||||||
for(n = seccursor + 1; n + 1 >= 0 && (sectext[n] & 0xc0) == 0x80; 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) {
|
|
||||||
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 {
|
} else {
|
||||||
drw_text(drw, x, y, mw, bh, "(y/n)", 0);
|
drw_text(drw, x, y, mw, bh, "(y/n)", 0);
|
||||||
|
@ -188,17 +202,20 @@ 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:
|
||||||
case XK_Return:
|
case XK_Return:
|
||||||
|
@ -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:
|
||||||
|
@ -239,12 +259,12 @@ keypress(XKeyEvent *ev) {
|
||||||
exit(1);*/
|
exit(1);*/
|
||||||
break;
|
break;
|
||||||
case XK_Left:
|
case XK_Left:
|
||||||
if(cursor > 0) {
|
if (cursor > 0) {
|
||||||
cursor = nextrune(cursor, -1);
|
cursor = nextrune(cursor, -1);
|
||||||
}
|
}
|
||||||
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;
|
||||||
|
@ -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…
Reference in a new issue