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.

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
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 1049: /* = 1047 and 1048 */
  750. case 1047:
  751. if(IS_SET(MODE_ALTSCREEN)) {
  752. tclearregion(0, 0, term.col-1, term.row-1);
  753. tswapscreen();
  754. }
  755. if(escseq.arg[0] == 1047)
  756. break;
  757. case 1048:
  758. tcursor(CURSOR_LOAD);
  759. break;
  760. default:
  761. goto unknown;
  762. }
  763. } else {
  764. switch(escseq.arg[0]) {
  765. case 4:
  766. term.mode &= ~MODE_INSERT;
  767. break;
  768. default:
  769. goto unknown;
  770. }
  771. }
  772. break;
  773. case 'M': /* DL -- Delete <n> lines */
  774. DEFAULT(escseq.arg[0], 1);
  775. tdeleteline(escseq.arg[0]);
  776. break;
  777. case 'X': /* ECH -- Erase <n> char */
  778. DEFAULT(escseq.arg[0], 1);
  779. tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
  780. break;
  781. case 'P': /* DCH -- Delete <n> char */
  782. DEFAULT(escseq.arg[0], 1);
  783. tdeletechar(escseq.arg[0]);
  784. break;
  785. /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
  786. case 'd': /* VPA -- Move to <row> */
  787. DEFAULT(escseq.arg[0], 1);
  788. tmoveto(term.c.x, escseq.arg[0]-1);
  789. break;
  790. case 'h': /* SM -- Set terminal mode */
  791. if(escseq.priv) {
  792. switch(escseq.arg[0]) {
  793. case 1:
  794. term.mode |= MODE_APPKEYPAD;
  795. break;
  796. case 7:
  797. term.mode |= MODE_WRAP;
  798. break;
  799. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  800. break;
  801. case 25:
  802. term.c.state &= ~CURSOR_HIDE;
  803. break;
  804. case 1049: /* = 1047 and 1048 */
  805. case 1047:
  806. if(IS_SET(MODE_ALTSCREEN))
  807. tclearregion(0, 0, term.col-1, term.row-1);
  808. else
  809. tswapscreen();
  810. if(escseq.arg[0] == 1047)
  811. break;
  812. case 1048:
  813. tcursor(CURSOR_SAVE);
  814. break;
  815. default: goto unknown;
  816. }
  817. } else {
  818. switch(escseq.arg[0]) {
  819. case 4:
  820. term.mode |= MODE_INSERT;
  821. break;
  822. default: goto unknown;
  823. }
  824. };
  825. break;
  826. case 'm': /* SGR -- Terminal attribute (color) */
  827. tsetattr(escseq.arg, escseq.narg);
  828. break;
  829. case 'r': /* DECSTBM -- Set Scrolling Region */
  830. if(escseq.priv)
  831. goto unknown;
  832. else {
  833. DEFAULT(escseq.arg[0], 1);
  834. DEFAULT(escseq.arg[1], term.row);
  835. tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
  836. tmoveto(0, 0);
  837. }
  838. break;
  839. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  840. tcursor(CURSOR_SAVE);
  841. break;
  842. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  843. tcursor(CURSOR_LOAD);
  844. break;
  845. }
  846. }
  847. void
  848. csidump(void) {
  849. int i;
  850. printf("ESC [ %s", escseq.priv ? "? " : "");
  851. if(escseq.narg)
  852. for(i = 0; i < escseq.narg; i++)
  853. printf("%d ", escseq.arg[i]);
  854. if(escseq.mode)
  855. putchar(escseq.mode);
  856. putchar('\n');
  857. }
  858. void
  859. csireset(void) {
  860. memset(&escseq, 0, sizeof(escseq));
  861. }
  862. void
  863. tputtab(void) {
  864. int space = TAB - term.c.x % TAB;
  865. tmoveto(term.c.x + space, term.c.y);
  866. }
  867. void
  868. tputc(char c) {
  869. if(term.esc & ESC_START) {
  870. if(term.esc & ESC_CSI) {
  871. escseq.buf[escseq.len++] = c;
  872. if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
  873. term.esc = 0;
  874. csiparse(), csihandle();
  875. }
  876. } else if(term.esc & ESC_OSC) {
  877. if(c == ';') {
  878. term.titlelen = 0;
  879. term.esc = ESC_START | ESC_TITLE;
  880. }
  881. } else if(term.esc & ESC_TITLE) {
  882. if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
  883. term.esc = 0;
  884. term.title[term.titlelen] = '\0';
  885. XStoreName(xw.dis, xw.win, term.title);
  886. } else {
  887. term.title[term.titlelen++] = c;
  888. }
  889. } else if(term.esc & ESC_ALTCHARSET) {
  890. switch(c) {
  891. case '0': /* Line drawing crap */
  892. term.c.attr.mode |= ATTR_GFX;
  893. break;
  894. case 'B': /* Back to regular text */
  895. term.c.attr.mode &= ~ATTR_GFX;
  896. break;
  897. default:
  898. printf("esc unhandled charset: ESC ( %c\n", c);
  899. }
  900. term.esc = 0;
  901. } else {
  902. switch(c) {
  903. case '[':
  904. term.esc |= ESC_CSI;
  905. break;
  906. case ']':
  907. term.esc |= ESC_OSC;
  908. break;
  909. case '(':
  910. term.esc |= ESC_ALTCHARSET;
  911. break;
  912. case 'D': /* IND -- Linefeed */
  913. if(term.c.y == term.bot)
  914. tscrollup(1);
  915. else
  916. tmoveto(term.c.x, term.c.y+1);
  917. term.esc = 0;
  918. break;
  919. case 'E': /* NEL -- Next line */
  920. tnewline();
  921. term.esc = 0;
  922. break;
  923. case 'M': /* RI -- Reverse index */
  924. if(term.c.y == term.top)
  925. tscrolldown(1);
  926. else
  927. tmoveto(term.c.x, term.c.y-1);
  928. term.esc = 0;
  929. break;
  930. case 'c': /* RIS -- Reset to inital state */
  931. treset();
  932. term.esc = 0;
  933. break;
  934. case '=': /* DECPAM -- Application keypad */
  935. term.mode |= MODE_APPKEYPAD;
  936. term.esc = 0;
  937. break;
  938. case '>': /* DECPNM -- Normal keypad */
  939. term.mode &= ~MODE_APPKEYPAD;
  940. term.esc = 0;
  941. break;
  942. case '7': /* DECSC -- Save Cursor*/
  943. tcursor(CURSOR_SAVE);
  944. term.esc = 0;
  945. break;
  946. case '8': /* DECRC -- Restore Cursor */
  947. tcursor(CURSOR_LOAD);
  948. term.esc = 0;
  949. break;
  950. default:
  951. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
  952. term.esc = 0;
  953. }
  954. }
  955. } else {
  956. switch(c) {
  957. case '\t':
  958. tputtab();
  959. break;
  960. case '\b':
  961. tmoveto(term.c.x-1, term.c.y);
  962. break;
  963. case '\r':
  964. tmoveto(0, term.c.y);
  965. break;
  966. case '\n':
  967. tnewline();
  968. break;
  969. case '\a':
  970. if(!xw.hasfocus)
  971. xseturgency(1);
  972. break;
  973. case '\033':
  974. csireset();
  975. term.esc = ESC_START;
  976. break;
  977. default:
  978. if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
  979. tnewline();
  980. tsetchar(c);
  981. if(term.c.x+1 < term.col)
  982. tmoveto(term.c.x+1, term.c.y);
  983. else
  984. term.c.state |= CURSOR_WRAPNEXT;
  985. break;
  986. }
  987. }
  988. }
  989. void
  990. tputs(char *s, int len) {
  991. for(; len > 0; len--)
  992. tputc(*s++);
  993. }
  994. void
  995. tresize(int col, int row) {
  996. int i;
  997. int minrow = MIN(row, term.row);
  998. int mincol = MIN(col, term.col);
  999. if(col < 1 || row < 1)
  1000. return;
  1001. /* free uneeded rows */
  1002. for(i = row; i < term.row; i++) {
  1003. free(term.line[i]);
  1004. free(term.alt[i]);
  1005. }
  1006. /* resize to new height */
  1007. term.line = realloc(term.line, row * sizeof(Line));
  1008. term.alt = realloc(term.alt, row * sizeof(Line));
  1009. /* resize each row to new width, zero-pad if needed */
  1010. for(i = 0; i < minrow; i++) {
  1011. term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
  1012. term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
  1013. memset(term.line[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
  1014. memset(term.alt[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
  1015. }
  1016. /* allocate any new rows */
  1017. for(/* i == minrow */; i < row; i++) {
  1018. term.line[i] = calloc(col, sizeof(Glyph));
  1019. term.alt [i] = calloc(col, sizeof(Glyph));
  1020. }
  1021. /* update terminal size */
  1022. term.col = col, term.row = row;
  1023. /* make use of the LIMIT in tmoveto */
  1024. tmoveto(term.c.x, term.c.y);
  1025. /* reset scrolling region */
  1026. tsetscroll(0, row-1);
  1027. }
  1028. void
  1029. xloadcols(void) {
  1030. int i, r, g, b;
  1031. XColor color;
  1032. Colormap cmap = DefaultColormap(xw.dis, xw.scr);
  1033. unsigned long white = WhitePixel(xw.dis, xw.scr);
  1034. for(i = 0; i < 16; i++) {
  1035. if (!XAllocNamedColor(xw.dis, cmap, colorname[i], &color, &color)) {
  1036. dc.col[i] = white;
  1037. fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
  1038. } else
  1039. dc.col[i] = color.pixel;
  1040. }
  1041. /* same colors as xterm */
  1042. for(r = 0; r < 6; r++)
  1043. for(g = 0; g < 6; g++)
  1044. for(b = 0; b < 6; b++) {
  1045. color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
  1046. color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
  1047. color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
  1048. if (!XAllocColor(xw.dis, cmap, &color)) {
  1049. dc.col[i] = white;
  1050. fprintf(stderr, "Could not allocate color %d\n", i);
  1051. } else
  1052. dc.col[i] = color.pixel;
  1053. i++;
  1054. }
  1055. for(r = 0; r < 24; r++, i++) {
  1056. color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
  1057. if (!XAllocColor(xw.dis, cmap, &color)) {
  1058. dc.col[i] = white;
  1059. fprintf(stderr, "Could not allocate color %d\n", i);
  1060. } else
  1061. dc.col[i] = color.pixel;
  1062. }
  1063. }
  1064. void
  1065. xclear(int x1, int y1, int x2, int y2) {
  1066. XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
  1067. XFillRectangle(xw.dis, xw.buf, dc.gc,
  1068. x1 * xw.cw, y1 * xw.ch,
  1069. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
  1070. }
  1071. void
  1072. xhints(void)
  1073. {
  1074. XClassHint class = {TNAME, TNAME};
  1075. XWMHints wm = {.flags = InputHint, .input = 1};
  1076. XSizeHints size = {
  1077. .flags = PSize | PResizeInc | PBaseSize,
  1078. .height = xw.h,
  1079. .width = xw.w,
  1080. .height_inc = xw.ch,
  1081. .width_inc = xw.cw,
  1082. .base_height = 2*BORDER,
  1083. .base_width = 2*BORDER,
  1084. };
  1085. XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
  1086. }
  1087. void
  1088. xinit(void) {
  1089. if(!(xw.dis = XOpenDisplay(NULL)))
  1090. die("Can't open display\n");
  1091. xw.scr = XDefaultScreen(xw.dis);
  1092. /* font */
  1093. if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
  1094. die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
  1095. /* XXX: Assuming same size for bold font */
  1096. xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
  1097. xw.ch = dc.font->ascent + dc.font->descent;
  1098. /* colors */
  1099. xloadcols();
  1100. /* windows */
  1101. xw.h = term.row * xw.ch + 2*BORDER;
  1102. xw.w = term.col * xw.cw + 2*BORDER;
  1103. xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
  1104. xw.w, xw.h, 0,
  1105. dc.col[DefaultBG],
  1106. dc.col[DefaultBG]);
  1107. xw.bufw = xw.w - 2*BORDER;
  1108. xw.bufh = xw.h - 2*BORDER;
  1109. xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
  1110. xw.hasfocus = 1;
  1111. /* gc */
  1112. dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
  1113. XMapWindow(xw.dis, xw.win);
  1114. xhints();
  1115. XStoreName(xw.dis, xw.win, "st");
  1116. XSync(xw.dis, 0);
  1117. }
  1118. void
  1119. xdraws(char *s, Glyph base, int x, int y, int len) {
  1120. unsigned long xfg, xbg;
  1121. int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
  1122. int i;
  1123. if(base.mode & ATTR_REVERSE)
  1124. xfg = dc.col[base.bg], xbg = dc.col[base.fg];
  1125. else
  1126. xfg = dc.col[base.fg], xbg = dc.col[base.bg];
  1127. XSetBackground(xw.dis, dc.gc, xbg);
  1128. XSetForeground(xw.dis, dc.gc, xfg);
  1129. if(base.mode & ATTR_GFX)
  1130. for(i = 0; i < len; i++)
  1131. s[i] = gfx[(int)s[i]];
  1132. XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
  1133. XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
  1134. if(base.mode & ATTR_UNDERLINE)
  1135. XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
  1136. }
  1137. void
  1138. xdrawcursor(void) {
  1139. static int oldx = 0;
  1140. static int oldy = 0;
  1141. Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
  1142. LIMIT(oldx, 0, term.col-1);
  1143. LIMIT(oldy, 0, term.row-1);
  1144. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  1145. g.c = term.line[term.c.y][term.c.x].c;
  1146. /* remove the old cursor */
  1147. if(term.line[oldy][oldx].state & GLYPH_SET)
  1148. xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
  1149. else
  1150. xclear(oldx, oldy, oldx, oldy);
  1151. /* draw the new one */
  1152. if(!(term.c.state & CURSOR_HIDE) && xw.hasfocus) {
  1153. xdraws(&g.c, g, term.c.x, term.c.y, 1);
  1154. oldx = term.c.x, oldy = term.c.y;
  1155. }
  1156. }
  1157. #ifdef DEBUG
  1158. /* basic drawing routines */
  1159. void
  1160. xdrawc(int x, int y, Glyph g) {
  1161. XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
  1162. XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
  1163. XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
  1164. XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
  1165. XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
  1166. }
  1167. void
  1168. draw(int dummy) {
  1169. int x, y;
  1170. xclear(0, 0, term.col-1, term.row-1);
  1171. for(y = 0; y < term.row; y++)
  1172. for(x = 0; x < term.col; x++)
  1173. if(term.line[y][x].state & GLYPH_SET)
  1174. xdrawc(x, y, term.line[y][x]);
  1175. xdrawcursor();
  1176. XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1177. XFlush(xw.dis);
  1178. }
  1179. #else
  1180. /* optimized drawing routine */
  1181. void
  1182. draw(int redraw_all) {
  1183. int i, x, y, ox;
  1184. Glyph base, new;
  1185. char buf[DRAW_BUF_SIZ];
  1186. XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
  1187. XFillRectangle(xw.dis, xw.buf, dc.gc, 0, 0, xw.bufw, xw.bufh);
  1188. for(y = 0; y < term.row; y++) {
  1189. base = term.line[y][0];
  1190. i = ox = 0;
  1191. for(x = 0; x < term.col; x++) {
  1192. new = term.line[y][x];
  1193. if(selbx!=-1 && new.c && selected(x, y))
  1194. new.mode ^= ATTR_REVERSE;
  1195. if(i > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
  1196. i >= DRAW_BUF_SIZ)) {
  1197. xdraws(buf, base, ox, y, i);
  1198. i = 0;
  1199. }
  1200. if(new.state & GLYPH_SET) {
  1201. if(i == 0) {
  1202. ox = x;
  1203. base = new;
  1204. }
  1205. buf[i++] = new.c;
  1206. }
  1207. }
  1208. if(i > 0)
  1209. xdraws(buf, base, ox, y, i);
  1210. }
  1211. xdrawcursor();
  1212. XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1213. XFlush(xw.dis);
  1214. }
  1215. #endif
  1216. void
  1217. expose(XEvent *ev) {
  1218. draw(SCREEN_REDRAW);
  1219. }
  1220. void
  1221. xseturgency(int add) {
  1222. XWMHints *h = XGetWMHints(xw.dis, xw.win);
  1223. h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
  1224. XSetWMHints(xw.dis, xw.win, h);
  1225. XFree(h);
  1226. }
  1227. void
  1228. focus(XEvent *ev) {
  1229. if((xw.hasfocus = ev->type == FocusIn))
  1230. xseturgency(0);
  1231. draw(SCREEN_UPDATE);
  1232. }
  1233. char*
  1234. kmap(KeySym k) {
  1235. int i;
  1236. for(i = 0; i < LEN(key); i++)
  1237. if(key[i].k == k)
  1238. return (char*)key[i].s;
  1239. return NULL;
  1240. }
  1241. void
  1242. kpress(XEvent *ev) {
  1243. XKeyEvent *e = &ev->xkey;
  1244. KeySym ksym;
  1245. char buf[32];
  1246. char *customkey;
  1247. int len;
  1248. int meta;
  1249. int shift;
  1250. meta = e->state & Mod1Mask;
  1251. shift = e->state & ShiftMask;
  1252. len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
  1253. if((customkey = kmap(ksym)))
  1254. ttywrite(customkey, strlen(customkey));
  1255. else if(len > 0) {
  1256. buf[sizeof(buf)-1] = '\0';
  1257. if(meta && len == 1)
  1258. ttywrite("\033", 1);
  1259. ttywrite(buf, len);
  1260. } else
  1261. switch(ksym) {
  1262. case XK_Up:
  1263. case XK_Down:
  1264. case XK_Left:
  1265. case XK_Right:
  1266. sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', "DACB"[ksym - XK_Left]);
  1267. ttywrite(buf, 3);
  1268. break;
  1269. case XK_Insert:
  1270. if(shift)
  1271. draw(1), puts("draw!")/* XXX: paste X clipboard */;
  1272. break;
  1273. default:
  1274. fprintf(stderr, "errkey: %d\n", (int)ksym);
  1275. break;
  1276. }
  1277. }
  1278. void
  1279. resize(XEvent *e) {
  1280. int col, row;
  1281. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  1282. return;
  1283. xw.w = e->xconfigure.width;
  1284. xw.h = e->xconfigure.height;
  1285. xw.bufw = xw.w - 2*BORDER;
  1286. xw.bufh = xw.h - 2*BORDER;
  1287. col = xw.bufw / xw.cw;
  1288. row = xw.bufh / xw.ch;
  1289. tresize(col, row);
  1290. ttyresize(col, row);
  1291. XFreePixmap(xw.dis, xw.buf);
  1292. if(xw.bufh<1)
  1293. xw.bufh = 1;
  1294. if(xw.bufw<1)
  1295. xw.bufw = 1;
  1296. xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
  1297. draw(SCREEN_REDRAW);
  1298. }
  1299. void
  1300. run(void) {
  1301. XEvent ev;
  1302. fd_set rfd;
  1303. int xfd = XConnectionNumber(xw.dis);
  1304. long mask = ExposureMask | KeyPressMask | StructureNotifyMask | FocusChangeMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask;
  1305. XSelectInput(xw.dis, xw.win, mask);
  1306. XResizeWindow(xw.dis, xw.win, xw.w, xw.h); /* XXX: fix resize bug in wmii (?) */
  1307. for(;;) {
  1308. FD_ZERO(&rfd);
  1309. FD_SET(cmdfd, &rfd);
  1310. FD_SET(xfd, &rfd);
  1311. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
  1312. if(errno == EINTR)
  1313. continue;
  1314. die("select failed: %s\n", SERRNO);
  1315. }
  1316. if(FD_ISSET(cmdfd, &rfd)) {
  1317. ttyread();
  1318. draw(SCREEN_UPDATE);
  1319. }
  1320. while(XPending(xw.dis)) {
  1321. XNextEvent(xw.dis, &ev);
  1322. if(handler[ev.type])
  1323. (handler[ev.type])(&ev);
  1324. }
  1325. }
  1326. }
  1327. int
  1328. main(int argc, char *argv[]) {
  1329. if(argc == 2 && !strncmp("-v", argv[1], 3))
  1330. die("st-" VERSION ", (c) 2010 st engineers\n");
  1331. else if(argc != 1)
  1332. die("usage: st [-v]\n");
  1333. setlocale(LC_CTYPE, "");
  1334. tnew(80, 24);
  1335. ttynew();
  1336. xinit();
  1337. run();
  1338. return 0;
  1339. }