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.

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