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.

382 lines
7.4 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. /*
  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. strncmp(pattern, i->text, plen) && strstr(i->text, pattern)) {
  106. if(!j)
  107. item = i;
  108. else
  109. j->right = i;
  110. i->left = j;
  111. i->right = NULL;
  112. j = i;
  113. nitem++;
  114. }
  115. curr = prev = next = sel = item;
  116. calcoffsets();
  117. }
  118. static void
  119. kpress(XKeyEvent * e) {
  120. char buf[32];
  121. int num, prev_nitem;
  122. unsigned int i, len;
  123. KeySym ksym;
  124. len = strlen(text);
  125. buf[0] = 0;
  126. num = XLookupString(e, buf, sizeof(buf), &ksym, 0);
  127. if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  128. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  129. || IsPrivateKeypadKey(ksym))
  130. return;
  131. /* first check if a control mask is omitted */
  132. if(e->state & ControlMask) {
  133. switch (ksym) {
  134. default: /* ignore other control sequences */
  135. return;
  136. break;
  137. case XK_h:
  138. case XK_H:
  139. ksym = XK_BackSpace;
  140. break;
  141. case XK_u:
  142. case XK_U:
  143. text[0] = 0;
  144. match(text);
  145. drawmenu();
  146. return;
  147. break;
  148. }
  149. }
  150. switch(ksym) {
  151. case XK_Left:
  152. if(!(sel && sel->left))
  153. return;
  154. sel=sel->left;
  155. if(sel->right == curr) {
  156. curr = prev;
  157. calcoffsets();
  158. }
  159. break;
  160. case XK_Tab:
  161. if(!sel)
  162. return;
  163. strncpy(text, sel->text, sizeof(text));
  164. match(text);
  165. break;
  166. case XK_Right:
  167. if(!(sel && sel->right))
  168. return;
  169. sel=sel->right;
  170. if(sel == next) {
  171. curr = next;
  172. calcoffsets();
  173. }
  174. break;
  175. case XK_Return:
  176. if((e->state & ShiftMask) && text)
  177. fprintf(stdout, "%s", text);
  178. else if(sel)
  179. fprintf(stdout, "%s", sel->text);
  180. else if(text)
  181. fprintf(stdout, "%s", text);
  182. fflush(stdout);
  183. running = False;
  184. break;
  185. case XK_Escape:
  186. ret = 1;
  187. running = False;
  188. break;
  189. case XK_BackSpace:
  190. if((i = len)) {
  191. prev_nitem = nitem;
  192. do {
  193. text[--i] = 0;
  194. match(text);
  195. } while(i && nitem && prev_nitem == nitem);
  196. match(text);
  197. }
  198. break;
  199. default:
  200. if(num && !iscntrl((int) buf[0])) {
  201. buf[num] = 0;
  202. if(len > 0)
  203. strncat(text, buf, sizeof(text));
  204. else
  205. strncpy(text, buf, sizeof(text));
  206. match(text);
  207. }
  208. }
  209. drawmenu();
  210. }
  211. static char *
  212. readstdin(void) {
  213. static char *maxname = NULL;
  214. char *p, buf[1024];
  215. unsigned int len = 0, max = 0;
  216. Item *i, *new;
  217. i = 0;
  218. while(fgets(buf, sizeof(buf), stdin)) {
  219. len = strlen(buf);
  220. if (buf[len - 1] == '\n')
  221. buf[len - 1] = 0;
  222. p = estrdup(buf);
  223. if(max < len) {
  224. maxname = p;
  225. max = len;
  226. }
  227. new = emalloc(sizeof(Item));
  228. new->next = new->left = new->right = NULL;
  229. new->text = p;
  230. if(!i)
  231. allitems = new;
  232. else
  233. i->next = new;
  234. i = new;
  235. }
  236. return maxname;
  237. }
  238. /* extern */
  239. int screen;
  240. Display *dpy;
  241. DC dc = {0};
  242. int
  243. main(int argc, char *argv[]) {
  244. char *maxname;
  245. fd_set rd;
  246. struct timeval timeout;
  247. Item *i;
  248. XEvent ev;
  249. XSetWindowAttributes wa;
  250. if(argc == 2 && !strncmp("-v", argv[1], 3)) {
  251. fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
  252. exit(EXIT_SUCCESS);
  253. }
  254. else if(argc != 1)
  255. eprint("usage: dmenu [-v]\n");
  256. dpy = XOpenDisplay(0);
  257. if(!dpy)
  258. eprint("dmenu: cannot open display\n");
  259. screen = DefaultScreen(dpy);
  260. root = RootWindow(dpy, screen);
  261. /* Note, the select() construction allows to grab all keypresses as
  262. * early as possible, to not loose them. But if there is no standard
  263. * input supplied, we will make sure to exit after MAX_WAIT_STDIN
  264. * seconds. This is convenience behavior for rapid typers.
  265. */
  266. while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
  267. GrabModeAsync, CurrentTime) != GrabSuccess)
  268. usleep(1000);
  269. timeout.tv_usec = 0;
  270. timeout.tv_sec = STDIN_TIMEOUT;
  271. FD_ZERO(&rd);
  272. FD_SET(STDIN_FILENO, &rd);
  273. if(select(ConnectionNumber(dpy) + 1, &rd, NULL, NULL, &timeout) < 1)
  274. goto UninitializedEnd;
  275. maxname = readstdin();
  276. /* style */
  277. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  278. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  279. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  280. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  281. setfont(FONT);
  282. wa.override_redirect = 1;
  283. wa.background_pixmap = ParentRelative;
  284. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  285. mx = my = 0;
  286. mw = DisplayWidth(dpy, screen);
  287. mh = dc.font.height + 2;
  288. win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
  289. DefaultDepth(dpy, screen), CopyFromParent,
  290. DefaultVisual(dpy, screen),
  291. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  292. XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
  293. /* pixmap */
  294. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  295. dc.gc = XCreateGC(dpy, root, 0, 0);
  296. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  297. if(maxname)
  298. cmdw = textw(maxname);
  299. if(cmdw > mw / 3)
  300. cmdw = mw / 3;
  301. text[0] = 0;
  302. match(text);
  303. XMapRaised(dpy, win);
  304. drawmenu();
  305. XSync(dpy, False);
  306. /* main event loop */
  307. while(running && !XNextEvent(dpy, &ev)) {
  308. switch (ev.type) {
  309. default: /* ignore all crap */
  310. break;
  311. case KeyPress:
  312. kpress(&ev.xkey);
  313. break;
  314. case Expose:
  315. if(ev.xexpose.count == 0)
  316. drawmenu();
  317. break;
  318. }
  319. }
  320. while(allitems) {
  321. i = allitems->next;
  322. free(allitems->text);
  323. free(allitems);
  324. allitems = i;
  325. }
  326. if(dc.font.set)
  327. XFreeFontSet(dpy, dc.font.set);
  328. else
  329. XFreeFont(dpy, dc.font.xfont);
  330. XFreePixmap(dpy, dc.drawable);
  331. XFreeGC(dpy, dc.gc);
  332. XDestroyWindow(dpy, win);
  333. UninitializedEnd:
  334. XUngrabKeyboard(dpy, CurrentTime);
  335. XCloseDisplay(dpy);
  336. return ret;
  337. }