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.

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