1 /* See LICENSE file for copyright and license details. */
10 #include <X11/Xutil.h>
11 #include <X11/keysym.h>
14 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
17 enum { ColFG, ColBG, ColLast };
22 unsigned long norm[ColLast];
23 unsigned long sel[ColLast];
33 } DC; /* draw context */
35 typedef struct Item Item;
37 Item *next; /* traverses all items */
38 Item *left, *right; /* traverses items matching current search pattern */
43 /* forward declarations */
44 Item *appenditem(Item *i, Item *last);
45 void calcoffsets(void);
48 void drawtext(const char *text, unsigned long col[ColLast]);
49 void *emalloc(unsigned int size);
50 void eprint(const char *errstr, ...);
51 char *estrdup(const char *str);
52 unsigned long getcolor(const char *colstr);
53 Bool grabkeyboard(void);
54 void initfont(const char *fontstr);
55 void kpress(XKeyEvent * e);
56 void match(char *pattern);
59 void setup(int x, int y, int w);
60 char *cistrstr(const char *s, const char *sub);
61 unsigned int textnw(const char *text, unsigned int len);
62 unsigned int textw(const char *text);
69 char *normbg = NORMBGCOLOR;
70 char *normfg = NORMFGCOLOR;
72 char *selbg = SELBGCOLOR;
73 char *selfg = SELFGCOLOR;
77 unsigned int cmdw = 0;
79 unsigned int promptw = 0;
80 unsigned int nitem = 0;
81 unsigned int numlockmask = 0;
85 Item *allitems = NULL; /* first of all items */
86 Item *item = NULL; /* first of pattern matching items */
92 char *(*fstrstr)(const char *, const char *) = strstr;
95 appenditem(Item *i, Item *last) {
114 w = promptw + cmdw + 2 * SPACE;
115 for(next = curr; next; next=next->right) {
116 tw = textw(next->text);
123 w = promptw + cmdw + 2 * SPACE;
124 for(prev = curr; prev && prev->left; prev=prev->left) {
125 tw = textw(prev->left->text);
139 itm = allitems->next;
140 free(allitems->text);
145 XFreeFontSet(dpy, dc.font.set);
147 XFreeFont(dpy, dc.font.xfont);
148 XFreePixmap(dpy, dc.drawable);
150 XDestroyWindow(dpy, win);
151 XUngrabKeyboard(dpy, CurrentTime);
162 drawtext(NULL, dc.norm);
166 drawtext(prompt, dc.sel);
173 drawtext(text[0] ? text : NULL, dc.norm);
177 drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
179 /* determine maximum items */
180 for(i = curr; i != next; i=i->right) {
181 dc.w = textw(i->text);
184 drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
189 drawtext(next ? ">" : NULL, dc.norm);
191 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
196 drawtext(const char *text, unsigned long col[ColLast]) {
198 static char buf[256];
199 unsigned int len, olen;
200 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
202 XSetForeground(dpy, dc.gc, col[ColBG]);
203 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
207 olen = len = strlen(text);
208 if(len >= sizeof buf)
209 len = sizeof buf - 1;
210 memcpy(buf, text, len);
212 h = dc.font.ascent + dc.font.descent;
213 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
215 /* shorten text if necessary */
216 while(len && (w = textnw(buf, len)) > dc.w - h)
227 return; /* too long */
228 XSetForeground(dpy, dc.gc, col[ColFG]);
230 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
232 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
236 emalloc(unsigned int size) {
237 void *res = malloc(size);
240 eprint("fatal: could not malloc() %u bytes\n", size);
245 eprint(const char *errstr, ...) {
248 va_start(ap, errstr);
249 vfprintf(stderr, errstr, ap);
255 estrdup(const char *str) {
256 void *res = strdup(str);
259 eprint("fatal: could not malloc() %u bytes\n", strlen(str));
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;
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) {
505 plen = strlen(pattern);
508 for(i = allitems; i; i=i->next)
510 for(i = allitems; i; i = i->next)
511 if(!i->matched && !strncasecmp(pattern, i->text, plen))
512 j = appenditem(i, j);
513 for(i = allitems; i; i = i->next)
514 if(!i->matched && fstrstr(i->text, pattern))
515 j = appenditem(i, j);
516 curr = prev = next = sel = item;
523 unsigned int len = 0, max = 0;
527 while(fgets(buf, sizeof buf, stdin)) {
529 if (buf[len - 1] == '\n')
536 new = emalloc(sizeof(Item));
537 new->next = new->left = new->right = NULL;
551 /* main event loop */
552 while(running && !XNextEvent(dpy, &ev))
554 default: /* ignore all crap */
560 if(ev.xexpose.count == 0)
567 setup(int x, int y, int w) {
569 XModifierKeymap *modmap;
570 XSetWindowAttributes wa;
572 /* init modifier map */
573 modmap = XGetModifierMapping(dpy);
574 for(i = 0; i < 8; i++)
575 for(j = 0; j < modmap->max_keypermod; j++) {
576 if(modmap->modifiermap[i * modmap->max_keypermod + j]
577 == XKeysymToKeycode(dpy, XK_Num_Lock))
578 numlockmask = (1 << i);
580 XFreeModifiermap(modmap);
583 dc.norm[ColBG] = getcolor(normbg);
584 dc.norm[ColFG] = getcolor(normfg);
585 dc.sel[ColBG] = getcolor(selbg);
586 dc.sel[ColFG] = getcolor(selfg);
590 wa.override_redirect = 1;
591 wa.background_pixmap = ParentRelative;
592 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
593 mw = w ? w : DisplayWidth(dpy, screen);
594 mh = dc.font.height + 2;
595 win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
596 DefaultDepth(dpy, screen), CopyFromParent,
597 DefaultVisual(dpy, screen),
598 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
601 dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
602 dc.gc = XCreateGC(dpy, root, 0, 0);
603 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
605 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
607 cmdw = textw(maxname);
611 promptw = textw(prompt);
616 XMapRaised(dpy, win);
620 cistrstr(const char *s, const char *sub) {
626 if((c = *sub++) != 0) {
631 if((csub = *s++) == 0)
634 while(tolower(csub) != c);
636 while(strncasecmp(s, sub, len) != 0);
643 textnw(const char *text, unsigned int len) {
647 XmbTextExtents(dc.font.set, text, len, NULL, &r);
650 return XTextWidth(dc.font.xfont, text, len);
654 textw(const char *text) {
655 return textnw(text, strlen(text)) + dc.font.height;
659 main(int argc, char *argv[]) {
660 int x = 0, y = 0, w = 0;
663 /* command line args */
664 for(i = 1; i < argc; i++)
665 if(!strcmp(argv[i], "-i"))
667 else if(!strcmp(argv[i], "-fn")) {
668 if(++i < argc) font = argv[i];
670 else if(!strcmp(argv[i], "-nb")) {
671 if(++i < argc) normbg = argv[i];
673 else if(!strcmp(argv[i], "-nf")) {
674 if(++i < argc) normfg = argv[i];
676 else if(!strcmp(argv[i], "-p")) {
677 if(++i < argc) prompt = argv[i];
679 else if(!strcmp(argv[i], "-sb")) {
680 if(++i < argc) selbg = argv[i];
682 else if(!strcmp(argv[i], "-sf")) {
683 if(++i < argc) selfg = argv[i];
685 else if(!strcmp(argv[i], "-x")) {
686 if(++i < argc) x = atoi(argv[i]);
688 else if(!strcmp(argv[i], "-y")) {
689 if(++i < argc) y = atoi(argv[i]);
691 else if(!strcmp(argv[i], "-w")) {
692 if(++i < argc) w = atoi(argv[i]);
694 else if(!strcmp(argv[i], "-v"))
695 eprint("dmenu-"VERSION", © 2006-2008 dmenu engineers, see LICENSE for details\n");
697 eprint("usage: dmenu [-i] [-fn <font>] [-nb <color>] [-nf <color>]\n"
698 " [-p <prompt>] [-sb <color>] [-sf <color>]\n"
699 " [-x <x>] [-y <y>] [-w <w>] [-v]\n");
700 setlocale(LC_CTYPE, "");
701 dpy = XOpenDisplay(0);
703 eprint("dmenu: cannot open display\n");
704 screen = DefaultScreen(dpy);
705 root = RootWindow(dpy, screen);
707 if(isatty(STDIN_FILENO)) {
709 running = grabkeyboard();
711 else { /* prevent keypress loss */
712 running = grabkeyboard();