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.

1105 lines
22 KiB

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