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.

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