You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
424 lines
8.7 KiB
424 lines
8.7 KiB
9 years ago
|
#include <signal.h>
|
||
|
#include <time.h>
|
||
|
#include <ctype.h>
|
||
|
#include <locale.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <strings.h>
|
||
|
#include <string.h>
|
||
|
#include <unistd.h>
|
||
|
#include <X11/Xlib.h>
|
||
|
#include <X11/Xatom.h>
|
||
|
#include <X11/Xutil.h>
|
||
|
#include <X11/Xft/Xft.h>
|
||
|
|
||
|
#include "drw.h"
|
||
|
#include "util.h"
|
||
|
|
||
|
#include "pinentry/pinentry.h"
|
||
|
#include "pinentry/memory.h"
|
||
|
|
||
|
/* macros */
|
||
7 years ago
|
#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)
|
||
9 years ago
|
|
||
|
const char *str_OK = "OK\n";
|
||
|
const char *str_ERRUNPARS = "ERR1337 dunno what to do with it\n";
|
||
|
const char *str_ERRNOTIMP = "ERR4100 not implemented yet\n";
|
||
|
|
||
|
/* enums */
|
||
|
enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
|
||
7 years ago
|
enum { WinPin, WinConfirm }; /* window modes */
|
||
|
enum { Ok, NotOk, Cancel }; /* return status */
|
||
9 years ago
|
|
||
|
static char text[2048] = "";
|
||
|
static int bh, mw, mh;
|
||
|
static int inputw, promptw;
|
||
|
static size_t cursor = 0;
|
||
|
static Atom clip, utf8;
|
||
|
static Window win;
|
||
|
static XIC xic;
|
||
|
static int mon = -1;
|
||
|
|
||
|
static ClrScheme scheme[SchemeLast];
|
||
|
static Display *dpy;
|
||
|
static int screen;
|
||
|
static Window root;
|
||
|
static Drw *drw;
|
||
|
static int sw, sh;
|
||
|
|
||
|
static int timed_out;
|
||
|
|
||
9 years ago
|
static int confirmed;
|
||
|
static int winmode;
|
||
|
|
||
9 years ago
|
pinentry_t pinentry;
|
||
|
|
||
9 years ago
|
#include "config.h"
|
||
|
|
||
|
void
|
||
|
grabkeyboard(void) {
|
||
|
int i;
|
||
|
|
||
7 years ago
|
/* 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) {
|
||
9 years ago
|
return;
|
||
7 years ago
|
}
|
||
9 years ago
|
usleep(1000);
|
||
|
}
|
||
|
die("cannot grab keyboard\n");
|
||
|
}
|
||
|
|
||
|
size_t
|
||
|
nextrune(int cursor, int inc) {
|
||
|
ssize_t n;
|
||
|
|
||
|
/* return location of next utf8 rune in the given direction (+1 or -1) */
|
||
7 years ago
|
for (n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc);
|
||
|
|
||
9 years ago
|
return n;
|
||
|
}
|
||
|
|
||
|
|
||
|
void
|
||
|
insert(const char *str, ssize_t n) {
|
||
7 years ago
|
if (strlen(text) + n > sizeof text - 1) {
|
||
9 years ago
|
return;
|
||
7 years ago
|
}
|
||
|
|
||
9 years ago
|
/* 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));
|
||
7 years ago
|
|
||
|
if (n > 0) {
|
||
9 years ago
|
memcpy(&text[cursor], str, n);
|
||
7 years ago
|
}
|
||
9 years ago
|
cursor += n;
|
||
|
}
|
||
|
|
||
|
void
|
||
7 years ago
|
drawwin(void) {
|
||
9 years ago
|
int curpos;
|
||
7 years ago
|
int x = 0, y = 0, bh, w;
|
||
|
char *sectext;
|
||
|
int i;
|
||
|
int seccursor = 0;
|
||
|
ssize_t n;
|
||
|
|
||
9 years ago
|
drw_setscheme(drw, &scheme[SchemeNorm]);
|
||
7 years ago
|
drw_rect(drw, 0, 0, mw, mh, True, 1, 1);
|
||
9 years ago
|
|
||
9 years ago
|
if ((pinentry->description) && *(pinentry->description)) {
|
||
9 years ago
|
drw_setscheme(drw, &scheme[SchemeSel]);
|
||
7 years ago
|
drw_text(drw, 0, 0, mw, bh, pinentry->description, 0);
|
||
|
y += bh;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if ((pinentry->prompt) && *(pinentry->prompt)) {
|
||
9 years ago
|
drw_setscheme(drw, &scheme[SchemeSel]);
|
||
9 years ago
|
drw_text(drw, x, y, promptw, bh, pinentry->prompt, 0);
|
||
9 years ago
|
x += promptw;
|
||
|
}
|
||
|
|
||
|
w = inputw;
|
||
|
drw_setscheme(drw, &scheme[SchemeNorm]);
|
||
|
|
||
7 years ago
|
sectext = malloc (sizeof (char) * 2048);
|
||
9 years ago
|
sectext[0] = '\0';
|
||
7 years ago
|
|
||
|
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 ++);
|
||
9 years ago
|
seccursor = n;
|
||
|
}
|
||
|
}
|
||
7 years ago
|
|
||
|
if (winmode == WinPin) {
|
||
9 years ago
|
drw_text(drw, x, y, mw, bh, sectext, 0);
|
||
7 years ago
|
|
||
|
if ((curpos = TEXTNW(sectext, seccursor) + bh/2 - 2) < w) {
|
||
|
drw_rect(drw, x + curpos + 2, y + 2, 1 , bh - 4 , True, 1, 0);
|
||
9 years ago
|
}
|
||
|
} else {
|
||
|
drw_text(drw, x, y, mw, bh, "(y/n)", 0);
|
||
9 years ago
|
}
|
||
|
|
||
|
drw_map(drw, win, 0, 0, mw, mh);
|
||
|
}
|
||
|
|
||
|
void
|
||
|
setup(void){
|
||
|
int x,y;
|
||
|
XSetWindowAttributes swa;
|
||
|
XIM xim;
|
||
|
scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
|
||
|
scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
|
||
|
scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
|
||
|
scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
|
||
|
clip = XInternAtom(dpy, "CLIPBOARD", False);
|
||
|
utf8 = XInternAtom(dpy, "UTF8_STRING", False);
|
||
|
bh = drw->fonts[0]->h + 2;
|
||
9 years ago
|
mh = (pinentry->description && *(pinentry->description))? bh * 2 : bh;
|
||
9 years ago
|
x = 0;
|
||
|
y = topbar ? 0 : sh - mh;
|
||
|
mw = sw;
|
||
9 years ago
|
promptw = (pinentry->prompt && *(pinentry->prompt)) ? TEXTW(pinentry->prompt) : 0;
|
||
9 years ago
|
inputw = mw-promptw;
|
||
|
swa.override_redirect = True;
|
||
|
swa.background_pixel = scheme[SchemeNorm].bg->pix;
|
||
|
swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
|
||
|
win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
|
||
|
DefaultDepth(dpy, screen), CopyFromParent,
|
||
|
DefaultVisual(dpy, screen),
|
||
|
CWOverrideRedirect | CWBackPixel | CWEventMask, &swa);
|
||
|
|
||
|
xim = XOpenIM(dpy, NULL, NULL, NULL);
|
||
|
xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
|
||
|
XNClientWindow, win, XNFocusWindow, win, NULL);
|
||
|
|
||
|
XMapRaised(dpy, win);
|
||
|
drw_resize(drw, mw, mh);
|
||
|
|
||
|
drawwin();
|
||
|
}
|
||
|
|
||
|
void
|
||
|
cleanup(void) {
|
||
|
XUngrabKey(dpy, AnyKey, AnyModifier, root);
|
||
|
drw_clr_free(scheme[SchemeNorm].bg);
|
||
|
drw_clr_free(scheme[SchemeNorm].fg);
|
||
|
drw_clr_free(scheme[SchemeSel].fg);
|
||
|
drw_clr_free(scheme[SchemeSel].bg);
|
||
|
drw_free(drw);
|
||
|
XSync(dpy, False);
|
||
|
XCloseDisplay(dpy);
|
||
|
}
|
||
|
|
||
9 years ago
|
static int
|
||
9 years ago
|
keypress(XKeyEvent *ev) {
|
||
|
char buf[32];
|
||
|
int len;
|
||
7 years ago
|
|
||
9 years ago
|
KeySym ksym = NoSymbol;
|
||
|
Status status;
|
||
|
len = XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status);
|
||
7 years ago
|
|
||
|
if (status == XBufferOverflow) {
|
||
9 years ago
|
return 0;
|
||
7 years ago
|
}
|
||
|
|
||
|
if (winmode == WinConfirm) {
|
||
9 years ago
|
switch(ksym){
|
||
|
case XK_KP_Enter:
|
||
|
case XK_Return:
|
||
|
case XK_y:
|
||
|
confirmed = 1;
|
||
|
return 1;
|
||
|
break;
|
||
|
case XK_n:
|
||
|
confirmed = 0;
|
||
|
return 1;
|
||
|
break;
|
||
|
case XK_Escape:
|
||
|
pinentry->canceled = 1;
|
||
|
confirmed = 0;
|
||
|
return 1;
|
||
|
break;
|
||
9 years ago
|
}
|
||
9 years ago
|
} else {
|
||
|
switch(ksym){
|
||
|
default:
|
||
7 years ago
|
if (!iscntrl(*buf)) {
|
||
9 years ago
|
insert(buf, len);
|
||
7 years ago
|
}
|
||
9 years ago
|
break;
|
||
|
case XK_Delete:
|
||
7 years ago
|
if (text[cursor] == '\0') {
|
||
9 years ago
|
return 0;
|
||
7 years ago
|
}
|
||
9 years ago
|
cursor = nextrune(cursor, +1);
|
||
9 years ago
|
/* fallthrough */
|
||
|
case XK_BackSpace:
|
||
7 years ago
|
if (cursor == 0) {
|
||
9 years ago
|
return 0;
|
||
7 years ago
|
}
|
||
9 years ago
|
insert(NULL, nextrune(cursor, -1) - cursor);
|
||
|
break;
|
||
|
case XK_Escape:
|
||
|
pinentry->canceled = 1;
|
||
|
return 1;
|
||
|
/*cleanup();
|
||
|
exit(1);*/
|
||
|
break;
|
||
|
case XK_Left:
|
||
7 years ago
|
if (cursor > 0) {
|
||
9 years ago
|
cursor = nextrune(cursor, -1);
|
||
|
}
|
||
|
break;
|
||
|
case XK_Right:
|
||
7 years ago
|
if (text[cursor]!='\0') {
|
||
9 years ago
|
cursor = nextrune(cursor, +1);
|
||
|
}
|
||
|
break;
|
||
|
case XK_Return:
|
||
|
case XK_KP_Enter:
|
||
|
return 1;
|
||
|
break;
|
||
9 years ago
|
}
|
||
|
}
|
||
|
drawwin();
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
paste(void) {
|
||
|
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, (sizeof text / 4) + 1, 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;
|
||
|
while(!XNextEvent(dpy, &ev)) {
|
||
7 years ago
|
if (XFilterEvent(&ev, win)) {
|
||
|
continue; /* what is this I don't even */
|
||
|
}
|
||
9 years ago
|
switch(ev.type) {
|
||
|
case Expose:
|
||
7 years ago
|
if (ev.xexpose.count == 0) {
|
||
9 years ago
|
drw_map(drw, win, 0, 0, mw, mh);
|
||
7 years ago
|
}
|
||
9 years ago
|
break;
|
||
|
case KeyPress:
|
||
7 years ago
|
if (keypress(&ev.xkey)) {
|
||
|
return;
|
||
|
}
|
||
9 years ago
|
break;
|
||
|
case SelectionNotify:
|
||
7 years ago
|
if (ev.xselection.property == utf8) {
|
||
9 years ago
|
paste();
|
||
7 years ago
|
}
|
||
9 years ago
|
break;
|
||
|
case VisibilityNotify:
|
||
7 years ago
|
if (ev.xvisibility.state != VisibilityUnobscured) {
|
||
9 years ago
|
XRaiseWindow(dpy, win);
|
||
7 years ago
|
}
|
||
9 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void
|
||
9 years ago
|
promptwin(void) {
|
||
9 years ago
|
grabkeyboard();
|
||
|
setup();
|
||
|
run();
|
||
|
cleanup();
|
||
|
}
|
||
|
|
||
|
static void
|
||
7 years ago
|
catchsig(int sig) {
|
||
|
if (sig == SIGALRM) {
|
||
9 years ago
|
timed_out = 1;
|
||
7 years ago
|
}
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
static int
|
||
9 years ago
|
password (void) {
|
||
7 years ago
|
char *buf;
|
||
|
|
||
9 years ago
|
winmode = WinPin;
|
||
9 years ago
|
promptwin();
|
||
7 years ago
|
|
||
|
if (pinentry->canceled) {
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
buf = secmem_malloc(strlen(text));
|
||
9 years ago
|
strcpy(buf, text);
|
||
|
pinentry_setbuffer_use (pinentry, buf, 0);
|
||
7 years ago
|
|
||
9 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
9 years ago
|
static int
|
||
9 years ago
|
confirm(void) {
|
||
9 years ago
|
winmode = WinConfirm;
|
||
|
confirmed = 0;
|
||
|
promptwin();
|
||
7 years ago
|
|
||
9 years ago
|
return confirmed;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
static int
|
||
7 years ago
|
cmdhandler (pinentry_t recieved_pinentry) {
|
||
|
struct sigaction sa;
|
||
|
|
||
9 years ago
|
text[0]='\0';
|
||
9 years ago
|
cursor = 0;
|
||
9 years ago
|
pinentry = recieved_pinentry;
|
||
9 years ago
|
|
||
7 years ago
|
if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) {
|
||
9 years ago
|
fputs("warning: no locale support\n", stderr);
|
||
7 years ago
|
}
|
||
|
if (!(dpy = XOpenDisplay(pinentry->display))) { /* NULL was here */
|
||
9 years ago
|
die("dmenu: cannot open display\n");
|
||
7 years ago
|
}
|
||
9 years ago
|
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));
|
||
7 years ago
|
if (!drw->fontcount) {
|
||
9 years ago
|
die("No fonts could be loaded.\n");
|
||
7 years ago
|
}
|
||
9 years ago
|
drw_setscheme(drw, &scheme[SchemeNorm]);
|
||
|
|
||
7 years ago
|
if (pinentry->timeout) {
|
||
9 years ago
|
memset(&sa, 0, sizeof(sa));
|
||
|
sa.sa_handler = catchsig;
|
||
|
sigaction(SIGALRM, &sa, NULL);
|
||
|
alarm(pinentry->timeout);
|
||
|
}
|
||
7 years ago
|
|
||
|
if (pinentry->pin) {
|
||
9 years ago
|
return password();
|
||
7 years ago
|
} else {
|
||
9 years ago
|
return confirm();
|
||
7 years ago
|
}
|
||
|
|
||
9 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
7 years ago
|
pinentry_cmd_handler_t pinentry_cmd_handler = cmdhandler;
|
||
9 years ago
|
|
||
|
int
|
||
9 years ago
|
main(int argc, char *argv[]) {
|
||
7 years ago
|
pinentry_init("pinentry-dmenu");
|
||
9 years ago
|
pinentry_parse_opts(argc, argv);
|
||
7 years ago
|
|
||
|
if (pinentry_loop()) {
|
||
9 years ago
|
return 1;
|
||
7 years ago
|
}
|
||
|
|
||
9 years ago
|
return 0;
|
||
9 years ago
|
}
|