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.

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