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.

373 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
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. 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. done = True;
  199. break;
  200. case XK_Escape:
  201. ret = 1;
  202. done = True;
  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. XEvent ev;
  263. XSetWindowAttributes wa;
  264. if(argc == 2 && !strncmp("-v", argv[1], 3)) {
  265. fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
  266. exit(EXIT_SUCCESS);
  267. }
  268. else if(argc != 1)
  269. eprint("usage: dmenu [-v]\n");
  270. dpy = XOpenDisplay(0);
  271. if(!dpy)
  272. eprint("dmenu: cannot open display\n");
  273. screen = DefaultScreen(dpy);
  274. root = RootWindow(dpy, screen);
  275. maxname = readstdin();
  276. /* grab as early as possible, but after reading all items!!! */
  277. while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
  278. GrabModeAsync, CurrentTime) != GrabSuccess)
  279. usleep(1000);
  280. /* style */
  281. dc.bg = getcolor(BGCOLOR);
  282. dc.fg = getcolor(FGCOLOR);
  283. dc.border = getcolor(BORDERCOLOR);
  284. setfont(FONT);
  285. wa.override_redirect = 1;
  286. wa.background_pixmap = ParentRelative;
  287. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  288. mx = my = 0;
  289. mw = DisplayWidth(dpy, screen);
  290. mh = dc.font.height + 4;
  291. win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
  292. DefaultDepth(dpy, screen), CopyFromParent,
  293. DefaultVisual(dpy, screen),
  294. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  295. XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
  296. /* pixmap */
  297. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  298. dc.gc = XCreateGC(dpy, root, 0, 0);
  299. if(maxname)
  300. cmdw = textw(maxname);
  301. if(cmdw > mw / 3)
  302. cmdw = mw / 3;
  303. text[0] = 0;
  304. match(text);
  305. XMapRaised(dpy, win);
  306. drawmenu();
  307. XSync(dpy, False);
  308. /* main event loop */
  309. while(!done && !XNextEvent(dpy, &ev)) {
  310. switch (ev.type) {
  311. case KeyPress:
  312. kpress(&ev.xkey);
  313. break;
  314. case Expose:
  315. if(ev.xexpose.count == 0)
  316. drawmenu();
  317. break;
  318. default:
  319. break;
  320. }
  321. }
  322. XUngrabKeyboard(dpy, CurrentTime);
  323. XFreePixmap(dpy, dc.drawable);
  324. XFreeGC(dpy, dc.gc);
  325. XDestroyWindow(dpy, win);
  326. XCloseDisplay(dpy);
  327. return ret;
  328. }