st.c 32 KB

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