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;
22 Item *next; /* traverses all items */
23 Item *left, *right; /* traverses matching items */
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 *s, ssize_t n);
32 static void keypress(XKeyEvent *ev);
33 static void match(void);
34 static size_t nextrune(int incr);
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;
42 static int inputw = 0;
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, *sel;
60 static Item *prev, *curr, *next;
61 static Window root, win;
63 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
66 main(int argc, char *argv[]) {
71 for(i = 1; i < argc; i++)
73 if(!strcmp(argv[i], "-v")) {
74 fputs("dmenu-"VERSION", © 2006-2011 dmenu engineers, see LICENSE for details\n", stdout);
77 else if(!strcmp(argv[i], "-b"))
79 else if(!strcmp(argv[i], "-f"))
81 else if(!strcmp(argv[i], "-i"))
82 fstrncmp = strncasecmp;
86 else if(!strcmp(argv[i], "-l"))
87 lines = atoi(argv[++i]);
88 else if(!strcmp(argv[i], "-m"))
89 monitor = 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];
121 fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-m monitor] [-p prompt] [-fn font]\n"
122 " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
127 appenditem(Item *item, Item **list, Item **last) {
131 (*last)->right = item;
144 n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">"));
146 for(i = 0, next = curr; next; next = next->right)
147 if((i += (lines > 0) ? bh : MIN(textw(dc, next->text), n)) > n)
149 for(i = 0, prev = curr; prev && prev->left; prev = prev->left)
150 if((i += (lines > 0) ? bh : MIN(textw(dc, prev->left->text), n)) > n)
162 drawrect(dc, 0, 0, mw, mh, True, BG(dc, normcol));
166 drawtext(dc, prompt, selcol);
169 dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw;
170 drawtext(dc, text, normcol);
171 if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w)
172 drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol));
176 for(item = curr; item != next; item = item->right) {
178 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
183 dc->w = textw(dc, "<");
185 drawtext(dc, "<", normcol);
186 for(item = curr; item != next; item = item->right) {
188 dc->w = MIN(textw(dc, item->text), mw - dc->x - textw(dc, ">"));
189 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
191 dc->w = textw(dc, ">");
194 drawtext(dc, ">", normcol);
196 mapdc(dc, win, mw, mh);
200 fstrstr(const char *s, const char *sub) {
203 for(len = strlen(sub); *s; s++)
204 if(!fstrncmp(s, sub, len))
213 for(i = 0; i < 1000; i++) {
214 if(!XGrabKeyboard(dc->dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime))
218 eprintf("cannot grab keyboard\n");
222 insert(const char *s, 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, s, n);
233 keypress(XKeyEvent *ev) {
239 XLookupString(ev, buf, sizeof buf, &ksym, NULL);
240 if(ev->state & ControlMask)
241 switch(tolower(ksym)) {
271 case XK_k: /* delete right */
281 case XK_u: /* delete left */
282 insert(NULL, 0 - cursor);
284 case XK_w: /* delete word */
285 while(cursor > 0 && text[nextrune(-1)] == ' ')
286 insert(NULL, nextrune(-1) - cursor);
287 while(cursor > 0 && text[nextrune(-1)] != ' ')
288 insert(NULL, nextrune(-1) - cursor);
290 case XK_y: /* paste selection */
291 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
297 insert(buf, strlen(buf));
302 cursor = nextrune(+1);
305 insert(NULL, nextrune(-1) - cursor);
316 while(sel && sel->right)
326 sel = curr = matches;
330 if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
331 cursor = nextrune(-1);
337 if(sel && sel->left && (sel = sel->left)->right == curr) {
356 fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout);
360 cursor = nextrune(+1);
366 if(sel && sel->right && (sel = sel->right) == next) {
374 strncpy(text, sel->text, sizeof text);
375 cursor = strlen(text);
385 Item *item, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
388 matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
389 for(item = items; item; item = item->next)
390 if(!fstrncmp(text, item->text, len + 1))
391 appenditem(item, &lexact, &exactend);
392 else if(!fstrncmp(text, item->text, len))
393 appenditem(item, &lprefix, &prefixend);
394 else if(fstrstr(item->text, text))
395 appenditem(item, &lsubstr, &substrend);
403 itemend->right = lprefix;
404 lprefix->left = itemend;
412 itemend->right = lsubstr;
413 lsubstr->left = itemend;
418 curr = sel = matches;
427 for(n = cursor + incr; n >= 0 && n < len && (text[n] & 0xc0) == 0x80; n += incr);
438 XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
439 utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
440 insert(p, (q = strchr(p, '\n')) ? q-p : strlen(p));
447 char buf[sizeof text], *p;
450 for(end = &items; fgets(buf, sizeof buf, stdin); *end = item, end = &item->next) {
451 if((p = strchr(buf, '\n')))
453 if(!(item = calloc(1, sizeof *item)))
454 eprintf("cannot malloc %u bytes:", sizeof *item);
455 if(!(item->text = strdup(buf)))
456 eprintf("cannot strdup %u bytes:", strlen(buf)+1);
457 inputw = MAX(inputw, textw(dc, item->text));
465 while(!XNextEvent(dc->dpy, &ev))
468 if(ev.xexpose.count == 0)
474 case SelectionNotify:
475 if(ev.xselection.property == utf8)
478 case VisibilityNotify:
479 if(ev.xvisibility.state != VisibilityUnobscured)
480 XRaiseWindow(dc->dpy, win);
488 XSetWindowAttributes wa;
491 XineramaScreenInfo *info;
494 screen = DefaultScreen(dc->dpy);
495 root = RootWindow(dc->dpy, screen);
496 utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
498 normcol[ColBG] = getcolor(dc, normbgcolor);
499 normcol[ColFG] = getcolor(dc, normfgcolor);
500 selcol[ColBG] = getcolor(dc, selbgcolor);
501 selcol[ColFG] = getcolor(dc, selfgcolor);
504 bh = dc->font.height + 2;
505 lines = MAX(lines, 0);
506 mh = (lines + 1) * bh;
508 if((info = XineramaQueryScreens(dc->dpy, &n))) {
513 XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
514 for(i = 0; i < n-1; i++)
515 if((monitor == info[i].screen_number)
516 || (monitor < 0 && INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height)))
519 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
527 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
528 mw = DisplayWidth(dc->dpy, screen);
531 wa.override_redirect = True;
532 wa.background_pixmap = ParentRelative;
533 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
534 win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
535 DefaultDepth(dc->dpy, screen), CopyFromParent,
536 DefaultVisual(dc->dpy, screen),
537 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
540 resizedc(dc, mw, mh);
541 inputw = MIN(inputw, mw/3);
542 promptw = prompt ? textw(dc, prompt) : 0;
543 XMapRaised(dc->dpy, win);