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.

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