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.

386 lines
7.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
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, 0, False);
  70. /* print command */
  71. if(cmdw && item)
  72. dc.w = cmdw;
  73. drawtext(text[0] ? text : NULL, 0, False);
  74. dc.x += cmdw;
  75. if(curr) {
  76. dc.w = SPACE;
  77. drawtext((curr && curr->left) ? "<" : NULL, 0, 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 ? 1 : 0, sel == i);
  85. dc.x += dc.w;
  86. }
  87. dc.x = mw - SPACE;
  88. dc.w = SPACE;
  89. drawtext(next ? ">" : NULL, 0, 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[0] = getcolor(NORMBGCOLOR);
  283. dc.fg[0] = getcolor(NORMFGCOLOR);
  284. dc.bg[1] = getcolor(SELBGCOLOR);
  285. dc.fg[1] = getcolor(SELFGCOLOR);
  286. setfont(FONT);
  287. wa.override_redirect = 1;
  288. wa.background_pixmap = ParentRelative;
  289. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  290. mx = my = 0;
  291. mw = DisplayWidth(dpy, screen);
  292. mh = dc.font.height + 4;
  293. win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
  294. DefaultDepth(dpy, screen), CopyFromParent,
  295. DefaultVisual(dpy, screen),
  296. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  297. XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
  298. /* pixmap */
  299. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  300. dc.gc = XCreateGC(dpy, root, 0, 0);
  301. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  302. if(maxname)
  303. cmdw = textw(maxname);
  304. if(cmdw > mw / 3)
  305. cmdw = mw / 3;
  306. text[0] = 0;
  307. match(text);
  308. XMapRaised(dpy, win);
  309. drawmenu();
  310. XSync(dpy, False);
  311. /* main event loop */
  312. while(running && !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. while(allitems) {
  327. i = allitems->next;
  328. free(allitems->text);
  329. free(allitems);
  330. allitems = i;
  331. }
  332. if(dc.font.set)
  333. XFreeFontSet(dpy, dc.font.set);
  334. else
  335. XFreeFont(dpy, dc.font.xfont);
  336. XFreePixmap(dpy, dc.drawable);
  337. XFreeGC(dpy, dc.gc);
  338. XDestroyWindow(dpy, win);
  339. XCloseDisplay(dpy);
  340. return ret;
  341. }