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) ((rx) < (x) && (x) < (rx)+(rw) && (ry) < (y) && (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) == 0x00)
22 typedef struct Item Item;
25 Item *next; /* traverses all items */
26 Item *left, *right; /* traverses items matching current search pattern */
29 static void appenditem(Item *i, Item **list, Item **last);
30 static void calcoffsetsh(void);
31 static void calcoffsetsv(void);
32 static char *cistrstr(const char *s, const char *sub);
33 static void cleanup(void);
34 static void drawitem(const char *s, unsigned long col[ColLast]);
35 static void drawmenu(void);
36 static void drawmenuh(void);
37 static void drawmenuv(void);
38 static void grabkeyboard(void);
39 static void keypress(XKeyEvent *e);
40 static void match(void);
41 static void readstdin(void);
42 static void run(void);
43 static void setup(void);
45 static char **argp = NULL;
46 static char *maxname = NULL;
48 static char text[4096];
51 static size_t cur = 0;
52 static unsigned int cmdw = 0;
53 static unsigned int lines = 0;
54 static unsigned int numlockmask;
55 static unsigned int mw, mh;
56 static unsigned long normcol[ColLast];
57 static unsigned long selcol[ColLast];
58 static Bool topbar = True;
61 static Item *allitems = NULL; /* first of all items */
62 static Item *item = NULL; /* first of pattern matching items */
63 static Item *sel = NULL;
64 static Item *next = NULL;
65 static Item *prev = NULL;
66 static Item *curr = NULL;
67 static Window win, root;
69 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
70 static char *(*fstrstr)(const char *, const char *) = strstr;
71 static void (*calcoffsets)(void) = calcoffsetsh;
74 appenditem(Item *i, Item **list, Item **last) {
88 w = promptw + cmdw + textw(&dc, "<") + textw(&dc, ">");
89 for(x = w, next = curr; next; next = next->right)
90 if((x += MIN(textw(&dc, next->text), mw / 3)) > mw)
92 for(x = w, prev = curr; prev && prev->left; prev = prev->left)
93 if((x += MIN(textw(&dc, prev->left->text), mw / 3)) > mw)
102 for(i = 0; i < lines && next; i++)
104 mh = (dc.font.height + 2) * (i + 1);
105 for(i = 0; i < lines && prev && prev->left; i++)
110 cistrstr(const char *s, const char *sub) {
116 if((c = tolower(*sub++)) != '\0') {
120 if((csub = *s++) == '\0')
123 while(tolower(csub) != c);
125 while(strncasecmp(s, sub, len) != 0);
136 itm = allitems->next;
137 free(allitems->text);
142 XDestroyWindow(dpy, win);
143 XUngrabKeyboard(dpy, CurrentTime);
148 drawitem(const char *s, unsigned long col[ColLast]) {
150 unsigned int w = textnw(&dc, text, strlen(text));
153 drawtext(&dc, s, col);
154 for(p = fstrstr(s, text); *text && (p = fstrstr(p, text)); p++)
155 drawline(&dc, textnw(&dc, s, p-s) + dc.h/2 - 1, dc.h-2, w, 1, col);
164 drawbox(&dc, normcol);
165 dc.h = dc.font.height + 2;
166 dc.y = topbar ? 0 : mh - dc.h;
170 drawbox(&dc, selcol);
171 drawtext(&dc, prompt, selcol);
176 if(cmdw && item && lines == 0)
178 drawtext(&dc, text, normcol);
179 drawline(&dc, textnw(&dc, text, cur) + dc.h/2 - 2, 2, 1, dc.h-4, normcol);
184 commitdraw(&dc, win);
192 dc.w = textw(&dc, "<");
193 drawtext(&dc, curr->left ? "<" : NULL, normcol);
195 for(i = curr; i != next; i = i->right) {
196 dc.w = MIN(textw(&dc, i->text), mw / 3);
197 drawitem(i->text, (sel == i) ? selcol : normcol);
200 dc.w = textw(&dc, ">");
202 drawtext(&dc, next ? ">" : NULL, normcol);
208 XWindowAttributes wa;
210 dc.y = topbar ? dc.h : 0;
212 for(i = curr; i != next; i = i->right) {
213 drawitem(i->text, (sel == i) ? selcol : normcol);
216 if(!XGetWindowAttributes(dpy, win, &wa))
217 eprint("cannot get window attributes");
218 XMoveResizeWindow(dpy, win, wa.x, wa.y + (topbar ? 0 : wa.height - mh), mw, mh);
225 for(n = 0; n < 1000; n++) {
226 if(!XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime))
234 keypress(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)) {
290 memmove(text, text + cur, sizeof text - cur + 1);
298 while(i-- > 0 && text[i] == ' ');
299 while(i-- > 0 && text[i] != ' ');
300 memmove(text + i + 1, text + cur, sizeof text - cur + 1);
308 if(!(fp = fopen("sselp", "r")))
309 eprint("cannot popen sselp\n");
310 s = fgets(buf, sizeof buf, fp);
316 if(num && buf[num-1] == '\n')
323 num = MIN(num, sizeof text);
324 if(num && !iscntrl((int) buf[0])) {
325 memmove(text + cur + num, text + cur, sizeof text - cur - num);
326 memcpy(text + cur, buf, num);
334 for(i = 1; len - i > 0 && !IS_UTF8_1ST_CHAR(text[cur - i]); i++);
335 memmove(text + cur - i, text + cur, sizeof text - cur + i);
342 for(i = 1; cur + i < len && !IS_UTF8_1ST_CHAR(text[cur + i]); i++);
343 memmove(text + cur, text + cur + i, sizeof text - cur);
355 while(sel && sel->right)
369 if(cur > 0 && (!sel || !sel->left || lines > 0)) {
370 while(cur-- > 0 && !IS_UTF8_1ST_CHAR(text[cur]));
376 if(!sel || !sel->left)
379 if(sel->right == curr) {
397 fprintf(stdout, "%s", ((e->state & ShiftMask) || sel) ? sel->text : text);
402 while(cur++ < len && !IS_UTF8_1ST_CHAR(text[cur]));
408 if(!sel || !sel->right)
419 strncpy(text, sel->text, sizeof text);
430 Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
433 item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
434 for(i = allitems; i; i = i->next)
435 if(!fstrncmp(text, i->text, len + 1))
436 appenditem(i, &lexact, &exactend);
437 else if(!fstrncmp(text, i->text, len))
438 appenditem(i, &lprefix, &prefixend);
439 else if(fstrstr(i->text, text))
440 appenditem(i, &lsubstr, &substrend);
447 itemend->right = lprefix;
448 lprefix->left = itemend;
456 itemend->right = lsubstr;
457 lsubstr->left = itemend;
462 curr = prev = next = sel = item;
468 char *p, buf[sizeof text];
469 unsigned int len = 0, max = 0;
473 while(fgets(buf, sizeof buf, stdin)) {
475 if(buf[len-1] == '\n')
477 if(!(p = strdup(buf)))
478 eprint("cannot strdup %u bytes\n", len);
479 if((max = MAX(max, len)) == len)
481 if(!(new = malloc(sizeof *new)))
482 eprint("cannot malloc %u bytes\n", sizeof *new);
483 new->next = new->left = new->right = NULL;
498 while(!XNextEvent(dpy, &ev))
501 if(ev.xexpose.count == 0)
507 case VisibilityNotify:
508 if(ev.xvisibility.state != VisibilityUnobscured)
509 XRaiseWindow(dpy, win);
520 XineramaScreenInfo *info = NULL;
522 XModifierKeymap *modmap;
523 XSetWindowAttributes wa;
525 /* init modifier map */
526 modmap = XGetModifierMapping(dpy);
527 for(i = 0; i < 8; i++)
528 for(j = 0; j < modmap->max_keypermod; j++) {
529 if(modmap->modifiermap[i * modmap->max_keypermod + j]
530 == XKeysymToKeycode(dpy, XK_Num_Lock))
531 numlockmask = (1 << i);
533 XFreeModifiermap(modmap);
536 normcol[ColBG] = getcolor(&dc, normbgcolor);
537 normcol[ColFG] = getcolor(&dc, normfgcolor);
538 selcol[ColBG] = getcolor(&dc, selbgcolor);
539 selcol[ColFG] = getcolor(&dc, selfgcolor);
543 wa.override_redirect = True;
544 wa.background_pixmap = ParentRelative;
545 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
547 /* input window geometry */
548 mh = (dc.font.height + 2) * (lines + 1);
550 if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
556 if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
557 for(i = 0; i < n; i++)
558 if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
562 y = topbar ? info[i].y_org : info[i].y_org + info[i].height - mh;
570 y = topbar ? 0 : DisplayHeight(dpy, screen) - mh;
571 mw = DisplayWidth(dpy, screen);
574 win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
575 DefaultDepth(dpy, screen), CopyFromParent,
576 DefaultVisual(dpy, screen),
577 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
581 promptw = MIN(textw(&dc, prompt), mw / 5);
582 XMapRaised(dpy, win);
586 main(int argc, char *argv[]) {
589 /* command line args */
591 for(i = 1; i < argc; i++)
592 if(!strcmp(argv[i], "-i")) {
593 fstrncmp = strncasecmp;
596 else if(!strcmp(argv[i], "-b"))
598 else if(!strcmp(argv[i], "-l")) {
599 if(++i < argc) lines = atoi(argv[i]);
601 calcoffsets = calcoffsetsv;
603 else if(!strcmp(argv[i], "-fn")) {
604 if(++i < argc) font = argv[i];
606 else if(!strcmp(argv[i], "-nb")) {
607 if(++i < argc) normbgcolor = argv[i];
609 else if(!strcmp(argv[i], "-nf")) {
610 if(++i < argc) normfgcolor = argv[i];
612 else if(!strcmp(argv[i], "-p")) {
613 if(++i < argc) prompt = argv[i];
615 else if(!strcmp(argv[i], "-sb")) {
616 if(++i < argc) selbgcolor = argv[i];
618 else if(!strcmp(argv[i], "-sf")) {
619 if(++i < argc) selfgcolor = argv[i];
621 else if(!strcmp(argv[i], "-v")) {
622 printf("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n");
626 fputs("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>]\n"
627 " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr);
630 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
631 fprintf(stderr, "dmenu: warning: no locale support\n");
632 if(!(dpy = XOpenDisplay(NULL)))
633 eprint("cannot open display\n");
634 if(atexit(&cleanup) != 0)
635 eprint("cannot register cleanup\n");
636 screen = DefaultScreen(dpy);
637 root = RootWindow(dpy, screen);
638 if(!(argp = malloc(sizeof *argp * (argc+2))))
639 eprint("cannot malloc %u bytes\n", sizeof *argp * (argc+2));
640 memcpy(argp + 2, argv + 1, sizeof *argp * argc);
646 cmdw = MIN(textw(&dc, maxname), mw / 3);