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

4091 lines
90 KiB

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