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.

711 lines
14 KiB

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