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 */
42 /* forward declarations */
43 Item *appenditem(Item *i, Item *last);
44 void calcoffsets(void);
47 void drawtext(const char *text, unsigned long col[ColLast]);
48 void *emalloc(unsigned int size);
49 void eprint(const char *errstr, ...);
50 char *estrdup(const char *str);
51 unsigned long getcolor(const char *colstr);
52 Bool grabkeyboard(void);
53 void initfont(const char *fontstr);
54 void kpress(XKeyEvent * e);
55 void match(char *pattern);
58 void setup(int x, int y, int w);
59 char *cistrstr(const char *s, const char *sub);
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 nitem = 0;
80 unsigned int numlockmask = 0;
84 Item *allitems = NULL; /* first of all items */
85 Item *item = NULL; /* first of pattern matching items */
91 int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
92 char *(*fstrstr)(const char *, const char *) = strstr;
95 appenditem(Item *i, 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);
138 itm = allitems->next;
139 free(allitems->text);
144 XFreeFontSet(dpy, dc.font.set);
146 XFreeFont(dpy, dc.font.xfont);
147 XFreePixmap(dpy, dc.drawable);
149 XDestroyWindow(dpy, win);
150 XUngrabKeyboard(dpy, CurrentTime);
161 drawtext(NULL, dc.norm);
165 drawtext(prompt, dc.sel);
172 drawtext(text[0] ? text : NULL, dc.norm);
176 drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
178 /* determine maximum items */
179 for(i = curr; i != next; i=i->right) {
180 dc.w = textw(i->text);
183 drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
188 drawtext(next ? ">" : NULL, dc.norm);
190 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
195 drawtext(const char *text, unsigned long col[ColLast]) {
197 static char buf[256];
198 unsigned int len, olen;
199 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
201 XSetForeground(dpy, dc.gc, col[ColBG]);
202 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
206 olen = len = strlen(text);
207 if(len >= sizeof buf)
208 len = sizeof buf - 1;
209 memcpy(buf, text, len);
211 h = dc.font.ascent + dc.font.descent;
212 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
214 /* shorten text if necessary */
215 while(len && (w = textnw(buf, len)) > dc.w - h)
226 return; /* too long */
227 XSetForeground(dpy, dc.gc, col[ColFG]);
229 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
231 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
235 emalloc(unsigned int size) {
236 void *res = malloc(size);
239 eprint("fatal: could not malloc() %u bytes\n", size);
244 eprint(const char *errstr, ...) {
247 va_start(ap, errstr);
248 vfprintf(stderr, errstr, ap);
254 estrdup(const char *str) {
255 void *res = strdup(str);
258 eprint("fatal: could not malloc() %u bytes\n", strlen(str));
263 getcolor(const char *colstr) {
264 Colormap cmap = DefaultColormap(dpy, screen);
267 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
268 eprint("error, cannot allocate color '%s'\n", colstr);
276 for(len = 1000; len; len--) {
277 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
286 initfont(const char *fontstr) {
287 char *def, **missing;
290 if(!fontstr || fontstr[0] == '\0')
291 eprint("error, cannot load font: '%s'\n", fontstr);
294 XFreeFontSet(dpy, dc.font.set);
295 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
297 XFreeStringList(missing);
299 XFontSetExtents *font_extents;
300 XFontStruct **xfonts;
302 dc.font.ascent = dc.font.descent = 0;
303 font_extents = XExtentsOfFontSet(dc.font.set);
304 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
305 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
306 if(dc.font.ascent < (*xfonts)->ascent)
307 dc.font.ascent = (*xfonts)->ascent;
308 if(dc.font.descent < (*xfonts)->descent)
309 dc.font.descent = (*xfonts)->descent;
315 XFreeFont(dpy, dc.font.xfont);
316 dc.font.xfont = NULL;
317 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
318 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
319 eprint("error, cannot load font: '%s'\n", fontstr);
320 dc.font.ascent = dc.font.xfont->ascent;
321 dc.font.descent = dc.font.xfont->descent;
323 dc.font.height = dc.font.ascent + dc.font.descent;
327 kpress(XKeyEvent * e) {
335 num = XLookupString(e, buf, sizeof buf, &ksym, 0);
336 if(IsKeypadKey(ksym)) {
337 if(ksym == XK_KP_Enter) {
339 } else if(ksym >= XK_KP_0 && ksym <= XK_KP_9) {
340 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) {
504 plen = strlen(pattern);
507 for(i = allitems; i; i = i->next)
508 if(!fstrncmp(pattern, i->text, plen))
509 j = appenditem(i, j);
510 else if(fstrstr(i->text, pattern))
511 j = appenditem(i, j);
512 curr = prev = next = sel = item;
519 unsigned int len = 0, max = 0;
523 while(fgets(buf, sizeof buf, stdin)) {
525 if (buf[len - 1] == '\n')
532 new = emalloc(sizeof(Item));
533 new->next = new->left = new->right = NULL;
547 /* main event loop */
548 while(running && !XNextEvent(dpy, &ev))
550 default: /* ignore all crap */
556 if(ev.xexpose.count == 0)
563 setup(int x, int y, int w) {
565 XModifierKeymap *modmap;
566 XSetWindowAttributes wa;
568 /* init modifier map */
569 modmap = XGetModifierMapping(dpy);
570 for(i = 0; i < 8; i++)
571 for(j = 0; j < modmap->max_keypermod; j++) {
572 if(modmap->modifiermap[i * modmap->max_keypermod + j]
573 == XKeysymToKeycode(dpy, XK_Num_Lock))
574 numlockmask = (1 << i);
576 XFreeModifiermap(modmap);
579 dc.norm[ColBG] = getcolor(normbg);
580 dc.norm[ColFG] = getcolor(normfg);
581 dc.sel[ColBG] = getcolor(selbg);
582 dc.sel[ColFG] = getcolor(selfg);
586 wa.override_redirect = 1;
587 wa.background_pixmap = ParentRelative;
588 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
589 mw = w ? w : DisplayWidth(dpy, screen);
590 mh = dc.font.height + 2;
591 win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
592 DefaultDepth(dpy, screen), CopyFromParent,
593 DefaultVisual(dpy, screen),
594 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
597 dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
598 dc.gc = XCreateGC(dpy, root, 0, 0);
599 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
601 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
603 cmdw = textw(maxname);
607 promptw = textw(prompt);
612 XMapRaised(dpy, win);
616 cistrstr(const char *s, const char *sub) {
622 if((c = *sub++) != 0) {
627 if((csub = *s++) == 0)
630 while(tolower(csub) != c);
632 while(strncasecmp(s, sub, len) != 0);
639 textnw(const char *text, unsigned int len) {
643 XmbTextExtents(dc.font.set, text, len, NULL, &r);
646 return XTextWidth(dc.font.xfont, text, len);
650 textw(const char *text) {
651 return textnw(text, strlen(text)) + dc.font.height;
655 main(int argc, char *argv[]) {
656 int x = 0, y = 0, w = 0;
659 /* command line args */
660 for(i = 1; i < argc; i++)
661 if(!strcmp(argv[i], "-i")) {
662 fstrncmp = strncasecmp;
665 else if(!strcmp(argv[i], "-fn")) {
666 if(++i < argc) font = argv[i];
668 else if(!strcmp(argv[i], "-nb")) {
669 if(++i < argc) normbg = argv[i];
671 else if(!strcmp(argv[i], "-nf")) {
672 if(++i < argc) normfg = argv[i];
674 else if(!strcmp(argv[i], "-p")) {
675 if(++i < argc) prompt = argv[i];
677 else if(!strcmp(argv[i], "-sb")) {
678 if(++i < argc) selbg = argv[i];
680 else if(!strcmp(argv[i], "-sf")) {
681 if(++i < argc) selfg = argv[i];
683 else if(!strcmp(argv[i], "-x")) {
684 if(++i < argc) x = atoi(argv[i]);
686 else if(!strcmp(argv[i], "-y")) {
687 if(++i < argc) y = atoi(argv[i]);
689 else if(!strcmp(argv[i], "-w")) {
690 if(++i < argc) w = atoi(argv[i]);
692 else if(!strcmp(argv[i], "-v"))
693 eprint("dmenu-"VERSION", © 2006-2008 dmenu engineers, see LICENSE for details\n");
695 eprint("usage: dmenu [-i] [-fn <font>] [-nb <color>] [-nf <color>]\n"
696 " [-p <prompt>] [-sb <color>] [-sf <color>]\n"
697 " [-x <x>] [-y <y>] [-w <w>] [-v]\n");
698 setlocale(LC_CTYPE, "");
699 dpy = XOpenDisplay(0);
701 eprint("dmenu: cannot open display\n");
702 screen = DefaultScreen(dpy);
703 root = RootWindow(dpy, screen);
705 if(isatty(STDIN_FILENO)) {
707 running = grabkeyboard();
709 else { /* prevent keypress loss */
710 running = grabkeyboard();