1 /* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
2 * (C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com>
3 * See LICENSE file for license details.
13 #include <sys/select.h>
15 #include <X11/Xutil.h>
16 #include <X11/keysym.h>
18 typedef struct Item Item;
20 Item *next; /* traverses all items */
21 Item *left, *right; /* traverses items matching current search pattern */
27 static char text[4096];
28 static int mx, my, mw, mh;
31 static unsigned int cmdw = 0;
32 static Bool running = True;
33 static Item *allitems = NULL; /* first of all items */
34 static Item *item = NULL; /* first of pattern matching items */
35 static Item *sel = NULL;
36 static Item *next = NULL;
37 static Item *prev = NULL;
38 static Item *curr = NULL;
49 for(next = curr; next; next=next->right) {
50 tw = textw(next->text);
58 for(prev = curr; prev && prev->left; prev=prev->left) {
59 tw = textw(prev->left->text);
76 drawtext(NULL, dc.norm);
80 drawtext(text[0] ? text : NULL, dc.norm);
84 drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
86 /* determine maximum items */
87 for(i = curr; i != next; i=i->right) {
88 dc.w = textw(i->text);
91 drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
96 drawtext(next ? ">" : NULL, dc.norm);
98 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
103 match(char *pattern) {
109 plen = strlen(pattern);
112 for(i = allitems; i; i=i->next)
113 if(!plen || !strncmp(pattern, i->text, plen)) {
123 for(i = allitems; i; i=i->next)
124 if(plen && strncmp(pattern, i->text, plen)
125 && strstr(i->text, pattern)) {
135 curr = prev = next = sel = item;
140 kpress(XKeyEvent * e) {
148 num = XLookupString(e, buf, sizeof buf, &ksym, 0);
149 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
150 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
151 || IsPrivateKeypadKey(ksym))
153 /* first check if a control mask is omitted */
154 if(e->state & ControlMask) {
156 default: /* ignore other control sequences */
174 if(num && !iscntrl((int) buf[0])) {
177 strncat(text, buf, sizeof text);
179 strncpy(text, buf, sizeof text);
189 } while(i && nitem && prev_nitem == nitem);
210 if(!(sel && sel->left))
213 if(sel->right == curr) {
231 if((e->state & ShiftMask) && text)
232 fprintf(stdout, "%s", text);
234 fprintf(stdout, "%s", sel->text);
236 fprintf(stdout, "%s", text);
241 if(!(sel && sel->right))
252 strncpy(text, sel->text, sizeof text);
261 static char *maxname = NULL;
263 unsigned int len = 0, max = 0;
267 while(fgets(buf, sizeof buf, stdin)) {
269 if (buf[len - 1] == '\n')
276 new = emalloc(sizeof(Item));
277 new->next = new->left = new->right = NULL;
296 main(int argc, char *argv[]) {
299 char *normbg = NORMBGCOLOR;
300 char *normfg = NORMFGCOLOR;
301 char *selbg = SELBGCOLOR;
302 char *selfg = SELFGCOLOR;
305 struct timeval timeout;
308 XSetWindowAttributes wa;
312 /* command line args */
313 for(i = 1; i < argc; i++)
314 if(!strncmp(argv[i], "-font", 6)) {
315 if(++i < argc) font = argv[i];
317 else if(!strncmp(argv[i], "-normbg", 8)) {
318 if(++i < argc) normbg = argv[i];
320 else if(!strncmp(argv[i], "-normfg", 8)) {
321 if(++i < argc) normfg = argv[i];
323 else if(!strncmp(argv[i], "-selbg", 7)) {
324 if(++i < argc) selbg = argv[i];
326 else if(!strncmp(argv[i], "-selfg", 7)) {
327 if(++i < argc) selfg = argv[i];
329 else if(!strncmp(argv[i], "-t", 3)) {
330 if(++i < argc) timeout.tv_sec = atoi(argv[i]);
332 else if(!strncmp(argv[i], "-v", 3)) {
333 fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
337 eprint("usage: dmenu [-font <name>] [-{norm,sel}{bg,fg} <color>] [-t <seconds>] [-v]\n", stdout);
338 setlocale(LC_CTYPE, "");
339 dpy = XOpenDisplay(0);
341 eprint("dmenu: cannot open display\n");
342 screen = DefaultScreen(dpy);
343 root = RootWindow(dpy, screen);
345 /* Note, the select() construction allows to grab all keypresses as
346 * early as possible, to not loose them. But if there is no standard
347 * input supplied, we will make sure to exit after MAX_WAIT_STDIN
348 * seconds. This is convenience behavior for rapid typers.
350 while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
351 GrabModeAsync, CurrentTime) != GrabSuccess)
354 FD_SET(STDIN_FILENO, &rd);
355 if(select(ConnectionNumber(dpy) + 1, &rd, NULL, NULL, &timeout) < 1)
356 goto UninitializedEnd;
357 maxname = readstdin();
359 dc.norm[ColBG] = getcolor(normbg);
360 dc.norm[ColFG] = getcolor(normfg);
361 dc.sel[ColBG] = getcolor(selbg);
362 dc.sel[ColFG] = getcolor(selfg);
365 wa.override_redirect = 1;
366 wa.background_pixmap = ParentRelative;
367 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
369 mw = DisplayWidth(dpy, screen);
370 mh = dc.font.height + 2;
371 win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
372 DefaultDepth(dpy, screen), CopyFromParent,
373 DefaultVisual(dpy, screen),
374 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
376 dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
377 dc.gc = XCreateGC(dpy, root, 0, 0);
378 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
380 cmdw = textw(maxname);
385 XMapRaised(dpy, win);
389 /* main event loop */
390 while(running && !XNextEvent(dpy, &ev))
392 default: /* ignore all crap */
398 if(ev.xexpose.count == 0)
405 itm = allitems->next;
406 free(allitems->text);
411 XFreeFontSet(dpy, dc.font.set);
413 XFreeFont(dpy, dc.font.xfont);
414 XFreePixmap(dpy, dc.drawable);
416 XDestroyWindow(dpy, win);
418 XUngrabKeyboard(dpy, CurrentTime);