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 *font = FONT;
72 static char *maxname = NULL;
73 static char *normbg = NORMBGCOLOR;
74 static char *normfg = NORMFGCOLOR;
75 static char *prompt = NULL;
76 static char *selbg = SELBGCOLOR;
77 static char *selfg = SELFGCOLOR;
78 static char text[4096];
80 static int promptw = 0;
84 static uint numlockmask = 0;
85 static Bool running = True;
88 static Item *allitems = NULL; /* first of all items */
89 static Item *item = NULL; /* first of pattern matching items */
90 static Item *sel = NULL;
91 static Item *next = NULL;
92 static Item *prev = NULL;
93 static Item *curr = NULL;
94 static Window root, win;
95 static int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
96 static char *(*fstrstr)(const char *, const char *) = strstr;
99 appenditem(Item *i, Item **list, Item **last) {
116 w = promptw + cmdw + 2 * spaceitem;
117 for(next = curr; next; next=next->right) {
118 tw = textw(next->text);
125 w = promptw + cmdw + 2 * spaceitem;
126 for(prev = curr; prev && prev->left; prev=prev->left) {
127 tw = textw(prev->left->text);
137 cistrstr(const char *s, const char *sub) {
143 if((c = *sub++) != 0) {
148 if((csub = *s++) == 0)
151 while(tolower(csub) != c);
153 while(strncasecmp(s, sub, len) != 0);
164 itm = allitems->next;
165 free(allitems->text);
170 XFreeFontSet(dpy, dc.font.set);
172 XFreeFont(dpy, dc.font.xfont);
173 XFreePixmap(dpy, dc.drawable);
175 XDestroyWindow(dpy, win);
176 XUngrabKeyboard(dpy, CurrentTime);
187 drawtext(NULL, dc.norm);
191 drawtext(prompt, dc.sel);
198 drawtext(text[0] ? text : NULL, dc.norm);
202 drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
204 /* determine maximum items */
205 for(i = curr; i != next; i=i->right) {
206 dc.w = textw(i->text);
209 drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
212 dc.x = mw - spaceitem;
214 drawtext(next ? ">" : NULL, dc.norm);
216 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
221 drawtext(const char *text, ulong col[ColLast]) {
223 static char buf[256];
225 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
227 XSetForeground(dpy, dc.gc, col[ColBG]);
228 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
232 olen = len = strlen(text);
233 if(len >= sizeof buf)
234 len = sizeof buf - 1;
235 memcpy(buf, text, len);
237 h = dc.font.ascent + dc.font.descent;
238 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
240 /* shorten text if necessary */
241 while(len && (w = textnw(buf, len)) > dc.w - h)
252 return; /* too long */
253 XSetForeground(dpy, dc.gc, col[ColFG]);
255 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
257 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
262 void *res = malloc(size);
265 eprint("fatal: could not malloc() %u bytes\n", size);
270 eprint(const char *errstr, ...) {
273 va_start(ap, errstr);
274 vfprintf(stderr, errstr, ap);
280 getcolor(const char *colstr) {
281 Colormap cmap = DefaultColormap(dpy, screen);
284 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
285 eprint("error, cannot allocate color '%s'\n", colstr);
293 for(len = 1000; len; len--) {
294 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
303 initfont(const char *fontstr) {
304 char *def, **missing;
307 if(!fontstr || fontstr[0] == '\0')
308 eprint("error, cannot load font: '%s'\n", fontstr);
311 XFreeFontSet(dpy, dc.font.set);
312 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
314 XFreeStringList(missing);
316 XFontSetExtents *font_extents;
317 XFontStruct **xfonts;
319 dc.font.ascent = dc.font.descent = 0;
320 font_extents = XExtentsOfFontSet(dc.font.set);
321 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
322 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
323 if(dc.font.ascent < (*xfonts)->ascent)
324 dc.font.ascent = (*xfonts)->ascent;
325 if(dc.font.descent < (*xfonts)->descent)
326 dc.font.descent = (*xfonts)->descent;
332 XFreeFont(dpy, dc.font.xfont);
333 dc.font.xfont = NULL;
334 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
335 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
336 eprint("error, cannot load font: '%s'\n", fontstr);
337 dc.font.ascent = dc.font.xfont->ascent;
338 dc.font.descent = dc.font.xfont->descent;
340 dc.font.height = dc.font.ascent + dc.font.descent;
344 kpress(XKeyEvent * e) {
352 num = XLookupString(e, buf, sizeof buf, &ksym, 0);
353 if(IsKeypadKey(ksym)) {
354 if(ksym == XK_KP_Enter)
356 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
357 ksym = (ksym - XK_KP_0) + XK_0;
359 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
360 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
361 || IsPrivateKeypadKey(ksym))
363 /* first check if a control mask is omitted */
364 if(e->state & ControlMask) {
366 default: /* ignore other control sequences */
393 while(i >= 0 && text[i] == ' ')
395 while(i >= 0 && text[i] != ' ')
403 if(CLEANMASK(e->state) & Mod1Mask) {
428 if(num && !iscntrl((int) buf[0])) {
431 strncat(text, buf, sizeof text);
433 strncpy(text, buf, sizeof text);
450 while(sel && sel->right)
464 if(!(sel && sel->left))
467 if(sel->right == curr) {
485 if((e->state & ShiftMask) && text)
486 fprintf(stdout, "%s", text);
488 fprintf(stdout, "%s", sel->text);
490 fprintf(stdout, "%s", text);
495 if(!(sel && sel->right))
506 strncpy(text, sel->text, sizeof text);
514 match(char *pattern) {
516 Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
520 plen = strlen(pattern);
521 item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
522 for(i = allitems; i; i = i->next)
523 if(!fstrncmp(pattern, i->text, plen + 1))
524 appenditem(i, &lexact, &exactend);
525 else if(!fstrncmp(pattern, i->text, plen))
526 appenditem(i, &lprefix, &prefixend);
527 else if(fstrstr(i->text, pattern))
528 appenditem(i, &lsubstr, &substrend);
535 itemend->right = lprefix;
536 lprefix->left = itemend;
544 itemend->right = lsubstr;
545 lsubstr->left = itemend;
550 curr = prev = next = sel = item;
557 uint len = 0, max = 0;
561 while(fgets(buf, sizeof buf, stdin)) {
563 if (buf[len - 1] == '\n')
565 if(!(p = strdup(buf)))
566 eprint("fatal: could not strdup() %u bytes\n", strlen(buf));
571 new = emalloc(sizeof(Item));
572 new->next = new->left = new->right = NULL;
586 /* main event loop */
587 while(running && !XNextEvent(dpy, &ev))
589 default: /* ignore all crap */
595 if(ev.xexpose.count == 0)
604 XModifierKeymap *modmap;
605 XSetWindowAttributes wa;
607 XineramaScreenInfo *info = NULL;
610 /* init modifier map */
611 modmap = XGetModifierMapping(dpy);
612 for(i = 0; i < 8; i++)
613 for(j = 0; j < modmap->max_keypermod; j++) {
614 if(modmap->modifiermap[i * modmap->max_keypermod + j]
615 == XKeysymToKeycode(dpy, XK_Num_Lock))
616 numlockmask = (1 << i);
618 XFreeModifiermap(modmap);
621 dc.norm[ColBG] = getcolor(normbg);
622 dc.norm[ColFG] = getcolor(normfg);
623 dc.sel[ColBG] = getcolor(selbg);
624 dc.sel[ColFG] = getcolor(selfg);
628 wa.override_redirect = 1;
629 wa.background_pixmap = ParentRelative;
630 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
632 /* menu window geometry */
633 mh = dc.font.height + 2;
635 if(XineramaIsActive(dpy)) {
636 info = XineramaQueryScreens(dpy, &i);
638 y = topbar ? info[0].y_org : info[0].y_org + info[0].height - mh;
646 y = topbar ? 0 : DisplayHeight(dpy, screen) - mh;
647 mw = DisplayWidth(dpy, screen);
650 win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
651 DefaultDepth(dpy, screen), CopyFromParent,
652 DefaultVisual(dpy, screen),
653 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
656 dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
657 dc.gc = XCreateGC(dpy, root, 0, 0);
658 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
660 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
662 cmdw = textw(maxname);
666 promptw = textw(prompt);
671 XMapRaised(dpy, win);
675 textnw(const char *text, uint len) {
679 XmbTextExtents(dc.font.set, text, len, NULL, &r);
682 return XTextWidth(dc.font.xfont, text, len);
686 textw(const char *text) {
687 return textnw(text, strlen(text)) + dc.font.height;
691 main(int argc, char *argv[]) {
695 /* command line args */
696 for(i = 1; i < argc; i++)
697 if(!strcmp(argv[i], "-i")) {
698 fstrncmp = strncasecmp;
701 else if(!strcmp(argv[i], "-b"))
703 else if(!strcmp(argv[i], "-fn")) {
704 if(++i < argc) font = argv[i];
706 else if(!strcmp(argv[i], "-nb")) {
707 if(++i < argc) normbg = argv[i];
709 else if(!strcmp(argv[i], "-nf")) {
710 if(++i < argc) normfg = argv[i];
712 else if(!strcmp(argv[i], "-p")) {
713 if(++i < argc) prompt = argv[i];
715 else if(!strcmp(argv[i], "-sb")) {
716 if(++i < argc) selbg = argv[i];
718 else if(!strcmp(argv[i], "-sf")) {
719 if(++i < argc) selfg = argv[i];
721 else if(!strcmp(argv[i], "-v"))
722 eprint("dmenu-"VERSION", © 2006-2008 dmenu engineers, see LICENSE for details\n");
724 eprint("usage: dmenu [-i] [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
725 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
726 setlocale(LC_CTYPE, "");
727 if(!(dpy = XOpenDisplay(0)))
728 eprint("dmenu: cannot open display\n");
729 screen = DefaultScreen(dpy);
730 root = RootWindow(dpy, screen);
732 if(isatty(STDIN_FILENO)) {
734 running = grabkeyboard();
736 else { /* prevent keypress loss */
737 running = grabkeyboard();