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.

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