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.

1104 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, TMaltcharset };
  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. for(i = term.top; i < term.bot; i++)
  292. term.line[i] = term.line[i+1];
  293. memset(temp, 0, sizeof(Glyph) * term.col);
  294. term.line[term.bot] = temp;
  295. xscroll();
  296. }
  297. void
  298. tnewline(void) {
  299. int y = term.c.y + 1;
  300. if(y > term.bot) {
  301. tscroll(), y = term.bot;
  302. }
  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. tinsertblankline (int n) {
  421. int i;
  422. Line blank;
  423. int bot = term.bot;
  424. if(term.c.y > term.bot)
  425. bot = term.row - 1;
  426. else if(term.c.y < term.top)
  427. bot = term.top - 1;
  428. if(term.c.y + n >= bot) {
  429. tclearregion(0, term.c.y, term.col-1, bot);
  430. return;
  431. }
  432. for(i = bot; i >= term.c.y+n; i--) {
  433. /* swap deleted line <-> blanked line */
  434. blank = term.line[i];
  435. term.line[i] = term.line[i-n];
  436. term.line[i-n] = blank;
  437. /* blank it */
  438. memset(blank, 0, term.col * sizeof(Glyph));
  439. }
  440. }
  441. void
  442. tdeleteline(int n) {
  443. int i;
  444. Line blank;
  445. int bot = term.bot;
  446. if(term.c.y > term.bot)
  447. bot = term.row - 1;
  448. else if(term.c.y < term.top)
  449. bot = term.top - 1;
  450. if(term.c.y + n >= bot) {
  451. tclearregion(0, term.c.y, term.col-1, bot);
  452. return;
  453. }
  454. for(i = term.c.y; i <= bot-n; i++) {
  455. /* swap deleted line <-> blanked line */
  456. blank = term.line[i];
  457. term.line[i] = term.line[i+n];
  458. term.line[i+n] = blank;
  459. /* blank it */
  460. memset(blank, 0, term.col * sizeof(Glyph));
  461. }
  462. }
  463. void
  464. tsetattr(int *attr, int l) {
  465. int i;
  466. for(i = 0; i < l; i++) {
  467. switch(attr[i]) {
  468. case 0:
  469. memset(&term.c.attr, 0, sizeof(term.c.attr));
  470. term.c.attr.fg = DefaultFG;
  471. term.c.attr.bg = DefaultBG;
  472. break;
  473. case 1:
  474. term.c.attr.mode |= ATbold;
  475. break;
  476. case 4:
  477. term.c.attr.mode |= ATunderline;
  478. break;
  479. case 7:
  480. term.c.attr.mode |= ATreverse;
  481. break;
  482. case 8:
  483. term.c.hidden = CShide;
  484. break;
  485. case 22:
  486. term.c.attr.mode &= ~ATbold;
  487. break;
  488. case 24:
  489. term.c.attr.mode &= ~ATunderline;
  490. break;
  491. case 27:
  492. term.c.attr.mode &= ~ATreverse;
  493. break;
  494. case 39:
  495. term.c.attr.fg = DefaultFG;
  496. break;
  497. case 49:
  498. term.c.attr.fg = DefaultBG;
  499. break;
  500. default:
  501. if(BETWEEN(attr[i], 30, 37))
  502. term.c.attr.fg = attr[i] - 30;
  503. else if(BETWEEN(attr[i], 40, 47))
  504. term.c.attr.bg = attr[i] - 40;
  505. break;
  506. }
  507. }
  508. }
  509. void
  510. tsetscroll(int t, int b) {
  511. int temp;
  512. LIMIT(t, 0, term.row-1);
  513. LIMIT(b, 0, term.row-1);
  514. if(t > b) {
  515. temp = t;
  516. t = b;
  517. b = temp;
  518. }
  519. term.top = t;
  520. term.bot = b;
  521. }
  522. void
  523. eschandle(void) {
  524. /* escdump(); */
  525. switch(escseq.pre) {
  526. case '[':
  527. switch(escseq.mode) {
  528. case '@': /* Insert <n> blank char */
  529. DEFAULT(escseq.arg[0], 1);
  530. tinsertblank(escseq.arg[0]);
  531. break;
  532. case 'A': /* Cursor <n> Up */
  533. case 'e':
  534. DEFAULT(escseq.arg[0], 1);
  535. tmoveto(term.c.x, term.c.y-escseq.arg[0]);
  536. break;
  537. case 'B': /* Cursor <n> Down */
  538. DEFAULT(escseq.arg[0], 1);
  539. tmoveto(term.c.x, term.c.y+escseq.arg[0]);
  540. break;
  541. case 'C': /* Cursor <n> Forward */
  542. case 'a':
  543. DEFAULT(escseq.arg[0], 1);
  544. tmoveto(term.c.x+escseq.arg[0], term.c.y);
  545. break;
  546. case 'D': /* Cursor <n> Backward */
  547. DEFAULT(escseq.arg[0], 1);
  548. tmoveto(term.c.x-escseq.arg[0], term.c.y);
  549. break;
  550. case 'E': /* Cursor <n> Down and first col */
  551. DEFAULT(escseq.arg[0], 1);
  552. tmoveto(0, term.c.y+escseq.arg[0]);
  553. break;
  554. case 'F': /* Cursor <n> Up and first col */
  555. DEFAULT(escseq.arg[0], 1);
  556. tmoveto(0, term.c.y-escseq.arg[0]);
  557. break;
  558. case 'G': /* Move to <col> */
  559. case '`':
  560. DEFAULT(escseq.arg[0], 1);
  561. tmoveto(escseq.arg[0]-1, term.c.y);
  562. break;
  563. case 'H': /* Move to <row> <col> */
  564. case 'f':
  565. DEFAULT(escseq.arg[0], 1);
  566. DEFAULT(escseq.arg[1], 1);
  567. tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
  568. break;
  569. case 'J': /* Clear screen */
  570. switch(escseq.arg[0]) {
  571. case 0: /* below */
  572. tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
  573. break;
  574. case 1: /* above */
  575. tclearregion(0, 0, term.c.x, term.c.y);
  576. break;
  577. case 2: /* all */
  578. tclearregion(0, 0, term.col-1, term.row-1);
  579. break;
  580. }
  581. break;
  582. case 'K': /* Clear line */
  583. switch(escseq.arg[0]) {
  584. case 0: /* right */
  585. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  586. break;
  587. case 1: /* left */
  588. tclearregion(0, term.c.y, term.c.x, term.c.y);
  589. break;
  590. case 2: /* all */
  591. tclearregion(0, term.c.y, term.col-1, term.c.y);
  592. break;
  593. }
  594. break;
  595. case 'L': /* Insert <n> blank lines */
  596. DEFAULT(escseq.arg[0], 1);
  597. tinsertblankline(escseq.arg[0]);
  598. break;
  599. case 'l':
  600. if(escseq.priv && escseq.arg[0] == 25)
  601. term.c.hidden = 1;
  602. break;
  603. case 'M': /* Delete <n> lines */
  604. DEFAULT(escseq.arg[0], 1);
  605. tdeleteline(escseq.arg[0]);
  606. break;
  607. case 'P': /* Delete <n> char */
  608. DEFAULT(escseq.arg[0], 1);
  609. tdeletechar(escseq.arg[0]);
  610. break;
  611. case 'd': /* Move to <row> */
  612. DEFAULT(escseq.arg[0], 1);
  613. tmoveto(term.c.x, escseq.arg[0]-1);
  614. break;
  615. case 'h': /* Set terminal mode */
  616. if(escseq.priv && escseq.arg[0] == 25)
  617. term.c.hidden = 0;
  618. break;
  619. case 'm': /* Terminal attribute (color) */
  620. tsetattr(escseq.arg, escseq.narg);
  621. break;
  622. case 'r':
  623. if(escseq.priv)
  624. ;
  625. else {
  626. DEFAULT(escseq.arg[0], 1);
  627. DEFAULT(escseq.arg[1], term.row);
  628. tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
  629. }
  630. break;
  631. case 's': /* Save cursor position */
  632. tcpos(CSsave);
  633. break;
  634. case 'u': /* Load cursor position */
  635. tcpos(CSload);
  636. break;
  637. }
  638. break;
  639. }
  640. }
  641. void
  642. escdump(void) {
  643. int i;
  644. puts("------");
  645. printf("rawbuf : %s\n", escseq.buf);
  646. printf("prechar : %c\n", escseq.pre);
  647. printf("private : %c\n", escseq.priv ? '?' : ' ');
  648. printf("narg : %d\n", escseq.narg);
  649. if(escseq.narg) {
  650. for(i = 0; i < escseq.narg; i++)
  651. printf("\targ %d = %d\n", i, escseq.arg[i]);
  652. }
  653. printf("mode : %c\n", escseq.mode);
  654. }
  655. void
  656. escreset(void) {
  657. memset(&escseq, 0, sizeof(escseq));
  658. }
  659. void
  660. tputtab(void) {
  661. int space = TAB - term.c.x % TAB;
  662. if(term.c.x + space >= term.col)
  663. space--;
  664. for(; space > 0; space--)
  665. tcursor(CSright);
  666. }
  667. void
  668. tputc(char c) {
  669. static int inesc = 0;
  670. #if 0
  671. dump(c);
  672. #endif
  673. /* start of escseq */
  674. if(c == '\033')
  675. escreset(), inesc = 1;
  676. else if(inesc) {
  677. inesc = escaddc(c);
  678. } /* normal char */
  679. else switch(c) {
  680. default:
  681. tsetchar(c);
  682. tcursor(CSright);
  683. break;
  684. case '\t':
  685. tputtab();
  686. break;
  687. case '\b':
  688. tcursor(CSleft);
  689. break;
  690. case '\r':
  691. tmoveto(0, term.c.y);
  692. break;
  693. case '\n':
  694. tnewline();
  695. break;
  696. case '\a':
  697. xbell();
  698. break;
  699. }
  700. }
  701. void
  702. tputs(char *s, int len) {
  703. for(; len > 0; len--)
  704. tputc(*s++);
  705. }
  706. void
  707. tdump(void) {
  708. int row, col;
  709. Glyph c;
  710. for(row = 0; row < term.row; row++) {
  711. for(col = 0; col < term.col; col++) {
  712. if(col == term.c.x && row == term.c.y)
  713. putchar('#');
  714. else {
  715. c = term.line[row][col];
  716. putchar(c.state & CRset ? c.c : '.');
  717. }
  718. }
  719. putchar('\n');
  720. }
  721. }
  722. void
  723. tresize(int col, int row) {
  724. int i;
  725. Line *line;
  726. int minrow = MIN(row, term.row);
  727. int mincol = MIN(col, term.col);
  728. if(col < 1 || row < 1)
  729. return;
  730. /* alloc */
  731. line = calloc(row, sizeof(Line));
  732. for(i = 0 ; i < row; i++)
  733. line[i] = calloc(col, sizeof(Glyph));
  734. /* copy */
  735. for(i = 0 ; i < minrow; i++)
  736. memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
  737. /* free */
  738. for(i = 0; i < term.row; i++)
  739. free(term.line[i]);
  740. free(term.line);
  741. LIMIT(term.c.x, 0, col-1);
  742. LIMIT(term.c.y, 0, row-1);
  743. LIMIT(term.top, 0, row-1);
  744. LIMIT(term.bot, 0, row-1);
  745. term.bot = row-1;
  746. term.line = line;
  747. term.col = col, term.row = row;
  748. }
  749. unsigned long
  750. xgetcol(const char *s) {
  751. XColor color;
  752. Colormap cmap = DefaultColormap(xw.dis, xw.scr);
  753. if(!XAllocNamedColor(xw.dis, cmap, s, &color, &color)) {
  754. color.pixel = WhitePixel(xw.dis, xw.scr);
  755. fprintf(stderr, "Could not allocate color '%s'\n", s);
  756. }
  757. return color.pixel;
  758. }
  759. void
  760. xclear(int x1, int y1, int x2, int y2) {
  761. XClearArea(xw.dis, xw.win,
  762. x1 * xw.cw, y1 * xw.ch,
  763. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch,
  764. False);
  765. }
  766. void
  767. xscroll(void) {
  768. int srcy = (term.top+1) * xw.ch;
  769. int dsty = term.top * xw.ch;
  770. int height = (term.bot-term.top) * xw.ch;
  771. xcursor(CShide);
  772. XCopyArea(xw.dis, xw.win, xw.win, dc.gc, 0, srcy, xw.w, height, 0, dsty);
  773. xclear(0, term.bot, term.col-1, term.bot);
  774. }
  775. void
  776. xinit(void) {
  777. XGCValues values;
  778. unsigned long valuemask;
  779. XClassHint chint;
  780. XWMHints wmhint;
  781. XSizeHints shint;
  782. char *args[] = {NULL};
  783. int i;
  784. xw.dis = XOpenDisplay(NULL);
  785. xw.scr = XDefaultScreen(xw.dis);
  786. if(!xw.dis)
  787. die("Can't open display\n");
  788. /* font */
  789. if(!(dc.font = XLoadQueryFont(xw.dis, FONT)))
  790. die("Can't load font %s\n", FONT);
  791. xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
  792. xw.ch = dc.font->ascent + dc.font->descent + LINESPACE;
  793. /* colors */
  794. for(i = 0; i < LEN(colorname); i++)
  795. dc.col[i] = xgetcol(colorname[i]);
  796. term.c.attr.fg = DefaultFG;
  797. term.c.attr.bg = DefaultBG;
  798. term.c.attr.mode = ATnone;
  799. /* windows */
  800. xw.h = term.row * xw.ch;
  801. xw.w = term.col * xw.cw;
  802. /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
  803. xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
  804. xw.w, xw.h, BORDER,
  805. dc.col[DefaultBG],
  806. dc.col[DefaultBG]);
  807. /* gc */
  808. values.foreground = XWhitePixel(xw.dis, xw.scr);
  809. values.font = dc.font->fid;
  810. valuemask = GCForeground | GCFont;
  811. dc.gc = XCreateGC(xw.dis, xw.win, valuemask, &values);
  812. XMapWindow(xw.dis, xw.win);
  813. /* wm stuff */
  814. chint.res_name = TNAME, chint.res_class = TNAME;
  815. wmhint.input = 1, wmhint.flags = InputHint;
  816. shint.height_inc = xw.ch, shint.width_inc = xw.cw;
  817. shint.height = xw.h, shint.width = xw.w;
  818. shint.flags = PSize | PResizeInc;
  819. XSetWMProperties(xw.dis, xw.win, NULL, NULL, &args[0], 0, &shint, &wmhint, &chint);
  820. XStoreName(xw.dis, xw.win, TNAME);
  821. XSync(xw.dis, 0);
  822. }
  823. void
  824. xdrawc(int x, int y, Glyph g) {
  825. XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
  826. unsigned long xfg, xbg;
  827. /* reverse video */
  828. if(g.mode & ATreverse)
  829. xfg = dc.col[g.bg], xbg = dc.col[g.fg];
  830. else
  831. xfg = dc.col[g.fg], xbg = dc.col[g.bg];
  832. /* background */
  833. XSetForeground(xw.dis, dc.gc, xbg);
  834. XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
  835. /* string */
  836. XSetForeground(xw.dis, dc.gc, xfg);
  837. XDrawString(xw.dis, xw.win, dc.gc, r.x, r.y+dc.font->ascent, &(g.c), 1);
  838. if(g.mode & ATbold) /* XXX: bold hack (draw again at x+1) */
  839. XDrawString(xw.dis, xw.win, dc.gc, r.x+1, r.y+dc.font->ascent, &(g.c), 1);
  840. /* underline */
  841. if(g.mode & ATunderline) {
  842. r.y += dc.font->ascent + 1;
  843. XDrawLine(xw.dis, xw.win, dc.gc, r.x, r.y, r.x+r.width-1, r.y);
  844. }
  845. }
  846. void
  847. xcursor(int mode) {
  848. static int oldx = 0;
  849. static int oldy = 0;
  850. Glyph g = {' ', ATnone, DefaultBG, DefaultCS, 0};
  851. LIMIT(oldx, 0, term.col-1);
  852. LIMIT(oldy, 0, term.row-1);
  853. if(term.line[term.c.y][term.c.x].state & CRset)
  854. g.c = term.line[term.c.y][term.c.x].c;
  855. /* remove the old cursor */
  856. if(term.line[oldy][oldx].state & CRset)
  857. xdrawc(oldx, oldy, term.line[oldy][oldx]);
  858. else xclear(oldx, oldy, oldx, oldy); /* XXX: maybe a bug */
  859. if(mode == CSdraw && !term.c.hidden) {
  860. xdrawc(term.c.x, term.c.y, g);
  861. oldx = term.c.x, oldy = term.c.y;
  862. }
  863. }
  864. void
  865. draw(int redraw_all) {
  866. int x, y;
  867. int changed, set;
  868. if(redraw_all)
  869. XClearWindow(xw.dis, xw.win);
  870. /* XXX: drawing could be optimised */
  871. for(y = 0; y < term.row; y++) {
  872. for(x = 0; x < term.col; x++) {
  873. changed = term.line[y][x].state & CRupdate;
  874. set = term.line[y][x].state & CRset;
  875. if((changed && set) || (redraw_all && set)) {
  876. term.line[y][x].state &= ~CRupdate;
  877. xdrawc(x, y, term.line[y][x]);
  878. }
  879. }
  880. }
  881. xcursor(CSdraw);
  882. }
  883. char*
  884. kmap(KeySym k) {
  885. int i;
  886. for(i = 0; i < LEN(key); i++)
  887. if(key[i].k == k)
  888. return (char*)key[i].s;
  889. return NULL;
  890. }
  891. void
  892. kpress(XKeyEvent *e) {
  893. KeySym ksym;
  894. char buf[32];
  895. int len;
  896. int meta;
  897. int shift;
  898. char* skmap;
  899. meta = e->state & Mod1Mask;
  900. shift = e->state & ShiftMask;
  901. len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
  902. if(skmap = kmap(ksym))
  903. ttywrite(skmap, strlen(skmap));
  904. else if(len > 0) {
  905. buf[sizeof(buf)-1] = '\0';
  906. if(meta && len == 1)
  907. ttywrite("\033", 1);
  908. ttywrite(buf, len);
  909. } else
  910. switch(ksym) {
  911. case XK_Insert:
  912. if(shift)
  913. /* XXX: paste X clipboard */;
  914. break;
  915. default:
  916. fprintf(stderr, "errkey: %d\n", (int)ksym);
  917. break;
  918. }
  919. }
  920. void
  921. resize(XEvent *e) {
  922. int col, row;
  923. col = e->xconfigure.width / xw.cw;
  924. row = e->xconfigure.height / xw.ch;
  925. if(term.col != col || term.row != row) {
  926. tresize(col, row);
  927. ttyresize(col, row);
  928. xw.w = e->xconfigure.width;
  929. xw.h = e->xconfigure.height;
  930. draw(SCredraw);
  931. }
  932. }
  933. void
  934. run(void) {
  935. int ret;
  936. XEvent ev;
  937. fd_set rfd;
  938. int xfd = XConnectionNumber(xw.dis);
  939. running = 1;
  940. XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
  941. XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* fix resize bug in wmii (?) */
  942. while(running) {
  943. FD_ZERO(&rfd);
  944. FD_SET(cmdfd, &rfd);
  945. FD_SET(xfd, &rfd);
  946. XFlush(xw.dis);
  947. ret = select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL);
  948. if(ret < 0)
  949. die("select failed: %s\n", SERRNO);
  950. if(FD_ISSET(xfd, &rfd)) {
  951. while(XPending(xw.dis)) {
  952. XNextEvent(xw.dis, &ev);
  953. switch (ev.type) {
  954. default:
  955. break;
  956. case KeyPress:
  957. kpress(&ev.xkey);
  958. break;
  959. case Expose:
  960. draw(SCredraw);
  961. break;
  962. case ConfigureNotify:
  963. resize(&ev);
  964. break;
  965. }
  966. }
  967. }
  968. if(FD_ISSET(cmdfd, &rfd)) {
  969. ttyread();
  970. draw(SCupdate);
  971. }
  972. }
  973. }
  974. int
  975. main(int argc, char *argv[]) {
  976. if(argc == 2 && !strncmp("-v", argv[1], 3))
  977. die("st-" VERSION ", © 2009 st engineers\n");
  978. else if(argc != 1)
  979. die("usage: st [-v]\n");
  980. setlocale(LC_CTYPE, "");
  981. tnew(80, 24);
  982. ttynew();
  983. xinit();
  984. run();
  985. return 0;
  986. }