st.c 32 KB

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