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.

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