st.c 29 KB

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