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.

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