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(void);
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;
63 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
64 static char *(*fstrstr)(const char *, const char *) = strstr;
67 main(int argc, char *argv[]) {
71 for(i = 1; i < argc; i++)
73 if(!strcmp(argv[i], "-v")) {
74 puts("dmenu-"VERSION", © 2006-2011 dmenu engineers, see LICENSE for details");
77 else if(!strcmp(argv[i], "-b"))
79 else if(!strcmp(argv[i], "-f"))
81 else if(!strcmp(argv[i], "-i")) {
82 fstrncmp = strncasecmp;
88 else if(!strcmp(argv[i], "-l"))
89 lines = atoi(argv[++i]);
90 else if(!strcmp(argv[i], "-p"))
92 else if(!strcmp(argv[i], "-fn"))
94 else if(!strcmp(argv[i], "-nb"))
95 normbgcolor = argv[++i];
96 else if(!strcmp(argv[i], "-nf"))
97 normfgcolor = argv[++i];
98 else if(!strcmp(argv[i], "-sb"))
99 selbgcolor = argv[++i];
100 else if(!strcmp(argv[i], "-sf"))
101 selfgcolor = argv[++i];
119 return EXIT_FAILURE; /* unreachable */
123 appenditem(Item *item, Item **list, Item **last) {
125 (*last)->right = item;
141 n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">"));
143 for(i = 0, next = curr; next; next = next->right)
144 if((i += (lines > 0) ? bh : MIN(textw(dc, next->text), n)) > n)
146 for(i = 0, prev = curr; prev && prev->left; prev = prev->left)
147 if((i += (lines > 0) ? bh : MIN(textw(dc, prev->left->text), n)) > n)
152 cistrstr(const char *s, const char *sub) {
155 for(len = strlen(sub); *s; s++)
156 if(!strncasecmp(s, sub, len))
169 drawrect(dc, 0, 0, mw, mh, True, BG(dc, normcol));
173 drawtext(dc, prompt, selcol);
176 dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw;
177 drawtext(dc, text, normcol);
178 if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w)
179 drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol));
183 for(item = curr; item != next; item = item->right) {
185 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
190 dc->w = textw(dc, "<");
192 drawtext(dc, "<", normcol);
193 for(item = curr; item != next; item = item->right) {
195 dc->w = MIN(textw(dc, item->text), mw - dc->x - textw(dc, ">"));
196 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
198 dc->w = textw(dc, ">");
201 drawtext(dc, ">", normcol);
203 mapdc(dc, win, mw, mh);
210 for(i = 0; i < 1000; i++) {
211 if(XGrabKeyboard(dc->dpy, DefaultRootWindow(dc->dpy), True,
212 GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
216 eprintf("cannot grab keyboard\n");
220 insert(const char *str, ssize_t n) {
221 if(strlen(text) + n > sizeof text - 1)
223 memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0));
225 memcpy(&text[cursor], str, n);
231 keypress(XKeyEvent *ev) {
237 len = XmbLookupString(xic, ev, buf, sizeof(buf), &ksym, &status);
238 if(ev->state & ControlMask) {
241 XConvertCase(ksym, &lower, &upper);
243 case XK_a: ksym = XK_Home; break;
244 case XK_b: ksym = XK_Left; break;
245 case XK_c: ksym = XK_Escape; break;
246 case XK_d: ksym = XK_Delete; break;
247 case XK_e: ksym = XK_End; break;
248 case XK_f: ksym = XK_Right; break;
249 case XK_h: ksym = XK_BackSpace; break;
250 case XK_i: ksym = XK_Tab; break;
251 case XK_j: ksym = XK_Return; break;
252 case XK_m: ksym = XK_Return; break;
253 case XK_n: ksym = XK_Up; break;
254 case XK_p: ksym = XK_Down; break;
256 case XK_k: /* delete right */
260 case XK_u: /* delete left */
261 insert(NULL, 0 - cursor);
263 case XK_w: /* delete word */
264 while(cursor > 0 && text[nextrune(-1)] == ' ')
265 insert(NULL, nextrune(-1) - cursor);
266 while(cursor > 0 && text[nextrune(-1)] != ' ')
267 insert(NULL, nextrune(-1) - cursor);
269 case XK_y: /* paste selection */
270 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
282 if(text[cursor] == '\0')
284 cursor = nextrune(+1);
289 insert(NULL, nextrune(-1) - cursor);
292 if(text[cursor] != '\0') {
293 cursor = strlen(text);
301 while(next && (curr = curr->right))
313 sel = curr = matches;
317 if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
318 cursor = nextrune(-1);
323 if(sel && sel->left && (sel = sel->left)->right == curr) {
342 puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
345 if(text[cursor] != '\0') {
346 cursor = nextrune(+1);
351 if(sel && sel->right && (sel = sel->right) == next) {
359 strncpy(text, sel->text, sizeof text);
360 cursor = strlen(text);
369 static char **tokv = NULL;
372 char buf[sizeof text], *s;
375 Item *item, *lprefix, *lsubstr, *prefixend, *substrend;
378 for(s = strtok(buf, " "); s; tokv[tokc-1] = s, s = strtok(NULL, " "))
379 if(++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv)))
380 eprintf("cannot realloc %u bytes\n", tokn * sizeof *tokv);
381 len = tokc ? strlen(tokv[0]) : 0;
383 matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
384 for(item = items; item && item->text; item++) {
385 for(i = 0; i < tokc; i++)
386 if(!fstrstr(item->text, tokv[i]))
390 if(!tokc || !fstrncmp(tokv[0], item->text, len+1))
391 appenditem(item, &matches, &matchend);
392 else if(!fstrncmp(tokv[0], item->text, len))
393 appenditem(item, &lprefix, &prefixend);
395 appenditem(item, &lsubstr, &substrend);
399 matchend->right = lprefix;
400 lprefix->left = matchend;
404 matchend = prefixend;
408 matchend->right = lsubstr;
409 lsubstr->left = matchend;
413 matchend = substrend;
415 curr = sel = matches;
423 for(n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc);
434 XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
435 utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
436 insert(p, (q = strchr(p, '\n')) ? q-p : (ssize_t)strlen(p));
443 char buf[sizeof text], *p, *maxstr = NULL;
444 size_t i, max = 0, size = 0;
446 for(i = 0; fgets(buf, sizeof buf, stdin); i++) {
447 if(i+1 >= size / sizeof *items)
448 if(!(items = realloc(items, (size += BUFSIZ))))
449 eprintf("cannot realloc %u bytes:", size);
450 if((p = strchr(buf, '\n')))
452 if(!(items[i].text = strdup(buf)))
453 eprintf("cannot strdup %u bytes:", strlen(buf)+1);
454 if(strlen(items[i].text) > max)
455 max = strlen(maxstr = items[i].text);
458 items[i].text = NULL;
459 inputw = maxstr ? textw(dc, maxstr) : 0;
460 lines = MIN(lines, i);
467 while(!XNextEvent(dc->dpy, &ev)) {
468 if(XFilterEvent(&ev, win))
472 if(ev.xexpose.count == 0)
473 mapdc(dc, win, mw, mh);
478 case SelectionNotify:
479 if(ev.xselection.property == utf8)
482 case VisibilityNotify:
483 if(ev.xvisibility.state != VisibilityUnobscured)
484 XRaiseWindow(dc->dpy, win);
492 int x, y, screen = DefaultScreen(dc->dpy);
493 Window root = RootWindow(dc->dpy, screen);
494 XSetWindowAttributes swa;
498 XineramaScreenInfo *info;
501 normcol[ColBG] = getcolor(dc, normbgcolor);
502 normcol[ColFG] = getcolor(dc, normfgcolor);
503 selcol[ColBG] = getcolor(dc, selbgcolor);
504 selcol[ColFG] = getcolor(dc, selfgcolor);
506 utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
509 bh = dc->font.height + 2;
510 lines = MAX(lines, 0);
511 mh = (lines + 1) * bh;
513 if((info = XineramaQueryScreens(dc->dpy, &n))) {
517 XWindowAttributes wa;
519 XGetInputFocus(dc->dpy, &w, &di);
520 if(w != root && w != PointerRoot && w != None && XGetWindowAttributes(dc->dpy, w, &wa))
521 XTranslateCoordinates(dc->dpy, w, root, wa.x, wa.y, &x, &y, &dw);
523 XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
524 for(i = 0; i < n-1; i++)
525 if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
528 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
536 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
537 mw = DisplayWidth(dc->dpy, screen);
539 promptw = prompt ? textw(dc, prompt) : 0;
540 inputw = MIN(inputw, mw/3);
544 swa.override_redirect = True;
545 swa.background_pixmap = ParentRelative;
546 swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
547 win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
548 DefaultDepth(dc->dpy, screen), CopyFromParent,
549 DefaultVisual(dc->dpy, screen),
550 CWOverrideRedirect | CWBackPixmap | CWEventMask, &swa);
553 xim = XOpenIM(dc->dpy, NULL, NULL, NULL);
554 xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
555 XNClientWindow, win, XNFocusWindow, win, NULL);
557 XMapRaised(dc->dpy, win);
558 resizedc(dc, mw, mh);
564 fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font]\n"
565 " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr);