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.

555 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
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
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
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
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
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(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 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. (*last)->right = item;
  114. else
  115. *list = 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();
  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();
  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();
  331. break;
  332. }
  333. drawmenu();
  334. }
  335. void
  336. match(void) {
  337. static char **tokv = NULL;
  338. static int tokn = 0;
  339. char buf[sizeof text], *s;
  340. int i, tokc = 0;
  341. size_t len;
  342. Item *item, *lprefix, *lsubstr, *prefixend, *substrend;
  343. strcpy(buf, text);
  344. for(s = strtok(buf, " "); s; tokv[tokc-1] = s, s = strtok(NULL, " "))
  345. if(++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv)))
  346. eprintf("cannot realloc %u bytes\n", tokn * sizeof *tokv);
  347. len = tokc ? strlen(tokv[0]) : 0;
  348. matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
  349. for(item = items; item && item->text; item++) {
  350. for(i = 0; i < tokc; i++)
  351. if(!fstrstr(item->text, tokv[i]))
  352. break;
  353. if(i != tokc)
  354. continue;
  355. if(!tokc || !fstrncmp(tokv[0], item->text, len+1))
  356. appenditem(item, &matches, &matchend);
  357. else if(!fstrncmp(tokv[0], item->text, len))
  358. appenditem(item, &lprefix, &prefixend);
  359. else
  360. appenditem(item, &lsubstr, &substrend);
  361. }
  362. if(lprefix) {
  363. if(matches) {
  364. matchend->right = lprefix;
  365. lprefix->left = matchend;
  366. }
  367. else
  368. matches = lprefix;
  369. matchend = prefixend;
  370. }
  371. if(lsubstr) {
  372. if(matches) {
  373. matchend->right = lsubstr;
  374. lsubstr->left = matchend;
  375. }
  376. else
  377. matches = lsubstr;
  378. matchend = substrend;
  379. }
  380. curr = sel = matches;
  381. calcoffsets();
  382. }
  383. size_t
  384. nextrune(int inc) {
  385. ssize_t n;
  386. for(n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc);
  387. return n;
  388. }
  389. void
  390. paste(void) {
  391. char *p, *q;
  392. int di;
  393. unsigned long dl;
  394. Atom da;
  395. XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
  396. utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
  397. insert(p, (q = strchr(p, '\n')) ? q-p : (ssize_t)strlen(p));
  398. XFree(p);
  399. drawmenu();
  400. }
  401. void
  402. readstdin(void) {
  403. char buf[sizeof text], *p, *maxstr = NULL;
  404. size_t i, max = 0, size = 0;
  405. for(i = 0; fgets(buf, sizeof buf, stdin); i++) {
  406. if(i+1 >= size / sizeof *items)
  407. if(!(items = realloc(items, (size += BUFSIZ))))
  408. eprintf("cannot realloc %u bytes:", size);
  409. if((p = strchr(buf, '\n')))
  410. *p = '\0';
  411. if(!(items[i].text = strdup(buf)))
  412. eprintf("cannot strdup %u bytes:", strlen(buf)+1);
  413. if(strlen(items[i].text) > max)
  414. max = strlen(maxstr = items[i].text);
  415. }
  416. if(items)
  417. items[i].text = NULL;
  418. inputw = maxstr ? textw(dc, maxstr) : 0;
  419. lines = MIN(lines, i);
  420. }
  421. void
  422. run(void) {
  423. XEvent ev;
  424. while(!XNextEvent(dc->dpy, &ev))
  425. switch(ev.type) {
  426. case Expose:
  427. if(ev.xexpose.count == 0)
  428. mapdc(dc, win, mw, mh);
  429. break;
  430. case KeyPress:
  431. keypress(&ev.xkey);
  432. break;
  433. case SelectionNotify:
  434. if(ev.xselection.property == utf8)
  435. paste();
  436. break;
  437. case VisibilityNotify:
  438. if(ev.xvisibility.state != VisibilityUnobscured)
  439. XRaiseWindow(dc->dpy, win);
  440. break;
  441. }
  442. }
  443. void
  444. setup(void) {
  445. int x, y, screen = DefaultScreen(dc->dpy);
  446. Window root = RootWindow(dc->dpy, screen);
  447. XSetWindowAttributes swa;
  448. #ifdef XINERAMA
  449. int n;
  450. XineramaScreenInfo *info;
  451. #endif
  452. normcol[ColBG] = getcolor(dc, normbgcolor);
  453. normcol[ColFG] = getcolor(dc, normfgcolor);
  454. selcol[ColBG] = getcolor(dc, selbgcolor);
  455. selcol[ColFG] = getcolor(dc, selfgcolor);
  456. utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
  457. /* menu geometry */
  458. bh = dc->font.height + 2;
  459. lines = MAX(lines, 0);
  460. mh = (lines + 1) * bh;
  461. #ifdef XINERAMA
  462. if((info = XineramaQueryScreens(dc->dpy, &n))) {
  463. int i, di;
  464. unsigned int du;
  465. Window w, dw;
  466. XWindowAttributes wa;
  467. XGetInputFocus(dc->dpy, &w, &di);
  468. if(w != root && w != PointerRoot && w != None && XGetWindowAttributes(dc->dpy, w, &wa))
  469. XTranslateCoordinates(dc->dpy, w, root, wa.x, wa.y, &x, &y, &dw);
  470. else
  471. XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
  472. for(i = 0; i < n-1; i++)
  473. if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
  474. break;
  475. x = info[i].x_org;
  476. y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
  477. mw = info[i].width;
  478. XFree(info);
  479. }
  480. else
  481. #endif
  482. {
  483. x = 0;
  484. y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
  485. mw = DisplayWidth(dc->dpy, screen);
  486. }
  487. promptw = prompt ? textw(dc, prompt) : 0;
  488. inputw = MIN(inputw, mw/3);
  489. match();
  490. /* menu window */
  491. swa.override_redirect = True;
  492. swa.background_pixmap = ParentRelative;
  493. swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
  494. win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
  495. DefaultDepth(dc->dpy, screen), CopyFromParent,
  496. DefaultVisual(dc->dpy, screen),
  497. CWOverrideRedirect | CWBackPixmap | CWEventMask, &swa);
  498. XMapRaised(dc->dpy, win);
  499. resizedc(dc, mw, mh);
  500. drawmenu();
  501. }
  502. void
  503. usage(void) {
  504. fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font]\n"
  505. " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
  506. exit(EXIT_FAILURE);
  507. }