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.

1478 lines
32 KiB

15 years ago
15 years ago
14 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
15 years ago
15 years ago
  1. /* See LICENSE for licence details. */
  2. #define _XOPEN_SOURCE 600
  3. #include <ctype.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <limits.h>
  7. #include <locale.h>
  8. #include <stdarg.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <signal.h>
  13. #include <sys/ioctl.h>
  14. #include <sys/select.h>
  15. #include <sys/stat.h>
  16. #include <sys/types.h>
  17. #include <sys/wait.h>
  18. #include <unistd.h>
  19. #include <X11/Xlib.h>
  20. #include <X11/keysym.h>
  21. #include <X11/Xutil.h>
  22. #if defined(__linux)
  23. #include <pty.h>
  24. #elif defined(__OpenBSD__) || defined(__NetBSD__)
  25. #include <util.h>
  26. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  27. #include <libutil.h>
  28. #endif
  29. /* Arbitrary sizes */
  30. #define ESC_TITLE_SIZ 256
  31. #define ESC_BUF_SIZ 256
  32. #define ESC_ARG_SIZ 16
  33. #define DRAW_BUF_SIZ 1024
  34. #define SERRNO strerror(errno)
  35. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  36. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  37. #define LEN(a) (sizeof(a) / sizeof(a[0]))
  38. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  39. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  40. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  41. #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
  42. #define IS_SET(flag) (term.mode & (flag))
  43. /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
  44. enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 };
  45. enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT,
  46. CURSOR_SAVE, CURSOR_LOAD };
  47. enum { CURSOR_DEFAULT = 0, CURSOR_HIDE = 1, CURSOR_WRAPNEXT = 2 };
  48. enum { GLYPH_SET=1, GLYPH_DIRTY=2 };
  49. enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4, MODE_ALTSCREEN=8 };
  50. enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 };
  51. enum { SCREEN_UPDATE, SCREEN_REDRAW };
  52. typedef struct {
  53. char c; /* character code */
  54. char mode; /* attribute flags */
  55. int fg; /* foreground */
  56. int bg; /* background */
  57. char state; /* state flags */
  58. } Glyph;
  59. typedef Glyph* Line;
  60. typedef struct {
  61. Glyph attr; /* current char attributes */
  62. int x;
  63. int y;
  64. char state;
  65. } TCursor;
  66. /* CSI Escape sequence structs */
  67. /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
  68. typedef struct {
  69. char buf[ESC_BUF_SIZ]; /* raw string */
  70. int len; /* raw string length */
  71. char priv;
  72. int arg[ESC_ARG_SIZ];
  73. int narg; /* nb of args */
  74. char mode;
  75. } CSIEscape;
  76. /* Internal representation of the screen */
  77. typedef struct {
  78. int row; /* nb row */
  79. int col; /* nb col */
  80. Line* line; /* screen */
  81. Line* alt; /* alternate screen */
  82. TCursor c; /* cursor */
  83. int top; /* top scroll limit */
  84. int bot; /* bottom scroll limit */
  85. int mode; /* terminal mode flags */
  86. int esc; /* escape state flags */
  87. char title[ESC_TITLE_SIZ];
  88. int titlelen;
  89. } Term;
  90. /* Purely graphic info */
  91. typedef struct {
  92. Display* dis;
  93. Window win;
  94. Pixmap buf;
  95. int scr;
  96. int w; /* window width */
  97. int h; /* window height */
  98. int bufw; /* pixmap width */
  99. int bufh; /* pixmap height */
  100. int ch; /* char height */
  101. int cw; /* char width */
  102. int hasfocus;
  103. } XWindow;
  104. typedef struct {
  105. KeySym k;
  106. char s[ESC_BUF_SIZ];
  107. } Key;
  108. /* Drawing Context */
  109. typedef struct {
  110. unsigned long col[256];
  111. XFontStruct* font;
  112. XFontStruct* bfont;
  113. GC gc;
  114. } DC;
  115. typedef struct {
  116. int mode;
  117. int bx, by;
  118. int ex, ey;
  119. int b[2], e[2];
  120. char *clip;
  121. } Selection;
  122. #include "config.h"
  123. static void die(const char *errstr, ...);
  124. static void draw(int);
  125. static void execsh(void);
  126. static void sigchld(int);
  127. static void run(void);
  128. static void csidump(void);
  129. static void csihandle(void);
  130. static void csiparse(void);
  131. static void csireset(void);
  132. static void tclearregion(int, int, int, int);
  133. static void tcursor(int);
  134. static void tdeletechar(int);
  135. static void tdeleteline(int);
  136. static void tinsertblank(int);
  137. static void tinsertblankline(int);
  138. static void tmoveto(int, int);
  139. static void tnew(int, int);
  140. static void tnewline(void);
  141. static void tputtab(void);
  142. static void tputc(char);
  143. static void tputs(char*, int);
  144. static void treset(void);
  145. static void tresize(int, int);
  146. static void tscrollup(int);
  147. static void tscrolldown(int);
  148. static void tsetattr(int*, int);
  149. static void tsetchar(char);
  150. static void tsetscroll(int, int);
  151. static void tswapscreen(void);
  152. static void ttynew(void);
  153. static void ttyread(void);
  154. static void ttyresize(int, int);
  155. static void ttywrite(const char *, size_t);
  156. static void xdraws(char *, Glyph, int, int, int);
  157. static void xhints(void);
  158. static void xclear(int, int, int, int);
  159. static void xdrawcursor(void);
  160. static void xinit(void);
  161. static void xloadcols(void);
  162. static void xseturgency(int);
  163. static void expose(XEvent *);
  164. static char* kmap(KeySym);
  165. static void kpress(XEvent *);
  166. static void resize(XEvent *);
  167. static void focus(XEvent *);
  168. static void brelease(XEvent *e);
  169. static void bpress(XEvent *e);
  170. static void bmotion(XEvent *e);
  171. static void (*handler[LASTEvent])(XEvent *) = {
  172. [KeyPress] = kpress,
  173. [Expose] = expose,
  174. [ConfigureNotify] = resize,
  175. [FocusIn] = focus,
  176. [FocusOut] = focus,
  177. [MotionNotify] = bmotion,
  178. [ButtonPress] = bpress,
  179. [ButtonRelease] = brelease,
  180. };
  181. /* Globals */
  182. static DC dc;
  183. static XWindow xw;
  184. static Term term;
  185. static CSIEscape escseq;
  186. static int cmdfd;
  187. static pid_t pid;
  188. static Selection sel;
  189. /* TODO: use X11 clipboard */
  190. static inline int selected(int x, int y) {
  191. if ((sel.ey==y && sel.by==y)) {
  192. int bx = MIN(sel.bx, sel.ex);
  193. int ex = MAX(sel.bx, sel.ex);
  194. return (x>=bx && x<=ex);
  195. }
  196. return (((y>sel.b[1] && y<sel.e[1]) || (y==sel.e[1] && x<=sel.e[0])) || \
  197. (y==sel.b[1] && x>=sel.b[0] && (x<=sel.e[0] || sel.b[1]!=sel.e[1])));
  198. }
  199. static void getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
  200. if(b) *b = e->xbutton.state,
  201. *b=*b==4096?5:*b==2048?4:*b==1024?3:*b==512?2:*b==256?1:-1;
  202. *x = e->xbutton.x/xw.cw;
  203. *y = e->xbutton.y/xw.ch;
  204. sel.b[0] = sel.by<sel.ey?sel.bx:sel.ex;
  205. sel.b[1] = MIN(sel.by, sel.ey);
  206. sel.e[0] = sel.by<sel.ey?sel.ex:sel.bx;
  207. sel.e[1] = MAX(sel.by, sel.ey);
  208. }
  209. static void bpress(XEvent *e) {
  210. sel.mode = 1;
  211. sel.ex = sel.bx = e->xbutton.x/xw.cw;
  212. sel.ey = sel.by = e->xbutton.y/xw.ch;
  213. }
  214. static char *getseltext() {
  215. char *str, *ptr;
  216. int ls, x, y, sz;
  217. if(sel.bx==-1)
  218. return NULL;
  219. sz = ((xw.w/xw.ch) * (sel.e[1]-sel.b[1]+2));
  220. ptr = str = malloc (sz);
  221. for(y = 0; y < term.row; y++) {
  222. for(x = 0; x < term.col; x++) {
  223. if(term.line[y][x].state & GLYPH_SET && (ls=selected(x, y))) {
  224. *ptr = term.line[y][x].c;
  225. ptr++;
  226. }
  227. }
  228. if (ls) {
  229. *ptr = '\n';
  230. ptr++;
  231. }
  232. }
  233. *ptr = 0;
  234. return str;
  235. }
  236. static void clipboard_copy(char *str) {
  237. free(sel.clip);
  238. sel.clip = str;
  239. }
  240. static void clipboard_paste() {
  241. if(sel.clip)
  242. ttywrite(sel.clip, strlen(sel.clip));
  243. }
  244. // TODO: doubleclick to select word
  245. static void brelease(XEvent *e) {
  246. int b;
  247. sel.mode = 0;
  248. getbuttoninfo(e, &b, &sel.ex, &sel.ey);
  249. if(b==4)
  250. tscrollup(1);
  251. else
  252. if(b==5)
  253. tscrolldown(1);
  254. else
  255. if(sel.bx==sel.ex && sel.by==sel.ey) {
  256. sel.bx = -1;
  257. if(b==2)
  258. clipboard_paste();
  259. } else {
  260. if(b==1)
  261. clipboard_copy(getseltext());
  262. }
  263. draw(1);
  264. }
  265. static void bmotion(XEvent *e) {
  266. if (sel.mode) {
  267. getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
  268. draw(1);
  269. }
  270. }
  271. #ifdef DEBUG
  272. void
  273. tdump(void) {
  274. int row, col;
  275. Glyph c;
  276. for(row = 0; row < term.row; row++) {
  277. for(col = 0; col < term.col; col++) {
  278. if(col == term.c.x && row == term.c.y)
  279. putchar('#');
  280. else {
  281. c = term.line[row][col];
  282. putchar(c.state & GLYPH_SET ? c.c : '.');
  283. }
  284. }
  285. putchar('\n');
  286. }
  287. }
  288. #endif
  289. void
  290. die(const char *errstr, ...) {
  291. va_list ap;
  292. va_start(ap, errstr);
  293. vfprintf(stderr, errstr, ap);
  294. va_end(ap);
  295. exit(EXIT_FAILURE);
  296. }
  297. void
  298. execsh(void) {
  299. char *args[3] = {getenv("SHELL"), "-i", NULL};
  300. DEFAULT(args[0], SHELL); /* if getenv() failed */
  301. putenv("TERM=" TNAME);
  302. execvp(args[0], args);
  303. }
  304. void
  305. sigchld(int a) {
  306. int stat = 0;
  307. if(waitpid(pid, &stat, 0) < 0)
  308. die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
  309. if(WIFEXITED(stat))
  310. exit(WEXITSTATUS(stat));
  311. else
  312. exit(EXIT_FAILURE);
  313. }
  314. void
  315. ttynew(void) {
  316. int m, s;
  317. /* seems to work fine on linux, openbsd and freebsd */
  318. struct winsize w = {term.row, term.col, 0, 0};
  319. if(openpty(&m, &s, NULL, NULL, &w) < 0)
  320. die("openpty failed: %s\n", SERRNO);
  321. switch(pid = fork()) {
  322. case -1:
  323. die("fork failed\n");
  324. break;
  325. case 0:
  326. setsid(); /* create a new process group */
  327. dup2(s, STDIN_FILENO);
  328. dup2(s, STDOUT_FILENO);
  329. dup2(s, STDERR_FILENO);
  330. if(ioctl(s, TIOCSCTTY, NULL) < 0)
  331. die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
  332. close(s);
  333. close(m);
  334. execsh();
  335. break;
  336. default:
  337. close(s);
  338. cmdfd = m;
  339. signal(SIGCHLD, sigchld);
  340. }
  341. }
  342. void
  343. dump(char c) {
  344. static int col;
  345. fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
  346. if(++col % 10 == 0)
  347. fprintf(stderr, "\n");
  348. }
  349. void
  350. ttyread(void) {
  351. char buf[BUFSIZ] = {0};
  352. int ret;
  353. if((ret = read(cmdfd, buf, BUFSIZ)) < 0)
  354. die("Couldn't read from shell: %s\n", SERRNO);
  355. else
  356. tputs(buf, ret);
  357. }
  358. void
  359. ttywrite(const char *s, size_t n) {
  360. if(write(cmdfd, s, n) == -1)
  361. die("write error on tty: %s\n", SERRNO);
  362. }
  363. void
  364. ttyresize(int x, int y) {
  365. struct winsize w;
  366. w.ws_row = term.row;
  367. w.ws_col = term.col;
  368. w.ws_xpixel = w.ws_ypixel = 0;
  369. if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  370. fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
  371. }
  372. void
  373. tcursor(int mode) {
  374. static TCursor c;
  375. if(mode == CURSOR_SAVE)
  376. c = term.c;
  377. else if(mode == CURSOR_LOAD)
  378. term.c = c, tmoveto(c.x, c.y);
  379. }
  380. void
  381. treset(void) {
  382. term.c = (TCursor){{
  383. .mode = ATTR_NULL,
  384. .fg = DefaultFG,
  385. .bg = DefaultBG
  386. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  387. term.top = 0, term.bot = term.row - 1;
  388. term.mode = MODE_WRAP;
  389. tclearregion(0, 0, term.col-1, term.row-1);
  390. }
  391. void
  392. tnew(int col, int row) {
  393. /* set screen size */
  394. term.row = row, term.col = col;
  395. term.line = malloc(term.row * sizeof(Line));
  396. term.alt = malloc(term.row * sizeof(Line));
  397. for(row = 0 ; row < term.row; row++) {
  398. term.line[row] = malloc(term.col * sizeof(Glyph));
  399. term.alt [row] = malloc(term.col * sizeof(Glyph));
  400. }
  401. /* setup screen */
  402. treset();
  403. }
  404. void
  405. tswapscreen(void) {
  406. Line* tmp = term.line;
  407. term.line = term.alt;
  408. term.alt = tmp;
  409. term.mode ^= MODE_ALTSCREEN;
  410. }
  411. void
  412. tscrolldown (int n) {
  413. int i;
  414. Line temp;
  415. LIMIT(n, 0, term.bot-term.top+1);
  416. for(i = 0; i < n; i++)
  417. memset(term.line[term.bot-i], 0, term.col*sizeof(Glyph));
  418. for(i = term.bot; i >= term.top+n; i--) {
  419. temp = term.line[i];
  420. term.line[i] = term.line[i-n];
  421. term.line[i-n] = temp;
  422. }
  423. }
  424. void
  425. tscrollup (int n) {
  426. int i;
  427. Line temp;
  428. LIMIT(n, 0, term.bot-term.top+1);
  429. for(i = 0; i < n; i++)
  430. memset(term.line[term.top+i], 0, term.col*sizeof(Glyph));
  431. for(i = term.top; i <= term.bot-n; i++) {
  432. temp = term.line[i];
  433. term.line[i] = term.line[i+n];
  434. term.line[i+n] = temp;
  435. }
  436. }
  437. void
  438. tnewline(void) {
  439. int y = term.c.y + 1;
  440. if(y > term.bot)
  441. tscrollup(1), y = term.bot;
  442. tmoveto(0, y);
  443. }
  444. void
  445. csiparse(void) {
  446. /* int noarg = 1; */
  447. char *p = escseq.buf;
  448. escseq.narg = 0;
  449. if(*p == '?')
  450. escseq.priv = 1, p++;
  451. while(p < escseq.buf+escseq.len) {
  452. while(isdigit(*p)) {
  453. escseq.arg[escseq.narg] *= 10;
  454. escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
  455. }
  456. if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
  457. escseq.narg++, p++;
  458. else {
  459. escseq.mode = *p;
  460. escseq.narg++;
  461. return;
  462. }
  463. }
  464. }
  465. void
  466. tmoveto(int x, int y) {
  467. LIMIT(x, 0, term.col-1);
  468. LIMIT(y, 0, term.row-1);
  469. term.c.state &= ~CURSOR_WRAPNEXT;
  470. term.c.x = x;
  471. term.c.y = y;
  472. }
  473. void
  474. tsetchar(char c) {
  475. term.line[term.c.y][term.c.x] = term.c.attr;
  476. term.line[term.c.y][term.c.x].c = c;
  477. term.line[term.c.y][term.c.x].state |= GLYPH_SET;
  478. }
  479. void
  480. tclearregion(int x1, int y1, int x2, int y2) {
  481. int y, temp;
  482. if(x1 > x2)
  483. temp = x1, x1 = x2, x2 = temp;
  484. if(y1 > y2)
  485. temp = y1, y1 = y2, y2 = temp;
  486. LIMIT(x1, 0, term.col-1);
  487. LIMIT(x2, 0, term.col-1);
  488. LIMIT(y1, 0, term.row-1);
  489. LIMIT(y2, 0, term.row-1);
  490. for(y = y1; y <= y2; y++)
  491. memset(&term.line[y][x1], 0, sizeof(Glyph)*(x2-x1+1));
  492. }
  493. void
  494. tdeletechar(int n) {
  495. int src = term.c.x + n;
  496. int dst = term.c.x;
  497. int size = term.col - src;
  498. if(src >= term.col) {
  499. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  500. return;
  501. }
  502. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  503. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  504. }
  505. void
  506. tinsertblank(int n) {
  507. int src = term.c.x;
  508. int dst = src + n;
  509. int size = term.col - dst;
  510. if(dst >= term.col) {
  511. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  512. return;
  513. }
  514. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  515. tclearregion(src, term.c.y, dst - 1, term.c.y);
  516. }
  517. void
  518. tinsertblankline(int n) {
  519. int i;
  520. Line blank;
  521. int bot = term.bot;
  522. if(term.c.y > term.bot)
  523. bot = term.row - 1;
  524. else if(term.c.y < term.top)
  525. bot = term.top - 1;
  526. if(term.c.y + n >= bot) {
  527. tclearregion(0, term.c.y, term.col-1, bot);
  528. return;
  529. }
  530. for(i = bot; i >= term.c.y+n; i--) {
  531. /* swap deleted line <-> blanked line */
  532. blank = term.line[i];
  533. term.line[i] = term.line[i-n];
  534. term.line[i-n] = blank;
  535. /* blank it */
  536. memset(blank, 0, term.col * sizeof(Glyph));
  537. }
  538. }
  539. void
  540. tdeleteline(int n) {
  541. int i;
  542. Line blank;
  543. int bot = term.bot;
  544. if(term.c.y > term.bot)
  545. bot = term.row - 1;
  546. else if(term.c.y < term.top)
  547. bot = term.top - 1;
  548. if(term.c.y + n >= bot) {
  549. tclearregion(0, term.c.y, term.col-1, bot);
  550. return;
  551. }
  552. for(i = term.c.y; i <= bot-n; i++) {
  553. /* swap deleted line <-> blanked line */
  554. blank = term.line[i];
  555. term.line[i] = term.line[i+n];
  556. term.line[i+n] = blank;
  557. /* blank it */
  558. memset(blank, 0, term.col * sizeof(Glyph));
  559. }
  560. }
  561. void
  562. tsetattr(int *attr, int l) {
  563. int i;
  564. for(i = 0; i < l; i++) {
  565. switch(attr[i]) {
  566. case 0:
  567. term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
  568. term.c.attr.fg = DefaultFG;
  569. term.c.attr.bg = DefaultBG;
  570. break;
  571. case 1:
  572. term.c.attr.mode |= ATTR_BOLD;
  573. break;
  574. case 4:
  575. term.c.attr.mode |= ATTR_UNDERLINE;
  576. break;
  577. case 7:
  578. term.c.attr.mode |= ATTR_REVERSE;
  579. break;
  580. case 22:
  581. term.c.attr.mode &= ~ATTR_BOLD;
  582. break;
  583. case 24:
  584. term.c.attr.mode &= ~ATTR_UNDERLINE;
  585. break;
  586. case 27:
  587. term.c.attr.mode &= ~ATTR_REVERSE;
  588. break;
  589. case 38:
  590. if (i + 2 < l && attr[i + 1] == 5) {
  591. i += 2;
  592. if (BETWEEN(attr[i], 0, 255))
  593. term.c.attr.fg = attr[i];
  594. else
  595. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
  596. }
  597. else
  598. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  599. break;
  600. case 39:
  601. term.c.attr.fg = DefaultFG;
  602. break;
  603. case 48:
  604. if (i + 2 < l && attr[i + 1] == 5) {
  605. i += 2;
  606. if (BETWEEN(attr[i], 0, 255))
  607. term.c.attr.bg = attr[i];
  608. else
  609. fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
  610. }
  611. else
  612. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  613. break;
  614. case 49:
  615. term.c.attr.bg = DefaultBG;
  616. break;
  617. default:
  618. if(BETWEEN(attr[i], 30, 37))
  619. term.c.attr.fg = attr[i] - 30;
  620. else if(BETWEEN(attr[i], 40, 47))
  621. term.c.attr.bg = attr[i] - 40;
  622. else if(BETWEEN(attr[i], 90, 97))
  623. term.c.attr.fg = attr[i] - 90 + 8;
  624. else if(BETWEEN(attr[i], 100, 107))
  625. term.c.attr.fg = attr[i] - 100 + 8;
  626. else
  627. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  628. break;
  629. }
  630. }
  631. }
  632. void
  633. tsetscroll(int t, int b) {
  634. int temp;
  635. LIMIT(t, 0, term.row-1);
  636. LIMIT(b, 0, term.row-1);
  637. if(t > b) {
  638. temp = t;
  639. t = b;
  640. b = temp;
  641. }
  642. term.top = t;
  643. term.bot = b;
  644. }
  645. void
  646. csihandle(void) {
  647. switch(escseq.mode) {
  648. default:
  649. unknown:
  650. printf("erresc: unknown csi ");
  651. csidump();
  652. /* die(""); */
  653. break;
  654. case '@': /* ICH -- Insert <n> blank char */
  655. DEFAULT(escseq.arg[0], 1);
  656. tinsertblank(escseq.arg[0]);
  657. break;
  658. case 'A': /* CUU -- Cursor <n> Up */
  659. case 'e':
  660. DEFAULT(escseq.arg[0], 1);
  661. tmoveto(term.c.x, term.c.y-escseq.arg[0]);
  662. break;
  663. case 'B': /* CUD -- Cursor <n> Down */
  664. DEFAULT(escseq.arg[0], 1);
  665. tmoveto(term.c.x, term.c.y+escseq.arg[0]);
  666. break;
  667. case 'C': /* CUF -- Cursor <n> Forward */
  668. case 'a':
  669. DEFAULT(escseq.arg[0], 1);
  670. tmoveto(term.c.x+escseq.arg[0], term.c.y);
  671. break;
  672. case 'D': /* CUB -- Cursor <n> Backward */
  673. DEFAULT(escseq.arg[0], 1);
  674. tmoveto(term.c.x-escseq.arg[0], term.c.y);
  675. break;
  676. case 'E': /* CNL -- Cursor <n> Down and first col */
  677. DEFAULT(escseq.arg[0], 1);
  678. tmoveto(0, term.c.y+escseq.arg[0]);
  679. break;
  680. case 'F': /* CPL -- Cursor <n> Up and first col */
  681. DEFAULT(escseq.arg[0], 1);
  682. tmoveto(0, term.c.y-escseq.arg[0]);
  683. break;
  684. case 'G': /* CHA -- Move to <col> */
  685. case '`': /* XXX: HPA -- same? */
  686. DEFAULT(escseq.arg[0], 1);
  687. tmoveto(escseq.arg[0]-1, term.c.y);
  688. break;
  689. case 'H': /* CUP -- Move to <row> <col> */
  690. case 'f': /* XXX: HVP -- same? */
  691. DEFAULT(escseq.arg[0], 1);
  692. DEFAULT(escseq.arg[1], 1);
  693. tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
  694. break;
  695. /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
  696. case 'J': /* ED -- Clear screen */
  697. switch(escseq.arg[0]) {
  698. case 0: /* below */
  699. tclearregion(term.c.x, term.c.y, term.col-1, term.row-1);
  700. break;
  701. case 1: /* above */
  702. tclearregion(0, 0, term.c.x, term.c.y);
  703. break;
  704. case 2: /* all */
  705. tclearregion(0, 0, term.col-1, term.row-1);
  706. break;
  707. default:
  708. goto unknown;
  709. }
  710. break;
  711. case 'K': /* EL -- Clear line */
  712. switch(escseq.arg[0]) {
  713. case 0: /* right */
  714. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  715. break;
  716. case 1: /* left */
  717. tclearregion(0, term.c.y, term.c.x, term.c.y);
  718. break;
  719. case 2: /* all */
  720. tclearregion(0, term.c.y, term.col-1, term.c.y);
  721. break;
  722. }
  723. break;
  724. case 'S': /* SU -- Scroll <n> line up */
  725. DEFAULT(escseq.arg[0], 1);
  726. tscrollup(escseq.arg[0]);
  727. break;
  728. case 'T': /* SD -- Scroll <n> line down */
  729. DEFAULT(escseq.arg[0], 1);
  730. tscrolldown(escseq.arg[0]);
  731. break;
  732. case 'L': /* IL -- Insert <n> blank lines */
  733. DEFAULT(escseq.arg[0], 1);
  734. tinsertblankline(escseq.arg[0]);
  735. break;
  736. case 'l': /* RM -- Reset Mode */
  737. if(escseq.priv) {
  738. switch(escseq.arg[0]) {
  739. case 1:
  740. term.mode &= ~MODE_APPKEYPAD;
  741. break;
  742. case 7:
  743. term.mode &= ~MODE_WRAP;
  744. break;
  745. case 12: /* att610 -- Stop blinking cursor (IGNORED) */
  746. break;
  747. case 25:
  748. term.c.state |= CURSOR_HIDE;
  749. break;
  750. case 1049: /* = 1047 and 1048 */
  751. case 1047:
  752. if(IS_SET(MODE_ALTSCREEN)) {
  753. tclearregion(0, 0, term.col-1, term.row-1);
  754. tswapscreen();
  755. }
  756. if(escseq.arg[0] == 1047)
  757. break;
  758. case 1048:
  759. tcursor(CURSOR_LOAD);
  760. break;
  761. default:
  762. goto unknown;
  763. }
  764. } else {
  765. switch(escseq.arg[0]) {
  766. case 4:
  767. term.mode &= ~MODE_INSERT;
  768. break;
  769. default:
  770. goto unknown;
  771. }
  772. }
  773. break;
  774. case 'M': /* DL -- Delete <n> lines */
  775. DEFAULT(escseq.arg[0], 1);
  776. tdeleteline(escseq.arg[0]);
  777. break;
  778. case 'X': /* ECH -- Erase <n> char */
  779. DEFAULT(escseq.arg[0], 1);
  780. tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
  781. break;
  782. case 'P': /* DCH -- Delete <n> char */
  783. DEFAULT(escseq.arg[0], 1);
  784. tdeletechar(escseq.arg[0]);
  785. break;
  786. /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
  787. case 'd': /* VPA -- Move to <row> */
  788. DEFAULT(escseq.arg[0], 1);
  789. tmoveto(term.c.x, escseq.arg[0]-1);
  790. break;
  791. case 'h': /* SM -- Set terminal mode */
  792. if(escseq.priv) {
  793. switch(escseq.arg[0]) {
  794. case 1:
  795. term.mode |= MODE_APPKEYPAD;
  796. break;
  797. case 7:
  798. term.mode |= MODE_WRAP;
  799. break;
  800. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  801. break;
  802. case 25:
  803. term.c.state &= ~CURSOR_HIDE;
  804. break;
  805. case 1049: /* = 1047 and 1048 */
  806. case 1047:
  807. if(IS_SET(MODE_ALTSCREEN))
  808. tclearregion(0, 0, term.col-1, term.row-1);
  809. else
  810. tswapscreen();
  811. if(escseq.arg[0] == 1047)
  812. break;
  813. case 1048:
  814. tcursor(CURSOR_SAVE);
  815. break;
  816. default: goto unknown;
  817. }
  818. } else {
  819. switch(escseq.arg[0]) {
  820. case 4:
  821. term.mode |= MODE_INSERT;
  822. break;
  823. default: goto unknown;
  824. }
  825. };
  826. break;
  827. case 'm': /* SGR -- Terminal attribute (color) */
  828. tsetattr(escseq.arg, escseq.narg);
  829. break;
  830. case 'r': /* DECSTBM -- Set Scrolling Region */
  831. if(escseq.priv)
  832. goto unknown;
  833. else {
  834. DEFAULT(escseq.arg[0], 1);
  835. DEFAULT(escseq.arg[1], term.row);
  836. tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
  837. tmoveto(0, 0);
  838. }
  839. break;
  840. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  841. tcursor(CURSOR_SAVE);
  842. break;
  843. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  844. tcursor(CURSOR_LOAD);
  845. break;
  846. }
  847. }
  848. void
  849. csidump(void) {
  850. int i;
  851. printf("ESC [ %s", escseq.priv ? "? " : "");
  852. if(escseq.narg)
  853. for(i = 0; i < escseq.narg; i++)
  854. printf("%d ", escseq.arg[i]);
  855. if(escseq.mode)
  856. putchar(escseq.mode);
  857. putchar('\n');
  858. }
  859. void
  860. csireset(void) {
  861. memset(&escseq, 0, sizeof(escseq));
  862. }
  863. void
  864. tputtab(void) {
  865. int space = TAB - term.c.x % TAB;
  866. tmoveto(term.c.x + space, term.c.y);
  867. }
  868. void
  869. tputc(char c) {
  870. if(term.esc & ESC_START) {
  871. if(term.esc & ESC_CSI) {
  872. escseq.buf[escseq.len++] = c;
  873. if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
  874. term.esc = 0;
  875. csiparse(), csihandle();
  876. }
  877. } else if(term.esc & ESC_OSC) {
  878. if(c == ';') {
  879. term.titlelen = 0;
  880. term.esc = ESC_START | ESC_TITLE;
  881. }
  882. } else if(term.esc & ESC_TITLE) {
  883. if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
  884. term.esc = 0;
  885. term.title[term.titlelen] = '\0';
  886. XStoreName(xw.dis, xw.win, term.title);
  887. } else {
  888. term.title[term.titlelen++] = c;
  889. }
  890. } else if(term.esc & ESC_ALTCHARSET) {
  891. switch(c) {
  892. case '0': /* Line drawing crap */
  893. term.c.attr.mode |= ATTR_GFX;
  894. break;
  895. case 'B': /* Back to regular text */
  896. term.c.attr.mode &= ~ATTR_GFX;
  897. break;
  898. default:
  899. printf("esc unhandled charset: ESC ( %c\n", c);
  900. }
  901. term.esc = 0;
  902. } else {
  903. switch(c) {
  904. case '[':
  905. term.esc |= ESC_CSI;
  906. break;
  907. case ']':
  908. term.esc |= ESC_OSC;
  909. break;
  910. case '(':
  911. term.esc |= ESC_ALTCHARSET;
  912. break;
  913. case 'D': /* IND -- Linefeed */
  914. if(term.c.y == term.bot)
  915. tscrollup(1);
  916. else
  917. tmoveto(term.c.x, term.c.y+1);
  918. term.esc = 0;
  919. break;
  920. case 'E': /* NEL -- Next line */
  921. tnewline();
  922. term.esc = 0;
  923. break;
  924. case 'M': /* RI -- Reverse index */
  925. if(term.c.y == term.top)
  926. tscrolldown(1);
  927. else
  928. tmoveto(term.c.x, term.c.y-1);
  929. term.esc = 0;
  930. break;
  931. case 'c': /* RIS -- Reset to inital state */
  932. treset();
  933. term.esc = 0;
  934. break;
  935. case '=': /* DECPAM -- Application keypad */
  936. term.mode |= MODE_APPKEYPAD;
  937. term.esc = 0;
  938. break;
  939. case '>': /* DECPNM -- Normal keypad */
  940. term.mode &= ~MODE_APPKEYPAD;
  941. term.esc = 0;
  942. break;
  943. case '7': /* DECSC -- Save Cursor */
  944. tcursor(CURSOR_SAVE);
  945. term.esc = 0;
  946. break;
  947. case '8': /* DECRC -- Restore Cursor */
  948. tcursor(CURSOR_LOAD);
  949. term.esc = 0;
  950. break;
  951. default:
  952. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
  953. term.esc = 0;
  954. }
  955. }
  956. } else {
  957. switch(c) {
  958. case '\t':
  959. tputtab();
  960. break;
  961. case '\b':
  962. tmoveto(term.c.x-1, term.c.y);
  963. break;
  964. case '\r':
  965. tmoveto(0, term.c.y);
  966. break;
  967. case '\n':
  968. tnewline();
  969. break;
  970. case '\a':
  971. if(!xw.hasfocus)
  972. xseturgency(1);
  973. break;
  974. case '\033':
  975. csireset();
  976. term.esc = ESC_START;
  977. break;
  978. default:
  979. if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
  980. tnewline();
  981. tsetchar(c);
  982. if(term.c.x+1 < term.col)
  983. tmoveto(term.c.x+1, term.c.y);
  984. else
  985. term.c.state |= CURSOR_WRAPNEXT;
  986. break;
  987. }
  988. }
  989. }
  990. void
  991. tputs(char *s, int len) {
  992. for(; len > 0; len--)
  993. tputc(*s++);
  994. }
  995. void
  996. tresize(int col, int row) {
  997. int i;
  998. int minrow = MIN(row, term.row);
  999. int mincol = MIN(col, term.col);
  1000. if(col < 1 || row < 1)
  1001. return;
  1002. /* free uneeded rows */
  1003. for(i = row; i < term.row; i++) {
  1004. free(term.line[i]);
  1005. free(term.alt[i]);
  1006. }
  1007. /* resize to new height */
  1008. term.line = realloc(term.line, row * sizeof(Line));
  1009. term.alt = realloc(term.alt, row * sizeof(Line));
  1010. /* resize each row to new width, zero-pad if needed */
  1011. for(i = 0; i < minrow; i++) {
  1012. term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
  1013. term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
  1014. memset(term.line[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
  1015. memset(term.alt[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
  1016. }
  1017. /* allocate any new rows */
  1018. for(/* i == minrow */; i < row; i++) {
  1019. term.line[i] = calloc(col, sizeof(Glyph));
  1020. term.alt [i] = calloc(col, sizeof(Glyph));
  1021. }
  1022. /* update terminal size */
  1023. term.col = col, term.row = row;
  1024. /* make use of the LIMIT in tmoveto */
  1025. tmoveto(term.c.x, term.c.y);
  1026. /* reset scrolling region */
  1027. tsetscroll(0, row-1);
  1028. }
  1029. void
  1030. xloadcols(void) {
  1031. int i, r, g, b;
  1032. XColor color;
  1033. Colormap cmap = DefaultColormap(xw.dis, xw.scr);
  1034. unsigned long white = WhitePixel(xw.dis, xw.scr);
  1035. for(i = 0; i < 16; i++) {
  1036. if (!XAllocNamedColor(xw.dis, cmap, colorname[i], &color, &color)) {
  1037. dc.col[i] = white;
  1038. fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
  1039. } else
  1040. dc.col[i] = color.pixel;
  1041. }
  1042. /* same colors as xterm */
  1043. for(r = 0; r < 6; r++)
  1044. for(g = 0; g < 6; g++)
  1045. for(b = 0; b < 6; b++) {
  1046. color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
  1047. color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
  1048. color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
  1049. if (!XAllocColor(xw.dis, cmap, &color)) {
  1050. dc.col[i] = white;
  1051. fprintf(stderr, "Could not allocate color %d\n", i);
  1052. } else
  1053. dc.col[i] = color.pixel;
  1054. i++;
  1055. }
  1056. for(r = 0; r < 24; r++, i++) {
  1057. color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
  1058. if (!XAllocColor(xw.dis, cmap, &color)) {
  1059. dc.col[i] = white;
  1060. fprintf(stderr, "Could not allocate color %d\n", i);
  1061. } else
  1062. dc.col[i] = color.pixel;
  1063. }
  1064. }
  1065. void
  1066. xclear(int x1, int y1, int x2, int y2) {
  1067. XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
  1068. XFillRectangle(xw.dis, xw.buf, dc.gc,
  1069. x1 * xw.cw, y1 * xw.ch,
  1070. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
  1071. }
  1072. void
  1073. xhints(void)
  1074. {
  1075. XClassHint class = {TNAME, TNAME};
  1076. XWMHints wm = {.flags = InputHint, .input = 1};
  1077. XSizeHints size = {
  1078. .flags = PSize | PResizeInc | PBaseSize,
  1079. .height = xw.h,
  1080. .width = xw.w,
  1081. .height_inc = xw.ch,
  1082. .width_inc = xw.cw,
  1083. .base_height = 2*BORDER,
  1084. .base_width = 2*BORDER,
  1085. };
  1086. XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
  1087. }
  1088. void
  1089. xinit(void) {
  1090. if(!(xw.dis = XOpenDisplay(NULL)))
  1091. die("Can't open display\n");
  1092. xw.scr = XDefaultScreen(xw.dis);
  1093. /* font */
  1094. if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
  1095. die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
  1096. /* XXX: Assuming same size for bold font */
  1097. xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
  1098. xw.ch = dc.font->ascent + dc.font->descent;
  1099. /* colors */
  1100. xloadcols();
  1101. /* windows */
  1102. xw.h = term.row * xw.ch + 2*BORDER;
  1103. xw.w = term.col * xw.cw + 2*BORDER;
  1104. xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
  1105. xw.w, xw.h, 0,
  1106. dc.col[DefaultBG],
  1107. dc.col[DefaultBG]);
  1108. xw.bufw = xw.w - 2*BORDER;
  1109. xw.bufh = xw.h - 2*BORDER;
  1110. xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
  1111. xw.hasfocus = 1;
  1112. /* gc */
  1113. dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
  1114. XMapWindow(xw.dis, xw.win);
  1115. xhints();
  1116. XStoreName(xw.dis, xw.win, "st");
  1117. XSync(xw.dis, 0);
  1118. }
  1119. void
  1120. xdraws(char *s, Glyph base, int x, int y, int len) {
  1121. unsigned long xfg, xbg;
  1122. int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
  1123. int i;
  1124. if(base.mode & ATTR_REVERSE)
  1125. xfg = dc.col[base.bg], xbg = dc.col[base.fg];
  1126. else
  1127. xfg = dc.col[base.fg], xbg = dc.col[base.bg];
  1128. XSetBackground(xw.dis, dc.gc, xbg);
  1129. XSetForeground(xw.dis, dc.gc, xfg);
  1130. if(base.mode & ATTR_GFX)
  1131. for(i = 0; i < len; i++)
  1132. s[i] = gfx[(int)s[i]];
  1133. XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
  1134. XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
  1135. if(base.mode & ATTR_UNDERLINE)
  1136. XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
  1137. }
  1138. void
  1139. xdrawcursor(void) {
  1140. static int oldx = 0;
  1141. static int oldy = 0;
  1142. Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
  1143. LIMIT(oldx, 0, term.col-1);
  1144. LIMIT(oldy, 0, term.row-1);
  1145. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  1146. g.c = term.line[term.c.y][term.c.x].c;
  1147. /* remove the old cursor */
  1148. if(term.line[oldy][oldx].state & GLYPH_SET)
  1149. xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
  1150. else
  1151. xclear(oldx, oldy, oldx, oldy);
  1152. /* draw the new one */
  1153. if(!(term.c.state & CURSOR_HIDE) && xw.hasfocus) {
  1154. xdraws(&g.c, g, term.c.x, term.c.y, 1);
  1155. oldx = term.c.x, oldy = term.c.y;
  1156. }
  1157. }
  1158. #ifdef DEBUG
  1159. /* basic drawing routines */
  1160. void
  1161. xdrawc(int x, int y, Glyph g) {
  1162. XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
  1163. XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
  1164. XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
  1165. XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
  1166. XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
  1167. }
  1168. void
  1169. draw(int dummy) {
  1170. int x, y;
  1171. xclear(0, 0, term.col-1, term.row-1);
  1172. for(y = 0; y < term.row; y++)
  1173. for(x = 0; x < term.col; x++)
  1174. if(term.line[y][x].state & GLYPH_SET)
  1175. xdrawc(x, y, term.line[y][x]);
  1176. xdrawcursor();
  1177. XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1178. XFlush(xw.dis);
  1179. }
  1180. #else
  1181. /* optimized drawing routine */
  1182. void
  1183. draw(int redraw_all) {
  1184. int i, x, y, ox;
  1185. Glyph base, new;
  1186. char buf[DRAW_BUF_SIZ];
  1187. XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
  1188. XFillRectangle(xw.dis, xw.buf, dc.gc, 0, 0, xw.bufw, xw.bufh);
  1189. for(y = 0; y < term.row; y++) {
  1190. base = term.line[y][0];
  1191. i = ox = 0;
  1192. for(x = 0; x < term.col; x++) {
  1193. new = term.line[y][x];
  1194. if(sel.bx!=-1 && new.c && selected(x, y))
  1195. new.mode ^= ATTR_REVERSE;
  1196. if(i > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
  1197. i >= DRAW_BUF_SIZ)) {
  1198. xdraws(buf, base, ox, y, i);
  1199. i = 0;
  1200. }
  1201. if(new.state & GLYPH_SET) {
  1202. if(i == 0) {
  1203. ox = x;
  1204. base = new;
  1205. }
  1206. buf[i++] = new.c;
  1207. }
  1208. }
  1209. if(i > 0)
  1210. xdraws(buf, base, ox, y, i);
  1211. }
  1212. xdrawcursor();
  1213. XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1214. XFlush(xw.dis);
  1215. }
  1216. #endif
  1217. void
  1218. expose(XEvent *ev) {
  1219. draw(SCREEN_REDRAW);
  1220. }
  1221. void
  1222. xseturgency(int add) {
  1223. XWMHints *h = XGetWMHints(xw.dis, xw.win);
  1224. h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
  1225. XSetWMHints(xw.dis, xw.win, h);
  1226. XFree(h);
  1227. }
  1228. void
  1229. focus(XEvent *ev) {
  1230. if((xw.hasfocus = ev->type == FocusIn))
  1231. xseturgency(0);
  1232. draw(SCREEN_UPDATE);
  1233. }
  1234. char*
  1235. kmap(KeySym k) {
  1236. int i;
  1237. for(i = 0; i < LEN(key); i++)
  1238. if(key[i].k == k)
  1239. return (char*)key[i].s;
  1240. return NULL;
  1241. }
  1242. void
  1243. kpress(XEvent *ev) {
  1244. XKeyEvent *e = &ev->xkey;
  1245. KeySym ksym;
  1246. char buf[32];
  1247. char *customkey;
  1248. int len;
  1249. int meta;
  1250. int shift;
  1251. meta = e->state & Mod1Mask;
  1252. shift = e->state & ShiftMask;
  1253. len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
  1254. if((customkey = kmap(ksym)))
  1255. ttywrite(customkey, strlen(customkey));
  1256. else if(len > 0) {
  1257. buf[sizeof(buf)-1] = '\0';
  1258. if(meta && len == 1)
  1259. ttywrite("\033", 1);
  1260. ttywrite(buf, len);
  1261. } else
  1262. switch(ksym) {
  1263. case XK_Up:
  1264. case XK_Down:
  1265. case XK_Left:
  1266. case XK_Right:
  1267. sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', "DACB"[ksym - XK_Left]);
  1268. ttywrite(buf, 3);
  1269. break;
  1270. case XK_Insert:
  1271. if(shift)
  1272. draw(1), puts("draw!")/* XXX: paste X clipboard */;
  1273. break;
  1274. default:
  1275. fprintf(stderr, "errkey: %d\n", (int)ksym);
  1276. break;
  1277. }
  1278. }
  1279. void
  1280. resize(XEvent *e) {
  1281. int col, row;
  1282. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  1283. return;
  1284. xw.w = e->xconfigure.width;
  1285. xw.h = e->xconfigure.height;
  1286. xw.bufw = xw.w - 2*BORDER;
  1287. xw.bufh = xw.h - 2*BORDER;
  1288. col = xw.bufw / xw.cw;
  1289. row = xw.bufh / xw.ch;
  1290. tresize(col, row);
  1291. ttyresize(col, row);
  1292. xw.bufh = MAX(1, xw.bufh);
  1293. xw.bufw = MAX(1, xw.bufw);
  1294. XFreePixmap(xw.dis, xw.buf);
  1295. xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
  1296. draw(SCREEN_REDRAW);
  1297. }
  1298. void
  1299. run(void) {
  1300. XEvent ev;
  1301. fd_set rfd;
  1302. int xfd = XConnectionNumber(xw.dis);
  1303. long mask = ExposureMask | KeyPressMask | StructureNotifyMask
  1304. | FocusChangeMask | PointerMotionMask | ButtonPressMask
  1305. | ButtonReleaseMask;
  1306. XSelectInput(xw.dis, xw.win, mask);
  1307. XResizeWindow(xw.dis, xw.win, xw.w, xw.h); /* XXX: fix resize bug in wmii (?) */
  1308. for(;;) {
  1309. FD_ZERO(&rfd);
  1310. FD_SET(cmdfd, &rfd);
  1311. FD_SET(xfd, &rfd);
  1312. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
  1313. if(errno == EINTR)
  1314. continue;
  1315. die("select failed: %s\n", SERRNO);
  1316. }
  1317. if(FD_ISSET(cmdfd, &rfd)) {
  1318. ttyread();
  1319. draw(SCREEN_UPDATE);
  1320. }
  1321. while(XPending(xw.dis)) {
  1322. XNextEvent(xw.dis, &ev);
  1323. if(handler[ev.type])
  1324. (handler[ev.type])(&ev);
  1325. }
  1326. }
  1327. }
  1328. int
  1329. main(int argc, char *argv[]) {
  1330. if(argc == 2 && !strncmp("-v", argv[1], 3))
  1331. die("st-" VERSION ", (c) 2010 st engineers\n");
  1332. else if(argc != 1)
  1333. die("usage: st [-v]\n");
  1334. setlocale(LC_CTYPE, "");
  1335. tnew(80, 24);
  1336. ttynew();
  1337. xinit();
  1338. run();
  1339. return 0;
  1340. }