st.c 32 KB

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