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.

396 lines
8.3 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
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 <locale.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/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. if(++i < argc) font = argv[i];
  272. }
  273. else if(!strncmp(argv[i], "-normbg", 8)) {
  274. if(++i < argc) normbg = argv[i];
  275. }
  276. else if(!strncmp(argv[i], "-normfg", 8)) {
  277. if(++i < argc) normfg = argv[i];
  278. }
  279. else if(!strncmp(argv[i], "-selbg", 7)) {
  280. if(++i < argc) selbg = argv[i];
  281. }
  282. else if(!strncmp(argv[i], "-selfg", 7)) {
  283. if(++i < argc) selfg = argv[i];
  284. }
  285. else if(!strncmp(argv[i], "-t", 3)) {
  286. if(++i < argc) timeout.tv_sec = atoi(argv[i]);
  287. }
  288. else if(!strncmp(argv[i], "-v", 3)) {
  289. fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
  290. exit(EXIT_SUCCESS);
  291. }
  292. else
  293. eprint("usage: dmenu [-font <name>] [-{norm,sel}{bg,fg} <color>] [-t <seconds>] [-v]\n", stdout);
  294. setlocale(LC_CTYPE, "");
  295. dpy = XOpenDisplay(0);
  296. if(!dpy)
  297. eprint("dmenu: cannot open display\n");
  298. screen = DefaultScreen(dpy);
  299. root = RootWindow(dpy, screen);
  300. /* Note, the select() construction allows to grab all keypresses as
  301. * early as possible, to not loose them. But if there is no standard
  302. * input supplied, we will make sure to exit after MAX_WAIT_STDIN
  303. * seconds. This is convenience behavior for rapid typers.
  304. */
  305. while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
  306. GrabModeAsync, CurrentTime) != GrabSuccess)
  307. usleep(1000);
  308. FD_ZERO(&rd);
  309. FD_SET(STDIN_FILENO, &rd);
  310. if(select(ConnectionNumber(dpy) + 1, &rd, NULL, NULL, &timeout) < 1)
  311. goto UninitializedEnd;
  312. maxname = readstdin();
  313. /* style */
  314. dc.norm[ColBG] = getcolor(normbg);
  315. dc.norm[ColFG] = getcolor(normfg);
  316. dc.sel[ColBG] = getcolor(selbg);
  317. dc.sel[ColFG] = getcolor(selfg);
  318. setfont(font);
  319. /* menu window */
  320. wa.override_redirect = 1;
  321. wa.background_pixmap = ParentRelative;
  322. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  323. mx = my = 0;
  324. mw = DisplayWidth(dpy, screen);
  325. mh = dc.font.height + 2;
  326. win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
  327. DefaultDepth(dpy, screen), CopyFromParent,
  328. DefaultVisual(dpy, screen),
  329. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  330. /* pixmap */
  331. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  332. dc.gc = XCreateGC(dpy, root, 0, 0);
  333. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  334. if(maxname)
  335. cmdw = textw(maxname);
  336. if(cmdw > mw / 3)
  337. cmdw = mw / 3;
  338. text[0] = 0;
  339. match(text);
  340. XMapRaised(dpy, win);
  341. drawmenu();
  342. XSync(dpy, False);
  343. /* main event loop */
  344. while(running && !XNextEvent(dpy, &ev))
  345. switch (ev.type) {
  346. default: /* ignore all crap */
  347. break;
  348. case KeyPress:
  349. kpress(&ev.xkey);
  350. break;
  351. case Expose:
  352. if(ev.xexpose.count == 0)
  353. drawmenu();
  354. break;
  355. }
  356. /* cleanup */
  357. while(allitems) {
  358. itm = allitems->next;
  359. free(allitems->text);
  360. free(allitems);
  361. allitems = itm;
  362. }
  363. if(dc.font.set)
  364. XFreeFontSet(dpy, dc.font.set);
  365. else
  366. XFreeFont(dpy, dc.font.xfont);
  367. XFreePixmap(dpy, dc.drawable);
  368. XFreeGC(dpy, dc.gc);
  369. XDestroyWindow(dpy, win);
  370. UninitializedEnd:
  371. XUngrabKeyboard(dpy, CurrentTime);
  372. XCloseDisplay(dpy);
  373. return ret;
  374. }