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 void drawmenu(void);
29 static char *fstrstr(const char *s, const char *sub);
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);
40 static char text[BUFSIZ] = "";
41 static int bh, mw, mh;
44 static int monitor = -1;
46 static size_t cursor = 0;
47 static const char *font = NULL;
48 static const char *prompt = NULL;
49 static const char *normbgcolor = "#cccccc";
50 static const char *normfgcolor = "#000000";
51 static const char *selbgcolor = "#0066ff";
52 static const char *selfgcolor = "#ffffff";
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;
63 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
66 main(int argc, char *argv[]) {
70 for(i = 1; i < argc; i++)
72 if(!strcmp(argv[i], "-v")) {
73 fputs("dmenu-"VERSION", © 2006-2011 dmenu engineers, see LICENSE for details\n", stdout);
76 else if(!strcmp(argv[i], "-b"))
78 else if(!strcmp(argv[i], "-f"))
80 else if(!strcmp(argv[i], "-i"))
81 fstrncmp = strncasecmp;
85 else if(!strcmp(argv[i], "-l"))
86 lines = atoi(argv[++i]);
87 else if(!strcmp(argv[i], "-m"))
88 monitor = 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];
120 fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-m monitor] [-p prompt] [-fn font]\n"
121 " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
126 appenditem(Item *item, Item **list, Item **last) {
130 (*last)->right = item;
143 n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">"));
145 for(i = 0, next = curr; next; next = next->right)
146 if((i += (lines > 0) ? bh : MIN(textw(dc, next->text), n)) > n)
148 for(i = 0, prev = curr; prev && prev->left; prev = prev->left)
149 if((i += (lines > 0) ? bh : MIN(textw(dc, prev->left->text), n)) > n)
161 drawrect(dc, 0, 0, mw, mh, True, BG(dc, normcol));
165 drawtext(dc, prompt, selcol);
168 dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw;
169 drawtext(dc, text, normcol);
170 if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w)
171 drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol));
175 for(item = curr; item != next; item = item->right) {
177 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
182 dc->w = textw(dc, "<");
184 drawtext(dc, "<", normcol);
185 for(item = curr; item != next; item = item->right) {
187 dc->w = MIN(textw(dc, item->text), mw - dc->x - textw(dc, ">"));
188 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
190 dc->w = textw(dc, ">");
193 drawtext(dc, ">", normcol);
195 mapdc(dc, win, mw, mh);
199 fstrstr(const char *s, const char *sub) {
202 for(len = strlen(sub); *s; s++)
203 if(!fstrncmp(s, sub, len))
212 for(i = 0; i < 1000; i++) {
213 if(XGrabKeyboard(dc->dpy, DefaultRootWindow(dc->dpy), True,
214 GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
218 eprintf("cannot grab keyboard\n");
222 insert(const char *str, ssize_t n) {
223 if(strlen(text) + n > sizeof text - 1)
225 memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0));
227 memcpy(&text[cursor], str, n);
233 keypress(XKeyEvent *ev) {
237 XLookupString(ev, buf, sizeof buf, &ksym, NULL);
238 if(ev->state & ControlMask) {
241 XConvertCase(ksym, &lower, &upper);
272 case XK_k: /* delete right */
282 case XK_u: /* delete left */
283 insert(NULL, 0 - cursor);
285 case XK_w: /* delete word */
286 while(cursor > 0 && text[nextrune(-1)] == ' ')
287 insert(NULL, nextrune(-1) - cursor);
288 while(cursor > 0 && text[nextrune(-1)] != ' ')
289 insert(NULL, nextrune(-1) - cursor);
291 case XK_y: /* paste selection */
292 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
299 insert(buf, strlen(buf));
302 if(text[cursor] == '\0')
304 cursor = nextrune(+1);
307 insert(NULL, nextrune(-1) - cursor);
310 if(text[cursor] != '\0') {
311 cursor = strlen(text);
319 while(next && (curr = curr->right))
331 sel = curr = matches;
335 if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
336 cursor = nextrune(-1);
342 if(sel && sel->left && (sel = sel->left)->right == curr) {
361 fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout);
364 if(text[cursor] != '\0') {
365 cursor = nextrune(+1);
371 if(sel && sel->right && (sel = sel->right) == next) {
379 strncpy(text, sel->text, sizeof text);
380 cursor = strlen(text);
389 size_t len = strlen(text);
390 Item *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
393 lexact = lprefix = lsubstr = exactend = prefixend = substrend = NULL;
394 for(item = sub ? matches : items; item && item->text; item = lnext) {
395 lnext = sub ? item->right : item + 1;
396 if(!fstrncmp(text, item->text, len + 1))
397 appenditem(item, &lexact, &exactend);
398 else if(!fstrncmp(text, item->text, len))
399 appenditem(item, &lprefix, &prefixend);
400 else if(fstrstr(item->text, text))
401 appenditem(item, &lsubstr, &substrend);
408 matchend->right = lprefix;
409 lprefix->left = matchend;
413 matchend = prefixend;
417 matchend->right = lsubstr;
418 lsubstr->left = matchend;
422 matchend = substrend;
424 curr = sel = matches;
432 for(n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc);
443 XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
444 utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
445 insert(p, (q = strchr(p, '\n')) ? q-p : (ssize_t)strlen(p));
452 char buf[sizeof text], *p, *maxstr = NULL;
453 size_t i, max = 0, size = 0;
455 for(i = 0; fgets(buf, sizeof buf, stdin); items[++i].text = NULL) {
456 if(i+1 >= size / sizeof *items)
457 if(!(items = realloc(items, (size += BUFSIZ))))
458 eprintf("cannot realloc %u bytes:", size);
459 if((p = strchr(buf, '\n')))
461 if(!(items[i].text = strdup(buf)))
462 eprintf("cannot strdup %u bytes:", strlen(buf)+1);
463 if(strlen(items[i].text) > max)
464 max = strlen(maxstr = items[i].text);
466 inputw = maxstr ? textw(dc, maxstr) : 0;
473 while(!XNextEvent(dc->dpy, &ev))
476 if(ev.xexpose.count == 0)
482 case SelectionNotify:
483 if(ev.xselection.property == utf8)
486 case VisibilityNotify:
487 if(ev.xvisibility.state != VisibilityUnobscured)
488 XRaiseWindow(dc->dpy, win);
495 int x, y, screen = DefaultScreen(dc->dpy);
496 Window root = RootWindow(dc->dpy, screen);
497 XSetWindowAttributes wa;
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))) {
520 XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
521 for(i = 0; i < n-1; i++)
522 if((monitor == info[i].screen_number)
523 || (monitor < 0 && INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height)))
526 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
534 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
535 mw = DisplayWidth(dc->dpy, screen);
537 inputw = MIN(inputw, mw/3);
538 promptw = prompt ? textw(dc, prompt) : 0;
542 wa.override_redirect = True;
543 wa.background_pixmap = ParentRelative;
544 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
545 win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
546 DefaultDepth(dc->dpy, screen), CopyFromParent,
547 DefaultVisual(dc->dpy, screen),
548 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
550 XMapRaised(dc->dpy, win);
551 resizedc(dc, mw, mh);