Simple Terminal from SuckLess
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2727 lines
59 KiB

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