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.

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