My build of suckless st terminal
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3584 lines
77 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
15 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
12 years ago
14 years ago
14 years ago
14 years ago
  1. /* See LICENSE for licence details. */
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <limits.h>
  6. #include <locale.h>
  7. #include <pwd.h>
  8. #include <stdarg.h>
  9. #include <stdbool.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <signal.h>
  14. #include <sys/ioctl.h>
  15. #include <sys/select.h>
  16. #include <sys/stat.h>
  17. #include <sys/time.h>
  18. #include <sys/types.h>
  19. #include <sys/wait.h>
  20. #include <time.h>
  21. #include <unistd.h>
  22. #include <X11/Xatom.h>
  23. #include <X11/Xlib.h>
  24. #include <X11/Xutil.h>
  25. #include <X11/cursorfont.h>
  26. #include <X11/keysym.h>
  27. #include <X11/Xft/Xft.h>
  28. #include <fontconfig/fontconfig.h>
  29. #include "arg.h"
  30. char *argv0;
  31. #define Glyph Glyph_
  32. #define Font Font_
  33. #define Draw XftDraw *
  34. #define Colour XftColor
  35. #define Colourmap Colormap
  36. #define Rectangle XRectangle
  37. #if defined(__linux)
  38. #include <pty.h>
  39. #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  40. #include <util.h>
  41. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  42. #include <libutil.h>
  43. #endif
  44. /* XEMBED messages */
  45. #define XEMBED_FOCUS_IN 4
  46. #define XEMBED_FOCUS_OUT 5
  47. /* Arbitrary sizes */
  48. #define UTF_SIZ 4
  49. #define ESC_BUF_SIZ (128*UTF_SIZ)
  50. #define ESC_ARG_SIZ 16
  51. #define STR_BUF_SIZ ESC_BUF_SIZ
  52. #define STR_ARG_SIZ ESC_ARG_SIZ
  53. #define DRAW_BUF_SIZ 20*1024
  54. #define XK_ANY_MOD UINT_MAX
  55. #define XK_NO_MOD 0
  56. #define XK_SWITCH_MOD (1<<13)
  57. #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
  58. /* macros */
  59. #define SERRNO strerror(errno)
  60. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  61. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  62. #define LEN(a) (sizeof(a) / sizeof(a[0]))
  63. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  64. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  65. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  66. #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
  67. #define IS_SET(flag) ((term.mode & (flag)) != 0)
  68. #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
  69. #define VT102ID "\033[?6c"
  70. enum glyph_attribute {
  71. ATTR_NULL = 0,
  72. ATTR_REVERSE = 1,
  73. ATTR_UNDERLINE = 2,
  74. ATTR_BOLD = 4,
  75. ATTR_GFX = 8,
  76. ATTR_ITALIC = 16,
  77. ATTR_BLINK = 32,
  78. ATTR_WRAP = 64,
  79. };
  80. enum cursor_movement {
  81. CURSOR_SAVE,
  82. CURSOR_LOAD
  83. };
  84. enum cursor_state {
  85. CURSOR_DEFAULT = 0,
  86. CURSOR_WRAPNEXT = 1,
  87. CURSOR_ORIGIN = 2
  88. };
  89. enum term_mode {
  90. MODE_WRAP = 1,
  91. MODE_INSERT = 2,
  92. MODE_APPKEYPAD = 4,
  93. MODE_ALTSCREEN = 8,
  94. MODE_CRLF = 16,
  95. MODE_MOUSEBTN = 32,
  96. MODE_MOUSEMOTION = 64,
  97. MODE_MOUSE = 32|64,
  98. MODE_REVERSE = 128,
  99. MODE_KBDLOCK = 256,
  100. MODE_HIDE = 512,
  101. MODE_ECHO = 1024,
  102. MODE_APPCURSOR = 2048,
  103. MODE_MOUSESGR = 4096,
  104. MODE_8BIT = 8192,
  105. MODE_BLINK = 16384,
  106. MODE_FBLINK = 32768,
  107. };
  108. enum escape_state {
  109. ESC_START = 1,
  110. ESC_CSI = 2,
  111. ESC_STR = 4, /* DSC, OSC, PM, APC */
  112. ESC_ALTCHARSET = 8,
  113. ESC_STR_END = 16, /* a final string was encountered */
  114. ESC_TEST = 32, /* Enter in test mode */
  115. };
  116. enum window_state {
  117. WIN_VISIBLE = 1,
  118. WIN_REDRAW = 2,
  119. WIN_FOCUSED = 4
  120. };
  121. enum selection_type {
  122. SEL_REGULAR = 1,
  123. SEL_RECTANGULAR = 2
  124. };
  125. enum selection_snap {
  126. SNAP_WORD = 1,
  127. SNAP_LINE = 2
  128. };
  129. /* bit macro */
  130. #undef B0
  131. enum { B0=1, B1=2, B2=4, B3=8, B4=16, B5=32, B6=64, B7=128 };
  132. typedef unsigned char uchar;
  133. typedef unsigned int uint;
  134. typedef unsigned long ulong;
  135. typedef unsigned short ushort;
  136. typedef struct {
  137. char c[UTF_SIZ]; /* character code */
  138. uchar mode; /* attribute flags */
  139. ushort fg; /* foreground */
  140. ushort bg; /* background */
  141. } Glyph;
  142. typedef Glyph *Line;
  143. typedef struct {
  144. Glyph attr; /* current char attributes */
  145. int x;
  146. int y;
  147. char state;
  148. } TCursor;
  149. /* CSI Escape sequence structs */
  150. /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
  151. typedef struct {
  152. char buf[ESC_BUF_SIZ]; /* raw string */
  153. int len; /* raw string length */
  154. char priv;
  155. int arg[ESC_ARG_SIZ];
  156. int narg; /* nb of args */
  157. char mode;
  158. } CSIEscape;
  159. /* STR Escape sequence structs */
  160. /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
  161. typedef struct {
  162. char type; /* ESC type ... */
  163. char buf[STR_BUF_SIZ]; /* raw string */
  164. int len; /* raw string length */
  165. char *args[STR_ARG_SIZ];
  166. int narg; /* nb of args */
  167. } STREscape;
  168. /* Internal representation of the screen */
  169. typedef struct {
  170. int row; /* nb row */
  171. int col; /* nb col */
  172. Line *line; /* screen */
  173. Line *alt; /* alternate screen */
  174. bool *dirty; /* dirtyness of lines */
  175. TCursor c; /* cursor */
  176. int top; /* top scroll limit */
  177. int bot; /* bottom scroll limit */
  178. int mode; /* terminal mode flags */
  179. int esc; /* escape state flags */
  180. bool numlock; /* lock numbers in keyboard */
  181. bool *tabs;
  182. } Term;
  183. /* Purely graphic info */
  184. typedef struct {
  185. Display *dpy;
  186. Colourmap cmap;
  187. Window win;
  188. Drawable buf;
  189. Atom xembed, wmdeletewin;
  190. XIM xim;
  191. XIC xic;
  192. Draw draw;
  193. Visual *vis;
  194. int scr;
  195. bool isfixed; /* is fixed geometry? */
  196. int fx, fy, fw, fh; /* fixed geometry */
  197. int tw, th; /* tty width and height */
  198. int w, h; /* window width and height */
  199. int ch; /* char height */
  200. int cw; /* char width */
  201. char state; /* focus, redraw, visible */
  202. } XWindow;
  203. typedef struct {
  204. int b;
  205. uint mask;
  206. char s[ESC_BUF_SIZ];
  207. } Mousekey;
  208. typedef struct {
  209. KeySym k;
  210. uint mask;
  211. char s[ESC_BUF_SIZ];
  212. /* three valued logic variables: 0 indifferent, 1 on, -1 off */
  213. signed char appkey; /* application keypad */
  214. signed char appcursor; /* application cursor */
  215. signed char crlf; /* crlf mode */
  216. } Key;
  217. /* TODO: use better name for vars... */
  218. typedef struct {
  219. int mode;
  220. int type;
  221. int snap;
  222. int bx, by;
  223. int ex, ey;
  224. struct {
  225. int x, y;
  226. } b, e;
  227. char *clip;
  228. Atom xtarget;
  229. bool alt;
  230. struct timeval tclick1;
  231. struct timeval tclick2;
  232. } Selection;
  233. typedef union {
  234. int i;
  235. unsigned int ui;
  236. float f;
  237. const void *v;
  238. } Arg;
  239. typedef struct {
  240. unsigned int mod;
  241. KeySym keysym;
  242. void (*func)(const Arg *);
  243. const Arg arg;
  244. } Shortcut;
  245. /* function definitions used in config.h */
  246. static void clippaste(const Arg *);
  247. static void numlock(const Arg *);
  248. static void selpaste(const Arg *);
  249. static void xzoom(const Arg *);
  250. /* Config.h for applying patches and the configuration. */
  251. #include "config.h"
  252. /* Font structure */
  253. typedef struct {
  254. int height;
  255. int width;
  256. int ascent;
  257. int descent;
  258. short lbearing;
  259. short rbearing;
  260. XftFont *match;
  261. FcFontSet *set;
  262. FcPattern *pattern;
  263. } Font;
  264. /* Drawing Context */
  265. typedef struct {
  266. Colour col[LEN(colorname) < 256 ? 256 : LEN(colorname)];
  267. Font font, bfont, ifont, ibfont;
  268. GC gc;
  269. } DC;
  270. static void die(const char *, ...);
  271. static void draw(void);
  272. static void redraw(int);
  273. static void drawregion(int, int, int, int);
  274. static void execsh(void);
  275. static void sigchld(int);
  276. static void run(void);
  277. static void csidump(void);
  278. static void csihandle(void);
  279. static void csiparse(void);
  280. static void csireset(void);
  281. static void strdump(void);
  282. static void strhandle(void);
  283. static void strparse(void);
  284. static void strreset(void);
  285. static int tattrset(int);
  286. static void tclearregion(int, int, int, int);
  287. static void tcursor(int);
  288. static void tdeletechar(int);
  289. static void tdeleteline(int);
  290. static void tinsertblank(int);
  291. static void tinsertblankline(int);
  292. static void tmoveto(int, int);
  293. static void tmoveato(int x, int y);
  294. static void tnew(int, int);
  295. static void tnewline(int);
  296. static void tputtab(bool);
  297. static void tputc(char *, int);
  298. static void treset(void);
  299. static int tresize(int, int);
  300. static void tscrollup(int, int);
  301. static void tscrolldown(int, int);
  302. static void tsetattr(int*, int);
  303. static void tsetchar(char *, Glyph *, int, int);
  304. static void tsetscroll(int, int);
  305. static void tswapscreen(void);
  306. static void tsetdirt(int, int);
  307. static void tsetdirtattr(int);
  308. static void tsetmode(bool, bool, int *, int);
  309. static void tfulldirt(void);
  310. static void techo(char *, int);
  311. static inline bool match(uint, uint);
  312. static void ttynew(void);
  313. static void ttyread(void);
  314. static void ttyresize(void);
  315. static void ttywrite(const char *, size_t);
  316. static void xdraws(char *, Glyph, int, int, int, int);
  317. static void xhints(void);
  318. static void xclear(int, int, int, int);
  319. static void xdrawcursor(void);
  320. static void xinit(void);
  321. static void xloadcols(void);
  322. static int xsetcolorname(int, const char *);
  323. static int xloadfont(Font *, FcPattern *);
  324. static void xloadfonts(char *, int);
  325. static void xsettitle(char *);
  326. static void xresettitle(void);
  327. static void xseturgency(int);
  328. static void xsetsel(char*);
  329. static void xtermclear(int, int, int, int);
  330. static void xunloadfonts(void);
  331. static void xresize(int, int);
  332. static void expose(XEvent *);
  333. static void visibility(XEvent *);
  334. static void unmap(XEvent *);
  335. static char *kmap(KeySym, uint);
  336. static void kpress(XEvent *);
  337. static void cmessage(XEvent *);
  338. static void cresize(int, int);
  339. static void resize(XEvent *);
  340. static void focus(XEvent *);
  341. static void brelease(XEvent *);
  342. static void bpress(XEvent *);
  343. static void bmotion(XEvent *);
  344. static void selnotify(XEvent *);
  345. static void selclear(XEvent *);
  346. static void selrequest(XEvent *);
  347. static void selinit(void);
  348. static inline bool selected(int, int);
  349. static void selcopy(void);
  350. static void selscroll(int, int);
  351. static void selsnap(int, int *, int *, int);
  352. static int utf8decode(char *, long *);
  353. static int utf8encode(long *, char *);
  354. static int utf8size(char *);
  355. static int isfullutf8(char *, int);
  356. static ssize_t xwrite(int, char *, size_t);
  357. static void *xmalloc(size_t);
  358. static void *xrealloc(void *, size_t);
  359. static void *xcalloc(size_t, size_t);
  360. static void (*handler[LASTEvent])(XEvent *) = {
  361. [KeyPress] = kpress,
  362. [ClientMessage] = cmessage,
  363. [ConfigureNotify] = resize,
  364. [VisibilityNotify] = visibility,
  365. [UnmapNotify] = unmap,
  366. [Expose] = expose,
  367. [FocusIn] = focus,
  368. [FocusOut] = focus,
  369. [MotionNotify] = bmotion,
  370. [ButtonPress] = bpress,
  371. [ButtonRelease] = brelease,
  372. [SelectionClear] = selclear,
  373. [SelectionNotify] = selnotify,
  374. [SelectionRequest] = selrequest,
  375. };
  376. /* Globals */
  377. static DC dc;
  378. static XWindow xw;
  379. static Term term;
  380. static CSIEscape csiescseq;
  381. static STREscape strescseq;
  382. static int cmdfd;
  383. static pid_t pid;
  384. static Selection sel;
  385. static int iofd = -1;
  386. static char **opt_cmd = NULL;
  387. static char *opt_io = NULL;
  388. static char *opt_title = NULL;
  389. static char *opt_embed = NULL;
  390. static char *opt_class = NULL;
  391. static char *opt_font = NULL;
  392. static char *usedfont = NULL;
  393. static int usedfontsize = 0;
  394. /* Font Ring Cache */
  395. enum {
  396. FRC_NORMAL,
  397. FRC_ITALIC,
  398. FRC_BOLD,
  399. FRC_ITALICBOLD
  400. };
  401. typedef struct {
  402. XftFont *font;
  403. long c;
  404. int flags;
  405. } Fontcache;
  406. /*
  407. * Fontcache is a ring buffer, with frccur as current position and frclen as
  408. * the current length of used elements.
  409. */
  410. static Fontcache frc[1024];
  411. static int frccur = -1, frclen = 0;
  412. ssize_t
  413. xwrite(int fd, char *s, size_t len) {
  414. size_t aux = len;
  415. while(len > 0) {
  416. ssize_t r = write(fd, s, len);
  417. if(r < 0)
  418. return r;
  419. len -= r;
  420. s += r;
  421. }
  422. return aux;
  423. }
  424. void *
  425. xmalloc(size_t len) {
  426. void *p = malloc(len);
  427. if(!p)
  428. die("Out of memory\n");
  429. return p;
  430. }
  431. void *
  432. xrealloc(void *p, size_t len) {
  433. if((p = realloc(p, len)) == NULL)
  434. die("Out of memory\n");
  435. return p;
  436. }
  437. void *
  438. xcalloc(size_t nmemb, size_t size) {
  439. void *p = calloc(nmemb, size);
  440. if(!p)
  441. die("Out of memory\n");
  442. return p;
  443. }
  444. int
  445. utf8decode(char *s, long *u) {
  446. uchar c;
  447. int i, n, rtn;
  448. rtn = 1;
  449. c = *s;
  450. if(~c & B7) { /* 0xxxxxxx */
  451. *u = c;
  452. return rtn;
  453. } else if((c & (B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
  454. *u = c&(B4|B3|B2|B1|B0);
  455. n = 1;
  456. } else if((c & (B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
  457. *u = c&(B3|B2|B1|B0);
  458. n = 2;
  459. } else if((c & (B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
  460. *u = c & (B2|B1|B0);
  461. n = 3;
  462. } else {
  463. goto invalid;
  464. }
  465. for(i = n, ++s; i > 0; --i, ++rtn, ++s) {
  466. c = *s;
  467. if((c & (B7|B6)) != B7) /* 10xxxxxx */
  468. goto invalid;
  469. *u <<= 6;
  470. *u |= c & (B5|B4|B3|B2|B1|B0);
  471. }
  472. if((n == 1 && *u < 0x80) ||
  473. (n == 2 && *u < 0x800) ||
  474. (n == 3 && *u < 0x10000) ||
  475. (*u >= 0xD800 && *u <= 0xDFFF)) {
  476. goto invalid;
  477. }
  478. return rtn;
  479. invalid:
  480. *u = 0xFFFD;
  481. return rtn;
  482. }
  483. int
  484. utf8encode(long *u, char *s) {
  485. uchar *sp;
  486. ulong uc;
  487. int i, n;
  488. sp = (uchar *)s;
  489. uc = *u;
  490. if(uc < 0x80) {
  491. *sp = uc; /* 0xxxxxxx */
  492. return 1;
  493. } else if(*u < 0x800) {
  494. *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
  495. n = 1;
  496. } else if(uc < 0x10000) {
  497. *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
  498. n = 2;
  499. } else if(uc <= 0x10FFFF) {
  500. *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
  501. n = 3;
  502. } else {
  503. goto invalid;
  504. }
  505. for(i=n,++sp; i>0; --i,++sp)
  506. *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
  507. return n+1;
  508. invalid:
  509. /* U+FFFD */
  510. *s++ = '\xEF';
  511. *s++ = '\xBF';
  512. *s = '\xBD';
  513. return 3;
  514. }
  515. /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
  516. UTF-8 otherwise return 0 */
  517. int
  518. isfullutf8(char *s, int b) {
  519. uchar *c1, *c2, *c3;
  520. c1 = (uchar *)s;
  521. c2 = (uchar *)++s;
  522. c3 = (uchar *)++s;
  523. if(b < 1) {
  524. return 0;
  525. } else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1) {
  526. return 0;
  527. } else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
  528. ((b == 1) ||
  529. ((b == 2) && (*c2&(B7|B6)) == B7))) {
  530. return 0;
  531. } else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
  532. ((b == 1) ||
  533. ((b == 2) && (*c2&(B7|B6)) == B7) ||
  534. ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7))) {
  535. return 0;
  536. } else {
  537. return 1;
  538. }
  539. }
  540. int
  541. utf8size(char *s) {
  542. uchar c = *s;
  543. if(~c&B7) {
  544. return 1;
  545. } else if((c&(B7|B6|B5)) == (B7|B6)) {
  546. return 2;
  547. } else if((c&(B7|B6|B5|B4)) == (B7|B6|B5)) {
  548. return 3;
  549. } else {
  550. return 4;
  551. }
  552. }
  553. void
  554. selinit(void) {
  555. memset(&sel.tclick1, 0, sizeof(sel.tclick1));
  556. memset(&sel.tclick2, 0, sizeof(sel.tclick2));
  557. sel.mode = 0;
  558. sel.bx = -1;
  559. sel.clip = NULL;
  560. sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
  561. if(sel.xtarget == None)
  562. sel.xtarget = XA_STRING;
  563. }
  564. static int
  565. x2col(int x) {
  566. x -= borderpx;
  567. x /= xw.cw;
  568. return LIMIT(x, 0, term.col-1);
  569. }
  570. static int
  571. y2row(int y) {
  572. y -= borderpx;
  573. y /= xw.ch;
  574. return LIMIT(y, 0, term.row-1);
  575. }
  576. static inline bool
  577. selected(int x, int y) {
  578. int bx, ex;
  579. if(sel.ey == y && sel.by == y) {
  580. bx = MIN(sel.bx, sel.ex);
  581. ex = MAX(sel.bx, sel.ex);
  582. return BETWEEN(x, bx, ex);
  583. }
  584. if(sel.type == SEL_RECTANGULAR) {
  585. return ((sel.b.y <= y && y <= sel.e.y)
  586. && (sel.b.x <= x && x <= sel.e.x));
  587. }
  588. return ((sel.b.y < y && y < sel.e.y)
  589. || (y == sel.e.y && x <= sel.e.x))
  590. || (y == sel.b.y && x >= sel.b.x
  591. && (x <= sel.e.x || sel.b.y != sel.e.y));
  592. }
  593. void
  594. selsnap(int mode, int *x, int *y, int direction) {
  595. switch(mode) {
  596. case SNAP_WORD:
  597. for(;;) {
  598. if(direction < 0 && *x <= 0) {
  599. if(*y > 0 && term.line[*y - 1][term.col-1].mode
  600. & ATTR_WRAP) {
  601. *y -= 1;
  602. *x = term.col-1;
  603. } else {
  604. break;
  605. }
  606. }
  607. if(direction > 0 && *x >= term.col-1) {
  608. if(*y < term.row-1 && term.line[*y][*x].mode
  609. & ATTR_WRAP) {
  610. *y += 1;
  611. *x = 0;
  612. } else {
  613. break;
  614. }
  615. }
  616. if(term.line[*y][*x + direction].c[0] == ' ')
  617. break;
  618. *x += direction;
  619. }
  620. break;
  621. case SNAP_LINE:
  622. *x = (direction < 0) ? 0 : term.col - 1;
  623. if(direction < 0 && *y > 0) {
  624. for(; *y > 0; *y += direction) {
  625. if(!(term.line[*y-1][term.col-1].mode
  626. & ATTR_WRAP)) {
  627. break;
  628. }
  629. }
  630. } else if(direction > 0 && *y < term.row-1) {
  631. for(; *y < term.row; *y += direction) {
  632. if(!(term.line[*y][term.col-1].mode
  633. & ATTR_WRAP)) {
  634. break;
  635. }
  636. }
  637. }
  638. break;
  639. default:
  640. break;
  641. }
  642. }
  643. void
  644. getbuttoninfo(XEvent *e) {
  645. int type;
  646. uint state = e->xbutton.state &~Button1Mask;
  647. sel.alt = IS_SET(MODE_ALTSCREEN);
  648. sel.ex = x2col(e->xbutton.x);
  649. sel.ey = y2row(e->xbutton.y);
  650. if (sel.by < sel.ey
  651. || (sel.by == sel.ey && sel.bx < sel.ex)) {
  652. selsnap(sel.snap, &sel.bx, &sel.by, -1);
  653. selsnap(sel.snap, &sel.ex, &sel.ey, +1);
  654. } else {
  655. selsnap(sel.snap, &sel.ex, &sel.ey, -1);
  656. selsnap(sel.snap, &sel.bx, &sel.by, +1);
  657. }
  658. sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
  659. sel.b.y = MIN(sel.by, sel.ey);
  660. sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
  661. sel.e.y = MAX(sel.by, sel.ey);
  662. sel.type = SEL_REGULAR;
  663. for(type = 1; type < LEN(selmasks); ++type) {
  664. if(match(selmasks[type], state)) {
  665. sel.type = type;
  666. break;
  667. }
  668. }
  669. }
  670. void
  671. mousereport(XEvent *e) {
  672. int x = x2col(e->xbutton.x), y = y2row(e->xbutton.y),
  673. button = e->xbutton.button, state = e->xbutton.state,
  674. len;
  675. char buf[40];
  676. static int ob, ox, oy;
  677. /* from urxvt */
  678. if(e->xbutton.type == MotionNotify) {
  679. if(!IS_SET(MODE_MOUSEMOTION) || (x == ox && y == oy))
  680. return;
  681. button = ob + 32;
  682. ox = x, oy = y;
  683. } else if(!IS_SET(MODE_MOUSESGR)
  684. && (e->xbutton.type == ButtonRelease
  685. || button == AnyButton)) {
  686. button = 3;
  687. } else {
  688. button -= Button1;
  689. if(button >= 3)
  690. button += 64 - 3;
  691. if(e->xbutton.type == ButtonPress) {
  692. ob = button;
  693. ox = x, oy = y;
  694. }
  695. }
  696. button += (state & ShiftMask ? 4 : 0)
  697. + (state & Mod4Mask ? 8 : 0)
  698. + (state & ControlMask ? 16 : 0);
  699. len = 0;
  700. if(IS_SET(MODE_MOUSESGR)) {
  701. len = snprintf(buf, sizeof(buf), "\033[<%d;%d;%d%c",
  702. button, x+1, y+1,
  703. e->xbutton.type == ButtonRelease ? 'm' : 'M');
  704. } else if(x < 223 && y < 223) {
  705. len = snprintf(buf, sizeof(buf), "\033[M%c%c%c",
  706. 32+button, 32+x+1, 32+y+1);
  707. } else {
  708. return;
  709. }
  710. ttywrite(buf, len);
  711. }
  712. void
  713. bpress(XEvent *e) {
  714. struct timeval now;
  715. Mousekey *mk;
  716. if(IS_SET(MODE_MOUSE)) {
  717. mousereport(e);
  718. return;
  719. }
  720. for(mk = mshortcuts; mk < mshortcuts + LEN(mshortcuts); mk++) {
  721. if(e->xbutton.button == mk->b
  722. && match(mk->mask, e->xbutton.state)) {
  723. ttywrite(mk->s, strlen(mk->s));
  724. if(IS_SET(MODE_ECHO))
  725. techo(mk->s, strlen(mk->s));
  726. return;
  727. }
  728. }
  729. if(e->xbutton.button == Button1) {
  730. gettimeofday(&now, NULL);
  731. /* Clear previous selection, logically and visually. */
  732. if(sel.bx != -1) {
  733. sel.bx = -1;
  734. tsetdirt(sel.b.y, sel.e.y);
  735. draw();
  736. }
  737. sel.mode = 1;
  738. sel.type = SEL_REGULAR;
  739. sel.ex = sel.bx = x2col(e->xbutton.x);
  740. sel.ey = sel.by = y2row(e->xbutton.y);
  741. /*
  742. * If the user clicks below predefined timeouts specific
  743. * snapping behaviour is exposed.
  744. */
  745. if(TIMEDIFF(now, sel.tclick2) <= tripleclicktimeout) {
  746. sel.snap = SNAP_LINE;
  747. } else if(TIMEDIFF(now, sel.tclick1) <= doubleclicktimeout) {
  748. sel.snap = SNAP_WORD;
  749. } else {
  750. sel.snap = 0;
  751. }
  752. selsnap(sel.snap, &sel.bx, &sel.by, -1);
  753. selsnap(sel.snap, &sel.ex, &sel.ey, +1);
  754. sel.b.x = sel.bx;
  755. sel.b.y = sel.by;
  756. sel.e.x = sel.ex;
  757. sel.e.y = sel.ey;
  758. /*
  759. * Draw selection, unless it's regular and we don't want to
  760. * make clicks visible
  761. */
  762. if(sel.snap != 0) {
  763. sel.mode++;
  764. tsetdirt(sel.b.y, sel.e.y);
  765. draw();
  766. }
  767. sel.tclick2 = sel.tclick1;
  768. sel.tclick1 = now;
  769. }
  770. }
  771. void
  772. selcopy(void) {
  773. char *str, *ptr;
  774. int x, y, bufsize, size;
  775. Glyph *gp, *last;
  776. if(sel.bx == -1) {
  777. str = NULL;
  778. } else {
  779. bufsize = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
  780. ptr = str = xmalloc(bufsize);
  781. /* append every set & selected glyph to the selection */
  782. for(y = sel.b.y; y < sel.e.y + 1; y++) {
  783. gp = &term.line[y][0];
  784. last = gp + term.col;
  785. while(--last >= gp && !(selected(last - gp, y) && \
  786. strcmp(last->c, " ") != 0))
  787. /* nothing */;
  788. for(x = 0; gp <= last; x++, ++gp) {
  789. if(!selected(x, y))
  790. continue;
  791. size = utf8size(gp->c);
  792. memcpy(ptr, gp->c, size);
  793. ptr += size;
  794. }
  795. /*
  796. * Copy and pasting of line endings is inconsistent
  797. * in the inconsistent terminal and GUI world.
  798. * The best solution seems like to produce '\n' when
  799. * something is copied from st and convert '\n' to
  800. * '\r', when something to be pasted is received by
  801. * st.
  802. * FIXME: Fix the computer world.
  803. */
  804. if(y < sel.e.y && !((gp-1)->mode & ATTR_WRAP))
  805. *ptr++ = '\n';
  806. }
  807. *ptr = 0;
  808. }
  809. xsetsel(str);
  810. }
  811. void
  812. selnotify(XEvent *e) {
  813. ulong nitems, ofs, rem;
  814. int format;
  815. uchar *data, *last, *repl;
  816. Atom type;
  817. ofs = 0;
  818. do {
  819. if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
  820. False, AnyPropertyType, &type, &format,
  821. &nitems, &rem, &data)) {
  822. fprintf(stderr, "Clipboard allocation failed\n");
  823. return;
  824. }
  825. /*
  826. * As seen in selcopy:
  827. * Line endings are inconsistent in the terminal and GUI world
  828. * copy and pasting. When receiving some selection data,
  829. * replace all '\n' with '\r'.
  830. * FIXME: Fix the computer world.
  831. */
  832. repl = data;
  833. last = data + nitems * format / 8;
  834. while((repl = memchr(repl, '\n', last - repl))) {
  835. *repl++ = '\r';
  836. }
  837. ttywrite((const char *)data, nitems * format / 8);
  838. XFree(data);
  839. /* number of 32-bit chunks returned */
  840. ofs += nitems * format / 32;
  841. } while(rem > 0);
  842. }
  843. void
  844. selpaste(const Arg *dummy) {
  845. XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY,
  846. xw.win, CurrentTime);
  847. }
  848. void
  849. clippaste(const Arg *dummy) {
  850. Atom clipboard;
  851. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  852. XConvertSelection(xw.dpy, clipboard, sel.xtarget, XA_PRIMARY,
  853. xw.win, CurrentTime);
  854. }
  855. void
  856. selclear(XEvent *e) {
  857. if(sel.bx == -1)
  858. return;
  859. sel.bx = -1;
  860. tsetdirt(sel.b.y, sel.e.y);
  861. }
  862. void
  863. selrequest(XEvent *e) {
  864. XSelectionRequestEvent *xsre;
  865. XSelectionEvent xev;
  866. Atom xa_targets, string;
  867. xsre = (XSelectionRequestEvent *) e;
  868. xev.type = SelectionNotify;
  869. xev.requestor = xsre->requestor;
  870. xev.selection = xsre->selection;
  871. xev.target = xsre->target;
  872. xev.time = xsre->time;
  873. /* reject */
  874. xev.property = None;
  875. xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
  876. if(xsre->target == xa_targets) {
  877. /* respond with the supported type */
  878. string = sel.xtarget;
  879. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  880. XA_ATOM, 32, PropModeReplace,
  881. (uchar *) &string, 1);
  882. xev.property = xsre->property;
  883. } else if(xsre->target == sel.xtarget && sel.clip != NULL) {
  884. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  885. xsre->target, 8, PropModeReplace,
  886. (uchar *) sel.clip, strlen(sel.clip));
  887. xev.property = xsre->property;
  888. }
  889. /* all done, send a notification to the listener */
  890. if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
  891. fprintf(stderr, "Error sending SelectionNotify event\n");
  892. }
  893. void
  894. xsetsel(char *str) {
  895. /* register the selection for both the clipboard and the primary */
  896. Atom clipboard;
  897. free(sel.clip);
  898. sel.clip = str;
  899. XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
  900. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  901. XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
  902. }
  903. void
  904. brelease(XEvent *e) {
  905. if(IS_SET(MODE_MOUSE)) {
  906. mousereport(e);
  907. return;
  908. }
  909. if(e->xbutton.button == Button2) {
  910. selpaste(NULL);
  911. } else if(e->xbutton.button == Button1) {
  912. if(sel.mode < 2) {
  913. sel.bx = -1;
  914. } else {
  915. getbuttoninfo(e);
  916. selcopy();
  917. }
  918. sel.mode = 0;
  919. term.dirty[sel.ey] = 1;
  920. }
  921. }
  922. void
  923. bmotion(XEvent *e) {
  924. int oldey, oldex, oldsby, oldsey;
  925. if(IS_SET(MODE_MOUSE)) {
  926. mousereport(e);
  927. return;
  928. }
  929. if(!sel.mode)
  930. return;
  931. sel.mode++;
  932. oldey = sel.ey;
  933. oldex = sel.ex;
  934. oldsby = sel.b.y;
  935. oldsey = sel.e.y;
  936. getbuttoninfo(e);
  937. if(oldey != sel.ey || oldex != sel.ex) {
  938. tsetdirt(MIN(sel.b.y, oldsby), MAX(sel.e.y, oldsey));
  939. }
  940. }
  941. void
  942. die(const char *errstr, ...) {
  943. va_list ap;
  944. va_start(ap, errstr);
  945. vfprintf(stderr, errstr, ap);
  946. va_end(ap);
  947. exit(EXIT_FAILURE);
  948. }
  949. void
  950. execsh(void) {
  951. char **args;
  952. char *envshell = getenv("SHELL");
  953. const struct passwd *pass = getpwuid(getuid());
  954. char buf[sizeof(long) * 8 + 1];
  955. unsetenv("COLUMNS");
  956. unsetenv("LINES");
  957. unsetenv("TERMCAP");
  958. if(pass) {
  959. setenv("LOGNAME", pass->pw_name, 1);
  960. setenv("USER", pass->pw_name, 1);
  961. setenv("SHELL", pass->pw_shell, 0);
  962. setenv("HOME", pass->pw_dir, 0);
  963. }
  964. snprintf(buf, sizeof(buf), "%lu", xw.win);
  965. setenv("WINDOWID", buf, 1);
  966. signal(SIGCHLD, SIG_DFL);
  967. signal(SIGHUP, SIG_DFL);
  968. signal(SIGINT, SIG_DFL);
  969. signal(SIGQUIT, SIG_DFL);
  970. signal(SIGTERM, SIG_DFL);
  971. signal(SIGALRM, SIG_DFL);
  972. DEFAULT(envshell, shell);
  973. setenv("TERM", termname, 1);
  974. args = opt_cmd ? opt_cmd : (char *[]){envshell, "-i", NULL};
  975. execvp(args[0], args);
  976. exit(EXIT_FAILURE);
  977. }
  978. void
  979. sigchld(int a) {
  980. int stat = 0;
  981. if(waitpid(pid, &stat, 0) < 0)
  982. die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
  983. if(WIFEXITED(stat)) {
  984. exit(WEXITSTATUS(stat));
  985. } else {
  986. exit(EXIT_FAILURE);
  987. }
  988. }
  989. void
  990. ttynew(void) {
  991. int m, s;
  992. struct winsize w = {term.row, term.col, 0, 0};
  993. /* seems to work fine on linux, openbsd and freebsd */
  994. if(openpty(&m, &s, NULL, NULL, &w) < 0)
  995. die("openpty failed: %s\n", SERRNO);
  996. switch(pid = fork()) {
  997. case -1:
  998. die("fork failed\n");
  999. break;
  1000. case 0:
  1001. setsid(); /* create a new process group */
  1002. dup2(s, STDIN_FILENO);
  1003. dup2(s, STDOUT_FILENO);
  1004. dup2(s, STDERR_FILENO);
  1005. if(ioctl(s, TIOCSCTTY, NULL) < 0)
  1006. die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
  1007. close(s);
  1008. close(m);
  1009. execsh();
  1010. break;
  1011. default:
  1012. close(s);
  1013. cmdfd = m;
  1014. signal(SIGCHLD, sigchld);
  1015. if(opt_io) {
  1016. iofd = (!strcmp(opt_io, "-")) ?
  1017. STDOUT_FILENO :
  1018. open(opt_io, O_WRONLY | O_CREAT, 0666);
  1019. if(iofd < 0) {
  1020. fprintf(stderr, "Error opening %s:%s\n",
  1021. opt_io, strerror(errno));
  1022. }
  1023. }
  1024. }
  1025. }
  1026. void
  1027. dump(char c) {
  1028. static int col;
  1029. fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
  1030. if(++col % 10 == 0)
  1031. fprintf(stderr, "\n");
  1032. }
  1033. void
  1034. ttyread(void) {
  1035. static char buf[BUFSIZ];
  1036. static int buflen = 0;
  1037. char *ptr;
  1038. char s[UTF_SIZ];
  1039. int charsize; /* size of utf8 char in bytes */
  1040. long utf8c;
  1041. int ret;
  1042. /* append read bytes to unprocessed bytes */
  1043. if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
  1044. die("Couldn't read from shell: %s\n", SERRNO);
  1045. /* process every complete utf8 char */
  1046. buflen += ret;
  1047. ptr = buf;
  1048. while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
  1049. charsize = utf8decode(ptr, &utf8c);
  1050. utf8encode(&utf8c, s);
  1051. tputc(s, charsize);
  1052. ptr += charsize;
  1053. buflen -= charsize;
  1054. }
  1055. /* keep any uncomplete utf8 char for the next call */
  1056. memmove(buf, ptr, buflen);
  1057. }
  1058. void
  1059. ttywrite(const char *s, size_t n) {
  1060. if(write(cmdfd, s, n) == -1)
  1061. die("write error on tty: %s\n", SERRNO);
  1062. }
  1063. void
  1064. ttyresize(void) {
  1065. struct winsize w;
  1066. w.ws_row = term.row;
  1067. w.ws_col = term.col;
  1068. w.ws_xpixel = xw.tw;
  1069. w.ws_ypixel = xw.th;
  1070. if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  1071. fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
  1072. }
  1073. int
  1074. tattrset(int attr) {
  1075. int i, j;
  1076. for(i = 0; i < term.row-1; i++) {
  1077. for(j = 0; j < term.col-1; j++) {
  1078. if(term.line[i][j].mode & attr)
  1079. return 1;
  1080. }
  1081. }
  1082. return 0;
  1083. }
  1084. void
  1085. tsetdirt(int top, int bot) {
  1086. int i;
  1087. LIMIT(top, 0, term.row-1);
  1088. LIMIT(bot, 0, term.row-1);
  1089. for(i = top; i <= bot; i++)
  1090. term.dirty[i] = 1;
  1091. }
  1092. void
  1093. tsetdirtattr(int attr) {
  1094. int i, j;
  1095. for(i = 0; i < term.row-1; i++) {
  1096. for(j = 0; j < term.col-1; j++) {
  1097. if(term.line[i][j].mode & attr) {
  1098. tsetdirt(i, i);
  1099. break;
  1100. }
  1101. }
  1102. }
  1103. }
  1104. void
  1105. tfulldirt(void) {
  1106. tsetdirt(0, term.row-1);
  1107. }
  1108. void
  1109. tcursor(int mode) {
  1110. static TCursor c;
  1111. if(mode == CURSOR_SAVE) {
  1112. c = term.c;
  1113. } else if(mode == CURSOR_LOAD) {
  1114. term.c = c;
  1115. tmoveto(c.x, c.y);
  1116. }
  1117. }
  1118. void
  1119. treset(void) {
  1120. uint i;
  1121. term.c = (TCursor){{
  1122. .mode = ATTR_NULL,
  1123. .fg = defaultfg,
  1124. .bg = defaultbg
  1125. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  1126. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  1127. for(i = tabspaces; i < term.col; i += tabspaces)
  1128. term.tabs[i] = 1;
  1129. term.top = 0;
  1130. term.bot = term.row - 1;
  1131. term.mode = MODE_WRAP;
  1132. tclearregion(0, 0, term.col-1, term.row-1);
  1133. tmoveto(0, 0);
  1134. tcursor(CURSOR_SAVE);
  1135. }
  1136. void
  1137. tnew(int col, int row) {
  1138. memset(&term, 0, sizeof(Term));
  1139. tresize(col, row);
  1140. term.numlock = 1;
  1141. treset();
  1142. }
  1143. void
  1144. tswapscreen(void) {
  1145. Line *tmp = term.line;
  1146. term.line = term.alt;
  1147. term.alt = tmp;
  1148. term.mode ^= MODE_ALTSCREEN;
  1149. tfulldirt();
  1150. }
  1151. void
  1152. tscrolldown(int orig, int n) {
  1153. int i;
  1154. Line temp;
  1155. LIMIT(n, 0, term.bot-orig+1);
  1156. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  1157. for(i = term.bot; i >= orig+n; i--) {
  1158. temp = term.line[i];
  1159. term.line[i] = term.line[i-n];
  1160. term.line[i-n] = temp;
  1161. term.dirty[i] = 1;
  1162. term.dirty[i-n] = 1;
  1163. }
  1164. selscroll(orig, n);
  1165. }
  1166. void
  1167. tscrollup(int orig, int n) {
  1168. int i;
  1169. Line temp;
  1170. LIMIT(n, 0, term.bot-orig+1);
  1171. tclearregion(0, orig, term.col-1, orig+n-1);
  1172. for(i = orig; i <= term.bot-n; i++) {
  1173. temp = term.line[i];
  1174. term.line[i] = term.line[i+n];
  1175. term.line[i+n] = temp;
  1176. term.dirty[i] = 1;
  1177. term.dirty[i+n] = 1;
  1178. }
  1179. selscroll(orig, -n);
  1180. }
  1181. void
  1182. selscroll(int orig, int n) {
  1183. if(sel.bx == -1)
  1184. return;
  1185. if(BETWEEN(sel.by, orig, term.bot) || BETWEEN(sel.ey, orig, term.bot)) {
  1186. if((sel.by += n) > term.bot || (sel.ey += n) < term.top) {
  1187. sel.bx = -1;
  1188. return;
  1189. }
  1190. if(sel.type == SEL_RECTANGULAR) {
  1191. if(sel.by < term.top)
  1192. sel.by = term.top;
  1193. if(sel.ey > term.bot)
  1194. sel.ey = term.bot;
  1195. } else {
  1196. if(sel.by < term.top) {
  1197. sel.by = term.top;
  1198. sel.bx = 0;
  1199. }
  1200. if(sel.ey > term.bot) {
  1201. sel.ey = term.bot;
  1202. sel.ex = term.col;
  1203. }
  1204. }
  1205. sel.b.y = sel.by, sel.b.x = sel.bx;
  1206. sel.e.y = sel.ey, sel.e.x = sel.ex;
  1207. }
  1208. }
  1209. void
  1210. tnewline(int first_col) {
  1211. int y = term.c.y;
  1212. if(y == term.bot) {
  1213. tscrollup(term.top, 1);
  1214. } else {
  1215. y++;
  1216. }
  1217. tmoveto(first_col ? 0 : term.c.x, y);
  1218. }
  1219. void
  1220. csiparse(void) {
  1221. char *p = csiescseq.buf, *np;
  1222. long int v;
  1223. csiescseq.narg = 0;
  1224. if(*p == '?') {
  1225. csiescseq.priv = 1;
  1226. p++;
  1227. }
  1228. csiescseq.buf[csiescseq.len] = '\0';
  1229. while(p < csiescseq.buf+csiescseq.len) {
  1230. np = NULL;
  1231. v = strtol(p, &np, 10);
  1232. if(np == p)
  1233. v = 0;
  1234. if(v == LONG_MAX || v == LONG_MIN)
  1235. v = -1;
  1236. csiescseq.arg[csiescseq.narg++] = v;
  1237. p = np;
  1238. if(*p != ';' || csiescseq.narg == ESC_ARG_SIZ)
  1239. break;
  1240. p++;
  1241. }
  1242. csiescseq.mode = *p;
  1243. }
  1244. /* for absolute user moves, when decom is set */
  1245. void
  1246. tmoveato(int x, int y) {
  1247. tmoveto(x, y + ((term.c.state & CURSOR_ORIGIN) ? term.top: 0));
  1248. }
  1249. void
  1250. tmoveto(int x, int y) {
  1251. int miny, maxy;
  1252. if(term.c.state & CURSOR_ORIGIN) {
  1253. miny = term.top;
  1254. maxy = term.bot;
  1255. } else {
  1256. miny = 0;
  1257. maxy = term.row - 1;
  1258. }
  1259. LIMIT(x, 0, term.col-1);
  1260. LIMIT(y, miny, maxy);
  1261. term.c.state &= ~CURSOR_WRAPNEXT;
  1262. term.c.x = x;
  1263. term.c.y = y;
  1264. }
  1265. void
  1266. tsetchar(char *c, Glyph *attr, int x, int y) {
  1267. static char *vt100_0[62] = { /* 0x41 - 0x7e */
  1268. "", "", "", "", "", "", "", /* A - G */
  1269. 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
  1270. 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
  1271. 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
  1272. "", "", "", "", "", "", "°", "±", /* ` - g */
  1273. "", "", "", "", "", "", "", "", /* h - o */
  1274. "", "", "", "", "", "", "", "", /* p - w */
  1275. "", "", "", "π", "", "£", "·", /* x - ~ */
  1276. };
  1277. /*
  1278. * The table is proudly stolen from rxvt.
  1279. */
  1280. if(attr->mode & ATTR_GFX) {
  1281. if(c[0] >= 0x41 && c[0] <= 0x7e
  1282. && vt100_0[c[0] - 0x41]) {
  1283. c = vt100_0[c[0] - 0x41];
  1284. }
  1285. }
  1286. term.dirty[y] = 1;
  1287. term.line[y][x] = *attr;
  1288. memcpy(term.line[y][x].c, c, UTF_SIZ);
  1289. }
  1290. void
  1291. tclearregion(int x1, int y1, int x2, int y2) {
  1292. int x, y, temp;
  1293. if(x1 > x2)
  1294. temp = x1, x1 = x2, x2 = temp;
  1295. if(y1 > y2)
  1296. temp = y1, y1 = y2, y2 = temp;
  1297. LIMIT(x1, 0, term.col-1);
  1298. LIMIT(x2, 0, term.col-1);
  1299. LIMIT(y1, 0, term.row-1);
  1300. LIMIT(y2, 0, term.row-1);
  1301. for(y = y1; y <= y2; y++) {
  1302. term.dirty[y] = 1;
  1303. for(x = x1; x <= x2; x++) {
  1304. if(selected(x, y))
  1305. selclear(NULL);
  1306. term.line[y][x] = term.c.attr;
  1307. memcpy(term.line[y][x].c, " ", 2);
  1308. }
  1309. }
  1310. }
  1311. void
  1312. tdeletechar(int n) {
  1313. int src = term.c.x + n;
  1314. int dst = term.c.x;
  1315. int size = term.col - src;
  1316. term.dirty[term.c.y] = 1;
  1317. if(src >= term.col) {
  1318. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1319. return;
  1320. }
  1321. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src],
  1322. size * sizeof(Glyph));
  1323. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  1324. }
  1325. void
  1326. tinsertblank(int n) {
  1327. int src = term.c.x;
  1328. int dst = src + n;
  1329. int size = term.col - dst;
  1330. term.dirty[term.c.y] = 1;
  1331. if(dst >= term.col) {
  1332. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1333. return;
  1334. }
  1335. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src],
  1336. size * sizeof(Glyph));
  1337. tclearregion(src, term.c.y, dst - 1, term.c.y);
  1338. }
  1339. void
  1340. tinsertblankline(int n) {
  1341. if(term.c.y < term.top || term.c.y > term.bot)
  1342. return;
  1343. tscrolldown(term.c.y, n);
  1344. }
  1345. void
  1346. tdeleteline(int n) {
  1347. if(term.c.y < term.top || term.c.y > term.bot)
  1348. return;
  1349. tscrollup(term.c.y, n);
  1350. }
  1351. void
  1352. tsetattr(int *attr, int l) {
  1353. int i;
  1354. for(i = 0; i < l; i++) {
  1355. switch(attr[i]) {
  1356. case 0:
  1357. term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE \
  1358. | ATTR_BOLD | ATTR_ITALIC \
  1359. | ATTR_BLINK);
  1360. term.c.attr.fg = defaultfg;
  1361. term.c.attr.bg = defaultbg;
  1362. break;
  1363. case 1:
  1364. term.c.attr.mode |= ATTR_BOLD;
  1365. break;
  1366. case 3:
  1367. term.c.attr.mode |= ATTR_ITALIC;
  1368. break;
  1369. case 4:
  1370. term.c.attr.mode |= ATTR_UNDERLINE;
  1371. break;
  1372. case 5: /* slow blink */
  1373. case 6: /* rapid blink */
  1374. term.c.attr.mode |= ATTR_BLINK;
  1375. break;
  1376. case 7:
  1377. term.c.attr.mode |= ATTR_REVERSE;
  1378. break;
  1379. case 21:
  1380. case 22:
  1381. term.c.attr.mode &= ~ATTR_BOLD;
  1382. break;
  1383. case 23:
  1384. term.c.attr.mode &= ~ATTR_ITALIC;
  1385. break;
  1386. case 24:
  1387. term.c.attr.mode &= ~ATTR_UNDERLINE;
  1388. break;
  1389. case 25:
  1390. case 26:
  1391. term.c.attr.mode &= ~ATTR_BLINK;
  1392. break;
  1393. case 27:
  1394. term.c.attr.mode &= ~ATTR_REVERSE;
  1395. break;
  1396. case 38:
  1397. if(i + 2 < l && attr[i + 1] == 5) {
  1398. i += 2;
  1399. if(BETWEEN(attr[i], 0, 255)) {
  1400. term.c.attr.fg = attr[i];
  1401. } else {
  1402. fprintf(stderr,
  1403. "erresc: bad fgcolor %d\n",
  1404. attr[i]);
  1405. }
  1406. } else {
  1407. fprintf(stderr,
  1408. "erresc(38): gfx attr %d unknown\n",
  1409. attr[i]);
  1410. }
  1411. break;
  1412. case 39:
  1413. term.c.attr.fg = defaultfg;
  1414. break;
  1415. case 48:
  1416. if(i + 2 < l && attr[i + 1] == 5) {
  1417. i += 2;
  1418. if(BETWEEN(attr[i], 0, 255)) {
  1419. term.c.attr.bg = attr[i];
  1420. } else {
  1421. fprintf(stderr,
  1422. "erresc: bad bgcolor %d\n",
  1423. attr[i]);
  1424. }
  1425. } else {
  1426. fprintf(stderr,
  1427. "erresc(48): gfx attr %d unknown\n",
  1428. attr[i]);
  1429. }
  1430. break;
  1431. case 49:
  1432. term.c.attr.bg = defaultbg;
  1433. break;
  1434. default:
  1435. if(BETWEEN(attr[i], 30, 37)) {
  1436. term.c.attr.fg = attr[i] - 30;
  1437. } else if(BETWEEN(attr[i], 40, 47)) {
  1438. term.c.attr.bg = attr[i] - 40;
  1439. } else if(BETWEEN(attr[i], 90, 97)) {
  1440. term.c.attr.fg = attr[i] - 90 + 8;
  1441. } else if(BETWEEN(attr[i], 100, 107)) {
  1442. term.c.attr.bg = attr[i] - 100 + 8;
  1443. } else {
  1444. fprintf(stderr,
  1445. "erresc(default): gfx attr %d unknown\n",
  1446. attr[i]), csidump();
  1447. }
  1448. break;
  1449. }
  1450. }
  1451. }
  1452. void
  1453. tsetscroll(int t, int b) {
  1454. int temp;
  1455. LIMIT(t, 0, term.row-1);
  1456. LIMIT(b, 0, term.row-1);
  1457. if(t > b) {
  1458. temp = t;
  1459. t = b;
  1460. b = temp;
  1461. }
  1462. term.top = t;
  1463. term.bot = b;
  1464. }
  1465. #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
  1466. void
  1467. tsetmode(bool priv, bool set, int *args, int narg) {
  1468. int *lim, mode;
  1469. bool alt;
  1470. for(lim = args + narg; args < lim; ++args) {
  1471. if(priv) {
  1472. switch(*args) {
  1473. break;
  1474. case 1: /* DECCKM -- Cursor key */
  1475. MODBIT(term.mode, set, MODE_APPCURSOR);
  1476. break;
  1477. case 5: /* DECSCNM -- Reverse video */
  1478. mode = term.mode;
  1479. MODBIT(term.mode, set, MODE_REVERSE);
  1480. if(mode != term.mode)
  1481. redraw(REDRAW_TIMEOUT);
  1482. break;
  1483. case 6: /* DECOM -- Origin */
  1484. MODBIT(term.c.state, set, CURSOR_ORIGIN);
  1485. tmoveato(0, 0);
  1486. break;
  1487. case 7: /* DECAWM -- Auto wrap */
  1488. MODBIT(term.mode, set, MODE_WRAP);
  1489. break;
  1490. case 0: /* Error (IGNORED) */
  1491. case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
  1492. case 3: /* DECCOLM -- Column (IGNORED) */
  1493. case 4: /* DECSCLM -- Scroll (IGNORED) */
  1494. case 8: /* DECARM -- Auto repeat (IGNORED) */
  1495. case 18: /* DECPFF -- Printer feed (IGNORED) */
  1496. case 19: /* DECPEX -- Printer extent (IGNORED) */
  1497. case 42: /* DECNRCM -- National characters (IGNORED) */
  1498. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1499. break;
  1500. case 25: /* DECTCEM -- Text Cursor Enable Mode */
  1501. MODBIT(term.mode, !set, MODE_HIDE);
  1502. break;
  1503. case 1000: /* 1000,1002: enable xterm mouse report */
  1504. MODBIT(term.mode, set, MODE_MOUSEBTN);
  1505. MODBIT(term.mode, 0, MODE_MOUSEMOTION);
  1506. break;
  1507. case 1002:
  1508. MODBIT(term.mode, set, MODE_MOUSEMOTION);
  1509. MODBIT(term.mode, 0, MODE_MOUSEBTN);
  1510. break;
  1511. case 1006:
  1512. MODBIT(term.mode, set, MODE_MOUSESGR);
  1513. break;
  1514. case 1034:
  1515. MODBIT(term.mode, set, MODE_8BIT);
  1516. break;
  1517. case 1049: /* = 1047 and 1048 */
  1518. case 47:
  1519. case 1047:
  1520. if (!allowaltscreen)
  1521. break;
  1522. alt = IS_SET(MODE_ALTSCREEN);
  1523. if(alt) {
  1524. tclearregion(0, 0, term.col-1,
  1525. term.row-1);
  1526. }
  1527. if(set ^ alt) /* set is always 1 or 0 */
  1528. tswapscreen();
  1529. if(*args != 1049)
  1530. break;
  1531. /* FALLTRU */
  1532. case 1048:
  1533. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1534. break;
  1535. default:
  1536. fprintf(stderr,
  1537. "erresc: unknown private set/reset mode %d\n",
  1538. *args);
  1539. break;
  1540. }
  1541. } else {
  1542. switch(*args) {
  1543. case 0: /* Error (IGNORED) */
  1544. break;
  1545. case 2: /* KAM -- keyboard action */
  1546. MODBIT(term.mode, set, MODE_KBDLOCK);
  1547. break;
  1548. case 4: /* IRM -- Insertion-replacement */
  1549. MODBIT(term.mode, set, MODE_INSERT);
  1550. break;
  1551. case 12: /* SRM -- Send/Receive */
  1552. MODBIT(term.mode, !set, MODE_ECHO);
  1553. break;
  1554. case 20: /* LNM -- Linefeed/new line */
  1555. MODBIT(term.mode, set, MODE_CRLF);
  1556. break;
  1557. default:
  1558. fprintf(stderr,
  1559. "erresc: unknown set/reset mode %d\n",
  1560. *args);
  1561. break;
  1562. }
  1563. }
  1564. }
  1565. }
  1566. #undef MODBIT
  1567. void
  1568. csihandle(void) {
  1569. switch(csiescseq.mode) {
  1570. default:
  1571. unknown:
  1572. fprintf(stderr, "erresc: unknown csi ");
  1573. csidump();
  1574. /* die(""); */
  1575. break;
  1576. case '@': /* ICH -- Insert <n> blank char */
  1577. DEFAULT(csiescseq.arg[0], 1);
  1578. tinsertblank(csiescseq.arg[0]);
  1579. break;
  1580. case 'A': /* CUU -- Cursor <n> Up */
  1581. DEFAULT(csiescseq.arg[0], 1);
  1582. tmoveto(term.c.x, term.c.y-csiescseq.arg[0]);
  1583. break;
  1584. case 'B': /* CUD -- Cursor <n> Down */
  1585. case 'e': /* VPR --Cursor <n> Down */
  1586. DEFAULT(csiescseq.arg[0], 1);
  1587. tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
  1588. break;
  1589. case 'c': /* DA -- Device Attributes */
  1590. if(csiescseq.arg[0] == 0)
  1591. ttywrite(VT102ID, sizeof(VT102ID) - 1);
  1592. break;
  1593. case 'C': /* CUF -- Cursor <n> Forward */
  1594. case 'a': /* HPR -- Cursor <n> Forward */
  1595. DEFAULT(csiescseq.arg[0], 1);
  1596. tmoveto(term.c.x+csiescseq.arg[0], term.c.y);
  1597. break;
  1598. case 'D': /* CUB -- Cursor <n> Backward */
  1599. DEFAULT(csiescseq.arg[0], 1);
  1600. tmoveto(term.c.x-csiescseq.arg[0], term.c.y);
  1601. break;
  1602. case 'E': /* CNL -- Cursor <n> Down and first col */
  1603. DEFAULT(csiescseq.arg[0], 1);
  1604. tmoveto(0, term.c.y+csiescseq.arg[0]);
  1605. break;
  1606. case 'F': /* CPL -- Cursor <n> Up and first col */
  1607. DEFAULT(csiescseq.arg[0], 1);
  1608. tmoveto(0, term.c.y-csiescseq.arg[0]);
  1609. break;
  1610. case 'g': /* TBC -- Tabulation clear */
  1611. switch(csiescseq.arg[0]) {
  1612. case 0: /* clear current tab stop */
  1613. term.tabs[term.c.x] = 0;
  1614. break;
  1615. case 3: /* clear all the tabs */
  1616. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  1617. break;
  1618. default:
  1619. goto unknown;
  1620. }
  1621. break;
  1622. case 'G': /* CHA -- Move to <col> */
  1623. case '`': /* HPA */
  1624. DEFAULT(csiescseq.arg[0], 1);
  1625. tmoveto(csiescseq.arg[0]-1, term.c.y);
  1626. break;
  1627. case 'H': /* CUP -- Move to <row> <col> */
  1628. case 'f': /* HVP */
  1629. DEFAULT(csiescseq.arg[0], 1);
  1630. DEFAULT(csiescseq.arg[1], 1);
  1631. tmoveato(csiescseq.arg[1]-1, csiescseq.arg[0]-1);
  1632. break;
  1633. case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
  1634. DEFAULT(csiescseq.arg[0], 1);
  1635. while(csiescseq.arg[0]--)
  1636. tputtab(1);
  1637. break;
  1638. case 'J': /* ED -- Clear screen */
  1639. sel.bx = -1;
  1640. switch(csiescseq.arg[0]) {
  1641. case 0: /* below */
  1642. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1643. if(term.c.y < term.row-1) {
  1644. tclearregion(0, term.c.y+1, term.col-1,
  1645. term.row-1);
  1646. }
  1647. break;
  1648. case 1: /* above */
  1649. if(term.c.y > 1)
  1650. tclearregion(0, 0, term.col-1, term.c.y-1);
  1651. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1652. break;
  1653. case 2: /* all */
  1654. tclearregion(0, 0, term.col-1, term.row-1);
  1655. break;
  1656. default:
  1657. goto unknown;
  1658. }
  1659. break;
  1660. case 'K': /* EL -- Clear line */
  1661. switch(csiescseq.arg[0]) {
  1662. case 0: /* right */
  1663. tclearregion(term.c.x, term.c.y, term.col-1,
  1664. term.c.y);
  1665. break;
  1666. case 1: /* left */
  1667. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1668. break;
  1669. case 2: /* all */
  1670. tclearregion(0, term.c.y, term.col-1, term.c.y);
  1671. break;
  1672. }
  1673. break;
  1674. case 'S': /* SU -- Scroll <n> line up */
  1675. DEFAULT(csiescseq.arg[0], 1);
  1676. tscrollup(term.top, csiescseq.arg[0]);
  1677. break;
  1678. case 'T': /* SD -- Scroll <n> line down */
  1679. DEFAULT(csiescseq.arg[0], 1);
  1680. tscrolldown(term.top, csiescseq.arg[0]);
  1681. break;
  1682. case 'L': /* IL -- Insert <n> blank lines */
  1683. DEFAULT(csiescseq.arg[0], 1);
  1684. tinsertblankline(csiescseq.arg[0]);
  1685. break;
  1686. case 'l': /* RM -- Reset Mode */
  1687. tsetmode(csiescseq.priv, 0, csiescseq.arg, csiescseq.narg);
  1688. break;
  1689. case 'M': /* DL -- Delete <n> lines */
  1690. DEFAULT(csiescseq.arg[0], 1);
  1691. tdeleteline(csiescseq.arg[0]);
  1692. break;
  1693. case 'X': /* ECH -- Erase <n> char */
  1694. DEFAULT(csiescseq.arg[0], 1);
  1695. tclearregion(term.c.x, term.c.y,
  1696. term.c.x + csiescseq.arg[0] - 1, term.c.y);
  1697. break;
  1698. case 'P': /* DCH -- Delete <n> char */
  1699. DEFAULT(csiescseq.arg[0], 1);
  1700. tdeletechar(csiescseq.arg[0]);
  1701. break;
  1702. case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
  1703. DEFAULT(csiescseq.arg[0], 1);
  1704. while(csiescseq.arg[0]--)
  1705. tputtab(0);
  1706. break;
  1707. case 'd': /* VPA -- Move to <row> */
  1708. DEFAULT(csiescseq.arg[0], 1);
  1709. tmoveato(term.c.x, csiescseq.arg[0]-1);
  1710. break;
  1711. case 'h': /* SM -- Set terminal mode */
  1712. tsetmode(csiescseq.priv, 1, csiescseq.arg, csiescseq.narg);
  1713. break;
  1714. case 'm': /* SGR -- Terminal attribute (color) */
  1715. tsetattr(csiescseq.arg, csiescseq.narg);
  1716. break;
  1717. case 'r': /* DECSTBM -- Set Scrolling Region */
  1718. if(csiescseq.priv) {
  1719. goto unknown;
  1720. } else {
  1721. DEFAULT(csiescseq.arg[0], 1);
  1722. DEFAULT(csiescseq.arg[1], term.row);
  1723. tsetscroll(csiescseq.arg[0]-1, csiescseq.arg[1]-1);
  1724. tmoveato(0, 0);
  1725. }
  1726. break;
  1727. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  1728. tcursor(CURSOR_SAVE);
  1729. break;
  1730. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  1731. tcursor(CURSOR_LOAD);
  1732. break;
  1733. }
  1734. }
  1735. void
  1736. csidump(void) {
  1737. int i;
  1738. uint c;
  1739. printf("ESC[");
  1740. for(i = 0; i < csiescseq.len; i++) {
  1741. c = csiescseq.buf[i] & 0xff;
  1742. if(isprint(c)) {
  1743. putchar(c);
  1744. } else if(c == '\n') {
  1745. printf("(\\n)");
  1746. } else if(c == '\r') {
  1747. printf("(\\r)");
  1748. } else if(c == 0x1b) {
  1749. printf("(\\e)");
  1750. } else {
  1751. printf("(%02x)", c);
  1752. }
  1753. }
  1754. putchar('\n');
  1755. }
  1756. void
  1757. csireset(void) {
  1758. memset(&csiescseq, 0, sizeof(csiescseq));
  1759. }
  1760. void
  1761. strhandle(void) {
  1762. char *p = NULL;
  1763. int i, j, narg;
  1764. strparse();
  1765. narg = strescseq.narg;
  1766. switch(strescseq.type) {
  1767. case ']': /* OSC -- Operating System Command */
  1768. switch(i = atoi(strescseq.args[0])) {
  1769. case 0:
  1770. case 1:
  1771. case 2:
  1772. if(narg > 1)
  1773. xsettitle(strescseq.args[1]);
  1774. break;
  1775. case 4: /* color set */
  1776. if(narg < 3)
  1777. break;
  1778. p = strescseq.args[2];
  1779. /* fall through */
  1780. case 104: /* color reset, here p = NULL */
  1781. j = (narg > 1) ? atoi(strescseq.args[1]) : -1;
  1782. if (!xsetcolorname(j, p)) {
  1783. fprintf(stderr, "erresc: invalid color %s\n", p);
  1784. } else {
  1785. /*
  1786. * TODO if defaultbg color is changed, borders
  1787. * are dirty
  1788. */
  1789. redraw(0);
  1790. }
  1791. break;
  1792. default:
  1793. fprintf(stderr, "erresc: unknown str ");
  1794. strdump();
  1795. break;
  1796. }
  1797. break;
  1798. case 'k': /* old title set compatibility */
  1799. xsettitle(strescseq.args[0]);
  1800. break;
  1801. case 'P': /* DSC -- Device Control String */
  1802. case '_': /* APC -- Application Program Command */
  1803. case '^': /* PM -- Privacy Message */
  1804. default:
  1805. fprintf(stderr, "erresc: unknown str ");
  1806. strdump();
  1807. /* die(""); */
  1808. break;
  1809. }
  1810. }
  1811. void
  1812. strparse(void) {
  1813. char *p = strescseq.buf;
  1814. strescseq.narg = 0;
  1815. strescseq.buf[strescseq.len] = '\0';
  1816. while(p && strescseq.narg < STR_ARG_SIZ)
  1817. strescseq.args[strescseq.narg++] = strsep(&p, ";");
  1818. }
  1819. void
  1820. strdump(void) {
  1821. int i;
  1822. uint c;
  1823. printf("ESC%c", strescseq.type);
  1824. for(i = 0; i < strescseq.len; i++) {
  1825. c = strescseq.buf[i] & 0xff;
  1826. if(c == '\0') {
  1827. return;
  1828. } else if(isprint(c)) {
  1829. putchar(c);
  1830. } else if(c == '\n') {
  1831. printf("(\\n)");
  1832. } else if(c == '\r') {
  1833. printf("(\\r)");
  1834. } else if(c == 0x1b) {
  1835. printf("(\\e)");
  1836. } else {
  1837. printf("(%02x)", c);
  1838. }
  1839. }
  1840. printf("ESC\\\n");
  1841. }
  1842. void
  1843. strreset(void) {
  1844. memset(&strescseq, 0, sizeof(strescseq));
  1845. }
  1846. void
  1847. tputtab(bool forward) {
  1848. uint x = term.c.x;
  1849. if(forward) {
  1850. if(x == term.col)
  1851. return;
  1852. for(++x; x < term.col && !term.tabs[x]; ++x)
  1853. /* nothing */ ;
  1854. } else {
  1855. if(x == 0)
  1856. return;
  1857. for(--x; x > 0 && !term.tabs[x]; --x)
  1858. /* nothing */ ;
  1859. }
  1860. tmoveto(x, term.c.y);
  1861. }
  1862. void
  1863. techo(char *buf, int len) {
  1864. for(; len > 0; buf++, len--) {
  1865. char c = *buf;
  1866. if(c == '\033') { /* escape */
  1867. tputc("^", 1);
  1868. tputc("[", 1);
  1869. } else if(c < '\x20') { /* control code */
  1870. if(c != '\n' && c != '\r' && c != '\t') {
  1871. c |= '\x40';
  1872. tputc("^", 1);
  1873. }
  1874. tputc(&c, 1);
  1875. } else {
  1876. break;
  1877. }
  1878. }
  1879. if(len)
  1880. tputc(buf, len);
  1881. }
  1882. void
  1883. tputc(char *c, int len) {
  1884. uchar ascii = *c;
  1885. bool control = ascii < '\x20' || ascii == 0177;
  1886. if(iofd != -1) {
  1887. if(xwrite(iofd, c, len) < 0) {
  1888. fprintf(stderr, "Error writing in %s:%s\n",
  1889. opt_io, strerror(errno));
  1890. close(iofd);
  1891. iofd = -1;
  1892. }
  1893. }
  1894. /*
  1895. * STR sequences must be checked before anything else
  1896. * because it can use some control codes as part of the sequence.
  1897. */
  1898. if(term.esc & ESC_STR) {
  1899. switch(ascii) {
  1900. case '\033':
  1901. term.esc = ESC_START | ESC_STR_END;
  1902. break;
  1903. case '\a': /* backwards compatibility to xterm */
  1904. term.esc = 0;
  1905. strhandle();
  1906. break;
  1907. default:
  1908. if(strescseq.len + len < sizeof(strescseq.buf) - 1) {
  1909. memmove(&strescseq.buf[strescseq.len], c, len);
  1910. strescseq.len += len;
  1911. } else {
  1912. /*
  1913. * Here is a bug in terminals. If the user never sends
  1914. * some code to stop the str or esc command, then st
  1915. * will stop responding. But this is better than
  1916. * silently failing with unknown characters. At least
  1917. * then users will report back.
  1918. *
  1919. * In the case users ever get fixed, here is the code:
  1920. */
  1921. /*
  1922. * term.esc = 0;
  1923. * strhandle();
  1924. */
  1925. }
  1926. }
  1927. return;
  1928. }
  1929. /*
  1930. * Actions of control codes must be performed as soon they arrive
  1931. * because they can be embedded inside a control sequence, and
  1932. * they must not cause conflicts with sequences.
  1933. */
  1934. if(control) {
  1935. switch(ascii) {
  1936. case '\t': /* HT */
  1937. tputtab(1);
  1938. return;
  1939. case '\b': /* BS */
  1940. tmoveto(term.c.x-1, term.c.y);
  1941. return;
  1942. case '\r': /* CR */
  1943. tmoveto(0, term.c.y);
  1944. return;
  1945. case '\f': /* LF */
  1946. case '\v': /* VT */
  1947. case '\n': /* LF */
  1948. /* go to first col if the mode is set */
  1949. tnewline(IS_SET(MODE_CRLF));
  1950. return;
  1951. case '\a': /* BEL */
  1952. if(!(xw.state & WIN_FOCUSED))
  1953. xseturgency(1);
  1954. return;
  1955. case '\033': /* ESC */
  1956. csireset();
  1957. term.esc = ESC_START;
  1958. return;
  1959. case '\016': /* SO */
  1960. case '\017': /* SI */
  1961. /*
  1962. * Different charsets are hard to handle. Applications
  1963. * should use the right alt charset escapes for the
  1964. * only reason they still exist: line drawing. The
  1965. * rest is incompatible history st should not support.
  1966. */
  1967. return;
  1968. case '\032': /* SUB */
  1969. case '\030': /* CAN */
  1970. csireset();
  1971. return;
  1972. case '\005': /* ENQ (IGNORED) */
  1973. case '\000': /* NUL (IGNORED) */
  1974. case '\021': /* XON (IGNORED) */
  1975. case '\023': /* XOFF (IGNORED) */
  1976. case 0177: /* DEL (IGNORED) */
  1977. return;
  1978. }
  1979. } else if(term.esc & ESC_START) {
  1980. if(term.esc & ESC_CSI) {
  1981. csiescseq.buf[csiescseq.len++] = ascii;
  1982. if(BETWEEN(ascii, 0x40, 0x7E)
  1983. || csiescseq.len >= \
  1984. sizeof(csiescseq.buf)-1) {
  1985. term.esc = 0;
  1986. csiparse();
  1987. csihandle();
  1988. }
  1989. } else if(term.esc & ESC_STR_END) {
  1990. term.esc = 0;
  1991. if(ascii == '\\')
  1992. strhandle();
  1993. } else if(term.esc & ESC_ALTCHARSET) {
  1994. switch(ascii) {
  1995. case '0': /* Line drawing set */
  1996. term.c.attr.mode |= ATTR_GFX;
  1997. break;
  1998. case 'B': /* USASCII */
  1999. term.c.attr.mode &= ~ATTR_GFX;
  2000. break;
  2001. case 'A': /* UK (IGNORED) */
  2002. case '<': /* multinational charset (IGNORED) */
  2003. case '5': /* Finnish (IGNORED) */
  2004. case 'C': /* Finnish (IGNORED) */
  2005. case 'K': /* German (IGNORED) */
  2006. break;
  2007. default:
  2008. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  2009. }
  2010. term.esc = 0;
  2011. } else if(term.esc & ESC_TEST) {
  2012. if(ascii == '8') { /* DEC screen alignment test. */
  2013. char E[UTF_SIZ] = "E";
  2014. int x, y;
  2015. for(x = 0; x < term.col; ++x) {
  2016. for(y = 0; y < term.row; ++y)
  2017. tsetchar(E, &term.c.attr, x, y);
  2018. }
  2019. }
  2020. term.esc = 0;
  2021. } else {
  2022. switch(ascii) {
  2023. case '[':
  2024. term.esc |= ESC_CSI;
  2025. break;
  2026. case '#':
  2027. term.esc |= ESC_TEST;
  2028. break;
  2029. case 'P': /* DCS -- Device Control String */
  2030. case '_': /* APC -- Application Program Command */
  2031. case '^': /* PM -- Privacy Message */
  2032. case ']': /* OSC -- Operating System Command */
  2033. case 'k': /* old title set compatibility */
  2034. strreset();
  2035. strescseq.type = ascii;
  2036. term.esc |= ESC_STR;
  2037. break;
  2038. case '(': /* set primary charset G0 */
  2039. term.esc |= ESC_ALTCHARSET;
  2040. break;
  2041. case ')': /* set secondary charset G1 (IGNORED) */
  2042. case '*': /* set tertiary charset G2 (IGNORED) */
  2043. case '+': /* set quaternary charset G3 (IGNORED) */
  2044. term.esc = 0;
  2045. break;
  2046. case 'D': /* IND -- Linefeed */
  2047. if(term.c.y == term.bot) {
  2048. tscrollup(term.top, 1);
  2049. } else {
  2050. tmoveto(term.c.x, term.c.y+1);
  2051. }
  2052. term.esc = 0;
  2053. break;
  2054. case 'E': /* NEL -- Next line */
  2055. tnewline(1); /* always go to first col */
  2056. term.esc = 0;
  2057. break;
  2058. case 'H': /* HTS -- Horizontal tab stop */
  2059. term.tabs[term.c.x] = 1;
  2060. term.esc = 0;
  2061. break;
  2062. case 'M': /* RI -- Reverse index */
  2063. if(term.c.y == term.top) {
  2064. tscrolldown(term.top, 1);
  2065. } else {
  2066. tmoveto(term.c.x, term.c.y-1);
  2067. }
  2068. term.esc = 0;
  2069. break;
  2070. case 'Z': /* DECID -- Identify Terminal */
  2071. ttywrite(VT102ID, sizeof(VT102ID) - 1);
  2072. term.esc = 0;
  2073. break;
  2074. case 'c': /* RIS -- Reset to inital state */
  2075. treset();
  2076. term.esc = 0;
  2077. xresettitle();
  2078. break;
  2079. case '=': /* DECPAM -- Application keypad */
  2080. term.mode |= MODE_APPKEYPAD;
  2081. term.esc = 0;
  2082. break;
  2083. case '>': /* DECPNM -- Normal keypad */
  2084. term.mode &= ~MODE_APPKEYPAD;
  2085. term.esc = 0;
  2086. break;
  2087. case '7': /* DECSC -- Save Cursor */
  2088. tcursor(CURSOR_SAVE);
  2089. term.esc = 0;
  2090. break;
  2091. case '8': /* DECRC -- Restore Cursor */
  2092. tcursor(CURSOR_LOAD);
  2093. term.esc = 0;
  2094. break;
  2095. case '\\': /* ST -- Stop */
  2096. term.esc = 0;
  2097. break;
  2098. default:
  2099. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  2100. (uchar) ascii, isprint(ascii)? ascii:'.');
  2101. term.esc = 0;
  2102. }
  2103. }
  2104. /*
  2105. * All characters which form part of a sequence are not
  2106. * printed
  2107. */
  2108. return;
  2109. }
  2110. /*
  2111. * Display control codes only if we are in graphic mode
  2112. */
  2113. if(control && !(term.c.attr.mode & ATTR_GFX))
  2114. return;
  2115. if(sel.bx != -1 && BETWEEN(term.c.y, sel.by, sel.ey))
  2116. sel.bx = -1;
  2117. if(IS_SET(MODE_WRAP) && (term.c.state & CURSOR_WRAPNEXT)) {
  2118. term.line[term.c.y][term.c.x].mode |= ATTR_WRAP;
  2119. tnewline(1);
  2120. }
  2121. if(IS_SET(MODE_INSERT) && term.c.x+1 < term.col) {
  2122. memmove(&term.line[term.c.y][term.c.x+1],
  2123. &term.line[term.c.y][term.c.x],
  2124. (term.col - term.c.x - 1) * sizeof(Glyph));
  2125. }
  2126. tsetchar(c, &term.c.attr, term.c.x, term.c.y);
  2127. if(term.c.x+1 < term.col) {
  2128. tmoveto(term.c.x+1, term.c.y);
  2129. } else {
  2130. term.c.state |= CURSOR_WRAPNEXT;
  2131. }
  2132. }
  2133. int
  2134. tresize(int col, int row) {
  2135. int i;
  2136. int minrow = MIN(row, term.row);
  2137. int mincol = MIN(col, term.col);
  2138. int slide = term.c.y - row + 1;
  2139. bool *bp;
  2140. Line *orig;
  2141. if(col < 1 || row < 1)
  2142. return 0;
  2143. /* free unneeded rows */
  2144. i = 0;
  2145. if(slide > 0) {
  2146. /*
  2147. * slide screen to keep cursor where we expect it -
  2148. * tscrollup would work here, but we can optimize to
  2149. * memmove because we're freeing the earlier lines
  2150. */
  2151. for(/* i = 0 */; i < slide; i++) {
  2152. free(term.line[i]);
  2153. free(term.alt[i]);
  2154. }
  2155. memmove(term.line, term.line + slide, row * sizeof(Line));
  2156. memmove(term.alt, term.alt + slide, row * sizeof(Line));
  2157. }
  2158. for(i += row; i < term.row; i++) {
  2159. free(term.line[i]);
  2160. free(term.alt[i]);
  2161. }
  2162. /* resize to new height */
  2163. term.line = xrealloc(term.line, row * sizeof(Line));
  2164. term.alt = xrealloc(term.alt, row * sizeof(Line));
  2165. term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
  2166. term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
  2167. /* resize each row to new width, zero-pad if needed */
  2168. for(i = 0; i < minrow; i++) {
  2169. term.dirty[i] = 1;
  2170. term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
  2171. term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
  2172. }
  2173. /* allocate any new rows */
  2174. for(/* i == minrow */; i < row; i++) {
  2175. term.dirty[i] = 1;
  2176. term.line[i] = xcalloc(col, sizeof(Glyph));
  2177. term.alt [i] = xcalloc(col, sizeof(Glyph));
  2178. }
  2179. if(col > term.col) {
  2180. bp = term.tabs + term.col;
  2181. memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
  2182. while(--bp > term.tabs && !*bp)
  2183. /* nothing */ ;
  2184. for(bp += tabspaces; bp < term.tabs + col; bp += tabspaces)
  2185. *bp = 1;
  2186. }
  2187. /* update terminal size */
  2188. term.col = col;
  2189. term.row = row;
  2190. /* reset scrolling region */
  2191. tsetscroll(0, row-1);
  2192. /* make use of the LIMIT in tmoveto */
  2193. tmoveto(term.c.x, term.c.y);
  2194. /* Clearing both screens */
  2195. orig = term.line;
  2196. do {
  2197. if(mincol < col && 0 < minrow) {
  2198. tclearregion(mincol, 0, col - 1, minrow - 1);
  2199. }
  2200. if(0 < col && minrow < row) {
  2201. tclearregion(0, minrow, col - 1, row - 1);
  2202. }
  2203. tswapscreen();
  2204. } while(orig != term.line);
  2205. return (slide > 0);
  2206. }
  2207. void
  2208. xresize(int col, int row) {
  2209. xw.tw = MAX(1, col * xw.cw);
  2210. xw.th = MAX(1, row * xw.ch);
  2211. XFreePixmap(xw.dpy, xw.buf);
  2212. xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.w, xw.h,
  2213. DefaultDepth(xw.dpy, xw.scr));
  2214. XftDrawChange(xw.draw, xw.buf);
  2215. xclear(0, 0, xw.w, xw.h);
  2216. }
  2217. static inline ushort
  2218. sixd_to_16bit(int x) {
  2219. return x == 0 ? 0 : 0x3737 + 0x2828 * x;
  2220. }
  2221. void
  2222. xloadcols(void) {
  2223. int i, r, g, b;
  2224. XRenderColor color = { .alpha = 0xffff };
  2225. /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
  2226. for(i = 0; i < LEN(colorname); i++) {
  2227. if(!colorname[i])
  2228. continue;
  2229. if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, colorname[i], &dc.col[i])) {
  2230. die("Could not allocate color '%s'\n", colorname[i]);
  2231. }
  2232. }
  2233. /* load colors [16-255] ; same colors as xterm */
  2234. for(i = 16, r = 0; r < 6; r++) {
  2235. for(g = 0; g < 6; g++) {
  2236. for(b = 0; b < 6; b++) {
  2237. color.red = sixd_to_16bit(r);
  2238. color.green = sixd_to_16bit(g);
  2239. color.blue = sixd_to_16bit(b);
  2240. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &dc.col[i])) {
  2241. die("Could not allocate color %d\n", i);
  2242. }
  2243. i++;
  2244. }
  2245. }
  2246. }
  2247. for(r = 0; r < 24; r++, i++) {
  2248. color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
  2249. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color,
  2250. &dc.col[i])) {
  2251. die("Could not allocate color %d\n", i);
  2252. }
  2253. }
  2254. }
  2255. int
  2256. xsetcolorname(int x, const char *name) {
  2257. XRenderColor color = { .alpha = 0xffff };
  2258. Colour colour;
  2259. if (x < 0 || x > LEN(colorname))
  2260. return -1;
  2261. if(!name) {
  2262. if(16 <= x && x < 16 + 216) {
  2263. int r = (x - 16) / 36, g = ((x - 16) % 36) / 6, b = (x - 16) % 6;
  2264. color.red = sixd_to_16bit(r);
  2265. color.green = sixd_to_16bit(g);
  2266. color.blue = sixd_to_16bit(b);
  2267. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &colour))
  2268. return 0; /* something went wrong */
  2269. dc.col[x] = colour;
  2270. return 1;
  2271. } else if (16 + 216 <= x && x < 256) {
  2272. color.red = color.green = color.blue = 0x0808 + 0x0a0a * (x - (16 + 216));
  2273. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &colour))
  2274. return 0; /* something went wrong */
  2275. dc.col[x] = colour;
  2276. return 1;
  2277. } else {
  2278. name = colorname[x];
  2279. }
  2280. }
  2281. if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, &colour))
  2282. return 0;
  2283. dc.col[x] = colour;
  2284. return 1;
  2285. }
  2286. void
  2287. xtermclear(int col1, int row1, int col2, int row2) {
  2288. XftDrawRect(xw.draw,
  2289. &dc.col[IS_SET(MODE_REVERSE) ? defaultfg : defaultbg],
  2290. borderpx + col1 * xw.cw,
  2291. borderpx + row1 * xw.ch,
  2292. (col2-col1+1) * xw.cw,
  2293. (row2-row1+1) * xw.ch);
  2294. }
  2295. /*
  2296. * Absolute coordinates.
  2297. */
  2298. void
  2299. xclear(int x1, int y1, int x2, int y2) {
  2300. XftDrawRect(xw.draw,
  2301. &dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg],
  2302. x1, y1, x2-x1, y2-y1);
  2303. }
  2304. void
  2305. xhints(void) {
  2306. XClassHint class = {opt_class ? opt_class : termname, termname};
  2307. XWMHints wm = {.flags = InputHint, .input = 1};
  2308. XSizeHints *sizeh = NULL;
  2309. sizeh = XAllocSizeHints();
  2310. if(xw.isfixed == False) {
  2311. sizeh->flags = PSize | PResizeInc | PBaseSize;
  2312. sizeh->height = xw.h;
  2313. sizeh->width = xw.w;
  2314. sizeh->height_inc = xw.ch;
  2315. sizeh->width_inc = xw.cw;
  2316. sizeh->base_height = 2 * borderpx;
  2317. sizeh->base_width = 2 * borderpx;
  2318. } else {
  2319. sizeh->flags = PMaxSize | PMinSize;
  2320. sizeh->min_width = sizeh->max_width = xw.fw;
  2321. sizeh->min_height = sizeh->max_height = xw.fh;
  2322. }
  2323. XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm, &class);
  2324. XFree(sizeh);
  2325. }
  2326. int
  2327. xloadfont(Font *f, FcPattern *pattern) {
  2328. FcPattern *match;
  2329. FcResult result;
  2330. match = FcFontMatch(NULL, pattern, &result);
  2331. if(!match)
  2332. return 1;
  2333. if(!(f->set = FcFontSort(0, match, FcTrue, 0, &result))) {
  2334. FcPatternDestroy(match);
  2335. return 1;
  2336. }
  2337. if(!(f->match = XftFontOpenPattern(xw.dpy, match))) {
  2338. FcPatternDestroy(match);
  2339. return 1;
  2340. }
  2341. f->pattern = FcPatternDuplicate(pattern);
  2342. f->ascent = f->match->ascent;
  2343. f->descent = f->match->descent;
  2344. f->lbearing = 0;
  2345. f->rbearing = f->match->max_advance_width;
  2346. f->height = f->ascent + f->descent;
  2347. f->width = f->lbearing + f->rbearing;
  2348. return 0;
  2349. }
  2350. void
  2351. xloadfonts(char *fontstr, int fontsize) {
  2352. FcPattern *pattern;
  2353. FcResult result;
  2354. double fontval;
  2355. if(fontstr[0] == '-') {
  2356. pattern = XftXlfdParse(fontstr, False, False);
  2357. } else {
  2358. pattern = FcNameParse((FcChar8 *)fontstr);
  2359. }
  2360. if(!pattern)
  2361. die("st: can't open font %s\n", fontstr);
  2362. if(fontsize > 0) {
  2363. FcPatternDel(pattern, FC_PIXEL_SIZE);
  2364. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize);
  2365. usedfontsize = fontsize;
  2366. } else {
  2367. result = FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval);
  2368. if(result == FcResultMatch) {
  2369. usedfontsize = (int)fontval;
  2370. } else {
  2371. /*
  2372. * Default font size is 12, if none given. This is to
  2373. * have a known usedfontsize value.
  2374. */
  2375. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
  2376. usedfontsize = 12;
  2377. }
  2378. }
  2379. FcConfigSubstitute(0, pattern, FcMatchPattern);
  2380. FcDefaultSubstitute(pattern);
  2381. if(xloadfont(&dc.font, pattern))
  2382. die("st: can't open font %s\n", fontstr);
  2383. /* Setting character width and height. */
  2384. xw.cw = dc.font.width;
  2385. xw.ch = dc.font.height;
  2386. FcPatternDel(pattern, FC_SLANT);
  2387. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
  2388. if(xloadfont(&dc.ifont, pattern))
  2389. die("st: can't open font %s\n", fontstr);
  2390. FcPatternDel(pattern, FC_WEIGHT);
  2391. FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
  2392. if(xloadfont(&dc.ibfont, pattern))
  2393. die("st: can't open font %s\n", fontstr);
  2394. FcPatternDel(pattern, FC_SLANT);
  2395. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
  2396. if(xloadfont(&dc.bfont, pattern))
  2397. die("st: can't open font %s\n", fontstr);
  2398. FcPatternDestroy(pattern);
  2399. }
  2400. void
  2401. xunloadfonts(void) {
  2402. int i, ip;
  2403. /*
  2404. * Free the loaded fonts in the font cache. This is done backwards
  2405. * from the frccur.
  2406. */
  2407. for(i = 0, ip = frccur; i < frclen; i++, ip--) {
  2408. if(ip < 0)
  2409. ip = LEN(frc) - 1;
  2410. XftFontClose(xw.dpy, frc[ip].font);
  2411. }
  2412. frccur = -1;
  2413. frclen = 0;
  2414. XftFontClose(xw.dpy, dc.font.match);
  2415. FcPatternDestroy(dc.font.pattern);
  2416. FcFontSetDestroy(dc.font.set);
  2417. XftFontClose(xw.dpy, dc.bfont.match);
  2418. FcPatternDestroy(dc.bfont.pattern);
  2419. FcFontSetDestroy(dc.bfont.set);
  2420. XftFontClose(xw.dpy, dc.ifont.match);
  2421. FcPatternDestroy(dc.ifont.pattern);
  2422. FcFontSetDestroy(dc.ifont.set);
  2423. XftFontClose(xw.dpy, dc.ibfont.match);
  2424. FcPatternDestroy(dc.ibfont.pattern);
  2425. FcFontSetDestroy(dc.ibfont.set);
  2426. }
  2427. void
  2428. xzoom(const Arg *arg) {
  2429. xunloadfonts();
  2430. xloadfonts(usedfont, usedfontsize + arg->i);
  2431. cresize(0, 0);
  2432. redraw(0);
  2433. }
  2434. void
  2435. xinit(void) {
  2436. XSetWindowAttributes attrs;
  2437. XGCValues gcvalues;
  2438. Cursor cursor;
  2439. Window parent;
  2440. int sw, sh;
  2441. if(!(xw.dpy = XOpenDisplay(NULL)))
  2442. die("Can't open display\n");
  2443. xw.scr = XDefaultScreen(xw.dpy);
  2444. xw.vis = XDefaultVisual(xw.dpy, xw.scr);
  2445. /* font */
  2446. if(!FcInit())
  2447. die("Could not init fontconfig.\n");
  2448. usedfont = (opt_font == NULL)? font : opt_font;
  2449. xloadfonts(usedfont, 0);
  2450. /* colors */
  2451. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  2452. xloadcols();
  2453. /* adjust fixed window geometry */
  2454. if(xw.isfixed) {
  2455. sw = DisplayWidth(xw.dpy, xw.scr);
  2456. sh = DisplayHeight(xw.dpy, xw.scr);
  2457. if(xw.fx < 0)
  2458. xw.fx = sw + xw.fx - xw.fw - 1;
  2459. if(xw.fy < 0)
  2460. xw.fy = sh + xw.fy - xw.fh - 1;
  2461. xw.h = xw.fh;
  2462. xw.w = xw.fw;
  2463. } else {
  2464. /* window - default size */
  2465. xw.h = 2 * borderpx + term.row * xw.ch;
  2466. xw.w = 2 * borderpx + term.col * xw.cw;
  2467. xw.fx = 0;
  2468. xw.fy = 0;
  2469. }
  2470. /* Events */
  2471. attrs.background_pixel = dc.col[defaultbg].pixel;
  2472. attrs.border_pixel = dc.col[defaultbg].pixel;
  2473. attrs.bit_gravity = NorthWestGravity;
  2474. attrs.event_mask = FocusChangeMask | KeyPressMask
  2475. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  2476. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
  2477. attrs.colormap = xw.cmap;
  2478. parent = opt_embed ? strtol(opt_embed, NULL, 0) : \
  2479. XRootWindow(xw.dpy, xw.scr);
  2480. xw.win = XCreateWindow(xw.dpy, parent, xw.fx, xw.fy,
  2481. xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
  2482. xw.vis,
  2483. CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
  2484. | CWColormap,
  2485. &attrs);
  2486. memset(&gcvalues, 0, sizeof(gcvalues));
  2487. gcvalues.graphics_exposures = False;
  2488. dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
  2489. &gcvalues);
  2490. xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.w, xw.h,
  2491. DefaultDepth(xw.dpy, xw.scr));
  2492. XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
  2493. XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, xw.w, xw.h);
  2494. /* Xft rendering context */
  2495. xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
  2496. /* input methods */
  2497. if((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
  2498. XSetLocaleModifiers("@im=local");
  2499. if((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
  2500. XSetLocaleModifiers("@im=");
  2501. if((xw.xim = XOpenIM(xw.dpy,
  2502. NULL, NULL, NULL)) == NULL) {
  2503. die("XOpenIM failed. Could not open input"
  2504. " device.\n");
  2505. }
  2506. }
  2507. }
  2508. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  2509. | XIMStatusNothing, XNClientWindow, xw.win,
  2510. XNFocusWindow, xw.win, NULL);
  2511. if(xw.xic == NULL)
  2512. die("XCreateIC failed. Could not obtain input method.\n");
  2513. /* white cursor, black outline */
  2514. cursor = XCreateFontCursor(xw.dpy, XC_xterm);
  2515. XDefineCursor(xw.dpy, xw.win, cursor);
  2516. XRecolorCursor(xw.dpy, cursor,
  2517. &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
  2518. &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
  2519. xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
  2520. xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
  2521. XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
  2522. xresettitle();
  2523. XMapWindow(xw.dpy, xw.win);
  2524. xhints();
  2525. XSync(xw.dpy, 0);
  2526. }
  2527. void
  2528. xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
  2529. int winx = borderpx + x * xw.cw, winy = borderpx + y * xw.ch,
  2530. width = charlen * xw.cw, xp, i;
  2531. int frp, frcflags;
  2532. int u8fl, u8fblen, u8cblen, doesexist;
  2533. char *u8c, *u8fs;
  2534. long u8char;
  2535. Font *font = &dc.font;
  2536. FcResult fcres;
  2537. FcPattern *fcpattern, *fontpattern;
  2538. FcFontSet *fcsets[] = { NULL };
  2539. FcCharSet *fccharset;
  2540. Colour *fg, *bg, *temp, revfg, revbg;
  2541. XRenderColor colfg, colbg;
  2542. Rectangle r;
  2543. frcflags = FRC_NORMAL;
  2544. if(base.mode & ATTR_ITALIC) {
  2545. if(base.fg == defaultfg)
  2546. base.fg = defaultitalic;
  2547. font = &dc.ifont;
  2548. frcflags = FRC_ITALIC;
  2549. } else if((base.mode & ATTR_ITALIC) && (base.mode & ATTR_BOLD)) {
  2550. if(base.fg == defaultfg)
  2551. base.fg = defaultitalic;
  2552. font = &dc.ibfont;
  2553. frcflags = FRC_ITALICBOLD;
  2554. } else if(base.mode & ATTR_UNDERLINE) {
  2555. if(base.fg == defaultfg)
  2556. base.fg = defaultunderline;
  2557. }
  2558. fg = &dc.col[base.fg];
  2559. bg = &dc.col[base.bg];
  2560. if(base.mode & ATTR_BOLD) {
  2561. if(BETWEEN(base.fg, 0, 7)) {
  2562. /* basic system colors */
  2563. fg = &dc.col[base.fg + 8];
  2564. } else if(BETWEEN(base.fg, 16, 195)) {
  2565. /* 256 colors */
  2566. fg = &dc.col[base.fg + 36];
  2567. } else if(BETWEEN(base.fg, 232, 251)) {
  2568. /* greyscale */
  2569. fg = &dc.col[base.fg + 4];
  2570. }
  2571. /*
  2572. * Those ranges will not be brightened:
  2573. * 8 - 15 bright system colors
  2574. * 196 - 231 highest 256 color cube
  2575. * 252 - 255 brightest colors in greyscale
  2576. */
  2577. font = &dc.bfont;
  2578. frcflags = FRC_BOLD;
  2579. }
  2580. if(IS_SET(MODE_REVERSE)) {
  2581. if(fg == &dc.col[defaultfg]) {
  2582. fg = &dc.col[defaultbg];
  2583. } else {
  2584. colfg.red = ~fg->color.red;
  2585. colfg.green = ~fg->color.green;
  2586. colfg.blue = ~fg->color.blue;
  2587. colfg.alpha = fg->color.alpha;
  2588. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
  2589. fg = &revfg;
  2590. }
  2591. if(bg == &dc.col[defaultbg]) {
  2592. bg = &dc.col[defaultfg];
  2593. } else {
  2594. colbg.red = ~bg->color.red;
  2595. colbg.green = ~bg->color.green;
  2596. colbg.blue = ~bg->color.blue;
  2597. colbg.alpha = bg->color.alpha;
  2598. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &revbg);
  2599. bg = &revbg;
  2600. }
  2601. }
  2602. if(base.mode & ATTR_REVERSE) {
  2603. temp = fg;
  2604. fg = bg;
  2605. bg = temp;
  2606. }
  2607. if(base.mode & ATTR_BLINK && term.mode & MODE_BLINK)
  2608. fg = bg;
  2609. /* Intelligent cleaning up of the borders. */
  2610. if(x == 0) {
  2611. xclear(0, (y == 0)? 0 : winy, borderpx,
  2612. winy + xw.ch + ((y >= term.row-1)? xw.h : 0));
  2613. }
  2614. if(x + charlen >= term.col) {
  2615. xclear(winx + width, (y == 0)? 0 : winy, xw.w,
  2616. ((y >= term.row-1)? xw.h : (winy + xw.ch)));
  2617. }
  2618. if(y == 0)
  2619. xclear(winx, 0, winx + width, borderpx);
  2620. if(y == term.row-1)
  2621. xclear(winx, winy + xw.ch, winx + width, xw.h);
  2622. /* Clean up the region we want to draw to. */
  2623. XftDrawRect(xw.draw, bg, winx, winy, width, xw.ch);
  2624. /* Set the clip region because Xft is sometimes dirty. */
  2625. r.x = 0;
  2626. r.y = 0;
  2627. r.height = xw.ch;
  2628. r.width = width;
  2629. XftDrawSetClipRectangles(xw.draw, winx, winy, &r, 1);
  2630. fcsets[0] = font->set;
  2631. for(xp = winx; bytelen > 0;) {
  2632. /*
  2633. * Search for the range in the to be printed string of glyphs
  2634. * that are in the main font. Then print that range. If
  2635. * some glyph is found that is not in the font, do the
  2636. * fallback dance.
  2637. */
  2638. u8fs = s;
  2639. u8fblen = 0;
  2640. u8fl = 0;
  2641. for(;;) {
  2642. u8c = s;
  2643. u8cblen = utf8decode(s, &u8char);
  2644. s += u8cblen;
  2645. bytelen -= u8cblen;
  2646. doesexist = XftCharIndex(xw.dpy, font->match, u8char);
  2647. if(!doesexist || bytelen <= 0) {
  2648. if(bytelen <= 0) {
  2649. if(doesexist) {
  2650. u8fl++;
  2651. u8fblen += u8cblen;
  2652. }
  2653. }
  2654. if(u8fl > 0) {
  2655. XftDrawStringUtf8(xw.draw, fg,
  2656. font->match, xp,
  2657. winy + font->ascent,
  2658. (FcChar8 *)u8fs,
  2659. u8fblen);
  2660. xp += font->width * u8fl;
  2661. }
  2662. break;
  2663. }
  2664. u8fl++;
  2665. u8fblen += u8cblen;
  2666. }
  2667. if(doesexist)
  2668. break;
  2669. frp = frccur;
  2670. /* Search the font cache. */
  2671. for(i = 0; i < frclen; i++, frp--) {
  2672. if(frp <= 0)
  2673. frp = LEN(frc) - 1;
  2674. if(frc[frp].c == u8char
  2675. && frc[frp].flags == frcflags) {
  2676. break;
  2677. }
  2678. }
  2679. /* Nothing was found. */
  2680. if(i >= frclen) {
  2681. /*
  2682. * Nothing was found in the cache. Now use
  2683. * some dozen of Fontconfig calls to get the
  2684. * font for one single character.
  2685. */
  2686. fcpattern = FcPatternDuplicate(font->pattern);
  2687. fccharset = FcCharSetCreate();
  2688. FcCharSetAddChar(fccharset, u8char);
  2689. FcPatternAddCharSet(fcpattern, FC_CHARSET,
  2690. fccharset);
  2691. FcPatternAddBool(fcpattern, FC_SCALABLE,
  2692. FcTrue);
  2693. FcConfigSubstitute(0, fcpattern,
  2694. FcMatchPattern);
  2695. FcDefaultSubstitute(fcpattern);
  2696. fontpattern = FcFontSetMatch(0, fcsets,
  2697. FcTrue, fcpattern, &fcres);
  2698. /*
  2699. * Overwrite or create the new cache entry.
  2700. */
  2701. frccur++;
  2702. frclen++;
  2703. if(frccur >= LEN(frc))
  2704. frccur = 0;
  2705. if(frclen > LEN(frc)) {
  2706. frclen = LEN(frc);
  2707. XftFontClose(xw.dpy, frc[frccur].font);
  2708. }
  2709. frc[frccur].font = XftFontOpenPattern(xw.dpy,
  2710. fontpattern);
  2711. frc[frccur].c = u8char;
  2712. frc[frccur].flags = frcflags;
  2713. FcPatternDestroy(fcpattern);
  2714. FcCharSetDestroy(fccharset);
  2715. frp = frccur;
  2716. }
  2717. XftDrawStringUtf8(xw.draw, fg, frc[frp].font,
  2718. xp, winy + frc[frp].font->ascent,
  2719. (FcChar8 *)u8c, u8cblen);
  2720. xp += font->width;
  2721. }
  2722. /*
  2723. XftDrawStringUtf8(xw.draw, fg, font->set, winx,
  2724. winy + font->ascent, (FcChar8 *)s, bytelen);
  2725. */
  2726. if(base.mode & ATTR_UNDERLINE) {
  2727. XftDrawRect(xw.draw, fg, winx, winy + font->ascent + 1,
  2728. width, 1);
  2729. }
  2730. /* Reset clip to none. */
  2731. XftDrawSetClip(xw.draw, 0);
  2732. }
  2733. void
  2734. xdrawcursor(void) {
  2735. static int oldx = 0, oldy = 0;
  2736. int sl;
  2737. Glyph g = {{' '}, ATTR_NULL, defaultbg, defaultcs};
  2738. LIMIT(oldx, 0, term.col-1);
  2739. LIMIT(oldy, 0, term.row-1);
  2740. memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
  2741. /* remove the old cursor */
  2742. sl = utf8size(term.line[oldy][oldx].c);
  2743. xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx,
  2744. oldy, 1, sl);
  2745. /* draw the new one */
  2746. if(!(IS_SET(MODE_HIDE))) {
  2747. if(xw.state & WIN_FOCUSED) {
  2748. if(IS_SET(MODE_REVERSE)) {
  2749. g.mode |= ATTR_REVERSE;
  2750. g.fg = defaultcs;
  2751. g.bg = defaultfg;
  2752. }
  2753. sl = utf8size(g.c);
  2754. xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
  2755. } else {
  2756. XftDrawRect(xw.draw, &dc.col[defaultcs],
  2757. borderpx + term.c.x * xw.cw,
  2758. borderpx + term.c.y * xw.ch,
  2759. xw.cw - 1, 1);
  2760. XftDrawRect(xw.draw, &dc.col[defaultcs],
  2761. borderpx + term.c.x * xw.cw,
  2762. borderpx + term.c.y * xw.ch,
  2763. 1, xw.ch - 1);
  2764. XftDrawRect(xw.draw, &dc.col[defaultcs],
  2765. borderpx + (term.c.x + 1) * xw.cw - 1,
  2766. borderpx + term.c.y * xw.ch,
  2767. 1, xw.ch - 1);
  2768. XftDrawRect(xw.draw, &dc.col[defaultcs],
  2769. borderpx + term.c.x * xw.cw,
  2770. borderpx + (term.c.y + 1) * xw.ch - 1,
  2771. xw.cw, 1);
  2772. }
  2773. oldx = term.c.x, oldy = term.c.y;
  2774. }
  2775. }
  2776. void
  2777. xsettitle(char *p) {
  2778. XTextProperty prop;
  2779. Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
  2780. &prop);
  2781. XSetWMName(xw.dpy, xw.win, &prop);
  2782. }
  2783. void
  2784. xresettitle(void) {
  2785. xsettitle(opt_title ? opt_title : "st");
  2786. }
  2787. void
  2788. redraw(int timeout) {
  2789. struct timespec tv = {0, timeout * 1000};
  2790. tfulldirt();
  2791. draw();
  2792. if(timeout > 0) {
  2793. nanosleep(&tv, NULL);
  2794. XSync(xw.dpy, False); /* necessary for a good tput flash */
  2795. }
  2796. }
  2797. void
  2798. draw(void) {
  2799. drawregion(0, 0, term.col, term.row);
  2800. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.w,
  2801. xw.h, 0, 0);
  2802. XSetForeground(xw.dpy, dc.gc,
  2803. dc.col[IS_SET(MODE_REVERSE)?
  2804. defaultfg : defaultbg].pixel);
  2805. }
  2806. void
  2807. drawregion(int x1, int y1, int x2, int y2) {
  2808. int ic, ib, x, y, ox, sl;
  2809. Glyph base, new;
  2810. char buf[DRAW_BUF_SIZ];
  2811. bool ena_sel = sel.bx != -1;
  2812. if(sel.alt ^ IS_SET(MODE_ALTSCREEN))
  2813. ena_sel = 0;
  2814. if(!(xw.state & WIN_VISIBLE))
  2815. return;
  2816. for(y = y1; y < y2; y++) {
  2817. if(!term.dirty[y])
  2818. continue;
  2819. xtermclear(0, y, term.col, y);
  2820. term.dirty[y] = 0;
  2821. base = term.line[y][0];
  2822. ic = ib = ox = 0;
  2823. for(x = x1; x < x2; x++) {
  2824. new = term.line[y][x];
  2825. if(ena_sel && selected(x, y))
  2826. new.mode ^= ATTR_REVERSE;
  2827. if(ib > 0 && (ATTRCMP(base, new)
  2828. || ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
  2829. xdraws(buf, base, ox, y, ic, ib);
  2830. ic = ib = 0;
  2831. }
  2832. if(ib == 0) {
  2833. ox = x;
  2834. base = new;
  2835. }
  2836. sl = utf8size(new.c);
  2837. memcpy(buf+ib, new.c, sl);
  2838. ib += sl;
  2839. ++ic;
  2840. }
  2841. if(ib > 0)
  2842. xdraws(buf, base, ox, y, ic, ib);
  2843. }
  2844. xdrawcursor();
  2845. }
  2846. void
  2847. expose(XEvent *ev) {
  2848. XExposeEvent *e = &ev->xexpose;
  2849. if(xw.state & WIN_REDRAW) {
  2850. if(!e->count)
  2851. xw.state &= ~WIN_REDRAW;
  2852. }
  2853. redraw(0);
  2854. }
  2855. void
  2856. visibility(XEvent *ev) {
  2857. XVisibilityEvent *e = &ev->xvisibility;
  2858. if(e->state == VisibilityFullyObscured) {
  2859. xw.state &= ~WIN_VISIBLE;
  2860. } else if(!(xw.state & WIN_VISIBLE)) {
  2861. /* need a full redraw for next Expose, not just a buf copy */
  2862. xw.state |= WIN_VISIBLE | WIN_REDRAW;
  2863. }
  2864. }
  2865. void
  2866. unmap(XEvent *ev) {
  2867. xw.state &= ~WIN_VISIBLE;
  2868. }
  2869. void
  2870. xseturgency(int add) {
  2871. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  2872. h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
  2873. XSetWMHints(xw.dpy, xw.win, h);
  2874. XFree(h);
  2875. }
  2876. void
  2877. focus(XEvent *ev) {
  2878. XFocusChangeEvent *e = &ev->xfocus;
  2879. if(e->mode == NotifyGrab)
  2880. return;
  2881. if(ev->type == FocusIn) {
  2882. XSetICFocus(xw.xic);
  2883. xw.state |= WIN_FOCUSED;
  2884. xseturgency(0);
  2885. } else {
  2886. XUnsetICFocus(xw.xic);
  2887. xw.state &= ~WIN_FOCUSED;
  2888. }
  2889. }
  2890. inline bool
  2891. match(uint mask, uint state) {
  2892. state &= ~(ignoremod);
  2893. if(mask == XK_NO_MOD && state)
  2894. return false;
  2895. if(mask != XK_ANY_MOD && mask != XK_NO_MOD && !state)
  2896. return false;
  2897. if((state & mask) != state)
  2898. return false;
  2899. return true;
  2900. }
  2901. void
  2902. numlock(const Arg *dummy) {
  2903. term.numlock ^= 1;
  2904. }
  2905. char*
  2906. kmap(KeySym k, uint state) {
  2907. uint mask;
  2908. Key *kp;
  2909. int i;
  2910. /* Check for mapped keys out of X11 function keys. */
  2911. for(i = 0; i < LEN(mappedkeys); i++) {
  2912. if(mappedkeys[i] == k)
  2913. break;
  2914. }
  2915. if(i == LEN(mappedkeys)) {
  2916. if((k & 0xFFFF) < 0xFD00)
  2917. return NULL;
  2918. }
  2919. for(kp = key; kp < key + LEN(key); kp++) {
  2920. mask = kp->mask;
  2921. if(kp->k != k)
  2922. continue;
  2923. if(!match(mask, state))
  2924. continue;
  2925. if(kp->appkey > 0) {
  2926. if(!IS_SET(MODE_APPKEYPAD))
  2927. continue;
  2928. if(term.numlock && kp->appkey == 2)
  2929. continue;
  2930. } else if(kp->appkey < 0 && IS_SET(MODE_APPKEYPAD)) {
  2931. continue;
  2932. }
  2933. if((kp->appcursor < 0 && IS_SET(MODE_APPCURSOR)) ||
  2934. (kp->appcursor > 0
  2935. && !IS_SET(MODE_APPCURSOR))) {
  2936. continue;
  2937. }
  2938. if((kp->crlf < 0 && IS_SET(MODE_CRLF)) ||
  2939. (kp->crlf > 0 && !IS_SET(MODE_CRLF))) {
  2940. continue;
  2941. }
  2942. return kp->s;
  2943. }
  2944. return NULL;
  2945. }
  2946. void
  2947. kpress(XEvent *ev) {
  2948. XKeyEvent *e = &ev->xkey;
  2949. KeySym ksym;
  2950. char xstr[31], buf[32], *customkey, *cp = buf;
  2951. int len, ret;
  2952. long c;
  2953. Status status;
  2954. Shortcut *bp;
  2955. if(IS_SET(MODE_KBDLOCK))
  2956. return;
  2957. len = XmbLookupString(xw.xic, e, xstr, sizeof(xstr), &ksym, &status);
  2958. e->state &= ~Mod2Mask;
  2959. /* 1. shortcuts */
  2960. for(bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
  2961. if(ksym == bp->keysym && match(bp->mod, e->state)) {
  2962. bp->func(&(bp->arg));
  2963. return;
  2964. }
  2965. }
  2966. /* 2. custom keys from config.h */
  2967. if((customkey = kmap(ksym, e->state))) {
  2968. len = strlen(customkey);
  2969. memcpy(buf, customkey, len);
  2970. /* 3. hardcoded (overrides X lookup) */
  2971. } else {
  2972. if(len == 0)
  2973. return;
  2974. if(len == 1 && e->state & Mod1Mask) {
  2975. if(IS_SET(MODE_8BIT)) {
  2976. if(*xstr < 0177) {
  2977. c = *xstr | B7;
  2978. ret = utf8encode(&c, cp);
  2979. cp += ret;
  2980. len = 0;
  2981. }
  2982. } else {
  2983. *cp++ = '\033';
  2984. }
  2985. }
  2986. memcpy(cp, xstr, len);
  2987. len = cp - buf + len;
  2988. }
  2989. ttywrite(buf, len);
  2990. if(IS_SET(MODE_ECHO))
  2991. techo(buf, len);
  2992. }
  2993. void
  2994. cmessage(XEvent *e) {
  2995. /*
  2996. * See xembed specs
  2997. * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
  2998. */
  2999. if(e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
  3000. if(e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
  3001. xw.state |= WIN_FOCUSED;
  3002. xseturgency(0);
  3003. } else if(e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
  3004. xw.state &= ~WIN_FOCUSED;
  3005. }
  3006. } else if(e->xclient.data.l[0] == xw.wmdeletewin) {
  3007. /* Send SIGHUP to shell */
  3008. kill(pid, SIGHUP);
  3009. exit(EXIT_SUCCESS);
  3010. }
  3011. }
  3012. void
  3013. cresize(int width, int height) {
  3014. int col, row;
  3015. if(width != 0)
  3016. xw.w = width;
  3017. if(height != 0)
  3018. xw.h = height;
  3019. col = (xw.w - 2 * borderpx) / xw.cw;
  3020. row = (xw.h - 2 * borderpx) / xw.ch;
  3021. tresize(col, row);
  3022. xresize(col, row);
  3023. ttyresize();
  3024. }
  3025. void
  3026. resize(XEvent *e) {
  3027. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  3028. return;
  3029. cresize(e->xconfigure.width, e->xconfigure.height);
  3030. }
  3031. void
  3032. run(void) {
  3033. XEvent ev;
  3034. fd_set rfd;
  3035. int xfd = XConnectionNumber(xw.dpy), xev, blinkset = 0, dodraw = 0;
  3036. struct timeval drawtimeout, *tv = NULL, now, last, lastblink;
  3037. gettimeofday(&lastblink, NULL);
  3038. gettimeofday(&last, NULL);
  3039. for(xev = actionfps;;) {
  3040. FD_ZERO(&rfd);
  3041. FD_SET(cmdfd, &rfd);
  3042. FD_SET(xfd, &rfd);
  3043. switch(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv) < 0) {
  3044. case -1:
  3045. if(errno == EINTR)
  3046. continue;
  3047. die("select failed: %s\n", SERRNO);
  3048. default:
  3049. if(FD_ISSET(cmdfd, &rfd)) {
  3050. ttyread();
  3051. if(blinktimeout) {
  3052. blinkset = tattrset(ATTR_BLINK);
  3053. if(!blinkset && term.mode & ATTR_BLINK)
  3054. term.mode &= ~(MODE_BLINK);
  3055. }
  3056. }
  3057. if(FD_ISSET(xfd, &rfd))
  3058. xev = actionfps;
  3059. break;
  3060. }
  3061. gettimeofday(&now, NULL);
  3062. drawtimeout.tv_sec = 0;
  3063. drawtimeout.tv_usec = (1000/xfps) * 1000;
  3064. tv = &drawtimeout;
  3065. dodraw = 0;
  3066. if(blinktimeout && TIMEDIFF(now, lastblink) > blinktimeout) {
  3067. tsetdirtattr(ATTR_BLINK);
  3068. term.mode ^= MODE_BLINK;
  3069. gettimeofday(&lastblink, NULL);
  3070. dodraw = 1;
  3071. }
  3072. if(TIMEDIFF(now, last) \
  3073. > (xev? (1000/xfps) : (1000/actionfps))) {
  3074. dodraw = 1;
  3075. last = now;
  3076. }
  3077. if(dodraw) {
  3078. while(XPending(xw.dpy)) {
  3079. XNextEvent(xw.dpy, &ev);
  3080. if(XFilterEvent(&ev, None))
  3081. continue;
  3082. if(handler[ev.type])
  3083. (handler[ev.type])(&ev);
  3084. }
  3085. draw();
  3086. XFlush(xw.dpy);
  3087. if(xev && !FD_ISSET(xfd, &rfd))
  3088. xev--;
  3089. if(!FD_ISSET(cmdfd, &rfd) && !FD_ISSET(xfd, &rfd)) {
  3090. if(blinkset) {
  3091. drawtimeout.tv_usec = 1000 * \
  3092. blinktimeout;
  3093. } else {
  3094. tv = NULL;
  3095. }
  3096. }
  3097. }
  3098. }
  3099. }
  3100. void
  3101. usage(void) {
  3102. die("%s " VERSION " (c) 2010-2013 st engineers\n" \
  3103. "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
  3104. " [-t title] [-w windowid] [-e command ...]\n", argv0);
  3105. }
  3106. int
  3107. main(int argc, char *argv[]) {
  3108. int bitm, xr, yr;
  3109. uint wr, hr;
  3110. xw.fw = xw.fh = xw.fx = xw.fy = 0;
  3111. xw.isfixed = False;
  3112. ARGBEGIN {
  3113. case 'a':
  3114. allowaltscreen = false;
  3115. break;
  3116. case 'c':
  3117. opt_class = EARGF(usage());
  3118. break;
  3119. case 'e':
  3120. /* eat all remaining arguments */
  3121. if(argc > 1)
  3122. opt_cmd = &argv[1];
  3123. goto run;
  3124. case 'f':
  3125. opt_font = EARGF(usage());
  3126. break;
  3127. case 'g':
  3128. bitm = XParseGeometry(EARGF(usage()), &xr, &yr, &wr, &hr);
  3129. if(bitm & XValue)
  3130. xw.fx = xr;
  3131. if(bitm & YValue)
  3132. xw.fy = yr;
  3133. if(bitm & WidthValue)
  3134. xw.fw = (int)wr;
  3135. if(bitm & HeightValue)
  3136. xw.fh = (int)hr;
  3137. if(bitm & XNegative && xw.fx == 0)
  3138. xw.fx = -1;
  3139. if(bitm & XNegative && xw.fy == 0)
  3140. xw.fy = -1;
  3141. if(xw.fh != 0 && xw.fw != 0)
  3142. xw.isfixed = True;
  3143. break;
  3144. case 'o':
  3145. opt_io = EARGF(usage());
  3146. break;
  3147. case 't':
  3148. opt_title = EARGF(usage());
  3149. break;
  3150. case 'w':
  3151. opt_embed = EARGF(usage());
  3152. break;
  3153. case 'v':
  3154. default:
  3155. usage();
  3156. } ARGEND;
  3157. run:
  3158. setlocale(LC_CTYPE, "");
  3159. XSetLocaleModifiers("");
  3160. tnew(80, 24);
  3161. xinit();
  3162. ttynew();
  3163. selinit();
  3164. if(xw.isfixed)
  3165. cresize(xw.h, xw.w);
  3166. run();
  3167. return 0;
  3168. }