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.

464 lines
9.3 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
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  2. * (C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include "dmenu.h"
  6. #include <ctype.h>
  7. #include <locale.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <sys/select.h>
  13. #include <sys/time.h>
  14. #include <X11/Xutil.h>
  15. #include <X11/keysym.h>
  16. typedef struct Item Item;
  17. struct Item {
  18. Item *next; /* traverses all items */
  19. Item *left, *right; /* traverses items matching current search pattern */
  20. char *text;
  21. };
  22. /* static */
  23. static char text[4096];
  24. static char *prompt = NULL;
  25. static int mx, my, mw, mh;
  26. static int ret = 0;
  27. static int nitem = 0;
  28. static unsigned int cmdw = 0;
  29. static unsigned int promptw = 0;
  30. static Bool running = True;
  31. static Item *allitems = NULL; /* first of all items */
  32. static Item *item = NULL; /* first of pattern matching items */
  33. static Item *sel = NULL;
  34. static Item *next = NULL;
  35. static Item *prev = NULL;
  36. static Item *curr = NULL;
  37. static Window root;
  38. static Window win;
  39. static void
  40. calcoffsets(void) {
  41. unsigned int tw, w;
  42. if(!curr)
  43. return;
  44. w = promptw + cmdw + 2 * SPACE;
  45. for(next = curr; next; next=next->right) {
  46. tw = textw(next->text);
  47. if(tw > mw / 3)
  48. tw = mw / 3;
  49. w += tw;
  50. if(w > mw)
  51. break;
  52. }
  53. w = promptw + cmdw + 2 * SPACE;
  54. for(prev = curr; prev && prev->left; prev=prev->left) {
  55. tw = textw(prev->left->text);
  56. if(tw > mw / 3)
  57. tw = mw / 3;
  58. w += tw;
  59. if(w > mw)
  60. break;
  61. }
  62. }
  63. static void
  64. drawmenu(void) {
  65. Item *i;
  66. dc.x = 0;
  67. dc.y = 0;
  68. dc.w = mw;
  69. dc.h = mh;
  70. drawtext(NULL, dc.norm);
  71. /* print prompt? */
  72. if(promptw) {
  73. dc.w = promptw;
  74. drawtext(prompt, dc.sel);
  75. }
  76. dc.x += promptw;
  77. dc.w = mw - promptw;
  78. /* print command */
  79. if(cmdw && item)
  80. dc.w = cmdw;
  81. drawtext(text[0] ? text : NULL, dc.norm);
  82. dc.x += cmdw;
  83. if(curr) {
  84. dc.w = SPACE;
  85. drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
  86. dc.x += dc.w;
  87. /* determine maximum items */
  88. for(i = curr; i != next; i=i->right) {
  89. dc.w = textw(i->text);
  90. if(dc.w > mw / 3)
  91. dc.w = mw / 3;
  92. drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
  93. dc.x += dc.w;
  94. }
  95. dc.x = mw - SPACE;
  96. dc.w = SPACE;
  97. drawtext(next ? ">" : NULL, dc.norm);
  98. }
  99. XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
  100. XFlush(dpy);
  101. }
  102. static void
  103. match(char *pattern) {
  104. unsigned int plen;
  105. Item *i, *j;
  106. if(!pattern)
  107. return;
  108. plen = strlen(pattern);
  109. item = j = NULL;
  110. nitem = 0;
  111. for(i = allitems; i; i=i->next)
  112. if(!plen || !strncmp(pattern, i->text, plen)) {
  113. if(!j)
  114. item = i;
  115. else
  116. j->right = i;
  117. i->left = j;
  118. i->right = NULL;
  119. j = i;
  120. nitem++;
  121. }
  122. for(i = allitems; i; i=i->next)
  123. if(plen && strncmp(pattern, i->text, plen)
  124. && strstr(i->text, pattern)) {
  125. if(!j)
  126. item = i;
  127. else
  128. j->right = i;
  129. i->left = j;
  130. i->right = NULL;
  131. j = i;
  132. nitem++;
  133. }
  134. curr = prev = next = sel = item;
  135. calcoffsets();
  136. }
  137. static void
  138. kpress(XKeyEvent * e) {
  139. char buf[32];
  140. int num, prev_nitem;
  141. unsigned int i, len;
  142. KeySym ksym;
  143. len = strlen(text);
  144. buf[0] = 0;
  145. num = XLookupString(e, buf, sizeof buf, &ksym, 0);
  146. if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  147. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  148. || IsPrivateKeypadKey(ksym))
  149. return;
  150. /* first check if a control mask is omitted */
  151. if(e->state & ControlMask) {
  152. switch (ksym) {
  153. default: /* ignore other control sequences */
  154. return;
  155. break;
  156. case XK_h:
  157. case XK_H:
  158. ksym = XK_BackSpace;
  159. break;
  160. case XK_u:
  161. case XK_U:
  162. text[0] = 0;
  163. match(text);
  164. drawmenu();
  165. return;
  166. break;
  167. }
  168. }
  169. if(e->state & Mod1Mask) {
  170. switch(ksym) {
  171. default: return;
  172. case XK_h:
  173. ksym = XK_Left;
  174. break;
  175. case XK_l:
  176. ksym = XK_Right;
  177. break;
  178. case XK_j:
  179. ksym = XK_Next;
  180. break;
  181. case XK_k:
  182. ksym = XK_Prior;
  183. break;
  184. case XK_g:
  185. ksym = XK_Home;
  186. break;
  187. case XK_G:
  188. ksym = XK_End;
  189. break;
  190. }
  191. }
  192. switch(ksym) {
  193. default:
  194. if(num && !iscntrl((int) buf[0])) {
  195. buf[num] = 0;
  196. if(len > 0)
  197. strncat(text, buf, sizeof text);
  198. else
  199. strncpy(text, buf, sizeof text);
  200. match(text);
  201. }
  202. break;
  203. case XK_BackSpace:
  204. if((i = len)) {
  205. prev_nitem = nitem;
  206. do {
  207. text[--i] = 0;
  208. match(text);
  209. } while(i && nitem && prev_nitem == nitem);
  210. match(text);
  211. }
  212. break;
  213. case XK_End:
  214. if(!item)
  215. return;
  216. while(next) {
  217. sel = curr = next;
  218. calcoffsets();
  219. }
  220. while(sel && sel->right)
  221. sel = sel->right;
  222. break;
  223. case XK_Escape:
  224. ret = 1;
  225. running = False;
  226. break;
  227. case XK_Home:
  228. if(!item)
  229. return;
  230. sel = curr = item;
  231. calcoffsets();
  232. break;
  233. case XK_Left:
  234. if(!(sel && sel->left))
  235. return;
  236. sel=sel->left;
  237. if(sel->right == curr) {
  238. curr = prev;
  239. calcoffsets();
  240. }
  241. break;
  242. case XK_Next:
  243. if(!next)
  244. return;
  245. sel = curr = next;
  246. calcoffsets();
  247. break;
  248. case XK_Prior:
  249. if(!prev)
  250. return;
  251. sel = curr = prev;
  252. calcoffsets();
  253. break;
  254. case XK_Return:
  255. if((e->state & ShiftMask) && text)
  256. fprintf(stdout, "%s", text);
  257. else if(sel)
  258. fprintf(stdout, "%s", sel->text);
  259. else if(text)
  260. fprintf(stdout, "%s", text);
  261. fflush(stdout);
  262. running = False;
  263. break;
  264. case XK_Right:
  265. if(!(sel && sel->right))
  266. return;
  267. sel=sel->right;
  268. if(sel == next) {
  269. curr = next;
  270. calcoffsets();
  271. }
  272. break;
  273. case XK_Tab:
  274. if(!sel)
  275. return;
  276. strncpy(text, sel->text, sizeof text);
  277. match(text);
  278. break;
  279. }
  280. drawmenu();
  281. }
  282. static char *
  283. readstdin(void) {
  284. static char *maxname = NULL;
  285. char *p, buf[1024];
  286. unsigned int len = 0, max = 0;
  287. Item *i, *new;
  288. i = 0;
  289. while(fgets(buf, sizeof buf, stdin)) {
  290. len = strlen(buf);
  291. if (buf[len - 1] == '\n')
  292. buf[len - 1] = 0;
  293. p = estrdup(buf);
  294. if(max < len) {
  295. maxname = p;
  296. max = len;
  297. }
  298. new = emalloc(sizeof(Item));
  299. new->next = new->left = new->right = NULL;
  300. new->text = p;
  301. if(!i)
  302. allitems = new;
  303. else
  304. i->next = new;
  305. i = new;
  306. }
  307. return maxname;
  308. }
  309. /* extern */
  310. int screen;
  311. Display *dpy;
  312. DC dc = {0};
  313. int
  314. main(int argc, char *argv[]) {
  315. char *font = FONT;
  316. char *maxname;
  317. char *normbg = NORMBGCOLOR;
  318. char *normfg = NORMFGCOLOR;
  319. char *selbg = SELBGCOLOR;
  320. char *selfg = SELFGCOLOR;
  321. fd_set rd;
  322. int i;
  323. struct timeval timeout;
  324. Item *itm;
  325. XEvent ev;
  326. XSetWindowAttributes wa;
  327. timeout.tv_usec = 0;
  328. timeout.tv_sec = 3;
  329. /* command line args */
  330. for(i = 1; i < argc; i++)
  331. if(!strncmp(argv[i], "-font", 6)) {
  332. if(++i < argc) font = argv[i];
  333. }
  334. else if(!strncmp(argv[i], "-normbg", 8)) {
  335. if(++i < argc) normbg = argv[i];
  336. }
  337. else if(!strncmp(argv[i], "-normfg", 8)) {
  338. if(++i < argc) normfg = argv[i];
  339. }
  340. else if(!strncmp(argv[i], "-selbg", 7)) {
  341. if(++i < argc) selbg = argv[i];
  342. }
  343. else if(!strncmp(argv[i], "-selfg", 7)) {
  344. if(++i < argc) selfg = argv[i];
  345. }
  346. else if(!strncmp(argv[i], "-p", 3)) {
  347. if(++i < argc) prompt = argv[i];
  348. }
  349. else if(!strncmp(argv[i], "-t", 3)) {
  350. if(++i < argc) timeout.tv_sec = atoi(argv[i]);
  351. }
  352. else if(!strncmp(argv[i], "-v", 3)) {
  353. fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
  354. exit(EXIT_SUCCESS);
  355. }
  356. else
  357. eprint("usage: dmenu [-font <name>] [-{norm,sel}{bg,fg} <color>] [-p <prompt>] [-t <seconds>] [-v]\n", stdout);
  358. setlocale(LC_CTYPE, "");
  359. dpy = XOpenDisplay(0);
  360. if(!dpy)
  361. eprint("dmenu: cannot open display\n");
  362. screen = DefaultScreen(dpy);
  363. root = RootWindow(dpy, screen);
  364. /* Note, the select() construction allows to grab all keypresses as
  365. * early as possible, to not loose them. But if there is no standard
  366. * input supplied, we will make sure to exit after MAX_WAIT_STDIN
  367. * seconds. This is convenience behavior for rapid typers.
  368. */
  369. while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
  370. GrabModeAsync, CurrentTime) != GrabSuccess)
  371. usleep(1000);
  372. FD_ZERO(&rd);
  373. FD_SET(STDIN_FILENO, &rd);
  374. if(select(ConnectionNumber(dpy) + 1, &rd, NULL, NULL, &timeout) < 1)
  375. goto UninitializedEnd;
  376. maxname = readstdin();
  377. /* style */
  378. dc.norm[ColBG] = getcolor(normbg);
  379. dc.norm[ColFG] = getcolor(normfg);
  380. dc.sel[ColBG] = getcolor(selbg);
  381. dc.sel[ColFG] = getcolor(selfg);
  382. setfont(font);
  383. /* menu window */
  384. wa.override_redirect = 1;
  385. wa.background_pixmap = ParentRelative;
  386. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  387. mx = my = 0;
  388. mw = DisplayWidth(dpy, screen);
  389. mh = dc.font.height + 2;
  390. win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
  391. DefaultDepth(dpy, screen), CopyFromParent,
  392. DefaultVisual(dpy, screen),
  393. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  394. /* pixmap */
  395. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  396. dc.gc = XCreateGC(dpy, root, 0, 0);
  397. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  398. if(maxname)
  399. cmdw = textw(maxname);
  400. if(cmdw > mw / 3)
  401. cmdw = mw / 3;
  402. if(prompt)
  403. promptw = textw(prompt);
  404. if(promptw > mw / 5)
  405. promptw = mw / 5;
  406. text[0] = 0;
  407. match(text);
  408. XMapRaised(dpy, win);
  409. drawmenu();
  410. XSync(dpy, False);
  411. /* main event loop */
  412. while(running && !XNextEvent(dpy, &ev))
  413. switch (ev.type) {
  414. default: /* ignore all crap */
  415. break;
  416. case KeyPress:
  417. kpress(&ev.xkey);
  418. break;
  419. case Expose:
  420. if(ev.xexpose.count == 0)
  421. drawmenu();
  422. break;
  423. }
  424. /* cleanup */
  425. while(allitems) {
  426. itm = allitems->next;
  427. free(allitems->text);
  428. free(allitems);
  429. allitems = itm;
  430. }
  431. if(dc.font.set)
  432. XFreeFontSet(dpy, dc.font.set);
  433. else
  434. XFreeFont(dpy, dc.font.xfont);
  435. XFreePixmap(dpy, dc.drawable);
  436. XFreeGC(dpy, dc.gc);
  437. XDestroyWindow(dpy, win);
  438. UninitializedEnd:
  439. XUngrabKeyboard(dpy, CurrentTime);
  440. XCloseDisplay(dpy);
  441. return ret;
  442. }