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.

1101 lines
23 KiB

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