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 eprint(const char *errstr, ...);
56 static ulong 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, uint 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;
78 static uint 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, ulong col[ColLast]) {
217 static char buf[256];
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 uint 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)
590 XModifierKeymap *modmap;
591 XSetWindowAttributes wa;
593 XineramaScreenInfo *info = NULL;
596 /* init modifier map */
597 modmap = XGetModifierMapping(dpy);
598 for(i = 0; i < 8; i++)
599 for(j = 0; j < modmap->max_keypermod; j++) {
600 if(modmap->modifiermap[i * modmap->max_keypermod + j]
601 == XKeysymToKeycode(dpy, XK_Num_Lock))
602 numlockmask = (1 << i);
604 XFreeModifiermap(modmap);
607 dc.norm[ColBG] = getcolor(normbgcolor);
608 dc.norm[ColFG] = getcolor(normfgcolor);
609 dc.sel[ColBG] = getcolor(selbgcolor);
610 dc.sel[ColFG] = getcolor(selfgcolor);
614 wa.override_redirect = 1;
615 wa.background_pixmap = ParentRelative;
616 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
618 /* menu window geometry */
619 mh = dc.font.height + 2;
621 if(XineramaIsActive(dpy)) {
622 info = XineramaQueryScreens(dpy, &i);
623 x = info[xidx].x_org;
624 y = topbar ? info[xidx].y_org : info[xidx].y_org + info[xidx].height - mh;
625 mw = info[xidx].width;
632 y = topbar ? 0 : DisplayHeight(dpy, screen) - mh;
633 mw = DisplayWidth(dpy, screen);
636 win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
637 DefaultDepth(dpy, screen), CopyFromParent,
638 DefaultVisual(dpy, screen),
639 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
642 dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
643 dc.gc = XCreateGC(dpy, root, 0, 0);
644 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
646 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
648 cmdw = textw(maxname);
652 promptw = textw(prompt);
657 XMapRaised(dpy, win);
661 textnw(const char *text, uint len) {
665 XmbTextExtents(dc.font.set, text, len, NULL, &r);
668 return XTextWidth(dc.font.xfont, text, len);
672 textw(const char *text) {
673 return textnw(text, strlen(text)) + dc.font.height;
677 main(int argc, char *argv[]) {
681 /* command line args */
682 for(i = 1; i < argc; i++)
683 if(!strcmp(argv[i], "-i")) {
684 fstrncmp = strncasecmp;
687 else if(!strcmp(argv[i], "-b"))
689 else if(!strcmp(argv[i], "-fn")) {
690 if(++i < argc) font = argv[i];
692 else if(!strcmp(argv[i], "-nb")) {
693 if(++i < argc) normbgcolor = argv[i];
695 else if(!strcmp(argv[i], "-nf")) {
696 if(++i < argc) normfgcolor = argv[i];
698 else if(!strcmp(argv[i], "-p")) {
699 if(++i < argc) prompt = argv[i];
701 else if(!strcmp(argv[i], "-sb")) {
702 if(++i < argc) selbgcolor = argv[i];
704 else if(!strcmp(argv[i], "-sf")) {
705 if(++i < argc) selfgcolor = argv[i];
707 else if(!strcmp(argv[i], "-v"))
708 eprint("dmenu-"VERSION", © 2006-2008 dmenu engineers, see LICENSE for details\n");
710 eprint("usage: dmenu [-i] [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
711 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
712 setlocale(LC_CTYPE, "");
713 if(!(dpy = XOpenDisplay(0)))
714 eprint("dmenu: cannot open display\n");
715 screen = DefaultScreen(dpy);
716 root = RootWindow(dpy, screen);
718 if(isatty(STDIN_FILENO)) {
720 running = grabkeyboard();
722 else { /* prevent keypress loss */
723 running = grabkeyboard();