st.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  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 5: /* TODO: DECSCNM -- Remove reverse video */
  715. break;
  716. case 7:
  717. term.mode &= ~MODE_WRAP;
  718. break;
  719. case 12: /* att610 -- Stop blinking cursor (IGNORED) */
  720. break;
  721. case 25:
  722. term.c.state |= CURSOR_HIDE;
  723. break;
  724. case 1049: /* = 1047 and 1048 */
  725. case 1047:
  726. if(IS_SET(MODE_ALTSCREEN)) {
  727. tclearregion(0, 0, term.col-1, term.row-1);
  728. tswapscreen();
  729. }
  730. if(escseq.arg[0] == 1047)
  731. break;
  732. case 1048:
  733. tcursor(CURSOR_LOAD);
  734. break;
  735. default:
  736. goto unknown;
  737. }
  738. } else {
  739. switch(escseq.arg[0]) {
  740. case 4:
  741. term.mode &= ~MODE_INSERT;
  742. break;
  743. default:
  744. goto unknown;
  745. }
  746. }
  747. break;
  748. case 'M': /* DL -- Delete <n> lines */
  749. DEFAULT(escseq.arg[0], 1);
  750. tdeleteline(escseq.arg[0]);
  751. break;
  752. case 'X': /* ECH -- Erase <n> char */
  753. DEFAULT(escseq.arg[0], 1);
  754. tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
  755. break;
  756. case 'P': /* DCH -- Delete <n> char */
  757. DEFAULT(escseq.arg[0], 1);
  758. tdeletechar(escseq.arg[0]);
  759. break;
  760. /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
  761. case 'd': /* VPA -- Move to <row> */
  762. DEFAULT(escseq.arg[0], 1);
  763. tmoveto(term.c.x, escseq.arg[0]-1);
  764. break;
  765. case 'h': /* SM -- Set terminal mode */
  766. if(escseq.priv) {
  767. switch(escseq.arg[0]) {
  768. case 1:
  769. term.mode |= MODE_APPKEYPAD;
  770. break;
  771. case 5: /* DECSCNM -- Reverve video */
  772. /* TODO: set REVERSE on the whole screen (f) */
  773. break;
  774. case 7:
  775. term.mode |= MODE_WRAP;
  776. break;
  777. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  778. /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
  779. if(escseq.narg > 1 && escseq.arg[1] != 25)
  780. break;
  781. case 25:
  782. term.c.state &= ~CURSOR_HIDE;
  783. break;
  784. case 1049: /* = 1047 and 1048 */
  785. case 1047:
  786. if(IS_SET(MODE_ALTSCREEN))
  787. tclearregion(0, 0, term.col-1, term.row-1);
  788. else
  789. tswapscreen();
  790. if(escseq.arg[0] == 1047)
  791. break;
  792. case 1048:
  793. tcursor(CURSOR_SAVE);
  794. break;
  795. default: goto unknown;
  796. }
  797. } else {
  798. switch(escseq.arg[0]) {
  799. case 4:
  800. term.mode |= MODE_INSERT;
  801. break;
  802. default: goto unknown;
  803. }
  804. };
  805. break;
  806. case 'm': /* SGR -- Terminal attribute (color) */
  807. tsetattr(escseq.arg, escseq.narg);
  808. break;
  809. case 'r': /* DECSTBM -- Set Scrolling Region */
  810. if(escseq.priv)
  811. goto unknown;
  812. else {
  813. DEFAULT(escseq.arg[0], 1);
  814. DEFAULT(escseq.arg[1], term.row);
  815. tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
  816. tmoveto(0, 0);
  817. }
  818. break;
  819. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  820. tcursor(CURSOR_SAVE);
  821. break;
  822. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  823. tcursor(CURSOR_LOAD);
  824. break;
  825. }
  826. }
  827. void
  828. csidump(void) {
  829. int i;
  830. printf("ESC [ %s", escseq.priv ? "? " : "");
  831. if(escseq.narg)
  832. for(i = 0; i < escseq.narg; i++)
  833. printf("%d ", escseq.arg[i]);
  834. if(escseq.mode)
  835. putchar(escseq.mode);
  836. putchar('\n');
  837. }
  838. void
  839. csireset(void) {
  840. memset(&escseq, 0, sizeof(escseq));
  841. }
  842. void
  843. tputtab(void) {
  844. int space = TAB - term.c.x % TAB;
  845. tmoveto(term.c.x + space, term.c.y);
  846. }
  847. void
  848. tputc(char c) {
  849. if(term.esc & ESC_START) {
  850. if(term.esc & ESC_CSI) {
  851. escseq.buf[escseq.len++] = c;
  852. if(BETWEEN(c, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
  853. term.esc = 0;
  854. csiparse(), csihandle();
  855. }
  856. /* TODO: handle other OSC */
  857. } else if(term.esc & ESC_OSC) {
  858. if(c == ';') {
  859. term.titlelen = 0;
  860. term.esc = ESC_START | ESC_TITLE;
  861. }
  862. } else if(term.esc & ESC_TITLE) {
  863. if(c == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
  864. term.esc = 0;
  865. term.title[term.titlelen] = '\0';
  866. XStoreName(xw.dis, xw.win, term.title);
  867. } else {
  868. term.title[term.titlelen++] = c;
  869. }
  870. } else if(term.esc & ESC_ALTCHARSET) {
  871. switch(c) {
  872. case '0': /* Line drawing crap */
  873. term.c.attr.mode |= ATTR_GFX;
  874. break;
  875. case 'B': /* Back to regular text */
  876. term.c.attr.mode &= ~ATTR_GFX;
  877. break;
  878. default:
  879. printf("esc unhandled charset: ESC ( %c\n", c);
  880. }
  881. term.esc = 0;
  882. } else {
  883. switch(c) {
  884. case '[':
  885. term.esc |= ESC_CSI;
  886. break;
  887. case ']':
  888. term.esc |= ESC_OSC;
  889. break;
  890. case '(':
  891. term.esc |= ESC_ALTCHARSET;
  892. break;
  893. case 'D': /* IND -- Linefeed */
  894. if(term.c.y == term.bot)
  895. tscrollup(term.top, 1);
  896. else
  897. tmoveto(term.c.x, term.c.y+1);
  898. term.esc = 0;
  899. break;
  900. case 'E': /* NEL -- Next line */
  901. tnewline();
  902. term.esc = 0;
  903. break;
  904. case 'M': /* RI -- Reverse index */
  905. if(term.c.y == term.top)
  906. tscrolldown(term.top, 1);
  907. else
  908. tmoveto(term.c.x, term.c.y-1);
  909. term.esc = 0;
  910. break;
  911. case 'c': /* RIS -- Reset to inital state */
  912. treset();
  913. term.esc = 0;
  914. break;
  915. case '=': /* DECPAM -- Application keypad */
  916. term.mode |= MODE_APPKEYPAD;
  917. term.esc = 0;
  918. break;
  919. case '>': /* DECPNM -- Normal keypad */
  920. term.mode &= ~MODE_APPKEYPAD;
  921. term.esc = 0;
  922. break;
  923. case '7': /* DECSC -- Save Cursor */
  924. tcursor(CURSOR_SAVE);
  925. term.esc = 0;
  926. break;
  927. case '8': /* DECRC -- Restore Cursor */
  928. tcursor(CURSOR_LOAD);
  929. term.esc = 0;
  930. break;
  931. default:
  932. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n", c, isprint(c)?c:'.');
  933. term.esc = 0;
  934. }
  935. }
  936. } else {
  937. switch(c) {
  938. case '\t':
  939. tputtab();
  940. break;
  941. case '\b':
  942. tmoveto(term.c.x-1, term.c.y);
  943. break;
  944. case '\r':
  945. tmoveto(0, term.c.y);
  946. break;
  947. case '\n':
  948. tnewline();
  949. break;
  950. case '\a':
  951. if(!xw.hasfocus)
  952. xseturgency(1);
  953. break;
  954. case '\033':
  955. csireset();
  956. term.esc = ESC_START;
  957. break;
  958. default:
  959. if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
  960. tnewline();
  961. tsetchar(c);
  962. if(term.c.x+1 < term.col)
  963. tmoveto(term.c.x+1, term.c.y);
  964. else
  965. term.c.state |= CURSOR_WRAPNEXT;
  966. break;
  967. }
  968. }
  969. }
  970. void
  971. tputs(char *s, int len) {
  972. for(; len > 0; len--)
  973. tputc(*s++);
  974. }
  975. void
  976. tresize(int col, int row) {
  977. int i;
  978. int minrow = MIN(row, term.row);
  979. int mincol = MIN(col, term.col);
  980. int slide = term.c.y - row + 1;
  981. if(col < 1 || row < 1)
  982. return;
  983. /* free unneeded rows */
  984. i = 0;
  985. if(slide > 0) {
  986. /* slide screen to keep cursor where we expect it -
  987. * tscrollup would work here, but we can optimize to
  988. * memmove because we're freeing the earlier lines */
  989. for(/* i = 0 */; i < slide; i++) {
  990. free(term.line[i]);
  991. free(term.alt[i]);
  992. }
  993. memmove(term.line, term.line + slide, row * sizeof(Line));
  994. memmove(term.alt, term.alt + slide, row * sizeof(Line));
  995. }
  996. for(i += row; i < term.row; i++) {
  997. free(term.line[i]);
  998. free(term.alt[i]);
  999. }
  1000. /* resize to new height */
  1001. term.line = realloc(term.line, row * sizeof(Line));
  1002. term.alt = realloc(term.alt, row * sizeof(Line));
  1003. /* resize each row to new width, zero-pad if needed */
  1004. for(i = 0; i < minrow; i++) {
  1005. term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
  1006. term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
  1007. memset(term.line[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
  1008. memset(term.alt[i] + mincol, 0, (col - mincol) * sizeof(Glyph));
  1009. }
  1010. /* allocate any new rows */
  1011. for(/* i == minrow */; i < row; i++) {
  1012. term.line[i] = calloc(col, sizeof(Glyph));
  1013. term.alt [i] = calloc(col, sizeof(Glyph));
  1014. }
  1015. /* update terminal size */
  1016. term.col = col, term.row = row;
  1017. /* make use of the LIMIT in tmoveto */
  1018. tmoveto(term.c.x, term.c.y);
  1019. /* reset scrolling region */
  1020. tsetscroll(0, row-1);
  1021. }
  1022. void
  1023. xloadcols(void) {
  1024. int i, r, g, b;
  1025. XColor color;
  1026. Colormap cmap = DefaultColormap(xw.dis, xw.scr);
  1027. unsigned long white = WhitePixel(xw.dis, xw.scr);
  1028. for(i = 0; i < 16; i++) {
  1029. if (!XAllocNamedColor(xw.dis, cmap, colorname[i], &color, &color)) {
  1030. dc.col[i] = white;
  1031. fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
  1032. } else
  1033. dc.col[i] = color.pixel;
  1034. }
  1035. /* same colors as xterm */
  1036. for(r = 0; r < 6; r++)
  1037. for(g = 0; g < 6; g++)
  1038. for(b = 0; b < 6; b++) {
  1039. color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
  1040. color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
  1041. color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
  1042. if (!XAllocColor(xw.dis, cmap, &color)) {
  1043. dc.col[i] = white;
  1044. fprintf(stderr, "Could not allocate color %d\n", i);
  1045. } else
  1046. dc.col[i] = color.pixel;
  1047. i++;
  1048. }
  1049. for(r = 0; r < 24; r++, i++) {
  1050. color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
  1051. if (!XAllocColor(xw.dis, cmap, &color)) {
  1052. dc.col[i] = white;
  1053. fprintf(stderr, "Could not allocate color %d\n", i);
  1054. } else
  1055. dc.col[i] = color.pixel;
  1056. }
  1057. }
  1058. void
  1059. xclear(int x1, int y1, int x2, int y2) {
  1060. XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
  1061. XFillRectangle(xw.dis, xw.buf, dc.gc,
  1062. x1 * xw.cw, y1 * xw.ch,
  1063. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
  1064. }
  1065. void
  1066. xhints(void)
  1067. {
  1068. XClassHint class = {TNAME, TNAME};
  1069. XWMHints wm = {.flags = InputHint, .input = 1};
  1070. XSizeHints size = {
  1071. .flags = PSize | PResizeInc | PBaseSize,
  1072. .height = xw.h,
  1073. .width = xw.w,
  1074. .height_inc = xw.ch,
  1075. .width_inc = xw.cw,
  1076. .base_height = 2*BORDER,
  1077. .base_width = 2*BORDER,
  1078. };
  1079. XSetWMProperties(xw.dis, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
  1080. }
  1081. void
  1082. xinit(void) {
  1083. if(!(xw.dis = XOpenDisplay(NULL)))
  1084. die("Can't open display\n");
  1085. xw.scr = XDefaultScreen(xw.dis);
  1086. /* font */
  1087. if(!(dc.font = XLoadQueryFont(xw.dis, FONT)) || !(dc.bfont = XLoadQueryFont(xw.dis, BOLDFONT)))
  1088. die("Can't load font %s\n", dc.font ? BOLDFONT : FONT);
  1089. /* XXX: Assuming same size for bold font */
  1090. xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing;
  1091. xw.ch = dc.font->ascent + dc.font->descent;
  1092. /* colors */
  1093. xloadcols();
  1094. /* windows */
  1095. xw.bufh = term.row * xw.ch;
  1096. xw.bufw = term.col * xw.cw;
  1097. xw.h = xw.bufh + 2*BORDER;
  1098. xw.w = xw.bufw + 2*BORDER;
  1099. xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0,
  1100. xw.w, xw.h, 0,
  1101. dc.col[DefaultBG],
  1102. dc.col[DefaultBG]);
  1103. xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
  1104. /* gc */
  1105. dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
  1106. /* event mask */
  1107. XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask
  1108. | StructureNotifyMask | FocusChangeMask | PointerMotionMask
  1109. | ButtonPressMask | ButtonReleaseMask);
  1110. XMapWindow(xw.dis, xw.win);
  1111. xhints();
  1112. XStoreName(xw.dis, xw.win, opt_title ? opt_title : "st");
  1113. XSync(xw.dis, 0);
  1114. }
  1115. void
  1116. xdraws(char *s, Glyph base, int x, int y, int len) {
  1117. unsigned long xfg, xbg;
  1118. int winx = x*xw.cw, winy = y*xw.ch + dc.font->ascent, width = len*xw.cw;
  1119. int i;
  1120. if(base.mode & ATTR_REVERSE)
  1121. xfg = dc.col[base.bg], xbg = dc.col[base.fg];
  1122. else
  1123. xfg = dc.col[base.fg], xbg = dc.col[base.bg];
  1124. XSetBackground(xw.dis, dc.gc, xbg);
  1125. XSetForeground(xw.dis, dc.gc, xfg);
  1126. if(base.mode & ATTR_GFX)
  1127. for(i = 0; i < len; i++) {
  1128. char c = gfx[(unsigned int)s[i] % 256];
  1129. if(c)
  1130. s[i] = c;
  1131. else if(s[i] > 0x5f)
  1132. s[i] -= 0x5f;
  1133. }
  1134. XSetFont(xw.dis, dc.gc, base.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
  1135. XDrawImageString(xw.dis, xw.buf, dc.gc, winx, winy, s, len);
  1136. if(base.mode & ATTR_UNDERLINE)
  1137. XDrawLine(xw.dis, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
  1138. }
  1139. void
  1140. xdrawcursor(void) {
  1141. static int oldx = 0;
  1142. static int oldy = 0;
  1143. Glyph g = {' ', ATTR_NULL, DefaultBG, DefaultCS, 0};
  1144. LIMIT(oldx, 0, term.col-1);
  1145. LIMIT(oldy, 0, term.row-1);
  1146. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  1147. g.c = term.line[term.c.y][term.c.x].c;
  1148. /* remove the old cursor */
  1149. if(term.line[oldy][oldx].state & GLYPH_SET)
  1150. xdraws(&term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1);
  1151. else
  1152. xclear(oldx, oldy, oldx, oldy);
  1153. /* draw the new one */
  1154. if(!(term.c.state & CURSOR_HIDE) && xw.hasfocus) {
  1155. xdraws(&g.c, g, term.c.x, term.c.y, 1);
  1156. oldx = term.c.x, oldy = term.c.y;
  1157. }
  1158. }
  1159. #ifdef DEBUG
  1160. /* basic drawing routines */
  1161. void
  1162. xdrawc(int x, int y, Glyph g) {
  1163. XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch };
  1164. XSetBackground(xw.dis, dc.gc, dc.col[g.bg]);
  1165. XSetForeground(xw.dis, dc.gc, dc.col[g.fg]);
  1166. XSetFont(xw.dis, dc.gc, g.mode & ATTR_BOLD ? dc.bfont->fid : dc.font->fid);
  1167. XDrawImageString(xw.dis, xw.buf, dc.gc, r.x, r.y+dc.font->ascent, &g.c, 1);
  1168. }
  1169. void
  1170. draw(int dummy) {
  1171. int x, y;
  1172. xclear(0, 0, term.col-1, term.row-1);
  1173. for(y = 0; y < term.row; y++)
  1174. for(x = 0; x < term.col; x++)
  1175. if(term.line[y][x].state & GLYPH_SET)
  1176. xdrawc(x, y, term.line[y][x]);
  1177. xdrawcursor();
  1178. XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1179. XFlush(xw.dis);
  1180. }
  1181. #else
  1182. /* optimized drawing routine */
  1183. void
  1184. draw(int redraw_all) {
  1185. int i, x, y, ox;
  1186. Glyph base, new;
  1187. char buf[DRAW_BUF_SIZ];
  1188. XSetForeground(xw.dis, dc.gc, dc.col[DefaultBG]);
  1189. XFillRectangle(xw.dis, xw.buf, dc.gc, 0, 0, xw.bufw, xw.bufh);
  1190. for(y = 0; y < term.row; y++) {
  1191. base = term.line[y][0];
  1192. i = ox = 0;
  1193. for(x = 0; x < term.col; x++) {
  1194. new = term.line[y][x];
  1195. if(sel.bx!=-1 && new.c && selected(x, y))
  1196. new.mode ^= ATTR_REVERSE;
  1197. if(i > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
  1198. i >= DRAW_BUF_SIZ)) {
  1199. xdraws(buf, base, ox, y, i);
  1200. i = 0;
  1201. }
  1202. if(new.state & GLYPH_SET) {
  1203. if(i == 0) {
  1204. ox = x;
  1205. base = new;
  1206. }
  1207. buf[i++] = new.c;
  1208. }
  1209. }
  1210. if(i > 0)
  1211. xdraws(buf, base, ox, y, i);
  1212. }
  1213. xdrawcursor();
  1214. XCopyArea(xw.dis, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1215. XFlush(xw.dis);
  1216. }
  1217. #endif
  1218. void
  1219. expose(XEvent *ev) {
  1220. draw(SCREEN_REDRAW);
  1221. }
  1222. void
  1223. xseturgency(int add) {
  1224. XWMHints *h = XGetWMHints(xw.dis, xw.win);
  1225. h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
  1226. XSetWMHints(xw.dis, xw.win, h);
  1227. XFree(h);
  1228. }
  1229. void
  1230. focus(XEvent *ev) {
  1231. if((xw.hasfocus = ev->type == FocusIn))
  1232. xseturgency(0);
  1233. draw(SCREEN_UPDATE);
  1234. }
  1235. char*
  1236. kmap(KeySym k) {
  1237. int i;
  1238. for(i = 0; i < LEN(key); i++)
  1239. if(key[i].k == k)
  1240. return (char*)key[i].s;
  1241. return NULL;
  1242. }
  1243. void
  1244. kpress(XEvent *ev) {
  1245. XKeyEvent *e = &ev->xkey;
  1246. KeySym ksym;
  1247. char buf[32];
  1248. char *customkey;
  1249. int len;
  1250. int meta;
  1251. int shift;
  1252. meta = e->state & Mod1Mask;
  1253. shift = e->state & ShiftMask;
  1254. len = XLookupString(e, buf, sizeof(buf), &ksym, NULL);
  1255. if((customkey = kmap(ksym)))
  1256. ttywrite(customkey, strlen(customkey));
  1257. else if(len > 0) {
  1258. buf[sizeof(buf)-1] = '\0';
  1259. if(meta && len == 1)
  1260. ttywrite("\033", 1);
  1261. ttywrite(buf, len);
  1262. } else
  1263. switch(ksym) {
  1264. case XK_Up:
  1265. case XK_Down:
  1266. case XK_Left:
  1267. case XK_Right:
  1268. sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', "DACB"[ksym - XK_Left]);
  1269. ttywrite(buf, 3);
  1270. break;
  1271. case XK_Insert:
  1272. if(shift)
  1273. selpaste(), draw(1);
  1274. break;
  1275. default:
  1276. fprintf(stderr, "errkey: %d\n", (int)ksym);
  1277. break;
  1278. }
  1279. }
  1280. void
  1281. resize(XEvent *e) {
  1282. int col, row;
  1283. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  1284. return;
  1285. xw.w = e->xconfigure.width;
  1286. xw.h = e->xconfigure.height;
  1287. xw.bufw = xw.w - 2*BORDER;
  1288. xw.bufh = xw.h - 2*BORDER;
  1289. col = xw.bufw / xw.cw;
  1290. row = xw.bufh / xw.ch;
  1291. tresize(col, row);
  1292. ttyresize(col, row);
  1293. xw.bufh = MAX(1, xw.bufh);
  1294. xw.bufw = MAX(1, xw.bufw);
  1295. XFreePixmap(xw.dis, xw.buf);
  1296. xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
  1297. draw(SCREEN_REDRAW);
  1298. }
  1299. void
  1300. run(void) {
  1301. XEvent ev;
  1302. fd_set rfd;
  1303. int xfd = XConnectionNumber(xw.dis);
  1304. for(;;) {
  1305. FD_ZERO(&rfd);
  1306. FD_SET(cmdfd, &rfd);
  1307. FD_SET(xfd, &rfd);
  1308. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
  1309. if(errno == EINTR)
  1310. continue;
  1311. die("select failed: %s\n", SERRNO);
  1312. }
  1313. if(FD_ISSET(cmdfd, &rfd)) {
  1314. ttyread();
  1315. draw(SCREEN_UPDATE);
  1316. }
  1317. while(XPending(xw.dis)) {
  1318. XNextEvent(xw.dis, &ev);
  1319. if(handler[ev.type])
  1320. (handler[ev.type])(&ev);
  1321. }
  1322. }
  1323. }
  1324. int
  1325. main(int argc, char *argv[]) {
  1326. int i;
  1327. for(i = 1; i < argc; i++) {
  1328. switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
  1329. case 't':
  1330. if(++i < argc) opt_title = argv[i];
  1331. break;
  1332. case 'e':
  1333. if(++i < argc) opt_cmd = argv[i];
  1334. break;
  1335. case 'v':
  1336. default:
  1337. die(USAGE);
  1338. }
  1339. }
  1340. setlocale(LC_CTYPE, "");
  1341. tnew(80, 24);
  1342. ttynew();
  1343. xinit();
  1344. selinit();
  1345. run();
  1346. return 0;
  1347. }