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 calcoffsets(void);
29 static void drawmenu(void);
30 static char *fstrstr(const char *s, const char *sub);
31 static void grabkeyboard(void);
32 static void insert(const char *s, ssize_t n);
33 static void keypress(XKeyEvent *ev);
34 static void match(void);
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 size_t cursor = 0;
43 static const char *font = NULL;
44 static const char *prompt = NULL;
45 static const char *normbgcolor = "#cccccc";
46 static const char *normfgcolor = "#000000";
47 static const char *selbgcolor = "#0066ff";
48 static const char *selfgcolor = "#ffffff";
49 static unsigned int bh, mw, mh;
50 static unsigned int inputw = 0;
51 static unsigned int lines = 0;
52 static unsigned int promptw;
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 appenditem(Item *item, Item **list, Item **last) {
70 (*last)->right = item;
83 n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">"));
85 for(i = 0, next = curr; next; next = next->right)
86 if((i += (lines > 0) ? bh : MIN(textw(dc, next->text), mw/3)) > n)
88 for(i = 0, prev = curr; prev && prev->left; prev = prev->left)
89 if((i += (lines > 0) ? bh : MIN(textw(dc, prev->left->text), mw/3)) > n)
101 drawrect(dc, 0, 0, mw, mh, True, BG(dc, normcol));
105 drawtext(dc, prompt, selcol);
108 dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw;
109 drawtext(dc, text, normcol);
110 if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w)
111 drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol));
115 for(item = curr; item != next; item = item->right) {
117 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
122 dc->w = textw(dc, "<");
124 drawtext(dc, "<", normcol);
125 for(item = curr; item != next; item = item->right) {
127 dc->w = MIN(textw(dc, item->text), mw/3);
128 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
130 dc->w = textw(dc, ">");
133 drawtext(dc, ">", normcol);
135 commitdraw(dc, win, mw, mh);
139 fstrstr(const char *s, const char *sub) {
142 for(len = strlen(sub); *s; s++)
143 if(!fstrncmp(s, sub, len))
152 for(i = 0; i < 1000; i++) {
153 if(!XGrabKeyboard(dc->dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime))
157 eprintf("cannot grab keyboard\n");
161 insert(const char *s, ssize_t n) {
162 memmove(text + cursor + n, text + cursor, sizeof text - cursor - n);
164 memcpy(text + cursor, s, n);
170 keypress(XKeyEvent *ev) {
177 XLookupString(ev, buf, sizeof buf, &ksym, NULL);
178 if(ev->state & ControlMask) {
179 switch(tolower(ksym)) {
209 case XK_k: /* delete right */
219 case XK_u: /* delete left */
220 insert(NULL, -cursor);
222 case XK_w: /* delete word */
226 while(cursor - n++ > 0 && text[cursor - n] == ' ');
227 while(cursor - n++ > 0 && text[cursor - n] != ' ');
230 case XK_y: /* paste selection */
231 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, None, win, CurrentTime);
238 insert(buf, MIN(strlen(buf), sizeof text - cursor));
243 for(n = 1; cursor - n > 0 && !UTF8_CODEPOINT(text[cursor - n]); n++);
249 for(n = 1; cursor + n < len && !UTF8_CODEPOINT(text[cursor + n]); n++);
262 while(sel && sel->right)
272 sel = curr = matches;
276 if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
277 while(cursor-- > 0 && !UTF8_CODEPOINT(text[cursor]));
283 if(!sel || !sel->left)
285 if((sel = sel->left)->right == curr) {
304 fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout);
309 while(cursor++ < len && !UTF8_CODEPOINT(text[cursor]));
315 if(!sel || !sel->right)
317 if((sel = sel->right) == next) {
325 strncpy(text, sel->text, sizeof text);
326 cursor = strlen(text);
336 Item *item, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
339 matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
340 for(item = items; item; item = item->next)
341 if(!fstrncmp(text, item->text, len + 1))
342 appenditem(item, &lexact, &exactend);
343 else if(!fstrncmp(text, item->text, len))
344 appenditem(item, &lprefix, &prefixend);
345 else if(fstrstr(item->text, text))
346 appenditem(item, &lsubstr, &substrend);
354 itemend->right = lprefix;
355 lprefix->left = itemend;
363 itemend->right = lsubstr;
364 lsubstr->left = itemend;
369 curr = prev = next = sel = matches;
380 XGetWindowProperty(dc->dpy, win, utf8, 0, sizeof text - cursor, True,
381 utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
382 insert(p, (q = strchr(p, '\n')) ? q-p : strlen(p));
389 char buf[sizeof text], *p;
392 for(end = &items; fgets(buf, sizeof buf, stdin); *end = item, end = &item->next) {
393 if((p = strchr(buf, '\n')))
395 if(!(item = malloc(sizeof *item)))
396 eprintf("cannot malloc %u bytes\n", sizeof *item);
397 if(!(item->text = strdup(buf)))
398 eprintf("cannot strdup %u bytes\n", strlen(buf)+1);
399 inputw = MAX(inputw, textw(dc, item->text));
400 item->next = item->left = item->right = NULL;
408 while(!XNextEvent(dc->dpy, &ev))
411 if(ev.xexpose.count == 0)
417 case SelectionNotify:
418 if(ev.xselection.property == utf8)
421 case VisibilityNotify:
422 if(ev.xvisibility.state != VisibilityUnobscured)
423 XRaiseWindow(dc->dpy, win);
431 XSetWindowAttributes wa;
434 XineramaScreenInfo *info;
437 screen = DefaultScreen(dc->dpy);
438 root = RootWindow(dc->dpy, screen);
439 utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
441 normcol[ColBG] = getcolor(dc, normbgcolor);
442 normcol[ColFG] = getcolor(dc, normfgcolor);
443 selcol[ColBG] = getcolor(dc, selbgcolor);
444 selcol[ColFG] = getcolor(dc, selfgcolor);
447 bh = dc->font.height + 2;
448 mh = (lines + 1) * bh;
450 if((info = XineramaQueryScreens(dc->dpy, &n))) {
455 XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
456 for(i = 0; i < n; i++)
457 if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
460 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
468 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
469 mw = DisplayWidth(dc->dpy, screen);
472 wa.override_redirect = True;
473 wa.background_pixmap = ParentRelative;
474 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
475 win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
476 DefaultDepth(dc->dpy, screen), CopyFromParent,
477 DefaultVisual(dc->dpy, screen),
478 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
481 setcanvas(dc, mw, mh);
482 inputw = MIN(inputw, mw/3);
483 promptw = prompt ? MIN(textw(dc, prompt), mw/5) : 0;
484 XMapRaised(dc->dpy, win);
491 fputs("usage: dmenu [-b] [-i] [-l lines] [-p prompt] [-fn font] [-nb color]\n"
492 " [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
497 main(int argc, char *argv[]) {
501 for(i = 1; i < argc; i++)
503 if(!strcmp(argv[i], "-v")) {
504 fputs("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n", stdout);
507 else if(!strcmp(argv[i], "-b"))
509 else if(!strcmp(argv[i], "-i"))
510 fstrncmp = strncasecmp;
514 else if(!strcmp(argv[i], "-l"))
515 lines = atoi(argv[++i]);
516 else if(!strcmp(argv[i], "-p"))
518 else if(!strcmp(argv[i], "-fn"))
520 else if(!strcmp(argv[i], "-nb"))
521 normbgcolor = argv[++i];
522 else if(!strcmp(argv[i], "-nf"))
523 normfgcolor = argv[++i];
524 else if(!strcmp(argv[i], "-sb"))
525 selbgcolor = argv[++i];
526 else if(!strcmp(argv[i], "-sf"))
527 selfgcolor = argv[++i];
537 return EXIT_FAILURE; /* should not reach */