add lots of comments 7b1493a6
Connor Lane Smith · 2011-10-26 13:20 1 file(s) · +33 −18
dmenu.c +33 −18
70 70
	int i;
71 71
72 72
	for(i = 1; i < argc; i++)
73 -
		/* single flags */
74 -
		if(!strcmp(argv[i], "-v")) {
73 +
		/* these options take no arguments */
74 +
		if(!strcmp(argv[i], "-v")) {      /* prints version information */
75 75
			puts("dmenu-"VERSION", © 2006-2011 dmenu engineers, see LICENSE for details");
76 76
			exit(EXIT_SUCCESS);
77 77
		}
78 -
		else if(!strcmp(argv[i], "-b"))
78 +
		else if(!strcmp(argv[i], "-b"))   /* appears at the bottom of the screen */
79 79
			topbar = False;
80 -
		else if(!strcmp(argv[i], "-f"))
80 +
		else if(!strcmp(argv[i], "-f"))   /* grabs keyboard before reading stdin */
81 81
			fast = True;
82 -
		else if(!strcmp(argv[i], "-i")) {
82 +
		else if(!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
83 83
			fstrncmp = strncasecmp;
84 84
			fstrstr = cistrstr;
85 85
		}
86 86
		else if(i+1 == argc)
87 87
			usage();
88 -
		/* double flags */
89 -
		else if(!strcmp(argv[i], "-l"))
88 +
		/* these options take one argument */
89 +
		else if(!strcmp(argv[i], "-l"))   /* number of lines in vertical list */
90 90
			lines = atoi(argv[++i]);
91 -
		else if(!strcmp(argv[i], "-p"))
91 +
		else if(!strcmp(argv[i], "-p"))   /* adds prompt to left of input field */
92 92
			prompt = argv[++i];
93 -
		else if(!strcmp(argv[i], "-fn"))
93 +
		else if(!strcmp(argv[i], "-fn"))  /* font or font set */
94 94
			font = argv[++i];
95 -
		else if(!strcmp(argv[i], "-nb"))
95 +
		else if(!strcmp(argv[i], "-nb"))  /* normal background color */
96 96
			normbgcolor = argv[++i];
97 -
		else if(!strcmp(argv[i], "-nf"))
97 +
		else if(!strcmp(argv[i], "-nf"))  /* normal foreground color */
98 98
			normfgcolor = argv[++i];
99 -
		else if(!strcmp(argv[i], "-sb"))
99 +
		else if(!strcmp(argv[i], "-sb"))  /* selected background color */
100 100
			selbgcolor = argv[++i];
101 -
		else if(!strcmp(argv[i], "-sf"))
101 +
		else if(!strcmp(argv[i], "-sf"))  /* selected foreground color */
102 102
			selfgcolor = argv[++i];
103 103
		else
104 104
			usage();
140 140
		n = lines * bh;
141 141
	else
142 142
		n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">"));
143 -
143 +
	/* calculate which items will begin the next page and previous page */
144 144
	for(i = 0, next = curr; next; next = next->right)
145 145
		if((i += (lines > 0) ? bh : MIN(textw(dc, next->text), n)) > n)
146 146
			break;
174 174
		drawtext(dc, prompt, selcol);
175 175
		dc->x = dc->w;
176 176
	}
177 +
	/* draw input field */
177 178
	dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw;
178 179
	drawtext(dc, text, normcol);
179 180
	if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w)
180 181
		drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol));
181 182
182 183
	if(lines > 0) {
184 +
		/* draw vertical list */
183 185
		dc->w = mw - dc->x;
184 186
		for(item = curr; item != next; item = item->right) {
185 187
			dc->y += dc->h;
187 189
		}
188 190
	}
