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.

538 lines
11 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
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
  2. * (C)opyright MMVI-MMVII 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 <X11/Xutil.h>
  13. #include <X11/keysym.h>
  14. #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
  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 char *prompt = NULL;
  24. static int mw, mh;
  25. static int ret = 0;
  26. static int nitem = 0;
  27. static unsigned int cmdw = 0;
  28. static unsigned int promptw = 0;
  29. static unsigned int numlockmask = 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 unsigned long
  103. initcolor(const char *colstr) {
  104. Colormap cmap = DefaultColormap(dpy, screen);
  105. XColor color;
  106. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  107. eprint("error, cannot allocate color '%s'\n", colstr);
  108. return color.pixel;
  109. }
  110. static void
  111. initfont(const char *fontstr) {
  112. char *def, **missing;
  113. int i, n;
  114. missing = NULL;
  115. if(dc.font.set)
  116. XFreeFontSet(dpy, dc.font.set);
  117. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  118. if(missing)
  119. XFreeStringList(missing);
  120. if(dc.font.set) {
  121. XFontSetExtents *font_extents;
  122. XFontStruct **xfonts;
  123. char **font_names;
  124. dc.font.ascent = dc.font.descent = 0;
  125. font_extents = XExtentsOfFontSet(dc.font.set);
  126. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  127. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  128. if(dc.font.ascent < (*xfonts)->ascent)
  129. dc.font.ascent = (*xfonts)->ascent;
  130. if(dc.font.descent < (*xfonts)->descent)
  131. dc.font.descent = (*xfonts)->descent;
  132. xfonts++;
  133. }
  134. }
  135. else {
  136. if(dc.font.xfont)
  137. XFreeFont(dpy, dc.font.xfont);
  138. dc.font.xfont = NULL;
  139. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
  140. eprint("error, cannot load font: '%s'\n", fontstr);
  141. dc.font.ascent = dc.font.xfont->ascent;
  142. dc.font.descent = dc.font.xfont->descent;
  143. }
  144. dc.font.height = dc.font.ascent + dc.font.descent;
  145. }
  146. static void
  147. match(char *pattern) {
  148. unsigned int plen;
  149. Item *i, *j;
  150. if(!pattern)
  151. return;
  152. plen = strlen(pattern);
  153. item = j = NULL;
  154. nitem = 0;
  155. for(i = allitems; i; i=i->next)
  156. if(!plen || !strncmp(pattern, i->text, plen)) {
  157. if(!j)
  158. item = i;
  159. else
  160. j->right = i;
  161. i->left = j;
  162. i->right = NULL;
  163. j = i;
  164. nitem++;
  165. }
  166. for(i = allitems; i; i=i->next)
  167. if(plen && strncmp(pattern, i->text, plen)
  168. && strstr(i->text, pattern)) {
  169. if(!j)
  170. item = i;
  171. else
  172. j->right = i;
  173. i->left = j;
  174. i->right = NULL;
  175. j = i;
  176. nitem++;
  177. }
  178. curr = prev = next = sel = item;
  179. calcoffsets();
  180. }
  181. static void
  182. kpress(XKeyEvent * e) {
  183. char buf[32];
  184. int i, num, prev_nitem;
  185. unsigned int len;
  186. KeySym ksym;
  187. len = strlen(text);
  188. buf[0] = 0;
  189. num = XLookupString(e, buf, sizeof buf, &ksym, 0);
  190. if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  191. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  192. || IsPrivateKeypadKey(ksym))
  193. return;
  194. /* first check if a control mask is omitted */
  195. if(e->state & ControlMask) {
  196. switch (ksym) {
  197. default: /* ignore other control sequences */
  198. return;
  199. case XK_bracketleft:
  200. ksym = XK_Escape;
  201. break;
  202. case XK_h:
  203. case XK_H:
  204. ksym = XK_BackSpace;
  205. break;
  206. case XK_i:
  207. case XK_I:
  208. ksym = XK_Tab;
  209. break;
  210. case XK_j:
  211. case XK_J:
  212. ksym = XK_Return;
  213. break;
  214. case XK_u:
  215. case XK_U:
  216. text[0] = 0;
  217. match(text);
  218. drawmenu();
  219. return;
  220. case XK_w:
  221. case XK_W:
  222. if(len) {
  223. i = len - 1;
  224. while(i >= 0 && text[i] == ' ')
  225. text[i--] = 0;
  226. while(i >= 0 && text[i] != ' ')
  227. text[i--] = 0;
  228. match(text);
  229. drawmenu();
  230. }
  231. return;
  232. }
  233. }
  234. if(CLEANMASK(e->state) & Mod1Mask) {
  235. switch(ksym) {
  236. default: return;
  237. case XK_h:
  238. ksym = XK_Left;
  239. break;
  240. case XK_l:
  241. ksym = XK_Right;
  242. break;
  243. case XK_j:
  244. ksym = XK_Next;
  245. break;
  246. case XK_k:
  247. ksym = XK_Prior;
  248. break;
  249. case XK_g:
  250. ksym = XK_Home;
  251. break;
  252. case XK_G:
  253. ksym = XK_End;
  254. break;
  255. }
  256. }
  257. switch(ksym) {
  258. default:
  259. if(num && !iscntrl((int) buf[0])) {
  260. buf[num] = 0;
  261. if(len > 0)
  262. strncat(text, buf, sizeof text);
  263. else
  264. strncpy(text, buf, sizeof text);
  265. match(text);
  266. }
  267. break;
  268. case XK_BackSpace:
  269. if((i = len)) {
  270. prev_nitem = nitem;
  271. do {
  272. text[--i] = 0;
  273. match(text);
  274. } while(i && nitem && prev_nitem == nitem);
  275. match(text);
  276. }
  277. break;
  278. case XK_End:
  279. if(!item)
  280. return;
  281. while(next) {
  282. sel = curr = next;
  283. calcoffsets();
  284. }
  285. while(sel && sel->right)
  286. sel = sel->right;
  287. break;
  288. case XK_Escape:
  289. ret = 1;
  290. running = False;
  291. break;
  292. case XK_Home:
  293. if(!item)
  294. return;
  295. sel = curr = item;
  296. calcoffsets();
  297. break;
  298. case XK_Left:
  299. if(!(sel && sel->left))
  300. return;
  301. sel=sel->left;
  302. if(sel->right == curr) {
  303. curr = prev;
  304. calcoffsets();
  305. }
  306. break;
  307. case XK_Next:
  308. if(!next)
  309. return;
  310. sel = curr = next;
  311. calcoffsets();
  312. break;
  313. case XK_Prior:
  314. if(!prev)
  315. return;
  316. sel = curr = prev;
  317. calcoffsets();
  318. break;
  319. case XK_Return:
  320. if((e->state & ShiftMask) && text)
  321. fprintf(stdout, "%s", text);
  322. else if(sel)
  323. fprintf(stdout, "%s", sel->text);
  324. else if(text)
  325. fprintf(stdout, "%s", text);
  326. fflush(stdout);
  327. running = False;
  328. break;
  329. case XK_Right:
  330. if(!(sel && sel->right))
  331. return;
  332. sel=sel->right;
  333. if(sel == next) {
  334. curr = next;
  335. calcoffsets();
  336. }
  337. break;
  338. case XK_Tab:
  339. if(!sel)
  340. return;
  341. strncpy(text, sel->text, sizeof text);
  342. match(text);
  343. break;
  344. }
  345. drawmenu();
  346. }
  347. static char *
  348. readstdin(void) {
  349. static char *maxname = NULL;
  350. char *p, buf[1024];
  351. unsigned int len = 0, max = 0;
  352. Item *i, *new;
  353. i = 0;
  354. while(fgets(buf, sizeof buf, stdin)) {
  355. len = strlen(buf);
  356. if (buf[len - 1] == '\n')
  357. buf[len - 1] = 0;
  358. p = estrdup(buf);
  359. if(max < len) {
  360. maxname = p;
  361. max = len;
  362. }
  363. new = emalloc(sizeof(Item));
  364. new->next = new->left = new->right = NULL;
  365. new->text = p;
  366. if(!i)
  367. allitems = new;
  368. else
  369. i->next = new;
  370. i = new;
  371. }
  372. return maxname;
  373. }
  374. static void
  375. usage(void) {
  376. eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
  377. " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
  378. }
  379. /* extern */
  380. int screen;
  381. Display *dpy;
  382. DC dc = {0};
  383. int
  384. main(int argc, char *argv[]) {
  385. Bool bottom = False;
  386. char *font = FONT;
  387. char *maxname;
  388. char *normbg = NORMBGCOLOR;
  389. char *normfg = NORMFGCOLOR;
  390. char *selbg = SELBGCOLOR;
  391. char *selfg = SELFGCOLOR;
  392. int i, j;
  393. Item *itm;
  394. XEvent ev;
  395. XModifierKeymap *modmap;
  396. XSetWindowAttributes wa;
  397. if(isatty(STDIN_FILENO)) {
  398. fputs("error: dmenu can't run in an interactive shell\n", stdout);
  399. usage();
  400. }
  401. /* command line args */
  402. for(i = 1; i < argc; i++)
  403. if(!strncmp(argv[i], "-b", 3)) {
  404. bottom = True;
  405. }
  406. else if(!strncmp(argv[i], "-fn", 4)) {
  407. if(++i < argc) font = argv[i];
  408. }
  409. else if(!strncmp(argv[i], "-nb", 4)) {
  410. if(++i < argc) normbg = argv[i];
  411. }
  412. else if(!strncmp(argv[i], "-nf", 4)) {
  413. if(++i < argc) normfg = argv[i];
  414. }
  415. else if(!strncmp(argv[i], "-p", 3)) {
  416. if(++i < argc) prompt = argv[i];
  417. }
  418. else if(!strncmp(argv[i], "-sb", 4)) {
  419. if(++i < argc) selbg = argv[i];
  420. }
  421. else if(!strncmp(argv[i], "-sf", 4)) {
  422. if(++i < argc) selfg = argv[i];
  423. }
  424. else if(!strncmp(argv[i], "-v", 3))
  425. eprint("dmenu-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
  426. else
  427. usage();
  428. setlocale(LC_CTYPE, "");
  429. dpy = XOpenDisplay(0);
  430. if(!dpy)
  431. eprint("dmenu: cannot open display\n");
  432. screen = DefaultScreen(dpy);
  433. root = RootWindow(dpy, screen);
  434. while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
  435. GrabModeAsync, CurrentTime) != GrabSuccess)
  436. usleep(1000);
  437. maxname = readstdin();
  438. /* init modifier map */
  439. modmap = XGetModifierMapping(dpy);
  440. for (i = 0; i < 8; i++) {
  441. for (j = 0; j < modmap->max_keypermod; j++) {
  442. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  443. == XKeysymToKeycode(dpy, XK_Num_Lock))
  444. numlockmask = (1 << i);
  445. }
  446. }
  447. XFreeModifiermap(modmap);
  448. /* style */
  449. dc.norm[ColBG] = initcolor(normbg);
  450. dc.norm[ColFG] = initcolor(normfg);
  451. dc.sel[ColBG] = initcolor(selbg);
  452. dc.sel[ColFG] = initcolor(selfg);
  453. initfont(font);
  454. /* menu window */
  455. wa.override_redirect = 1;
  456. wa.background_pixmap = ParentRelative;
  457. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  458. mw = DisplayWidth(dpy, screen);
  459. mh = dc.font.height + 2;
  460. win = XCreateWindow(dpy, root, 0,
  461. bottom ? DisplayHeight(dpy, screen) - mh : 0, mw, mh, 0,
  462. DefaultDepth(dpy, screen), CopyFromParent,
  463. DefaultVisual(dpy, screen),
  464. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  465. /* pixmap */
  466. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  467. dc.gc = XCreateGC(dpy, root, 0, 0);
  468. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  469. if(maxname)
  470. cmdw = textw(maxname);
  471. if(cmdw > mw / 3)
  472. cmdw = mw / 3;
  473. if(prompt)
  474. promptw = textw(prompt);
  475. if(promptw > mw / 5)
  476. promptw = mw / 5;
  477. text[0] = 0;
  478. match(text);
  479. XMapRaised(dpy, win);
  480. drawmenu();
  481. XSync(dpy, False);
  482. /* main event loop */
  483. while(running && !XNextEvent(dpy, &ev))
  484. switch (ev.type) {
  485. default: /* ignore all crap */
  486. break;
  487. case KeyPress:
  488. kpress(&ev.xkey);
  489. break;
  490. case Expose:
  491. if(ev.xexpose.count == 0)
  492. drawmenu();
  493. break;
  494. }
  495. /* cleanup */
  496. while(allitems) {
  497. itm = allitems->next;
  498. free(allitems->text);
  499. free(allitems);
  500. allitems = itm;
  501. }
  502. if(dc.font.set)
  503. XFreeFontSet(dpy, dc.font.set);
  504. else
  505. XFreeFont(dpy, dc.font.xfont);
  506. XFreePixmap(dpy, dc.drawable);
  507. XFreeGC(dpy, dc.gc);
  508. XDestroyWindow(dpy, win);
  509. XUngrabKeyboard(dpy, CurrentTime);
  510. XCloseDisplay(dpy);
  511. return ret;
  512. }