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.

1212 lines
26 KiB

  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. #define TNAME "xterm"
  23. /* Arbitrary sizes */
  24. #define ESC_TITLE_SIZ 256
  25. #define ESC_BUF_SIZ 256
  26. #define ESC_ARG_SIZ 16
  27. #define DRAW_BUF_SIZ 1024
  28. #define SERRNO strerror(errno)
  29. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  30. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  31. #define LEN(a) (sizeof(a) / sizeof(a[0]))
  32. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  33. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  34. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  35. #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
  36. /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
  37. enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 };
  38. enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT, CURSOR_HIDE, CURSOR_DRAW, CURSOR_SAVE, CURSOR_LOAD };
  39. enum { GLYPH_SET=1, GLYPH_DIRTY=2 };
  40. enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4 };
  41. enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 };
  42. enum { SCREEN_UPDATE, SCREEN_REDRAW };
  43. typedef struct {
  44. char c; /* character code */
  45. char mode; /* attribute flags */
  46. int fg; /* foreground */
  47. int bg; /* background */
  48. char state; /* state flags */
  49. } Glyph;
  50. typedef Glyph* Line;
  51. typedef struct {
  52. Glyph attr; /* current char attributes */
  53. char hidden;
  54. int x;
  55. int y;
  56. } TCursor;
  57. /* CSI Escape sequence structs */
  58. /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
  59. typedef struct {
  60. char buf[ESC_BUF_SIZ]; /* raw string */
  61. int len; /* raw string length */
  62. char priv;
  63. int arg[ESC_ARG_SIZ];
  64. int narg; /* nb of args */
  65. char mode;
  66. } CSIEscape;
  67. /* Internal representation of the screen */
  68. typedef struct {
  69. int row; /* nb row */
  70. int col; /* nb col */
  71. Line* line; /* screen */
  72. TCursor c; /* cursor */
  73. int top; /* top scroll limit */
  74. int bot; /* bottom scroll limit */
  75. int mode; /* terminal mode flags */
  76. int esc; /* escape state flags */
  77. char title[ESC_TITLE_SIZ];
  78. int titlelen;
  79. } Term;
  80. /* Purely graphic info */
  81. typedef struct {
  82. Display* dis;
  83. Window win;
  84. int scr;
  85. int w; /* window width */
  86. int h; /* window height */
  87. int ch; /* char height */
  88. int cw; /* char width */
  89. } XWindow;
  90. typedef struct {
  91. KeySym k;
  92. char s[ESC_BUF_SIZ];
  93. } Key;
  94. #include "config.h"
  95. /* Drawing Context */
  96. typedef struct {
  97. unsigned long col[LEN(colorname)];
  98. XFontStruct* font;
  99. GC gc;
  100. } DC;
  101. static void die(const char *errstr, ...);
  102. static void draw(int);
  103. static void execsh(void);
  104. static void sigchld(int);
  105. static void run(void);
  106. static void csidump(void);
  107. static void csihandle(void);
  108. static void csiparse(void);
  109. static void csireset(void);
  110. static void tclearregion(int, int, int, int);
  111. static void tcursor(int);
  112. static void tmovecursor(int);
  113. static void tdeletechar(int);
  114. static void tdeleteline(int);
  115. static void tinsertblank(int);
  116. static void tinsertblankline(int);
  117. static void tmoveto(int, int);
  118. static void tnew(int, int);
  119. static void tnewline(void);
  120. static void tputc(char);
  121. static void tputs(char*, int);
  122. static void tresize(int, int);
  123. static void tscroll(void);
  124. static void tsetattr(int*, int);
  125. static void tsetchar(char);
  126. static void tsetscroll(int, int);
  127. static void ttynew(void);
  128. static void ttyread(void);
  129. static void ttyresize(int, int);
  130. static void ttywrite(const char *, size_t);
  131. static unsigned long xgetcol(const char *);
  132. static void xclear(int, int, int, int);
  133. static void xcursor(int);
  134. static void xdrawc(int, int, Glyph);
  135. static void xinit(void);
  136. static void xscroll(void);
  137. static void expose(XEvent *);
  138. static char * kmap(KeySym);
  139. static void kpress(XEvent *);
  140. static void resize(XEvent *);
  141. static void (*handler[LASTEvent])(XEvent *) = {
  142. [KeyPress] = kpress,
  143. [Expose] = expose,
  144. [ConfigureNotify] = resize
  145. };
  146. /* Globals */
  147. static DC dc;
  148. static XWindow xw;
  149. static Term term;
  150. static CSIEscape escseq;
  151. static int cmdfd;
  152. static pid_t pid;
  153. static int running;
  154. #ifdef DEBUG
  155. void
  156. tdump(void) {
  157. int row, col;
  158. Glyph c;
  159. for(row = 0; row < term.row; row++) {
  160. for(col = 0; col < term.col; col++) {
  161. if(col == term.c.x && row == term.c.y)
  162. putchar('#');
  163. else {
  164. c = term.line[row][col];
  165. putchar(c.state & GLYPH_SET ? c.c : '.');
  166. }
  167. }
  168. putchar('\n');
  169. }
  170. }
  171. #endif
  172. void
  173. die(const char *errstr, ...) {
  174. va_list ap;
  175. va_start(ap, errstr);
  176. vfprintf(stderr, errstr, ap);
  177. va_end(ap);
  178. exit(EXIT_FAILURE);
  179. }
  180. void
  181. execsh(void) {
  182. char *args[3] = {SHELL, "-i", NULL};
  183. putenv("TERM=" TNAME);
  184. execvp(SHELL, args);
  185. }
  186. void
  187. xbell(void) { /* visual bell */
  188. XRectangle r = { 0, 0, xw.w, xw.h };
  189. XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
  190. XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
  191. /* usleep(30000); */
  192. draw(SCREEN_REDRAW);
  193. }
  194. void
  195. sigchld(int a) {
  196. int stat = 0;
  197. if(waitpid(pid, &stat, 0) < 0)
  198. die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
  199. if(WIFEXITED(stat))
  200. exit(WEXITSTATUS(stat));
  201. else
  202. exit(EXIT_FAILURE);
  203. }
  204. void
  205. ttynew(void) {
  206. int m, s;
  207. char *pts;
  208. if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
  209. die("openpt failed: %s\n", SERRNO);
  210. if(grantpt(m) < 0)
  211. die("grandpt failed: %s\n", SERRNO);
  212. if(unlockpt(m) < 0)
  213. die("unlockpt failed: %s\n", SERRNO);
  214. if(!(pts = ptsname(m)))
  215. die("ptsname failed: %s\n", SERRNO);
  216. if((s = open(pts, O_RDWR | O_NOCTTY)) < 0)
  217. die("Couldn't open slave: %s\n", SERRNO);
  218. fcntl(s, F_SETFL, O_NDELAY);
  219. switch(pid = fork()) {
  220. case -1:
  221. die("fork failed\n");
  222. break;
  223. case 0:
  224. setsid(); /* create a new process group */
  225. dup2(s, STDIN_FILENO);
  226. dup2(s, STDOUT_FILENO);
  227. dup2(s, STDERR_FILENO);
  228. if(ioctl(s, TIOCSCTTY, NULL) < 0)
  229. die("ioctl TTIOCSTTY failed: %s\n", SERRNO);
  230. execsh();
  231. break;
  232. default:
  233. close(s);
  234. cmdfd = m;
  235. signal(SIGCHLD, sigchld);
  236. }
  237. }
  238. void
  239. dump(char c) {
  240. static int col;
  241. fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
  242. if(++col % 10 == 0)
  243. fprintf(stderr, "\n");
  244. }
  245. void
  246. ttyread(void) {
  247. char buf[BUFSIZ] = {0};
  248. int ret;
  249. if((ret = read(cmdfd, buf, BUFSIZ)) < 0)
  250. die("Couldn't read from shell: %s\n", SERRNO);
  251. else
  252. tputs(buf, ret);
  253. }
  254. void
  255. ttywrite(const char *s, size_t n) {
  256. if(write(cmdfd, s, n) == -1)
  257. die("write error on tty: %s\n", SERRNO);
  258. }
  259. void
  260. ttyresize(int x, int y) {
  261. struct winsize w;
  262. w.ws_row = term.row;
  263. w.ws_col = term.col;
  264. w.ws_xpixel = w.ws_ypixel = 0;
  265. if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  266. fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
  267. }
  268. void
  269. tcursor(int mode) {
  270. static TCursor c;
  271. if(mode == CURSOR_SAVE)
  272. c = term.c;
  273. else if(mode == CURSOR_LOAD)
  274. term.c = c, tmoveto(c.x, c.y);
  275. }
  276. void
  277. tnew(int col, int row) { /* screen size */
  278. term.row = row, term.col = col;
  279. term.top = 0, term.bot = term.row - 1;
  280. /* mode */
  281. term.mode = MODE_WRAP;
  282. /* cursor */
  283. term.c.attr.mode = ATTR_NULL;
  284. term.c.attr.fg = DefaultFG;
  285. term.c.attr.bg = DefaultBG;
  286. term.c.x = term.c.y = 0;
  287. term.c.hidden = 0;
  288. /* allocate screen */
  289. term.line = calloc(term.row, sizeof(Line));
  290. for(row = 0 ; row < term.row; row++)
  291. term.line[row] = calloc(term.col, sizeof(Glyph));
  292. }
  293. void
  294. tscroll(void) {
  295. Line temp = term.line[term.top];
  296. int i;
  297. /* X stuff _before_ the line swapping (results in wrong line index) */
  298. xscroll();
  299. for(i = term.top; i < term.bot; i++)
  300. term.line[i] = term.line[i+1];
  301. memset(temp, 0, sizeof(Glyph) * term.col);
  302. term.line[term.bot] = temp;
  303. }
  304. void
  305. tnewline(void) {
  306. int y = term.c.y + 1;
  307. if(y > term.bot)
  308. tscroll(), y = term.bot;
  309. tmoveto(0, y);
  310. }
  311. void
  312. csiparse(void) {
  313. /* int noarg = 1; */
  314. char *p = escseq.buf;
  315. escseq.narg = 0;
  316. if(*p == '?')
  317. escseq.priv = 1, p++;
  318. while(p < escseq.buf+escseq.len) {
  319. while(isdigit(*p)) {
  320. escseq.arg[escseq.narg] *= 10;
  321. escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
  322. }
  323. if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
  324. escseq.narg++, p++;
  325. else {
  326. escseq.mode = *p;
  327. escseq.narg++;
  328. return;
  329. }
  330. }
  331. }
  332. void
  333. tmoveto(int x, int y) {
  334. term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x;
  335. term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y;
  336. }
  337. void
  338. tmovecursor(int dir) {
  339. int xf = term.c.x, yf = term.c.y;
  340. switch(dir) {
  341. case CURSOR_UP:
  342. yf--;
  343. break;
  344. case CURSOR_DOWN:
  345. yf++;
  346. break;
  347. case CURSOR_LEFT:
  348. xf--;
  349. if(term.mode & MODE_WRAP && xf < 0) {
  350. xf = term.col-1, yf--;
  351. if(yf < term.top)
  352. yf = term.top, xf = 0;
  353. }
  354. break;
  355. case CURSOR_RIGHT:
  356. xf++;
  357. if(term.mode & MODE_WRAP && xf >= term.col) {
  358. xf = 0, yf++;
  359. if(yf > term.bot)
  360. yf = term.bot, tscroll();
  361. }
  362. break;
  363. }
  364. tmoveto(xf, yf);
  365. }
  366. void
  367. tsetchar(char c) {
  368. term.line[term.c.y][term.c.x] = term.c.attr;
  369. term.line[term.c.y][term.c.x].c = c;
  370. term.line[term.c.y][term.c.x].state |= GLYPH_SET | GLYPH_DIRTY;
  371. }
  372. void
  373. tclearregion(int x1, int y1, int x2, int y2) {
  374. int x, y;
  375. LIMIT(x1, 0, term.col-1);
  376. LIMIT(x2, 0, term.col-1);
  377. LIMIT(y1, 0, term.row-1);
  378. LIMIT(y2, 0, term.row-1);
  379. /* XXX: could be optimized */
  380. for(x = x1; x <= x2; x++)
  381. for(y = y1; y <= y2; y++)
  382. memset(&term.line[y][x], 0, sizeof(Glyph));
  383. xclear(x1, y1, x2, y2);
  384. }
  385. void
  386. tdeletechar(int n) {
  387. int src = term.c.x + n;
  388. int dst = term.c.x;
  389. int size = term.col - src;
  390. if(src >= term.col) {
  391. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  392. return;
  393. }
  394. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  395. tclearregion(term.col-size, term.c.y, term.col-1, term.c.y);
  396. }
  397. void
  398. tinsertblank(int n) {
  399. int src = term.c.x;
  400. int dst = src + n;
  401. int size = term.col - n - src;
  402. if(dst >= term.col) {
  403. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  404. return;
  405. }
  406. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  407. tclearregion(src, term.c.y, dst, term.c.y);
  408. }
  409. void
  410. tsetlinestate(int n, int state) {
  411. int i;
  412. for(i = 0; i < term.col; i++)
  413. term.line[n][i].state |= state;
  414. }
  415. void
  416. tinsertblankline(int n) {
  417. int i;
  418. Line blank;
  419. int bot = term.bot;
  420. if(term.c.y > term.bot)
  421. bot = term.row - 1;
  422. else if(term.c.y < term.top)
  423. bot = term.top - 1;
  424. if(term.c.y + n >= bot) {
  425. tclearregion(0, term.c.y, term.col-1, bot);
  426. return;
  427. }
  428. for(i = bot; i >= term.c.y+n; i--) {
  429. /* swap deleted line <-> blanked line */
  430. blank = term.line[i];
  431. term.line[i] = term.line[i-n];
  432. term.line[i-n] = blank;
  433. /* blank it */
  434. memset(blank, 0, term.col * sizeof(Glyph));
  435. tsetlinestate(i, GLYPH_DIRTY);
  436. tsetlinestate(i-n, GLYPH_DIRTY);
  437. }
  438. }
  439. void
  440. tdeleteline(int n) {
  441. int i;
  442. Line blank;
  443. int bot = term.bot;
  444. if(term.c.y > term.bot)
  445. bot = term.row - 1;
  446. else if(term.c.y < term.top)
  447. bot = term.top - 1;
  448. if(term.c.y + n >= bot) {
  449. tclearregion(0, term.c.y, term.col-1, bot);
  450. return;
  451. }
  452. for(i = term.c.y; i <= bot-n; i++) {
  453. /* swap deleted line <-> blanked line */
  454. blank = term.line[i];
  455. term.line[i] = term.line[i+n];
  456. term.line[i+n] = blank;
  457. /* blank it */
  458. memset(blank, 0, term.col * sizeof(Glyph));
  459. tsetlinestate(i, GLYPH_DIRTY);
  460. tsetlinestate(i-n, GLYPH_DIRTY);
  461. }
  462. }
  463. void
  464. tsetattr(int *attr, int l) {
  465. int i;
  466. for(i = 0; i < l; i++) {
  467. switch(attr[i]) {
  468. case 0:
  469. term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
  470. term.c.attr.fg = DefaultFG;
  471. term.c.attr.bg = DefaultBG;
  472. break;
  473. case 1:
  474. term.c.attr.mode |= ATTR_BOLD;
  475. break;
  476. case 4:
  477. term.c.attr.mode |= ATTR_UNDERLINE;
  478. break;
  479. case 7:
  480. term.c.attr.mode |= ATTR_REVERSE;
  481. break;
  482. case 8:
  483. term.c.hidden = CURSOR_HIDE;
  484. break;
  485. case 22:
  486. term.c.attr.mode &= ~ATTR_BOLD;
  487. break;
  488. case 24:
  489. term.c.attr.mode &= ~ATTR_UNDERLINE;
  490. break;
  491. case 27:
  492. term.c.attr.mode &= ~ATTR_REVERSE;
  493. break;
  494. case 39:
  495. term.c.attr.fg = DefaultFG;
  496. break;
  497. case 49:
  498. term.c.attr.fg = DefaultBG;
  499. break;
  500. default:
  501. if(BETWEEN(attr[i], 30, 37))
  502. term.c.attr.fg = attr[i] - 30;
  503. else if(BETWEEN(attr[i], 40, 47))
  504. term.c.attr.bg = attr[i] - 40;
  505. break;
  506. }
  507. }
  508. }
  509. void
  510. tsetscroll(int t, int b) {
  511. int temp;
  512. LIMIT(t, 0, term.row-1);
  513. LIMIT(b, 0, term.row-1);
  514. if(t > b) {
  515. temp = t;
  516. t = b;
  517. b = temp;
  518. }
  519. term.top = t;
  520. term.bot = b;
  521. }
  522. void
  523. csihandle(void) {
  524. switch(escseq.mode) {
  525. default:
  526. unknown:
  527. printf("erresc: unknown sequence -- ");
  528. csidump();
  529. /* die(""); */
  530. break;
  531. case '@': /* ICH -- Insert <n> blank char */
  532. DEFAULT(escseq.arg[0], 1);
  533. tinsertblank(escseq.arg[0]);
  534. break;
  535. case 'A': /* CUU -- Cursor <n> Up */
  536. case 'e':
  537. DEFAULT(escseq.arg[0], 1);
  538. tmoveto(term.c.x, term.c.y-escseq.arg[0]);
  539. break;
  540. case 'B': /* CUD -- Cursor <n> Down */
  541. DEFAULT(escseq.arg[0], 1);
  542. tmoveto(term.c.x, term.c.y+escseq.arg[0]);
  543. break;
  544. case 'C': /* CUF -- Cursor <n> Forward */
  545. case 'a':
  546. DEFAULT(escseq.arg[0], 1);
  547. tmoveto(term.c.x+escseq.arg[0], term.c.y);
  548. break;
  549. case 'D': /* CUB -- Cursor <n> Backward */
  550. DEFAULT(escseq.arg[0], 1);
  551. tmoveto(term.c.x-escseq.arg[0], term.c.y);
  552. break;
  553. case 'E': /* CNL -- Cursor <n> Down and first col */
  554. DEFAULT(escseq.arg[0], 1);
  555. tmoveto(0, term.c.y+escseq.arg[0]);
  556. break;
  557. case 'F': /* CPL -- Cursor <n> Up and first col */
  558. DEFAULT(escseq.arg[0], 1);
  559. tmoveto(0, term.c.y-escseq.arg[0]);
  560. break;
  561. case 'G': /* CHA -- Move to <col> */
  562. case '`': /* XXX: HPA -- same? */
  563. DEFAULT(escseq.arg[0], 1);
  564. tmoveto(escseq.arg[0]-1, term.c.y);
  565. break;
  566. case 'H': /* CUP -- Move to <row> <col> */
  567. case 'f': /* XXX: HVP -- same? */
  568. DEFAULT(escseq.arg[0], 1);
  569. DEFAULT(escseq.arg[1], 1);
  570. tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
  571. break;
  572. /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
  573. case 'J': /* ED -- Clear screen */
  574. switch(escseq.arg[0]) {
  575. case 0: /* below */
  576. tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
  577. break;
  578. case 1: /* above */
  579. tclearregion(0, 0, term.c.x, term.c.y);
  580. break;
  581. case 2: /* all */
  582. tclearregion(0, 0, term.col-1, term.row-1);
  583. break;
  584. case 3: /* XXX: erase saved lines (xterm) */
  585. default:
  586. goto unknown;
  587. }
  588. break;
  589. case 'K': /* EL -- Clear line */
  590. switch(escseq.arg[0]) {
  591. case 0: /* right */
  592. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  593. break;
  594. case 1: /* left */
  595. tclearregion(0, term.c.y, term.c.x, term.c.y);
  596. break;
  597. case 2: /* all */
  598. tclearregion(0, term.c.y, term.col-1, term.c.y);
  599. break;
  600. }
  601. break;
  602. case 'S': /* XXX: SU -- Scroll <n> line up (faked) */
  603. case 'L': /* IL -- Insert <n> blank lines */
  604. DEFAULT(escseq.arg[0], 1);
  605. tinsertblankline(escseq.arg[0]);
  606. break;
  607. case 'l': /* RM -- Reset Mode */
  608. if(escseq.priv) {
  609. switch(escseq.arg[0]) {
  610. case 1:
  611. term.mode &= ~MODE_APPKEYPAD;
  612. break;
  613. case 7:
  614. term.mode &= ~MODE_WRAP;
  615. break;
  616. case 12: /* att610 -- Stop blinking cursor (IGNORED) */
  617. break;
  618. case 25:
  619. term.c.hidden = 1;
  620. break;
  621. case 1048: /* XXX: no alt. screen to erase/save */
  622. case 1049:
  623. tcursor(CURSOR_LOAD);
  624. tclearregion(0, 0, term.col-1, term.row-1);
  625. break;
  626. default:
  627. goto unknown;
  628. }
  629. } else goto unknown;
  630. break;
  631. case 'M': /* DL -- Delete <n> lines */
  632. DEFAULT(escseq.arg[0], 1);
  633. tdeleteline(escseq.arg[0]);
  634. break;
  635. case 'X': /* ECH -- Erase <n> char XXX: same? */
  636. case 'P': /* DCH -- Delete <n> char */
  637. DEFAULT(escseq.arg[0], 1);
  638. tdeletechar(escseq.arg[0]);
  639. break;
  640. /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
  641. case 'd': /* VPA -- Move to <row> */
  642. DEFAULT(escseq.arg[0], 1);
  643. tmoveto(term.c.x, escseq.arg[0]-1);
  644. break;
  645. case 'h': /* SM -- Set terminal mode */
  646. if(escseq.priv) {
  647. switch(escseq.arg[0]) {
  648. case 1:
  649. term.mode |= MODE_APPKEYPAD;
  650. break;
  651. case 7:
  652. term.mode |= MODE_WRAP;
  653. break;
  654. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  655. break;
  656. case 25:
  657. term.c.hidden = 0;
  658. break;
  659. case 1048:
  660. case 1049: /* XXX: no alt. screen to erase/save */
  661. tcursor(CURSOR_SAVE);
  662. tclearregion(0, 0, term.col-1, term.row-1);
  663. break;
  664. default:
  665. goto unknown;
  666. }
  667. } else goto unknown;
  668. break;
  669. case 'm': /* SGR -- Terminal attribute (color) */
  670. tsetattr(escseq.arg, escseq.narg);
  671. break;
  672. case 'r': /* DECSTBM -- Set Scrolling Region */
  673. if(escseq.priv)
  674. goto unknown;
  675. else {
  676. DEFAULT(escseq.arg[0], 1);
  677. DEFAULT(escseq.arg[1], term.row);
  678. tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
  679. }
  680. break;
  681. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  682. tcursor(CURSOR_SAVE);
  683. break;
  684. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  685. tcursor(CURSOR_LOAD);
  686. break;
  687. }
  688. }
  689. void
  690. csidump(void) {
  691. int i;
  692. printf("ESC [ %s", escseq.priv ? "? " : "");
  693. if(escseq.narg)
  694. for(i = 0; i < escseq.narg; i++)
  695. printf("%d ", escseq.arg[i]);
  696. if(escseq.mode)
  697. putchar(escseq.mode);
  698. putchar('\n');
  699. }
  700. void
  701. csireset(void) {
  702. memset(&escseq, 0, sizeof(escseq));
  703. }
  704. void
  705. tputtab(void) {
  706. int space = TAB - term.c.x % TAB;
  707. if(term.c.x + space >= term.col)
  708. space--;
  709. for(; space > 0; space--)
  710. tmovecursor(CURSOR_RIGHT);
  711. }
  712. void
  713. tputc(char c) {
  714. /* dump(c); */
  715. if(term.esc & ESC_START) {
  716. if(term.esc & ESC_CSI) {
  717. escseq.buf[escseq.len++] = c;
  718. if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
  719. term.esc = 0;
  720. csiparse(), csihandle();
  721. }
  722. } else if(term.esc & ESC_OSC) {
  723. if(c == ';') {
  724. term.titlelen = 0;
  725. term.esc = ESC_START | ESC_TITLE;
  726. }
  727. } else if(term.esc & ESC_TITLE) {
  728. if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
  729. term.esc = 0;
  730. term.title[term.titlelen] = '\0';
  731. XStoreName(xw.dis, xw.win, term.title);
  732. } else {
  733. term.title[term.titlelen++] = c;
  734. }
  735. } else if(term.esc & ESC_ALTCHARSET) {
  736. switch(c) {
  737. case '0': /* Line drawing crap */
  738. term.c.attr.mode |= ATTR_GFX;
  739. break;
  740. case 'B': /* Back to regular text */
  741. term.c.attr.mode &= ~ATTR_GFX;
  742. break;
  743. default:
  744. printf("esc unhandled charset: ESC ( %c\n", c);
  745. }
  746. term.esc = 0;
  747. } else {
  748. switch(c) {
  749. case '[':
  750. term.esc |= ESC_CSI;
  751. break;
  752. case ']':
  753. term.esc |= ESC_OSC;
  754. break;
  755. case '(':
  756. term.esc |= ESC_ALTCHARSET;
  757. break;
  758. case 'A':
  759. tmoveto(term.c.x, term.c.y-1);
  760. term.esc = 0;
  761. break;
  762. case 'B':
  763. tmoveto(term.c.x, term.c.y+1);
  764. term.esc = 0;
  765. break;
  766. case 'C':
  767. tmoveto(term.c.x+1, term.c.y);
  768. term.esc = 0;
  769. break;
  770. case 'D':
  771. tmoveto(term.c.x-1, term.c.y);
  772. term.esc = 0;
  773. break;
  774. case '=': /* DECPAM */
  775. term.mode |= MODE_APPKEYPAD;
  776. term.esc = 0;
  777. break;
  778. case '>': /* DECPNM */
  779. term.mode &= ~MODE_APPKEYPAD;
  780. term.esc = 0;
  781. break;
  782. default:
  783. fprintf(stderr, "erresc: unknown sequence ESC %02X '%c'\n", c, isprint(c)?c:'.');
  784. term.esc = 0;
  785. }
  786. }
  787. } else {
  788. switch(c) {
  789. case '\t':
  790. tputtab();
  791. break;
  792. case '\b':
  793. tmovecursor(CURSOR_LEFT);
  794. break;
  795. case '\r':
  796. tmoveto(0, term.c.y);
  797. break;
  798. case '\n':
  799. tnewline();
  800. break;
  801. case '\a':
  802. xbell();
  803. break;
  804. case '\033':
  805. csireset();
  806. term.esc = ESC_START;
  807. break;
  808. default:
  809. tsetchar(c);
  810. tmovecursor(CURSOR_RIGHT);
  811. break;
  812. }
  813. }
  814. }
  815. void
  816. tputs(char *s, int len) {
  817. for(; len > 0; len--)
  818. tputc(*s++);
  819. }
  820. void
  821. tresize(int col, int row) {
  822. int i;
  823. Line *line;
  824. int minrow = MIN(row, term.row);
  825. int mincol = MIN(col, term.col);
  826. if(col < 1 || row < 1)
  827. return;
  828. /* alloc */
  829. line = calloc(row, sizeof(Line));
  830. for(i = 0 ; i < row; i++)
  831. line[i] = calloc(col, sizeof(Glyph));
  832. /* copy */
  833. for(i = 0 ; i < minrow; i++)
  834. memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
  835. /* free */
  836. for(i = 0; i < term.row; i++)
  837. free(term.line[i]);
  838. free(term.line);
  839. LIMIT(term.c.x, 0, col-1);
  840. LIMIT(term.c.y, 0, row-1);
  841. LIMIT(term.top, 0, row-1);
  842. LIMIT(term.bot, 0, row-1);
  843. term.bot = row-1;
  844. term.line = line;
  845. term.col = col, term.row = row;
  846. }
  847. unsigned long
  848. xgetcol(const char *s) {
  849. XColor color;
  850. Colormap cmap = DefaultColormap(xw.dis, xw.scr);
  851. if(!XAllocNamedColor(xw.dis, cmap, s, &color, &color)) {
  852. color.pixel = WhitePixel(xw.dis, xw.scr);
  853. fprintf(stderr, "Could not allocate color '%s'\n", s);
  854. }
  855. return color.pixel;
  856. }
  857. void
  858. xclear(int x1, int y1, int x2, int y2) {
  859. XClearArea(xw.dis, xw.win,
  860. x1 * xw.cw, y1 * xw.ch,
  861. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch,
  862. False);
  863. }
  864. void
  865. xscroll(void) {
  866. int srcy = (term.top+1) * xw.ch;
  867. int dsty = term.top * xw.ch;
  868. int height = (term.bot-term.top) * xw.ch;
  869. xcursor(CURSOR_HIDE);
  870. XCopyArea(xw.dis, xw.win, xw.win, dc.gc, 0, srcy, xw.w, height, 0, dsty);
  871. xclear(0, term.bot, term.col-1, term.bot);
  872. }
  873. void
  874. xinit(void) {
  875. XGCValues values;
  876. unsigned long valuemask;
  877. XClassHint chint;
  878. XWMHints wmhint;
  879. XSizeHints shint;
  880. char *args[] = {NULL};
  881. int i;
  882. xw.dis = XOpenDisplay(NULL);
  883. xw.scr = XDefaultScreen(xw.dis);
  884. if(!xw.dis)
  885. die("Can't open display\n");
  886. /* font */
  887. if(!(dc.font = XLoadQueryFont(xw.dis, FONT)))
  888. die("Can't load font %s\n", FONT);
  889. xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
  890. xw.ch = dc.font->ascent + dc.font->descent + LINESPACE;
  891. /* colors */
  892. for(i = 0; i < LEN(colorname); i++)
  893. dc.col[i] = xgetcol(colorname[i]);
  894. term.c.attr.fg = DefaultFG;
  895. term.c.attr.bg = DefaultBG;
  896. term.c.attr.mode = ATTR_NULL;
  897. /* windows */
  898. xw.h = term.row * xw.ch;
  899. xw.w = term.col * xw.cw;
  900. /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
  901. xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
  902. xw.w, xw.h, BORDER,
  903. dc.col[DefaultBG],
  904. dc.col[DefaultBG]);
  905. /* gc */
  906. values.foreground = XWhitePixel(xw.dis, xw.scr);
  907. values.font = dc.font->fid;
  908. valuemask = GCForeground | GCFont;
  909. dc.gc = XCreateGC(xw.dis, xw.win, valuemask, &values);
  910. XMapWindow(xw.dis, xw.win);
  911. /* wm stuff */
  912. chint.res_name = TNAME, chint.res_class = TNAME;
  913. wmhint.input = 1, wmhint.flags = InputHint;
  914. shint.height_inc = xw.ch, shint.width_inc = xw.cw;
  915. shint.height = xw.h, shint.width = xw.w;
  916. shint.flags = PSize | PResizeInc;
  917. XSetWMProperties(xw.dis, xw.win, NULL, NULL, &args[0], 0, &shint, &wmhint, &chint);
  918. XStoreName(xw.dis, xw.win, TNAME);
  919. XSync(xw.dis, 0);
  920. }
  921. void
  922. xdraws(char *s, Glyph base, int x, int y, int len) {
  923. unsigned long xfg, xbg;
  924. int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
  925. int i;
  926. if(base.mode & ATTR_REVERSE)
  927. xfg = dc.col[base.bg], xbg = dc.col[base.fg];
  928. else
  929. xfg = dc.col[base.fg], xbg = dc.col[base.bg];
  930. XSetBackground(xw.dis, dc.gc, xbg);
  931. XSetForeground(xw.dis, dc.gc, xfg);
  932. if(base.mode & ATTR_GFX)
  933. for(i = 0; i < len; i++)
  934. s[i] = gfx[s[i]];
  935. XDrawImageString(xw.dis, xw.win, dc.gc, winx, winy, s, len);
  936. if(base.mode & ATTR_UNDERLINE)
  937. XDrawLine(xw.dis, xw.win, dc.gc, winx, winy+1, winx+width-1, winy+1);
  938. }
  939. void
  940. xdrawc(int x, int y, Glyph g) {
  941. XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
  942. unsigned long xfg, xbg;
  943. /* reverse video */
  944. if(g.mode & ATTR_REVERSE)
  945. xfg = dc.col[g.bg], xbg = dc.col[g.fg];
  946. else
  947. xfg = dc.col[g.fg], xbg = dc.col[g.bg];
  948. /* background */
  949. XSetBackground(xw.dis, dc.gc, xbg);
  950. XSetForeground(xw.dis, dc.gc, xfg);
  951. XDrawImageString(xw.dis, xw.win, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
  952. }
  953. void
  954. xcursor(int mode) {
  955. static int oldx = 0;
  956. static int oldy = 0;
  957. Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
  958. LIMIT(oldx, 0, term.col-1);
  959. LIMIT(oldy, 0, term.row-1);
  960. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  961. g.c = term.line[term.c.y][term.c.x].c;
  962. /* remove the old cursor */
  963. if(term.line[oldy][oldx].state & GLYPH_SET)
  964. xdrawc(oldx, oldy, term.line[oldy][oldx]);
  965. else
  966. xclear(oldx, oldy, oldx, oldy);
  967. /* draw the new one */
  968. if(mode == CURSOR_DRAW) {
  969. xdrawc(term.c.x, term.c.y, g);
  970. oldx = term.c.x, oldy = term.c.y;
  971. }
  972. }
  973. void
  974. draw(int redraw_all) {
  975. int i, x, y, ox;
  976. Glyph base, new;
  977. char buf[DRAW_BUF_SIZ];
  978. for(y = 0; y < term.row; y++) {
  979. base = term.line[y][0];
  980. i = ox = 0;
  981. for(x = 0; x < term.col; x++) {
  982. new = term.line[y][x];
  983. if(!ATTRCMP(base, new) && i < DRAW_BUF_SIZ)
  984. buf[i++] = new.c;
  985. else {
  986. xdraws(buf, base, ox, y, i);
  987. buf[0] = new.c;
  988. i = 1;
  989. ox = x;
  990. base = new;
  991. }
  992. }
  993. xdraws(buf, base, ox, y, i);
  994. }
  995. if(!term.c.hidden)
  996. xcursor(CURSOR_DRAW);
  997. }
  998. void
  999. expose(XEvent *ev) {
  1000. draw(SCREEN_REDRAW);
  1001. }
  1002. char*
  1003. kmap(KeySym k) {
  1004. int i;
  1005. for(i = 0; i < LEN(key); i++)
  1006. if(key[i].k == k)
  1007. return (char*)key[i].s;
  1008. return NULL;
  1009. }
  1010. void
  1011. kpress(XEvent *ev) {
  1012. XKeyEvent *e = &ev->xkey;
  1013. KeySym ksym;
  1014. char buf[32];
  1015. char *customkey;
  1016. int len;
  1017. int meta;
  1018. int shift;
  1019. meta = e->state & Mod1Mask;
  1020. shift = e->state & ShiftMask;
  1021. len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
  1022. if(customkey = kmap(ksym))
  1023. ttywrite(customkey, strlen(customkey));
  1024. else if(len > 0) {
  1025. buf[sizeof(buf)-1] = '\0';
  1026. if(meta && len == 1)
  1027. ttywrite("\033", 1);
  1028. ttywrite(buf, len);
  1029. } else
  1030. switch(ksym) {
  1031. case XK_Up:
  1032. case XK_Down:
  1033. case XK_Left:
  1034. case XK_Right:
  1035. sprintf(buf, "\033%c%c", term.mode & MODE_APPKEYPAD ? 'O' : '[', "DACB"[ksym - XK_Left]);
  1036. ttywrite(buf, 3);
  1037. break;
  1038. case XK_Insert:
  1039. if(shift)
  1040. /* XXX: paste X clipboard */;
  1041. break;
  1042. default:
  1043. fprintf(stderr, "errkey: %d\n", (int)ksym);
  1044. break;
  1045. }
  1046. }
  1047. void
  1048. resize(XEvent *e) {
  1049. int col, row;
  1050. col = e->xconfigure.width / xw.cw;
  1051. row = e->xconfigure.height / xw.ch;
  1052. if(term.col != col || term.row != row) {
  1053. tresize(col, row);
  1054. ttyresize(col, row);
  1055. xw.w = e->xconfigure.width;
  1056. xw.h = e->xconfigure.height;
  1057. draw(SCREEN_REDRAW);
  1058. }
  1059. }
  1060. void
  1061. run(void) {
  1062. XEvent ev;
  1063. fd_set rfd;
  1064. int xfd = XConnectionNumber(xw.dis);
  1065. running = 1;
  1066. XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
  1067. XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* fix resize bug in wmii (?) */
  1068. while(running) {
  1069. FD_ZERO(&rfd);
  1070. FD_SET(cmdfd, &rfd);
  1071. FD_SET(xfd, &rfd);
  1072. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) == -1) {
  1073. if(errno == EINTR)
  1074. continue;
  1075. die("select failed: %s\n", SERRNO);
  1076. }
  1077. if(FD_ISSET(cmdfd, &rfd)) {
  1078. ttyread();
  1079. draw(SCREEN_UPDATE);
  1080. }
  1081. while(XPending(xw.dis)) {
  1082. XNextEvent(xw.dis, &ev);
  1083. if(handler[ev.type])
  1084. (handler[ev.type])(&ev);
  1085. }
  1086. }
  1087. }
  1088. int
  1089. main(int argc, char *argv[]) {
  1090. if(argc == 2 && !strncmp("-v", argv[1], 3))
  1091. die("st-" VERSION ", © 2009 st engineers\n");
  1092. else if(argc != 1)
  1093. die("usage: st [-v]\n");
  1094. setlocale(LC_CTYPE, "");
  1095. tnew(80, 24);
  1096. ttynew();
  1097. xinit();
  1098. run();
  1099. return 0;
  1100. }