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))
22 enum { ColFG, ColBG, ColLast };
27 unsigned long norm[ColLast];
28 unsigned long sel[ColLast];
38 } DC; /* draw context */
40 typedef struct Item Item;
43 Item *next; /* traverses all items */
44 Item *left, *right; /* traverses items matching current search pattern */
47 /* forward declarations */
48 static void appenditem(Item *i, Item **list, Item **last);
49 static void calcoffsets(void);
50 static char *cistrstr(const char *s, const char *sub);
51 static void cleanup(void);
52 static void drawmenu(void);
53 static void drawtext(const char *text, unsigned long col[ColLast]);
54 static void eprint(const char *errstr, ...);
55 static unsigned long getcolor(const char *colstr);
56 static Bool grabkeyboard(void);
57 static void initfont(const char *fontstr);
58 static void kpress(XKeyEvent * e);
59 static void match(char *pattern);
60 static void readstdin(void);
61 static void run(void);
62 static void setup(Bool topbar);
63 static int textnw(const char *text, unsigned int len);
64 static int textw(const char *text);
69 static char *maxname = NULL;
70 static char *prompt = NULL;
71 static char text[4096];
73 static int promptw = 0;
76 static unsigned int mw, mh;
77 static unsigned int numlockmask = 0;
78 static Bool running = True;
81 static Item *allitems = NULL; /* first of all items */
82 static Item *item = NULL; /* first of pattern matching items */
83 static Item *sel = NULL;
84 static Item *next = NULL;
85 static Item *prev = NULL;
86 static Item *curr = NULL;
87 static Window root, win;
88 static int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
89 static char *(*fstrstr)(const char *, const char *) = strstr;
92 appenditem(Item *i, Item **list, Item **last) {
109 w = promptw + cmdw + 2 * spaceitem;
110 for(next = curr; next; next=next->right) {
111 tw = textw(next->text);
118 w = promptw + cmdw + 2 * spaceitem;
119 for(prev = curr; prev && prev->left; prev=prev->left) {
120 tw = textw(prev->left->text);
130 cistrstr(const char *s, const char *sub) {
136 if((c = *sub++) != 0) {
141 if((csub = *s++) == 0)
144 while(tolower(csub) != c);
146 while(strncasecmp(s, sub, len) != 0);
157 itm = allitems->next;
158 free(allitems->text);
163 XFreeFontSet(dpy, dc.font.set);
165 XFreeFont(dpy, dc.font.xfont);
166 XFreePixmap(dpy, dc.drawable);
168 XDestroyWindow(dpy, win);
169 XUngrabKeyboard(dpy, CurrentTime);
180 drawtext(NULL, dc.norm);
184 drawtext(prompt, dc.sel);
191 drawtext(text[0] ? text : NULL, dc.norm);
195 drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
197 /* determine maximum items */
198 for(i = curr; i != next; i=i->right) {
199 dc.w = textw(i->text);
202 drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
205 dc.x = mw - spaceitem;
207 drawtext(next ? ">" : NULL, dc.norm);
209 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
214 drawtext(const char *text, unsigned long col[ColLast]) {
216 static char buf[256];
217 unsigned int len, olen;
218 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
220 XSetForeground(dpy, dc.gc, col[ColBG]);
221 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
225 olen = len = strlen(text);
226 if(len >= sizeof buf)
227 len = sizeof buf - 1;
228 memcpy(buf, text, len);
230 h = dc.font.ascent + dc.font.descent;
231 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
233 /* shorten text if necessary */
234 while(len && (w = textnw(buf, len)) > dc.w - h)
245 return; /* too long */
246 XSetForeground(dpy, dc.gc, col[ColFG]);
248 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
250 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
254 eprint(const char *errstr, ...) {
257 va_start(ap, errstr);
258 vfprintf(stderr, errstr, ap);
264 getcolor(const char *colstr) {
265 Colormap cmap = DefaultColormap(dpy, screen);
268 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
269 eprint("error, cannot allocate color '%s'\n", colstr);
277 for(len = 1000; len; len--) {
278 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
287 initfont(const char *fontstr) {
288 char *def, **missing;
291 if(!fontstr || fontstr[0] == '\0')
292 eprint("error, cannot load font: '%s'\n", fontstr);
295 XFreeFontSet(dpy, dc.font.set);
296 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
298 XFreeStringList(missing);
300 XFontSetExtents *font_extents;
301 XFontStruct **xfonts;
303 dc.font.ascent = dc.font.descent = 0;
304 font_extents = XExtentsOfFontSet(dc.font.set);
305 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
306 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
307 if(dc.font.ascent < (*xfonts)->ascent)
308 dc.font.ascent = (*xfonts)->ascent;
309 if(dc.font.descent < (*xfonts)->descent)
310 dc.font.descent = (*xfonts)->descent;
316 XFreeFont(dpy, dc.font.xfont);
317 dc.font.xfont = NULL;
318 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
319 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
320 eprint("error, cannot load font: '%s'\n", fontstr);
321 dc.font.ascent = dc.font.xfont->ascent;
322 dc.font.descent = dc.font.xfont->descent;
324 dc.font.height = dc.font.ascent + dc.font.descent;
328 kpress(XKeyEvent * e) {
336 num = XLookupString(e, buf, sizeof buf, &ksym, 0);
337 if(IsKeypadKey(ksym)) {
338 if(ksym == XK_KP_Enter)
340 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
341 ksym = (ksym - XK_KP_0) + XK_0;
343 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
344 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
345 || IsPrivateKeypadKey(ksym))
347 /* first check if a control mask is omitted */
348 if(e->state & ControlMask) {
350 default: /* ignore other control sequences */
377 while(i >= 0 && text[i] == ' ')
379 while(i >= 0 && text[i] != ' ')
387 if(CLEANMASK(e->state) & Mod1Mask) {
412 if(num && !iscntrl((int) buf[0])) {
415 strncat(text, buf, sizeof text);
417 strncpy(text, buf, sizeof text);
434 while(sel && sel->right)
448 if(!(sel && sel->left))
451 if(sel->right == curr) {
469 if((e->state & ShiftMask) && *text)
470 fprintf(stdout, "%s", text);
472 fprintf(stdout, "%s", sel->text);
474 fprintf(stdout, "%s", text);
479 if(!(sel && sel->right))
490 strncpy(text, sel->text, sizeof text);
498 match(char *pattern) {
500 Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
504 plen = strlen(pattern);
505 item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
506 for(i = allitems; i; i = i->next)
507 if(!fstrncmp(pattern, i->text, plen + 1))
508 appenditem(i, &lexact, &exactend);
509 else if(!fstrncmp(pattern, i->text, plen))
510 appenditem(i, &lprefix, &prefixend);
511 else if(fstrstr(i->text, pattern))
512 appenditem(i, &lsubstr, &substrend);
519 itemend->right = lprefix;
520 lprefix->left = itemend;
528 itemend->right = lsubstr;
529 lsubstr->left = itemend;
534 curr = prev = next = sel = item;
541 unsigned int len = 0, max = 0;
545 while(fgets(buf, sizeof buf, stdin)) {
547 if (buf[len - 1] == '\n')
549 if(!(p = strdup(buf)))
550 eprint("fatal: could not strdup() %u bytes\n", strlen(buf));
555 if((new = (Item *)malloc(sizeof(Item))) == NULL)
556 eprint("fatal: could not malloc() %u bytes\n", sizeof(Item));
557 new->next = new->left = new->right = NULL;
571 /* main event loop */
572 while(running && !XNextEvent(dpy, &ev))
574 default: /* ignore all crap */
580 if(ev.xexpose.count == 0)
589 XModifierKeymap *modmap;
590 XSetWindowAttributes wa;
592 XineramaScreenInfo *info = NULL;
595 /* init modifier map */
596 modmap = XGetModifierMapping(dpy);
597 for(i = 0; i < 8; i++)
598 for(j = 0; j < modmap->max_keypermod; j++) {
599 if(modmap->modifiermap[i * modmap->max_keypermod + j]
600 == XKeysymToKeycode(dpy, XK_Num_Lock))
601 numlockmask = (1 << i);
603 XFreeModifiermap(modmap);
606 dc.norm[ColBG] = getcolor(normbgcolor);
607 dc.norm[ColFG] = getcolor(normfgcolor);
608 dc.sel[ColBG] = getcolor(selbgcolor);
609 dc.sel[ColFG] = getcolor(selfgcolor);
613 wa.override_redirect = 1;
614 wa.background_pixmap = ParentRelative;
615 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
617 /* menu window geometry */
618 mh = dc.font.height + 2;
620 if(XineramaIsActive(dpy)) {
621 info = XineramaQueryScreens(dpy, &i);
622 x = info[xidx].x_org;
623 y = topbar ? info[xidx].y_org : info[xidx].y_org + info[xidx].height - mh;
624 mw = info[xidx].width;
631 y = topbar ? 0 : DisplayHeight(dpy, screen) - mh;
632 mw = DisplayWidth(dpy, screen);
635 win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
636 DefaultDepth(dpy, screen), CopyFromParent,
637 DefaultVisual(dpy, screen),
638 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
641 dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
642 dc.gc = XCreateGC(dpy, root, 0, 0);
643 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
645 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
647 cmdw = textw(maxname);
651 promptw = textw(prompt);
656 XMapRaised(dpy, win);
660 textnw(const char *text, unsigned int len) {
664 XmbTextExtents(dc.font.set, text, len, NULL, &r);
667 return XTextWidth(dc.font.xfont, text, len);
671 textw(const char *text) {
672 return textnw(text, strlen(text)) + dc.font.height;
676 main(int argc, char *argv[]) {
680 /* command line args */
681 for(i = 1; i < argc; i++)
682 if(!strcmp(argv[i], "-i")) {
683 fstrncmp = strncasecmp;
686 else if(!strcmp(argv[i], "-b"))
688 else if(!strcmp(argv[i], "-fn")) {
689 if(++i < argc) font = argv[i];
691 else if(!strcmp(argv[i], "-nb")) {
692 if(++i < argc) normbgcolor = argv[i];
694 else if(!strcmp(argv[i], "-nf")) {
695 if(++i < argc) normfgcolor = argv[i];
697 else if(!strcmp(argv[i], "-p")) {
698 if(++i < argc) prompt = argv[i];
700 else if(!strcmp(argv[i], "-sb")) {
701 if(++i < argc) selbgcolor = argv[i];
703 else if(!strcmp(argv[i], "-sf")) {
704 if(++i < argc) selfgcolor = argv[i];
706 else if(!strcmp(argv[i], "-v"))
707 eprint("dmenu-"VERSION", © 2006-2008 dmenu engineers, see LICENSE for details\n");
709 eprint("usage: dmenu [-i] [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
710 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
711 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
712 fprintf(stderr, "warning: no locale support\n");
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();