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.

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