diff options
-rw-r--r-- | dinput.c | 35 | ||||
-rw-r--r-- | dmenu.c | 47 |
2 files changed, 36 insertions, 46 deletions
@@ -26,7 +26,7 @@ static void drawinput(void); | |||
26 | static Bool grabkeyboard(void); | 26 | static Bool grabkeyboard(void); |
27 | static void kpress(XKeyEvent *e); | 27 | static void kpress(XKeyEvent *e); |
28 | static void run(void); | 28 | static void run(void); |
29 | static void setup(Bool topbar); | 29 | static void setup(void); |
30 | 30 | ||
31 | #include "config.h" | 31 | #include "config.h" |
32 | 32 | ||
@@ -34,14 +34,13 @@ static void setup(Bool topbar); | |||
34 | static char *prompt = NULL; | 34 | static char *prompt = NULL; |
35 | static char text[4096]; | 35 | static char text[4096]; |
36 | static int promptw = 0; | 36 | static int promptw = 0; |
37 | static int ret = 0; | ||
38 | static int screen; | 37 | static int screen; |
39 | static unsigned int cursor = 0; | 38 | static unsigned int cursor = 0; |
40 | static unsigned int numlockmask = 0; | 39 | static unsigned int numlockmask = 0; |
41 | static unsigned int mw, mh; | 40 | static unsigned int mw, mh; |
42 | static unsigned long normcol[ColLast]; | 41 | static unsigned long normcol[ColLast]; |
43 | static unsigned long selcol[ColLast]; | 42 | static unsigned long selcol[ColLast]; |
44 | static Bool running = True; | 43 | static Bool topbar = True; |
45 | static DC dc; | 44 | static DC dc; |
46 | static Display *dpy; | 45 | static Display *dpy; |
47 | static Window win, root; | 46 | static Window win, root; |
@@ -51,6 +50,7 @@ cleanup(void) { | |||
51 | cleanupdraw(&dc); | 50 | cleanupdraw(&dc); |
52 | XDestroyWindow(dpy, win); | 51 | XDestroyWindow(dpy, win); |
53 | XUngrabKeyboard(dpy, CurrentTime); | 52 | XUngrabKeyboard(dpy, CurrentTime); |
53 | XCloseDisplay(dpy); | ||
54 | } | 54 | } |
55 | 55 | ||
56 | void | 56 | void |
@@ -81,7 +81,6 @@ drawinput(void) | |||
81 | drawtext(&dc, *text ? text : NULL, normcol, False); | 81 | drawtext(&dc, *text ? text : NULL, normcol, False); |
82 | drawcursor(); | 82 | drawcursor(); |
83 | XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0); | 83 | XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0); |
84 | XFlush(dpy); | ||
85 | } | 84 | } |
86 | 85 | ||
87 | Bool | 86 | Bool |
@@ -200,9 +199,7 @@ kpress(XKeyEvent *e) { | |||
200 | cursor = len; | 199 | cursor = len; |
201 | break; | 200 | break; |
202 | case XK_Escape: | 201 | case XK_Escape: |
203 | ret = 1; | 202 | exit(EXIT_FAILURE); |
204 | running = False; | ||
205 | return; | ||
206 | case XK_Home: | 203 | case XK_Home: |
207 | cursor = 0; | 204 | cursor = 0; |
208 | break; | 205 | break; |
@@ -214,8 +211,7 @@ kpress(XKeyEvent *e) { | |||
214 | case XK_Return: | 211 | case XK_Return: |
215 | fprintf(stdout, "%s", text); | 212 | fprintf(stdout, "%s", text); |
216 | fflush(stdout); | 213 | fflush(stdout); |
217 | running = False; | 214 | exit(EXIT_SUCCESS); |
218 | return; | ||
219 | case XK_Right: | 215 | case XK_Right: |
220 | if(cursor == len) | 216 | if(cursor == len) |
221 | return; | 217 | return; |
@@ -230,7 +226,8 @@ run(void) { | |||
230 | XEvent ev; | 226 | XEvent ev; |
231 | 227 | ||
232 | /* main event loop */ | 228 | /* main event loop */ |
233 | while(running && !XNextEvent(dpy, &ev)) | 229 | XSync(dpy, False); |
230 | while(!XNextEvent(dpy, &ev)) | ||
234 | switch(ev.type) { | 231 | switch(ev.type) { |
235 | case KeyPress: | 232 | case KeyPress: |
236 | kpress(&ev.xkey); | 233 | kpress(&ev.xkey); |
@@ -240,14 +237,15 @@ run(void) { | |||
240 | drawinput(); | 237 | drawinput(); |
241 | break; | 238 | break; |
242 | case VisibilityNotify: | 239 | case VisibilityNotify: |
243 | if (ev.xvisibility.state != VisibilityUnobscured) | 240 | if(ev.xvisibility.state != VisibilityUnobscured) |
244 | XRaiseWindow(dpy, win); | 241 | XRaiseWindow(dpy, win); |
245 | break; | 242 | break; |
246 | } | 243 | } |
244 | exit(EXIT_FAILURE); | ||
247 | } | 245 | } |
248 | 246 | ||
249 | void | 247 | void |
250 | setup(Bool topbar) { | 248 | setup(void) { |
251 | int i, j, x, y; | 249 | int i, j, x, y; |
252 | #if XINERAMA | 250 | #if XINERAMA |
253 | int n; | 251 | int n; |
@@ -320,7 +318,6 @@ setup(Bool topbar) { | |||
320 | int | 318 | int |
321 | main(int argc, char *argv[]) { | 319 | main(int argc, char *argv[]) { |
322 | unsigned int i; | 320 | unsigned int i; |
323 | Bool topbar = True; | ||
324 | 321 | ||
325 | /* command line args */ | 322 | /* command line args */ |
326 | progname = "dinput"; | 323 | progname = "dinput"; |
@@ -364,15 +361,13 @@ main(int argc, char *argv[]) { | |||
364 | fprintf(stderr, "dinput: warning: no locale support\n"); | 361 | fprintf(stderr, "dinput: warning: no locale support\n"); |
365 | if(!(dpy = XOpenDisplay(NULL))) | 362 | if(!(dpy = XOpenDisplay(NULL))) |
366 | eprint("cannot open display\n"); | 363 | eprint("cannot open display\n"); |
364 | if(atexit(&cleanup) != 0) | ||
365 | eprint("cannot register cleanup\n"); | ||
367 | screen = DefaultScreen(dpy); | 366 | screen = DefaultScreen(dpy); |
368 | root = RootWindow(dpy, screen); | 367 | root = RootWindow(dpy, screen); |
369 | 368 | ||
370 | running = grabkeyboard(); | 369 | grabkeyboard(); |
371 | setup(topbar); | 370 | setup(); |
372 | drawinput(); | ||
373 | XSync(dpy, False); | ||
374 | run(); | 371 | run(); |
375 | cleanup(); | 372 | return 0; |
376 | XCloseDisplay(dpy); | ||
377 | return ret; | ||
378 | } | 373 | } |
@@ -36,7 +36,7 @@ static void dinput(void); | |||
36 | static void drawmenu(void); | 36 | static void drawmenu(void); |
37 | static void drawmenuh(void); | 37 | static void drawmenuh(void); |
38 | static void drawmenuv(void); | 38 | static void drawmenuv(void); |
39 | static Bool grabkeyboard(void); | 39 | static void grabkeyboard(void); |
40 | static void kpress(XKeyEvent *e); | 40 | static void kpress(XKeyEvent *e); |
41 | static void match(char *pattern); | 41 | static void match(char *pattern); |
42 | static void readstdin(void); | 42 | static void readstdin(void); |
@@ -52,14 +52,12 @@ static char *prompt = NULL; | |||
52 | static char text[4096]; | 52 | static char text[4096]; |
53 | static int cmdw = 0; | 53 | static int cmdw = 0; |
54 | static int promptw = 0; | 54 | static int promptw = 0; |
55 | static int ret = 0; | ||
56 | static int screen; | 55 | static int screen; |
57 | static unsigned int lines = 0; | 56 | static unsigned int lines = 0; |
58 | static unsigned int numlockmask = 0; | 57 | static unsigned int numlockmask = 0; |
59 | static unsigned int mw, mh; | 58 | static unsigned int mw, mh; |
60 | static unsigned long normcol[ColLast]; | 59 | static unsigned long normcol[ColLast]; |
61 | static unsigned long selcol[ColLast]; | 60 | static unsigned long selcol[ColLast]; |
62 | static Bool running = True; | ||
63 | static Bool topbar = True; | 61 | static Bool topbar = True; |
64 | static DC dc; | 62 | static DC dc; |
65 | static Display *dpy; | 63 | static Display *dpy; |
@@ -87,15 +85,15 @@ appenditem(Item *i, Item **list, Item **last) { | |||
87 | 85 | ||
88 | void | 86 | void |
89 | calcoffsetsh(void) { | 87 | calcoffsetsh(void) { |
90 | unsigned int w; | 88 | unsigned int x; |
91 | 89 | ||
92 | w = promptw + cmdw + (2 * spaceitem); | 90 | x = promptw + cmdw + (2 * spaceitem); |
93 | for(next = curr; next; next = next->right) | 91 | for(next = curr; next; next = next->right) |
94 | if((w += MIN(textw(&dc, next->text), mw / 3)) > mw) | 92 | if((x += MIN(textw(&dc, next->text), mw / 3)) > mw) |
95 | break; | 93 | break; |
96 | w = promptw + cmdw + (2 * spaceitem); | 94 | x = promptw + cmdw + (2 * spaceitem); |
97 | for(prev = curr; prev && prev->left; prev = prev->left) | 95 | for(prev = curr; prev && prev->left; prev = prev->left) |
98 | if((w += MIN(textw(&dc, prev->left->text), mw / 3)) > mw) | 96 | if((x += MIN(textw(&dc, prev->left->text), mw / 3)) > mw) |
99 | break; | 97 | break; |
100 | } | 98 | } |
101 | 99 | ||
@@ -146,6 +144,7 @@ cleanup(void) { | |||
146 | cleanupdraw(&dc); | 144 | cleanupdraw(&dc); |
147 | XDestroyWindow(dpy, win); | 145 | XDestroyWindow(dpy, win); |
148 | XUngrabKeyboard(dpy, CurrentTime); | 146 | XUngrabKeyboard(dpy, CurrentTime); |
147 | XCloseDisplay(dpy); | ||
149 | } | 148 | } |
150 | 149 | ||
151 | void | 150 | void |
@@ -182,7 +181,6 @@ drawmenu(void) { | |||
182 | else if(curr) | 181 | else if(curr) |
183 | drawmenuh(); | 182 | drawmenuh(); |
184 | XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0); | 183 | XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0); |
185 | XFlush(dpy); | ||
186 | } | 184 | } |
187 | 185 | ||
188 | void | 186 | void |
@@ -219,7 +217,7 @@ drawmenuv(void) { | |||
219 | XMoveResizeWindow(dpy, win, wa.x, wa.y + (topbar ? 0 : wa.height - mh), mw, mh); | 217 | XMoveResizeWindow(dpy, win, wa.x, wa.y + (topbar ? 0 : wa.height - mh), mw, mh); |
220 | } | 218 | } |
221 | 219 | ||
222 | Bool | 220 | void |
223 | grabkeyboard(void) { | 221 | grabkeyboard(void) { |
224 | unsigned int len; | 222 | unsigned int len; |
225 | 223 | ||
@@ -229,7 +227,8 @@ grabkeyboard(void) { | |||
229 | break; | 227 | break; |
230 | usleep(1000); | 228 | usleep(1000); |
231 | } | 229 | } |
232 | return len > 0; | 230 | if(!len) |
231 | exit(EXIT_FAILURE); | ||
233 | } | 232 | } |
234 | 233 | ||
235 | void | 234 | void |
@@ -326,9 +325,7 @@ kpress(XKeyEvent *e) { | |||
326 | sel = sel->right; | 325 | sel = sel->right; |
327 | break; | 326 | break; |
328 | case XK_Escape: | 327 | case XK_Escape: |
329 | ret = 1; | 328 | exit(EXIT_FAILURE); |
330 | running = False; | ||
331 | return; | ||
332 | case XK_Home: | 329 | case XK_Home: |
333 | sel = curr = item; | 330 | sel = curr = item; |
334 | calcoffsets(); | 331 | calcoffsets(); |
@@ -360,8 +357,7 @@ kpress(XKeyEvent *e) { | |||
360 | dinput(); | 357 | dinput(); |
361 | fprintf(stdout, "%s", sel ? sel->text : text); | 358 | fprintf(stdout, "%s", sel ? sel->text : text); |
362 | fflush(stdout); | 359 | fflush(stdout); |
363 | running = False; | 360 | exit(EXIT_SUCCESS); |
364 | return; | ||
365 | case XK_Right: | 361 | case XK_Right: |
366 | case XK_Down: | 362 | case XK_Down: |
367 | if(!sel || !sel->right) | 363 | if(!sel || !sel->right) |
@@ -454,7 +450,8 @@ run(void) { | |||
454 | XEvent ev; | 450 | XEvent ev; |
455 | 451 | ||
456 | /* main event loop */ | 452 | /* main event loop */ |
457 | while(running && !XNextEvent(dpy, &ev)) | 453 | XSync(dpy, False); |
454 | while(!XNextEvent(dpy, &ev)) | ||
458 | switch(ev.type) { | 455 | switch(ev.type) { |
459 | case KeyPress: | 456 | case KeyPress: |
460 | kpress(&ev.xkey); | 457 | kpress(&ev.xkey); |
@@ -464,10 +461,11 @@ run(void) { | |||
464 | drawmenu(); | 461 | drawmenu(); |
465 | break; | 462 | break; |
466 | case VisibilityNotify: | 463 | case VisibilityNotify: |
467 | if (ev.xvisibility.state != VisibilityUnobscured) | 464 | if(ev.xvisibility.state != VisibilityUnobscured) |
468 | XRaiseWindow(dpy, win); | 465 | XRaiseWindow(dpy, win); |
469 | break; | 466 | break; |
470 | } | 467 | } |
468 | exit(EXIT_FAILURE); | ||
471 | } | 469 | } |
472 | 470 | ||
473 | void | 471 | void |
@@ -586,13 +584,15 @@ main(int argc, char *argv[]) { | |||
586 | } | 584 | } |
587 | else { | 585 | else { |
588 | fputs("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>]\n" | 586 | fputs("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>]\n" |
589 | " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr); | 587 | " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr); |
590 | exit(EXIT_FAILURE); | 588 | exit(EXIT_FAILURE); |
591 | } | 589 | } |
592 | if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) | 590 | if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) |
593 | fprintf(stderr, "dmenu: warning: no locale support\n"); | 591 | fprintf(stderr, "dmenu: warning: no locale support\n"); |
594 | if(!(dpy = XOpenDisplay(NULL))) | 592 | if(!(dpy = XOpenDisplay(NULL))) |
595 | eprint("cannot open display\n"); | 593 | eprint("cannot open display\n"); |
594 | if(atexit(&cleanup) != 0) | ||
595 | eprint("cannot register cleanup\n"); | ||
596 | screen = DefaultScreen(dpy); | 596 | screen = DefaultScreen(dpy); |
597 | root = RootWindow(dpy, screen); | 597 | root = RootWindow(dpy, screen); |
598 | if(!(argp = malloc(sizeof *argp * (argc+2)))) | 598 | if(!(argp = malloc(sizeof *argp * (argc+2)))) |
@@ -600,13 +600,8 @@ main(int argc, char *argv[]) { | |||
600 | memcpy(argp + 2, argv + 1, sizeof *argp * argc); | 600 | memcpy(argp + 2, argv + 1, sizeof *argp * argc); |
601 | 601 | ||
602 | readstdin(); | 602 | readstdin(); |
603 | running = grabkeyboard(); | 603 | grabkeyboard(); |
604 | |||
605 | setup(); | 604 | setup(); |
606 | drawmenu(); | ||
607 | XSync(dpy, False); | ||
608 | run(); | 605 | run(); |
609 | cleanup(); | 606 | return 0; |
610 | XCloseDisplay(dpy); | ||
611 | return ret; | ||
612 | } | 607 | } |