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.

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