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.

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