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.

566 lines
12 KiB

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