X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=dinput.c;h=0d0adebc3ab9348ecb504a648fe977a33a3746da;hb=29e8faed6cf4b296382439651cf04596d276f080;hp=b789bd762eee05b2214ef8947794841fac1dc78f;hpb=6ba5103df4552a330ba29fdac88ae3a2897b94cf;p=dmenu.git diff --git a/dinput.c b/dinput.c index b789bd7..0d0adeb 100644 --- a/dinput.c +++ b/dinput.c @@ -21,12 +21,11 @@ /* forward declarations */ static void cleanup(void); -static void drawcursor(void); static void drawinput(void); -static Bool grabkeyboard(void); +static void grabkeyboard(void); static void kpress(XKeyEvent *e); static void run(void); -static void setup(Bool topbar); +static void setup(void); #include "config.h" @@ -34,14 +33,13 @@ static void setup(Bool topbar); static char *prompt = NULL; static char text[4096]; static int promptw = 0; -static int ret = 0; static int screen; -static unsigned int cursor = 0; +static size_t cursor = 0; static unsigned int numlockmask = 0; static unsigned int mw, mh; static unsigned long normcol[ColLast]; static unsigned long selcol[ColLast]; -static Bool running = True; +static Bool topbar = True; static DC dc; static Display *dpy; static Window win, root; @@ -51,16 +49,7 @@ cleanup(void) { cleanupdraw(&dc); XDestroyWindow(dpy, win); XUngrabKeyboard(dpy, CurrentTime); -} - -void -drawcursor(void) { - XRectangle r = { dc.x, dc.y + 2, 1, dc.font.height - 2 }; - - r.x += textnw(&dc, text, cursor) + dc.font.height / 2; - - XSetForeground(dpy, dc.gc, normcol[ColFG]); - XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); + XCloseDisplay(dpy); } void @@ -70,31 +59,30 @@ drawinput(void) dc.y = 0; dc.w = mw; dc.h = mh; - drawtext(&dc, NULL, normcol, False); + drawtext(&dc, NULL, normcol); /* print prompt? */ if(prompt) { dc.w = promptw; - drawtext(&dc, prompt, selcol, False); + drawtext(&dc, prompt, selcol); dc.x += dc.w; } dc.w = mw - dc.x; - drawtext(&dc, *text ? text : NULL, normcol, False); - drawcursor(); - XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0); - XFlush(dpy); + drawtext(&dc, text, normcol); + drawcursor(&dc, text, cursor, normcol); + commitdraw(&dc, win); } -Bool +void grabkeyboard(void) { unsigned int len; for(len = 1000; len; len--) { if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess) - break; + return; usleep(1000); } - return len > 0; + exit(EXIT_FAILURE); } void @@ -200,9 +188,7 @@ kpress(XKeyEvent *e) { cursor = len; break; case XK_Escape: - ret = 1; - running = False; - return; + exit(EXIT_FAILURE); case XK_Home: cursor = 0; break; @@ -214,8 +200,7 @@ kpress(XKeyEvent *e) { case XK_Return: fprintf(stdout, "%s", text); fflush(stdout); - running = False; - return; + exit(EXIT_SUCCESS); case XK_Right: if(cursor == len) return; @@ -230,7 +215,8 @@ run(void) { XEvent ev; /* main event loop */ - while(running && !XNextEvent(dpy, &ev)) + XSync(dpy, False); + while(!XNextEvent(dpy, &ev)) switch(ev.type) { case KeyPress: kpress(&ev.xkey); @@ -240,14 +226,15 @@ run(void) { drawinput(); break; case VisibilityNotify: - if (ev.xvisibility.state != VisibilityUnobscured) + if(ev.xvisibility.state != VisibilityUnobscured) XRaiseWindow(dpy, win); break; } + exit(EXIT_FAILURE); } void -setup(Bool topbar) { +setup(void) { int i, j, x, y; #if XINERAMA int n; @@ -320,7 +307,6 @@ setup(Bool topbar) { int main(int argc, char *argv[]) { unsigned int i; - Bool topbar = True; /* command line args */ progname = "dinput"; @@ -364,15 +350,13 @@ main(int argc, char *argv[]) { fprintf(stderr, "dinput: warning: no locale support\n"); if(!(dpy = XOpenDisplay(NULL))) eprint("cannot open display\n"); + if(atexit(&cleanup) != 0) + eprint("cannot register cleanup\n"); screen = DefaultScreen(dpy); root = RootWindow(dpy, screen); - running = grabkeyboard(); - setup(topbar); - drawinput(); - XSync(dpy, False); + grabkeyboard(); + setup(); run(); - cleanup(); - XCloseDisplay(dpy); - return ret; + return 0; }