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.

1115 lines
22 KiB

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