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.

2737 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': /* HT */
  1590. tputtab(1);
  1591. return;
  1592. case '\b': /* BS */
  1593. tmoveto(term.c.x-1, term.c.y);
  1594. return;
  1595. case '\r': /* CR */
  1596. tmoveto(0, term.c.y);
  1597. return;
  1598. case '\f': /* LF */
  1599. case '\v': /* VT */
  1600. case '\n': /* LF */
  1601. /* go to first col if the mode is set */
  1602. tnewline(IS_SET(MODE_CRLF));
  1603. return;
  1604. case '\a': /* BEL */
  1605. if(term.esc & ESC_STR)
  1606. break;
  1607. if(!(xw.state & WIN_FOCUSED))
  1608. xseturgency(1);
  1609. return;
  1610. case '\033': /* ESC */
  1611. csireset();
  1612. term.esc = ESC_START;
  1613. return;
  1614. case '\016': /* XXX: SO */
  1615. case '\017': /* XXX: SI */
  1616. case '\032': /* XXX: SUB */
  1617. case '\030': /* XXX: CAN */
  1618. default:
  1619. /* case '\005': ENQ (IGNORED) */
  1620. /* case '\000': NUL (IGNORED) */
  1621. /* case '\021': XON (IGNORED) */
  1622. /* case '\023': XOFF (IGNORED) */
  1623. /* case 0177: DEL (IGNORED) */
  1624. break;
  1625. }
  1626. if(term.esc & ESC_START) {
  1627. if(term.esc & ESC_CSI) {
  1628. csiescseq.buf[csiescseq.len++] = ascii;
  1629. if(BETWEEN(ascii, 0x40, 0x7E)
  1630. || csiescseq.len >= ESC_BUF_SIZ) {
  1631. term.esc = 0;
  1632. csiparse(), csihandle();
  1633. }
  1634. } else if(term.esc & ESC_STR) {
  1635. switch(ascii) {
  1636. case '\033':
  1637. term.esc = ESC_START | ESC_STR_END;
  1638. break;
  1639. case '\a': /* backwards compatibility to xterm */
  1640. term.esc = 0;
  1641. strhandle();
  1642. break;
  1643. default:
  1644. strescseq.buf[strescseq.len++] = ascii;
  1645. if(strescseq.len+1 >= STR_BUF_SIZ) {
  1646. term.esc = 0;
  1647. strhandle();
  1648. }
  1649. }
  1650. } else if(term.esc & ESC_STR_END) {
  1651. term.esc = 0;
  1652. if(ascii == '\\')
  1653. strhandle();
  1654. } else if(term.esc & ESC_ALTCHARSET) {
  1655. switch(ascii) {
  1656. case '0': /* Line drawing set */
  1657. term.c.attr.mode |= ATTR_GFX;
  1658. break;
  1659. case 'B': /* USASCII */
  1660. term.c.attr.mode &= ~ATTR_GFX;
  1661. break;
  1662. case 'A': /* UK (IGNORED) */
  1663. case '<': /* multinational charset (IGNORED) */
  1664. case '5': /* Finnish (IGNORED) */
  1665. case 'C': /* Finnish (IGNORED) */
  1666. case 'K': /* German (IGNORED) */
  1667. break;
  1668. default:
  1669. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  1670. }
  1671. term.esc = 0;
  1672. } else {
  1673. switch(ascii) {
  1674. case '[':
  1675. term.esc |= ESC_CSI;
  1676. break;
  1677. case 'P': /* DCS -- Device Control String */
  1678. case '_': /* APC -- Application Program Command */
  1679. case '^': /* PM -- Privacy Message */
  1680. case ']': /* OSC -- Operating System Command */
  1681. case 'k': /* old title set compatibility */
  1682. strreset();
  1683. strescseq.type = ascii;
  1684. term.esc |= ESC_STR;
  1685. break;
  1686. case '(': /* set primary charset G0 */
  1687. term.esc |= ESC_ALTCHARSET;
  1688. break;
  1689. case ')': /* set secondary charset G1 (IGNORED) */
  1690. case '*': /* set tertiary charset G2 (IGNORED) */
  1691. case '+': /* set quaternary charset G3 (IGNORED) */
  1692. term.esc = 0;
  1693. break;
  1694. case 'D': /* IND -- Linefeed */
  1695. if(term.c.y == term.bot) {
  1696. tscrollup(term.top, 1);
  1697. } else {
  1698. tmoveto(term.c.x, term.c.y+1);
  1699. }
  1700. term.esc = 0;
  1701. break;
  1702. case 'E': /* NEL -- Next line */
  1703. tnewline(1); /* always go to first col */
  1704. term.esc = 0;
  1705. break;
  1706. case 'H': /* HTS -- Horizontal tab stop */
  1707. term.tabs[term.c.x] = 1;
  1708. term.esc = 0;
  1709. break;
  1710. case 'M': /* RI -- Reverse index */
  1711. if(term.c.y == term.top) {
  1712. tscrolldown(term.top, 1);
  1713. } else {
  1714. tmoveto(term.c.x, term.c.y-1);
  1715. }
  1716. term.esc = 0;
  1717. break;
  1718. case 'c': /* RIS -- Reset to inital state */
  1719. treset();
  1720. term.esc = 0;
  1721. xresettitle();
  1722. break;
  1723. case '=': /* DECPAM -- Application keypad */
  1724. term.mode |= MODE_APPKEYPAD;
  1725. term.esc = 0;
  1726. break;
  1727. case '>': /* DECPNM -- Normal keypad */
  1728. term.mode &= ~MODE_APPKEYPAD;
  1729. term.esc = 0;
  1730. break;
  1731. case '7': /* DECSC -- Save Cursor */
  1732. tcursor(CURSOR_SAVE);
  1733. term.esc = 0;
  1734. break;
  1735. case '8': /* DECRC -- Restore Cursor */
  1736. tcursor(CURSOR_LOAD);
  1737. term.esc = 0;
  1738. break;
  1739. case '\\': /* ST -- Stop */
  1740. term.esc = 0;
  1741. break;
  1742. default:
  1743. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  1744. (uchar) ascii, isprint(ascii)? ascii:'.');
  1745. term.esc = 0;
  1746. }
  1747. }
  1748. } else {
  1749. if(sel.bx != -1 && BETWEEN(term.c.y, sel.by, sel.ey))
  1750. sel.bx = -1;
  1751. if(ascii >= '\020' || term.c.attr.mode & ATTR_GFX) {
  1752. if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
  1753. tnewline(1); /* always go to first col */
  1754. tsetchar(c);
  1755. if(term.c.x+1 < term.col) {
  1756. tmoveto(term.c.x+1, term.c.y);
  1757. } else {
  1758. term.c.state |= CURSOR_WRAPNEXT;
  1759. }
  1760. }
  1761. }
  1762. }
  1763. int
  1764. tresize(int col, int row) {
  1765. int i, x;
  1766. int minrow = MIN(row, term.row);
  1767. int mincol = MIN(col, term.col);
  1768. int slide = term.c.y - row + 1;
  1769. bool *bp;
  1770. if(col < 1 || row < 1)
  1771. return 0;
  1772. /* free unneeded rows */
  1773. i = 0;
  1774. if(slide > 0) {
  1775. /* slide screen to keep cursor where we expect it -
  1776. * tscrollup would work here, but we can optimize to
  1777. * memmove because we're freeing the earlier lines */
  1778. for(/* i = 0 */; i < slide; i++) {
  1779. free(term.line[i]);
  1780. free(term.alt[i]);
  1781. }
  1782. memmove(term.line, term.line + slide, row * sizeof(Line));
  1783. memmove(term.alt, term.alt + slide, row * sizeof(Line));
  1784. }
  1785. for(i += row; i < term.row; i++) {
  1786. free(term.line[i]);
  1787. free(term.alt[i]);
  1788. }
  1789. /* resize to new height */
  1790. term.line = xrealloc(term.line, row * sizeof(Line));
  1791. term.alt = xrealloc(term.alt, row * sizeof(Line));
  1792. term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
  1793. term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
  1794. /* resize each row to new width, zero-pad if needed */
  1795. for(i = 0; i < minrow; i++) {
  1796. term.dirty[i] = 1;
  1797. term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
  1798. term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
  1799. for(x = mincol; x < col; x++) {
  1800. term.line[i][x].state = 0;
  1801. term.alt[i][x].state = 0;
  1802. }
  1803. }
  1804. /* allocate any new rows */
  1805. for(/* i == minrow */; i < row; i++) {
  1806. term.dirty[i] = 1;
  1807. term.line[i] = xcalloc(col, sizeof(Glyph));
  1808. term.alt [i] = xcalloc(col, sizeof(Glyph));
  1809. }
  1810. if(col > term.col) {
  1811. bp = term.tabs + term.col;
  1812. memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
  1813. while(--bp > term.tabs && !*bp)
  1814. /* nothing */ ;
  1815. for(bp += TAB; bp < term.tabs + col; bp += TAB)
  1816. *bp = 1;
  1817. }
  1818. /* update terminal size */
  1819. term.col = col, term.row = row;
  1820. /* make use of the LIMIT in tmoveto */
  1821. tmoveto(term.c.x, term.c.y);
  1822. /* reset scrolling region */
  1823. tsetscroll(0, row-1);
  1824. return (slide > 0);
  1825. }
  1826. void
  1827. xresize(int col, int row) {
  1828. xw.tw = MAX(1, 2*BORDER + col * xw.cw);
  1829. xw.th = MAX(1, 2*BORDER + row * xw.ch);
  1830. XftDrawChange(xw.xft_draw, xw.buf);
  1831. }
  1832. void
  1833. xloadcols(void) {
  1834. int i, r, g, b;
  1835. XRenderColor xft_color = { .alpha = 0 };
  1836. /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
  1837. for(i = 0; i < LEN(colorname); i++) {
  1838. if(!colorname[i])
  1839. continue;
  1840. if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, colorname[i], &dc.xft_col[i])) {
  1841. die("Could not allocate color '%s'\n", colorname[i]);
  1842. }
  1843. }
  1844. /* load colors [16-255] ; same colors as xterm */
  1845. for(i = 16, r = 0; r < 6; r++) {
  1846. for(g = 0; g < 6; g++) {
  1847. for(b = 0; b < 6; b++) {
  1848. xft_color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
  1849. xft_color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
  1850. xft_color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
  1851. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &xft_color, &dc.xft_col[i])) {
  1852. die("Could not allocate color %d\n", i);
  1853. }
  1854. i++;
  1855. }
  1856. }
  1857. }
  1858. for(r = 0; r < 24; r++, i++) {
  1859. xft_color.red = xft_color.green = xft_color.blue = 0x0808 + 0x0a0a * r;
  1860. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &xft_color,
  1861. &dc.xft_col[i])) {
  1862. die("Could not allocate color %d\n", i);
  1863. }
  1864. }
  1865. }
  1866. void
  1867. xtermclear(int col1, int row1, int col2, int row2) {
  1868. XftDrawRect(xw.xft_draw,
  1869. &dc.xft_col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG],
  1870. BORDER + col1 * xw.cw,
  1871. BORDER + row1 * xw.ch,
  1872. (col2-col1+1) * xw.cw,
  1873. (row2-row1+1) * xw.ch);
  1874. }
  1875. /*
  1876. * Absolute coordinates.
  1877. */
  1878. void
  1879. xclear(int x1, int y1, int x2, int y2) {
  1880. XftDrawRect(xw.xft_draw,
  1881. &dc.xft_col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG],
  1882. x1, y1, x2-x1, y2-y1);
  1883. }
  1884. void
  1885. xhints(void) {
  1886. XClassHint class = {opt_class ? opt_class : TNAME, TNAME};
  1887. XWMHints wm = {.flags = InputHint, .input = 1};
  1888. XSizeHints *sizeh = NULL;
  1889. sizeh = XAllocSizeHints();
  1890. if(xw.isfixed == False) {
  1891. sizeh->flags = PSize | PResizeInc | PBaseSize;
  1892. sizeh->height = xw.h;
  1893. sizeh->width = xw.w;
  1894. sizeh->height_inc = xw.ch;
  1895. sizeh->width_inc = xw.cw;
  1896. sizeh->base_height = 2*BORDER;
  1897. sizeh->base_width = 2*BORDER;
  1898. } else {
  1899. sizeh->flags = PMaxSize | PMinSize;
  1900. sizeh->min_width = sizeh->max_width = xw.fw;
  1901. sizeh->min_height = sizeh->max_height = xw.fh;
  1902. }
  1903. XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm, &class);
  1904. XFree(sizeh);
  1905. }
  1906. void
  1907. xinitfont(Font *f, char *fontstr) {
  1908. FcPattern *pattern, *match;
  1909. FcResult result;
  1910. pattern = FcNameParse((FcChar8 *)fontstr);
  1911. if(!pattern)
  1912. die("st: can't open font %s\n", fontstr);
  1913. match = XftFontMatch(xw.dpy, xw.scr, pattern, &result);
  1914. FcPatternDestroy(pattern);
  1915. if(!match)
  1916. die("st: can't open font %s\n", fontstr);
  1917. if(!(f->xft_set = XftFontOpenPattern(xw.dpy, match))) {
  1918. FcPatternDestroy(match);
  1919. die("st: can't open font %s.\n", fontstr);
  1920. }
  1921. f->ascent = f->xft_set->ascent;
  1922. f->descent = f->xft_set->descent;
  1923. f->lbearing = 0;
  1924. f->rbearing = f->xft_set->max_advance_width;
  1925. f->height = f->xft_set->height;
  1926. f->width = f->lbearing + f->rbearing;
  1927. }
  1928. void
  1929. initfonts(char *fontstr) {
  1930. char *fstr;
  1931. xinitfont(&dc.font, fontstr);
  1932. xw.cw = dc.font.width;
  1933. xw.ch = dc.font.height;
  1934. fstr = smstrcat(fontstr, ":weight=bold", NULL);
  1935. xinitfont(&dc.bfont, fstr);
  1936. free(fstr);
  1937. fstr = smstrcat(fontstr, ":slant=italic,oblique", NULL);
  1938. xinitfont(&dc.ifont, fstr);
  1939. free(fstr);
  1940. fstr = smstrcat(fontstr, ":weight=bold:slant=italic,oblique", NULL);
  1941. xinitfont(&dc.ibfont, fstr);
  1942. free(fstr);
  1943. }
  1944. void
  1945. xinit(void) {
  1946. XSetWindowAttributes attrs;
  1947. Cursor cursor;
  1948. Window parent;
  1949. int sw, sh, major, minor;
  1950. if(!(xw.dpy = XOpenDisplay(NULL)))
  1951. die("Can't open display\n");
  1952. xw.scr = XDefaultScreen(xw.dpy);
  1953. xw.vis = XDefaultVisual(xw.dpy, xw.scr);
  1954. /* font */
  1955. initfonts((opt_font != NULL)? opt_font : FONT);
  1956. /* colors */
  1957. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  1958. xloadcols();
  1959. /* adjust fixed window geometry */
  1960. if(xw.isfixed) {
  1961. sw = DisplayWidth(xw.dpy, xw.scr);
  1962. sh = DisplayHeight(xw.dpy, xw.scr);
  1963. if(xw.fx < 0)
  1964. xw.fx = sw + xw.fx - xw.fw - 1;
  1965. if(xw.fy < 0)
  1966. xw.fy = sh + xw.fy - xw.fh - 1;
  1967. xw.h = xw.fh;
  1968. xw.w = xw.fw;
  1969. } else {
  1970. /* window - default size */
  1971. xw.h = 2*BORDER + term.row * xw.ch;
  1972. xw.w = 2*BORDER + term.col * xw.cw;
  1973. xw.fx = 0;
  1974. xw.fy = 0;
  1975. }
  1976. attrs.background_pixel = dc.xft_col[DefaultBG].pixel;
  1977. attrs.border_pixel = dc.xft_col[DefaultBG].pixel;
  1978. attrs.bit_gravity = NorthWestGravity;
  1979. attrs.event_mask = FocusChangeMask | KeyPressMask
  1980. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  1981. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
  1982. attrs.colormap = xw.cmap;
  1983. parent = opt_embed ? strtol(opt_embed, NULL, 0) : XRootWindow(xw.dpy, xw.scr);
  1984. xw.win = XCreateWindow(xw.dpy, parent, xw.fx, xw.fy,
  1985. xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
  1986. xw.vis,
  1987. CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
  1988. | CWColormap,
  1989. &attrs);
  1990. /* double buffering */
  1991. if(!XdbeQueryExtension(xw.dpy, &major, &minor))
  1992. die("Xdbe extension is not present\n");
  1993. xw.buf = XdbeAllocateBackBufferName(xw.dpy, xw.win, XdbeCopied);
  1994. /* Xft rendering context */
  1995. xw.xft_draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
  1996. /* input methods */
  1997. xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
  1998. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  1999. | XIMStatusNothing, XNClientWindow, xw.win,
  2000. XNFocusWindow, xw.win, NULL);
  2001. /* gc */
  2002. dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
  2003. /* white cursor, black outline */
  2004. cursor = XCreateFontCursor(xw.dpy, XC_xterm);
  2005. XDefineCursor(xw.dpy, xw.win, cursor);
  2006. XRecolorCursor(xw.dpy, cursor,
  2007. &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
  2008. &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
  2009. xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
  2010. xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
  2011. XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
  2012. xresettitle();
  2013. XMapWindow(xw.dpy, xw.win);
  2014. xhints();
  2015. XSync(xw.dpy, 0);
  2016. }
  2017. void
  2018. xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
  2019. int winx = BORDER + x * xw.cw, winy = BORDER + y * xw.ch,
  2020. width = charlen * xw.cw;
  2021. Font *font = &dc.font;
  2022. XGlyphInfo extents;
  2023. XftColor *fg = &dc.xft_col[base.fg], *bg = &dc.xft_col[base.bg],
  2024. *temp, revfg, revbg;
  2025. XRenderColor colfg, colbg;
  2026. if(base.mode & ATTR_REVERSE)
  2027. temp = fg, fg = bg, bg = temp;
  2028. if(base.mode & ATTR_BOLD) {
  2029. if(BETWEEN(base.fg, 0, 7)) {
  2030. /* basic system colors */
  2031. fg = &dc.xft_col[base.fg + 8];
  2032. } else if(BETWEEN(base.fg, 16, 195)) {
  2033. /* 256 colors */
  2034. fg = &dc.xft_col[base.fg + 36];
  2035. } else if(BETWEEN(base.fg, 232, 251)) {
  2036. /* greyscale */
  2037. fg = &dc.xft_col[base.fg + 4];
  2038. }
  2039. /*
  2040. * Those ranges will not be brightened:
  2041. * 8 - 15 bright system colors
  2042. * 196 - 231 highest 256 color cube
  2043. * 252 - 255 brightest colors in grescale
  2044. */
  2045. font = &dc.bfont;
  2046. }
  2047. if(base.mode & ATTR_ITALIC)
  2048. font = &dc.ifont;
  2049. if(base.mode & (ATTR_ITALIC|ATTR_ITALIC))
  2050. font = &dc.ibfont;
  2051. if(IS_SET(MODE_REVERSE)) {
  2052. if(fg == &dc.xft_col[DefaultFG]) {
  2053. fg = &dc.xft_col[DefaultBG];
  2054. } else {
  2055. colfg.red = ~fg->color.red;
  2056. colfg.green = ~fg->color.green;
  2057. colfg.blue = ~fg->color.blue;
  2058. colfg.alpha = fg->color.alpha;
  2059. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
  2060. fg = &revfg;
  2061. }
  2062. if(bg == &dc.xft_col[DefaultBG]) {
  2063. bg = &dc.xft_col[DefaultFG];
  2064. } else {
  2065. colbg.red = ~bg->color.red;
  2066. colbg.green = ~bg->color.green;
  2067. colbg.blue = ~bg->color.blue;
  2068. colbg.alpha = bg->color.alpha;
  2069. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &revbg);
  2070. bg = &revbg;
  2071. }
  2072. }
  2073. XftTextExtentsUtf8(xw.dpy, font->xft_set, (FcChar8 *)s, bytelen,
  2074. &extents);
  2075. width = extents.xOff;
  2076. /* Intelligent cleaning up of the borders. */
  2077. if(x == 0) {
  2078. xclear(0, (y == 0)? 0 : winy, BORDER,
  2079. winy + xw.ch + (y == term.row-1)? xw.h : 0);
  2080. }
  2081. if(x + charlen >= term.col-1) {
  2082. xclear(winx + width, (y == 0)? 0 : winy, xw.w,
  2083. winy + xw.ch + (y == term.row-1)? xw.h : 0);
  2084. }
  2085. if(y == 0)
  2086. xclear(winx, 0, winx + width, BORDER);
  2087. if(y == term.row-1)
  2088. xclear(winx, winy + xw.ch, winx + width, xw.h);
  2089. XftDrawRect(xw.xft_draw, bg, winx, winy, width, xw.ch);
  2090. XftDrawStringUtf8(xw.xft_draw, fg, font->xft_set, winx,
  2091. winy + font->ascent, (FcChar8 *)s, bytelen);
  2092. if(base.mode & ATTR_UNDERLINE) {
  2093. XftDrawRect(xw.xft_draw, fg, winx, winy + font->ascent + 1,
  2094. width, 1);
  2095. }
  2096. }
  2097. void
  2098. xdrawcursor(void) {
  2099. static int oldx = 0, oldy = 0;
  2100. int sl;
  2101. Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
  2102. LIMIT(oldx, 0, term.col-1);
  2103. LIMIT(oldy, 0, term.row-1);
  2104. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  2105. memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
  2106. /* remove the old cursor */
  2107. if(term.line[oldy][oldx].state & GLYPH_SET) {
  2108. sl = utf8size(term.line[oldy][oldx].c);
  2109. xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx,
  2110. oldy, 1, sl);
  2111. } else {
  2112. xtermclear(oldx, oldy, oldx, oldy);
  2113. }
  2114. /* draw the new one */
  2115. if(!(term.c.state & CURSOR_HIDE)) {
  2116. if(!(xw.state & WIN_FOCUSED))
  2117. g.bg = DefaultUCS;
  2118. if(IS_SET(MODE_REVERSE))
  2119. g.mode |= ATTR_REVERSE, g.fg = DefaultCS, g.bg = DefaultFG;
  2120. sl = utf8size(g.c);
  2121. xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
  2122. oldx = term.c.x, oldy = term.c.y;
  2123. }
  2124. }
  2125. void
  2126. xresettitle(void) {
  2127. XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
  2128. }
  2129. void
  2130. redraw(void) {
  2131. struct timespec tv = {0, REDRAW_TIMEOUT * 1000};
  2132. tfulldirt();
  2133. draw();
  2134. XSync(xw.dpy, False); /* necessary for a good tput flash */
  2135. nanosleep(&tv, NULL);
  2136. }
  2137. void
  2138. draw(void) {
  2139. XdbeSwapInfo swpinfo[1] = {{xw.win, XdbeCopied}};
  2140. drawregion(0, 0, term.col, term.row);
  2141. XdbeSwapBuffers(xw.dpy, swpinfo, 1);
  2142. }
  2143. void
  2144. drawregion(int x1, int y1, int x2, int y2) {
  2145. int ic, ib, x, y, ox, sl;
  2146. Glyph base, new;
  2147. char buf[DRAW_BUF_SIZ];
  2148. bool ena_sel = sel.bx != -1, alt = IS_SET(MODE_ALTSCREEN);
  2149. if((sel.alt && !alt) || (!sel.alt && alt))
  2150. ena_sel = 0;
  2151. if(!(xw.state & WIN_VISIBLE))
  2152. return;
  2153. for(y = y1; y < y2; y++) {
  2154. if(!term.dirty[y])
  2155. continue;
  2156. xtermclear(0, y, term.col, y);
  2157. term.dirty[y] = 0;
  2158. base = term.line[y][0];
  2159. ic = ib = ox = 0;
  2160. for(x = x1; x < x2; x++) {
  2161. new = term.line[y][x];
  2162. if(ena_sel && *(new.c) && selected(x, y))
  2163. new.mode ^= ATTR_REVERSE;
  2164. if(ib > 0 && (!(new.state & GLYPH_SET)
  2165. || ATTRCMP(base, new)
  2166. || ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
  2167. xdraws(buf, base, ox, y, ic, ib);
  2168. ic = ib = 0;
  2169. }
  2170. if(new.state & GLYPH_SET) {
  2171. if(ib == 0) {
  2172. ox = x;
  2173. base = new;
  2174. }
  2175. sl = utf8size(new.c);
  2176. memcpy(buf+ib, new.c, sl);
  2177. ib += sl;
  2178. ++ic;
  2179. }
  2180. }
  2181. if(ib > 0)
  2182. xdraws(buf, base, ox, y, ic, ib);
  2183. }
  2184. xdrawcursor();
  2185. }
  2186. void
  2187. expose(XEvent *ev) {
  2188. XExposeEvent *e = &ev->xexpose;
  2189. if(xw.state & WIN_REDRAW) {
  2190. if(!e->count)
  2191. xw.state &= ~WIN_REDRAW;
  2192. }
  2193. }
  2194. void
  2195. visibility(XEvent *ev) {
  2196. XVisibilityEvent *e = &ev->xvisibility;
  2197. if(e->state == VisibilityFullyObscured) {
  2198. xw.state &= ~WIN_VISIBLE;
  2199. } else if(!(xw.state & WIN_VISIBLE)) {
  2200. /* need a full redraw for next Expose, not just a buf copy */
  2201. xw.state |= WIN_VISIBLE | WIN_REDRAW;
  2202. }
  2203. }
  2204. void
  2205. unmap(XEvent *ev) {
  2206. xw.state &= ~WIN_VISIBLE;
  2207. }
  2208. void
  2209. xseturgency(int add) {
  2210. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  2211. h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
  2212. XSetWMHints(xw.dpy, xw.win, h);
  2213. XFree(h);
  2214. }
  2215. void
  2216. focus(XEvent *ev) {
  2217. if(ev->type == FocusIn) {
  2218. xw.state |= WIN_FOCUSED;
  2219. xseturgency(0);
  2220. } else {
  2221. xw.state &= ~WIN_FOCUSED;
  2222. }
  2223. }
  2224. char*
  2225. kmap(KeySym k, uint state) {
  2226. int i;
  2227. uint mask;
  2228. state &= ~Mod2Mask;
  2229. for(i = 0; i < LEN(key); i++) {
  2230. mask = key[i].mask;
  2231. if(key[i].k == k && ((state & mask) == mask
  2232. || (mask == XK_NO_MOD && !state))) {
  2233. return (char*)key[i].s;
  2234. }
  2235. }
  2236. return NULL;
  2237. }
  2238. void
  2239. kpress(XEvent *ev) {
  2240. XKeyEvent *e = &ev->xkey;
  2241. KeySym ksym;
  2242. char buf[32];
  2243. char *customkey;
  2244. int len;
  2245. int meta;
  2246. int shift;
  2247. Status status;
  2248. if (IS_SET(MODE_KBDLOCK))
  2249. return;
  2250. meta = e->state & Mod1Mask;
  2251. shift = e->state & ShiftMask;
  2252. len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
  2253. /* 1. custom keys from config.h */
  2254. if((customkey = kmap(ksym, e->state))) {
  2255. ttywrite(customkey, strlen(customkey));
  2256. /* 2. hardcoded (overrides X lookup) */
  2257. } else {
  2258. switch(ksym) {
  2259. case XK_Up:
  2260. case XK_Down:
  2261. case XK_Left:
  2262. case XK_Right:
  2263. /* XXX: shift up/down doesn't work */
  2264. sprintf(buf, "\033%c%c",
  2265. IS_SET(MODE_APPKEYPAD) ? 'O' : '[',
  2266. (shift ? "dacb":"DACB")[ksym - XK_Left]);
  2267. ttywrite(buf, 3);
  2268. break;
  2269. case XK_Insert:
  2270. if(shift)
  2271. selpaste();
  2272. break;
  2273. case XK_Return:
  2274. if(IS_SET(MODE_CRLF)) {
  2275. ttywrite("\r\n", 2);
  2276. } else {
  2277. ttywrite("\r", 1);
  2278. }
  2279. break;
  2280. /* 3. X lookup */
  2281. default:
  2282. if(len > 0) {
  2283. if(meta && len == 1)
  2284. ttywrite("\033", 1);
  2285. ttywrite(buf, len);
  2286. }
  2287. break;
  2288. }
  2289. }
  2290. }
  2291. void
  2292. cmessage(XEvent *e) {
  2293. /* See xembed specs
  2294. http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
  2295. if(e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
  2296. if(e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
  2297. xw.state |= WIN_FOCUSED;
  2298. xseturgency(0);
  2299. } else if(e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
  2300. xw.state &= ~WIN_FOCUSED;
  2301. }
  2302. } else if(e->xclient.data.l[0] == xw.wmdeletewin) {
  2303. /* Send SIGHUP to shell */
  2304. kill(pid, SIGHUP);
  2305. exit(EXIT_SUCCESS);
  2306. }
  2307. }
  2308. void
  2309. resize(XEvent *e) {
  2310. int col, row;
  2311. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  2312. return;
  2313. xw.w = e->xconfigure.width;
  2314. xw.h = e->xconfigure.height;
  2315. col = (xw.w - 2*BORDER) / xw.cw;
  2316. row = (xw.h - 2*BORDER) / xw.ch;
  2317. if(col == term.col && row == term.row)
  2318. return;
  2319. tresize(col, row);
  2320. xresize(col, row);
  2321. ttyresize();
  2322. }
  2323. void
  2324. run(void) {
  2325. XEvent ev;
  2326. fd_set rfd;
  2327. int xfd = XConnectionNumber(xw.dpy), i;
  2328. struct timeval drawtimeout, *tv = NULL;
  2329. for(i = 0;; i++) {
  2330. FD_ZERO(&rfd);
  2331. FD_SET(cmdfd, &rfd);
  2332. FD_SET(xfd, &rfd);
  2333. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv) < 0) {
  2334. if(errno == EINTR)
  2335. continue;
  2336. die("select failed: %s\n", SERRNO);
  2337. }
  2338. /*
  2339. * Stop after a certain number of reads so the user does not
  2340. * feel like the system is stuttering.
  2341. */
  2342. if(i < 1000 && FD_ISSET(cmdfd, &rfd)) {
  2343. ttyread();
  2344. /*
  2345. * Just wait a bit so it isn't disturbing the
  2346. * user and the system is able to write something.
  2347. */
  2348. drawtimeout.tv_sec = 0;
  2349. drawtimeout.tv_usec = 5;
  2350. tv = &drawtimeout;
  2351. continue;
  2352. }
  2353. i = 0;
  2354. tv = NULL;
  2355. while(XPending(xw.dpy)) {
  2356. XNextEvent(xw.dpy, &ev);
  2357. if(XFilterEvent(&ev, xw.win))
  2358. continue;
  2359. if(handler[ev.type])
  2360. (handler[ev.type])(&ev);
  2361. }
  2362. draw();
  2363. XFlush(xw.dpy);
  2364. }
  2365. }
  2366. int
  2367. main(int argc, char *argv[]) {
  2368. int i, bitm, xr, yr;
  2369. uint wr, hr;
  2370. xw.fw = xw.fh = xw.fx = xw.fy = 0;
  2371. xw.isfixed = False;
  2372. for(i = 1; i < argc; i++) {
  2373. switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
  2374. case 'c':
  2375. if(++i < argc)
  2376. opt_class = argv[i];
  2377. break;
  2378. case 'e':
  2379. /* eat every remaining arguments */
  2380. if(++i < argc)
  2381. opt_cmd = &argv[i];
  2382. goto run;
  2383. case 'f':
  2384. if(++i < argc)
  2385. opt_font = argv[i];
  2386. break;
  2387. case 'g':
  2388. if(++i >= argc)
  2389. break;
  2390. bitm = XParseGeometry(argv[i], &xr, &yr, &wr, &hr);
  2391. if(bitm & XValue)
  2392. xw.fx = xr;
  2393. if(bitm & YValue)
  2394. xw.fy = yr;
  2395. if(bitm & WidthValue)
  2396. xw.fw = (int)wr;
  2397. if(bitm & HeightValue)
  2398. xw.fh = (int)hr;
  2399. if(bitm & XNegative && xw.fx == 0)
  2400. xw.fx = -1;
  2401. if(bitm & XNegative && xw.fy == 0)
  2402. xw.fy = -1;
  2403. if(xw.fh != 0 && xw.fw != 0)
  2404. xw.isfixed = True;
  2405. break;
  2406. case 'o':
  2407. if(++i < argc)
  2408. opt_io = argv[i];
  2409. break;
  2410. case 't':
  2411. if(++i < argc)
  2412. opt_title = argv[i];
  2413. break;
  2414. case 'v':
  2415. default:
  2416. die(USAGE);
  2417. case 'w':
  2418. if(++i < argc)
  2419. opt_embed = argv[i];
  2420. break;
  2421. }
  2422. }
  2423. run:
  2424. setlocale(LC_CTYPE, "");
  2425. tnew(80, 24);
  2426. ttynew();
  2427. xinit();
  2428. selinit();
  2429. run();
  2430. return 0;
  2431. }