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.

1302 lines
28 KiB

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