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 appenditem(Item *item, Item **list, Item **last) {
71 (*last)->right = item;
84 n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">"));
86 for(i = 0, next = curr; next; next = next->right)
87 if((i += (lines > 0) ? bh : MIN(textw(dc, next->text), n)) > n)
89 for(i = 0, prev = curr; prev && prev->left; prev = prev->left)
90 if((i += (lines > 0) ? bh : MIN(textw(dc, prev->left->text), n)) > n)
102 drawrect(dc, 0, 0, mw, mh, True, BG(dc, normcol));
106 drawtext(dc, prompt, selcol);
109 dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw;
110 drawtext(dc, text, normcol);
111 if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w)
112 drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol));
116 for(item = curr; item != next; item = item->right) {
118 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
123 dc->w = textw(dc, "<");
125 drawtext(dc, "<", normcol);
126 for(item = curr; item != next; item = item->right) {
128 dc->w = MIN(textw(dc, item->text), mw - dc->x - textw(dc, ">"));
129 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
131 dc->w = textw(dc, ">");
134 drawtext(dc, ">", normcol);
136 mapdc(dc, win, mw, mh);
140 fstrstr(const char *s, const char *sub) {
143 for(len = strlen(sub); *s; s++)
144 if(!fstrncmp(s, sub, len))
153 for(i = 0; i < 1000; i++) {
154 if(!XGrabKeyboard(dc->dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime))
158 eprintf("cannot grab keyboard\n");
162 insert(const char *s, ssize_t n) {
163 if(strlen(text) + n > sizeof text - 1)
165 memmove(text + cursor + n, text + cursor, sizeof text - cursor - MAX(n, 0));
167 memcpy(text + cursor, s, n);
173 keypress(XKeyEvent *ev) {
179 XLookupString(ev, buf, sizeof buf, &ksym, NULL);
180 if(ev->state & ControlMask) {
181 switch(tolower(ksym)) {
211 case XK_k: /* delete right */
221 case XK_u: /* delete left */
222 insert(NULL, 0 - cursor);
224 case XK_w: /* delete word */
225 while(cursor > 0 && text[nextrune(-1)] == ' ')
226 insert(NULL, nextrune(-1) - cursor);
227 while(cursor > 0 && text[nextrune(-1)] != ' ')
228 insert(NULL, nextrune(-1) - cursor);
230 case XK_y: /* paste selection */
231 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
238 insert(buf, strlen(buf));
243 cursor = nextrune(+1);
246 insert(NULL, nextrune(-1) - cursor);
257 while(sel && sel->right)
267 sel = curr = matches;
271 if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
272 cursor = nextrune(-1);
278 if(sel && sel->left && (sel = sel->left)->right == curr) {
297 fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout);
302 cursor = nextrune(+1);
308 if(sel && sel->right && (sel = sel->right) == next) {
316 strncpy(text, sel->text, sizeof text);
317 cursor = strlen(text);
327 Item *item, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
330 matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
331 for(item = items; item; item = item->next)
332 if(!fstrncmp(text, item->text, len + 1))
333 appenditem(item, &lexact, &exactend);
334 else if(!fstrncmp(text, item->text, len))
335 appenditem(item, &lprefix, &prefixend);
336 else if(fstrstr(item->text, text))
337 appenditem(item, &lsubstr, &substrend);
345 itemend->right = lprefix;
346 lprefix->left = itemend;
354 itemend->right = lsubstr;
355 lsubstr->left = itemend;
360 curr = prev = next = sel = matches;
369 for(n = cursor + incr; n >= 0 && n < len && (text[n] & 0xc0) == 0x80; n += incr);
380 XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
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 item->next = item->left = item->right = NULL;
400 inputw = MAX(inputw, textw(dc, item->text));
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 lines = MAX(lines, 0);
449 mh = (lines + 1) * bh;
451 if((info = XineramaQueryScreens(dc->dpy, &n))) {
456 XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
457 for(i = 0; i < n; i++)
458 if((monitor == info[i].screen_number)
459 || (monitor < 0 && INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height)))
462 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
470 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
471 mw = DisplayWidth(dc->dpy, screen);
474 wa.override_redirect = True;
475 wa.background_pixmap = ParentRelative;
476 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
477 win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
478 DefaultDepth(dc->dpy, screen), CopyFromParent,
479 DefaultVisual(dc->dpy, screen),
480 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
483 resizedc(dc, mw, mh);
484 inputw = MIN(inputw, mw/3);
485 promptw = prompt ? textw(dc, prompt) : 0;
486 XMapRaised(dc->dpy, win);
493 fputs("usage: dmenu [-b] [-i] [-l lines] [-p prompt] [-fn font] [-nb color]\n"
494 " [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
499 main(int argc, char *argv[]) {
503 for(i = 1; i < argc; i++)
505 if(!strcmp(argv[i], "-v")) {
506 fputs("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n", stdout);
509 else if(!strcmp(argv[i], "-b"))
511 else if(!strcmp(argv[i], "-i"))
512 fstrncmp = strncasecmp;
516 else if(!strcmp(argv[i], "-l"))
517 lines = atoi(argv[++i]);
518 else if(!strcmp(argv[i], "-m"))
519 monitor = atoi(argv[++i]);
520 else if(!strcmp(argv[i], "-p"))
522 else if(!strcmp(argv[i], "-fn"))
524 else if(!strcmp(argv[i], "-nb"))
525 normbgcolor = argv[++i];
526 else if(!strcmp(argv[i], "-nf"))
527 normfgcolor = argv[++i];
528 else if(!strcmp(argv[i], "-sb"))
529 selbgcolor = argv[++i];
530 else if(!strcmp(argv[i], "-sf"))
531 selfgcolor = argv[++i];
541 return EXIT_FAILURE; /* should not reach */