189 191
	else if(matches) {
192 +
		/* draw horizontal list */
190 193
		dc->x += inputw;
191 194
		dc->w = textw(dc, "<");
192 195
		if(curr->left)
208 211
grabkeyboard(void) {
209 212
	int i;
210 213
214 +
	/* try to grab keyboard, we may have to wait for another process to ungrab */
211 215
	for(i = 0; i < 1000; i++) {
212 216
		if(XGrabKeyboard(dc->dpy, DefaultRootWindow(dc->dpy), True,
213 217
		                 GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
221 225
insert(const char *str, ssize_t n) {
222 226
	if(strlen(text) + n > sizeof text - 1)
223 227
		return;
228 +
	/* move existing text out of the way, insert new text, and update cursor */
224 229
	memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0));
225 230
	if(n > 0)
226 231
		memcpy(&text[cursor], str, n);
297 302
			break;
298 303
		}
299 304
		if(next) {
305 +
			/* jump to end of list and position items in reverse */
300 306
			curr = matchend;
301 307
			calcoffsets();
302 308
			curr = prev;
378 384
	Item *item, *lprefix, *lsubstr, *prefixend, *substrend;
379 385
380 386
	strcpy(buf, text);
387 +
	/* separate input text into tokens to be matched individually */
381 388
	for(s = strtok(buf, " "); s; tokv[tokc-1] = s, s = strtok(NULL, " "))
382 389
		if(++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv)))
383 390
			eprintf("cannot realloc %u bytes\n", tokn * sizeof *tokv);
388 395
		for(i = 0; i < tokc; i++)
389 396
			if(!fstrstr(item->text, tokv[i]))
390 397
				break;
391 -
		if(i != tokc)
398 +
		if(i != tokc) /* not all tokens match */
392 399
			continue;
400 +
		/* exact matches go first, then prefixes, then substrings */
393 401
		if(!tokc || !fstrncmp(tokv[0], item->text, len+1))
394 402
			appenditem(item, &matches, &matchend);
395 403
		else if(!fstrncmp(tokv[0], item->text, len))
423 431
nextrune(int inc) {
424 432
	ssize_t n;
425 433
434 +
	/* return location of next utf8 rune in the given direction (+1 or -1) */
426 435
	for(n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc);
427 436
	return n;
428 437
}
434 443
	unsigned long dl;
435 444
	Atom da;
436 445
446 +
	/* we have been given the current selection, now insert it into input */
437 447
	XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
438 448
	                   utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
439 449
	insert(p, (q = strchr(p, '\n')) ? q-p : (ssize_t)strlen(p));
446 456
	char buf[sizeof text], *p, *maxstr = NULL;
447 457
	size_t i, max = 0, size = 0;
448 458
459 +
	/* read each line from stdin and add it to the item list */
449 460
	for(i = 0; fgets(buf, sizeof buf, stdin); i++) {
450 461
		if(i+1 >= size / sizeof *items)
451 462
			if(!(items = realloc(items, (size += BUFSIZ))))
508 519
509 520
	utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
510 521
511 -
	/* menu geometry */
522 +
	/* calculate menu geometry */
512 523
	bh = dc->font.height + 2;
513 524
	lines = MAX(lines, 0);
514 525
	mh = (lines + 1) * bh;
521 532
522 533
		XGetInputFocus(dc->dpy, &w, &di);
523 534
		if(w != root && w != PointerRoot && w != None) {
535 +
			/* find top-level window containing current input focus */
524 536
			do {
525 537
				if(XQueryTree(dc->dpy, (pw = w), &dw, &w, &dws, &du) && dws)
526 538
					XFree(dws);
527 539
			} while(w != root && w != pw);
540 +
			/* find xinerama screen with which the window intersects most */
528 541
			if(XGetWindowAttributes(dc->dpy, pw, &wa))
529 542
				for(j = 0; j < n; j++)
530 543
					if((a = INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j])) > area) {
532 545
						i = j;
533 546
					}
534 547
		}
548 +
		/* no focused window is on screen, so use pointer location instead */
535 549
		if(!area && XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du))
536 550
			for(i = 0; i < n; i++)
537 551
				if(INTERSECT(x, y, 1, 1, info[i]))
538 552
					break;
553 +
539 554
		x = info[i].x_org;
540 555
		y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
541 556
		mw = info[i].width;
552 567
	inputw = MIN(inputw, mw/3);
553 568
	match();
554 569
555 -
	/* menu window */
570 +
	/* create menu window */
556 571
	swa.override_redirect = True;
557 572
	swa.background_pixmap = ParentRelative;
558 573
	swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
561 576
	                    DefaultVisual(dc->dpy, screen),
562 577
	                    CWOverrideRedirect | CWBackPixmap | CWEventMask, &swa);
563 578
564 -
	/* input methods */
579 +
	/* open input methods */
565 580
	xim = XOpenIM(dc->dpy, NULL, NULL, NULL);
566 581
	xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
567 582
	                XNClientWindow, win, XNFocusWindow, win, NULL);