1 /* See LICENSE file for copyright and license details. */
10 #include <X11/Xutil.h>
11 #include <X11/keysym.h>
13 #include <X11/extensions/Xinerama.h>
17 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
20 enum { ColFG, ColBG, ColLast };
25 unsigned long norm[ColLast];
26 unsigned long sel[ColLast];
36 } DC; /* draw context */
38 typedef struct Item Item;
41 Item *next; /* traverses all items */
42 Item *left, *right; /* traverses items matching current search pattern */
45 /* forward declarations */
46 void appenditem(Item *i, Item **list, Item **last);
47 void calcoffsets(void);
48 char *cistrstr(const char *s, const char *sub);
51 void drawtext(const char *text, unsigned long col[ColLast]);
52 void *emalloc(unsigned int size);
53 void eprint(const char *errstr, ...);
54 char *estrdup(const char *str);
55 unsigned long getcolor(const char *colstr);
56 Bool grabkeyboard(void);
57 void initfont(const char *fontstr);
58 void kpress(XKeyEvent * e);
59 void match(char *pattern);
62 void setup(Bool topbar);
63 unsigned int textnw(const char *text, unsigned int len);
64 unsigned int textw(const char *text);
71 char *normbg = NORMBGCOLOR;
72 char *normfg = NORMFGCOLOR;
74 char *selbg = SELBGCOLOR;
75 char *selfg = SELFGCOLOR;
79 unsigned int cmdw = 0;
81 unsigned int promptw = 0;
82 unsigned int numlockmask = 0;
86 Item *allitems = NULL; /* first of all items */
87 Item *item = NULL; /* first of pattern matching items */
93 int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
94 char *(*fstrstr)(const char *, const char *) = strstr;
97 appenditem(Item *i, Item **list, Item **last) {
113 w = promptw + cmdw + 2 * SPACE;
114 for(next = curr; next; next=next->right) {
115 tw = textw(next->text);
122 w = promptw + cmdw + 2 * SPACE;
123 for(prev = curr; prev && prev->left; prev=prev->left) {
124 tw = textw(prev->left->text);
134 cistrstr(const char *s, const char *sub) {
140 if((c = *sub++) != 0) {
145 if((csub = *s++) == 0)
148 while(tolower(csub) != c);
150 while(strncasecmp(s, sub, len) != 0);
161 itm = allitems->next;
162 free(allitems->text);
167 XFreeFontSet(dpy, dc.font.set);
169 XFreeFont(dpy, dc.font.xfont);
170 XFreePixmap(dpy, dc.drawable);
172 XDestroyWindow(dpy, win);
173 XUngrabKeyboard(dpy, CurrentTime);
184 drawtext(NULL, dc.norm);
188 drawtext(prompt, dc.sel);
195 drawtext(text[0] ? text : NULL, dc.norm);
199 drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
201 /* determine maximum items */
202 for(i = curr; i != next; i=i->right) {
203 dc.w = textw(i->text);
206 drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
211 drawtext(next ? ">" : NULL, dc.norm);
213 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
218 drawtext(const char *text, unsigned long col[ColLast]) {
220 static char buf[256];
221 unsigned int len, olen;
222 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
224 XSetForeground(dpy, dc.gc, col[ColBG]);
225 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
229 olen = len = strlen(text);
230 if(len >= sizeof buf)
231 len = sizeof buf - 1;
232 memcpy(buf, text, len);
234 h = dc.font.ascent + dc.font.descent;
235 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
237 /* shorten text if necessary */
238 while(len && (w = textnw(buf, len)) > dc.w - h)
249 return; /* too long */
250 XSetForeground(dpy, dc.gc, col[ColFG]);
252 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
254 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
258 emalloc(unsigned int size) {
259 void *res = malloc(size);
262 eprint("fatal: could not malloc() %u bytes\n", size);
267 eprint(const char *errstr, ...) {
270 va_start(ap, errstr);
271 vfprintf(stderr, errstr, ap);
277 estrdup(const char *str) {
278 void *res = strdup(str);
281 eprint("fatal: could not malloc() %u bytes\n", strlen(str));
286 getcolor(const char *colstr) {
287 Colormap cmap = DefaultColormap(dpy, screen);
290 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
291 eprint("error, cannot allocate color '%s'\n", colstr);
299 for(len = 1000; len; len--) {
300 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
309 initfont(const char *fontstr) {
310 char *def, **missing;
313 if(!fontstr || fontstr[0] == '\0')
314 eprint("error, cannot load font: '%s'\n", fontstr);
317 XFreeFontSet(dpy, dc.font.set);
318 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
320 XFreeStringList(missing);
322 XFontSetExtents *font_extents;
323 XFontStruct **xfonts;
325 dc.font.ascent = dc.font.descent = 0;
326 font_extents = XExtentsOfFontSet(dc.font.set);
327 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
328 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
329 if(dc.font.ascent < (*xfonts)->ascent)
330 dc.font.ascent = (*xfonts)->ascent;
331 if(dc.font.descent < (*xfonts)->descent)
332 dc.font.descent = (*xfonts)->descent;
338 XFreeFont(dpy, dc.font.xfont);
339 dc.font.xfont = NULL;
340 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
341 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
342 eprint("error, cannot load font: '%s'\n", fontstr);
343 dc.font.ascent = dc.font.xfont->ascent;
344 dc.font.descent = dc.font.xfont->descent;
346 dc.font.height = dc.font.ascent + dc.font.descent;
350 kpress(XKeyEvent * e) {
358 num = XLookupString(e, buf, sizeof buf, &ksym, 0);
359 if(IsKeypadKey(ksym))
360 if(ksym == XK_KP_Enter)
362 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
363 ksym = (ksym - XK_KP_0) + XK_0;
364 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
365 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
366 || IsPrivateKeypadKey(ksym))
368 /* first check if a control mask is omitted */
369 if(e->state & ControlMask) {
371 default: /* ignore other control sequences */
398 while(i >= 0 && text[i] == ' ')
400 while(i >= 0 && text[i] != ' ')
408 if(CLEANMASK(e->state) & Mod1Mask) {
433 if(num && !iscntrl((int) buf[0])) {
436 strncat(text, buf, sizeof text);
438 strncpy(text, buf, sizeof text);
455 while(sel && sel->right)
469 if(!(sel && sel->left))
472 if(sel->right == curr) {
490 if((e->state & ShiftMask) && text)
491 fprintf(stdout, "%s", text);
493 fprintf(stdout, "%s", sel->text);
495 fprintf(stdout, "%s", text);
500 if(!(sel && sel->right))
511 strncpy(text, sel->text, sizeof text);
519 match(char *pattern) {
521 Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
525 plen = strlen(pattern);
526 item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
527 for(i = allitems; i; i = i->next)
528 if(!fstrncmp(pattern, i->text, plen + 1))
529 appenditem(i, &lexact, &exactend);
530 else if(!fstrncmp(pattern, i->text, plen))
531 appenditem(i, &lprefix, &prefixend);
532 else if(fstrstr(i->text, pattern))
533 appenditem(i, &lsubstr, &substrend);
540 itemend->right = lprefix;
541 lprefix->left = itemend;
549 itemend->right = lsubstr;
550 lsubstr->left = itemend;
555 curr = prev = next = sel = item;
562 unsigned int len = 0, max = 0;
566 while(fgets(buf, sizeof buf, stdin)) {
568 if (buf[len - 1] == '\n')
575 new = emalloc(sizeof(Item));
576 new->next = new->left = new->right = NULL;
590 /* main event loop */
591 while(running && !XNextEvent(dpy, &ev))
593 default: /* ignore all crap */
599 if(ev.xexpose.count == 0)
608 XModifierKeymap *modmap;
609 XSetWindowAttributes wa;
611 XineramaScreenInfo *info = NULL;
614 /* init modifier map */
615 modmap = XGetModifierMapping(dpy);
616 for(i = 0; i < 8; i++)
617 for(j = 0; j < modmap->max_keypermod; j++) {
618 if(modmap->modifiermap[i * modmap->max_keypermod + j]
619 == XKeysymToKeycode(dpy, XK_Num_Lock))
620 numlockmask = (1 << i);
622 XFreeModifiermap(modmap);
625 dc.norm[ColBG] = getcolor(normbg);
626 dc.norm[ColFG] = getcolor(normfg);
627 dc.sel[ColBG] = getcolor(selbg);
628 dc.sel[ColFG] = getcolor(selfg);
632 wa.override_redirect = 1;
633 wa.background_pixmap = ParentRelative;
634 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
636 /* menu window geometry */
637 mh = dc.font.height + 2;
639 if(XineramaIsActive(dpy)) {
640 info = XineramaQueryScreens(dpy, &i);
642 y = topbar ? info[0].y_org : info[0].y_org + info[0].height - mh;
650 y = topbar ? 0 : DisplayHeight(dpy, screen) - mh;
651 mw = DisplayWidth(dpy, screen);
654 win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
655 DefaultDepth(dpy, screen), CopyFromParent,
656 DefaultVisual(dpy, screen),
657 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
660 dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
661 dc.gc = XCreateGC(dpy, root, 0, 0);
662 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
664 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
666 cmdw = textw(maxname);
670 promptw = textw(prompt);
675 XMapRaised(dpy, win);
679 textnw(const char *text, unsigned int len) {
683 XmbTextExtents(dc.font.set, text, len, NULL, &r);
686 return XTextWidth(dc.font.xfont, text, len);
690 textw(const char *text) {
691 return textnw(text, strlen(text)) + dc.font.height;
695 main(int argc, char *argv[]) {
699 /* command line args */
700 for(i = 1; i < argc; i++)
701 if(!strcmp(argv[i], "-i")) {
702 fstrncmp = strncasecmp;
705 else if(!strcmp(argv[i], "-b"))
707 else if(!strcmp(argv[i], "-fn")) {
708 if(++i < argc) font = argv[i];
710 else if(!strcmp(argv[i], "-nb")) {
711 if(++i < argc) normbg = argv[i];
713 else if(!strcmp(argv[i], "-nf")) {
714 if(++i < argc) normfg = argv[i];
716 else if(!strcmp(argv[i], "-p")) {
717 if(++i < argc) prompt = argv[i];
719 else if(!strcmp(argv[i], "-sb")) {
720 if(++i < argc) selbg = argv[i];
722 else if(!strcmp(argv[i], "-sf")) {
723 if(++i < argc) selfg = argv[i];
725 else if(!strcmp(argv[i], "-v"))
726 eprint("dmenu-"VERSION", © 2006-2008 dmenu engineers, see LICENSE for details\n");
728 eprint("usage: dmenu [-i] [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
729 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
730 setlocale(LC_CTYPE, "");
731 dpy = XOpenDisplay(0);
733 eprint("dmenu: cannot open display\n");
734 screen = DefaultScreen(dpy);
735 root = RootWindow(dpy, screen);
737 if(isatty(STDIN_FILENO)) {
739 running = grabkeyboard();
741 else { /* prevent keypress loss */
742 running = grabkeyboard();