menu.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 "config.h"
  7. #include "draw.h"
  8. #include "util.h"
  9. #include <ctype.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <sys/stat.h>
  14. #include <sys/wait.h>
  15. #include <time.h>
  16. #include <unistd.h>
  17. #include <X11/cursorfont.h>
  18. #include <X11/Xutil.h>
  19. #include <X11/keysym.h>
  20. typedef struct Item Item;
  21. struct Item {
  22. Item *next; /* traverses all items */
  23. Item *left, *right; /* traverses items matching current search pattern */
  24. char *text;
  25. };
  26. static Display *dpy;
  27. static Window root;
  28. static Window win;
  29. static Bool done = False;
  30. static Item *allitem = NULL; /* first of all items */
  31. static Item *item = NULL; /* first of pattern matching items */
  32. static Item *sel = NULL;
  33. static Item *nextoff = NULL;
  34. static Item *prevoff = NULL;
  35. static Item *curroff = NULL;
  36. static int screen, mx, my, mw, mh;
  37. static char *title = NULL;
  38. static char text[4096];
  39. static int ret = 0;
  40. static int nitem = 0;
  41. static unsigned int cmdw = 0;
  42. static unsigned int tw = 0;
  43. static unsigned int cw = 0;
  44. static const int seek = 30; /* 30px */
  45. static Brush brush = {0};
  46. static void draw_menu();
  47. static void kpress(XKeyEvent * e);
  48. static char version[] = "gridmenu - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
  49. static void
  50. usage()
  51. {
  52. fprintf(stderr, "%s", "usage: gridmenu [-v] [-t <title>]\n");
  53. exit(1);
  54. }
  55. static void
  56. update_offsets()
  57. {
  58. unsigned int tw, w = cmdw + 2 * seek;
  59. if(!curroff)
  60. return;
  61. for(nextoff = curroff; nextoff; nextoff=nextoff->right) {
  62. tw = textw(&brush.font, nextoff->text);
  63. if(tw > mw / 3)
  64. tw = mw / 3;
  65. w += tw + brush.font.height;
  66. if(w > mw)
  67. break;
  68. }
  69. w = cmdw + 2 * seek;
  70. for(prevoff = curroff; prevoff && prevoff->left; prevoff=prevoff->left) {
  71. tw = textw(&brush.font, prevoff->left->text);
  72. if(tw > mw / 3)
  73. tw = mw / 3;
  74. w += tw + brush.font.height;
  75. if(w > mw)
  76. break;
  77. }
  78. }
  79. static void
  80. update_items(char *pattern)
  81. {
  82. unsigned int plen = strlen(pattern);
  83. Item *i, *j;
  84. if(!pattern)
  85. return;
  86. if(!title || *pattern)
  87. cmdw = cw;
  88. else
  89. cmdw = tw;
  90. item = j = NULL;
  91. nitem = 0;
  92. for(i = allitem; i; i=i->next)
  93. if(!plen || !strncmp(pattern, i->text, plen)) {
  94. if(!j)
  95. item = i;
  96. else
  97. j->right = i;
  98. i->left = j;
  99. i->right = NULL;
  100. j = i;
  101. nitem++;
  102. }
  103. for(i = allitem; i; i=i->next)
  104. if(plen && strncmp(pattern, i->text, plen)
  105. && 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. curroff = prevoff = nextoff = sel = item;
  116. update_offsets();
  117. }
  118. /* creates brush structs for brush mode drawing */
  119. static void
  120. draw_menu()
  121. {
  122. Item *i;
  123. brush.x = 0;
  124. brush.y = 0;
  125. brush.w = mw;
  126. brush.h = mh;
  127. draw(dpy, &brush, False, 0);
  128. /* print command */
  129. if(!title || text[0]) {
  130. cmdw = cw;
  131. if(cmdw && item)
  132. brush.w = cmdw;
  133. draw(dpy, &brush, False, text);
  134. }
  135. else {
  136. cmdw = tw;
  137. brush.w = cmdw;
  138. draw(dpy, &brush, False, title);
  139. }
  140. brush.x += brush.w;
  141. if(curroff) {
  142. brush.w = seek;
  143. draw(dpy, &brush, False, (curroff && curroff->left) ? "<" : 0);
  144. brush.x += brush.w;
  145. /* determine maximum items */
  146. for(i = curroff; i != nextoff; i=i->right) {
  147. brush.border = False;
  148. brush.w = textw(&brush.font, i->text);
  149. if(brush.w > mw / 3)
  150. brush.w = mw / 3;
  151. brush.w += brush.font.height;
  152. if(sel == i) {
  153. swap((void **)&brush.fg, (void **)&brush.bg);
  154. draw(dpy, &brush, True, i->text);
  155. swap((void **)&brush.fg, (void **)&brush.bg);
  156. }
  157. else
  158. draw(dpy, &brush, False, i->text);
  159. brush.x += brush.w;
  160. }
  161. brush.x = mw - seek;
  162. brush.w = seek;
  163. draw(dpy, &brush, False, nextoff ? ">" : 0);
  164. }
  165. XCopyArea(dpy, brush.drawable, win, brush.gc, 0, 0, mw, mh, 0, 0);
  166. XFlush(dpy);
  167. }
  168. static void
  169. kpress(XKeyEvent * e)
  170. {
  171. KeySym ksym;
  172. char buf[32];
  173. int num, prev_nitem;
  174. unsigned int i, len = strlen(text);
  175. buf[0] = 0;
  176. num = XLookupString(e, buf, sizeof(buf), &ksym, 0);
  177. if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  178. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  179. || IsPrivateKeypadKey(ksym))
  180. return;
  181. /* first check if a control mask is omitted */
  182. if(e->state & ControlMask) {
  183. switch (ksym) {
  184. case XK_H:
  185. case XK_h:
  186. ksym = XK_BackSpace;
  187. break;
  188. case XK_I:
  189. case XK_i:
  190. ksym = XK_Tab;
  191. break;
  192. case XK_J:
  193. case XK_j:
  194. ksym = XK_Return;
  195. break;
  196. case XK_N:
  197. case XK_n:
  198. ksym = XK_Right;
  199. break;
  200. case XK_P:
  201. case XK_p:
  202. ksym = XK_Left;
  203. break;
  204. case XK_U:
  205. case XK_u:
  206. text[0] = 0;
  207. update_items(text);
  208. draw_menu();
  209. return;
  210. break;
  211. case XK_bracketleft:
  212. ksym = XK_Escape;
  213. break;
  214. default: /* ignore other control sequences */
  215. return;
  216. break;
  217. }
  218. }
  219. switch (ksym) {
  220. case XK_Left:
  221. if(!(sel && sel->left))
  222. return;
  223. sel=sel->left;
  224. if(sel->right == curroff) {
  225. curroff = prevoff;
  226. update_offsets();
  227. }
  228. break;
  229. case XK_Tab:
  230. if(!sel)
  231. return;
  232. strncpy(text, sel->text, sizeof(text));
  233. update_items(text);
  234. break;
  235. case XK_Right:
  236. if(!(sel && sel->right))
  237. return;
  238. sel=sel->right;
  239. if(sel == nextoff) {
  240. curroff = nextoff;
  241. update_offsets();
  242. }
  243. break;
  244. case XK_Return:
  245. if(e->state & ShiftMask) {
  246. if(text)
  247. fprintf(stdout, "%s", text);
  248. }
  249. else if(sel)
  250. fprintf(stdout, "%s", sel->text);
  251. else if(text)
  252. fprintf(stdout, "%s", text);
  253. fflush(stdout);
  254. done = True;
  255. break;
  256. case XK_Escape:
  257. ret = 1;
  258. done = True;
  259. break;
  260. case XK_BackSpace:
  261. if((i = len)) {
  262. prev_nitem = nitem;
  263. do {
  264. text[--i] = 0;
  265. update_items(text);
  266. } while(i && nitem && prev_nitem == nitem);
  267. update_items(text);
  268. }
  269. break;
  270. default:
  271. if(num && !iscntrl((int) buf[0])) {
  272. buf[num] = 0;
  273. if(len > 0)
  274. strncat(text, buf, sizeof(text));
  275. else
  276. strncpy(text, buf, sizeof(text));
  277. update_items(text);
  278. }
  279. }
  280. draw_menu();
  281. }
  282. static char *
  283. read_allitems()
  284. {
  285. static char *maxname = NULL;
  286. char *p, buf[1024];
  287. unsigned int len = 0, max = 0;
  288. Item *i, *new;
  289. i = 0;
  290. while(fgets(buf, sizeof(buf), stdin)) {
  291. len = strlen(buf);
  292. if (buf[len - 1] == '\n')
  293. buf[len - 1] = 0;
  294. p = estrdup(buf);
  295. if(max < len) {
  296. maxname = p;
  297. max = len;
  298. }
  299. new = emalloc(sizeof(Item));
  300. new->next = new->left = new->right = NULL;
  301. new->text = p;
  302. if(!i)
  303. allitem = new;
  304. else
  305. i->next = new;
  306. i = new;
  307. }
  308. return maxname;
  309. }
  310. int
  311. main(int argc, char *argv[])
  312. {
  313. int i;
  314. XSetWindowAttributes wa;
  315. char *maxname;
  316. XEvent ev;
  317. /* command line args */
  318. for(i = 1; i < argc; i++) {
  319. if (argv[i][0] == '-')
  320. switch (argv[i][1]) {
  321. case 'v':
  322. fprintf(stdout, "%s", version);
  323. exit(0);
  324. break;
  325. case 't':
  326. if(++i < argc)
  327. title = argv[i];
  328. else
  329. usage();
  330. break;
  331. default:
  332. usage();
  333. break;
  334. }
  335. else
  336. usage();
  337. }
  338. dpy = XOpenDisplay(0);
  339. if(!dpy)
  340. error("gridmenu: cannot open dpy\n");
  341. screen = DefaultScreen(dpy);
  342. root = RootWindow(dpy, screen);
  343. maxname = read_allitems();
  344. /* grab as early as possible, but after reading all items!!! */
  345. while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
  346. GrabModeAsync, CurrentTime) != GrabSuccess)
  347. usleep(1000);
  348. /* style */
  349. loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
  350. loadfont(dpy, &brush.font, FONT);
  351. wa.override_redirect = 1;
  352. wa.background_pixmap = ParentRelative;
  353. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  354. mx = my = 0;
  355. mw = DisplayWidth(dpy, screen);
  356. mh = texth(&brush.font);
  357. win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
  358. DefaultDepth(dpy, screen), CopyFromParent,
  359. DefaultVisual(dpy, screen),
  360. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  361. XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
  362. XFlush(dpy);
  363. /* pixmap */
  364. brush.gc = XCreateGC(dpy, root, 0, 0);
  365. brush.drawable = XCreatePixmap(dpy, win, mw, mh,
  366. DefaultDepth(dpy, screen));
  367. XFlush(dpy);
  368. if(maxname)
  369. cw = textw(&brush.font, maxname) + brush.font.height;
  370. if(cw > mw / 3)
  371. cw = mw / 3;
  372. if(title) {
  373. tw = textw(&brush.font, title) + brush.font.height;
  374. if(tw > mw / 3)
  375. tw = mw / 3;
  376. }
  377. cmdw = title ? tw : cw;
  378. text[0] = 0;
  379. update_items(text);
  380. XMapRaised(dpy, win);
  381. draw_menu();
  382. XFlush(dpy);
  383. /* main event loop */
  384. while(!XNextEvent(dpy, &ev)) {
  385. switch (ev.type) {
  386. case KeyPress:
  387. kpress(&ev.xkey);
  388. break;
  389. case Expose:
  390. if(ev.xexpose.count == 0) {
  391. draw_menu();
  392. }
  393. break;
  394. default:
  395. break;
  396. }
  397. if(done)
  398. break;
  399. }
  400. XUngrabKeyboard(dpy, CurrentTime);
  401. XFreePixmap(dpy, brush.drawable);
  402. XFreeGC(dpy, brush.gc);
  403. XDestroyWindow(dpy, win);
  404. XCloseDisplay(dpy);
  405. return ret;
  406. }