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.

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