1 /* See LICENSE file for copyright and license details. */
10 #include <X11/Xutil.h>
12 #include <X11/extensions/Xinerama.h>
16 #define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \
17 * MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org)))
18 #define MIN(a,b) ((a) < (b) ? (a) : (b))
19 #define MAX(a,b) ((a) > (b) ? (a) : (b))
21 typedef struct Item Item;
27 static void appenditem(Item *item, Item **list, Item **last);
28 static void calcoffsets(void);
29 static char *cistrstr(const char *s, const char *sub);
30 static void drawmenu(void);
31 static void grabkeyboard(void);
32 static void insert(const char *str, ssize_t n);
33 static void keypress(XKeyEvent *ev);
34 static void match(void);
35 static size_t nextrune(int inc);
36 static void paste(void);
37 static void readstdin(void);
38 static void run(void);
39 static void setup(void);
40 static void usage(void);
42 static char text[BUFSIZ] = "";
43 static int bh, mw, mh;
44 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 int lines = 0;
53 static unsigned long normcol[ColLast];
54 static unsigned long selcol[ColLast];
56 static Bool topbar = True;
58 static Item *items = NULL;
59 static Item *matches, *matchend;
60 static Item *prev, *curr, *next, *sel;
64 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
65 static char *(*fstrstr)(const char *, const char *) = strstr;
68 main(int argc, char *argv[]) {
72 for(i = 1; i < argc; i++)
74 if(!strcmp(argv[i], "-v")) {
75 puts("dmenu-"VERSION", © 2006-2011 dmenu engineers, see LICENSE for details");
78 else if(!strcmp(argv[i], "-b"))
80 else if(!strcmp(argv[i], "-f"))
82 else if(!strcmp(argv[i], "-i")) {
83 fstrncmp = strncasecmp;
89 else if(!strcmp(argv[i], "-l"))
90 lines = atoi(argv[++i]);
91 else if(!strcmp(argv[i], "-p"))
93 else if(!strcmp(argv[i], "-fn"))
95 else if(!strcmp(argv[i], "-nb"))
96 normbgcolor = argv[++i];
97 else if(!strcmp(argv[i], "-nf"))
98 normfgcolor = argv[++i];
99 else if(!strcmp(argv[i], "-sb"))
100 selbgcolor = argv[++i];
101 else if(!strcmp(argv[i], "-sf"))
102 selfgcolor = argv[++i];
120 return EXIT_FAILURE; /* unreachable */
124 appenditem(Item *item, Item **list, Item **last) {
126 (*last)->right = item;
142 n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">"));
144 for(i = 0, next = curr; next; next = next->right)
145 if((i += (lines > 0) ? bh : MIN(textw(dc, next->text), n)) > n)
147 for(i = 0, prev = curr; prev && prev->left; prev = prev->left)
148 if((i += (lines > 0) ? bh : MIN(textw(dc, prev->left->text), n)) > n)
153 cistrstr(const char *s, const char *sub) {
156 for(len = strlen(sub); *s; s++)
157 if(!strncasecmp(s, sub, len))
170 drawrect(dc, 0, 0, mw, mh, True, BG(dc, normcol));
174 drawtext(dc, prompt, selcol);
177 dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw;
178 drawtext(dc, text, normcol);
179 if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w)
180 drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol));
184 for(item = curr; item != next; item = item->right) {
186 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
191 dc->w = textw(dc, "<");
193 drawtext(dc, "<", normcol);
194 for(item = curr; item != next; item = item->right) {
196 dc->w = MIN(textw(dc, item->text), mw - dc->x - textw(dc, ">"));
197 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
199 dc->w = textw(dc, ">");
202 drawtext(dc, ">", normcol);
204 mapdc(dc, win, mw, mh);
211 for(i = 0; i < 1000; i++) {
212 if(XGrabKeyboard(dc->dpy, DefaultRootWindow(dc->dpy), True,
213 GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
217 eprintf("cannot grab keyboard\n");
221 insert(const char *str, ssize_t n) {
222 if(strlen(text) + n > sizeof text - 1)
224 memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0));
226 memcpy(&text[cursor], str, n);
232 keypress(XKeyEvent *ev) {
235 KeySym ksym = NoSymbol;
238 len = XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status);
239 if(status == XBufferOverflow)
241 if(ev->state & ControlMask) {
244 XConvertCase(ksym, &lower, &upper);
246 case XK_a: ksym = XK_Home; break;
247 case XK_b: ksym = XK_Left; break;
248 case XK_c: ksym = XK_Escape; break;
249 case XK_d: ksym = XK_Delete; break;
250 case XK_e: ksym = XK_End; break;
251 case XK_f: ksym = XK_Right; break;
252 case XK_h: ksym = XK_BackSpace; break;
253 case XK_i: ksym = XK_Tab; break;
254 case XK_j: ksym = XK_Return; break;
255 case XK_m: ksym = XK_Return; break;
256 case XK_n: ksym = XK_Up; break;
257 case XK_p: ksym = XK_Down; break;
259 case XK_k: /* delete right */
263 case XK_u: /* delete left */
264 insert(NULL, 0 - cursor);
266 case XK_w: /* delete word */
267 while(cursor > 0 && text[nextrune(-1)] == ' ')
268 insert(NULL, nextrune(-1) - cursor);
269 while(cursor > 0 && text[nextrune(-1)] != ' ')
270 insert(NULL, nextrune(-1) - cursor);
272 case XK_y: /* paste selection */
273 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
285 if(text[cursor] == '\0')
287 cursor = nextrune(+1);
292 insert(NULL, nextrune(-1) - cursor);
295 if(text[cursor] != '\0') {
296 cursor = strlen(text);
304 while(next && (curr = curr->right))
316 sel = curr = matches;
320 if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
321 cursor = nextrune(-1);
326 if(sel && sel->left && (sel = sel->left)->right == curr) {
345 puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
348 if(text[cursor] != '\0') {
349 cursor = nextrune(+1);
354 if(sel && sel->right && (sel = sel->right) == next) {
362 strncpy(text, sel->text, sizeof text);
363 cursor = strlen(text);
372 static char **tokv = NULL;
375 char buf[sizeof text], *s;
378 Item *item, *lprefix, *lsubstr, *prefixend, *substrend;
381 for(s = strtok(buf, " "); s; tokv[tokc-1] = s, s = strtok(NULL, " "))
382 if(++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv)))
383 eprintf("cannot realloc %u bytes\n", tokn * sizeof *tokv);
384 len = tokc ? strlen(tokv[0]) : 0;
386 matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
387 for(item = items; item && item->text; item++) {
388 for(i = 0; i < tokc; i++)
389 if(!fstrstr(item->text, tokv[i]))
393 if(!tokc || !fstrncmp(tokv[0], item->text, len+1))
394 appenditem(item, &matches, &matchend);
395 else if(!fstrncmp(tokv[0], item->text, len))
396 appenditem(item, &lprefix, &prefixend);
398 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;
463 lines = MIN(lines, i);
470 while(!XNextEvent(dc->dpy, &ev)) {
471 if(XFilterEvent(&ev, win))
475 if(ev.xexpose.count == 0)
476 mapdc(dc, win, mw, mh);
481 case SelectionNotify:
482 if(ev.xselection.property == utf8)
485 case VisibilityNotify:
486 if(ev.xvisibility.state != VisibilityUnobscured)
487 XRaiseWindow(dc->dpy, win);
495 int x, y, screen = DefaultScreen(dc->dpy);
496 Window root = RootWindow(dc->dpy, screen);
497 XSetWindowAttributes swa;
501 XineramaScreenInfo *info;
504 normcol[ColBG] = getcolor(dc, normbgcolor);
505 normcol[ColFG] = getcolor(dc, normfgcolor);
506 selcol[ColBG] = getcolor(dc, selbgcolor);
507 selcol[ColFG] = getcolor(dc, selfgcolor);
509 utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
512 bh = dc->font.height + 2;
513 lines = MAX(lines, 0);
514 mh = (lines + 1) * bh;
516 if((info = XineramaQueryScreens(dc->dpy, &n))) {
517 int a, j, di, i = 0, area = 0;
519 Window w, pw, dw, *dws;
520 XWindowAttributes wa;
522 XGetInputFocus(dc->dpy, &w, &di);
523 if(w != root && w != PointerRoot && w != None) {
525 if(XQueryTree(dc->dpy, (pw = w), &dw, &w, &dws, &du) && dws)
527 } while(w != root && w != pw);
528 if(XGetWindowAttributes(dc->dpy, pw, &wa))
529 for(j = 0; j < n; j++)
530 if((a = INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j])) > area) {
535 if(!area && XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du))
536 for(i = 0; i < n; i++)
537 if(INTERSECT(x, y, 1, 1, info[i]))
540 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
548 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
549 mw = DisplayWidth(dc->dpy, screen);
551 promptw = prompt ? textw(dc, prompt) : 0;
552 inputw = MIN(inputw, mw/3);
556 swa.override_redirect = True;
557 swa.background_pixmap = ParentRelative;
558 swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
559 win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
560 DefaultDepth(dc->dpy, screen), CopyFromParent,
561 DefaultVisual(dc->dpy, screen),
562 CWOverrideRedirect | CWBackPixmap | CWEventMask, &swa);
565 xim = XOpenIM(dc->dpy, NULL, NULL, NULL);
566 xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
567 XNClientWindow, win, XNFocusWindow, win, NULL);
569 XMapRaised(dc->dpy, win);
570 resizedc(dc, mw, mh);
576 fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font]\n"
577 " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr);