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

1346 lines
30 KiB

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