1 /* See LICENSE file for copyright and license details. */
10 #include <X11/keysym.h>
12 #include <X11/Xutil.h>
14 #include <X11/extensions/Xinerama.h>
18 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
21 enum { ColFG, ColBG, ColLast };
24 typedef unsigned int uint;
25 typedef unsigned long ulong;
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, ulong col[ColLast]);
55 static void *emalloc(uint size);
56 static void eprint(const char *errstr, ...);
57 static ulong getcolor(const char *colstr);
58 static Bool grabkeyboard(void);
59 static void initfont(const char *fontstr);
60 static void kpress(XKeyEvent * e);
61 static void match(char *pattern);
62 static void readstdin(void);
63 static void run(void);
64 static void setup(Bool topbar);
65 static int textnw(const char *text, uint len);
66 static int textw(const char *text);
71 static char *maxname = NULL;
72 static char *prompt = NULL;
73 static char text[4096];
75 static int promptw = 0;
79 static uint numlockmask = 0;
80 static Bool running = True;
83 static Item *allitems = NULL; /* first of all items */
84 static Item *item = NULL; /* first of pattern matching items */
85 static Item *sel = NULL;
86 static Item *next = NULL;
87 static Item *prev = NULL;
88 static Item *curr = NULL;
89 static Window root, win;
90 static int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
91 static char *(*fstrstr)(const char *, const char *) = strstr;
94 appenditem(Item *i, Item **list, Item **last) {
111 w = promptw + cmdw + 2 * spaceitem;
112 for(next = curr; next; next=next->right) {
113 tw = textw(next->text);
120 w = promptw + cmdw + 2 * spaceitem;
121 for(prev = curr; prev && prev->left; prev=prev->left) {
122 tw = textw(prev->left->text);
132 cistrstr(const char *s, const char *sub) {
138 if((c = *sub++) != 0) {
143 if((csub = *s++) == 0)
146 while(tolower(csub) != c);
148 while(strncasecmp(s, sub, len) != 0);
159 itm = allitems->next;
160 free(allitems->text);
165 XFreeFontSet(dpy, dc.font.set);
167 XFreeFont(dpy, dc.font.xfont);
168 XFreePixmap(dpy, dc.drawable);
170 XDestroyWindow(dpy, win);
171 XUngrabKeyboard(dpy, CurrentTime);
182 drawtext(NULL, dc.norm);
186 drawtext(prompt, dc.sel);
193 drawtext(text[0] ? text : NULL, dc.norm);
197 drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
199 /* determine maximum items */
200 for(i = curr; i != next; i=i->right) {
201 dc.w = textw(i->text);
204 drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
207 dc.x = mw - spaceitem;
209 drawtext(next ? ">" : NULL, dc.norm);
211 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
216 drawtext(const char *text, ulong col[ColLast]) {
218 static char buf[256];
220 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
222 XSetForeground(dpy, dc.gc, col[ColBG]);
223 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
227 olen = len = strlen(text);
228 if(len >= sizeof buf)
229 len = sizeof buf - 1;
230 memcpy(buf, text, len);
232 h = dc.font.ascent + dc.font.descent;
233 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
235 /* shorten text if necessary */
236 while(len && (w = textnw(buf, len)) > dc.w - h)
247 return; /* too long */
248 XSetForeground(dpy, dc.gc, col[ColFG]);
250 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
252 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
257 void *res = malloc(size);
260 eprint("fatal: could not malloc() %u bytes\n", size);
265 eprint(const char *errstr, ...) {
268 va_start(ap, errstr);
269 vfprintf(stderr, errstr, ap);
275 getcolor(const char *colstr) {
276 Colormap cmap = DefaultColormap(dpy, screen);
279 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
280 eprint("error, cannot allocate color '%s'\n", colstr);
288 for(len = 1000; len; len--) {
289 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
298 initfont(const char *fontstr) {
299 char *def, **missing;
302 if(!fontstr || fontstr[0] == '\0')
303 eprint("error, cannot load font: '%s'\n", fontstr);
306 XFreeFontSet(dpy, dc.font.set);
307 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
309 XFreeStringList(missing);
311 XFontSetExtents *font_extents;
312 XFontStruct **xfonts;
314 dc.font.ascent = dc.font.descent = 0;
315 font_extents = XExtentsOfFontSet(dc.font.set);
316 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
317 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
318 if(dc.font.ascent < (*xfonts)->ascent)
319 dc.font.ascent = (*xfonts)->ascent;
320 if(dc.font.descent < (*xfonts)->descent)
321 dc.font.descent = (*xfonts)->descent;
327 XFreeFont(dpy, dc.font.xfont);
328 dc.font.xfont = NULL;
329 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
330 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
331 eprint("error, cannot load font: '%s'\n", fontstr);
332 dc.font.ascent = dc.font.xfont->ascent;
333 dc.font.descent = dc.font.xfont->descent;
335 dc.font.height = dc.font.ascent + dc.font.descent;
339 kpress(XKeyEvent * e) {
347 num = XLookupString(e, buf, sizeof buf, &ksym, 0);
348 if(IsKeypadKey(ksym)) {
349 if(ksym == XK_KP_Enter)
351 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
352 ksym = (ksym - XK_KP_0) + XK_0;
354 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
355 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
356 || IsPrivateKeypadKey(ksym))
358 /* first check if a control mask is omitted */
359 if(e->state & ControlMask) {
361 default: /* ignore other control sequences */
388 while(i >= 0 && text[i] == ' ')
390 while(i >= 0 && text[i] != ' ')
398 if(CLEANMASK(e->state) & Mod1Mask) {
423 if(num && !iscntrl((int) buf[0])) {
426 strncat(text, buf, sizeof text);
428 strncpy(text, buf, sizeof text);
445 while(sel && sel->right)
459 if(!(sel && sel->left))
462 if(sel->right == curr) {
480 if((e->state & ShiftMask) && text)
481 fprintf(stdout, "%s", text);
483 fprintf(stdout, "%s", sel->text);
485 fprintf(stdout, "%s", text);
490 if(!(sel && sel->right))
501 strncpy(text, sel->text, sizeof text);
509 match(char *pattern) {
511 Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
515 plen = strlen(pattern);
516 item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
517 for(i = allitems; i; i = i->next)
518 if(!fstrncmp(pattern, i->text, plen + 1))
519 appenditem(i, &lexact, &exactend);
520 else if(!fstrncmp(pattern, i->text, plen))
521 appenditem(i, &lprefix, &prefixend);
522 else if(fstrstr(i->text, pattern))
523 appenditem(i, &lsubstr, &substrend);
530 itemend->right = lprefix;
531 lprefix->left = itemend;
539 itemend->right = lsubstr;
540 lsubstr->left = itemend;
545 curr = prev = next = sel = item;
552 uint len = 0, max = 0;
556 while(fgets(buf, sizeof buf, stdin)) {
558 if (buf[len - 1] == '\n')
560 if(!(p = strdup(buf)))
561 eprint("fatal: could not strdup() %u bytes\n", strlen(buf));
566 new = emalloc(sizeof(Item));
567 new->next = new->left = new->right = NULL;
581 /* main event loop */
582 while(running && !XNextEvent(dpy, &ev))
584 default: /* ignore all crap */
590 if(ev.xexpose.count == 0)
599 XModifierKeymap *modmap;
600 XSetWindowAttributes wa;
602 XineramaScreenInfo *info = NULL;
605 /* init modifier map */
606 modmap = XGetModifierMapping(dpy);
607 for(i = 0; i < 8; i++)
608 for(j = 0; j < modmap->max_keypermod; j++) {
609 if(modmap->modifiermap[i * modmap->max_keypermod + j]
610 == XKeysymToKeycode(dpy, XK_Num_Lock))
611 numlockmask = (1 << i);
613 XFreeModifiermap(modmap);
616 dc.norm[ColBG] = getcolor(normbgcolor);
617 dc.norm[ColFG] = getcolor(normfgcolor);
618 dc.sel[ColBG] = getcolor(selbgcolor);
619 dc.sel[ColFG] = getcolor(selfgcolor);
623 wa.override_redirect = 1;
624 wa.background_pixmap = ParentRelative;
625 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
627 /* menu window geometry */
628 mh = dc.font.height + 2;
630 if(XineramaIsActive(dpy)) {
631 info = XineramaQueryScreens(dpy, &i);
632 x = info[xidx].x_org;
633 y = topbar ? info[xidx].y_org : info[xidx].y_org + info[xidx].height - mh;
634 mw = info[xidx].width;
641 y = topbar ? 0 : DisplayHeight(dpy, screen) - mh;
642 mw = DisplayWidth(dpy, screen);
645 win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
646 DefaultDepth(dpy, screen), CopyFromParent,
647 DefaultVisual(dpy, screen),
648 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
651 dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
652 dc.gc = XCreateGC(dpy, root, 0, 0);
653 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
655 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
657 cmdw = textw(maxname);
661 promptw = textw(prompt);
666 XMapRaised(dpy, win);
670 textnw(const char *text, uint len) {
674 XmbTextExtents(dc.font.set, text, len, NULL, &r);
677 return XTextWidth(dc.font.xfont, text, len);
681 textw(const char *text) {
682 return textnw(text, strlen(text)) + dc.font.height;
686 main(int argc, char *argv[]) {
690 /* command line args */
691 for(i = 1; i < argc; i++)
692 if(!strcmp(argv[i], "-i")) {
693 fstrncmp = strncasecmp;
696 else if(!strcmp(argv[i], "-b"))
698 else if(!strcmp(argv[i], "-fn")) {
699 if(++i < argc) font = argv[i];
701 else if(!strcmp(argv[i], "-nb")) {
702 if(++i < argc) normbgcolor = argv[i];
704 else if(!strcmp(argv[i], "-nf")) {
705 if(++i < argc) normfgcolor = argv[i];
707 else if(!strcmp(argv[i], "-p")) {
708 if(++i < argc) prompt = argv[i];
710 else if(!strcmp(argv[i], "-sb")) {
711 if(++i < argc) selbgcolor = argv[i];
713 else if(!strcmp(argv[i], "-sf")) {
714 if(++i < argc) selfgcolor = argv[i];
716 else if(!strcmp(argv[i], "-v"))
717 eprint("dmenu-"VERSION", © 2006-2008 dmenu engineers, see LICENSE for details\n");
719 eprint("usage: dmenu [-i] [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
720 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
721 setlocale(LC_CTYPE, "");
722 if(!(dpy = XOpenDisplay(0)))
723 eprint("dmenu: cannot open display\n");
724 screen = DefaultScreen(dpy);
725 root = RootWindow(dpy, screen);
727 if(isatty(STDIN_FILENO)) {
729 running = grabkeyboard();
731 else { /* prevent keypress loss */
732 running = grabkeyboard();