Simple Terminal from SuckLess
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.

1126 lines
23 KiB

15 years ago
  1. /* See LICENSE for licence details. */
  2. #define _XOPEN_SOURCE 600
  3. #include <ctype.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <limits.h>
  7. #include <locale.h>
  8. #include <stdarg.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <signal.h>
  13. #include <sys/ioctl.h>
  14. #include <sys/select.h>
  15. #include <sys/stat.h>
  16. #include <sys/types.h>
  17. #include <sys/wait.h>
  18. #include <unistd.h>
  19. #include <X11/Xlib.h>
  20. #include <X11/keysym.h>
  21. #include <X11/Xutil.h>
  22. #define TNAME "st"
  23. /* Arbitrary sizes */
  24. #define ESCSIZ 256
  25. #define ESCARG 16
  26. #define MAXDRAWBUF 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. /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
  36. enum { ATnone=0 , ATreverse=1 , ATunderline=2, ATbold=4 };
  37. enum { CSup, CSdown, CSright, CSleft, CShide, CSdraw, CSwrap, CSsave, CSload };
  38. enum { CRset=1, CRupdate=2 };
  39. enum { TMwrap=1, TMinsert=2 };
  40. enum { SCupdate, SCredraw };
  41. typedef int Color;
  42. typedef struct {
  43. char c; /* character code */
  44. char mode; /* attribute flags */
  45. Color fg; /* foreground */
  46. Color bg; /* background */
  47. char state; /* state flag */
  48. } Glyph;
  49. typedef Glyph* Line;
  50. typedef struct {
  51. Glyph attr; /* current char attributes */
  52. char hidden;
  53. int x;
  54. int y;
  55. } TCursor;
  56. /* Escape sequence structs */
  57. /* ESC <pre> [[ [<priv>] <arg> [;]] <mode>] */
  58. typedef struct {
  59. char buf[ESCSIZ+1]; /* raw string */
  60. int len; /* raw string length */
  61. char pre;
  62. char priv;
  63. int arg[ESCARG+1];
  64. int narg; /* nb of args */
  65. char mode;
  66. } Escseq;
  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. int top; /* top scroll limit */
  74. int bot; /* bottom scroll limit */
  75. int mode; /* terminal mode */
  76. } Term;
  77. /* Purely graphic info */
  78. typedef struct {
  79. Display* dis;
  80. Window win;
  81. int scr;
  82. int w; /* window width */
  83. int h; /* window height */
  84. int ch; /* char height */
  85. int cw; /* char width */
  86. } XWindow;
  87. typedef struct {
  88. KeySym k;
  89. char s[ESCSIZ];
  90. } Key;
  91. #include "config.h"
  92. /* Drawing Context */
  93. typedef struct {
  94. unsigned long col[LEN(colorname)];
  95. XFontStruct* font;
  96. GC gc;
  97. } DC;
  98. static void die(const char *errstr, ...);
  99. static void draw(int);
  100. static void execsh(void);
  101. static void sigchld(int);
  102. static void run(void);
  103. static int escaddc(char);
  104. static int escfinal(char);
  105. static void escdump(void);
  106. static void eschandle(void);
  107. static void escparse(void);
  108. static void escreset(void);
  109. static void tclearregion(int, int, int, int);
  110. static void tcpos(int);
  111. static void tcursor(int);
  112. static void tdeletechar(int);
  113. static void tdeleteline(int);
  114. static void tinsertblank(int);
  115. static void tinsertblankline(int);
  116. static void tmoveto(int, int);
  117. static void tnew(int, int);
  118. static void tnewline(void);
  119. static void tputc(char);
  120. static void tputs(char*, int);
  121. static void tresize(int, int);
  122. static void tscroll(void);
  123. static void tsetattr(int*, int);
  124. static void tsetchar(char);
  125. static void tsetscroll(int, int);
  126. static void ttynew(void);
  127. static void ttyread(void);
  128. static void ttyresize(int, int);
  129. static void ttywrite(const char *, size_t);
  130. static unsigned long xgetcol(const char *);
  131. static void xclear(int, int, int, int);
  132. static void xcursor(int);
  133. static void xdrawc(int, int, Glyph);
  134. static void xinit(void);
  135. static void xscroll(void);
  136. static void expose(XEvent *);
  137. static char * kmap(KeySym);
  138. static void kpress(XEvent *);
  139. static void resize(XEvent *);
  140. static void (*handler[LASTEvent])(XEvent *) = {
  141. [KeyPress] = kpress,
  142. [Expose] = expose,
  143. [ConfigureNotify] = resize
  144. };
  145. /* Globals */
  146. static DC dc;
  147. static XWindow xw;
  148. static Term term;
  149. static Escseq escseq;
  150. static int cmdfd;
  151. static pid_t pid;
  152. static int running;
  153. #ifdef DEBUG
  154. void
  155. tdump(void) {
  156. int row, col;
  157. Glyph c;
  158. for(row = 0; row < term.row; row++) {
  159. for(col = 0; col < term.col; col++) {
  160. if(col == term.c.x && row == term.c.y)
  161. putchar('#');
  162. else {
  163. c = term.line[row][col];
  164. putchar(c.state & CRset ? c.c : '.');
  165. }
  166. }
  167. putchar('\n');
  168. }
  169. }
  170. #endif
  171. void
  172. die(const char *errstr, ...) {
  173. va_list ap;
  174. va_start(ap, errstr);
  175. vfprintf(stderr, errstr, ap);
  176. va_end(ap);
  177. exit(EXIT_FAILURE);
  178. }
  179. void
  180. execsh(void) {
  181. char *args[3] = {SHELL, "-i", NULL};
  182. putenv("TERM=" TNAME);
  183. execvp(SHELL, args);
  184. }
  185. void
  186. xbell(void) { /* visual bell */
  187. XRectangle r = { 0, 0, xw.w, xw.h };
  188. XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
  189. XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
  190. /* usleep(30000); */
  191. draw(SCredraw);
  192. }
  193. void
  194. sigchld(int a) {
  195. int stat = 0;
  196. if(waitpid(pid, &stat, 0) < 0)
  197. die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
  198. if(WIFEXITED(stat))
  199. exit(WEXITSTATUS(stat));
  200. else
  201. exit(EXIT_FAILURE);
  202. }
  203. void
  204. ttynew(void) {
  205. int m, s;
  206. char *pts;
  207. if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
  208. die("openpt failed: %s\n", SERRNO);
  209. if(grantpt(m) < 0)
  210. die("grandpt failed: %s\n", SERRNO);
  211. if(unlockpt(m) < 0)
  212. die("unlockpt failed: %s\n", SERRNO);
  213. if(!(pts = ptsname(m)))
  214. die("ptsname failed: %s\n", SERRNO);
  215. if((s = open(pts, O_RDWR | O_NOCTTY)) < 0)
  216. die("Couldn't open slave: %s\n", SERRNO);
  217. fcntl(s, F_SETFL, O_NDELAY);
  218. switch(pid = fork()) {
  219. case -1:
  220. die("fork failed\n");
  221. break;
  222. case 0:
  223. setsid(); /* create a new process group */
  224. dup2(s, STDIN_FILENO);
  225. dup2(s, STDOUT_FILENO);
  226. dup2(s, STDERR_FILENO);
  227. if(ioctl(s, TIOCSCTTY, NULL) < 0)
  228. die("ioctl TTIOCSTTY failed: %s\n", SERRNO);
  229. execsh();
  230. break;
  231. default:
  232. close(s);
  233. cmdfd = m;
  234. signal(SIGCHLD, sigchld);
  235. }
  236. }
  237. void
  238. dump(char c) {
  239. static int col;
  240. fprintf(stderr, " %02x %c ", c, isprint(c)?c:'.');
  241. if(++col % 10 == 0)
  242. fprintf(stderr, "\n");
  243. }
  244. void
  245. ttyread(void) {
  246. char buf[BUFSIZ] = {0};
  247. int ret;
  248. switch(ret = read(cmdfd, buf, BUFSIZ)) {
  249. case -1:
  250. die("Couldn't read from shell: %s\n", SERRNO);
  251. break;
  252. default:
  253. tputs(buf, ret);
  254. }
  255. }
  256. void
  257. ttywrite(const char *s, size_t n) {
  258. if(write(cmdfd, s, n) == -1)
  259. die("write error on tty: %s\n", SERRNO);
  260. }
  261. void
  262. ttyresize(int x, int y) {
  263. struct winsize w;
  264. w.ws_row = term.row;
  265. w.ws_col = term.col;
  266. w.ws_xpixel = w.ws_ypixel = 0;
  267. if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  268. fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
  269. }
  270. int
  271. escfinal(char c) {
  272. if(escseq.len == 1)
  273. switch(c) {
  274. case '[':
  275. case ']':
  276. case '(':
  277. return 0;
  278. case '=':
  279. case '>':
  280. default:
  281. return 1;
  282. }
  283. else if(BETWEEN(c, 0x40, 0x7E))
  284. return 1;
  285. return 0;
  286. }
  287. void
  288. tcpos(int mode) {
  289. static int x = 0;
  290. static int y = 0;
  291. if(mode == CSsave)
  292. x = term.c.x, y = term.c.y;
  293. else if(mode == CSload)
  294. tmoveto(x, y);
  295. }
  296. void
  297. tnew(int col, int row) { /* screen size */
  298. term.row = row, term.col = col;
  299. term.top = 0, term.bot = term.row - 1;
  300. /* mode */
  301. term.mode = TMwrap;
  302. /* cursor */
  303. term.c.attr.mode = ATnone;
  304. term.c.attr.fg = DefaultFG;
  305. term.c.attr.bg = DefaultBG;
  306. term.c.x = term.c.y = 0;
  307. term.c.hidden = 0;
  308. /* allocate screen */
  309. term.line = calloc(term.row, sizeof(Line));
  310. for(row = 0 ; row < term.row; row++)
  311. term.line[row] = calloc(term.col, sizeof(Glyph));
  312. }
  313. void
  314. tscroll(void) {
  315. Line temp = term.line[term.top];
  316. int i;
  317. /* X stuff _before_ the line swapping (results in wrong line index) */
  318. xscroll();
  319. for(i = term.top; i < term.bot; i++)
  320. term.line[i] = term.line[i+1];
  321. memset(temp, 0, sizeof(Glyph) * term.col);
  322. term.line[term.bot] = temp;
  323. }
  324. void
  325. tnewline(void) {
  326. int y = term.c.y + 1;
  327. if(y > term.bot)
  328. tscroll(), y = term.bot;
  329. tmoveto(0, y);
  330. }
  331. int
  332. escaddc(char c) {
  333. escseq.buf[escseq.len++] = c;
  334. if(escfinal(c) || escseq.len >= ESCSIZ) {
  335. escparse(), eschandle();
  336. return 0;
  337. }
  338. return 1;
  339. }
  340. void
  341. escparse(void) {
  342. /* int noarg = 1; */
  343. char *p = escseq.buf;
  344. escseq.narg = 0;
  345. switch(escseq.pre = *p++) {
  346. case '[': /* CSI */
  347. if(*p == '?')
  348. escseq.priv = 1, p++;
  349. while(p < escseq.buf+escseq.len) {
  350. while(isdigit(*p)) {
  351. escseq.arg[escseq.narg] *= 10;
  352. escseq.arg[escseq.narg] += *(p++) - '0'/*, noarg = 0 */;
  353. }
  354. if(*p == ';')
  355. escseq.narg++, p++;
  356. else {
  357. escseq.mode = *p;
  358. escseq.narg++;
  359. return;
  360. }
  361. }
  362. break;
  363. case '(':
  364. /* XXX: graphic character set */
  365. break;
  366. }
  367. }
  368. void
  369. tmoveto(int x, int y) {
  370. term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x;
  371. term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y;
  372. }
  373. void
  374. tcursor(int dir) {
  375. int xf = term.c.x, yf = term.c.y;
  376. switch(dir) {
  377. case CSup:
  378. yf--;
  379. break;
  380. case CSdown:
  381. yf++;
  382. break;
  383. case CSleft:
  384. xf--;
  385. if(xf < 0) {
  386. xf = term.col-1, yf--;
  387. if(yf < term.top)
  388. yf = term.top, xf = 0;
  389. }
  390. break;
  391. case CSright:
  392. xf++;
  393. if(xf >= term.col) {
  394. xf = 0, yf++;
  395. if(yf > term.bot)
  396. yf = term.bot, tscroll();
  397. }
  398. break;
  399. }
  400. tmoveto(xf, yf);
  401. }
  402. void
  403. tsetchar(char c) {
  404. term.line[term.c.y][term.c.x] = term.c.attr;
  405. term.line[term.c.y][term.c.x].c = c;
  406. term.line[term.c.y][term.c.x].state |= CRset | CRupdate;
  407. }
  408. void
  409. tclearregion(int x1, int y1, int x2, int y2) {
  410. int x, y;
  411. LIMIT(x1, 0, term.col-1);
  412. LIMIT(x2, 0, term.col-1);
  413. LIMIT(y1, 0, term.row-1);
  414. LIMIT(y2, 0, term.row-1);
  415. /* XXX: could be optimized */
  416. for(x = x1; x <= x2; x++)
  417. for(y = y1; y <= y2; y++)
  418. memset(&term.line[y][x], 0, sizeof(Glyph));
  419. xclear(x1, y1, x2, y2);
  420. }
  421. void
  422. tdeletechar(int n) {
  423. int src = term.c.x + n;
  424. int dst = term.c.x;
  425. int size = term.col - src;
  426. if(src >= term.col) {
  427. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  428. return;
  429. }
  430. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  431. tclearregion(term.col-size, term.c.y, term.col-1, term.c.y);
  432. }
  433. void
  434. tinsertblank(int n) {
  435. int src = term.c.x;
  436. int dst = src + n;
  437. int size = term.col - n - src;
  438. if(dst >= term.col) {
  439. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  440. return;
  441. }
  442. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  443. tclearregion(src, term.c.y, dst, term.c.y);
  444. }
  445. void
  446. tsetlinestate(int n, int state) {
  447. int i;
  448. for(i = 0; i < term.col; i++)
  449. term.line[n][i].state |= state;
  450. }
  451. void
  452. tinsertblankline (int n) {
  453. int i;
  454. Line blank;
  455. int bot = term.bot;
  456. if(term.c.y > term.bot)
  457. bot = term.row - 1;
  458. else if(term.c.y < term.top)
  459. bot = term.top - 1;
  460. if(term.c.y + n >= bot) {
  461. tclearregion(0, term.c.y, term.col-1, bot);
  462. return;
  463. }
  464. for(i = bot; i >= term.c.y+n; i--) {
  465. /* swap deleted line <-> blanked line */
  466. blank = term.line[i];
  467. term.line[i] = term.line[i-n];
  468. term.line[i-n] = blank;
  469. /* blank it */
  470. memset(blank, 0, term.col * sizeof(Glyph));
  471. tsetlinestate(i, CRupdate);
  472. tsetlinestate(i-n, CRupdate);
  473. }
  474. }
  475. void
  476. tdeleteline(int n) {
  477. int i;
  478. Line blank;
  479. int bot = term.bot;
  480. if(term.c.y > term.bot)
  481. bot = term.row - 1;
  482. else if(term.c.y < term.top)
  483. bot = term.top - 1;
  484. if(term.c.y + n >= bot) {
  485. tclearregion(0, term.c.y, term.col-1, bot);
  486. return;
  487. }
  488. for(i = term.c.y; i <= bot-n; i++) {
  489. /* swap deleted line <-> blanked line */
  490. blank = term.line[i];
  491. term.line[i] = term.line[i+n];
  492. term.line[i+n] = blank;
  493. /* blank it */
  494. memset(blank, 0, term.col * sizeof(Glyph));
  495. tsetlinestate(i, CRupdate);
  496. tsetlinestate(i-n, CRupdate);
  497. }
  498. }
  499. void
  500. tsetattr(int *attr, int l) {
  501. int i;
  502. for(i = 0; i < l; i++) {
  503. switch(attr[i]) {
  504. case 0:
  505. memset(&term.c.attr, 0, sizeof(term.c.attr));
  506. term.c.attr.fg = DefaultFG;
  507. term.c.attr.bg = DefaultBG;
  508. break;
  509. case 1:
  510. term.c.attr.mode |= ATbold;
  511. break;
  512. case 4:
  513. term.c.attr.mode |= ATunderline;
  514. break;
  515. case 7:
  516. term.c.attr.mode |= ATreverse;
  517. break;
  518. case 8:
  519. term.c.hidden = CShide;
  520. break;
  521. case 22:
  522. term.c.attr.mode &= ~ATbold;
  523. break;
  524. case 24:
  525. term.c.attr.mode &= ~ATunderline;
  526. break;
  527. case 27:
  528. term.c.attr.mode &= ~ATreverse;
  529. break;
  530. case 39:
  531. term.c.attr.fg = DefaultFG;
  532. break;
  533. case 49:
  534. term.c.attr.fg = DefaultBG;
  535. break;
  536. default:
  537. if(BETWEEN(attr[i], 30, 37))
  538. term.c.attr.fg = attr[i] - 30;
  539. else if(BETWEEN(attr[i], 40, 47))
  540. term.c.attr.bg = attr[i] - 40;
  541. break;
  542. }
  543. }
  544. }
  545. void
  546. tsetscroll(int t, int b) {
  547. int temp;
  548. LIMIT(t, 0, term.row-1);
  549. LIMIT(b, 0, term.row-1);
  550. if(t > b) {
  551. temp = t;
  552. t = b;
  553. b = temp;
  554. }
  555. term.top = t;
  556. term.bot = b;
  557. }
  558. void
  559. eschandle(void) {
  560. switch(escseq.pre) {
  561. default:
  562. goto unknown_seq;
  563. case '[':
  564. switch(escseq.mode) {
  565. default:
  566. unknown_seq:
  567. fprintf(stderr, "erresc: unknown sequence\n");
  568. escdump();
  569. break;
  570. case '@': /* Insert <n> blank char */
  571. DEFAULT(escseq.arg[0], 1);
  572. tinsertblank(escseq.arg[0]);
  573. break;
  574. case 'A': /* Cursor <n> Up */
  575. case 'e':
  576. DEFAULT(escseq.arg[0], 1);
  577. tmoveto(term.c.x, term.c.y-escseq.arg[0]);
  578. break;
  579. case 'B': /* Cursor <n> Down */
  580. DEFAULT(escseq.arg[0], 1);
  581. tmoveto(term.c.x, term.c.y+escseq.arg[0]);
  582. break;
  583. case 'C': /* Cursor <n> Forward */
  584. case 'a':
  585. DEFAULT(escseq.arg[0], 1);
  586. tmoveto(term.c.x+escseq.arg[0], term.c.y);
  587. break;
  588. case 'D': /* Cursor <n> Backward */
  589. DEFAULT(escseq.arg[0], 1);
  590. tmoveto(term.c.x-escseq.arg[0], term.c.y);
  591. break;
  592. case 'E': /* Cursor <n> Down and first col */
  593. DEFAULT(escseq.arg[0], 1);
  594. tmoveto(0, term.c.y+escseq.arg[0]);
  595. break;
  596. case 'F': /* Cursor <n> Up and first col */
  597. DEFAULT(escseq.arg[0], 1);
  598. tmoveto(0, term.c.y-escseq.arg[0]);
  599. break;
  600. case 'G': /* Move to <col> */
  601. case '`':
  602. DEFAULT(escseq.arg[0], 1);
  603. tmoveto(escseq.arg[0]-1, term.c.y);
  604. break;
  605. case 'H': /* Move to <row> <col> */
  606. case 'f':
  607. DEFAULT(escseq.arg[0], 1);
  608. DEFAULT(escseq.arg[1], 1);
  609. tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
  610. break;
  611. case 'J': /* Clear screen */
  612. switch(escseq.arg[0]) {
  613. case 0: /* below */
  614. tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
  615. break;
  616. case 1: /* above */
  617. tclearregion(0, 0, term.c.x, term.c.y);
  618. break;
  619. case 2: /* all */
  620. tclearregion(0, 0, term.col-1, term.row-1);
  621. break;
  622. }
  623. break;
  624. case 'K': /* Clear line */
  625. switch(escseq.arg[0]) {
  626. case 0: /* right */
  627. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  628. break;
  629. case 1: /* left */
  630. tclearregion(0, term.c.y, term.c.x, term.c.y);
  631. break;
  632. case 2: /* all */
  633. tclearregion(0, term.c.y, term.col-1, term.c.y);
  634. break;
  635. }
  636. break;
  637. case 'L': /* Insert <n> blank lines */
  638. DEFAULT(escseq.arg[0], 1);
  639. tinsertblankline(escseq.arg[0]);
  640. break;
  641. case 'l':
  642. if(escseq.priv && escseq.arg[0] == 25)
  643. term.c.hidden = 1;
  644. break;
  645. case 'M': /* Delete <n> lines */
  646. DEFAULT(escseq.arg[0], 1);
  647. tdeleteline(escseq.arg[0]);
  648. break;
  649. case 'P': /* Delete <n> char */
  650. DEFAULT(escseq.arg[0], 1);
  651. tdeletechar(escseq.arg[0]);
  652. break;
  653. case 'd': /* Move to <row> */
  654. DEFAULT(escseq.arg[0], 1);
  655. tmoveto(term.c.x, escseq.arg[0]-1);
  656. break;
  657. case 'h': /* Set terminal mode */
  658. if(escseq.priv && escseq.arg[0] == 25)
  659. term.c.hidden = 0;
  660. break;
  661. case 'm': /* Terminal attribute (color) */
  662. tsetattr(escseq.arg, escseq.narg);
  663. break;
  664. case 'r':
  665. if(escseq.priv)
  666. ;
  667. else {
  668. DEFAULT(escseq.arg[0], 1);
  669. DEFAULT(escseq.arg[1], term.row);
  670. tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
  671. }
  672. break;
  673. case 's': /* Save cursor position */
  674. tcpos(CSsave);
  675. break;
  676. case 'u': /* Load cursor position */
  677. tcpos(CSload);
  678. break;
  679. }
  680. break;
  681. }
  682. }
  683. void
  684. escdump(void) {
  685. int i;
  686. printf("rawbuf : %s\n", escseq.buf);
  687. printf("prechar : %c\n", escseq.pre);
  688. printf("private : %c\n", escseq.priv ? '?' : ' ');
  689. printf("narg : %d\n", escseq.narg);
  690. if(escseq.narg)
  691. for(i = 0; i < escseq.narg; i++)
  692. printf("\targ %d = %d\n", i, escseq.arg[i]);
  693. printf("mode : %c\n", escseq.mode);
  694. }
  695. void
  696. escreset(void) {
  697. memset(&escseq, 0, sizeof(escseq));
  698. }
  699. void
  700. tputtab(void) {
  701. int space = TAB - term.c.x % TAB;
  702. if(term.c.x + space >= term.col)
  703. space--;
  704. for(; space > 0; space--)
  705. tcursor(CSright);
  706. }
  707. void
  708. tputc(char c) {
  709. static int inesc = 0;
  710. #if 0
  711. dump(c);
  712. #endif
  713. /* start of escseq */
  714. if(c == '\033')
  715. escreset(), inesc = 1;
  716. else if(inesc) {
  717. inesc = escaddc(c);
  718. } /* normal char */
  719. else switch(c) {
  720. default:
  721. tsetchar(c);
  722. tcursor(CSright);
  723. break;
  724. case '\t':
  725. tputtab();
  726. break;
  727. case '\b':
  728. tcursor(CSleft);
  729. break;
  730. case '\r':
  731. tmoveto(0, term.c.y);
  732. break;
  733. case '\n':
  734. tnewline();
  735. break;
  736. case '\a':
  737. xbell();
  738. break;
  739. }
  740. }
  741. void
  742. tputs(char *s, int len) {
  743. for(; len > 0; len--)
  744. tputc(*s++);
  745. }
  746. void
  747. tresize(int col, int row) {
  748. int i;
  749. Line *line;
  750. int minrow = MIN(row, term.row);
  751. int mincol = MIN(col, term.col);
  752. if(col < 1 || row < 1)
  753. return;
  754. /* alloc */
  755. line = calloc(row, sizeof(Line));
  756. for(i = 0 ; i < row; i++)
  757. line[i] = calloc(col, sizeof(Glyph));
  758. /* copy */
  759. for(i = 0 ; i < minrow; i++)
  760. memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
  761. /* free */
  762. for(i = 0; i < term.row; i++)
  763. free(term.line[i]);
  764. free(term.line);
  765. LIMIT(term.c.x, 0, col-1);
  766. LIMIT(term.c.y, 0, row-1);
  767. LIMIT(term.top, 0, row-1);
  768. LIMIT(term.bot, 0, row-1);
  769. term.bot = row-1;
  770. term.line = line;
  771. term.col = col, term.row = row;
  772. }
  773. unsigned long
  774. xgetcol(const char *s) {
  775. XColor color;
  776. Colormap cmap = DefaultColormap(xw.dis, xw.scr);
  777. if(!XAllocNamedColor(xw.dis, cmap, s, &color, &color)) {
  778. color.pixel = WhitePixel(xw.dis, xw.scr);
  779. fprintf(stderr, "Could not allocate color '%s'\n", s);
  780. }
  781. return color.pixel;
  782. }
  783. void
  784. xclear(int x1, int y1, int x2, int y2) {
  785. XClearArea(xw.dis, xw.win,
  786. x1 * xw.cw, y1 * xw.ch,
  787. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch,
  788. False);
  789. }
  790. void
  791. xscroll(void) {
  792. int srcy = (term.top+1) * xw.ch;
  793. int dsty = term.top * xw.ch;
  794. int height = (term.bot-term.top) * xw.ch;
  795. xcursor(CShide);
  796. XCopyArea(xw.dis, xw.win, xw.win, dc.gc, 0, srcy, xw.w, height, 0, dsty);
  797. xclear(0, term.bot, term.col-1, term.bot);
  798. }
  799. void
  800. xinit(void) {
  801. XGCValues values;
  802. unsigned long valuemask;
  803. XClassHint chint;
  804. XWMHints wmhint;
  805. XSizeHints shint;
  806. char *args[] = {NULL};
  807. int i;
  808. xw.dis = XOpenDisplay(NULL);
  809. xw.scr = XDefaultScreen(xw.dis);
  810. if(!xw.dis)
  811. die("Can't open display\n");
  812. /* font */
  813. if(!(dc.font = XLoadQueryFont(xw.dis, FONT)))
  814. die("Can't load font %s\n", FONT);
  815. xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
  816. xw.ch = dc.font->ascent + dc.font->descent + LINESPACE;
  817. /* colors */
  818. for(i = 0; i < LEN(colorname); i++)
  819. dc.col[i] = xgetcol(colorname[i]);
  820. term.c.attr.fg = DefaultFG;
  821. term.c.attr.bg = DefaultBG;
  822. term.c.attr.mode = ATnone;
  823. /* windows */
  824. xw.h = term.row * xw.ch;
  825. xw.w = term.col * xw.cw;
  826. /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
  827. xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
  828. xw.w, xw.h, BORDER,
  829. dc.col[DefaultBG],
  830. dc.col[DefaultBG]);
  831. /* gc */
  832. values.foreground = XWhitePixel(xw.dis, xw.scr);
  833. values.font = dc.font->fid;
  834. valuemask = GCForeground | GCFont;
  835. dc.gc = XCreateGC(xw.dis, xw.win, valuemask, &values);
  836. XMapWindow(xw.dis, xw.win);
  837. /* wm stuff */
  838. chint.res_name = TNAME, chint.res_class = TNAME;
  839. wmhint.input = 1, wmhint.flags = InputHint;
  840. shint.height_inc = xw.ch, shint.width_inc = xw.cw;
  841. shint.height = xw.h, shint.width = xw.w;
  842. shint.flags = PSize | PResizeInc;
  843. XSetWMProperties(xw.dis, xw.win, NULL, NULL, &args[0], 0, &shint, &wmhint, &chint);
  844. XStoreName(xw.dis, xw.win, TNAME);
  845. XSync(xw.dis, 0);
  846. }
  847. void
  848. xdraws (char *s, Glyph base, int x, int y, int len) {
  849. unsigned long xfg, xbg;
  850. int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
  851. if(base.mode & ATreverse)
  852. xfg = dc.col[base.bg], xbg = dc.col[base.fg];
  853. else
  854. xfg = dc.col[base.fg], xbg = dc.col[base.bg];
  855. XSetBackground(xw.dis, dc.gc, xbg);
  856. XSetForeground(xw.dis, dc.gc, xfg);
  857. XDrawImageString(xw.dis, xw.win, dc.gc, winx, winy, s, len);
  858. if(base.mode & ATunderline)
  859. XDrawLine(xw.dis, xw.win, dc.gc, winx, winy+1, winx+width-1, winy+1);
  860. }
  861. void
  862. xdrawc(int x, int y, Glyph g) {
  863. XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
  864. unsigned long xfg, xbg;
  865. /* reverse video */
  866. if(g.mode & ATreverse)
  867. xfg = dc.col[g.bg], xbg = dc.col[g.fg];
  868. else
  869. xfg = dc.col[g.fg], xbg = dc.col[g.bg];
  870. /* background */
  871. XSetBackground(xw.dis, dc.gc, xbg);
  872. XSetForeground(xw.dis, dc.gc, xfg);
  873. XDrawImageString(xw.dis, xw.win, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
  874. }
  875. void
  876. xcursor(int mode) {
  877. static int oldx = 0;
  878. static int oldy = 0;
  879. Glyph g = {' ', ATnone, DefaultBG, DefaultCS, 0};
  880. LIMIT(oldx, 0, term.col-1);
  881. LIMIT(oldy, 0, term.row-1);
  882. if(term.line[term.c.y][term.c.x].state & CRset)
  883. g.c = term.line[term.c.y][term.c.x].c;
  884. /* remove the old cursor */
  885. if(term.line[oldy][oldx].state & CRset)
  886. xdrawc(oldx, oldy, term.line[oldy][oldx]);
  887. else
  888. xclear(oldx, oldy, oldx, oldy);
  889. /* draw the new one */
  890. if(mode == CSdraw) {
  891. xdrawc(term.c.x, term.c.y, g);
  892. oldx = term.c.x, oldy = term.c.y;
  893. }
  894. }
  895. void
  896. draw(int redraw_all) {
  897. int i, x, y, ox;
  898. Glyph base, new;
  899. char buf[MAXDRAWBUF];
  900. for(y = 0; y < term.row; y++) {
  901. base = term.line[y][0];
  902. i = ox = 0;
  903. for(x = 0; x < term.col; x++) {
  904. new = term.line[y][x];
  905. if(!ATTRCMP(base, new) && i < MAXDRAWBUF)
  906. buf[i++] = new.c;
  907. else {
  908. xdraws(buf, base, ox, y, i);
  909. buf[0] = new.c;
  910. i = 1;
  911. ox = x;
  912. base = new;
  913. }
  914. }
  915. xdraws(buf, base, ox, y, i);
  916. }
  917. xcursor(CSdraw);
  918. }
  919. void
  920. expose(XEvent *ev) {
  921. draw(SCredraw);
  922. }
  923. char *
  924. kmap(KeySym k) {
  925. int i;
  926. for(i = 0; i < LEN(key); i++)
  927. if(key[i].k == k)
  928. return (char*)key[i].s;
  929. return NULL;
  930. }
  931. void
  932. kpress(XEvent *ev) {
  933. XKeyEvent *e = &ev->xkey;
  934. KeySym ksym;
  935. char buf[32];
  936. char *customkey;
  937. int len;
  938. int meta;
  939. int shift;
  940. meta = e->state & Mod1Mask;
  941. shift = e->state & ShiftMask;
  942. len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
  943. if(customkey = kmap(ksym))
  944. ttywrite(customkey, strlen(customkey));
  945. else if(len > 0) {
  946. buf[sizeof(buf)-1] = '\0';
  947. if(meta && len == 1)
  948. ttywrite("\033", 1);
  949. ttywrite(buf, len);
  950. } else
  951. switch(ksym) {
  952. case XK_Insert:
  953. if(shift)
  954. /* XXX: paste X clipboard */;
  955. break;
  956. default:
  957. fprintf(stderr, "errkey: %d\n", (int)ksym);
  958. break;
  959. }
  960. }
  961. void
  962. resize(XEvent *e) {
  963. int col, row;
  964. col = e->xconfigure.width / xw.cw;
  965. row = e->xconfigure.height / xw.ch;
  966. if(term.col != col || term.row != row) {
  967. tresize(col, row);
  968. ttyresize(col, row);
  969. xw.w = e->xconfigure.width;
  970. xw.h = e->xconfigure.height;
  971. draw(SCredraw);
  972. }
  973. }
  974. void
  975. run(void) {
  976. XEvent ev;
  977. fd_set rfd;
  978. int xfd = XConnectionNumber(xw.dis);
  979. running = 1;
  980. XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
  981. XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* fix resize bug in wmii (?) */
  982. while(running) {
  983. FD_ZERO(&rfd);
  984. FD_SET(cmdfd, &rfd);
  985. FD_SET(xfd, &rfd);
  986. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) == -1) {
  987. if(errno == EINTR)
  988. continue;
  989. die("select failed: %s\n", SERRNO);
  990. }
  991. if(FD_ISSET(cmdfd, &rfd)) {
  992. ttyread();
  993. draw(SCupdate);
  994. }
  995. while(XPending(xw.dis)) {
  996. XNextEvent(xw.dis, &ev);
  997. if(handler[ev.type])
  998. (handler[ev.type])(&ev);
  999. }
  1000. }
  1001. }
  1002. int
  1003. main(int argc, char *argv[]) {
  1004. if(argc == 2 && !strncmp("-v", argv[1], 3))
  1005. die("st-" VERSION ", © 2009 st engineers\n");
  1006. else if(argc != 1)
  1007. die("usage: st [-v]\n");
  1008. setlocale(LC_CTYPE, "");
  1009. tnew(80, 24);
  1010. ttynew();
  1011. xinit();
  1012. run();
  1013. return 0;
  1014. }