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.

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