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.

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