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.

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