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))
19 typedef struct Item Item;
25 static void appenditem(Item *item, Item **list, Item **last);
26 static void calcoffsets(void);
27 static void drawmenu(void);
28 static char *fstrstr(const char *s, const char *sub);
29 static void grabkeyboard(void);
30 static void insert(const char *s, ssize_t n);
31 static void keypress(XKeyEvent *ev);
32 static void match(void);
33 static size_t nextrune(int incr);
34 static void paste(void);
35 static void readstdin(void);
36 static void run(void);
37 static void setup(void);
39 static char text[BUFSIZ] = "";
40 static int bh, mw, mh;
43 static int monitor = -1;
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;
62 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
65 main(int argc, char *argv[]) {
69 for(i = 1; i < argc; i++)
71 if(!strcmp(argv[i], "-v")) {
72 fputs("dmenu-"VERSION", © 2006-2011 dmenu engineers, see LICENSE for details\n", stdout);
75 else if(!strcmp(argv[i], "-b"))
77 else if(!strcmp(argv[i], "-f"))
79 else if(!strcmp(argv[i], "-i"))
80 fstrncmp = strncasecmp;
84 else if(!strcmp(argv[i], "-l"))
85 lines = atoi(argv[++i]);
86 else if(!strcmp(argv[i], "-m"))
87 monitor = atoi(argv[++i]);
88 else if(!strcmp(argv[i], "-p"))
90 else if(!strcmp(argv[i], "-fn"))
92 else if(!strcmp(argv[i], "-nb"))
93 normbgcolor = argv[++i];
94 else if(!strcmp(argv[i], "-nf"))
95 normfgcolor = argv[++i];
96 else if(!strcmp(argv[i], "-sb"))
97 selbgcolor = argv[++i];
98 else if(!strcmp(argv[i], "-sf"))
99 selfgcolor = argv[++i];
119 fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-m monitor] [-p prompt] [-fn font]\n"
120 " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
125 appenditem(Item *item, Item **list, Item **last) {
129 (*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)
160 drawrect(dc, 0, 0, mw, mh, True, BG(dc, normcol));
164 drawtext(dc, prompt, selcol);
167 dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw;
168 drawtext(dc, text, normcol);
169 if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w)
170 drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol));
174 for(item = curr; item != next; item = item->right) {
176 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
181 dc->w = textw(dc, "<");
183 drawtext(dc, "<", normcol);
184 for(item = curr; item != next; item = item->right) {
186 dc->w = MIN(textw(dc, item->text), mw - dc->x - textw(dc, ">"));
187 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
189 dc->w = textw(dc, ">");
192 drawtext(dc, ">", normcol);
194 mapdc(dc, win, mw, mh);
198 fstrstr(const char *s, const char *sub) {
201 for(len = strlen(sub); *s; s++)
202 if(!fstrncmp(s, sub, len))
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 *s, 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], s, n);
232 keypress(XKeyEvent *ev) {
238 XLookupString(ev, buf, sizeof buf, &ksym, NULL);
239 if(ev->state & ControlMask)
240 switch(tolower(ksym)) {
270 case XK_k: /* delete right */
280 case XK_u: /* delete left */
281 insert(NULL, 0 - cursor);
283 case XK_w: /* delete word */
284 while(cursor > 0 && text[nextrune(-1)] == ' ')
285 insert(NULL, nextrune(-1) - cursor);
286 while(cursor > 0 && text[nextrune(-1)] != ' ')
287 insert(NULL, nextrune(-1) - cursor);
289 case XK_y: /* paste selection */
290 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
296 insert(buf, strlen(buf));
301 cursor = nextrune(+1);
304 insert(NULL, nextrune(-1) - cursor);
316 while(next && (curr = curr->right))
328 sel = curr = matches;
332 if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
333 cursor = nextrune(-1);
339 if(sel && sel->left && (sel = sel->left)->right == curr) {
358 fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout);
362 cursor = nextrune(+1);
368 if(sel && sel->right && (sel = sel->right) == next) {
376 strncpy(text, sel->text, sizeof text);
377 cursor = strlen(text);
386 size_t len = strlen(text);
387 Item *item, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
389 matches = lexact = lprefix = lsubstr = matchend = exactend = prefixend = substrend = NULL;
390 for(item = items; item && item->text; item++)
391 if(!fstrncmp(text, item->text, len + 1))
392 appenditem(item, &lexact, &exactend);
393 else if(!fstrncmp(text, item->text, len))
394 appenditem(item, &lprefix, &prefixend);
395 else if(fstrstr(item->text, text))
396 appenditem(item, &lsubstr, &substrend);
404 matchend->right = lprefix;
405 lprefix->left = matchend;
409 matchend = prefixend;
413 matchend->right = lsubstr;
414 lsubstr->left = matchend;
418 matchend = substrend;
420 curr = sel = matches;
426 size_t n, len = strlen(text);
428 for(n = cursor + incr; n >= 0 && n < len && (text[n] & 0xc0) == 0x80; n += incr);
439 XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
440 utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
441 insert(p, (q = strchr(p, '\n')) ? q-p : strlen(p));
448 char buf[sizeof text], *p, *maxstr = NULL;
449 size_t i, max = 0, size = 0;
451 for(i = 0; fgets(buf, sizeof buf, stdin); items[++i].text = NULL) {
452 if(i+1 >= size / sizeof *items)
453 if(!(items = realloc(items, (size += BUFSIZ))))
454 eprintf("cannot realloc %u bytes:", size);
455 if((p = strchr(buf, '\n')))
457 if(!(items[i].text = strdup(buf)))
458 eprintf("cannot strdup %u bytes:", strlen(buf)+1);
459 if(strlen(items[i].text) > max)
460 max = strlen(maxstr = items[i].text);
462 inputw = maxstr ? textw(dc, maxstr) : 0;
469 while(!XNextEvent(dc->dpy, &ev))
472 if(ev.xexpose.count == 0)
478 case SelectionNotify:
479 if(ev.xselection.property == utf8)
482 case VisibilityNotify:
483 if(ev.xvisibility.state != VisibilityUnobscured)
484 XRaiseWindow(dc->dpy, win);
491 int x, y, screen = DefaultScreen(dc->dpy);
492 Window root = RootWindow(dc->dpy, screen);
493 XSetWindowAttributes wa;
496 XineramaScreenInfo *info;
499 normcol[ColBG] = getcolor(dc, normbgcolor);
500 normcol[ColFG] = getcolor(dc, normfgcolor);
501 selcol[ColBG] = getcolor(dc, selbgcolor);
502 selcol[ColFG] = getcolor(dc, selfgcolor);
504 utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
507 bh = dc->font.height + 2;
508 lines = MAX(lines, 0);
509 mh = (lines + 1) * bh;
511 if((info = XineramaQueryScreens(dc->dpy, &n))) {
516 XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
517 for(i = 0; i < n-1; i++)
518 if((monitor == info[i].screen_number)
519 || (monitor < 0 && INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height)))
522 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
530 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
531 mw = DisplayWidth(dc->dpy, screen);
533 inputw = MIN(inputw, mw/3);
534 promptw = prompt ? textw(dc, prompt) : 0;
538 wa.override_redirect = True;
539 wa.background_pixmap = ParentRelative;
540 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
541 win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
542 DefaultDepth(dc->dpy, screen), CopyFromParent,
543 DefaultVisual(dc->dpy, screen),
544 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
546 XMapRaised(dc->dpy, win);
547 resizedc(dc, mw, mh);