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.

1330 lines
29 KiB

14 years ago
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 <pty.h>
  20. #include <X11/Xlib.h>
  21. #include <X11/keysym.h>
  22. #include <X11/Xutil.h>
  23. #if defined(LINUX)
  24. #include <pty.h>
  25. #elif defined(OPENBSD)
  26. #include <util.h>
  27. #elif defined(FREEBSD)
  28. #include <libutil.h>
  29. #endif
  30. /* Arbitrary sizes */
  31. #define ESC_TITLE_SIZ 256
  32. #define ESC_BUF_SIZ 256
  33. #define ESC_ARG_SIZ 16
  34. #define DRAW_BUF_SIZ 1024
  35. #define SERRNO strerror(errno)
  36. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  37. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  38. #define LEN(a) (sizeof(a) / sizeof(a[0]))
  39. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  40. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  41. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  42. #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
  43. #define IS_SET(flag) (term.mode & (flag))
  44. /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
  45. enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 };
  46. enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT, CURSOR_HIDE = 1,
  47. CURSOR_DRAW = 0, CURSOR_SAVE, CURSOR_LOAD };
  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 hide;
  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 xcursor(int);
  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, .hide = 0};
  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. term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x;
  365. term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y;
  366. }
  367. void
  368. tsetchar(char c) {
  369. term.line[term.c.y][term.c.x] = term.c.attr;
  370. term.line[term.c.y][term.c.x].c = c;
  371. term.line[term.c.y][term.c.x].state |= GLYPH_SET;
  372. }
  373. void
  374. tclearregion(int x1, int y1, int x2, int y2) {
  375. int y, temp;
  376. if(x1 > x2)
  377. temp = x1, x1 = x2, x2 = temp;
  378. if(y1 > y2)
  379. temp = y1, y1 = y2, y2 = temp;
  380. LIMIT(x1, 0, term.col-1);
  381. LIMIT(x2, 0, term.col-1);
  382. LIMIT(y1, 0, term.row-1);
  383. LIMIT(y2, 0, term.row-1);
  384. for(y = y1; y <= y2; y++)
  385. memset(&term.line[y][x1], 0, sizeof(Glyph)*(x2-x1+1));
  386. }
  387. void
  388. tdeletechar(int n) {
  389. int src = term.c.x + n;
  390. int dst = term.c.x;
  391. int size = term.col - src;
  392. if(src >= term.col) {
  393. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  394. return;
  395. }
  396. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  397. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  398. }
  399. void
  400. tinsertblank(int n) {
  401. int src = term.c.x;
  402. int dst = src + n;
  403. int size = term.col - dst;
  404. if(dst >= term.col) {
  405. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  406. return;
  407. }
  408. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  409. tclearregion(src, term.c.y, dst - 1, term.c.y);
  410. }
  411. void
  412. tinsertblankline(int n) {
  413. int i;
  414. Line blank;
  415. int bot = term.bot;
  416. if(term.c.y > term.bot)
  417. bot = term.row - 1;
  418. else if(term.c.y < term.top)
  419. bot = term.top - 1;
  420. if(term.c.y + n >= bot) {
  421. tclearregion(0, term.c.y, term.col-1, bot);
  422. return;
  423. }
  424. for(i = bot; i >= term.c.y+n; i--) {
  425. /* swap deleted line <-> blanked line */
  426. blank = term.line[i];
  427. term.line[i] = term.line[i-n];
  428. term.line[i-n] = blank;
  429. /* blank it */
  430. memset(blank, 0, term.col * sizeof(Glyph));
  431. }
  432. }
  433. void
  434. tdeleteline(int n) {
  435. int i;
  436. Line blank;
  437. int bot = term.bot;
  438. if(term.c.y > term.bot)
  439. bot = term.row - 1;
  440. else if(term.c.y < term.top)
  441. bot = term.top - 1;
  442. if(term.c.y + n >= bot) {
  443. tclearregion(0, term.c.y, term.col-1, bot);
  444. return;
  445. }
  446. for(i = term.c.y; i <= bot-n; i++) {
  447. /* swap deleted line <-> blanked line */
  448. blank = term.line[i];
  449. term.line[i] = term.line[i+n];
  450. term.line[i+n] = blank;
  451. /* blank it */
  452. memset(blank, 0, term.col * sizeof(Glyph));
  453. }
  454. }
  455. void
  456. tsetattr(int *attr, int l) {
  457. int i;
  458. for(i = 0; i < l; i++) {
  459. switch(attr[i]) {
  460. case 0:
  461. term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
  462. term.c.attr.fg = DefaultFG;
  463. term.c.attr.bg = DefaultBG;
  464. break;
  465. case 1:
  466. term.c.attr.mode |= ATTR_BOLD;
  467. break;
  468. case 4:
  469. term.c.attr.mode |= ATTR_UNDERLINE;
  470. break;
  471. case 7:
  472. term.c.attr.mode |= ATTR_REVERSE;
  473. break;
  474. case 22:
  475. term.c.attr.mode &= ~ATTR_BOLD;
  476. break;
  477. case 24:
  478. term.c.attr.mode &= ~ATTR_UNDERLINE;
  479. break;
  480. case 27:
  481. term.c.attr.mode &= ~ATTR_REVERSE;
  482. break;
  483. case 38:
  484. if (i + 2 < l && attr[i + 1] == 5) {
  485. i += 2;
  486. if (BETWEEN(attr[i], 0, 255))
  487. term.c.attr.fg = attr[i];
  488. else
  489. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
  490. }
  491. else
  492. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  493. break;
  494. case 39:
  495. term.c.attr.fg = DefaultFG;
  496. break;
  497. case 48:
  498. if (i + 2 < l && attr[i + 1] == 5) {
  499. i += 2;
  500. if (BETWEEN(attr[i], 0, 255))
  501. term.c.attr.bg = attr[i];
  502. else
  503. fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
  504. }
  505. else
  506. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  507. break;
  508. case 49:
  509. term.c.attr.bg = DefaultBG;
  510. break;
  511. default:
  512. if(BETWEEN(attr[i], 30, 37))
  513. term.c.attr.fg = attr[i] - 30;
  514. else if(BETWEEN(attr[i], 40, 47))
  515. term.c.attr.bg = attr[i] - 40;
  516. else if(BETWEEN(attr[i], 90, 97))
  517. term.c.attr.fg = attr[i] - 90 + 8;
  518. else if(BETWEEN(attr[i], 100, 107))
  519. term.c.attr.fg = attr[i] - 100 + 8;
  520. else
  521. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  522. break;
  523. }
  524. }
  525. }
  526. void
  527. tsetscroll(int t, int b) {
  528. int temp;
  529. LIMIT(t, 0, term.row-1);
  530. LIMIT(b, 0, term.row-1);
  531. if(t > b) {
  532. temp = t;
  533. t = b;
  534. b = temp;
  535. }
  536. term.top = t;
  537. term.bot = b;
  538. }
  539. void
  540. csihandle(void) {
  541. switch(escseq.mode) {
  542. default:
  543. unknown:
  544. printf("erresc: unknown csi ");
  545. csidump();
  546. /* die(""); */
  547. break;
  548. case '@': /* ICH -- Insert <n> blank char */
  549. DEFAULT(escseq.arg[0], 1);
  550. tinsertblank(escseq.arg[0]);
  551. break;
  552. case 'A': /* CUU -- Cursor <n> Up */
  553. case 'e':
  554. DEFAULT(escseq.arg[0], 1);
  555. tmoveto(term.c.x, term.c.y-escseq.arg[0]);
  556. break;
  557. case 'B': /* CUD -- Cursor <n> Down */
  558. DEFAULT(escseq.arg[0], 1);
  559. tmoveto(term.c.x, term.c.y+escseq.arg[0]);
  560. break;
  561. case 'C': /* CUF -- Cursor <n> Forward */
  562. case 'a':
  563. DEFAULT(escseq.arg[0], 1);
  564. tmoveto(term.c.x+escseq.arg[0], term.c.y);
  565. break;
  566. case 'D': /* CUB -- Cursor <n> Backward */
  567. DEFAULT(escseq.arg[0], 1);
  568. tmoveto(term.c.x-escseq.arg[0], term.c.y);
  569. break;
  570. case 'E': /* CNL -- Cursor <n> Down and first col */
  571. DEFAULT(escseq.arg[0], 1);
  572. tmoveto(0, term.c.y+escseq.arg[0]);
  573. break;
  574. case 'F': /* CPL -- Cursor <n> Up and first col */
  575. DEFAULT(escseq.arg[0], 1);
  576. tmoveto(0, term.c.y-escseq.arg[0]);
  577. break;
  578. case 'G': /* CHA -- Move to <col> */
  579. case '`': /* XXX: HPA -- same? */
  580. DEFAULT(escseq.arg[0], 1);
  581. tmoveto(escseq.arg[0]-1, term.c.y);
  582. break;
  583. case 'H': /* CUP -- Move to <row> <col> */
  584. case 'f': /* XXX: HVP -- same? */
  585. DEFAULT(escseq.arg[0], 1);
  586. DEFAULT(escseq.arg[1], 1);
  587. tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
  588. break;
  589. /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
  590. case 'J': /* ED -- Clear screen */
  591. switch(escseq.arg[0]) {
  592. case 0: /* below */
  593. tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
  594. break;
  595. case 1: /* above */
  596. tclearregion(0, 0, term.c.x, term.c.y);
  597. break;
  598. case 2: /* all */
  599. tclearregion(0, 0, term.col-1, term.row-1);
  600. break;
  601. case 3: /* XXX: erase saved lines (xterm) */
  602. default:
  603. goto unknown;
  604. }
  605. break;
  606. case 'K': /* EL -- Clear line */
  607. switch(escseq.arg[0]) {
  608. case 0: /* right */
  609. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  610. break;
  611. case 1: /* left */
  612. tclearregion(0, term.c.y, term.c.x, term.c.y);
  613. break;
  614. case 2: /* all */
  615. tclearregion(0, term.c.y, term.col-1, term.c.y);
  616. break;
  617. }
  618. break;
  619. case 'S': /* SU -- Scroll <n> line up */
  620. DEFAULT(escseq.arg[0], 1);
  621. tscrollup(escseq.arg[0]);
  622. break;
  623. case 'T': /* SD -- Scroll <n> line down */
  624. DEFAULT(escseq.arg[0], 1);
  625. tscrolldown(escseq.arg[0]);
  626. break;
  627. case 'L': /* IL -- Insert <n> blank lines */
  628. DEFAULT(escseq.arg[0], 1);
  629. tinsertblankline(escseq.arg[0]);
  630. break;
  631. case 'l': /* RM -- Reset Mode */
  632. if(escseq.priv) {
  633. switch(escseq.arg[0]) {
  634. case 1:
  635. term.mode &= ~MODE_APPKEYPAD;
  636. break;
  637. case 7:
  638. term.mode &= ~MODE_WRAP;
  639. break;
  640. case 12: /* att610 -- Stop blinking cursor (IGNORED) */
  641. break;
  642. case 25:
  643. term.c.hide = CURSOR_HIDE;
  644. break;
  645. case 1048: /* XXX: no alt. screen to erase/save */
  646. case 1049:
  647. tcursor(CURSOR_LOAD);
  648. tclearregion(0, 0, term.col-1, term.row-1);
  649. break;
  650. default:
  651. goto unknown;
  652. }
  653. } else {
  654. switch(escseq.arg[0]) {
  655. case 4:
  656. term.mode &= ~MODE_INSERT;
  657. break;
  658. default:
  659. goto unknown;
  660. }
  661. }
  662. break;
  663. case 'M': /* DL -- Delete <n> lines */
  664. DEFAULT(escseq.arg[0], 1);
  665. tdeleteline(escseq.arg[0]);
  666. break;
  667. case 'X': /* ECH -- Erase <n> char */
  668. DEFAULT(escseq.arg[0], 1);
  669. tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
  670. break;
  671. case 'P': /* DCH -- Delete <n> char */
  672. DEFAULT(escseq.arg[0], 1);
  673. tdeletechar(escseq.arg[0]);
  674. break;
  675. /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
  676. case 'd': /* VPA -- Move to <row> */
  677. DEFAULT(escseq.arg[0], 1);
  678. tmoveto(term.c.x, escseq.arg[0]-1);
  679. break;
  680. case 'h': /* SM -- Set terminal mode */
  681. if(escseq.priv) {
  682. switch(escseq.arg[0]) {
  683. case 1:
  684. term.mode |= MODE_APPKEYPAD;
  685. break;
  686. case 7:
  687. term.mode |= MODE_WRAP;
  688. break;
  689. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  690. break;
  691. case 25:
  692. term.c.hide = CURSOR_DRAW;
  693. break;
  694. case 1048:
  695. case 1049: /* XXX: no alt. screen to erase/save */
  696. tcursor(CURSOR_SAVE);
  697. tclearregion(0, 0, term.col-1, term.row-1);
  698. break;
  699. default: goto unknown;
  700. }
  701. } else {
  702. switch(escseq.arg[0]) {
  703. case 4:
  704. term.mode |= MODE_INSERT;
  705. break;
  706. default: goto unknown;
  707. }
  708. };
  709. break;
  710. case 'm': /* SGR -- Terminal attribute (color) */
  711. tsetattr(escseq.arg, escseq.narg);
  712. break;
  713. case 'r': /* DECSTBM -- Set Scrolling Region */
  714. if(escseq.priv)
  715. goto unknown;
  716. else {
  717. DEFAULT(escseq.arg[0], 1);
  718. DEFAULT(escseq.arg[1], term.row);
  719. tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
  720. }
  721. break;
  722. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  723. tcursor(CURSOR_SAVE);
  724. break;
  725. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  726. tcursor(CURSOR_LOAD);
  727. break;
  728. }
  729. }
  730. void
  731. csidump(void) {
  732. int i;
  733. printf("ESC [ %s", escseq.priv ? "? " : "");
  734. if(escseq.narg)
  735. for(i = 0; i < escseq.narg; i++)
  736. printf("%d ", escseq.arg[i]);
  737. if(escseq.mode)
  738. putchar(escseq.mode);
  739. putchar('\n');
  740. }
  741. void
  742. csireset(void) {
  743. memset(&escseq, 0, sizeof(escseq));
  744. }
  745. void
  746. tputtab(void) {
  747. int space = TAB - term.c.x % TAB;
  748. tmoveto(term.c.x + space, term.c.y);
  749. }
  750. void
  751. tputc(char c) {
  752. if(term.esc & ESC_START) {
  753. if(term.esc & ESC_CSI) {
  754. escseq.buf[escseq.len++] = c;
  755. if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
  756. term.esc = 0;
  757. csiparse(), csihandle();
  758. }
  759. } else if(term.esc & ESC_OSC) {
  760. if(c == ';') {
  761. term.titlelen = 0;
  762. term.esc = ESC_START | ESC_TITLE;
  763. }
  764. } else if(term.esc & ESC_TITLE) {
  765. if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
  766. term.esc = 0;
  767. term.title[term.titlelen] = '\0';
  768. XStoreName(xw.dis, xw.win, term.title);
  769. } else {
  770. term.title[term.titlelen++] = c;
  771. }
  772. } else if(term.esc & ESC_ALTCHARSET) {
  773. switch(c) {
  774. case '0': /* Line drawing crap */
  775. term.c.attr.mode |= ATTR_GFX;
  776. break;
  777. case 'B': /* Back to regular text */
  778. term.c.attr.mode &= ~ATTR_GFX;
  779. break;
  780. default:
  781. printf("esc unhandled charset: ESC ( %c\n", c);
  782. }
  783. term.esc = 0;
  784. } else {
  785. switch(c) {
  786. case '[':
  787. term.esc |= ESC_CSI;
  788. break;
  789. case ']':
  790. term.esc |= ESC_OSC;
  791. break;
  792. case '(':
  793. term.esc |= ESC_ALTCHARSET;
  794. break;
  795. case 'A':
  796. tmoveto(term.c.x, term.c.y-1);
  797. term.esc = 0;
  798. break;
  799. case 'B':
  800. tmoveto(term.c.x, term.c.y+1);
  801. term.esc = 0;
  802. break;
  803. case 'C':
  804. tmoveto(term.c.x+1, term.c.y);
  805. term.esc = 0;
  806. break;
  807. case 'D': /* XXX: CUP (VT100) or IND (VT52) ... */
  808. tmoveto(term.c.x-1, term.c.y);
  809. term.esc = 0;
  810. break;
  811. case 'E': /* NEL -- Next line */
  812. tnewline();
  813. term.esc = 0;
  814. break;
  815. case 'M': /* RI -- Reverse index */
  816. if(term.c.y == term.top)
  817. tscrolldown(1);
  818. else
  819. tmoveto(term.c.x, term.c.y-1);
  820. term.esc = 0;
  821. break;
  822. case 'c': /* RIS -- Reset to inital state */
  823. treset();
  824. term.esc = 0;
  825. break;
  826. case '=': /* DECPAM */
  827. term.mode |= MODE_APPKEYPAD;
  828. term.esc = 0;
  829. break;
  830. case '>': /* DECPNM */
  831. term.mode &= ~MODE_APPKEYPAD;
  832. term.esc = 0;
  833. break;
  834. case '7':
  835. tcursor(CURSOR_SAVE);
  836. term.esc = 0;
  837. break;
  838. case '8':
  839. tcursor(CURSOR_LOAD);
  840. term.esc = 0;
  841. break;
  842. default:
  843. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
  844. term.esc = 0;
  845. }
  846. }
  847. } else {
  848. switch(c) {
  849. case '\t':
  850. tputtab();
  851. break;
  852. case '\b':
  853. tmoveto(term.c.x-1, term.c.y);
  854. break;
  855. case '\r':
  856. tmoveto(0, term.c.y);
  857. break;
  858. case '\n':
  859. tnewline();
  860. break;
  861. case '\a':
  862. xbell();
  863. break;
  864. case '\033':
  865. csireset();
  866. term.esc = ESC_START;
  867. break;
  868. default:
  869. tsetchar(c);
  870. if(term.c.x+1 < term.col) {
  871. tmoveto(term.c.x+1, term.c.y);
  872. } else if(IS_SET(MODE_WRAP))
  873. tnewline();
  874. break;
  875. }
  876. }
  877. }
  878. void
  879. tputs(char *s, int len) {
  880. for(; len > 0; len--)
  881. tputc(*s++);
  882. }
  883. void
  884. tresize(int col, int row) {
  885. int i;
  886. int minrow = MIN(row, term.row);
  887. int mincol = MIN(col, term.col);
  888. if(col < 1 || row < 1)
  889. return;
  890. /* free uneeded rows */
  891. for(i = row; i < term.row; i++)
  892. free(term.line[i]);
  893. /* resize to new height */
  894. term.line = realloc(term.line, row * sizeof(Line));
  895. /* resize each row to new width, zero-pad if needed */
  896. for(i = 0; i < minrow; i++) {
  897. term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
  898. memset(term.line[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
  899. }
  900. /* allocate any new rows */
  901. for(/* i == minrow */; i < row; i++)
  902. term.line[i] = calloc(col, sizeof(Glyph));
  903. LIMIT(term.c.x, 0, col-1);
  904. LIMIT(term.c.y, 0, row-1);
  905. LIMIT(term.top, 0, row-1);
  906. LIMIT(term.bot, 0, row-1);
  907. term.bot = row-1;
  908. term.col = col, term.row = row;
  909. }
  910. void
  911. xloadcols(void) {
  912. int i, r, g, b;
  913. XColor color;
  914. Colormap cmap = DefaultColormap(xw.dis, xw.scr);
  915. unsigned long white = WhitePixel(xw.dis, xw.scr);
  916. for(i = 0; i < 16; i++) {
  917. if (!XAllocNamedColor(xw.dis, cmap, colorname[i], &color, &color)) {
  918. dc.col[i] = white;
  919. fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
  920. } else
  921. dc.col[i] = color.pixel;
  922. }
  923. /* same colors as xterm */
  924. for(r = 0; r < 6; r++)
  925. for(g = 0; g < 6; g++)
  926. for(b = 0; b < 6; b++) {
  927. color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
  928. color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
  929. color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
  930. if (!XAllocColor(xw.dis, cmap, &color)) {
  931. dc.col[i] = white;
  932. fprintf(stderr, "Could not allocate color %d\n", i);
  933. } else
  934. dc.col[i] = color.pixel;
  935. i++;
  936. }
  937. for(r = 0; r < 24; r++, i++) {
  938. color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
  939. if (!XAllocColor(xw.dis, cmap, &color)) {
  940. dc.col[i] = white;
  941. fprintf(stderr, "Could not allocate color %d\n", i);
  942. } else
  943. dc.col[i] = color.pixel;
  944. }
  945. }
  946. void
  947. xclear(int x1, int y1, int x2, int y2) {
  948. XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
  949. XFillRectangle(xw.dis, xw.buf, dc.gc,
  950. x1 * xw.cw, y1 * xw.ch,
  951. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
  952. }
  953. void
  954. xhints(void)
  955. {
  956. XClassHint class = {TNAME, TNAME};
  957. XWMHints wm = {.flags = InputHint, .input = 1};
  958. XSizeHints size = {
  959. .flags = PSize | PResizeInc | PBaseSize,
  960. .height = xw.h,
  961. .width = xw.w,
  962. .height_inc = xw.ch,
  963. .width_inc = xw.cw,
  964. .base_height = 2*BORDER,
  965. .base_width = 2*BORDER,
  966. };
  967. XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
  968. }
  969. void
  970. xinit(void) {
  971. if(!(xw.dis = XOpenDisplay(NULL)))
  972. die("Can't open display\n");
  973. xw.scr = XDefaultScreen(xw.dis);
  974. /* font */
  975. if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
  976. die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
  977. /* XXX: Assuming same size for bold font */
  978. xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
  979. xw.ch = dc.font->ascent + dc.font->descent;
  980. /* colors */
  981. xloadcols();
  982. term.c.attr.fg = DefaultFG;
  983. term.c.attr.bg = DefaultBG;
  984. term.c.attr.mode = ATTR_NULL;
  985. /* windows */
  986. xw.h = term.row * xw.ch + 2*BORDER;
  987. xw.w = term.col * xw.cw + 2*BORDER;
  988. xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
  989. xw.w, xw.h, 0,
  990. dc.col[DefaultBG],
  991. dc.col[DefaultBG]);
  992. xw.bufw = xw.w - 2*BORDER;
  993. xw.bufh = xw.h - 2*BORDER;
  994. xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
  995. /* gc */
  996. dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
  997. XMapWindow(xw.dis, xw.win);
  998. xhints();
  999. XStoreName(xw.dis, xw.win, "st");
  1000. XSync(xw.dis, 0);
  1001. }
  1002. void
  1003. xdraws(char *s, Glyph base, int x, int y, int len) {
  1004. unsigned long xfg, xbg;
  1005. int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
  1006. int i;
  1007. if(base.mode & ATTR_REVERSE)
  1008. xfg = dc.col[base.bg], xbg = dc.col[base.fg];
  1009. else
  1010. xfg = dc.col[base.fg], xbg = dc.col[base.bg];
  1011. XSetBackground(xw.dis, dc.gc, xbg);
  1012. XSetForeground(xw.dis, dc.gc, xfg);
  1013. if(base.mode & ATTR_GFX)
  1014. for(i = 0; i < len; i++)
  1015. s[i] = gfx[(int)s[i]];
  1016. XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
  1017. XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
  1018. if(base.mode & ATTR_UNDERLINE)
  1019. XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
  1020. }
  1021. void
  1022. xcursor(int mode) {
  1023. static int oldx = 0;
  1024. static int oldy = 0;
  1025. Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
  1026. LIMIT(oldx, 0, term.col-1);
  1027. LIMIT(oldy, 0, term.row-1);
  1028. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  1029. g.c = term.line[term.c.y][term.c.x].c;
  1030. /* remove the old cursor */
  1031. if(term.line[oldy][oldx].state & GLYPH_SET)
  1032. xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
  1033. else
  1034. xclear(oldx, oldy, oldx, oldy);
  1035. /* draw the new one */
  1036. if(mode == CURSOR_DRAW) {
  1037. xdraws(&g.c, g, term.c.x, term.c.y, 1);
  1038. oldx = term.c.x, oldy = term.c.y;
  1039. }
  1040. }
  1041. #ifdef DEBUG
  1042. /* basic drawing routines */
  1043. void
  1044. xdrawc(int x, int y, Glyph g) {
  1045. XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
  1046. XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
  1047. XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
  1048. XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
  1049. XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
  1050. }
  1051. void
  1052. draw(int dummy) {
  1053. int x, y;
  1054. xclear(0, 0, term.col-1, term.row-1);
  1055. for(y = 0; y < term.row; y++)
  1056. for(x = 0; x < term.col; x++)
  1057. if(term.line[y][x].state & GLYPH_SET)
  1058. xdrawc(x, y, term.line[y][x]);
  1059. xcursor(term.c.hide);
  1060. XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1061. XFlush(xw.dis);
  1062. }
  1063. #else
  1064. /* optimized drawing routine */
  1065. void
  1066. draw(int redraw_all) {
  1067. int i, x, y, ox;
  1068. Glyph base, new;
  1069. char buf[DRAW_BUF_SIZ];
  1070. XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
  1071. XFillRectangle(xw.dis, xw.buf, dc.gc, 0, 0, xw.bufw, xw.bufh);
  1072. for(y = 0; y < term.row; y++) {
  1073. base = term.line[y][0];
  1074. i = ox = 0;
  1075. for(x = 0; x < term.col; x++) {
  1076. new = term.line[y][x];
  1077. if(i > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
  1078. i >= DRAW_BUF_SIZ)) {
  1079. xdraws(buf, base, ox, y, i);
  1080. i = 0;
  1081. }
  1082. if(new.state & GLYPH_SET) {
  1083. if(i == 0) {
  1084. ox = x;
  1085. base = new;
  1086. }
  1087. buf[i++] = new.c;
  1088. }
  1089. }
  1090. if(i > 0)
  1091. xdraws(buf, base, ox, y, i);
  1092. }
  1093. xcursor(term.c.hide);
  1094. XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1095. XFlush(xw.dis);
  1096. }
  1097. #endif
  1098. void
  1099. expose(XEvent *ev) {
  1100. draw(SCREEN_REDRAW);
  1101. }
  1102. char*
  1103. kmap(KeySym k) {
  1104. int i;
  1105. for(i = 0; i < LEN(key); i++)
  1106. if(key[i].k == k)
  1107. return (char*)key[i].s;
  1108. return NULL;
  1109. }
  1110. void
  1111. kpress(XEvent *ev) {
  1112. XKeyEvent *e = &ev->xkey;
  1113. KeySym ksym;
  1114. char buf[32];
  1115. char *customkey;
  1116. int len;
  1117. int meta;
  1118. int shift;
  1119. meta = e->state & Mod1Mask;
  1120. shift = e->state & ShiftMask;
  1121. len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
  1122. if((customkey = kmap(ksym)))
  1123. ttywrite(customkey, strlen(customkey));
  1124. else if(len > 0) {
  1125. buf[sizeof(buf)-1] = '\0';
  1126. if(meta && len == 1)
  1127. ttywrite("\033", 1);
  1128. ttywrite(buf, len);
  1129. } else
  1130. switch(ksym) {
  1131. case XK_Up:
  1132. case XK_Down:
  1133. case XK_Left:
  1134. case XK_Right:
  1135. sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', "DACB"[ksym - XK_Left]);
  1136. ttywrite(buf, 3);
  1137. break;
  1138. case XK_Insert:
  1139. if(shift)
  1140. draw(1), puts("draw!")/* XXX: paste X clipboard */;
  1141. break;
  1142. default:
  1143. fprintf(stderr, "errkey: %d\n", (int)ksym);
  1144. break;
  1145. }
  1146. }
  1147. void
  1148. resize(XEvent *e) {
  1149. int col, row;
  1150. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  1151. return;
  1152. xw.w = e->xconfigure.width;
  1153. xw.h = e->xconfigure.height;
  1154. xw.bufw = xw.w - 2*BORDER;
  1155. xw.bufh = xw.h - 2*BORDER;
  1156. col = xw.bufw / xw.cw;
  1157. row = xw.bufh / xw.ch;
  1158. tresize(col, row);
  1159. ttyresize(col, row);
  1160. XFreePixmap(xw.dis, xw.buf);
  1161. xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
  1162. draw(SCREEN_REDRAW);
  1163. }
  1164. void
  1165. run(void) {
  1166. XEvent ev;
  1167. fd_set rfd;
  1168. int xfd = XConnectionNumber(xw.dis);
  1169. running = 1;
  1170. XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
  1171. XResizeWindow(xw.dis, xw.win, xw.w, xw.h); /* XXX: fix resize bug in wmii (?) */
  1172. while(running) {
  1173. FD_ZERO(&rfd);
  1174. FD_SET(cmdfd, &rfd);
  1175. FD_SET(xfd, &rfd);
  1176. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
  1177. if(errno == EINTR)
  1178. continue;
  1179. die("select failed: %s\n", SERRNO);
  1180. }
  1181. if(FD_ISSET(cmdfd, &rfd)) {
  1182. ttyread();
  1183. draw(SCREEN_UPDATE);
  1184. }
  1185. while(XPending(xw.dis)) {
  1186. XNextEvent(xw.dis, &ev);
  1187. if(handler[ev.type])
  1188. (handler[ev.type])(&ev);
  1189. }
  1190. }
  1191. }
  1192. int
  1193. main(int argc, char *argv[]) {
  1194. if(argc == 2 && !strncmp("-v", argv[1], 3))
  1195. die("st-" VERSION ", (c) 2010 st engineers\n");
  1196. else if(argc != 1)
  1197. die("usage: st [-v]\n");
  1198. setlocale(LC_CTYPE, "");
  1199. tnew(80, 24);
  1200. ttynew();
  1201. xinit();
  1202. run();
  1203. return 0;
  1204. }