My build of suckless st terminal
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2743 lines
60 KiB

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