st.c 33 KB

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