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.

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