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.

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