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.

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