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.

1965 lines
45 KiB

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
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
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
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
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
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 <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <signal.h>
  13. #include <sys/ioctl.h>
  14. #include <sys/select.h>
  15. #include <sys/stat.h>
  16. #include <sys/types.h>
  17. #include <sys/wait.h>
  18. #include <unistd.h>
  19. #include <X11/Xatom.h>
  20. #include <X11/Xlib.h>
  21. #include <X11/Xutil.h>
  22. #include <X11/cursorfont.h>
  23. #include <X11/keysym.h>
  24. #include <sys/time.h>
  25. #include <time.h>
  26. #if defined(__linux)
  27. #include <pty.h>
  28. #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  29. #include <util.h>
  30. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  31. #include <libutil.h>
  32. #endif
  33. #define USAGE \
  34. "st-" VERSION ", (c) 2010-2011 st engineers\n" \
  35. "usage: st [-t title] [-c class] [-v] [-e command...]\n"
  36. /* Arbitrary sizes */
  37. #define ESC_TITLE_SIZ 256
  38. #define ESC_BUF_SIZ 256
  39. #define ESC_ARG_SIZ 16
  40. #define DRAW_BUF_SIZ 1024
  41. #define UTF_SIZ 4
  42. #define SERRNO strerror(errno)
  43. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  44. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  45. #define LEN(a) (sizeof(a) / sizeof(a[0]))
  46. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  47. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  48. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  49. #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
  50. #define IS_SET(flag) (term.mode & (flag))
  51. #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
  52. #define X2COL(x) (((x) - BORDER)/xw.cw)
  53. #define Y2ROW(y) (((y) - BORDER)/xw.ch)
  54. /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
  55. enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 };
  56. enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT,
  57. CURSOR_SAVE, CURSOR_LOAD };
  58. enum { CURSOR_DEFAULT = 0, CURSOR_HIDE = 1, CURSOR_WRAPNEXT = 2 };
  59. enum { GLYPH_SET=1, GLYPH_DIRTY=2 };
  60. enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4, MODE_ALTSCREEN=8,
  61. MODE_CRLF=16, MODE_MOUSEBTN=32, MODE_MOUSEMOTION=64, MODE_MOUSE=32|64, MODE_REVERSE=128 };
  62. enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 };
  63. enum { WIN_VISIBLE=1, WIN_REDRAW=2, WIN_FOCUSED=4 };
  64. #undef B0
  65. enum { B0=1, B1=2, B2=4, B3=8, B4=16, B5=32, B6=64, B7=128 };
  66. typedef struct {
  67. char c[UTF_SIZ]; /* character code */
  68. char mode; /* attribute flags */
  69. int fg; /* foreground */
  70. int bg; /* background */
  71. char state; /* state flags */
  72. } Glyph;
  73. typedef Glyph* Line;
  74. typedef struct {
  75. Glyph attr; /* current char attributes */
  76. int x;
  77. int y;
  78. char state;
  79. } TCursor;
  80. /* CSI Escape sequence structs */
  81. /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
  82. typedef struct {
  83. char buf[ESC_BUF_SIZ]; /* raw string */
  84. int len; /* raw string length */
  85. char priv;
  86. int arg[ESC_ARG_SIZ];
  87. int narg; /* nb of args */
  88. char mode;
  89. } CSIEscape;
  90. /* Internal representation of the screen */
  91. typedef struct {
  92. int row; /* nb row */
  93. int col; /* nb col */
  94. Line* line; /* screen */
  95. Line* alt; /* alternate screen */
  96. TCursor c; /* cursor */
  97. int top; /* top scroll limit */
  98. int bot; /* bottom scroll limit */
  99. int mode; /* terminal mode flags */
  100. int esc; /* escape state flags */
  101. char title[ESC_TITLE_SIZ];
  102. int titlelen;
  103. } Term;
  104. /* Purely graphic info */
  105. typedef struct {
  106. Display* dpy;
  107. Colormap cmap;
  108. Window win;
  109. Pixmap buf;
  110. XIM xim;
  111. XIC xic;
  112. int scr;
  113. int w; /* window width */
  114. int h; /* window height */
  115. int bufw; /* pixmap width */
  116. int bufh; /* pixmap height */
  117. int ch; /* char height */
  118. int cw; /* char width */
  119. char state; /* focus, redraw, visible */
  120. } XWindow;
  121. typedef struct {
  122. KeySym k;
  123. unsigned int mask;
  124. char s[ESC_BUF_SIZ];
  125. } Key;
  126. /* Drawing Context */
  127. typedef struct {
  128. unsigned long col[256];
  129. GC gc;
  130. struct {
  131. int ascent;
  132. int descent;
  133. short lbearing;
  134. short rbearing;
  135. XFontSet set;
  136. } font, bfont;
  137. } DC;
  138. /* TODO: use better name for vars... */
  139. typedef struct {
  140. int mode;
  141. int bx, by;
  142. int ex, ey;
  143. struct {int x, y;} b, e;
  144. char *clip;
  145. Atom xtarget;
  146. struct timeval tclick1;
  147. struct timeval tclick2;
  148. } Selection;
  149. #include "config.h"
  150. static void die(const char*, ...);
  151. static void draw(void);
  152. static void drawregion(int, int, int, int);
  153. static void execsh(void);
  154. static void sigchld(int);
  155. static void run(void);
  156. static void csidump(void);
  157. static void csihandle(void);
  158. static void csiparse(void);
  159. static void csireset(void);
  160. static void tclearregion(int, int, int, int);
  161. static void tcursor(int);
  162. static void tdeletechar(int);
  163. static void tdeleteline(int);
  164. static void tinsertblank(int);
  165. static void tinsertblankline(int);
  166. static void tmoveto(int, int);
  167. static void tnew(int, int);
  168. static void tnewline(int);
  169. static void tputtab(void);
  170. static void tputc(char*);
  171. static void treset(void);
  172. static int tresize(int, int);
  173. static void tscrollup(int, int);
  174. static void tscrolldown(int, int);
  175. static void tsetattr(int*, int);
  176. static void tsetchar(char*);
  177. static void tsetscroll(int, int);
  178. static void tswapscreen(void);
  179. static void ttynew(void);
  180. static void ttyread(void);
  181. static void ttyresize(int, int);
  182. static void ttywrite(const char *, size_t);
  183. static void xdraws(char *, Glyph, int, int, int, int);
  184. static void xhints(void);
  185. static void xclear(int, int, int, int);
  186. static void xdrawcursor(void);
  187. static void xinit(void);
  188. static void xloadcols(void);
  189. static void xseturgency(int);
  190. static void xsetsel(char*);
  191. static void xresize(int, int);
  192. static void expose(XEvent *);
  193. static void visibility(XEvent *);
  194. static void unmap(XEvent *);
  195. static char* kmap(KeySym, unsigned int state);
  196. static void kpress(XEvent *);
  197. static void resize(XEvent *);
  198. static void focus(XEvent *);
  199. static void brelease(XEvent *);
  200. static void bpress(XEvent *);
  201. static void bmotion(XEvent *);
  202. static void selnotify(XEvent *);
  203. static void selrequest(XEvent *);
  204. static void selinit(void);
  205. static inline int selected(int, int);
  206. static void selcopy(void);
  207. static void selpaste();
  208. static int utf8decode(char *, long *);
  209. static int utf8encode(long *, char *);
  210. static int utf8size(char *);
  211. static int isfullutf8(char *, int);
  212. static void (*handler[LASTEvent])(XEvent *) = {
  213. [KeyPress] = kpress,
  214. [ConfigureNotify] = resize,
  215. [VisibilityNotify] = visibility,
  216. [UnmapNotify] = unmap,
  217. [Expose] = expose,
  218. [FocusIn] = focus,
  219. [FocusOut] = focus,
  220. [MotionNotify] = bmotion,
  221. [ButtonPress] = bpress,
  222. [ButtonRelease] = brelease,
  223. [SelectionNotify] = selnotify,
  224. [SelectionRequest] = selrequest,
  225. };
  226. /* Globals */
  227. static DC dc;
  228. static XWindow xw;
  229. static Term term;
  230. static CSIEscape escseq;
  231. static int cmdfd;
  232. static pid_t pid;
  233. static Selection sel;
  234. static char **opt_cmd = NULL;
  235. static char *opt_title = NULL;
  236. static char *opt_class = NULL;
  237. int
  238. utf8decode(char *s, long *u) {
  239. unsigned char c;
  240. int i, n, rtn;
  241. rtn = 1;
  242. c = *s;
  243. if(~c&B7) { /* 0xxxxxxx */
  244. *u = c;
  245. return rtn;
  246. } else if((c&(B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
  247. *u = c&(B4|B3|B2|B1|B0);
  248. n = 1;
  249. } else if((c&(B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
  250. *u = c&(B3|B2|B1|B0);
  251. n = 2;
  252. } else if((c&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
  253. *u = c&(B2|B1|B0);
  254. n = 3;
  255. } else
  256. goto invalid;
  257. for(i=n,++s; i>0; --i,++rtn,++s) {
  258. c = *s;
  259. if((c&(B7|B6)) != B7) /* 10xxxxxx */
  260. goto invalid;
  261. *u <<= 6;
  262. *u |= c&(B5|B4|B3|B2|B1|B0);
  263. }
  264. if((n == 1 && *u < 0x80) ||
  265. (n == 2 && *u < 0x800) ||
  266. (n == 3 && *u < 0x10000) ||
  267. (*u >= 0xD800 && *u <= 0xDFFF))
  268. goto invalid;
  269. return rtn;
  270. invalid:
  271. *u = 0xFFFD;
  272. return rtn;
  273. }
  274. int
  275. utf8encode(long *u, char *s) {
  276. unsigned char *sp;
  277. unsigned long uc;
  278. int i, n;
  279. sp = (unsigned char*) s;
  280. uc = *u;
  281. if(uc < 0x80) {
  282. *sp = uc; /* 0xxxxxxx */
  283. return 1;
  284. } else if(*u < 0x800) {
  285. *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
  286. n = 1;
  287. } else if(uc < 0x10000) {
  288. *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
  289. n = 2;
  290. } else if(uc <= 0x10FFFF) {
  291. *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
  292. n = 3;
  293. } else {
  294. goto invalid;
  295. }
  296. for(i=n,++sp; i>0; --i,++sp)
  297. *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
  298. return n+1;
  299. invalid:
  300. /* U+FFFD */
  301. *s++ = '\xEF';
  302. *s++ = '\xBF';
  303. *s = '\xBD';
  304. return 3;
  305. }
  306. /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
  307. UTF-8 otherwise return 0 */
  308. int
  309. isfullutf8(char *s, int b) {
  310. unsigned char *c1, *c2, *c3;
  311. c1 = (unsigned char *) s;
  312. c2 = (unsigned char *) ++s;
  313. c3 = (unsigned char *) ++s;
  314. if(b < 1)
  315. return 0;
  316. else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1)
  317. return 0;
  318. else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
  319. ((b == 1) ||
  320. ((b == 2) && (*c2&(B7|B6)) == B7)))
  321. return 0;
  322. else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
  323. ((b == 1) ||
  324. ((b == 2) && (*c2&(B7|B6)) == B7) ||
  325. ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7)))
  326. return 0;
  327. else
  328. return 1;
  329. }
  330. int
  331. utf8size(char *s) {
  332. unsigned char c = *s;
  333. if(~c&B7)
  334. return 1;
  335. else if((c&(B7|B6|B5)) == (B7|B6))
  336. return 2;
  337. else if((c&(B7|B6|B5|B4)) == (B7|B6|B5))
  338. return 3;
  339. else
  340. return 4;
  341. }
  342. void
  343. selinit(void) {
  344. sel.tclick1.tv_sec = 0;
  345. sel.tclick1.tv_usec = 0;
  346. sel.mode = 0;
  347. sel.bx = -1;
  348. sel.clip = NULL;
  349. sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
  350. if(sel.xtarget == None)
  351. sel.xtarget = XA_STRING;
  352. }
  353. static inline int
  354. selected(int x, int y) {
  355. if(sel.ey == y && sel.by == y) {
  356. int bx = MIN(sel.bx, sel.ex);
  357. int ex = MAX(sel.bx, sel.ex);
  358. return BETWEEN(x, bx, ex);
  359. }
  360. return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x))
  361. || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y));
  362. }
  363. void
  364. getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
  365. if(b)
  366. *b = e->xbutton.button;
  367. *x = X2COL(e->xbutton.x);
  368. *y = Y2ROW(e->xbutton.y);
  369. sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
  370. sel.b.y = MIN(sel.by, sel.ey);
  371. sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
  372. sel.e.y = MAX(sel.by, sel.ey);
  373. }
  374. void
  375. mousereport(XEvent *e) {
  376. int x = X2COL(e->xbutton.x);
  377. int y = Y2ROW(e->xbutton.y);
  378. int button = e->xbutton.button;
  379. int state = e->xbutton.state;
  380. char buf[] = { '\033', '[', 'M', 0, 32+x+1, 32+y+1 };
  381. static int ob, ox, oy;
  382. /* from urxvt */
  383. if(e->xbutton.type == MotionNotify) {
  384. if(!IS_SET(MODE_MOUSEMOTION) || (x == ox && y == oy))
  385. return;
  386. button = ob + 32;
  387. ox = x, oy = y;
  388. } else if(e->xbutton.type == ButtonRelease || button == AnyButton) {
  389. button = 3;
  390. } else {
  391. button -= Button1;
  392. if(button >= 3)
  393. button += 64 - 3;
  394. if(e->xbutton.type == ButtonPress) {
  395. ob = button;
  396. ox = x, oy = y;
  397. }
  398. }
  399. buf[3] = 32 + button + (state & ShiftMask ? 4 : 0)
  400. + (state & Mod4Mask ? 8 : 0)
  401. + (state & ControlMask ? 16 : 0);
  402. ttywrite(buf, sizeof(buf));
  403. }
  404. void
  405. bpress(XEvent *e) {
  406. if(IS_SET(MODE_MOUSE))
  407. mousereport(e);
  408. else if(e->xbutton.button == Button1) {
  409. sel.mode = 1;
  410. sel.ex = sel.bx = X2COL(e->xbutton.x);
  411. sel.ey = sel.by = Y2ROW(e->xbutton.y);
  412. }
  413. }
  414. void
  415. selcopy(void) {
  416. char *str, *ptr;
  417. int x, y, sz, sl, ls = 0;
  418. if(sel.bx == -1)
  419. str = NULL;
  420. else {
  421. sz = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
  422. ptr = str = malloc(sz);
  423. for(y = 0; y < term.row; y++) {
  424. for(x = 0; x < term.col; x++)
  425. if(term.line[y][x].state & GLYPH_SET && (ls = selected(x, y))) {
  426. sl = utf8size(term.line[y][x].c);
  427. memcpy(ptr, term.line[y][x].c, sl);
  428. ptr += sl;
  429. }
  430. if(ls && y < sel.e.y)
  431. *ptr++ = '\n';
  432. }
  433. *ptr = 0;
  434. }
  435. xsetsel(str);
  436. }
  437. void
  438. selnotify(XEvent *e) {
  439. unsigned long nitems;
  440. unsigned long ofs, rem;
  441. int format;
  442. unsigned char *data;
  443. Atom type;
  444. ofs = 0;
  445. do {
  446. if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
  447. False, AnyPropertyType, &type, &format,
  448. &nitems, &rem, &data)) {
  449. fprintf(stderr, "Clipboard allocation failed\n");
  450. return;
  451. }
  452. ttywrite((const char *) data, nitems * format / 8);
  453. XFree(data);
  454. /* number of 32-bit chunks returned */
  455. ofs += nitems * format / 32;
  456. } while(rem > 0);
  457. }
  458. void
  459. selpaste() {
  460. XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY, xw.win, CurrentTime);
  461. }
  462. void
  463. selrequest(XEvent *e) {
  464. XSelectionRequestEvent *xsre;
  465. XSelectionEvent xev;
  466. Atom xa_targets;
  467. xsre = (XSelectionRequestEvent *) e;
  468. xev.type = SelectionNotify;
  469. xev.requestor = xsre->requestor;
  470. xev.selection = xsre->selection;
  471. xev.target = xsre->target;
  472. xev.time = xsre->time;
  473. /* reject */
  474. xev.property = None;
  475. xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
  476. if(xsre->target == xa_targets) {
  477. /* respond with the supported type */
  478. Atom string = sel.xtarget;
  479. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  480. XA_ATOM, 32, PropModeReplace,
  481. (unsigned char *) &string, 1);
  482. xev.property = xsre->property;
  483. } else if(xsre->target == sel.xtarget) {
  484. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  485. xsre->target, 8, PropModeReplace,
  486. (unsigned char *) sel.clip, strlen(sel.clip));
  487. xev.property = xsre->property;
  488. }
  489. /* all done, send a notification to the listener */
  490. if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
  491. fprintf(stderr, "Error sending SelectionNotify event\n");
  492. }
  493. void
  494. xsetsel(char *str) {
  495. /* register the selection for both the clipboard and the primary */
  496. Atom clipboard;
  497. free(sel.clip);
  498. sel.clip = str;
  499. XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
  500. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  501. XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
  502. XFlush(xw.dpy);
  503. }
  504. void
  505. brelease(XEvent *e) {
  506. if(IS_SET(MODE_MOUSE)) {
  507. mousereport(e);
  508. return;
  509. }
  510. if(e->xbutton.button == Button2)
  511. selpaste();
  512. else if(e->xbutton.button == Button1) {
  513. sel.mode = 0;
  514. getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
  515. if(sel.bx == sel.ex && sel.by == sel.ey) {
  516. struct timeval now;
  517. sel.bx = -1;
  518. gettimeofday(&now, NULL);
  519. if(TIMEDIFF(now, sel.tclick2) <= TRIPLECLICK_TIMEOUT) {
  520. /* triple click on the line */
  521. sel.b.x = sel.bx = 0;
  522. sel.e.x = sel.ex = term.col;
  523. sel.b.y = sel.e.y = sel.ey;
  524. selcopy();
  525. } else if(TIMEDIFF(now, sel.tclick1) <= DOUBLECLICK_TIMEOUT) {
  526. /* double click to select word */
  527. sel.bx = sel.ex;
  528. while(sel.bx > 0 && term.line[sel.ey][sel.bx-1].state & GLYPH_SET &&
  529. term.line[sel.ey][sel.bx-1].c[0] != ' ') sel.bx--;
  530. sel.b.x = sel.bx;
  531. while(sel.ex < term.col-1 && term.line[sel.ey][sel.ex+1].state & GLYPH_SET &&
  532. term.line[sel.ey][sel.ex+1].c[0] != ' ') sel.ex++;
  533. sel.e.x = sel.ex;
  534. sel.b.y = sel.e.y = sel.ey;
  535. selcopy();
  536. }
  537. } else
  538. selcopy();
  539. }
  540. memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
  541. gettimeofday(&sel.tclick1, NULL);
  542. draw();
  543. }
  544. void
  545. bmotion(XEvent *e) {
  546. if(IS_SET(MODE_MOUSE)) {
  547. mousereport(e);
  548. return;
  549. }
  550. if(sel.mode) {
  551. int oldey = sel.ey, oldex = sel.ex;
  552. getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
  553. if(oldey != sel.ey || oldex != sel.ex) {
  554. int starty = MIN(oldey, sel.ey);
  555. int endy = MAX(oldey, sel.ey);
  556. drawregion(0, (starty > 0 ? starty : 0), term.col, (sel.ey < term.row ? endy+1 : term.row));
  557. }
  558. }
  559. }
  560. void
  561. die(const char *errstr, ...) {
  562. va_list ap;
  563. va_start(ap, errstr);
  564. vfprintf(stderr, errstr, ap);
  565. va_end(ap);
  566. exit(EXIT_FAILURE);
  567. }
  568. void
  569. execsh(void) {
  570. char **args;
  571. char *envshell = getenv("SHELL");
  572. DEFAULT(envshell, "sh");
  573. putenv("TERM="TNAME);
  574. args = opt_cmd ? opt_cmd : (char*[]){envshell, "-i", NULL};
  575. execvp(args[0], args);
  576. exit(EXIT_FAILURE);
  577. }
  578. void
  579. sigchld(int a) {
  580. int stat = 0;
  581. if(waitpid(pid, &stat, 0) < 0)
  582. die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
  583. if(WIFEXITED(stat))
  584. exit(WEXITSTATUS(stat));
  585. else
  586. exit(EXIT_FAILURE);
  587. }
  588. void
  589. ttynew(void) {
  590. int m, s;
  591. /* seems to work fine on linux, openbsd and freebsd */
  592. struct winsize w = {term.row, term.col, 0, 0};
  593. if(openpty(&m, &s, NULL, NULL, &w) < 0)
  594. die("openpty failed: %s\n", SERRNO);
  595. switch(pid = fork()) {
  596. case -1:
  597. die("fork failed\n");
  598. break;
  599. case 0:
  600. setsid(); /* create a new process group */
  601. dup2(s, STDIN_FILENO);
  602. dup2(s, STDOUT_FILENO);
  603. dup2(s, STDERR_FILENO);
  604. if(ioctl(s, TIOCSCTTY, NULL) < 0)
  605. die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
  606. close(s);
  607. close(m);
  608. execsh();
  609. break;
  610. default:
  611. close(s);
  612. cmdfd = m;
  613. signal(SIGCHLD, sigchld);
  614. }
  615. }
  616. void
  617. dump(char c) {
  618. static int col;
  619. fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
  620. if(++col % 10 == 0)
  621. fprintf(stderr, "\n");
  622. }
  623. void
  624. ttyread(void) {
  625. static char buf[BUFSIZ];
  626. static int buflen = 0;
  627. char *ptr;
  628. char s[UTF_SIZ];
  629. int charsize; /* size of utf8 char in bytes */
  630. long utf8c;
  631. int ret;
  632. /* append read bytes to unprocessed bytes */
  633. if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
  634. die("Couldn't read from shell: %s\n", SERRNO);
  635. /* process every complete utf8 char */
  636. buflen += ret;
  637. ptr = buf;
  638. while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
  639. charsize = utf8decode(ptr, &utf8c);
  640. utf8encode(&utf8c, s);
  641. tputc(s);
  642. ptr += charsize;
  643. buflen -= charsize;
  644. }
  645. /* keep any uncomplete utf8 char for the next call */
  646. memmove(buf, ptr, buflen);
  647. }
  648. void
  649. ttywrite(const char *s, size_t n) {
  650. if(write(cmdfd, s, n) == -1)
  651. die("write error on tty: %s\n", SERRNO);
  652. }
  653. void
  654. ttyresize(int x, int y) {
  655. struct winsize w;
  656. w.ws_row = term.row;
  657. w.ws_col = term.col;
  658. w.ws_xpixel = w.ws_ypixel = 0;
  659. if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  660. fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
  661. }
  662. void
  663. tcursor(int mode) {
  664. static TCursor c;
  665. if(mode == CURSOR_SAVE)
  666. c = term.c;
  667. else if(mode == CURSOR_LOAD)
  668. term.c = c, tmoveto(c.x, c.y);
  669. }
  670. void
  671. treset(void) {
  672. term.c = (TCursor){{
  673. .mode = ATTR_NULL,
  674. .fg = DefaultFG,
  675. .bg = DefaultBG
  676. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  677. term.top = 0, term.bot = term.row - 1;
  678. term.mode = MODE_WRAP;
  679. tclearregion(0, 0, term.col-1, term.row-1);
  680. }
  681. void
  682. tnew(int col, int row) {
  683. /* set screen size */
  684. term.row = row, term.col = col;
  685. term.line = malloc(term.row * sizeof(Line));
  686. term.alt = malloc(term.row * sizeof(Line));
  687. for(row = 0 ; row < term.row; row++) {
  688. term.line[row] = malloc(term.col * sizeof(Glyph));
  689. term.alt [row] = malloc(term.col * sizeof(Glyph));
  690. }
  691. /* setup screen */
  692. treset();
  693. }
  694. void
  695. tswapscreen(void) {
  696. Line* tmp = term.line;
  697. term.line = term.alt;
  698. term.alt = tmp;
  699. term.mode ^= MODE_ALTSCREEN;
  700. }
  701. void
  702. tscrolldown(int orig, int n) {
  703. int i;
  704. Line temp;
  705. LIMIT(n, 0, term.bot-orig+1);
  706. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  707. for(i = term.bot; i >= orig+n; i--) {
  708. temp = term.line[i];
  709. term.line[i] = term.line[i-n];
  710. term.line[i-n] = temp;
  711. }
  712. }
  713. void
  714. tscrollup(int orig, int n) {
  715. int i;
  716. Line temp;
  717. LIMIT(n, 0, term.bot-orig+1);
  718. tclearregion(0, orig, term.col-1, orig+n-1);
  719. for(i = orig; i <= term.bot-n; i++) {
  720. temp = term.line[i];
  721. term.line[i] = term.line[i+n];
  722. term.line[i+n] = temp;
  723. }
  724. }
  725. void
  726. tnewline(int first_col) {
  727. int y = term.c.y;
  728. if(y == term.bot)
  729. tscrollup(term.top, 1);
  730. else
  731. y++;
  732. tmoveto(first_col ? 0 : term.c.x, y);
  733. }
  734. void
  735. csiparse(void) {
  736. /* int noarg = 1; */
  737. char *p = escseq.buf;
  738. escseq.narg = 0;
  739. if(*p == '?')
  740. escseq.priv = 1, p++;
  741. while(p < escseq.buf+escseq.len) {
  742. while(isdigit(*p)) {
  743. escseq.arg[escseq.narg] *= 10;
  744. escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
  745. }
  746. if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
  747. escseq.narg++, p++;
  748. else {
  749. escseq.mode = *p;
  750. escseq.narg++;
  751. return;
  752. }
  753. }
  754. }
  755. void
  756. tmoveto(int x, int y) {
  757. LIMIT(x, 0, term.col-1);
  758. LIMIT(y, 0, term.row-1);
  759. term.c.state &= ~CURSOR_WRAPNEXT;
  760. term.c.x = x;
  761. term.c.y = y;
  762. }
  763. void
  764. tsetchar(char *c) {
  765. term.line[term.c.y][term.c.x] = term.c.attr;
  766. memcpy(term.line[term.c.y][term.c.x].c, c, UTF_SIZ);
  767. term.line[term.c.y][term.c.x].state |= GLYPH_SET;
  768. }
  769. void
  770. tclearregion(int x1, int y1, int x2, int y2) {
  771. int x, y, temp;
  772. if(x1 > x2)
  773. temp = x1, x1 = x2, x2 = temp;
  774. if(y1 > y2)
  775. temp = y1, y1 = y2, y2 = temp;
  776. LIMIT(x1, 0, term.col-1);
  777. LIMIT(x2, 0, term.col-1);
  778. LIMIT(y1, 0, term.row-1);
  779. LIMIT(y2, 0, term.row-1);
  780. for(y = y1; y <= y2; y++)
  781. for(x = x1; x <= x2; x++)
  782. term.line[y][x].state = 0;
  783. }
  784. void
  785. tdeletechar(int n) {
  786. int src = term.c.x + n;
  787. int dst = term.c.x;
  788. int size = term.col - src;
  789. if(src >= term.col) {
  790. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  791. return;
  792. }
  793. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  794. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  795. }
  796. void
  797. tinsertblank(int n) {
  798. int src = term.c.x;
  799. int dst = src + n;
  800. int size = term.col - dst;
  801. if(dst >= term.col) {
  802. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  803. return;
  804. }
  805. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  806. tclearregion(src, term.c.y, dst - 1, term.c.y);
  807. }
  808. void
  809. tinsertblankline(int n) {
  810. if(term.c.y < term.top || term.c.y > term.bot)
  811. return;
  812. tscrolldown(term.c.y, n);
  813. }
  814. void
  815. tdeleteline(int n) {
  816. if(term.c.y < term.top || term.c.y > term.bot)
  817. return;
  818. tscrollup(term.c.y, n);
  819. }
  820. void
  821. tsetattr(int *attr, int l) {
  822. int i;
  823. for(i = 0; i < l; i++) {
  824. switch(attr[i]) {
  825. case 0:
  826. term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
  827. term.c.attr.fg = DefaultFG;
  828. term.c.attr.bg = DefaultBG;
  829. break;
  830. case 1:
  831. term.c.attr.mode |= ATTR_BOLD;
  832. break;
  833. case 4:
  834. term.c.attr.mode |= ATTR_UNDERLINE;
  835. break;
  836. case 7:
  837. term.c.attr.mode |= ATTR_REVERSE;
  838. break;
  839. case 22:
  840. term.c.attr.mode &= ~ATTR_BOLD;
  841. break;
  842. case 24:
  843. term.c.attr.mode &= ~ATTR_UNDERLINE;
  844. break;
  845. case 27:
  846. term.c.attr.mode &= ~ATTR_REVERSE;
  847. break;
  848. case 38:
  849. if(i + 2 < l && attr[i + 1] == 5) {
  850. i += 2;
  851. if(BETWEEN(attr[i], 0, 255))
  852. term.c.attr.fg = attr[i];
  853. else
  854. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
  855. }
  856. else
  857. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  858. break;
  859. case 39:
  860. term.c.attr.fg = DefaultFG;
  861. break;
  862. case 48:
  863. if(i + 2 < l && attr[i + 1] == 5) {
  864. i += 2;
  865. if(BETWEEN(attr[i], 0, 255))
  866. term.c.attr.bg = attr[i];
  867. else
  868. fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
  869. }
  870. else
  871. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  872. break;
  873. case 49:
  874. term.c.attr.bg = DefaultBG;
  875. break;
  876. default:
  877. if(BETWEEN(attr[i], 30, 37))
  878. term.c.attr.fg = attr[i] - 30;
  879. else if(BETWEEN(attr[i], 40, 47))
  880. term.c.attr.bg = attr[i] - 40;
  881. else if(BETWEEN(attr[i], 90, 97))
  882. term.c.attr.fg = attr[i] - 90 + 8;
  883. else if(BETWEEN(attr[i], 100, 107))
  884. term.c.attr.fg = attr[i] - 100 + 8;
  885. else
  886. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]), csidump();
  887. break;
  888. }
  889. }
  890. }
  891. void
  892. tsetscroll(int t, int b) {
  893. int temp;
  894. LIMIT(t, 0, term.row-1);
  895. LIMIT(b, 0, term.row-1);
  896. if(t > b) {
  897. temp = t;
  898. t = b;
  899. b = temp;
  900. }
  901. term.top = t;
  902. term.bot = b;
  903. }
  904. void
  905. csihandle(void) {
  906. switch(escseq.mode) {
  907. default:
  908. unknown:
  909. fprintf(stderr, "erresc: unknown csi ");
  910. csidump();
  911. /* die(""); */
  912. break;
  913. case '@': /* ICH -- Insert <n> blank char */
  914. DEFAULT(escseq.arg[0], 1);
  915. tinsertblank(escseq.arg[0]);
  916. break;
  917. case 'A': /* CUU -- Cursor <n> Up */
  918. case 'e':
  919. DEFAULT(escseq.arg[0], 1);
  920. tmoveto(term.c.x, term.c.y-escseq.arg[0]);
  921. break;
  922. case 'B': /* CUD -- Cursor <n> Down */
  923. DEFAULT(escseq.arg[0], 1);
  924. tmoveto(term.c.x, term.c.y+escseq.arg[0]);
  925. break;
  926. case 'C': /* CUF -- Cursor <n> Forward */
  927. case 'a':
  928. DEFAULT(escseq.arg[0], 1);
  929. tmoveto(term.c.x+escseq.arg[0], term.c.y);
  930. break;
  931. case 'D': /* CUB -- Cursor <n> Backward */
  932. DEFAULT(escseq.arg[0], 1);
  933. tmoveto(term.c.x-escseq.arg[0], term.c.y);
  934. break;
  935. case 'E': /* CNL -- Cursor <n> Down and first col */
  936. DEFAULT(escseq.arg[0], 1);
  937. tmoveto(0, term.c.y+escseq.arg[0]);
  938. break;
  939. case 'F': /* CPL -- Cursor <n> Up and first col */
  940. DEFAULT(escseq.arg[0], 1);
  941. tmoveto(0, term.c.y-escseq.arg[0]);
  942. break;
  943. case 'G': /* CHA -- Move to <col> */
  944. case '`': /* XXX: HPA -- same? */
  945. DEFAULT(escseq.arg[0], 1);
  946. tmoveto(escseq.arg[0]-1, term.c.y);
  947. break;
  948. case 'H': /* CUP -- Move to <row> <col> */
  949. case 'f': /* XXX: HVP -- same? */
  950. DEFAULT(escseq.arg[0], 1);
  951. DEFAULT(escseq.arg[1], 1);
  952. tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
  953. break;
  954. /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
  955. case 'J': /* ED -- Clear screen */
  956. switch(escseq.arg[0]) {
  957. case 0: /* below */
  958. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  959. if(term.c.y < term.row-1)
  960. tclearregion(0, term.c.y+1, term.col-1, term.row-1);
  961. break;
  962. case 1: /* above */
  963. if(term.c.y > 1)
  964. tclearregion(0, 0, term.col-1, term.c.y-1);
  965. tclearregion(0, term.c.y, term.c.x, term.c.y);
  966. break;
  967. case 2: /* all */
  968. tclearregion(0, 0, term.col-1, term.row-1);
  969. break;
  970. default:
  971. goto unknown;
  972. }
  973. break;
  974. case 'K': /* EL -- Clear line */
  975. switch(escseq.arg[0]) {
  976. case 0: /* right */
  977. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  978. break;
  979. case 1: /* left */
  980. tclearregion(0, term.c.y, term.c.x, term.c.y);
  981. break;
  982. case 2: /* all */
  983. tclearregion(0, term.c.y, term.col-1, term.c.y);
  984. break;
  985. }
  986. break;
  987. case 'S': /* SU -- Scroll <n> line up */
  988. DEFAULT(escseq.arg[0], 1);
  989. tscrollup(term.top, escseq.arg[0]);
  990. break;
  991. case 'T': /* SD -- Scroll <n> line down */
  992. DEFAULT(escseq.arg[0], 1);
  993. tscrolldown(term.top, escseq.arg[0]);
  994. break;
  995. case 'L': /* IL -- Insert <n> blank lines */
  996. DEFAULT(escseq.arg[0], 1);
  997. tinsertblankline(escseq.arg[0]);
  998. break;
  999. case 'l': /* RM -- Reset Mode */
  1000. if(escseq.priv) {
  1001. switch(escseq.arg[0]) {
  1002. case 1:
  1003. term.mode &= ~MODE_APPKEYPAD;
  1004. break;
  1005. case 5: /* DECSCNM -- Remove reverse video */
  1006. if(IS_SET(MODE_REVERSE)) {
  1007. term.mode &= ~MODE_REVERSE;
  1008. draw();
  1009. }
  1010. break;
  1011. case 7:
  1012. term.mode &= ~MODE_WRAP;
  1013. break;
  1014. case 12: /* att610 -- Stop blinking cursor (IGNORED) */
  1015. break;
  1016. case 20:
  1017. term.mode &= ~MODE_CRLF;
  1018. break;
  1019. case 25:
  1020. term.c.state |= CURSOR_HIDE;
  1021. break;
  1022. case 1000: /* disable X11 xterm mouse reporting */
  1023. term.mode &= ~MODE_MOUSEBTN;
  1024. break;
  1025. case 1002:
  1026. term.mode &= ~MODE_MOUSEMOTION;
  1027. break;
  1028. case 1049: /* = 1047 and 1048 */
  1029. case 47:
  1030. case 1047:
  1031. if(IS_SET(MODE_ALTSCREEN)) {
  1032. tclearregion(0, 0, term.col-1, term.row-1);
  1033. tswapscreen();
  1034. }
  1035. if(escseq.arg[0] != 1049)
  1036. break;
  1037. case 1048:
  1038. tcursor(CURSOR_LOAD);
  1039. break;
  1040. default:
  1041. goto unknown;
  1042. }
  1043. } else {
  1044. switch(escseq.arg[0]) {
  1045. case 4:
  1046. term.mode &= ~MODE_INSERT;
  1047. break;
  1048. default:
  1049. goto unknown;
  1050. }
  1051. }
  1052. break;
  1053. case 'M': /* DL -- Delete <n> lines */
  1054. DEFAULT(escseq.arg[0], 1);
  1055. tdeleteline(escseq.arg[0]);
  1056. break;
  1057. case 'X': /* ECH -- Erase <n> char */
  1058. DEFAULT(escseq.arg[0], 1);
  1059. tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
  1060. break;
  1061. case 'P': /* DCH -- Delete <n> char */
  1062. DEFAULT(escseq.arg[0], 1);
  1063. tdeletechar(escseq.arg[0]);
  1064. break;
  1065. /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
  1066. case 'd': /* VPA -- Move to <row> */
  1067. DEFAULT(escseq.arg[0], 1);
  1068. tmoveto(term.c.x, escseq.arg[0]-1);
  1069. break;
  1070. case 'h': /* SM -- Set terminal mode */
  1071. if(escseq.priv) {
  1072. switch(escseq.arg[0]) {
  1073. case 1:
  1074. term.mode |= MODE_APPKEYPAD;
  1075. break;
  1076. case 5: /* DECSCNM -- Reverve video */
  1077. if(!IS_SET(MODE_REVERSE)) {
  1078. term.mode |= MODE_REVERSE;
  1079. draw();
  1080. }
  1081. break;
  1082. case 7:
  1083. term.mode |= MODE_WRAP;
  1084. break;
  1085. case 20:
  1086. term.mode |= MODE_CRLF;
  1087. break;
  1088. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1089. /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
  1090. if(escseq.narg > 1 && escseq.arg[1] != 25)
  1091. break;
  1092. case 25:
  1093. term.c.state &= ~CURSOR_HIDE;
  1094. break;
  1095. case 1000: /* 1000,1002: enable xterm mouse report */
  1096. term.mode |= MODE_MOUSEBTN;
  1097. break;
  1098. case 1002:
  1099. term.mode |= MODE_MOUSEMOTION;
  1100. break;
  1101. case 1049: /* = 1047 and 1048 */
  1102. case 47:
  1103. case 1047:
  1104. if(IS_SET(MODE_ALTSCREEN))
  1105. tclearregion(0, 0, term.col-1, term.row-1);
  1106. else
  1107. tswapscreen();
  1108. if(escseq.arg[0] != 1049)
  1109. break;
  1110. case 1048:
  1111. tcursor(CURSOR_SAVE);
  1112. break;
  1113. default: goto unknown;
  1114. }
  1115. } else {
  1116. switch(escseq.arg[0]) {
  1117. case 4:
  1118. term.mode |= MODE_INSERT;
  1119. break;
  1120. default: goto unknown;
  1121. }
  1122. };
  1123. break;
  1124. case 'm': /* SGR -- Terminal attribute (color) */
  1125. tsetattr(escseq.arg, escseq.narg);
  1126. break;
  1127. case 'r': /* DECSTBM -- Set Scrolling Region */
  1128. if(escseq.priv)
  1129. goto unknown;
  1130. else {
  1131. DEFAULT(escseq.arg[0], 1);
  1132. DEFAULT(escseq.arg[1], term.row);
  1133. tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
  1134. tmoveto(0, 0);
  1135. }
  1136. break;
  1137. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  1138. tcursor(CURSOR_SAVE);
  1139. break;
  1140. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  1141. tcursor(CURSOR_LOAD);
  1142. break;
  1143. }
  1144. }
  1145. void
  1146. csidump(void) {
  1147. int i;
  1148. printf("ESC [ %s", escseq.priv ? "? " : "");
  1149. if(escseq.narg)
  1150. for(i = 0; i < escseq.narg; i++)
  1151. printf("%d ", escseq.arg[i]);
  1152. if(escseq.mode)
  1153. putchar(escseq.mode);
  1154. putchar('\n');
  1155. }
  1156. void
  1157. csireset(void) {
  1158. memset(&escseq, 0, sizeof(escseq));
  1159. }
  1160. void
  1161. tputtab(void) {
  1162. int space = TAB - term.c.x % TAB;
  1163. tmoveto(term.c.x + space, term.c.y);
  1164. }
  1165. void
  1166. tputc(char *c) {
  1167. char ascii = *c;
  1168. if(term.esc & ESC_START) {
  1169. if(term.esc & ESC_CSI) {
  1170. escseq.buf[escseq.len++] = ascii;
  1171. if(BETWEEN(ascii, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
  1172. term.esc = 0;
  1173. csiparse(), csihandle();
  1174. }
  1175. /* TODO: handle other OSC */
  1176. } else if(term.esc & ESC_OSC) {
  1177. if(ascii == ';') {
  1178. term.titlelen = 0;
  1179. term.esc = ESC_START | ESC_TITLE;
  1180. }
  1181. } else if(term.esc & ESC_TITLE) {
  1182. if(ascii == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
  1183. term.esc = 0;
  1184. term.title[term.titlelen] = '\0';
  1185. XStoreName(xw.dpy, xw.win, term.title);
  1186. } else {
  1187. term.title[term.titlelen++] = ascii;
  1188. }
  1189. } else if(term.esc & ESC_ALTCHARSET) {
  1190. switch(ascii) {
  1191. case '0': /* Line drawing crap */
  1192. term.c.attr.mode |= ATTR_GFX;
  1193. break;
  1194. case 'B': /* Back to regular text */
  1195. term.c.attr.mode &= ~ATTR_GFX;
  1196. break;
  1197. default:
  1198. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  1199. }
  1200. term.esc = 0;
  1201. } else {
  1202. switch(ascii) {
  1203. case '[':
  1204. term.esc |= ESC_CSI;
  1205. break;
  1206. case ']':
  1207. term.esc |= ESC_OSC;
  1208. break;
  1209. case '(':
  1210. term.esc |= ESC_ALTCHARSET;
  1211. break;
  1212. case 'D': /* IND -- Linefeed */
  1213. if(term.c.y == term.bot)
  1214. tscrollup(term.top, 1);
  1215. else
  1216. tmoveto(term.c.x, term.c.y+1);
  1217. term.esc = 0;
  1218. break;
  1219. case 'E': /* NEL -- Next line */
  1220. tnewline(1); /* always go to first col */
  1221. term.esc = 0;
  1222. break;
  1223. case 'M': /* RI -- Reverse index */
  1224. if(term.c.y == term.top)
  1225. tscrolldown(term.top, 1);
  1226. else
  1227. tmoveto(term.c.x, term.c.y-1);
  1228. term.esc = 0;
  1229. break;
  1230. case 'c': /* RIS -- Reset to inital state */
  1231. treset();
  1232. term.esc = 0;
  1233. break;
  1234. case '=': /* DECPAM -- Application keypad */
  1235. term.mode |= MODE_APPKEYPAD;
  1236. term.esc = 0;
  1237. break;
  1238. case '>': /* DECPNM -- Normal keypad */
  1239. term.mode &= ~MODE_APPKEYPAD;
  1240. term.esc = 0;
  1241. break;
  1242. case '7': /* DECSC -- Save Cursor */
  1243. tcursor(CURSOR_SAVE);
  1244. term.esc = 0;
  1245. break;
  1246. case '8': /* DECRC -- Restore Cursor */
  1247. tcursor(CURSOR_LOAD);
  1248. term.esc = 0;
  1249. break;
  1250. default:
  1251. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  1252. (unsigned char) ascii, isprint(ascii)?ascii:'.');
  1253. term.esc = 0;
  1254. }
  1255. }
  1256. } else {
  1257. switch(ascii) {
  1258. case '\t':
  1259. tputtab();
  1260. break;
  1261. case '\b':
  1262. tmoveto(term.c.x-1, term.c.y);
  1263. break;
  1264. case '\r':
  1265. tmoveto(0, term.c.y);
  1266. break;
  1267. case '\f':
  1268. case '\v':
  1269. case '\n':
  1270. /* go to first col if the mode is set */
  1271. tnewline(IS_SET(MODE_CRLF));
  1272. break;
  1273. case '\a':
  1274. if(!(xw.state & WIN_FOCUSED))
  1275. xseturgency(1);
  1276. break;
  1277. case '\033':
  1278. csireset();
  1279. term.esc = ESC_START;
  1280. break;
  1281. default:
  1282. if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
  1283. tnewline(1); /* always go to first col */
  1284. tsetchar(c);
  1285. if(term.c.x+1 < term.col)
  1286. tmoveto(term.c.x+1, term.c.y);
  1287. else
  1288. term.c.state |= CURSOR_WRAPNEXT;
  1289. }
  1290. }
  1291. }
  1292. int
  1293. tresize(int col, int row) {
  1294. int i, x;
  1295. int minrow = MIN(row, term.row);
  1296. int mincol = MIN(col, term.col);
  1297. int slide = term.c.y - row + 1;
  1298. if(col < 1 || row < 1)
  1299. return 0;
  1300. /* free unneeded rows */
  1301. i = 0;
  1302. if(slide > 0) {
  1303. /* slide screen to keep cursor where we expect it -
  1304. * tscrollup would work here, but we can optimize to
  1305. * memmove because we're freeing the earlier lines */
  1306. for(/* i = 0 */; i < slide; i++) {
  1307. free(term.line[i]);
  1308. free(term.alt[i]);
  1309. }
  1310. memmove(term.line, term.line + slide, row * sizeof(Line));
  1311. memmove(term.alt, term.alt + slide, row * sizeof(Line));
  1312. }
  1313. for(i += row; i < term.row; i++) {
  1314. free(term.line[i]);
  1315. free(term.alt[i]);
  1316. }
  1317. /* resize to new height */
  1318. term.line = realloc(term.line, row * sizeof(Line));
  1319. term.alt = realloc(term.alt, row * sizeof(Line));
  1320. /* resize each row to new width, zero-pad if needed */
  1321. for(i = 0; i < minrow; i++) {
  1322. term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
  1323. term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
  1324. for(x = mincol; x < col; x++) {
  1325. term.line[i][x].state = 0;
  1326. term.alt[i][x].state = 0;
  1327. }
  1328. }
  1329. /* allocate any new rows */
  1330. for(/* i == minrow */; i < row; i++) {
  1331. term.line[i] = calloc(col, sizeof(Glyph));
  1332. term.alt [i] = calloc(col, sizeof(Glyph));
  1333. }
  1334. /* update terminal size */
  1335. term.col = col, term.row = row;
  1336. /* make use of the LIMIT in tmoveto */
  1337. tmoveto(term.c.x, term.c.y);
  1338. /* reset scrolling region */
  1339. tsetscroll(0, row-1);
  1340. return (slide > 0);
  1341. }
  1342. void
  1343. xresize(int col, int row) {
  1344. Pixmap newbuf;
  1345. int oldw, oldh;
  1346. oldw = xw.bufw;
  1347. oldh = xw.bufh;
  1348. xw.bufw = MAX(1, col * xw.cw);
  1349. xw.bufh = MAX(1, row * xw.ch);
  1350. newbuf = XCreatePixmap(xw.dpy, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dpy, xw.scr));
  1351. XCopyArea(xw.dpy, xw.buf, newbuf, dc.gc, 0, 0, xw.bufw, xw.bufh, 0, 0);
  1352. XFreePixmap(xw.dpy, xw.buf);
  1353. XSetForeground(xw.dpy, dc.gc, dc.col[DefaultBG]);
  1354. if(xw.bufw > oldw)
  1355. XFillRectangle(xw.dpy, newbuf, dc.gc, oldw, 0,
  1356. xw.bufw-oldw, MIN(xw.bufh, oldh));
  1357. else if(xw.bufw < oldw && (BORDER > 0 || xw.w > xw.bufw))
  1358. XClearArea(xw.dpy, xw.win, BORDER+xw.bufw, BORDER,
  1359. xw.w-xw.bufh-BORDER, BORDER+MIN(xw.bufh, oldh),
  1360. False);
  1361. if(xw.bufh > oldh)
  1362. XFillRectangle(xw.dpy, newbuf, dc.gc, 0, oldh,
  1363. xw.bufw, xw.bufh-oldh);
  1364. else if(xw.bufh < oldh && (BORDER > 0 || xw.h > xw.bufh))
  1365. XClearArea(xw.dpy, xw.win, BORDER, BORDER+xw.bufh,
  1366. xw.w-2*BORDER, xw.h-xw.bufh-BORDER,
  1367. False);
  1368. xw.buf = newbuf;
  1369. }
  1370. void
  1371. xloadcols(void) {
  1372. int i, r, g, b;
  1373. XColor color;
  1374. unsigned long white = WhitePixel(xw.dpy, xw.scr);
  1375. for(i = 0; i < 16; i++) {
  1376. if(!XAllocNamedColor(xw.dpy, xw.cmap, colorname[i], &color, &color)) {
  1377. dc.col[i] = white;
  1378. fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
  1379. } else
  1380. dc.col[i] = color.pixel;
  1381. }
  1382. /* same colors as xterm */
  1383. for(r = 0; r < 6; r++)
  1384. for(g = 0; g < 6; g++)
  1385. for(b = 0; b < 6; b++) {
  1386. color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
  1387. color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
  1388. color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
  1389. if(!XAllocColor(xw.dpy, xw.cmap, &color)) {
  1390. dc.col[i] = white;
  1391. fprintf(stderr, "Could not allocate color %d\n", i);
  1392. } else
  1393. dc.col[i] = color.pixel;
  1394. i++;
  1395. }
  1396. for(r = 0; r < 24; r++, i++) {
  1397. color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
  1398. if (!XAllocColor(xw.dpy, xw.cmap, &color)) {
  1399. dc.col[i] = white;
  1400. fprintf(stderr, "Could not allocate color %d\n", i);
  1401. } else
  1402. dc.col[i] = color.pixel;
  1403. }
  1404. }
  1405. void
  1406. xclear(int x1, int y1, int x2, int y2) {
  1407. XSetForeground(xw.dpy, dc.gc, dc.col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG]);
  1408. XFillRectangle(xw.dpy, xw.buf, dc.gc,
  1409. x1 * xw.cw, y1 * xw.ch,
  1410. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
  1411. }
  1412. void
  1413. xhints(void) {
  1414. XClassHint class = {opt_class ? opt_class : TNAME, TNAME};
  1415. XWMHints wm = {.flags = InputHint, .input = 1};
  1416. XSizeHints size = {
  1417. .flags = PSize | PResizeInc | PBaseSize,
  1418. .height = xw.h,
  1419. .width = xw.w,
  1420. .height_inc = xw.ch,
  1421. .width_inc = xw.cw,
  1422. .base_height = 2*BORDER,
  1423. .base_width = 2*BORDER,
  1424. };
  1425. XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
  1426. }
  1427. XFontSet
  1428. xinitfont(char *fontstr) {
  1429. XFontSet set;
  1430. char *def, **missing;
  1431. int n;
  1432. missing = NULL;
  1433. set = XCreateFontSet(xw.dpy, fontstr, &missing, &n, &def);
  1434. if(missing) {
  1435. while(n--)
  1436. fprintf(stderr, "st: missing fontset: %s\n", missing[n]);
  1437. XFreeStringList(missing);
  1438. }
  1439. return set;
  1440. }
  1441. void
  1442. xgetfontinfo(XFontSet set, int *ascent, int *descent, short *lbearing, short *rbearing) {
  1443. XFontStruct **xfonts;
  1444. char **font_names;
  1445. int i, n;
  1446. *ascent = *descent = *lbearing = *rbearing = 0;
  1447. n = XFontsOfFontSet(set, &xfonts, &font_names);
  1448. for(i = 0; i < n; i++) {
  1449. *ascent = MAX(*ascent, (*xfonts)->ascent);
  1450. *descent = MAX(*descent, (*xfonts)->descent);
  1451. *lbearing = MAX(*lbearing, (*xfonts)->min_bounds.lbearing);
  1452. *rbearing = MAX(*rbearing, (*xfonts)->max_bounds.rbearing);
  1453. xfonts++;
  1454. }
  1455. }
  1456. void
  1457. initfonts(char *fontstr, char *bfontstr) {
  1458. if((dc.font.set = xinitfont(fontstr)) == NULL ||
  1459. (dc.bfont.set = xinitfont(bfontstr)) == NULL)
  1460. die("Can't load font %s\n", dc.font.set ? BOLDFONT : FONT);
  1461. xgetfontinfo(dc.font.set, &dc.font.ascent, &dc.font.descent,
  1462. &dc.font.lbearing, &dc.font.rbearing);
  1463. xgetfontinfo(dc.bfont.set, &dc.bfont.ascent, &dc.bfont.descent,
  1464. &dc.bfont.lbearing, &dc.bfont.rbearing);
  1465. }
  1466. void
  1467. xinit(void) {
  1468. XSetWindowAttributes attrs;
  1469. Cursor cursor;
  1470. if(!(xw.dpy = XOpenDisplay(NULL)))
  1471. die("Can't open display\n");
  1472. xw.scr = XDefaultScreen(xw.dpy);
  1473. /* font */
  1474. initfonts(FONT, BOLDFONT);
  1475. /* XXX: Assuming same size for bold font */
  1476. xw.cw = dc.font.rbearing - dc.font.lbearing;
  1477. xw.ch = dc.font.ascent + dc.font.descent;
  1478. /* colors */
  1479. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  1480. xloadcols();
  1481. /* window - default size */
  1482. xw.bufh = 24 * xw.ch;
  1483. xw.bufw = 80 * xw.cw;
  1484. xw.h = xw.bufh + 2*BORDER;
  1485. xw.w = xw.bufw + 2*BORDER;
  1486. attrs.background_pixel = dc.col[DefaultBG];
  1487. attrs.border_pixel = dc.col[DefaultBG];
  1488. attrs.bit_gravity = NorthWestGravity;
  1489. attrs.event_mask = FocusChangeMask | KeyPressMask
  1490. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  1491. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
  1492. attrs.colormap = xw.cmap;
  1493. xw.win = XCreateWindow(xw.dpy, XRootWindow(xw.dpy, xw.scr), 0, 0,
  1494. xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
  1495. XDefaultVisual(xw.dpy, xw.scr),
  1496. CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
  1497. | CWColormap,
  1498. &attrs);
  1499. xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dpy, xw.scr));
  1500. /* input methods */
  1501. xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
  1502. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  1503. | XIMStatusNothing, XNClientWindow, xw.win,
  1504. XNFocusWindow, xw.win, NULL);
  1505. /* gc */
  1506. dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
  1507. /* white cursor, black outline */
  1508. cursor = XCreateFontCursor(xw.dpy, XC_xterm);
  1509. XDefineCursor(xw.dpy, xw.win, cursor);
  1510. XRecolorCursor(xw.dpy, cursor,
  1511. &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
  1512. &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
  1513. XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
  1514. XMapWindow(xw.dpy, xw.win);
  1515. xhints();
  1516. XSync(xw.dpy, 0);
  1517. }
  1518. void
  1519. xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
  1520. unsigned long xfg = dc.col[base.fg], xbg = dc.col[base.bg], temp;
  1521. int winx = x*xw.cw, winy = y*xw.ch + dc.font.ascent, width = charlen*xw.cw;
  1522. int i;
  1523. /* only switch default fg/bg if term is in RV mode */
  1524. if(IS_SET(MODE_REVERSE)) {
  1525. if(base.fg == DefaultFG)
  1526. xfg = dc.col[DefaultBG];
  1527. if(base.bg == DefaultBG)
  1528. xbg = dc.col[DefaultFG];
  1529. }
  1530. if(base.mode & ATTR_REVERSE)
  1531. temp = xfg, xfg = xbg, xbg = temp;
  1532. XSetBackground(xw.dpy, dc.gc, xbg);
  1533. XSetForeground(xw.dpy, dc.gc, xfg);
  1534. if(base.mode & ATTR_GFX) {
  1535. for(i = 0; i < bytelen; i++) {
  1536. char c = gfx[(unsigned int)s[i] % 256];
  1537. if(c)
  1538. s[i] = c;
  1539. else if(s[i] > 0x5f)
  1540. s[i] -= 0x5f;
  1541. }
  1542. }
  1543. XmbDrawImageString(xw.dpy, xw.buf, base.mode & ATTR_BOLD ? dc.bfont.set : dc.font.set,
  1544. dc.gc, winx, winy, s, bytelen);
  1545. if(base.mode & ATTR_UNDERLINE)
  1546. XDrawLine(xw.dpy, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
  1547. }
  1548. void
  1549. xdrawcursor(void) {
  1550. static int oldx = 0;
  1551. static int oldy = 0;
  1552. int sl;
  1553. Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
  1554. LIMIT(oldx, 0, term.col-1);
  1555. LIMIT(oldy, 0, term.row-1);
  1556. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  1557. memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
  1558. /* remove the old cursor */
  1559. if(term.line[oldy][oldx].state & GLYPH_SET) {
  1560. sl = utf8size(term.line[oldy][oldx].c);
  1561. xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1, sl);
  1562. } else
  1563. xclear(oldx, oldy, oldx, oldy);
  1564. /* draw the new one */
  1565. if(!(term.c.state & CURSOR_HIDE) && (xw.state & WIN_FOCUSED)) {
  1566. sl = utf8size(g.c);
  1567. if(IS_SET(MODE_REVERSE))
  1568. g.mode |= ATTR_REVERSE, g.fg = DefaultCS, g.bg = DefaultFG;
  1569. xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
  1570. oldx = term.c.x, oldy = term.c.y;
  1571. }
  1572. }
  1573. void
  1574. draw() {
  1575. drawregion(0, 0, term.col, term.row);
  1576. }
  1577. void
  1578. drawregion(int x1, int y1, int x2, int y2) {
  1579. int ic, ib, x, y, ox, sl;
  1580. Glyph base, new;
  1581. char buf[DRAW_BUF_SIZ];
  1582. if(!(xw.state & WIN_VISIBLE))
  1583. return;
  1584. xclear(x1, y1, x2-1, y2-1);
  1585. for(y = y1; y < y2; y++) {
  1586. base = term.line[y][0];
  1587. ic = ib = ox = 0;
  1588. for(x = x1; x < x2; x++) {
  1589. new = term.line[y][x];
  1590. if(sel.bx != -1 && *(new.c) && selected(x, y))
  1591. new.mode ^= ATTR_REVERSE;
  1592. if(ib > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
  1593. ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
  1594. xdraws(buf, base, ox, y, ic, ib);
  1595. ic = ib = 0;
  1596. }
  1597. if(new.state & GLYPH_SET) {
  1598. if(ib == 0) {
  1599. ox = x;
  1600. base = new;
  1601. }
  1602. sl = utf8size(new.c);
  1603. memcpy(buf+ib, new.c, sl);
  1604. ib += sl;
  1605. ++ic;
  1606. }
  1607. }
  1608. if(ib > 0)
  1609. xdraws(buf, base, ox, y, ic, ib);
  1610. }
  1611. xdrawcursor();
  1612. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1613. }
  1614. void
  1615. expose(XEvent *ev) {
  1616. XExposeEvent *e = &ev->xexpose;
  1617. if(xw.state & WIN_REDRAW) {
  1618. if(!e->count) {
  1619. xw.state &= ~WIN_REDRAW;
  1620. draw();
  1621. }
  1622. } else
  1623. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, e->x-BORDER, e->y-BORDER,
  1624. e->width, e->height, e->x, e->y);
  1625. }
  1626. void
  1627. visibility(XEvent *ev) {
  1628. XVisibilityEvent *e = &ev->xvisibility;
  1629. if(e->state == VisibilityFullyObscured)
  1630. xw.state &= ~WIN_VISIBLE;
  1631. else if(!(xw.state & WIN_VISIBLE))
  1632. /* need a full redraw for next Expose, not just a buf copy */
  1633. xw.state |= WIN_VISIBLE | WIN_REDRAW;
  1634. }
  1635. void
  1636. unmap(XEvent *ev) {
  1637. xw.state &= ~WIN_VISIBLE;
  1638. }
  1639. void
  1640. xseturgency(int add) {
  1641. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  1642. h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
  1643. XSetWMHints(xw.dpy, xw.win, h);
  1644. XFree(h);
  1645. }
  1646. void
  1647. focus(XEvent *ev) {
  1648. if(ev->type == FocusIn) {
  1649. xw.state |= WIN_FOCUSED;
  1650. xseturgency(0);
  1651. } else
  1652. xw.state &= ~WIN_FOCUSED;
  1653. draw();
  1654. }
  1655. char*
  1656. kmap(KeySym k, unsigned int state) {
  1657. int i;
  1658. for(i = 0; i < LEN(key); i++)
  1659. if(key[i].k == k && (key[i].mask == 0 || key[i].mask & state))
  1660. return (char*)key[i].s;
  1661. return NULL;
  1662. }
  1663. void
  1664. kpress(XEvent *ev) {
  1665. XKeyEvent *e = &ev->xkey;
  1666. KeySym ksym;
  1667. char buf[32];
  1668. char *customkey;
  1669. int len;
  1670. int meta;
  1671. int shift;
  1672. Status status;
  1673. meta = e->state & Mod1Mask;
  1674. shift = e->state & ShiftMask;
  1675. len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
  1676. /* 1. custom keys from config.h */
  1677. if((customkey = kmap(ksym, e->state)))
  1678. ttywrite(customkey, strlen(customkey));
  1679. /* 2. hardcoded (overrides X lookup) */
  1680. else
  1681. switch(ksym) {
  1682. case XK_Up:
  1683. case XK_Down:
  1684. case XK_Left:
  1685. case XK_Right:
  1686. /* XXX: shift up/down doesn't work */
  1687. sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', (shift ? "dacb":"DACB")[ksym - XK_Left]);
  1688. ttywrite(buf, 3);
  1689. break;
  1690. case XK_Insert:
  1691. if(shift)
  1692. selpaste();
  1693. break;
  1694. case XK_Return:
  1695. if(IS_SET(MODE_CRLF))
  1696. ttywrite("\r\n", 2);
  1697. else
  1698. ttywrite("\r", 1);
  1699. break;
  1700. /* 3. X lookup */
  1701. default:
  1702. if(len > 0) {
  1703. if(meta && len == 1)
  1704. ttywrite("\033", 1);
  1705. ttywrite(buf, len);
  1706. }
  1707. break;
  1708. }
  1709. }
  1710. void
  1711. resize(XEvent *e) {
  1712. int col, row;
  1713. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  1714. return;
  1715. xw.w = e->xconfigure.width;
  1716. xw.h = e->xconfigure.height;
  1717. col = (xw.w - 2*BORDER) / xw.cw;
  1718. row = (xw.h - 2*BORDER) / xw.ch;
  1719. if(col == term.col && row == term.row)
  1720. return;
  1721. if(tresize(col, row))
  1722. draw();
  1723. ttyresize(col, row);
  1724. xresize(col, row);
  1725. }
  1726. void
  1727. run(void) {
  1728. XEvent ev;
  1729. fd_set rfd;
  1730. int xfd = XConnectionNumber(xw.dpy);
  1731. for(;;) {
  1732. FD_ZERO(&rfd);
  1733. FD_SET(cmdfd, &rfd);
  1734. FD_SET(xfd, &rfd);
  1735. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
  1736. if(errno == EINTR)
  1737. continue;
  1738. die("select failed: %s\n", SERRNO);
  1739. }
  1740. if(FD_ISSET(cmdfd, &rfd)) {
  1741. ttyread();
  1742. draw();
  1743. }
  1744. while(XPending(xw.dpy)) {
  1745. XNextEvent(xw.dpy, &ev);
  1746. if(XFilterEvent(&ev, xw.win))
  1747. continue;
  1748. if(handler[ev.type])
  1749. (handler[ev.type])(&ev);
  1750. }
  1751. }
  1752. }
  1753. int
  1754. main(int argc, char *argv[]) {
  1755. int i;
  1756. for(i = 1; i < argc; i++) {
  1757. switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
  1758. case 't':
  1759. if(++i < argc) opt_title = argv[i];
  1760. break;
  1761. case 'c':
  1762. if(++i < argc) opt_class = argv[i];
  1763. break;
  1764. case 'e':
  1765. /* eat every remaining arguments */
  1766. if(++i < argc) opt_cmd = &argv[i];
  1767. goto run;
  1768. case 'v':
  1769. default:
  1770. die(USAGE);
  1771. }
  1772. }
  1773. run:
  1774. setlocale(LC_CTYPE, "");
  1775. tnew(80, 24);
  1776. ttynew();
  1777. xinit();
  1778. selinit();
  1779. run();
  1780. return 0;
  1781. }