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.

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