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.

1364 lines
30 KiB

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