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.

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