dmenu

my build of dmenu
git clone git://git.hanetzok.net/dmenu
Log | Files | Refs | README | LICENSE

dmenu.c (23324B)


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