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