My build of suckless st terminal
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.

4080 lines
90 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
14 years ago
11 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
12 years ago
14 years ago
14 years ago
11 years ago
11 years ago
11 years ago
14 years ago
14 years ago
14 years ago
14 years ago
11 years ago
14 years ago
14 years ago
14 years ago
14 years ago
11 years ago
14 years ago
11 years ago
14 years ago
14 years ago
14 years ago
  1. /* See LICENSE for license details. */
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <limits.h>
  6. #include <locale.h>
  7. #include <pwd.h>
  8. #include <stdarg.h>
  9. #include <stdbool.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <signal.h>
  14. #include <stdint.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/select.h>
  17. #include <sys/stat.h>
  18. #include <sys/time.h>
  19. #include <sys/types.h>
  20. #include <sys/wait.h>
  21. #include <time.h>
  22. #include <unistd.h>
  23. #include <libgen.h>
  24. #include <X11/Xatom.h>
  25. #include <X11/Xlib.h>
  26. #include <X11/Xutil.h>
  27. #include <X11/cursorfont.h>
  28. #include <X11/keysym.h>
  29. #include <X11/Xft/Xft.h>
  30. #include <X11/XKBlib.h>
  31. #include <fontconfig/fontconfig.h>
  32. #include <wchar.h>
  33. #include "arg.h"
  34. char *argv0;
  35. #define Glyph Glyph_
  36. #define Font Font_
  37. #if defined(__linux)
  38. #include <pty.h>
  39. #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  40. #include <util.h>
  41. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  42. #include <libutil.h>
  43. #endif
  44. /* XEMBED messages */
  45. #define XEMBED_FOCUS_IN 4
  46. #define XEMBED_FOCUS_OUT 5
  47. /* Arbitrary sizes */
  48. #define UTF_INVALID 0xFFFD
  49. #define UTF_SIZ 4
  50. #define ESC_BUF_SIZ (128*UTF_SIZ)
  51. #define ESC_ARG_SIZ 16
  52. #define STR_BUF_SIZ ESC_BUF_SIZ
  53. #define STR_ARG_SIZ ESC_ARG_SIZ
  54. #define DRAW_BUF_SIZ 20*1024
  55. #define XK_ANY_MOD UINT_MAX
  56. #define XK_NO_MOD 0
  57. #define XK_SWITCH_MOD (1<<13)
  58. /* macros */
  59. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  60. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  61. #define LEN(a) (sizeof(a) / sizeof(a)[0])
  62. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  63. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  64. #define ISCONTROLC0(c) (BETWEEN(c, 0, 0x1f) || (c) == '\177')
  65. #define ISCONTROLC1(c) (BETWEEN(c, 0x80, 0x9f))
  66. #define ISCONTROL(c) (ISCONTROLC0(c) || ISCONTROLC1(c))
  67. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  68. #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
  69. #define IS_SET(flag) ((term.mode & (flag)) != 0)
  70. #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_nsec-t2.tv_nsec)/1E6)
  71. #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
  72. #define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b))
  73. #define IS_TRUECOL(x) (1 << 24 & (x))
  74. #define TRUERED(x) (((x) & 0xff0000) >> 8)
  75. #define TRUEGREEN(x) (((x) & 0xff00))
  76. #define TRUEBLUE(x) (((x) & 0xff) << 8)
  77. enum glyph_attribute {
  78. ATTR_NULL = 0,
  79. ATTR_BOLD = 1 << 0,
  80. ATTR_FAINT = 1 << 1,
  81. ATTR_ITALIC = 1 << 2,
  82. ATTR_UNDERLINE = 1 << 3,
  83. ATTR_BLINK = 1 << 4,
  84. ATTR_REVERSE = 1 << 5,
  85. ATTR_INVISIBLE = 1 << 6,
  86. ATTR_STRUCK = 1 << 7,
  87. ATTR_WRAP = 1 << 8,
  88. ATTR_WIDE = 1 << 9,
  89. ATTR_WDUMMY = 1 << 10,
  90. };
  91. enum cursor_movement {
  92. CURSOR_SAVE,
  93. CURSOR_LOAD
  94. };
  95. enum cursor_state {
  96. CURSOR_DEFAULT = 0,
  97. CURSOR_WRAPNEXT = 1,
  98. CURSOR_ORIGIN = 2
  99. };
  100. enum term_mode {
  101. MODE_WRAP = 1 << 0,
  102. MODE_INSERT = 1 << 1,
  103. MODE_APPKEYPAD = 1 << 2,
  104. MODE_ALTSCREEN = 1 << 3,
  105. MODE_CRLF = 1 << 4,
  106. MODE_MOUSEBTN = 1 << 5,
  107. MODE_MOUSEMOTION = 1 << 6,
  108. MODE_REVERSE = 1 << 7,
  109. MODE_KBDLOCK = 1 << 8,
  110. MODE_HIDE = 1 << 9,
  111. MODE_ECHO = 1 << 10,
  112. MODE_APPCURSOR = 1 << 11,
  113. MODE_MOUSESGR = 1 << 12,
  114. MODE_8BIT = 1 << 13,
  115. MODE_BLINK = 1 << 14,
  116. MODE_FBLINK = 1 << 15,
  117. MODE_FOCUS = 1 << 16,
  118. MODE_MOUSEX10 = 1 << 17,
  119. MODE_MOUSEMANY = 1 << 18,
  120. MODE_BRCKTPASTE = 1 << 19,
  121. MODE_PRINT = 1 << 20,
  122. MODE_MOUSE = MODE_MOUSEBTN|MODE_MOUSEMOTION|MODE_MOUSEX10\
  123. |MODE_MOUSEMANY,
  124. };
  125. enum charset {
  126. CS_GRAPHIC0,
  127. CS_GRAPHIC1,
  128. CS_UK,
  129. CS_USA,
  130. CS_MULTI,
  131. CS_GER,
  132. CS_FIN
  133. };
  134. enum escape_state {
  135. ESC_START = 1,
  136. ESC_CSI = 2,
  137. ESC_STR = 4, /* DCS, OSC, PM, APC */
  138. ESC_ALTCHARSET = 8,
  139. ESC_STR_END = 16, /* a final string was encountered */
  140. ESC_TEST = 32, /* Enter in test mode */
  141. };
  142. enum window_state {
  143. WIN_VISIBLE = 1,
  144. WIN_FOCUSED = 2
  145. };
  146. enum selection_type {
  147. SEL_REGULAR = 1,
  148. SEL_RECTANGULAR = 2
  149. };
  150. enum selection_snap {
  151. SNAP_WORD = 1,
  152. SNAP_LINE = 2
  153. };
  154. typedef unsigned char uchar;
  155. typedef unsigned int uint;
  156. typedef unsigned long ulong;
  157. typedef unsigned short ushort;
  158. typedef XftDraw *Draw;
  159. typedef XftColor Color;
  160. typedef struct {
  161. char c[UTF_SIZ]; /* character code */
  162. ushort mode; /* attribute flags */
  163. uint32_t fg; /* foreground */
  164. uint32_t bg; /* background */
  165. } Glyph;
  166. typedef Glyph *Line;
  167. typedef struct {
  168. Glyph attr; /* current char attributes */
  169. int x;
  170. int y;
  171. char state;
  172. } TCursor;
  173. /* CSI Escape sequence structs */
  174. /* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
  175. typedef struct {
  176. char buf[ESC_BUF_SIZ]; /* raw string */
  177. int len; /* raw string length */
  178. char priv;
  179. int arg[ESC_ARG_SIZ];
  180. int narg; /* nb of args */
  181. char mode[2];
  182. } CSIEscape;
  183. /* STR Escape sequence structs */
  184. /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
  185. typedef struct {
  186. char type; /* ESC type ... */
  187. char buf[STR_BUF_SIZ]; /* raw string */
  188. int len; /* raw string length */
  189. char *args[STR_ARG_SIZ];
  190. int narg; /* nb of args */
  191. } STREscape;
  192. /* Internal representation of the screen */
  193. typedef struct {
  194. int row; /* nb row */
  195. int col; /* nb col */
  196. Line *line; /* screen */
  197. Line *alt; /* alternate screen */
  198. bool *dirty; /* dirtyness of lines */
  199. TCursor c; /* cursor */
  200. int top; /* top scroll limit */
  201. int bot; /* bottom scroll limit */
  202. int mode; /* terminal mode flags */
  203. int esc; /* escape state flags */
  204. char trantbl[4]; /* charset table translation */
  205. int charset; /* current charset */
  206. int icharset; /* selected charset for sequence */
  207. bool numlock; /* lock numbers in keyboard */
  208. bool *tabs;
  209. } Term;
  210. /* Purely graphic info */
  211. typedef struct {
  212. Display *dpy;
  213. Colormap cmap;
  214. Window win;
  215. Drawable buf;
  216. Atom xembed, wmdeletewin, netwmname, netwmpid;
  217. XIM xim;
  218. XIC xic;
  219. Draw draw;
  220. Visual *vis;
  221. XSetWindowAttributes attrs;
  222. int scr;
  223. bool isfixed; /* is fixed geometry? */
  224. int l, t; /* left and top offset */
  225. int gm; /* geometry mask */
  226. int tw, th; /* tty width and height */
  227. int w, h; /* window width and height */
  228. int ch; /* char height */
  229. int cw; /* char width */
  230. char state; /* focus, redraw, visible */
  231. int cursor; /* cursor style */
  232. } XWindow;
  233. typedef struct {
  234. uint b;
  235. uint mask;
  236. char *s;
  237. } Mousekey;
  238. typedef struct {
  239. KeySym k;
  240. uint mask;
  241. char *s;
  242. /* three valued logic variables: 0 indifferent, 1 on, -1 off */
  243. signed char appkey; /* application keypad */
  244. signed char appcursor; /* application cursor */
  245. signed char crlf; /* crlf mode */
  246. } Key;
  247. typedef struct {
  248. int mode;
  249. int type;
  250. int snap;
  251. /*
  252. * Selection variables:
  253. * nb normalized coordinates of the beginning of the selection
  254. * ne normalized coordinates of the end of the selection
  255. * ob original coordinates of the beginning of the selection
  256. * oe original coordinates of the end of the selection
  257. */
  258. struct {
  259. int x, y;
  260. } nb, ne, ob, oe;
  261. char *primary, *clipboard;
  262. Atom xtarget;
  263. bool alt;
  264. struct timespec tclick1;
  265. struct timespec tclick2;
  266. } Selection;
  267. typedef union {
  268. int i;
  269. uint ui;
  270. float f;
  271. const void *v;
  272. } Arg;
  273. typedef struct {
  274. uint mod;
  275. KeySym keysym;
  276. void (*func)(const Arg *);
  277. const Arg arg;
  278. } Shortcut;
  279. /* function definitions used in config.h */
  280. static void clipcopy(const Arg *);
  281. static void clippaste(const Arg *);
  282. static void numlock(const Arg *);
  283. static void selpaste(const Arg *);
  284. static void xzoom(const Arg *);
  285. static void xzoomabs(const Arg *);
  286. static void xzoomreset(const Arg *);
  287. static void printsel(const Arg *);
  288. static void printscreen(const Arg *) ;
  289. static void toggleprinter(const Arg *);
  290. /* Config.h for applying patches and the configuration. */
  291. #include "config.h"
  292. /* Font structure */
  293. typedef struct {
  294. int height;
  295. int width;
  296. int ascent;
  297. int descent;
  298. short lbearing;
  299. short rbearing;
  300. XftFont *match;
  301. FcFontSet *set;
  302. FcPattern *pattern;
  303. } Font;
  304. /* Drawing Context */
  305. typedef struct {
  306. Color col[MAX(LEN(colorname), 256)];
  307. Font font, bfont, ifont, ibfont;
  308. GC gc;
  309. } DC;
  310. static void die(const char *, ...);
  311. static void draw(void);
  312. static void redraw(void);
  313. static void drawregion(int, int, int, int);
  314. static void execsh(void);
  315. static void stty(void);
  316. static void sigchld(int);
  317. static void run(void);
  318. static void csidump(void);
  319. static void csihandle(void);
  320. static void csiparse(void);
  321. static void csireset(void);
  322. static int eschandle(uchar);
  323. static void strdump(void);
  324. static void strhandle(void);
  325. static void strparse(void);
  326. static void strreset(void);
  327. static int tattrset(int);
  328. static void tprinter(char *, size_t);
  329. static void tdumpsel(void);
  330. static void tdumpline(int);
  331. static void tdump(void);
  332. static void tclearregion(int, int, int, int);
  333. static void tcursor(int);
  334. static void tdeletechar(int);
  335. static void tdeleteline(int);
  336. static void tinsertblank(int);
  337. static void tinsertblankline(int);
  338. static int tlinelen(int);
  339. static void tmoveto(int, int);
  340. static void tmoveato(int, int);
  341. static void tnew(int, int);
  342. static void tnewline(int);
  343. static void tputtab(int);
  344. static void tputc(char *, int);
  345. static void treset(void);
  346. static void tresize(int, int);
  347. static void tscrollup(int, int);
  348. static void tscrolldown(int, int);
  349. static void tsetattr(int *, int);
  350. static void tsetchar(char *, Glyph *, int, int);
  351. static void tsetscroll(int, int);
  352. static void tswapscreen(void);
  353. static void tsetdirt(int, int);
  354. static void tsetdirtattr(int);
  355. static void tsetmode(bool, bool, int *, int);
  356. static void tfulldirt(void);
  357. static void techo(char *, int);
  358. static void tcontrolcode(uchar );
  359. static void tdectest(char );
  360. static int32_t tdefcolor(int *, int *, int);
  361. static void tdeftran(char);
  362. static inline bool match(uint, uint);
  363. static void ttynew(void);
  364. static void ttyread(void);
  365. static void ttyresize(void);
  366. static void ttysend(char *, size_t);
  367. static void ttywrite(const char *, size_t);
  368. static void tstrsequence(uchar);
  369. static inline ushort sixd_to_16bit(int);
  370. static void xdraws(char *, Glyph, int, int, int, int);
  371. static void xhints(void);
  372. static void xclear(int, int, int, int);
  373. static void xdrawcursor(void);
  374. static void xinit(void);
  375. static void xloadcols(void);
  376. static int xsetcolorname(int, const char *);
  377. static int xgeommasktogravity(int);
  378. static int xloadfont(Font *, FcPattern *);
  379. static void xloadfonts(char *, double);
  380. static void xsettitle(char *);
  381. static void xresettitle(void);
  382. static void xsetpointermotion(int);
  383. static void xseturgency(int);
  384. static void xsetsel(char *, Time);
  385. static void xtermclear(int, int, int, int);
  386. static void xunloadfont(Font *);
  387. static void xunloadfonts(void);
  388. static void xresize(int, int);
  389. static void expose(XEvent *);
  390. static void visibility(XEvent *);
  391. static void unmap(XEvent *);
  392. static char *kmap(KeySym, uint);
  393. static void kpress(XEvent *);
  394. static void cmessage(XEvent *);
  395. static void cresize(int, int);
  396. static void resize(XEvent *);
  397. static void focus(XEvent *);
  398. static void brelease(XEvent *);
  399. static void bpress(XEvent *);
  400. static void bmotion(XEvent *);
  401. static void selnotify(XEvent *);
  402. static void selclear(XEvent *);
  403. static void selrequest(XEvent *);
  404. static void selinit(void);
  405. static void selnormalize(void);
  406. static inline bool selected(int, int);
  407. static char *getsel(void);
  408. static void selcopy(Time);
  409. static void selscroll(int, int);
  410. static void selsnap(int, int *, int *, int);
  411. static int x2col(int);
  412. static int y2row(int);
  413. static void getbuttoninfo(XEvent *);
  414. static void mousereport(XEvent *);
  415. static size_t utf8decode(char *, long *, size_t);
  416. static long utf8decodebyte(char, size_t *);
  417. static size_t utf8encode(long, char *, size_t);
  418. static char utf8encodebyte(long, size_t);
  419. static size_t utf8len(char *);
  420. static size_t utf8validate(long *, size_t);
  421. static ssize_t xwrite(int, const char *, size_t);
  422. static void *xmalloc(size_t);
  423. static void *xrealloc(void *, size_t);
  424. static char *xstrdup(char *);
  425. static void usage(void);
  426. static void (*handler[LASTEvent])(XEvent *) = {
  427. [KeyPress] = kpress,
  428. [ClientMessage] = cmessage,
  429. [ConfigureNotify] = resize,
  430. [VisibilityNotify] = visibility,
  431. [UnmapNotify] = unmap,
  432. [Expose] = expose,
  433. [FocusIn] = focus,
  434. [FocusOut] = focus,
  435. [MotionNotify] = bmotion,
  436. [ButtonPress] = bpress,
  437. [ButtonRelease] = brelease,
  438. /*
  439. * Uncomment if you want the selection to disappear when you select something
  440. * different in another window.
  441. */
  442. /* [SelectionClear] = selclear, */
  443. [SelectionNotify] = selnotify,
  444. [SelectionRequest] = selrequest,
  445. };
  446. /* Globals */
  447. static DC dc;
  448. static XWindow xw;
  449. static Term term;
  450. static CSIEscape csiescseq;
  451. static STREscape strescseq;
  452. static int cmdfd;
  453. static pid_t pid;
  454. static Selection sel;
  455. static int iofd = STDOUT_FILENO;
  456. static char **opt_cmd = NULL;
  457. static char *opt_io = NULL;
  458. static char *opt_title = NULL;
  459. static char *opt_embed = NULL;
  460. static char *opt_class = NULL;
  461. static char *opt_font = NULL;
  462. static char *opt_line = NULL;
  463. static int oldbutton = 3; /* button event on startup: 3 = release */
  464. static char *usedfont = NULL;
  465. static double usedfontsize = 0;
  466. static double defaultfontsize = 0;
  467. static uchar utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
  468. static uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
  469. static long utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
  470. static long utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
  471. /* Font Ring Cache */
  472. enum {
  473. FRC_NORMAL,
  474. FRC_ITALIC,
  475. FRC_BOLD,
  476. FRC_ITALICBOLD
  477. };
  478. typedef struct {
  479. XftFont *font;
  480. int flags;
  481. long unicodep;
  482. } Fontcache;
  483. /* Fontcache is an array now. A new font will be appended to the array. */
  484. static Fontcache frc[16];
  485. static int frclen = 0;
  486. ssize_t
  487. xwrite(int fd, const char *s, size_t len) {
  488. size_t aux = len;
  489. while(len > 0) {
  490. ssize_t r = write(fd, s, len);
  491. if(r < 0)
  492. return r;
  493. len -= r;
  494. s += r;
  495. }
  496. return aux;
  497. }
  498. void *
  499. xmalloc(size_t len) {
  500. void *p = malloc(len);
  501. if(!p)
  502. die("Out of memory\n");
  503. return p;
  504. }
  505. void *
  506. xrealloc(void *p, size_t len) {
  507. if((p = realloc(p, len)) == NULL)
  508. die("Out of memory\n");
  509. return p;
  510. }
  511. char *
  512. xstrdup(char *s) {
  513. if((s = strdup(s)) == NULL)
  514. die("Out of memory\n");
  515. return s;
  516. }
  517. size_t
  518. utf8decode(char *c, long *u, size_t clen) {
  519. size_t i, j, len, type;
  520. long udecoded;
  521. *u = UTF_INVALID;
  522. if(!clen)
  523. return 0;
  524. udecoded = utf8decodebyte(c[0], &len);
  525. if(!BETWEEN(len, 1, UTF_SIZ))
  526. return 1;
  527. for(i = 1, j = 1; i < clen && j < len; ++i, ++j) {
  528. udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
  529. if(type != 0)
  530. return j;
  531. }
  532. if(j < len)
  533. return 0;
  534. *u = udecoded;
  535. utf8validate(u, len);
  536. return len;
  537. }
  538. long
  539. utf8decodebyte(char c, size_t *i) {
  540. for(*i = 0; *i < LEN(utfmask); ++(*i))
  541. if(((uchar)c & utfmask[*i]) == utfbyte[*i])
  542. return (uchar)c & ~utfmask[*i];
  543. return 0;
  544. }
  545. size_t
  546. utf8encode(long u, char *c, size_t clen) {
  547. size_t len, i;
  548. len = utf8validate(&u, 0);
  549. if(clen < len)
  550. return 0;
  551. for(i = len - 1; i != 0; --i) {
  552. c[i] = utf8encodebyte(u, 0);
  553. u >>= 6;
  554. }
  555. c[0] = utf8encodebyte(u, len);
  556. return len;
  557. }
  558. char
  559. utf8encodebyte(long u, size_t i) {
  560. return utfbyte[i] | (u & ~utfmask[i]);
  561. }
  562. size_t
  563. utf8len(char *c) {
  564. return utf8decode(c, &(long){0}, UTF_SIZ);
  565. }
  566. size_t
  567. utf8validate(long *u, size_t i) {
  568. if(!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF))
  569. *u = UTF_INVALID;
  570. for(i = 1; *u > utfmax[i]; ++i)
  571. ;
  572. return i;
  573. }
  574. void
  575. selinit(void) {
  576. memset(&sel.tclick1, 0, sizeof(sel.tclick1));
  577. memset(&sel.tclick2, 0, sizeof(sel.tclick2));
  578. sel.mode = 0;
  579. sel.ob.x = -1;
  580. sel.primary = NULL;
  581. sel.clipboard = NULL;
  582. sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
  583. if(sel.xtarget == None)
  584. sel.xtarget = XA_STRING;
  585. }
  586. int
  587. x2col(int x) {
  588. x -= borderpx;
  589. x /= xw.cw;
  590. return LIMIT(x, 0, term.col-1);
  591. }
  592. int
  593. y2row(int y) {
  594. y -= borderpx;
  595. y /= xw.ch;
  596. return LIMIT(y, 0, term.row-1);
  597. }
  598. int
  599. tlinelen(int y) {
  600. int i = term.col;
  601. if(term.line[y][i - 1].mode & ATTR_WRAP)
  602. return i;
  603. while(i > 0 && term.line[y][i - 1].c[0] == ' ')
  604. --i;
  605. return i;
  606. }
  607. void
  608. selnormalize(void) {
  609. int i;
  610. if(sel.ob.y == sel.oe.y || sel.type == SEL_RECTANGULAR) {
  611. sel.nb.x = MIN(sel.ob.x, sel.oe.x);
  612. sel.ne.x = MAX(sel.ob.x, sel.oe.x);
  613. } else {
  614. sel.nb.x = sel.ob.y < sel.oe.y ? sel.ob.x : sel.oe.x;
  615. sel.ne.x = sel.ob.y < sel.oe.y ? sel.oe.x : sel.ob.x;
  616. }
  617. sel.nb.y = MIN(sel.ob.y, sel.oe.y);
  618. sel.ne.y = MAX(sel.ob.y, sel.oe.y);
  619. selsnap(sel.snap, &sel.nb.x, &sel.nb.y, -1);
  620. selsnap(sel.snap, &sel.ne.x, &sel.ne.y, +1);
  621. /* expand selection over line breaks */
  622. if (sel.type == SEL_RECTANGULAR)
  623. return;
  624. i = tlinelen(sel.nb.y);
  625. if (i < sel.nb.x)
  626. sel.nb.x = i;
  627. if (tlinelen(sel.ne.y) <= sel.ne.x)
  628. sel.ne.x = term.col - 1;
  629. }
  630. bool
  631. selected(int x, int y) {
  632. if(sel.type == SEL_RECTANGULAR)
  633. return BETWEEN(y, sel.nb.y, sel.ne.y)
  634. && BETWEEN(x, sel.nb.x, sel.ne.x);
  635. return BETWEEN(y, sel.nb.y, sel.ne.y)
  636. && (y != sel.nb.y || x >= sel.nb.x)
  637. && (y != sel.ne.y || x <= sel.ne.x);
  638. }
  639. void
  640. selsnap(int mode, int *x, int *y, int direction) {
  641. int newx, newy, xt, yt;
  642. bool delim, prevdelim;
  643. Glyph *gp, *prevgp;
  644. switch(mode) {
  645. case SNAP_WORD:
  646. /*
  647. * Snap around if the word wraps around at the end or
  648. * beginning of a line.
  649. */
  650. prevgp = &term.line[*y][*x];
  651. prevdelim = strchr(worddelimiters, prevgp->c[0]) != NULL;
  652. for(;;) {
  653. newx = *x + direction;
  654. newy = *y;
  655. if(!BETWEEN(newx, 0, term.col - 1)) {
  656. newy += direction;
  657. newx = (newx + term.col) % term.col;
  658. if (!BETWEEN(newy, 0, term.row - 1))
  659. break;
  660. if(direction > 0)
  661. yt = *y, xt = *x;
  662. else
  663. yt = newy, xt = newx;
  664. if(!(term.line[yt][xt].mode & ATTR_WRAP))
  665. break;
  666. }
  667. if (newx >= tlinelen(newy))
  668. break;
  669. gp = &term.line[newy][newx];
  670. delim = strchr(worddelimiters, gp->c[0]) != NULL;
  671. if(!(gp->mode & ATTR_WDUMMY) && (delim != prevdelim
  672. || (delim && gp->c[0] != prevgp->c[0])))
  673. break;
  674. *x = newx;
  675. *y = newy;
  676. prevgp = gp;
  677. prevdelim = delim;
  678. }
  679. break;
  680. case SNAP_LINE:
  681. /*
  682. * Snap around if the the previous line or the current one
  683. * has set ATTR_WRAP at its end. Then the whole next or
  684. * previous line will be selected.
  685. */
  686. *x = (direction < 0) ? 0 : term.col - 1;
  687. if(direction < 0 && *y > 0) {
  688. for(; *y > 0; *y += direction) {
  689. if(!(term.line[*y-1][term.col-1].mode
  690. & ATTR_WRAP)) {
  691. break;
  692. }
  693. }
  694. } else if(direction > 0 && *y < term.row-1) {
  695. for(; *y < term.row; *y += direction) {
  696. if(!(term.line[*y][term.col-1].mode
  697. & ATTR_WRAP)) {
  698. break;
  699. }
  700. }
  701. }
  702. break;
  703. }
  704. }
  705. void
  706. getbuttoninfo(XEvent *e) {
  707. int type;
  708. uint state = e->xbutton.state & ~(Button1Mask | forceselmod);
  709. sel.alt = IS_SET(MODE_ALTSCREEN);
  710. sel.oe.x = x2col(e->xbutton.x);
  711. sel.oe.y = y2row(e->xbutton.y);
  712. selnormalize();
  713. sel.type = SEL_REGULAR;
  714. for(type = 1; type < LEN(selmasks); ++type) {
  715. if(match(selmasks[type], state)) {
  716. sel.type = type;
  717. break;
  718. }
  719. }
  720. }
  721. void
  722. mousereport(XEvent *e) {
  723. int x = x2col(e->xbutton.x), y = y2row(e->xbutton.y),
  724. button = e->xbutton.button, state = e->xbutton.state,
  725. len;
  726. char buf[40];
  727. static int ox, oy;
  728. /* from urxvt */
  729. if(e->xbutton.type == MotionNotify) {
  730. if(x == ox && y == oy)
  731. return;
  732. if(!IS_SET(MODE_MOUSEMOTION) && !IS_SET(MODE_MOUSEMANY))
  733. return;
  734. /* MOUSE_MOTION: no reporting if no button is pressed */
  735. if(IS_SET(MODE_MOUSEMOTION) && oldbutton == 3)
  736. return;
  737. button = oldbutton + 32;
  738. ox = x;
  739. oy = y;
  740. } else {
  741. if(!IS_SET(MODE_MOUSESGR) && e->xbutton.type == ButtonRelease) {
  742. button = 3;
  743. } else {
  744. button -= Button1;
  745. if(button >= 3)
  746. button += 64 - 3;
  747. }
  748. if(e->xbutton.type == ButtonPress) {
  749. oldbutton = button;
  750. ox = x;
  751. oy = y;
  752. } else if(e->xbutton.type == ButtonRelease) {
  753. oldbutton = 3;
  754. /* MODE_MOUSEX10: no button release reporting */
  755. if(IS_SET(MODE_MOUSEX10))
  756. return;
  757. if (button == 64 || button == 65)
  758. return;
  759. }
  760. }
  761. if(!IS_SET(MODE_MOUSEX10)) {
  762. button += (state & ShiftMask ? 4 : 0)
  763. + (state & Mod4Mask ? 8 : 0)
  764. + (state & ControlMask ? 16 : 0);
  765. }
  766. len = 0;
  767. if(IS_SET(MODE_MOUSESGR)) {
  768. len = snprintf(buf, sizeof(buf), "\033[<%d;%d;%d%c",
  769. button, x+1, y+1,
  770. e->xbutton.type == ButtonRelease ? 'm' : 'M');
  771. } else if(x < 223 && y < 223) {
  772. len = snprintf(buf, sizeof(buf), "\033[M%c%c%c",
  773. 32+button, 32+x+1, 32+y+1);
  774. } else {
  775. return;
  776. }
  777. ttywrite(buf, len);
  778. }
  779. void
  780. bpress(XEvent *e) {
  781. struct timespec now;
  782. Mousekey *mk;
  783. if(IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
  784. mousereport(e);
  785. return;
  786. }
  787. for(mk = mshortcuts; mk < mshortcuts + LEN(mshortcuts); mk++) {
  788. if(e->xbutton.button == mk->b
  789. && match(mk->mask, e->xbutton.state)) {
  790. ttysend(mk->s, strlen(mk->s));
  791. return;
  792. }
  793. }
  794. if(e->xbutton.button == Button1) {
  795. clock_gettime(CLOCK_MONOTONIC, &now);
  796. /* Clear previous selection, logically and visually. */
  797. selclear(NULL);
  798. sel.mode = 1;
  799. sel.type = SEL_REGULAR;
  800. sel.oe.x = sel.ob.x = x2col(e->xbutton.x);
  801. sel.oe.y = sel.ob.y = y2row(e->xbutton.y);
  802. /*
  803. * If the user clicks below predefined timeouts specific
  804. * snapping behaviour is exposed.
  805. */
  806. if(TIMEDIFF(now, sel.tclick2) <= tripleclicktimeout) {
  807. sel.snap = SNAP_LINE;
  808. } else if(TIMEDIFF(now, sel.tclick1) <= doubleclicktimeout) {
  809. sel.snap = SNAP_WORD;
  810. } else {
  811. sel.snap = 0;
  812. }
  813. selnormalize();
  814. /*
  815. * Draw selection, unless it's regular and we don't want to
  816. * make clicks visible
  817. */
  818. if(sel.snap != 0) {
  819. sel.mode++;
  820. tsetdirt(sel.nb.y, sel.ne.y);
  821. }
  822. sel.tclick2 = sel.tclick1;
  823. sel.tclick1 = now;
  824. }
  825. }
  826. char *
  827. getsel(void) {
  828. char *str, *ptr;
  829. int y, bufsize, size, lastx, linelen;
  830. Glyph *gp, *last;
  831. if(sel.ob.x == -1)
  832. return NULL;
  833. bufsize = (term.col+1) * (sel.ne.y-sel.nb.y+1) * UTF_SIZ;
  834. ptr = str = xmalloc(bufsize);
  835. /* append every set & selected glyph to the selection */
  836. for(y = sel.nb.y; y <= sel.ne.y; y++) {
  837. linelen = tlinelen(y);
  838. if(sel.type == SEL_RECTANGULAR) {
  839. gp = &term.line[y][sel.nb.x];
  840. lastx = sel.ne.x;
  841. } else {
  842. gp = &term.line[y][sel.nb.y == y ? sel.nb.x : 0];
  843. lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1;
  844. }
  845. last = &term.line[y][MIN(lastx, linelen-1)];
  846. while(last >= gp && last->c[0] == ' ')
  847. --last;
  848. for( ; gp <= last; ++gp) {
  849. if(gp->mode & ATTR_WDUMMY)
  850. continue;
  851. size = utf8len(gp->c);
  852. memcpy(ptr, gp->c, size);
  853. ptr += size;
  854. }
  855. /*
  856. * Copy and pasting of line endings is inconsistent
  857. * in the inconsistent terminal and GUI world.
  858. * The best solution seems like to produce '\n' when
  859. * something is copied from st and convert '\n' to
  860. * '\r', when something to be pasted is received by
  861. * st.
  862. * FIXME: Fix the computer world.
  863. */
  864. if((y < sel.ne.y || lastx >= linelen) && !(last->mode & ATTR_WRAP))
  865. *ptr++ = '\n';
  866. }
  867. *ptr = 0;
  868. return str;
  869. }
  870. void
  871. selcopy(Time t) {
  872. xsetsel(getsel(), t);
  873. }
  874. void
  875. selnotify(XEvent *e) {
  876. ulong nitems, ofs, rem;
  877. int format;
  878. uchar *data, *last, *repl;
  879. Atom type;
  880. XSelectionEvent *xsev;
  881. ofs = 0;
  882. xsev = &e->xselection;
  883. if (xsev->property == None)
  884. return;
  885. do {
  886. if(XGetWindowProperty(xw.dpy, xw.win, xsev->property, ofs,
  887. BUFSIZ/4, False, AnyPropertyType,
  888. &type, &format, &nitems, &rem,
  889. &data)) {
  890. fprintf(stderr, "Clipboard allocation failed\n");
  891. return;
  892. }
  893. /*
  894. * As seen in getsel:
  895. * Line endings are inconsistent in the terminal and GUI world
  896. * copy and pasting. When receiving some selection data,
  897. * replace all '\n' with '\r'.
  898. * FIXME: Fix the computer world.
  899. */
  900. repl = data;
  901. last = data + nitems * format / 8;
  902. while((repl = memchr(repl, '\n', last - repl))) {
  903. *repl++ = '\r';
  904. }
  905. if(IS_SET(MODE_BRCKTPASTE))
  906. ttywrite("\033[200~", 6);
  907. ttysend((char *)data, nitems * format / 8);
  908. if(IS_SET(MODE_BRCKTPASTE))
  909. ttywrite("\033[201~", 6);
  910. XFree(data);
  911. /* number of 32-bit chunks returned */
  912. ofs += nitems * format / 32;
  913. } while(rem > 0);
  914. }
  915. void
  916. selpaste(const Arg *dummy) {
  917. XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY,
  918. xw.win, CurrentTime);
  919. }
  920. void
  921. clipcopy(const Arg *dummy) {
  922. Atom clipboard;
  923. if(sel.clipboard != NULL)
  924. free(sel.clipboard);
  925. if(sel.primary != NULL) {
  926. sel.clipboard = xstrdup(sel.primary);
  927. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  928. XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
  929. }
  930. }
  931. void
  932. clippaste(const Arg *dummy) {
  933. Atom clipboard;
  934. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  935. XConvertSelection(xw.dpy, clipboard, sel.xtarget, clipboard,
  936. xw.win, CurrentTime);
  937. }
  938. void
  939. selclear(XEvent *e) {
  940. if(sel.ob.x == -1)
  941. return;
  942. sel.ob.x = -1;
  943. tsetdirt(sel.nb.y, sel.ne.y);
  944. }
  945. void
  946. selrequest(XEvent *e) {
  947. XSelectionRequestEvent *xsre;
  948. XSelectionEvent xev;
  949. Atom xa_targets, string, clipboard;
  950. char *seltext;
  951. xsre = (XSelectionRequestEvent *) e;
  952. xev.type = SelectionNotify;
  953. xev.requestor = xsre->requestor;
  954. xev.selection = xsre->selection;
  955. xev.target = xsre->target;
  956. xev.time = xsre->time;
  957. if (xsre->property == None)
  958. xsre->property = xsre->target;
  959. /* reject */
  960. xev.property = None;
  961. xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
  962. if(xsre->target == xa_targets) {
  963. /* respond with the supported type */
  964. string = sel.xtarget;
  965. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  966. XA_ATOM, 32, PropModeReplace,
  967. (uchar *) &string, 1);
  968. xev.property = xsre->property;
  969. } else if(xsre->target == sel.xtarget || xsre->target == XA_STRING) {
  970. /*
  971. * xith XA_STRING non ascii characters may be incorrect in the
  972. * requestor. It is not our problem, use utf8.
  973. */
  974. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  975. if(xsre->selection == XA_PRIMARY) {
  976. seltext = sel.primary;
  977. } else if(xsre->selection == clipboard) {
  978. seltext = sel.clipboard;
  979. } else {
  980. fprintf(stderr,
  981. "Unhandled clipboard selection 0x%lx\n",
  982. xsre->selection);
  983. return;
  984. }
  985. if(seltext != NULL) {
  986. XChangeProperty(xsre->display, xsre->requestor,
  987. xsre->property, xsre->target,
  988. 8, PropModeReplace,
  989. (uchar *)seltext, strlen(seltext));
  990. xev.property = xsre->property;
  991. }
  992. }
  993. /* all done, send a notification to the listener */
  994. if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
  995. fprintf(stderr, "Error sending SelectionNotify event\n");
  996. }
  997. void
  998. xsetsel(char *str, Time t) {
  999. free(sel.primary);
  1000. sel.primary = str;
  1001. XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, t);
  1002. if (XGetSelectionOwner(xw.dpy, XA_PRIMARY) != xw.win)
  1003. selclear(0);
  1004. }
  1005. void
  1006. brelease(XEvent *e) {
  1007. if(IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
  1008. mousereport(e);
  1009. return;
  1010. }
  1011. if(e->xbutton.button == Button2) {
  1012. selpaste(NULL);
  1013. } else if(e->xbutton.button == Button1) {
  1014. if(sel.mode < 2) {
  1015. selclear(NULL);
  1016. } else {
  1017. getbuttoninfo(e);
  1018. selcopy(e->xbutton.time);
  1019. }
  1020. sel.mode = 0;
  1021. tsetdirt(sel.nb.y, sel.ne.y);
  1022. }
  1023. }
  1024. void
  1025. bmotion(XEvent *e) {
  1026. int oldey, oldex, oldsby, oldsey;
  1027. if(IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
  1028. mousereport(e);
  1029. return;
  1030. }
  1031. if(!sel.mode)
  1032. return;
  1033. sel.mode++;
  1034. oldey = sel.oe.y;
  1035. oldex = sel.oe.x;
  1036. oldsby = sel.nb.y;
  1037. oldsey = sel.ne.y;
  1038. getbuttoninfo(e);
  1039. if(oldey != sel.oe.y || oldex != sel.oe.x)
  1040. tsetdirt(MIN(sel.nb.y, oldsby), MAX(sel.ne.y, oldsey));
  1041. }
  1042. void
  1043. die(const char *errstr, ...) {
  1044. va_list ap;
  1045. va_start(ap, errstr);
  1046. vfprintf(stderr, errstr, ap);
  1047. va_end(ap);
  1048. exit(EXIT_FAILURE);
  1049. }
  1050. void
  1051. execsh(void) {
  1052. char **args, *sh, *prog;
  1053. const struct passwd *pw;
  1054. char buf[sizeof(long) * 8 + 1];
  1055. errno = 0;
  1056. if((pw = getpwuid(getuid())) == NULL) {
  1057. if(errno)
  1058. die("getpwuid:%s\n", strerror(errno));
  1059. else
  1060. die("who are you?\n");
  1061. }
  1062. if (!(sh = getenv("SHELL"))) {
  1063. sh = (pw->pw_shell[0]) ? pw->pw_shell : shell;
  1064. }
  1065. if(opt_cmd)
  1066. prog = opt_cmd[0];
  1067. else if(utmp)
  1068. prog = utmp;
  1069. else
  1070. prog = sh;
  1071. args = (opt_cmd) ? opt_cmd : (char *[]) {prog, NULL};
  1072. snprintf(buf, sizeof(buf), "%lu", xw.win);
  1073. unsetenv("COLUMNS");
  1074. unsetenv("LINES");
  1075. unsetenv("TERMCAP");
  1076. setenv("LOGNAME", pw->pw_name, 1);
  1077. setenv("USER", pw->pw_name, 1);
  1078. setenv("SHELL", sh, 1);
  1079. setenv("HOME", pw->pw_dir, 1);
  1080. setenv("TERM", termname, 1);
  1081. setenv("WINDOWID", buf, 1);
  1082. signal(SIGCHLD, SIG_DFL);
  1083. signal(SIGHUP, SIG_DFL);
  1084. signal(SIGINT, SIG_DFL);
  1085. signal(SIGQUIT, SIG_DFL);
  1086. signal(SIGTERM, SIG_DFL);
  1087. signal(SIGALRM, SIG_DFL);
  1088. execvp(prog, args);
  1089. _exit(EXIT_FAILURE);
  1090. }
  1091. void
  1092. sigchld(int a) {
  1093. int stat, ret;
  1094. if(waitpid(pid, &stat, 0) < 0)
  1095. die("Waiting for pid %hd failed: %s\n", pid, strerror(errno));
  1096. ret = WIFEXITED(stat) ? WEXITSTATUS(stat) : EXIT_FAILURE;
  1097. if (ret != EXIT_SUCCESS)
  1098. die("child finished with error '%d'\n", stat);
  1099. exit(EXIT_SUCCESS);
  1100. }
  1101. void
  1102. stty(void)
  1103. {
  1104. char cmd[_POSIX_ARG_MAX], **p, *q, *s;
  1105. size_t n, siz;
  1106. if((n = strlen(stty_args)) > sizeof(cmd)-1)
  1107. die("incorrect stty parameters\n");
  1108. memcpy(cmd, stty_args, n);
  1109. q = cmd + n;
  1110. siz = sizeof(cmd) - n;
  1111. for(p = opt_cmd; p && (s = *p); ++p) {
  1112. if((n = strlen(s)) > siz-1)
  1113. die("stty parameter length too long\n");
  1114. *q++ = ' ';
  1115. q = memcpy(q, s, n);
  1116. q += n;
  1117. siz-= n + 1;
  1118. }
  1119. *q = '\0';
  1120. if (system(cmd) != 0)
  1121. perror("Couldn't call stty");
  1122. }
  1123. void
  1124. ttynew(void) {
  1125. int m, s;
  1126. struct winsize w = {term.row, term.col, 0, 0};
  1127. if(opt_io) {
  1128. term.mode |= MODE_PRINT;
  1129. iofd = (!strcmp(opt_io, "-")) ?
  1130. STDOUT_FILENO :
  1131. open(opt_io, O_WRONLY | O_CREAT, 0666);
  1132. if(iofd < 0) {
  1133. fprintf(stderr, "Error opening %s:%s\n",
  1134. opt_io, strerror(errno));
  1135. }
  1136. }
  1137. if (opt_line) {
  1138. if((cmdfd = open(opt_line, O_RDWR)) < 0)
  1139. die("open line failed: %s\n", strerror(errno));
  1140. close(STDIN_FILENO);
  1141. dup(cmdfd);
  1142. stty();
  1143. return;
  1144. }
  1145. /* seems to work fine on linux, openbsd and freebsd */
  1146. if(openpty(&m, &s, NULL, NULL, &w) < 0)
  1147. die("openpty failed: %s\n", strerror(errno));
  1148. switch(pid = fork()) {
  1149. case -1:
  1150. die("fork failed\n");
  1151. break;
  1152. case 0:
  1153. close(iofd);
  1154. setsid(); /* create a new process group */
  1155. dup2(s, STDIN_FILENO);
  1156. dup2(s, STDOUT_FILENO);
  1157. dup2(s, STDERR_FILENO);
  1158. if(ioctl(s, TIOCSCTTY, NULL) < 0)
  1159. die("ioctl TIOCSCTTY failed: %s\n", strerror(errno));
  1160. close(s);
  1161. close(m);
  1162. execsh();
  1163. break;
  1164. default:
  1165. close(s);
  1166. cmdfd = m;
  1167. signal(SIGCHLD, sigchld);
  1168. break;
  1169. }
  1170. }
  1171. void
  1172. ttyread(void) {
  1173. static char buf[BUFSIZ];
  1174. static int buflen = 0;
  1175. char *ptr;
  1176. char s[UTF_SIZ];
  1177. int charsize; /* size of utf8 char in bytes */
  1178. long unicodep;
  1179. int ret;
  1180. /* append read bytes to unprocessed bytes */
  1181. if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
  1182. die("Couldn't read from shell: %s\n", strerror(errno));
  1183. /* process every complete utf8 char */
  1184. buflen += ret;
  1185. ptr = buf;
  1186. while((charsize = utf8decode(ptr, &unicodep, buflen))) {
  1187. utf8encode(unicodep, s, UTF_SIZ);
  1188. tputc(s, charsize);
  1189. ptr += charsize;
  1190. buflen -= charsize;
  1191. }
  1192. /* keep any uncomplete utf8 char for the next call */
  1193. memmove(buf, ptr, buflen);
  1194. }
  1195. void
  1196. ttywrite(const char *s, size_t n) {
  1197. if(xwrite(cmdfd, s, n) == -1)
  1198. die("write error on tty: %s\n", strerror(errno));
  1199. }
  1200. void
  1201. ttysend(char *s, size_t n) {
  1202. ttywrite(s, n);
  1203. if(IS_SET(MODE_ECHO))
  1204. techo(s, n);
  1205. }
  1206. void
  1207. ttyresize(void) {
  1208. struct winsize w;
  1209. w.ws_row = term.row;
  1210. w.ws_col = term.col;
  1211. w.ws_xpixel = xw.tw;
  1212. w.ws_ypixel = xw.th;
  1213. if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  1214. fprintf(stderr, "Couldn't set window size: %s\n", strerror(errno));
  1215. }
  1216. int
  1217. tattrset(int attr) {
  1218. int i, j;
  1219. for(i = 0; i < term.row-1; i++) {
  1220. for(j = 0; j < term.col-1; j++) {
  1221. if(term.line[i][j].mode & attr)
  1222. return 1;
  1223. }
  1224. }
  1225. return 0;
  1226. }
  1227. void
  1228. tsetdirt(int top, int bot) {
  1229. int i;
  1230. LIMIT(top, 0, term.row-1);
  1231. LIMIT(bot, 0, term.row-1);
  1232. for(i = top; i <= bot; i++)
  1233. term.dirty[i] = 1;
  1234. }
  1235. void
  1236. tsetdirtattr(int attr) {
  1237. int i, j;
  1238. for(i = 0; i < term.row-1; i++) {
  1239. for(j = 0; j < term.col-1; j++) {
  1240. if(term.line[i][j].mode & attr) {
  1241. tsetdirt(i, i);
  1242. break;
  1243. }
  1244. }
  1245. }
  1246. }
  1247. void
  1248. tfulldirt(void) {
  1249. tsetdirt(0, term.row-1);
  1250. }
  1251. void
  1252. tcursor(int mode) {
  1253. static TCursor c[2];
  1254. bool alt = IS_SET(MODE_ALTSCREEN);
  1255. if(mode == CURSOR_SAVE) {
  1256. c[alt] = term.c;
  1257. } else if(mode == CURSOR_LOAD) {
  1258. term.c = c[alt];
  1259. tmoveto(c[alt].x, c[alt].y);
  1260. }
  1261. }
  1262. void
  1263. treset(void) {
  1264. uint i;
  1265. term.c = (TCursor){{
  1266. .mode = ATTR_NULL,
  1267. .fg = defaultfg,
  1268. .bg = defaultbg
  1269. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  1270. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  1271. for(i = tabspaces; i < term.col; i += tabspaces)
  1272. term.tabs[i] = 1;
  1273. term.top = 0;
  1274. term.bot = term.row - 1;
  1275. term.mode = MODE_WRAP;
  1276. memset(term.trantbl, CS_USA, sizeof(term.trantbl));
  1277. term.charset = 0;
  1278. for(i = 0; i < 2; i++) {
  1279. tmoveto(0, 0);
  1280. tcursor(CURSOR_SAVE);
  1281. tclearregion(0, 0, term.col-1, term.row-1);
  1282. tswapscreen();
  1283. }
  1284. }
  1285. void
  1286. tnew(int col, int row) {
  1287. term = (Term){ .c = { .attr = { .fg = defaultfg, .bg = defaultbg } } };
  1288. tresize(col, row);
  1289. term.numlock = 1;
  1290. treset();
  1291. }
  1292. void
  1293. tswapscreen(void) {
  1294. Line *tmp = term.line;
  1295. term.line = term.alt;
  1296. term.alt = tmp;
  1297. term.mode ^= MODE_ALTSCREEN;
  1298. tfulldirt();
  1299. }
  1300. void
  1301. tscrolldown(int orig, int n) {
  1302. int i;
  1303. Line temp;
  1304. LIMIT(n, 0, term.bot-orig+1);
  1305. tsetdirt(orig, term.bot-n);
  1306. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  1307. for(i = term.bot; i >= orig+n; i--) {
  1308. temp = term.line[i];
  1309. term.line[i] = term.line[i-n];
  1310. term.line[i-n] = temp;
  1311. }
  1312. selscroll(orig, n);
  1313. }
  1314. void
  1315. tscrollup(int orig, int n) {
  1316. int i;
  1317. Line temp;
  1318. LIMIT(n, 0, term.bot-orig+1);
  1319. tclearregion(0, orig, term.col-1, orig+n-1);
  1320. tsetdirt(orig+n, term.bot);
  1321. for(i = orig; i <= term.bot-n; i++) {
  1322. temp = term.line[i];
  1323. term.line[i] = term.line[i+n];
  1324. term.line[i+n] = temp;
  1325. }
  1326. selscroll(orig, -n);
  1327. }
  1328. void
  1329. selscroll(int orig, int n) {
  1330. if(sel.ob.x == -1)
  1331. return;
  1332. if(BETWEEN(sel.ob.y, orig, term.bot) || BETWEEN(sel.oe.y, orig, term.bot)) {
  1333. if((sel.ob.y += n) > term.bot || (sel.oe.y += n) < term.top) {
  1334. selclear(NULL);
  1335. return;
  1336. }
  1337. if(sel.type == SEL_RECTANGULAR) {
  1338. if(sel.ob.y < term.top)
  1339. sel.ob.y = term.top;
  1340. if(sel.oe.y > term.bot)
  1341. sel.oe.y = term.bot;
  1342. } else {
  1343. if(sel.ob.y < term.top) {
  1344. sel.ob.y = term.top;
  1345. sel.ob.x = 0;
  1346. }
  1347. if(sel.oe.y > term.bot) {
  1348. sel.oe.y = term.bot;
  1349. sel.oe.x = term.col;
  1350. }
  1351. }
  1352. selnormalize();
  1353. }
  1354. }
  1355. void
  1356. tnewline(int first_col) {
  1357. int y = term.c.y;
  1358. if(y == term.bot) {
  1359. tscrollup(term.top, 1);
  1360. } else {
  1361. y++;
  1362. }
  1363. tmoveto(first_col ? 0 : term.c.x, y);
  1364. }
  1365. void
  1366. csiparse(void) {
  1367. char *p = csiescseq.buf, *np;
  1368. long int v;
  1369. csiescseq.narg = 0;
  1370. if(*p == '?') {
  1371. csiescseq.priv = 1;
  1372. p++;
  1373. }
  1374. csiescseq.buf[csiescseq.len] = '\0';
  1375. while(p < csiescseq.buf+csiescseq.len) {
  1376. np = NULL;
  1377. v = strtol(p, &np, 10);
  1378. if(np == p)
  1379. v = 0;
  1380. if(v == LONG_MAX || v == LONG_MIN)
  1381. v = -1;
  1382. csiescseq.arg[csiescseq.narg++] = v;
  1383. p = np;
  1384. if(*p != ';' || csiescseq.narg == ESC_ARG_SIZ)
  1385. break;
  1386. p++;
  1387. }
  1388. csiescseq.mode[0] = *p++;
  1389. csiescseq.mode[1] = (p < csiescseq.buf+csiescseq.len) ? *p : '\0';
  1390. }
  1391. /* for absolute user moves, when decom is set */
  1392. void
  1393. tmoveato(int x, int y) {
  1394. tmoveto(x, y + ((term.c.state & CURSOR_ORIGIN) ? term.top: 0));
  1395. }
  1396. void
  1397. tmoveto(int x, int y) {
  1398. int miny, maxy;
  1399. if(term.c.state & CURSOR_ORIGIN) {
  1400. miny = term.top;
  1401. maxy = term.bot;
  1402. } else {
  1403. miny = 0;
  1404. maxy = term.row - 1;
  1405. }
  1406. term.c.state &= ~CURSOR_WRAPNEXT;
  1407. term.c.x = LIMIT(x, 0, term.col-1);
  1408. term.c.y = LIMIT(y, miny, maxy);
  1409. }
  1410. void
  1411. tsetchar(char *c, Glyph *attr, int x, int y) {
  1412. static char *vt100_0[62] = { /* 0x41 - 0x7e */
  1413. "", "", "", "", "", "", "", /* A - G */
  1414. 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
  1415. 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
  1416. 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
  1417. "", "", "", "", "", "", "°", "±", /* ` - g */
  1418. "", "", "", "", "", "", "", "", /* h - o */
  1419. "", "", "", "", "", "", "", "", /* p - w */
  1420. "", "", "", "π", "", "£", "·", /* x - ~ */
  1421. };
  1422. /*
  1423. * The table is proudly stolen from rxvt.
  1424. */
  1425. if(term.trantbl[term.charset] == CS_GRAPHIC0) {
  1426. if(BETWEEN(c[0], 0x41, 0x7e) && vt100_0[c[0] - 0x41]) {
  1427. c = vt100_0[c[0] - 0x41];
  1428. }
  1429. }
  1430. if(term.line[y][x].mode & ATTR_WIDE) {
  1431. if(x+1 < term.col) {
  1432. term.line[y][x+1].c[0] = ' ';
  1433. term.line[y][x+1].mode &= ~ATTR_WDUMMY;
  1434. }
  1435. } else if(term.line[y][x].mode & ATTR_WDUMMY) {
  1436. term.line[y][x-1].c[0] = ' ';
  1437. term.line[y][x-1].mode &= ~ATTR_WIDE;
  1438. }
  1439. term.dirty[y] = 1;
  1440. term.line[y][x] = *attr;
  1441. memcpy(term.line[y][x].c, c, UTF_SIZ);
  1442. }
  1443. void
  1444. tclearregion(int x1, int y1, int x2, int y2) {
  1445. int x, y, temp;
  1446. Glyph *gp;
  1447. if(x1 > x2)
  1448. temp = x1, x1 = x2, x2 = temp;
  1449. if(y1 > y2)
  1450. temp = y1, y1 = y2, y2 = temp;
  1451. LIMIT(x1, 0, term.col-1);
  1452. LIMIT(x2, 0, term.col-1);
  1453. LIMIT(y1, 0, term.row-1);
  1454. LIMIT(y2, 0, term.row-1);
  1455. for(y = y1; y <= y2; y++) {
  1456. term.dirty[y] = 1;
  1457. for(x = x1; x <= x2; x++) {
  1458. gp = &term.line[y][x];
  1459. if(selected(x, y))
  1460. selclear(NULL);
  1461. gp->fg = term.c.attr.fg;
  1462. gp->bg = term.c.attr.bg;
  1463. gp->mode = 0;
  1464. memcpy(gp->c, " ", 2);
  1465. }
  1466. }
  1467. }
  1468. void
  1469. tdeletechar(int n) {
  1470. int dst, src, size;
  1471. Glyph *line;
  1472. LIMIT(n, 0, term.col - term.c.x);
  1473. dst = term.c.x;
  1474. src = term.c.x + n;
  1475. size = term.col - src;
  1476. line = term.line[term.c.y];
  1477. memmove(&line[dst], &line[src], size * sizeof(Glyph));
  1478. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  1479. }
  1480. void
  1481. tinsertblank(int n) {
  1482. int dst, src, size;
  1483. Glyph *line;
  1484. LIMIT(n, 0, term.col - term.c.x);
  1485. dst = term.c.x + n;
  1486. src = term.c.x;
  1487. size = term.col - dst;
  1488. line = term.line[term.c.y];
  1489. memmove(&line[dst], &line[src], size * sizeof(Glyph));
  1490. tclearregion(src, term.c.y, dst - 1, term.c.y);
  1491. }
  1492. void
  1493. tinsertblankline(int n) {
  1494. if(BETWEEN(term.c.y, term.top, term.bot))
  1495. tscrolldown(term.c.y, n);
  1496. }
  1497. void
  1498. tdeleteline(int n) {
  1499. if(BETWEEN(term.c.y, term.top, term.bot))
  1500. tscrollup(term.c.y, n);
  1501. }
  1502. int32_t
  1503. tdefcolor(int *attr, int *npar, int l) {
  1504. int32_t idx = -1;
  1505. uint r, g, b;
  1506. switch (attr[*npar + 1]) {
  1507. case 2: /* direct color in RGB space */
  1508. if (*npar + 4 >= l) {
  1509. fprintf(stderr,
  1510. "erresc(38): Incorrect number of parameters (%d)\n",
  1511. *npar);
  1512. break;
  1513. }
  1514. r = attr[*npar + 2];
  1515. g = attr[*npar + 3];
  1516. b = attr[*npar + 4];
  1517. *npar += 4;
  1518. if(!BETWEEN(r, 0, 255) || !BETWEEN(g, 0, 255) || !BETWEEN(b, 0, 255))
  1519. fprintf(stderr, "erresc: bad rgb color (%d,%d,%d)\n",
  1520. r, g, b);
  1521. else
  1522. idx = TRUECOLOR(r, g, b);
  1523. break;
  1524. case 5: /* indexed color */
  1525. if (*npar + 2 >= l) {
  1526. fprintf(stderr,
  1527. "erresc(38): Incorrect number of parameters (%d)\n",
  1528. *npar);
  1529. break;
  1530. }
  1531. *npar += 2;
  1532. if(!BETWEEN(attr[*npar], 0, 255))
  1533. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[*npar]);
  1534. else
  1535. idx = attr[*npar];
  1536. break;
  1537. case 0: /* implemented defined (only foreground) */
  1538. case 1: /* transparent */
  1539. case 3: /* direct color in CMY space */
  1540. case 4: /* direct color in CMYK space */
  1541. default:
  1542. fprintf(stderr,
  1543. "erresc(38): gfx attr %d unknown\n", attr[*npar]);
  1544. break;
  1545. }
  1546. return idx;
  1547. }
  1548. void
  1549. tsetattr(int *attr, int l) {
  1550. int i;
  1551. int32_t idx;
  1552. for(i = 0; i < l; i++) {
  1553. switch(attr[i]) {
  1554. case 0:
  1555. term.c.attr.mode &= ~(
  1556. ATTR_BOLD |
  1557. ATTR_FAINT |
  1558. ATTR_ITALIC |
  1559. ATTR_UNDERLINE |
  1560. ATTR_BLINK |
  1561. ATTR_REVERSE |
  1562. ATTR_INVISIBLE |
  1563. ATTR_STRUCK );
  1564. term.c.attr.fg = defaultfg;
  1565. term.c.attr.bg = defaultbg;
  1566. break;
  1567. case 1:
  1568. term.c.attr.mode |= ATTR_BOLD;
  1569. break;
  1570. case 2:
  1571. term.c.attr.mode |= ATTR_FAINT;
  1572. break;
  1573. case 3:
  1574. term.c.attr.mode |= ATTR_ITALIC;
  1575. break;
  1576. case 4:
  1577. term.c.attr.mode |= ATTR_UNDERLINE;
  1578. break;
  1579. case 5: /* slow blink */
  1580. /* FALLTHROUGH */
  1581. case 6: /* rapid blink */
  1582. term.c.attr.mode |= ATTR_BLINK;
  1583. break;
  1584. case 7:
  1585. term.c.attr.mode |= ATTR_REVERSE;
  1586. break;
  1587. case 8:
  1588. term.c.attr.mode |= ATTR_INVISIBLE;
  1589. break;
  1590. case 9:
  1591. term.c.attr.mode |= ATTR_STRUCK;
  1592. break;
  1593. case 22:
  1594. term.c.attr.mode &= ~(ATTR_BOLD | ATTR_FAINT);
  1595. break;
  1596. case 23:
  1597. term.c.attr.mode &= ~ATTR_ITALIC;
  1598. break;
  1599. case 24:
  1600. term.c.attr.mode &= ~ATTR_UNDERLINE;
  1601. break;
  1602. case 25:
  1603. term.c.attr.mode &= ~ATTR_BLINK;
  1604. break;
  1605. case 27:
  1606. term.c.attr.mode &= ~ATTR_REVERSE;
  1607. break;
  1608. case 28:
  1609. term.c.attr.mode &= ~ATTR_INVISIBLE;
  1610. break;
  1611. case 29:
  1612. term.c.attr.mode &= ~ATTR_STRUCK;
  1613. break;
  1614. case 38:
  1615. if ((idx = tdefcolor(attr, &i, l)) >= 0)
  1616. term.c.attr.fg = idx;
  1617. break;
  1618. case 39:
  1619. term.c.attr.fg = defaultfg;
  1620. break;
  1621. case 48:
  1622. if ((idx = tdefcolor(attr, &i, l)) >= 0)
  1623. term.c.attr.bg = idx;
  1624. break;
  1625. case 49:
  1626. term.c.attr.bg = defaultbg;
  1627. break;
  1628. default:
  1629. if(BETWEEN(attr[i], 30, 37)) {
  1630. term.c.attr.fg = attr[i] - 30;
  1631. } else if(BETWEEN(attr[i], 40, 47)) {
  1632. term.c.attr.bg = attr[i] - 40;
  1633. } else if(BETWEEN(attr[i], 90, 97)) {
  1634. term.c.attr.fg = attr[i] - 90 + 8;
  1635. } else if(BETWEEN(attr[i], 100, 107)) {
  1636. term.c.attr.bg = attr[i] - 100 + 8;
  1637. } else {
  1638. fprintf(stderr,
  1639. "erresc(default): gfx attr %d unknown\n",
  1640. attr[i]), csidump();
  1641. }
  1642. break;
  1643. }
  1644. }
  1645. }
  1646. void
  1647. tsetscroll(int t, int b) {
  1648. int temp;
  1649. LIMIT(t, 0, term.row-1);
  1650. LIMIT(b, 0, term.row-1);
  1651. if(t > b) {
  1652. temp = t;
  1653. t = b;
  1654. b = temp;
  1655. }
  1656. term.top = t;
  1657. term.bot = b;
  1658. }
  1659. void
  1660. tsetmode(bool priv, bool set, int *args, int narg) {
  1661. int *lim, mode;
  1662. bool alt;
  1663. for(lim = args + narg; args < lim; ++args) {
  1664. if(priv) {
  1665. switch(*args) {
  1666. case 1: /* DECCKM -- Cursor key */
  1667. MODBIT(term.mode, set, MODE_APPCURSOR);
  1668. break;
  1669. case 5: /* DECSCNM -- Reverse video */
  1670. mode = term.mode;
  1671. MODBIT(term.mode, set, MODE_REVERSE);
  1672. if(mode != term.mode)
  1673. redraw();
  1674. break;
  1675. case 6: /* DECOM -- Origin */
  1676. MODBIT(term.c.state, set, CURSOR_ORIGIN);
  1677. tmoveato(0, 0);
  1678. break;
  1679. case 7: /* DECAWM -- Auto wrap */
  1680. MODBIT(term.mode, set, MODE_WRAP);
  1681. break;
  1682. case 0: /* Error (IGNORED) */
  1683. case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
  1684. case 3: /* DECCOLM -- Column (IGNORED) */
  1685. case 4: /* DECSCLM -- Scroll (IGNORED) */
  1686. case 8: /* DECARM -- Auto repeat (IGNORED) */
  1687. case 18: /* DECPFF -- Printer feed (IGNORED) */
  1688. case 19: /* DECPEX -- Printer extent (IGNORED) */
  1689. case 42: /* DECNRCM -- National characters (IGNORED) */
  1690. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1691. break;
  1692. case 25: /* DECTCEM -- Text Cursor Enable Mode */
  1693. MODBIT(term.mode, !set, MODE_HIDE);
  1694. break;
  1695. case 9: /* X10 mouse compatibility mode */
  1696. xsetpointermotion(0);
  1697. MODBIT(term.mode, 0, MODE_MOUSE);
  1698. MODBIT(term.mode, set, MODE_MOUSEX10);
  1699. break;
  1700. case 1000: /* 1000: report button press */
  1701. xsetpointermotion(0);
  1702. MODBIT(term.mode, 0, MODE_MOUSE);
  1703. MODBIT(term.mode, set, MODE_MOUSEBTN);
  1704. break;
  1705. case 1002: /* 1002: report motion on button press */
  1706. xsetpointermotion(0);
  1707. MODBIT(term.mode, 0, MODE_MOUSE);
  1708. MODBIT(term.mode, set, MODE_MOUSEMOTION);
  1709. break;
  1710. case 1003: /* 1003: enable all mouse motions */
  1711. xsetpointermotion(set);
  1712. MODBIT(term.mode, 0, MODE_MOUSE);
  1713. MODBIT(term.mode, set, MODE_MOUSEMANY);
  1714. break;
  1715. case 1004: /* 1004: send focus events to tty */
  1716. MODBIT(term.mode, set, MODE_FOCUS);
  1717. break;
  1718. case 1006: /* 1006: extended reporting mode */
  1719. MODBIT(term.mode, set, MODE_MOUSESGR);
  1720. break;
  1721. case 1034:
  1722. MODBIT(term.mode, set, MODE_8BIT);
  1723. break;
  1724. case 1049: /* swap screen & set/restore cursor as xterm */
  1725. if (!allowaltscreen)
  1726. break;
  1727. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1728. /* FALLTHROUGH */
  1729. case 47: /* swap screen */
  1730. case 1047:
  1731. if (!allowaltscreen)
  1732. break;
  1733. alt = IS_SET(MODE_ALTSCREEN);
  1734. if(alt) {
  1735. tclearregion(0, 0, term.col-1,
  1736. term.row-1);
  1737. }
  1738. if(set ^ alt) /* set is always 1 or 0 */
  1739. tswapscreen();
  1740. if(*args != 1049)
  1741. break;
  1742. /* FALLTHROUGH */
  1743. case 1048:
  1744. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1745. break;
  1746. case 2004: /* 2004: bracketed paste mode */
  1747. MODBIT(term.mode, set, MODE_BRCKTPASTE);
  1748. break;
  1749. /* Not implemented mouse modes. See comments there. */
  1750. case 1001: /* mouse highlight mode; can hang the
  1751. terminal by design when implemented. */
  1752. case 1005: /* UTF-8 mouse mode; will confuse
  1753. applications not supporting UTF-8
  1754. and luit. */
  1755. case 1015: /* urxvt mangled mouse mode; incompatible
  1756. and can be mistaken for other control
  1757. codes. */
  1758. default:
  1759. fprintf(stderr,
  1760. "erresc: unknown private set/reset mode %d\n",
  1761. *args);
  1762. break;
  1763. }
  1764. } else {
  1765. switch(*args) {
  1766. case 0: /* Error (IGNORED) */
  1767. break;
  1768. case 2: /* KAM -- keyboard action */
  1769. MODBIT(term.mode, set, MODE_KBDLOCK);
  1770. break;
  1771. case 4: /* IRM -- Insertion-replacement */
  1772. MODBIT(term.mode, set, MODE_INSERT);
  1773. break;
  1774. case 12: /* SRM -- Send/Receive */
  1775. MODBIT(term.mode, !set, MODE_ECHO);
  1776. break;
  1777. case 20: /* LNM -- Linefeed/new line */
  1778. MODBIT(term.mode, set, MODE_CRLF);
  1779. break;
  1780. default:
  1781. fprintf(stderr,
  1782. "erresc: unknown set/reset mode %d\n",
  1783. *args);
  1784. break;
  1785. }
  1786. }
  1787. }
  1788. }
  1789. void
  1790. csihandle(void) {
  1791. char buf[40];
  1792. int len;
  1793. switch(csiescseq.mode[0]) {
  1794. default:
  1795. unknown:
  1796. fprintf(stderr, "erresc: unknown csi ");
  1797. csidump();
  1798. /* die(""); */
  1799. break;
  1800. case '@': /* ICH -- Insert <n> blank char */
  1801. DEFAULT(csiescseq.arg[0], 1);
  1802. tinsertblank(csiescseq.arg[0]);
  1803. break;
  1804. case 'A': /* CUU -- Cursor <n> Up */
  1805. DEFAULT(csiescseq.arg[0], 1);
  1806. tmoveto(term.c.x, term.c.y-csiescseq.arg[0]);
  1807. break;
  1808. case 'B': /* CUD -- Cursor <n> Down */
  1809. case 'e': /* VPR --Cursor <n> Down */
  1810. DEFAULT(csiescseq.arg[0], 1);
  1811. tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
  1812. break;
  1813. case 'i': /* MC -- Media Copy */
  1814. switch(csiescseq.arg[0]) {
  1815. case 0:
  1816. tdump();
  1817. break;
  1818. case 1:
  1819. tdumpline(term.c.y);
  1820. break;
  1821. case 2:
  1822. tdumpsel();
  1823. break;
  1824. case 4:
  1825. term.mode &= ~MODE_PRINT;
  1826. break;
  1827. case 5:
  1828. term.mode |= MODE_PRINT;
  1829. break;
  1830. }
  1831. break;
  1832. case 'c': /* DA -- Device Attributes */
  1833. if(csiescseq.arg[0] == 0)
  1834. ttywrite(vtiden, sizeof(vtiden) - 1);
  1835. break;
  1836. case 'C': /* CUF -- Cursor <n> Forward */
  1837. case 'a': /* HPR -- Cursor <n> Forward */
  1838. DEFAULT(csiescseq.arg[0], 1);
  1839. tmoveto(term.c.x+csiescseq.arg[0], term.c.y);
  1840. break;
  1841. case 'D': /* CUB -- Cursor <n> Backward */
  1842. DEFAULT(csiescseq.arg[0], 1);
  1843. tmoveto(term.c.x-csiescseq.arg[0], term.c.y);
  1844. break;
  1845. case 'E': /* CNL -- Cursor <n> Down and first col */
  1846. DEFAULT(csiescseq.arg[0], 1);
  1847. tmoveto(0, term.c.y+csiescseq.arg[0]);
  1848. break;
  1849. case 'F': /* CPL -- Cursor <n> Up and first col */
  1850. DEFAULT(csiescseq.arg[0], 1);
  1851. tmoveto(0, term.c.y-csiescseq.arg[0]);
  1852. break;
  1853. case 'g': /* TBC -- Tabulation clear */
  1854. switch(csiescseq.arg[0]) {
  1855. case 0: /* clear current tab stop */
  1856. term.tabs[term.c.x] = 0;
  1857. break;
  1858. case 3: /* clear all the tabs */
  1859. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  1860. break;
  1861. default:
  1862. goto unknown;
  1863. }
  1864. break;
  1865. case 'G': /* CHA -- Move to <col> */
  1866. case '`': /* HPA */
  1867. DEFAULT(csiescseq.arg[0], 1);
  1868. tmoveto(csiescseq.arg[0]-1, term.c.y);
  1869. break;
  1870. case 'H': /* CUP -- Move to <row> <col> */
  1871. case 'f': /* HVP */
  1872. DEFAULT(csiescseq.arg[0], 1);
  1873. DEFAULT(csiescseq.arg[1], 1);
  1874. tmoveato(csiescseq.arg[1]-1, csiescseq.arg[0]-1);
  1875. break;
  1876. case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
  1877. DEFAULT(csiescseq.arg[0], 1);
  1878. tputtab(csiescseq.arg[0]);
  1879. break;
  1880. case 'J': /* ED -- Clear screen */
  1881. selclear(NULL);
  1882. switch(csiescseq.arg[0]) {
  1883. case 0: /* below */
  1884. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1885. if(term.c.y < term.row-1) {
  1886. tclearregion(0, term.c.y+1, term.col-1,
  1887. term.row-1);
  1888. }
  1889. break;
  1890. case 1: /* above */
  1891. if(term.c.y > 1)
  1892. tclearregion(0, 0, term.col-1, term.c.y-1);
  1893. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1894. break;
  1895. case 2: /* all */
  1896. tclearregion(0, 0, term.col-1, term.row-1);
  1897. break;
  1898. default:
  1899. goto unknown;
  1900. }
  1901. break;
  1902. case 'K': /* EL -- Clear line */
  1903. switch(csiescseq.arg[0]) {
  1904. case 0: /* right */
  1905. tclearregion(term.c.x, term.c.y, term.col-1,
  1906. term.c.y);
  1907. break;
  1908. case 1: /* left */
  1909. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1910. break;
  1911. case 2: /* all */
  1912. tclearregion(0, term.c.y, term.col-1, term.c.y);
  1913. break;
  1914. }
  1915. break;
  1916. case 'S': /* SU -- Scroll <n> line up */
  1917. DEFAULT(csiescseq.arg[0], 1);
  1918. tscrollup(term.top, csiescseq.arg[0]);
  1919. break;
  1920. case 'T': /* SD -- Scroll <n> line down */
  1921. DEFAULT(csiescseq.arg[0], 1);
  1922. tscrolldown(term.top, csiescseq.arg[0]);
  1923. break;
  1924. case 'L': /* IL -- Insert <n> blank lines */
  1925. DEFAULT(csiescseq.arg[0], 1);
  1926. tinsertblankline(csiescseq.arg[0]);
  1927. break;
  1928. case 'l': /* RM -- Reset Mode */
  1929. tsetmode(csiescseq.priv, 0, csiescseq.arg, csiescseq.narg);
  1930. break;
  1931. case 'M': /* DL -- Delete <n> lines */
  1932. DEFAULT(csiescseq.arg[0], 1);
  1933. tdeleteline(csiescseq.arg[0]);
  1934. break;
  1935. case 'X': /* ECH -- Erase <n> char */
  1936. DEFAULT(csiescseq.arg[0], 1);
  1937. tclearregion(term.c.x, term.c.y,
  1938. term.c.x + csiescseq.arg[0] - 1, term.c.y);
  1939. break;
  1940. case 'P': /* DCH -- Delete <n> char */
  1941. DEFAULT(csiescseq.arg[0], 1);
  1942. tdeletechar(csiescseq.arg[0]);
  1943. break;
  1944. case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
  1945. DEFAULT(csiescseq.arg[0], 1);
  1946. tputtab(-csiescseq.arg[0]);
  1947. break;
  1948. case 'd': /* VPA -- Move to <row> */
  1949. DEFAULT(csiescseq.arg[0], 1);
  1950. tmoveato(term.c.x, csiescseq.arg[0]-1);
  1951. break;
  1952. case 'h': /* SM -- Set terminal mode */
  1953. tsetmode(csiescseq.priv, 1, csiescseq.arg, csiescseq.narg);
  1954. break;
  1955. case 'm': /* SGR -- Terminal attribute (color) */
  1956. tsetattr(csiescseq.arg, csiescseq.narg);
  1957. break;
  1958. case 'n': /* DSR – Device Status Report (cursor position) */
  1959. if (csiescseq.arg[0] == 6) {
  1960. len = snprintf(buf, sizeof(buf),"\033[%i;%iR",
  1961. term.c.y+1, term.c.x+1);
  1962. ttywrite(buf, len);
  1963. }
  1964. break;
  1965. case 'r': /* DECSTBM -- Set Scrolling Region */
  1966. if(csiescseq.priv) {
  1967. goto unknown;
  1968. } else {
  1969. DEFAULT(csiescseq.arg[0], 1);
  1970. DEFAULT(csiescseq.arg[1], term.row);
  1971. tsetscroll(csiescseq.arg[0]-1, csiescseq.arg[1]-1);
  1972. tmoveato(0, 0);
  1973. }
  1974. break;
  1975. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  1976. tcursor(CURSOR_SAVE);
  1977. break;
  1978. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  1979. tcursor(CURSOR_LOAD);
  1980. break;
  1981. case ' ':
  1982. switch (csiescseq.mode[1]) {
  1983. case 'q': /* DECSCUSR -- Set Cursor Style */
  1984. DEFAULT(csiescseq.arg[0], 1);
  1985. if (!BETWEEN(csiescseq.arg[0], 0, 6)) {
  1986. goto unknown;
  1987. }
  1988. xw.cursor = csiescseq.arg[0];
  1989. break;
  1990. default:
  1991. goto unknown;
  1992. }
  1993. break;
  1994. }
  1995. }
  1996. void
  1997. csidump(void) {
  1998. int i;
  1999. uint c;
  2000. printf("ESC[");
  2001. for(i = 0; i < csiescseq.len; i++) {
  2002. c = csiescseq.buf[i] & 0xff;
  2003. if(isprint(c)) {
  2004. putchar(c);
  2005. } else if(c == '\n') {
  2006. printf("(\\n)");
  2007. } else if(c == '\r') {
  2008. printf("(\\r)");
  2009. } else if(c == 0x1b) {
  2010. printf("(\\e)");
  2011. } else {
  2012. printf("(%02x)", c);
  2013. }
  2014. }
  2015. putchar('\n');
  2016. }
  2017. void
  2018. csireset(void) {
  2019. memset(&csiescseq, 0, sizeof(csiescseq));
  2020. }
  2021. void
  2022. strhandle(void) {
  2023. char *p = NULL;
  2024. int j, narg, par;
  2025. term.esc &= ~(ESC_STR_END|ESC_STR);
  2026. strparse();
  2027. par = (narg = strescseq.narg) ? atoi(strescseq.args[0]) : 0;
  2028. switch(strescseq.type) {
  2029. case ']': /* OSC -- Operating System Command */
  2030. switch(par) {
  2031. case 0:
  2032. case 1:
  2033. case 2:
  2034. if(narg > 1)
  2035. xsettitle(strescseq.args[1]);
  2036. return;
  2037. case 4: /* color set */
  2038. if(narg < 3)
  2039. break;
  2040. p = strescseq.args[2];
  2041. /* FALLTHROUGH */
  2042. case 104: /* color reset, here p = NULL */
  2043. j = (narg > 1) ? atoi(strescseq.args[1]) : -1;
  2044. if(xsetcolorname(j, p)) {
  2045. fprintf(stderr, "erresc: invalid color %s\n", p);
  2046. } else {
  2047. /*
  2048. * TODO if defaultbg color is changed, borders
  2049. * are dirty
  2050. */
  2051. redraw();
  2052. }
  2053. return;
  2054. }
  2055. break;
  2056. case 'k': /* old title set compatibility */
  2057. xsettitle(strescseq.args[0]);
  2058. return;
  2059. case 'P': /* DCS -- Device Control String */
  2060. case '_': /* APC -- Application Program Command */
  2061. case '^': /* PM -- Privacy Message */
  2062. return;
  2063. }
  2064. fprintf(stderr, "erresc: unknown str ");
  2065. strdump();
  2066. }
  2067. void
  2068. strparse(void) {
  2069. int c;
  2070. char *p = strescseq.buf;
  2071. strescseq.narg = 0;
  2072. strescseq.buf[strescseq.len] = '\0';
  2073. if(*p == '\0')
  2074. return;
  2075. while(strescseq.narg < STR_ARG_SIZ) {
  2076. strescseq.args[strescseq.narg++] = p;
  2077. while((c = *p) != ';' && c != '\0')
  2078. ++p;
  2079. if(c == '\0')
  2080. return;
  2081. *p++ = '\0';
  2082. }
  2083. }
  2084. void
  2085. strdump(void) {
  2086. int i;
  2087. uint c;
  2088. printf("ESC%c", strescseq.type);
  2089. for(i = 0; i < strescseq.len; i++) {
  2090. c = strescseq.buf[i] & 0xff;
  2091. if(c == '\0') {
  2092. return;
  2093. } else if(isprint(c)) {
  2094. putchar(c);
  2095. } else if(c == '\n') {
  2096. printf("(\\n)");
  2097. } else if(c == '\r') {
  2098. printf("(\\r)");
  2099. } else if(c == 0x1b) {
  2100. printf("(\\e)");
  2101. } else {
  2102. printf("(%02x)", c);
  2103. }
  2104. }
  2105. printf("ESC\\\n");
  2106. }
  2107. void
  2108. strreset(void) {
  2109. memset(&strescseq, 0, sizeof(strescseq));
  2110. }
  2111. void
  2112. tprinter(char *s, size_t len) {
  2113. if(iofd != -1 && xwrite(iofd, s, len) < 0) {
  2114. fprintf(stderr, "Error writing in %s:%s\n",
  2115. opt_io, strerror(errno));
  2116. close(iofd);
  2117. iofd = -1;
  2118. }
  2119. }
  2120. void
  2121. toggleprinter(const Arg *arg) {
  2122. term.mode ^= MODE_PRINT;
  2123. }
  2124. void
  2125. printscreen(const Arg *arg) {
  2126. tdump();
  2127. }
  2128. void
  2129. printsel(const Arg *arg) {
  2130. tdumpsel();
  2131. }
  2132. void
  2133. tdumpsel(void) {
  2134. char *ptr;
  2135. if((ptr = getsel())) {
  2136. tprinter(ptr, strlen(ptr));
  2137. free(ptr);
  2138. }
  2139. }
  2140. void
  2141. tdumpline(int n) {
  2142. Glyph *bp, *end;
  2143. bp = &term.line[n][0];
  2144. end = &bp[MIN(tlinelen(n), term.col) - 1];
  2145. if(bp != end || bp->c[0] != ' ') {
  2146. for( ;bp <= end; ++bp)
  2147. tprinter(bp->c, utf8len(bp->c));
  2148. }
  2149. tprinter("\n", 1);
  2150. }
  2151. void
  2152. tdump(void) {
  2153. int i;
  2154. for(i = 0; i < term.row; ++i)
  2155. tdumpline(i);
  2156. }
  2157. void
  2158. tputtab(int n) {
  2159. uint x = term.c.x;
  2160. if(n > 0) {
  2161. while(x < term.col && n--)
  2162. for(++x; x < term.col && !term.tabs[x]; ++x)
  2163. /* nothing */ ;
  2164. } else if(n < 0) {
  2165. while(x > 0 && n++)
  2166. for(--x; x > 0 && !term.tabs[x]; --x)
  2167. /* nothing */ ;
  2168. }
  2169. term.c.x = LIMIT(x, 0, term.col-1);
  2170. }
  2171. void
  2172. techo(char *buf, int len) {
  2173. for(; len > 0; buf++, len--) {
  2174. char c = *buf;
  2175. if(ISCONTROL((uchar) c)) { /* control code */
  2176. if(c & 0x80) {
  2177. c &= 0x7f;
  2178. tputc("^", 1);
  2179. tputc("[", 1);
  2180. } else if(c != '\n' && c != '\r' && c != '\t') {
  2181. c ^= 0x40;
  2182. tputc("^", 1);
  2183. }
  2184. tputc(&c, 1);
  2185. } else {
  2186. break;
  2187. }
  2188. }
  2189. if(len)
  2190. tputc(buf, len);
  2191. }
  2192. void
  2193. tdeftran(char ascii) {
  2194. static char cs[] = "0B";
  2195. static int vcs[] = {CS_GRAPHIC0, CS_USA};
  2196. char *p;
  2197. if((p = strchr(cs, ascii)) == NULL) {
  2198. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  2199. } else {
  2200. term.trantbl[term.icharset] = vcs[p - cs];
  2201. }
  2202. }
  2203. void
  2204. tdectest(char c) {
  2205. static char E[UTF_SIZ] = "E";
  2206. int x, y;
  2207. if(c == '8') { /* DEC screen alignment test. */
  2208. for(x = 0; x < term.col; ++x) {
  2209. for(y = 0; y < term.row; ++y)
  2210. tsetchar(E, &term.c.attr, x, y);
  2211. }
  2212. }
  2213. }
  2214. void
  2215. tstrsequence(uchar c) {
  2216. switch (c) {
  2217. case 0x90: /* DCS -- Device Control String */
  2218. c = 'P';
  2219. break;
  2220. case 0x9f: /* APC -- Application Program Command */
  2221. c = '_';
  2222. break;
  2223. case 0x9e: /* PM -- Privacy Message */
  2224. c = '^';
  2225. break;
  2226. case 0x9d: /* OSC -- Operating System Command */
  2227. c = ']';
  2228. break;
  2229. }
  2230. strreset();
  2231. strescseq.type = c;
  2232. term.esc |= ESC_STR;
  2233. }
  2234. void
  2235. tcontrolcode(uchar ascii) {
  2236. static char question[UTF_SIZ] = "?";
  2237. switch(ascii) {
  2238. case '\t': /* HT */
  2239. tputtab(1);
  2240. return;
  2241. case '\b': /* BS */
  2242. tmoveto(term.c.x-1, term.c.y);
  2243. return;
  2244. case '\r': /* CR */
  2245. tmoveto(0, term.c.y);
  2246. return;
  2247. case '\f': /* LF */
  2248. case '\v': /* VT */
  2249. case '\n': /* LF */
  2250. /* go to first col if the mode is set */
  2251. tnewline(IS_SET(MODE_CRLF));
  2252. return;
  2253. case '\a': /* BEL */
  2254. if(term.esc & ESC_STR_END) {
  2255. /* backwards compatibility to xterm */
  2256. strhandle();
  2257. } else {
  2258. if(!(xw.state & WIN_FOCUSED))
  2259. xseturgency(1);
  2260. if (bellvolume)
  2261. XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL);
  2262. }
  2263. break;
  2264. case '\033': /* ESC */
  2265. csireset();
  2266. term.esc &= ~(ESC_CSI|ESC_ALTCHARSET|ESC_TEST);
  2267. term.esc |= ESC_START;
  2268. return;
  2269. case '\016': /* SO (LS1 -- Locking shift 1) */
  2270. case '\017': /* SI (LS0 -- Locking shift 0) */
  2271. term.charset = 1 - (ascii - '\016');
  2272. return;
  2273. case '\032': /* SUB */
  2274. tsetchar(question, &term.c.attr, term.c.x, term.c.y);
  2275. case '\030': /* CAN */
  2276. csireset();
  2277. break;
  2278. case '\005': /* ENQ (IGNORED) */
  2279. case '\000': /* NUL (IGNORED) */
  2280. case '\021': /* XON (IGNORED) */
  2281. case '\023': /* XOFF (IGNORED) */
  2282. case 0177: /* DEL (IGNORED) */
  2283. return;
  2284. case 0x84: /* TODO: IND */
  2285. break;
  2286. case 0x85: /* NEL -- Next line */
  2287. tnewline(1); /* always go to first col */
  2288. break;
  2289. case 0x88: /* HTS -- Horizontal tab stop */
  2290. term.tabs[term.c.x] = 1;
  2291. break;
  2292. case 0x8d: /* TODO: RI */
  2293. case 0x8e: /* TODO: SS2 */
  2294. case 0x8f: /* TODO: SS3 */
  2295. case 0x98: /* TODO: SOS */
  2296. break;
  2297. case 0x9a: /* DECID -- Identify Terminal */
  2298. ttywrite(vtiden, sizeof(vtiden) - 1);
  2299. break;
  2300. case 0x9b: /* TODO: CSI */
  2301. case 0x9c: /* TODO: ST */
  2302. break;
  2303. case 0x90: /* DCS -- Device Control String */
  2304. case 0x9f: /* APC -- Application Program Command */
  2305. case 0x9e: /* PM -- Privacy Message */
  2306. case 0x9d: /* OSC -- Operating System Command */
  2307. tstrsequence(ascii);
  2308. return;
  2309. }
  2310. /* only CAN, SUB, \a and C1 chars interrupt a sequence */
  2311. term.esc &= ~(ESC_STR_END|ESC_STR);
  2312. }
  2313. /*
  2314. * returns 1 when the sequence is finished and it hasn't to read
  2315. * more characters for this sequence, otherwise 0
  2316. */
  2317. int
  2318. eschandle(uchar ascii) {
  2319. switch(ascii) {
  2320. case '[':
  2321. term.esc |= ESC_CSI;
  2322. return 0;
  2323. case '#':
  2324. term.esc |= ESC_TEST;
  2325. return 0;
  2326. case 'P': /* DCS -- Device Control String */
  2327. case '_': /* APC -- Application Program Command */
  2328. case '^': /* PM -- Privacy Message */
  2329. case ']': /* OSC -- Operating System Command */
  2330. case 'k': /* old title set compatibility */
  2331. tstrsequence(ascii);
  2332. return 0;
  2333. case 'n': /* LS2 -- Locking shift 2 */
  2334. case 'o': /* LS3 -- Locking shift 3 */
  2335. term.charset = 2 + (ascii - 'n');
  2336. break;
  2337. case '(': /* GZD4 -- set primary charset G0 */
  2338. case ')': /* G1D4 -- set secondary charset G1 */
  2339. case '*': /* G2D4 -- set tertiary charset G2 */
  2340. case '+': /* G3D4 -- set quaternary charset G3 */
  2341. term.icharset = ascii - '(';
  2342. term.esc |= ESC_ALTCHARSET;
  2343. return 0;
  2344. case 'D': /* IND -- Linefeed */
  2345. if(term.c.y == term.bot) {
  2346. tscrollup(term.top, 1);
  2347. } else {
  2348. tmoveto(term.c.x, term.c.y+1);
  2349. }
  2350. break;
  2351. case 'E': /* NEL -- Next line */
  2352. tnewline(1); /* always go to first col */
  2353. break;
  2354. case 'H': /* HTS -- Horizontal tab stop */
  2355. term.tabs[term.c.x] = 1;
  2356. break;
  2357. case 'M': /* RI -- Reverse index */
  2358. if(term.c.y == term.top) {
  2359. tscrolldown(term.top, 1);
  2360. } else {
  2361. tmoveto(term.c.x, term.c.y-1);
  2362. }
  2363. break;
  2364. case 'Z': /* DECID -- Identify Terminal */
  2365. ttywrite(vtiden, sizeof(vtiden) - 1);
  2366. break;
  2367. case 'c': /* RIS -- Reset to inital state */
  2368. treset();
  2369. xresettitle();
  2370. xloadcols();
  2371. break;
  2372. case '=': /* DECPAM -- Application keypad */
  2373. term.mode |= MODE_APPKEYPAD;
  2374. break;
  2375. case '>': /* DECPNM -- Normal keypad */
  2376. term.mode &= ~MODE_APPKEYPAD;
  2377. break;
  2378. case '7': /* DECSC -- Save Cursor */
  2379. tcursor(CURSOR_SAVE);
  2380. break;
  2381. case '8': /* DECRC -- Restore Cursor */
  2382. tcursor(CURSOR_LOAD);
  2383. break;
  2384. case '\\': /* ST -- String Terminator */
  2385. if(term.esc & ESC_STR_END)
  2386. strhandle();
  2387. break;
  2388. default:
  2389. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  2390. (uchar) ascii, isprint(ascii)? ascii:'.');
  2391. break;
  2392. }
  2393. return 1;
  2394. }
  2395. void
  2396. tputc(char *c, int len) {
  2397. uchar ascii;
  2398. bool control;
  2399. long unicodep;
  2400. int width;
  2401. Glyph *gp;
  2402. if(len == 1) {
  2403. width = 1;
  2404. unicodep = ascii = *c;
  2405. } else {
  2406. utf8decode(c, &unicodep, UTF_SIZ);
  2407. if ((width = wcwidth(unicodep)) == -1) {
  2408. c = "\357\277\275"; /* UTF_INVALID */
  2409. width = 1;
  2410. }
  2411. ascii = unicodep;
  2412. }
  2413. if(IS_SET(MODE_PRINT))
  2414. tprinter(c, len);
  2415. control = ISCONTROL(unicodep);
  2416. /*
  2417. * STR sequence must be checked before anything else
  2418. * because it uses all following characters until it
  2419. * receives a ESC, a SUB, a ST or any other C1 control
  2420. * character.
  2421. */
  2422. if(term.esc & ESC_STR) {
  2423. if(len == 1 &&
  2424. (ascii == '\a' || ascii == 030 ||
  2425. ascii == 032 || ascii == 033 ||
  2426. ISCONTROLC1(unicodep))) {
  2427. term.esc &= ~(ESC_START|ESC_STR);
  2428. term.esc |= ESC_STR_END;
  2429. } else if(strescseq.len + len < sizeof(strescseq.buf) - 1) {
  2430. memmove(&strescseq.buf[strescseq.len], c, len);
  2431. strescseq.len += len;
  2432. return;
  2433. } else {
  2434. /*
  2435. * Here is a bug in terminals. If the user never sends
  2436. * some code to stop the str or esc command, then st
  2437. * will stop responding. But this is better than
  2438. * silently failing with unknown characters. At least
  2439. * then users will report back.
  2440. *
  2441. * In the case users ever get fixed, here is the code:
  2442. */
  2443. /*
  2444. * term.esc = 0;
  2445. * strhandle();
  2446. */
  2447. return;
  2448. }
  2449. }
  2450. /*
  2451. * Actions of control codes must be performed as soon they arrive
  2452. * because they can be embedded inside a control sequence, and
  2453. * they must not cause conflicts with sequences.
  2454. */
  2455. if(control) {
  2456. tcontrolcode(ascii);
  2457. /*
  2458. * control codes are not shown ever
  2459. */
  2460. return;
  2461. } else if(term.esc & ESC_START) {
  2462. if(term.esc & ESC_CSI) {
  2463. csiescseq.buf[csiescseq.len++] = ascii;
  2464. if(BETWEEN(ascii, 0x40, 0x7E)
  2465. || csiescseq.len >= \
  2466. sizeof(csiescseq.buf)-1) {
  2467. term.esc = 0;
  2468. csiparse();
  2469. csihandle();
  2470. }
  2471. return;
  2472. } else if(term.esc & ESC_ALTCHARSET) {
  2473. tdeftran(ascii);
  2474. } else if(term.esc & ESC_TEST) {
  2475. tdectest(ascii);
  2476. } else {
  2477. if (!eschandle(ascii))
  2478. return;
  2479. /* sequence already finished */
  2480. }
  2481. term.esc = 0;
  2482. /*
  2483. * All characters which form part of a sequence are not
  2484. * printed
  2485. */
  2486. return;
  2487. }
  2488. if(sel.ob.x != -1 && BETWEEN(term.c.y, sel.ob.y, sel.oe.y))
  2489. selclear(NULL);
  2490. gp = &term.line[term.c.y][term.c.x];
  2491. if(IS_SET(MODE_WRAP) && (term.c.state & CURSOR_WRAPNEXT)) {
  2492. gp->mode |= ATTR_WRAP;
  2493. tnewline(1);
  2494. gp = &term.line[term.c.y][term.c.x];
  2495. }
  2496. if(IS_SET(MODE_INSERT) && term.c.x+width < term.col)
  2497. memmove(gp+width, gp, (term.col - term.c.x - width) * sizeof(Glyph));
  2498. if(term.c.x+width > term.col) {
  2499. tnewline(1);
  2500. gp = &term.line[term.c.y][term.c.x];
  2501. }
  2502. tsetchar(c, &term.c.attr, term.c.x, term.c.y);
  2503. if(width == 2) {
  2504. gp->mode |= ATTR_WIDE;
  2505. if(term.c.x+1 < term.col) {
  2506. gp[1].c[0] = '\0';
  2507. gp[1].mode = ATTR_WDUMMY;
  2508. }
  2509. }
  2510. if(term.c.x+width < term.col) {
  2511. tmoveto(term.c.x+width, term.c.y);
  2512. } else {
  2513. term.c.state |= CURSOR_WRAPNEXT;
  2514. }
  2515. }
  2516. void
  2517. tresize(int col, int row) {
  2518. int i;
  2519. int minrow = MIN(row, term.row);
  2520. int mincol = MIN(col, term.col);
  2521. bool *bp;
  2522. TCursor c;
  2523. if(col < 1 || row < 1) {
  2524. fprintf(stderr,
  2525. "tresize: error resizing to %dx%d\n", col, row);
  2526. return;
  2527. }
  2528. /*
  2529. * slide screen to keep cursor where we expect it -
  2530. * tscrollup would work here, but we can optimize to
  2531. * memmove because we're freeing the earlier lines
  2532. */
  2533. for(i = 0; i <= term.c.y - row; i++) {
  2534. free(term.line[i]);
  2535. free(term.alt[i]);
  2536. }
  2537. /* ensure that both src and dst are not NULL */
  2538. if (i > 0) {
  2539. memmove(term.line, term.line + i, row * sizeof(Line));
  2540. memmove(term.alt, term.alt + i, row * sizeof(Line));
  2541. }
  2542. for(i += row; i < term.row; i++) {
  2543. free(term.line[i]);
  2544. free(term.alt[i]);
  2545. }
  2546. /* resize to new height */
  2547. term.line = xrealloc(term.line, row * sizeof(Line));
  2548. term.alt = xrealloc(term.alt, row * sizeof(Line));
  2549. term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
  2550. term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
  2551. /* resize each row to new width, zero-pad if needed */
  2552. for(i = 0; i < minrow; i++) {
  2553. term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
  2554. term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
  2555. }
  2556. /* allocate any new rows */
  2557. for(/* i == minrow */; i < row; i++) {
  2558. term.line[i] = xmalloc(col * sizeof(Glyph));
  2559. term.alt[i] = xmalloc(col * sizeof(Glyph));
  2560. }
  2561. if(col > term.col) {
  2562. bp = term.tabs + term.col;
  2563. memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
  2564. while(--bp > term.tabs && !*bp)
  2565. /* nothing */ ;
  2566. for(bp += tabspaces; bp < term.tabs + col; bp += tabspaces)
  2567. *bp = 1;
  2568. }
  2569. /* update terminal size */
  2570. term.col = col;
  2571. term.row = row;
  2572. /* reset scrolling region */
  2573. tsetscroll(0, row-1);
  2574. /* make use of the LIMIT in tmoveto */
  2575. tmoveto(term.c.x, term.c.y);
  2576. /* Clearing both screens (it makes dirty all lines) */
  2577. c = term.c;
  2578. for(i = 0; i < 2; i++) {
  2579. if(mincol < col && 0 < minrow) {
  2580. tclearregion(mincol, 0, col - 1, minrow - 1);
  2581. }
  2582. if(0 < col && minrow < row) {
  2583. tclearregion(0, minrow, col - 1, row - 1);
  2584. }
  2585. tswapscreen();
  2586. tcursor(CURSOR_LOAD);
  2587. }
  2588. term.c = c;
  2589. }
  2590. void
  2591. xresize(int col, int row) {
  2592. xw.tw = MAX(1, col * xw.cw);
  2593. xw.th = MAX(1, row * xw.ch);
  2594. XFreePixmap(xw.dpy, xw.buf);
  2595. xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.w, xw.h,
  2596. DefaultDepth(xw.dpy, xw.scr));
  2597. XftDrawChange(xw.draw, xw.buf);
  2598. xclear(0, 0, xw.w, xw.h);
  2599. }
  2600. ushort
  2601. sixd_to_16bit(int x) {
  2602. return x == 0 ? 0 : 0x3737 + 0x2828 * x;
  2603. }
  2604. bool
  2605. xloadcolor(int i, const char *name, Color *ncolor) {
  2606. XRenderColor color = { .alpha = 0xffff };
  2607. if(!name) {
  2608. if(BETWEEN(i, 16, 255)) { /* 256 color */
  2609. if(i < 6*6*6+16) { /* same colors as xterm */
  2610. color.red = sixd_to_16bit( ((i-16)/36)%6 );
  2611. color.green = sixd_to_16bit( ((i-16)/6) %6 );
  2612. color.blue = sixd_to_16bit( ((i-16)/1) %6 );
  2613. } else { /* greyscale */
  2614. color.red = 0x0808 + 0x0a0a * (i - (6*6*6+16));
  2615. color.green = color.blue = color.red;
  2616. }
  2617. return XftColorAllocValue(xw.dpy, xw.vis,
  2618. xw.cmap, &color, ncolor);
  2619. } else
  2620. name = colorname[i];
  2621. }
  2622. return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
  2623. }
  2624. void
  2625. xloadcols(void) {
  2626. int i;
  2627. static bool loaded;
  2628. Color *cp;
  2629. if(loaded) {
  2630. for (cp = dc.col; cp < &dc.col[LEN(dc.col)]; ++cp)
  2631. XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
  2632. }
  2633. for(i = 0; i < LEN(dc.col); i++)
  2634. if(!xloadcolor(i, NULL, &dc.col[i])) {
  2635. if(colorname[i])
  2636. die("Could not allocate color '%s'\n", colorname[i]);
  2637. else
  2638. die("Could not allocate color %d\n", i);
  2639. }
  2640. loaded = true;
  2641. }
  2642. int
  2643. xsetcolorname(int x, const char *name) {
  2644. Color ncolor;
  2645. if(!BETWEEN(x, 0, LEN(dc.col)))
  2646. return 1;
  2647. if(!xloadcolor(x, name, &ncolor))
  2648. return 1;
  2649. XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
  2650. dc.col[x] = ncolor;
  2651. return 0;
  2652. }
  2653. void
  2654. xtermclear(int col1, int row1, int col2, int row2) {
  2655. XftDrawRect(xw.draw,
  2656. &dc.col[IS_SET(MODE_REVERSE) ? defaultfg : defaultbg],
  2657. borderpx + col1 * xw.cw,
  2658. borderpx + row1 * xw.ch,
  2659. (col2-col1+1) * xw.cw,
  2660. (row2-row1+1) * xw.ch);
  2661. }
  2662. /*
  2663. * Absolute coordinates.
  2664. */
  2665. void
  2666. xclear(int x1, int y1, int x2, int y2) {
  2667. XftDrawRect(xw.draw,
  2668. &dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg],
  2669. x1, y1, x2-x1, y2-y1);
  2670. }
  2671. void
  2672. xhints(void) {
  2673. XClassHint class = {opt_class ? opt_class : termname, termname};
  2674. XWMHints wm = {.flags = InputHint, .input = 1};
  2675. XSizeHints *sizeh = NULL;
  2676. sizeh = XAllocSizeHints();
  2677. sizeh->flags = PSize | PResizeInc | PBaseSize;
  2678. sizeh->height = xw.h;
  2679. sizeh->width = xw.w;
  2680. sizeh->height_inc = xw.ch;
  2681. sizeh->width_inc = xw.cw;
  2682. sizeh->base_height = 2 * borderpx;
  2683. sizeh->base_width = 2 * borderpx;
  2684. if(xw.isfixed == True) {
  2685. sizeh->flags |= PMaxSize | PMinSize;
  2686. sizeh->min_width = sizeh->max_width = xw.w;
  2687. sizeh->min_height = sizeh->max_height = xw.h;
  2688. }
  2689. if(xw.gm & (XValue|YValue)) {
  2690. sizeh->flags |= USPosition | PWinGravity;
  2691. sizeh->x = xw.l;
  2692. sizeh->y = xw.t;
  2693. sizeh->win_gravity = xgeommasktogravity(xw.gm);
  2694. }
  2695. XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm,
  2696. &class);
  2697. XFree(sizeh);
  2698. }
  2699. int
  2700. xgeommasktogravity(int mask) {
  2701. switch(mask & (XNegative|YNegative)) {
  2702. case 0:
  2703. return NorthWestGravity;
  2704. case XNegative:
  2705. return NorthEastGravity;
  2706. case YNegative:
  2707. return SouthWestGravity;
  2708. }
  2709. return SouthEastGravity;
  2710. }
  2711. int
  2712. xloadfont(Font *f, FcPattern *pattern) {
  2713. FcPattern *match;
  2714. FcResult result;
  2715. match = FcFontMatch(NULL, pattern, &result);
  2716. if(!match)
  2717. return 1;
  2718. if(!(f->match = XftFontOpenPattern(xw.dpy, match))) {
  2719. FcPatternDestroy(match);
  2720. return 1;
  2721. }
  2722. f->set = NULL;
  2723. f->pattern = FcPatternDuplicate(pattern);
  2724. f->ascent = f->match->ascent;
  2725. f->descent = f->match->descent;
  2726. f->lbearing = 0;
  2727. f->rbearing = f->match->max_advance_width;
  2728. f->height = f->ascent + f->descent;
  2729. f->width = f->lbearing + f->rbearing;
  2730. return 0;
  2731. }
  2732. void
  2733. xloadfonts(char *fontstr, double fontsize) {
  2734. FcPattern *pattern;
  2735. FcResult r_sz, r_psz;
  2736. double fontval;
  2737. float ceilf(float);
  2738. if(fontstr[0] == '-') {
  2739. pattern = XftXlfdParse(fontstr, False, False);
  2740. } else {
  2741. pattern = FcNameParse((FcChar8 *)fontstr);
  2742. }
  2743. if(!pattern)
  2744. die("st: can't open font %s\n", fontstr);
  2745. if(fontsize > 1) {
  2746. FcPatternDel(pattern, FC_PIXEL_SIZE);
  2747. FcPatternDel(pattern, FC_SIZE);
  2748. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize);
  2749. usedfontsize = fontsize;
  2750. } else {
  2751. r_psz = FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval);
  2752. r_sz = FcPatternGetDouble(pattern, FC_SIZE, 0, &fontval);
  2753. if(r_psz == FcResultMatch) {
  2754. usedfontsize = fontval;
  2755. } else if(r_sz == FcResultMatch) {
  2756. usedfontsize = -1;
  2757. } else {
  2758. /*
  2759. * Default font size is 12, if none given. This is to
  2760. * have a known usedfontsize value.
  2761. */
  2762. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
  2763. usedfontsize = 12;
  2764. }
  2765. defaultfontsize = usedfontsize;
  2766. }
  2767. FcConfigSubstitute(0, pattern, FcMatchPattern);
  2768. FcDefaultSubstitute(pattern);
  2769. if(xloadfont(&dc.font, pattern))
  2770. die("st: can't open font %s\n", fontstr);
  2771. if(usedfontsize < 0) {
  2772. FcPatternGetDouble(dc.font.match->pattern,
  2773. FC_PIXEL_SIZE, 0, &fontval);
  2774. usedfontsize = fontval;
  2775. if(fontsize == 0)
  2776. defaultfontsize = fontval;
  2777. }
  2778. /* Setting character width and height. */
  2779. xw.cw = ceilf(dc.font.width * cwscale);
  2780. xw.ch = ceilf(dc.font.height * chscale);
  2781. FcPatternDel(pattern, FC_SLANT);
  2782. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
  2783. if(xloadfont(&dc.ifont, pattern))
  2784. die("st: can't open font %s\n", fontstr);
  2785. FcPatternDel(pattern, FC_WEIGHT);
  2786. FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
  2787. if(xloadfont(&dc.ibfont, pattern))
  2788. die("st: can't open font %s\n", fontstr);
  2789. FcPatternDel(pattern, FC_SLANT);
  2790. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
  2791. if(xloadfont(&dc.bfont, pattern))
  2792. die("st: can't open font %s\n", fontstr);
  2793. FcPatternDestroy(pattern);
  2794. }
  2795. void
  2796. xunloadfont(Font *f) {
  2797. XftFontClose(xw.dpy, f->match);
  2798. FcPatternDestroy(f->pattern);
  2799. if(f->set)
  2800. FcFontSetDestroy(f->set);
  2801. }
  2802. void
  2803. xunloadfonts(void) {
  2804. /* Free the loaded fonts in the font cache. */
  2805. while(frclen > 0)
  2806. XftFontClose(xw.dpy, frc[--frclen].font);
  2807. xunloadfont(&dc.font);
  2808. xunloadfont(&dc.bfont);
  2809. xunloadfont(&dc.ifont);
  2810. xunloadfont(&dc.ibfont);
  2811. }
  2812. void
  2813. xzoom(const Arg *arg) {
  2814. Arg larg;
  2815. larg.i = usedfontsize + arg->i;
  2816. xzoomabs(&larg);
  2817. }
  2818. void
  2819. xzoomabs(const Arg *arg) {
  2820. xunloadfonts();
  2821. xloadfonts(usedfont, arg->i);
  2822. cresize(0, 0);
  2823. redraw();
  2824. xhints();
  2825. }
  2826. void
  2827. xzoomreset(const Arg *arg) {
  2828. Arg larg;
  2829. if(defaultfontsize > 0) {
  2830. larg.i = defaultfontsize;
  2831. xzoomabs(&larg);
  2832. }
  2833. }
  2834. void
  2835. xinit(void) {
  2836. XGCValues gcvalues;
  2837. Cursor cursor;
  2838. Window parent;
  2839. pid_t thispid = getpid();
  2840. if(!(xw.dpy = XOpenDisplay(NULL)))
  2841. die("Can't open display\n");
  2842. xw.scr = XDefaultScreen(xw.dpy);
  2843. xw.vis = XDefaultVisual(xw.dpy, xw.scr);
  2844. /* font */
  2845. if(!FcInit())
  2846. die("Could not init fontconfig.\n");
  2847. usedfont = (opt_font == NULL)? font : opt_font;
  2848. xloadfonts(usedfont, 0);
  2849. /* colors */
  2850. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  2851. xloadcols();
  2852. /* adjust fixed window geometry */
  2853. xw.w = 2 * borderpx + term.col * xw.cw;
  2854. xw.h = 2 * borderpx + term.row * xw.ch;
  2855. if(xw.gm & XNegative)
  2856. xw.l += DisplayWidth(xw.dpy, xw.scr) - xw.w - 2;
  2857. if(xw.gm & YNegative)
  2858. xw.t += DisplayWidth(xw.dpy, xw.scr) - xw.h - 2;
  2859. /* Events */
  2860. xw.attrs.background_pixel = dc.col[defaultbg].pixel;
  2861. xw.attrs.border_pixel = dc.col[defaultbg].pixel;
  2862. xw.attrs.bit_gravity = NorthWestGravity;
  2863. xw.attrs.event_mask = FocusChangeMask | KeyPressMask
  2864. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  2865. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
  2866. xw.attrs.colormap = xw.cmap;
  2867. if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0))))
  2868. parent = XRootWindow(xw.dpy, xw.scr);
  2869. xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
  2870. xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
  2871. xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
  2872. | CWEventMask | CWColormap, &xw.attrs);
  2873. memset(&gcvalues, 0, sizeof(gcvalues));
  2874. gcvalues.graphics_exposures = False;
  2875. dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
  2876. &gcvalues);
  2877. xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.w, xw.h,
  2878. DefaultDepth(xw.dpy, xw.scr));
  2879. XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
  2880. XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, xw.w, xw.h);
  2881. /* Xft rendering context */
  2882. xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
  2883. /* input methods */
  2884. if((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
  2885. XSetLocaleModifiers("@im=local");
  2886. if((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
  2887. XSetLocaleModifiers("@im=");
  2888. if((xw.xim = XOpenIM(xw.dpy,
  2889. NULL, NULL, NULL)) == NULL) {
  2890. die("XOpenIM failed. Could not open input"
  2891. " device.\n");
  2892. }
  2893. }
  2894. }
  2895. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  2896. | XIMStatusNothing, XNClientWindow, xw.win,
  2897. XNFocusWindow, xw.win, NULL);
  2898. if(xw.xic == NULL)
  2899. die("XCreateIC failed. Could not obtain input method.\n");
  2900. /* white cursor, black outline */
  2901. cursor = XCreateFontCursor(xw.dpy, XC_xterm);
  2902. XDefineCursor(xw.dpy, xw.win, cursor);
  2903. XRecolorCursor(xw.dpy, cursor,
  2904. &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
  2905. &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
  2906. xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
  2907. xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
  2908. xw.netwmname = XInternAtom(xw.dpy, "_NET_WM_NAME", False);
  2909. XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
  2910. xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False);
  2911. XChangeProperty(xw.dpy, xw.win, xw.netwmpid, XA_CARDINAL, 32,
  2912. PropModeReplace, (uchar *)&thispid, 1);
  2913. xresettitle();
  2914. XMapWindow(xw.dpy, xw.win);
  2915. xhints();
  2916. XSync(xw.dpy, False);
  2917. }
  2918. void
  2919. xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
  2920. int winx = borderpx + x * xw.cw, winy = borderpx + y * xw.ch,
  2921. width = charlen * xw.cw, xp, i;
  2922. int frcflags, charexists;
  2923. int u8fl, u8fblen, u8cblen, doesexist;
  2924. char *u8c, *u8fs;
  2925. long unicodep;
  2926. Font *font = &dc.font;
  2927. FcResult fcres;
  2928. FcPattern *fcpattern, *fontpattern;
  2929. FcFontSet *fcsets[] = { NULL };
  2930. FcCharSet *fccharset;
  2931. Color *fg, *bg, *temp, revfg, revbg, truefg, truebg;
  2932. XRenderColor colfg, colbg;
  2933. XRectangle r;
  2934. int oneatatime;
  2935. frcflags = FRC_NORMAL;
  2936. if(base.mode & ATTR_ITALIC) {
  2937. if(base.fg == defaultfg)
  2938. base.fg = defaultitalic;
  2939. font = &dc.ifont;
  2940. frcflags = FRC_ITALIC;
  2941. } else if((base.mode & ATTR_ITALIC) && (base.mode & ATTR_BOLD)) {
  2942. if(base.fg == defaultfg)
  2943. base.fg = defaultitalic;
  2944. font = &dc.ibfont;
  2945. frcflags = FRC_ITALICBOLD;
  2946. } else if(base.mode & ATTR_UNDERLINE) {
  2947. if(base.fg == defaultfg)
  2948. base.fg = defaultunderline;
  2949. }
  2950. if(IS_TRUECOL(base.fg)) {
  2951. colfg.alpha = 0xffff;
  2952. colfg.red = TRUERED(base.fg);
  2953. colfg.green = TRUEGREEN(base.fg);
  2954. colfg.blue = TRUEBLUE(base.fg);
  2955. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &truefg);
  2956. fg = &truefg;
  2957. } else {
  2958. fg = &dc.col[base.fg];
  2959. }
  2960. if(IS_TRUECOL(base.bg)) {
  2961. colbg.alpha = 0xffff;
  2962. colbg.green = TRUEGREEN(base.bg);
  2963. colbg.red = TRUERED(base.bg);
  2964. colbg.blue = TRUEBLUE(base.bg);
  2965. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &truebg);
  2966. bg = &truebg;
  2967. } else {
  2968. bg = &dc.col[base.bg];
  2969. }
  2970. if(base.mode & ATTR_BOLD) {
  2971. /*
  2972. * change basic system colors [0-7]
  2973. * to bright system colors [8-15]
  2974. */
  2975. if(BETWEEN(base.fg, 0, 7) && !(base.mode & ATTR_FAINT))
  2976. fg = &dc.col[base.fg + 8];
  2977. if(base.mode & ATTR_ITALIC) {
  2978. font = &dc.ibfont;
  2979. frcflags = FRC_ITALICBOLD;
  2980. } else {
  2981. font = &dc.bfont;
  2982. frcflags = FRC_BOLD;
  2983. }
  2984. }
  2985. if(IS_SET(MODE_REVERSE)) {
  2986. if(fg == &dc.col[defaultfg]) {
  2987. fg = &dc.col[defaultbg];
  2988. } else {
  2989. colfg.red = ~fg->color.red;
  2990. colfg.green = ~fg->color.green;
  2991. colfg.blue = ~fg->color.blue;
  2992. colfg.alpha = fg->color.alpha;
  2993. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg,
  2994. &revfg);
  2995. fg = &revfg;
  2996. }
  2997. if(bg == &dc.col[defaultbg]) {
  2998. bg = &dc.col[defaultfg];
  2999. } else {
  3000. colbg.red = ~bg->color.red;
  3001. colbg.green = ~bg->color.green;
  3002. colbg.blue = ~bg->color.blue;
  3003. colbg.alpha = bg->color.alpha;
  3004. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg,
  3005. &revbg);
  3006. bg = &revbg;
  3007. }
  3008. }
  3009. if(base.mode & ATTR_REVERSE) {
  3010. temp = fg;
  3011. fg = bg;
  3012. bg = temp;
  3013. }
  3014. if(base.mode & ATTR_FAINT && !(base.mode & ATTR_BOLD)) {
  3015. colfg.red = fg->color.red / 2;
  3016. colfg.green = fg->color.green / 2;
  3017. colfg.blue = fg->color.blue / 2;
  3018. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
  3019. fg = &revfg;
  3020. }
  3021. if(base.mode & ATTR_BLINK && term.mode & MODE_BLINK)
  3022. fg = bg;
  3023. if(base.mode & ATTR_INVISIBLE)
  3024. fg = bg;
  3025. /* Intelligent cleaning up of the borders. */
  3026. if(x == 0) {
  3027. xclear(0, (y == 0)? 0 : winy, borderpx,
  3028. winy + xw.ch + ((y >= term.row-1)? xw.h : 0));
  3029. }
  3030. if(x + charlen >= term.col) {
  3031. xclear(winx + width, (y == 0)? 0 : winy, xw.w,
  3032. ((y >= term.row-1)? xw.h : (winy + xw.ch)));
  3033. }
  3034. if(y == 0)
  3035. xclear(winx, 0, winx + width, borderpx);
  3036. if(y == term.row-1)
  3037. xclear(winx, winy + xw.ch, winx + width, xw.h);
  3038. /* Clean up the region we want to draw to. */
  3039. XftDrawRect(xw.draw, bg, winx, winy, width, xw.ch);
  3040. /* Set the clip region because Xft is sometimes dirty. */
  3041. r.x = 0;
  3042. r.y = 0;
  3043. r.height = xw.ch;
  3044. r.width = width;
  3045. XftDrawSetClipRectangles(xw.draw, winx, winy, &r, 1);
  3046. for(xp = winx; bytelen > 0;) {
  3047. /*
  3048. * Search for the range in the to be printed string of glyphs
  3049. * that are in the main font. Then print that range. If
  3050. * some glyph is found that is not in the font, do the
  3051. * fallback dance.
  3052. */
  3053. u8fs = s;
  3054. u8fblen = 0;
  3055. u8fl = 0;
  3056. oneatatime = font->width != xw.cw;
  3057. for(;;) {
  3058. u8c = s;
  3059. u8cblen = utf8decode(s, &unicodep, UTF_SIZ);
  3060. s += u8cblen;
  3061. bytelen -= u8cblen;
  3062. doesexist = XftCharExists(xw.dpy, font->match, unicodep);
  3063. if(doesexist) {
  3064. u8fl++;
  3065. u8fblen += u8cblen;
  3066. if(!oneatatime && bytelen > 0)
  3067. continue;
  3068. }
  3069. if(u8fl > 0) {
  3070. XftDrawStringUtf8(xw.draw, fg,
  3071. font->match, xp,
  3072. winy + font->ascent,
  3073. (FcChar8 *)u8fs,
  3074. u8fblen);
  3075. xp += xw.cw * u8fl;
  3076. }
  3077. break;
  3078. }
  3079. if(doesexist) {
  3080. if(oneatatime)
  3081. continue;
  3082. break;
  3083. }
  3084. /* Search the font cache. */
  3085. for(i = 0; i < frclen; i++) {
  3086. charexists = XftCharExists(xw.dpy, frc[i].font, unicodep);
  3087. /* Everything correct. */
  3088. if(charexists && frc[i].flags == frcflags)
  3089. break;
  3090. /* We got a default font for a not found glyph. */
  3091. if(!charexists && frc[i].flags == frcflags \
  3092. && frc[i].unicodep == unicodep) {
  3093. break;
  3094. }
  3095. }
  3096. /* Nothing was found. */
  3097. if(i >= frclen) {
  3098. if(!font->set)
  3099. font->set = FcFontSort(0, font->pattern,
  3100. FcTrue, 0, &fcres);
  3101. fcsets[0] = font->set;
  3102. /*
  3103. * Nothing was found in the cache. Now use
  3104. * some dozen of Fontconfig calls to get the
  3105. * font for one single character.
  3106. *
  3107. * Xft and fontconfig are design failures.
  3108. */
  3109. fcpattern = FcPatternDuplicate(font->pattern);
  3110. fccharset = FcCharSetCreate();
  3111. FcCharSetAddChar(fccharset, unicodep);
  3112. FcPatternAddCharSet(fcpattern, FC_CHARSET,
  3113. fccharset);
  3114. FcPatternAddBool(fcpattern, FC_SCALABLE,
  3115. FcTrue);
  3116. FcConfigSubstitute(0, fcpattern,
  3117. FcMatchPattern);
  3118. FcDefaultSubstitute(fcpattern);
  3119. fontpattern = FcFontSetMatch(0, fcsets, 1,
  3120. fcpattern, &fcres);
  3121. /*
  3122. * Overwrite or create the new cache entry.
  3123. */
  3124. if(frclen >= LEN(frc)) {
  3125. frclen = LEN(frc) - 1;
  3126. XftFontClose(xw.dpy, frc[frclen].font);
  3127. frc[frclen].unicodep = 0;
  3128. }
  3129. frc[frclen].font = XftFontOpenPattern(xw.dpy,
  3130. fontpattern);
  3131. frc[frclen].flags = frcflags;
  3132. frc[frclen].unicodep = unicodep;
  3133. i = frclen;
  3134. frclen++;
  3135. FcPatternDestroy(fcpattern);
  3136. FcCharSetDestroy(fccharset);
  3137. }
  3138. XftDrawStringUtf8(xw.draw, fg, frc[i].font,
  3139. xp, winy + frc[i].font->ascent,
  3140. (FcChar8 *)u8c, u8cblen);
  3141. xp += xw.cw * wcwidth(unicodep);
  3142. }
  3143. /*
  3144. * This is how the loop above actually should be. Why does the
  3145. * application have to care about font details?
  3146. *
  3147. * I have to repeat: Xft and Fontconfig are design failures.
  3148. */
  3149. /*
  3150. XftDrawStringUtf8(xw.draw, fg, font->set, winx,
  3151. winy + font->ascent, (FcChar8 *)s, bytelen);
  3152. */
  3153. if(base.mode & ATTR_UNDERLINE) {
  3154. XftDrawRect(xw.draw, fg, winx, winy + font->ascent + 1,
  3155. width, 1);
  3156. }
  3157. if(base.mode & ATTR_STRUCK) {
  3158. XftDrawRect(xw.draw, fg, winx, winy + 2 * font->ascent / 3,
  3159. width, 1);
  3160. }
  3161. /* Reset clip to none. */
  3162. XftDrawSetClip(xw.draw, 0);
  3163. }
  3164. void
  3165. xdrawcursor(void) {
  3166. static int oldx = 0, oldy = 0;
  3167. int sl, width, curx;
  3168. Glyph g = {{' '}, ATTR_NULL, defaultbg, defaultcs};
  3169. LIMIT(oldx, 0, term.col-1);
  3170. LIMIT(oldy, 0, term.row-1);
  3171. curx = term.c.x;
  3172. /* adjust position if in dummy */
  3173. if(term.line[oldy][oldx].mode & ATTR_WDUMMY)
  3174. oldx--;
  3175. if(term.line[term.c.y][curx].mode & ATTR_WDUMMY)
  3176. curx--;
  3177. memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
  3178. /* remove the old cursor */
  3179. sl = utf8len(term.line[oldy][oldx].c);
  3180. width = (term.line[oldy][oldx].mode & ATTR_WIDE)? 2 : 1;
  3181. xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx,
  3182. oldy, width, sl);
  3183. if(IS_SET(MODE_HIDE))
  3184. return;
  3185. /* draw the new one */
  3186. if(xw.state & WIN_FOCUSED) {
  3187. switch (xw.cursor) {
  3188. case 0: /* Blinking Block */
  3189. case 1: /* Blinking Block (Default) */
  3190. case 2: /* Steady Block */
  3191. if(IS_SET(MODE_REVERSE)) {
  3192. g.mode |= ATTR_REVERSE;
  3193. g.fg = defaultcs;
  3194. g.bg = defaultfg;
  3195. }
  3196. sl = utf8len(g.c);
  3197. width = (term.line[term.c.y][curx].mode & ATTR_WIDE)\
  3198. ? 2 : 1;
  3199. xdraws(g.c, g, term.c.x, term.c.y, width, sl);
  3200. break;
  3201. case 3: /* Blinking Underline */
  3202. case 4: /* Steady Underline */
  3203. XftDrawRect(xw.draw, &dc.col[defaultcs],
  3204. borderpx + curx * xw.cw,
  3205. borderpx + (term.c.y + 1) * xw.ch - cursorthickness,
  3206. xw.cw, cursorthickness);
  3207. break;
  3208. case 5: /* Blinking bar */
  3209. case 6: /* Steady bar */
  3210. XftDrawRect(xw.draw, &dc.col[defaultcs],
  3211. borderpx + curx * xw.cw,
  3212. borderpx + term.c.y * xw.ch,
  3213. cursorthickness, xw.ch);
  3214. break;
  3215. }
  3216. } else {
  3217. XftDrawRect(xw.draw, &dc.col[defaultcs],
  3218. borderpx + curx * xw.cw,
  3219. borderpx + term.c.y * xw.ch,
  3220. xw.cw - 1, 1);
  3221. XftDrawRect(xw.draw, &dc.col[defaultcs],
  3222. borderpx + curx * xw.cw,
  3223. borderpx + term.c.y * xw.ch,
  3224. 1, xw.ch - 1);
  3225. XftDrawRect(xw.draw, &dc.col[defaultcs],
  3226. borderpx + (curx + 1) * xw.cw - 1,
  3227. borderpx + term.c.y * xw.ch,
  3228. 1, xw.ch - 1);
  3229. XftDrawRect(xw.draw, &dc.col[defaultcs],
  3230. borderpx + curx * xw.cw,
  3231. borderpx + (term.c.y + 1) * xw.ch - 1,
  3232. xw.cw, 1);
  3233. }
  3234. oldx = curx, oldy = term.c.y;
  3235. }
  3236. void
  3237. xsettitle(char *p) {
  3238. XTextProperty prop;
  3239. Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
  3240. &prop);
  3241. XSetWMName(xw.dpy, xw.win, &prop);
  3242. XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmname);
  3243. XFree(prop.value);
  3244. }
  3245. void
  3246. xresettitle(void) {
  3247. xsettitle(opt_title ? opt_title : "st");
  3248. }
  3249. void
  3250. redraw(void) {
  3251. tfulldirt();
  3252. draw();
  3253. }
  3254. void
  3255. draw(void) {
  3256. drawregion(0, 0, term.col, term.row);
  3257. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.w,
  3258. xw.h, 0, 0);
  3259. XSetForeground(xw.dpy, dc.gc,
  3260. dc.col[IS_SET(MODE_REVERSE)?
  3261. defaultfg : defaultbg].pixel);
  3262. }
  3263. void
  3264. drawregion(int x1, int y1, int x2, int y2) {
  3265. int ic, ib, x, y, ox, sl;
  3266. Glyph base, new;
  3267. char buf[DRAW_BUF_SIZ];
  3268. bool ena_sel = sel.ob.x != -1 && sel.alt == IS_SET(MODE_ALTSCREEN);
  3269. if(!(xw.state & WIN_VISIBLE))
  3270. return;
  3271. for(y = y1; y < y2; y++) {
  3272. if(!term.dirty[y])
  3273. continue;
  3274. xtermclear(0, y, term.col, y);
  3275. term.dirty[y] = 0;
  3276. base = term.line[y][0];
  3277. ic = ib = ox = 0;
  3278. for(x = x1; x < x2; x++) {
  3279. new = term.line[y][x];
  3280. if(new.mode == ATTR_WDUMMY)
  3281. continue;
  3282. if(ena_sel && selected(x, y))
  3283. new.mode ^= ATTR_REVERSE;
  3284. if(ib > 0 && (ATTRCMP(base, new)
  3285. || ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
  3286. xdraws(buf, base, ox, y, ic, ib);
  3287. ic = ib = 0;
  3288. }
  3289. if(ib == 0) {
  3290. ox = x;
  3291. base = new;
  3292. }
  3293. sl = utf8len(new.c);
  3294. memcpy(buf+ib, new.c, sl);
  3295. ib += sl;
  3296. ic += (new.mode & ATTR_WIDE)? 2 : 1;
  3297. }
  3298. if(ib > 0)
  3299. xdraws(buf, base, ox, y, ic, ib);
  3300. }
  3301. xdrawcursor();
  3302. }
  3303. void
  3304. expose(XEvent *ev) {
  3305. redraw();
  3306. }
  3307. void
  3308. visibility(XEvent *ev) {
  3309. XVisibilityEvent *e = &ev->xvisibility;
  3310. MODBIT(xw.state, e->state != VisibilityFullyObscured, WIN_VISIBLE);
  3311. }
  3312. void
  3313. unmap(XEvent *ev) {
  3314. xw.state &= ~WIN_VISIBLE;
  3315. }
  3316. void
  3317. xsetpointermotion(int set) {
  3318. MODBIT(xw.attrs.event_mask, set, PointerMotionMask);
  3319. XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs);
  3320. }
  3321. void
  3322. xseturgency(int add) {
  3323. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  3324. MODBIT(h->flags, add, XUrgencyHint);
  3325. XSetWMHints(xw.dpy, xw.win, h);
  3326. XFree(h);
  3327. }
  3328. void
  3329. focus(XEvent *ev) {
  3330. XFocusChangeEvent *e = &ev->xfocus;
  3331. if(e->mode == NotifyGrab)
  3332. return;
  3333. if(ev->type == FocusIn) {
  3334. XSetICFocus(xw.xic);
  3335. xw.state |= WIN_FOCUSED;
  3336. xseturgency(0);
  3337. if(IS_SET(MODE_FOCUS))
  3338. ttywrite("\033[I", 3);
  3339. } else {
  3340. XUnsetICFocus(xw.xic);
  3341. xw.state &= ~WIN_FOCUSED;
  3342. if(IS_SET(MODE_FOCUS))
  3343. ttywrite("\033[O", 3);
  3344. }
  3345. }
  3346. bool
  3347. match(uint mask, uint state) {
  3348. return mask == XK_ANY_MOD || mask == (state & ~ignoremod);
  3349. }
  3350. void
  3351. numlock(const Arg *dummy) {
  3352. term.numlock ^= 1;
  3353. }
  3354. char*
  3355. kmap(KeySym k, uint state) {
  3356. Key *kp;
  3357. int i;
  3358. /* Check for mapped keys out of X11 function keys. */
  3359. for(i = 0; i < LEN(mappedkeys); i++) {
  3360. if(mappedkeys[i] == k)
  3361. break;
  3362. }
  3363. if(i == LEN(mappedkeys)) {
  3364. if((k & 0xFFFF) < 0xFD00)
  3365. return NULL;
  3366. }
  3367. for(kp = key; kp < key + LEN(key); kp++) {
  3368. if(kp->k != k)
  3369. continue;
  3370. if(!match(kp->mask, state))
  3371. continue;
  3372. if(IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0)
  3373. continue;
  3374. if(term.numlock && kp->appkey == 2)
  3375. continue;
  3376. if(IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0)
  3377. continue;
  3378. if(IS_SET(MODE_CRLF) ? kp->crlf < 0 : kp->crlf > 0)
  3379. continue;
  3380. return kp->s;
  3381. }
  3382. return NULL;
  3383. }
  3384. void
  3385. kpress(XEvent *ev) {
  3386. XKeyEvent *e = &ev->xkey;
  3387. KeySym ksym;
  3388. char buf[32], *customkey;
  3389. int len;
  3390. long c;
  3391. Status status;
  3392. Shortcut *bp;
  3393. if(IS_SET(MODE_KBDLOCK))
  3394. return;
  3395. len = XmbLookupString(xw.xic, e, buf, sizeof buf, &ksym, &status);
  3396. /* 1. shortcuts */
  3397. for(bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
  3398. if(ksym == bp->keysym && match(bp->mod, e->state)) {
  3399. bp->func(&(bp->arg));
  3400. return;
  3401. }
  3402. }
  3403. /* 2. custom keys from config.h */
  3404. if((customkey = kmap(ksym, e->state))) {
  3405. ttysend(customkey, strlen(customkey));
  3406. return;
  3407. }
  3408. /* 3. composed string from input method */
  3409. if(len == 0)
  3410. return;
  3411. if(len == 1 && e->state & Mod1Mask) {
  3412. if(IS_SET(MODE_8BIT)) {
  3413. if(*buf < 0177) {
  3414. c = *buf | 0x80;
  3415. len = utf8encode(c, buf, UTF_SIZ);
  3416. }
  3417. } else {
  3418. buf[1] = buf[0];
  3419. buf[0] = '\033';
  3420. len = 2;
  3421. }
  3422. }
  3423. ttysend(buf, len);
  3424. }
  3425. void
  3426. cmessage(XEvent *e) {
  3427. /*
  3428. * See xembed specs
  3429. * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
  3430. */
  3431. if(e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
  3432. if(e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
  3433. xw.state |= WIN_FOCUSED;
  3434. xseturgency(0);
  3435. } else if(e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
  3436. xw.state &= ~WIN_FOCUSED;
  3437. }
  3438. } else if(e->xclient.data.l[0] == xw.wmdeletewin) {
  3439. /* Send SIGHUP to shell */
  3440. kill(pid, SIGHUP);
  3441. exit(EXIT_SUCCESS);
  3442. }
  3443. }
  3444. void
  3445. cresize(int width, int height) {
  3446. int col, row;
  3447. if(width != 0)
  3448. xw.w = width;
  3449. if(height != 0)
  3450. xw.h = height;
  3451. col = (xw.w - 2 * borderpx) / xw.cw;
  3452. row = (xw.h - 2 * borderpx) / xw.ch;
  3453. tresize(col, row);
  3454. xresize(col, row);
  3455. ttyresize();
  3456. }
  3457. void
  3458. resize(XEvent *e) {
  3459. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  3460. return;
  3461. cresize(e->xconfigure.width, e->xconfigure.height);
  3462. }
  3463. void
  3464. run(void) {
  3465. XEvent ev;
  3466. int w = xw.w, h = xw.h;
  3467. fd_set rfd;
  3468. int xfd = XConnectionNumber(xw.dpy), xev, blinkset = 0, dodraw = 0;
  3469. struct timespec drawtimeout, *tv = NULL, now, last, lastblink;
  3470. long deltatime;
  3471. /* Waiting for window mapping */
  3472. do {
  3473. XNextEvent(xw.dpy, &ev);
  3474. if(ev.type == ConfigureNotify) {
  3475. w = ev.xconfigure.width;
  3476. h = ev.xconfigure.height;
  3477. }
  3478. } while(ev.type != MapNotify);
  3479. ttynew();
  3480. cresize(w, h);
  3481. clock_gettime(CLOCK_MONOTONIC, &last);
  3482. lastblink = last;
  3483. for(xev = actionfps;;) {
  3484. FD_ZERO(&rfd);
  3485. FD_SET(cmdfd, &rfd);
  3486. FD_SET(xfd, &rfd);
  3487. if(pselect(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv, NULL) < 0) {
  3488. if(errno == EINTR)
  3489. continue;
  3490. die("select failed: %s\n", strerror(errno));
  3491. }
  3492. if(FD_ISSET(cmdfd, &rfd)) {
  3493. ttyread();
  3494. if(blinktimeout) {
  3495. blinkset = tattrset(ATTR_BLINK);
  3496. if(!blinkset)
  3497. MODBIT(term.mode, 0, MODE_BLINK);
  3498. }
  3499. }
  3500. if(FD_ISSET(xfd, &rfd))
  3501. xev = actionfps;
  3502. clock_gettime(CLOCK_MONOTONIC, &now);
  3503. drawtimeout.tv_sec = 0;
  3504. drawtimeout.tv_nsec = (1000 * 1E6)/ xfps;
  3505. tv = &drawtimeout;
  3506. dodraw = 0;
  3507. if(blinktimeout && TIMEDIFF(now, lastblink) > blinktimeout) {
  3508. tsetdirtattr(ATTR_BLINK);
  3509. term.mode ^= MODE_BLINK;
  3510. lastblink = now;
  3511. dodraw = 1;
  3512. }
  3513. deltatime = TIMEDIFF(now, last);
  3514. if(deltatime > 1000 / (xev ? xfps : actionfps)) {
  3515. dodraw = 1;
  3516. last = now;
  3517. }
  3518. if(dodraw) {
  3519. while(XPending(xw.dpy)) {
  3520. XNextEvent(xw.dpy, &ev);
  3521. if(XFilterEvent(&ev, None))
  3522. continue;
  3523. if(handler[ev.type])
  3524. (handler[ev.type])(&ev);
  3525. }
  3526. draw();
  3527. XFlush(xw.dpy);
  3528. if(xev && !FD_ISSET(xfd, &rfd))
  3529. xev--;
  3530. if(!FD_ISSET(cmdfd, &rfd) && !FD_ISSET(xfd, &rfd)) {
  3531. if(blinkset) {
  3532. if(TIMEDIFF(now, lastblink) \
  3533. > blinktimeout) {
  3534. drawtimeout.tv_nsec = 1000;
  3535. } else {
  3536. drawtimeout.tv_nsec = (1E6 * \
  3537. (blinktimeout - \
  3538. TIMEDIFF(now,
  3539. lastblink)));
  3540. }
  3541. drawtimeout.tv_sec = \
  3542. drawtimeout.tv_nsec / 1E9;
  3543. drawtimeout.tv_nsec %= (long)1E9;
  3544. } else {
  3545. tv = NULL;
  3546. }
  3547. }
  3548. }
  3549. }
  3550. }
  3551. void
  3552. usage(void) {
  3553. die("%s " VERSION " (c) 2010-2015 st engineers\n"
  3554. "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]\n"
  3555. " [-i] [-t title] [-w windowid] [-e command ...] [command ...]\n"
  3556. " st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]\n"
  3557. " [-i] [-t title] [-w windowid] [-l line] [stty_args ...]\n",
  3558. argv0);
  3559. }
  3560. int
  3561. main(int argc, char *argv[]) {
  3562. uint cols = 80, rows = 24;
  3563. xw.l = xw.t = 0;
  3564. xw.isfixed = False;
  3565. xw.cursor = 0;
  3566. ARGBEGIN {
  3567. case 'a':
  3568. allowaltscreen = false;
  3569. break;
  3570. case 'c':
  3571. opt_class = EARGF(usage());
  3572. break;
  3573. case 'e':
  3574. if(argc > 1)
  3575. --argc, ++argv;
  3576. goto run;
  3577. case 'f':
  3578. opt_font = EARGF(usage());
  3579. break;
  3580. case 'g':
  3581. xw.gm = XParseGeometry(EARGF(usage()),
  3582. &xw.l, &xw.t, &cols, &rows);
  3583. break;
  3584. case 'i':
  3585. xw.isfixed = True;
  3586. break;
  3587. case 'o':
  3588. opt_io = EARGF(usage());
  3589. break;
  3590. case 'l':
  3591. opt_line = EARGF(usage());
  3592. break;
  3593. case 't':
  3594. opt_title = EARGF(usage());
  3595. break;
  3596. case 'w':
  3597. opt_embed = EARGF(usage());
  3598. break;
  3599. case 'v':
  3600. default:
  3601. usage();
  3602. } ARGEND;
  3603. run:
  3604. if(argc > 0) {
  3605. /* eat all remaining arguments */
  3606. opt_cmd = argv;
  3607. if(!opt_title && !opt_line)
  3608. opt_title = basename(xstrdup(argv[0]));
  3609. }
  3610. setlocale(LC_CTYPE, "");
  3611. XSetLocaleModifiers("");
  3612. tnew(MAX(cols, 1), MAX(rows, 1));
  3613. xinit();
  3614. selinit();
  3615. run();
  3616. return 0;
  3617. }