st.c 29 KB

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