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.

1302 lines
28 KiB

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