Simple Terminal from SuckLess
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.

1097 lines
23 KiB

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