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.

441 lines
9.0 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  2. * (C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include "dmenu.h"
  6. #include <ctype.h>
  7. #include <locale.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <sys/select.h>
  13. #include <sys/time.h>
  14. #include <X11/Xutil.h>
  15. #include <X11/keysym.h>
  16. typedef struct Item Item;
  17. struct Item {
  18. Item *next; /* traverses all items */
  19. Item *left, *right; /* traverses items matching current search pattern */
  20. char *text;
  21. };
  22. /* static */
  23. static char text[4096];
  24. static char *prompt = NULL;
  25. static int mx, my, mw, mh;
  26. static int ret = 0;
  27. static int nitem = 0;
  28. static unsigned int cmdw = 0;
  29. static unsigned int promptw = 0;
  30. static Bool running = True;
  31. static Item *allitems = NULL; /* first of all items */
  32. static Item *item = NULL; /* first of pattern matching items */
  33. static Item *sel = NULL;
  34. static Item *next = NULL;
  35. static Item *prev = NULL;
  36. static Item *curr = NULL;
  37. static Window root;
  38. static Window win;
  39. static void
  40. calcoffsets(void) {
  41. unsigned int tw, w;
  42. if(!curr)
  43. return;
  44. w = promptw + cmdw + 2 * SPACE;
  45. for(next = curr; next; next=next->right) {
  46. tw = textw(next->text);
  47. if(tw > mw / 3)
  48. tw = mw / 3;
  49. w += tw;
  50. if(w > mw)
  51. break;
  52. }
  53. w = promptw + cmdw + 2 * SPACE;
  54. for(prev = curr; prev && prev->left; prev=prev->left) {
  55. tw = textw(prev->left->text);
  56. if(tw > mw / 3)
  57. tw = mw / 3;
  58. w += tw;
  59. if(w > mw)
  60. break;
  61. }
  62. }
  63. static void
  64. drawmenu(void) {
  65. Item *i;
  66. dc.x = 0;
  67. dc.y = 0;
  68. dc.w = mw;
  69. dc.h = mh;
  70. drawtext(NULL, dc.norm);
  71. /* print prompt? */
  72. if(promptw) {
  73. dc.w = promptw;
  74. drawtext(prompt, dc.sel);
  75. }
  76. dc.x += promptw;
  77. dc.w = mw - promptw;
  78. /* print command */
  79. if(cmdw && item)
  80. dc.w = cmdw;
  81. drawtext(text[0] ? text : NULL, dc.norm);
  82. dc.x += cmdw;
  83. if(curr) {
  84. dc.w = SPACE;
  85. drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
  86. dc.x += dc.w;
  87. /* determine maximum items */
  88. for(i = curr; i != next; i=i->right) {
  89. dc.w = textw(i->text);
  90. if(dc.w > mw / 3)
  91. dc.w = mw / 3;
  92. drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
  93. dc.x += dc.w;
  94. }
  95. dc.x = mw - SPACE;
  96. dc.w = SPACE;
  97. drawtext(next ? ">" : NULL, dc.norm);
  98. }
  99. XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
  100. XFlush(dpy);
  101. }
  102. static void
  103. match(char *pattern) {
  104. unsigned int plen;
  105. Item *i, *j;
  106. if(!pattern)
  107. return;
  108. plen = strlen(pattern);
  109. item = j = NULL;
  110. nitem = 0;
  111. for(i = allitems; i; i=i->next)
  112. if(!plen || !strncmp(pattern, i->text, plen)) {
  113. if(!j)
  114. item = i;
  115. else
  116. j->right = i;
  117. i->left = j;
  118. i->right = NULL;
  119. j = i;
  120. nitem++;
  121. }
  122. for(i = allitems; i; i=i->next)
  123. if(plen && strncmp(pattern, i->text, plen)
  124. && strstr(i->text, pattern)) {
  125. if(!j)
  126. item = i;
  127. else
  128. j->right = i;
  129. i->left = j;
  130. i->right = NULL;
  131. j = i;
  132. nitem++;
  133. }
  134. curr = prev = next = sel = item;
  135. calcoffsets();
  136. }
  137. static void
  138. kpress(XKeyEvent * e) {
  139. char buf[32];
  140. int num, prev_nitem;
  141. unsigned int i, len;
  142. KeySym ksym;
  143. len = strlen(text);
  144. buf[0] = 0;
  145. num = XLookupString(e, buf, sizeof buf, &ksym, 0);
  146. if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  147. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  148. || IsPrivateKeypadKey(ksym))
  149. return;
  150. /* first check if a control mask is omitted */
  151. if(e->state & ControlMask) {
  152. switch (ksym) {
  153. default: /* ignore other control sequences */
  154. return;
  155. break;
  156. case XK_h:
  157. case XK_H:
  158. ksym = XK_BackSpace;
  159. break;
  160. case XK_u:
  161. case XK_U:
  162. text[0] = 0;
  163. match(text);
  164. drawmenu();
  165. return;
  166. break;
  167. }
  168. }
  169. switch(ksym) {
  170. default:
  171. if(num && !iscntrl((int) buf[0])) {
  172. buf[num] = 0;
  173. if(len > 0)
  174. strncat(text, buf, sizeof text);
  175. else
  176. strncpy(text, buf, sizeof text);
  177. match(text);
  178. }
  179. break;
  180. case XK_BackSpace:
  181. if((i = len)) {
  182. prev_nitem = nitem;
  183. do {
  184. text[--i] = 0;
  185. match(text);
  186. } while(i && nitem && prev_nitem == nitem);
  187. match(text);
  188. }
  189. break;
  190. case XK_End:
  191. if(!item)
  192. return;
  193. while(next) {
  194. sel = curr = next;
  195. calcoffsets();
  196. }
  197. while(sel && sel->right)
  198. sel = sel->right;
  199. break;
  200. case XK_Escape:
  201. ret = 1;
  202. running = False;
  203. break;
  204. case XK_Home:
  205. if(!item)
  206. return;
  207. sel = curr = item;
  208. calcoffsets();
  209. break;
  210. case XK_Left:
  211. if(!(sel && sel->left))
  212. return;
  213. sel=sel->left;
  214. if(sel->right == curr) {
  215. curr = prev;
  216. calcoffsets();
  217. }
  218. break;
  219. case XK_Next:
  220. if(!next)
  221. return;
  222. sel = curr = next;
  223. calcoffsets();
  224. break;
  225. case XK_Prior:
  226. if(!prev)
  227. return;
  228. sel = curr = prev;
  229. calcoffsets();
  230. break;
  231. case XK_Return:
  232. if((e->state & ShiftMask) && text)
  233. fprintf(stdout, "%s", text);
  234. else if(sel)
  235. fprintf(stdout, "%s", sel->text);
  236. else if(text)
  237. fprintf(stdout, "%s", text);
  238. fflush(stdout);
  239. running = False;
  240. break;
  241. case XK_Right:
  242. if(!(sel && sel->right))
  243. return;
  244. sel=sel->right;
  245. if(sel == next) {
  246. curr = next;
  247. calcoffsets();
  248. }
  249. break;
  250. case XK_Tab:
  251. if(!sel)
  252. return;
  253. strncpy(text, sel->text, sizeof text);
  254. match(text);
  255. break;
  256. }
  257. drawmenu();
  258. }
  259. static char *
  260. readstdin(void) {
  261. static char *maxname = NULL;
  262. char *p, buf[1024];
  263. unsigned int len = 0, max = 0;
  264. Item *i, *new;
  265. i = 0;
  266. while(fgets(buf, sizeof buf, stdin)) {
  267. len = strlen(buf);
  268. if (buf[len - 1] == '\n')
  269. buf[len - 1] = 0;
  270. p = estrdup(buf);
  271. if(max < len) {
  272. maxname = p;
  273. max = len;
  274. }
  275. new = emalloc(sizeof(Item));
  276. new->next = new->left = new->right = NULL;
  277. new->text = p;
  278. if(!i)
  279. allitems = new;
  280. else
  281. i->next = new;
  282. i = new;
  283. }
  284. return maxname;
  285. }
  286. /* extern */
  287. int screen;
  288. Display *dpy;
  289. DC dc = {0};
  290. int
  291. main(int argc, char *argv[]) {
  292. char *font = FONT;
  293. char *maxname;
  294. char *normbg = NORMBGCOLOR;
  295. char *normfg = NORMFGCOLOR;
  296. char *selbg = SELBGCOLOR;
  297. char *selfg = SELFGCOLOR;
  298. fd_set rd;
  299. int i;
  300. struct timeval timeout;
  301. Item *itm;
  302. XEvent ev;
  303. XSetWindowAttributes wa;
  304. timeout.tv_usec = 0;
  305. timeout.tv_sec = 3;
  306. /* command line args */
  307. for(i = 1; i < argc; i++)
  308. if(!strncmp(argv[i], "-font", 6)) {
  309. if(++i < argc) font = argv[i];
  310. }
  311. else if(!strncmp(argv[i], "-normbg", 8)) {
  312. if(++i < argc) normbg = argv[i];
  313. }
  314. else if(!strncmp(argv[i], "-normfg", 8)) {
  315. if(++i < argc) normfg = argv[i];
  316. }
  317. else if(!strncmp(argv[i], "-selbg", 7)) {
  318. if(++i < argc) selbg = argv[i];
  319. }
  320. else if(!strncmp(argv[i], "-selfg", 7)) {
  321. if(++i < argc) selfg = argv[i];
  322. }
  323. else if(!strncmp(argv[i], "-p", 3)) {
  324. if(++i < argc) prompt = argv[i];
  325. }
  326. else if(!strncmp(argv[i], "-t", 3)) {
  327. if(++i < argc) timeout.tv_sec = atoi(argv[i]);
  328. }
  329. else if(!strncmp(argv[i], "-v", 3)) {
  330. fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
  331. exit(EXIT_SUCCESS);
  332. }
  333. else
  334. eprint("usage: dmenu [-font <name>] [-{norm,sel}{bg,fg} <color>] [-p <prompt>] [-t <seconds>] [-v]\n", stdout);
  335. setlocale(LC_CTYPE, "");
  336. dpy = XOpenDisplay(0);
  337. if(!dpy)
  338. eprint("dmenu: cannot open display\n");
  339. screen = DefaultScreen(dpy);
  340. root = RootWindow(dpy, screen);
  341. /* Note, the select() construction allows to grab all keypresses as
  342. * early as possible, to not loose them. But if there is no standard
  343. * input supplied, we will make sure to exit after MAX_WAIT_STDIN
  344. * seconds. This is convenience behavior for rapid typers.
  345. */
  346. while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
  347. GrabModeAsync, CurrentTime) != GrabSuccess)
  348. usleep(1000);
  349. FD_ZERO(&rd);
  350. FD_SET(STDIN_FILENO, &rd);
  351. if(select(ConnectionNumber(dpy) + 1, &rd, NULL, NULL, &timeout) < 1)
  352. goto UninitializedEnd;
  353. maxname = readstdin();
  354. /* style */
  355. dc.norm[ColBG] = getcolor(normbg);
  356. dc.norm[ColFG] = getcolor(normfg);
  357. dc.sel[ColBG] = getcolor(selbg);
  358. dc.sel[ColFG] = getcolor(selfg);
  359. setfont(font);
  360. /* menu window */
  361. wa.override_redirect = 1;
  362. wa.background_pixmap = ParentRelative;
  363. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  364. mx = my = 0;
  365. mw = DisplayWidth(dpy, screen);
  366. mh = dc.font.height + 2;
  367. win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
  368. DefaultDepth(dpy, screen), CopyFromParent,
  369. DefaultVisual(dpy, screen),
  370. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  371. /* pixmap */
  372. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  373. dc.gc = XCreateGC(dpy, root, 0, 0);
  374. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  375. if(maxname)
  376. cmdw = textw(maxname);
  377. if(cmdw > mw / 3)
  378. cmdw = mw / 3;
  379. if(prompt)
  380. promptw = textw(prompt);
  381. if(promptw > mw / 5)
  382. promptw = mw / 5;
  383. text[0] = 0;
  384. match(text);
  385. XMapRaised(dpy, win);
  386. drawmenu();
  387. XSync(dpy, False);
  388. /* main event loop */
  389. while(running && !XNextEvent(dpy, &ev))
  390. switch (ev.type) {
  391. default: /* ignore all crap */
  392. break;
  393. case KeyPress:
  394. kpress(&ev.xkey);
  395. break;
  396. case Expose:
  397. if(ev.xexpose.count == 0)
  398. drawmenu();
  399. break;
  400. }
  401. /* cleanup */
  402. while(allitems) {
  403. itm = allitems->next;
  404. free(allitems->text);
  405. free(allitems);
  406. allitems = itm;
  407. }
  408. if(dc.font.set)
  409. XFreeFontSet(dpy, dc.font.set);
  410. else
  411. XFreeFont(dpy, dc.font.xfont);
  412. XFreePixmap(dpy, dc.drawable);
  413. XFreeGC(dpy, dc.gc);
  414. XDestroyWindow(dpy, win);
  415. UninitializedEnd:
  416. XUngrabKeyboard(dpy, CurrentTime);
  417. XCloseDisplay(dpy);
  418. return ret;
  419. }