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.

1484 lines
32 KiB

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