st.c 30 KB

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