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);
235 insert(buf, strlen(buf));
240 cursor = nextrune(+1);
243 insert(NULL, nextrune(-1) - cursor);
254 while(sel && sel->right)
264 sel = curr = matches;
267 case XK_Insert: /* paste selection */
268 if(ev->state & ShiftMask)
269 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
272 if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
273 cursor = nextrune(-1);
279 if(sel && sel->left && (sel = sel->left)->right == curr) {
298 fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout);
303 cursor = nextrune(+1);
309 if(sel && sel->right && (sel = sel->right) == next) {
317 strncpy(text, sel->text, sizeof text);
318 cursor = strlen(text);
328 Item *item, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
331 matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
332 for(item = items; item; item = item->next)
333 if(!fstrncmp(text, item->text, len + 1))
334 appenditem(item, &lexact, &exactend);
335 else if(!fstrncmp(text, item->text, len))
336 appenditem(item, &lprefix, &prefixend);
337 else if(fstrstr(item->text, text))
338 appenditem(item, &lsubstr, &substrend);
346 itemend->right = lprefix;
347 lprefix->left = itemend;
355 itemend->right = lsubstr;
356 lsubstr->left = itemend;
361 curr = prev = next = sel = matches;
370 for(n = cursor + incr; n >= 0 && n < len && (text[n] & 0xc0) == 0x80; n += incr);
381 XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
382 utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
383 insert(p, (q = strchr(p, '\n')) ? q-p : strlen(p));
390 char buf[sizeof text], *p;
393 for(end = &items; fgets(buf, sizeof buf, stdin); *end = item, end = &item->next) {
394 if((p = strchr(buf, '\n')))
396 if(!(item = malloc(sizeof *item)))
397 eprintf("cannot malloc %u bytes\n", sizeof *item);
398 if(!(item->text = strdup(buf)))
399 eprintf("cannot strdup %u bytes\n", strlen(buf)+1);
400 item->next = item->left = item->right = NULL;
401 inputw = MAX(inputw, textw(dc, item->text));
409 while(!XNextEvent(dc->dpy, &ev))
412 if(ev.xexpose.count == 0)
418 case SelectionNotify:
419 if(ev.xselection.property == utf8)
422 case VisibilityNotify:
423 if(ev.xvisibility.state != VisibilityUnobscured)
424 XRaiseWindow(dc->dpy, win);
432 XSetWindowAttributes wa;
435 XineramaScreenInfo *info;
438 screen = DefaultScreen(dc->dpy);
439 root = RootWindow(dc->dpy, screen);
440 utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
442 normcol[ColBG] = getcolor(dc, normbgcolor);
443 normcol[ColFG] = getcolor(dc, normfgcolor);
444 selcol[ColBG] = getcolor(dc, selbgcolor);
445 selcol[ColFG] = getcolor(dc, selfgcolor);
448 bh = dc->font.height + 2;
449 lines = MAX(lines, 0);
450 mh = (lines + 1) * bh;
452 if((info = XineramaQueryScreens(dc->dpy, &n))) {
457 XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
458 for(i = 0; i < n; i++)
459 if((monitor == info[i].screen_number)
460 || (monitor < 0 && INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height)))
463 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
471 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
472 mw = DisplayWidth(dc->dpy, screen);
475 wa.override_redirect = True;
476 wa.background_pixmap = ParentRelative;
477 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
478 win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
479 DefaultDepth(dc->dpy, screen), CopyFromParent,
480 DefaultVisual(dc->dpy, screen),
481 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
484 resizedc(dc, mw, mh);
485 inputw = MIN(inputw, mw/3);
486 promptw = prompt ? textw(dc, prompt) : 0;
487 XMapRaised(dc->dpy, win);
494 fputs("usage: dmenu [-b] [-i] [-l lines] [-p prompt] [-fn font] [-nb color]\n"
495 " [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
500 main(int argc, char *argv[]) {
504 for(i = 1; i < argc; i++)
506 if(!strcmp(argv[i], "-v")) {
507 fputs("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n", stdout);
510 else if(!strcmp(argv[i], "-b"))
512 else if(!strcmp(argv[i], "-i"))
513 fstrncmp = strncasecmp;
517 else if(!strcmp(argv[i], "-l"))
518 lines = atoi(argv[++i]);
519 else if(!strcmp(argv[i], "-m"))
520 monitor = atoi(argv[++i]);
521 else if(!strcmp(argv[i], "-p"))
523 else if(!strcmp(argv[i], "-fn"))
525 else if(!strcmp(argv[i], "-nb"))
526 normbgcolor = argv[++i];
527 else if(!strcmp(argv[i], "-nf"))
528 normfgcolor = argv[++i];
529 else if(!strcmp(argv[i], "-sb"))
530 selbgcolor = argv[++i];
531 else if(!strcmp(argv[i], "-sf"))
532 selfgcolor = argv[++i];
542 return EXIT_FAILURE; /* should not reach */