aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--dinput.c35
-rw-r--r--dmenu.c47
2 files changed, 36 insertions, 46 deletions
diff --git a/dinput.c b/dinput.c
index b789bd7..4c90cd7 100644
--- a/dinput.c
+++ b/dinput.c
@@ -26,7 +26,7 @@ static void drawinput(void);
26static Bool grabkeyboard(void); 26static Bool grabkeyboard(void);
27static void kpress(XKeyEvent *e); 27static void kpress(XKeyEvent *e);
28static void run(void); 28static void run(void);
29static void setup(Bool topbar); 29static void setup(void);
30 30
31#include "config.h" 31#include "config.h"
32 32
@@ -34,14 +34,13 @@ static void setup(Bool topbar);
34static char *prompt = NULL; 34static char *prompt = NULL;
35static char text[4096]; 35static char text[4096];
36static int promptw = 0; 36static int promptw = 0;
37static int ret = 0;
38static int screen; 37static int screen;
39static unsigned int cursor = 0; 38static unsigned int cursor = 0;
40static unsigned int numlockmask = 0; 39static unsigned int numlockmask = 0;
41static unsigned int mw, mh; 40static unsigned int mw, mh;
42static unsigned long normcol[ColLast]; 41static unsigned long normcol[ColLast];
43static unsigned long selcol[ColLast]; 42static unsigned long selcol[ColLast];
44static Bool running = True; 43static Bool topbar = True;
45static DC dc; 44static DC dc;
46static Display *dpy; 45static Display *dpy;
47static Window win, root; 46static 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
56void 56void
@@ -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
87Bool 86Bool
@@ -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
249void 247void
250setup(Bool topbar) { 248setup(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) {
320int 318int
321main(int argc, char *argv[]) { 319main(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}
diff --git a/dmenu.c b/dmenu.c
index 680b4e7..2cc2cad 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -36,7 +36,7 @@ static void dinput(void);
36static void drawmenu(void); 36static void drawmenu(void);
37static void drawmenuh(void); 37static void drawmenuh(void);
38static void drawmenuv(void); 38static void drawmenuv(void);
39static Bool grabkeyboard(void); 39static void grabkeyboard(void);
40static void kpress(XKeyEvent *e); 40static void kpress(XKeyEvent *e);
41static void match(char *pattern); 41static void match(char *pattern);
42static void readstdin(void); 42static void readstdin(void);
@@ -52,14 +52,12 @@ static char *prompt = NULL;
52static char text[4096]; 52static char text[4096];
53static int cmdw = 0; 53static int cmdw = 0;
54static int promptw = 0; 54static int promptw = 0;
55static int ret = 0;
56static int screen; 55static int screen;
57static unsigned int lines = 0; 56static unsigned int lines = 0;
58static unsigned int numlockmask = 0; 57static unsigned int numlockmask = 0;
59static unsigned int mw, mh; 58static unsigned int mw, mh;
60static unsigned long normcol[ColLast]; 59static unsigned long normcol[ColLast];
61static unsigned long selcol[ColLast]; 60static unsigned long selcol[ColLast];
62static Bool running = True;
63static Bool topbar = True; 61static Bool topbar = True;
64static DC dc; 62static DC dc;
65static Display *dpy; 63static Display *dpy;
@@ -87,15 +85,15 @@ appenditem(Item *i, Item **list, Item **last) {
87 85
88void 86void
89calcoffsetsh(void) { 87calcoffsetsh(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
151void 150void
@@ -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
188void 186void
@@ -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
222Bool 220void
223grabkeyboard(void) { 221grabkeyboard(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
235void 234void
@@ -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
473void 471void
@@ -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}