st.c 32 KB

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