1 /* See LICENSE file for copyright and license details. */
8 #include <X11/keysym.h>
10 #include <X11/Xutil.h>
12 #include <X11/extensions/Xinerama.h>
17 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
18 #define MIN(a, b) ((a) < (b) ? (a) : (b))
19 #define MAX(a, b) ((a) > (b) ? (a) : (b))
20 #define IS_UTF8_1ST_CHAR(c) ((((c) & 0xc0) == 0xc0) || !((c) & 0x80))
22 typedef struct Item Item;
25 Item *next; /* traverses all items */
26 Item *left, *right; /* traverses items matching current search pattern */
29 /* forward declarations */
30 static void appenditem(Item *i, Item **list, Item **last);
31 static void calcoffsetsh(void);
32 static void calcoffsetsv(void);
33 static char *cistrstr(const char *s, const char *sub);
34 static void cleanup(void);
35 static void dinput(void);
36 static void drawmenu(void);
37 static void drawmenuh(void);
38 static void drawmenuv(void);
39 static void grabkeyboard(void);
40 static void kpress(XKeyEvent *e);
41 static void match(char *pattern);
42 static void readstdin(void);
43 static void run(void);
44 static void setup(void);
49 static char **argp = NULL;
50 static char *maxname = NULL;
51 static char *prompt = NULL;
52 static char text[4096];
54 static int promptw = 0;
56 static unsigned int lines = 0;
57 static unsigned int numlockmask = 0;
58 static unsigned int mw, mh;
59 static unsigned long normcol[ColLast];
60 static unsigned long selcol[ColLast];
61 static Bool topbar = True;
64 static Item *allitems = NULL; /* first of all items */
65 static Item *item = NULL; /* first of pattern matching items */
66 static Item *sel = NULL;
67 static Item *next = NULL;
68 static Item *prev = NULL;
69 static Item *curr = NULL;
70 static Window win, root;
71 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
72 static char *(*fstrstr)(const char *, const char *) = strstr;
73 static void (*calcoffsets)(void) = calcoffsetsh;
76 appenditem(Item *i, Item **list, Item **last) {
90 x = promptw + cmdw + (2 * spaceitem);
91 for(next = curr; next; next = next->right)
92 if((x += MIN(textw(&dc, next->text), mw / 3)) > mw)
94 x = promptw + cmdw + (2 * spaceitem);
95 for(prev = curr; prev && prev->left; prev = prev->left)
96 if((x += MIN(textw(&dc, prev->left->text), mw / 3)) > mw)
105 for(i = 0; i < lines && next; i++)
107 mh = (dc.font.height + 2) * (i + 1);
108 for(i = 0; i < lines && prev && prev->left; i++)
113 cistrstr(const char *s, const char *sub) {
119 if((c = tolower(*sub++)) != '\0') {
123 if((csub = *s++) == '\0')
126 while(tolower(csub) != c);
128 while(strncasecmp(s, sub, len) != 0);
139 itm = allitems->next;
140 free(allitems->text);
145 XDestroyWindow(dpy, win);
146 XUngrabKeyboard(dpy, CurrentTime);
155 execvp("dinput", argp);
156 eprint("cannot exec dinput\n");
165 drawtext(&dc, NULL, normcol);
166 dc.h = dc.font.height + 2;
167 dc.y = topbar ? 0 : mh - dc.h;
171 drawtext(&dc, prompt, selcol);
176 if(cmdw && item && lines == 0)
178 drawtext(&dc, text, normcol);
183 commitdraw(&dc, win);
192 drawtext(&dc, curr->left ? "<" : NULL, normcol);
194 for(i = curr; i != next; i = i->right) {
195 dc.w = MIN(textw(&dc, i->text), mw / 3);
196 drawtext(&dc, i->text, (sel == i) ? selcol : normcol);
201 drawtext(&dc, next ? ">" : NULL, normcol);
207 XWindowAttributes wa;
209 dc.y = topbar ? dc.h : 0;
211 for(i = curr; i != next; i = i->right) {
212 drawtext(&dc, i->text, (sel == i) ? selcol : normcol);
215 if(!XGetWindowAttributes(dpy, win, &wa))
216 eprint("cannot get window attributes");
217 XMoveResizeWindow(dpy, win, wa.x, wa.y + (topbar ? 0 : wa.height - mh), mw, mh);
224 for(len = 1000; len; len--) {
225 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
234 kpress(XKeyEvent *e) {
235 char buf[sizeof text];
241 num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
242 if(ksym == XK_KP_Enter)
244 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
245 ksym = (ksym - XK_KP_0) + XK_0;
246 else if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
247 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
248 || IsPrivateKeypadKey(ksym))
250 /* first check if a control mask is omitted */
251 if(e->state & ControlMask) {
252 switch(tolower(ksym)) {
294 while(i-- > 0 && text[i] == ' ');
295 while(i-- > 0 && text[i] != ' ');
303 num = MIN(num, sizeof text);
304 if(num && !iscntrl((int) buf[0])) {
305 memcpy(text + len, buf, num + 1);
313 for(i = 1; len - i > 0 && !IS_UTF8_1ST_CHAR(text[len - i]); i++);
323 while(sel && sel->right)
334 if(!sel || !sel->left)
337 if(sel->right == curr) {
355 if(e->state & ShiftMask)
357 fprintf(stdout, "%s", sel ? sel->text : text);
362 if(!sel || !sel->right)
372 strncpy(text, sel->text, sizeof text);
380 match(char *pattern) {
382 Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
386 plen = strlen(pattern);
387 item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
388 for(i = allitems; i; i = i->next)
389 if(!fstrncmp(pattern, i->text, plen + 1))
390 appenditem(i, &lexact, &exactend);
391 else if(!fstrncmp(pattern, i->text, plen))
392 appenditem(i, &lprefix, &prefixend);
393 else if(fstrstr(i->text, pattern))
394 appenditem(i, &lsubstr, &substrend);
401 itemend->right = lprefix;
402 lprefix->left = itemend;
410 itemend->right = lsubstr;
411 lsubstr->left = itemend;
416 curr = prev = next = sel = item;
422 char *p, buf[sizeof text];
423 unsigned int len = 0, max = 0;
427 while(fgets(buf, sizeof buf, stdin)) {
429 if(buf[len-1] == '\n')
431 if(!(p = strdup(buf)))
432 eprint("cannot strdup %u bytes\n", len);
433 if((max = MAX(max, len)) == len)
435 if(!(new = malloc(sizeof *new)))
436 eprint("cannot malloc %u bytes\n", sizeof *new);
437 new->next = new->left = new->right = NULL;
451 /* main event loop */
453 while(!XNextEvent(dpy, &ev))
459 if(ev.xexpose.count == 0)
462 case VisibilityNotify:
463 if(ev.xvisibility.state != VisibilityUnobscured)
464 XRaiseWindow(dpy, win);
475 XineramaScreenInfo *info = NULL;
477 XModifierKeymap *modmap;
478 XSetWindowAttributes wa;
480 /* init modifier map */
481 modmap = XGetModifierMapping(dpy);
482 for(i = 0; i < 8; i++)
483 for(j = 0; j < modmap->max_keypermod; j++) {
484 if(modmap->modifiermap[i * modmap->max_keypermod + j]
485 == XKeysymToKeycode(dpy, XK_Num_Lock))
486 numlockmask = (1 << i);
488 XFreeModifiermap(modmap);
491 normcol[ColBG] = getcolor(&dc, normbgcolor);
492 normcol[ColFG] = getcolor(&dc, normfgcolor);
493 selcol[ColBG] = getcolor(&dc, selbgcolor);
494 selcol[ColFG] = getcolor(&dc, selfgcolor);
498 wa.override_redirect = True;
499 wa.background_pixmap = ParentRelative;
500 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
502 /* menu window geometry */
503 mh = (dc.font.height + 2) * (lines + 1);
505 if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
511 if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
512 for(i = 0; i < n; i++)
513 if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
517 y = topbar ? info[i].y_org : info[i].y_org + info[i].height - mh;
525 y = topbar ? 0 : mh - DisplayHeight(dpy, screen);
526 mw = DisplayWidth(dpy, screen);
529 win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
530 DefaultDepth(dpy, screen), CopyFromParent,
531 DefaultVisual(dpy, screen),
532 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
536 cmdw = MIN(textw(&dc, maxname), mw / 3);
538 promptw = MIN(textw(&dc, prompt), mw / 5);
541 XMapRaised(dpy, win);
545 main(int argc, char *argv[]) {
548 /* command line args */
550 for(i = 1; i < argc; i++)
551 if(!strcmp(argv[i], "-i")) {
552 fstrncmp = strncasecmp;
555 else if(!strcmp(argv[i], "-b"))
557 else if(!strcmp(argv[i], "-l")) {
558 if(++i < argc) lines = atoi(argv[i]);
560 calcoffsets = calcoffsetsv;
562 else if(!strcmp(argv[i], "-fn")) {
563 if(++i < argc) font = argv[i];
565 else if(!strcmp(argv[i], "-nb")) {
566 if(++i < argc) normbgcolor = argv[i];
568 else if(!strcmp(argv[i], "-nf")) {
569 if(++i < argc) normfgcolor = argv[i];
571 else if(!strcmp(argv[i], "-p")) {
572 if(++i < argc) prompt = argv[i];
574 else if(!strcmp(argv[i], "-sb")) {
575 if(++i < argc) selbgcolor = argv[i];
577 else if(!strcmp(argv[i], "-sf")) {
578 if(++i < argc) selfgcolor = argv[i];
580 else if(!strcmp(argv[i], "-v")) {
581 printf("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n");
585 fputs("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>]\n"
586 " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr);
589 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
590 fprintf(stderr, "dmenu: warning: no locale support\n");
591 if(!(dpy = XOpenDisplay(NULL)))
592 eprint("cannot open display\n");
593 if(atexit(&cleanup) != 0)
594 eprint("cannot register cleanup\n");
595 screen = DefaultScreen(dpy);
596 root = RootWindow(dpy, screen);
597 if(!(argp = malloc(sizeof *argp * (argc+2))))
598 eprint("cannot malloc %u bytes\n", sizeof *argp * (argc+2));
599 memcpy(argp + 2, argv + 1, sizeof *argp * argc);