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);
39 static void usage(void);
41 static char text[BUFSIZ];
42 static int bh, mw, mh;
43 static int inputw = 0;
45 static int monitor = -1;
47 static size_t cursor = 0;
48 static const char *font = NULL;
49 static const char *prompt = NULL;
50 static const char *normbgcolor = "#cccccc";
51 static const char *normfgcolor = "#000000";
52 static const char *selbgcolor = "#0066ff";
53 static const char *selfgcolor = "#ffffff";
54 static unsigned long normcol[ColLast];
55 static unsigned long selcol[ColLast];
57 static Bool topbar = True;
59 static Item *items = NULL;
60 static Item *matches, *sel;
61 static Item *prev, *curr, *next;
62 static Window root, win;
64 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
67 main(int argc, char *argv[]) {
71 for(i = 1; i < argc; i++)
73 if(!strcmp(argv[i], "-v")) {
74 fputs("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n", stdout);
77 else if(!strcmp(argv[i], "-b"))
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];
109 return EXIT_FAILURE; /* should not reach */
113 appenditem(Item *item, Item **list, Item **last) {
117 (*last)->right = item;
130 n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">"));
132 for(i = 0, next = curr; next; next = next->right)
133 if((i += (lines > 0) ? bh : MIN(textw(dc, next->text), n)) > n)
135 for(i = 0, prev = curr; prev && prev->left; prev = prev->left)
136 if((i += (lines > 0) ? bh : MIN(textw(dc, prev->left->text), n)) > n)
148 drawrect(dc, 0, 0, mw, mh, True, BG(dc, normcol));
152 drawtext(dc, prompt, selcol);
155 dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw;
156 drawtext(dc, text, normcol);
157 if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w)
158 drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol));
162 for(item = curr; item != next; item = item->right) {
164 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
169 dc->w = textw(dc, "<");
171 drawtext(dc, "<", normcol);
172 for(item = curr; item != next; item = item->right) {
174 dc->w = MIN(textw(dc, item->text), mw - dc->x - textw(dc, ">"));
175 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
177 dc->w = textw(dc, ">");
180 drawtext(dc, ">", normcol);
182 mapdc(dc, win, mw, mh);
186 fstrstr(const char *s, const char *sub) {
189 for(len = strlen(sub); *s; s++)
190 if(!fstrncmp(s, sub, len))
199 for(i = 0; i < 1000; i++) {
200 if(!XGrabKeyboard(dc->dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime))
204 eprintf("cannot grab keyboard\n");
208 insert(const char *s, ssize_t n) {
209 if(strlen(text) + n > sizeof text - 1)
211 memmove(text + cursor + n, text + cursor, sizeof text - cursor - MAX(n, 0));
213 memcpy(text + cursor, s, n);
219 keypress(XKeyEvent *ev) {
225 XLookupString(ev, buf, sizeof buf, &ksym, NULL);
226 if(ev->state & ControlMask) {
227 switch(tolower(ksym)) {
257 case XK_k: /* delete right */
267 case XK_u: /* delete left */
268 insert(NULL, 0 - cursor);
270 case XK_w: /* delete word */
271 while(cursor > 0 && text[nextrune(-1)] == ' ')
272 insert(NULL, nextrune(-1) - cursor);
273 while(cursor > 0 && text[nextrune(-1)] != ' ')
274 insert(NULL, nextrune(-1) - cursor);
276 case XK_y: /* paste selection */
277 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
284 insert(buf, strlen(buf));
289 cursor = nextrune(+1);
292 insert(NULL, nextrune(-1) - cursor);
303 while(sel && sel->right)
313 sel = curr = matches;
317 if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
318 cursor = nextrune(-1);
324 if(sel && sel->left && (sel = sel->left)->right == curr) {
343 fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout);
348 cursor = nextrune(+1);
354 if(sel && sel->right && (sel = sel->right) == next) {
362 strncpy(text, sel->text, sizeof text);
363 cursor = strlen(text);
373 Item *item, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
376 matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
377 for(item = items; item; item = item->next)
378 if(!fstrncmp(text, item->text, len + 1))
379 appenditem(item, &lexact, &exactend);
380 else if(!fstrncmp(text, item->text, len))
381 appenditem(item, &lprefix, &prefixend);
382 else if(fstrstr(item->text, text))
383 appenditem(item, &lsubstr, &substrend);
391 itemend->right = lprefix;
392 lprefix->left = itemend;
400 itemend->right = lsubstr;
401 lsubstr->left = itemend;
406 curr = prev = next = sel = matches;
415 for(n = cursor + incr; n >= 0 && n < len && (text[n] & 0xc0) == 0x80; n += incr);
426 XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
427 utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
428 insert(p, (q = strchr(p, '\n')) ? q-p : strlen(p));
435 char buf[sizeof text], *p;
438 for(end = &items; fgets(buf, sizeof buf, stdin); *end = item, end = &item->next) {
439 if((p = strchr(buf, '\n')))
441 if(!(item = malloc(sizeof *item)))
442 eprintf("cannot malloc %u bytes\n", sizeof *item);
443 if(!(item->text = strdup(buf)))
444 eprintf("cannot strdup %u bytes\n", strlen(buf)+1);
445 item->next = item->left = item->right = NULL;
446 inputw = MAX(inputw, textw(dc, item->text));
454 while(!XNextEvent(dc->dpy, &ev))
457 if(ev.xexpose.count == 0)
463 case SelectionNotify:
464 if(ev.xselection.property == utf8)
467 case VisibilityNotify:
468 if(ev.xvisibility.state != VisibilityUnobscured)
469 XRaiseWindow(dc->dpy, win);
477 XSetWindowAttributes wa;
480 XineramaScreenInfo *info;
483 screen = DefaultScreen(dc->dpy);
484 root = RootWindow(dc->dpy, screen);
485 utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
487 normcol[ColBG] = getcolor(dc, normbgcolor);
488 normcol[ColFG] = getcolor(dc, normfgcolor);
489 selcol[ColBG] = getcolor(dc, selbgcolor);
490 selcol[ColFG] = getcolor(dc, selfgcolor);
493 bh = dc->font.height + 2;
494 lines = MAX(lines, 0);
495 mh = (lines + 1) * bh;
497 if((info = XineramaQueryScreens(dc->dpy, &n))) {
502 XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
503 for(i = 0; i < n; i++)
504 if((monitor == info[i].screen_number)
505 || (monitor < 0 && INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height)))
508 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
516 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
517 mw = DisplayWidth(dc->dpy, screen);
520 wa.override_redirect = True;
521 wa.background_pixmap = ParentRelative;
522 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
523 win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
524 DefaultDepth(dc->dpy, screen), CopyFromParent,
525 DefaultVisual(dc->dpy, screen),
526 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
529 resizedc(dc, mw, mh);
530 inputw = MIN(inputw, mw/3);
531 promptw = prompt ? textw(dc, prompt) : 0;
532 XMapRaised(dc->dpy, win);
539 fputs("usage: dmenu [-b] [-i] [-l lines] [-m monitor] [-p prompt] [-fn font]\n"
540 " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr);