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));
112 dc->h = dc->font.height + 2;
113 dc->y = topbar ? 0 : mh - dc->h;
117 drawtext(dc, prompt, selcol);
121 /* print input area */
122 if(matches && lines == 0 && textw(dc, text) <= inputw)
124 drawtext(dc, text, normcol);
125 drawrect(dc, textnw(dc, text, cursor) + dc->h/2 - 2, 2, 1, dc->h - 4, FG(dc, normcol));
128 dc->y = topbar ? dc->h : 0;
130 for(item = curr; item != next; item = item->right) {
131 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
135 else if(curr && (dc->w == inputw || curr->next)) {
137 dc->w = textw(dc, "<");
139 drawtext(dc, "<", normcol);
140 for(item = curr; item != next; item = item->right) {
142 dc->w = MIN(textw(dc, item->text), mw/3);
143 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
145 dc->w = textw(dc, ">");
148 drawtext(dc, ">", normcol);
157 for(i = 0; i < 1000; i++) {
158 if(!XGrabKeyboard(dc->dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime))
162 eprintf("cannot grab keyboard\n");
166 insert(const char *s, ssize_t n) {
167 memmove(text + cursor + n, text + cursor, sizeof text - cursor - n);
169 memcpy(text + cursor, s, n);
175 keypress(XKeyEvent *e) {
176 char buf[sizeof text];
182 XLookupString(e, buf, sizeof buf, &ksym, NULL);
183 if(e->state & ControlMask) {
184 switch(tolower(ksym)) {
212 case XK_k: /* delete right */
222 case XK_u: /* delete left */
223 insert(NULL, -cursor);
225 case XK_w: /* delete word */
229 while(cursor - n++ > 0 && text[cursor - n] == ' ');
230 while(cursor - n++ > 0 && text[cursor - n] != ' ');
233 case XK_y: /* paste selection */
234 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, None, win, CurrentTime);
235 /* causes SelectionNotify event */
241 if(!iscntrl((int)*buf))
242 insert(buf, MIN(strlen(buf), sizeof text - cursor));
247 for(n = 1; cursor - n > 0 && !UTF8_CODEPOINT(text[cursor - n]); n++);
253 for(n = 1; cursor + n < len && !UTF8_CODEPOINT(text[cursor + n]); n++);
266 while(sel && sel->right)
276 sel = curr = matches;
280 if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
281 while(cursor-- > 0 && !UTF8_CODEPOINT(text[cursor]));
287 if(!sel || !sel->left)
290 if(sel->right == curr) {
309 fputs((sel && !(e->state & ShiftMask)) ? sel->text : text, stdout);
314 while(cursor++ < len && !UTF8_CODEPOINT(text[cursor]));
320 if(!sel || !sel->right)
331 strncpy(text, sel->text, sizeof text);
332 cursor = strlen(text);
342 Item *item, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
345 matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
346 for(item = allitems; item; item = item->next)
347 if(!fstrncmp(text, item->text, len + 1))
348 appenditem(item, &lexact, &exactend);
349 else if(!fstrncmp(text, item->text, len))
350 appenditem(item, &lprefix, &prefixend);
351 else if(fstrstr(item->text, text))
352 appenditem(item, &lsubstr, &substrend);
359 itemend->right = lprefix;
360 lprefix->left = itemend;
368 itemend->right = lsubstr;
369 lsubstr->left = itemend;
374 curr = prev = next = sel = matches;
385 XGetWindowProperty(dc->dpy, win, utf8, 0, sizeof text - cursor, True,
386 utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
387 insert(p, (q = strchr(p, '\n')) ? q-p : strlen(p));
394 char buf[sizeof text], *p;
398 for(item = NULL; fgets(buf, sizeof buf, stdin); item = new) {
399 if((p = strchr(buf, '\n')))
401 if(!(new = malloc(sizeof *new)))
402 eprintf("cannot malloc %u bytes\n", sizeof *new);
403 if(!(new->text = strdup(buf)))
404 eprintf("cannot strdup %u bytes\n", strlen(buf));
405 inputw = MAX(inputw, textw(dc, new->text));
406 new->next = new->left = new->right = NULL;
418 while(!XNextEvent(dc->dpy, &ev))
421 if(ev.xexpose.count == 0)
427 case SelectionNotify:
428 if(ev.xselection.property == utf8)
431 case VisibilityNotify:
432 if(ev.xvisibility.state != VisibilityUnobscured)
433 XRaiseWindow(dc->dpy, win);
441 XSetWindowAttributes wa;
444 XineramaScreenInfo *info;
447 screen = DefaultScreen(dc->dpy);
448 root = RootWindow(dc->dpy, screen);
449 utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
451 normcol[ColBG] = getcolor(dc, normbgcolor);
452 normcol[ColFG] = getcolor(dc, normfgcolor);
453 selcol[ColBG] = getcolor(dc, selbgcolor);
454 selcol[ColFG] = getcolor(dc, selfgcolor);
456 /* input window geometry */
457 mh = (dc->font.height + 2) * (lines + 1);
459 if((info = XineramaQueryScreens(dc->dpy, &n))) {
464 XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
465 for(i = 0; i < n; i++)
466 if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
469 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
477 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
478 mw = DisplayWidth(dc->dpy, screen);
481 wa.override_redirect = True;
482 wa.background_pixmap = ParentRelative;
483 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
484 win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
485 DefaultDepth(dc->dpy, screen), CopyFromParent,
486 DefaultVisual(dc->dpy, screen),
487 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
490 setcanvas(dc, win, mw, mh);
491 inputw = MIN(inputw, mw/3);
492 promptw = prompt ? MIN(textw(dc, prompt), mw/5) : 0;
493 XMapRaised(dc->dpy, win);
500 fputs("usage: dmenu [-b] [-i] [-l lines] [-p prompt] [-fn font] [-nb color]\n"
501 " [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
506 main(int argc, char *argv[]) {
512 for(i = 1; i < argc; i++)
514 if(!strcmp(argv[i], "-v")) {
515 fputs("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n", stdout);
518 else if(!strcmp(argv[i], "-b"))
520 else if(!strcmp(argv[i], "-i")) {
521 fstrncmp = strncasecmp;
527 else if(!strcmp(argv[i], "-l")) {
528 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 */