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.

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