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.

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