dmenu.c (21782B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <ctype.h> 3 #include <locale.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <strings.h> 8 #include <time.h> 9 #include <unistd.h> 10 11 #include <X11/Xlib.h> 12 #include <X11/Xatom.h> 13 #include <X11/Xutil.h> 14 #ifdef XINERAMA 15 #include <X11/extensions/Xinerama.h> 16 #endif 17 #include <X11/Xft/Xft.h> 18 19 #include "drw.h" 20 #include "util.h" 21 22 /* macros */ 23 #define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \ 24 * MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org))) 25 #define LENGTH(X) (sizeof X / sizeof X[0]) 26 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) 27 28 /* enums */ 29 enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */ 30 31 struct item { 32 char *text; 33 struct item *left, *right; 34 int out; 35 }; 36 37 static char text[BUFSIZ] = ""; 38 static char *embed; 39 static int bh, mw, mh; 40 static int inputw = 0, promptw, passwd = 0; 41 static int lrpad; /* sum of left and right padding */ 42 static size_t cursor; 43 static struct item *items = NULL; 44 static struct item *matches, *matchend; 45 static struct item *prev, *curr, *next, *sel; 46 static int mon = -1, screen; 47 48 static Atom clip, utf8; 49 static Display *dpy; 50 static Window root, parentwin, win; 51 static XIC xic; 52 53 static Drw *drw; 54 static Clr *scheme[SchemeLast]; 55 56 #include "config.h" 57 58 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp; 59 static char *(*fstrstr)(const char *, const char *) = strstr; 60 61 static void 62 appenditem(struct item *item, struct item **list, struct item **last) 63 { 64 if (*last) 65 (*last)->right = item; 66 else 67 *list = item; 68 69 item->left = *last; 70 item->right = NULL; 71 *last = item; 72 } 73 74 static void 75 calcoffsets(void) 76 { 77 int i, n; 78 79 if (lines > 0) 80 n = lines * bh; 81 else 82 n = mw - (promptw + inputw + TEXTW("<") + TEXTW(">")); 83 /* calculate which items will begin the next page and previous page */ 84 for (i = 0, next = curr; next; next = next->right) 85 if ((i += (lines > 0) ? bh : MIN(TEXTW(next->text), n)) > n) 86 break; 87 for (i = 0, prev = curr; prev && prev->left; prev = prev->left) 88 if ((i += (lines > 0) ? bh : MIN(TEXTW(prev->left->text), n)) > n) 89 break; 90 } 91 92 static void 93 cleanup(void) 94 { 95 size_t i; 96 97 XUngrabKey(dpy, AnyKey, AnyModifier, root); 98 for (i = 0; i < SchemeLast; i++) 99 free(scheme[i]); 100 drw_free(drw); 101 XSync(dpy, False); 102 XCloseDisplay(dpy); 103 } 104 105 static char * 106 cistrstr(const char *s, const char *sub) 107 { 108 size_t len; 109 110 for (len = strlen(sub); *s; s++) 111 if (!strncasecmp(s, sub, len)) 112 return (char *)s; 113 return NULL; 114 } 115 116 static int 117 drawitem(struct item *item, int x, int y, int w) 118 { 119 if (item == sel) 120 drw_setscheme(drw, scheme[SchemeSel]); 121 else if (item->out) 122 drw_setscheme(drw, scheme[SchemeOut]); 123 else 124 drw_setscheme(drw, scheme[SchemeNorm]); 125 126 return drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0); 127 } 128 129 static void 130 drawmenu(void) 131 { 132 unsigned int curpos; 133 struct item *item; 134 int x = 0, y = 0, w; 135 char *censort; 136 137 drw_setscheme(drw, scheme[SchemeNorm]); 138 drw_rect(drw, 0, 0, mw, mh, 1, 1); 139 140 if (prompt && *prompt) { 141 drw_setscheme(drw, scheme[SchemeSel]); 142 x = drw_text(drw, x, 0, promptw, bh, lrpad / 2, prompt, 0); 143 } 144 /* draw input field */ 145 w = (lines > 0 || !matches) ? mw - x : inputw; 146 drw_setscheme(drw, scheme[SchemeNorm]); 147 if (passwd) { 148 censort = ecalloc(1, sizeof(text)); 149 memset(censort, '.', strlen(text)); 150 drw_text(drw, x, 0, w, bh, lrpad / 2, censort, 0); 151 free(censort); 152 } else drw_text(drw, x, 0, w, bh, lrpad / 2, text, 0); 153 154 curpos = TEXTW(text) - TEXTW(&text[cursor]); 155 if ((curpos += lrpad / 2 - 1) < w) { 156 drw_setscheme(drw, scheme[SchemeNorm]); 157 drw_rect(drw, x + curpos, 2, 2, bh - 4, 1, 0); 158 } 159 160 if (lines > 0) { 161 /* draw vertical list */ 162 for (item = curr; item != next; item = item->right) 163 drawitem(item, x, y += bh, mw - x); 164 } else if (matches) { 165 /* draw horizontal list */ 166 x += inputw; 167 w = TEXTW("<"); 168 if (curr->left) { 169 drw_setscheme(drw, scheme[SchemeNorm]); 170 drw_text(drw, x, 0, w, bh, lrpad / 2, "<", 0); 171 } 172 x += w; 173 for (item = curr; item != next; item = item->right) 174 x = drawitem(item, x, 0, MIN(TEXTW(item->text), mw - x - TEXTW(">"))); 175 if (next) { 176 w = TEXTW(">"); 177 drw_setscheme(drw, scheme[SchemeNorm]); 178 drw_text(drw, mw - w, 0, w, bh, lrpad / 2, ">", 0); 179 } 180 } 181 drw_map(drw, win, 0, 0, mw, mh); 182 } 183 184 static void 185 grabfocus(void) 186 { 187 struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 }; 188 Window focuswin; 189 int i, revertwin; 190 191 for (i = 0; i < 100; ++i) { 192 XGetInputFocus(dpy, &focuswin, &revertwin); 193 if (focuswin == win) 194 return; 195 XSetInputFocus(dpy, win, RevertToParent, CurrentTime); 196 nanosleep(&ts, NULL); 197 } 198 die("cannot grab focus"); 199 } 200 201 static void 202 grabkeyboard(void) 203 { 204 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 }; 205 int i; 206 207 if (embed) 208 return; 209 /* try to grab keyboard, we may have to wait for another process to ungrab */ 210 for (i = 0; i < 1000; i++) { 211 if (XGrabKeyboard(dpy, DefaultRootWindow(dpy), True, GrabModeAsync, 212 GrabModeAsync, CurrentTime) == GrabSuccess) 213 return; 214 nanosleep(&ts, NULL); 215 } 216 die("cannot grab keyboard"); 217 } 218 219 static void 220 match(void) 221 { 222 static char **tokv = NULL; 223 static int tokn = 0; 224 225 char buf[sizeof text], *s; 226 int i, tokc = 0; 227 size_t len, textsize; 228 struct item *item, *lprefix, *lsubstr, *prefixend, *substrend; 229 230 strcpy(buf, text); 231 /* separate input text into tokens to be matched individually */ 232 for (s = strtok(buf, " "); s; tokv[tokc - 1] = s, s = strtok(NULL, " ")) 233 if (++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv))) 234 die("cannot realloc %u bytes:", tokn * sizeof *tokv); 235 len = tokc ? strlen(tokv[0]) : 0; 236 237 matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL; 238 textsize = strlen(text) + 1; 239 for (item = items; item && item->text; item++) { 240 for (i = 0; i < tokc; i++) 241 if (!fstrstr(item->text, tokv[i])) 242 break; 243 if (i != tokc) /* not all tokens match */ 244 continue; 245 /* exact matches go first, then prefixes, then substrings */ 246 if (!tokc || !fstrncmp(text, item->text, textsize)) 247 appenditem(item, &matches, &matchend); 248 else if (!fstrncmp(tokv[0], item->text, len)) 249 appenditem(item, &lprefix, &prefixend); 250 else 251 appenditem(item, &lsubstr, &substrend); 252 } 253 if (lprefix) { 254 if (matches) { 255 matchend->right = lprefix; 256 lprefix->left = matchend; 257 } else 258 matches = lprefix; 259 matchend = prefixend; 260 } 261 if (lsubstr) { 262 if (matches) { 263 matchend->right = lsubstr; 264 lsubstr->left = matchend; 265 } else 266 matches = lsubstr; 267 matchend = substrend; 268 } 269 curr = sel = matches; 270 calcoffsets(); 271 } 272 273 static void 274 insert(const char *str, ssize_t n) 275 { 276 if (strlen(text) + n > sizeof text - 1) 277 return; 278 /* move existing text out of the way, insert new text, and update cursor */ 279 memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0)); 280 if (n > 0) 281 memcpy(&text[cursor], str, n); 282 cursor += n; 283 match(); 284 } 285 286 static size_t 287 nextrune(int inc) 288 { 289 ssize_t n; 290 291 /* return location of next utf8 rune in the given direction (+1 or -1) */ 292 for (n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc) 293 ; 294 return n; 295 } 296 297 static void 298 movewordedge(int dir) 299 { 300 if (dir < 0) { /* move cursor to the start of the word*/ 301 while (cursor > 0 && strchr(worddelimiters, text[nextrune(-1)])) 302 cursor = nextrune(-1); 303 while (cursor > 0 && !strchr(worddelimiters, text[nextrune(-1)])) 304 cursor = nextrune(-1); 305 } else { /* move cursor to the end of the word */ 306 while (text[cursor] && strchr(worddelimiters, text[cursor])) 307 cursor = nextrune(+1); 308 while (text[cursor] && !strchr(worddelimiters, text[cursor])) 309 cursor = nextrune(+1); 310 } 311 } 312 313 static void 314 keypress(XKeyEvent *ev) 315 { 316 char buf[32]; 317 int len; 318 KeySym ksym; 319 Status status; 320 321 len = XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status); 322 switch (status) { 323 default: /* XLookupNone, XBufferOverflow */ 324 return; 325 case XLookupChars: 326 goto insert; 327 case XLookupKeySym: 328 case XLookupBoth: 329 break; 330 } 331 332 if (ev->state & ControlMask) { 333 switch(ksym) { 334 case XK_a: ksym = XK_Home; break; 335 case XK_b: ksym = XK_Left; break; 336 case XK_c: ksym = XK_Escape; break; 337 case XK_d: ksym = XK_Delete; break; 338 case XK_e: ksym = XK_End; break; 339 case XK_f: ksym = XK_Right; break; 340 case XK_g: ksym = XK_Escape; break; 341 case XK_h: ksym = XK_BackSpace; break; 342 case XK_i: ksym = XK_Tab; break; 343 case XK_j: /* fallthrough */ 344 case XK_J: /* fallthrough */ 345 case XK_m: /* fallthrough */ 346 case XK_M: ksym = XK_Return; ev->state &= ~ControlMask; break; 347 case XK_n: ksym = XK_Down; break; 348 case XK_p: ksym = XK_Up; break; 349 350 case XK_k: /* delete right */ 351 text[cursor] = '\0'; 352 match(); 353 break; 354 case XK_u: /* delete left */ 355 insert(NULL, 0 - cursor); 356 break; 357 case XK_w: /* delete word */ 358 while (cursor > 0 && strchr(worddelimiters, text[nextrune(-1)])) 359 insert(NULL, nextrune(-1) - cursor); 360 while (cursor > 0 && !strchr(worddelimiters, text[nextrune(-1)])) 361 insert(NULL, nextrune(-1) - cursor); 362 break; 363 case XK_y: /* paste selection */ 364 case XK_Y: 365 XConvertSelection(dpy, (ev->state & ShiftMask) ? clip : XA_PRIMARY, 366 utf8, utf8, win, CurrentTime); 367 return; 368 case XK_Left: 369 movewordedge(-1); 370 goto draw; 371 case XK_Right: 372 movewordedge(+1); 373 goto draw; 374 case XK_Return: 375 case XK_KP_Enter: 376 break; 377 case XK_bracketleft: 378 cleanup(); 379 exit(1); 380 default: 381 return; 382 } 383 } else if (ev->state & Mod1Mask) { 384 switch(ksym) { 385 case XK_b: 386 movewordedge(-1); 387 goto draw; 388 case XK_f: 389 movewordedge(+1); 390 goto draw; 391 case XK_g: ksym = XK_Home; break; 392 case XK_G: ksym = XK_End; break; 393 case XK_h: ksym = XK_Up; break; 394 case XK_j: ksym = XK_Next; break; 395 case XK_k: ksym = XK_Prior; break; 396 case XK_l: ksym = XK_Down; break; 397 default: 398 return; 399 } 400 } 401 402 switch(ksym) { 403 default: 404 insert: 405 if (!iscntrl(*buf)) 406 insert(buf, len); 407 break; 408 case XK_Delete: 409 if (text[cursor] == '\0') 410 return; 411 cursor = nextrune(+1); 412 /* fallthrough */ 413 case XK_BackSpace: 414 if (cursor == 0) 415 return; 416 insert(NULL, nextrune(-1) - cursor); 417 break; 418 case XK_End: 419 if (text[cursor] != '\0') { 420 cursor = strlen(text); 421 break; 422 } 423 if (next) { 424 /* jump to end of list and position items in reverse */ 425 curr = matchend; 426 calcoffsets(); 427 curr = prev; 428 calcoffsets(); 429 while (next && (curr = curr->right)) 430 calcoffsets(); 431 } 432 sel = matchend; 433 break; 434 case XK_Escape: 435 cleanup(); 436 exit(1); 437 case XK_Home: 438 if (sel == matches) { 439 cursor = 0; 440 break; 441 } 442 sel = curr = matches; 443 calcoffsets(); 444 break; 445 case XK_Left: 446 if (cursor > 0 && (!sel || !sel->left || lines > 0)) { 447 cursor = nextrune(-1); 448 break; 449 } 450 if (lines > 0) 451 return; 452 /* fallthrough */ 453 case XK_Up: 454 if (sel && sel->left && (sel = sel->left)->right == curr) { 455 curr = prev; 456 calcoffsets(); 457 } 458 break; 459 case XK_Next: 460 if (!next) 461 return; 462 sel = curr = next; 463 calcoffsets(); 464 break; 465 case XK_Prior: 466 if (!prev) 467 return; 468 sel = curr = prev; 469 calcoffsets(); 470 break; 471 case XK_Return: 472 case XK_KP_Enter: 473 puts((sel && !(ev->state & ShiftMask)) ? sel->text : text); 474 if (!(ev->state & ControlMask)) { 475 cleanup(); 476 exit(0); 477 } 478 if (sel) 479 sel->out = 1; 480 break; 481 case XK_Right: 482 if (text[cursor] != '\0') { 483 cursor = nextrune(+1); 484 break; 485 } 486 if (lines > 0) 487 return; 488 /* fallthrough */ 489 case XK_Down: 490 if (sel && sel->right && (sel = sel->right) == next) { 491 curr = next; 492 calcoffsets(); 493 } 494 break; 495 case XK_Tab: 496 if (!sel) 497 return; 498 strncpy(text, sel->text, sizeof text - 1); 499 text[sizeof text - 1] = '\0'; 500 cursor = strlen(text); 501 match(); 502 break; 503 } 504 505 draw: 506 drawmenu(); 507 } 508 509 static void 510 paste(void) 511 { 512 char *p, *q; 513 int di; 514 unsigned long dl; 515 Atom da; 516 517 /* we have been given the current selection, now insert it into input */ 518 if (XGetWindowProperty(dpy, win, utf8, 0, (sizeof text / 4) + 1, False, 519 utf8, &da, &di, &dl, &dl, (unsigned char **)&p) 520 == Success && p) { 521 insert(p, (q = strchr(p, '\n')) ? q - p : (ssize_t)strlen(p)); 522 XFree(p); 523 } 524 drawmenu(); 525 } 526 527 static void 528 readstdin(void) 529 { 530 char buf[sizeof text], *p; 531 size_t i, imax = 0, size = 0; 532 unsigned int tmpmax = 0; 533 if(passwd){ 534 inputw = lines = 0; 535 return; 536 } 537 538 539 /* read each line from stdin and add it to the item list */ 540 for (i = 0; fgets(buf, sizeof buf, stdin); i++) { 541 if (i + 1 >= size / sizeof *items) 542 if (!(items = realloc(items, (size += BUFSIZ)))) 543 die("cannot realloc %u bytes:", size); 544 if ((p = strchr(buf, '\n'))) 545 *p = '\0'; 546 if (!(items[i].text = strdup(buf))) 547 die("cannot strdup %u bytes:", strlen(buf) + 1); 548 items[i].out = 0; 549 drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL); 550 if (tmpmax > inputw) { 551 inputw = tmpmax; 552 imax = i; 553 } 554 } 555 if (items) 556 items[i].text = NULL; 557 inputw = items ? TEXTW(items[imax].text) : 0; 558 lines = MIN(lines, i); 559 } 560 561 static void 562 buttonpress(XEvent *e) 563 { 564 struct item *item; 565 XButtonPressedEvent *ev = &e->xbutton; 566 int x = 0, y = 0, h = bh, w; 567 568 if (ev->window != win) 569 return; 570 571 /* right-click: exit */ 572 if (ev->button == Button3) 573 exit(1); 574 575 if (prompt && *prompt) 576 x += promptw; 577 578 /* input field */ 579 w = (lines > 0 || !matches) ? mw - x : inputw; 580 581 /* left-click on input: clear input, 582 * NOTE: if there is no left-arrow the space for < is reserved so 583 * add that to the input width */ 584 if (ev->button == Button1 && 585 ((lines <= 0 && ev->x >= 0 && ev->x <= x + w + 586 ((!prev || !curr->left) ? TEXTW("<") : 0)) || 587 (lines > 0 && ev->y >= y && ev->y <= y + h))) { 588 insert(NULL, -cursor); 589 drawmenu(); 590 return; 591 } 592 /* middle-mouse click: paste selection */ 593 if (ev->button == Button2) { 594 XConvertSelection(dpy, (ev->state & ShiftMask) ? clip : XA_PRIMARY, 595 utf8, utf8, win, CurrentTime); 596 drawmenu(); 597 return; 598 } 599 /* scroll up */ 600 if (ev->button == Button4 && prev) { 601 sel = curr = prev; 602 calcoffsets(); 603 drawmenu(); 604 return; 605 } 606 /* scroll down */ 607 if (ev->button == Button5 && next) { 608 sel = curr = next; 609 calcoffsets(); 610 drawmenu(); 611 return; 612 } 613 if (ev->button != Button1) 614 return; 615 if (ev->state & ~ControlMask) 616 return; 617 if (lines > 0) { 618 /* vertical list: (ctrl)left-click on item */ 619 w = mw - x; 620 for (item = curr; item != next; item = item->right) { 621 y += h; 622 if (ev->y >= y && ev->y <= (y + h)) { 623 puts(item->text); 624 if (!(ev->state & ControlMask)) 625 exit(0); 626 sel = item; 627 if (sel) { 628 sel->out = 1; 629 drawmenu(); 630 } 631 return; 632 } 633 } 634 } else if (matches) { 635 /* left-click on left arrow */ 636 x += inputw; 637 w = TEXTW("<"); 638 if (prev && curr->left) { 639 if (ev->x >= x && ev->x <= x + w) { 640 sel = curr = prev; 641 calcoffsets(); 642 drawmenu(); 643 return; 644 } 645 } 646 /* horizontal list: (ctrl)left-click on item */ 647 for (item = curr; item != next; item = item->right) { 648 x += w; 649 w = MIN(TEXTW(item->text), mw - x - TEXTW(">")); 650 if (ev->x >= x && ev->x <= x + w) { 651 puts(item->text); 652 if (!(ev->state & ControlMask)) 653 exit(0); 654 sel = item; 655 if (sel) { 656 sel->out = 1; 657 drawmenu(); 658 } 659 return; 660 } 661 } 662 /* left-click on right arrow */ 663 w = TEXTW(">"); 664 x = mw - w; 665 if (next && ev->x >= x && ev->x <= x + w) { 666 sel = curr = next; 667 calcoffsets(); 668 drawmenu(); 669 return; 670 } 671 } 672 } 673 674 static void 675 run(void) 676 { 677 XEvent ev; 678 679 while (!XNextEvent(dpy, &ev)) { 680 if (XFilterEvent(&ev, win)) 681 continue; 682 switch(ev.type) { 683 case DestroyNotify: 684 if (ev.xdestroywindow.window != win) 685 break; 686 cleanup(); 687 exit(1); 688 case ButtonPress: 689 buttonpress(&ev); 690 break; 691 case Expose: 692 if (ev.xexpose.count == 0) 693 drw_map(drw, win, 0, 0, mw, mh); 694 break; 695 case FocusIn: 696 /* regrab focus from parent window */ 697 if (ev.xfocus.window != win) 698 grabfocus(); 699 break; 700 case KeyPress: 701 keypress(&ev.xkey); 702 break; 703 case SelectionNotify: 704 if (ev.xselection.property == utf8) 705 paste(); 706 break; 707 case VisibilityNotify: 708 if (ev.xvisibility.state != VisibilityUnobscured) 709 XRaiseWindow(dpy, win); 710 break; 711 } 712 } 713 } 714 715 static void 716 setup(void) 717 { 718 int x, y, i, j; 719 unsigned int du; 720 XSetWindowAttributes swa; 721 XIM xim; 722 Window w, dw, *dws; 723 XWindowAttributes wa; 724 XClassHint ch = {"dmenu", "dmenu"}; 725 #ifdef XINERAMA 726 XineramaScreenInfo *info; 727 Window pw; 728 int a, di, n, area = 0; 729 #endif 730 /* init appearance */ 731 for (j = 0; j < SchemeLast; j++) 732 scheme[j] = drw_scm_create(drw, colors[j], 2); 733 734 clip = XInternAtom(dpy, "CLIPBOARD", False); 735 utf8 = XInternAtom(dpy, "UTF8_STRING", False); 736 737 /* calculate menu geometry */ 738 bh = drw->fonts->h + 2; 739 lines = MAX(lines, 0); 740 mh = (lines + 1) * bh; 741 #ifdef XINERAMA 742 i = 0; 743 if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) { 744 XGetInputFocus(dpy, &w, &di); 745 if (mon >= 0 && mon < n) 746 i = mon; 747 else if (w != root && w != PointerRoot && w != None) { 748 /* find top-level window containing current input focus */ 749 do { 750 if (XQueryTree(dpy, (pw = w), &dw, &w, &dws, &du) && dws) 751 XFree(dws); 752 } while (w != root && w != pw); 753 /* find xinerama screen with which the window intersects most */ 754 if (XGetWindowAttributes(dpy, pw, &wa)) 755 for (j = 0; j < n; j++) 756 if ((a = INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j])) > area) { 757 area = a; 758 i = j; 759 } 760 } 761 /* no focused window is on screen, so use pointer location instead */ 762 if (mon < 0 && !area && XQueryPointer(dpy, root, &dw, &dw, &x, &y, &di, &di, &du)) 763 for (i = 0; i < n; i++) 764 if (INTERSECT(x, y, 1, 1, info[i]) != 0) 765 break; 766 767 x = info[i].x_org; 768 y = info[i].y_org + (topbar ? 0 : info[i].height - mh); 769 mw = info[i].width; 770 XFree(info); 771 } else 772 #endif 773 { 774 if (!XGetWindowAttributes(dpy, parentwin, &wa)) 775 die("could not get embedding window attributes: 0x%lx", 776 parentwin); 777 x = 0; 778 y = topbar ? 0 : wa.height - mh; 779 mw = wa.width; 780 } 781 promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0; 782 inputw = MIN(inputw, mw/3); 783 match(); 784 785 /* create menu window */ 786 swa.override_redirect = True; 787 swa.background_pixel = scheme[SchemeNorm][ColBg].pixel; 788 swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask | ButtonPressMask; 789 win = XCreateWindow(dpy, parentwin, x, y, mw, mh, 0, 790 CopyFromParent, CopyFromParent, CopyFromParent, 791 CWOverrideRedirect | CWBackPixel | CWEventMask, &swa); 792 XSetClassHint(dpy, win, &ch); 793 794 795 /* input methods */ 796 if ((xim = XOpenIM(dpy, NULL, NULL, NULL)) == NULL) 797 die("XOpenIM failed: could not open input device"); 798 799 xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, 800 XNClientWindow, win, XNFocusWindow, win, NULL); 801 802 XMapRaised(dpy, win); 803 if (embed) { 804 XSelectInput(dpy, parentwin, FocusChangeMask | SubstructureNotifyMask); 805 if (XQueryTree(dpy, parentwin, &dw, &w, &dws, &du) && dws) { 806 for (i = 0; i < du && dws[i] != win; ++i) 807 XSelectInput(dpy, dws[i], FocusChangeMask); 808 XFree(dws); 809 } 810 grabfocus(); 811 } 812 drw_resize(drw, mw, mh); 813 drawmenu(); 814 } 815 816 static void 817 usage(void) 818 { 819 fputs("usage: dmenu [-bfivP] [-l lines] [-p prompt] [-fn font] [-m monitor]\n" 820 " [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr); 821 exit(1); 822 } 823 824 int 825 main(int argc, char *argv[]) 826 { 827 XWindowAttributes wa; 828 int i, fast = 0; 829 830 for (i = 1; i < argc; i++) 831 /* these options take no arguments */ 832 if (!strcmp(argv[i], "-v")) { /* prints version information */ 833 puts("dmenu-"VERSION); 834 exit(0); 835 } else if (!strcmp(argv[i], "-b")) /* appears at the bottom of the screen */ 836 topbar = 0; 837 else if (!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */ 838 fast = 1; 839 else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */ 840 fstrncmp = strncasecmp; 841 fstrstr = cistrstr; 842 } else if (!strcmp(argv[i], "-P")) /* is the input a password */ 843 passwd = 1; 844 else if (i + 1 == argc) 845 usage(); 846 /* these options take one argument */ 847 else if (!strcmp(argv[i], "-l")) /* number of lines in vertical list */ 848 lines = atoi(argv[++i]); 849 else if (!strcmp(argv[i], "-m")) 850 mon = atoi(argv[++i]); 851 else if (!strcmp(argv[i], "-p")) /* adds prompt to left of input field */ 852 prompt = argv[++i]; 853 else if (!strcmp(argv[i], "-fn")) /* font or font set */ 854 fonts[0] = argv[++i]; 855 else if (!strcmp(argv[i], "-nb")) /* normal background color */ 856 colors[SchemeNorm][ColBg] = argv[++i]; 857 else if (!strcmp(argv[i], "-nf")) /* normal foreground color */ 858 colors[SchemeNorm][ColFg] = argv[++i]; 859 else if (!strcmp(argv[i], "-sb")) /* selected background color */ 860 colors[SchemeSel][ColBg] = argv[++i]; 861 else if (!strcmp(argv[i], "-sf")) /* selected foreground color */ 862 colors[SchemeSel][ColFg] = argv[++i]; 863 else if (!strcmp(argv[i], "-w")) /* embedding window id */ 864 embed = argv[++i]; 865 else 866 usage(); 867 868 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) 869 fputs("warning: no locale support\n", stderr); 870 if (!(dpy = XOpenDisplay(NULL))) 871 die("cannot open display"); 872 screen = DefaultScreen(dpy); 873 root = RootWindow(dpy, screen); 874 if (!embed || !(parentwin = strtol(embed, NULL, 0))) 875 parentwin = root; 876 if (!XGetWindowAttributes(dpy, parentwin, &wa)) 877 die("could not get embedding window attributes: 0x%lx", 878 parentwin); 879 drw = drw_create(dpy, screen, root, wa.width, wa.height); 880 if (!drw_fontset_create(drw, fonts, LENGTH(fonts))) 881 die("no fonts could be loaded."); 882 lrpad = drw->fonts->h; 883 884 #ifdef __OpenBSD__ 885 if (pledge("stdio rpath", NULL) == -1) 886 die("pledge"); 887 #endif 888 889 if (fast && !isatty(0)) { 890 grabkeyboard(); 891 readstdin(); 892 } else { 893 readstdin(); 894 grabkeyboard(); 895 } 896 setup(); 897 run(); 898 899 return 1; /* unreachable */ 900 }