1 /* See LICENSE file for copyright and license details. */
11 #include <X11/extensions/Xinerama.h>
15 #define INRECT(x,y,rx,ry,rw,rh) ((x) >= (rx) && (x) < (rx)+(rw) && (y) >= (ry) && (y) < (ry)+(rh))
16 #define MIN(a,b) ((a) < (b) ? (a) : (b))
17 #define MAX(a,b) ((a) > (b) ? (a) : (b))
18 #define UTF8_CODEPOINT(c) (((c) & 0xc0) != 0x80)
20 typedef struct Item Item;
23 Item *next; /* traverses all items */
24 Item *left, *right; /* traverses matching items */
27 static void appenditem(Item *item, Item **list, Item **last);
28 static void calcoffsetsh(void);
29 static void calcoffsetsv(void);
30 static char *cistrstr(const char *s, const char *sub);
31 static void drawmenu(void);
32 static void drawmenuh(void);
33 static void drawmenuv(void);
34 static void grabkeyboard(void);
35 static void insert(const char *s, ssize_t n);
36 static void keypress(XKeyEvent *e);
37 static void match(void);
38 static void paste(void);
39 static void readstdin(void);
40 static void run(void);
41 static void setup(void);
42 static void usage(void);
44 static char text[4096];
45 static size_t cursor = 0;
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 inputw = 0;
52 static unsigned int lines = 0;
53 static unsigned int mw, mh;
54 static unsigned int promptw;
55 static unsigned long normcol[ColLast];
56 static unsigned long selcol[ColLast];
58 static Bool topbar = True;
60 static Item *allitems, *matches;
61 static Item *curr, *prev, *next, *sel;
62 static Window root, win;
64 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
65 static char *(*fstrstr)(const char *, const char *) = strstr;
66 static void (*calcoffsets)(void) = calcoffsetsh;
69 appenditem(Item *item, Item **list, Item **last) {
73 (*last)->right = item;
83 w = promptw + inputw + textw(dc, "<") + textw(dc, ">");
84 for(x = w, next = curr; next; next = next->right)
85 if((x += MIN(textw(dc, next->text), mw / 3)) > mw)
87 for(x = w, prev = curr; prev && prev->left; prev = prev->left)
88 if((x += MIN(textw(dc, prev->left->text), mw / 3)) > mw)
97 for(i = 0; i < lines && next; i++)
99 for(i = 0; i < lines && prev && prev->left; i++)
104 cistrstr(const char *s, const char *sub) {
107 for(len = strlen(sub); *s; s++)
108 if(!strncasecmp(s, sub, len))
117 drawrect(dc, 0, 0, mw, mh, BG(dc, normcol));
118 dc->h = dc->font.height + 2;
119 dc->y = topbar ? 0 : mh - dc->h;
123 drawtext(dc, prompt, selcol);
127 /* print input area */
128 if(matches && lines == 0 && textw(dc, text) <= inputw)
130 drawtext(dc, text, normcol);
131 drawrect(dc, textnw(dc, text, cursor) + dc->h/2 - 2, 2, 1, dc->h - 4, FG(dc, normcol));
134 else if(curr && (dc->w == inputw || curr->next))
144 dc->w = textw(dc, "<");
146 drawtext(dc, "<", normcol);
147 for(item = curr; item != next; item = item->right) {
149 dc->w = MIN(textw(dc, item->text), mw / 3);
150 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
152 dc->w = textw(dc, ">");
155 drawtext(dc, ">", normcol);
162 dc->y = topbar ? dc->h : 0;
164 for(item = curr; item != next; item = item->right) {
165 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
174 for(i = 0; i < 1000; i++) {
175 if(!XGrabKeyboard(dc->dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime))
179 eprintf("cannot grab keyboard\n");
183 insert(const char *s, ssize_t n) {
184 memmove(text + cursor + n, text + cursor, sizeof text - cursor - n);
186 memcpy(text + cursor, s, n);
192 keypress(XKeyEvent *e) {
193 char buf[sizeof text];
199 XLookupString(e, buf, sizeof buf, &ksym, NULL);
200 if(e->state & ControlMask) {
201 switch(tolower(ksym)) {
229 case XK_k: /* delete right */
239 case XK_u: /* delete left */
240 insert(NULL, -cursor);
242 case XK_w: /* delete word */
246 while(cursor - n++ > 0 && text[cursor - n] == ' ');
247 while(cursor - n++ > 0 && text[cursor - n] != ' ');
250 case XK_y: /* paste selection */
251 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, None, win, CurrentTime);
252 /* causes SelectionNotify event */
258 if(!iscntrl((int)*buf))
259 insert(buf, MIN(strlen(buf), sizeof text - cursor));
264 for(n = 1; cursor - n > 0 && !UTF8_CODEPOINT(text[cursor - n]); n++);
270 for(n = 1; cursor + n < len && !UTF8_CODEPOINT(text[cursor + n]); n++);
283 while(sel && sel->right)
293 sel = curr = matches;
297 if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
298 while(cursor-- > 0 && !UTF8_CODEPOINT(text[cursor]));
304 if(!sel || !sel->left)
307 if(sel->right == curr) {
326 fputs((sel && !(e->state & ShiftMask)) ? sel->text : text, stdout);
331 while(cursor++ < len && !UTF8_CODEPOINT(text[cursor]));
337 if(!sel || !sel->right)
348 strncpy(text, sel->text, sizeof text);
349 cursor = strlen(text);
359 Item *item, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
362 matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
363 for(item = allitems; item; item = item->next)
364 if(!fstrncmp(text, item->text, len + 1))
365 appenditem(item, &lexact, &exactend);
366 else if(!fstrncmp(text, item->text, len))
367 appenditem(item, &lprefix, &prefixend);
368 else if(fstrstr(item->text, text))
369 appenditem(item, &lsubstr, &substrend);
376 itemend->right = lprefix;
377 lprefix->left = itemend;
385 itemend->right = lsubstr;
386 lsubstr->left = itemend;
391 curr = prev = next = sel = matches;
402 XGetWindowProperty(dc->dpy, win, utf8, 0, sizeof text - cursor, True,
403 utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
404 insert(p, (q = strchr(p, '\n')) ? q-p : strlen(p));
411 char buf[sizeof text], *p;
415 for(item = NULL; fgets(buf, sizeof buf, stdin); item = new) {
416 if((p = strchr(buf, '\n')))
418 if(!(new = malloc(sizeof *new)))
419 eprintf("cannot malloc %u bytes\n", sizeof *new);
420 if(!(new->text = strdup(buf)))
421 eprintf("cannot strdup %u bytes\n", strlen(buf));
422 inputw = MAX(inputw, textw(dc, new->text));
423 new->next = new->left = new->right = NULL;
435 while(!XNextEvent(dc->dpy, &ev))
438 if(ev.xexpose.count == 0)
444 case SelectionNotify:
445 if(ev.xselection.property == utf8)
448 case VisibilityNotify:
449 if(ev.xvisibility.state != VisibilityUnobscured)
450 XRaiseWindow(dc->dpy, win);
458 XSetWindowAttributes wa;
461 XineramaScreenInfo *info;
464 screen = DefaultScreen(dc->dpy);
465 root = RootWindow(dc->dpy, screen);
466 utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
468 normcol[ColBG] = getcolor(dc, normbgcolor);
469 normcol[ColFG] = getcolor(dc, normfgcolor);
470 selcol[ColBG] = getcolor(dc, selbgcolor);
471 selcol[ColFG] = getcolor(dc, selfgcolor);
473 /* input window geometry */
474 mh = (dc->font.height + 2) * (lines + 1);
476 if((info = XineramaQueryScreens(dc->dpy, &n))) {
481 XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
482 for(i = 0; i < n; i++)
483 if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
486 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
494 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
495 mw = DisplayWidth(dc->dpy, screen);
499 wa.override_redirect = True;
500 wa.background_pixmap = ParentRelative;
501 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
502 win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
503 DefaultDepth(dc->dpy, screen), CopyFromParent,
504 DefaultVisual(dc->dpy, screen),
505 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
508 setcanvas(dc, win, mw, mh);
509 inputw = MIN(inputw, mw/3);
510 promptw = prompt ? MIN(textw(dc, prompt), mw/5) : 0;
511 XMapRaised(dc->dpy, win);
518 fputs("usage: dmenu [-b] [-i] [-l lines] [-p prompt] [-fn font] [-nb color]\n"
519 " [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
524 main(int argc, char *argv[]) {
530 for(i = 1; i < argc; i++)
532 if(!strcmp(argv[i], "-v")) {
533 fputs("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n", stdout);
536 else if(!strcmp(argv[i], "-b"))
538 else if(!strcmp(argv[i], "-i")) {
539 fstrncmp = strncasecmp;
545 else if(!strcmp(argv[i], "-l")) {
546 if((lines = atoi(argv[++i])) > 0)
547 calcoffsets = calcoffsetsv;
549 else if(!strcmp(argv[i], "-p"))
551 else if(!strcmp(argv[i], "-fn"))
552 initfont(dc, argv[i++]);
553 else if(!strcmp(argv[i], "-nb"))
554 normbgcolor = argv[++i];
555 else if(!strcmp(argv[i], "-nf"))
556 normfgcolor = argv[++i];
557 else if(!strcmp(argv[i], "-sb"))
558 selbgcolor = argv[++i];
559 else if(!strcmp(argv[i], "-sf"))
560 selfgcolor = argv[++i];
568 return EXIT_FAILURE; /* should not reach */