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.

720 lines
14 KiB

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