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.

927 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. 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. line = calloc(row, sizeof(Line));
  580. for(i = 0 ; i < row; i++)
  581. line[i] = calloc(col, sizeof(Glyph));
  582. for(i = 0 ; i < minrow; i++) {
  583. memcpy(line[i], term.line[i], mincol * sizeof(Glyph));
  584. free(term.line[i]);
  585. }
  586. free(term.line);
  587. LIMIT(term.c.x, 0, col-1);
  588. LIMIT(term.c.y, 0, row-1);
  589. LIMIT(term.top, 0, row-1);
  590. LIMIT(term.bot, 0, row-1);
  591. // if(term.bot == term.row-1)
  592. term.bot = row-1;
  593. term.line = line;
  594. term.col = col, term.row = row;
  595. }
  596. unsigned long
  597. xgetcol(const char *s) {
  598. XColor color;
  599. Colormap cmap = DefaultColormap(xw.dis, xw.scr);
  600. if(!XAllocNamedColor(xw.dis, cmap, s, &color, &color)) {
  601. color.pixel = WhitePixel(xw.dis, xw.scr);
  602. fprintf(stderr, "Could not allocate color '%s'\n", s);
  603. }
  604. return color.pixel;
  605. }
  606. void
  607. xclear(int x1, int y1, int x2, int y2) {
  608. XClearArea(xw.dis, xw.win,
  609. x1 * xw.cw, y1 * xw.ch,
  610. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch,
  611. False);
  612. }
  613. void
  614. xscroll(void) {
  615. int srcy = (term.top+1) * xw.ch;
  616. int dsty = term.top * xw.ch;
  617. int height = (term.bot-term.top) * xw.ch;
  618. xcursor(CShide);
  619. XCopyArea(xw.dis, xw.win, xw.win, dc.gc, 0, srcy, xw.w, height, 0, dsty);
  620. xclear(0, term.bot, term.col-1, term.bot);
  621. }
  622. void
  623. xinit(void) {
  624. XGCValues values;
  625. unsigned long valuemask;
  626. XClassHint chint;
  627. XWMHints wmhint;
  628. XSizeHints shint;
  629. char *args[] = {NULL};
  630. int i;
  631. xw.dis = XOpenDisplay(NULL);
  632. xw.scr = XDefaultScreen(xw.dis);
  633. if(!xw.dis)
  634. die("can not open display");
  635. /* font */
  636. if(!(dc.font = XLoadQueryFont(xw.dis, FONT)))
  637. die("can not find font " FONT);
  638. xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
  639. xw.ch = dc.font->ascent + dc.font->descent + LINESPACE;
  640. /* colors */
  641. for(i = 0; i < LEN(colorname); i++)
  642. dc.col[i] = xgetcol(colorname[i]);
  643. term.c.attr.fg = DefaultFG;
  644. term.c.attr.bg = DefaultBG;
  645. term.c.attr.mode = ATnone;
  646. /* windows */
  647. xw.h = term.row * xw.ch;
  648. xw.w = term.col * xw.cw;
  649. /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
  650. xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
  651. xw.w, xw.h, BORDER,
  652. dc.col[DefaultBG],
  653. dc.col[DefaultBG]);
  654. /* gc */
  655. values.foreground = XWhitePixel(xw.dis, xw.scr);
  656. values.font = dc.font->fid;
  657. valuemask = GCForeground | GCFont;
  658. dc.gc = XCreateGC(xw.dis, xw.win, valuemask, &values);
  659. XMapWindow(xw.dis, xw.win);
  660. /* wm stuff */
  661. chint.res_name = TNAME, chint.res_class = TNAME;
  662. wmhint.input = 1, wmhint.flags = InputHint;
  663. shint.height_inc = xw.ch, shint.width_inc = xw.cw;
  664. shint.height = xw.h, shint.width = xw.w;
  665. shint.flags = PSize | PResizeInc;
  666. XSetWMProperties(xw.dis, xw.win, NULL, NULL, &args[0], 0, &shint, &wmhint, &chint);
  667. XStoreName(xw.dis, xw.win, TNAME);
  668. XSync(xw.dis, 0);
  669. }
  670. void
  671. xdrawc(int x, int y, Glyph g) {
  672. XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
  673. unsigned long xfg, xbg;
  674. /* reverse video */
  675. if(g.mode & ATreverse)
  676. xfg = dc.col[g.bg], xbg = dc.col[g.fg];
  677. else
  678. xfg = dc.col[g.fg], xbg = dc.col[g.bg];
  679. /* background */
  680. XSetForeground(xw.dis, dc.gc, xbg);
  681. XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
  682. /* string */
  683. XSetForeground(xw.dis, dc.gc, xfg);
  684. XDrawString(xw.dis, xw.win, dc.gc, r.x, r.y+dc.font->ascent, &(g.c), 1);
  685. if(g.mode & ATbold) /* XXX: bold hack (draw again at x+1) */
  686. XDrawString(xw.dis, xw.win, dc.gc, r.x+1, r.y+dc.font->ascent, &(g.c), 1);
  687. /* underline */
  688. if(g.mode & ATunderline) {
  689. r.y += dc.font->ascent + 1;
  690. XDrawLine(xw.dis, xw.win, dc.gc, r.x, r.y, r.x+r.width-1, r.y);
  691. }
  692. }
  693. void
  694. xcursor(int mode) {
  695. static int oldx = 0;
  696. static int oldy = 0;
  697. Glyph g = {' ', ATnone, DefaultBG, DefaultCS, 0};
  698. if(term.line[term.c.y][term.c.x].state & CRset)
  699. g.c = term.line[term.c.y][term.c.x].c;
  700. /* remove the old cursor */
  701. if(term.line[oldy][oldx].state & CRset)
  702. xdrawc(oldx, oldy, term.line[oldy][oldx]);
  703. else xclear(oldx, oldy, oldx, oldy); /* XXX: maybe a bug */
  704. if(mode == CSdraw && !term.c.hidden) {
  705. xdrawc(term.c.x, term.c.y, g);
  706. oldx = term.c.x, oldy = term.c.y;
  707. }
  708. }
  709. void
  710. draw(int redraw_all) {
  711. int x, y;
  712. int changed, set;
  713. if(redraw_all)
  714. XClearWindow(xw.dis, xw.win);
  715. /* XXX: drawing could be optimised */
  716. for(y = 0; y < term.row; y++) {
  717. for(x = 0; x < term.col; x++) {
  718. changed = term.line[y][x].state & CRupdate;
  719. set = term.line[y][x].state & CRset;
  720. if((changed && set) || (redraw_all && set)) {
  721. term.line[y][x].state &= ~CRupdate;
  722. xdrawc(x, y, term.line[y][x]);
  723. }
  724. }
  725. }
  726. xcursor(CSdraw);
  727. }
  728. void
  729. kpress(XKeyEvent *e) {
  730. KeySym ksym;
  731. char buf[32];
  732. int len;
  733. int meta;
  734. int shift;
  735. meta = e->state & Mod4Mask;
  736. shift = e->state & ShiftMask;
  737. len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
  738. if(len > 0) {
  739. buf[sizeof(buf)-1] = '\0';
  740. if(meta && len == 1)
  741. ttywrite("\033", 1);
  742. ttywrite(buf, len);
  743. return;
  744. }
  745. switch(ksym) {
  746. #ifdef DEBUG1
  747. default:
  748. printf("errkey: %d\n", (int)ksym);
  749. break;
  750. #endif
  751. case XK_Up:
  752. case XK_Down:
  753. case XK_Left:
  754. case XK_Right:
  755. sprintf(buf, "\033[%c", "DACB"[ksym - XK_Left]);
  756. ttywrite(buf, 3);
  757. break;
  758. case XK_Delete: ttywrite(KEYDELETE, sizeof(KEYDELETE)-1); break;
  759. case XK_Home: ttywrite( KEYHOME, sizeof( KEYHOME)-1); break;
  760. case XK_End: ttywrite( KEYEND, sizeof( KEYEND)-1); break;
  761. case XK_Prior: ttywrite( KEYPREV, sizeof( KEYPREV)-1); break;
  762. case XK_Next: ttywrite( KEYNEXT, sizeof( KEYNEXT)-1); break;
  763. case XK_Insert:
  764. /* XXX: paste X clipboard */
  765. if(shift);
  766. break;
  767. }
  768. }
  769. void
  770. resize(XEvent *e) {
  771. int col, row;
  772. col = e->xconfigure.width / xw.cw;
  773. row = e->xconfigure.height / xw.ch;
  774. if(term.col != col && term.row != row) {
  775. tresize(col, row);
  776. ttyresize(col, row);
  777. xw.w = e->xconfigure.width;
  778. xw.h = e->xconfigure.height;
  779. draw(SCredraw);
  780. }
  781. }
  782. void
  783. run(void) {
  784. int ret;
  785. XEvent ev;
  786. fd_set rfd;
  787. struct timeval tv = {0, 10000};
  788. running = 1;
  789. XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
  790. XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* seems to fix the resize bug in wmii */
  791. while(running) {
  792. while(XPending(xw.dis)) {
  793. XNextEvent(xw.dis, &ev);
  794. switch (ev.type) {
  795. default:
  796. break;
  797. case KeyPress:
  798. kpress(&ev.xkey);
  799. break;
  800. case Expose:
  801. draw(SCredraw);
  802. break;
  803. case ConfigureNotify:
  804. resize(&ev);
  805. break;
  806. }
  807. }
  808. FD_ZERO(&rfd);
  809. FD_SET(cmdfd, &rfd);
  810. ret = select(cmdfd+1, &rfd, NULL, NULL, &tv);
  811. if(ret < 0) {
  812. fprintf(stderr, "select: %m\n");
  813. running = 0;
  814. }
  815. if(!ret)
  816. continue;
  817. if(FD_ISSET(cmdfd, &rfd)) {
  818. ttyread();
  819. draw(SCupdate);
  820. }
  821. }
  822. }
  823. int
  824. main(int argc, char *argv[]) {
  825. if(argc == 2 && !strncmp("-v", argv[1], 3))
  826. die("st-" VERSION ", © 2009 st engineers\n");
  827. else if(argc != 1)
  828. die("usage: st [-v]\n");
  829. setlocale(LC_CTYPE, "");
  830. tnew(80, 24);
  831. ttynew();
  832. xinit();
  833. run();
  834. return 0;
  835. }