1 /* See LICENSE file for copyright and license details. */
10 #include <X11/Xutil.h>
12 #include <X11/extensions/Xinerama.h>
16 #define INRECT(x,y,rx,ry,rw,rh) ((x) >= (rx) && (x) < (rx)+(rw) && (y) >= (ry) && (y) < (ry)+(rh))
17 #define MIN(a,b) ((a) < (b) ? (a) : (b))
18 #define MAX(a,b) ((a) > (b) ? (a) : (b))
20 typedef struct Item Item;
26 static void appenditem(Item *item, Item **list, Item **last);
27 static void calcoffsets(void);
28 static void drawmenu(void);
29 static char *fstrstr(const char *s, const char *sub);
30 static void grabkeyboard(void);
31 static void insert(const char *str, ssize_t n);
32 static void keypress(XKeyEvent *ev);
33 static void match(Bool sub);
34 static size_t nextrune(int inc);
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[BUFSIZ] = "";
42 static int bh, mw, mh;
43 static int inputw, promptw;
45 static size_t cursor = 0;
46 static const char *font = NULL;
47 static const char *prompt = NULL;
48 static const char *normbgcolor = "#cccccc";
49 static const char *normfgcolor = "#000000";
50 static const char *selbgcolor = "#0066ff";
51 static const char *selfgcolor = "#ffffff";
52 static unsigned long normcol[ColLast];
53 static unsigned long selcol[ColLast];
55 static Bool topbar = True;
57 static Item *items = NULL;
58 static Item *matches, *matchend;
59 static Item *prev, *curr, *next, *sel;
62 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
65 main(int argc, char *argv[]) {
69 for(i = 1; i < argc; i++)
71 if(!strcmp(argv[i], "-v")) {
72 puts("dmenu-"VERSION", © 2006-2011 dmenu engineers, see LICENSE for details");
75 else if(!strcmp(argv[i], "-b"))
77 else if(!strcmp(argv[i], "-f"))
79 else if(!strcmp(argv[i], "-i"))
80 fstrncmp = strncasecmp;
84 else if(!strcmp(argv[i], "-l"))
85 lines = atoi(argv[++i]);
86 else if(!strcmp(argv[i], "-p"))
88 else if(!strcmp(argv[i], "-fn"))
90 else if(!strcmp(argv[i], "-nb"))
91 normbgcolor = argv[++i];
92 else if(!strcmp(argv[i], "-nf"))
93 normfgcolor = argv[++i];
94 else if(!strcmp(argv[i], "-sb"))
95 selbgcolor = argv[++i];
96 else if(!strcmp(argv[i], "-sf"))
97 selfgcolor = argv[++i];
115 return EXIT_FAILURE; /* should not reach */
119 appenditem(Item *item, Item **list, Item **last) {
123 (*last)->right = item;
136 n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">"));
138 for(i = 0, next = curr; next; next = next->right)
139 if((i += (lines > 0) ? bh : MIN(textw(dc, next->text), n)) > n)
141 for(i = 0, prev = curr; prev && prev->left; prev = prev->left)
142 if((i += (lines > 0) ? bh : MIN(textw(dc, prev->left->text), n)) > n)
154 drawrect(dc, 0, 0, mw, mh, True, BG(dc, normcol));
158 drawtext(dc, prompt, selcol);
161 dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw;
162 drawtext(dc, text, normcol);
163 if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w)
164 drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol));
168 for(item = curr; item != next; item = item->right) {
170 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
175 dc->w = textw(dc, "<");
177 drawtext(dc, "<", normcol);
178 for(item = curr; item != next; item = item->right) {
180 dc->w = MIN(textw(dc, item->text), mw - dc->x - textw(dc, ">"));
181 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
183 dc->w = textw(dc, ">");
186 drawtext(dc, ">", normcol);
188 mapdc(dc, win, mw, mh);
192 fstrstr(const char *s, const char *sub) {
195 for(len = strlen(sub); *s; s++)
196 if(!fstrncmp(s, sub, len))
205 for(i = 0; i < 1000; i++) {
206 if(XGrabKeyboard(dc->dpy, DefaultRootWindow(dc->dpy), True,
207 GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
211 eprintf("cannot grab keyboard\n");
215 insert(const char *str, ssize_t n) {
216 if(strlen(text) + n > sizeof text - 1)
218 memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0));
220 memcpy(&text[cursor], str, n);
222 match(n > 0 && text[cursor] == '\0');
226 keypress(XKeyEvent *ev) {
230 XLookupString(ev, buf, sizeof buf, &ksym, NULL);
231 if(ev->state & ControlMask) {
234 XConvertCase(ksym, &lower, &upper);
266 case XK_k: /* delete right */
276 case XK_u: /* delete left */
277 insert(NULL, 0 - cursor);
279 case XK_w: /* delete word */
280 while(cursor > 0 && text[nextrune(-1)] == ' ')
281 insert(NULL, nextrune(-1) - cursor);
282 while(cursor > 0 && text[nextrune(-1)] != ' ')
283 insert(NULL, nextrune(-1) - cursor);
285 case XK_y: /* paste selection */
286 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
293 insert(buf, strlen(buf));
296 if(text[cursor] == '\0')
298 cursor = nextrune(+1);
301 insert(NULL, nextrune(-1) - cursor);
304 if(text[cursor] != '\0') {
305 cursor = strlen(text);
313 while(next && (curr = curr->right))
325 sel = curr = matches;
329 if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
330 cursor = nextrune(-1);
336 if(sel && sel->left && (sel = sel->left)->right == curr) {
355 fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout);
358 if(text[cursor] != '\0') {
359 cursor = nextrune(+1);
365 if(sel && sel->right && (sel = sel->right) == next) {
373 strncpy(text, sel->text, sizeof text);
374 cursor = strlen(text);
383 size_t len = strlen(text);
384 Item *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
387 lexact = lprefix = lsubstr = exactend = prefixend = substrend = NULL;
388 for(item = sub ? matches : items; item && item->text; item = lnext) {
389 lnext = sub ? item->right : item + 1;
390 if(!fstrncmp(text, item->text, len + 1))
391 appenditem(item, &lexact, &exactend);
392 else if(!fstrncmp(text, item->text, len))
393 appenditem(item, &lprefix, &prefixend);
394 else if(fstrstr(item->text, text))
395 appenditem(item, &lsubstr, &substrend);
402 matchend->right = lprefix;
403 lprefix->left = matchend;
407 matchend = prefixend;
411 matchend->right = lsubstr;
412 lsubstr->left = matchend;
416 matchend = substrend;
418 curr = sel = matches;
426 for(n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc);
437 XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
438 utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
439 insert(p, (q = strchr(p, '\n')) ? q-p : (ssize_t)strlen(p));
446 char buf[sizeof text], *p, *maxstr = NULL;
447 size_t i, max = 0, size = 0;
449 for(i = 0; fgets(buf, sizeof buf, stdin); i++) {
450 if(i+1 >= size / sizeof *items)
451 if(!(items = realloc(items, (size += BUFSIZ))))
452 eprintf("cannot realloc %u bytes:", size);
453 if((p = strchr(buf, '\n')))
455 if(!(items[i].text = strdup(buf)))
456 eprintf("cannot strdup %u bytes:", strlen(buf)+1);
457 if(strlen(items[i].text) > max)
458 max = strlen(maxstr = items[i].text);
461 items[i].text = NULL;
462 inputw = maxstr ? textw(dc, maxstr) : 0;
469 while(!XNextEvent(dc->dpy, &ev))
472 if(ev.xexpose.count == 0)
478 case SelectionNotify:
479 if(ev.xselection.property == utf8)
482 case VisibilityNotify:
483 if(ev.xvisibility.state != VisibilityUnobscured)
484 XRaiseWindow(dc->dpy, win);
491 int x, y, screen = DefaultScreen(dc->dpy);
492 Window root = RootWindow(dc->dpy, screen);
493 XSetWindowAttributes wa;
496 XineramaScreenInfo *info;
499 normcol[ColBG] = getcolor(dc, normbgcolor);
500 normcol[ColFG] = getcolor(dc, normfgcolor);
501 selcol[ColBG] = getcolor(dc, selbgcolor);
502 selcol[ColFG] = getcolor(dc, selfgcolor);
504 utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
507 bh = dc->font.height + 2;
508 lines = MAX(lines, 0);
509 mh = (lines + 1) * bh;
511 if((info = XineramaQueryScreens(dc->dpy, &n))) {
516 XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
517 for(i = 0; i < n-1; i++)
518 if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
521 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
529 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
530 mw = DisplayWidth(dc->dpy, screen);
532 promptw = prompt ? textw(dc, prompt) : 0;
533 inputw = MIN(inputw, mw/3);
537 wa.override_redirect = True;
538 wa.background_pixmap = ParentRelative;
539 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
540 win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
541 DefaultDepth(dc->dpy, screen), CopyFromParent,
542 DefaultVisual(dc->dpy, screen),
543 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
545 XMapRaised(dc->dpy, win);
546 resizedc(dc, mw, mh);
552 fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font]\n"
553 " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr);