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.

600 lines
13 KiB

18 years ago
18 years ago
16 years ago
18 years ago
16 years ago
18 years ago
18 years ago
18 years ago
16 years ago
16 years ago
14 years ago
14 years ago
16 years ago
17 years ago
17 years ago
18 years ago
14 years ago
14 years ago
18 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
17 years ago
18 years ago
18 years ago
14 years ago
18 years ago
18 years ago
18 years ago
14 years ago
18 years ago
14 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
14 years ago
18 years ago
18 years ago
18 years ago
17 years ago
17 years ago
17 years ago
17 years ago
18 years ago
18 years ago
18 years ago
18 years ago
17 years ago
17 years ago
17 years ago
17 years ago
18 years ago
17 years ago
18 years ago
  1. /* See LICENSE file for copyright and license details. */
  2. #include <ctype.h>
  3. #include <locale.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <X11/keysym.h>
  9. #include <X11/Xatom.h>
  10. #include <X11/Xlib.h>
  11. #include <X11/Xutil.h>
  12. #ifdef XINERAMA
  13. #include <X11/extensions/Xinerama.h>
  14. #endif
  15. #include <draw.h>
  16. #include "config.h"
  17. #define INRECT(x,y,rx,ry,rw,rh) ((x) >= (rx) && (x) < (rx)+(rw) && (y) >= (ry) && (y) < (ry)+(rh))
  18. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  19. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  20. #define UTF8_CODEPOINT(c) (((c) & 0xc0) != 0x80)
  21. typedef struct Item Item;
  22. struct Item {
  23. char *text;
  24. Item *next; /* traverses all items */
  25. Item *left, *right; /* traverses matching items */
  26. };
  27. static void appenditem(Item *item, Item **list, Item **last);
  28. static void calcoffsetsh(void);
  29. static void calcoffsetsv(void);
  30. static char *cistrstr(const char *s, const char *sub);
  31. static void drawmenu(void);
  32. static void drawmenuh(void);
  33. static void drawmenuv(void);
  34. static void grabkeyboard(void);
  35. static void insert(const char *s, ssize_t n);
  36. static void keypress(XKeyEvent *e);
  37. static void match(void);
  38. static void paste(Atom atom);
  39. static void readstdin(void);
  40. static void run(void);
  41. static void setup(void);
  42. static void usage(void);
  43. static char *prompt;
  44. static char text[4096];
  45. static int screen;
  46. static size_t cursor = 0;
  47. static unsigned int inputw = 0;
  48. static unsigned int lines = 0;
  49. static unsigned int mw, mh;
  50. static unsigned int promptw = 0;
  51. static unsigned long normcol[ColLast];
  52. static unsigned long selcol[ColLast];
  53. static Atom utf8;
  54. static Bool topbar = True;
  55. static DC dc;
  56. static Item *allitems, *matches;
  57. static Item *curr, *prev, *next, *sel;
  58. static Window root, win;
  59. static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
  60. static char *(*fstrstr)(const char *, const char *) = strstr;
  61. static void (*calcoffsets)(void) = calcoffsetsh;
  62. void
  63. appenditem(Item *item, Item **list, Item **last) {
  64. if(!(*last))
  65. *list = item;
  66. else
  67. (*last)->right = item;
  68. item->left = *last;
  69. item->right = NULL;
  70. *last = item;
  71. }
  72. void
  73. calcoffsetsh(void) {
  74. unsigned int w, x;
  75. w = promptw + inputw + textw(&dc, "<") + textw(&dc, ">");
  76. for(x = w, next = curr; next; next = next->right)
  77. if((x += MIN(textw(&dc, next->text), mw / 3)) > mw)
  78. break;
  79. for(x = w, prev = curr; prev && prev->left; prev = prev->left)
  80. if((x += MIN(textw(&dc, prev->left->text), mw / 3)) > mw)
  81. break;
  82. }
  83. void
  84. calcoffsetsv(void) {
  85. unsigned int i;
  86. next = prev = curr;
  87. for(i = 0; i < lines && next; i++)
  88. next = next->right;
  89. mh = (dc.font.height + 2) * (i + 1);
  90. for(i = 0; i < lines && prev && prev->left; i++)
  91. prev = prev->left;
  92. }
  93. char *
  94. cistrstr(const char *s, const char *sub) {
  95. int c, csub;
  96. unsigned int len;
  97. if(!sub)
  98. return (char *)s;
  99. if((c = tolower(*sub++)) != '\0') {
  100. len = strlen(sub);
  101. do {
  102. do {
  103. if((csub = *s++) == '\0')
  104. return NULL;
  105. }
  106. while(tolower(csub) != c);
  107. }
  108. while(strncasecmp(s, sub, len) != 0);
  109. s--;
  110. }
  111. return (char *)s;
  112. }
  113. void
  114. drawmenu(void) {
  115. dc.x = 0;
  116. dc.y = 0;
  117. dc.w = mw;
  118. dc.h = mh;
  119. drawbox(&dc, normcol);
  120. dc.h = dc.font.height + 2;
  121. dc.y = topbar ? 0 : mh - dc.h;
  122. /* print prompt? */
  123. if(prompt) {
  124. dc.w = promptw;
  125. drawbox(&dc, selcol);
  126. drawtext(&dc, prompt, selcol);
  127. dc.x += dc.w;
  128. }
  129. dc.w = mw - dc.x;
  130. /* print input area */
  131. if(matches && lines == 0 && textw(&dc, text) <= inputw)
  132. dc.w = inputw;
  133. drawtext(&dc, text, normcol);
  134. drawline(&dc, textnw(&dc, text, cursor) + dc.h/2 - 2, 2, 1, dc.h-4, normcol);
  135. if(lines > 0)
  136. drawmenuv();
  137. else if(curr && (dc.w == inputw || curr->next))
  138. drawmenuh();
  139. commitdraw(&dc, win);
  140. }
  141. void
  142. drawmenuh(void) {
  143. Item *item;
  144. dc.x += inputw;
  145. dc.w = textw(&dc, "<");
  146. if(curr->left)
  147. drawtext(&dc, "<", normcol);
  148. dc.x += dc.w;
  149. for(item = curr; item != next; item = item->right) {
  150. dc.w = MIN(textw(&dc, item->text), mw / 3);
  151. if(item == sel)
  152. drawbox(&dc, selcol);
  153. drawtext(&dc, item->text, (item == sel) ? selcol : normcol);
  154. dc.x += dc.w;
  155. }
  156. dc.w = textw(&dc, ">");
  157. dc.x = mw - dc.w;
  158. if(next)
  159. drawtext(&dc, ">", normcol);
  160. }
  161. void
  162. drawmenuv(void) {
  163. Item *item;
  164. XWindowAttributes wa;
  165. dc.y = topbar ? dc.h : 0;
  166. dc.w = mw - dc.x;
  167. for(item = curr; item != next; item = item->right) {
  168. if(item == sel)
  169. drawbox(&dc, selcol);
  170. drawtext(&dc, item->text, (item == sel) ? selcol : normcol);
  171. dc.y += dc.h;
  172. }
  173. if(!XGetWindowAttributes(dc.dpy, win, &wa))
  174. eprintf("cannot get window attributes\n");
  175. if(wa.height != mh)
  176. XMoveResizeWindow(dc.dpy, win, wa.x, wa.y + (topbar ? 0 : wa.height - mh), mw, mh);
  177. }
  178. void
  179. grabkeyboard(void) {
  180. int i;
  181. for(i = 0; i < 1000; i++) {
  182. if(!XGrabKeyboard(dc.dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime))
  183. return;
  184. usleep(1000);
  185. }
  186. eprintf("cannot grab keyboard\n");
  187. }
  188. void
  189. insert(const char *s, ssize_t n) {
  190. memmove(text + cursor + n, text + cursor, sizeof text - cursor - n);
  191. if(n > 0)
  192. memcpy(text + cursor, s, n);
  193. cursor += n;
  194. match();
  195. }
  196. void
  197. keypress(XKeyEvent *e) {
  198. char buf[sizeof text];
  199. int n;
  200. size_t len;
  201. KeySym ksym;
  202. len = strlen(text);
  203. XLookupString(e, buf, sizeof buf, &ksym, NULL);
  204. if(e->state & ControlMask) {
  205. switch(tolower(ksym)) {
  206. default:
  207. return;
  208. case XK_a:
  209. ksym = XK_Home;
  210. break;
  211. case XK_b:
  212. ksym = XK_Left;
  213. break;
  214. case XK_c:
  215. ksym = XK_Escape;
  216. break;
  217. case XK_e:
  218. ksym = XK_End;
  219. break;
  220. case XK_f:
  221. ksym = XK_Right;
  222. break;
  223. case XK_h:
  224. ksym = XK_BackSpace;
  225. break;
  226. case XK_i:
  227. ksym = XK_Tab;
  228. break;
  229. case XK_j:
  230. case XK_m:
  231. ksym = XK_Return;
  232. break;
  233. case XK_k: /* delete right */
  234. text[cursor] = '\0';
  235. break;
  236. case XK_n:
  237. ksym = XK_Down;
  238. break;
  239. case XK_p:
  240. ksym = XK_Up;
  241. break;
  242. case XK_u: /* delete left */
  243. insert(NULL, -cursor);
  244. break;
  245. case XK_w: /* delete word */
  246. if(cursor == 0)
  247. return;
  248. n = 0;
  249. while(cursor - n++ > 0 && text[cursor - n] == ' ');
  250. while(cursor - n++ > 0 && text[cursor - n] != ' ');
  251. insert(NULL, -(--n));
  252. break;
  253. case XK_y: /* paste selection */
  254. XConvertSelection(dc.dpy, XA_PRIMARY, utf8, None, win, CurrentTime);
  255. /* causes SelectionNotify event */
  256. return;
  257. }
  258. }
  259. switch(ksym) {
  260. default:
  261. if(!iscntrl((int)*buf))
  262. insert(buf, MIN(strlen(buf), sizeof text - cursor));
  263. break;
  264. case XK_BackSpace:
  265. if(cursor == 0)
  266. return;
  267. for(n = 1; cursor - n > 0 && !UTF8_CODEPOINT(text[cursor - n]); n++);
  268. insert(NULL, -n);
  269. break;
  270. case XK_Delete:
  271. if(cursor == len)
  272. return;
  273. for(n = 1; cursor + n < len && !UTF8_CODEPOINT(text[cursor + n]); n++);
  274. cursor += n;
  275. insert(NULL, -n);
  276. break;
  277. case XK_End:
  278. if(cursor < len) {
  279. cursor = len;
  280. break;
  281. }
  282. while(next) {
  283. sel = curr = next;
  284. calcoffsets();
  285. }
  286. while(sel && sel->right)
  287. sel = sel->right;
  288. break;
  289. case XK_Escape:
  290. exit(EXIT_FAILURE);
  291. case XK_Home:
  292. if(sel == matches) {
  293. cursor = 0;
  294. break;
  295. }
  296. sel = curr = matches;
  297. calcoffsets();
  298. break;
  299. case XK_Left:
  300. if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
  301. while(cursor-- > 0 && !UTF8_CODEPOINT(text[cursor]));
  302. break;
  303. }
  304. else if(lines > 0)
  305. return;
  306. case XK_Up:
  307. if(!sel || !sel->left)
  308. return;
  309. sel = sel->left;
  310. if(sel->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(((e->state & ShiftMask) || sel) ? sel->text : text, stdout);
  330. fflush(stdout);
  331. exit(EXIT_SUCCESS);
  332. case XK_Right:
  333. if(cursor < len) {
  334. while(cursor++ < len && !UTF8_CODEPOINT(text[cursor]));
  335. break;
  336. }
  337. else if(lines > 0)
  338. return;
  339. case XK_Down:
  340. if(!sel || !sel->right)
  341. return;
  342. sel = sel->right;
  343. if(sel == next) {
  344. curr = next;
  345. calcoffsets();
  346. }
  347. break;
  348. case XK_Tab:
  349. if(!sel)
  350. return;
  351. strncpy(text, sel->text, sizeof text);
  352. cursor = strlen(text);
  353. match();
  354. break;
  355. }
  356. drawmenu();
  357. }
  358. void
  359. match(void) {
  360. unsigned int len;
  361. Item *item, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
  362. len = strlen(text);
  363. matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
  364. for(item = allitems; item; item = item->next)
  365. if(!fstrncmp(text, item->text, len + 1))
  366. appenditem(item, &lexact, &exactend);
  367. else if(!fstrncmp(text, item->text, len))
  368. appenditem(item, &lprefix, &prefixend);
  369. else if(fstrstr(item->text, text))
  370. appenditem(item, &lsubstr, &substrend);
  371. if(lexact) {
  372. matches = lexact;
  373. itemend = exactend;
  374. }
  375. if(lprefix) {
  376. if(itemend) {
  377. itemend->right = lprefix;
  378. lprefix->left = itemend;
  379. }
  380. else
  381. matches = lprefix;
  382. itemend = prefixend;
  383. }
  384. if(lsubstr) {
  385. if(itemend) {
  386. itemend->right = lsubstr;
  387. lsubstr->left = itemend;
  388. }
  389. else
  390. matches = lsubstr;
  391. }
  392. curr = prev = next = sel = matches;
  393. calcoffsets();
  394. }
  395. void
  396. paste(Atom atom)
  397. {
  398. char *p, *q;
  399. int di;
  400. unsigned long dl;
  401. Atom da;
  402. XGetWindowProperty(dc.dpy, win, atom, 0, sizeof text - cursor, True,
  403. utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
  404. insert(p, (q = strchr(p, '\n')) ? q-p : strlen(p));
  405. XFree(p);
  406. drawmenu();
  407. }
  408. void
  409. readstdin(void) {
  410. char buf[sizeof text];
  411. size_t len;
  412. Item *item, *new;
  413. allitems = NULL;
  414. for(item = NULL; fgets(buf, sizeof buf, stdin); item = new) {
  415. len = strlen(buf);
  416. if(buf[len-1] == '\n')
  417. buf[--len] = '\0';
  418. if(!(new = malloc(sizeof *new)))
  419. eprintf("cannot malloc %u bytes\n", sizeof *new);
  420. if(!(new->text = strdup(buf)))
  421. eprintf("cannot strdup %u bytes\n", len);
  422. inputw = MAX(inputw, textw(&dc, new->text));
  423. new->next = new->left = new->right = NULL;
  424. if(item)
  425. item->next = new;
  426. else
  427. allitems = new;
  428. }
  429. }
  430. void
  431. run(void) {
  432. XEvent ev;
  433. while(!XNextEvent(dc.dpy, &ev))
  434. switch(ev.type) {
  435. case Expose:
  436. if(ev.xexpose.count == 0)
  437. drawmenu();
  438. break;
  439. case KeyPress:
  440. keypress(&ev.xkey);
  441. break;
  442. case SelectionNotify:
  443. if(ev.xselection.property != None)
  444. paste(ev.xselection.property);
  445. break;
  446. case VisibilityNotify:
  447. if(ev.xvisibility.state != VisibilityUnobscured)
  448. XRaiseWindow(dc.dpy, win);
  449. break;
  450. }
  451. }
  452. void
  453. setup(void) {
  454. int x, y;
  455. #ifdef XINERAMA
  456. int i, n;
  457. XineramaScreenInfo *info;
  458. #endif
  459. XSetWindowAttributes wa;
  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. /* input window geometry */
  465. mh = (dc.font.height + 2) * (lines + 1);
  466. #ifdef XINERAMA
  467. if((info = XineramaQueryScreens(dc.dpy, &n))) {
  468. int di;
  469. unsigned int du;
  470. Window dw;
  471. XQueryPointer(dc.dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
  472. for(i = 0; i < n; 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. /* input window */
  488. wa.override_redirect = True;
  489. wa.background_pixmap = ParentRelative;
  490. wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
  491. win = XCreateWindow(dc.dpy, root, x, y, mw, mh, 0,
  492. DefaultDepth(dc.dpy, screen), CopyFromParent,
  493. DefaultVisual(dc.dpy, screen),
  494. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  495. match();
  496. grabkeyboard();
  497. setupdraw(&dc, win);
  498. inputw = MIN(inputw, mw / 3);
  499. utf8 = XInternAtom(dc.dpy, "UTF8_STRING", False);
  500. XMapRaised(dc.dpy, win);
  501. }
  502. void
  503. usage(void) {
  504. fputs("usage: dmenu [-b] [-i] [-l <lines>] [-p <prompt>] [-fn <font>] [-nb <color>]\n"
  505. " [-nf <color>] [-sb <color>] [-sf <color>] [-v]\n", stderr);
  506. exit(EXIT_FAILURE);
  507. }
  508. int
  509. main(int argc, char *argv[]) {
  510. int i;
  511. progname = "dmenu";
  512. for(i = 1; i < argc; i++)
  513. /* 1-arg flags */
  514. if(!strcmp(argv[i], "-v")) {
  515. fputs("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n", stdout);
  516. exit(EXIT_SUCCESS);
  517. }
  518. else if(!strcmp(argv[i], "-b"))
  519. topbar = False;
  520. else if(!strcmp(argv[i], "-i")) {
  521. fstrncmp = strncasecmp;
  522. fstrstr = cistrstr;
  523. }
  524. else if(i == argc-1)
  525. usage();
  526. /* 2-arg flags */
  527. else if(!strcmp(argv[i], "-l")) {
  528. if((lines = atoi(argv[++i])) > 0)
  529. calcoffsets = calcoffsetsv;
  530. }
  531. else if(!strcmp(argv[i], "-p")) {
  532. prompt = argv[++i];
  533. promptw = MIN(textw(&dc, prompt), mw/5);
  534. }
  535. else if(!strcmp(argv[i], "-fn"))
  536. font = argv[++i];
  537. else if(!strcmp(argv[i], "-nb"))
  538. normbgcolor = argv[++i];
  539. else if(!strcmp(argv[i], "-nf"))
  540. normfgcolor = argv[++i];
  541. else if(!strcmp(argv[i], "-sb"))
  542. selbgcolor = argv[++i];
  543. else if(!strcmp(argv[i], "-sf"))
  544. selfgcolor = argv[++i];
  545. else
  546. usage();
  547. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  548. fputs("dmenu: warning: no locale support\n", stderr);
  549. if(!(dc.dpy = XOpenDisplay(NULL)))
  550. eprintf("cannot open display\n");
  551. screen = DefaultScreen(dc.dpy);
  552. root = RootWindow(dc.dpy, screen);
  553. initfont(&dc, font);
  554. readstdin();
  555. setup();
  556. run();
  557. return EXIT_FAILURE; /* should not reach */
  558. }