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.

1117 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 <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 };
  38. enum { CSup, CSdown, CSright, CSleft, CShide, CSdraw, CSwrap, CSsave, CSload };
  39. enum { CRset=1, CRupdate=2 };
  40. enum { TMwrap=1, TMinsert=2, TMtitle=4 };
  41. enum { ESCin = 1, ESCcsi = 2, ESCosc = 4, ESCtitle = 8 };
  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(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(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. memset(&term.c.attr, 0, sizeof(term.c.attr));
  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. switch(escseq.mode) {
  530. default:
  531. fprintf(stderr, "erresc: unknown sequence\n");
  532. csidump();
  533. /* die(""); */
  534. break;
  535. case '@': /* Insert <n> blank char */
  536. DEFAULT(escseq.arg[0], 1);
  537. tinsertblank(escseq.arg[0]);
  538. break;
  539. case 'A': /* Cursor <n> Up */
  540. case 'e':
  541. DEFAULT(escseq.arg[0], 1);
  542. tmoveto(term.c.x, term.c.y-escseq.arg[0]);
  543. break;
  544. case 'B': /* Cursor <n> Down */
  545. DEFAULT(escseq.arg[0], 1);
  546. tmoveto(term.c.x, term.c.y+escseq.arg[0]);
  547. break;
  548. case 'C': /* Cursor <n> Forward */
  549. case 'a':
  550. DEFAULT(escseq.arg[0], 1);
  551. tmoveto(term.c.x+escseq.arg[0], term.c.y);
  552. break;
  553. case 'D': /* Cursor <n> Backward */
  554. DEFAULT(escseq.arg[0], 1);
  555. tmoveto(term.c.x-escseq.arg[0], term.c.y);
  556. break;
  557. case 'E': /* Cursor <n> Down and first col */
  558. DEFAULT(escseq.arg[0], 1);
  559. tmoveto(0, term.c.y+escseq.arg[0]);
  560. break;
  561. case 'F': /* Cursor <n> Up and first col */
  562. DEFAULT(escseq.arg[0], 1);
  563. tmoveto(0, term.c.y-escseq.arg[0]);
  564. break;
  565. case 'G': /* Move to <col> */
  566. case '`':
  567. DEFAULT(escseq.arg[0], 1);
  568. tmoveto(escseq.arg[0]-1, term.c.y);
  569. break;
  570. case 'H': /* Move to <row> <col> */
  571. case 'f':
  572. DEFAULT(escseq.arg[0], 1);
  573. DEFAULT(escseq.arg[1], 1);
  574. tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
  575. break;
  576. case 'J': /* Clear screen */
  577. switch(escseq.arg[0]) {
  578. case 0: /* below */
  579. tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
  580. break;
  581. case 1: /* above */
  582. tclearregion(0, 0, term.c.x, term.c.y);
  583. break;
  584. case 2: /* all */
  585. tclearregion(0, 0, term.col-1, term.row-1);
  586. break;
  587. }
  588. break;
  589. case 'K': /* Clear line */
  590. switch(escseq.arg[0]) {
  591. case 0: /* right */
  592. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  593. break;
  594. case 1: /* left */
  595. tclearregion(0, term.c.y, term.c.x, term.c.y);
  596. break;
  597. case 2: /* all */
  598. tclearregion(0, term.c.y, term.col-1, term.c.y);
  599. break;
  600. }
  601. break;
  602. case 'S':
  603. case 'L': /* Insert <n> blank lines */
  604. DEFAULT(escseq.arg[0], 1);
  605. tinsertblankline(escseq.arg[0]);
  606. break;
  607. case 'l':
  608. if(escseq.priv && escseq.arg[0] == 25)
  609. term.c.hidden = 1;
  610. break;
  611. case 'M': /* Delete <n> lines */
  612. DEFAULT(escseq.arg[0], 1);
  613. tdeleteline(escseq.arg[0]);
  614. break;
  615. case 'X':
  616. case 'P': /* Delete <n> char */
  617. DEFAULT(escseq.arg[0], 1);
  618. tdeletechar(escseq.arg[0]);
  619. break;
  620. case 'd': /* Move to <row> */
  621. DEFAULT(escseq.arg[0], 1);
  622. tmoveto(term.c.x, escseq.arg[0]-1);
  623. break;
  624. case 'h': /* Set terminal mode */
  625. if(escseq.priv && escseq.arg[0] == 25)
  626. term.c.hidden = 0;
  627. break;
  628. case 'm': /* Terminal attribute (color) */
  629. tsetattr(escseq.arg, escseq.narg);
  630. break;
  631. case 'r':
  632. if(escseq.priv)
  633. ;
  634. else {
  635. DEFAULT(escseq.arg[0], 1);
  636. DEFAULT(escseq.arg[1], term.row);
  637. tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
  638. }
  639. break;
  640. case 's': /* Save cursor position */
  641. tcpos(CSsave);
  642. break;
  643. case 'u': /* Load cursor position */
  644. tcpos(CSload);
  645. break;
  646. }
  647. }
  648. void
  649. csidump(void) {
  650. int i;
  651. printf("ESC [ %s", escseq.priv ? "? " : "");
  652. if(escseq.narg)
  653. for(i = 0; i < escseq.narg; i++)
  654. printf("%d ", escseq.arg[i]);
  655. if(escseq.mode)
  656. putchar(escseq.mode);
  657. putchar('\n');
  658. }
  659. void
  660. csireset(void) {
  661. memset(&escseq, 0, sizeof(escseq));
  662. }
  663. void
  664. tputtab(void) {
  665. int space = TAB - term.c.x % TAB;
  666. if(term.c.x + space >= term.col)
  667. space--;
  668. for(; space > 0; space--)
  669. tcursor(CSright);
  670. }
  671. void
  672. tputc(char c) {
  673. #if 0
  674. dump(c);
  675. #endif
  676. if(term.esc & ESCin) {
  677. if(term.esc & ESCcsi) {
  678. escseq.buf[escseq.len++] = c;
  679. if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESCSIZ) {
  680. term.esc = 0;
  681. csiparse(), csihandle();
  682. }
  683. } else if (term.esc & ESCosc) {
  684. if(c == ';') {
  685. term.titlelen = 0;
  686. term.esc = ESCin | ESCtitle;
  687. }
  688. } else if(term.esc & ESCtitle) {
  689. if(c == '\a' || term.titlelen+1 >= TITLESIZ) {
  690. term.esc = 0;
  691. term.title[term.titlelen] = '\0';
  692. XStoreName(xw.dis, xw.win, term.title);
  693. } else {
  694. term.title[term.titlelen++] = c;
  695. }
  696. } else {
  697. switch(c) {
  698. case '[':
  699. term.esc |= ESCcsi;
  700. break;
  701. case ']':
  702. term.esc |= ESCosc;
  703. break;
  704. }
  705. }
  706. } else {
  707. switch(c) {
  708. case '\t':
  709. tputtab();
  710. break;
  711. case '\b':
  712. tcursor(CSleft);
  713. break;
  714. case '\r':
  715. tmoveto(0, term.c.y);
  716. break;
  717. case '\n':
  718. tnewline();
  719. break;
  720. case '\a':
  721. xbell();
  722. break;
  723. case '\033':
  724. csireset();
  725. term.esc = ESCin;
  726. break;
  727. default:
  728. tsetchar(c);
  729. tcursor(CSright);
  730. break;
  731. }
  732. }
  733. }
  734. void
  735. tputs(char *s, int len) {
  736. for(; len > 0; len--)
  737. tputc(*s++);
  738. }
  739. void
  740. tresize(int col, int row) {
  741. int i;
  742. Line *line;
  743. int minrow = MIN(row, term.row);
  744. int mincol = MIN(col, term.col);
  745. if(col < 1 || row < 1)
  746. return;
  747. /* alloc */
  748. line = calloc(row, sizeof(Line));
  749. for(i = 0 ; i < row; i++)
  750. line[i] = calloc(col, sizeof(Glyph));
  751. /* copy */
  752. for(i = 0 ; i < minrow; i++)
  753. memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
  754. /* free */
  755. for(i = 0; i < term.row; i++)
  756. free(term.line[i]);
  757. free(term.line);
  758. LIMIT(term.c.x, 0, col-1);
  759. LIMIT(term.c.y, 0, row-1);
  760. LIMIT(term.top, 0, row-1);
  761. LIMIT(term.bot, 0, row-1);
  762. term.bot = row-1;
  763. term.line = line;
  764. term.col = col, term.row = row;
  765. }
  766. unsigned long
  767. xgetcol(const char *s) {
  768. XColor color;
  769. Colormap cmap = DefaultColormap(xw.dis, xw.scr);
  770. if(!XAllocNamedColor(xw.dis, cmap, s, &color, &color)) {
  771. color.pixel = WhitePixel(xw.dis, xw.scr);
  772. fprintf(stderr, "Could not allocate color '%s'\n", s);
  773. }
  774. return color.pixel;
  775. }
  776. void
  777. xclear(int x1, int y1, int x2, int y2) {
  778. XClearArea(xw.dis, xw.win,
  779. x1 * xw.cw, y1 * xw.ch,
  780. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch,
  781. False);
  782. }
  783. void
  784. xscroll(void) {
  785. int srcy = (term.top+1) * xw.ch;
  786. int dsty = term.top * xw.ch;
  787. int height = (term.bot-term.top) * xw.ch;
  788. xcursor(CShide);
  789. XCopyArea(xw.dis, xw.win, xw.win, dc.gc, 0, srcy, xw.w, height, 0, dsty);
  790. xclear(0, term.bot, term.col-1, term.bot);
  791. }
  792. void
  793. xinit(void) {
  794. XGCValues values;
  795. unsigned long valuemask;
  796. XClassHint chint;
  797. XWMHints wmhint;
  798. XSizeHints shint;
  799. char *args[] = {NULL};
  800. int i;
  801. xw.dis = XOpenDisplay(NULL);
  802. xw.scr = XDefaultScreen(xw.dis);
  803. if(!xw.dis)
  804. die("Can't open display\n");
  805. /* font */
  806. if(!(dc.font = XLoadQueryFont(xw.dis, FONT)))
  807. die("Can't load font %s\n", FONT);
  808. xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
  809. xw.ch = dc.font->ascent + dc.font->descent + LINESPACE;
  810. /* colors */
  811. for(i = 0; i < LEN(colorname); i++)
  812. dc.col[i] = xgetcol(colorname[i]);
  813. term.c.attr.fg = DefaultFG;
  814. term.c.attr.bg = DefaultBG;
  815. term.c.attr.mode = ATnone;
  816. /* windows */
  817. xw.h = term.row * xw.ch;
  818. xw.w = term.col * xw.cw;
  819. /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
  820. xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
  821. xw.w, xw.h, BORDER,
  822. dc.col[DefaultBG],
  823. dc.col[DefaultBG]);
  824. /* gc */
  825. values.foreground = XWhitePixel(xw.dis, xw.scr);
  826. values.font = dc.font->fid;
  827. valuemask = GCForeground | GCFont;
  828. dc.gc = XCreateGC(xw.dis, xw.win, valuemask, &values);
  829. XMapWindow(xw.dis, xw.win);
  830. /* wm stuff */
  831. chint.res_name = TNAME, chint.res_class = TNAME;
  832. wmhint.input = 1, wmhint.flags = InputHint;
  833. shint.height_inc = xw.ch, shint.width_inc = xw.cw;
  834. shint.height = xw.h, shint.width = xw.w;
  835. shint.flags = PSize | PResizeInc;
  836. XSetWMProperties(xw.dis, xw.win, NULL, NULL, &args[0], 0, &shint, &wmhint, &chint);
  837. XStoreName(xw.dis, xw.win, TNAME);
  838. XSync(xw.dis, 0);
  839. }
  840. void
  841. xdraws (char *s, Glyph base, int x, int y, int len) {
  842. unsigned long xfg, xbg;
  843. int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
  844. if(base.mode & ATreverse)
  845. xfg = dc.col[base.bg], xbg = dc.col[base.fg];
  846. else
  847. xfg = dc.col[base.fg], xbg = dc.col[base.bg];
  848. XSetBackground(xw.dis, dc.gc, xbg);
  849. XSetForeground(xw.dis, dc.gc, xfg);
  850. XDrawImageString(xw.dis, xw.win, dc.gc, winx, winy, s, len);
  851. if(base.mode & ATunderline)
  852. XDrawLine(xw.dis, xw.win, dc.gc, winx, winy+1, winx+width-1, winy+1);
  853. }
  854. void
  855. xdrawc(int x, int y, Glyph g) {
  856. XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
  857. unsigned long xfg, xbg;
  858. /* reverse video */
  859. if(g.mode & ATreverse)
  860. xfg = dc.col[g.bg], xbg = dc.col[g.fg];
  861. else
  862. xfg = dc.col[g.fg], xbg = dc.col[g.bg];
  863. /* background */
  864. XSetBackground(xw.dis, dc.gc, xbg);
  865. XSetForeground(xw.dis, dc.gc, xfg);
  866. XDrawImageString(xw.dis, xw.win, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
  867. }
  868. void
  869. xcursor(int mode) {
  870. static int oldx = 0;
  871. static int oldy = 0;
  872. Glyph g = {' ', ATnone, DefaultBG, DefaultCS, 0};
  873. LIMIT(oldx, 0, term.col-1);
  874. LIMIT(oldy, 0, term.row-1);
  875. if(term.line[term.c.y][term.c.x].state & CRset)
  876. g.c = term.line[term.c.y][term.c.x].c;
  877. /* remove the old cursor */
  878. if(term.line[oldy][oldx].state & CRset)
  879. xdrawc(oldx, oldy, term.line[oldy][oldx]);
  880. else
  881. xclear(oldx, oldy, oldx, oldy);
  882. /* draw the new one */
  883. if(mode == CSdraw) {
  884. xdrawc(term.c.x, term.c.y, g);
  885. oldx = term.c.x, oldy = term.c.y;
  886. }
  887. }
  888. void
  889. draw(int redraw_all) {
  890. int i, x, y, ox;
  891. Glyph base, new;
  892. char buf[MAXDRAWBUF];
  893. for(y = 0; y < term.row; y++) {
  894. base = term.line[y][0];
  895. i = ox = 0;
  896. for(x = 0; x < term.col; x++) {
  897. new = term.line[y][x];
  898. if(!ATTRCMP(base, new) && i < MAXDRAWBUF)
  899. buf[i++] = new.c;
  900. else {
  901. xdraws(buf, base, ox, y, i);
  902. buf[0] = new.c;
  903. i = 1;
  904. ox = x;
  905. base = new;
  906. }
  907. }
  908. xdraws(buf, base, ox, y, i);
  909. }
  910. xcursor(CSdraw);
  911. }
  912. void
  913. expose(XEvent *ev) {
  914. draw(SCredraw);
  915. }
  916. char *
  917. kmap(KeySym k) {
  918. int i;
  919. for(i = 0; i < LEN(key); i++)
  920. if(key[i].k == k)
  921. return (char*)key[i].s;
  922. return NULL;
  923. }
  924. void
  925. kpress(XEvent *ev) {
  926. XKeyEvent *e = &ev->xkey;
  927. KeySym ksym;
  928. char buf[32];
  929. char *customkey;
  930. int len;
  931. int meta;
  932. int shift;
  933. meta = e->state & Mod1Mask;
  934. shift = e->state & ShiftMask;
  935. len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
  936. if(customkey = kmap(ksym))
  937. ttywrite(customkey, strlen(customkey));
  938. else if(len > 0) {
  939. buf[sizeof(buf)-1] = '\0';
  940. if(meta && len == 1)
  941. ttywrite("\033", 1);
  942. ttywrite(buf, len);
  943. } else
  944. switch(ksym) {
  945. case XK_Insert:
  946. if(shift)
  947. /* XXX: paste X clipboard */;
  948. break;
  949. default:
  950. fprintf(stderr, "errkey: %d\n", (int)ksym);
  951. break;
  952. }
  953. }
  954. void
  955. resize(XEvent *e) {
  956. int col, row;
  957. col = e->xconfigure.width / xw.cw;
  958. row = e->xconfigure.height / xw.ch;
  959. if(term.col != col || term.row != row) {
  960. tresize(col, row);
  961. ttyresize(col, row);
  962. xw.w = e->xconfigure.width;
  963. xw.h = e->xconfigure.height;
  964. draw(SCredraw);
  965. }
  966. }
  967. void
  968. run(void) {
  969. XEvent ev;
  970. fd_set rfd;
  971. int xfd = XConnectionNumber(xw.dis);
  972. running = 1;
  973. XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
  974. XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* fix resize bug in wmii (?) */
  975. while(running) {
  976. FD_ZERO(&rfd);
  977. FD_SET(cmdfd, &rfd);
  978. FD_SET(xfd, &rfd);
  979. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) == -1) {
  980. if(errno == EINTR)
  981. continue;
  982. die("select failed: %s\n", SERRNO);
  983. }
  984. if(FD_ISSET(cmdfd, &rfd)) {
  985. ttyread();
  986. draw(SCupdate);
  987. }
  988. while(XPending(xw.dis)) {
  989. XNextEvent(xw.dis, &ev);
  990. if(handler[ev.type])
  991. (handler[ev.type])(&ev);
  992. }
  993. }
  994. }
  995. int
  996. main(int argc, char *argv[]) {
  997. if(argc == 2 && !strncmp("-v", argv[1], 3))
  998. die("st-" VERSION ", © 2009 st engineers\n");
  999. else if(argc != 1)
  1000. die("usage: st [-v]\n");
  1001. setlocale(LC_CTYPE, "");
  1002. tnew(80, 24);
  1003. ttynew();
  1004. xinit();
  1005. run();
  1006. return 0;
  1007. }