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.

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