Configuration file for DWM on MacBook Air
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.

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