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.

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