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.

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