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.

1970 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] [-w windowid] [-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_embed = NULL;
  237. static char *opt_class = NULL;
  238. int
  239. utf8decode(char *s, long *u) {
  240. unsigned char c;
  241. int i, n, rtn;
  242. rtn = 1;
  243. c = *s;
  244. if(~c&B7) { /* 0xxxxxxx */
  245. *u = c;
  246. return rtn;
  247. } else if((c&(B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
  248. *u = c&(B4|B3|B2|B1|B0);
  249. n = 1;
  250. } else if((c&(B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
  251. *u = c&(B3|B2|B1|B0);
  252. n = 2;
  253. } else if((c&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
  254. *u = c&(B2|B1|B0);
  255. n = 3;
  256. } else
  257. goto invalid;
  258. for(i=n,++s; i>0; --i,++rtn,++s) {
  259. c = *s;
  260. if((c&(B7|B6)) != B7) /* 10xxxxxx */
  261. goto invalid;
  262. *u <<= 6;
  263. *u |= c&(B5|B4|B3|B2|B1|B0);
  264. }
  265. if((n == 1 && *u < 0x80) ||
  266. (n == 2 && *u < 0x800) ||
  267. (n == 3 && *u < 0x10000) ||
  268. (*u >= 0xD800 && *u <= 0xDFFF))
  269. goto invalid;
  270. return rtn;
  271. invalid:
  272. *u = 0xFFFD;
  273. return rtn;
  274. }
  275. int
  276. utf8encode(long *u, char *s) {
  277. unsigned char *sp;
  278. unsigned long uc;
  279. int i, n;
  280. sp = (unsigned char*) s;
  281. uc = *u;
  282. if(uc < 0x80) {
  283. *sp = uc; /* 0xxxxxxx */
  284. return 1;
  285. } else if(*u < 0x800) {
  286. *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
  287. n = 1;
  288. } else if(uc < 0x10000) {
  289. *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
  290. n = 2;
  291. } else if(uc <= 0x10FFFF) {
  292. *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
  293. n = 3;
  294. } else {
  295. goto invalid;
  296. }
  297. for(i=n,++sp; i>0; --i,++sp)
  298. *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
  299. return n+1;
  300. invalid:
  301. /* U+FFFD */
  302. *s++ = '\xEF';
  303. *s++ = '\xBF';
  304. *s = '\xBD';
  305. return 3;
  306. }
  307. /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
  308. UTF-8 otherwise return 0 */
  309. int
  310. isfullutf8(char *s, int b) {
  311. unsigned char *c1, *c2, *c3;
  312. c1 = (unsigned char *) s;
  313. c2 = (unsigned char *) ++s;
  314. c3 = (unsigned char *) ++s;
  315. if(b < 1)
  316. return 0;
  317. else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1)
  318. return 0;
  319. else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
  320. ((b == 1) ||
  321. ((b == 2) && (*c2&(B7|B6)) == B7)))
  322. return 0;
  323. else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
  324. ((b == 1) ||
  325. ((b == 2) && (*c2&(B7|B6)) == B7) ||
  326. ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7)))
  327. return 0;
  328. else
  329. return 1;
  330. }
  331. int
  332. utf8size(char *s) {
  333. unsigned char c = *s;
  334. if(~c&B7)
  335. return 1;
  336. else if((c&(B7|B6|B5)) == (B7|B6))
  337. return 2;
  338. else if((c&(B7|B6|B5|B4)) == (B7|B6|B5))
  339. return 3;
  340. else
  341. return 4;
  342. }
  343. void
  344. selinit(void) {
  345. sel.tclick1.tv_sec = 0;
  346. sel.tclick1.tv_usec = 0;
  347. sel.mode = 0;
  348. sel.bx = -1;
  349. sel.clip = NULL;
  350. sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
  351. if(sel.xtarget == None)
  352. sel.xtarget = XA_STRING;
  353. }
  354. static inline int
  355. selected(int x, int y) {
  356. if(sel.ey == y && sel.by == y) {
  357. int bx = MIN(sel.bx, sel.ex);
  358. int ex = MAX(sel.bx, sel.ex);
  359. return BETWEEN(x, bx, ex);
  360. }
  361. return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x))
  362. || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y));
  363. }
  364. void
  365. getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
  366. if(b)
  367. *b = e->xbutton.button;
  368. *x = X2COL(e->xbutton.x);
  369. *y = Y2ROW(e->xbutton.y);
  370. sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
  371. sel.b.y = MIN(sel.by, sel.ey);
  372. sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
  373. sel.e.y = MAX(sel.by, sel.ey);
  374. }
  375. void
  376. mousereport(XEvent *e) {
  377. int x = X2COL(e->xbutton.x);
  378. int y = Y2ROW(e->xbutton.y);
  379. int button = e->xbutton.button;
  380. int state = e->xbutton.state;
  381. char buf[] = { '\033', '[', 'M', 0, 32+x+1, 32+y+1 };
  382. static int ob, ox, oy;
  383. /* from urxvt */
  384. if(e->xbutton.type == MotionNotify) {
  385. if(!IS_SET(MODE_MOUSEMOTION) || (x == ox && y == oy))
  386. return;
  387. button = ob + 32;
  388. ox = x, oy = y;
  389. } else if(e->xbutton.type == ButtonRelease || button == AnyButton) {
  390. button = 3;
  391. } else {
  392. button -= Button1;
  393. if(button >= 3)
  394. button += 64 - 3;
  395. if(e->xbutton.type == ButtonPress) {
  396. ob = button;
  397. ox = x, oy = y;
  398. }
  399. }
  400. buf[3] = 32 + button + (state & ShiftMask ? 4 : 0)
  401. + (state & Mod4Mask ? 8 : 0)
  402. + (state & ControlMask ? 16 : 0);
  403. ttywrite(buf, sizeof(buf));
  404. }
  405. void
  406. bpress(XEvent *e) {
  407. if(IS_SET(MODE_MOUSE))
  408. mousereport(e);
  409. else if(e->xbutton.button == Button1) {
  410. sel.mode = 1;
  411. sel.ex = sel.bx = X2COL(e->xbutton.x);
  412. sel.ey = sel.by = Y2ROW(e->xbutton.y);
  413. }
  414. }
  415. void
  416. selcopy(void) {
  417. char *str, *ptr;
  418. int x, y, sz, sl, ls = 0;
  419. if(sel.bx == -1)
  420. str = NULL;
  421. else {
  422. sz = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
  423. ptr = str = malloc(sz);
  424. for(y = 0; y < term.row; y++) {
  425. for(x = 0; x < term.col; x++)
  426. if(term.line[y][x].state & GLYPH_SET && (ls = selected(x, y))) {
  427. sl = utf8size(term.line[y][x].c);
  428. memcpy(ptr, term.line[y][x].c, sl);
  429. ptr += sl;
  430. }
  431. if(ls && y < sel.e.y)
  432. *ptr++ = '\n';
  433. }
  434. *ptr = 0;
  435. }
  436. xsetsel(str);
  437. }
  438. void
  439. selnotify(XEvent *e) {
  440. unsigned long nitems, 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, (endy < 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. Window parent;
  1471. if(!(xw.dpy = XOpenDisplay(NULL)))
  1472. die("Can't open display\n");
  1473. xw.scr = XDefaultScreen(xw.dpy);
  1474. /* font */
  1475. initfonts(FONT, BOLDFONT);
  1476. /* XXX: Assuming same size for bold font */
  1477. xw.cw = dc.font.rbearing - dc.font.lbearing;
  1478. xw.ch = dc.font.ascent + dc.font.descent;
  1479. /* colors */
  1480. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  1481. xloadcols();
  1482. /* window - default size */
  1483. xw.bufh = 24 * xw.ch;
  1484. xw.bufw = 80 * xw.cw;
  1485. xw.h = xw.bufh + 2*BORDER;
  1486. xw.w = xw.bufw + 2*BORDER;
  1487. attrs.background_pixel = dc.col[DefaultBG];
  1488. attrs.border_pixel = dc.col[DefaultBG];
  1489. attrs.bit_gravity = NorthWestGravity;
  1490. attrs.event_mask = FocusChangeMask | KeyPressMask
  1491. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  1492. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
  1493. attrs.colormap = xw.cmap;
  1494. parent = opt_embed ? strtol(opt_embed, NULL, 0) : XRootWindow(xw.dpy, xw.scr);
  1495. xw.win = XCreateWindow(xw.dpy, parent, 0, 0,
  1496. xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
  1497. XDefaultVisual(xw.dpy, xw.scr),
  1498. CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
  1499. | CWColormap,
  1500. &attrs);
  1501. xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dpy, xw.scr));
  1502. /* input methods */
  1503. xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
  1504. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  1505. | XIMStatusNothing, XNClientWindow, xw.win,
  1506. XNFocusWindow, xw.win, NULL);
  1507. /* gc */
  1508. dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
  1509. /* white cursor, black outline */
  1510. cursor = XCreateFontCursor(xw.dpy, XC_xterm);
  1511. XDefineCursor(xw.dpy, xw.win, cursor);
  1512. XRecolorCursor(xw.dpy, cursor,
  1513. &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
  1514. &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
  1515. XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
  1516. XMapWindow(xw.dpy, xw.win);
  1517. xhints();
  1518. XSync(xw.dpy, 0);
  1519. }
  1520. void
  1521. xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
  1522. unsigned long xfg = dc.col[base.fg], xbg = dc.col[base.bg], temp;
  1523. int winx = x*xw.cw, winy = y*xw.ch + dc.font.ascent, width = charlen*xw.cw;
  1524. int i;
  1525. /* only switch default fg/bg if term is in RV mode */
  1526. if(IS_SET(MODE_REVERSE)) {
  1527. if(base.fg == DefaultFG)
  1528. xfg = dc.col[DefaultBG];
  1529. if(base.bg == DefaultBG)
  1530. xbg = dc.col[DefaultFG];
  1531. }
  1532. if(base.mode & ATTR_REVERSE)
  1533. temp = xfg, xfg = xbg, xbg = temp;
  1534. XSetBackground(xw.dpy, dc.gc, xbg);
  1535. XSetForeground(xw.dpy, dc.gc, xfg);
  1536. if(base.mode & ATTR_GFX) {
  1537. for(i = 0; i < bytelen; i++) {
  1538. char c = gfx[(unsigned int)s[i] % 256];
  1539. if(c)
  1540. s[i] = c;
  1541. else if(s[i] > 0x5f)
  1542. s[i] -= 0x5f;
  1543. }
  1544. }
  1545. XmbDrawImageString(xw.dpy, xw.buf, base.mode & ATTR_BOLD ? dc.bfont.set : dc.font.set,
  1546. dc.gc, winx, winy, s, bytelen);
  1547. if(base.mode & ATTR_UNDERLINE)
  1548. XDrawLine(xw.dpy, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
  1549. }
  1550. void
  1551. xdrawcursor(void) {
  1552. static int oldx = 0;
  1553. static int oldy = 0;
  1554. int sl;
  1555. Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
  1556. LIMIT(oldx, 0, term.col-1);
  1557. LIMIT(oldy, 0, term.row-1);
  1558. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  1559. memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
  1560. /* remove the old cursor */
  1561. if(term.line[oldy][oldx].state & GLYPH_SET) {
  1562. sl = utf8size(term.line[oldy][oldx].c);
  1563. xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1, sl);
  1564. } else
  1565. xclear(oldx, oldy, oldx, oldy);
  1566. /* draw the new one */
  1567. if(!(term.c.state & CURSOR_HIDE) && (xw.state & WIN_FOCUSED)) {
  1568. sl = utf8size(g.c);
  1569. if(IS_SET(MODE_REVERSE))
  1570. g.mode |= ATTR_REVERSE, g.fg = DefaultCS, g.bg = DefaultFG;
  1571. xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
  1572. oldx = term.c.x, oldy = term.c.y;
  1573. }
  1574. }
  1575. void
  1576. draw() {
  1577. drawregion(0, 0, term.col, term.row);
  1578. }
  1579. void
  1580. drawregion(int x1, int y1, int x2, int y2) {
  1581. int ic, ib, x, y, ox, sl;
  1582. Glyph base, new;
  1583. char buf[DRAW_BUF_SIZ];
  1584. if(!(xw.state & WIN_VISIBLE))
  1585. return;
  1586. xclear(x1, y1, x2-1, y2-1);
  1587. for(y = y1; y < y2; y++) {
  1588. base = term.line[y][0];
  1589. ic = ib = ox = 0;
  1590. for(x = x1; x < x2; x++) {
  1591. new = term.line[y][x];
  1592. if(sel.bx != -1 && *(new.c) && selected(x, y))
  1593. new.mode ^= ATTR_REVERSE;
  1594. if(ib > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
  1595. ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
  1596. xdraws(buf, base, ox, y, ic, ib);
  1597. ic = ib = 0;
  1598. }
  1599. if(new.state & GLYPH_SET) {
  1600. if(ib == 0) {
  1601. ox = x;
  1602. base = new;
  1603. }
  1604. sl = utf8size(new.c);
  1605. memcpy(buf+ib, new.c, sl);
  1606. ib += sl;
  1607. ++ic;
  1608. }
  1609. }
  1610. if(ib > 0)
  1611. xdraws(buf, base, ox, y, ic, ib);
  1612. }
  1613. xdrawcursor();
  1614. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1615. }
  1616. void
  1617. expose(XEvent *ev) {
  1618. XExposeEvent *e = &ev->xexpose;
  1619. if(xw.state & WIN_REDRAW) {
  1620. if(!e->count) {
  1621. xw.state &= ~WIN_REDRAW;
  1622. draw();
  1623. }
  1624. } else
  1625. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, e->x-BORDER, e->y-BORDER,
  1626. e->width, e->height, e->x, e->y);
  1627. }
  1628. void
  1629. visibility(XEvent *ev) {
  1630. XVisibilityEvent *e = &ev->xvisibility;
  1631. if(e->state == VisibilityFullyObscured)
  1632. xw.state &= ~WIN_VISIBLE;
  1633. else if(!(xw.state & WIN_VISIBLE))
  1634. /* need a full redraw for next Expose, not just a buf copy */
  1635. xw.state |= WIN_VISIBLE | WIN_REDRAW;
  1636. }
  1637. void
  1638. unmap(XEvent *ev) {
  1639. xw.state &= ~WIN_VISIBLE;
  1640. }
  1641. void
  1642. xseturgency(int add) {
  1643. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  1644. h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
  1645. XSetWMHints(xw.dpy, xw.win, h);
  1646. XFree(h);
  1647. }
  1648. void
  1649. focus(XEvent *ev) {
  1650. if(ev->type == FocusIn) {
  1651. xw.state |= WIN_FOCUSED;
  1652. xseturgency(0);
  1653. } else
  1654. xw.state &= ~WIN_FOCUSED;
  1655. draw();
  1656. }
  1657. char*
  1658. kmap(KeySym k, unsigned int state) {
  1659. int i;
  1660. for(i = 0; i < LEN(key); i++)
  1661. if(key[i].k == k && (key[i].mask == 0 || key[i].mask & state))
  1662. return (char*)key[i].s;
  1663. return NULL;
  1664. }
  1665. void
  1666. kpress(XEvent *ev) {
  1667. XKeyEvent *e = &ev->xkey;
  1668. KeySym ksym;
  1669. char buf[32];
  1670. char *customkey;
  1671. int len;
  1672. int meta;
  1673. int shift;
  1674. Status status;
  1675. meta = e->state & Mod1Mask;
  1676. shift = e->state & ShiftMask;
  1677. len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
  1678. /* 1. custom keys from config.h */
  1679. if((customkey = kmap(ksym, e->state)))
  1680. ttywrite(customkey, strlen(customkey));
  1681. /* 2. hardcoded (overrides X lookup) */
  1682. else
  1683. switch(ksym) {
  1684. case XK_Up:
  1685. case XK_Down:
  1686. case XK_Left:
  1687. case XK_Right:
  1688. /* XXX: shift up/down doesn't work */
  1689. sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', (shift ? "dacb":"DACB")[ksym - XK_Left]);
  1690. ttywrite(buf, 3);
  1691. break;
  1692. case XK_Insert:
  1693. if(shift)
  1694. selpaste();
  1695. break;
  1696. case XK_Return:
  1697. if(IS_SET(MODE_CRLF))
  1698. ttywrite("\r\n", 2);
  1699. else
  1700. ttywrite("\r", 1);
  1701. break;
  1702. /* 3. X lookup */
  1703. default:
  1704. if(len > 0) {
  1705. if(meta && len == 1)
  1706. ttywrite("\033", 1);
  1707. ttywrite(buf, len);
  1708. }
  1709. break;
  1710. }
  1711. }
  1712. void
  1713. resize(XEvent *e) {
  1714. int col, row;
  1715. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  1716. return;
  1717. xw.w = e->xconfigure.width;
  1718. xw.h = e->xconfigure.height;
  1719. col = (xw.w - 2*BORDER) / xw.cw;
  1720. row = (xw.h - 2*BORDER) / xw.ch;
  1721. if(col == term.col && row == term.row)
  1722. return;
  1723. if(tresize(col, row))
  1724. draw();
  1725. ttyresize(col, row);
  1726. xresize(col, row);
  1727. }
  1728. void
  1729. run(void) {
  1730. XEvent ev;
  1731. fd_set rfd;
  1732. int xfd = XConnectionNumber(xw.dpy);
  1733. for(;;) {
  1734. FD_ZERO(&rfd);
  1735. FD_SET(cmdfd, &rfd);
  1736. FD_SET(xfd, &rfd);
  1737. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
  1738. if(errno == EINTR)
  1739. continue;
  1740. die("select failed: %s\n", SERRNO);
  1741. }
  1742. if(FD_ISSET(cmdfd, &rfd)) {
  1743. ttyread();
  1744. draw();
  1745. }
  1746. while(XPending(xw.dpy)) {
  1747. XNextEvent(xw.dpy, &ev);
  1748. if(XFilterEvent(&ev, xw.win))
  1749. continue;
  1750. if(handler[ev.type])
  1751. (handler[ev.type])(&ev);
  1752. }
  1753. }
  1754. }
  1755. int
  1756. main(int argc, char *argv[]) {
  1757. int i;
  1758. for(i = 1; i < argc; i++) {
  1759. switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
  1760. case 't':
  1761. if(++i < argc) opt_title = argv[i];
  1762. break;
  1763. case 'c':
  1764. if(++i < argc) opt_class = argv[i];
  1765. break;
  1766. case 'w':
  1767. if(++i < argc) opt_embed = argv[i];
  1768. break;
  1769. case 'e':
  1770. /* eat every remaining arguments */
  1771. if(++i < argc) opt_cmd = &argv[i];
  1772. goto run;
  1773. case 'v':
  1774. default:
  1775. die(USAGE);
  1776. }
  1777. }
  1778. run:
  1779. setlocale(LC_CTYPE, "");
  1780. tnew(80, 24);
  1781. ttynew();
  1782. xinit();
  1783. selinit();
  1784. run();
  1785. return 0;
  1786. }