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.

390 lines
8.2 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
  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 <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <sys/select.h>
  12. #include <sys/time.h>
  13. #include <X11/cursorfont.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. case XK_Left:
  162. if(!(sel && sel->left))
  163. return;
  164. sel=sel->left;
  165. if(sel->right == curr) {
  166. curr = prev;
  167. calcoffsets();
  168. }
  169. break;
  170. case XK_Tab:
  171. if(!sel)
  172. return;
  173. strncpy(text, sel->text, sizeof(text));
  174. match(text);
  175. break;
  176. case XK_Right:
  177. if(!(sel && sel->right))
  178. return;
  179. sel=sel->right;
  180. if(sel == next) {
  181. curr = next;
  182. calcoffsets();
  183. }
  184. break;
  185. case XK_Return:
  186. if((e->state & ShiftMask) && text)
  187. fprintf(stdout, "%s", text);
  188. else if(sel)
  189. fprintf(stdout, "%s", sel->text);
  190. else if(text)
  191. fprintf(stdout, "%s", text);
  192. fflush(stdout);
  193. running = False;
  194. break;
  195. case XK_Escape:
  196. ret = 1;
  197. running = False;
  198. break;
  199. case XK_BackSpace:
  200. if((i = len)) {
  201. prev_nitem = nitem;
  202. do {
  203. text[--i] = 0;
  204. match(text);
  205. } while(i && nitem && prev_nitem == nitem);
  206. match(text);
  207. }
  208. break;
  209. default:
  210. if(num && !iscntrl((int) buf[0])) {
  211. buf[num] = 0;
  212. if(len > 0)
  213. strncat(text, buf, sizeof(text));
  214. else
  215. strncpy(text, buf, sizeof(text));
  216. match(text);
  217. }
  218. }
  219. drawmenu();
  220. }
  221. static char *
  222. readstdin(void) {
  223. static char *maxname = NULL;
  224. char *p, buf[1024];
  225. unsigned int len = 0, max = 0;
  226. Item *i, *new;
  227. i = 0;
  228. while(fgets(buf, sizeof(buf), stdin)) {
  229. len = strlen(buf);
  230. if (buf[len - 1] == '\n')
  231. buf[len - 1] = 0;
  232. p = estrdup(buf);
  233. if(max < len) {
  234. maxname = p;
  235. max = len;
  236. }
  237. new = emalloc(sizeof(Item));
  238. new->next = new->left = new->right = NULL;
  239. new->text = p;
  240. if(!i)
  241. allitems = new;
  242. else
  243. i->next = new;
  244. i = new;
  245. }
  246. return maxname;
  247. }
  248. /* extern */
  249. int screen;
  250. Display *dpy;
  251. DC dc = {0};
  252. int
  253. main(int argc, char *argv[]) {
  254. char *font = FONT;
  255. char *maxname;
  256. char *normbg = NORMBGCOLOR;
  257. char *normfg = NORMFGCOLOR;
  258. char *selbg = SELBGCOLOR;
  259. char *selfg = SELFGCOLOR;
  260. fd_set rd;
  261. int i;
  262. struct timeval timeout;
  263. Item *itm;
  264. XEvent ev;
  265. XSetWindowAttributes wa;
  266. timeout.tv_usec = 0;
  267. timeout.tv_sec = 3;
  268. /* command line args */
  269. for(i = 1; i < argc; i++)
  270. if(!strncmp(argv[i], "-font", 6))
  271. font = argv[++i];
  272. else if(!strncmp(argv[i], "-normbg", 8))
  273. normbg = argv[++i];
  274. else if(!strncmp(argv[i], "-normfg", 8))
  275. normfg = argv[++i];
  276. else if(!strncmp(argv[i], "-selbg", 7))
  277. selbg = argv[++i];
  278. else if(!strncmp(argv[i], "-selfg", 7))
  279. selfg = argv[++i];
  280. else if(!strncmp(argv[i], "-t", 3))
  281. timeout.tv_sec = atoi(argv[++i]);
  282. else if(!strncmp(argv[i], "-v", 3)) {
  283. fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
  284. exit(EXIT_SUCCESS);
  285. }
  286. else
  287. eprint("usage: dmenu [-font <name>] [-{norm,sel}{bg,fg} <color>] [-t <seconds>] [-v]\n", stdout);
  288. dpy = XOpenDisplay(0);
  289. if(!dpy)
  290. eprint("dmenu: cannot open display\n");
  291. screen = DefaultScreen(dpy);
  292. root = RootWindow(dpy, screen);
  293. /* Note, the select() construction allows to grab all keypresses as
  294. * early as possible, to not loose them. But if there is no standard
  295. * input supplied, we will make sure to exit after MAX_WAIT_STDIN
  296. * seconds. This is convenience behavior for rapid typers.
  297. */
  298. while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
  299. GrabModeAsync, CurrentTime) != GrabSuccess)
  300. usleep(1000);
  301. FD_ZERO(&rd);
  302. FD_SET(STDIN_FILENO, &rd);
  303. if(select(ConnectionNumber(dpy) + 1, &rd, NULL, NULL, &timeout) < 1)
  304. goto UninitializedEnd;
  305. maxname = readstdin();
  306. /* style */
  307. dc.norm[ColBG] = getcolor(normbg);
  308. dc.norm[ColFG] = getcolor(normfg);
  309. dc.sel[ColBG] = getcolor(selbg);
  310. dc.sel[ColFG] = getcolor(selfg);
  311. setfont(font);
  312. /* menu window */
  313. wa.override_redirect = 1;
  314. wa.background_pixmap = ParentRelative;
  315. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  316. mx = my = 0;
  317. mw = DisplayWidth(dpy, screen);
  318. mh = dc.font.height + 2;
  319. win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
  320. DefaultDepth(dpy, screen), CopyFromParent,
  321. DefaultVisual(dpy, screen),
  322. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  323. XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
  324. /* pixmap */
  325. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  326. dc.gc = XCreateGC(dpy, root, 0, 0);
  327. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  328. if(maxname)
  329. cmdw = textw(maxname);
  330. if(cmdw > mw / 3)
  331. cmdw = mw / 3;
  332. text[0] = 0;
  333. match(text);
  334. XMapRaised(dpy, win);
  335. drawmenu();
  336. XSync(dpy, False);
  337. /* main event loop */
  338. while(running && !XNextEvent(dpy, &ev))
  339. switch (ev.type) {
  340. default: /* ignore all crap */
  341. break;
  342. case KeyPress:
  343. kpress(&ev.xkey);
  344. break;
  345. case Expose:
  346. if(ev.xexpose.count == 0)
  347. drawmenu();
  348. break;
  349. }
  350. /* cleanup */
  351. while(allitems) {
  352. itm = allitems->next;
  353. free(allitems->text);
  354. free(allitems);
  355. allitems = itm;
  356. }
  357. if(dc.font.set)
  358. XFreeFontSet(dpy, dc.font.set);
  359. else
  360. XFreeFont(dpy, dc.font.xfont);
  361. XFreePixmap(dpy, dc.drawable);
  362. XFreeGC(dpy, dc.gc);
  363. XDestroyWindow(dpy, win);
  364. UninitializedEnd:
  365. XUngrabKeyboard(dpy, CurrentTime);
  366. XCloseDisplay(dpy);
  367. return ret;
  368. }