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.

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