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