Simple Terminal from SuckLess
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.

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