st.c 32 KB

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