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.

473 lines
9.1 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
  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 = 0; /* first of all items */
  32. static Item *item = 0; /* first of pattern matching items */
  33. static Item *sel = 0;
  34. static Item *nextoff = 0;
  35. static Item *prevoff = 0;
  36. static Item *curroff = 0;
  37. static int screen;
  38. static char *title = 0;
  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 = 0;
  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 = 0;
  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 = 0;
  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 == 1) && !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 = 0;
  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 = 0;
  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. char buf[256];
  322. fputs(STATUSCMD, stdout);
  323. fputs("\n", stdout);
  324. pipe_spawn(buf, sizeof(buf), NULL, STATUSCMD);
  325. fputs(buf, stderr);
  326. return 0;
  327. /* command line args */
  328. for(i = 1; i < argc; i++) {
  329. if (argv[i][0] == '-')
  330. switch (argv[i][1]) {
  331. case 'v':
  332. fprintf(stdout, "%s", version);
  333. exit(0);
  334. break;
  335. case 't':
  336. if(++i < argc)
  337. title = argv[i];
  338. else
  339. usage();
  340. break;
  341. default:
  342. usage();
  343. break;
  344. }
  345. else
  346. usage();
  347. }
  348. dpy = XOpenDisplay(0);
  349. if(!dpy)
  350. error("gridmenu: cannot open dpy\n");
  351. screen = DefaultScreen(dpy);
  352. root = RootWindow(dpy, screen);
  353. maxname = read_allitems();
  354. /* grab as early as possible, but after reading all items!!! */
  355. while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
  356. GrabModeAsync, CurrentTime) != GrabSuccess)
  357. usleep(1000);
  358. /* style */
  359. loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
  360. loadfont(dpy, &brush.font, FONT);
  361. wa.override_redirect = 1;
  362. wa.background_pixmap = ParentRelative;
  363. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  364. rect.width = DisplayWidth(dpy, screen);
  365. rect.height = labelheight(&brush.font);
  366. rect.y = DisplayHeight(dpy, screen) - rect.height;
  367. rect.x = 0;
  368. win = XCreateWindow(dpy, root, rect.x, rect.y,
  369. rect.width, rect.height, 0, DefaultDepth(dpy, screen),
  370. CopyFromParent, DefaultVisual(dpy, screen),
  371. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  372. XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
  373. XFlush(dpy);
  374. /* pixmap */
  375. brush.gc = XCreateGC(dpy, root, 0, 0);
  376. brush.drawable = XCreatePixmap(dpy, win, rect.width, rect.height,
  377. DefaultDepth(dpy, screen));
  378. XFlush(dpy);
  379. if(maxname)
  380. cwidth = textwidth(&brush.font, maxname) + brush.font.height;
  381. if(cwidth > rect.width / 3)
  382. cwidth = rect.width / 3;
  383. if(title) {
  384. twidth = textwidth(&brush.font, title) + brush.font.height;
  385. if(twidth > rect.width / 3)
  386. twidth = rect.width / 3;
  387. }
  388. cmdw = title ? twidth : cwidth;
  389. text[0] = 0;
  390. update_items(text);
  391. XMapRaised(dpy, win);
  392. draw_menu();
  393. XFlush(dpy);
  394. /* main event loop */
  395. while(!XNextEvent(dpy, &ev)) {
  396. switch (ev.type) {
  397. case KeyPress:
  398. kpress(&ev.xkey);
  399. break;
  400. case Expose:
  401. if(ev.xexpose.count == 0) {
  402. draw_menu();
  403. }
  404. break;
  405. default:
  406. break;
  407. }
  408. if(done)
  409. break;
  410. }
  411. XUngrabKeyboard(dpy, CurrentTime);
  412. XFreePixmap(dpy, brush.drawable);
  413. XFreeGC(dpy, brush.gc);
  414. XDestroyWindow(dpy, win);
  415. XCloseDisplay(dpy);
  416. return ret;
  417. }