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.

385 lines
7.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 "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 running = True;
  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. case XK_H:
  152. ksym = XK_BackSpace;
  153. break;
  154. case XK_u:
  155. case XK_U:
  156. text[0] = 0;
  157. match(text);
  158. drawmenu();
  159. return;
  160. break;
  161. }
  162. }
  163. switch(ksym) {
  164. case XK_Left:
  165. if(!(sel && sel->left))
  166. return;
  167. sel=sel->left;
  168. if(sel->right == curr) {
  169. curr = prev;
  170. calcoffsets();
  171. }
  172. break;
  173. case XK_Tab:
  174. if(!sel)
  175. return;
  176. strncpy(text, sel->text, sizeof(text));
  177. match(text);
  178. break;
  179. case XK_Right:
  180. if(!(sel && sel->right))
  181. return;
  182. sel=sel->right;
  183. if(sel == next) {
  184. curr = next;
  185. calcoffsets();
  186. }
  187. break;
  188. case XK_Return:
  189. if(e->state & ShiftMask) {
  190. if(text)
  191. fprintf(stdout, "%s", text);
  192. }
  193. else if(sel)
  194. fprintf(stdout, "%s", sel->text);
  195. else if(text)
  196. fprintf(stdout, "%s", text);
  197. fflush(stdout);
  198. running = False;
  199. break;
  200. case XK_Escape:
  201. ret = 1;
  202. running = False;
  203. break;
  204. case XK_BackSpace:
  205. if((i = len)) {
  206. prev_nitem = nitem;
  207. do {
  208. text[--i] = 0;
  209. match(text);
  210. } while(i && nitem && prev_nitem == nitem);
  211. match(text);
  212. }
  213. break;
  214. default:
  215. if(num && !iscntrl((int) buf[0])) {
  216. buf[num] = 0;
  217. if(len > 0)
  218. strncat(text, buf, sizeof(text));
  219. else
  220. strncpy(text, buf, sizeof(text));
  221. match(text);
  222. }
  223. }
  224. drawmenu();
  225. }
  226. static char *
  227. readstdin()
  228. {
  229. static char *maxname = NULL;
  230. char *p, buf[1024];
  231. unsigned int len = 0, max = 0;
  232. Item *i, *new;
  233. i = 0;
  234. while(fgets(buf, sizeof(buf), stdin)) {
  235. len = strlen(buf);
  236. if (buf[len - 1] == '\n')
  237. buf[len - 1] = 0;
  238. p = estrdup(buf);
  239. if(max < len) {
  240. maxname = p;
  241. max = len;
  242. }
  243. new = emalloc(sizeof(Item));
  244. new->next = new->left = new->right = NULL;
  245. new->text = p;
  246. if(!i)
  247. allitems = new;
  248. else
  249. i->next = new;
  250. i = new;
  251. }
  252. return maxname;
  253. }
  254. /* extern */
  255. int screen;
  256. Display *dpy;
  257. DC dc = {0};
  258. int
  259. main(int argc, char *argv[])
  260. {
  261. char *maxname;
  262. Item *i;
  263. XEvent ev;
  264. XSetWindowAttributes wa;
  265. if(argc == 2 && !strncmp("-v", argv[1], 3)) {
  266. fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
  267. exit(EXIT_SUCCESS);
  268. }
  269. else if(argc != 1)
  270. eprint("usage: dmenu [-v]\n");
  271. dpy = XOpenDisplay(0);
  272. if(!dpy)
  273. eprint("dmenu: cannot open display\n");
  274. screen = DefaultScreen(dpy);
  275. root = RootWindow(dpy, screen);
  276. maxname = readstdin();
  277. /* grab as early as possible, but after reading all items!!! */
  278. while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
  279. GrabModeAsync, CurrentTime) != GrabSuccess)
  280. usleep(1000);
  281. /* style */
  282. dc.bg = getcolor(BGCOLOR);
  283. dc.fg = getcolor(FGCOLOR);
  284. dc.border = getcolor(BORDERCOLOR);
  285. setfont(FONT);
  286. wa.override_redirect = 1;
  287. wa.background_pixmap = ParentRelative;
  288. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  289. mx = my = 0;
  290. mw = DisplayWidth(dpy, screen);
  291. mh = dc.font.height + 4;
  292. win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
  293. DefaultDepth(dpy, screen), CopyFromParent,
  294. DefaultVisual(dpy, screen),
  295. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  296. XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
  297. /* pixmap */
  298. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  299. dc.gc = XCreateGC(dpy, root, 0, 0);
  300. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  301. if(maxname)
  302. cmdw = textw(maxname);
  303. if(cmdw > mw / 3)
  304. cmdw = mw / 3;
  305. text[0] = 0;
  306. match(text);
  307. XMapRaised(dpy, win);
  308. drawmenu();
  309. XSync(dpy, False);
  310. /* main event loop */
  311. while(running && !XNextEvent(dpy, &ev)) {
  312. switch (ev.type) {
  313. case KeyPress:
  314. kpress(&ev.xkey);
  315. break;
  316. case Expose:
  317. if(ev.xexpose.count == 0)
  318. drawmenu();
  319. break;
  320. default:
  321. break;
  322. }
  323. }
  324. XUngrabKeyboard(dpy, CurrentTime);
  325. while(allitems) {
  326. i = allitems->next;
  327. free(allitems->text);
  328. free(allitems);
  329. allitems = i;
  330. }
  331. if(dc.font.set)
  332. XFreeFontSet(dpy, dc.font.set);
  333. else
  334. XFreeFont(dpy, dc.font.xfont);
  335. XFreePixmap(dpy, dc.drawable);
  336. XFreeGC(dpy, dc.gc);
  337. XDestroyWindow(dpy, win);
  338. XCloseDisplay(dpy);
  339. return ret;
  340. }