1 /* See LICENSE file for copyright and license details. */
11 #include <X11/keysym.h>
13 #include <X11/Xutil.h>
15 #include <X11/extensions/Xinerama.h>
19 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
20 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
23 enum { ColFG, ColBG, ColLast };
28 unsigned long norm[ColLast];
29 unsigned long sel[ColLast];
39 } DC; /* draw context */
41 typedef struct Item Item;
44 Item *next; /* traverses all items */
45 Item *left, *right; /* traverses items matching current search pattern */
48 /* forward declarations */
49 static void appenditem(Item *i, Item **list, Item **last);
50 static void calcoffsets(void);
51 static char *cistrstr(const char *s, const char *sub);
52 static void cleanup(void);
53 static void drawmenu(void);
54 static void drawtext(const char *text, unsigned long col[ColLast]);
55 static void eprint(const char *errstr, ...);
56 static unsigned long getcolor(const char *colstr);
57 static Bool grabkeyboard(void);
58 static void initfont(const char *fontstr);
59 static void kpress(XKeyEvent * e);
60 static void match(char *pattern);
61 static void readstdin(void);
62 static void run(void);
63 static void setup(Bool topbar);
64 static int textnw(const char *text, unsigned int len);
65 static int textw(const char *text);
70 static char *maxname = NULL;
71 static char *prompt = NULL;
72 static char text[4096];
74 static int promptw = 0;
77 static unsigned int mw, mh;
78 static unsigned int numlockmask = 0;
79 static Bool running = True;
82 static Item *allitems = NULL; /* first of all items */
83 static Item *item = NULL; /* first of pattern matching items */
84 static Item *sel = NULL;
85 static Item *next = NULL;
86 static Item *prev = NULL;
87 static Item *curr = NULL;
88 static Window root, win;
89 static int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
90 static char *(*fstrstr)(const char *, const char *) = strstr;
93 appenditem(Item *i, Item **list, Item **last) {
110 w = promptw + cmdw + 2 * spaceitem;
111 for(next = curr; next; next=next->right) {
112 tw = textw(next->text);
119 w = promptw + cmdw + 2 * spaceitem;
120 for(prev = curr; prev && prev->left; prev=prev->left) {
121 tw = textw(prev->left->text);
131 cistrstr(const char *s, const char *sub) {
137 if((c = *sub++) != 0) {
142 if((csub = *s++) == 0)
145 while(tolower(csub) != c);
147 while(strncasecmp(s, sub, len) != 0);
158 itm = allitems->next;
159 free(allitems->text);
164 XFreeFontSet(dpy, dc.font.set);
166 XFreeFont(dpy, dc.font.xfont);
167 XFreePixmap(dpy, dc.drawable);
169 XDestroyWindow(dpy, win);
170 XUngrabKeyboard(dpy, CurrentTime);
181 drawtext(NULL, dc.norm);
185 drawtext(prompt, dc.sel);
192 drawtext(text[0] ? text : NULL, dc.norm);
196 drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
198 /* determine maximum items */
199 for(i = curr; i != next; i=i->right) {
200 dc.w = textw(i->text);
203 drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
206 dc.x = mw - spaceitem;
208 drawtext(next ? ">" : NULL, dc.norm);
210 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
215 drawtext(const char *text, unsigned long col[ColLast]) {
217 static char buf[256];
218 unsigned int len, olen;
219 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
221 XSetForeground(dpy, dc.gc, col[ColBG]);
222 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
226 olen = len = strlen(text);
227 if(len >= sizeof buf)
228 len = sizeof buf - 1;
229 memcpy(buf, text, len);
231 h = dc.font.ascent + dc.font.descent;
232 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
234 /* shorten text if necessary */
235 while(len && (w = textnw(buf, len)) > dc.w - h)
246 return; /* too long */
247 XSetForeground(dpy, dc.gc, col[ColFG]);
249 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
251 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
255 eprint(const char *errstr, ...) {
258 va_start(ap, errstr);
259 vfprintf(stderr, errstr, ap);
265 getcolor(const char *colstr) {
266 Colormap cmap = DefaultColormap(dpy, screen);
269 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
270 eprint("error, cannot allocate color '%s'\n", colstr);
278 for(len = 1000; len; len--) {
279 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
288 initfont(const char *fontstr) {
289 char *def, **missing;
292 if(!fontstr || fontstr[0] == '\0')
293 eprint("error, cannot load font: '%s'\n", fontstr);
296 XFreeFontSet(dpy, dc.font.set);
297 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
299 XFreeStringList(missing);
301 XFontSetExtents *font_extents;
302 XFontStruct **xfonts;
304 dc.font.ascent = dc.font.descent = 0;
305 font_extents = XExtentsOfFontSet(dc.font.set);
306 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
307 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
308 if(dc.font.ascent < (*xfonts)->ascent)
309 dc.font.ascent = (*xfonts)->ascent;
310 if(dc.font.descent < (*xfonts)->descent)
311 dc.font.descent = (*xfonts)->descent;
317 XFreeFont(dpy, dc.font.xfont);
318 dc.font.xfont = NULL;
319 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
320 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
321 eprint("error, cannot load font: '%s'\n", fontstr);
322 dc.font.ascent = dc.font.xfont->ascent;
323 dc.font.descent = dc.font.xfont->descent;
325 dc.font.height = dc.font.ascent + dc.font.descent;
329 kpress(XKeyEvent * e) {
337 num = XLookupString(e, buf, sizeof buf, &ksym, 0);
338 if(IsKeypadKey(ksym)) {
339 if(ksym == XK_KP_Enter)
341 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
342 ksym = (ksym - XK_KP_0) + XK_0;
344 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
345 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
346 || IsPrivateKeypadKey(ksym))
348 /* first check if a control mask is omitted */
349 if(e->state & ControlMask) {
351 default: /* ignore other control sequences */
378 while(i >= 0 && text[i] == ' ')
380 while(i >= 0 && text[i] != ' ')
388 if(CLEANMASK(e->state) & Mod1Mask) {
413 if(num && !iscntrl((int) buf[0])) {
416 strncat(text, buf, sizeof text);
418 strncpy(text, buf, sizeof text);
435 while(sel && sel->right)
449 if(!(sel && sel->left))
452 if(sel->right == curr) {
470 if((e->state & ShiftMask) && *text)
471 fprintf(stdout, "%s", text);
473 fprintf(stdout, "%s", sel->text);
475 fprintf(stdout, "%s", text);
480 if(!(sel && sel->right))
491 strncpy(text, sel->text, sizeof text);
499 match(char *pattern) {
501 Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
505 plen = strlen(pattern);
506 item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
507 for(i = allitems; i; i = i->next)
508 if(!fstrncmp(pattern, i->text, plen + 1))
509 appenditem(i, &lexact, &exactend);
510 else if(!fstrncmp(pattern, i->text, plen))
511 appenditem(i, &lprefix, &prefixend);
512 else if(fstrstr(i->text, pattern))
513 appenditem(i, &lsubstr, &substrend);
520 itemend->right = lprefix;
521 lprefix->left = itemend;
529 itemend->right = lsubstr;
530 lsubstr->left = itemend;
535 curr = prev = next = sel = item;
542 unsigned int len = 0, max = 0;
546 while(fgets(buf, sizeof buf, stdin)) {
548 if (buf[len - 1] == '\n')
550 if(!(p = strdup(buf)))
551 eprint("fatal: could not strdup() %u bytes\n", strlen(buf));
556 if((new = (Item *)malloc(sizeof(Item))) == NULL)
557 eprint("fatal: could not malloc() %u bytes\n", sizeof(Item));
558 new->next = new->left = new->right = NULL;
572 /* main event loop */
573 while(running && !XNextEvent(dpy, &ev))
575 default: /* ignore all crap */
581 if(ev.xexpose.count == 0)
592 XineramaScreenInfo *info = NULL;
594 XModifierKeymap *modmap;
595 XSetWindowAttributes wa;
597 /* init modifier map */
598 modmap = XGetModifierMapping(dpy);
599 for(i = 0; i < 8; i++)
600 for(j = 0; j < modmap->max_keypermod; j++) {
601 if(modmap->modifiermap[i * modmap->max_keypermod + j]
602 == XKeysymToKeycode(dpy, XK_Num_Lock))
603 numlockmask = (1 << i);
605 XFreeModifiermap(modmap);
608 dc.norm[ColBG] = getcolor(normbgcolor);
609 dc.norm[ColFG] = getcolor(normfgcolor);
610 dc.sel[ColBG] = getcolor(selbgcolor);
611 dc.sel[ColFG] = getcolor(selfgcolor);
615 wa.override_redirect = 1;
616 wa.background_pixmap = ParentRelative;
617 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
619 /* menu window geometry */
620 mh = dc.font.height + 2;
622 if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
628 if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
629 for(i = 0; i < n; i++)
630 if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
634 y = topbar ? info[i].y_org : info[i].y_org + info[i].height - mh;
642 y = topbar ? 0 : DisplayHeight(dpy, screen) - mh;
643 mw = DisplayWidth(dpy, screen);
646 win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
647 DefaultDepth(dpy, screen), CopyFromParent,
648 DefaultVisual(dpy, screen),
649 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
652 dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
653 dc.gc = XCreateGC(dpy, root, 0, 0);
654 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
656 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
658 cmdw = textw(maxname);
662 promptw = textw(prompt);
667 XMapRaised(dpy, win);
671 textnw(const char *text, unsigned int len) {
675 XmbTextExtents(dc.font.set, text, len, NULL, &r);
678 return XTextWidth(dc.font.xfont, text, len);
682 textw(const char *text) {
683 return textnw(text, strlen(text)) + dc.font.height;
687 main(int argc, char *argv[]) {
691 /* command line args */
692 for(i = 1; i < argc; i++)
693 if(!strcmp(argv[i], "-i")) {
694 fstrncmp = strncasecmp;
697 else if(!strcmp(argv[i], "-b"))
699 else if(!strcmp(argv[i], "-fn")) {
700 if(++i < argc) font = argv[i];
702 else if(!strcmp(argv[i], "-nb")) {
703 if(++i < argc) normbgcolor = argv[i];
705 else if(!strcmp(argv[i], "-nf")) {
706 if(++i < argc) normfgcolor = argv[i];
708 else if(!strcmp(argv[i], "-p")) {
709 if(++i < argc) prompt = argv[i];
711 else if(!strcmp(argv[i], "-sb")) {
712 if(++i < argc) selbgcolor = argv[i];
714 else if(!strcmp(argv[i], "-sf")) {
715 if(++i < argc) selfgcolor = argv[i];
717 else if(!strcmp(argv[i], "-v"))
718 eprint("dmenu-"VERSION", © 2006-2008 dmenu engineers, see LICENSE for details\n");
720 eprint("usage: dmenu [-i] [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
721 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
722 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
723 fprintf(stderr, "warning: no locale support\n");
724 if(!(dpy = XOpenDisplay(0)))
725 eprint("dmenu: cannot open display\n");
726 screen = DefaultScreen(dpy);
727 root = RootWindow(dpy, screen);
729 if(isatty(STDIN_FILENO)) {
731 running = grabkeyboard();
733 else { /* prevent keypress loss */
734 running = grabkeyboard();