chamesle

chamesle.org: the site of Chamesle, a small Linux distribution
git clone https://github.com/tortr-rs/chamesle
Log | Files | Refs | README

launchel.c (17100B)


      1 /* launchel - one line to type what you want to run, like window maker's */
      2 #include <ctype.h>
      3 #include <dirent.h>
      4 #include <locale.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 #include <sys/stat.h>
     12 #include <sys/wait.h>
     13 #include <X11/Xlib.h>
     14 #include <X11/Xutil.h>
     15 #include <X11/keysym.h>
     16 #include <X11/Xft/Xft.h>
     17 
     18 #define LENGTH(X)       (sizeof(X) / sizeof(X)[0])
     19 #define MIN(A, B)       ((A) < (B) ? (A) : (B))
     20 #define MAX(A, B)       ((A) > (B) ? (A) : (B))
     21 #define MAXITEMS        8192
     22 
     23 typedef struct {
     24 	char *name;	/* what you see and type */
     25 	char *cmd;	/* what runs */
     26 	int used;	/* how often, from the history */
     27 	int app;	/* from a .desktop file */
     28 } Item;
     29 
     30 /* settings, from ~/.config/launchel/config */
     31 static char fontname[256] = "VGA", fgname[32] = "grey", bgname[32] = "black", selname[32] = "grey";
     32 static char terminal[256] = "ttywm-term", place[16] = "bottom";
     33 static int fontsize;
     34 
     35 static const char defaultconfig[] =
     36 "# launchel settings\n"
     37 "\n"
     38 "font = VGA\n"
     39 "# nothing means ttywm's size, or 16\n"
     40 "font size =\n"
     41 "# top or bottom of the screen\n"
     42 "place = bottom\n"
     43 "# what runs programs that want a terminal, and anything you start with !\n"
     44 "terminal = ttywm-term\n"
     45 "\n"
     46 "# colours: black blue green cyan red magenta brown grey\n"
     47 "#          dark grey, light blue, light green, light cyan, light red, pink, yellow, white\n"
     48 "#          or #rrggbb\n"
     49 "text colour = grey\n"
     50 "background colour = black\n"
     51 "highlight colour = grey\n"
     52 "\n"
     53 "# type and press enter. tab and the arrows pick another match,\n"
     54 "# shift+enter runs exactly what you typed, !htop runs htop in a terminal, escape goes away\n";
     55 
     56 static Display *dpy;
     57 static Window win, root;
     58 static int scr, sw, sh, cw, ch, ascent;
     59 static XftFont *font;
     60 static XftDraw *draw;
     61 static XftColor fg, bg, hl;
     62 static Pixmap buf;
     63 static GC gc;
     64 static XIC xic;
     65 
     66 static Item items[MAXITEMS];
     67 static int nitems;
     68 static Item *match[MAXITEMS];
     69 static int nmatch, cur, first;	/* first: the leftmost match shown */
     70 static char text[1024];
     71 static size_t len;
     72 static char histpath[1100];
     73 
     74 /* ---- small things ---- */
     75 
     76 static void
     77 die(const char *msg)
     78 {
     79 	fprintf(stderr, "launchel: %s\n", msg);
     80 	exit(1);
     81 }
     82 
     83 static char *
     84 trim(char *s)
     85 {
     86 	char *e;
     87 
     88 	while (isspace((unsigned char)*s))
     89 		s++;
     90 	for (e = s + strlen(s); e > s && isspace((unsigned char)e[-1]); e--);
     91 	*e = '\0';
     92 	return s;
     93 }
     94 
     95 static void
     96 squash(char *dst, const char *src, size_t n)
     97 {
     98 	size_t i = 0;
     99 
    100 	for (; *src && i < n - 1; src++)
    101 		if (!isspace((unsigned char)*src))
    102 			dst[i++] = tolower((unsigned char)*src);
    103 	dst[i] = '\0';
    104 }
    105 
    106 static const char *
    107 colourhex(const char *name)
    108 {
    109 	static const char *vga[][2] = {
    110 		{ "black", "#000000" }, { "blue", "#0000aa" }, { "green", "#00aa00" }, { "cyan", "#00aaaa" },
    111 		{ "red", "#aa0000" }, { "magenta", "#aa00aa" }, { "brown", "#aa5500" }, { "grey", "#aaaaaa" },
    112 		{ "gray", "#aaaaaa" }, { "darkgrey", "#555555" }, { "darkgray", "#555555" },
    113 		{ "lightblue", "#5555ff" }, { "lightgreen", "#55ff55" }, { "lightcyan", "#55ffff" },
    114 		{ "lightred", "#ff5555" }, { "pink", "#ff55ff" }, { "yellow", "#ffff55" }, { "white", "#ffffff" },
    115 	};
    116 	char s[32];
    117 	unsigned int i;
    118 
    119 	squash(s, name, sizeof s);
    120 	for (i = 0; i < LENGTH(vga); i++)
    121 		if (!strcmp(s, vga[i][0]))
    122 			return vga[i][1];
    123 	return name[0] == '#' ? name : "#aaaaaa";
    124 }
    125 
    126 static void
    127 colour(const char *name, XftColor *c)
    128 {
    129 	if (!XftColorAllocName(dpy, DefaultVisual(dpy, scr), DefaultColormap(dpy, scr), colourhex(name), c))
    130 		XftColorAllocName(dpy, DefaultVisual(dpy, scr), DefaultColormap(dpy, scr), "#aaaaaa", c);
    131 }
    132 
    133 static int
    134 textw(const char *s)
    135 {
    136 	XGlyphInfo gi;
    137 
    138 	XftTextExtentsUtf8(dpy, font, (const FcChar8 *)s, strlen(s), &gi);
    139 	return gi.xOff;
    140 }
    141 
    142 /* ---- settings ---- */
    143 
    144 static void
    145 loadconfig(const char *path)
    146 {
    147 	char line[1024], key[64], *name, *value, *eq;
    148 	struct stat st;
    149 	FILE *f;
    150 
    151 	if (stat(path, &st) < 0 && (f = fopen(path, "w"))) {
    152 		fputs(defaultconfig, f);
    153 		fclose(f);
    154 	}
    155 	if (!(f = fopen(path, "r")))
    156 		return;
    157 	while (fgets(line, sizeof line, f)) {
    158 		if (*trim(line) == '#' || !(eq = strchr(line, '=')))
    159 			continue;
    160 		*eq = '\0';
    161 		name = trim(line);
    162 		value = trim(eq + 1);
    163 		squash(key, name, sizeof key);
    164 		if (!strcmp(key, "font"))
    165 			snprintf(fontname, sizeof fontname, "%s", *value ? value : "VGA");
    166 		else if (!strcmp(key, "fontsize"))
    167 			fontsize = atoi(value);
    168 		else if (!strcmp(key, "place"))
    169 			squash(place, value, sizeof place);
    170 		else if (!strcmp(key, "terminal"))
    171 			snprintf(terminal, sizeof terminal, "%s", *value ? value : "ttywm-term");
    172 		else if (!strcmp(key, "textcolour") || !strcmp(key, "textcolor"))
    173 			snprintf(fgname, sizeof fgname, "%s", value);
    174 		else if (!strcmp(key, "backgroundcolour") || !strcmp(key, "backgroundcolor"))
    175 			snprintf(bgname, sizeof bgname, "%s", value);
    176 		else if (!strcmp(key, "highlightcolour") || !strcmp(key, "highlightcolor"))
    177 			snprintf(selname, sizeof selname, "%s", value);
    178 		else
    179 			fprintf(stderr, "launchel: %s: don't know '%s'\n", path, name);
    180 	}
    181 	fclose(f);
    182 }
    183 
    184 /* ---- what there is to run ---- */
    185 
    186 static Item *
    187 find(const char *name)
    188 {
    189 	int i;
    190 
    191 	for (i = 0; i < nitems; i++)
    192 		if (!strcmp(items[i].name, name))
    193 			return &items[i];
    194 	return NULL;
    195 }
    196 
    197 static void
    198 add(const char *name, const char *cmd, int app)
    199 {
    200 	Item *it;
    201 
    202 	if (nitems == MAXITEMS || !*name || ((it = find(name)) && (it->app || !app)))
    203 		return;
    204 	if (it) {
    205 		/* an app by the same name as a program: the app wins, it knows its arguments */
    206 		free(it->cmd);
    207 		it->cmd = strdup(cmd);
    208 		it->app = app;
    209 		return;
    210 	}
    211 	items[nitems].name = strdup(name);
    212 	items[nitems].cmd = strdup(cmd);
    213 	items[nitems].app = app;
    214 	items[nitems++].used = 0;
    215 }
    216 
    217 /* Exec= without the %f and %u bits */
    218 static void
    219 cleanexec(char *dst, const char *src, size_t n)
    220 {
    221 	size_t i = 0;
    222 
    223 	for (; *src && i < n - 1; src++) {
    224 		if (*src == '%' && src[1]) {
    225 			src++;
    226 			if (*src == '%')
    227 				dst[i++] = '%';
    228 			continue;
    229 		}
    230 		dst[i++] = *src;
    231 	}
    232 	while (i > 0 && dst[i - 1] == ' ')
    233 		i--;
    234 	dst[i] = '\0';
    235 }
    236 
    237 static void
    238 readdesktop(const char *path)
    239 {
    240 	char line[2048], name[256] = "", exec[1024] = "", cmd[1400], *v;
    241 	int inentry = 0, hidden = 0, term = 0;
    242 	FILE *f;
    243 
    244 	if (!(f = fopen(path, "r")))
    245 		return;
    246 	while (fgets(line, sizeof line, f)) {
    247 		trim(line);
    248 		if (line[0] == '[') {
    249 			inentry = !strcmp(line, "[Desktop Entry]");
    250 			continue;
    251 		}
    252 		if (!inentry || !(v = strchr(line, '=')))
    253 			continue;
    254 		*v++ = '\0';
    255 		if (!strcmp(line, "Name") && !*name)
    256 			snprintf(name, sizeof name, "%s", v);
    257 		else if (!strcmp(line, "Exec"))
    258 			cleanexec(exec, v, sizeof exec);
    259 		else if ((!strcmp(line, "NoDisplay") || !strcmp(line, "Hidden")) && !strcmp(v, "true"))
    260 			hidden = 1;
    261 		else if (!strcmp(line, "Terminal") && !strcmp(v, "true"))
    262 			term = 1;
    263 		else if (!strcmp(line, "Type") && strcmp(v, "Application"))
    264 			hidden = 1;
    265 	}
    266 	fclose(f);
    267 	if (hidden || !*name || !*exec)
    268 		return;
    269 	if (term)
    270 		snprintf(cmd, sizeof cmd, "%s -e %s", terminal, exec);
    271 	else
    272 		snprintf(cmd, sizeof cmd, "%s", exec);
    273 	add(name, cmd, 1);
    274 }
    275 
    276 static void
    277 readapps(const char *dir)
    278 {
    279 	char path[2048];
    280 	struct dirent *d;
    281 	DIR *dp;
    282 	size_t n;
    283 
    284 	if (!(dp = opendir(dir)))
    285 		return;
    286 	while ((d = readdir(dp))) {
    287 		n = strlen(d->d_name);
    288 		if (n > 8 && !strcmp(d->d_name + n - 8, ".desktop")) {
    289 			snprintf(path, sizeof path, "%s/%s", dir, d->d_name);
    290 			readdesktop(path);
    291 		}
    292 	}
    293 	closedir(dp);
    294 }
    295 
    296 static void
    297 readpath(void)
    298 {
    299 	char *path = getenv("PATH"), *p, *dir, file[4096];
    300 	struct dirent *d;
    301 	struct stat st;
    302 	DIR *dp;
    303 
    304 	if (!path)
    305 		return;
    306 	p = path = strdup(path);
    307 	while ((dir = strsep(&p, ":"))) {
    308 		if (!*dir || !(dp = opendir(dir)))
    309 			continue;
    310 		while ((d = readdir(dp))) {
    311 			if (d->d_name[0] == '.')
    312 				continue;
    313 			snprintf(file, sizeof file, "%s/%s", dir, d->d_name);
    314 			if (stat(file, &st) == 0 && S_ISREG(st.st_mode) && (st.st_mode & 0111))
    315 				add(d->d_name, d->d_name, 0);
    316 		}
    317 		closedir(dp);
    318 	}
    319 	free(path);
    320 }
    321 
    322 /* the history: one name a line, the newest at the bottom */
    323 static void
    324 readhistory(void)
    325 {
    326 	char line[1100];
    327 	Item *it;
    328 	FILE *f;
    329 
    330 	if (!(f = fopen(histpath, "r")))
    331 		return;
    332 	while (fgets(line, sizeof line, f)) {
    333 		trim(line);
    334 		if ((it = find(line)))
    335 			it->used++;
    336 		else if (*line) {
    337 			/* the list can be full, then it isn't added */
    338 			add(line, line, 0);
    339 			if ((it = find(line)))
    340 				it->used = 1;
    341 		}
    342 	}
    343 	fclose(f);
    344 }
    345 
    346 static void
    347 remember(const char *name)
    348 {
    349 	char *lines[500], line[1100];
    350 	int n = 0, i;
    351 	FILE *f;
    352 
    353 	/* keep the last 500 */
    354 	if ((f = fopen(histpath, "r"))) {
    355 		while (n < (int)LENGTH(lines) && fgets(line, sizeof line, f))
    356 			lines[n++] = strdup(line);
    357 		fclose(f);
    358 	}
    359 	if (!(f = fopen(histpath, "w")))
    360 		return;
    361 	for (i = n >= (int)LENGTH(lines) ? 1 : 0; i < n; i++)
    362 		fputs(lines[i], f);
    363 	fprintf(f, "%s\n", name);
    364 	fclose(f);
    365 	for (i = 0; i < n; i++)
    366 		free(lines[i]);
    367 }
    368 
    369 /* ---- matching ---- */
    370 
    371 static int
    372 score(Item *it, const char *q)
    373 {
    374 	size_t n = strlen(q);
    375 
    376 	if (!n)
    377 		return it->used ? 1000 + it->used : -1;
    378 	if (!strncasecmp(it->name, q, n))
    379 		return 3000 + it->used * 10 + (it->app ? 5 : 0) - (int)strlen(it->name);
    380 	/* the start of a word in it: "fox" finds "Firefox" less than "fir" does */
    381 	if (strcasestr(it->name, q))
    382 		return 2000 + it->used * 10 + (it->app ? 5 : 0) - (int)strlen(it->name);
    383 	return -1;
    384 }
    385 
    386 static int scores[MAXITEMS];
    387 
    388 static int
    389 bysc(const void *a, const void *b)
    390 {
    391 	const Item *x = *(Item *const *)a, *y = *(Item *const *)b;
    392 	int d = scores[y - items] - scores[x - items];
    393 
    394 	return d ? d : strcasecmp(x->name, y->name);
    395 }
    396 
    397 static void
    398 domatch(void)
    399 {
    400 	char q[1024], *sp;
    401 	int i;
    402 
    403 	/* only the first word is matched: "firefox github.com" still finds firefox */
    404 	snprintf(q, sizeof q, "%s", text[0] == '!' ? text + 1 : text);
    405 	if ((sp = strchr(q, ' ')))
    406 		*sp = '\0';
    407 	nmatch = 0;
    408 	for (i = 0; i < nitems; i++)
    409 		if ((scores[i] = score(&items[i], q)) >= 0)
    410 			match[nmatch++] = &items[i];
    411 	qsort(match, nmatch, sizeof *match, bysc);
    412 	cur = first = 0;
    413 }
    414 
    415 /* ---- running it ---- */
    416 
    417 static void
    418 spawn(const char *cmd)
    419 {
    420 	pid_t pid;
    421 
    422 	if ((pid = fork()) == 0) {
    423 		/* twice, so it isn't ours when we're gone */
    424 		if (fork() == 0) {
    425 			setsid();
    426 			execl("/bin/sh", "sh", "-c", cmd, (char *)NULL);
    427 			_exit(127);
    428 		}
    429 		_exit(0);
    430 	}
    431 	if (pid > 0)
    432 		waitpid(pid, NULL, 0);
    433 }
    434 
    435 static void
    436 go(int exact)
    437 {
    438 	char cmd[2400], *args;
    439 	const char *t = text[0] == '!' ? text + 1 : text;
    440 	Item *it = !exact && nmatch ? match[cur] : NULL;
    441 
    442 	while (*t == ' ')
    443 		t++;
    444 	if (!*t && !it)
    445 		return;
    446 	args = strchr(t, ' ');
    447 	if (it && args)
    448 		/* the match, and what you typed after its name */
    449 		snprintf(cmd, sizeof cmd, "%s%s", it->cmd, args);
    450 	else if (it)
    451 		snprintf(cmd, sizeof cmd, "%s", it->cmd);
    452 	else
    453 		snprintf(cmd, sizeof cmd, "%s", t);
    454 	if (text[0] == '!') {
    455 		char c2[2600];
    456 
    457 		if (strchr(cmd, '\''))
    458 			snprintf(c2, sizeof c2, "%s -hold -e %s", terminal, cmd);
    459 		else
    460 			snprintf(c2, sizeof c2, "%s -hold -e sh -c '%s'", terminal, cmd);
    461 		snprintf(cmd, sizeof cmd, "%s", c2);
    462 	}
    463 	remember(it ? it->name : t);
    464 	spawn(cmd);
    465 }
    466 
    467 /* ---- drawing ---- */
    468 
    469 static void
    470 drawbar(void)
    471 {
    472 	char s[1100];
    473 	int x, w, i, avail;
    474 
    475 	XftDrawRect(draw, &bg, 0, 0, sw, ch);
    476 	snprintf(s, sizeof s, "run: %s", text);
    477 	XftDrawStringUtf8(draw, &fg, font, 0, ascent, (FcChar8 *)s, strlen(s));
    478 	x = textw(s);
    479 	XftDrawRect(draw, &fg, x, 0, cw, ch);	/* the cursor */
    480 	x += cw * 3;
    481 	/* keep the one picked in sight */
    482 	avail = sw - x;
    483 	if (cur < first)
    484 		first = cur;
    485 	for (;;) {
    486 		w = 0;
    487 		for (i = first; i <= cur && i < nmatch; i++)
    488 			w += textw(match[i]->name) + cw * 2;
    489 		if (w <= avail || first >= cur)
    490 			break;
    491 		first++;
    492 	}
    493 	if (first > 0) {
    494 		XftDrawStringUtf8(draw, &fg, font, x, ascent, (FcChar8 *)"<", 1);
    495 		x += cw * 2;
    496 	}
    497 	for (i = first; i < nmatch; i++) {
    498 		w = textw(match[i]->name);
    499 		if (x + w + cw * 2 > sw) {
    500 			XftDrawStringUtf8(draw, &fg, font, sw - cw, ascent, (FcChar8 *)">", 1);
    501 			break;
    502 		}
    503 		if (i == cur) {
    504 			XftDrawRect(draw, &hl, x - cw / 2, 0, w + cw, ch);
    505 			XftDrawStringUtf8(draw, &bg, font, x, ascent, (FcChar8 *)match[i]->name, strlen(match[i]->name));
    506 		} else
    507 			XftDrawStringUtf8(draw, &fg, font, x, ascent, (FcChar8 *)match[i]->name, strlen(match[i]->name));
    508 		x += w + cw * 2;
    509 	}
    510 	XCopyArea(dpy, buf, win, gc, 0, 0, sw, ch, 0, 0);
    511 }
    512 
    513 /* ---- keys ---- */
    514 
    515 static int
    516 keypress(XKeyEvent *e)
    517 {
    518 	char s[64];
    519 	KeySym sym;
    520 	Status st;
    521 	int n = xic ? Xutf8LookupString(xic, e, s, sizeof s - 1, &sym, &st) : XLookupString(e, s, sizeof s - 1, &sym, NULL);
    522 	int ctrl = e->state & ControlMask, shift = e->state & ShiftMask;
    523 
    524 	s[MAX(n, 0)] = '\0';
    525 	if (ctrl) {
    526 		switch (sym) {
    527 		case XK_u: len = 0; text[0] = '\0'; domatch(); return 0;
    528 		case XK_w:
    529 			while (len > 0 && text[len - 1] == ' ')
    530 				len--;
    531 			while (len > 0 && text[len - 1] != ' ')
    532 				len--;
    533 			text[len] = '\0';
    534 			domatch();
    535 			return 0;
    536 		case XK_n: sym = XK_Right; break;
    537 		case XK_p: sym = XK_Left; break;
    538 		case XK_c: case XK_g: case XK_bracketleft: return 1;
    539 		case XK_Return: go(1); return 1;
    540 		default: return 0;
    541 		}
    542 	}
    543 	switch (sym) {
    544 	case XK_Escape:
    545 		return 1;
    546 	case XK_Return: case XK_KP_Enter:
    547 		go(shift);
    548 		return 1;
    549 	case XK_Tab: case XK_Right: case XK_Down:
    550 		if (nmatch)
    551 			cur = (cur + 1) % nmatch;
    552 		return 0;
    553 	case XK_ISO_Left_Tab: case XK_Left: case XK_Up:
    554 		if (nmatch)
    555 			cur = (cur + nmatch - 1) % nmatch;
    556 		return 0;
    557 	case XK_BackSpace:
    558 		/* a whole letter, not a byte of it */
    559 		while (len > 0 && (text[--len] & 0xc0) == 0x80);
    560 		text[len] = '\0';
    561 		domatch();
    562 		return 0;
    563 	}
    564 	if (n > 0 && (unsigned char)s[0] >= 0x20 && s[0] != 0x7f && len + n < sizeof text) {
    565 		memcpy(text + len, s, n);
    566 		len += n;
    567 		text[len] = '\0';
    568 		domatch();
    569 	}
    570 	return 0;
    571 }
    572 
    573 static void
    574 grab(void)
    575 {
    576 	struct timespec ts = { 0, 1000000 };
    577 	int i;
    578 
    579 	/* the window manager might still have the keyboard for the key that started us */
    580 	for (i = 0; i < 1000; i++) {
    581 		if (XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
    582 			return;
    583 		nanosleep(&ts, NULL);
    584 	}
    585 	die("can't have the keyboard");
    586 }
    587 
    588 int
    589 main(int argc, char *argv[])
    590 {
    591 	XSetWindowAttributes wa;
    592 	XClassHint hint = { "launchel", "launchel" };
    593 	XIM xim;
    594 	XEvent ev;
    595 	char dir[1100], path[1200];
    596 	const char *home = getenv("HOME") ? getenv("HOME") : ".", *xdg, *size;
    597 	int y;
    598 
    599 	if (argc == 2 && !strcmp(argv[1], "-v")) {
    600 		puts("launchel-" VERSION);
    601 		return 0;
    602 	} else if (argc != 1)
    603 		die("usage: launchel [-v]");
    604 	setlocale(LC_CTYPE, "");
    605 	XSetLocaleModifiers("");
    606 	if (!(dpy = XOpenDisplay(NULL)))
    607 		die("can't open the display");
    608 	scr = DefaultScreen(dpy);
    609 	root = RootWindow(dpy, scr);
    610 	sw = DisplayWidth(dpy, scr);
    611 	sh = DisplayHeight(dpy, scr);
    612 
    613 	if ((xdg = getenv("XDG_CONFIG_HOME")) && *xdg)
    614 		snprintf(dir, sizeof dir, "%s/launchel", xdg);
    615 	else
    616 		snprintf(dir, sizeof dir, "%s/.config/launchel", home);
    617 	snprintf(path, sizeof path, "%s/.config", home);
    618 	mkdir(path, 0755);
    619 	mkdir(dir, 0755);
    620 	snprintf(path, sizeof path, "%s/config", dir);
    621 	loadconfig(path);
    622 	snprintf(histpath, sizeof histpath, "%s/history", dir);
    623 
    624 	if (!fontsize)
    625 		fontsize = (size = getenv("TTYWM_SIZE")) && atoi(size) > 3 ? atoi(size) : 16;
    626 	snprintf(path, sizeof path, "%s:pixelsize=%d", fontname, fontsize);
    627 	if (!(font = XftFontOpenName(dpy, scr, path)) && !(font = XftFontOpenName(dpy, scr, "monospace:pixelsize=16")))
    628 		die("no font");
    629 	cw = textw("M");
    630 	ch = font->ascent + font->descent;
    631 	ascent = font->ascent;
    632 	colour(fgname, &fg);
    633 	colour(bgname, &bg);
    634 	colour(selname, &hl);
    635 
    636 	/* show the bar before reading every program there is */
    637 	y = !strcmp(place, "top") ? 0 : sh - ch;
    638 	wa.override_redirect = True;
    639 	wa.background_pixel = BlackPixel(dpy, scr);
    640 	wa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask;
    641 	win = XCreateWindow(dpy, root, 0, y, sw, ch, 0, CopyFromParent, CopyFromParent, CopyFromParent,
    642 	                    CWOverrideRedirect | CWBackPixel | CWEventMask, &wa);
    643 	XSetClassHint(dpy, win, &hint);
    644 	XStoreName(dpy, win, "launchel");
    645 	buf = XCreatePixmap(dpy, win, sw, ch, DefaultDepth(dpy, scr));
    646 	draw = XftDrawCreate(dpy, buf, DefaultVisual(dpy, scr), DefaultColormap(dpy, scr));
    647 	gc = XCreateGC(dpy, win, 0, NULL);
    648 	if ((xim = XOpenIM(dpy, NULL, NULL, NULL)))
    649 		xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, win,
    650 		                XNFocusWindow, win, NULL);
    651 	XMapRaised(dpy, win);
    652 	grab();
    653 	drawbar();
    654 	XFlush(dpy);
    655 
    656 	snprintf(path, sizeof path, "%s/.local/share/applications", home);
    657 	readapps(path);
    658 	readapps("/usr/local/share/applications");
    659 	readapps("/usr/share/applications");
    660 	readapps("/var/lib/flatpak/exports/share/applications");
    661 	snprintf(path, sizeof path, "%s/.local/share/flatpak/exports/share/applications", home);
    662 	readapps(path);
    663 	readpath();
    664 	readhistory();
    665 	domatch();
    666 	drawbar();
    667 
    668 	while (!XNextEvent(dpy, &ev)) {
    669 		if (XFilterEvent(&ev, None))
    670 			continue;
    671 		if (ev.type == Expose)
    672 			drawbar();
    673 		else if (ev.type == KeyPress) {
    674 			if (keypress(&ev.xkey))
    675 				break;
    676 			drawbar();
    677 		} else if (ev.type == ButtonPress)
    678 			break;	/* a click anywhere: never mind */
    679 	}
    680 	XUngrabKeyboard(dpy, CurrentTime);
    681 	XCloseDisplay(dpy);
    682 	return 0;
    683 }