dmenu for lunch applications in dwm
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

569 lines
13 KiB

13 years ago
18 years ago
16 years ago
18 years ago
13 years ago
18 years ago
14 years ago
18 years ago
18 years ago
18 years ago
13 years ago
14 years ago
13 years ago
16 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
13 years ago
17 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
17 years ago
14 years ago
18 years ago
18 years ago
13 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
17 years ago
13 years ago
13 years ago
13 years ago
14 years ago
13 years ago
13 years ago
18 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
18 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
18 years ago
13 years ago
18 years ago
18 years ago
18 years ago
13 years ago
14 years ago
13 years ago
13 years ago
18 years ago
13 years ago
18 years ago
18 years ago
17 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
17 years ago
17 years ago
13 years ago
18 years ago
13 years ago
13 years ago
13 years ago
13 years ago
18 years ago
13 years ago
13 years ago
18 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. /* See LICENSE file for copyright and license details. */
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <strings.h>
  7. #include <unistd.h>
  8. #include <X11/Xlib.h>
  9. #include <X11/Xatom.h>
  10. #include <X11/Xutil.h>
  11. #ifdef XINERAMA
  12. #include <X11/extensions/Xinerama.h>
  13. #endif
  14. #include "draw.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. typedef struct Item Item;
  19. struct Item {
  20. char *text;
  21. Item *left, *right;
  22. };
  23. static void appenditem(Item *item, Item **list, Item **last);
  24. static void calcoffsets(void);
  25. static char *cistrstr(const char *s, const char *sub);
  26. static void drawmenu(void);
  27. static void grabkeyboard(void);
  28. static void insert(const char *str, ssize_t n);
  29. static void keypress(XKeyEvent *ev);
  30. static void match(void);
  31. static size_t nextrune(int inc);
  32. static void paste(void);
  33. static void readstdin(void);
  34. static void run(void);
  35. static void setup(void);
  36. static void usage(void);
  37. static char text[BUFSIZ] = "";
  38. static int bh, mw, mh;
  39. static int inputw, promptw;
  40. static size_t cursor = 0;
  41. static const char *font = NULL;
  42. static const char *prompt = NULL;
  43. static const char *normbgcolor = "#cccccc";
  44. static const char *normfgcolor = "#000000";
  45. static const char *selbgcolor = "#0066ff";
  46. static const char *selfgcolor = "#ffffff";
  47. static unsigned int lines = 0;
  48. static unsigned long normcol[ColLast];
  49. static unsigned long selcol[ColLast];
  50. static Atom utf8;
  51. static Bool topbar = True;
  52. static DC *dc;
  53. static Item *items = NULL;
  54. static Item *matches, *matchend;
  55. static Item *prev, *curr, *next, *sel;
  56. static Window win;
  57. static XIC xic;
  58. static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
  59. static char *(*fstrstr)(const char *, const char *) = strstr;
  60. int
  61. main(int argc, char *argv[]) {
  62. Bool fast = False;
  63. int i;
  64. for(i = 1; i < argc; i++)
  65. /* single flags */
  66. if(!strcmp(argv[i], "-v")) {
  67. puts("dmenu-"VERSION", © 2006-2011 dmenu engineers, see LICENSE for details");
  68. exit(EXIT_SUCCESS);
  69. }
  70. else if(!strcmp(argv[i], "-b"))
  71. topbar = False;
  72. else if(!strcmp(argv[i], "-f"))
  73. fast = True;
  74. else if(!strcmp(argv[i], "-i")) {
  75. fstrncmp = strncasecmp;
  76. fstrstr = cistrstr;
  77. }
  78. else if(i+1 == argc)
  79. usage();
  80. /* double flags */
  81. else if(!strcmp(argv[i], "-l"))
  82. lines = atoi(argv[++i]);
  83. else if(!strcmp(argv[i], "-p"))
  84. prompt = argv[++i];
  85. else if(!strcmp(argv[i], "-fn"))
  86. font = argv[++i];
  87. else if(!strcmp(argv[i], "-nb"))
  88. normbgcolor = argv[++i];
  89. else if(!strcmp(argv[i], "-nf"))
  90. normfgcolor = argv[++i];
  91. else if(!strcmp(argv[i], "-sb"))
  92. selbgcolor = argv[++i];
  93. else if(!strcmp(argv[i], "-sf"))
  94. selfgcolor = argv[++i];
  95. else
  96. usage();
  97. dc = initdc();
  98. initfont(dc, font);
  99. if(fast) {
  100. grabkeyboard();
  101. readstdin();
  102. }
  103. else {
  104. readstdin();
  105. grabkeyboard();
  106. }
  107. setup();
  108. run();
  109. return EXIT_FAILURE; /* unreachable */
  110. }
  111. void
  112. appenditem(Item *item, Item **list, Item **last) {
  113. if(*last)
  114. (*last)->right = item;
  115. else
  116. *list = item;
  117. item->left = *last;
  118. item->right = NULL;
  119. *last = item;
  120. }
  121. void
  122. calcoffsets(void) {
  123. int i, n;
  124. if(lines > 0)
  125. n = lines * bh;
  126. else
  127. n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">"));
  128. for(i = 0, next = curr; next; next = next->right)
  129. if((i += (lines > 0) ? bh : MIN(textw(dc, next->text), n)) > n)
  130. break;
  131. for(i = 0, prev = curr; prev && prev->left; prev = prev->left)
  132. if((i += (lines > 0) ? bh : MIN(textw(dc, prev->left->text), n)) > n)
  133. break;
  134. }
  135. char *
  136. cistrstr(const char *s, const char *sub) {
  137. size_t len;
  138. for(len = strlen(sub); *s; s++)
  139. if(!strncasecmp(s, sub, len))
  140. return (char *)s;
  141. return NULL;
  142. }
  143. void
  144. drawmenu(void) {
  145. int curpos;
  146. Item *item;
  147. dc->x = 0;
  148. dc->y = 0;
  149. dc->h = bh;
  150. drawrect(dc, 0, 0, mw, mh, True, BG(dc, normcol));
  151. if(prompt) {
  152. dc->w = promptw;
  153. drawtext(dc, prompt, selcol);
  154. dc->x = dc->w;
  155. }
  156. dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw;
  157. drawtext(dc, text, normcol);
  158. if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w)
  159. drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol));
  160. if(lines > 0) {
  161. dc->w = mw - dc->x;
  162. for(item = curr; item != next; item = item->right) {
  163. dc->y += dc->h;
  164. drawtext(dc, item->text, (item == sel) ? selcol : normcol);
  165. }
  166. }
  167. else if(matches) {
  168. dc->x += inputw;
  169. dc->w = textw(dc, "<");
  170. if(curr->left)
  171. drawtext(dc, "<", normcol);
  172. for(item = curr; item != next; item = item->right) {
  173. dc->x += dc->w;
  174. dc->w = MIN(textw(dc, item->text), mw - dc->x - textw(dc, ">"));
  175. drawtext(dc, item->text, (item == sel) ? selcol : normcol);
  176. }
  177. dc->w = textw(dc, ">");
  178. dc->x = mw - dc->w;
  179. if(next)
  180. drawtext(dc, ">", normcol);
  181. }
  182. mapdc(dc, win, mw, mh);
  183. }
  184. void
  185. grabkeyboard(void) {
  186. int i;
  187. for(i = 0; i < 1000; i++) {
  188. if(XGrabKeyboard(dc->dpy, DefaultRootWindow(dc->dpy), True,
  189. GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
  190. return;
  191. usleep(1000);
  192. }
  193. eprintf("cannot grab keyboard\n");
  194. }
  195. void
  196. insert(const char *str, ssize_t n) {
  197. if(strlen(text) + n > sizeof text - 1)
  198. return;
  199. memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0));
  200. if(n > 0)
  201. memcpy(&text[cursor], str, n);
  202. cursor += n;
  203. match();
  204. }
  205. void
  206. keypress(XKeyEvent *ev) {
  207. char buf[32];
  208. int len;
  209. KeySym ksym = NoSymbol;
  210. Status status;
  211. len = XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status);
  212. if(status == XBufferOverflow)
  213. return;
  214. if(ev->state & ControlMask) {
  215. KeySym lower, upper;
  216. XConvertCase(ksym, &lower, &upper);
  217. switch(lower) {
  218. case XK_a: ksym = XK_Home; break;
  219. case XK_b: ksym = XK_Left; break;
  220. case XK_c: ksym = XK_Escape; break;
  221. case XK_d: ksym = XK_Delete; break;
  222. case XK_e: ksym = XK_End; break;
  223. case XK_f: ksym = XK_Right; break;
  224. case XK_h: ksym = XK_BackSpace; break;
  225. case XK_i: ksym = XK_Tab; break;
  226. case XK_j: ksym = XK_Return; break;
  227. case XK_m: ksym = XK_Return; break;
  228. case XK_n: ksym = XK_Up; break;
  229. case XK_p: ksym = XK_Down; break;
  230. case XK_k: /* delete right */
  231. text[cursor] = '\0';
  232. match();
  233. break;
  234. case XK_u: /* delete left */
  235. insert(NULL, 0 - cursor);
  236. break;
  237. case XK_w: /* delete word */
  238. while(cursor > 0 && text[nextrune(-1)] == ' ')
  239. insert(NULL, nextrune(-1) - cursor);
  240. while(cursor > 0 && text[nextrune(-1)] != ' ')
  241. insert(NULL, nextrune(-1) - cursor);
  242. break;
  243. case XK_y: /* paste selection */
  244. XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
  245. return;
  246. default:
  247. return;
  248. }
  249. }
  250. switch(ksym) {
  251. default:
  252. if(!iscntrl(*buf))
  253. insert(buf, len);
  254. break;
  255. case XK_Delete:
  256. if(text[cursor] == '\0')
  257. return;
  258. cursor = nextrune(+1);
  259. /* fallthrough */
  260. case XK_BackSpace:
  261. if(cursor == 0)
  262. return;
  263. insert(NULL, nextrune(-1) - cursor);
  264. break;
  265. case XK_End:
  266. if(text[cursor] != '\0') {
  267. cursor = strlen(text);
  268. break;
  269. }
  270. if(next) {
  271. curr = matchend;
  272. calcoffsets();
  273. curr = prev;
  274. calcoffsets();
  275. while(next && (curr = curr->right))
  276. calcoffsets();
  277. }
  278. sel = matchend;
  279. break;
  280. case XK_Escape:
  281. exit(EXIT_FAILURE);
  282. case XK_Home:
  283. if(sel == matches) {
  284. cursor = 0;
  285. break;
  286. }
  287. sel = curr = matches;
  288. calcoffsets();
  289. break;
  290. case XK_Left:
  291. if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
  292. cursor = nextrune(-1);
  293. break;
  294. }
  295. /* fallthrough */
  296. case XK_Up:
  297. if(sel && sel->left && (sel = sel->left)->right == curr) {
  298. curr = prev;
  299. calcoffsets();
  300. }
  301. break;
  302. case XK_Next:
  303. if(!next)
  304. return;
  305. sel = curr = next;
  306. calcoffsets();
  307. break;
  308. case XK_Prior:
  309. if(!prev)
  310. return;
  311. sel = curr = prev;
  312. calcoffsets();
  313. break;
  314. case XK_Return:
  315. case XK_KP_Enter:
  316. puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
  317. exit(EXIT_SUCCESS);
  318. case XK_Right:
  319. if(text[cursor] != '\0') {
  320. cursor = nextrune(+1);
  321. break;
  322. }
  323. /* fallthrough */
  324. case XK_Down:
  325. if(sel && sel->right && (sel = sel->right) == next) {
  326. curr = next;
  327. calcoffsets();
  328. }
  329. break;
  330. case XK_Tab:
  331. if(!sel)
  332. return;
  333. strncpy(text, sel->text, sizeof text);
  334. cursor = strlen(text);
  335. match();
  336. break;
  337. }
  338. drawmenu();
  339. }
  340. void
  341. match(void) {
  342. static char **tokv = NULL;
  343. static int tokn = 0;
  344. char buf[sizeof text], *s;
  345. int i, tokc = 0;
  346. size_t len;
  347. Item *item, *lprefix, *lsubstr, *prefixend, *substrend;
  348. strcpy(buf, text);
  349. for(s = strtok(buf, " "); s; tokv[tokc-1] = s, s = strtok(NULL, " "))
  350. if(++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv)))
  351. eprintf("cannot realloc %u bytes\n", tokn * sizeof *tokv);
  352. len = tokc ? strlen(tokv[0]) : 0;
  353. matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
  354. for(item = items; item && item->text; item++) {
  355. for(i = 0; i < tokc; i++)
  356. if(!fstrstr(item->text, tokv[i]))
  357. break;
  358. if(i != tokc)
  359. continue;
  360. if(!tokc || !fstrncmp(tokv[0], item->text, len+1))
  361. appenditem(item, &matches, &matchend);
  362. else if(!fstrncmp(tokv[0], item->text, len))
  363. appenditem(item, &lprefix, &prefixend);
  364. else
  365. appenditem(item, &lsubstr, &substrend);
  366. }
  367. if(lprefix) {
  368. if(matches) {
  369. matchend->right = lprefix;
  370. lprefix->left = matchend;
  371. }
  372. else
  373. matches = lprefix;
  374. matchend = prefixend;
  375. }
  376. if(lsubstr) {
  377. if(matches) {
  378. matchend->right = lsubstr;
  379. lsubstr->left = matchend;
  380. }
  381. else
  382. matches = lsubstr;
  383. matchend = substrend;
  384. }
  385. curr = sel = matches;
  386. calcoffsets();
  387. }
  388. size_t
  389. nextrune(int inc) {
  390. ssize_t n;
  391. for(n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc);
  392. return n;
  393. }
  394. void
  395. paste(void) {
  396. char *p, *q;
  397. int di;
  398. unsigned long dl;
  399. Atom da;
  400. XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
  401. utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
  402. insert(p, (q = strchr(p, '\n')) ? q-p : (ssize_t)strlen(p));
  403. XFree(p);
  404. drawmenu();
  405. }
  406. void
  407. readstdin(void) {
  408. char buf[sizeof text], *p, *maxstr = NULL;
  409. size_t i, max = 0, size = 0;
  410. for(i = 0; fgets(buf, sizeof buf, stdin); i++) {
  411. if(i+1 >= size / sizeof *items)
  412. if(!(items = realloc(items, (size += BUFSIZ))))
  413. eprintf("cannot realloc %u bytes:", size);
  414. if((p = strchr(buf, '\n')))
  415. *p = '\0';
  416. if(!(items[i].text = strdup(buf)))
  417. eprintf("cannot strdup %u bytes:", strlen(buf)+1);
  418. if(strlen(items[i].text) > max)
  419. max = strlen(maxstr = items[i].text);
  420. }
  421. if(items)
  422. items[i].text = NULL;
  423. inputw = maxstr ? textw(dc, maxstr) : 0;
  424. lines = MIN(lines, i);
  425. }
  426. void
  427. run(void) {
  428. XEvent ev;
  429. while(!XNextEvent(dc->dpy, &ev)) {
  430. if(XFilterEvent(&ev, win))
  431. continue;
  432. switch(ev.type) {
  433. case Expose:
  434. if(ev.xexpose.count == 0)
  435. mapdc(dc, win, mw, mh);
  436. break;
  437. case KeyPress:
  438. keypress(&ev.xkey);
  439. break;
  440. case SelectionNotify:
  441. if(ev.xselection.property == utf8)
  442. paste();
  443. break;
  444. case VisibilityNotify:
  445. if(ev.xvisibility.state != VisibilityUnobscured)
  446. XRaiseWindow(dc->dpy, win);
  447. break;
  448. }
  449. }
  450. }
  451. void
  452. setup(void) {
  453. int x, y, screen = DefaultScreen(dc->dpy);
  454. Window root = RootWindow(dc->dpy, screen);
  455. XSetWindowAttributes swa;
  456. XIM xim;
  457. #ifdef XINERAMA
  458. int n;
  459. XineramaScreenInfo *info;
  460. #endif
  461. normcol[ColBG] = getcolor(dc, normbgcolor);
  462. normcol[ColFG] = getcolor(dc, normfgcolor);
  463. selcol[ColBG] = getcolor(dc, selbgcolor);
  464. selcol[ColFG] = getcolor(dc, selfgcolor);
  465. utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
  466. /* menu geometry */
  467. bh = dc->font.height + 2;
  468. lines = MAX(lines, 0);
  469. mh = (lines + 1) * bh;
  470. #ifdef XINERAMA
  471. if((info = XineramaQueryScreens(dc->dpy, &n))) {
  472. int i, di;
  473. unsigned int du;
  474. Window w, dw;
  475. XWindowAttributes wa;
  476. XGetInputFocus(dc->dpy, &w, &di);
  477. if(w != root && w != PointerRoot && w != None && XGetWindowAttributes(dc->dpy, w, &wa))
  478. XTranslateCoordinates(dc->dpy, w, root, wa.x, wa.y, &x, &y, &dw);
  479. else
  480. XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
  481. for(i = 0; i < n-1; i++)
  482. if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
  483. break;
  484. x = info[i].x_org;
  485. y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
  486. mw = info[i].width;
  487. XFree(info);
  488. }
  489. else
  490. #endif
  491. {
  492. x = 0;
  493. y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
  494. mw = DisplayWidth(dc->dpy, screen);
  495. }
  496. promptw = prompt ? textw(dc, prompt) : 0;
  497. inputw = MIN(inputw, mw/3);
  498. match();
  499. /* menu window */
  500. swa.override_redirect = True;
  501. swa.background_pixmap = ParentRelative;
  502. swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
  503. win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
  504. DefaultDepth(dc->dpy, screen), CopyFromParent,
  505. DefaultVisual(dc->dpy, screen),
  506. CWOverrideRedirect | CWBackPixmap | CWEventMask, &swa);
  507. /* input methods */
  508. xim = XOpenIM(dc->dpy, NULL, NULL, NULL);
  509. xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
  510. XNClientWindow, win, XNFocusWindow, win, NULL);
  511. XMapRaised(dc->dpy, win);
  512. resizedc(dc, mw, mh);
  513. drawmenu();
  514. }
  515. void
  516. usage(void) {
  517. fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font]\n"
  518. " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
  519. exit(EXIT_FAILURE);
  520. }