My build of suckless st terminal
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2804 lines
61 KiB

13 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
15 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
12 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
  1. /* See LICENSE for licence details. */
  2. #define _XOPEN_SOURCE 600
  3. #include <ctype.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <limits.h>
  7. #include <locale.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 <sys/ioctl.h>
  15. #include <sys/select.h>
  16. #include <sys/stat.h>
  17. #include <sys/time.h>
  18. #include <sys/types.h>
  19. #include <sys/wait.h>
  20. #include <time.h>
  21. #include <unistd.h>
  22. #include <X11/Xatom.h>
  23. #include <X11/Xlib.h>
  24. #include <X11/Xutil.h>
  25. #include <X11/cursorfont.h>
  26. #include <X11/keysym.h>
  27. #include <X11/extensions/Xdbe.h>
  28. #include <X11/Xft/Xft.h>
  29. #include <fontconfig/fontconfig.h>
  30. #define Glyph Glyph_
  31. #define Font Font_
  32. #if defined(__linux)
  33. #include <pty.h>
  34. #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  35. #include <util.h>
  36. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  37. #include <libutil.h>
  38. #endif
  39. #define USAGE \
  40. "st " VERSION " (c) 2010-2012 st engineers\n" \
  41. "usage: st [-v] [-c class] [-f font] [-g geometry] [-o file]" \
  42. " [-t title] [-w windowid] [-e command ...]\n"
  43. /* XEMBED messages */
  44. #define XEMBED_FOCUS_IN 4
  45. #define XEMBED_FOCUS_OUT 5
  46. /* Arbitrary sizes */
  47. #define ESC_BUF_SIZ 256
  48. #define ESC_ARG_SIZ 16
  49. #define STR_BUF_SIZ 256
  50. #define STR_ARG_SIZ 16
  51. #define DRAW_BUF_SIZ 20*1024
  52. #define UTF_SIZ 4
  53. #define XK_NO_MOD UINT_MAX
  54. #define XK_ANY_MOD 0
  55. #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
  56. #define SERRNO strerror(errno)
  57. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  58. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  59. #define LEN(a) (sizeof(a) / sizeof(a[0]))
  60. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  61. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  62. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  63. #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
  64. #define IS_SET(flag) (term.mode & (flag))
  65. #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
  66. #define X2COL(x) (((x) - BORDER)/xw.cw)
  67. #define Y2ROW(y) (((y) - BORDER)/xw.ch)
  68. #define VT102ID "\033[?6c"
  69. enum glyph_attribute {
  70. ATTR_NULL = 0,
  71. ATTR_REVERSE = 1,
  72. ATTR_UNDERLINE = 2,
  73. ATTR_BOLD = 4,
  74. ATTR_GFX = 8,
  75. ATTR_ITALIC = 16,
  76. ATTR_BLINK = 32,
  77. };
  78. enum cursor_movement {
  79. CURSOR_UP,
  80. CURSOR_DOWN,
  81. CURSOR_LEFT,
  82. CURSOR_RIGHT,
  83. CURSOR_SAVE,
  84. CURSOR_LOAD
  85. };
  86. enum cursor_state {
  87. CURSOR_DEFAULT = 0,
  88. CURSOR_HIDE = 1,
  89. CURSOR_WRAPNEXT = 2
  90. };
  91. enum glyph_state {
  92. GLYPH_SET = 1,
  93. GLYPH_DIRTY = 2
  94. };
  95. enum term_mode {
  96. MODE_WRAP = 1,
  97. MODE_INSERT = 2,
  98. MODE_APPKEYPAD = 4,
  99. MODE_ALTSCREEN = 8,
  100. MODE_CRLF = 16,
  101. MODE_MOUSEBTN = 32,
  102. MODE_MOUSEMOTION = 64,
  103. MODE_MOUSE = 32|64,
  104. MODE_REVERSE = 128,
  105. MODE_KBDLOCK = 256
  106. };
  107. enum escape_state {
  108. ESC_START = 1,
  109. ESC_CSI = 2,
  110. ESC_STR = 4, /* DSC, OSC, PM, APC */
  111. ESC_ALTCHARSET = 8,
  112. ESC_STR_END = 16, /* a final string was encountered */
  113. ESC_TEST = 32, /* Enter in test mode */
  114. };
  115. enum window_state {
  116. WIN_VISIBLE = 1,
  117. WIN_REDRAW = 2,
  118. WIN_FOCUSED = 4
  119. };
  120. /* bit macro */
  121. #undef B0
  122. enum { B0=1, B1=2, B2=4, B3=8, B4=16, B5=32, B6=64, B7=128 };
  123. typedef unsigned char uchar;
  124. typedef unsigned int uint;
  125. typedef unsigned long ulong;
  126. typedef unsigned short ushort;
  127. typedef struct {
  128. char c[UTF_SIZ]; /* character code */
  129. uchar mode; /* attribute flags */
  130. ushort fg; /* foreground */
  131. ushort bg; /* background */
  132. uchar state; /* state flags */
  133. } Glyph;
  134. typedef Glyph* Line;
  135. typedef struct {
  136. Glyph attr; /* current char attributes */
  137. int x;
  138. int y;
  139. char state;
  140. } TCursor;
  141. /* CSI Escape sequence structs */
  142. /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
  143. typedef struct {
  144. char buf[ESC_BUF_SIZ]; /* raw string */
  145. int len; /* raw string length */
  146. char priv;
  147. int arg[ESC_ARG_SIZ];
  148. int narg; /* nb of args */
  149. char mode;
  150. } CSIEscape;
  151. /* STR Escape sequence structs */
  152. /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
  153. typedef struct {
  154. char type; /* ESC type ... */
  155. char buf[STR_BUF_SIZ]; /* raw string */
  156. int len; /* raw string length */
  157. char *args[STR_ARG_SIZ];
  158. int narg; /* nb of args */
  159. } STREscape;
  160. /* Internal representation of the screen */
  161. typedef struct {
  162. int row; /* nb row */
  163. int col; /* nb col */
  164. Line *line; /* screen */
  165. Line *alt; /* alternate screen */
  166. bool *dirty; /* dirtyness of lines */
  167. TCursor c; /* cursor */
  168. int top; /* top scroll limit */
  169. int bot; /* bottom scroll limit */
  170. int mode; /* terminal mode flags */
  171. int esc; /* escape state flags */
  172. bool *tabs;
  173. } Term;
  174. /* Purely graphic info */
  175. typedef struct {
  176. Display* dpy;
  177. Colormap cmap;
  178. Window win;
  179. XdbeBackBuffer buf;
  180. Atom xembed, wmdeletewin;
  181. XIM xim;
  182. XIC xic;
  183. XftDraw *xft_draw;
  184. Visual *vis;
  185. int scr;
  186. bool isfixed; /* is fixed geometry? */
  187. int fx, fy, fw, fh; /* fixed geometry */
  188. int tw, th; /* tty width and height */
  189. int w; /* window width */
  190. int h; /* window height */
  191. int ch; /* char height */
  192. int cw; /* char width */
  193. char state; /* focus, redraw, visible */
  194. } XWindow;
  195. typedef struct {
  196. KeySym k;
  197. uint mask;
  198. char s[ESC_BUF_SIZ];
  199. } Key;
  200. /* TODO: use better name for vars... */
  201. typedef struct {
  202. int mode;
  203. int bx, by;
  204. int ex, ey;
  205. struct {
  206. int x, y;
  207. } b, e;
  208. char *clip;
  209. Atom xtarget;
  210. bool alt;
  211. struct timeval tclick1;
  212. struct timeval tclick2;
  213. } Selection;
  214. #include "config.h"
  215. /* Font structure */
  216. typedef struct {
  217. int height;
  218. int width;
  219. int ascent;
  220. int descent;
  221. short lbearing;
  222. short rbearing;
  223. XftFont *xft_set;
  224. } Font;
  225. /* Drawing Context */
  226. typedef struct {
  227. XftColor xft_col[LEN(colorname) < 256 ? 256 : LEN(colorname)];
  228. GC gc;
  229. Font font, bfont, ifont, ibfont;
  230. } DC;
  231. static void die(const char *, ...);
  232. static void draw(void);
  233. static void redraw(void);
  234. static void drawregion(int, int, int, int);
  235. static void execsh(void);
  236. static void sigchld(int);
  237. static void run(void);
  238. static void csidump(void);
  239. static void csihandle(void);
  240. static void csiparse(void);
  241. static void csireset(void);
  242. static void strdump(void);
  243. static void strhandle(void);
  244. static void strparse(void);
  245. static void strreset(void);
  246. static void tclearregion(int, int, int, int);
  247. static void tcursor(int);
  248. static void tdeletechar(int);
  249. static void tdeleteline(int);
  250. static void tinsertblank(int);
  251. static void tinsertblankline(int);
  252. static void tmoveto(int, int);
  253. static void tnew(int, int);
  254. static void tnewline(int);
  255. static void tputtab(bool);
  256. static void tputc(char *, int);
  257. static void treset(void);
  258. static int tresize(int, int);
  259. static void tscrollup(int, int);
  260. static void tscrolldown(int, int);
  261. static void tsetattr(int*, int);
  262. static void tsetchar(char *, Glyph *, int, int);
  263. static void tsetscroll(int, int);
  264. static void tswapscreen(void);
  265. static void tsetdirt(int, int);
  266. static void tsetmode(bool, bool, int *, int);
  267. static void tfulldirt(void);
  268. static void ttynew(void);
  269. static void ttyread(void);
  270. static void ttyresize(void);
  271. static void ttywrite(const char *, size_t);
  272. static void xdraws(char *, Glyph, int, int, int, int);
  273. static void xhints(void);
  274. static void xclear(int, int, int, int);
  275. static void xdrawcursor(void);
  276. static void xinit(void);
  277. static void xloadcols(void);
  278. static void xresettitle(void);
  279. static void xseturgency(int);
  280. static void xsetsel(char*);
  281. static void xtermclear(int, int, int, int);
  282. static void xresize(int, int);
  283. static void expose(XEvent *);
  284. static void visibility(XEvent *);
  285. static void unmap(XEvent *);
  286. static char *kmap(KeySym, uint);
  287. static void kpress(XEvent *);
  288. static void cmessage(XEvent *);
  289. static void resize(XEvent *);
  290. static void focus(XEvent *);
  291. static void brelease(XEvent *);
  292. static void bpress(XEvent *);
  293. static void bmotion(XEvent *);
  294. static void selnotify(XEvent *);
  295. static void selclear(XEvent *);
  296. static void selrequest(XEvent *);
  297. static void selinit(void);
  298. static inline bool selected(int, int);
  299. static void selcopy(void);
  300. static void selpaste(void);
  301. static void selscroll(int, int);
  302. static int utf8decode(char *, long *);
  303. static int utf8encode(long *, char *);
  304. static int utf8size(char *);
  305. static int isfullutf8(char *, int);
  306. static ssize_t xwrite(int, char *, size_t);
  307. static void *xmalloc(size_t);
  308. static void *xrealloc(void *, size_t);
  309. static void *xcalloc(size_t nmemb, size_t size);
  310. static char *smstrcat(char *, ...);
  311. static void (*handler[LASTEvent])(XEvent *) = {
  312. [KeyPress] = kpress,
  313. [ClientMessage] = cmessage,
  314. [ConfigureNotify] = resize,
  315. [VisibilityNotify] = visibility,
  316. [UnmapNotify] = unmap,
  317. [Expose] = expose,
  318. [FocusIn] = focus,
  319. [FocusOut] = focus,
  320. [MotionNotify] = bmotion,
  321. [ButtonPress] = bpress,
  322. [ButtonRelease] = brelease,
  323. [SelectionClear] = selclear,
  324. [SelectionNotify] = selnotify,
  325. [SelectionRequest] = selrequest,
  326. };
  327. /* Globals */
  328. static DC dc;
  329. static XWindow xw;
  330. static Term term;
  331. static CSIEscape csiescseq;
  332. static STREscape strescseq;
  333. static int cmdfd;
  334. static pid_t pid;
  335. static Selection sel;
  336. static int iofd = -1;
  337. static char **opt_cmd = NULL;
  338. static char *opt_io = NULL;
  339. static char *opt_title = NULL;
  340. static char *opt_embed = NULL;
  341. static char *opt_class = NULL;
  342. static char *opt_font = NULL;
  343. ssize_t
  344. xwrite(int fd, char *s, size_t len) {
  345. size_t aux = len;
  346. while(len > 0) {
  347. ssize_t r = write(fd, s, len);
  348. if(r < 0)
  349. return r;
  350. len -= r;
  351. s += r;
  352. }
  353. return aux;
  354. }
  355. void *
  356. xmalloc(size_t len) {
  357. void *p = malloc(len);
  358. if(!p)
  359. die("Out of memory\n");
  360. return p;
  361. }
  362. void *
  363. xrealloc(void *p, size_t len) {
  364. if((p = realloc(p, len)) == NULL)
  365. die("Out of memory\n");
  366. return p;
  367. }
  368. void *
  369. xcalloc(size_t nmemb, size_t size) {
  370. void *p = calloc(nmemb, size);
  371. if(!p)
  372. die("Out of memory\n");
  373. return p;
  374. }
  375. char *
  376. smstrcat(char *src, ...)
  377. {
  378. va_list fmtargs;
  379. char *ret, *p, *v;
  380. int len, slen, flen;
  381. len = slen = strlen(src);
  382. va_start(fmtargs, src);
  383. for(;;) {
  384. v = va_arg(fmtargs, char *);
  385. if(v == NULL)
  386. break;
  387. len += strlen(v);
  388. }
  389. va_end(fmtargs);
  390. p = ret = xmalloc(len+1);
  391. memmove(p, src, slen);
  392. p += slen;
  393. va_start(fmtargs, src);
  394. for(;;) {
  395. v = va_arg(fmtargs, char *);
  396. if(v == NULL)
  397. break;
  398. flen = strlen(v);
  399. memmove(p, v, flen);
  400. p += flen;
  401. }
  402. va_end(fmtargs);
  403. ret[len] = '\0';
  404. return ret;
  405. }
  406. int
  407. utf8decode(char *s, long *u) {
  408. uchar c;
  409. int i, n, rtn;
  410. rtn = 1;
  411. c = *s;
  412. if(~c & B7) { /* 0xxxxxxx */
  413. *u = c;
  414. return rtn;
  415. } else if((c & (B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
  416. *u = c&(B4|B3|B2|B1|B0);
  417. n = 1;
  418. } else if((c & (B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
  419. *u = c&(B3|B2|B1|B0);
  420. n = 2;
  421. } else if((c & (B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
  422. *u = c & (B2|B1|B0);
  423. n = 3;
  424. } else {
  425. goto invalid;
  426. }
  427. for(i = n, ++s; i > 0; --i, ++rtn, ++s) {
  428. c = *s;
  429. if((c & (B7|B6)) != B7) /* 10xxxxxx */
  430. goto invalid;
  431. *u <<= 6;
  432. *u |= c & (B5|B4|B3|B2|B1|B0);
  433. }
  434. if((n == 1 && *u < 0x80) ||
  435. (n == 2 && *u < 0x800) ||
  436. (n == 3 && *u < 0x10000) ||
  437. (*u >= 0xD800 && *u <= 0xDFFF)) {
  438. goto invalid;
  439. }
  440. return rtn;
  441. invalid:
  442. *u = 0xFFFD;
  443. return rtn;
  444. }
  445. int
  446. utf8encode(long *u, char *s) {
  447. uchar *sp;
  448. ulong uc;
  449. int i, n;
  450. sp = (uchar *)s;
  451. uc = *u;
  452. if(uc < 0x80) {
  453. *sp = uc; /* 0xxxxxxx */
  454. return 1;
  455. } else if(*u < 0x800) {
  456. *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
  457. n = 1;
  458. } else if(uc < 0x10000) {
  459. *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
  460. n = 2;
  461. } else if(uc <= 0x10FFFF) {
  462. *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
  463. n = 3;
  464. } else {
  465. goto invalid;
  466. }
  467. for(i=n,++sp; i>0; --i,++sp)
  468. *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
  469. return n+1;
  470. invalid:
  471. /* U+FFFD */
  472. *s++ = '\xEF';
  473. *s++ = '\xBF';
  474. *s = '\xBD';
  475. return 3;
  476. }
  477. /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
  478. UTF-8 otherwise return 0 */
  479. int
  480. isfullutf8(char *s, int b) {
  481. uchar *c1, *c2, *c3;
  482. c1 = (uchar *)s;
  483. c2 = (uchar *)++s;
  484. c3 = (uchar *)++s;
  485. if(b < 1) {
  486. return 0;
  487. } else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1) {
  488. return 0;
  489. } else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
  490. ((b == 1) ||
  491. ((b == 2) && (*c2&(B7|B6)) == B7))) {
  492. return 0;
  493. } else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
  494. ((b == 1) ||
  495. ((b == 2) && (*c2&(B7|B6)) == B7) ||
  496. ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7))) {
  497. return 0;
  498. } else {
  499. return 1;
  500. }
  501. }
  502. int
  503. utf8size(char *s) {
  504. uchar c = *s;
  505. if(~c&B7) {
  506. return 1;
  507. } else if((c&(B7|B6|B5)) == (B7|B6)) {
  508. return 2;
  509. } else if((c&(B7|B6|B5|B4)) == (B7|B6|B5)) {
  510. return 3;
  511. } else {
  512. return 4;
  513. }
  514. }
  515. void
  516. selinit(void) {
  517. memset(&sel.tclick1, 0, sizeof(sel.tclick1));
  518. memset(&sel.tclick2, 0, sizeof(sel.tclick2));
  519. sel.mode = 0;
  520. sel.bx = -1;
  521. sel.clip = NULL;
  522. sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
  523. if(sel.xtarget == None)
  524. sel.xtarget = XA_STRING;
  525. }
  526. static inline bool
  527. selected(int x, int y) {
  528. int bx, ex;
  529. if(sel.ey == y && sel.by == y) {
  530. bx = MIN(sel.bx, sel.ex);
  531. ex = MAX(sel.bx, sel.ex);
  532. return BETWEEN(x, bx, ex);
  533. }
  534. return ((sel.b.y < y && y < sel.e.y)
  535. || (y == sel.e.y && x <= sel.e.x))
  536. || (y == sel.b.y && x >= sel.b.x
  537. && (x <= sel.e.x || sel.b.y != sel.e.y));
  538. }
  539. void
  540. getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
  541. if(b)
  542. *b = e->xbutton.button;
  543. *x = X2COL(e->xbutton.x);
  544. *y = Y2ROW(e->xbutton.y);
  545. sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
  546. sel.b.y = MIN(sel.by, sel.ey);
  547. sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
  548. sel.e.y = MAX(sel.by, sel.ey);
  549. }
  550. void
  551. mousereport(XEvent *e) {
  552. int x = X2COL(e->xbutton.x);
  553. int y = Y2ROW(e->xbutton.y);
  554. int button = e->xbutton.button;
  555. int state = e->xbutton.state;
  556. char buf[] = { '\033', '[', 'M', 0, 32+x+1, 32+y+1 };
  557. static int ob, ox, oy;
  558. /* from urxvt */
  559. if(e->xbutton.type == MotionNotify) {
  560. if(!IS_SET(MODE_MOUSEMOTION) || (x == ox && y == oy))
  561. return;
  562. button = ob + 32;
  563. ox = x, oy = y;
  564. } else if(e->xbutton.type == ButtonRelease || button == AnyButton) {
  565. button = 3;
  566. } else {
  567. button -= Button1;
  568. if(button >= 3)
  569. button += 64 - 3;
  570. if(e->xbutton.type == ButtonPress) {
  571. ob = button;
  572. ox = x, oy = y;
  573. }
  574. }
  575. buf[3] = 32 + button + (state & ShiftMask ? 4 : 0)
  576. + (state & Mod4Mask ? 8 : 0)
  577. + (state & ControlMask ? 16 : 0);
  578. ttywrite(buf, sizeof(buf));
  579. }
  580. void
  581. bpress(XEvent *e) {
  582. if(IS_SET(MODE_MOUSE)) {
  583. mousereport(e);
  584. } else if(e->xbutton.button == Button1) {
  585. if(sel.bx != -1) {
  586. sel.bx = -1;
  587. tsetdirt(sel.b.y, sel.e.y);
  588. draw();
  589. }
  590. sel.mode = 1;
  591. sel.ex = sel.bx = X2COL(e->xbutton.x);
  592. sel.ey = sel.by = Y2ROW(e->xbutton.y);
  593. }
  594. }
  595. void
  596. selcopy(void) {
  597. char *str, *ptr;
  598. int x, y, bufsize, is_selected = 0, size;
  599. Glyph *gp;
  600. if(sel.bx == -1) {
  601. str = NULL;
  602. } else {
  603. bufsize = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
  604. ptr = str = xmalloc(bufsize);
  605. /* append every set & selected glyph to the selection */
  606. for(y = 0; y < term.row; y++) {
  607. for(x = 0; x < term.col; x++) {
  608. gp = &term.line[y][x];
  609. if(!(is_selected = selected(x, y))
  610. || !(gp->state & GLYPH_SET)) {
  611. continue;
  612. }
  613. size = utf8size(gp->c);
  614. memcpy(ptr, gp->c, size);
  615. ptr += size;
  616. }
  617. /* \n at the end of every selected line except for the last one */
  618. if(is_selected && y < sel.e.y)
  619. *ptr++ = '\n';
  620. }
  621. *ptr = 0;
  622. }
  623. sel.alt = IS_SET(MODE_ALTSCREEN);
  624. xsetsel(str);
  625. }
  626. void
  627. selnotify(XEvent *e) {
  628. ulong nitems, ofs, rem;
  629. int format;
  630. uchar *data;
  631. Atom type;
  632. ofs = 0;
  633. do {
  634. if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
  635. False, AnyPropertyType, &type, &format,
  636. &nitems, &rem, &data)) {
  637. fprintf(stderr, "Clipboard allocation failed\n");
  638. return;
  639. }
  640. ttywrite((const char *) data, nitems * format / 8);
  641. XFree(data);
  642. /* number of 32-bit chunks returned */
  643. ofs += nitems * format / 32;
  644. } while(rem > 0);
  645. }
  646. void
  647. selpaste(void) {
  648. XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY,
  649. xw.win, CurrentTime);
  650. }
  651. void selclear(XEvent *e) {
  652. if(sel.bx == -1)
  653. return;
  654. sel.bx = -1;
  655. tsetdirt(sel.b.y, sel.e.y);
  656. }
  657. void
  658. selrequest(XEvent *e) {
  659. XSelectionRequestEvent *xsre;
  660. XSelectionEvent xev;
  661. Atom xa_targets, string;
  662. xsre = (XSelectionRequestEvent *) e;
  663. xev.type = SelectionNotify;
  664. xev.requestor = xsre->requestor;
  665. xev.selection = xsre->selection;
  666. xev.target = xsre->target;
  667. xev.time = xsre->time;
  668. /* reject */
  669. xev.property = None;
  670. xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
  671. if(xsre->target == xa_targets) {
  672. /* respond with the supported type */
  673. string = sel.xtarget;
  674. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  675. XA_ATOM, 32, PropModeReplace,
  676. (uchar *) &string, 1);
  677. xev.property = xsre->property;
  678. } else if(xsre->target == sel.xtarget && sel.clip != NULL) {
  679. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  680. xsre->target, 8, PropModeReplace,
  681. (uchar *) sel.clip, strlen(sel.clip));
  682. xev.property = xsre->property;
  683. }
  684. /* all done, send a notification to the listener */
  685. if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
  686. fprintf(stderr, "Error sending SelectionNotify event\n");
  687. }
  688. void
  689. xsetsel(char *str) {
  690. /* register the selection for both the clipboard and the primary */
  691. Atom clipboard;
  692. free(sel.clip);
  693. sel.clip = str;
  694. XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
  695. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  696. XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
  697. }
  698. void
  699. brelease(XEvent *e) {
  700. struct timeval now;
  701. if(IS_SET(MODE_MOUSE)) {
  702. mousereport(e);
  703. return;
  704. }
  705. if(e->xbutton.button == Button2) {
  706. selpaste();
  707. } else if(e->xbutton.button == Button1) {
  708. sel.mode = 0;
  709. getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
  710. term.dirty[sel.ey] = 1;
  711. if(sel.bx == sel.ex && sel.by == sel.ey) {
  712. sel.bx = -1;
  713. gettimeofday(&now, NULL);
  714. if(TIMEDIFF(now, sel.tclick2) <= TRIPLECLICK_TIMEOUT) {
  715. /* triple click on the line */
  716. sel.b.x = sel.bx = 0;
  717. sel.e.x = sel.ex = term.col;
  718. sel.b.y = sel.e.y = sel.ey;
  719. selcopy();
  720. } else if(TIMEDIFF(now, sel.tclick1) <= DOUBLECLICK_TIMEOUT) {
  721. /* double click to select word */
  722. sel.bx = sel.ex;
  723. while(sel.bx > 0 && term.line[sel.ey][sel.bx-1].state & GLYPH_SET &&
  724. term.line[sel.ey][sel.bx-1].c[0] != ' ') {
  725. sel.bx--;
  726. }
  727. sel.b.x = sel.bx;
  728. while(sel.ex < term.col-1 && term.line[sel.ey][sel.ex+1].state & GLYPH_SET &&
  729. term.line[sel.ey][sel.ex+1].c[0] != ' ') {
  730. sel.ex++;
  731. }
  732. sel.e.x = sel.ex;
  733. sel.b.y = sel.e.y = sel.ey;
  734. selcopy();
  735. }
  736. } else {
  737. selcopy();
  738. }
  739. }
  740. memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
  741. gettimeofday(&sel.tclick1, NULL);
  742. }
  743. void
  744. bmotion(XEvent *e) {
  745. int starty, endy, oldey, oldex;
  746. if(IS_SET(MODE_MOUSE)) {
  747. mousereport(e);
  748. return;
  749. }
  750. if(sel.mode) {
  751. oldey = sel.ey;
  752. oldex = sel.ex;
  753. getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
  754. if(oldey != sel.ey || oldex != sel.ex) {
  755. starty = MIN(oldey, sel.ey);
  756. endy = MAX(oldey, sel.ey);
  757. tsetdirt(starty, endy);
  758. }
  759. }
  760. }
  761. void
  762. die(const char *errstr, ...) {
  763. va_list ap;
  764. va_start(ap, errstr);
  765. vfprintf(stderr, errstr, ap);
  766. va_end(ap);
  767. exit(EXIT_FAILURE);
  768. }
  769. void
  770. execsh(void) {
  771. char **args;
  772. char *envshell = getenv("SHELL");
  773. unsetenv("COLUMNS");
  774. unsetenv("LINES");
  775. unsetenv("TERMCAP");
  776. signal(SIGCHLD, SIG_DFL);
  777. signal(SIGHUP, SIG_DFL);
  778. signal(SIGINT, SIG_DFL);
  779. signal(SIGQUIT, SIG_DFL);
  780. signal(SIGTERM, SIG_DFL);
  781. signal(SIGALRM, SIG_DFL);
  782. DEFAULT(envshell, SHELL);
  783. putenv("TERM="TNAME);
  784. args = opt_cmd ? opt_cmd : (char *[]){envshell, "-i", NULL};
  785. execvp(args[0], args);
  786. exit(EXIT_FAILURE);
  787. }
  788. void
  789. sigchld(int a) {
  790. int stat = 0;
  791. if(waitpid(pid, &stat, 0) < 0)
  792. die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
  793. if(WIFEXITED(stat)) {
  794. exit(WEXITSTATUS(stat));
  795. } else {
  796. exit(EXIT_FAILURE);
  797. }
  798. }
  799. void
  800. ttynew(void) {
  801. int m, s;
  802. struct winsize w = {term.row, term.col, 0, 0};
  803. /* seems to work fine on linux, openbsd and freebsd */
  804. if(openpty(&m, &s, NULL, NULL, &w) < 0)
  805. die("openpty failed: %s\n", SERRNO);
  806. switch(pid = fork()) {
  807. case -1:
  808. die("fork failed\n");
  809. break;
  810. case 0:
  811. setsid(); /* create a new process group */
  812. dup2(s, STDIN_FILENO);
  813. dup2(s, STDOUT_FILENO);
  814. dup2(s, STDERR_FILENO);
  815. if(ioctl(s, TIOCSCTTY, NULL) < 0)
  816. die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
  817. close(s);
  818. close(m);
  819. execsh();
  820. break;
  821. default:
  822. close(s);
  823. cmdfd = m;
  824. signal(SIGCHLD, sigchld);
  825. if(opt_io) {
  826. iofd = (!strcmp(opt_io, "-")) ?
  827. STDOUT_FILENO :
  828. open(opt_io, O_WRONLY | O_CREAT, 0666);
  829. if(iofd < 0) {
  830. fprintf(stderr, "Error opening %s:%s\n",
  831. opt_io, strerror(errno));
  832. }
  833. }
  834. }
  835. }
  836. void
  837. dump(char c) {
  838. static int col;
  839. fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
  840. if(++col % 10 == 0)
  841. fprintf(stderr, "\n");
  842. }
  843. void
  844. ttyread(void) {
  845. static char buf[BUFSIZ];
  846. static int buflen = 0;
  847. char *ptr;
  848. char s[UTF_SIZ];
  849. int charsize; /* size of utf8 char in bytes */
  850. long utf8c;
  851. int ret;
  852. /* append read bytes to unprocessed bytes */
  853. if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
  854. die("Couldn't read from shell: %s\n", SERRNO);
  855. /* process every complete utf8 char */
  856. buflen += ret;
  857. ptr = buf;
  858. while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
  859. charsize = utf8decode(ptr, &utf8c);
  860. utf8encode(&utf8c, s);
  861. tputc(s, charsize);
  862. ptr += charsize;
  863. buflen -= charsize;
  864. }
  865. /* keep any uncomplete utf8 char for the next call */
  866. memmove(buf, ptr, buflen);
  867. }
  868. void
  869. ttywrite(const char *s, size_t n) {
  870. if(write(cmdfd, s, n) == -1)
  871. die("write error on tty: %s\n", SERRNO);
  872. }
  873. void
  874. ttyresize(void) {
  875. struct winsize w;
  876. w.ws_row = term.row;
  877. w.ws_col = term.col;
  878. w.ws_xpixel = xw.tw;
  879. w.ws_ypixel = xw.th;
  880. if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  881. fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
  882. }
  883. void
  884. tsetdirt(int top, int bot) {
  885. int i;
  886. LIMIT(top, 0, term.row-1);
  887. LIMIT(bot, 0, term.row-1);
  888. for(i = top; i <= bot; i++)
  889. term.dirty[i] = 1;
  890. }
  891. void
  892. tfulldirt(void) {
  893. tsetdirt(0, term.row-1);
  894. }
  895. void
  896. tcursor(int mode) {
  897. static TCursor c;
  898. if(mode == CURSOR_SAVE) {
  899. c = term.c;
  900. } else if(mode == CURSOR_LOAD) {
  901. term.c = c;
  902. tmoveto(c.x, c.y);
  903. }
  904. }
  905. void
  906. treset(void) {
  907. uint i;
  908. term.c = (TCursor){{
  909. .mode = ATTR_NULL,
  910. .fg = DefaultFG,
  911. .bg = DefaultBG
  912. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  913. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  914. for(i = TAB; i < term.col; i += TAB)
  915. term.tabs[i] = 1;
  916. term.top = 0;
  917. term.bot = term.row - 1;
  918. term.mode = MODE_WRAP;
  919. tclearregion(0, 0, term.col-1, term.row-1);
  920. }
  921. void
  922. tnew(int col, int row) {
  923. /* set screen size */
  924. term.row = row;
  925. term.col = col;
  926. term.line = xmalloc(term.row * sizeof(Line));
  927. term.alt = xmalloc(term.row * sizeof(Line));
  928. term.dirty = xmalloc(term.row * sizeof(*term.dirty));
  929. term.tabs = xmalloc(term.col * sizeof(*term.tabs));
  930. for(row = 0; row < term.row; row++) {
  931. term.line[row] = xmalloc(term.col * sizeof(Glyph));
  932. term.alt [row] = xmalloc(term.col * sizeof(Glyph));
  933. term.dirty[row] = 0;
  934. }
  935. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  936. /* setup screen */
  937. treset();
  938. }
  939. void
  940. tswapscreen(void) {
  941. Line *tmp = term.line;
  942. term.line = term.alt;
  943. term.alt = tmp;
  944. term.mode ^= MODE_ALTSCREEN;
  945. tfulldirt();
  946. }
  947. void
  948. tscrolldown(int orig, int n) {
  949. int i;
  950. Line temp;
  951. LIMIT(n, 0, term.bot-orig+1);
  952. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  953. for(i = term.bot; i >= orig+n; i--) {
  954. temp = term.line[i];
  955. term.line[i] = term.line[i-n];
  956. term.line[i-n] = temp;
  957. term.dirty[i] = 1;
  958. term.dirty[i-n] = 1;
  959. }
  960. selscroll(orig, n);
  961. }
  962. void
  963. tscrollup(int orig, int n) {
  964. int i;
  965. Line temp;
  966. LIMIT(n, 0, term.bot-orig+1);
  967. tclearregion(0, orig, term.col-1, orig+n-1);
  968. for(i = orig; i <= term.bot-n; i++) {
  969. temp = term.line[i];
  970. term.line[i] = term.line[i+n];
  971. term.line[i+n] = temp;
  972. term.dirty[i] = 1;
  973. term.dirty[i+n] = 1;
  974. }
  975. selscroll(orig, -n);
  976. }
  977. void
  978. selscroll(int orig, int n) {
  979. if(sel.bx == -1)
  980. return;
  981. if(BETWEEN(sel.by, orig, term.bot) || BETWEEN(sel.ey, orig, term.bot)) {
  982. if((sel.by += n) > term.bot || (sel.ey += n) < term.top) {
  983. sel.bx = -1;
  984. return;
  985. }
  986. if(sel.by < term.top) {
  987. sel.by = term.top;
  988. sel.bx = 0;
  989. }
  990. if(sel.ey > term.bot) {
  991. sel.ey = term.bot;
  992. sel.ex = term.col;
  993. }
  994. sel.b.y = sel.by, sel.b.x = sel.bx;
  995. sel.e.y = sel.ey, sel.e.x = sel.ex;
  996. }
  997. }
  998. void
  999. tnewline(int first_col) {
  1000. int y = term.c.y;
  1001. if(y == term.bot) {
  1002. tscrollup(term.top, 1);
  1003. } else {
  1004. y++;
  1005. }
  1006. tmoveto(first_col ? 0 : term.c.x, y);
  1007. }
  1008. void
  1009. csiparse(void) {
  1010. /* int noarg = 1; */
  1011. char *p = csiescseq.buf;
  1012. csiescseq.narg = 0;
  1013. if(*p == '?')
  1014. csiescseq.priv = 1, p++;
  1015. while(p < csiescseq.buf+csiescseq.len) {
  1016. while(isdigit(*p)) {
  1017. csiescseq.arg[csiescseq.narg] *= 10;
  1018. csiescseq.arg[csiescseq.narg] += *p++ - '0'/*, noarg = 0 */;
  1019. }
  1020. if(*p == ';' && csiescseq.narg+1 < ESC_ARG_SIZ) {
  1021. csiescseq.narg++, p++;
  1022. } else {
  1023. csiescseq.mode = *p;
  1024. csiescseq.narg++;
  1025. return;
  1026. }
  1027. }
  1028. }
  1029. void
  1030. tmoveto(int x, int y) {
  1031. LIMIT(x, 0, term.col-1);
  1032. LIMIT(y, 0, term.row-1);
  1033. term.c.state &= ~CURSOR_WRAPNEXT;
  1034. term.c.x = x;
  1035. term.c.y = y;
  1036. }
  1037. void
  1038. tsetchar(char *c, Glyph *attr, int x, int y) {
  1039. static char *vt100_0[62] = { /* 0x41 - 0x7e */
  1040. "", "", "", "", "", "", "", /* A - G */
  1041. 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
  1042. 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
  1043. 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
  1044. "", "", "", "", "", "", "°", "±", /* ` - g */
  1045. "", "", "", "", "", "", "", "", /* h - o */
  1046. "", "", "", "", "", "", "", "", /* p - w */
  1047. "", "", "", "π", "", "£", "·", /* x - ~ */
  1048. };
  1049. /*
  1050. * The table is proudly stolen from rxvt.
  1051. */
  1052. if(attr->mode & ATTR_GFX) {
  1053. if(c[0] >= 0x41 && c[0] <= 0x7e
  1054. && vt100_0[c[0] - 0x41]) {
  1055. c = vt100_0[c[0] - 0x41];
  1056. }
  1057. }
  1058. term.dirty[y] = 1;
  1059. term.line[y][x] = *attr;
  1060. memcpy(term.line[y][x].c, c, UTF_SIZ);
  1061. term.line[y][x].state |= GLYPH_SET;
  1062. }
  1063. void
  1064. tclearregion(int x1, int y1, int x2, int y2) {
  1065. int x, y, temp;
  1066. if(x1 > x2)
  1067. temp = x1, x1 = x2, x2 = temp;
  1068. if(y1 > y2)
  1069. temp = y1, y1 = y2, y2 = temp;
  1070. LIMIT(x1, 0, term.col-1);
  1071. LIMIT(x2, 0, term.col-1);
  1072. LIMIT(y1, 0, term.row-1);
  1073. LIMIT(y2, 0, term.row-1);
  1074. for(y = y1; y <= y2; y++) {
  1075. term.dirty[y] = 1;
  1076. for(x = x1; x <= x2; x++)
  1077. term.line[y][x].state = 0;
  1078. }
  1079. }
  1080. void
  1081. tdeletechar(int n) {
  1082. int src = term.c.x + n;
  1083. int dst = term.c.x;
  1084. int size = term.col - src;
  1085. term.dirty[term.c.y] = 1;
  1086. if(src >= term.col) {
  1087. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1088. return;
  1089. }
  1090. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src],
  1091. size * sizeof(Glyph));
  1092. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  1093. }
  1094. void
  1095. tinsertblank(int n) {
  1096. int src = term.c.x;
  1097. int dst = src + n;
  1098. int size = term.col - dst;
  1099. term.dirty[term.c.y] = 1;
  1100. if(dst >= term.col) {
  1101. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1102. return;
  1103. }
  1104. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src],
  1105. size * sizeof(Glyph));
  1106. tclearregion(src, term.c.y, dst - 1, term.c.y);
  1107. }
  1108. void
  1109. tinsertblankline(int n) {
  1110. if(term.c.y < term.top || term.c.y > term.bot)
  1111. return;
  1112. tscrolldown(term.c.y, n);
  1113. }
  1114. void
  1115. tdeleteline(int n) {
  1116. if(term.c.y < term.top || term.c.y > term.bot)
  1117. return;
  1118. tscrollup(term.c.y, n);
  1119. }
  1120. void
  1121. tsetattr(int *attr, int l) {
  1122. int i;
  1123. for(i = 0; i < l; i++) {
  1124. switch(attr[i]) {
  1125. case 0:
  1126. term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD \
  1127. | ATTR_ITALIC | ATTR_BLINK);
  1128. term.c.attr.fg = DefaultFG;
  1129. term.c.attr.bg = DefaultBG;
  1130. break;
  1131. case 1:
  1132. term.c.attr.mode |= ATTR_BOLD;
  1133. break;
  1134. case 3: /* enter standout (highlight) */
  1135. term.c.attr.mode |= ATTR_ITALIC;
  1136. break;
  1137. case 4:
  1138. term.c.attr.mode |= ATTR_UNDERLINE;
  1139. break;
  1140. case 5:
  1141. term.c.attr.mode |= ATTR_BLINK;
  1142. break;
  1143. case 7:
  1144. term.c.attr.mode |= ATTR_REVERSE;
  1145. break;
  1146. case 21:
  1147. case 22:
  1148. term.c.attr.mode &= ~ATTR_BOLD;
  1149. break;
  1150. case 23: /* leave standout (highlight) mode */
  1151. term.c.attr.mode &= ~ATTR_ITALIC;
  1152. break;
  1153. case 24:
  1154. term.c.attr.mode &= ~ATTR_UNDERLINE;
  1155. break;
  1156. case 25:
  1157. term.c.attr.mode &= ~ATTR_BLINK;
  1158. break;
  1159. case 27:
  1160. term.c.attr.mode &= ~ATTR_REVERSE;
  1161. break;
  1162. case 38:
  1163. if(i + 2 < l && attr[i + 1] == 5) {
  1164. i += 2;
  1165. if(BETWEEN(attr[i], 0, 255)) {
  1166. term.c.attr.fg = attr[i];
  1167. } else {
  1168. fprintf(stderr,
  1169. "erresc: bad fgcolor %d\n",
  1170. attr[i]);
  1171. }
  1172. } else {
  1173. fprintf(stderr,
  1174. "erresc(38): gfx attr %d unknown\n",
  1175. attr[i]);
  1176. }
  1177. break;
  1178. case 39:
  1179. term.c.attr.fg = DefaultFG;
  1180. break;
  1181. case 48:
  1182. if(i + 2 < l && attr[i + 1] == 5) {
  1183. i += 2;
  1184. if(BETWEEN(attr[i], 0, 255)) {
  1185. term.c.attr.bg = attr[i];
  1186. } else {
  1187. fprintf(stderr,
  1188. "erresc: bad bgcolor %d\n",
  1189. attr[i]);
  1190. }
  1191. } else {
  1192. fprintf(stderr,
  1193. "erresc(48): gfx attr %d unknown\n",
  1194. attr[i]);
  1195. }
  1196. break;
  1197. case 49:
  1198. term.c.attr.bg = DefaultBG;
  1199. break;
  1200. default:
  1201. if(BETWEEN(attr[i], 30, 37)) {
  1202. term.c.attr.fg = attr[i] - 30;
  1203. } else if(BETWEEN(attr[i], 40, 47)) {
  1204. term.c.attr.bg = attr[i] - 40;
  1205. } else if(BETWEEN(attr[i], 90, 97)) {
  1206. term.c.attr.fg = attr[i] - 90 + 8;
  1207. } else if(BETWEEN(attr[i], 100, 107)) {
  1208. term.c.attr.bg = attr[i] - 100 + 8;
  1209. } else {
  1210. fprintf(stderr,
  1211. "erresc(default): gfx attr %d unknown\n",
  1212. attr[i]), csidump();
  1213. }
  1214. break;
  1215. }
  1216. }
  1217. }
  1218. void
  1219. tsetscroll(int t, int b) {
  1220. int temp;
  1221. LIMIT(t, 0, term.row-1);
  1222. LIMIT(b, 0, term.row-1);
  1223. if(t > b) {
  1224. temp = t;
  1225. t = b;
  1226. b = temp;
  1227. }
  1228. term.top = t;
  1229. term.bot = b;
  1230. }
  1231. #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
  1232. void
  1233. tsetmode(bool priv, bool set, int *args, int narg) {
  1234. int *lim, mode;
  1235. for(lim = args + narg; args < lim; ++args) {
  1236. if(priv) {
  1237. switch(*args) {
  1238. break;
  1239. case 1: /* DECCKM -- Cursor key */
  1240. MODBIT(term.mode, set, MODE_APPKEYPAD);
  1241. break;
  1242. case 5: /* DECSCNM -- Reverse video */
  1243. mode = term.mode;
  1244. MODBIT(term.mode, set, MODE_REVERSE);
  1245. if(mode != term.mode)
  1246. redraw();
  1247. break;
  1248. case 6: /* XXX: DECOM -- Origin */
  1249. break;
  1250. case 7: /* DECAWM -- Auto wrap */
  1251. MODBIT(term.mode, set, MODE_WRAP);
  1252. break;
  1253. case 8: /* XXX: DECARM -- Auto repeat */
  1254. break;
  1255. case 0: /* Error (IGNORED) */
  1256. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1257. break;
  1258. case 25:
  1259. MODBIT(term.c.state, !set, CURSOR_HIDE);
  1260. break;
  1261. case 1000: /* 1000,1002: enable xterm mouse report */
  1262. MODBIT(term.mode, set, MODE_MOUSEBTN);
  1263. break;
  1264. case 1002:
  1265. MODBIT(term.mode, set, MODE_MOUSEMOTION);
  1266. break;
  1267. case 1049: /* = 1047 and 1048 */
  1268. case 47:
  1269. case 1047:
  1270. if(IS_SET(MODE_ALTSCREEN))
  1271. tclearregion(0, 0, term.col-1, term.row-1);
  1272. if((set && !IS_SET(MODE_ALTSCREEN)) ||
  1273. (!set && IS_SET(MODE_ALTSCREEN))) {
  1274. tswapscreen();
  1275. }
  1276. if(*args != 1049)
  1277. break;
  1278. /* pass through */
  1279. case 1048:
  1280. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1281. break;
  1282. default:
  1283. /* case 2: DECANM -- ANSI/VT52 (NOT SUPPOURTED) */
  1284. /* case 3: DECCOLM -- Column (NOT SUPPORTED) */
  1285. /* case 4: DECSCLM -- Scroll (NOT SUPPORTED) */
  1286. /* case 18: DECPFF -- Printer feed (NOT SUPPORTED) */
  1287. /* case 19: DECPEX -- Printer extent (NOT SUPPORTED) */
  1288. /* case 42: DECNRCM -- National characters (NOT SUPPORTED) */
  1289. fprintf(stderr,
  1290. "erresc: unknown private set/reset mode %d\n",
  1291. *args);
  1292. break;
  1293. }
  1294. } else {
  1295. switch(*args) {
  1296. case 0: /* Error (IGNORED) */
  1297. break;
  1298. case 2: /* KAM -- keyboard action */
  1299. MODBIT(term.mode, set, MODE_KBDLOCK);
  1300. break;
  1301. case 4: /* IRM -- Insertion-replacement */
  1302. MODBIT(term.mode, set, MODE_INSERT);
  1303. break;
  1304. case 12: /* XXX: SRM -- Send/Receive */
  1305. break;
  1306. case 20: /* LNM -- Linefeed/new line */
  1307. MODBIT(term.mode, set, MODE_CRLF);
  1308. break;
  1309. default:
  1310. fprintf(stderr,
  1311. "erresc: unknown set/reset mode %d\n",
  1312. *args);
  1313. break;
  1314. }
  1315. }
  1316. }
  1317. }
  1318. #undef MODBIT
  1319. void
  1320. csihandle(void) {
  1321. switch(csiescseq.mode) {
  1322. default:
  1323. unknown:
  1324. fprintf(stderr, "erresc: unknown csi ");
  1325. csidump();
  1326. /* die(""); */
  1327. break;
  1328. case '@': /* ICH -- Insert <n> blank char */
  1329. DEFAULT(csiescseq.arg[0], 1);
  1330. tinsertblank(csiescseq.arg[0]);
  1331. break;
  1332. case 'A': /* CUU -- Cursor <n> Up */
  1333. case 'e':
  1334. DEFAULT(csiescseq.arg[0], 1);
  1335. tmoveto(term.c.x, term.c.y-csiescseq.arg[0]);
  1336. break;
  1337. case 'B': /* CUD -- Cursor <n> Down */
  1338. DEFAULT(csiescseq.arg[0], 1);
  1339. tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
  1340. break;
  1341. case 'c': /* DA -- Device Attributes */
  1342. if(csiescseq.arg[0] == 0)
  1343. ttywrite(VT102ID, sizeof(VT102ID) - 1);
  1344. break;
  1345. case 'C': /* CUF -- Cursor <n> Forward */
  1346. case 'a':
  1347. DEFAULT(csiescseq.arg[0], 1);
  1348. tmoveto(term.c.x+csiescseq.arg[0], term.c.y);
  1349. break;
  1350. case 'D': /* CUB -- Cursor <n> Backward */
  1351. DEFAULT(csiescseq.arg[0], 1);
  1352. tmoveto(term.c.x-csiescseq.arg[0], term.c.y);
  1353. break;
  1354. case 'E': /* CNL -- Cursor <n> Down and first col */
  1355. DEFAULT(csiescseq.arg[0], 1);
  1356. tmoveto(0, term.c.y+csiescseq.arg[0]);
  1357. break;
  1358. case 'F': /* CPL -- Cursor <n> Up and first col */
  1359. DEFAULT(csiescseq.arg[0], 1);
  1360. tmoveto(0, term.c.y-csiescseq.arg[0]);
  1361. break;
  1362. case 'g': /* TBC -- Tabulation clear */
  1363. switch (csiescseq.arg[0]) {
  1364. case 0: /* clear current tab stop */
  1365. term.tabs[term.c.x] = 0;
  1366. break;
  1367. case 3: /* clear all the tabs */
  1368. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  1369. break;
  1370. default:
  1371. goto unknown;
  1372. }
  1373. break;
  1374. case 'G': /* CHA -- Move to <col> */
  1375. case '`': /* HPA */
  1376. DEFAULT(csiescseq.arg[0], 1);
  1377. tmoveto(csiescseq.arg[0]-1, term.c.y);
  1378. break;
  1379. case 'H': /* CUP -- Move to <row> <col> */
  1380. case 'f': /* HVP */
  1381. DEFAULT(csiescseq.arg[0], 1);
  1382. DEFAULT(csiescseq.arg[1], 1);
  1383. tmoveto(csiescseq.arg[1]-1, csiescseq.arg[0]-1);
  1384. break;
  1385. case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
  1386. DEFAULT(csiescseq.arg[0], 1);
  1387. while(csiescseq.arg[0]--)
  1388. tputtab(1);
  1389. break;
  1390. case 'J': /* ED -- Clear screen */
  1391. sel.bx = -1;
  1392. switch(csiescseq.arg[0]) {
  1393. case 0: /* below */
  1394. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1395. if(term.c.y < term.row-1)
  1396. tclearregion(0, term.c.y+1, term.col-1, term.row-1);
  1397. break;
  1398. case 1: /* above */
  1399. if(term.c.y > 1)
  1400. tclearregion(0, 0, term.col-1, term.c.y-1);
  1401. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1402. break;
  1403. case 2: /* all */
  1404. tclearregion(0, 0, term.col-1, term.row-1);
  1405. break;
  1406. default:
  1407. goto unknown;
  1408. }
  1409. break;
  1410. case 'K': /* EL -- Clear line */
  1411. switch(csiescseq.arg[0]) {
  1412. case 0: /* right */
  1413. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1414. break;
  1415. case 1: /* left */
  1416. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1417. break;
  1418. case 2: /* all */
  1419. tclearregion(0, term.c.y, term.col-1, term.c.y);
  1420. break;
  1421. }
  1422. break;
  1423. case 'S': /* SU -- Scroll <n> line up */
  1424. DEFAULT(csiescseq.arg[0], 1);
  1425. tscrollup(term.top, csiescseq.arg[0]);
  1426. break;
  1427. case 'T': /* SD -- Scroll <n> line down */
  1428. DEFAULT(csiescseq.arg[0], 1);
  1429. tscrolldown(term.top, csiescseq.arg[0]);
  1430. break;
  1431. case 'L': /* IL -- Insert <n> blank lines */
  1432. DEFAULT(csiescseq.arg[0], 1);
  1433. tinsertblankline(csiescseq.arg[0]);
  1434. break;
  1435. case 'l': /* RM -- Reset Mode */
  1436. tsetmode(csiescseq.priv, 0, csiescseq.arg, csiescseq.narg);
  1437. break;
  1438. case 'M': /* DL -- Delete <n> lines */
  1439. DEFAULT(csiescseq.arg[0], 1);
  1440. tdeleteline(csiescseq.arg[0]);
  1441. break;
  1442. case 'X': /* ECH -- Erase <n> char */
  1443. DEFAULT(csiescseq.arg[0], 1);
  1444. tclearregion(term.c.x, term.c.y, term.c.x + csiescseq.arg[0], term.c.y);
  1445. break;
  1446. case 'P': /* DCH -- Delete <n> char */
  1447. DEFAULT(csiescseq.arg[0], 1);
  1448. tdeletechar(csiescseq.arg[0]);
  1449. break;
  1450. case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
  1451. DEFAULT(csiescseq.arg[0], 1);
  1452. while(csiescseq.arg[0]--)
  1453. tputtab(0);
  1454. break;
  1455. case 'd': /* VPA -- Move to <row> */
  1456. DEFAULT(csiescseq.arg[0], 1);
  1457. tmoveto(term.c.x, csiescseq.arg[0]-1);
  1458. break;
  1459. case 'h': /* SM -- Set terminal mode */
  1460. tsetmode(csiescseq.priv, 1, csiescseq.arg, csiescseq.narg);
  1461. break;
  1462. case 'm': /* SGR -- Terminal attribute (color) */
  1463. tsetattr(csiescseq.arg, csiescseq.narg);
  1464. break;
  1465. case 'r': /* DECSTBM -- Set Scrolling Region */
  1466. if(csiescseq.priv) {
  1467. goto unknown;
  1468. } else {
  1469. DEFAULT(csiescseq.arg[0], 1);
  1470. DEFAULT(csiescseq.arg[1], term.row);
  1471. tsetscroll(csiescseq.arg[0]-1, csiescseq.arg[1]-1);
  1472. tmoveto(0, 0);
  1473. }
  1474. break;
  1475. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  1476. tcursor(CURSOR_SAVE);
  1477. break;
  1478. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  1479. tcursor(CURSOR_LOAD);
  1480. break;
  1481. }
  1482. }
  1483. void
  1484. csidump(void) {
  1485. int i;
  1486. uint c;
  1487. printf("ESC[");
  1488. for(i = 0; i < csiescseq.len; i++) {
  1489. c = csiescseq.buf[i] & 0xff;
  1490. if(isprint(c)) {
  1491. putchar(c);
  1492. } else if(c == '\n') {
  1493. printf("(\\n)");
  1494. } else if(c == '\r') {
  1495. printf("(\\r)");
  1496. } else if(c == 0x1b) {
  1497. printf("(\\e)");
  1498. } else {
  1499. printf("(%02x)", c);
  1500. }
  1501. }
  1502. putchar('\n');
  1503. }
  1504. void
  1505. csireset(void) {
  1506. memset(&csiescseq, 0, sizeof(csiescseq));
  1507. }
  1508. void
  1509. strhandle(void) {
  1510. char *p;
  1511. /*
  1512. * TODO: make this being useful in case of color palette change.
  1513. */
  1514. strparse();
  1515. p = strescseq.buf;
  1516. switch(strescseq.type) {
  1517. case ']': /* OSC -- Operating System Command */
  1518. switch(p[0]) {
  1519. case '0':
  1520. case '1':
  1521. case '2':
  1522. /*
  1523. * TODO: Handle special chars in string, like umlauts.
  1524. */
  1525. if(p[1] == ';') {
  1526. XStoreName(xw.dpy, xw.win, strescseq.buf+2);
  1527. }
  1528. break;
  1529. case ';':
  1530. XStoreName(xw.dpy, xw.win, strescseq.buf+1);
  1531. break;
  1532. case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
  1533. break;
  1534. default:
  1535. fprintf(stderr, "erresc: unknown str ");
  1536. strdump();
  1537. break;
  1538. }
  1539. break;
  1540. case 'k': /* old title set compatibility */
  1541. XStoreName(xw.dpy, xw.win, strescseq.buf);
  1542. break;
  1543. case 'P': /* DSC -- Device Control String */
  1544. case '_': /* APC -- Application Program Command */
  1545. case '^': /* PM -- Privacy Message */
  1546. default:
  1547. fprintf(stderr, "erresc: unknown str ");
  1548. strdump();
  1549. /* die(""); */
  1550. break;
  1551. }
  1552. }
  1553. void
  1554. strparse(void) {
  1555. /*
  1556. * TODO: Implement parsing like for CSI when required.
  1557. * Format: ESC type cmd ';' arg0 [';' argn] ESC \
  1558. */
  1559. return;
  1560. }
  1561. void
  1562. strdump(void) {
  1563. int i;
  1564. uint c;
  1565. printf("ESC%c", strescseq.type);
  1566. for(i = 0; i < strescseq.len; i++) {
  1567. c = strescseq.buf[i] & 0xff;
  1568. if(isprint(c)) {
  1569. putchar(c);
  1570. } else if(c == '\n') {
  1571. printf("(\\n)");
  1572. } else if(c == '\r') {
  1573. printf("(\\r)");
  1574. } else if(c == 0x1b) {
  1575. printf("(\\e)");
  1576. } else {
  1577. printf("(%02x)", c);
  1578. }
  1579. }
  1580. printf("ESC\\\n");
  1581. }
  1582. void
  1583. strreset(void) {
  1584. memset(&strescseq, 0, sizeof(strescseq));
  1585. }
  1586. void
  1587. tputtab(bool forward) {
  1588. uint x = term.c.x;
  1589. if(forward) {
  1590. if(x == term.col)
  1591. return;
  1592. for(++x; x < term.col && !term.tabs[x]; ++x)
  1593. /* nothing */ ;
  1594. } else {
  1595. if(x == 0)
  1596. return;
  1597. for(--x; x > 0 && !term.tabs[x]; --x)
  1598. /* nothing */ ;
  1599. }
  1600. tmoveto(x, term.c.y);
  1601. }
  1602. void
  1603. tputc(char *c, int len) {
  1604. uchar ascii = *c;
  1605. bool control = ascii < '\x20' || ascii == 0177;
  1606. if(iofd != -1) {
  1607. if (xwrite(iofd, c, len) < 0) {
  1608. fprintf(stderr, "Error writting in %s:%s\n",
  1609. opt_io, strerror(errno));
  1610. close(iofd);
  1611. iofd = -1;
  1612. }
  1613. }
  1614. /*
  1615. * STR sequences must be checked before of anything
  1616. * because it can use some control codes as part of the sequence
  1617. */
  1618. if(term.esc & ESC_STR) {
  1619. switch(ascii) {
  1620. case '\033':
  1621. term.esc = ESC_START | ESC_STR_END;
  1622. break;
  1623. case '\a': /* backwards compatibility to xterm */
  1624. term.esc = 0;
  1625. strhandle();
  1626. break;
  1627. default:
  1628. strescseq.buf[strescseq.len++] = ascii;
  1629. if(strescseq.len+1 >= STR_BUF_SIZ) {
  1630. term.esc = 0;
  1631. strhandle();
  1632. }
  1633. }
  1634. return;
  1635. }
  1636. /*
  1637. * Actions of control codes must be performed as soon they arrive
  1638. * because they can be embedded inside a control sequence, and
  1639. * they must not cause conflicts with sequences.
  1640. */
  1641. if(control) {
  1642. switch(ascii) {
  1643. case '\t': /* HT */
  1644. tputtab(1);
  1645. return;
  1646. case '\b': /* BS */
  1647. tmoveto(term.c.x-1, term.c.y);
  1648. return;
  1649. case '\r': /* CR */
  1650. tmoveto(0, term.c.y);
  1651. return;
  1652. case '\f': /* LF */
  1653. case '\v': /* VT */
  1654. case '\n': /* LF */
  1655. /* go to first col if the mode is set */
  1656. tnewline(IS_SET(MODE_CRLF));
  1657. return;
  1658. case '\a': /* BEL */
  1659. if(!(xw.state & WIN_FOCUSED))
  1660. xseturgency(1);
  1661. return;
  1662. case '\033': /* ESC */
  1663. csireset();
  1664. term.esc = ESC_START;
  1665. return;
  1666. case '\016': /* SO */
  1667. term.c.attr.mode |= ATTR_GFX;
  1668. return;
  1669. case '\017': /* SI */
  1670. term.c.attr.mode &= ~ATTR_GFX;
  1671. return;
  1672. case '\032': /* SUB */
  1673. case '\030': /* CAN */
  1674. csireset();
  1675. return;
  1676. case '\005': /* ENQ (IGNORED) */
  1677. case '\000': /* NUL (IGNORED) */
  1678. case '\021': /* XON (IGNORED) */
  1679. case '\023': /* XOFF (IGNORED) */
  1680. case 0177: /* DEL (IGNORED) */
  1681. return;
  1682. }
  1683. } else if(term.esc & ESC_START) {
  1684. if(term.esc & ESC_CSI) {
  1685. csiescseq.buf[csiescseq.len++] = ascii;
  1686. if(BETWEEN(ascii, 0x40, 0x7E)
  1687. || csiescseq.len >= ESC_BUF_SIZ) {
  1688. term.esc = 0;
  1689. csiparse(), csihandle();
  1690. }
  1691. } else if(term.esc & ESC_STR_END) {
  1692. term.esc = 0;
  1693. if(ascii == '\\')
  1694. strhandle();
  1695. } else if(term.esc & ESC_ALTCHARSET) {
  1696. switch(ascii) {
  1697. case '0': /* Line drawing set */
  1698. term.c.attr.mode |= ATTR_GFX;
  1699. break;
  1700. case 'B': /* USASCII */
  1701. term.c.attr.mode &= ~ATTR_GFX;
  1702. break;
  1703. case 'A': /* UK (IGNORED) */
  1704. case '<': /* multinational charset (IGNORED) */
  1705. case '5': /* Finnish (IGNORED) */
  1706. case 'C': /* Finnish (IGNORED) */
  1707. case 'K': /* German (IGNORED) */
  1708. break;
  1709. default:
  1710. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  1711. }
  1712. term.esc = 0;
  1713. } else if(term.esc & ESC_TEST) {
  1714. if(ascii == '8') { /* DEC screen alignment test. */
  1715. char E[UTF_SIZ] = "E";
  1716. int x, y;
  1717. for(x = 0; x < term.col; ++x) {
  1718. for(y = 0; y < term.row; ++y)
  1719. tsetchar(E, &term.c.attr, x, y);
  1720. }
  1721. }
  1722. term.esc = 0;
  1723. } else {
  1724. switch(ascii) {
  1725. case '[':
  1726. term.esc |= ESC_CSI;
  1727. break;
  1728. case '#':
  1729. term.esc |= ESC_TEST;
  1730. break;
  1731. case 'P': /* DCS -- Device Control String */
  1732. case '_': /* APC -- Application Program Command */
  1733. case '^': /* PM -- Privacy Message */
  1734. case ']': /* OSC -- Operating System Command */
  1735. case 'k': /* old title set compatibility */
  1736. strreset();
  1737. strescseq.type = ascii;
  1738. term.esc |= ESC_STR;
  1739. break;
  1740. case '(': /* set primary charset G0 */
  1741. term.esc |= ESC_ALTCHARSET;
  1742. break;
  1743. case ')': /* set secondary charset G1 (IGNORED) */
  1744. case '*': /* set tertiary charset G2 (IGNORED) */
  1745. case '+': /* set quaternary charset G3 (IGNORED) */
  1746. term.esc = 0;
  1747. break;
  1748. case 'D': /* IND -- Linefeed */
  1749. if(term.c.y == term.bot) {
  1750. tscrollup(term.top, 1);
  1751. } else {
  1752. tmoveto(term.c.x, term.c.y+1);
  1753. }
  1754. term.esc = 0;
  1755. break;
  1756. case 'E': /* NEL -- Next line */
  1757. tnewline(1); /* always go to first col */
  1758. term.esc = 0;
  1759. break;
  1760. case 'H': /* HTS -- Horizontal tab stop */
  1761. term.tabs[term.c.x] = 1;
  1762. term.esc = 0;
  1763. break;
  1764. case 'M': /* RI -- Reverse index */
  1765. if(term.c.y == term.top) {
  1766. tscrolldown(term.top, 1);
  1767. } else {
  1768. tmoveto(term.c.x, term.c.y-1);
  1769. }
  1770. term.esc = 0;
  1771. break;
  1772. case 'Z': /* DECID -- Identify Terminal */
  1773. ttywrite(VT102ID, sizeof(VT102ID) - 1);
  1774. term.esc = 0;
  1775. break;
  1776. case 'c': /* RIS -- Reset to inital state */
  1777. treset();
  1778. term.esc = 0;
  1779. xresettitle();
  1780. break;
  1781. case '=': /* DECPAM -- Application keypad */
  1782. term.mode |= MODE_APPKEYPAD;
  1783. term.esc = 0;
  1784. break;
  1785. case '>': /* DECPNM -- Normal keypad */
  1786. term.mode &= ~MODE_APPKEYPAD;
  1787. term.esc = 0;
  1788. break;
  1789. case '7': /* DECSC -- Save Cursor */
  1790. tcursor(CURSOR_SAVE);
  1791. term.esc = 0;
  1792. break;
  1793. case '8': /* DECRC -- Restore Cursor */
  1794. tcursor(CURSOR_LOAD);
  1795. term.esc = 0;
  1796. break;
  1797. case '\\': /* ST -- Stop */
  1798. term.esc = 0;
  1799. break;
  1800. default:
  1801. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  1802. (uchar) ascii, isprint(ascii)? ascii:'.');
  1803. term.esc = 0;
  1804. }
  1805. }
  1806. /*
  1807. * All characters which forms part of a sequence are not
  1808. * printed
  1809. */
  1810. return;
  1811. }
  1812. /*
  1813. * Display control codes only if we are in graphic mode
  1814. */
  1815. if(control && !(term.c.attr.mode & ATTR_GFX))
  1816. return;
  1817. if(sel.bx != -1 && BETWEEN(term.c.y, sel.by, sel.ey))
  1818. sel.bx = -1;
  1819. if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
  1820. tnewline(1); /* always go to first col */
  1821. tsetchar(c, &term.c.attr, term.c.x, term.c.y);
  1822. if(term.c.x+1 < term.col)
  1823. tmoveto(term.c.x+1, term.c.y);
  1824. else
  1825. term.c.state |= CURSOR_WRAPNEXT;
  1826. }
  1827. int
  1828. tresize(int col, int row) {
  1829. int i, x;
  1830. int minrow = MIN(row, term.row);
  1831. int mincol = MIN(col, term.col);
  1832. int slide = term.c.y - row + 1;
  1833. bool *bp;
  1834. if(col < 1 || row < 1)
  1835. return 0;
  1836. /* free unneeded rows */
  1837. i = 0;
  1838. if(slide > 0) {
  1839. /* slide screen to keep cursor where we expect it -
  1840. * tscrollup would work here, but we can optimize to
  1841. * memmove because we're freeing the earlier lines */
  1842. for(/* i = 0 */; i < slide; i++) {
  1843. free(term.line[i]);
  1844. free(term.alt[i]);
  1845. }
  1846. memmove(term.line, term.line + slide, row * sizeof(Line));
  1847. memmove(term.alt, term.alt + slide, row * sizeof(Line));
  1848. }
  1849. for(i += row; i < term.row; i++) {
  1850. free(term.line[i]);
  1851. free(term.alt[i]);
  1852. }
  1853. /* resize to new height */
  1854. term.line = xrealloc(term.line, row * sizeof(Line));
  1855. term.alt = xrealloc(term.alt, row * sizeof(Line));
  1856. term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
  1857. term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
  1858. /* resize each row to new width, zero-pad if needed */
  1859. for(i = 0; i < minrow; i++) {
  1860. term.dirty[i] = 1;
  1861. term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
  1862. term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
  1863. for(x = mincol; x < col; x++) {
  1864. term.line[i][x].state = 0;
  1865. term.alt[i][x].state = 0;
  1866. }
  1867. }
  1868. /* allocate any new rows */
  1869. for(/* i == minrow */; i < row; i++) {
  1870. term.dirty[i] = 1;
  1871. term.line[i] = xcalloc(col, sizeof(Glyph));
  1872. term.alt [i] = xcalloc(col, sizeof(Glyph));
  1873. }
  1874. if(col > term.col) {
  1875. bp = term.tabs + term.col;
  1876. memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
  1877. while(--bp > term.tabs && !*bp)
  1878. /* nothing */ ;
  1879. for(bp += TAB; bp < term.tabs + col; bp += TAB)
  1880. *bp = 1;
  1881. }
  1882. /* update terminal size */
  1883. term.col = col, term.row = row;
  1884. /* make use of the LIMIT in tmoveto */
  1885. tmoveto(term.c.x, term.c.y);
  1886. /* reset scrolling region */
  1887. tsetscroll(0, row-1);
  1888. return (slide > 0);
  1889. }
  1890. void
  1891. xresize(int col, int row) {
  1892. xw.tw = MAX(1, 2*BORDER + col * xw.cw);
  1893. xw.th = MAX(1, 2*BORDER + row * xw.ch);
  1894. XftDrawChange(xw.xft_draw, xw.buf);
  1895. }
  1896. void
  1897. xloadcols(void) {
  1898. int i, r, g, b;
  1899. XRenderColor xft_color = { .alpha = 0 };
  1900. /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
  1901. for(i = 0; i < LEN(colorname); i++) {
  1902. if(!colorname[i])
  1903. continue;
  1904. if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, colorname[i], &dc.xft_col[i])) {
  1905. die("Could not allocate color '%s'\n", colorname[i]);
  1906. }
  1907. }
  1908. /* load colors [16-255] ; same colors as xterm */
  1909. for(i = 16, r = 0; r < 6; r++) {
  1910. for(g = 0; g < 6; g++) {
  1911. for(b = 0; b < 6; b++) {
  1912. xft_color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
  1913. xft_color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
  1914. xft_color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
  1915. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &xft_color, &dc.xft_col[i])) {
  1916. die("Could not allocate color %d\n", i);
  1917. }
  1918. i++;
  1919. }
  1920. }
  1921. }
  1922. for(r = 0; r < 24; r++, i++) {
  1923. xft_color.red = xft_color.green = xft_color.blue = 0x0808 + 0x0a0a * r;
  1924. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &xft_color,
  1925. &dc.xft_col[i])) {
  1926. die("Could not allocate color %d\n", i);
  1927. }
  1928. }
  1929. }
  1930. void
  1931. xtermclear(int col1, int row1, int col2, int row2) {
  1932. XftDrawRect(xw.xft_draw,
  1933. &dc.xft_col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG],
  1934. BORDER + col1 * xw.cw,
  1935. BORDER + row1 * xw.ch,
  1936. (col2-col1+1) * xw.cw,
  1937. (row2-row1+1) * xw.ch);
  1938. }
  1939. /*
  1940. * Absolute coordinates.
  1941. */
  1942. void
  1943. xclear(int x1, int y1, int x2, int y2) {
  1944. XftDrawRect(xw.xft_draw,
  1945. &dc.xft_col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG],
  1946. x1, y1, x2-x1, y2-y1);
  1947. }
  1948. void
  1949. xhints(void) {
  1950. XClassHint class = {opt_class ? opt_class : TNAME, TNAME};
  1951. XWMHints wm = {.flags = InputHint, .input = 1};
  1952. XSizeHints *sizeh = NULL;
  1953. sizeh = XAllocSizeHints();
  1954. if(xw.isfixed == False) {
  1955. sizeh->flags = PSize | PResizeInc | PBaseSize;
  1956. sizeh->height = xw.h;
  1957. sizeh->width = xw.w;
  1958. sizeh->height_inc = xw.ch;
  1959. sizeh->width_inc = xw.cw;
  1960. sizeh->base_height = 2*BORDER;
  1961. sizeh->base_width = 2*BORDER;
  1962. } else {
  1963. sizeh->flags = PMaxSize | PMinSize;
  1964. sizeh->min_width = sizeh->max_width = xw.fw;
  1965. sizeh->min_height = sizeh->max_height = xw.fh;
  1966. }
  1967. XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm, &class);
  1968. XFree(sizeh);
  1969. }
  1970. void
  1971. xinitfont(Font *f, char *fontstr) {
  1972. FcPattern *pattern, *match;
  1973. FcResult result;
  1974. pattern = FcNameParse((FcChar8 *)fontstr);
  1975. if(!pattern)
  1976. die("st: can't open font %s\n", fontstr);
  1977. match = XftFontMatch(xw.dpy, xw.scr, pattern, &result);
  1978. FcPatternDestroy(pattern);
  1979. if(!match)
  1980. die("st: can't open font %s\n", fontstr);
  1981. if(!(f->xft_set = XftFontOpenPattern(xw.dpy, match))) {
  1982. FcPatternDestroy(match);
  1983. die("st: can't open font %s.\n", fontstr);
  1984. }
  1985. f->ascent = f->xft_set->ascent;
  1986. f->descent = f->xft_set->descent;
  1987. f->lbearing = 0;
  1988. f->rbearing = f->xft_set->max_advance_width;
  1989. f->height = f->xft_set->height;
  1990. f->width = f->lbearing + f->rbearing;
  1991. }
  1992. void
  1993. initfonts(char *fontstr) {
  1994. char *fstr;
  1995. xinitfont(&dc.font, fontstr);
  1996. xw.cw = dc.font.width;
  1997. xw.ch = dc.font.height;
  1998. fstr = smstrcat(fontstr, ":weight=bold", NULL);
  1999. xinitfont(&dc.bfont, fstr);
  2000. free(fstr);
  2001. fstr = smstrcat(fontstr, ":slant=italic,oblique", NULL);
  2002. xinitfont(&dc.ifont, fstr);
  2003. free(fstr);
  2004. fstr = smstrcat(fontstr, ":weight=bold:slant=italic,oblique", NULL);
  2005. xinitfont(&dc.ibfont, fstr);
  2006. free(fstr);
  2007. }
  2008. void
  2009. xinit(void) {
  2010. XSetWindowAttributes attrs;
  2011. Cursor cursor;
  2012. Window parent;
  2013. int sw, sh, major, minor;
  2014. if(!(xw.dpy = XOpenDisplay(NULL)))
  2015. die("Can't open display\n");
  2016. xw.scr = XDefaultScreen(xw.dpy);
  2017. xw.vis = XDefaultVisual(xw.dpy, xw.scr);
  2018. /* font */
  2019. initfonts((opt_font != NULL)? opt_font : FONT);
  2020. /* colors */
  2021. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  2022. xloadcols();
  2023. /* adjust fixed window geometry */
  2024. if(xw.isfixed) {
  2025. sw = DisplayWidth(xw.dpy, xw.scr);
  2026. sh = DisplayHeight(xw.dpy, xw.scr);
  2027. if(xw.fx < 0)
  2028. xw.fx = sw + xw.fx - xw.fw - 1;
  2029. if(xw.fy < 0)
  2030. xw.fy = sh + xw.fy - xw.fh - 1;
  2031. xw.h = xw.fh;
  2032. xw.w = xw.fw;
  2033. } else {
  2034. /* window - default size */
  2035. xw.h = 2*BORDER + term.row * xw.ch;
  2036. xw.w = 2*BORDER + term.col * xw.cw;
  2037. xw.fx = 0;
  2038. xw.fy = 0;
  2039. }
  2040. attrs.background_pixel = dc.xft_col[DefaultBG].pixel;
  2041. attrs.border_pixel = dc.xft_col[DefaultBG].pixel;
  2042. attrs.bit_gravity = NorthWestGravity;
  2043. attrs.event_mask = FocusChangeMask | KeyPressMask
  2044. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  2045. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
  2046. attrs.colormap = xw.cmap;
  2047. parent = opt_embed ? strtol(opt_embed, NULL, 0) : XRootWindow(xw.dpy, xw.scr);
  2048. xw.win = XCreateWindow(xw.dpy, parent, xw.fx, xw.fy,
  2049. xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
  2050. xw.vis,
  2051. CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
  2052. | CWColormap,
  2053. &attrs);
  2054. /* double buffering */
  2055. if(!XdbeQueryExtension(xw.dpy, &major, &minor))
  2056. die("Xdbe extension is not present\n");
  2057. xw.buf = XdbeAllocateBackBufferName(xw.dpy, xw.win, XdbeCopied);
  2058. /* Xft rendering context */
  2059. xw.xft_draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
  2060. /* input methods */
  2061. xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
  2062. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  2063. | XIMStatusNothing, XNClientWindow, xw.win,
  2064. XNFocusWindow, xw.win, NULL);
  2065. /* gc */
  2066. dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
  2067. /* white cursor, black outline */
  2068. cursor = XCreateFontCursor(xw.dpy, XC_xterm);
  2069. XDefineCursor(xw.dpy, xw.win, cursor);
  2070. XRecolorCursor(xw.dpy, cursor,
  2071. &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
  2072. &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
  2073. xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
  2074. xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
  2075. XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
  2076. xresettitle();
  2077. XMapWindow(xw.dpy, xw.win);
  2078. xhints();
  2079. XSync(xw.dpy, 0);
  2080. }
  2081. void
  2082. xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
  2083. int winx = BORDER + x * xw.cw, winy = BORDER + y * xw.ch,
  2084. width = charlen * xw.cw;
  2085. Font *font = &dc.font;
  2086. XGlyphInfo extents;
  2087. XftColor *fg = &dc.xft_col[base.fg], *bg = &dc.xft_col[base.bg],
  2088. *temp, revfg, revbg;
  2089. XRenderColor colfg, colbg;
  2090. if(base.mode & ATTR_REVERSE)
  2091. temp = fg, fg = bg, bg = temp;
  2092. if(base.mode & ATTR_BOLD) {
  2093. if(BETWEEN(base.fg, 0, 7)) {
  2094. /* basic system colors */
  2095. fg = &dc.xft_col[base.fg + 8];
  2096. } else if(BETWEEN(base.fg, 16, 195)) {
  2097. /* 256 colors */
  2098. fg = &dc.xft_col[base.fg + 36];
  2099. } else if(BETWEEN(base.fg, 232, 251)) {
  2100. /* greyscale */
  2101. fg = &dc.xft_col[base.fg + 4];
  2102. }
  2103. /*
  2104. * Those ranges will not be brightened:
  2105. * 8 - 15 bright system colors
  2106. * 196 - 231 highest 256 color cube
  2107. * 252 - 255 brightest colors in greyscale
  2108. */
  2109. font = &dc.bfont;
  2110. }
  2111. if(base.mode & ATTR_ITALIC)
  2112. font = &dc.ifont;
  2113. if(base.mode & (ATTR_ITALIC|ATTR_ITALIC))
  2114. font = &dc.ibfont;
  2115. if(IS_SET(MODE_REVERSE)) {
  2116. if(fg == &dc.xft_col[DefaultFG]) {
  2117. fg = &dc.xft_col[DefaultBG];
  2118. } else {
  2119. colfg.red = ~fg->color.red;
  2120. colfg.green = ~fg->color.green;
  2121. colfg.blue = ~fg->color.blue;
  2122. colfg.alpha = fg->color.alpha;
  2123. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
  2124. fg = &revfg;
  2125. }
  2126. if(bg == &dc.xft_col[DefaultBG]) {
  2127. bg = &dc.xft_col[DefaultFG];
  2128. } else {
  2129. colbg.red = ~bg->color.red;
  2130. colbg.green = ~bg->color.green;
  2131. colbg.blue = ~bg->color.blue;
  2132. colbg.alpha = bg->color.alpha;
  2133. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &revbg);
  2134. bg = &revbg;
  2135. }
  2136. }
  2137. XftTextExtentsUtf8(xw.dpy, font->xft_set, (FcChar8 *)s, bytelen,
  2138. &extents);
  2139. width = extents.xOff;
  2140. /* Intelligent cleaning up of the borders. */
  2141. if(x == 0) {
  2142. xclear(0, (y == 0)? 0 : winy, BORDER,
  2143. winy + xw.ch + (y == term.row-1)? xw.h : 0);
  2144. }
  2145. if(x + charlen >= term.col-1) {
  2146. xclear(winx + width, (y == 0)? 0 : winy, xw.w,
  2147. (y == term.row-1)? xw.h : (winy + xw.ch));
  2148. }
  2149. if(y == 0)
  2150. xclear(winx, 0, winx + width, BORDER);
  2151. if(y == term.row-1)
  2152. xclear(winx, winy + xw.ch, winx + width, xw.h);
  2153. XftDrawRect(xw.xft_draw, bg, winx, winy, width, xw.ch);
  2154. XftDrawStringUtf8(xw.xft_draw, fg, font->xft_set, winx,
  2155. winy + font->ascent, (FcChar8 *)s, bytelen);
  2156. if(base.mode & ATTR_UNDERLINE) {
  2157. XftDrawRect(xw.xft_draw, fg, winx, winy + font->ascent + 1,
  2158. width, 1);
  2159. }
  2160. }
  2161. void
  2162. xdrawcursor(void) {
  2163. static int oldx = 0, oldy = 0;
  2164. int sl;
  2165. Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
  2166. LIMIT(oldx, 0, term.col-1);
  2167. LIMIT(oldy, 0, term.row-1);
  2168. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  2169. memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
  2170. /* remove the old cursor */
  2171. if(term.line[oldy][oldx].state & GLYPH_SET) {
  2172. sl = utf8size(term.line[oldy][oldx].c);
  2173. xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx,
  2174. oldy, 1, sl);
  2175. } else {
  2176. xtermclear(oldx, oldy, oldx, oldy);
  2177. }
  2178. /* draw the new one */
  2179. if(!(term.c.state & CURSOR_HIDE)) {
  2180. if(!(xw.state & WIN_FOCUSED))
  2181. g.bg = DefaultUCS;
  2182. if(IS_SET(MODE_REVERSE))
  2183. g.mode |= ATTR_REVERSE, g.fg = DefaultCS, g.bg = DefaultFG;
  2184. sl = utf8size(g.c);
  2185. xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
  2186. oldx = term.c.x, oldy = term.c.y;
  2187. }
  2188. }
  2189. void
  2190. xresettitle(void) {
  2191. XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
  2192. }
  2193. void
  2194. redraw(void) {
  2195. struct timespec tv = {0, REDRAW_TIMEOUT * 1000};
  2196. tfulldirt();
  2197. draw();
  2198. XSync(xw.dpy, False); /* necessary for a good tput flash */
  2199. nanosleep(&tv, NULL);
  2200. }
  2201. void
  2202. draw(void) {
  2203. XdbeSwapInfo swpinfo[1] = {{xw.win, XdbeCopied}};
  2204. drawregion(0, 0, term.col, term.row);
  2205. XdbeSwapBuffers(xw.dpy, swpinfo, 1);
  2206. }
  2207. void
  2208. drawregion(int x1, int y1, int x2, int y2) {
  2209. int ic, ib, x, y, ox, sl;
  2210. Glyph base, new;
  2211. char buf[DRAW_BUF_SIZ];
  2212. bool ena_sel = sel.bx != -1, alt = IS_SET(MODE_ALTSCREEN);
  2213. if((sel.alt && !alt) || (!sel.alt && alt))
  2214. ena_sel = 0;
  2215. if(!(xw.state & WIN_VISIBLE))
  2216. return;
  2217. for(y = y1; y < y2; y++) {
  2218. if(!term.dirty[y])
  2219. continue;
  2220. xtermclear(0, y, term.col, y);
  2221. term.dirty[y] = 0;
  2222. base = term.line[y][0];
  2223. ic = ib = ox = 0;
  2224. for(x = x1; x < x2; x++) {
  2225. new = term.line[y][x];
  2226. if(ena_sel && *(new.c) && selected(x, y))
  2227. new.mode ^= ATTR_REVERSE;
  2228. if(ib > 0 && (!(new.state & GLYPH_SET)
  2229. || ATTRCMP(base, new)
  2230. || ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
  2231. xdraws(buf, base, ox, y, ic, ib);
  2232. ic = ib = 0;
  2233. }
  2234. if(new.state & GLYPH_SET) {
  2235. if(ib == 0) {
  2236. ox = x;
  2237. base = new;
  2238. }
  2239. sl = utf8size(new.c);
  2240. memcpy(buf+ib, new.c, sl);
  2241. ib += sl;
  2242. ++ic;
  2243. }
  2244. }
  2245. if(ib > 0)
  2246. xdraws(buf, base, ox, y, ic, ib);
  2247. }
  2248. xdrawcursor();
  2249. }
  2250. void
  2251. expose(XEvent *ev) {
  2252. XExposeEvent *e = &ev->xexpose;
  2253. if(xw.state & WIN_REDRAW) {
  2254. if(!e->count)
  2255. xw.state &= ~WIN_REDRAW;
  2256. }
  2257. }
  2258. void
  2259. visibility(XEvent *ev) {
  2260. XVisibilityEvent *e = &ev->xvisibility;
  2261. if(e->state == VisibilityFullyObscured) {
  2262. xw.state &= ~WIN_VISIBLE;
  2263. } else if(!(xw.state & WIN_VISIBLE)) {
  2264. /* need a full redraw for next Expose, not just a buf copy */
  2265. xw.state |= WIN_VISIBLE | WIN_REDRAW;
  2266. }
  2267. }
  2268. void
  2269. unmap(XEvent *ev) {
  2270. xw.state &= ~WIN_VISIBLE;
  2271. }
  2272. void
  2273. xseturgency(int add) {
  2274. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  2275. h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
  2276. XSetWMHints(xw.dpy, xw.win, h);
  2277. XFree(h);
  2278. }
  2279. void
  2280. focus(XEvent *ev) {
  2281. if(ev->type == FocusIn) {
  2282. xw.state |= WIN_FOCUSED;
  2283. xseturgency(0);
  2284. } else {
  2285. xw.state &= ~WIN_FOCUSED;
  2286. }
  2287. }
  2288. char*
  2289. kmap(KeySym k, uint state) {
  2290. int i;
  2291. uint mask;
  2292. state &= ~Mod2Mask;
  2293. for(i = 0; i < LEN(key); i++) {
  2294. mask = key[i].mask;
  2295. if(key[i].k == k && ((state & mask) == mask
  2296. || (mask == XK_NO_MOD && !state))) {
  2297. return (char*)key[i].s;
  2298. }
  2299. }
  2300. return NULL;
  2301. }
  2302. void
  2303. kpress(XEvent *ev) {
  2304. XKeyEvent *e = &ev->xkey;
  2305. KeySym ksym;
  2306. char buf[32];
  2307. char *customkey;
  2308. int len;
  2309. int meta;
  2310. int shift;
  2311. Status status;
  2312. if (IS_SET(MODE_KBDLOCK))
  2313. return;
  2314. meta = e->state & Mod1Mask;
  2315. shift = e->state & ShiftMask;
  2316. len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
  2317. /* 1. custom keys from config.h */
  2318. if((customkey = kmap(ksym, e->state))) {
  2319. ttywrite(customkey, strlen(customkey));
  2320. /* 2. hardcoded (overrides X lookup) */
  2321. } else {
  2322. switch(ksym) {
  2323. case XK_Up:
  2324. case XK_Down:
  2325. case XK_Left:
  2326. case XK_Right:
  2327. /* XXX: shift up/down doesn't work */
  2328. sprintf(buf, "\033%c%c",
  2329. IS_SET(MODE_APPKEYPAD) ? 'O' : '[',
  2330. (shift ? "dacb":"DACB")[ksym - XK_Left]);
  2331. ttywrite(buf, 3);
  2332. break;
  2333. case XK_Insert:
  2334. if(shift)
  2335. selpaste();
  2336. break;
  2337. case XK_Return:
  2338. if(IS_SET(MODE_CRLF)) {
  2339. ttywrite("\r\n", 2);
  2340. } else {
  2341. ttywrite("\r", 1);
  2342. }
  2343. break;
  2344. /* 3. X lookup */
  2345. default:
  2346. if(len > 0) {
  2347. if(meta && len == 1)
  2348. ttywrite("\033", 1);
  2349. ttywrite(buf, len);
  2350. }
  2351. break;
  2352. }
  2353. }
  2354. }
  2355. void
  2356. cmessage(XEvent *e) {
  2357. /* See xembed specs
  2358. http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
  2359. if(e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
  2360. if(e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
  2361. xw.state |= WIN_FOCUSED;
  2362. xseturgency(0);
  2363. } else if(e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
  2364. xw.state &= ~WIN_FOCUSED;
  2365. }
  2366. } else if(e->xclient.data.l[0] == xw.wmdeletewin) {
  2367. /* Send SIGHUP to shell */
  2368. kill(pid, SIGHUP);
  2369. exit(EXIT_SUCCESS);
  2370. }
  2371. }
  2372. void
  2373. resize(XEvent *e) {
  2374. int col, row;
  2375. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  2376. return;
  2377. xw.w = e->xconfigure.width;
  2378. xw.h = e->xconfigure.height;
  2379. col = (xw.w - 2*BORDER) / xw.cw;
  2380. row = (xw.h - 2*BORDER) / xw.ch;
  2381. if(col == term.col && row == term.row)
  2382. return;
  2383. tresize(col, row);
  2384. xresize(col, row);
  2385. ttyresize();
  2386. }
  2387. void
  2388. run(void) {
  2389. XEvent ev;
  2390. fd_set rfd;
  2391. int xfd = XConnectionNumber(xw.dpy), i;
  2392. struct timeval drawtimeout, *tv = NULL;
  2393. for(i = 0;; i++) {
  2394. FD_ZERO(&rfd);
  2395. FD_SET(cmdfd, &rfd);
  2396. FD_SET(xfd, &rfd);
  2397. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv) < 0) {
  2398. if(errno == EINTR)
  2399. continue;
  2400. die("select failed: %s\n", SERRNO);
  2401. }
  2402. /*
  2403. * Stop after a certain number of reads so the user does not
  2404. * feel like the system is stuttering.
  2405. */
  2406. if(i < 1000 && FD_ISSET(cmdfd, &rfd)) {
  2407. ttyread();
  2408. /*
  2409. * Just wait a bit so it isn't disturbing the
  2410. * user and the system is able to write something.
  2411. */
  2412. drawtimeout.tv_sec = 0;
  2413. drawtimeout.tv_usec = 5;
  2414. tv = &drawtimeout;
  2415. continue;
  2416. }
  2417. i = 0;
  2418. tv = NULL;
  2419. while(XPending(xw.dpy)) {
  2420. XNextEvent(xw.dpy, &ev);
  2421. if(XFilterEvent(&ev, xw.win))
  2422. continue;
  2423. if(handler[ev.type])
  2424. (handler[ev.type])(&ev);
  2425. }
  2426. draw();
  2427. XFlush(xw.dpy);
  2428. }
  2429. }
  2430. int
  2431. main(int argc, char *argv[]) {
  2432. int i, bitm, xr, yr;
  2433. uint wr, hr;
  2434. xw.fw = xw.fh = xw.fx = xw.fy = 0;
  2435. xw.isfixed = False;
  2436. for(i = 1; i < argc; i++) {
  2437. switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
  2438. case 'c':
  2439. if(++i < argc)
  2440. opt_class = argv[i];
  2441. break;
  2442. case 'e':
  2443. /* eat every remaining arguments */
  2444. if(++i < argc)
  2445. opt_cmd = &argv[i];
  2446. goto run;
  2447. case 'f':
  2448. if(++i < argc)
  2449. opt_font = argv[i];
  2450. break;
  2451. case 'g':
  2452. if(++i >= argc)
  2453. break;
  2454. bitm = XParseGeometry(argv[i], &xr, &yr, &wr, &hr);
  2455. if(bitm & XValue)
  2456. xw.fx = xr;
  2457. if(bitm & YValue)
  2458. xw.fy = yr;
  2459. if(bitm & WidthValue)
  2460. xw.fw = (int)wr;
  2461. if(bitm & HeightValue)
  2462. xw.fh = (int)hr;
  2463. if(bitm & XNegative && xw.fx == 0)
  2464. xw.fx = -1;
  2465. if(bitm & XNegative && xw.fy == 0)
  2466. xw.fy = -1;
  2467. if(xw.fh != 0 && xw.fw != 0)
  2468. xw.isfixed = True;
  2469. break;
  2470. case 'o':
  2471. if(++i < argc)
  2472. opt_io = argv[i];
  2473. break;
  2474. case 't':
  2475. if(++i < argc)
  2476. opt_title = argv[i];
  2477. break;
  2478. case 'v':
  2479. default:
  2480. die(USAGE);
  2481. case 'w':
  2482. if(++i < argc)
  2483. opt_embed = argv[i];
  2484. break;
  2485. }
  2486. }
  2487. run:
  2488. setlocale(LC_CTYPE, "");
  2489. tnew(80, 24);
  2490. ttynew();
  2491. xinit();
  2492. selinit();
  2493. run();
  2494. return 0;
  2495. }