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.

393 lines
7.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
  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 *maxname;
  256. fd_set rd;
  257. struct timeval timeout;
  258. Item *i;
  259. XEvent ev;
  260. XSetWindowAttributes wa;
  261. if(argc == 2 && !strncmp("-v", argv[1], 3)) {
  262. fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
  263. exit(EXIT_SUCCESS);
  264. }
  265. else if(argc != 1)
  266. eprint("usage: dmenu [-v]\n");
  267. dpy = XOpenDisplay(0);
  268. if(!dpy)
  269. eprint("dmenu: cannot open display\n");
  270. screen = DefaultScreen(dpy);
  271. root = RootWindow(dpy, screen);
  272. /* Note, the select() construction allows to grab all keypresses as
  273. * early as possible, to not loose them. But if there is no standard
  274. * input supplied, we will make sure to exit after MAX_WAIT_STDIN
  275. * seconds. This is convenience behavior for rapid typers.
  276. */
  277. while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
  278. GrabModeAsync, CurrentTime) != GrabSuccess)
  279. usleep(1000);
  280. timeout.tv_usec = 0;
  281. timeout.tv_sec = STDIN_TIMEOUT;
  282. FD_ZERO(&rd);
  283. FD_SET(STDIN_FILENO, &rd);
  284. if(select(ConnectionNumber(dpy) + 1, &rd, NULL, NULL, &timeout) < 1)
  285. goto UninitializedEnd;
  286. maxname = readstdin();
  287. /* style */
  288. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  289. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  290. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  291. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  292. setfont(FONT);
  293. wa.override_redirect = 1;
  294. wa.background_pixmap = ParentRelative;
  295. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  296. mx = my = 0;
  297. mw = DisplayWidth(dpy, screen);
  298. mh = dc.font.height + 2;
  299. win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
  300. DefaultDepth(dpy, screen), CopyFromParent,
  301. DefaultVisual(dpy, screen),
  302. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  303. XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
  304. /* pixmap */
  305. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  306. dc.gc = XCreateGC(dpy, root, 0, 0);
  307. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  308. if(maxname)
  309. cmdw = textw(maxname);
  310. if(cmdw > mw / 3)
  311. cmdw = mw / 3;
  312. text[0] = 0;
  313. match(text);
  314. XMapRaised(dpy, win);
  315. drawmenu();
  316. XSync(dpy, False);
  317. /* main event loop */
  318. while(running && !XNextEvent(dpy, &ev)) {
  319. switch (ev.type) {
  320. default: /* ignore all crap */
  321. break;
  322. case KeyPress:
  323. kpress(&ev.xkey);
  324. break;
  325. case Expose:
  326. if(ev.xexpose.count == 0)
  327. drawmenu();
  328. break;
  329. }
  330. }
  331. while(allitems) {
  332. i = allitems->next;
  333. free(allitems->text);
  334. free(allitems);
  335. allitems = i;
  336. }
  337. if(dc.font.set)
  338. XFreeFontSet(dpy, dc.font.set);
  339. else
  340. XFreeFont(dpy, dc.font.xfont);
  341. XFreePixmap(dpy, dc.drawable);
  342. XFreeGC(dpy, dc.gc);
  343. XDestroyWindow(dpy, win);
  344. UninitializedEnd:
  345. XUngrabKeyboard(dpy, CurrentTime);
  346. XCloseDisplay(dpy);
  347. return ret;
  348. }