My build of suckless st terminal
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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