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.

1323 lines
28 KiB

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