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.

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