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

1964 lines
45 KiB

13 years ago
14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
14 years ago
13 years ago
13 years ago
13 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
14 years ago
14 years ago
14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
14 years ago
13 years ago
14 years ago
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
14 years ago
14 years ago
13 years ago
13 years ago
14 years ago
14 years ago
13 years ago
14 years ago
13 years ago
14 years ago
13 years ago
13 years ago
14 years ago
14 years ago
13 years ago
14 years ago
14 years ago
14 years ago
14 years ago
13 years ago
14 years ago
13 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
13 years ago
14 years ago
13 years ago
14 years ago
14 years ago
14 years ago
14 years ago
13 years ago
14 years ago
13 years ago
14 years ago
13 years ago
  1. /* See LICENSE for licence details. */
  2. #define _XOPEN_SOURCE 600
  3. #include <ctype.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <limits.h>
  7. #include <locale.h>
  8. #include <stdarg.h>
  9. #include <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, ofs, rem;
  440. int format;
  441. unsigned char *data;
  442. Atom type;
  443. ofs = 0;
  444. do {
  445. if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
  446. False, AnyPropertyType, &type, &format,
  447. &nitems, &rem, &data)) {
  448. fprintf(stderr, "Clipboard allocation failed\n");
  449. return;
  450. }
  451. ttywrite((const char *) data, nitems * format / 8);
  452. XFree(data);
  453. /* number of 32-bit chunks returned */
  454. ofs += nitems * format / 32;
  455. } while(rem > 0);
  456. }
  457. void
  458. selpaste() {
  459. XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY, xw.win, CurrentTime);
  460. }
  461. void
  462. selrequest(XEvent *e) {
  463. XSelectionRequestEvent *xsre;
  464. XSelectionEvent xev;
  465. Atom xa_targets;
  466. xsre = (XSelectionRequestEvent *) e;
  467. xev.type = SelectionNotify;
  468. xev.requestor = xsre->requestor;
  469. xev.selection = xsre->selection;
  470. xev.target = xsre->target;
  471. xev.time = xsre->time;
  472. /* reject */
  473. xev.property = None;
  474. xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
  475. if(xsre->target == xa_targets) {
  476. /* respond with the supported type */
  477. Atom string = sel.xtarget;
  478. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  479. XA_ATOM, 32, PropModeReplace,
  480. (unsigned char *) &string, 1);
  481. xev.property = xsre->property;
  482. } else if(xsre->target == sel.xtarget) {
  483. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  484. xsre->target, 8, PropModeReplace,
  485. (unsigned char *) sel.clip, strlen(sel.clip));
  486. xev.property = xsre->property;
  487. }
  488. /* all done, send a notification to the listener */
  489. if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
  490. fprintf(stderr, "Error sending SelectionNotify event\n");
  491. }
  492. void
  493. xsetsel(char *str) {
  494. /* register the selection for both the clipboard and the primary */
  495. Atom clipboard;
  496. free(sel.clip);
  497. sel.clip = str;
  498. XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
  499. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  500. XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
  501. XFlush(xw.dpy);
  502. }
  503. void
  504. brelease(XEvent *e) {
  505. if(IS_SET(MODE_MOUSE)) {
  506. mousereport(e);
  507. return;
  508. }
  509. if(e->xbutton.button == Button2)
  510. selpaste();
  511. else if(e->xbutton.button == Button1) {
  512. sel.mode = 0;
  513. getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
  514. if(sel.bx == sel.ex && sel.by == sel.ey) {
  515. struct timeval now;
  516. sel.bx = -1;
  517. gettimeofday(&now, NULL);
  518. if(TIMEDIFF(now, sel.tclick2) <= TRIPLECLICK_TIMEOUT) {
  519. /* triple click on the line */
  520. sel.b.x = sel.bx = 0;
  521. sel.e.x = sel.ex = term.col;
  522. sel.b.y = sel.e.y = sel.ey;
  523. selcopy();
  524. } else if(TIMEDIFF(now, sel.tclick1) <= DOUBLECLICK_TIMEOUT) {
  525. /* double click to select word */
  526. sel.bx = sel.ex;
  527. while(sel.bx > 0 && term.line[sel.ey][sel.bx-1].state & GLYPH_SET &&
  528. term.line[sel.ey][sel.bx-1].c[0] != ' ') sel.bx--;
  529. sel.b.x = sel.bx;
  530. while(sel.ex < term.col-1 && term.line[sel.ey][sel.ex+1].state & GLYPH_SET &&
  531. term.line[sel.ey][sel.ex+1].c[0] != ' ') sel.ex++;
  532. sel.e.x = sel.ex;
  533. sel.b.y = sel.e.y = sel.ey;
  534. selcopy();
  535. }
  536. } else
  537. selcopy();
  538. }
  539. memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
  540. gettimeofday(&sel.tclick1, NULL);
  541. draw();
  542. }
  543. void
  544. bmotion(XEvent *e) {
  545. if(IS_SET(MODE_MOUSE)) {
  546. mousereport(e);
  547. return;
  548. }
  549. if(sel.mode) {
  550. int oldey = sel.ey, oldex = sel.ex;
  551. getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
  552. if(oldey != sel.ey || oldex != sel.ex) {
  553. int starty = MIN(oldey, sel.ey);
  554. int endy = MAX(oldey, sel.ey);
  555. drawregion(0, (starty > 0 ? starty : 0), term.col, (sel.ey < term.row ? endy+1 : term.row));
  556. }
  557. }
  558. }
  559. void
  560. die(const char *errstr, ...) {
  561. va_list ap;
  562. va_start(ap, errstr);
  563. vfprintf(stderr, errstr, ap);
  564. va_end(ap);
  565. exit(EXIT_FAILURE);
  566. }
  567. void
  568. execsh(void) {
  569. char **args;
  570. char *envshell = getenv("SHELL");
  571. DEFAULT(envshell, "sh");
  572. putenv("TERM="TNAME);
  573. args = opt_cmd ? opt_cmd : (char*[]){envshell, "-i", NULL};
  574. execvp(args[0], args);
  575. exit(EXIT_FAILURE);
  576. }
  577. void
  578. sigchld(int a) {
  579. int stat = 0;
  580. if(waitpid(pid, &stat, 0) < 0)
  581. die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
  582. if(WIFEXITED(stat))
  583. exit(WEXITSTATUS(stat));
  584. else
  585. exit(EXIT_FAILURE);
  586. }
  587. void
  588. ttynew(void) {
  589. int m, s;
  590. /* seems to work fine on linux, openbsd and freebsd */
  591. struct winsize w = {term.row, term.col, 0, 0};
  592. if(openpty(&m, &s, NULL, NULL, &w) < 0)
  593. die("openpty failed: %s\n", SERRNO);
  594. switch(pid = fork()) {
  595. case -1:
  596. die("fork failed\n");
  597. break;
  598. case 0:
  599. setsid(); /* create a new process group */
  600. dup2(s, STDIN_FILENO);
  601. dup2(s, STDOUT_FILENO);
  602. dup2(s, STDERR_FILENO);
  603. if(ioctl(s, TIOCSCTTY, NULL) < 0)
  604. die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
  605. close(s);
  606. close(m);
  607. execsh();
  608. break;
  609. default:
  610. close(s);
  611. cmdfd = m;
  612. signal(SIGCHLD, sigchld);
  613. }
  614. }
  615. void
  616. dump(char c) {
  617. static int col;
  618. fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
  619. if(++col % 10 == 0)
  620. fprintf(stderr, "\n");
  621. }
  622. void
  623. ttyread(void) {
  624. static char buf[BUFSIZ];
  625. static int buflen = 0;
  626. char *ptr;
  627. char s[UTF_SIZ];
  628. int charsize; /* size of utf8 char in bytes */
  629. long utf8c;
  630. int ret;
  631. /* append read bytes to unprocessed bytes */
  632. if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
  633. die("Couldn't read from shell: %s\n", SERRNO);
  634. /* process every complete utf8 char */
  635. buflen += ret;
  636. ptr = buf;
  637. while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
  638. charsize = utf8decode(ptr, &utf8c);
  639. utf8encode(&utf8c, s);
  640. tputc(s);
  641. ptr += charsize;
  642. buflen -= charsize;
  643. }
  644. /* keep any uncomplete utf8 char for the next call */
  645. memmove(buf, ptr, buflen);
  646. }
  647. void
  648. ttywrite(const char *s, size_t n) {
  649. if(write(cmdfd, s, n) == -1)
  650. die("write error on tty: %s\n", SERRNO);
  651. }
  652. void
  653. ttyresize(int x, int y) {
  654. struct winsize w;
  655. w.ws_row = term.row;
  656. w.ws_col = term.col;
  657. w.ws_xpixel = w.ws_ypixel = 0;
  658. if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  659. fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
  660. }
  661. void
  662. tcursor(int mode) {
  663. static TCursor c;
  664. if(mode == CURSOR_SAVE)
  665. c = term.c;
  666. else if(mode == CURSOR_LOAD)
  667. term.c = c, tmoveto(c.x, c.y);
  668. }
  669. void
  670. treset(void) {
  671. term.c = (TCursor){{
  672. .mode = ATTR_NULL,
  673. .fg = DefaultFG,
  674. .bg = DefaultBG
  675. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  676. term.top = 0, term.bot = term.row - 1;
  677. term.mode = MODE_WRAP;
  678. tclearregion(0, 0, term.col-1, term.row-1);
  679. }
  680. void
  681. tnew(int col, int row) {
  682. /* set screen size */
  683. term.row = row, term.col = col;
  684. term.line = malloc(term.row * sizeof(Line));
  685. term.alt = malloc(term.row * sizeof(Line));
  686. for(row = 0; row < term.row; row++) {
  687. term.line[row] = malloc(term.col * sizeof(Glyph));
  688. term.alt [row] = malloc(term.col * sizeof(Glyph));
  689. }
  690. /* setup screen */
  691. treset();
  692. }
  693. void
  694. tswapscreen(void) {
  695. Line* tmp = term.line;
  696. term.line = term.alt;
  697. term.alt = tmp;
  698. term.mode ^= MODE_ALTSCREEN;
  699. }
  700. void
  701. tscrolldown(int orig, int n) {
  702. int i;
  703. Line temp;
  704. LIMIT(n, 0, term.bot-orig+1);
  705. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  706. for(i = term.bot; i >= orig+n; i--) {
  707. temp = term.line[i];
  708. term.line[i] = term.line[i-n];
  709. term.line[i-n] = temp;
  710. }
  711. }
  712. void
  713. tscrollup(int orig, int n) {
  714. int i;
  715. Line temp;
  716. LIMIT(n, 0, term.bot-orig+1);
  717. tclearregion(0, orig, term.col-1, orig+n-1);
  718. for(i = orig; i <= term.bot-n; i++) {
  719. temp = term.line[i];
  720. term.line[i] = term.line[i+n];
  721. term.line[i+n] = temp;
  722. }
  723. }
  724. void
  725. tnewline(int first_col) {
  726. int y = term.c.y;
  727. if(y == term.bot)
  728. tscrollup(term.top, 1);
  729. else
  730. y++;
  731. tmoveto(first_col ? 0 : term.c.x, y);
  732. }
  733. void
  734. csiparse(void) {
  735. /* int noarg = 1; */
  736. char *p = escseq.buf;
  737. escseq.narg = 0;
  738. if(*p == '?')
  739. escseq.priv = 1, p++;
  740. while(p < escseq.buf+escseq.len) {
  741. while(isdigit(*p)) {
  742. escseq.arg[escseq.narg] *= 10;
  743. escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
  744. }
  745. if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
  746. escseq.narg++, p++;
  747. else {
  748. escseq.mode = *p;
  749. escseq.narg++;
  750. return;
  751. }
  752. }
  753. }
  754. void
  755. tmoveto(int x, int y) {
  756. LIMIT(x, 0, term.col-1);
  757. LIMIT(y, 0, term.row-1);
  758. term.c.state &= ~CURSOR_WRAPNEXT;
  759. term.c.x = x;
  760. term.c.y = y;
  761. }
  762. void
  763. tsetchar(char *c) {
  764. term.line[term.c.y][term.c.x] = term.c.attr;
  765. memcpy(term.line[term.c.y][term.c.x].c, c, UTF_SIZ);
  766. term.line[term.c.y][term.c.x].state |= GLYPH_SET;
  767. }
  768. void
  769. tclearregion(int x1, int y1, int x2, int y2) {
  770. int x, y, temp;
  771. if(x1 > x2)
  772. temp = x1, x1 = x2, x2 = temp;
  773. if(y1 > y2)
  774. temp = y1, y1 = y2, y2 = temp;
  775. LIMIT(x1, 0, term.col-1);
  776. LIMIT(x2, 0, term.col-1);
  777. LIMIT(y1, 0, term.row-1);
  778. LIMIT(y2, 0, term.row-1);
  779. for(y = y1; y <= y2; y++)
  780. for(x = x1; x <= x2; x++)
  781. term.line[y][x].state = 0;
  782. }
  783. void
  784. tdeletechar(int n) {
  785. int src = term.c.x + n;
  786. int dst = term.c.x;
  787. int size = term.col - src;
  788. if(src >= term.col) {
  789. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  790. return;
  791. }
  792. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  793. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  794. }
  795. void
  796. tinsertblank(int n) {
  797. int src = term.c.x;
  798. int dst = src + n;
  799. int size = term.col - dst;
  800. if(dst >= term.col) {
  801. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  802. return;
  803. }
  804. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  805. tclearregion(src, term.c.y, dst - 1, term.c.y);
  806. }
  807. void
  808. tinsertblankline(int n) {
  809. if(term.c.y < term.top || term.c.y > term.bot)
  810. return;
  811. tscrolldown(term.c.y, n);
  812. }
  813. void
  814. tdeleteline(int n) {
  815. if(term.c.y < term.top || term.c.y > term.bot)
  816. return;
  817. tscrollup(term.c.y, n);
  818. }
  819. void
  820. tsetattr(int *attr, int l) {
  821. int i;
  822. for(i = 0; i < l; i++) {
  823. switch(attr[i]) {
  824. case 0:
  825. term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
  826. term.c.attr.fg = DefaultFG;
  827. term.c.attr.bg = DefaultBG;
  828. break;
  829. case 1:
  830. term.c.attr.mode |= ATTR_BOLD;
  831. break;
  832. case 4:
  833. term.c.attr.mode |= ATTR_UNDERLINE;
  834. break;
  835. case 7:
  836. term.c.attr.mode |= ATTR_REVERSE;
  837. break;
  838. case 22:
  839. term.c.attr.mode &= ~ATTR_BOLD;
  840. break;
  841. case 24:
  842. term.c.attr.mode &= ~ATTR_UNDERLINE;
  843. break;
  844. case 27:
  845. term.c.attr.mode &= ~ATTR_REVERSE;
  846. break;
  847. case 38:
  848. if(i + 2 < l && attr[i + 1] == 5) {
  849. i += 2;
  850. if(BETWEEN(attr[i], 0, 255))
  851. term.c.attr.fg = attr[i];
  852. else
  853. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
  854. }
  855. else
  856. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  857. break;
  858. case 39:
  859. term.c.attr.fg = DefaultFG;
  860. break;
  861. case 48:
  862. if(i + 2 < l && attr[i + 1] == 5) {
  863. i += 2;
  864. if(BETWEEN(attr[i], 0, 255))
  865. term.c.attr.bg = attr[i];
  866. else
  867. fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
  868. }
  869. else
  870. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  871. break;
  872. case 49:
  873. term.c.attr.bg = DefaultBG;
  874. break;
  875. default:
  876. if(BETWEEN(attr[i], 30, 37))
  877. term.c.attr.fg = attr[i] - 30;
  878. else if(BETWEEN(attr[i], 40, 47))
  879. term.c.attr.bg = attr[i] - 40;
  880. else if(BETWEEN(attr[i], 90, 97))
  881. term.c.attr.fg = attr[i] - 90 + 8;
  882. else if(BETWEEN(attr[i], 100, 107))
  883. term.c.attr.fg = attr[i] - 100 + 8;
  884. else
  885. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]), csidump();
  886. break;
  887. }
  888. }
  889. }
  890. void
  891. tsetscroll(int t, int b) {
  892. int temp;
  893. LIMIT(t, 0, term.row-1);
  894. LIMIT(b, 0, term.row-1);
  895. if(t > b) {
  896. temp = t;
  897. t = b;
  898. b = temp;
  899. }
  900. term.top = t;
  901. term.bot = b;
  902. }
  903. void
  904. csihandle(void) {
  905. switch(escseq.mode) {
  906. default:
  907. unknown:
  908. fprintf(stderr, "erresc: unknown csi ");
  909. csidump();
  910. /* die(""); */
  911. break;
  912. case '@': /* ICH -- Insert <n> blank char */
  913. DEFAULT(escseq.arg[0], 1);
  914. tinsertblank(escseq.arg[0]);
  915. break;
  916. case 'A': /* CUU -- Cursor <n> Up */
  917. case 'e':
  918. DEFAULT(escseq.arg[0], 1);
  919. tmoveto(term.c.x, term.c.y-escseq.arg[0]);
  920. break;
  921. case 'B': /* CUD -- Cursor <n> Down */
  922. DEFAULT(escseq.arg[0], 1);
  923. tmoveto(term.c.x, term.c.y+escseq.arg[0]);
  924. break;
  925. case 'C': /* CUF -- Cursor <n> Forward */
  926. case 'a':
  927. DEFAULT(escseq.arg[0], 1);
  928. tmoveto(term.c.x+escseq.arg[0], term.c.y);
  929. break;
  930. case 'D': /* CUB -- Cursor <n> Backward */
  931. DEFAULT(escseq.arg[0], 1);
  932. tmoveto(term.c.x-escseq.arg[0], term.c.y);
  933. break;
  934. case 'E': /* CNL -- Cursor <n> Down and first col */
  935. DEFAULT(escseq.arg[0], 1);
  936. tmoveto(0, term.c.y+escseq.arg[0]);
  937. break;
  938. case 'F': /* CPL -- Cursor <n> Up and first col */
  939. DEFAULT(escseq.arg[0], 1);
  940. tmoveto(0, term.c.y-escseq.arg[0]);
  941. break;
  942. case 'G': /* CHA -- Move to <col> */
  943. case '`': /* XXX: HPA -- same? */
  944. DEFAULT(escseq.arg[0], 1);
  945. tmoveto(escseq.arg[0]-1, term.c.y);
  946. break;
  947. case 'H': /* CUP -- Move to <row> <col> */
  948. case 'f': /* XXX: HVP -- same? */
  949. DEFAULT(escseq.arg[0], 1);
  950. DEFAULT(escseq.arg[1], 1);
  951. tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
  952. break;
  953. /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
  954. case 'J': /* ED -- Clear screen */
  955. switch(escseq.arg[0]) {
  956. case 0: /* below */
  957. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  958. if(term.c.y < term.row-1)
  959. tclearregion(0, term.c.y+1, term.col-1, term.row-1);
  960. break;
  961. case 1: /* above */
  962. if(term.c.y > 1)
  963. tclearregion(0, 0, term.col-1, term.c.y-1);
  964. tclearregion(0, term.c.y, term.c.x, term.c.y);
  965. break;
  966. case 2: /* all */
  967. tclearregion(0, 0, term.col-1, term.row-1);
  968. break;
  969. default:
  970. goto unknown;
  971. }
  972. break;
  973. case 'K': /* EL -- Clear line */
  974. switch(escseq.arg[0]) {
  975. case 0: /* right */
  976. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  977. break;
  978. case 1: /* left */
  979. tclearregion(0, term.c.y, term.c.x, term.c.y);
  980. break;
  981. case 2: /* all */
  982. tclearregion(0, term.c.y, term.col-1, term.c.y);
  983. break;
  984. }
  985. break;
  986. case 'S': /* SU -- Scroll <n> line up */
  987. DEFAULT(escseq.arg[0], 1);
  988. tscrollup(term.top, escseq.arg[0]);
  989. break;
  990. case 'T': /* SD -- Scroll <n> line down */
  991. DEFAULT(escseq.arg[0], 1);
  992. tscrolldown(term.top, escseq.arg[0]);
  993. break;
  994. case 'L': /* IL -- Insert <n> blank lines */
  995. DEFAULT(escseq.arg[0], 1);
  996. tinsertblankline(escseq.arg[0]);
  997. break;
  998. case 'l': /* RM -- Reset Mode */
  999. if(escseq.priv) {
  1000. switch(escseq.arg[0]) {
  1001. case 1:
  1002. term.mode &= ~MODE_APPKEYPAD;
  1003. break;
  1004. case 5: /* DECSCNM -- Remove reverse video */
  1005. if(IS_SET(MODE_REVERSE)) {
  1006. term.mode &= ~MODE_REVERSE;
  1007. draw();
  1008. }
  1009. break;
  1010. case 7:
  1011. term.mode &= ~MODE_WRAP;
  1012. break;
  1013. case 12: /* att610 -- Stop blinking cursor (IGNORED) */
  1014. break;
  1015. case 20:
  1016. term.mode &= ~MODE_CRLF;
  1017. break;
  1018. case 25:
  1019. term.c.state |= CURSOR_HIDE;
  1020. break;
  1021. case 1000: /* disable X11 xterm mouse reporting */
  1022. term.mode &= ~MODE_MOUSEBTN;
  1023. break;
  1024. case 1002:
  1025. term.mode &= ~MODE_MOUSEMOTION;
  1026. break;
  1027. case 1049: /* = 1047 and 1048 */
  1028. case 47:
  1029. case 1047:
  1030. if(IS_SET(MODE_ALTSCREEN)) {
  1031. tclearregion(0, 0, term.col-1, term.row-1);
  1032. tswapscreen();
  1033. }
  1034. if(escseq.arg[0] != 1049)
  1035. break;
  1036. case 1048:
  1037. tcursor(CURSOR_LOAD);
  1038. break;
  1039. default:
  1040. goto unknown;
  1041. }
  1042. } else {
  1043. switch(escseq.arg[0]) {
  1044. case 4:
  1045. term.mode &= ~MODE_INSERT;
  1046. break;
  1047. default:
  1048. goto unknown;
  1049. }
  1050. }
  1051. break;
  1052. case 'M': /* DL -- Delete <n> lines */
  1053. DEFAULT(escseq.arg[0], 1);
  1054. tdeleteline(escseq.arg[0]);
  1055. break;
  1056. case 'X': /* ECH -- Erase <n> char */
  1057. DEFAULT(escseq.arg[0], 1);
  1058. tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
  1059. break;
  1060. case 'P': /* DCH -- Delete <n> char */
  1061. DEFAULT(escseq.arg[0], 1);
  1062. tdeletechar(escseq.arg[0]);
  1063. break;
  1064. /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
  1065. case 'd': /* VPA -- Move to <row> */
  1066. DEFAULT(escseq.arg[0], 1);
  1067. tmoveto(term.c.x, escseq.arg[0]-1);
  1068. break;
  1069. case 'h': /* SM -- Set terminal mode */
  1070. if(escseq.priv) {
  1071. switch(escseq.arg[0]) {
  1072. case 1:
  1073. term.mode |= MODE_APPKEYPAD;
  1074. break;
  1075. case 5: /* DECSCNM -- Reverve video */
  1076. if(!IS_SET(MODE_REVERSE)) {
  1077. term.mode |= MODE_REVERSE;
  1078. draw();
  1079. }
  1080. break;
  1081. case 7:
  1082. term.mode |= MODE_WRAP;
  1083. break;
  1084. case 20:
  1085. term.mode |= MODE_CRLF;
  1086. break;
  1087. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1088. /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
  1089. if(escseq.narg > 1 && escseq.arg[1] != 25)
  1090. break;
  1091. case 25:
  1092. term.c.state &= ~CURSOR_HIDE;
  1093. break;
  1094. case 1000: /* 1000,1002: enable xterm mouse report */
  1095. term.mode |= MODE_MOUSEBTN;
  1096. break;
  1097. case 1002:
  1098. term.mode |= MODE_MOUSEMOTION;
  1099. break;
  1100. case 1049: /* = 1047 and 1048 */
  1101. case 47:
  1102. case 1047:
  1103. if(IS_SET(MODE_ALTSCREEN))
  1104. tclearregion(0, 0, term.col-1, term.row-1);
  1105. else
  1106. tswapscreen();
  1107. if(escseq.arg[0] != 1049)
  1108. break;
  1109. case 1048:
  1110. tcursor(CURSOR_SAVE);
  1111. break;
  1112. default: goto unknown;
  1113. }
  1114. } else {
  1115. switch(escseq.arg[0]) {
  1116. case 4:
  1117. term.mode |= MODE_INSERT;
  1118. break;
  1119. default: goto unknown;
  1120. }
  1121. };
  1122. break;
  1123. case 'm': /* SGR -- Terminal attribute (color) */
  1124. tsetattr(escseq.arg, escseq.narg);
  1125. break;
  1126. case 'r': /* DECSTBM -- Set Scrolling Region */
  1127. if(escseq.priv)
  1128. goto unknown;
  1129. else {
  1130. DEFAULT(escseq.arg[0], 1);
  1131. DEFAULT(escseq.arg[1], term.row);
  1132. tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
  1133. tmoveto(0, 0);
  1134. }
  1135. break;
  1136. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  1137. tcursor(CURSOR_SAVE);
  1138. break;
  1139. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  1140. tcursor(CURSOR_LOAD);
  1141. break;
  1142. }
  1143. }
  1144. void
  1145. csidump(void) {
  1146. int i;
  1147. printf("ESC [ %s", escseq.priv ? "? " : "");
  1148. if(escseq.narg)
  1149. for(i = 0; i < escseq.narg; i++)
  1150. printf("%d ", escseq.arg[i]);
  1151. if(escseq.mode)
  1152. putchar(escseq.mode);
  1153. putchar('\n');
  1154. }
  1155. void
  1156. csireset(void) {
  1157. memset(&escseq, 0, sizeof(escseq));
  1158. }
  1159. void
  1160. tputtab(void) {
  1161. int space = TAB - term.c.x % TAB;
  1162. tmoveto(term.c.x + space, term.c.y);
  1163. }
  1164. void
  1165. tputc(char *c) {
  1166. char ascii = *c;
  1167. if(term.esc & ESC_START) {
  1168. if(term.esc & ESC_CSI) {
  1169. escseq.buf[escseq.len++] = ascii;
  1170. if(BETWEEN(ascii, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
  1171. term.esc = 0;
  1172. csiparse(), csihandle();
  1173. }
  1174. /* TODO: handle other OSC */
  1175. } else if(term.esc & ESC_OSC) {
  1176. if(ascii == ';') {
  1177. term.titlelen = 0;
  1178. term.esc = ESC_START | ESC_TITLE;
  1179. }
  1180. } else if(term.esc & ESC_TITLE) {
  1181. if(ascii == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
  1182. term.esc = 0;
  1183. term.title[term.titlelen] = '\0';
  1184. XStoreName(xw.dpy, xw.win, term.title);
  1185. } else {
  1186. term.title[term.titlelen++] = ascii;
  1187. }
  1188. } else if(term.esc & ESC_ALTCHARSET) {
  1189. switch(ascii) {
  1190. case '0': /* Line drawing crap */
  1191. term.c.attr.mode |= ATTR_GFX;
  1192. break;
  1193. case 'B': /* Back to regular text */
  1194. term.c.attr.mode &= ~ATTR_GFX;
  1195. break;
  1196. default:
  1197. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  1198. }
  1199. term.esc = 0;
  1200. } else {
  1201. switch(ascii) {
  1202. case '[':
  1203. term.esc |= ESC_CSI;
  1204. break;
  1205. case ']':
  1206. term.esc |= ESC_OSC;
  1207. break;
  1208. case '(':
  1209. term.esc |= ESC_ALTCHARSET;
  1210. break;
  1211. case 'D': /* IND -- Linefeed */
  1212. if(term.c.y == term.bot)
  1213. tscrollup(term.top, 1);
  1214. else
  1215. tmoveto(term.c.x, term.c.y+1);
  1216. term.esc = 0;
  1217. break;
  1218. case 'E': /* NEL -- Next line */
  1219. tnewline(1); /* always go to first col */
  1220. term.esc = 0;
  1221. break;
  1222. case 'M': /* RI -- Reverse index */
  1223. if(term.c.y == term.top)
  1224. tscrolldown(term.top, 1);
  1225. else
  1226. tmoveto(term.c.x, term.c.y-1);
  1227. term.esc = 0;
  1228. break;
  1229. case 'c': /* RIS -- Reset to inital state */
  1230. treset();
  1231. term.esc = 0;
  1232. break;
  1233. case '=': /* DECPAM -- Application keypad */
  1234. term.mode |= MODE_APPKEYPAD;
  1235. term.esc = 0;
  1236. break;
  1237. case '>': /* DECPNM -- Normal keypad */
  1238. term.mode &= ~MODE_APPKEYPAD;
  1239. term.esc = 0;
  1240. break;
  1241. case '7': /* DECSC -- Save Cursor */
  1242. tcursor(CURSOR_SAVE);
  1243. term.esc = 0;
  1244. break;
  1245. case '8': /* DECRC -- Restore Cursor */
  1246. tcursor(CURSOR_LOAD);
  1247. term.esc = 0;
  1248. break;
  1249. default:
  1250. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  1251. (unsigned char) ascii, isprint(ascii)?ascii:'.');
  1252. term.esc = 0;
  1253. }
  1254. }
  1255. } else {
  1256. switch(ascii) {
  1257. case '\t':
  1258. tputtab();
  1259. break;
  1260. case '\b':
  1261. tmoveto(term.c.x-1, term.c.y);
  1262. break;
  1263. case '\r':
  1264. tmoveto(0, term.c.y);
  1265. break;
  1266. case '\f':
  1267. case '\v':
  1268. case '\n':
  1269. /* go to first col if the mode is set */
  1270. tnewline(IS_SET(MODE_CRLF));
  1271. break;
  1272. case '\a':
  1273. if(!(xw.state & WIN_FOCUSED))
  1274. xseturgency(1);
  1275. break;
  1276. case '\033':
  1277. csireset();
  1278. term.esc = ESC_START;
  1279. break;
  1280. default:
  1281. if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
  1282. tnewline(1); /* always go to first col */
  1283. tsetchar(c);
  1284. if(term.c.x+1 < term.col)
  1285. tmoveto(term.c.x+1, term.c.y);
  1286. else
  1287. term.c.state |= CURSOR_WRAPNEXT;
  1288. }
  1289. }
  1290. }
  1291. int
  1292. tresize(int col, int row) {
  1293. int i, x;
  1294. int minrow = MIN(row, term.row);
  1295. int mincol = MIN(col, term.col);
  1296. int slide = term.c.y - row + 1;
  1297. if(col < 1 || row < 1)
  1298. return 0;
  1299. /* free unneeded rows */
  1300. i = 0;
  1301. if(slide > 0) {
  1302. /* slide screen to keep cursor where we expect it -
  1303. * tscrollup would work here, but we can optimize to
  1304. * memmove because we're freeing the earlier lines */
  1305. for(/* i = 0 */; i < slide; i++) {
  1306. free(term.line[i]);
  1307. free(term.alt[i]);
  1308. }
  1309. memmove(term.line, term.line + slide, row * sizeof(Line));
  1310. memmove(term.alt, term.alt + slide, row * sizeof(Line));
  1311. }
  1312. for(i += row; i < term.row; i++) {
  1313. free(term.line[i]);
  1314. free(term.alt[i]);
  1315. }
  1316. /* resize to new height */
  1317. term.line = realloc(term.line, row * sizeof(Line));
  1318. term.alt = realloc(term.alt, row * sizeof(Line));
  1319. /* resize each row to new width, zero-pad if needed */
  1320. for(i = 0; i < minrow; i++) {
  1321. term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
  1322. term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
  1323. for(x = mincol; x < col; x++) {
  1324. term.line[i][x].state = 0;
  1325. term.alt[i][x].state = 0;
  1326. }
  1327. }
  1328. /* allocate any new rows */
  1329. for(/* i == minrow */; i < row; i++) {
  1330. term.line[i] = calloc(col, sizeof(Glyph));
  1331. term.alt [i] = calloc(col, sizeof(Glyph));
  1332. }
  1333. /* update terminal size */
  1334. term.col = col, term.row = row;
  1335. /* make use of the LIMIT in tmoveto */
  1336. tmoveto(term.c.x, term.c.y);
  1337. /* reset scrolling region */
  1338. tsetscroll(0, row-1);
  1339. return (slide > 0);
  1340. }
  1341. void
  1342. xresize(int col, int row) {
  1343. Pixmap newbuf;
  1344. int oldw, oldh;
  1345. oldw = xw.bufw;
  1346. oldh = xw.bufh;
  1347. xw.bufw = MAX(1, col * xw.cw);
  1348. xw.bufh = MAX(1, row * xw.ch);
  1349. newbuf = XCreatePixmap(xw.dpy, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dpy, xw.scr));
  1350. XCopyArea(xw.dpy, xw.buf, newbuf, dc.gc, 0, 0, xw.bufw, xw.bufh, 0, 0);
  1351. XFreePixmap(xw.dpy, xw.buf);
  1352. XSetForeground(xw.dpy, dc.gc, dc.col[DefaultBG]);
  1353. if(xw.bufw > oldw)
  1354. XFillRectangle(xw.dpy, newbuf, dc.gc, oldw, 0,
  1355. xw.bufw-oldw, MIN(xw.bufh, oldh));
  1356. else if(xw.bufw < oldw && (BORDER > 0 || xw.w > xw.bufw))
  1357. XClearArea(xw.dpy, xw.win, BORDER+xw.bufw, BORDER,
  1358. xw.w-xw.bufh-BORDER, BORDER+MIN(xw.bufh, oldh),
  1359. False);
  1360. if(xw.bufh > oldh)
  1361. XFillRectangle(xw.dpy, newbuf, dc.gc, 0, oldh,
  1362. xw.bufw, xw.bufh-oldh);
  1363. else if(xw.bufh < oldh && (BORDER > 0 || xw.h > xw.bufh))
  1364. XClearArea(xw.dpy, xw.win, BORDER, BORDER+xw.bufh,
  1365. xw.w-2*BORDER, xw.h-xw.bufh-BORDER,
  1366. False);
  1367. xw.buf = newbuf;
  1368. }
  1369. void
  1370. xloadcols(void) {
  1371. int i, r, g, b;
  1372. XColor color;
  1373. unsigned long white = WhitePixel(xw.dpy, xw.scr);
  1374. for(i = 0; i < 16; i++) {
  1375. if(!XAllocNamedColor(xw.dpy, xw.cmap, colorname[i], &color, &color)) {
  1376. dc.col[i] = white;
  1377. fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
  1378. } else
  1379. dc.col[i] = color.pixel;
  1380. }
  1381. /* same colors as xterm */
  1382. for(r = 0; r < 6; r++)
  1383. for(g = 0; g < 6; g++)
  1384. for(b = 0; b < 6; b++) {
  1385. color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
  1386. color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
  1387. color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
  1388. if(!XAllocColor(xw.dpy, xw.cmap, &color)) {
  1389. dc.col[i] = white;
  1390. fprintf(stderr, "Could not allocate color %d\n", i);
  1391. } else
  1392. dc.col[i] = color.pixel;
  1393. i++;
  1394. }
  1395. for(r = 0; r < 24; r++, i++) {
  1396. color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
  1397. if (!XAllocColor(xw.dpy, xw.cmap, &color)) {
  1398. dc.col[i] = white;
  1399. fprintf(stderr, "Could not allocate color %d\n", i);
  1400. } else
  1401. dc.col[i] = color.pixel;
  1402. }
  1403. }
  1404. void
  1405. xclear(int x1, int y1, int x2, int y2) {
  1406. XSetForeground(xw.dpy, dc.gc, dc.col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG]);
  1407. XFillRectangle(xw.dpy, xw.buf, dc.gc,
  1408. x1 * xw.cw, y1 * xw.ch,
  1409. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
  1410. }
  1411. void
  1412. xhints(void) {
  1413. XClassHint class = {opt_class ? opt_class : TNAME, TNAME};
  1414. XWMHints wm = {.flags = InputHint, .input = 1};
  1415. XSizeHints size = {
  1416. .flags = PSize | PResizeInc | PBaseSize,
  1417. .height = xw.h,
  1418. .width = xw.w,
  1419. .height_inc = xw.ch,
  1420. .width_inc = xw.cw,
  1421. .base_height = 2*BORDER,
  1422. .base_width = 2*BORDER,
  1423. };
  1424. XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
  1425. }
  1426. XFontSet
  1427. xinitfont(char *fontstr) {
  1428. XFontSet set;
  1429. char *def, **missing;
  1430. int n;
  1431. missing = NULL;
  1432. set = XCreateFontSet(xw.dpy, fontstr, &missing, &n, &def);
  1433. if(missing) {
  1434. while(n--)
  1435. fprintf(stderr, "st: missing fontset: %s\n", missing[n]);
  1436. XFreeStringList(missing);
  1437. }
  1438. return set;
  1439. }
  1440. void
  1441. xgetfontinfo(XFontSet set, int *ascent, int *descent, short *lbearing, short *rbearing) {
  1442. XFontStruct **xfonts;
  1443. char **font_names;
  1444. int i, n;
  1445. *ascent = *descent = *lbearing = *rbearing = 0;
  1446. n = XFontsOfFontSet(set, &xfonts, &font_names);
  1447. for(i = 0; i < n; i++) {
  1448. *ascent = MAX(*ascent, (*xfonts)->ascent);
  1449. *descent = MAX(*descent, (*xfonts)->descent);
  1450. *lbearing = MAX(*lbearing, (*xfonts)->min_bounds.lbearing);
  1451. *rbearing = MAX(*rbearing, (*xfonts)->max_bounds.rbearing);
  1452. xfonts++;
  1453. }
  1454. }
  1455. void
  1456. initfonts(char *fontstr, char *bfontstr) {
  1457. if((dc.font.set = xinitfont(fontstr)) == NULL ||
  1458. (dc.bfont.set = xinitfont(bfontstr)) == NULL)
  1459. die("Can't load font %s\n", dc.font.set ? BOLDFONT : FONT);
  1460. xgetfontinfo(dc.font.set, &dc.font.ascent, &dc.font.descent,
  1461. &dc.font.lbearing, &dc.font.rbearing);
  1462. xgetfontinfo(dc.bfont.set, &dc.bfont.ascent, &dc.bfont.descent,
  1463. &dc.bfont.lbearing, &dc.bfont.rbearing);
  1464. }
  1465. void
  1466. xinit(void) {
  1467. XSetWindowAttributes attrs;
  1468. Cursor cursor;
  1469. if(!(xw.dpy = XOpenDisplay(NULL)))
  1470. die("Can't open display\n");
  1471. xw.scr = XDefaultScreen(xw.dpy);
  1472. /* font */
  1473. initfonts(FONT, BOLDFONT);
  1474. /* XXX: Assuming same size for bold font */
  1475. xw.cw = dc.font.rbearing - dc.font.lbearing;
  1476. xw.ch = dc.font.ascent + dc.font.descent;
  1477. /* colors */
  1478. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  1479. xloadcols();
  1480. /* window - default size */
  1481. xw.bufh = 24 * xw.ch;
  1482. xw.bufw = 80 * xw.cw;
  1483. xw.h = xw.bufh + 2*BORDER;
  1484. xw.w = xw.bufw + 2*BORDER;
  1485. attrs.background_pixel = dc.col[DefaultBG];
  1486. attrs.border_pixel = dc.col[DefaultBG];
  1487. attrs.bit_gravity = NorthWestGravity;
  1488. attrs.event_mask = FocusChangeMask | KeyPressMask
  1489. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  1490. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
  1491. attrs.colormap = xw.cmap;
  1492. xw.win = XCreateWindow(xw.dpy, XRootWindow(xw.dpy, xw.scr), 0, 0,
  1493. xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
  1494. XDefaultVisual(xw.dpy, xw.scr),
  1495. CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
  1496. | CWColormap,
  1497. &attrs);
  1498. xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dpy, xw.scr));
  1499. /* input methods */
  1500. xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
  1501. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  1502. | XIMStatusNothing, XNClientWindow, xw.win,
  1503. XNFocusWindow, xw.win, NULL);
  1504. /* gc */
  1505. dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
  1506. /* white cursor, black outline */
  1507. cursor = XCreateFontCursor(xw.dpy, XC_xterm);
  1508. XDefineCursor(xw.dpy, xw.win, cursor);
  1509. XRecolorCursor(xw.dpy, cursor,
  1510. &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
  1511. &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
  1512. XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
  1513. XMapWindow(xw.dpy, xw.win);
  1514. xhints();
  1515. XSync(xw.dpy, 0);
  1516. }
  1517. void
  1518. xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
  1519. unsigned long xfg = dc.col[base.fg], xbg = dc.col[base.bg], temp;
  1520. int winx = x*xw.cw, winy = y*xw.ch + dc.font.ascent, width = charlen*xw.cw;
  1521. int i;
  1522. /* only switch default fg/bg if term is in RV mode */
  1523. if(IS_SET(MODE_REVERSE)) {
  1524. if(base.fg == DefaultFG)
  1525. xfg = dc.col[DefaultBG];
  1526. if(base.bg == DefaultBG)
  1527. xbg = dc.col[DefaultFG];
  1528. }
  1529. if(base.mode & ATTR_REVERSE)
  1530. temp = xfg, xfg = xbg, xbg = temp;
  1531. XSetBackground(xw.dpy, dc.gc, xbg);
  1532. XSetForeground(xw.dpy, dc.gc, xfg);
  1533. if(base.mode & ATTR_GFX) {
  1534. for(i = 0; i < bytelen; i++) {
  1535. char c = gfx[(unsigned int)s[i] % 256];
  1536. if(c)
  1537. s[i] = c;
  1538. else if(s[i] > 0x5f)
  1539. s[i] -= 0x5f;
  1540. }
  1541. }
  1542. XmbDrawImageString(xw.dpy, xw.buf, base.mode & ATTR_BOLD ? dc.bfont.set : dc.font.set,
  1543. dc.gc, winx, winy, s, bytelen);
  1544. if(base.mode & ATTR_UNDERLINE)
  1545. XDrawLine(xw.dpy, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
  1546. }
  1547. void
  1548. xdrawcursor(void) {
  1549. static int oldx = 0;
  1550. static int oldy = 0;
  1551. int sl;
  1552. Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
  1553. LIMIT(oldx, 0, term.col-1);
  1554. LIMIT(oldy, 0, term.row-1);
  1555. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  1556. memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
  1557. /* remove the old cursor */
  1558. if(term.line[oldy][oldx].state & GLYPH_SET) {
  1559. sl = utf8size(term.line[oldy][oldx].c);
  1560. xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1, sl);
  1561. } else
  1562. xclear(oldx, oldy, oldx, oldy);
  1563. /* draw the new one */
  1564. if(!(term.c.state & CURSOR_HIDE) && (xw.state & WIN_FOCUSED)) {
  1565. sl = utf8size(g.c);
  1566. if(IS_SET(MODE_REVERSE))
  1567. g.mode |= ATTR_REVERSE, g.fg = DefaultCS, g.bg = DefaultFG;
  1568. xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
  1569. oldx = term.c.x, oldy = term.c.y;
  1570. }
  1571. }
  1572. void
  1573. draw() {
  1574. drawregion(0, 0, term.col, term.row);
  1575. }
  1576. void
  1577. drawregion(int x1, int y1, int x2, int y2) {
  1578. int ic, ib, x, y, ox, sl;
  1579. Glyph base, new;
  1580. char buf[DRAW_BUF_SIZ];
  1581. if(!(xw.state & WIN_VISIBLE))
  1582. return;
  1583. xclear(x1, y1, x2-1, y2-1);
  1584. for(y = y1; y < y2; y++) {
  1585. base = term.line[y][0];
  1586. ic = ib = ox = 0;
  1587. for(x = x1; x < x2; x++) {
  1588. new = term.line[y][x];
  1589. if(sel.bx != -1 && *(new.c) && selected(x, y))
  1590. new.mode ^= ATTR_REVERSE;
  1591. if(ib > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
  1592. ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
  1593. xdraws(buf, base, ox, y, ic, ib);
  1594. ic = ib = 0;
  1595. }
  1596. if(new.state & GLYPH_SET) {
  1597. if(ib == 0) {
  1598. ox = x;
  1599. base = new;
  1600. }
  1601. sl = utf8size(new.c);
  1602. memcpy(buf+ib, new.c, sl);
  1603. ib += sl;
  1604. ++ic;
  1605. }
  1606. }
  1607. if(ib > 0)
  1608. xdraws(buf, base, ox, y, ic, ib);
  1609. }
  1610. xdrawcursor();
  1611. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1612. }
  1613. void
  1614. expose(XEvent *ev) {
  1615. XExposeEvent *e = &ev->xexpose;
  1616. if(xw.state & WIN_REDRAW) {
  1617. if(!e->count) {
  1618. xw.state &= ~WIN_REDRAW;
  1619. draw();
  1620. }
  1621. } else
  1622. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, e->x-BORDER, e->y-BORDER,
  1623. e->width, e->height, e->x, e->y);
  1624. }
  1625. void
  1626. visibility(XEvent *ev) {
  1627. XVisibilityEvent *e = &ev->xvisibility;
  1628. if(e->state == VisibilityFullyObscured)
  1629. xw.state &= ~WIN_VISIBLE;
  1630. else if(!(xw.state & WIN_VISIBLE))
  1631. /* need a full redraw for next Expose, not just a buf copy */
  1632. xw.state |= WIN_VISIBLE | WIN_REDRAW;
  1633. }
  1634. void
  1635. unmap(XEvent *ev) {
  1636. xw.state &= ~WIN_VISIBLE;
  1637. }
  1638. void
  1639. xseturgency(int add) {
  1640. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  1641. h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
  1642. XSetWMHints(xw.dpy, xw.win, h);
  1643. XFree(h);
  1644. }
  1645. void
  1646. focus(XEvent *ev) {
  1647. if(ev->type == FocusIn) {
  1648. xw.state |= WIN_FOCUSED;
  1649. xseturgency(0);
  1650. } else
  1651. xw.state &= ~WIN_FOCUSED;
  1652. draw();
  1653. }
  1654. char*
  1655. kmap(KeySym k, unsigned int state) {
  1656. int i;
  1657. for(i = 0; i < LEN(key); i++)
  1658. if(key[i].k == k && (key[i].mask == 0 || key[i].mask & state))
  1659. return (char*)key[i].s;
  1660. return NULL;
  1661. }
  1662. void
  1663. kpress(XEvent *ev) {
  1664. XKeyEvent *e = &ev->xkey;
  1665. KeySym ksym;
  1666. char buf[32];
  1667. char *customkey;
  1668. int len;
  1669. int meta;
  1670. int shift;
  1671. Status status;
  1672. meta = e->state & Mod1Mask;
  1673. shift = e->state & ShiftMask;
  1674. len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
  1675. /* 1. custom keys from config.h */
  1676. if((customkey = kmap(ksym, e->state)))
  1677. ttywrite(customkey, strlen(customkey));
  1678. /* 2. hardcoded (overrides X lookup) */
  1679. else
  1680. switch(ksym) {
  1681. case XK_Up:
  1682. case XK_Down:
  1683. case XK_Left:
  1684. case XK_Right:
  1685. /* XXX: shift up/down doesn't work */
  1686. sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', (shift ? "dacb":"DACB")[ksym - XK_Left]);
  1687. ttywrite(buf, 3);
  1688. break;
  1689. case XK_Insert:
  1690. if(shift)
  1691. selpaste();
  1692. break;
  1693. case XK_Return:
  1694. if(IS_SET(MODE_CRLF))
  1695. ttywrite("\r\n", 2);
  1696. else
  1697. ttywrite("\r", 1);
  1698. break;
  1699. /* 3. X lookup */
  1700. default:
  1701. if(len > 0) {
  1702. if(meta && len == 1)
  1703. ttywrite("\033", 1);
  1704. ttywrite(buf, len);
  1705. }
  1706. break;
  1707. }
  1708. }
  1709. void
  1710. resize(XEvent *e) {
  1711. int col, row;
  1712. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  1713. return;
  1714. xw.w = e->xconfigure.width;
  1715. xw.h = e->xconfigure.height;
  1716. col = (xw.w - 2*BORDER) / xw.cw;
  1717. row = (xw.h - 2*BORDER) / xw.ch;
  1718. if(col == term.col && row == term.row)
  1719. return;
  1720. if(tresize(col, row))
  1721. draw();
  1722. ttyresize(col, row);
  1723. xresize(col, row);
  1724. }
  1725. void
  1726. run(void) {
  1727. XEvent ev;
  1728. fd_set rfd;
  1729. int xfd = XConnectionNumber(xw.dpy);
  1730. for(;;) {
  1731. FD_ZERO(&rfd);
  1732. FD_SET(cmdfd, &rfd);
  1733. FD_SET(xfd, &rfd);
  1734. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
  1735. if(errno == EINTR)
  1736. continue;
  1737. die("select failed: %s\n", SERRNO);
  1738. }
  1739. if(FD_ISSET(cmdfd, &rfd)) {
  1740. ttyread();
  1741. draw();
  1742. }
  1743. while(XPending(xw.dpy)) {
  1744. XNextEvent(xw.dpy, &ev);
  1745. if(XFilterEvent(&ev, xw.win))
  1746. continue;
  1747. if(handler[ev.type])
  1748. (handler[ev.type])(&ev);
  1749. }
  1750. }
  1751. }
  1752. int
  1753. main(int argc, char *argv[]) {
  1754. int i;
  1755. for(i = 1; i < argc; i++) {
  1756. switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
  1757. case 't':
  1758. if(++i < argc) opt_title = argv[i];
  1759. break;
  1760. case 'c':
  1761. if(++i < argc) opt_class = argv[i];
  1762. break;
  1763. case 'e':
  1764. /* eat every remaining arguments */
  1765. if(++i < argc) opt_cmd = &argv[i];
  1766. goto run;
  1767. case 'v':
  1768. default:
  1769. die(USAGE);
  1770. }
  1771. }
  1772. run:
  1773. setlocale(LC_CTYPE, "");
  1774. tnew(80, 24);
  1775. ttynew();
  1776. xinit();
  1777. selinit();
  1778. run();
  1779. return 0;
  1780. }