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.

953 lines
19 KiB

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