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.

1346 lines
30 KiB

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