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;
44 static size_t cursor = 0;
45 static const char *font = NULL;
46 static const char *prompt = NULL;
47 static const char *normbgcolor = "#cccccc";
48 static const char *normfgcolor = "#000000";
49 static const char *selbgcolor = "#0066ff";
50 static const char *selfgcolor = "#ffffff";
51 static unsigned int lines = 0;
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) {
234 KeySym ksym = NoSymbol;
237 len = XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status);
238 if(status == XBufferOverflow)
240 if(ev->state & ControlMask) {
243 XConvertCase(ksym, &lower, &upper);
245 case XK_a: ksym = XK_Home; break;
246 case XK_b: ksym = XK_Left; break;
247 case XK_c: ksym = XK_Escape; break;
248 case XK_d: ksym = XK_Delete; break;
249 case XK_e: ksym = XK_End; break;
250 case XK_f: ksym = XK_Right; break;
251 case XK_h: ksym = XK_BackSpace; break;
252 case XK_i: ksym = XK_Tab; break;
253 case XK_j: ksym = XK_Return; break;
254 case XK_m: ksym = XK_Return; break;
255 case XK_n: ksym = XK_Up; break;
256 case XK_p: ksym = XK_Down; break;
258 case XK_k: /* delete right */
262 case XK_u: /* delete left */
263 insert(NULL, 0 - cursor);
265 case XK_w: /* delete word */
266 while(cursor > 0 && text[nextrune(-1)] == ' ')
267 insert(NULL, nextrune(-1) - cursor);
268 while(cursor > 0 && text[nextrune(-1)] != ' ')
269 insert(NULL, nextrune(-1) - cursor);
271 case XK_y: /* paste selection */
272 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
284 if(text[cursor] == '\0')
286 cursor = nextrune(+1);
291 insert(NULL, nextrune(-1) - cursor);
294 if(text[cursor] != '\0') {
295 cursor = strlen(text);
303 while(next && (curr = curr->right))
315 sel = curr = matches;
319 if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
320 cursor = nextrune(-1);
325 if(sel && sel->left && (sel = sel->left)->right == curr) {
344 puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
347 if(text[cursor] != '\0') {
348 cursor = nextrune(+1);
353 if(sel && sel->right && (sel = sel->right) == next) {
361 strncpy(text, sel->text, sizeof text);
362 cursor = strlen(text);
371 static char **tokv = NULL;
374 char buf[sizeof text], *s;
377 Item *item, *lprefix, *lsubstr, *prefixend, *substrend;
380 for(s = strtok(buf, " "); s; tokv[tokc-1] = s, s = strtok(NULL, " "))
381 if(++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv)))
382 eprintf("cannot realloc %u bytes\n", tokn * sizeof *tokv);
383 len = tokc ? strlen(tokv[0]) : 0;
385 matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
386 for(item = items; item && item->text; item++) {
387 for(i = 0; i < tokc; i++)
388 if(!fstrstr(item->text, tokv[i]))
392 if(!tokc || !fstrncmp(tokv[0], item->text, len+1))
393 appenditem(item, &matches, &matchend);
394 else if(!fstrncmp(tokv[0], item->text, len))
395 appenditem(item, &lprefix, &prefixend);
397 appenditem(item, &lsubstr, &substrend);
401 matchend->right = lprefix;
402 lprefix->left = matchend;
406 matchend = prefixend;
410 matchend->right = lsubstr;
411 lsubstr->left = matchend;
415 matchend = substrend;
417 curr = sel = matches;
425 for(n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc);
436 XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
437 utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
438 insert(p, (q = strchr(p, '\n')) ? q-p : (ssize_t)strlen(p));
445 char buf[sizeof text], *p, *maxstr = NULL;
446 size_t i, max = 0, size = 0;
448 for(i = 0; fgets(buf, sizeof buf, stdin); i++) {
449 if(i+1 >= size / sizeof *items)
450 if(!(items = realloc(items, (size += BUFSIZ))))
451 eprintf("cannot realloc %u bytes:", size);
452 if((p = strchr(buf, '\n')))
454 if(!(items[i].text = strdup(buf)))
455 eprintf("cannot strdup %u bytes:", strlen(buf)+1);
456 if(strlen(items[i].text) > max)
457 max = strlen(maxstr = items[i].text);
460 items[i].text = NULL;
461 inputw = maxstr ? textw(dc, maxstr) : 0;
462 lines = MIN(lines, i);
469 while(!XNextEvent(dc->dpy, &ev)) {
470 if(XFilterEvent(&ev, win))
474 if(ev.xexpose.count == 0)
475 mapdc(dc, win, mw, mh);
480 case SelectionNotify:
481 if(ev.xselection.property == utf8)
484 case VisibilityNotify:
485 if(ev.xvisibility.state != VisibilityUnobscured)
486 XRaiseWindow(dc->dpy, win);
494 int x, y, screen = DefaultScreen(dc->dpy);
495 Window root = RootWindow(dc->dpy, screen);
496 XSetWindowAttributes swa;
500 XineramaScreenInfo *info;
503 normcol[ColBG] = getcolor(dc, normbgcolor);
504 normcol[ColFG] = getcolor(dc, normfgcolor);
505 selcol[ColBG] = getcolor(dc, selbgcolor);
506 selcol[ColFG] = getcolor(dc, selfgcolor);
508 utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
511 bh = dc->font.height + 2;
512 lines = MAX(lines, 0);
513 mh = (lines + 1) * bh;
515 if((info = XineramaQueryScreens(dc->dpy, &n))) {
519 XWindowAttributes wa;
521 XGetInputFocus(dc->dpy, &w, &di);
522 if(w != root && w != PointerRoot && w != None && XGetWindowAttributes(dc->dpy, w, &wa))
523 XTranslateCoordinates(dc->dpy, w, root, wa.x, wa.y, &x, &y, &dw);
525 XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
526 for(i = 0; i < n-1; i++)
527 if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
530 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
538 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
539 mw = DisplayWidth(dc->dpy, screen);
541 promptw = prompt ? textw(dc, prompt) : 0;
542 inputw = MIN(inputw, mw/3);
546 swa.override_redirect = True;
547 swa.background_pixmap = ParentRelative;
548 swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
549 win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
550 DefaultDepth(dc->dpy, screen), CopyFromParent,
551 DefaultVisual(dc->dpy, screen),
552 CWOverrideRedirect | CWBackPixmap | CWEventMask, &swa);
555 xim = XOpenIM(dc->dpy, NULL, NULL, NULL);
556 xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
557 XNClientWindow, win, XNFocusWindow, win, NULL);
559 XMapRaised(dc->dpy, win);
560 resizedc(dc, mw, mh);
566 fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font]\n"
567 " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr);