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.

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