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 char *cistrstr(const char *s, const char *sub);
29 static void drawmenu(void);
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;
63 static char *(*fstrstr)(const char *, const char *) = strstr;
66 main(int argc, char *argv[]) {
70 for(i = 1; i < argc; i++)
72 if(!strcmp(argv[i], "-v")) {
73 puts("dmenu-"VERSION", © 2006-2011 dmenu engineers, see LICENSE for details");
76 else if(!strcmp(argv[i], "-b"))
78 else if(!strcmp(argv[i], "-f"))
80 else if(!strcmp(argv[i], "-i")) {
81 fstrncmp = strncasecmp;
87 else if(!strcmp(argv[i], "-l"))
88 lines = atoi(argv[++i]);
89 else if(!strcmp(argv[i], "-p"))
91 else if(!strcmp(argv[i], "-fn"))
93 else if(!strcmp(argv[i], "-nb"))
94 normbgcolor = argv[++i];
95 else if(!strcmp(argv[i], "-nf"))
96 normfgcolor = argv[++i];
97 else if(!strcmp(argv[i], "-sb"))
98 selbgcolor = argv[++i];
99 else if(!strcmp(argv[i], "-sf"))
100 selfgcolor = argv[++i];
118 return EXIT_FAILURE; /* unreachable */
122 appenditem(Item *item, Item **list, Item **last) {
126 (*last)->right = item;
140 n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">"));
142 for(i = 0, next = curr; next; next = next->right)
143 if((i += (lines > 0) ? bh : MIN(textw(dc, next->text), n)) > n)
145 for(i = 0, prev = curr; prev && prev->left; prev = prev->left)
146 if((i += (lines > 0) ? bh : MIN(textw(dc, prev->left->text), n)) > n)
151 cistrstr(const char *s, const char *sub) {
154 for(len = strlen(sub); *s; s++)
155 if(!strncasecmp(s, sub, len))
168 drawrect(dc, 0, 0, mw, mh, True, BG(dc, normcol));
172 drawtext(dc, prompt, selcol);
175 dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw;
176 drawtext(dc, text, normcol);
177 if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w)
178 drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol));
182 for(item = curr; item != next; item = item->right) {
184 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
189 dc->w = textw(dc, "<");
191 drawtext(dc, "<", normcol);
192 for(item = curr; item != next; item = item->right) {
194 dc->w = MIN(textw(dc, item->text), mw - dc->x - textw(dc, ">"));
195 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
197 dc->w = textw(dc, ">");
200 drawtext(dc, ">", normcol);
202 mapdc(dc, win, mw, mh);
209 for(i = 0; i < 1000; i++) {
210 if(XGrabKeyboard(dc->dpy, DefaultRootWindow(dc->dpy), True,
211 GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
215 eprintf("cannot grab keyboard\n");
219 insert(const char *str, ssize_t n) {
220 if(strlen(text) + n > sizeof text - 1)
222 memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0));
224 memcpy(&text[cursor], str, n);
226 match(n > 0 && text[cursor] == '\0');
230 keypress(XKeyEvent *ev) {
234 XLookupString(ev, buf, sizeof buf, &ksym, NULL);
235 if(ev->state & ControlMask) {
238 XConvertCase(ksym, &lower, &upper);
240 case XK_a: ksym = XK_Home; break;
241 case XK_b: ksym = XK_Left; break;
242 case XK_c: ksym = XK_Escape; break;
243 case XK_d: ksym = XK_Delete; break;
244 case XK_e: ksym = XK_End; break;
245 case XK_f: ksym = XK_Right; break;
246 case XK_h: ksym = XK_BackSpace; break;
247 case XK_i: ksym = XK_Tab; break;
248 case XK_j: ksym = XK_Return; break;
249 case XK_m: ksym = XK_Return; break;
250 case XK_n: ksym = XK_Up; break;
251 case XK_p: ksym = XK_Down; break;
253 case XK_k: /* delete right */
257 case XK_u: /* delete left */
258 insert(NULL, 0 - cursor);
260 case XK_w: /* delete word */
261 while(cursor > 0 && text[nextrune(-1)] == ' ')
262 insert(NULL, nextrune(-1) - cursor);
263 while(cursor > 0 && text[nextrune(-1)] != ' ')
264 insert(NULL, nextrune(-1) - cursor);
266 case XK_y: /* paste selection */
267 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
276 insert(buf, strlen(buf));
279 if(text[cursor] == '\0')
281 cursor = nextrune(+1);
285 insert(NULL, nextrune(-1) - cursor);
288 if(text[cursor] != '\0') {
289 cursor = strlen(text);
297 while(next && (curr = curr->right))
309 sel = curr = matches;
313 if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
314 cursor = nextrune(-1);
319 if(sel && sel->left && (sel = sel->left)->right == curr) {
338 puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
341 if(text[cursor] != '\0') {
342 cursor = nextrune(+1);
347 if(sel && sel->right && (sel = sel->right) == next) {
355 strncpy(text, sel->text, sizeof text);
356 cursor = strlen(text);
365 size_t len = strlen(text);
366 Item *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
369 lexact = lprefix = lsubstr = exactend = prefixend = substrend = NULL;
370 for(item = sub ? matches : items; item && item->text; item = lnext) {
371 lnext = sub ? item->right : item + 1;
372 if(!fstrncmp(text, item->text, len + 1))
373 appenditem(item, &lexact, &exactend);
374 else if(!fstrncmp(text, item->text, len))
375 appenditem(item, &lprefix, &prefixend);
376 else if(fstrstr(item->text, text))
377 appenditem(item, &lsubstr, &substrend);
384 matchend->right = lprefix;
385 lprefix->left = matchend;
389 matchend = prefixend;
393 matchend->right = lsubstr;
394 lsubstr->left = matchend;
398 matchend = substrend;
400 curr = sel = matches;
408 for(n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc);
419 XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
420 utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
421 insert(p, (q = strchr(p, '\n')) ? q-p : (ssize_t)strlen(p));
428 char buf[sizeof text], *p, *maxstr = NULL;
429 size_t i, max = 0, size = 0;
431 for(i = 0; fgets(buf, sizeof buf, stdin); i++) {
432 if(i+1 >= size / sizeof *items)
433 if(!(items = realloc(items, (size += BUFSIZ))))
434 eprintf("cannot realloc %u bytes:", size);
435 if((p = strchr(buf, '\n')))
437 if(!(items[i].text = strdup(buf)))
438 eprintf("cannot strdup %u bytes:", strlen(buf)+1);
439 if(strlen(items[i].text) > max)
440 max = strlen(maxstr = items[i].text);
443 items[i].text = NULL;
444 inputw = maxstr ? textw(dc, maxstr) : 0;
451 while(!XNextEvent(dc->dpy, &ev))
454 if(ev.xexpose.count == 0)
455 mapdc(dc, win, mw, mh);
460 case SelectionNotify:
461 if(ev.xselection.property == utf8)
464 case VisibilityNotify:
465 if(ev.xvisibility.state != VisibilityUnobscured)
466 XRaiseWindow(dc->dpy, win);
473 int x, y, screen = DefaultScreen(dc->dpy);
474 Window root = RootWindow(dc->dpy, screen);
475 XSetWindowAttributes wa;
478 XineramaScreenInfo *info;
481 normcol[ColBG] = getcolor(dc, normbgcolor);
482 normcol[ColFG] = getcolor(dc, normfgcolor);
483 selcol[ColBG] = getcolor(dc, selbgcolor);
484 selcol[ColFG] = getcolor(dc, selfgcolor);
486 utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
489 bh = dc->font.height + 2;
490 lines = MAX(lines, 0);
491 mh = (lines + 1) * bh;
493 if((info = XineramaQueryScreens(dc->dpy, &n))) {
498 XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
499 for(i = 0; i < n-1; i++)
500 if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
503 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
511 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
512 mw = DisplayWidth(dc->dpy, screen);
514 promptw = prompt ? textw(dc, prompt) : 0;
515 inputw = MIN(inputw, mw/3);
519 wa.override_redirect = True;
520 wa.background_pixmap = ParentRelative;
521 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
522 win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
523 DefaultDepth(dc->dpy, screen), CopyFromParent,
524 DefaultVisual(dc->dpy, screen),
525 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
527 XMapRaised(dc->dpy, win);
528 resizedc(dc, mw, mh);
534 fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font]\n"
535 " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr);