My build of suckless st terminal
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.

1304 lines
28 KiB

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