st.c 34 KB

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