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.

4068 lines
89 KiB

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