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

1497 lines
33 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
  1. /* See LICENSE for licence details. */
  2. #define _XOPEN_SOURCE 600
  3. #include <ctype.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <limits.h>
  7. #include <locale.h>
  8. #include <stdarg.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <signal.h>
  13. #include <sys/ioctl.h>
  14. #include <sys/select.h>
  15. #include <sys/stat.h>
  16. #include <sys/types.h>
  17. #include <sys/wait.h>
  18. #include <unistd.h>
  19. #include <X11/Xlib.h>
  20. #include <X11/keysym.h>
  21. #include <X11/Xutil.h>
  22. #if defined(__linux)
  23. #include <pty.h>
  24. #elif defined(__OpenBSD__) || defined(__NetBSD__)
  25. #include <util.h>
  26. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  27. #include <libutil.h>
  28. #endif
  29. #define USAGE \
  30. "st-" VERSION ", (c) 2010 st engineers\n" \
  31. "usage: st [-t title] [-e cmd] [-v]\n"
  32. /* Arbitrary sizes */
  33. #define ESC_TITLE_SIZ 256
  34. #define ESC_BUF_SIZ 256
  35. #define ESC_ARG_SIZ 16
  36. #define DRAW_BUF_SIZ 1024
  37. #define SERRNO strerror(errno)
  38. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  39. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  40. #define LEN(a) (sizeof(a) / sizeof(a[0]))
  41. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  42. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  43. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  44. #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
  45. #define IS_SET(flag) (term.mode & (flag))
  46. /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
  47. enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 };
  48. enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT,
  49. CURSOR_SAVE, CURSOR_LOAD };
  50. enum { CURSOR_DEFAULT = 0, CURSOR_HIDE = 1, CURSOR_WRAPNEXT = 2 };
  51. enum { GLYPH_SET=1, GLYPH_DIRTY=2 };
  52. enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4, MODE_ALTSCREEN=8 };
  53. enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 };
  54. enum { SCREEN_UPDATE, SCREEN_REDRAW };
  55. typedef struct {
  56. char c; /* character code */
  57. char mode; /* attribute flags */
  58. int fg; /* foreground */
  59. int bg; /* background */
  60. char state; /* state flags */
  61. } Glyph;
  62. typedef Glyph* Line;
  63. typedef struct {
  64. Glyph attr; /* current char attributes */
  65. int x;
  66. int y;
  67. char state;
  68. } TCursor;
  69. /* CSI Escape sequence structs */
  70. /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
  71. typedef struct {
  72. char buf[ESC_BUF_SIZ]; /* raw string */
  73. int len; /* raw string length */
  74. char priv;
  75. int arg[ESC_ARG_SIZ];
  76. int narg; /* nb of args */
  77. char mode;
  78. } CSIEscape;
  79. /* Internal representation of the screen */
  80. typedef struct {
  81. int row; /* nb row */
  82. int col; /* nb col */
  83. Line* line; /* screen */
  84. Line* alt; /* alternate screen */
  85. TCursor c; /* cursor */
  86. int top; /* top scroll limit */
  87. int bot; /* bottom scroll limit */
  88. int mode; /* terminal mode flags */
  89. int esc; /* escape state flags */
  90. char title[ESC_TITLE_SIZ];
  91. int titlelen;
  92. } Term;
  93. /* Purely graphic info */
  94. typedef struct {
  95. Display* dis;
  96. Colormap cmap;
  97. Window win;
  98. Pixmap buf;
  99. int scr;
  100. int w; /* window width */
  101. int h; /* window height */
  102. int bufw; /* pixmap width */
  103. int bufh; /* pixmap height */
  104. int ch; /* char height */
  105. int cw; /* char width */
  106. int hasfocus;
  107. } XWindow;
  108. typedef struct {
  109. KeySym k;
  110. char s[ESC_BUF_SIZ];
  111. } Key;
  112. /* Drawing Context */
  113. typedef struct {
  114. unsigned long col[256];
  115. XFontStruct* font;
  116. XFontStruct* bfont;
  117. GC gc;
  118. } DC;
  119. /* TODO: use better name for vars... */
  120. typedef struct {
  121. int mode;
  122. int bx, by;
  123. int ex, ey;
  124. struct {int x, y;} b, e;
  125. char *clip;
  126. } Selection;
  127. #include "config.h"
  128. static void die(const char *errstr, ...);
  129. static void draw(int);
  130. static void execsh(void);
  131. static void sigchld(int);
  132. static void run(void);
  133. static void csidump(void);
  134. static void csihandle(void);
  135. static void csiparse(void);
  136. static void csireset(void);
  137. static void tclearregion(int, int, int, int);
  138. static void tcursor(int);
  139. static void tdeletechar(int);
  140. static void tdeleteline(int);
  141. static void tinsertblank(int);
  142. static void tinsertblankline(int);
  143. static void tmoveto(int, int);
  144. static void tnew(int, int);
  145. static void tnewline(void);
  146. static void tputtab(void);
  147. static void tputc(char);
  148. static void tputs(char*, int);
  149. static void treset(void);
  150. static void tresize(int, int);
  151. static void tscrollup(int, int);
  152. static void tscrolldown(int, int);
  153. static void tsetattr(int*, int);
  154. static void tsetchar(char);
  155. static void tsetscroll(int, int);
  156. static void tswapscreen(void);
  157. static void ttynew(void);
  158. static void ttyread(void);
  159. static void ttyresize(int, int);
  160. static void ttywrite(const char *, size_t);
  161. static void xdraws(char *, Glyph, int, int, int);
  162. static void xhints(void);
  163. static void xclear(int, int, int, int);
  164. static void xdrawcursor(void);
  165. static void xinit(void);
  166. static void xloadcols(void);
  167. static void xseturgency(int);
  168. static void expose(XEvent *);
  169. static char* kmap(KeySym);
  170. static void kpress(XEvent *);
  171. static void resize(XEvent *);
  172. static void focus(XEvent *);
  173. static void brelease(XEvent *);
  174. static void bpress(XEvent *);
  175. static void bmotion(XEvent *);
  176. static void (*handler[LASTEvent])(XEvent *) = {
  177. [KeyPress] = kpress,
  178. [Expose] = expose,
  179. [ConfigureNotify] = resize,
  180. [FocusIn] = focus,
  181. [FocusOut] = focus,
  182. [MotionNotify] = bmotion,
  183. [ButtonPress] = bpress,
  184. [ButtonRelease] = brelease,
  185. };
  186. /* Globals */
  187. static DC dc;
  188. static XWindow xw;
  189. static Term term;
  190. static CSIEscape escseq;
  191. static int cmdfd;
  192. static pid_t pid;
  193. static Selection sel;
  194. static char *opt_cmd = NULL;
  195. static char *opt_title = NULL;
  196. void
  197. selinit(void) {
  198. sel.mode = 0;
  199. sel.bx = -1;
  200. sel.clip = NULL;
  201. }
  202. static inline int selected(int x, int y) {
  203. if(sel.ey == y && sel.by == y) {
  204. int bx = MIN(sel.bx, sel.ex);
  205. int ex = MAX(sel.bx, sel.ex);
  206. return BETWEEN(x, bx, ex);
  207. }
  208. return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x))
  209. || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y));
  210. }
  211. static void getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
  212. if(b) *b = e->xbutton.state,
  213. *b=*b==4096?5:*b==2048?4:*b==1024?3:*b==512?2:*b==256?1:-1;
  214. *x = e->xbutton.x/xw.cw;
  215. *y = e->xbutton.y/xw.ch;
  216. sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
  217. sel.b.y = MIN(sel.by, sel.ey);
  218. sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
  219. sel.e.y = MAX(sel.by, sel.ey);
  220. }
  221. static void bpress(XEvent *e) {
  222. sel.mode = 1;
  223. sel.ex = sel.bx = e->xbutton.x/xw.cw;
  224. sel.ey = sel.by = e->xbutton.y/xw.ch;
  225. }
  226. static char *getseltext() {
  227. char *str, *ptr;
  228. int ls, x, y, sz;
  229. if(sel.bx == -1)
  230. return NULL;
  231. sz = (term.col+1) * (sel.e.y-sel.b.y+1);
  232. ptr = str = malloc(sz);
  233. for(y = 0; y < term.row; y++) {
  234. for(x = 0; x < term.col; x++)
  235. if(term.line[y][x].state & GLYPH_SET && (ls = selected(x, y)))
  236. *ptr = term.line[y][x].c, ptr++;
  237. if(ls)
  238. *ptr = '\n', ptr++;
  239. }
  240. *ptr = 0;
  241. return str;
  242. }
  243. /* TODO: use X11 clipboard */
  244. static void selcopy(char *str) {
  245. free(sel.clip);
  246. sel.clip = str;
  247. }
  248. static void selpaste() {
  249. if(sel.clip)
  250. ttywrite(sel.clip, strlen(sel.clip));
  251. }
  252. /* TODO: doubleclick to select word */
  253. static void brelease(XEvent *e) {
  254. int b;
  255. sel.mode = 0;
  256. getbuttoninfo(e, &b, &sel.ex, &sel.ey);
  257. if(sel.bx==sel.ex && sel.by==sel.ey) {
  258. sel.bx = -1;
  259. if(b==2)
  260. selpaste();
  261. } else {
  262. if(b==1)
  263. selcopy(getseltext());
  264. }
  265. draw(1);
  266. }
  267. static void bmotion(XEvent *e) {
  268. if (sel.mode) {
  269. getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
  270. draw(1);
  271. }
  272. }
  273. #ifdef DEBUG
  274. void
  275. tdump(void) {
  276. int row, col;
  277. Glyph c;
  278. for(row = 0; row < term.row; row++) {
  279. for(col = 0; col < term.col; col++) {
  280. if(col == term.c.x && row == term.c.y)
  281. putchar('#');
  282. else {
  283. c = term.line[row][col];
  284. putchar(c.state & GLYPH_SET ? c.c : '.');
  285. }
  286. }
  287. putchar('\n');
  288. }
  289. }
  290. #endif
  291. void
  292. die(const char *errstr, ...) {
  293. va_list ap;
  294. va_start(ap, errstr);
  295. vfprintf(stderr, errstr, ap);
  296. va_end(ap);
  297. exit(EXIT_FAILURE);
  298. }
  299. void
  300. execsh(void) {
  301. char *args[] = {getenv("SHELL"), "-i", NULL};
  302. if(opt_cmd)
  303. args[0] = opt_cmd, args[1] = NULL;
  304. else
  305. DEFAULT(args[0], SHELL);
  306. putenv("TERM="TNAME);
  307. execvp(args[0], args);
  308. }
  309. void
  310. sigchld(int a) {
  311. int stat = 0;
  312. if(waitpid(pid, &stat, 0) < 0)
  313. die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
  314. if(WIFEXITED(stat))
  315. exit(WEXITSTATUS(stat));
  316. else
  317. exit(EXIT_FAILURE);
  318. }
  319. void
  320. ttynew(void) {
  321. int m, s;
  322. /* seems to work fine on linux, openbsd and freebsd */
  323. struct winsize w = {term.row, term.col, 0, 0};
  324. if(openpty(&m, &s, NULL, NULL, &w) < 0)
  325. die("openpty failed: %s\n", SERRNO);
  326. switch(pid = fork()) {
  327. case -1:
  328. die("fork failed\n");
  329. break;
  330. case 0:
  331. setsid(); /* create a new process group */
  332. dup2(s, STDIN_FILENO);
  333. dup2(s, STDOUT_FILENO);
  334. dup2(s, STDERR_FILENO);
  335. if(ioctl(s, TIOCSCTTY, NULL) < 0)
  336. die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
  337. close(s);
  338. close(m);
  339. execsh();
  340. break;
  341. default:
  342. close(s);
  343. cmdfd = m;
  344. signal(SIGCHLD, sigchld);
  345. }
  346. }
  347. void
  348. dump(char c) {
  349. static int col;
  350. fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
  351. if(++col % 10 == 0)
  352. fprintf(stderr, "\n");
  353. }
  354. void
  355. ttyread(void) {
  356. char buf[BUFSIZ];
  357. int ret;
  358. if((ret = read(cmdfd, buf, LEN(buf))) < 0)
  359. die("Couldn't read from shell: %s\n", SERRNO);
  360. else
  361. tputs(buf, ret);
  362. }
  363. void
  364. ttywrite(const char *s, size_t n) {
  365. if(write(cmdfd, s, n) == -1)
  366. die("write error on tty: %s\n", SERRNO);
  367. }
  368. void
  369. ttyresize(int x, int y) {
  370. struct winsize w;
  371. w.ws_row = term.row;
  372. w.ws_col = term.col;
  373. w.ws_xpixel = w.ws_ypixel = 0;
  374. if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  375. fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
  376. }
  377. void
  378. tcursor(int mode) {
  379. static TCursor c;
  380. if(mode == CURSOR_SAVE)
  381. c = term.c;
  382. else if(mode == CURSOR_LOAD)
  383. term.c = c, tmoveto(c.x, c.y);
  384. }
  385. void
  386. treset(void) {
  387. term.c = (TCursor){{
  388. .mode = ATTR_NULL,
  389. .fg = DefaultFG,
  390. .bg = DefaultBG
  391. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  392. term.top = 0, term.bot = term.row - 1;
  393. term.mode = MODE_WRAP;
  394. tclearregion(0, 0, term.col-1, term.row-1);
  395. }
  396. void
  397. tnew(int col, int row) {
  398. /* set screen size */
  399. term.row = row, term.col = col;
  400. term.line = malloc(term.row * sizeof(Line));
  401. term.alt = malloc(term.row * sizeof(Line));
  402. for(row = 0 ; row < term.row; row++) {
  403. term.line[row] = malloc(term.col * sizeof(Glyph));
  404. term.alt [row] = malloc(term.col * sizeof(Glyph));
  405. }
  406. /* setup screen */
  407. treset();
  408. }
  409. void
  410. tswapscreen(void) {
  411. Line* tmp = term.line;
  412. term.line = term.alt;
  413. term.alt = tmp;
  414. term.mode ^= MODE_ALTSCREEN;
  415. }
  416. void
  417. tscrolldown(int orig, int n) {
  418. int i;
  419. Line temp;
  420. LIMIT(n, 0, term.bot-orig+1);
  421. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  422. for(i = term.bot; i >= orig+n; i--) {
  423. temp = term.line[i];
  424. term.line[i] = term.line[i-n];
  425. term.line[i-n] = temp;
  426. }
  427. }
  428. void
  429. tscrollup(int orig, int n) {
  430. int i;
  431. Line temp;
  432. LIMIT(n, 0, term.bot-orig+1);
  433. tclearregion(0, orig, term.col-1, orig+n-1);
  434. for(i = orig; i <= term.bot-n; i++) {
  435. temp = term.line[i];
  436. term.line[i] = term.line[i+n];
  437. term.line[i+n] = temp;
  438. }
  439. }
  440. void
  441. tnewline(void) {
  442. int y = term.c.y;
  443. if(term.c.y == term.bot)
  444. tscrollup(term.top, 1);
  445. else
  446. y++;
  447. tmoveto(0, y);
  448. }
  449. void
  450. csiparse(void) {
  451. /* int noarg = 1; */
  452. char *p = escseq.buf;
  453. escseq.narg = 0;
  454. if(*p == '?')
  455. escseq.priv = 1, p++;
  456. while(p < escseq.buf+escseq.len) {
  457. while(isdigit(*p)) {
  458. escseq.arg[escseq.narg] *= 10;
  459. escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
  460. }
  461. if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
  462. escseq.narg++, p++;
  463. else {
  464. escseq.mode = *p;
  465. escseq.narg++;
  466. return;
  467. }
  468. }
  469. }
  470. void
  471. tmoveto(int x, int y) {
  472. LIMIT(x, 0, term.col-1);
  473. LIMIT(y, 0, term.row-1);
  474. term.c.state &= ~CURSOR_WRAPNEXT;
  475. term.c.x = x;
  476. term.c.y = y;
  477. }
  478. void
  479. tsetchar(char c) {
  480. term.line[term.c.y][term.c.x] = term.c.attr;
  481. term.line[term.c.y][term.c.x].c = c;
  482. term.line[term.c.y][term.c.x].state |= GLYPH_SET;
  483. }
  484. void
  485. tclearregion(int x1, int y1, int x2, int y2) {
  486. int y, temp;
  487. if(x1 > x2)
  488. temp = x1, x1 = x2, x2 = temp;
  489. if(y1 > y2)
  490. temp = y1, y1 = y2, y2 = temp;
  491. LIMIT(x1, 0, term.col-1);
  492. LIMIT(x2, 0, term.col-1);
  493. LIMIT(y1, 0, term.row-1);
  494. LIMIT(y2, 0, term.row-1);
  495. for(y = y1; y <= y2; y++)
  496. memset(&term.line[y][x1], 0, sizeof(Glyph)*(x2-x1+1));
  497. }
  498. void
  499. tdeletechar(int n) {
  500. int src = term.c.x + n;
  501. int dst = term.c.x;
  502. int size = term.col - src;
  503. if(src >= term.col) {
  504. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  505. return;
  506. }
  507. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  508. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  509. }
  510. void
  511. tinsertblank(int n) {
  512. int src = term.c.x;
  513. int dst = src + n;
  514. int size = term.col - dst;
  515. if(dst >= term.col) {
  516. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  517. return;
  518. }
  519. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  520. tclearregion(src, term.c.y, dst - 1, term.c.y);
  521. }
  522. void
  523. tinsertblankline(int n) {
  524. if(term.c.y < term.top || term.c.y > term.bot)
  525. return;
  526. tscrolldown(term.c.y, n);
  527. }
  528. void
  529. tdeleteline(int n) {
  530. if(term.c.y < term.top || term.c.y > term.bot)
  531. return;
  532. tscrollup(term.c.y, n);
  533. }
  534. void
  535. tsetattr(int *attr, int l) {
  536. int i;
  537. for(i = 0; i < l; i++) {
  538. switch(attr[i]) {
  539. case 0:
  540. term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
  541. term.c.attr.fg = DefaultFG;
  542. term.c.attr.bg = DefaultBG;
  543. break;
  544. case 1:
  545. term.c.attr.mode |= ATTR_BOLD;
  546. break;
  547. case 4:
  548. term.c.attr.mode |= ATTR_UNDERLINE;
  549. break;
  550. case 7:
  551. term.c.attr.mode |= ATTR_REVERSE;
  552. break;
  553. case 22:
  554. term.c.attr.mode &= ~ATTR_BOLD;
  555. break;
  556. case 24:
  557. term.c.attr.mode &= ~ATTR_UNDERLINE;
  558. break;
  559. case 27:
  560. term.c.attr.mode &= ~ATTR_REVERSE;
  561. break;
  562. case 38:
  563. if (i + 2 < l && attr[i + 1] == 5) {
  564. i += 2;
  565. if (BETWEEN(attr[i], 0, 255))
  566. term.c.attr.fg = attr[i];
  567. else
  568. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
  569. }
  570. else
  571. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  572. break;
  573. case 39:
  574. term.c.attr.fg = DefaultFG;
  575. break;
  576. case 48:
  577. if (i + 2 < l && attr[i + 1] == 5) {
  578. i += 2;
  579. if (BETWEEN(attr[i], 0, 255))
  580. term.c.attr.bg = attr[i];
  581. else
  582. fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
  583. }
  584. else
  585. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  586. break;
  587. case 49:
  588. term.c.attr.bg = DefaultBG;
  589. break;
  590. default:
  591. if(BETWEEN(attr[i], 30, 37))
  592. term.c.attr.fg = attr[i] - 30;
  593. else if(BETWEEN(attr[i], 40, 47))
  594. term.c.attr.bg = attr[i] - 40;
  595. else if(BETWEEN(attr[i], 90, 97))
  596. term.c.attr.fg = attr[i] - 90 + 8;
  597. else if(BETWEEN(attr[i], 100, 107))
  598. term.c.attr.fg = attr[i] - 100 + 8;
  599. else
  600. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]), csidump();
  601. break;
  602. }
  603. }
  604. }
  605. void
  606. tsetscroll(int t, int b) {
  607. int temp;
  608. LIMIT(t, 0, term.row-1);
  609. LIMIT(b, 0, term.row-1);
  610. if(t > b) {
  611. temp = t;
  612. t = b;
  613. b = temp;
  614. }
  615. term.top = t;
  616. term.bot = b;
  617. }
  618. void
  619. csihandle(void) {
  620. switch(escseq.mode) {
  621. default:
  622. unknown:
  623. printf("erresc: unknown csi ");
  624. csidump();
  625. /* die(""); */
  626. break;
  627. case '@': /* ICH -- Insert <n> blank char */
  628. DEFAULT(escseq.arg[0], 1);
  629. tinsertblank(escseq.arg[0]);
  630. break;
  631. case 'A': /* CUU -- Cursor <n> Up */
  632. case 'e':
  633. DEFAULT(escseq.arg[0], 1);
  634. tmoveto(term.c.x, term.c.y-escseq.arg[0]);
  635. break;
  636. case 'B': /* CUD -- Cursor <n> Down */
  637. DEFAULT(escseq.arg[0], 1);
  638. tmoveto(term.c.x, term.c.y+escseq.arg[0]);
  639. break;
  640. case 'C': /* CUF -- Cursor <n> Forward */
  641. case 'a':
  642. DEFAULT(escseq.arg[0], 1);
  643. tmoveto(term.c.x+escseq.arg[0], term.c.y);
  644. break;
  645. case 'D': /* CUB -- Cursor <n> Backward */
  646. DEFAULT(escseq.arg[0], 1);
  647. tmoveto(term.c.x-escseq.arg[0], term.c.y);
  648. break;
  649. case 'E': /* CNL -- Cursor <n> Down and first col */
  650. DEFAULT(escseq.arg[0], 1);
  651. tmoveto(0, term.c.y+escseq.arg[0]);
  652. break;
  653. case 'F': /* CPL -- Cursor <n> Up and first col */
  654. DEFAULT(escseq.arg[0], 1);
  655. tmoveto(0, term.c.y-escseq.arg[0]);
  656. break;
  657. case 'G': /* CHA -- Move to <col> */
  658. case '`': /* XXX: HPA -- same? */
  659. DEFAULT(escseq.arg[0], 1);
  660. tmoveto(escseq.arg[0]-1, term.c.y);
  661. break;
  662. case 'H': /* CUP -- Move to <row> <col> */
  663. case 'f': /* XXX: HVP -- same? */
  664. DEFAULT(escseq.arg[0], 1);
  665. DEFAULT(escseq.arg[1], 1);
  666. tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
  667. break;
  668. /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
  669. case 'J': /* ED -- Clear screen */
  670. switch(escseq.arg[0]) {
  671. case 0: /* below */
  672. tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
  673. break;
  674. case 1: /* above */
  675. tclearregion(0, 0, term.c.x, term.c.y);
  676. break;
  677. case 2: /* all */
  678. tclearregion(0, 0, term.col-1, term.row-1);
  679. break;
  680. default:
  681. goto unknown;
  682. }
  683. break;
  684. case 'K': /* EL -- Clear line */
  685. switch(escseq.arg[0]) {
  686. case 0: /* right */
  687. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  688. break;
  689. case 1: /* left */
  690. tclearregion(0, term.c.y, term.c.x, term.c.y);
  691. break;
  692. case 2: /* all */
  693. tclearregion(0, term.c.y, term.col-1, term.c.y);
  694. break;
  695. }
  696. break;
  697. case 'S': /* SU -- Scroll <n> line up */
  698. DEFAULT(escseq.arg[0], 1);
  699. tscrollup(term.top, escseq.arg[0]);
  700. break;
  701. case 'T': /* SD -- Scroll <n> line down */
  702. DEFAULT(escseq.arg[0], 1);
  703. tscrolldown(term.top, escseq.arg[0]);
  704. break;
  705. case 'L': /* IL -- Insert <n> blank lines */
  706. DEFAULT(escseq.arg[0], 1);
  707. tinsertblankline(escseq.arg[0]);
  708. break;
  709. case 'l': /* RM -- Reset Mode */
  710. if(escseq.priv) {
  711. switch(escseq.arg[0]) {
  712. case 1:
  713. term.mode &= ~MODE_APPKEYPAD;
  714. break;
  715. case 5: /* TODO: DECSCNM -- Remove reverse video */
  716. break;
  717. case 7:
  718. term.mode &= ~MODE_WRAP;
  719. break;
  720. case 12: /* att610 -- Stop blinking cursor (IGNORED) */
  721. break;
  722. case 25:
  723. term.c.state |= CURSOR_HIDE;
  724. break;
  725. case 1049: /* = 1047 and 1048 */
  726. case 1047:
  727. if(IS_SET(MODE_ALTSCREEN)) {
  728. tclearregion(0, 0, term.col-1, term.row-1);
  729. tswapscreen();
  730. }
  731. if(escseq.arg[0] == 1047)
  732. break;
  733. case 1048:
  734. tcursor(CURSOR_LOAD);
  735. break;
  736. default:
  737. goto unknown;
  738. }
  739. } else {
  740. switch(escseq.arg[0]) {
  741. case 4:
  742. term.mode &= ~MODE_INSERT;
  743. break;
  744. default:
  745. goto unknown;
  746. }
  747. }
  748. break;
  749. case 'M': /* DL -- Delete <n> lines */
  750. DEFAULT(escseq.arg[0], 1);
  751. tdeleteline(escseq.arg[0]);
  752. break;
  753. case 'X': /* ECH -- Erase <n> char */
  754. DEFAULT(escseq.arg[0], 1);
  755. tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
  756. break;
  757. case 'P': /* DCH -- Delete <n> char */
  758. DEFAULT(escseq.arg[0], 1);
  759. tdeletechar(escseq.arg[0]);
  760. break;
  761. /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
  762. case 'd': /* VPA -- Move to <row> */
  763. DEFAULT(escseq.arg[0], 1);
  764. tmoveto(term.c.x, escseq.arg[0]-1);
  765. break;
  766. case 'h': /* SM -- Set terminal mode */
  767. if(escseq.priv) {
  768. switch(escseq.arg[0]) {
  769. case 1:
  770. term.mode |= MODE_APPKEYPAD;
  771. break;
  772. case 5: /* DECSCNM -- Reverve video */
  773. /* TODO: set REVERSE on the whole screen (f) */
  774. break;
  775. case 7:
  776. term.mode |= MODE_WRAP;
  777. break;
  778. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  779. /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
  780. if(escseq.narg > 1 && escseq.arg[1] != 25)
  781. break;
  782. case 25:
  783. term.c.state &= ~CURSOR_HIDE;
  784. break;
  785. case 1049: /* = 1047 and 1048 */
  786. case 1047:
  787. if(IS_SET(MODE_ALTSCREEN))
  788. tclearregion(0, 0, term.col-1, term.row-1);
  789. else
  790. tswapscreen();
  791. if(escseq.arg[0] == 1047)
  792. break;
  793. case 1048:
  794. tcursor(CURSOR_SAVE);
  795. break;
  796. default: goto unknown;
  797. }
  798. } else {
  799. switch(escseq.arg[0]) {
  800. case 4:
  801. term.mode |= MODE_INSERT;
  802. break;
  803. default: goto unknown;
  804. }
  805. };
  806. break;
  807. case 'm': /* SGR -- Terminal attribute (color) */
  808. tsetattr(escseq.arg, escseq.narg);
  809. break;
  810. case 'r': /* DECSTBM -- Set Scrolling Region */
  811. if(escseq.priv)
  812. goto unknown;
  813. else {
  814. DEFAULT(escseq.arg[0], 1);
  815. DEFAULT(escseq.arg[1], term.row);
  816. tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
  817. tmoveto(0, 0);
  818. }
  819. break;
  820. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  821. tcursor(CURSOR_SAVE);
  822. break;
  823. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  824. tcursor(CURSOR_LOAD);
  825. break;
  826. }
  827. }
  828. void
  829. csidump(void) {
  830. int i;
  831. printf("ESC [ %s", escseq.priv ? "? " : "");
  832. if(escseq.narg)
  833. for(i = 0; i < escseq.narg; i++)
  834. printf("%d ", escseq.arg[i]);
  835. if(escseq.mode)
  836. putchar(escseq.mode);
  837. putchar('\n');
  838. }
  839. void
  840. csireset(void) {
  841. memset(&escseq, 0, sizeof(escseq));
  842. }
  843. void
  844. tputtab(void) {
  845. int space = TAB - term.c.x % TAB;
  846. tmoveto(term.c.x + space, term.c.y);
  847. }
  848. void
  849. tputc(char c) {
  850. if(term.esc & ESC_START) {
  851. if(term.esc & ESC_CSI) {
  852. escseq.buf[escseq.len++] = c;
  853. if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
  854. term.esc = 0;
  855. csiparse(), csihandle();
  856. }
  857. /* TODO: handle other OSC */
  858. } else if(term.esc & ESC_OSC) {
  859. if(c == ';') {
  860. term.titlelen = 0;
  861. term.esc = ESC_START | ESC_TITLE;
  862. }
  863. } else if(term.esc & ESC_TITLE) {
  864. if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
  865. term.esc = 0;
  866. term.title[term.titlelen] = '\0';
  867. XStoreName(xw.dis, xw.win, term.title);
  868. } else {
  869. term.title[term.titlelen++] = c;
  870. }
  871. } else if(term.esc & ESC_ALTCHARSET) {
  872. switch(c) {
  873. case '0': /* Line drawing crap */
  874. term.c.attr.mode |= ATTR_GFX;
  875. break;
  876. case 'B': /* Back to regular text */
  877. term.c.attr.mode &= ~ATTR_GFX;
  878. break;
  879. default:
  880. printf("esc unhandled charset: ESC ( %c\n", c);
  881. }
  882. term.esc = 0;
  883. } else {
  884. switch(c) {
  885. case '[':
  886. term.esc |= ESC_CSI;
  887. break;
  888. case ']':
  889. term.esc |= ESC_OSC;
  890. break;
  891. case '(':
  892. term.esc |= ESC_ALTCHARSET;
  893. break;
  894. case 'D': /* IND -- Linefeed */
  895. if(term.c.y == term.bot)
  896. tscrollup(term.top, 1);
  897. else
  898. tmoveto(term.c.x, term.c.y+1);
  899. term.esc = 0;
  900. break;
  901. case 'E': /* NEL -- Next line */
  902. tnewline();
  903. term.esc = 0;
  904. break;
  905. case 'M': /* RI -- Reverse index */
  906. if(term.c.y == term.top)
  907. tscrolldown(term.top, 1);
  908. else
  909. tmoveto(term.c.x, term.c.y-1);
  910. term.esc = 0;
  911. break;
  912. case 'c': /* RIS -- Reset to inital state */
  913. treset();
  914. term.esc = 0;
  915. break;
  916. case '=': /* DECPAM -- Application keypad */
  917. term.mode |= MODE_APPKEYPAD;
  918. term.esc = 0;
  919. break;
  920. case '>': /* DECPNM -- Normal keypad */
  921. term.mode &= ~MODE_APPKEYPAD;
  922. term.esc = 0;
  923. break;
  924. case '7': /* DECSC -- Save Cursor */
  925. tcursor(CURSOR_SAVE);
  926. term.esc = 0;
  927. break;
  928. case '8': /* DECRC -- Restore Cursor */
  929. tcursor(CURSOR_LOAD);
  930. term.esc = 0;
  931. break;
  932. default:
  933. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
  934. term.esc = 0;
  935. }
  936. }
  937. } else {
  938. switch(c) {
  939. case '\t':
  940. tputtab();
  941. break;
  942. case '\b':
  943. tmoveto(term.c.x-1, term.c.y);
  944. break;
  945. case '\r':
  946. tmoveto(0, term.c.y);
  947. break;
  948. case '\n':
  949. tnewline();
  950. break;
  951. case '\a':
  952. if(!xw.hasfocus)
  953. xseturgency(1);
  954. break;
  955. case '\033':
  956. csireset();
  957. term.esc = ESC_START;
  958. break;
  959. default:
  960. if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
  961. tnewline();
  962. tsetchar(c);
  963. if(term.c.x+1 < term.col)
  964. tmoveto(term.c.x+1, term.c.y);
  965. else
  966. term.c.state |= CURSOR_WRAPNEXT;
  967. break;
  968. }
  969. }
  970. }
  971. void
  972. tputs(char *s, int len) {
  973. for(; len > 0; len--)
  974. tputc(*s++);
  975. }
  976. void
  977. tresize(int col, int row) {
  978. int i;
  979. int minrow = MIN(row, term.row);
  980. int mincol = MIN(col, term.col);
  981. int slide = term.c.y - row + 1;
  982. if(col < 1 || row < 1)
  983. return;
  984. /* free unneeded rows */
  985. i = 0;
  986. if(slide > 0) {
  987. /* slide screen to keep cursor where we expect it -
  988. * tscrollup would work here, but we can optimize to
  989. * memmove because we're freeing the earlier lines */
  990. for(/* i = 0 */; i < slide; i++) {
  991. free(term.line[i]);
  992. free(term.alt[i]);
  993. }
  994. memmove(term.line, term.line + slide, row * sizeof(Line));
  995. memmove(term.alt, term.alt + slide, row * sizeof(Line));
  996. }
  997. for(i += row; i < term.row; i++) {
  998. free(term.line[i]);
  999. free(term.alt[i]);
  1000. }
  1001. /* resize to new height */
  1002. term.line = realloc(term.line, row * sizeof(Line));
  1003. term.alt = realloc(term.alt, row * sizeof(Line));
  1004. /* resize each row to new width, zero-pad if needed */
  1005. for(i = 0; i < minrow; i++) {
  1006. term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
  1007. term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
  1008. memset(term.line[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
  1009. memset(term.alt[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
  1010. }
  1011. /* allocate any new rows */
  1012. for(/* i == minrow */; i < row; i++) {
  1013. term.line[i] = calloc(col, sizeof(Glyph));
  1014. term.alt [i] = calloc(col, sizeof(Glyph));
  1015. }
  1016. /* update terminal size */
  1017. term.col = col, term.row = row;
  1018. /* make use of the LIMIT in tmoveto */
  1019. tmoveto(term.c.x, term.c.y);
  1020. /* reset scrolling region */
  1021. tsetscroll(0, row-1);
  1022. }
  1023. void
  1024. xloadcols(void) {
  1025. int i, r, g, b;
  1026. XColor color;
  1027. unsigned long white = WhitePixel(xw.dis, xw.scr);
  1028. for(i = 0; i < 16; i++) {
  1029. if (!XAllocNamedColor(xw.dis, xw.cmap, colorname[i], &color, &color)) {
  1030. dc.col[i] = white;
  1031. fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
  1032. } else
  1033. dc.col[i] = color.pixel;
  1034. }
  1035. /* same colors as xterm */
  1036. for(r = 0; r < 6; r++)
  1037. for(g = 0; g < 6; g++)
  1038. for(b = 0; b < 6; b++) {
  1039. color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
  1040. color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
  1041. color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
  1042. if (!XAllocColor(xw.dis, xw.cmap, &color)) {
  1043. dc.col[i] = white;
  1044. fprintf(stderr, "Could not allocate color %d\n", i);
  1045. } else
  1046. dc.col[i] = color.pixel;
  1047. i++;
  1048. }
  1049. for(r = 0; r < 24; r++, i++) {
  1050. color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
  1051. if (!XAllocColor(xw.dis, xw.cmap, &color)) {
  1052. dc.col[i] = white;
  1053. fprintf(stderr, "Could not allocate color %d\n", i);
  1054. } else
  1055. dc.col[i] = color.pixel;
  1056. }
  1057. }
  1058. void
  1059. xclear(int x1, int y1, int x2, int y2) {
  1060. XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
  1061. XFillRectangle(xw.dis, xw.buf, dc.gc,
  1062. x1 * xw.cw, y1 * xw.ch,
  1063. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
  1064. }
  1065. void
  1066. xhints(void)
  1067. {
  1068. XClassHint class = {TNAME, TNAME};
  1069. XWMHints wm = {.flags = InputHint, .input = 1};
  1070. XSizeHints size = {
  1071. .flags = PSize | PResizeInc | PBaseSize,
  1072. .height = xw.h,
  1073. .width = xw.w,
  1074. .height_inc = xw.ch,
  1075. .width_inc = xw.cw,
  1076. .base_height = 2*BORDER,
  1077. .base_width = 2*BORDER,
  1078. };
  1079. XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
  1080. }
  1081. void
  1082. xinit(void) {
  1083. XSetWindowAttributes attrs;
  1084. if(!(xw.dis = XOpenDisplay(NULL)))
  1085. die("Can't open display\n");
  1086. xw.scr = XDefaultScreen(xw.dis);
  1087. /* font */
  1088. if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
  1089. die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
  1090. /* XXX: Assuming same size for bold font */
  1091. xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
  1092. xw.ch = dc.font->ascent + dc.font->descent;
  1093. /* colors */
  1094. xw.cmap = XDefaultColormap(xw.dis, xw.scr);
  1095. xloadcols();
  1096. /* window - default size */
  1097. xw.bufh = 24 * xw.ch;
  1098. xw.bufw = 80 * xw.cw;
  1099. xw.h = xw.bufh + 2*BORDER;
  1100. xw.w = xw.bufw + 2*BORDER;
  1101. attrs.background_pixel = dc.col[DefaultBG];
  1102. attrs.border_pixel = dc.col[DefaultBG];
  1103. attrs.bit_gravity = NorthWestGravity;
  1104. attrs.event_mask = ExposureMask | KeyPressMask
  1105. | StructureNotifyMask | FocusChangeMask | PointerMotionMask
  1106. | ButtonPressMask | ButtonReleaseMask;
  1107. attrs.colormap = xw.cmap;
  1108. xw.win = XCreateWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
  1109. xw.w, xw.h, 0, XDefaultDepth(xw.dis, xw.scr), InputOutput,
  1110. XDefaultVisual(xw.dis, xw.scr),
  1111. CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
  1112. | CWColormap,
  1113. &attrs);
  1114. xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
  1115. /* gc */
  1116. dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
  1117. XMapWindow(xw.dis, xw.win);
  1118. xhints();
  1119. XStoreName(xw.dis, xw.win, opt_title ? opt_title : "st");
  1120. XSync(xw.dis, 0);
  1121. }
  1122. void
  1123. xdraws(char *s, Glyph base, int x, int y, int len) {
  1124. unsigned long xfg, xbg;
  1125. int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
  1126. int i;
  1127. if(base.mode & ATTR_REVERSE)
  1128. xfg = dc.col[base.bg], xbg = dc.col[base.fg];
  1129. else
  1130. xfg = dc.col[base.fg], xbg = dc.col[base.bg];
  1131. XSetBackground(xw.dis, dc.gc, xbg);
  1132. XSetForeground(xw.dis, dc.gc, xfg);
  1133. if(base.mode & ATTR_GFX)
  1134. for(i = 0; i < len; i++) {
  1135. char c = gfx[(unsigned int)s[i] % 256];
  1136. if(c)
  1137. s[i] = c;
  1138. else if(s[i] > 0x5f)
  1139. s[i] -= 0x5f;
  1140. }
  1141. XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
  1142. XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
  1143. if(base.mode & ATTR_UNDERLINE)
  1144. XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
  1145. }
  1146. void
  1147. xdrawcursor(void) {
  1148. static int oldx = 0;
  1149. static int oldy = 0;
  1150. Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
  1151. LIMIT(oldx, 0, term.col-1);
  1152. LIMIT(oldy, 0, term.row-1);
  1153. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  1154. g.c = term.line[term.c.y][term.c.x].c;
  1155. /* remove the old cursor */
  1156. if(term.line[oldy][oldx].state & GLYPH_SET)
  1157. xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
  1158. else
  1159. xclear(oldx, oldy, oldx, oldy);
  1160. /* draw the new one */
  1161. if(!(term.c.state & CURSOR_HIDE) && xw.hasfocus) {
  1162. xdraws(&g.c, g, term.c.x, term.c.y, 1);
  1163. oldx = term.c.x, oldy = term.c.y;
  1164. }
  1165. }
  1166. #ifdef DEBUG
  1167. /* basic drawing routines */
  1168. void
  1169. xdrawc(int x, int y, Glyph g) {
  1170. XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
  1171. XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
  1172. XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
  1173. XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
  1174. XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
  1175. }
  1176. void
  1177. draw(int dummy) {
  1178. int x, y;
  1179. xclear(0, 0, term.col-1, term.row-1);
  1180. for(y = 0; y < term.row; y++)
  1181. for(x = 0; x < term.col; x++)
  1182. if(term.line[y][x].state & GLYPH_SET)
  1183. xdrawc(x, y, term.line[y][x]);
  1184. xdrawcursor();
  1185. XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1186. XFlush(xw.dis);
  1187. }
  1188. #else
  1189. /* optimized drawing routine */
  1190. void
  1191. draw(int redraw_all) {
  1192. int i, x, y, ox;
  1193. Glyph base, new;
  1194. char buf[DRAW_BUF_SIZ];
  1195. XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
  1196. XFillRectangle(xw.dis, xw.buf, dc.gc, 0, 0, xw.bufw, xw.bufh);
  1197. for(y = 0; y < term.row; y++) {
  1198. base = term.line[y][0];
  1199. i = ox = 0;
  1200. for(x = 0; x < term.col; x++) {
  1201. new = term.line[y][x];
  1202. if(sel.bx!=-1 && new.c && selected(x, y))
  1203. new.mode ^= ATTR_REVERSE;
  1204. if(i > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
  1205. i >= DRAW_BUF_SIZ)) {
  1206. xdraws(buf, base, ox, y, i);
  1207. i = 0;
  1208. }
  1209. if(new.state & GLYPH_SET) {
  1210. if(i == 0) {
  1211. ox = x;
  1212. base = new;
  1213. }
  1214. buf[i++] = new.c;
  1215. }
  1216. }
  1217. if(i > 0)
  1218. xdraws(buf, base, ox, y, i);
  1219. }
  1220. xdrawcursor();
  1221. XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1222. XFlush(xw.dis);
  1223. }
  1224. #endif
  1225. void
  1226. expose(XEvent *ev) {
  1227. draw(SCREEN_REDRAW);
  1228. }
  1229. void
  1230. xseturgency(int add) {
  1231. XWMHints *h = XGetWMHints(xw.dis, xw.win);
  1232. h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
  1233. XSetWMHints(xw.dis, xw.win, h);
  1234. XFree(h);
  1235. }
  1236. void
  1237. focus(XEvent *ev) {
  1238. if((xw.hasfocus = ev->type == FocusIn))
  1239. xseturgency(0);
  1240. draw(SCREEN_UPDATE);
  1241. }
  1242. char*
  1243. kmap(KeySym k) {
  1244. int i;
  1245. for(i = 0; i < LEN(key); i++)
  1246. if(key[i].k == k)
  1247. return (char*)key[i].s;
  1248. return NULL;
  1249. }
  1250. void
  1251. kpress(XEvent *ev) {
  1252. XKeyEvent *e = &ev->xkey;
  1253. KeySym ksym;
  1254. char buf[32];
  1255. char *customkey;
  1256. int len;
  1257. int meta;
  1258. int shift;
  1259. meta = e->state & Mod1Mask;
  1260. shift = e->state & ShiftMask;
  1261. len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
  1262. if((customkey = kmap(ksym)))
  1263. ttywrite(customkey, strlen(customkey));
  1264. else if(len > 0) {
  1265. buf[sizeof(buf)-1] = '\0';
  1266. if(meta && len == 1)
  1267. ttywrite("\033", 1);
  1268. ttywrite(buf, len);
  1269. } else
  1270. switch(ksym) {
  1271. case XK_Up:
  1272. case XK_Down:
  1273. case XK_Left:
  1274. case XK_Right:
  1275. sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', "DACB"[ksym - XK_Left]);
  1276. ttywrite(buf, 3);
  1277. break;
  1278. case XK_Insert:
  1279. if(shift)
  1280. selpaste(), draw(1);
  1281. break;
  1282. default:
  1283. fprintf(stderr, "errkey: %d\n", (int)ksym);
  1284. break;
  1285. }
  1286. }
  1287. void
  1288. resize(XEvent *e) {
  1289. int col, row;
  1290. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  1291. return;
  1292. xw.w = e->xconfigure.width;
  1293. xw.h = e->xconfigure.height;
  1294. xw.bufw = xw.w - 2*BORDER;
  1295. xw.bufh = xw.h - 2*BORDER;
  1296. col = xw.bufw / xw.cw;
  1297. row = xw.bufh / xw.ch;
  1298. tresize(col, row);
  1299. ttyresize(col, row);
  1300. xw.bufh = MAX(1, xw.bufh);
  1301. xw.bufw = MAX(1, xw.bufw);
  1302. XFreePixmap(xw.dis, xw.buf);
  1303. xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
  1304. }
  1305. void
  1306. run(void) {
  1307. XEvent ev;
  1308. fd_set rfd;
  1309. int xfd = XConnectionNumber(xw.dis);
  1310. for(;;) {
  1311. FD_ZERO(&rfd);
  1312. FD_SET(cmdfd, &rfd);
  1313. FD_SET(xfd, &rfd);
  1314. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
  1315. if(errno == EINTR)
  1316. continue;
  1317. die("select failed: %s\n", SERRNO);
  1318. }
  1319. if(FD_ISSET(cmdfd, &rfd)) {
  1320. ttyread();
  1321. draw(SCREEN_UPDATE);
  1322. }
  1323. while(XPending(xw.dis)) {
  1324. XNextEvent(xw.dis, &ev);
  1325. if(handler[ev.type])
  1326. (handler[ev.type])(&ev);
  1327. }
  1328. }
  1329. }
  1330. int
  1331. main(int argc, char *argv[]) {
  1332. int i;
  1333. for(i = 1; i < argc; i++) {
  1334. switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
  1335. case 't':
  1336. if(++i < argc) opt_title = argv[i];
  1337. break;
  1338. case 'e':
  1339. if(++i < argc) opt_cmd = argv[i];
  1340. break;
  1341. case 'v':
  1342. default:
  1343. die(USAGE);
  1344. }
  1345. }
  1346. setlocale(LC_CTYPE, "");
  1347. tnew(80, 24);
  1348. ttynew();
  1349. xinit();
  1350. selinit();
  1351. run();
  1352. return 0;
  1353. }