1 /* See LICENSE file for copyright and license details. */
10 #include <X11/keysym.h>
12 #include <X11/Xutil.h>
14 #include <X11/extensions/Xinerama.h>
19 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
20 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
21 #define MIN(a, b) ((a) < (b) ? (a) : (b))
22 #define MAX(a, b) ((a) > (b) ? (a) : (b))
23 #define IS_UTF8_1ST_CHAR(c) ((((c) & 0xc0) == 0xc0) || !((c) & 0x80))
25 typedef struct Item Item;
28 Item *next; /* traverses all items */
29 Item *left, *right; /* traverses items matching current search pattern */
32 /* forward declarations */
33 static void appenditem(Item *i, Item **list, Item **last);
34 static void calcoffsetsh(void);
35 static void calcoffsetsv(void);
36 static char *cistrstr(const char *s, const char *sub);
37 static void cleanup(void);
38 static void dinput(void);
39 static void drawmenu(void);
40 static void drawmenuh(void);
41 static void drawmenuv(void);
42 static Bool grabkeyboard(void);
43 static void kpress(XKeyEvent *e);
44 static void match(char *pattern);
45 static void readstdin(void);
46 static void run(void);
47 static void setup(void);
52 static char **argp = NULL;
53 static char *maxname = NULL;
54 static char *prompt = NULL;
55 static char text[4096];
57 static int promptw = 0;
60 static unsigned int lines = 0;
61 static unsigned int numlockmask = 0;
62 static unsigned int mw, mh;
63 static unsigned long normcol[ColLast];
64 static unsigned long selcol[ColLast];
65 static Bool running = True;
66 static Bool topbar = True;
69 static Item *allitems = NULL; /* first of all items */
70 static Item *item = NULL; /* first of pattern matching items */
71 static Item *sel = NULL;
72 static Item *next = NULL;
73 static Item *prev = NULL;
74 static Item *curr = NULL;
75 static Window win, parent;
76 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
77 static char *(*fstrstr)(const char *, const char *) = strstr;
78 static void (*calcoffsets)(void) = calcoffsetsh;
81 appenditem(Item *i, Item **list, Item **last) {
95 w = promptw + cmdw + (2 * spaceitem);
96 for(next = curr; next; next = next->right)
97 if((w += MIN(textw(&dc, next->text), mw / 3)) > mw)
99 w = promptw + cmdw + (2 * spaceitem);
100 for(prev = curr; prev && prev->left; prev = prev->left)
101 if((w += MIN(textw(&dc, prev->left->text), mw / 3)) > mw)
110 for(i = 0; i < lines && next; i++)
112 mh = (dc.font.height + 2) * (i + 1);
113 for(i = 0; i < lines && prev && prev->left; i++)
118 cistrstr(const char *s, const char *sub) {
124 if((c = tolower(*sub++)) != '\0') {
128 if((csub = *s++) == '\0')
131 while(tolower(csub) != c);
133 while(strncasecmp(s, sub, len) != 0);
144 itm = allitems->next;
145 free(allitems->text);
150 XDestroyWindow(dpy, win);
151 XUngrabKeyboard(dpy, CurrentTime);
159 execvp("dinput", argp);
160 eprint("cannot exec dinput\n");
169 drawtext(&dc, NULL, normcol, False);
170 dc.h = dc.font.height + 2;
171 dc.y = topbar ? 0 : mh - dc.h;
175 drawtext(&dc, prompt, selcol, False);
180 if(cmdw && item && lines == 0)
182 drawtext(&dc, *text ? text : NULL, normcol, False);
187 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
197 drawtext(&dc, curr->left ? "<" : NULL, normcol, False);
199 for(i = curr; i != next; i = i->right) {
200 dc.w = MIN(textw(&dc, i->text), mw / 3);
201 drawtext(&dc, i->text, (sel == i) ? selcol : normcol, False);
206 drawtext(&dc, next ? ">" : NULL, normcol, False);
212 XWindowAttributes wa;
214 dc.y = topbar ? dc.h : 0;
216 for(i = curr; i != next; i = i->right) {
217 drawtext(&dc, i->text, (sel == i) ? selcol : normcol, False);
220 if(!XGetWindowAttributes(dpy, win, &wa))
221 eprint("cannot get window attributes");
222 XMoveResizeWindow(dpy, win, wa.x, wa.y + (topbar ? 0 : wa.height - mh), mw, mh);
229 for(len = 1000; len; len--) {
230 if(XGrabKeyboard(dpy, parent, True, GrabModeAsync, GrabModeAsync, CurrentTime)
239 kpress(XKeyEvent *e) {
240 char buf[sizeof text];
246 num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
247 if(ksym == XK_KP_Enter)
249 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
250 ksym = (ksym - XK_KP_0) + XK_0;
251 else if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
252 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
253 || IsPrivateKeypadKey(ksym))
255 /* first check if a control mask is omitted */
256 if(e->state & ControlMask) {
257 switch(tolower(ksym)) {
299 while(i-- > 0 && text[i] == ' ');
300 while(i-- > 0 && text[i] != ' ');
308 num = MIN(num, sizeof text);
309 if(num && !iscntrl((int) buf[0])) {
310 memcpy(text + len, buf, num + 1);
318 for(i = 1; len - i > 0 && !IS_UTF8_1ST_CHAR(text[len - i]); i++);
328 while(sel && sel->right)
341 if(!sel || !sel->left)
344 if(sel->right == curr) {
362 if(e->state & ShiftMask)
364 fprintf(stdout, "%s", sel ? sel->text : text);
370 if(!sel || !sel->right)
380 strncpy(text, sel->text, sizeof text);
388 match(char *pattern) {
390 Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
394 plen = strlen(pattern);
395 item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
396 for(i = allitems; i; i = i->next)
397 if(!fstrncmp(pattern, i->text, plen + 1))
398 appenditem(i, &lexact, &exactend);
399 else if(!fstrncmp(pattern, i->text, plen))
400 appenditem(i, &lprefix, &prefixend);
401 else if(fstrstr(i->text, pattern))
402 appenditem(i, &lsubstr, &substrend);
409 itemend->right = lprefix;
410 lprefix->left = itemend;
418 itemend->right = lsubstr;
419 lsubstr->left = itemend;
424 curr = prev = next = sel = item;
430 char *p, buf[sizeof text];
431 unsigned int len = 0, max = 0;
435 while(fgets(buf, sizeof buf, stdin)) {
437 if(buf[len-1] == '\n')
439 if(!(p = strdup(buf)))
440 eprint("cannot strdup %u bytes\n", len);
441 if((max = MAX(max, len)) == len)
443 if(!(new = malloc(sizeof *new)))
444 eprint("cannot malloc %u bytes\n", sizeof *new);
445 new->next = new->left = new->right = NULL;
459 /* main event loop */
460 while(running && !XNextEvent(dpy, &ev))
466 if(ev.xexpose.count == 0)
469 case VisibilityNotify:
470 if (ev.xvisibility.state != VisibilityUnobscured)
471 XRaiseWindow(dpy, win);
481 XineramaScreenInfo *info = NULL;
483 XModifierKeymap *modmap;
484 XSetWindowAttributes wa;
485 XWindowAttributes pwa;
487 /* init modifier map */
488 modmap = XGetModifierMapping(dpy);
489 for(i = 0; i < 8; i++)
490 for(j = 0; j < modmap->max_keypermod; j++) {
491 if(modmap->modifiermap[i * modmap->max_keypermod + j]
492 == XKeysymToKeycode(dpy, XK_Num_Lock))
493 numlockmask = (1 << i);
495 XFreeModifiermap(modmap);
498 normcol[ColBG] = getcolor(&dc, normbgcolor);
499 normcol[ColFG] = getcolor(&dc, normfgcolor);
500 selcol[ColBG] = getcolor(&dc, selbgcolor);
501 selcol[ColFG] = getcolor(&dc, selfgcolor);
505 wa.override_redirect = True;
506 wa.background_pixmap = ParentRelative;
507 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
509 /* menu window geometry */
510 mh = (dc.font.height + 2) * (lines + 1);
512 if(parent == RootWindow(dpy, screen) && XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
518 if(XQueryPointer(dpy, parent, &dummy, &dummy, &x, &y, &di, &di, &dui))
519 for(i = 0; i < n; i++)
520 if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
524 y = topbar ? info[i].y_org : info[i].y_org + info[i].height - mh;
531 if(!XGetWindowAttributes(dpy, parent, &pwa))
532 eprint("cannot get window attributes");
534 y = topbar ? 0 : pwa.height - mh;
538 win = XCreateWindow(dpy, parent, x, y, mw, mh, 0,
539 DefaultDepth(dpy, screen), CopyFromParent,
540 DefaultVisual(dpy, screen),
541 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
545 cmdw = MIN(textw(&dc, maxname), mw / 3);
547 promptw = MIN(textw(&dc, prompt), mw / 5);
550 XMapRaised(dpy, win);
554 main(int argc, char *argv[]) {
557 /* command line args */
559 for(i = 1; i < argc; i++)
560 if(!strcmp(argv[i], "-i")) {
561 fstrncmp = strncasecmp;
564 else if(!strcmp(argv[i], "-b"))
566 else if(!strcmp(argv[i], "-e")) {
567 if(++i < argc) parent = atoi(argv[i]);
569 else if(!strcmp(argv[i], "-l")) {
570 if(++i < argc) lines = atoi(argv[i]);
572 calcoffsets = calcoffsetsv;
574 else if(!strcmp(argv[i], "-fn")) {
575 if(++i < argc) font = argv[i];
577 else if(!strcmp(argv[i], "-nb")) {
578 if(++i < argc) normbgcolor = argv[i];
580 else if(!strcmp(argv[i], "-nf")) {
581 if(++i < argc) normfgcolor = argv[i];
583 else if(!strcmp(argv[i], "-p")) {
584 if(++i < argc) prompt = argv[i];
586 else if(!strcmp(argv[i], "-sb")) {
587 if(++i < argc) selbgcolor = argv[i];
589 else if(!strcmp(argv[i], "-sf")) {
590 if(++i < argc) selfgcolor = argv[i];
592 else if(!strcmp(argv[i], "-v")) {
593 printf("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n");
597 fputs("usage: dmenu [-i] [-b] [-e <xid>] [-l <lines>] [-fn <font>] [-nb <color>]\n"
598 " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr);
601 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
602 fprintf(stderr, "dmenu: warning: no locale support\n");
603 if(!(dpy = XOpenDisplay(NULL)))
604 eprint("cannot open display\n");
605 screen = DefaultScreen(dpy);
607 parent = RootWindow(dpy, screen);
608 if(!(argp = malloc(sizeof *argp * (argc+2))))
609 eprint("cannot malloc %u bytes\n", sizeof *argp * (argc+2));
610 memcpy(argp + 2, argv + 1, sizeof *argp * argc);
613 running = grabkeyboard();