Simple Terminal from SuckLess
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3472 lines
75 KiB

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