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.

934 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. 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. for(i = 0; i < l; i++) {
  324. switch(attr[i]) {
  325. case 0:
  326. memset(&term.c.attr, 0, sizeof(term.c.attr));
  327. term.c.attr.fg = DefaultFG;
  328. term.c.attr.bg = DefaultBG;
  329. break;
  330. case 1:
  331. term.c.attr.mode |= ATbold;
  332. break;
  333. case 4:
  334. term.c.attr.mode |= ATunderline;
  335. break;
  336. case 7:
  337. term.c.attr.mode |= ATreverse;
  338. break;
  339. case 8:
  340. term.c.hidden = CShide;
  341. break;
  342. case 22:
  343. term.c.attr.mode &= ~ATbold;
  344. break;
  345. case 24:
  346. term.c.attr.mode &= ~ATunderline;
  347. break;
  348. case 27:
  349. term.c.attr.mode &= ~ATreverse;
  350. break;
  351. case 39:
  352. term.c.attr.fg = DefaultFG;
  353. break;
  354. case 49:
  355. term.c.attr.fg = DefaultBG;
  356. break;
  357. default:
  358. if(BETWEEN(attr[i], 30, 37))
  359. term.c.attr.fg = attr[i] - 30;
  360. else if(BETWEEN(attr[i], 40, 47))
  361. term.c.attr.bg = attr[i] - 40;
  362. break;
  363. }
  364. }
  365. }
  366. void
  367. tsetscroll(int t, int b) {
  368. int temp;
  369. LIMIT(t, 0, term.row-1);
  370. LIMIT(b, 0, term.row-1);
  371. if(t > b) {
  372. temp = t;
  373. t = b;
  374. b = temp;
  375. }
  376. term.top = t;
  377. term.bot = b;
  378. }
  379. void
  380. eschandle(void) {
  381. /* escdump(); */
  382. switch(escseq.pre) {
  383. case '[':
  384. switch(escseq.mode) {
  385. case '@': /* Insert <n> blank char */
  386. DEFAULT(escseq.arg[0], 1);
  387. tinsertblank(escseq.arg[0]);
  388. break;
  389. case 'A': /* Cursor <n> Up */
  390. case 'e':
  391. DEFAULT(escseq.arg[0], 1);
  392. tmoveto(term.c.x, term.c.y-escseq.arg[0]);
  393. break;
  394. case 'B': /* Cursor <n> Down */
  395. DEFAULT(escseq.arg[0], 1);
  396. tmoveto(term.c.x, term.c.y+escseq.arg[0]);
  397. break;
  398. case 'C': /* Cursor <n> Forward */
  399. case 'a':
  400. DEFAULT(escseq.arg[0], 1);
  401. tmoveto(term.c.x+escseq.arg[0], term.c.y);
  402. break;
  403. case 'D': /* Cursor <n> Backward */
  404. DEFAULT(escseq.arg[0], 1);
  405. tmoveto(term.c.x-escseq.arg[0], term.c.y);
  406. break;
  407. case 'E': /* Cursor <n> Down and first col */
  408. DEFAULT(escseq.arg[0], 1);
  409. tmoveto(0, term.c.y+escseq.arg[0]);
  410. break;
  411. case 'F': /* Cursor <n> Up and first col */
  412. DEFAULT(escseq.arg[0], 1);
  413. tmoveto(0, term.c.y-escseq.arg[0]);
  414. break;
  415. case 'G': /* Move to <col> */
  416. case '`':
  417. DEFAULT(escseq.arg[0], 1);
  418. tmoveto(escseq.arg[0]-1, term.c.y);
  419. break;
  420. case 'H': /* Move to <row> <col> */
  421. case 'f':
  422. DEFAULT(escseq.arg[0], 1);
  423. DEFAULT(escseq.arg[1], 1);
  424. tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
  425. break;
  426. case 'J': /* Clear screen */
  427. switch(escseq.arg[0]) {
  428. case 0: /* below */
  429. tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
  430. break;
  431. case 1: /* above */
  432. tclearregion(0, 0, term.c.x, term.c.y);
  433. break;
  434. case 2: /* all */
  435. tclearregion(0, 0, term.col-1, term.row-1);
  436. break;
  437. }
  438. break;
  439. case 'K': /* Clear line */
  440. switch(escseq.arg[0]) {
  441. case 0: /* right */
  442. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  443. break;
  444. case 1: /* left */
  445. tclearregion(0, term.c.y, term.c.x, term.c.y);
  446. break;
  447. case 2: /* all */
  448. tclearregion(0, term.c.y, term.col-1, term.c.y);
  449. break;
  450. }
  451. break;
  452. case 'L': /* Insert <n> blank lines */
  453. DEFAULT(escseq.arg[0], 1);
  454. tinsertblankline(escseq.arg[0]);
  455. break;
  456. case 'M': /* Delete <n> lines */
  457. DEFAULT(escseq.arg[0], 1);
  458. tdeleteline(escseq.arg[0]);
  459. break;
  460. case 'P': /* Delete <n> char */
  461. DEFAULT(escseq.arg[0], 1);
  462. tdeletechar(escseq.arg[0]);
  463. break;
  464. case 'd': /* Move to <row> */
  465. DEFAULT(escseq.arg[0], 1);
  466. tmoveto(term.c.x, escseq.arg[0]-1);
  467. break;
  468. case 'h': /* Set terminal mode */
  469. break;
  470. case 'm': /* Terminal attribute (color) */
  471. tsetattr(escseq.arg, escseq.narg);
  472. break;
  473. case 'r':
  474. if(escseq.priv)
  475. ;
  476. else {
  477. DEFAULT(escseq.arg[0], 1);
  478. DEFAULT(escseq.arg[1], term.row);
  479. tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
  480. }
  481. break;
  482. case 's': /* Save cursor position */
  483. tcpos(CSsave);
  484. break;
  485. case 'u': /* Load cursor position */
  486. tcpos(CSload);
  487. break;
  488. }
  489. break;
  490. }
  491. }
  492. void
  493. escdump(void) {
  494. int i;
  495. puts("------");
  496. printf("rawbuf : %s\n", escseq.buf);
  497. printf("prechar : %c\n", escseq.pre);
  498. printf("private : %c\n", escseq.priv ? '?' : ' ');
  499. printf("narg : %d\n", escseq.narg);
  500. if(escseq.narg) {
  501. for(i = 0; i < escseq.narg; i++)
  502. printf("\targ %d = %d\n", i, escseq.arg[i]);
  503. }
  504. printf("mode : %c\n", escseq.mode);
  505. }
  506. void
  507. escreset(void) {
  508. memset(&escseq, 0, sizeof(escseq));
  509. }
  510. void
  511. tputtab(void) {
  512. int space = TAB - term.c.x % TAB;
  513. if(term.c.x + space >= term.col)
  514. space--;
  515. for(; space > 0; space--)
  516. tcursor(CSright);
  517. }
  518. void
  519. tputc(char c) {
  520. static int inesc = 0;
  521. //dump(c);
  522. /* start of escseq */
  523. if(c == '\033')
  524. escreset(), inesc = 1;
  525. else if(inesc) {
  526. inesc = escaddc(c);
  527. } /* normal char */
  528. else switch(c) {
  529. default:
  530. tsetchar(c);
  531. tcursor(CSright);
  532. break;
  533. case '\t':
  534. tputtab();
  535. break;
  536. case '\b':
  537. tcursor(CSleft);
  538. break;
  539. case '\r':
  540. tmoveto(0, term.c.y);
  541. break;
  542. case '\n':
  543. tnewline();
  544. break;
  545. case '\a':
  546. xbell();
  547. break;
  548. }
  549. }
  550. void
  551. tputs(char *s, int len) {
  552. for(; len > 0; len--)
  553. tputc(*s++);
  554. }
  555. void
  556. tdump(void) {
  557. int row, col;
  558. Glyph c;
  559. for(row = 0; row < term.row; row++) {
  560. for(col = 0; col < term.col; col++) {
  561. if(col == term.c.x && row == term.c.y)
  562. putchar('#');
  563. else {
  564. c = term.line[row][col];
  565. putchar(c.state & CRset ? c.c : '.');
  566. }
  567. }
  568. putchar('\n');
  569. }
  570. }
  571. void
  572. tresize(int col, int row) {
  573. int i;
  574. Line *line;
  575. int minrow = MIN(row, term.row);
  576. int mincol = MIN(col, term.col);
  577. if(col < 1 || row < 1)
  578. return;
  579. /* alloc */
  580. line = calloc(row, sizeof(Line));
  581. for(i = 0 ; i < row; i++)
  582. line[i] = calloc(col, sizeof(Glyph));
  583. /* copy */
  584. for(i = 0 ; i < minrow; i++)
  585. memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
  586. /* free */
  587. for(i = 0; i < term.row; i++)
  588. free(term.line[i]);
  589. free(term.line);
  590. LIMIT(term.c.x, 0, col-1);
  591. LIMIT(term.c.y, 0, row-1);
  592. LIMIT(term.top, 0, row-1);
  593. LIMIT(term.bot, 0, row-1);
  594. term.bot = row-1;
  595. term.line = line;
  596. term.col = col, term.row = row;
  597. }
  598. unsigned long
  599. xgetcol(const char *s) {
  600. XColor color;
  601. Colormap cmap = DefaultColormap(xw.dis, xw.scr);
  602. if(!XAllocNamedColor(xw.dis, cmap, s, &color, &color)) {
  603. color.pixel = WhitePixel(xw.dis, xw.scr);
  604. fprintf(stderr, "Could not allocate color '%s'\n", s);
  605. }
  606. return color.pixel;
  607. }
  608. void
  609. xclear(int x1, int y1, int x2, int y2) {
  610. XClearArea(xw.dis, xw.win,
  611. x1 * xw.cw, y1 * xw.ch,
  612. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch,
  613. False);
  614. }
  615. void
  616. xscroll(void) {
  617. int srcy = (term.top+1) * xw.ch;
  618. int dsty = term.top * xw.ch;
  619. int height = (term.bot-term.top) * xw.ch;
  620. xcursor(CShide);
  621. XCopyArea(xw.dis, xw.win, xw.win, dc.gc, 0, srcy, xw.w, height, 0, dsty);
  622. xclear(0, term.bot, term.col-1, term.bot);
  623. }
  624. void
  625. xinit(void) {
  626. XGCValues values;
  627. unsigned long valuemask;
  628. XClassHint chint;
  629. XWMHints wmhint;
  630. XSizeHints shint;
  631. char *args[] = {NULL};
  632. int i;
  633. xw.dis = XOpenDisplay(NULL);
  634. xw.scr = XDefaultScreen(xw.dis);
  635. if(!xw.dis)
  636. die("can not open display");
  637. /* font */
  638. if(!(dc.font = XLoadQueryFont(xw.dis, FONT)))
  639. die("can not find font " 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. LIMIT(oldx, 0, term.col-1);
  701. LIMIT(oldy, 0, term.row-1);
  702. if(term.line[term.c.y][term.c.x].state & CRset)
  703. g.c = term.line[term.c.y][term.c.x].c;
  704. /* remove the old cursor */
  705. if(term.line[oldy][oldx].state & CRset)
  706. xdrawc(oldx, oldy, term.line[oldy][oldx]);
  707. else xclear(oldx, oldy, oldx, oldy); /* XXX: maybe a bug */
  708. if(mode == CSdraw && !term.c.hidden) {
  709. xdrawc(term.c.x, term.c.y, g);
  710. oldx = term.c.x, oldy = term.c.y;
  711. }
  712. }
  713. void
  714. draw(int redraw_all) {
  715. int x, y;
  716. int changed, set;
  717. if(redraw_all)
  718. XClearWindow(xw.dis, xw.win);
  719. /* XXX: drawing could be optimised */
  720. for(y = 0; y < term.row; y++) {
  721. for(x = 0; x < term.col; x++) {
  722. changed = term.line[y][x].state & CRupdate;
  723. set = term.line[y][x].state & CRset;
  724. if((changed && set) || (redraw_all && set)) {
  725. term.line[y][x].state &= ~CRupdate;
  726. xdrawc(x, y, term.line[y][x]);
  727. }
  728. }
  729. }
  730. xcursor(CSdraw);
  731. }
  732. void
  733. kpress(XKeyEvent *e) {
  734. KeySym ksym;
  735. char buf[32];
  736. int len;
  737. int meta;
  738. int shift;
  739. meta = e->state & Mod4Mask;
  740. shift = e->state & ShiftMask;
  741. len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
  742. if(len > 0) {
  743. buf[sizeof(buf)-1] = '\0';
  744. if(meta && len == 1)
  745. ttywrite("\033", 1);
  746. ttywrite(buf, len);
  747. return;
  748. }
  749. switch(ksym) {
  750. #ifdef DEBUG1
  751. default:
  752. printf("errkey: %d\n", (int)ksym);
  753. break;
  754. #endif
  755. case XK_Up:
  756. case XK_Down:
  757. case XK_Left:
  758. case XK_Right:
  759. sprintf(buf, "\033[%c", "DACB"[ksym - XK_Left]);
  760. ttywrite(buf, 3);
  761. break;
  762. case XK_Delete: ttywrite(KEYDELETE, sizeof(KEYDELETE)-1); break;
  763. case XK_Home: ttywrite( KEYHOME, sizeof( KEYHOME)-1); break;
  764. case XK_End: ttywrite( KEYEND, sizeof( KEYEND)-1); break;
  765. case XK_Prior: ttywrite( KEYPREV, sizeof( KEYPREV)-1); break;
  766. case XK_Next: ttywrite( KEYNEXT, sizeof( KEYNEXT)-1); break;
  767. case XK_Insert:
  768. /* XXX: paste X clipboard */
  769. if(shift);
  770. break;
  771. }
  772. }
  773. void
  774. resize(XEvent *e) {
  775. int col, row;
  776. col = e->xconfigure.width / xw.cw;
  777. row = e->xconfigure.height / xw.ch;
  778. if(term.col != col || term.row != row) {
  779. tresize(col, row);
  780. ttyresize(col, row);
  781. xw.w = e->xconfigure.width;
  782. xw.h = e->xconfigure.height;
  783. draw(SCredraw);
  784. }
  785. }
  786. void
  787. run(void) {
  788. int ret;
  789. XEvent ev;
  790. fd_set rfd;
  791. struct timeval tv = {0, 10000};
  792. running = 1;
  793. XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
  794. XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* seems to fix the resize bug in wmii */
  795. while(running) {
  796. while(XPending(xw.dis)) {
  797. XNextEvent(xw.dis, &ev);
  798. switch (ev.type) {
  799. default:
  800. break;
  801. case KeyPress:
  802. kpress(&ev.xkey);
  803. break;
  804. case Expose:
  805. draw(SCredraw);
  806. break;
  807. case ConfigureNotify:
  808. resize(&ev);
  809. break;
  810. }
  811. }
  812. FD_ZERO(&rfd);
  813. FD_SET(cmdfd, &rfd);
  814. ret = select(cmdfd+1, &rfd, NULL, NULL, &tv);
  815. if(ret < 0) {
  816. fprintf(stderr, "select: %m\n");
  817. running = 0;
  818. }
  819. if(!ret)
  820. continue;
  821. if(FD_ISSET(cmdfd, &rfd)) {
  822. ttyread();
  823. draw(SCupdate);
  824. }
  825. }
  826. }
  827. int
  828. main(int argc, char *argv[]) {
  829. if(argc == 2 && !strncmp("-v", argv[1], 3))
  830. die("st-" VERSION ", © 2009 st engineers\n");
  831. else if(argc != 1)
  832. die("usage: st [-v]\n");
  833. setlocale(LC_CTYPE, "");
  834. tnew(80, 24);
  835. ttynew();
  836. xinit();
  837. run();
  838. return 0;
  839. }