My patched version of suckless' terminal - st.
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.

2165 lines
48 KiB

9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
  1. /* See LICENSE for license details. */
  2. #include <errno.h>
  3. #include <math.h>
  4. #include <limits.h>
  5. #include <locale.h>
  6. #include <signal.h>
  7. #include <sys/select.h>
  8. #include <time.h>
  9. #include <unistd.h>
  10. #include <libgen.h>
  11. #include <X11/Xatom.h>
  12. #include <X11/Xlib.h>
  13. #include <X11/cursorfont.h>
  14. #include <X11/keysym.h>
  15. #include <X11/Xft/Xft.h>
  16. #include <X11/XKBlib.h>
  17. static char *argv0;
  18. #include "arg.h"
  19. #include "st.h"
  20. #include "win.h"
  21. /* types used in config.h */
  22. typedef struct {
  23. uint mod;
  24. KeySym keysym;
  25. void (*func)(const Arg *);
  26. const Arg arg;
  27. } Shortcut;
  28. typedef struct {
  29. uint mod;
  30. uint button;
  31. void (*func)(const Arg *);
  32. const Arg arg;
  33. uint release;
  34. } MouseShortcut;
  35. typedef struct {
  36. KeySym k;
  37. uint mask;
  38. char *s;
  39. /* three-valued logic variables: 0 indifferent, 1 on, -1 off */
  40. signed char appkey; /* application keypad */
  41. signed char appcursor; /* application cursor */
  42. } Key;
  43. /* X modifiers */
  44. #define XK_ANY_MOD UINT_MAX
  45. #define XK_NO_MOD 0
  46. #define XK_SWITCH_MOD (1<<13)
  47. /* function definitions used in config.h */
  48. static void clipcopy(const Arg *);
  49. static void clippaste(const Arg *);
  50. static void numlock(const Arg *);
  51. static void selpaste(const Arg *);
  52. static void zoom(const Arg *);
  53. static void zoomabs(const Arg *);
  54. static void zoomreset(const Arg *);
  55. static void ttysend(const Arg *);
  56. /* config.h for applying patches and the configuration. */
  57. #include "config.h"
  58. /* XEMBED messages */
  59. #define XEMBED_FOCUS_IN 4
  60. #define XEMBED_FOCUS_OUT 5
  61. /* macros */
  62. #define IS_SET(flag) ((win.mode & (flag)) != 0)
  63. #define TRUERED(x) (((x) & 0xff0000) >> 8)
  64. #define TRUEGREEN(x) (((x) & 0xff00))
  65. #define TRUEBLUE(x) (((x) & 0xff) << 8)
  66. typedef XftDraw *Draw;
  67. typedef XftColor Color;
  68. typedef XftGlyphFontSpec GlyphFontSpec;
  69. /* Purely graphic info */
  70. typedef struct {
  71. int tw, th; /* tty width and height */
  72. int w, h; /* window width and height */
  73. int hborderpx, vborderpx;
  74. int ch; /* char height */
  75. int cw; /* char width */
  76. int mode; /* window state/mode flags */
  77. int cursor; /* cursor style */
  78. } TermWindow;
  79. typedef struct {
  80. Display *dpy;
  81. Colormap cmap;
  82. Window win;
  83. Drawable buf;
  84. GlyphFontSpec *specbuf; /* font spec buffer used for rendering */
  85. Atom xembed, wmdeletewin, netwmname, netwmpid;
  86. struct {
  87. XIM xim;
  88. XIC xic;
  89. XPoint spot;
  90. XVaNestedList spotlist;
  91. } ime;
  92. Draw draw;
  93. Visual *vis;
  94. XSetWindowAttributes attrs;
  95. int scr;
  96. int isfixed; /* is fixed geometry? */
  97. int depth; /* bit depth */
  98. int l, t; /* left and top offset */
  99. int gm; /* geometry mask */
  100. } XWindow;
  101. typedef struct {
  102. Atom xtarget;
  103. char *primary, *clipboard;
  104. struct timespec tclick1;
  105. struct timespec tclick2;
  106. } XSelection;
  107. /* Font structure */
  108. #define Font Font_
  109. typedef struct {
  110. int height;
  111. int width;
  112. int ascent;
  113. int descent;
  114. int badslant;
  115. int badweight;
  116. short lbearing;
  117. short rbearing;
  118. XftFont *match;
  119. FcFontSet *set;
  120. FcPattern *pattern;
  121. } Font;
  122. /* Drawing Context */
  123. typedef struct {
  124. Color *col;
  125. size_t collen;
  126. Font font, bfont, ifont, ibfont;
  127. GC gc;
  128. } DC;
  129. static inline ushort sixd_to_16bit(int);
  130. static int xmakeglyphfontspecs(XftGlyphFontSpec *, const Glyph *, int, int, int);
  131. static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int);
  132. static void xdrawglyph(Glyph, int, int);
  133. static void xclear(int, int, int, int);
  134. static int xgeommasktogravity(int);
  135. static int ximopen(Display *);
  136. static void ximinstantiate(Display *, XPointer, XPointer);
  137. static void ximdestroy(XIM, XPointer, XPointer);
  138. static int xicdestroy(XIC, XPointer, XPointer);
  139. static void xinit(int, int);
  140. static void cresize(int, int);
  141. static void xresize(int, int);
  142. static void xhints(void);
  143. static int xloadcolor(int, const char *, Color *);
  144. static int xloadfont(Font *, FcPattern *);
  145. static void xloadfonts(char *, double);
  146. static int xloadsparefont(FcPattern *, int);
  147. static void xloadsparefonts(void);
  148. static void xunloadfont(Font *);
  149. static void xunloadfonts(void);
  150. static void xsetenv(void);
  151. static void xseturgency(int);
  152. static int evcol(XEvent *);
  153. static int evrow(XEvent *);
  154. static void expose(XEvent *);
  155. static void visibility(XEvent *);
  156. static void unmap(XEvent *);
  157. static void kpress(XEvent *);
  158. static void cmessage(XEvent *);
  159. static void resize(XEvent *);
  160. static void focus(XEvent *);
  161. static int mouseaction(XEvent *, uint);
  162. static void brelease(XEvent *);
  163. static void bpress(XEvent *);
  164. static void bmotion(XEvent *);
  165. static void propnotify(XEvent *);
  166. static void selnotify(XEvent *);
  167. static void selclear_(XEvent *);
  168. static void selrequest(XEvent *);
  169. static void setsel(char *, Time);
  170. static void mousesel(XEvent *, int);
  171. static void mousereport(XEvent *);
  172. static char *kmap(KeySym, uint);
  173. static int match(uint, uint);
  174. static void run(void);
  175. static void usage(void);
  176. static void (*handler[LASTEvent])(XEvent *) = {
  177. [KeyPress] = kpress,
  178. [ClientMessage] = cmessage,
  179. [ConfigureNotify] = resize,
  180. [VisibilityNotify] = visibility,
  181. [UnmapNotify] = unmap,
  182. [Expose] = expose,
  183. [FocusIn] = focus,
  184. [FocusOut] = focus,
  185. [MotionNotify] = bmotion,
  186. [ButtonPress] = bpress,
  187. [ButtonRelease] = brelease,
  188. /*
  189. * Uncomment if you want the selection to disappear when you select something
  190. * different in another window.
  191. */
  192. /* [SelectionClear] = selclear_, */
  193. [SelectionNotify] = selnotify,
  194. /*
  195. * PropertyNotify is only turned on when there is some INCR transfer happening
  196. * for the selection retrieval.
  197. */
  198. [PropertyNotify] = propnotify,
  199. [SelectionRequest] = selrequest,
  200. };
  201. /* Globals */
  202. static DC dc;
  203. static XWindow xw;
  204. static XSelection xsel;
  205. static TermWindow win;
  206. /* Font Ring Cache */
  207. enum {
  208. FRC_NORMAL,
  209. FRC_ITALIC,
  210. FRC_BOLD,
  211. FRC_ITALICBOLD
  212. };
  213. typedef struct {
  214. XftFont *font;
  215. int flags;
  216. Rune unicodep;
  217. } Fontcache;
  218. /* Fontcache is an array now. A new font will be appended to the array. */
  219. static Fontcache *frc = NULL;
  220. static int frclen = 0;
  221. static int frccap = 0;
  222. static char *usedfont = NULL;
  223. static double usedfontsize = 0;
  224. static double defaultfontsize = 0;
  225. static char *opt_alpha = NULL;
  226. static char *opt_class = NULL;
  227. static char **opt_cmd = NULL;
  228. static char *opt_embed = NULL;
  229. static char *opt_font = NULL;
  230. static char *opt_io = NULL;
  231. static char *opt_line = NULL;
  232. static char *opt_name = NULL;
  233. static char *opt_title = NULL;
  234. static int oldbutton = 3; /* button event on startup: 3 = release */
  235. void
  236. clipcopy(const Arg *dummy)
  237. {
  238. Atom clipboard;
  239. free(xsel.clipboard);
  240. xsel.clipboard = NULL;
  241. if (xsel.primary != NULL) {
  242. xsel.clipboard = xstrdup(xsel.primary);
  243. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  244. XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
  245. }
  246. }
  247. void
  248. clippaste(const Arg *dummy)
  249. {
  250. Atom clipboard;
  251. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  252. XConvertSelection(xw.dpy, clipboard, xsel.xtarget, clipboard,
  253. xw.win, CurrentTime);
  254. }
  255. void
  256. selpaste(const Arg *dummy)
  257. {
  258. XConvertSelection(xw.dpy, XA_PRIMARY, xsel.xtarget, XA_PRIMARY,
  259. xw.win, CurrentTime);
  260. }
  261. void
  262. numlock(const Arg *dummy)
  263. {
  264. win.mode ^= MODE_NUMLOCK;
  265. }
  266. void
  267. zoom(const Arg *arg)
  268. {
  269. Arg larg;
  270. larg.f = usedfontsize + arg->f;
  271. zoomabs(&larg);
  272. }
  273. void
  274. zoomabs(const Arg *arg)
  275. {
  276. xunloadfonts();
  277. xloadfonts(usedfont, arg->f);
  278. xloadsparefonts();
  279. cresize(0, 0);
  280. redraw();
  281. xhints();
  282. }
  283. void
  284. zoomreset(const Arg *arg)
  285. {
  286. Arg larg;
  287. if (defaultfontsize > 0) {
  288. larg.f = defaultfontsize;
  289. zoomabs(&larg);
  290. }
  291. }
  292. void
  293. ttysend(const Arg *arg)
  294. {
  295. ttywrite(arg->s, strlen(arg->s), 1);
  296. }
  297. int
  298. evcol(XEvent *e)
  299. {
  300. int x = e->xbutton.x - win.hborderpx;
  301. LIMIT(x, 0, win.tw - 1);
  302. return x / win.cw;
  303. }
  304. int
  305. evrow(XEvent *e)
  306. {
  307. int y = e->xbutton.y - win.vborderpx;
  308. LIMIT(y, 0, win.th - 1);
  309. return y / win.ch;
  310. }
  311. void
  312. mousesel(XEvent *e, int done)
  313. {
  314. int type, seltype = SEL_REGULAR;
  315. uint state = e->xbutton.state & ~(Button1Mask | forcemousemod);
  316. for (type = 1; type < LEN(selmasks); ++type) {
  317. if (match(selmasks[type], state)) {
  318. seltype = type;
  319. break;
  320. }
  321. }
  322. selextend(evcol(e), evrow(e), seltype, done);
  323. if (done)
  324. setsel(getsel(), e->xbutton.time);
  325. }
  326. void
  327. mousereport(XEvent *e)
  328. {
  329. int len, x = evcol(e), y = evrow(e),
  330. button = e->xbutton.button, state = e->xbutton.state;
  331. char buf[40];
  332. static int ox, oy;
  333. /* from urxvt */
  334. if (e->xbutton.type == MotionNotify) {
  335. if (x == ox && y == oy)
  336. return;
  337. if (!IS_SET(MODE_MOUSEMOTION) && !IS_SET(MODE_MOUSEMANY))
  338. return;
  339. /* MOUSE_MOTION: no reporting if no button is pressed */
  340. if (IS_SET(MODE_MOUSEMOTION) && oldbutton == 3)
  341. return;
  342. button = oldbutton + 32;
  343. ox = x;
  344. oy = y;
  345. } else {
  346. if (!IS_SET(MODE_MOUSESGR) && e->xbutton.type == ButtonRelease) {
  347. button = 3;
  348. } else {
  349. button -= Button1;
  350. if (button >= 3)
  351. button += 64 - 3;
  352. }
  353. if (e->xbutton.type == ButtonPress) {
  354. oldbutton = button;
  355. ox = x;
  356. oy = y;
  357. } else if (e->xbutton.type == ButtonRelease) {
  358. oldbutton = 3;
  359. /* MODE_MOUSEX10: no button release reporting */
  360. if (IS_SET(MODE_MOUSEX10))
  361. return;
  362. if (button == 64 || button == 65)
  363. return;
  364. }
  365. }
  366. if (!IS_SET(MODE_MOUSEX10)) {
  367. button += ((state & ShiftMask ) ? 4 : 0)
  368. + ((state & Mod4Mask ) ? 8 : 0)
  369. + ((state & ControlMask) ? 16 : 0);
  370. }
  371. if (IS_SET(MODE_MOUSESGR)) {
  372. len = snprintf(buf, sizeof(buf), "\033[<%d;%d;%d%c",
  373. button, x+1, y+1,
  374. e->xbutton.type == ButtonRelease ? 'm' : 'M');
  375. } else if (x < 223 && y < 223) {
  376. len = snprintf(buf, sizeof(buf), "\033[M%c%c%c",
  377. 32+button, 32+x+1, 32+y+1);
  378. } else {
  379. return;
  380. }
  381. ttywrite(buf, len, 0);
  382. }
  383. int
  384. mouseaction(XEvent *e, uint release)
  385. {
  386. MouseShortcut *ms;
  387. for (ms = mshortcuts; ms < mshortcuts + LEN(mshortcuts); ms++) {
  388. if (ms->release == release &&
  389. ms->button == e->xbutton.button &&
  390. (match(ms->mod, e->xbutton.state) || /* exact or forced */
  391. match(ms->mod, e->xbutton.state & ~forcemousemod))) {
  392. ms->func(&(ms->arg));
  393. return 1;
  394. }
  395. }
  396. return 0;
  397. }
  398. void
  399. bpress(XEvent *e)
  400. {
  401. struct timespec now;
  402. int snap;
  403. if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
  404. mousereport(e);
  405. return;
  406. }
  407. if (mouseaction(e, 0))
  408. return;
  409. if (e->xbutton.button == Button1) {
  410. /*
  411. * If the user clicks below predefined timeouts specific
  412. * snapping behaviour is exposed.
  413. */
  414. clock_gettime(CLOCK_MONOTONIC, &now);
  415. if (TIMEDIFF(now, xsel.tclick2) <= tripleclicktimeout) {
  416. snap = SNAP_LINE;
  417. } else if (TIMEDIFF(now, xsel.tclick1) <= doubleclicktimeout) {
  418. snap = SNAP_WORD;
  419. } else {
  420. snap = 0;
  421. }
  422. xsel.tclick2 = xsel.tclick1;
  423. xsel.tclick1 = now;
  424. selstart(evcol(e), evrow(e), snap);
  425. }
  426. }
  427. void
  428. propnotify(XEvent *e)
  429. {
  430. XPropertyEvent *xpev;
  431. Atom clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  432. xpev = &e->xproperty;
  433. if (xpev->state == PropertyNewValue &&
  434. (xpev->atom == XA_PRIMARY ||
  435. xpev->atom == clipboard)) {
  436. selnotify(e);
  437. }
  438. }
  439. void
  440. selnotify(XEvent *e)
  441. {
  442. ulong nitems, ofs, rem;
  443. int format;
  444. uchar *data, *last, *repl;
  445. Atom type, incratom, property = None;
  446. incratom = XInternAtom(xw.dpy, "INCR", 0);
  447. ofs = 0;
  448. if (e->type == SelectionNotify)
  449. property = e->xselection.property;
  450. else if (e->type == PropertyNotify)
  451. property = e->xproperty.atom;
  452. if (property == None)
  453. return;
  454. do {
  455. if (XGetWindowProperty(xw.dpy, xw.win, property, ofs,
  456. BUFSIZ/4, False, AnyPropertyType,
  457. &type, &format, &nitems, &rem,
  458. &data)) {
  459. fprintf(stderr, "Clipboard allocation failed\n");
  460. return;
  461. }
  462. if (e->type == PropertyNotify && nitems == 0 && rem == 0) {
  463. /*
  464. * If there is some PropertyNotify with no data, then
  465. * this is the signal of the selection owner that all
  466. * data has been transferred. We won't need to receive
  467. * PropertyNotify events anymore.
  468. */
  469. MODBIT(xw.attrs.event_mask, 0, PropertyChangeMask);
  470. XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
  471. &xw.attrs);
  472. }
  473. if (type == incratom) {
  474. /*
  475. * Activate the PropertyNotify events so we receive
  476. * when the selection owner does send us the next
  477. * chunk of data.
  478. */
  479. MODBIT(xw.attrs.event_mask, 1, PropertyChangeMask);
  480. XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
  481. &xw.attrs);
  482. /*
  483. * Deleting the property is the transfer start signal.
  484. */
  485. XDeleteProperty(xw.dpy, xw.win, (int)property);
  486. continue;
  487. }
  488. /*
  489. * As seen in getsel:
  490. * Line endings are inconsistent in the terminal and GUI world
  491. * copy and pasting. When receiving some selection data,
  492. * replace all '\n' with '\r'.
  493. * FIXME: Fix the computer world.
  494. */
  495. repl = data;
  496. last = data + nitems * format / 8;
  497. while ((repl = memchr(repl, '\n', last - repl))) {
  498. *repl++ = '\r';
  499. }
  500. if (IS_SET(MODE_BRCKTPASTE) && ofs == 0)
  501. ttywrite("\033[200~", 6, 0);
  502. ttywrite((char *)data, nitems * format / 8, 1);
  503. if (IS_SET(MODE_BRCKTPASTE) && rem == 0)
  504. ttywrite("\033[201~", 6, 0);
  505. XFree(data);
  506. /* number of 32-bit chunks returned */
  507. ofs += nitems * format / 32;
  508. } while (rem > 0);
  509. /*
  510. * Deleting the property again tells the selection owner to send the
  511. * next data chunk in the property.
  512. */
  513. XDeleteProperty(xw.dpy, xw.win, (int)property);
  514. }
  515. void
  516. xclipcopy(void)
  517. {
  518. clipcopy(NULL);
  519. }
  520. void
  521. selclear_(XEvent *e)
  522. {
  523. selclear();
  524. }
  525. void
  526. selrequest(XEvent *e)
  527. {
  528. XSelectionRequestEvent *xsre;
  529. XSelectionEvent xev;
  530. Atom xa_targets, string, clipboard;
  531. char *seltext;
  532. xsre = (XSelectionRequestEvent *) e;
  533. xev.type = SelectionNotify;
  534. xev.requestor = xsre->requestor;
  535. xev.selection = xsre->selection;
  536. xev.target = xsre->target;
  537. xev.time = xsre->time;
  538. if (xsre->property == None)
  539. xsre->property = xsre->target;
  540. /* reject */
  541. xev.property = None;
  542. xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
  543. if (xsre->target == xa_targets) {
  544. /* respond with the supported type */
  545. string = xsel.xtarget;
  546. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  547. XA_ATOM, 32, PropModeReplace,
  548. (uchar *) &string, 1);
  549. xev.property = xsre->property;
  550. } else if (xsre->target == xsel.xtarget || xsre->target == XA_STRING) {
  551. /*
  552. * xith XA_STRING non ascii characters may be incorrect in the
  553. * requestor. It is not our problem, use utf8.
  554. */
  555. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  556. if (xsre->selection == XA_PRIMARY) {
  557. seltext = xsel.primary;
  558. } else if (xsre->selection == clipboard) {
  559. seltext = xsel.clipboard;
  560. } else {
  561. fprintf(stderr,
  562. "Unhandled clipboard selection 0x%lx\n",
  563. xsre->selection);
  564. return;
  565. }
  566. if (seltext != NULL) {
  567. XChangeProperty(xsre->display, xsre->requestor,
  568. xsre->property, xsre->target,
  569. 8, PropModeReplace,
  570. (uchar *)seltext, strlen(seltext));
  571. xev.property = xsre->property;
  572. }
  573. }
  574. /* all done, send a notification to the listener */
  575. if (!XSendEvent(xsre->display, xsre->requestor, 1, 0, (XEvent *) &xev))
  576. fprintf(stderr, "Error sending SelectionNotify event\n");
  577. }
  578. void
  579. setsel(char *str, Time t)
  580. {
  581. if (!str)
  582. return;
  583. free(xsel.primary);
  584. xsel.primary = str;
  585. XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, t);
  586. if (XGetSelectionOwner(xw.dpy, XA_PRIMARY) != xw.win)
  587. selclear();
  588. }
  589. void
  590. xsetsel(char *str)
  591. {
  592. setsel(str, CurrentTime);
  593. }
  594. void
  595. brelease(XEvent *e)
  596. {
  597. if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
  598. mousereport(e);
  599. return;
  600. }
  601. if (mouseaction(e, 1))
  602. return;
  603. if (e->xbutton.button == Button1)
  604. mousesel(e, 1);
  605. }
  606. void
  607. bmotion(XEvent *e)
  608. {
  609. if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
  610. mousereport(e);
  611. return;
  612. }
  613. mousesel(e, 0);
  614. }
  615. void
  616. cresize(int width, int height)
  617. {
  618. int col, row;
  619. if (width != 0)
  620. win.w = width;
  621. if (height != 0)
  622. win.h = height;
  623. col = (win.w - 2 * borderpx) / win.cw;
  624. row = (win.h - 2 * borderpx) / win.ch;
  625. col = MAX(1, col);
  626. row = MAX(1, row);
  627. win.hborderpx = (win.w - col * win.cw) / 2;
  628. win.vborderpx = (win.h - row * win.ch) / 2;
  629. tresize(col, row);
  630. xresize(col, row);
  631. ttyresize(win.tw, win.th);
  632. }
  633. void
  634. xresize(int col, int row)
  635. {
  636. win.tw = col * win.cw;
  637. win.th = row * win.ch;
  638. XFreePixmap(xw.dpy, xw.buf);
  639. xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
  640. xw.depth);
  641. XftDrawChange(xw.draw, xw.buf);
  642. xclear(0, 0, win.w, win.h);
  643. /* resize to new width */
  644. xw.specbuf = xrealloc(xw.specbuf, col * sizeof(GlyphFontSpec));
  645. }
  646. ushort
  647. sixd_to_16bit(int x)
  648. {
  649. return x == 0 ? 0 : 0x3737 + 0x2828 * x;
  650. }
  651. int
  652. xloadcolor(int i, const char *name, Color *ncolor)
  653. {
  654. XRenderColor color = { .alpha = 0xffff };
  655. if (!name) {
  656. if (BETWEEN(i, 16, 255)) { /* 256 color */
  657. if (i < 6*6*6+16) { /* same colors as xterm */
  658. color.red = sixd_to_16bit( ((i-16)/36)%6 );
  659. color.green = sixd_to_16bit( ((i-16)/6) %6 );
  660. color.blue = sixd_to_16bit( ((i-16)/1) %6 );
  661. } else { /* greyscale */
  662. color.red = 0x0808 + 0x0a0a * (i - (6*6*6+16));
  663. color.green = color.blue = color.red;
  664. }
  665. return XftColorAllocValue(xw.dpy, xw.vis,
  666. xw.cmap, &color, ncolor);
  667. } else
  668. name = colorname[i];
  669. }
  670. return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
  671. }
  672. void
  673. xloadcols(void)
  674. {
  675. int i;
  676. static int loaded;
  677. Color *cp;
  678. if (loaded) {
  679. for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp)
  680. XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
  681. } else {
  682. dc.collen = MAX(LEN(colorname), 256);
  683. dc.col = xmalloc(dc.collen * sizeof(Color));
  684. }
  685. for (i = 0; i < dc.collen; i++)
  686. if (!xloadcolor(i, NULL, &dc.col[i])) {
  687. if (colorname[i])
  688. die("could not allocate color '%s'\n", colorname[i]);
  689. else
  690. die("could not allocate color %d\n", i);
  691. }
  692. /* set alpha value of bg color */
  693. if (opt_alpha)
  694. alpha = strtof(opt_alpha, NULL);
  695. dc.col[defaultbg].color.alpha = (unsigned short)(0xffff * alpha);
  696. dc.col[defaultbg].pixel &= 0x00FFFFFF;
  697. dc.col[defaultbg].pixel |= (unsigned char)(0xff * alpha) << 24;
  698. loaded = 1;
  699. }
  700. int
  701. xsetcolorname(int x, const char *name)
  702. {
  703. Color ncolor;
  704. if (!BETWEEN(x, 0, dc.collen))
  705. return 1;
  706. if (!xloadcolor(x, name, &ncolor))
  707. return 1;
  708. XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
  709. dc.col[x] = ncolor;
  710. return 0;
  711. }
  712. /*
  713. * Absolute coordinates.
  714. */
  715. void
  716. xclear(int x1, int y1, int x2, int y2)
  717. {
  718. XftDrawRect(xw.draw,
  719. &dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg],
  720. x1, y1, x2-x1, y2-y1);
  721. }
  722. void
  723. xhints(void)
  724. {
  725. XClassHint class = {opt_name ? opt_name : termname,
  726. opt_class ? opt_class : termname};
  727. XWMHints wm = {.flags = InputHint, .input = 1};
  728. XSizeHints *sizeh;
  729. sizeh = XAllocSizeHints();
  730. sizeh->flags = PSize | PResizeInc | PBaseSize | PMinSize;
  731. sizeh->height = win.h;
  732. sizeh->width = win.w;
  733. sizeh->height_inc = 1;
  734. sizeh->width_inc = 1;
  735. sizeh->base_height = 2 * borderpx;
  736. sizeh->base_width = 2 * borderpx;
  737. sizeh->min_height = win.ch + 2 * borderpx;
  738. sizeh->min_width = win.cw + 2 * borderpx;
  739. if (xw.isfixed) {
  740. sizeh->flags |= PMaxSize;
  741. sizeh->min_width = sizeh->max_width = win.w;
  742. sizeh->min_height = sizeh->max_height = win.h;
  743. }
  744. if (xw.gm & (XValue|YValue)) {
  745. sizeh->flags |= USPosition | PWinGravity;
  746. sizeh->x = xw.l;
  747. sizeh->y = xw.t;
  748. sizeh->win_gravity = xgeommasktogravity(xw.gm);
  749. }
  750. XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm,
  751. &class);
  752. XFree(sizeh);
  753. }
  754. int
  755. xgeommasktogravity(int mask)
  756. {
  757. switch (mask & (XNegative|YNegative)) {
  758. case 0:
  759. return NorthWestGravity;
  760. case XNegative:
  761. return NorthEastGravity;
  762. case YNegative:
  763. return SouthWestGravity;
  764. }
  765. return SouthEastGravity;
  766. }
  767. int
  768. xloadfont(Font *f, FcPattern *pattern)
  769. {
  770. FcPattern *configured;
  771. FcPattern *match;
  772. FcResult result;
  773. XGlyphInfo extents;
  774. int wantattr, haveattr;
  775. /*
  776. * Manually configure instead of calling XftMatchFont
  777. * so that we can use the configured pattern for
  778. * "missing glyph" lookups.
  779. */
  780. configured = FcPatternDuplicate(pattern);
  781. if (!configured)
  782. return 1;
  783. FcConfigSubstitute(NULL, configured, FcMatchPattern);
  784. XftDefaultSubstitute(xw.dpy, xw.scr, configured);
  785. match = FcFontMatch(NULL, configured, &result);
  786. if (!match) {
  787. FcPatternDestroy(configured);
  788. return 1;
  789. }
  790. if (!(f->match = XftFontOpenPattern(xw.dpy, match))) {
  791. FcPatternDestroy(configured);
  792. FcPatternDestroy(match);
  793. return 1;
  794. }
  795. if ((XftPatternGetInteger(pattern, "slant", 0, &wantattr) ==
  796. XftResultMatch)) {
  797. /*
  798. * Check if xft was unable to find a font with the appropriate
  799. * slant but gave us one anyway. Try to mitigate.
  800. */
  801. if ((XftPatternGetInteger(f->match->pattern, "slant", 0,
  802. &haveattr) != XftResultMatch) || haveattr < wantattr) {
  803. f->badslant = 1;
  804. fputs("font slant does not match\n", stderr);
  805. }
  806. }
  807. if ((XftPatternGetInteger(pattern, "weight", 0, &wantattr) ==
  808. XftResultMatch)) {
  809. if ((XftPatternGetInteger(f->match->pattern, "weight", 0,
  810. &haveattr) != XftResultMatch) || haveattr != wantattr) {
  811. f->badweight = 1;
  812. fputs("font weight does not match\n", stderr);
  813. }
  814. }
  815. XftTextExtentsUtf8(xw.dpy, f->match,
  816. (const FcChar8 *) ascii_printable,
  817. strlen(ascii_printable), &extents);
  818. f->set = NULL;
  819. f->pattern = configured;
  820. f->ascent = f->match->ascent;
  821. f->descent = f->match->descent;
  822. f->lbearing = 0;
  823. f->rbearing = f->match->max_advance_width;
  824. f->height = f->ascent + f->descent;
  825. f->width = DIVCEIL(extents.xOff, strlen(ascii_printable));
  826. return 0;
  827. }
  828. void
  829. xloadfonts(char *fontstr, double fontsize)
  830. {
  831. FcPattern *pattern;
  832. double fontval;
  833. if (fontstr[0] == '-')
  834. pattern = XftXlfdParse(fontstr, False, False);
  835. else
  836. pattern = FcNameParse((FcChar8 *)fontstr);
  837. if (!pattern)
  838. die("can't open font %s\n", fontstr);
  839. if (fontsize > 1) {
  840. FcPatternDel(pattern, FC_PIXEL_SIZE);
  841. FcPatternDel(pattern, FC_SIZE);
  842. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize);
  843. usedfontsize = fontsize;
  844. } else {
  845. if (FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval) ==
  846. FcResultMatch) {
  847. usedfontsize = fontval;
  848. } else if (FcPatternGetDouble(pattern, FC_SIZE, 0, &fontval) ==
  849. FcResultMatch) {
  850. usedfontsize = -1;
  851. } else {
  852. /*
  853. * Default font size is 12, if none given. This is to
  854. * have a known usedfontsize value.
  855. */
  856. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
  857. usedfontsize = 12;
  858. }
  859. defaultfontsize = usedfontsize;
  860. }
  861. if (xloadfont(&dc.font, pattern))
  862. die("can't open font %s\n", fontstr);
  863. if (usedfontsize < 0) {
  864. FcPatternGetDouble(dc.font.match->pattern,
  865. FC_PIXEL_SIZE, 0, &fontval);
  866. usedfontsize = fontval;
  867. if (fontsize == 0)
  868. defaultfontsize = fontval;
  869. }
  870. /* Setting character width and height. */
  871. win.cw = ceilf(dc.font.width * cwscale);
  872. win.ch = ceilf(dc.font.height * chscale);
  873. FcPatternDel(pattern, FC_SLANT);
  874. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
  875. if (xloadfont(&dc.ifont, pattern))
  876. die("can't open font %s\n", fontstr);
  877. FcPatternDel(pattern, FC_WEIGHT);
  878. FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
  879. if (xloadfont(&dc.ibfont, pattern))
  880. die("can't open font %s\n", fontstr);
  881. FcPatternDel(pattern, FC_SLANT);
  882. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
  883. if (xloadfont(&dc.bfont, pattern))
  884. die("can't open font %s\n", fontstr);
  885. FcPatternDestroy(pattern);
  886. }
  887. int
  888. xloadsparefont(FcPattern *pattern, int flags)
  889. {
  890. FcPattern *match;
  891. FcResult result;
  892. match = FcFontMatch(NULL, pattern, &result);
  893. if (!match) {
  894. return 1;
  895. }
  896. if (!(frc[frclen].font = XftFontOpenPattern(xw.dpy, match))) {
  897. FcPatternDestroy(match);
  898. return 1;
  899. }
  900. frc[frclen].flags = flags;
  901. /* Believe U+0000 glyph will present in each default font */
  902. frc[frclen].unicodep = 0;
  903. frclen++;
  904. return 0;
  905. }
  906. void
  907. xloadsparefonts(void)
  908. {
  909. FcPattern *pattern;
  910. double sizeshift, fontval;
  911. int fc;
  912. char **fp;
  913. if (frclen != 0)
  914. die("can't embed spare fonts. cache isn't empty");
  915. /* Calculate count of spare fonts */
  916. fc = sizeof(font2) / sizeof(*font2);
  917. if (fc == 0)
  918. return;
  919. /* Allocate memory for cache entries. */
  920. if (frccap < 4 * fc) {
  921. frccap += 4 * fc - frccap;
  922. frc = xrealloc(frc, frccap * sizeof(Fontcache));
  923. }
  924. for (fp = font2; fp - font2 < fc; ++fp) {
  925. if (**fp == '-')
  926. pattern = XftXlfdParse(*fp, False, False);
  927. else
  928. pattern = FcNameParse((FcChar8 *)*fp);
  929. if (!pattern)
  930. die("can't open spare font %s\n", *fp);
  931. if (defaultfontsize > 0) {
  932. sizeshift = usedfontsize - defaultfontsize;
  933. if (sizeshift != 0 &&
  934. FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval) ==
  935. FcResultMatch) {
  936. fontval += sizeshift;
  937. FcPatternDel(pattern, FC_PIXEL_SIZE);
  938. FcPatternDel(pattern, FC_SIZE);
  939. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, fontval);
  940. }
  941. }
  942. FcPatternAddBool(pattern, FC_SCALABLE, 1);
  943. FcConfigSubstitute(NULL, pattern, FcMatchPattern);
  944. XftDefaultSubstitute(xw.dpy, xw.scr, pattern);
  945. if (xloadsparefont(pattern, FRC_NORMAL))
  946. die("can't open spare font %s\n", *fp);
  947. FcPatternDel(pattern, FC_SLANT);
  948. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
  949. if (xloadsparefont(pattern, FRC_ITALIC))
  950. die("can't open spare font %s\n", *fp);
  951. FcPatternDel(pattern, FC_WEIGHT);
  952. FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
  953. if (xloadsparefont(pattern, FRC_ITALICBOLD))
  954. die("can't open spare font %s\n", *fp);
  955. FcPatternDel(pattern, FC_SLANT);
  956. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
  957. if (xloadsparefont(pattern, FRC_BOLD))
  958. die("can't open spare font %s\n", *fp);
  959. FcPatternDestroy(pattern);
  960. }
  961. }
  962. void
  963. xunloadfont(Font *f)
  964. {
  965. XftFontClose(xw.dpy, f->match);
  966. FcPatternDestroy(f->pattern);
  967. if (f->set)
  968. FcFontSetDestroy(f->set);
  969. }
  970. void
  971. xunloadfonts(void)
  972. {
  973. /* Free the loaded fonts in the font cache. */
  974. while (frclen > 0)
  975. XftFontClose(xw.dpy, frc[--frclen].font);
  976. xunloadfont(&dc.font);
  977. xunloadfont(&dc.bfont);
  978. xunloadfont(&dc.ifont);
  979. xunloadfont(&dc.ibfont);
  980. }
  981. int
  982. ximopen(Display *dpy)
  983. {
  984. XIMCallback imdestroy = { .client_data = NULL, .callback = ximdestroy };
  985. XICCallback icdestroy = { .client_data = NULL, .callback = xicdestroy };
  986. xw.ime.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
  987. if (xw.ime.xim == NULL)
  988. return 0;
  989. if (XSetIMValues(xw.ime.xim, XNDestroyCallback, &imdestroy, NULL))
  990. fprintf(stderr, "XSetIMValues: "
  991. "Could not set XNDestroyCallback.\n");
  992. xw.ime.spotlist = XVaCreateNestedList(0, XNSpotLocation, &xw.ime.spot,
  993. NULL);
  994. if (xw.ime.xic == NULL) {
  995. xw.ime.xic = XCreateIC(xw.ime.xim, XNInputStyle,
  996. XIMPreeditNothing | XIMStatusNothing,
  997. XNClientWindow, xw.win,
  998. XNDestroyCallback, &icdestroy,
  999. NULL);
  1000. }
  1001. if (xw.ime.xic == NULL)
  1002. fprintf(stderr, "XCreateIC: Could not create input context.\n");
  1003. return 1;
  1004. }
  1005. void
  1006. ximinstantiate(Display *dpy, XPointer client, XPointer call)
  1007. {
  1008. if (ximopen(dpy))
  1009. XUnregisterIMInstantiateCallback(xw.dpy, NULL, NULL, NULL,
  1010. ximinstantiate, NULL);
  1011. }
  1012. void
  1013. ximdestroy(XIM xim, XPointer client, XPointer call)
  1014. {
  1015. xw.ime.xim = NULL;
  1016. XRegisterIMInstantiateCallback(xw.dpy, NULL, NULL, NULL,
  1017. ximinstantiate, NULL);
  1018. XFree(xw.ime.spotlist);
  1019. }
  1020. int
  1021. xicdestroy(XIC xim, XPointer client, XPointer call)
  1022. {
  1023. xw.ime.xic = NULL;
  1024. return 1;
  1025. }
  1026. void
  1027. xinit(int cols, int rows)
  1028. {
  1029. XGCValues gcvalues;
  1030. Cursor cursor;
  1031. Window parent;
  1032. pid_t thispid = getpid();
  1033. XColor xmousefg, xmousebg;
  1034. XWindowAttributes attr;
  1035. XVisualInfo vis;
  1036. if (!(xw.dpy = XOpenDisplay(NULL)))
  1037. die("can't open display\n");
  1038. xw.scr = XDefaultScreen(xw.dpy);
  1039. if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0)))) {
  1040. parent = XRootWindow(xw.dpy, xw.scr);
  1041. xw.depth = 32;
  1042. } else {
  1043. XGetWindowAttributes(xw.dpy, parent, &attr);
  1044. xw.depth = attr.depth;
  1045. }
  1046. XMatchVisualInfo(xw.dpy, xw.scr, xw.depth, TrueColor, &vis);
  1047. xw.vis = vis.visual;
  1048. /* font */
  1049. if (!FcInit())
  1050. die("could not init fontconfig.\n");
  1051. usedfont = (opt_font == NULL)? font : opt_font;
  1052. xloadfonts(usedfont, 0);
  1053. /* spare fonts */
  1054. xloadsparefonts();
  1055. /* colors */
  1056. xw.cmap = XCreateColormap(xw.dpy, parent, xw.vis, None);
  1057. xloadcols();
  1058. /* adjust fixed window geometry */
  1059. win.w = 2 * win.hborderpx + cols * win.cw;
  1060. win.h = 2 * win.vborderpx + rows * win.ch;
  1061. if (xw.gm & XNegative)
  1062. xw.l += DisplayWidth(xw.dpy, xw.scr) - win.w - 2;
  1063. if (xw.gm & YNegative)
  1064. xw.t += DisplayHeight(xw.dpy, xw.scr) - win.h - 2;
  1065. /* Events */
  1066. xw.attrs.background_pixel = dc.col[defaultbg].pixel;
  1067. xw.attrs.border_pixel = dc.col[defaultbg].pixel;
  1068. xw.attrs.bit_gravity = NorthWestGravity;
  1069. xw.attrs.event_mask = FocusChangeMask | KeyPressMask | KeyReleaseMask
  1070. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  1071. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
  1072. xw.attrs.colormap = xw.cmap;
  1073. xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
  1074. win.w, win.h, 0, xw.depth, InputOutput,
  1075. xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
  1076. | CWEventMask | CWColormap, &xw.attrs);
  1077. memset(&gcvalues, 0, sizeof(gcvalues));
  1078. gcvalues.graphics_exposures = False;
  1079. xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h, xw.depth);
  1080. dc.gc = XCreateGC(xw.dpy, xw.buf, GCGraphicsExposures, &gcvalues);
  1081. XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
  1082. XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h);
  1083. /* font spec buffer */
  1084. xw.specbuf = xmalloc(cols * sizeof(GlyphFontSpec));
  1085. /* Xft rendering context */
  1086. xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
  1087. /* input methods */
  1088. if (!ximopen(xw.dpy)) {
  1089. XRegisterIMInstantiateCallback(xw.dpy, NULL, NULL, NULL,
  1090. ximinstantiate, NULL);
  1091. }
  1092. /* white cursor, black outline */
  1093. cursor = XCreateFontCursor(xw.dpy, mouseshape);
  1094. XDefineCursor(xw.dpy, xw.win, cursor);
  1095. if (XParseColor(xw.dpy, xw.cmap, colorname[mousefg], &xmousefg) == 0) {
  1096. xmousefg.red = 0xffff;
  1097. xmousefg.green = 0xffff;
  1098. xmousefg.blue = 0xffff;
  1099. }
  1100. if (XParseColor(xw.dpy, xw.cmap, colorname[mousebg], &xmousebg) == 0) {
  1101. xmousebg.red = 0x0000;
  1102. xmousebg.green = 0x0000;
  1103. xmousebg.blue = 0x0000;
  1104. }
  1105. XRecolorCursor(xw.dpy, cursor, &xmousefg, &xmousebg);
  1106. xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
  1107. xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
  1108. xw.netwmname = XInternAtom(xw.dpy, "_NET_WM_NAME", False);
  1109. XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
  1110. xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False);
  1111. XChangeProperty(xw.dpy, xw.win, xw.netwmpid, XA_CARDINAL, 32,
  1112. PropModeReplace, (uchar *)&thispid, 1);
  1113. win.mode = MODE_NUMLOCK;
  1114. resettitle();
  1115. xhints();
  1116. XMapWindow(xw.dpy, xw.win);
  1117. XSync(xw.dpy, False);
  1118. clock_gettime(CLOCK_MONOTONIC, &xsel.tclick1);
  1119. clock_gettime(CLOCK_MONOTONIC, &xsel.tclick2);
  1120. xsel.primary = NULL;
  1121. xsel.clipboard = NULL;
  1122. xsel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
  1123. if (xsel.xtarget == None)
  1124. xsel.xtarget = XA_STRING;
  1125. }
  1126. int
  1127. xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x, int y)
  1128. {
  1129. float winx = win.hborderpx + x * win.cw, winy = win.vborderpx + y * win.ch, xp, yp;
  1130. ushort mode, prevmode = USHRT_MAX;
  1131. Font *font = &dc.font;
  1132. int frcflags = FRC_NORMAL;
  1133. float runewidth = win.cw;
  1134. Rune rune;
  1135. FT_UInt glyphidx;
  1136. FcResult fcres;
  1137. FcPattern *fcpattern, *fontpattern;
  1138. FcFontSet *fcsets[] = { NULL };
  1139. FcCharSet *fccharset;
  1140. int i, f, numspecs = 0;
  1141. for (i = 0, xp = winx, yp = winy + font->ascent; i < len; ++i) {
  1142. /* Fetch rune and mode for current glyph. */
  1143. rune = glyphs[i].u;
  1144. mode = glyphs[i].mode;
  1145. /* Skip dummy wide-character spacing. */
  1146. if (mode == ATTR_WDUMMY)
  1147. continue;
  1148. /* Determine font for glyph if different from previous glyph. */
  1149. if (prevmode != mode) {
  1150. prevmode = mode;
  1151. font = &dc.font;
  1152. frcflags = FRC_NORMAL;
  1153. runewidth = win.cw * ((mode & ATTR_WIDE) ? 2.0f : 1.0f);
  1154. if ((mode & ATTR_ITALIC) && (mode & ATTR_BOLD)) {
  1155. font = &dc.ibfont;
  1156. frcflags = FRC_ITALICBOLD;
  1157. } else if (mode & ATTR_ITALIC) {
  1158. font = &dc.ifont;
  1159. frcflags = FRC_ITALIC;
  1160. } else if (mode & ATTR_BOLD) {
  1161. font = &dc.bfont;
  1162. frcflags = FRC_BOLD;
  1163. }
  1164. yp = winy + font->ascent;
  1165. }
  1166. /* Lookup character index with default font. */
  1167. glyphidx = XftCharIndex(xw.dpy, font->match, rune);
  1168. if (glyphidx) {
  1169. specs[numspecs].font = font->match;
  1170. specs[numspecs].glyph = glyphidx;
  1171. specs[numspecs].x = (short)xp;
  1172. specs[numspecs].y = (short)yp;
  1173. xp += runewidth;
  1174. numspecs++;
  1175. continue;
  1176. }
  1177. /* Fallback on font cache, search the font cache for match. */
  1178. for (f = 0; f < frclen; f++) {
  1179. glyphidx = XftCharIndex(xw.dpy, frc[f].font, rune);
  1180. /* Everything correct. */
  1181. if (glyphidx && frc[f].flags == frcflags)
  1182. break;
  1183. /* We got a default font for a not found glyph. */
  1184. if (!glyphidx && frc[f].flags == frcflags
  1185. && frc[f].unicodep == rune) {
  1186. break;
  1187. }
  1188. }
  1189. /* Nothing was found. Use fontconfig to find matching font. */
  1190. if (f >= frclen) {
  1191. if (!font->set)
  1192. font->set = FcFontSort(0, font->pattern,
  1193. 1, 0, &fcres);
  1194. fcsets[0] = font->set;
  1195. /*
  1196. * Nothing was found in the cache. Now use
  1197. * some dozen of Fontconfig calls to get the
  1198. * font for one single character.
  1199. *
  1200. * Xft and fontconfig are design failures.
  1201. */
  1202. fcpattern = FcPatternDuplicate(font->pattern);
  1203. fccharset = FcCharSetCreate();
  1204. FcCharSetAddChar(fccharset, rune);
  1205. FcPatternAddCharSet(fcpattern, FC_CHARSET,
  1206. fccharset);
  1207. FcPatternAddBool(fcpattern, FC_SCALABLE, 1);
  1208. FcConfigSubstitute(0, fcpattern,
  1209. FcMatchPattern);
  1210. FcDefaultSubstitute(fcpattern);
  1211. fontpattern = FcFontSetMatch(0, fcsets, 1,
  1212. fcpattern, &fcres);
  1213. /* Allocate memory for the new cache entry. */
  1214. if (frclen >= frccap) {
  1215. frccap += 16;
  1216. frc = xrealloc(frc, frccap * sizeof(Fontcache));
  1217. }
  1218. frc[frclen].font = XftFontOpenPattern(xw.dpy,
  1219. fontpattern);
  1220. if (!frc[frclen].font)
  1221. die("XftFontOpenPattern failed seeking fallback font: %s\n",
  1222. strerror(errno));
  1223. frc[frclen].flags = frcflags;
  1224. frc[frclen].unicodep = rune;
  1225. glyphidx = XftCharIndex(xw.dpy, frc[frclen].font, rune);
  1226. f = frclen;
  1227. frclen++;
  1228. FcPatternDestroy(fcpattern);
  1229. FcCharSetDestroy(fccharset);
  1230. }
  1231. specs[numspecs].font = frc[f].font;
  1232. specs[numspecs].glyph = glyphidx;
  1233. specs[numspecs].x = (short)xp;
  1234. specs[numspecs].y = (short)yp;
  1235. xp += runewidth;
  1236. numspecs++;
  1237. }
  1238. return numspecs;
  1239. }
  1240. void
  1241. xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, int y)
  1242. {
  1243. int charlen = len * ((base.mode & ATTR_WIDE) ? 2 : 1);
  1244. int winx = win.hborderpx + x * win.cw, winy = win.vborderpx + y * win.ch,
  1245. width = charlen * win.cw;
  1246. Color *fg, *bg, *temp, revfg, revbg, truefg, truebg;
  1247. XRenderColor colfg, colbg;
  1248. XRectangle r;
  1249. /* Fallback on color display for attributes not supported by the font */
  1250. if (base.mode & ATTR_ITALIC && base.mode & ATTR_BOLD) {
  1251. if (dc.ibfont.badslant || dc.ibfont.badweight)
  1252. base.fg = defaultattr;
  1253. } else if ((base.mode & ATTR_ITALIC && dc.ifont.badslant) ||
  1254. (base.mode & ATTR_BOLD && dc.bfont.badweight)) {
  1255. base.fg = defaultattr;
  1256. }
  1257. if (IS_TRUECOL(base.fg)) {
  1258. colfg.alpha = 0xffff;
  1259. colfg.red = TRUERED(base.fg);
  1260. colfg.green = TRUEGREEN(base.fg);
  1261. colfg.blue = TRUEBLUE(base.fg);
  1262. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &truefg);
  1263. fg = &truefg;
  1264. } else {
  1265. fg = &dc.col[base.fg];
  1266. }
  1267. if (IS_TRUECOL(base.bg)) {
  1268. colbg.alpha = 0xffff;
  1269. colbg.green = TRUEGREEN(base.bg);
  1270. colbg.red = TRUERED(base.bg);
  1271. colbg.blue = TRUEBLUE(base.bg);
  1272. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &truebg);
  1273. bg = &truebg;
  1274. } else {
  1275. bg = &dc.col[base.bg];
  1276. }
  1277. /* Change basic system colors [0-7] to bright system colors [8-15] */
  1278. if ((base.mode & ATTR_BOLD_FAINT) == ATTR_BOLD && BETWEEN(base.fg, 0, 7))
  1279. fg = &dc.col[base.fg + 8];
  1280. if (IS_SET(MODE_REVERSE)) {
  1281. if (fg == &dc.col[defaultfg]) {
  1282. fg = &dc.col[defaultbg];
  1283. } else {
  1284. colfg.red = ~fg->color.red;
  1285. colfg.green = ~fg->color.green;
  1286. colfg.blue = ~fg->color.blue;
  1287. colfg.alpha = fg->color.alpha;
  1288. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg,
  1289. &revfg);
  1290. fg = &revfg;
  1291. }
  1292. if (bg == &dc.col[defaultbg]) {
  1293. bg = &dc.col[defaultfg];
  1294. } else {
  1295. colbg.red = ~bg->color.red;
  1296. colbg.green = ~bg->color.green;
  1297. colbg.blue = ~bg->color.blue;
  1298. colbg.alpha = bg->color.alpha;
  1299. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg,
  1300. &revbg);
  1301. bg = &revbg;
  1302. }
  1303. }
  1304. if ((base.mode & ATTR_BOLD_FAINT) == ATTR_FAINT) {
  1305. colfg.red = fg->color.red / 2;
  1306. colfg.green = fg->color.green / 2;
  1307. colfg.blue = fg->color.blue / 2;
  1308. colfg.alpha = fg->color.alpha;
  1309. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
  1310. fg = &revfg;
  1311. }
  1312. if (base.mode & ATTR_REVERSE) {
  1313. temp = fg;
  1314. fg = bg;
  1315. bg = temp;
  1316. }
  1317. if (base.mode & ATTR_BLINK && win.mode & MODE_BLINK)
  1318. fg = bg;
  1319. if (base.mode & ATTR_INVISIBLE)
  1320. fg = bg;
  1321. /* Intelligent cleaning up of the borders. */
  1322. if (x == 0) {
  1323. xclear(0, (y == 0)? 0 : winy, win.vborderpx,
  1324. winy + win.ch +
  1325. ((winy + win.ch >= win.vborderpx + win.th)? win.h : 0));
  1326. }
  1327. if (winx + width >= win.hborderpx + win.tw) {
  1328. xclear(winx + width, (y == 0)? 0 : winy, win.w,
  1329. ((winy + win.ch >= win.vborderpx + win.th)? win.h : (winy + win.ch)));
  1330. }
  1331. if (y == 0)
  1332. xclear(winx, 0, winx + width, win.hborderpx);
  1333. if (winy + win.ch >= win.vborderpx + win.th)
  1334. xclear(winx, winy + win.ch, winx + width, win.h);
  1335. /* Clean up the region we want to draw to. */
  1336. XftDrawRect(xw.draw, bg, winx, winy, width, win.ch);
  1337. /* Set the clip region because Xft is sometimes dirty. */
  1338. r.x = 0;
  1339. r.y = 0;
  1340. r.height = win.ch;
  1341. r.width = width;
  1342. XftDrawSetClipRectangles(xw.draw, winx, winy, &r, 1);
  1343. /* Render the glyphs. */
  1344. XftDrawGlyphFontSpec(xw.draw, fg, specs, len);
  1345. /* Render underline and strikethrough. */
  1346. if (base.mode & ATTR_UNDERLINE) {
  1347. XftDrawRect(xw.draw, fg, winx, winy + dc.font.ascent + 1,
  1348. width, 1);
  1349. }
  1350. if (base.mode & ATTR_STRUCK) {
  1351. XftDrawRect(xw.draw, fg, winx, winy + 2 * dc.font.ascent / 3,
  1352. width, 1);
  1353. }
  1354. /* Reset clip to none. */
  1355. XftDrawSetClip(xw.draw, 0);
  1356. }
  1357. void
  1358. xdrawglyph(Glyph g, int x, int y)
  1359. {
  1360. int numspecs;
  1361. XftGlyphFontSpec spec;
  1362. numspecs = xmakeglyphfontspecs(&spec, &g, 1, x, y);
  1363. xdrawglyphfontspecs(&spec, g, numspecs, x, y);
  1364. }
  1365. void
  1366. xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og)
  1367. {
  1368. Color drawcol;
  1369. /* remove the old cursor */
  1370. if (selected(ox, oy))
  1371. og.mode ^= ATTR_REVERSE;
  1372. xdrawglyph(og, ox, oy);
  1373. if (IS_SET(MODE_HIDE))
  1374. return;
  1375. /*
  1376. * Select the right color for the right mode.
  1377. */
  1378. g.mode &= ATTR_BOLD|ATTR_ITALIC|ATTR_UNDERLINE|ATTR_STRUCK|ATTR_WIDE;
  1379. if (IS_SET(MODE_REVERSE)) {
  1380. g.mode |= ATTR_REVERSE;
  1381. g.bg = defaultfg;
  1382. if (selected(cx, cy)) {
  1383. drawcol = dc.col[defaultcs];
  1384. g.fg = defaultrcs;
  1385. } else {
  1386. drawcol = dc.col[defaultrcs];
  1387. g.fg = defaultcs;
  1388. }
  1389. } else {
  1390. if (selected(cx, cy)) {
  1391. g.fg = defaultfg;
  1392. g.bg = defaultrcs;
  1393. } else {
  1394. g.fg = defaultbg;
  1395. g.bg = defaultcs;
  1396. }
  1397. drawcol = dc.col[g.bg];
  1398. }
  1399. /* draw the new one */
  1400. if (IS_SET(MODE_FOCUSED)) {
  1401. switch (win.cursor) {
  1402. case 7: /* st extension: snowman (U+2603) */
  1403. g.u = 0x2603;
  1404. case 0: /* Blinking Block */
  1405. case 1: /* Blinking Block (Default) */
  1406. case 2: /* Steady Block */
  1407. xdrawglyph(g, cx, cy);
  1408. break;
  1409. case 3: /* Blinking Underline */
  1410. case 4: /* Steady Underline */
  1411. XftDrawRect(xw.draw, &drawcol,
  1412. win.hborderpx + cx * win.cw,
  1413. win.vborderpx + (cy + 1) * win.ch - \
  1414. cursorthickness,
  1415. win.cw, cursorthickness);
  1416. break;
  1417. case 5: /* Blinking bar */
  1418. case 6: /* Steady bar */
  1419. XftDrawRect(xw.draw, &drawcol,
  1420. win.hborderpx + cx * win.cw,
  1421. win.vborderpx + cy * win.ch,
  1422. cursorthickness, win.ch);
  1423. break;
  1424. }
  1425. } else {
  1426. XftDrawRect(xw.draw, &drawcol,
  1427. win.hborderpx + cx * win.cw,
  1428. win.vborderpx + cy * win.ch,
  1429. win.cw - 1, 1);
  1430. XftDrawRect(xw.draw, &drawcol,
  1431. win.hborderpx + cx * win.cw,
  1432. win.vborderpx + cy * win.ch,
  1433. 1, win.ch - 1);
  1434. XftDrawRect(xw.draw, &drawcol,
  1435. win.hborderpx + (cx + 1) * win.cw - 1,
  1436. win.vborderpx + cy * win.ch,
  1437. 1, win.ch - 1);
  1438. XftDrawRect(xw.draw, &drawcol,
  1439. win.hborderpx + cx * win.cw,
  1440. win.vborderpx + (cy + 1) * win.ch - 1,
  1441. win.cw, 1);
  1442. }
  1443. }
  1444. void
  1445. xsetenv(void)
  1446. {
  1447. char buf[sizeof(long) * 8 + 1];
  1448. snprintf(buf, sizeof(buf), "%lu", xw.win);
  1449. setenv("WINDOWID", buf, 1);
  1450. }
  1451. void
  1452. xsettitle(char *p)
  1453. {
  1454. XTextProperty prop;
  1455. DEFAULT(p, opt_title);
  1456. Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
  1457. &prop);
  1458. XSetWMName(xw.dpy, xw.win, &prop);
  1459. XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmname);
  1460. XFree(prop.value);
  1461. }
  1462. int
  1463. xstartdraw(void)
  1464. {
  1465. return IS_SET(MODE_VISIBLE);
  1466. }
  1467. void
  1468. xdrawline(Line line, int x1, int y1, int x2)
  1469. {
  1470. int i, x, ox, numspecs;
  1471. Glyph base, new;
  1472. XftGlyphFontSpec *specs = xw.specbuf;
  1473. numspecs = xmakeglyphfontspecs(specs, &line[x1], x2 - x1, x1, y1);
  1474. i = ox = 0;
  1475. for (x = x1; x < x2 && i < numspecs; x++) {
  1476. new = line[x];
  1477. if (new.mode == ATTR_WDUMMY)
  1478. continue;
  1479. if (selected(x, y1))
  1480. new.mode ^= ATTR_REVERSE;
  1481. if (i > 0 && ATTRCMP(base, new)) {
  1482. xdrawglyphfontspecs(specs, base, i, ox, y1);
  1483. specs += i;
  1484. numspecs -= i;
  1485. i = 0;
  1486. }
  1487. if (i == 0) {
  1488. ox = x;
  1489. base = new;
  1490. }
  1491. i++;
  1492. }
  1493. if (i > 0)
  1494. xdrawglyphfontspecs(specs, base, i, ox, y1);
  1495. }
  1496. void
  1497. xfinishdraw(void)
  1498. {
  1499. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, win.w,
  1500. win.h, 0, 0);
  1501. XSetForeground(xw.dpy, dc.gc,
  1502. dc.col[IS_SET(MODE_REVERSE)?
  1503. defaultfg : defaultbg].pixel);
  1504. }
  1505. void
  1506. xximspot(int x, int y)
  1507. {
  1508. if (xw.ime.xic == NULL)
  1509. return;
  1510. xw.ime.spot.x = borderpx + x * win.cw;
  1511. xw.ime.spot.y = borderpx + (y + 1) * win.ch;
  1512. XSetICValues(xw.ime.xic, XNPreeditAttributes, xw.ime.spotlist, NULL);
  1513. }
  1514. void
  1515. expose(XEvent *ev)
  1516. {
  1517. redraw();
  1518. }
  1519. void
  1520. visibility(XEvent *ev)
  1521. {
  1522. XVisibilityEvent *e = &ev->xvisibility;
  1523. MODBIT(win.mode, e->state != VisibilityFullyObscured, MODE_VISIBLE);
  1524. }
  1525. void
  1526. unmap(XEvent *ev)
  1527. {
  1528. win.mode &= ~MODE_VISIBLE;
  1529. }
  1530. void
  1531. xsetpointermotion(int set)
  1532. {
  1533. MODBIT(xw.attrs.event_mask, set, PointerMotionMask);
  1534. XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs);
  1535. }
  1536. void
  1537. xsetmode(int set, unsigned int flags)
  1538. {
  1539. int mode = win.mode;
  1540. MODBIT(win.mode, set, flags);
  1541. if ((win.mode & MODE_REVERSE) != (mode & MODE_REVERSE))
  1542. redraw();
  1543. }
  1544. int
  1545. xsetcursor(int cursor)
  1546. {
  1547. DEFAULT(cursor, 1);
  1548. if (!BETWEEN(cursor, 0, 6))
  1549. return 1;
  1550. win.cursor = cursor;
  1551. return 0;
  1552. }
  1553. void
  1554. xseturgency(int add)
  1555. {
  1556. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  1557. MODBIT(h->flags, add, XUrgencyHint);
  1558. XSetWMHints(xw.dpy, xw.win, h);
  1559. XFree(h);
  1560. }
  1561. void
  1562. xbell(void)
  1563. {
  1564. if (!(IS_SET(MODE_FOCUSED)))
  1565. xseturgency(1);
  1566. if (bellvolume)
  1567. XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL);
  1568. }
  1569. void
  1570. focus(XEvent *ev)
  1571. {
  1572. XFocusChangeEvent *e = &ev->xfocus;
  1573. if (e->mode == NotifyGrab)
  1574. return;
  1575. if (ev->type == FocusIn) {
  1576. if (xw.ime.xic)
  1577. XSetICFocus(xw.ime.xic);
  1578. win.mode |= MODE_FOCUSED;
  1579. xseturgency(0);
  1580. if (IS_SET(MODE_FOCUS))
  1581. ttywrite("\033[I", 3, 0);
  1582. } else {
  1583. if (xw.ime.xic)
  1584. XUnsetICFocus(xw.ime.xic);
  1585. win.mode &= ~MODE_FOCUSED;
  1586. if (IS_SET(MODE_FOCUS))
  1587. ttywrite("\033[O", 3, 0);
  1588. }
  1589. }
  1590. int
  1591. match(uint mask, uint state)
  1592. {
  1593. return mask == XK_ANY_MOD || mask == (state & ~ignoremod);
  1594. }
  1595. char*
  1596. kmap(KeySym k, uint state)
  1597. {
  1598. Key *kp;
  1599. int i;
  1600. /* Check for mapped keys out of X11 function keys. */
  1601. for (i = 0; i < LEN(mappedkeys); i++) {
  1602. if (mappedkeys[i] == k)
  1603. break;
  1604. }
  1605. if (i == LEN(mappedkeys)) {
  1606. if ((k & 0xFFFF) < 0xFD00)
  1607. return NULL;
  1608. }
  1609. for (kp = key; kp < key + LEN(key); kp++) {
  1610. if (kp->k != k)
  1611. continue;
  1612. if (!match(kp->mask, state))
  1613. continue;
  1614. if (IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0)
  1615. continue;
  1616. if (IS_SET(MODE_NUMLOCK) && kp->appkey == 2)
  1617. continue;
  1618. if (IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0)
  1619. continue;
  1620. return kp->s;
  1621. }
  1622. return NULL;
  1623. }
  1624. void
  1625. kpress(XEvent *ev)
  1626. {
  1627. XKeyEvent *e = &ev->xkey;
  1628. KeySym ksym;
  1629. char buf[64], *customkey;
  1630. int len;
  1631. Rune c;
  1632. Status status;
  1633. Shortcut *bp;
  1634. if (IS_SET(MODE_KBDLOCK))
  1635. return;
  1636. if (xw.ime.xic)
  1637. len = XmbLookupString(xw.ime.xic, e, buf, sizeof buf, &ksym, &status);
  1638. else
  1639. len = XLookupString(e, buf, sizeof buf, &ksym, NULL);
  1640. /* 1. shortcuts */
  1641. for (bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
  1642. if (ksym == bp->keysym && match(bp->mod, e->state)) {
  1643. bp->func(&(bp->arg));
  1644. return;
  1645. }
  1646. }
  1647. /* 2. custom keys from config.h */
  1648. if ((customkey = kmap(ksym, e->state))) {
  1649. ttywrite(customkey, strlen(customkey), 1);
  1650. return;
  1651. }
  1652. /* 3. composed string from input method */
  1653. if (len == 0)
  1654. return;
  1655. if (len == 1 && e->state & Mod1Mask) {
  1656. if (IS_SET(MODE_8BIT)) {
  1657. if (*buf < 0177) {
  1658. c = *buf | 0x80;
  1659. len = utf8encode(c, buf);
  1660. }
  1661. } else {
  1662. buf[1] = buf[0];
  1663. buf[0] = '\033';
  1664. len = 2;
  1665. }
  1666. }
  1667. ttywrite(buf, len, 1);
  1668. }
  1669. void
  1670. cmessage(XEvent *e)
  1671. {
  1672. /*
  1673. * See xembed specs
  1674. * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
  1675. */
  1676. if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
  1677. if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
  1678. win.mode |= MODE_FOCUSED;
  1679. xseturgency(0);
  1680. } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
  1681. win.mode &= ~MODE_FOCUSED;
  1682. }
  1683. } else if (e->xclient.data.l[0] == xw.wmdeletewin) {
  1684. ttyhangup();
  1685. exit(0);
  1686. }
  1687. }
  1688. void
  1689. resize(XEvent *e)
  1690. {
  1691. if (e->xconfigure.width == win.w && e->xconfigure.height == win.h)
  1692. return;
  1693. cresize(e->xconfigure.width, e->xconfigure.height);
  1694. }
  1695. void
  1696. run(void)
  1697. {
  1698. XEvent ev;
  1699. int w = win.w, h = win.h;
  1700. fd_set rfd;
  1701. int xfd = XConnectionNumber(xw.dpy), xev, blinkset = 0, dodraw = 0;
  1702. int ttyfd;
  1703. struct timespec drawtimeout, *tv = NULL, now, last, lastblink;
  1704. long deltatime;
  1705. /* Waiting for window mapping */
  1706. do {
  1707. XNextEvent(xw.dpy, &ev);
  1708. /*
  1709. * This XFilterEvent call is required because of XOpenIM. It
  1710. * does filter out the key event and some client message for
  1711. * the input method too.
  1712. */
  1713. if (XFilterEvent(&ev, None))
  1714. continue;
  1715. if (ev.type == ConfigureNotify) {
  1716. w = ev.xconfigure.width;
  1717. h = ev.xconfigure.height;
  1718. }
  1719. } while (ev.type != MapNotify);
  1720. ttyfd = ttynew(opt_line, shell, opt_io, opt_cmd);
  1721. cresize(w, h);
  1722. clock_gettime(CLOCK_MONOTONIC, &last);
  1723. lastblink = last;
  1724. for (xev = actionfps;;) {
  1725. FD_ZERO(&rfd);
  1726. FD_SET(ttyfd, &rfd);
  1727. FD_SET(xfd, &rfd);
  1728. if (pselect(MAX(xfd, ttyfd)+1, &rfd, NULL, NULL, tv, NULL) < 0) {
  1729. if (errno == EINTR)
  1730. continue;
  1731. die("select failed: %s\n", strerror(errno));
  1732. }
  1733. if (FD_ISSET(ttyfd, &rfd)) {
  1734. ttyread();
  1735. if (blinktimeout) {
  1736. blinkset = tattrset(ATTR_BLINK);
  1737. if (!blinkset)
  1738. MODBIT(win.mode, 0, MODE_BLINK);
  1739. }
  1740. }
  1741. if (FD_ISSET(xfd, &rfd))
  1742. xev = actionfps;
  1743. clock_gettime(CLOCK_MONOTONIC, &now);
  1744. drawtimeout.tv_sec = 0;
  1745. drawtimeout.tv_nsec = (1000 * 1E6)/ xfps;
  1746. tv = &drawtimeout;
  1747. dodraw = 0;
  1748. if (blinktimeout && TIMEDIFF(now, lastblink) > blinktimeout) {
  1749. tsetdirtattr(ATTR_BLINK);
  1750. win.mode ^= MODE_BLINK;
  1751. lastblink = now;
  1752. dodraw = 1;
  1753. }
  1754. deltatime = TIMEDIFF(now, last);
  1755. if (deltatime > 1000 / (xev ? xfps : actionfps)) {
  1756. dodraw = 1;
  1757. last = now;
  1758. }
  1759. if (dodraw) {
  1760. while (XPending(xw.dpy)) {
  1761. XNextEvent(xw.dpy, &ev);
  1762. if (XFilterEvent(&ev, None))
  1763. continue;
  1764. if (handler[ev.type])
  1765. (handler[ev.type])(&ev);
  1766. }
  1767. draw();
  1768. XFlush(xw.dpy);
  1769. if (xev && !FD_ISSET(xfd, &rfd))
  1770. xev--;
  1771. if (!FD_ISSET(ttyfd, &rfd) && !FD_ISSET(xfd, &rfd)) {
  1772. if (blinkset) {
  1773. if (TIMEDIFF(now, lastblink) \
  1774. > blinktimeout) {
  1775. drawtimeout.tv_nsec = 1000;
  1776. } else {
  1777. drawtimeout.tv_nsec = (1E6 * \
  1778. (blinktimeout - \
  1779. TIMEDIFF(now,
  1780. lastblink)));
  1781. }
  1782. drawtimeout.tv_sec = \
  1783. drawtimeout.tv_nsec / 1E9;
  1784. drawtimeout.tv_nsec %= (long)1E9;
  1785. } else {
  1786. tv = NULL;
  1787. }
  1788. }
  1789. }
  1790. }
  1791. }
  1792. void
  1793. usage(void)
  1794. {
  1795. die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
  1796. " [-n name] [-o file]\n"
  1797. " [-T title] [-t title] [-w windowid]"
  1798. " [[-e] command [args ...]]\n"
  1799. " %s [-aiv] [-c class] [-f font] [-g geometry]"
  1800. " [-n name] [-o file]\n"
  1801. " [-T title] [-t title] [-w windowid] -l line"
  1802. " [stty_args ...]\n", argv0, argv0);
  1803. }
  1804. int
  1805. main(int argc, char *argv[])
  1806. {
  1807. xw.l = xw.t = 0;
  1808. xw.isfixed = False;
  1809. win.cursor = cursorshape;
  1810. ARGBEGIN {
  1811. case 'a':
  1812. allowaltscreen = 0;
  1813. break;
  1814. case 'A':
  1815. opt_alpha = EARGF(usage());
  1816. break;
  1817. case 'c':
  1818. opt_class = EARGF(usage());
  1819. break;
  1820. case 'e':
  1821. if (argc > 0)
  1822. --argc, ++argv;
  1823. goto run;
  1824. case 'f':
  1825. opt_font = EARGF(usage());
  1826. break;
  1827. case 'g':
  1828. xw.gm = XParseGeometry(EARGF(usage()),
  1829. &xw.l, &xw.t, &cols, &rows);
  1830. break;
  1831. case 'i':
  1832. xw.isfixed = 1;
  1833. break;
  1834. case 'o':
  1835. opt_io = EARGF(usage());
  1836. break;
  1837. case 'l':
  1838. opt_line = EARGF(usage());
  1839. break;
  1840. case 'n':
  1841. opt_name = EARGF(usage());
  1842. break;
  1843. case 't':
  1844. case 'T':
  1845. opt_title = EARGF(usage());
  1846. break;
  1847. case 'w':
  1848. opt_embed = EARGF(usage());
  1849. break;
  1850. case 'v':
  1851. die("%s " VERSION "\n", argv0);
  1852. break;
  1853. default:
  1854. usage();
  1855. } ARGEND;
  1856. run:
  1857. if (argc > 0) /* eat all remaining arguments */
  1858. opt_cmd = argv;
  1859. if (!opt_title)
  1860. opt_title = (opt_line || !opt_cmd) ? "st" : opt_cmd[0];
  1861. setlocale(LC_CTYPE, "");
  1862. XSetLocaleModifiers("");
  1863. cols = MAX(cols, 1);
  1864. rows = MAX(rows, 1);
  1865. tnew(cols, rows);
  1866. xinit(cols, rows);
  1867. xsetenv();
  1868. selinit();
  1869. run();
  1870. return 0;
  1871. }