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;
38 Item *next; /* traverses all items */
39 Item *left, *right; /* traverses items matching current search pattern */
42 /* forward declarations */
43 void appenditem(Item *i, Item **list, Item **last);
44 void calcoffsets(void);
45 char *cistrstr(const char *s, const char *sub);
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 unsigned int textnw(const char *text, unsigned int len);
61 unsigned int textw(const char *text);
68 char *normbg = NORMBGCOLOR;
69 char *normfg = NORMFGCOLOR;
71 char *selbg = SELBGCOLOR;
72 char *selfg = SELFGCOLOR;
76 unsigned int cmdw = 0;
78 unsigned int promptw = 0;
79 unsigned int numlockmask = 0;
83 Item *allitems = NULL; /* first of all items */
84 Item *item = NULL; /* first of pattern matching items */
90 int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
91 char *(*fstrstr)(const char *, const char *) = strstr;
94 appenditem(Item *i, Item **list, Item **last) {
110 w = promptw + cmdw + 2 * SPACE;
111 for(next = curr; next; next=next->right) {
112 tw = textw(next->text);
119 w = promptw + cmdw + 2 * SPACE;
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);
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, unsigned long col[ColLast]) {
217 static char buf[256];
218 unsigned int len, olen;
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 emalloc(unsigned int size) {
256 void *res = malloc(size);
259 eprint("fatal: could not malloc() %u bytes\n", size);
264 eprint(const char *errstr, ...) {
267 va_start(ap, errstr);
268 vfprintf(stderr, errstr, ap);
274 estrdup(const char *str) {
275 void *res = strdup(str);
278 eprint("fatal: could not malloc() %u bytes\n", strlen(str));
283 getcolor(const char *colstr) {
284 Colormap cmap = DefaultColormap(dpy, screen);
287 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
288 eprint("error, cannot allocate color '%s'\n", colstr);
296 for(len = 1000; len; len--) {
297 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
306 initfont(const char *fontstr) {
307 char *def, **missing;
310 if(!fontstr || fontstr[0] == '\0')
311 eprint("error, cannot load font: '%s'\n", fontstr);
314 XFreeFontSet(dpy, dc.font.set);
315 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
317 XFreeStringList(missing);
319 XFontSetExtents *font_extents;
320 XFontStruct **xfonts;
322 dc.font.ascent = dc.font.descent = 0;
323 font_extents = XExtentsOfFontSet(dc.font.set);
324 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
325 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
326 if(dc.font.ascent < (*xfonts)->ascent)
327 dc.font.ascent = (*xfonts)->ascent;
328 if(dc.font.descent < (*xfonts)->descent)
329 dc.font.descent = (*xfonts)->descent;
335 XFreeFont(dpy, dc.font.xfont);
336 dc.font.xfont = NULL;
337 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
338 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
339 eprint("error, cannot load font: '%s'\n", fontstr);
340 dc.font.ascent = dc.font.xfont->ascent;
341 dc.font.descent = dc.font.xfont->descent;
343 dc.font.height = dc.font.ascent + dc.font.descent;
347 kpress(XKeyEvent * e) {
355 num = XLookupString(e, buf, sizeof buf, &ksym, 0);
356 if(IsKeypadKey(ksym))
357 if(ksym == XK_KP_Enter)
359 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
360 ksym = (ksym - XK_KP_0) + XK_0;
361 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
362 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
363 || IsPrivateKeypadKey(ksym))
365 /* first check if a control mask is omitted */
366 if(e->state & ControlMask) {
368 default: /* ignore other control sequences */
395 while(i >= 0 && text[i] == ' ')
397 while(i >= 0 && text[i] != ' ')
405 if(CLEANMASK(e->state) & Mod1Mask) {
430 if(num && !iscntrl((int) buf[0])) {
433 strncat(text, buf, sizeof text);
435 strncpy(text, buf, sizeof text);
452 while(sel && sel->right)
466 if(!(sel && sel->left))
469 if(sel->right == curr) {
487 if((e->state & ShiftMask) && text)
488 fprintf(stdout, "%s", text);
490 fprintf(stdout, "%s", sel->text);
492 fprintf(stdout, "%s", text);
497 if(!(sel && sel->right))
508 strncpy(text, sel->text, sizeof text);
516 match(char *pattern) {
518 Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
522 plen = strlen(pattern);
523 item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
524 for(i = allitems; i; i = i->next)
525 if(!fstrncmp(pattern, i->text, plen + 1))
526 appenditem(i, &lexact, &exactend);
527 else if(!fstrncmp(pattern, i->text, plen))
528 appenditem(i, &lprefix, &prefixend);
529 else if(fstrstr(i->text, pattern))
530 appenditem(i, &lsubstr, &substrend);
537 itemend->right - lprefix;
538 lprefix->left = itemend;
546 itemend->right = lsubstr;
547 lsubstr->left = itemend;
552 curr = prev = next = sel = item;
559 unsigned int len = 0, max = 0;
563 while(fgets(buf, sizeof buf, stdin)) {
565 if (buf[len - 1] == '\n')
572 new = emalloc(sizeof(Item));
573 new->next = new->left = new->right = NULL;
587 /* main event loop */
588 while(running && !XNextEvent(dpy, &ev))
590 default: /* ignore all crap */
596 if(ev.xexpose.count == 0)
603 setup(int x, int y, int w) {
605 XModifierKeymap *modmap;
606 XSetWindowAttributes wa;
608 /* init modifier map */
609 modmap = XGetModifierMapping(dpy);
610 for(i = 0; i < 8; i++)
611 for(j = 0; j < modmap->max_keypermod; j++) {
612 if(modmap->modifiermap[i * modmap->max_keypermod + j]
613 == XKeysymToKeycode(dpy, XK_Num_Lock))
614 numlockmask = (1 << i);
616 XFreeModifiermap(modmap);
619 dc.norm[ColBG] = getcolor(normbg);
620 dc.norm[ColFG] = getcolor(normfg);
621 dc.sel[ColBG] = getcolor(selbg);
622 dc.sel[ColFG] = getcolor(selfg);
626 wa.override_redirect = 1;
627 wa.background_pixmap = ParentRelative;
628 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
629 mw = w ? w : DisplayWidth(dpy, screen);
630 mh = dc.font.height + 2;
632 if(y == (int)(unsigned int)-1)
633 y = DisplayHeight(dpy, screen) - mh;
637 win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
638 DefaultDepth(dpy, screen), CopyFromParent,
639 DefaultVisual(dpy, screen),
640 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
643 dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
644 dc.gc = XCreateGC(dpy, root, 0, 0);
645 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
647 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
649 cmdw = textw(maxname);
653 promptw = textw(prompt);
658 XMapRaised(dpy, win);
662 textnw(const char *text, unsigned int len) {
666 XmbTextExtents(dc.font.set, text, len, NULL, &r);
669 return XTextWidth(dc.font.xfont, text, len);
673 textw(const char *text) {
674 return textnw(text, strlen(text)) + dc.font.height;
678 main(int argc, char *argv[]) {
679 int x = 0, y = 0, w = 0;
682 /* command line args */
683 for(i = 1; i < argc; i++)
684 if(!strcmp(argv[i], "-i")) {
685 fstrncmp = strncasecmp;
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) normbg = argv[i];
694 else if(!strcmp(argv[i], "-nf")) {
695 if(++i < argc) normfg = 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) selbg = argv[i];
703 else if(!strcmp(argv[i], "-sf")) {
704 if(++i < argc) selfg = argv[i];
706 else if(!strcmp(argv[i], "-x")) {
707 if(++i < argc) x = atoi(argv[i]);
709 else if(!strcmp(argv[i], "-y")) {
711 if(!strcmp(argv[i], "-0"))
712 y = (int)(unsigned int)-1;
717 else if(!strcmp(argv[i], "-w")) {
718 if(++i < argc) w = atoi(argv[i]);
720 else if(!strcmp(argv[i], "-v"))
721 eprint("dmenu-"VERSION", © 2006-2008 dmenu engineers, see LICENSE for details\n");
723 eprint("usage: dmenu [-i] [-fn <font>] [-nb <color>] [-nf <color>]\n"
724 " [-p <prompt>] [-sb <color>] [-sf <color>]\n"
725 " [-x <x>] [-y <y>] [-w <w>] [-v]\n");
726 setlocale(LC_CTYPE, "");
727 dpy = XOpenDisplay(0);
729 eprint("dmenu: cannot open display\n");
730 screen = DefaultScreen(dpy);
731 root = RootWindow(dpy, screen);
733 if(isatty(STDIN_FILENO)) {
735 running = grabkeyboard();
737 else { /* prevent keypress loss */
738 running = grabkeyboard();