st.c 33 KB

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