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.

920 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. /* XXX: graphic character set */
  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 xf = term.c.x, yf = term.c.y;
  207. switch(dir) {
  208. case CSup:
  209. yf--;
  210. break;
  211. case CSdown:
  212. yf++;
  213. break;
  214. case CSleft:
  215. xf--;
  216. if(xf < 0) {
  217. xf = term.col-1, yf--;
  218. if(yf < term.top)
  219. yf = term.top, xf = 0;
  220. }
  221. break;
  222. case CSright:
  223. xf++;
  224. if(xf >= term.col) {
  225. xf = 0, yf++;
  226. if(yf > term.bot)
  227. yf = term.bot, tscroll();
  228. }
  229. break;
  230. }
  231. tmoveto(xf, yf);
  232. }
  233. void
  234. tsetchar(char c) {
  235. term.line[term.c.y][term.c.x] = term.c.attr;
  236. term.line[term.c.y][term.c.x].c = c;
  237. term.line[term.c.y][term.c.x].state |= CRset | CRupdate;
  238. }
  239. void
  240. tclearregion(int x1, int y1, int x2, int y2) {
  241. int x, y;
  242. LIMIT(x1, 0, term.col-1);
  243. LIMIT(x2, 0, term.col-1);
  244. LIMIT(y1, 0, term.row-1);
  245. LIMIT(y2, 0, term.row-1);
  246. /* XXX: could be optimized */
  247. for(x = x1; x <= x2; x++)
  248. for(y = y1; y <= y2; y++)
  249. memset(&term.line[y][x], 0, sizeof(Glyph));
  250. xclear(x1, y1, x2, y2);
  251. }
  252. void
  253. tdeletechar(int n) {
  254. int src = term.c.x + n;
  255. int dst = term.c.x;
  256. int size = term.col - src;
  257. if(src >= term.col) {
  258. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  259. return;
  260. }
  261. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  262. tclearregion(term.col-size, term.c.y, term.col-1, term.c.y);
  263. }
  264. void
  265. tinsertblank(int n) {
  266. int src = term.c.x;
  267. int dst = src + n;
  268. int size = term.col - n - src;
  269. if(dst >= term.col) {
  270. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  271. return;
  272. }
  273. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  274. tclearregion(src, term.c.y, dst, term.c.y);
  275. }
  276. void
  277. tinsertblankline (int n) {
  278. int i;
  279. Line blank;
  280. int bot = term.bot;
  281. if(term.c.y > term.bot)
  282. bot = term.row - 1;
  283. else if(term.c.y < term.top)
  284. bot = term.top - 1;
  285. if(term.c.y + n >= bot) {
  286. tclearregion(0, term.c.y, term.col-1, bot);
  287. return;
  288. }
  289. for(i = bot; i >= term.c.y+n; i--) {
  290. /* swap deleted line <-> blanked line */
  291. blank = term.line[i];
  292. term.line[i] = term.line[i-n];
  293. term.line[i-n] = blank;
  294. /* blank it */
  295. memset(blank, 0, term.col * sizeof(Glyph));
  296. }
  297. }
  298. void
  299. tdeleteline(int n) {
  300. int i;
  301. Line blank;
  302. int bot = term.bot;
  303. if(term.c.y > term.bot)
  304. bot = term.row - 1;
  305. else if(term.c.y < term.top)
  306. bot = term.top - 1;
  307. if(term.c.y + n >= bot) {
  308. tclearregion(0, term.c.y, term.col-1, bot);
  309. return;
  310. }
  311. for(i = term.c.y; i <= bot-n; i++) {
  312. /* swap deleted line <-> blanked line */
  313. blank = term.line[i];
  314. term.line[i] = term.line[i+n];
  315. term.line[i+n] = blank;
  316. /* blank it */
  317. memset(blank, 0, term.col * sizeof(Glyph));
  318. }
  319. }
  320. void
  321. tsetattr(int *attr, int l) {
  322. int i;
  323. #ifdef TRUECOLOR /* ESC [ ? <fg/bg> ; <r> ; <g> ; <b> m */
  324. Color col;
  325. if(escseq.priv && escseq.len == 4) { /* True color extension :) */
  326. col = (escseq.arg[1]<<16) + (escseq.arg[2]<<8) + escseq.arg[3];
  327. switch(escseq.arg[0]) {
  328. case 3: /* foreground */
  329. term.c.attr.fg = col;
  330. break;
  331. case 4: /* background */
  332. term.c.attr.bg = col;
  333. break;
  334. }
  335. }
  336. else
  337. #endif
  338. for(i = 0; i < l; i++) {
  339. switch(attr[i]) {
  340. case 0:
  341. memset(&term.c.attr, 0, sizeof(term.c.attr));
  342. term.c.attr.fg = DefaultFG;
  343. term.c.attr.bg = DefaultBG;
  344. break;
  345. case 1:
  346. term.c.attr.mode |= ATbold;
  347. break;
  348. case 4:
  349. term.c.attr.mode |= ATunderline;
  350. break;
  351. case 7:
  352. term.c.attr.mode |= ATreverse;
  353. break;
  354. case 8:
  355. term.c.hidden = CShide;
  356. break;
  357. case 22:
  358. term.c.attr.mode &= ~ATbold;
  359. break;
  360. case 24:
  361. term.c.attr.mode &= ~ATunderline;
  362. break;
  363. case 27:
  364. term.c.attr.mode &= ~ATreverse;
  365. break;
  366. case 39:
  367. term.c.attr.fg = DefaultFG;
  368. break;
  369. case 49:
  370. term.c.attr.fg = DefaultBG;
  371. break;
  372. default:
  373. if(BETWEEN(attr[i], 30, 37))
  374. term.c.attr.fg = attr[i] - 30;
  375. else if(BETWEEN(attr[i], 40, 47))
  376. term.c.attr.bg = attr[i] - 40;
  377. break;
  378. }
  379. }
  380. }
  381. void
  382. tsetscroll(int t, int b) {
  383. int temp;
  384. LIMIT(t, 0, term.row-1);
  385. LIMIT(b, 0, term.row-1);
  386. if(t > b) {
  387. temp = t;
  388. t = b;
  389. b = temp;
  390. }
  391. term.top = t;
  392. term.bot = b;
  393. }
  394. void
  395. eschandle(void) {
  396. /* escdump(); */
  397. switch(escseq.pre) {
  398. case '[':
  399. switch(escseq.mode) {
  400. case '@': /* Insert <n> blank char */
  401. DEFAULT(escseq.arg[0], 1);
  402. tinsertblank(escseq.arg[0]);
  403. break;
  404. case 'A': /* Cursor <n> Up */
  405. case 'e':
  406. DEFAULT(escseq.arg[0], 1);
  407. tmoveto(term.c.x, term.c.y-escseq.arg[0]);
  408. break;
  409. case 'B': /* Cursor <n> Down */
  410. DEFAULT(escseq.arg[0], 1);
  411. tmoveto(term.c.x, term.c.y+escseq.arg[0]);
  412. break;
  413. case 'C': /* Cursor <n> Forward */
  414. case 'a':
  415. DEFAULT(escseq.arg[0], 1);
  416. tmoveto(term.c.x+escseq.arg[0], term.c.y);
  417. break;
  418. case 'D': /* Cursor <n> Backward */
  419. DEFAULT(escseq.arg[0], 1);
  420. tmoveto(term.c.x-escseq.arg[0], term.c.y);
  421. break;
  422. case 'E': /* Cursor <n> Down and first col */
  423. DEFAULT(escseq.arg[0], 1);
  424. tmoveto(0, term.c.y+escseq.arg[0]);
  425. break;
  426. case 'F': /* Cursor <n> Up and first col */
  427. DEFAULT(escseq.arg[0], 1);
  428. tmoveto(0, term.c.y-escseq.arg[0]);
  429. break;
  430. case 'G': /* Move to <col> */
  431. case '`':
  432. DEFAULT(escseq.arg[0], 1);
  433. tmoveto(escseq.arg[0]-1, term.c.y);
  434. break;
  435. case 'H': /* Move to <row> <col> */
  436. case 'f':
  437. DEFAULT(escseq.arg[0], 1);
  438. DEFAULT(escseq.arg[1], 1);
  439. tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
  440. break;
  441. case 'J': /* Clear screen */
  442. switch(escseq.arg[0]) {
  443. case 0: /* below */
  444. tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
  445. break;
  446. case 1: /* above */
  447. tclearregion(0, 0, term.c.x, term.c.y);
  448. break;
  449. case 2: /* all */
  450. tclearregion(0, 0, term.col-1, term.row-1);
  451. break;
  452. }
  453. break;
  454. case 'K': /* Clear line */
  455. switch(escseq.arg[0]) {
  456. case 0: /* right */
  457. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  458. break;
  459. case 1: /* left */
  460. tclearregion(0, term.c.y, term.c.x, term.c.y);
  461. break;
  462. case 2: /* all */
  463. tclearregion(0, term.c.y, term.col-1, term.c.y);
  464. break;
  465. }
  466. break;
  467. case 'L': /* Insert <n> blank lines */
  468. DEFAULT(escseq.arg[0], 1);
  469. tinsertblankline(escseq.arg[0]);
  470. break;
  471. case 'M': /* Delete <n> lines */
  472. DEFAULT(escseq.arg[0], 1);
  473. tdeleteline(escseq.arg[0]);
  474. break;
  475. case 'P': /* Delete <n> char */
  476. DEFAULT(escseq.arg[0], 1);
  477. tdeletechar(escseq.arg[0]);
  478. break;
  479. case 'd': /* Move to <row> */
  480. DEFAULT(escseq.arg[0], 1);
  481. tmoveto(term.c.x, escseq.arg[0]-1);
  482. break;
  483. case 'h': /* Set terminal mode */
  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. tputc(char c) {
  527. static int inesc = 0;
  528. dump(c);
  529. /* start of escseq */
  530. if(c == '\033')
  531. escreset(), inesc = 1;
  532. else if(inesc) {
  533. inesc = escaddc(c);
  534. } /* normal char */
  535. else switch(c) {
  536. default:
  537. tsetchar(c);
  538. tcursor(CSright);
  539. break;
  540. case '\b':
  541. tcursor(CSleft);
  542. break;
  543. case '\r':
  544. tmoveto(0, term.c.y);
  545. break;
  546. case '\n':
  547. tnewline();
  548. break;
  549. case '\a':
  550. xbell();
  551. break;
  552. }
  553. }
  554. void
  555. tputs(char *s, int len) {
  556. for(; len > 0; len--)
  557. tputc(*s++);
  558. }
  559. void
  560. tdump(void) {
  561. int row, col;
  562. Glyph c;
  563. for(row = 0; row < term.row; row++) {
  564. for(col = 0; col < term.col; col++) {
  565. if(col == term.c.x && row == term.c.y)
  566. putchar('#');
  567. else {
  568. c = term.line[row][col];
  569. putchar(c.state & CRset ? c.c : '.');
  570. }
  571. }
  572. putchar('\n');
  573. }
  574. }
  575. void
  576. tresize(int col, int row) {
  577. int i;
  578. Line *line;
  579. int minrow = MIN(row, term.row);
  580. int mincol = MIN(col, term.col);
  581. if(col < 1 || row < 1)
  582. return;
  583. line = calloc(row, sizeof(Line));
  584. for(i = 0 ; i < row; i++)
  585. line[i] = calloc(col, sizeof(Glyph));
  586. for(i = 0 ; i < minrow; i++) {
  587. memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
  588. free(term.line[i]);
  589. }
  590. free(term.line);
  591. LIMIT(term.c.x, 0, col-1);
  592. LIMIT(term.c.y, 0, row-1);
  593. LIMIT(term.top, 0, row-1);
  594. LIMIT(term.bot, 0, row-1);
  595. // if(term.bot == term.row-1)
  596. term.bot = row-1;
  597. term.line = line;
  598. term.col = col, term.row = row;
  599. }
  600. unsigned long
  601. xgetcol(const char *s) {
  602. XColor color;
  603. Colormap cmap = DefaultColormap(xw.dis, xw.scr);
  604. if(!XAllocNamedColor(xw.dis, cmap, s, &color, &color)) {
  605. color.pixel = WhitePixel(xw.dis, xw.scr);
  606. fprintf(stderr, "Could not allocate color '%s'\n", s);
  607. }
  608. return color.pixel;
  609. }
  610. void
  611. xclear(int x1, int y1, int x2, int y2) {
  612. XClearArea(xw.dis, xw.win,
  613. x1 * xw.cw, y1 * xw.ch,
  614. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch,
  615. False);
  616. }
  617. void
  618. xscroll(void) {
  619. int srcy = (term.top+1) * xw.ch;
  620. int dsty = term.top * xw.ch;
  621. int height = (term.bot-term.top) * xw.ch;
  622. xcursor(CShide);
  623. XCopyArea(xw.dis, xw.win, xw.win, dc.gc, 0, srcy, xw.w, height, 0, dsty);
  624. xclear(0, term.bot, term.col-1, term.bot);
  625. }
  626. void
  627. xinit(void) {
  628. XGCValues values;
  629. unsigned long valuemask;
  630. XClassHint chint;
  631. XWMHints wmhint;
  632. XSizeHints shint;
  633. char *args[] = {NULL};
  634. int i;
  635. xw.dis = XOpenDisplay(NULL);
  636. xw.scr = XDefaultScreen(xw.dis);
  637. /* font */
  638. dc.font = XLoadQueryFont(xw.dis, FONT);
  639. xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
  640. xw.ch = dc.font->ascent + dc.font->descent + LINESPACE;
  641. /* colors */
  642. for(i = 0; i < LEN(colorname); i++)
  643. dc.col[i] = xgetcol(colorname[i]);
  644. term.c.attr.fg = DefaultFG;
  645. term.c.attr.bg = DefaultBG;
  646. term.c.attr.mode = ATnone;
  647. /* windows */
  648. xw.h = term.row * xw.ch;
  649. xw.w = term.col * xw.cw;
  650. /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
  651. xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
  652. xw.w, xw.h, BORDER,
  653. dc.col[DefaultBG],
  654. dc.col[DefaultBG]);
  655. /* gc */
  656. values.foreground = XWhitePixel(xw.dis, xw.scr);
  657. values.font = dc.font->fid;
  658. valuemask = GCForeground | GCFont;
  659. dc.gc = XCreateGC(xw.dis, xw.win, valuemask, &values);
  660. XMapWindow(xw.dis, xw.win);
  661. /* wm stuff */
  662. chint.res_name = TNAME, chint.res_class = TNAME;
  663. wmhint.input = 1, wmhint.flags = InputHint;
  664. shint.height_inc = xw.ch, shint.width_inc = xw.cw;
  665. shint.height = xw.h, shint.width = xw.w;
  666. shint.flags = PSize | PResizeInc;
  667. XSetWMProperties(xw.dis, xw.win, NULL, NULL, &args[0], 0, &shint, &wmhint, &chint);
  668. XStoreName(xw.dis, xw.win, TNAME);
  669. XSync(xw.dis, 0);
  670. }
  671. void
  672. xdrawc(int x, int y, Glyph g) {
  673. XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
  674. unsigned long xfg, xbg;
  675. /* reverse video */
  676. if(g.mode & ATreverse)
  677. xfg = dc.col[g.bg], xbg = dc.col[g.fg];
  678. else
  679. xfg = dc.col[g.fg], xbg = dc.col[g.bg];
  680. /* background */
  681. XSetForeground(xw.dis, dc.gc, xbg);
  682. XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
  683. /* string */
  684. XSetForeground(xw.dis, dc.gc, xfg);
  685. XDrawString(xw.dis, xw.win, dc.gc, r.x, r.y+dc.font->ascent, &(g.c), 1);
  686. if(g.mode & ATbold) /* XXX: bold hack (draw again at x+1) */
  687. XDrawString(xw.dis, xw.win, dc.gc, r.x+1, r.y+dc.font->ascent, &(g.c), 1);
  688. /* underline */
  689. if(g.mode & ATunderline) {
  690. r.y += dc.font->ascent + 1;
  691. XDrawLine(xw.dis, xw.win, dc.gc, r.x, r.y, r.x+r.width-1, r.y);
  692. }
  693. }
  694. void
  695. xcursor(int mode) {
  696. static int oldx = 0;
  697. static int oldy = 0;
  698. Glyph g = {' ', ATnone, DefaultBG, DefaultCS, 0};
  699. if(term.line[term.c.y][term.c.x].state & CRset)
  700. g.c = term.line[term.c.y][term.c.x].c;
  701. /* remove the old cursor */
  702. if(term.line[oldy][oldx].state & CRset)
  703. xdrawc(oldx, oldy, term.line[oldy][oldx]);
  704. else xclear(oldx, oldy, oldx, oldy); /* XXX: maybe a bug */
  705. if(mode == CSdraw && !term.c.hidden) {
  706. xdrawc(term.c.x, term.c.y, g);
  707. oldx = term.c.x, oldy = term.c.y;
  708. }
  709. }
  710. void
  711. draw(int redraw_all) {
  712. int x, y;
  713. int changed, set;
  714. if(redraw_all)
  715. XClearWindow(xw.dis, xw.win);
  716. /* XXX: drawing could be optimised */
  717. for(y = 0; y < term.row; y++) {
  718. for(x = 0; x < term.col; x++) {
  719. changed = term.line[y][x].state & CRupdate;
  720. set = term.line[y][x].state & CRset;
  721. if((changed && set) || (redraw_all && set)) {
  722. term.line[y][x].state &= ~CRupdate;
  723. xdrawc(x, y, term.line[y][x]);
  724. }
  725. }
  726. }
  727. xcursor(CSdraw);
  728. }
  729. void
  730. kpress(XKeyEvent *e) {
  731. KeySym ksym;
  732. char buf[32];
  733. int len;
  734. int meta;
  735. int shift;
  736. meta = e->state & Mod4Mask;
  737. shift = e->state & ShiftMask;
  738. len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
  739. if(len > 0) {
  740. buf[sizeof(buf)-1] = '\0';
  741. if(meta && len == 1)
  742. ttywrite("\033", 1);
  743. ttywrite(buf, len);
  744. return;
  745. }
  746. switch(ksym) {
  747. #ifdef DEBUG1
  748. default:
  749. printf("errkey: %d\n", (int)ksym);
  750. break;
  751. #endif
  752. case XK_Up:
  753. case XK_Down:
  754. case XK_Left:
  755. case XK_Right:
  756. sprintf(buf, "\033[%c", "DACB"[ksym - XK_Left]);
  757. ttywrite(buf, 3);
  758. break;
  759. case XK_Delete: ttywrite(KEYDELETE, sizeof(KEYDELETE)-1); break;
  760. case XK_Home: ttywrite( KEYHOME, sizeof( KEYHOME)-1); break;
  761. case XK_End: ttywrite( KEYEND, sizeof( KEYEND)-1); break;
  762. case XK_Prior: ttywrite( KEYPREV, sizeof( KEYPREV)-1); break;
  763. case XK_Next: ttywrite( KEYNEXT, sizeof( KEYNEXT)-1); break;
  764. case XK_Insert:
  765. /* XXX: paste X clipboard */
  766. if(shift);
  767. break;
  768. }
  769. }
  770. void
  771. resize(XEvent *e) {
  772. int col, row;
  773. col = e->xconfigure.width / xw.cw;
  774. row = e->xconfigure.height / xw.ch;
  775. if(term.col != col && term.row != row) {
  776. tresize(col, row);
  777. ttyresize(col, row);
  778. xw.w = e->xconfigure.width;
  779. xw.h = e->xconfigure.height;
  780. draw(SCredraw);
  781. }
  782. }
  783. void
  784. run(void) {
  785. int ret;
  786. XEvent ev;
  787. fd_set rfd;
  788. struct timeval tv = {0, 10000};
  789. running = 1;
  790. XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
  791. XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* seems to fix the resize bug in wmii */
  792. while(running) {
  793. while(XPending(xw.dis)) {
  794. XNextEvent(xw.dis, &ev);
  795. switch (ev.type) {
  796. default:
  797. break;
  798. case KeyPress:
  799. kpress(&ev.xkey);
  800. break;
  801. case Expose:
  802. draw(SCredraw);
  803. break;
  804. case ConfigureNotify:
  805. resize(&ev);
  806. break;
  807. }
  808. }
  809. FD_ZERO(&rfd);
  810. FD_SET(cmdfd, &rfd);
  811. ret = select(cmdfd+1, &rfd, NULL, NULL, &tv);
  812. if(ret < 0) {
  813. fprintf(stderr, "select: %m\n");
  814. running = 0;
  815. }
  816. if(!ret)
  817. continue;
  818. if(FD_ISSET(cmdfd, &rfd)) {
  819. ttyread();
  820. draw(SCupdate);
  821. }
  822. }
  823. }
  824. int
  825. main(int argc, char *argv[]) {
  826. if(argc == 2 && !strncmp("-v", argv[1], 3))
  827. die("st-"VERSION", © 2009 st engineers\n");
  828. else if(argc != 1)
  829. die("usage: st [-v]\n");
  830. setlocale(LC_CTYPE, "");
  831. tnew(80, 24);
  832. ttynew();
  833. xinit();
  834. run();
  835. return 0;
  836. }