fixed offsets, updated eprint, cleaned up
6c1d0e4d
4 file(s) · +42 −33
| 164 | 164 | FILE *fp; |
|
| 165 | 165 | char *s; |
|
| 166 | 166 | if(!(fp = popen("sselp", "r"))) |
|
| 167 | - | eprint("dinput: cannot popen sselp\n"); |
|
| 167 | + | eprint("cannot popen sselp\n"); |
|
| 168 | 168 | s = fgets(buf, sizeof buf, fp); |
|
| 169 | 169 | pclose(fp); |
|
| 170 | 170 | if(s == NULL) |
|
| 322 | 322 | Bool topbar = True; |
|
| 323 | 323 | ||
| 324 | 324 | /* command line args */ |
|
| 325 | + | progname = argv[0]; |
|
| 325 | 326 | for(i = 1; i < argc; i++) |
|
| 326 | 327 | if(!strcmp(argv[i], "-b")) |
|
| 327 | 328 | topbar = False; |
|
| 356 | 357 | if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) |
|
| 357 | 358 | fprintf(stderr, "dinput: warning: no locale support\n"); |
|
| 358 | 359 | if(!(dpy = XOpenDisplay(NULL))) |
|
| 359 | - | eprint("dinput: cannot open display\n"); |
|
| 360 | + | eprint("cannot open display\n"); |
|
| 360 | 361 | screen = DefaultScreen(dpy); |
|
| 361 | 362 | if(!parent) |
|
| 362 | 363 | parent = RootWindow(dpy, screen); |
|
| 34 | 34 | static void calcoffsetsv(void); |
|
| 35 | 35 | static char *cistrstr(const char *s, const char *sub); |
|
| 36 | 36 | static void cleanup(void); |
|
| 37 | + | static void dinput(void); |
|
| 37 | 38 | static void drawmenu(void); |
|
| 38 | 39 | static void drawmenuh(void); |
|
| 39 | 40 | static void drawmenuv(void); |
|
| 89 | 90 | calcoffsetsh(void) { |
|
| 90 | 91 | unsigned int w; |
|
| 91 | 92 | ||
| 92 | - | if(!curr) |
|
| 93 | - | return; |
|
| 94 | - | w = promptw + cmdw + 2 * spaceitem; |
|
| 95 | - | for(next = curr; next && w < mw; next=next->right) |
|
| 96 | - | w += MIN(textw(next->text), mw / 3); |
|
| 97 | - | w = promptw + cmdw + 2 * spaceitem; |
|
| 98 | - | for(prev = curr; prev && prev->left && w < mw; prev=prev->left) |
|
| 99 | - | w += MIN(textw(prev->left->text), mw / 3); |
|
| 93 | + | w = promptw + cmdw + (2 * spaceitem); |
|
| 94 | + | for(next = curr; next; next = next->right) |
|
| 95 | + | if((w += MIN(textw(next->text), mw / 3)) > mw) |
|
| 96 | + | break; |
|
| 97 | + | w = promptw + cmdw + (2 * spaceitem); |
|
| 98 | + | for(prev = curr; prev && prev->left; prev = prev->left) |
|
| 99 | + | if((w += MIN(textw(prev->left->text), mw / 3)) > mw) |
|
| 100 | + | break; |
|
| 100 | 101 | } |
|
| 101 | 102 | ||
| 102 | 103 | void |
|
| 103 | 104 | calcoffsetsv(void) { |
|
| 104 | - | unsigned int h; |
|
| 105 | + | unsigned int i; |
|
| 105 | 106 | ||
| 106 | - | if(!curr) |
|
| 107 | - | return; |
|
| 108 | - | h = (dc.font.height + 2) * lines; |
|
| 109 | - | for(next = curr; next && h > 0; next = next->right) |
|
| 110 | - | h -= dc.font.height + 2; |
|
| 111 | - | h = (dc.font.height + 2) * lines; |
|
| 112 | - | for(prev = curr; prev && prev->left && h > 0; prev = prev->left) |
|
| 113 | - | h -= dc.font.height + 2; |
|
| 107 | + | next = prev = curr; |
|
| 108 | + | for(i = 0; i < lines && next; i++) |
|
| 109 | + | next = next->right; |
|
| 110 | + | for(i = 0; i < lines && prev && prev->left; i++) |
|
| 111 | + | prev = prev->left; |
|
| 114 | 112 | } |
|
| 115 | 113 | ||
| 116 | 114 | char * |
|
| 148 | 146 | drawcleanup(); |
|
| 149 | 147 | XDestroyWindow(dpy, win); |
|
| 150 | 148 | XUngrabKeyboard(dpy, CurrentTime); |
|
| 149 | + | } |
|
| 150 | + | ||
| 151 | + | void |
|
| 152 | + | dinput(void) { |
|
| 153 | + | cleanup(); |
|
| 154 | + | execlp("dinput", "dinput", text, NULL); /* todo: argv */ |
|
| 155 | + | eprint("cannot exec dinput\n"); |
|
| 151 | 156 | } |
|
| 152 | 157 | ||
| 153 | 158 | void |
|
| 290 | 295 | match(text); |
|
| 291 | 296 | break; |
|
| 292 | 297 | case XK_x: |
|
| 293 | - | execlp("dinput", "dinput", text, NULL); /* todo: argv */ |
|
| 294 | - | eprint("dmenu: cannot exec dinput:"); |
|
| 298 | + | dinput(); |
|
| 295 | 299 | break; |
|
| 296 | 300 | } |
|
| 297 | 301 | } |
|
| 369 | 373 | } |
|
| 370 | 374 | break; |
|
| 371 | 375 | case XK_Tab: |
|
| 372 | - | if(!sel) |
|
| 373 | - | return; |
|
| 374 | - | strncpy(text, sel->text, sizeof text); |
|
| 375 | - | match(text); |
|
| 376 | + | if(sel) |
|
| 377 | + | strncpy(text, sel->text, sizeof text); |
|
| 378 | + | dinput(); |
|
| 376 | 379 | break; |
|
| 377 | 380 | } |
|
| 378 | 381 | drawmenu(); |
|
| 431 | 434 | if(buf[len-1] == '\n') |
|
| 432 | 435 | buf[--len] = '\0'; |
|
| 433 | 436 | if(!(p = strdup(buf))) |
|
| 434 | - | eprint("dmenu: cannot strdup %u bytes\n", len); |
|
| 437 | + | eprint("cannot strdup %u bytes\n", len); |
|
| 435 | 438 | if((max = MAX(max, len)) == len) |
|
| 436 | 439 | maxname = p; |
|
| 437 | 440 | if(!(new = malloc(sizeof *new))) |
|
| 438 | - | eprint("dmenu: cannot malloc %u bytes\n", sizeof *new); |
|
| 441 | + | eprint("cannot malloc %u bytes\n", sizeof *new); |
|
| 439 | 442 | new->next = new->left = new->right = NULL; |
|
| 440 | 443 | new->text = p; |
|
| 441 | 444 | if(!i) |
|
| 544 | 547 | Bool topbar = True; |
|
| 545 | 548 | ||
| 546 | 549 | /* command line args */ |
|
| 550 | + | progname = argv[0]; |
|
| 547 | 551 | for(i = 1; i < argc; i++) |
|
| 548 | 552 | if(!strcmp(argv[i], "-i")) { |
|
| 549 | 553 | fstrncmp = strncasecmp; |
|
| 585 | 589 | if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) |
|
| 586 | 590 | fprintf(stderr, "dmenu: warning: no locale support\n"); |
|
| 587 | 591 | if(!(dpy = XOpenDisplay(NULL))) |
|
| 588 | - | eprint("dmenu: cannot open display\n"); |
|
| 592 | + | eprint("cannot open display\n"); |
|
| 589 | 593 | screen = DefaultScreen(dpy); |
|
| 590 | 594 | if(!parent) |
|
| 591 | 595 | parent = RootWindow(dpy, screen); |
|
| 13 | 13 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) |
|
| 14 | 14 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) |
|
| 15 | 15 | ||
| 16 | + | /* variables */ |
|
| 17 | + | char *progname; |
|
| 18 | + | ||
| 16 | 19 | void |
|
| 17 | 20 | drawcleanup(void) { |
|
| 18 | 21 | if(dc.font.set) |
|
| 71 | 74 | eprint(const char *errstr, ...) { |
|
| 72 | 75 | va_list ap; |
|
| 73 | 76 | ||
| 77 | + | fprintf(stderr, "%s: ", progname); |
|
| 74 | 78 | va_start(ap, errstr); |
|
| 75 | 79 | vfprintf(stderr, errstr, ap); |
|
| 76 | 80 | va_end(ap); |
|
| 83 | 87 | XColor color; |
|
| 84 | 88 | ||
| 85 | 89 | if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color)) |
|
| 86 | - | eprint("drawtext: cannot allocate color '%s'\n", colstr); |
|
| 90 | + | eprint("cannot allocate color '%s'\n", colstr); |
|
| 87 | 91 | return color.pixel; |
|
| 88 | 92 | } |
|
| 89 | 93 | ||
| 92 | 96 | char *def, **missing = NULL; |
|
| 93 | 97 | int i, n; |
|
| 94 | 98 | ||
| 95 | - | if(!fontstr || fontstr[0] == '\0') |
|
| 96 | - | eprint("drawtext: cannot load font: '%s'\n", fontstr); |
|
| 99 | + | if(!fontstr || !*fontstr) |
|
| 100 | + | eprint("cannot load null font\n"); |
|
| 97 | 101 | dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); |
|
| 98 | 102 | if(missing) |
|
| 99 | 103 | XFreeStringList(missing); |
|
| 111 | 115 | else { |
|
| 112 | 116 | if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)) |
|
| 113 | 117 | && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed"))) |
|
| 114 | - | eprint("drawtext: cannot load font: '%s'\n", fontstr); |
|
| 118 | + | eprint("cannot load font '%s'\n", fontstr); |
|
| 115 | 119 | dc.font.ascent = dc.font.xfont->ascent; |
|
| 116 | 120 | dc.font.descent = dc.font.xfont->descent; |
|
| 117 | 121 | } |
|
| 30 | 30 | int textw(const char *text); |
|
| 31 | 31 | ||
| 32 | 32 | /* variables */ |
|
| 33 | + | extern char *progname; |
|
| 33 | 34 | extern Display *dpy; |
|
| 34 | 35 | extern DC dc; |
|
| 35 | 36 | extern int screen; |
|
| 36 | 37 | extern unsigned int mw, mh; |
|
| 37 | 38 | extern Window parent; |
|
| 38 | 39 | ||
| 39 | - | /* style */ |
|
| 40 | 40 | extern const char *font; |
|
| 41 | 41 | extern const char *normbgcolor; |
|
| 42 | 42 | extern const char *normfgcolor; |