st.c 30 KB

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