st.c 33 KB

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