Simple Terminal from SuckLess
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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