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 field */
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)) {
215 case XK_k: /* delete right */
225 case XK_u: /* delete left */
226 insert(NULL, -cursor);
228 case XK_w: /* delete word */
232 while(cursor - n++ > 0 && text[cursor - n] == ' ');
233 while(cursor - n++ > 0 && text[cursor - n] != ' ');
236 case XK_y: /* paste selection */
237 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, None, win, CurrentTime);
238 /* causes SelectionNotify event */
244 if(!iscntrl((int)*buf))
245 insert(buf, MIN(strlen(buf), sizeof text - cursor));
250 for(n = 1; cursor - n > 0 && !UTF8_CODEPOINT(text[cursor - n]); n++);
256 for(n = 1; cursor + n < len && !UTF8_CODEPOINT(text[cursor + n]); n++);
269 while(sel && sel->right)
279 sel = curr = matches;
283 if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
284 while(cursor-- > 0 && !UTF8_CODEPOINT(text[cursor]));
290 if(!sel || !sel->left)
293 if(sel->right == curr) {
312 fputs((sel && !(e->state & ShiftMask)) ? sel->text : text, stdout);
317 while(cursor++ < len && !UTF8_CODEPOINT(text[cursor]));
323 if(!sel || !sel->right)
334 strncpy(text, sel->text, sizeof text);
335 cursor = strlen(text);
345 Item *item, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
348 matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
349 for(item = allitems; item; item = item->next)
350 if(!fstrncmp(text, item->text, len + 1))
351 appenditem(item, &lexact, &exactend);
352 else if(!fstrncmp(text, item->text, len))
353 appenditem(item, &lprefix, &prefixend);
354 else if(fstrstr(item->text, text))
355 appenditem(item, &lsubstr, &substrend);
362 itemend->right = lprefix;
363 lprefix->left = itemend;
371 itemend->right = lsubstr;
372 lsubstr->left = itemend;
377 curr = prev = next = sel = matches;
388 XGetWindowProperty(dc->dpy, win, utf8, 0, sizeof text - cursor, True,
389 utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
390 insert(p, (q = strchr(p, '\n')) ? q-p : strlen(p));
397 char buf[sizeof text], *p;
401 for(item = NULL; fgets(buf, sizeof buf, stdin); item = new) {
402 if((p = strchr(buf, '\n')))
404 if(!(new = malloc(sizeof *new)))
405 eprintf("cannot malloc %u bytes\n", sizeof *new);
406 if(!(new->text = strdup(buf)))
407 eprintf("cannot strdup %u bytes\n", strlen(buf));
408 inputw = MAX(inputw, textw(dc, new->text));
409 new->next = new->left = new->right = NULL;
421 while(!XNextEvent(dc->dpy, &ev))
424 if(ev.xexpose.count == 0)
430 case SelectionNotify:
431 if(ev.xselection.property == utf8)
434 case VisibilityNotify:
435 if(ev.xvisibility.state != VisibilityUnobscured)
436 XRaiseWindow(dc->dpy, win);
444 XSetWindowAttributes wa;
447 XineramaScreenInfo *info;
450 screen = DefaultScreen(dc->dpy);
451 root = RootWindow(dc->dpy, screen);
452 utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
454 normcol[ColBG] = getcolor(dc, normbgcolor);
455 normcol[ColFG] = getcolor(dc, normfgcolor);
456 selcol[ColBG] = getcolor(dc, selbgcolor);
457 selcol[ColFG] = getcolor(dc, selfgcolor);
460 mh = (dc->font.height + 2) * (lines + 1);
462 if((info = XineramaQueryScreens(dc->dpy, &n))) {
467 XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
468 for(i = 0; i < n; i++)
469 if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
472 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
480 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
481 mw = DisplayWidth(dc->dpy, screen);
484 wa.override_redirect = True;
485 wa.background_pixmap = ParentRelative;
486 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
487 win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
488 DefaultDepth(dc->dpy, screen), CopyFromParent,
489 DefaultVisual(dc->dpy, screen),
490 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
493 setcanvas(dc, mw, mh);
494 inputw = MIN(inputw, mw/3);
495 promptw = prompt ? MIN(textw(dc, prompt), mw/5) : 0;
496 XMapRaised(dc->dpy, win);
503 fputs("usage: dmenu [-b] [-i] [-l lines] [-p prompt] [-fn font] [-nb color]\n"
504 " [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
509 main(int argc, char *argv[]) {
515 for(i = 1; i < argc; i++)
517 if(!strcmp(argv[i], "-v")) {
518 fputs("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n", stdout);
521 else if(!strcmp(argv[i], "-b"))
523 else if(!strcmp(argv[i], "-i")) {
524 fstrncmp = strncasecmp;
530 else if(!strcmp(argv[i], "-l"))
531 lines = atoi(argv[++i]);
532 else if(!strcmp(argv[i], "-p"))
534 else if(!strcmp(argv[i], "-fn"))
535 initfont(dc, argv[++i]);
536 else if(!strcmp(argv[i], "-nb"))
537 normbgcolor = argv[++i];
538 else if(!strcmp(argv[i], "-nf"))
539 normfgcolor = argv[++i];
540 else if(!strcmp(argv[i], "-sb"))
541 selbgcolor = argv[++i];
542 else if(!strcmp(argv[i], "-sf"))
543 selfgcolor = argv[++i];
551 return EXIT_FAILURE; /* should not reach */