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.

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