st.c 30 KB

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