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.

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