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);
286 insert(NULL, nextrune(-1) - cursor);
289 if(text[cursor] != '\0') {
290 cursor = strlen(text);
298 while(next && (curr = curr->right))
310 sel = curr = matches;
314 if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
315 cursor = nextrune(-1);
320 if(sel && sel->left && (sel = sel->left)->right == curr) {
339 puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
342 if(text[cursor] != '\0') {
343 cursor = nextrune(+1);
348 if(sel && sel->right && (sel = sel->right) == next) {
356 strncpy(text, sel->text, sizeof text);
357 cursor = strlen(text);
366 size_t len = strlen(text);
367 Item *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
370 lexact = lprefix = lsubstr = exactend = prefixend = substrend = NULL;
371 for(item = sub ? matches : items; item && item->text; item = lnext) {
372 lnext = sub ? item->right : item + 1;
373 if(!fstrncmp(text, item->text, len + 1))
374 appenditem(item, &lexact, &exactend);
375 else if(!fstrncmp(text, item->text, len))
376 appenditem(item, &lprefix, &prefixend);
377 else if(fstrstr(item->text, text))
378 appenditem(item, &lsubstr, &substrend);
385 matchend->right = lprefix;
386 lprefix->left = matchend;
390 matchend = prefixend;
394 matchend->right = lsubstr;
395 lsubstr->left = matchend;
399 matchend = substrend;
401 curr = sel = matches;
409 for(n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc);
420 XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
421 utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
422 insert(p, (q = strchr(p, '\n')) ? q-p : (ssize_t)strlen(p));
429 char buf[sizeof text], *p, *maxstr = NULL;
430 size_t i, max = 0, size = 0;
432 for(i = 0; fgets(buf, sizeof buf, stdin); i++) {
433 if(i+1 >= size / sizeof *items)
434 if(!(items = realloc(items, (size += BUFSIZ))))
435 eprintf("cannot realloc %u bytes:", size);
436 if((p = strchr(buf, '\n')))
438 if(!(items[i].text = strdup(buf)))
439 eprintf("cannot strdup %u bytes:", strlen(buf)+1);
440 if(strlen(items[i].text) > max)
441 max = strlen(maxstr = items[i].text);
444 items[i].text = NULL;
445 inputw = maxstr ? textw(dc, maxstr) : 0;
452 while(!XNextEvent(dc->dpy, &ev))
455 if(ev.xexpose.count == 0)
456 mapdc(dc, win, mw, mh);
461 case SelectionNotify:
462 if(ev.xselection.property == utf8)
465 case VisibilityNotify:
466 if(ev.xvisibility.state != VisibilityUnobscured)
467 XRaiseWindow(dc->dpy, win);
474 int x, y, screen = DefaultScreen(dc->dpy);
475 Window root = RootWindow(dc->dpy, screen);
476 XSetWindowAttributes wa;
479 XineramaScreenInfo *info;
482 normcol[ColBG] = getcolor(dc, normbgcolor);
483 normcol[ColFG] = getcolor(dc, normfgcolor);
484 selcol[ColBG] = getcolor(dc, selbgcolor);
485 selcol[ColFG] = getcolor(dc, selfgcolor);
487 utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
490 bh = dc->font.height + 2;
491 lines = MAX(lines, 0);
492 mh = (lines + 1) * bh;
494 if((info = XineramaQueryScreens(dc->dpy, &n))) {
499 XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
500 for(i = 0; i < n-1; i++)
501 if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
504 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
512 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
513 mw = DisplayWidth(dc->dpy, screen);
515 promptw = prompt ? textw(dc, prompt) : 0;
516 inputw = MIN(inputw, mw/3);
520 wa.override_redirect = True;
521 wa.background_pixmap = ParentRelative;
522 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
523 win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
524 DefaultDepth(dc->dpy, screen), CopyFromParent,
525 DefaultVisual(dc->dpy, screen),
526 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
528 XMapRaised(dc->dpy, win);
529 resizedc(dc, mw, mh);
535 fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font]\n"
536 " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr);