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.

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