aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--dmenu.c51
1 files changed, 33 insertions, 18 deletions
diff --git a/dmenu.c b/dmenu.c
index 841af4a..5e01441 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -70,35 +70,35 @@ main(int argc, char *argv[]) {
70 int i; 70 int i;
71 71
72 for(i = 1; i < argc; i++) 72 for(i = 1; i < argc; i++)
73 /* single flags */ 73 /* these options take no arguments */
74 if(!strcmp(argv[i], "-v")) { 74 if(!strcmp(argv[i], "-v")) { /* prints version information */
75 puts("dmenu-"VERSION", © 2006-2011 dmenu engineers, see LICENSE for details"); 75 puts("dmenu-"VERSION", © 2006-2011 dmenu engineers, see LICENSE for details");
76 exit(EXIT_SUCCESS); 76 exit(EXIT_SUCCESS);
77 } 77 }
78 else if(!strcmp(argv[i], "-b")) 78 else if(!strcmp(argv[i], "-b")) /* appears at the bottom of the screen */
79 topbar = False; 79 topbar = False;
80 else if(!strcmp(argv[i], "-f")) 80 else if(!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */
81 fast = True; 81 fast = True;
82 else if(!strcmp(argv[i], "-i")) { 82 else if(!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
83 fstrncmp = strncasecmp; 83 fstrncmp = strncasecmp;
84 fstrstr = cistrstr; 84 fstrstr = cistrstr;
85 } 85 }
86 else if(i+1 == argc) 86 else if(i+1 == argc)
87 usage(); 87 usage();
88 /* double flags */ 88 /* these options take one argument */
89 else if(!strcmp(argv[i], "-l")) 89 else if(!strcmp(argv[i], "-l")) /* number of lines in vertical list */
90 lines = atoi(argv[++i]); 90 lines = atoi(argv[++i]);
91 else if(!strcmp(argv[i], "-p")) 91 else if(!strcmp(argv[i], "-p")) /* adds prompt to left of input field */
92 prompt = argv[++i]; 92 prompt = argv[++i];
93 else if(!strcmp(argv[i], "-fn")) 93 else if(!strcmp(argv[i], "-fn")) /* font or font set */
94 font = argv[++i]; 94 font = argv[++i];
95 else if(!strcmp(argv[i], "-nb")) 95 else if(!strcmp(argv[i], "-nb")) /* normal background color */
96 normbgcolor = argv[++i]; 96 normbgcolor = argv[++i];
97 else if(!strcmp(argv[i], "-nf")) 97 else if(!strcmp(argv[i], "-nf")) /* normal foreground color */
98 normfgcolor = argv[++i]; 98 normfgcolor = argv[++i];
99 else if(!strcmp(argv[i], "-sb")) 99 else if(!strcmp(argv[i], "-sb")) /* selected background color */
100 selbgcolor = argv[++i]; 100 selbgcolor = argv[++i];
101 else if(!strcmp(argv[i], "-sf")) 101 else if(!strcmp(argv[i], "-sf")) /* selected foreground color */
102 selfgcolor = argv[++i]; 102 selfgcolor = argv[++i];
103 else 103 else
104 usage(); 104 usage();
@@ -140,7 +140,7 @@ calcoffsets(void) {
140 n = lines * bh; 140 n = lines * bh;
141 else 141 else
142 n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">")); 142 n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">"));
143 143 /* calculate which items will begin the next page and previous page */
144 for(i = 0, next = curr; next; next = next->right) 144 for(i = 0, next = curr; next; next = next->right)
145 if((i += (lines > 0) ? bh : MIN(textw(dc, next->text), n)) > n) 145 if((i += (lines > 0) ? bh : MIN(textw(dc, next->text), n)) > n)
146 break; 146 break;
@@ -174,12 +174,14 @@ drawmenu(void) {
174 drawtext(dc, prompt, selcol); 174 drawtext(dc, prompt, selcol);
175 dc->x = dc->w; 175 dc->x = dc->w;
176 } 176 }
177 /* draw input field */
177 dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw; 178 dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw;
178 drawtext(dc, text, normcol); 179 drawtext(dc, text, normcol);
179 if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w) 180 if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w)
180 drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol)); 181 drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol));
181 182
182 if(lines > 0) { 183 if(lines > 0) {
184 /* draw vertical list */
183 dc->w = mw - dc->x; 185 dc->w = mw - dc->x;
184 for(item = curr; item != next; item = item->right) { 186 for(item = curr; item != next; item = item->right) {
185 dc->y += dc->h; 187 dc->y += dc->h;
@@ -187,6 +189,7 @@ drawmenu(void) {
187 } 189 }
188 } 190 }
189 else if(matches) { 191 else if(matches) {
192 /* draw horizontal list */
190 dc->x += inputw; 193 dc->x += inputw;
191 dc->w = textw(dc, "<"); 194 dc->w = textw(dc, "<");
192 if(curr->left) 195 if(curr->left)
@@ -208,6 +211,7 @@ void
208grabkeyboard(void) { 211grabkeyboard(void) {
209 int i; 212 int i;
210 213
214 /* try to grab keyboard, we may have to wait for another process to ungrab */
211 for(i = 0; i < 1000; i++) { 215 for(i = 0; i < 1000; i++) {
212 if(XGrabKeyboard(dc->dpy, DefaultRootWindow(dc->dpy), True, 216 if(XGrabKeyboard(dc->dpy, DefaultRootWindow(dc->dpy), True,
213 GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess) 217 GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
@@ -221,6 +225,7 @@ void
221insert(const char *str, ssize_t n) { 225insert(const char *str, ssize_t n) {
222 if(strlen(text) + n > sizeof text - 1) 226 if(strlen(text) + n > sizeof text - 1)
223 return; 227 return;
228 /* move existing text out of the way, insert new text, and update cursor */
224 memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0)); 229 memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0));
225 if(n > 0) 230 if(n > 0)
226 memcpy(&text[cursor], str, n); 231 memcpy(&text[cursor], str, n);
@@ -297,6 +302,7 @@ keypress(XKeyEvent *ev) {
297 break; 302 break;
298 } 303 }
299 if(next) { 304 if(next) {
305 /* jump to end of list and position items in reverse */
300 curr = matchend; 306 curr = matchend;
301 calcoffsets(); 307 calcoffsets();
302 curr = prev; 308 curr = prev;
@@ -378,6 +384,7 @@ match(void) {
378 Item *item, *lprefix, *lsubstr, *prefixend, *substrend; 384 Item *item, *lprefix, *lsubstr, *prefixend, *substrend;
379 385
380 strcpy(buf, text); 386 strcpy(buf, text);
387 /* separate input text into tokens to be matched individually */
381 for(s = strtok(buf, " "); s; tokv[tokc-1] = s, s = strtok(NULL, " ")) 388 for(s = strtok(buf, " "); s; tokv[tokc-1] = s, s = strtok(NULL, " "))
382 if(++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv))) 389 if(++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv)))
383 eprintf("cannot realloc %u bytes\n", tokn * sizeof *tokv); 390 eprintf("cannot realloc %u bytes\n", tokn * sizeof *tokv);
@@ -388,8 +395,9 @@ match(void) {
388 for(i = 0; i < tokc; i++) 395 for(i = 0; i < tokc; i++)
389 if(!fstrstr(item->text, tokv[i])) 396 if(!fstrstr(item->text, tokv[i]))
390 break; 397 break;
391 if(i != tokc) 398 if(i != tokc) /* not all tokens match */
392 continue; 399 continue;
400 /* exact matches go first, then prefixes, then substrings */
393 if(!tokc || !fstrncmp(tokv[0], item->text, len+1)) 401 if(!tokc || !fstrncmp(tokv[0], item->text, len+1))
394 appenditem(item, &matches, &matchend); 402 appenditem(item, &matches, &matchend);
395 else if(!fstrncmp(tokv[0], item->text, len)) 403 else if(!fstrncmp(tokv[0], item->text, len))
@@ -423,6 +431,7 @@ size_t
423nextrune(int inc) { 431nextrune(int inc) {
424 ssize_t n; 432 ssize_t n;
425 433
434 /* return location of next utf8 rune in the given direction (+1 or -1) */
426 for(n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc); 435 for(n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc);
427 return n; 436 return n;
428} 437}
@@ -434,6 +443,7 @@ paste(void) {
434 unsigned long dl; 443 unsigned long dl;
435 Atom da; 444 Atom da;
436 445
446 /* we have been given the current selection, now insert it into input */
437 XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False, 447 XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
438 utf8, &da, &di, &dl, &dl, (unsigned char **)&p); 448 utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
439 insert(p, (q = strchr(p, '\n')) ? q-p : (ssize_t)strlen(p)); 449 insert(p, (q = strchr(p, '\n')) ? q-p : (ssize_t)strlen(p));
@@ -446,6 +456,7 @@ readstdin(void) {
446 char buf[sizeof text], *p, *maxstr = NULL; 456 char buf[sizeof text], *p, *maxstr = NULL;
447 size_t i, max = 0, size = 0; 457 size_t i, max = 0, size = 0;
448 458
459 /* read each line from stdin and add it to the item list */
449 for(i = 0; fgets(buf, sizeof buf, stdin); i++) { 460 for(i = 0; fgets(buf, sizeof buf, stdin); i++) {
450 if(i+1 >= size / sizeof *items) 461 if(i+1 >= size / sizeof *items)
451 if(!(items = realloc(items, (size += BUFSIZ)))) 462 if(!(items = realloc(items, (size += BUFSIZ))))
@@ -508,7 +519,7 @@ setup(void) {
508 519
509 utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False); 520 utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
510 521
511 /* menu geometry */ 522 /* calculate menu geometry */
512 bh = dc->font.height + 2; 523 bh = dc->font.height + 2;
513 lines = MAX(lines, 0); 524 lines = MAX(lines, 0);
514 mh = (lines + 1) * bh; 525 mh = (lines + 1) * bh;
@@ -521,10 +532,12 @@ setup(void) {
521 532
522 XGetInputFocus(dc->dpy, &w, &di); 533 XGetInputFocus(dc->dpy, &w, &di);
523 if(w != root && w != PointerRoot && w != None) { 534 if(w != root && w != PointerRoot && w != None) {
535 /* find top-level window containing current input focus */
524 do { 536 do {
525 if(XQueryTree(dc->dpy, (pw = w), &dw, &w, &dws, &du) && dws) 537 if(XQueryTree(dc->dpy, (pw = w), &dw, &w, &dws, &du) && dws)
526 XFree(dws); 538 XFree(dws);
527 } while(w != root && w != pw); 539 } while(w != root && w != pw);
540 /* find xinerama screen with which the window intersects most */
528 if(XGetWindowAttributes(dc->dpy, pw, &wa)) 541 if(XGetWindowAttributes(dc->dpy, pw, &wa))
529 for(j = 0; j < n; j++) 542 for(j = 0; j < n; j++)
530 if((a = INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j])) > area) { 543 if((a = INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j])) > area) {
@@ -532,10 +545,12 @@ setup(void) {
532 i = j; 545 i = j;
533 } 546 }
534 } 547 }
548 /* no focused window is on screen, so use pointer location instead */
535 if(!area && XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du)) 549 if(!area && XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du))
536 for(i = 0; i < n; i++) 550 for(i = 0; i < n; i++)
537 if(INTERSECT(x, y, 1, 1, info[i])) 551 if(INTERSECT(x, y, 1, 1, info[i]))
538 break; 552 break;
553
539 x = info[i].x_org; 554 x = info[i].x_org;
540 y = info[i].y_org + (topbar ? 0 : info[i].height - mh); 555 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
541 mw = info[i].width; 556 mw = info[i].width;
@@ -552,7 +567,7 @@ setup(void) {
552 inputw = MIN(inputw, mw/3); 567 inputw = MIN(inputw, mw/3);
553 match(); 568 match();
554 569
555 /* menu window */ 570 /* create menu window */
556 swa.override_redirect = True; 571 swa.override_redirect = True;
557 swa.background_pixmap = ParentRelative; 572 swa.background_pixmap = ParentRelative;
558 swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask; 573 swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
@@ -561,7 +576,7 @@ setup(void) {
561 DefaultVisual(dc->dpy, screen), 576 DefaultVisual(dc->dpy, screen),
562 CWOverrideRedirect | CWBackPixmap | CWEventMask, &swa); 577 CWOverrideRedirect | CWBackPixmap | CWEventMask, &swa);
563 578
564 /* input methods */ 579 /* open input methods */
565 xim = XOpenIM(dc->dpy, NULL, NULL, NULL); 580 xim = XOpenIM(dc->dpy, NULL, NULL, NULL);
566 xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, 581 xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
567 XNClientWindow, win, XNFocusWindow, win, NULL); 582 XNClientWindow, win, XNFocusWindow, win, NULL);