st.c 32 KB

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