st.c 30 KB

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