My build of suckless st terminal
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1108 lines
22 KiB

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