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.

1463 lines
32 KiB

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