diff options
| -rw-r--r-- | dmenu.c | 83 |
1 files changed, 32 insertions, 51 deletions
| @@ -118,18 +118,14 @@ calcoffsetsh(void) { | |||
| 118 | return; | 118 | return; |
| 119 | w = promptw + cmdw + 2 * spaceitem; | 119 | w = promptw + cmdw + 2 * spaceitem; |
| 120 | for(next = curr; next; next=next->right) { | 120 | for(next = curr; next; next=next->right) { |
| 121 | tw = textw(next->text); | 121 | tw = MIN(textw(next->text), mw / 3); |
| 122 | if(tw > mw / 3) | ||
| 123 | tw = mw / 3; | ||
| 124 | w += tw; | 122 | w += tw; |
| 125 | if(w > mw) | 123 | if(w > mw) |
| 126 | break; | 124 | break; |
| 127 | } | 125 | } |
| 128 | w = promptw + cmdw + 2 * spaceitem; | 126 | w = promptw + cmdw + 2 * spaceitem; |
| 129 | for(prev = curr; prev && prev->left; prev=prev->left) { | 127 | for(prev = curr; prev && prev->left; prev=prev->left) { |
| 130 | tw = textw(prev->left->text); | 128 | tw = MIN(textw(prev->left->text), mw / 3); |
| 131 | if(tw > mw / 3) | ||
| 132 | tw = mw / 3; | ||
| 133 | w += tw; | 129 | w += tw; |
| 134 | if(w > mw) | 130 | if(w > mw) |
| 135 | break; | 131 | break; |
| @@ -138,20 +134,20 @@ calcoffsetsh(void) { | |||
| 138 | 134 | ||
| 139 | void | 135 | void |
| 140 | calcoffsetsv(void) { | 136 | calcoffsetsv(void) { |
| 141 | static unsigned int w; | 137 | static unsigned int h; |
| 142 | 138 | ||
| 143 | if(!curr) | 139 | if(!curr) |
| 144 | return; | 140 | return; |
| 145 | w = (dc.font.height + 2) * (lines + 1); | 141 | h = (dc.font.height + 2) * (lines + 1); |
| 146 | for(next = curr; next; next=next->right) { | 142 | for(next = curr; next; next=next->right) { |
| 147 | w -= dc.font.height + 2; | 143 | h -= dc.font.height + 2; |
| 148 | if(w <= 0) | 144 | if(h <= 0) |
| 149 | break; | 145 | break; |
| 150 | } | 146 | } |
| 151 | w = (dc.font.height + 2) * (lines + 1); | 147 | h = (dc.font.height + 2) * (lines + 1); |
| 152 | for(prev = curr; prev && prev->left; prev=prev->left) { | 148 | for(prev = curr; prev && prev->left; prev=prev->left) { |
| 153 | w -= dc.font.height + 2; | 149 | h -= dc.font.height + 2; |
| 154 | if(w <= 0) | 150 | if(h <= 0) |
| 155 | break; | 151 | break; |
| 156 | } | 152 | } |
| 157 | } | 153 | } |
| @@ -352,10 +348,8 @@ initfont(const char *fontstr) { | |||
| 352 | font_extents = XExtentsOfFontSet(dc.font.set); | 348 | font_extents = XExtentsOfFontSet(dc.font.set); |
| 353 | n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names); | 349 | n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names); |
| 354 | for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) { | 350 | for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) { |
| 355 | if(dc.font.ascent < (*xfonts)->ascent) | 351 | dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent); |
| 356 | dc.font.ascent = (*xfonts)->ascent; | 352 | dc.font.descent = MAX(dc.font.descent, (*xfonts)->descent); |
| 357 | if(dc.font.descent < (*xfonts)->descent) | ||
| 358 | dc.font.descent = (*xfonts)->descent; | ||
| 359 | xfonts++; | 353 | xfonts++; |
| 360 | } | 354 | } |
| 361 | } | 355 | } |
| @@ -396,16 +390,16 @@ kpress(XKeyEvent * e) { | |||
| 396 | return; | 390 | return; |
| 397 | case XK_a: | 391 | case XK_a: |
| 398 | case XK_A: | 392 | case XK_A: |
| 399 | cursor = 0; | 393 | ksym = XK_Home; |
| 400 | break; | ||
| 401 | case XK_e: | ||
| 402 | case XK_E: | ||
| 403 | cursor = strlen(text); | ||
| 404 | break; | 394 | break; |
| 405 | case XK_c: | 395 | case XK_c: |
| 406 | case XK_C: | 396 | case XK_C: |
| 407 | ksym = XK_Escape; | 397 | ksym = XK_Escape; |
| 408 | break; | 398 | break; |
| 399 | case XK_e: | ||
| 400 | case XK_E: | ||
| 401 | ksym = XK_End; | ||
| 402 | break; | ||
| 409 | case XK_h: | 403 | case XK_h: |
| 410 | case XK_H: | 404 | case XK_H: |
| 411 | ksym = XK_BackSpace; | 405 | ksym = XK_BackSpace; |
| @@ -429,7 +423,7 @@ kpress(XKeyEvent * e) { | |||
| 429 | i = cursor; | 423 | i = cursor; |
| 430 | while(i-- > 0 && text[i] == ' '); | 424 | while(i-- > 0 && text[i] == ' '); |
| 431 | while(i-- > 0 && text[i] != ' '); | 425 | while(i-- > 0 && text[i] != ' '); |
| 432 | memmove(text + i + 1, text + cursor, sizeof text - cursor); | 426 | memmove(text + i + 1, text + cursor, sizeof text - cursor + 1); |
| 433 | cursor = i + 1; | 427 | cursor = i + 1; |
| 434 | match(text); | 428 | match(text); |
| 435 | } | 429 | } |
| @@ -460,12 +454,12 @@ kpress(XKeyEvent * e) { | |||
| 460 | case XK_p: | 454 | case XK_p: |
| 461 | { | 455 | { |
| 462 | FILE *fp; | 456 | FILE *fp; |
| 463 | char *c; | 457 | char *s; |
| 464 | if(!(fp = (FILE*)popen("sselp", "r"))) | 458 | if(!(fp = (FILE*)popen("sselp", "r"))) |
| 465 | eprint("dmenu: Could not popen sselp\n"); | 459 | eprint("dmenu: Could not popen sselp\n"); |
| 466 | c = fgets(buf, sizeof buf, fp); | 460 | s = fgets(buf, sizeof buf, fp); |
| 467 | pclose(fp); | 461 | pclose(fp); |
| 468 | if(c == NULL) | 462 | if(s == NULL) |
| 469 | return; | 463 | return; |
| 470 | } | 464 | } |
| 471 | num = strlen(buf); | 465 | num = strlen(buf); |
| @@ -621,32 +615,25 @@ match(char *pattern) { | |||
| 621 | 615 | ||
| 622 | void | 616 | void |
| 623 | readstdin(void) { | 617 | readstdin(void) { |
| 624 | char *p, buf[1024]; | 618 | char *p, buf[sizeof text]; |
| 625 | unsigned int len = 0, blen = 0, max = 0; | 619 | unsigned int len = 0, max = 0; |
| 626 | Item *i, *new; | 620 | Item *i, *new; |
| 627 | 621 | ||
| 628 | i = 0, p = NULL; | 622 | i = NULL; |
| 629 | while(fgets(buf, sizeof buf, stdin)) { | 623 | while(fgets(buf, sizeof buf, stdin)) { |
| 630 | len += (blen = strlen(buf)); | 624 | len = strlen(buf); |
| 631 | if(!(p = realloc(p, len))) { | 625 | if(buf[len-1] == '\n') |
| 632 | eprint("fatal: could not realloc() %u bytes\n", len); | 626 | buf[--len] = '\0'; |
| 633 | return; | 627 | if(!(p = strdup(buf))) |
| 634 | } | 628 | eprint("fatal: could not strdup() %u bytes\n", len); |
| 635 | memcpy (p + len - blen, buf, blen); | ||
| 636 | if (p[len - 1] == '\n') | ||
| 637 | p[len - 1] = 0; | ||
| 638 | else if (!feof(stdin)) | ||
| 639 | continue; | ||
| 640 | if(max < len) { | 629 | if(max < len) { |
| 641 | maxname = p; | 630 | maxname = p; |
| 642 | max = len; | 631 | max = len; |
| 643 | } | 632 | } |
| 644 | len = 0; | ||
| 645 | if(!(new = (Item *)malloc(sizeof(Item)))) | 633 | if(!(new = (Item *)malloc(sizeof(Item)))) |
| 646 | eprint("fatal: could not malloc() %u bytes\n", sizeof(Item)); | 634 | eprint("fatal: could not malloc() %u bytes\n", sizeof(Item)); |
| 647 | new->next = new->left = new->right = NULL; | 635 | new->next = new->left = new->right = NULL; |
| 648 | new->text = p; | 636 | new->text = p; |
| 649 | p = NULL; | ||
| 650 | if(!i) | 637 | if(!i) |
| 651 | allitems = new; | 638 | allitems = new; |
| 652 | else | 639 | else |
| @@ -812,8 +799,8 @@ main(int argc, char *argv[]) { | |||
| 812 | else if(!strcmp(argv[i], "-v")) | 799 | else if(!strcmp(argv[i], "-v")) |
| 813 | eprint("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n"); | 800 | eprint("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n"); |
| 814 | else | 801 | else |
| 815 | eprint("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>] [-nf <color>]\n" | 802 | eprint("usage: dmenu [-i] [-b] [-e <xid>] [-l <lines>] [-fn <font>] [-nb <color>]\n" |
| 816 | " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n"); | 803 | " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n"); |
| 817 | if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) | 804 | if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) |
| 818 | fprintf(stderr, "warning: no locale support\n"); | 805 | fprintf(stderr, "warning: no locale support\n"); |
| 819 | if(!(dpy = XOpenDisplay(NULL))) | 806 | if(!(dpy = XOpenDisplay(NULL))) |
| @@ -822,14 +809,8 @@ main(int argc, char *argv[]) { | |||
| 822 | if(!root) | 809 | if(!root) |
| 823 | root = RootWindow(dpy, screen); | 810 | root = RootWindow(dpy, screen); |
| 824 | 811 | ||
| 825 | if(isatty(STDIN_FILENO)) { | 812 | readstdin(); |
| 826 | readstdin(); | 813 | running = grabkeyboard(); |
| 827 | running = grabkeyboard(); | ||
| 828 | } | ||
| 829 | else { /* prevent keypress loss */ | ||
| 830 | running = grabkeyboard(); | ||
| 831 | readstdin(); | ||
| 832 | } | ||
| 833 | 814 | ||
| 834 | setup(topbar); | 815 | setup(topbar); |
| 835 | drawmenu(); | 816 | drawmenu(); |
