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.

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