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.

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