1 /* See LICENSE file for copyright and license details. */
11 #include <X11/extensions/Xinerama.h>
15 #define INRECT(x,y,rx,ry,rw,rh) ((x) >= (rx) && (x) < (rx)+(rw) && (y) >= (ry) && (y) < (ry)+(rh))
16 #define MIN(a,b) ((a) < (b) ? (a) : (b))
17 #define MAX(a,b) ((a) > (b) ? (a) : (b))
18 #define UTF8_CODEPOINT(c) (((c) & 0xc0) != 0x80)
20 typedef struct Item Item;
23 Item *next; /* traverses all items */
24 Item *left, *right; /* traverses matching items */
27 static void appenditem(Item *item, Item **list, Item **last);
28 static void calcoffsets(void);
29 static char *cistrstr(const char *s, const char *sub);
30 static void drawmenu(void);
31 static void grabkeyboard(void);
32 static void insert(const char *s, ssize_t n);
33 static void keypress(XKeyEvent *e);
34 static void match(void);
35 static void paste(void);
36 static void readstdin(void);
37 static void run(void);
38 static void setup(void);
39 static void usage(void);
41 static char text[4096];
42 static size_t cursor = 0;
43 static const char *prompt = NULL;
44 static const char *normbgcolor = "#cccccc";
45 static const char *normfgcolor = "#000000";
46 static const char *selbgcolor = "#0066ff";
47 static const char *selfgcolor = "#ffffff";
48 static unsigned int inputw = 0;
49 static unsigned int lines = 0;
50 static unsigned int mw, mh;
51 static unsigned int promptw;
52 static unsigned long normcol[ColLast];
53 static unsigned long selcol[ColLast];
55 static Bool topbar = True;
57 static Item *allitems, *matches;
58 static Item *curr, *prev, *next, *sel;
59 static Window root, win;
61 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
62 static char *(*fstrstr)(const char *, const char *) = strstr;
65 appenditem(Item *item, Item **list, Item **last) {
69 (*last)->right = item;
80 h = dc->font.height+2;
84 n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">"));
87 for(i = 0; next; next = next->right)
88 if((i += (lines > 0) ? h : MIN(textw(dc, next->text), mw/3)) > n)
90 for(i = 0; prev && prev->left; prev = prev->left)
91 if((i += (lines > 0) ? h : MIN(textw(dc, prev->left->text), mw/3)) > n)
96 cistrstr(const char *s, const char *sub) {
99 for(len = strlen(sub); *s; s++)
100 if(!strncasecmp(s, sub, len))
111 drawrect(dc, 0, 0, mw, mh, BG(dc, normcol));
113 dc->h = dc->font.height + 2;
114 dc->y = topbar ? 0 : mh - dc->h;
118 drawtext(dc, prompt, selcol);
122 /* print input area */
123 if(matches && lines == 0 && textw(dc, text) <= inputw)
125 drawtext(dc, text, normcol);
126 drawrect(dc, textnw(dc, text, cursor) + dc->h/2 - 2, 2, 1, dc->h - 4, FG(dc, normcol));
129 dc->y = topbar ? dc->h : 0;
131 for(item = curr; item != next; item = item->right) {
132 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
136 else if(curr && (dc->w == inputw || curr->next)) {
138 dc->w = textw(dc, "<");
140 drawtext(dc, "<", normcol);
141 for(item = curr; item != next; item = item->right) {
143 dc->w = MIN(textw(dc, item->text), mw/3);
144 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
146 dc->w = textw(dc, ">");
149 drawtext(dc, ">", normcol);
158 for(i = 0; i < 1000; i++) {
159 if(!XGrabKeyboard(dc->dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime))
163 eprintf("cannot grab keyboard\n");
167 insert(const char *s, ssize_t n) {
168 memmove(text + cursor + n, text + cursor, sizeof text - cursor - n);
170 memcpy(text + cursor, s, n);
176 keypress(XKeyEvent *e) {
177 char buf[sizeof text];
183 XLookupString(e, buf, sizeof buf, &ksym, NULL);
184 if(e->state & ControlMask) {
185 switch(tolower(ksym)) {
213 case XK_k: /* delete right */
223 case XK_u: /* delete left */
224 insert(NULL, -cursor);
226 case XK_w: /* delete word */
230 while(cursor - n++ > 0 && text[cursor - n] == ' ');
231 while(cursor - n++ > 0 && text[cursor - n] != ' ');
234 case XK_y: /* paste selection */
235 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, None, win, CurrentTime);
236 /* causes SelectionNotify event */
242 if(!iscntrl((int)*buf))
243 insert(buf, MIN(strlen(buf), sizeof text - cursor));
248 for(n = 1; cursor - n > 0 && !UTF8_CODEPOINT(text[cursor - n]); n++);
254 for(n = 1; cursor + n < len && !UTF8_CODEPOINT(text[cursor + n]); n++);
267 while(sel && sel->right)
277 sel = curr = matches;
281 if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
282 while(cursor-- > 0 && !UTF8_CODEPOINT(text[cursor]));
288 if(!sel || !sel->left)
291 if(sel->right == curr) {
310 fputs((sel && !(e->state & ShiftMask)) ? sel->text : text, stdout);
315 while(cursor++ < len && !UTF8_CODEPOINT(text[cursor]));
321 if(!sel || !sel->right)
332 strncpy(text, sel->text, sizeof text);
333 cursor = strlen(text);
343 Item *item, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
346 matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
347 for(item = allitems; item; item = item->next)
348 if(!fstrncmp(text, item->text, len + 1))
349 appenditem(item, &lexact, &exactend);
350 else if(!fstrncmp(text, item->text, len))
351 appenditem(item, &lprefix, &prefixend);
352 else if(fstrstr(item->text, text))
353 appenditem(item, &lsubstr, &substrend);
360 itemend->right = lprefix;
361 lprefix->left = itemend;
369 itemend->right = lsubstr;
370 lsubstr->left = itemend;
375 curr = prev = next = sel = matches;
386 XGetWindowProperty(dc->dpy, win, utf8, 0, sizeof text - cursor, True,
387 utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
388 insert(p, (q = strchr(p, '\n')) ? q-p : strlen(p));
395 char buf[sizeof text], *p;
399 for(item = NULL; fgets(buf, sizeof buf, stdin); item = new) {
400 if((p = strchr(buf, '\n')))
402 if(!(new = malloc(sizeof *new)))
403 eprintf("cannot malloc %u bytes\n", sizeof *new);
404 if(!(new->text = strdup(buf)))
405 eprintf("cannot strdup %u bytes\n", strlen(buf));
406 inputw = MAX(inputw, textw(dc, new->text));
407 new->next = new->left = new->right = NULL;
419 while(!XNextEvent(dc->dpy, &ev))
422 if(ev.xexpose.count == 0)
428 case SelectionNotify:
429 if(ev.xselection.property == utf8)
432 case VisibilityNotify:
433 if(ev.xvisibility.state != VisibilityUnobscured)
434 XRaiseWindow(dc->dpy, win);
442 XSetWindowAttributes wa;
445 XineramaScreenInfo *info;
448 screen = DefaultScreen(dc->dpy);
449 root = RootWindow(dc->dpy, screen);
450 utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
452 normcol[ColBG] = getcolor(dc, normbgcolor);
453 normcol[ColFG] = getcolor(dc, normfgcolor);
454 selcol[ColBG] = getcolor(dc, selbgcolor);
455 selcol[ColFG] = getcolor(dc, selfgcolor);
457 /* input window geometry */
458 mh = (dc->font.height + 2) * (lines + 1);
460 if((info = XineramaQueryScreens(dc->dpy, &n))) {
465 XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
466 for(i = 0; i < n; i++)
467 if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
470 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
478 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
479 mw = DisplayWidth(dc->dpy, screen);
482 wa.override_redirect = True;
483 wa.background_pixmap = ParentRelative;
484 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
485 win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
486 DefaultDepth(dc->dpy, screen), CopyFromParent,
487 DefaultVisual(dc->dpy, screen),
488 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
491 setcanvas(dc, win, mw, mh);
492 inputw = MIN(inputw, mw/3);
493 promptw = prompt ? MIN(textw(dc, prompt), mw/5) : 0;
494 XMapRaised(dc->dpy, win);
501 fputs("usage: dmenu [-b] [-i] [-l lines] [-p prompt] [-fn font] [-nb color]\n"
502 " [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
507 main(int argc, char *argv[]) {
513 for(i = 1; i < argc; i++)
515 if(!strcmp(argv[i], "-v")) {
516 fputs("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n", stdout);
519 else if(!strcmp(argv[i], "-b"))
521 else if(!strcmp(argv[i], "-i")) {
522 fstrncmp = strncasecmp;
528 else if(!strcmp(argv[i], "-l"))
529 lines = atoi(argv[++i]);
530 else if(!strcmp(argv[i], "-p"))
532 else if(!strcmp(argv[i], "-fn"))
533 initfont(dc, argv[++i]);
534 else if(!strcmp(argv[i], "-nb"))
535 normbgcolor = argv[++i];
536 else if(!strcmp(argv[i], "-nf"))
537 normfgcolor = argv[++i];
538 else if(!strcmp(argv[i], "-sb"))
539 selbgcolor = argv[++i];
540 else if(!strcmp(argv[i], "-sf"))
541 selfgcolor = argv[++i];
549 return EXIT_FAILURE; /* should not reach */