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.

1476 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
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. #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. int slide = term.c.y - row + 1;
  974. if(col < 1 || row < 1)
  975. return;
  976. /* free unneeded rows */
  977. i = 0;
  978. if(slide > 0) {
  979. /* slide screen to keep cursor where we expect it -
  980. * tscrollup would work here, but we can optimize to
  981. * memmove because we're freeing the earlier lines */
  982. for(/* i = 0 */; i < slide; i++) {
  983. free(term.line[i]);
  984. free(term.alt[i]);
  985. }
  986. memmove(term.line, term.line + slide, row * sizeof(Line));
  987. memmove(term.alt, term.alt + slide, row * sizeof(Line));
  988. }
  989. for(i += row; i < term.row; i++) {
  990. free(term.line[i]);
  991. free(term.alt[i]);
  992. }
  993. /* resize to new height */
  994. term.line = realloc(term.line, row * sizeof(Line));
  995. term.alt = realloc(term.alt, row * sizeof(Line));
  996. /* resize each row to new width, zero-pad if needed */
  997. for(i = 0; i < minrow; i++) {
  998. term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
  999. term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
  1000. memset(term.line[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
  1001. memset(term.alt[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
  1002. }
  1003. /* allocate any new rows */
  1004. for(/* i == minrow */; i < row; i++) {
  1005. term.line[i] = calloc(col, sizeof(Glyph));
  1006. term.alt [i] = calloc(col, sizeof(Glyph));
  1007. }
  1008. /* update terminal size */
  1009. term.col = col, term.row = row;
  1010. /* make use of the LIMIT in tmoveto */
  1011. tmoveto(term.c.x, term.c.y);
  1012. /* reset scrolling region */
  1013. tsetscroll(0, row-1);
  1014. }
  1015. void
  1016. xloadcols(void) {
  1017. int i, r, g, b;
  1018. XColor color;
  1019. Colormap cmap = DefaultColormap(xw.dis, xw.scr);
  1020. unsigned long white = WhitePixel(xw.dis, xw.scr);
  1021. for(i = 0; i < 16; i++) {
  1022. if (!XAllocNamedColor(xw.dis, cmap, colorname[i], &color, &color)) {
  1023. dc.col[i] = white;
  1024. fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
  1025. } else
  1026. dc.col[i] = color.pixel;
  1027. }
  1028. /* same colors as xterm */
  1029. for(r = 0; r < 6; r++)
  1030. for(g = 0; g < 6; g++)
  1031. for(b = 0; b < 6; b++) {
  1032. color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
  1033. color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
  1034. color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
  1035. if (!XAllocColor(xw.dis, cmap, &color)) {
  1036. dc.col[i] = white;
  1037. fprintf(stderr, "Could not allocate color %d\n", i);
  1038. } else
  1039. dc.col[i] = color.pixel;
  1040. i++;
  1041. }
  1042. for(r = 0; r < 24; r++, i++) {
  1043. color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
  1044. if (!XAllocColor(xw.dis, cmap, &color)) {
  1045. dc.col[i] = white;
  1046. fprintf(stderr, "Could not allocate color %d\n", i);
  1047. } else
  1048. dc.col[i] = color.pixel;
  1049. }
  1050. }
  1051. void
  1052. xclear(int x1, int y1, int x2, int y2) {
  1053. XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
  1054. XFillRectangle(xw.dis, xw.buf, dc.gc,
  1055. x1 * xw.cw, y1 * xw.ch,
  1056. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
  1057. }
  1058. void
  1059. xhints(void)
  1060. {
  1061. XClassHint class = {TNAME, TNAME};
  1062. XWMHints wm = {.flags = InputHint, .input = 1};
  1063. XSizeHints size = {
  1064. .flags = PSize | PResizeInc | PBaseSize,
  1065. .height = xw.h,
  1066. .width = xw.w,
  1067. .height_inc = xw.ch,
  1068. .width_inc = xw.cw,
  1069. .base_height = 2*BORDER,
  1070. .base_width = 2*BORDER,
  1071. };
  1072. XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
  1073. }
  1074. void
  1075. xinit(void) {
  1076. if(!(xw.dis = XOpenDisplay(NULL)))
  1077. die("Can't open display\n");
  1078. xw.scr = XDefaultScreen(xw.dis);
  1079. /* font */
  1080. if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
  1081. die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
  1082. /* XXX: Assuming same size for bold font */
  1083. xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
  1084. xw.ch = dc.font->ascent + dc.font->descent;
  1085. /* colors */
  1086. xloadcols();
  1087. /* windows */
  1088. xw.bufh = term.row * xw.ch;
  1089. xw.bufw = term.col * xw.cw;
  1090. xw.h = xw.bufh + 2*BORDER;
  1091. xw.w = xw.bufw + 2*BORDER;
  1092. xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
  1093. xw.w, xw.h, 0,
  1094. dc.col[DefaultBG],
  1095. dc.col[DefaultBG]);
  1096. xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
  1097. /* gc */
  1098. dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
  1099. /* event mask */
  1100. XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask
  1101. | StructureNotifyMask | FocusChangeMask | PointerMotionMask
  1102. | ButtonPressMask | ButtonReleaseMask);
  1103. XMapWindow(xw.dis, xw.win);
  1104. xhints();
  1105. XStoreName(xw.dis, xw.win, opt_title ? opt_title : "st");
  1106. XSync(xw.dis, 0);
  1107. }
  1108. void
  1109. xdraws(char *s, Glyph base, int x, int y, int len) {
  1110. unsigned long xfg, xbg;
  1111. int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
  1112. int i;
  1113. if(base.mode & ATTR_REVERSE)
  1114. xfg = dc.col[base.bg], xbg = dc.col[base.fg];
  1115. else
  1116. xfg = dc.col[base.fg], xbg = dc.col[base.bg];
  1117. XSetBackground(xw.dis, dc.gc, xbg);
  1118. XSetForeground(xw.dis, dc.gc, xfg);
  1119. if(base.mode & ATTR_GFX)
  1120. for(i = 0; i < len; i++)
  1121. s[i] = gfx[(int)s[i]];
  1122. XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
  1123. XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
  1124. if(base.mode & ATTR_UNDERLINE)
  1125. XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
  1126. }
  1127. void
  1128. xdrawcursor(void) {
  1129. static int oldx = 0;
  1130. static int oldy = 0;
  1131. Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
  1132. LIMIT(oldx, 0, term.col-1);
  1133. LIMIT(oldy, 0, term.row-1);
  1134. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  1135. g.c = term.line[term.c.y][term.c.x].c;
  1136. /* remove the old cursor */
  1137. if(term.line[oldy][oldx].state & GLYPH_SET)
  1138. xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
  1139. else
  1140. xclear(oldx, oldy, oldx, oldy);
  1141. /* draw the new one */
  1142. if(!(term.c.state & CURSOR_HIDE) && xw.hasfocus) {
  1143. xdraws(&g.c, g, term.c.x, term.c.y, 1);
  1144. oldx = term.c.x, oldy = term.c.y;
  1145. }
  1146. }
  1147. #ifdef DEBUG
  1148. /* basic drawing routines */
  1149. void
  1150. xdrawc(int x, int y, Glyph g) {
  1151. XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
  1152. XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
  1153. XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
  1154. XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
  1155. XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
  1156. }
  1157. void
  1158. draw(int dummy) {
  1159. int x, y;
  1160. xclear(0, 0, term.col-1, term.row-1);
  1161. for(y = 0; y < term.row; y++)
  1162. for(x = 0; x < term.col; x++)
  1163. if(term.line[y][x].state & GLYPH_SET)
  1164. xdrawc(x, y, term.line[y][x]);
  1165. xdrawcursor();
  1166. XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1167. XFlush(xw.dis);
  1168. }
  1169. #else
  1170. /* optimized drawing routine */
  1171. void
  1172. draw(int redraw_all) {
  1173. int i, x, y, ox;
  1174. Glyph base, new;
  1175. char buf[DRAW_BUF_SIZ];
  1176. XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
  1177. XFillRectangle(xw.dis, xw.buf, dc.gc, 0, 0, xw.bufw, xw.bufh);
  1178. for(y = 0; y < term.row; y++) {
  1179. base = term.line[y][0];
  1180. i = ox = 0;
  1181. for(x = 0; x < term.col; x++) {
  1182. new = term.line[y][x];
  1183. if(sel.bx!=-1 && new.c && selected(x, y))
  1184. new.mode ^= ATTR_REVERSE;
  1185. if(i > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
  1186. i >= DRAW_BUF_SIZ)) {
  1187. xdraws(buf, base, ox, y, i);
  1188. i = 0;
  1189. }
  1190. if(new.state & GLYPH_SET) {
  1191. if(i == 0) {
  1192. ox = x;
  1193. base = new;
  1194. }
  1195. buf[i++] = new.c;
  1196. }
  1197. }
  1198. if(i > 0)
  1199. xdraws(buf, base, ox, y, i);
  1200. }
  1201. xdrawcursor();
  1202. XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1203. XFlush(xw.dis);
  1204. }
  1205. #endif
  1206. void
  1207. expose(XEvent *ev) {
  1208. draw(SCREEN_REDRAW);
  1209. }
  1210. void
  1211. xseturgency(int add) {
  1212. XWMHints *h = XGetWMHints(xw.dis, xw.win);
  1213. h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
  1214. XSetWMHints(xw.dis, xw.win, h);
  1215. XFree(h);
  1216. }
  1217. void
  1218. focus(XEvent *ev) {
  1219. if((xw.hasfocus = ev->type == FocusIn))
  1220. xseturgency(0);
  1221. draw(SCREEN_UPDATE);
  1222. }
  1223. char*
  1224. kmap(KeySym k) {
  1225. int i;
  1226. for(i = 0; i < LEN(key); i++)
  1227. if(key[i].k == k)
  1228. return (char*)key[i].s;
  1229. return NULL;
  1230. }
  1231. void
  1232. kpress(XEvent *ev) {
  1233. XKeyEvent *e = &ev->xkey;
  1234. KeySym ksym;
  1235. char buf[32];
  1236. char *customkey;
  1237. int len;
  1238. int meta;
  1239. int shift;
  1240. meta = e->state & Mod1Mask;
  1241. shift = e->state & ShiftMask;
  1242. len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
  1243. if((customkey = kmap(ksym)))
  1244. ttywrite(customkey, strlen(customkey));
  1245. else if(len > 0) {
  1246. buf[sizeof(buf)-1] = '\0';
  1247. if(meta && len == 1)
  1248. ttywrite("\033", 1);
  1249. ttywrite(buf, len);
  1250. } else
  1251. switch(ksym) {
  1252. case XK_Up:
  1253. case XK_Down:
  1254. case XK_Left:
  1255. case XK_Right:
  1256. sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', "DACB"[ksym - XK_Left]);
  1257. ttywrite(buf, 3);
  1258. break;
  1259. case XK_Insert:
  1260. if(shift)
  1261. selpaste(), draw(1);
  1262. break;
  1263. default:
  1264. fprintf(stderr, "errkey: %d\n", (int)ksym);
  1265. break;
  1266. }
  1267. }
  1268. void
  1269. resize(XEvent *e) {
  1270. int col, row;
  1271. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  1272. return;
  1273. xw.w = e->xconfigure.width;
  1274. xw.h = e->xconfigure.height;
  1275. xw.bufw = xw.w - 2*BORDER;
  1276. xw.bufh = xw.h - 2*BORDER;
  1277. col = xw.bufw / xw.cw;
  1278. row = xw.bufh / xw.ch;
  1279. tresize(col, row);
  1280. ttyresize(col, row);
  1281. xw.bufh = MAX(1, xw.bufh);
  1282. xw.bufw = MAX(1, xw.bufw);
  1283. XFreePixmap(xw.dis, xw.buf);
  1284. xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
  1285. draw(SCREEN_REDRAW);
  1286. }
  1287. void
  1288. run(void) {
  1289. XEvent ev;
  1290. fd_set rfd;
  1291. int xfd = XConnectionNumber(xw.dis);
  1292. for(;;) {
  1293. FD_ZERO(&rfd);
  1294. FD_SET(cmdfd, &rfd);
  1295. FD_SET(xfd, &rfd);
  1296. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
  1297. if(errno == EINTR)
  1298. continue;
  1299. die("select failed: %s\n", SERRNO);
  1300. }
  1301. if(FD_ISSET(cmdfd, &rfd)) {
  1302. ttyread();
  1303. draw(SCREEN_UPDATE);
  1304. }
  1305. while(XPending(xw.dis)) {
  1306. XNextEvent(xw.dis, &ev);
  1307. if(handler[ev.type])
  1308. (handler[ev.type])(&ev);
  1309. }
  1310. }
  1311. }
  1312. int
  1313. main(int argc, char *argv[]) {
  1314. int i;
  1315. for(i = 1; i < argc; i++) {
  1316. switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
  1317. case 't':
  1318. if(++i < argc) opt_title = argv[i];
  1319. break;
  1320. case 'e':
  1321. if(++i < argc) opt_cmd = argv[i];
  1322. break;
  1323. case 'v':
  1324. default:
  1325. die(USAGE);
  1326. }
  1327. }
  1328. setlocale(LC_CTYPE, "");
  1329. tnew(80, 24);
  1330. ttynew();
  1331. xinit();
  1332. selinit();
  1333. run();
  1334. return 0;
  1335. }