st.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466
  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 <stdbool.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <signal.h>
  14. #include <sys/ioctl.h>
  15. #include <sys/select.h>
  16. #include <sys/stat.h>
  17. #include <sys/time.h>
  18. #include <sys/types.h>
  19. #include <sys/wait.h>
  20. #include <time.h>
  21. #include <unistd.h>
  22. #include <X11/Xatom.h>
  23. #include <X11/Xlib.h>
  24. #include <X11/Xutil.h>
  25. #include <X11/cursorfont.h>
  26. #include <X11/keysym.h>
  27. #include <X11/extensions/Xdbe.h>
  28. #include <X11/Xft/Xft.h>
  29. #define Glyph Glyph_
  30. #define Font Font_
  31. #if defined(__linux)
  32. #include <pty.h>
  33. #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  34. #include <util.h>
  35. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  36. #include <libutil.h>
  37. #endif
  38. #define USAGE \
  39. "st " VERSION " (c) 2010-2012 st engineers\n" \
  40. "usage: st [-t title] [-c class] [-g geometry]" \
  41. " [-w windowid] [-v] [-f file] [-e command...]\n"
  42. /* XEMBED messages */
  43. #define XEMBED_FOCUS_IN 4
  44. #define XEMBED_FOCUS_OUT 5
  45. /* Arbitrary sizes */
  46. #define ESC_BUF_SIZ 256
  47. #define ESC_ARG_SIZ 16
  48. #define STR_BUF_SIZ 256
  49. #define STR_ARG_SIZ 16
  50. #define DRAW_BUF_SIZ 20*1024
  51. #define UTF_SIZ 4
  52. #define XK_NO_MOD UINT_MAX
  53. #define XK_ANY_MOD 0
  54. #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
  55. #define SERRNO strerror(errno)
  56. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  57. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  58. #define LEN(a) (sizeof(a) / sizeof(a[0]))
  59. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  60. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  61. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  62. #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
  63. #define IS_SET(flag) (term.mode & (flag))
  64. #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
  65. #define X2COL(x) (((x) - BORDER)/xw.cw)
  66. #define Y2ROW(y) (((y) - BORDER)/xw.ch)
  67. enum glyph_attribute {
  68. ATTR_NULL = 0,
  69. ATTR_REVERSE = 1,
  70. ATTR_UNDERLINE = 2,
  71. ATTR_BOLD = 4,
  72. ATTR_GFX = 8,
  73. ATTR_ITALIC = 16,
  74. ATTR_BLINK = 32,
  75. };
  76. enum cursor_movement {
  77. CURSOR_UP,
  78. CURSOR_DOWN,
  79. CURSOR_LEFT,
  80. CURSOR_RIGHT,
  81. CURSOR_SAVE,
  82. CURSOR_LOAD
  83. };
  84. enum cursor_state {
  85. CURSOR_DEFAULT = 0,
  86. CURSOR_HIDE = 1,
  87. CURSOR_WRAPNEXT = 2
  88. };
  89. enum glyph_state {
  90. GLYPH_SET = 1,
  91. GLYPH_DIRTY = 2
  92. };
  93. enum term_mode {
  94. MODE_WRAP = 1,
  95. MODE_INSERT = 2,
  96. MODE_APPKEYPAD = 4,
  97. MODE_ALTSCREEN = 8,
  98. MODE_CRLF = 16,
  99. MODE_MOUSEBTN = 32,
  100. MODE_MOUSEMOTION = 64,
  101. MODE_MOUSE = 32|64,
  102. MODE_REVERSE = 128,
  103. MODE_KBDLOCK = 256
  104. };
  105. enum escape_state {
  106. ESC_START = 1,
  107. ESC_CSI = 2,
  108. ESC_STR = 4, /* DSC, OSC, PM, APC */
  109. ESC_ALTCHARSET = 8,
  110. ESC_STR_END = 16, /* a final string was encountered */
  111. };
  112. enum window_state {
  113. WIN_VISIBLE = 1,
  114. WIN_REDRAW = 2,
  115. WIN_FOCUSED = 4
  116. };
  117. /* bit macro */
  118. #undef B0
  119. enum { B0=1, B1=2, B2=4, B3=8, B4=16, B5=32, B6=64, B7=128 };
  120. typedef unsigned char uchar;
  121. typedef unsigned int uint;
  122. typedef unsigned long ulong;
  123. typedef unsigned short ushort;
  124. typedef struct {
  125. char c[UTF_SIZ]; /* character code */
  126. uchar mode; /* attribute flags */
  127. ushort fg; /* foreground */
  128. ushort bg; /* background */
  129. uchar state; /* state flags */
  130. } Glyph;
  131. typedef Glyph* Line;
  132. typedef struct {
  133. Glyph attr; /* current char attributes */
  134. int x;
  135. int y;
  136. char state;
  137. } TCursor;
  138. /* CSI Escape sequence structs */
  139. /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
  140. typedef struct {
  141. char buf[ESC_BUF_SIZ]; /* raw string */
  142. int len; /* raw string length */
  143. char priv;
  144. int arg[ESC_ARG_SIZ];
  145. int narg; /* nb of args */
  146. char mode;
  147. } CSIEscape;
  148. /* STR Escape sequence structs */
  149. /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
  150. typedef struct {
  151. char type; /* ESC type ... */
  152. char buf[STR_BUF_SIZ]; /* raw string */
  153. int len; /* raw string length */
  154. char *args[STR_ARG_SIZ];
  155. int narg; /* nb of args */
  156. } STREscape;
  157. /* Internal representation of the screen */
  158. typedef struct {
  159. int row; /* nb row */
  160. int col; /* nb col */
  161. Line* line; /* screen */
  162. Line* alt; /* alternate screen */
  163. bool* dirty; /* dirtyness of lines */
  164. TCursor c; /* cursor */
  165. int top; /* top scroll limit */
  166. int bot; /* bottom scroll limit */
  167. int mode; /* terminal mode flags */
  168. int esc; /* escape state flags */
  169. bool *tabs;
  170. } Term;
  171. /* Purely graphic info */
  172. typedef struct {
  173. Display* dpy;
  174. Colormap cmap;
  175. Window win;
  176. XdbeBackBuffer buf;
  177. Atom xembed;
  178. XIM xim;
  179. XIC xic;
  180. XftDraw *xft_draw;
  181. Visual *vis;
  182. int scr;
  183. bool isfixed; /* is fixed geometry? */
  184. int fx, fy, fw, fh; /* fixed geometry */
  185. int tw, th; /* tty width and height */
  186. int w; /* window width */
  187. int h; /* window height */
  188. int ch; /* char height */
  189. int cw; /* char width */
  190. char state; /* focus, redraw, visible */
  191. } XWindow;
  192. typedef struct {
  193. KeySym k;
  194. uint mask;
  195. char s[ESC_BUF_SIZ];
  196. } Key;
  197. /* TODO: use better name for vars... */
  198. typedef struct {
  199. int mode;
  200. int bx, by;
  201. int ex, ey;
  202. struct {int x, y;} b, e;
  203. char *clip;
  204. Atom xtarget;
  205. bool alt;
  206. struct timeval tclick1;
  207. struct timeval tclick2;
  208. } Selection;
  209. #include "config.h"
  210. /* Font structure */
  211. typedef struct {
  212. int ascent;
  213. int descent;
  214. short lbearing;
  215. short rbearing;
  216. XFontSet set;
  217. XftFont* xft_set;
  218. } Font;
  219. /* Drawing Context */
  220. typedef struct {
  221. ulong col[LEN(colorname) < 256 ? 256 : LEN(colorname)];
  222. XftColor xft_col[LEN(colorname) < 256 ? 256 : LEN(colorname)];
  223. GC gc;
  224. Font font, bfont, ifont, ibfont;
  225. } DC;
  226. static void die(const char*, ...);
  227. static void draw(void);
  228. static void redraw(void);
  229. static void drawregion(int, int, int, int);
  230. static void execsh(void);
  231. static void sigchld(int);
  232. static void run(void);
  233. static void csidump(void);
  234. static void csihandle(void);
  235. static void csiparse(void);
  236. static void csireset(void);
  237. static void strdump(void);
  238. static void strhandle(void);
  239. static void strparse(void);
  240. static void strreset(void);
  241. static void tclearregion(int, int, int, int);
  242. static void tcursor(int);
  243. static void tdeletechar(int);
  244. static void tdeleteline(int);
  245. static void tinsertblank(int);
  246. static void tinsertblankline(int);
  247. static void tmoveto(int, int);
  248. static void tnew(int, int);
  249. static void tnewline(int);
  250. static void tputtab(bool);
  251. static void tputc(char*);
  252. static void treset(void);
  253. static int tresize(int, int);
  254. static void tscrollup(int, int);
  255. static void tscrolldown(int, int);
  256. static void tsetattr(int*, int);
  257. static void tsetchar(char*);
  258. static void tsetscroll(int, int);
  259. static void tswapscreen(void);
  260. static void tsetdirt(int, int);
  261. static void tsetmode(bool, bool, int *, int);
  262. static void tfulldirt(void);
  263. static void ttynew(void);
  264. static void ttyread(void);
  265. static void ttyresize(void);
  266. static void ttywrite(const char *, size_t);
  267. static void xdraws(char *, Glyph, int, int, int, int);
  268. static void xhints(void);
  269. static void xclear(int, int, int, int);
  270. static void xdrawcursor(void);
  271. static void xinit(void);
  272. static void xloadcols(void);
  273. static void xresettitle(void);
  274. static void xseturgency(int);
  275. static void xsetsel(char*);
  276. static void xresize(int, int);
  277. static void expose(XEvent *);
  278. static void visibility(XEvent *);
  279. static void unmap(XEvent *);
  280. static char* kmap(KeySym, uint);
  281. static void kpress(XEvent *);
  282. static void cmessage(XEvent *);
  283. static void resize(XEvent *);
  284. static void focus(XEvent *);
  285. static void brelease(XEvent *);
  286. static void bpress(XEvent *);
  287. static void bmotion(XEvent *);
  288. static void selnotify(XEvent *);
  289. static void selclear(XEvent *);
  290. static void selrequest(XEvent *);
  291. static void selinit(void);
  292. static inline bool selected(int, int);
  293. static void selcopy(void);
  294. static void selpaste(void);
  295. static void selscroll(int, int);
  296. static int utf8decode(char *, long *);
  297. static int utf8encode(long *, char *);
  298. static int utf8size(char *);
  299. static int isfullutf8(char *, int);
  300. static void *xmalloc(size_t);
  301. static void *xrealloc(void *, size_t);
  302. static void *xcalloc(size_t nmemb, size_t size);
  303. static void (*handler[LASTEvent])(XEvent *) = {
  304. [KeyPress] = kpress,
  305. [ClientMessage] = cmessage,
  306. [ConfigureNotify] = resize,
  307. [VisibilityNotify] = visibility,
  308. [UnmapNotify] = unmap,
  309. [Expose] = expose,
  310. [FocusIn] = focus,
  311. [FocusOut] = focus,
  312. [MotionNotify] = bmotion,
  313. [ButtonPress] = bpress,
  314. [ButtonRelease] = brelease,
  315. [SelectionClear] = selclear,
  316. [SelectionNotify] = selnotify,
  317. [SelectionRequest] = selrequest,
  318. };
  319. /* Globals */
  320. static DC dc;
  321. static XWindow xw;
  322. static Term term;
  323. static CSIEscape csiescseq;
  324. static STREscape strescseq;
  325. static int cmdfd;
  326. static pid_t pid;
  327. static Selection sel;
  328. static int iofd = -1;
  329. static char **opt_cmd = NULL;
  330. static char *opt_io = NULL;
  331. static char *opt_title = NULL;
  332. static char *opt_embed = NULL;
  333. static char *opt_class = NULL;
  334. void *
  335. xmalloc(size_t len) {
  336. void *p = malloc(len);
  337. if(!p)
  338. die("Out of memory\n");
  339. return p;
  340. }
  341. void *
  342. xrealloc(void *p, size_t len) {
  343. if((p = realloc(p, len)) == NULL)
  344. die("Out of memory\n");
  345. return p;
  346. }
  347. void *
  348. xcalloc(size_t nmemb, size_t size) {
  349. void *p = calloc(nmemb, size);
  350. if(!p)
  351. die("Out of memory\n");
  352. return p;
  353. }
  354. int
  355. utf8decode(char *s, long *u) {
  356. uchar c;
  357. int i, n, rtn;
  358. rtn = 1;
  359. c = *s;
  360. if(~c & B7) { /* 0xxxxxxx */
  361. *u = c;
  362. return rtn;
  363. } else if((c & (B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
  364. *u = c&(B4|B3|B2|B1|B0);
  365. n = 1;
  366. } else if((c & (B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
  367. *u = c&(B3|B2|B1|B0);
  368. n = 2;
  369. } else if((c & (B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
  370. *u = c & (B2|B1|B0);
  371. n = 3;
  372. } else
  373. goto invalid;
  374. for(i = n, ++s; i > 0; --i, ++rtn, ++s) {
  375. c = *s;
  376. if((c & (B7|B6)) != B7) /* 10xxxxxx */
  377. goto invalid;
  378. *u <<= 6;
  379. *u |= c & (B5|B4|B3|B2|B1|B0);
  380. }
  381. if((n == 1 && *u < 0x80) ||
  382. (n == 2 && *u < 0x800) ||
  383. (n == 3 && *u < 0x10000) ||
  384. (*u >= 0xD800 && *u <= 0xDFFF))
  385. goto invalid;
  386. return rtn;
  387. invalid:
  388. *u = 0xFFFD;
  389. return rtn;
  390. }
  391. int
  392. utf8encode(long *u, char *s) {
  393. uchar *sp;
  394. ulong uc;
  395. int i, n;
  396. sp = (uchar*) s;
  397. uc = *u;
  398. if(uc < 0x80) {
  399. *sp = uc; /* 0xxxxxxx */
  400. return 1;
  401. } else if(*u < 0x800) {
  402. *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
  403. n = 1;
  404. } else if(uc < 0x10000) {
  405. *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
  406. n = 2;
  407. } else if(uc <= 0x10FFFF) {
  408. *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
  409. n = 3;
  410. } else {
  411. goto invalid;
  412. }
  413. for(i=n,++sp; i>0; --i,++sp)
  414. *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
  415. return n+1;
  416. invalid:
  417. /* U+FFFD */
  418. *s++ = '\xEF';
  419. *s++ = '\xBF';
  420. *s = '\xBD';
  421. return 3;
  422. }
  423. /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
  424. UTF-8 otherwise return 0 */
  425. int
  426. isfullutf8(char *s, int b) {
  427. uchar *c1, *c2, *c3;
  428. c1 = (uchar *) s;
  429. c2 = (uchar *) ++s;
  430. c3 = (uchar *) ++s;
  431. if(b < 1)
  432. return 0;
  433. else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1)
  434. return 0;
  435. else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
  436. ((b == 1) ||
  437. ((b == 2) && (*c2&(B7|B6)) == B7)))
  438. return 0;
  439. else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
  440. ((b == 1) ||
  441. ((b == 2) && (*c2&(B7|B6)) == B7) ||
  442. ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7)))
  443. return 0;
  444. else
  445. return 1;
  446. }
  447. int
  448. utf8size(char *s) {
  449. uchar c = *s;
  450. if(~c&B7)
  451. return 1;
  452. else if((c&(B7|B6|B5)) == (B7|B6))
  453. return 2;
  454. else if((c&(B7|B6|B5|B4)) == (B7|B6|B5))
  455. return 3;
  456. else
  457. return 4;
  458. }
  459. void
  460. selinit(void) {
  461. memset(&sel.tclick1, 0, sizeof(sel.tclick1));
  462. memset(&sel.tclick2, 0, sizeof(sel.tclick2));
  463. sel.mode = 0;
  464. sel.bx = -1;
  465. sel.clip = NULL;
  466. sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
  467. if(sel.xtarget == None)
  468. sel.xtarget = XA_STRING;
  469. }
  470. static inline bool
  471. selected(int x, int y) {
  472. if(sel.ey == y && sel.by == y) {
  473. int bx = MIN(sel.bx, sel.ex);
  474. int ex = MAX(sel.bx, sel.ex);
  475. return BETWEEN(x, bx, ex);
  476. }
  477. return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x))
  478. || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y));
  479. }
  480. void
  481. getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
  482. if(b)
  483. *b = e->xbutton.button;
  484. *x = X2COL(e->xbutton.x);
  485. *y = Y2ROW(e->xbutton.y);
  486. sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
  487. sel.b.y = MIN(sel.by, sel.ey);
  488. sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
  489. sel.e.y = MAX(sel.by, sel.ey);
  490. }
  491. void
  492. mousereport(XEvent *e) {
  493. int x = X2COL(e->xbutton.x);
  494. int y = Y2ROW(e->xbutton.y);
  495. int button = e->xbutton.button;
  496. int state = e->xbutton.state;
  497. char buf[] = { '\033', '[', 'M', 0, 32+x+1, 32+y+1 };
  498. static int ob, ox, oy;
  499. /* from urxvt */
  500. if(e->xbutton.type == MotionNotify) {
  501. if(!IS_SET(MODE_MOUSEMOTION) || (x == ox && y == oy))
  502. return;
  503. button = ob + 32;
  504. ox = x, oy = y;
  505. } else if(e->xbutton.type == ButtonRelease || button == AnyButton) {
  506. button = 3;
  507. } else {
  508. button -= Button1;
  509. if(button >= 3)
  510. button += 64 - 3;
  511. if(e->xbutton.type == ButtonPress) {
  512. ob = button;
  513. ox = x, oy = y;
  514. }
  515. }
  516. buf[3] = 32 + button + (state & ShiftMask ? 4 : 0)
  517. + (state & Mod4Mask ? 8 : 0)
  518. + (state & ControlMask ? 16 : 0);
  519. ttywrite(buf, sizeof(buf));
  520. }
  521. void
  522. bpress(XEvent *e) {
  523. if(IS_SET(MODE_MOUSE))
  524. mousereport(e);
  525. else if(e->xbutton.button == Button1) {
  526. if(sel.bx != -1) {
  527. sel.bx = -1;
  528. tsetdirt(sel.b.y, sel.e.y);
  529. draw();
  530. }
  531. sel.mode = 1;
  532. sel.ex = sel.bx = X2COL(e->xbutton.x);
  533. sel.ey = sel.by = Y2ROW(e->xbutton.y);
  534. }
  535. }
  536. void
  537. selcopy(void) {
  538. char *str, *ptr;
  539. int x, y, bufsize, is_selected = 0;
  540. if(sel.bx == -1)
  541. str = NULL;
  542. else {
  543. bufsize = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
  544. ptr = str = xmalloc(bufsize);
  545. /* append every set & selected glyph to the selection */
  546. for(y = 0; y < term.row; y++) {
  547. for(x = 0; x < term.col; x++) {
  548. int size;
  549. char *p;
  550. Glyph *gp = &term.line[y][x];
  551. if(!(is_selected = selected(x, y)))
  552. continue;
  553. p = (gp->state & GLYPH_SET) ? gp->c : " ";
  554. size = utf8size(p);
  555. memcpy(ptr, p, size);
  556. ptr += size;
  557. }
  558. /* \n at the end of every selected line except for the last one */
  559. if(is_selected && y < sel.e.y)
  560. *ptr++ = '\n';
  561. }
  562. *ptr = 0;
  563. }
  564. sel.alt = IS_SET(MODE_ALTSCREEN);
  565. xsetsel(str);
  566. }
  567. void
  568. selnotify(XEvent *e) {
  569. ulong nitems, ofs, rem;
  570. int format;
  571. uchar *data;
  572. Atom type;
  573. ofs = 0;
  574. do {
  575. if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
  576. False, AnyPropertyType, &type, &format,
  577. &nitems, &rem, &data)) {
  578. fprintf(stderr, "Clipboard allocation failed\n");
  579. return;
  580. }
  581. ttywrite((const char *) data, nitems * format / 8);
  582. XFree(data);
  583. /* number of 32-bit chunks returned */
  584. ofs += nitems * format / 32;
  585. } while(rem > 0);
  586. }
  587. void
  588. selpaste() {
  589. XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY, xw.win, CurrentTime);
  590. }
  591. void selclear(XEvent *e) {
  592. if(sel.bx == -1)
  593. return;
  594. sel.bx = -1;
  595. tsetdirt(sel.b.y, sel.e.y);
  596. }
  597. void
  598. selrequest(XEvent *e) {
  599. XSelectionRequestEvent *xsre;
  600. XSelectionEvent xev;
  601. Atom xa_targets;
  602. xsre = (XSelectionRequestEvent *) e;
  603. xev.type = SelectionNotify;
  604. xev.requestor = xsre->requestor;
  605. xev.selection = xsre->selection;
  606. xev.target = xsre->target;
  607. xev.time = xsre->time;
  608. /* reject */
  609. xev.property = None;
  610. xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
  611. if(xsre->target == xa_targets) {
  612. /* respond with the supported type */
  613. Atom string = sel.xtarget;
  614. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  615. XA_ATOM, 32, PropModeReplace,
  616. (uchar *) &string, 1);
  617. xev.property = xsre->property;
  618. } else if(xsre->target == sel.xtarget && sel.clip != NULL) {
  619. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  620. xsre->target, 8, PropModeReplace,
  621. (uchar *) sel.clip, strlen(sel.clip));
  622. xev.property = xsre->property;
  623. }
  624. /* all done, send a notification to the listener */
  625. if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
  626. fprintf(stderr, "Error sending SelectionNotify event\n");
  627. }
  628. void
  629. xsetsel(char *str) {
  630. /* register the selection for both the clipboard and the primary */
  631. Atom clipboard;
  632. free(sel.clip);
  633. sel.clip = str;
  634. XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
  635. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  636. XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
  637. }
  638. void
  639. brelease(XEvent *e) {
  640. if(IS_SET(MODE_MOUSE)) {
  641. mousereport(e);
  642. return;
  643. }
  644. if(e->xbutton.button == Button2)
  645. selpaste();
  646. else if(e->xbutton.button == Button1) {
  647. sel.mode = 0;
  648. getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
  649. term.dirty[sel.ey] = 1;
  650. if(sel.bx == sel.ex && sel.by == sel.ey) {
  651. struct timeval now;
  652. sel.bx = -1;
  653. gettimeofday(&now, NULL);
  654. if(TIMEDIFF(now, sel.tclick2) <= TRIPLECLICK_TIMEOUT) {
  655. /* triple click on the line */
  656. sel.b.x = sel.bx = 0;
  657. sel.e.x = sel.ex = term.col;
  658. sel.b.y = sel.e.y = sel.ey;
  659. selcopy();
  660. } else if(TIMEDIFF(now, sel.tclick1) <= DOUBLECLICK_TIMEOUT) {
  661. /* double click to select word */
  662. sel.bx = sel.ex;
  663. while(sel.bx > 0 && term.line[sel.ey][sel.bx-1].state & GLYPH_SET &&
  664. term.line[sel.ey][sel.bx-1].c[0] != ' ') sel.bx--;
  665. sel.b.x = sel.bx;
  666. while(sel.ex < term.col-1 && term.line[sel.ey][sel.ex+1].state & GLYPH_SET &&
  667. term.line[sel.ey][sel.ex+1].c[0] != ' ') sel.ex++;
  668. sel.e.x = sel.ex;
  669. sel.b.y = sel.e.y = sel.ey;
  670. selcopy();
  671. }
  672. } else
  673. selcopy();
  674. }
  675. memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
  676. gettimeofday(&sel.tclick1, NULL);
  677. }
  678. void
  679. bmotion(XEvent *e) {
  680. if(IS_SET(MODE_MOUSE)) {
  681. mousereport(e);
  682. return;
  683. }
  684. if(sel.mode) {
  685. int oldey = sel.ey, oldex = sel.ex;
  686. getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
  687. if(oldey != sel.ey || oldex != sel.ex) {
  688. int starty = MIN(oldey, sel.ey);
  689. int endy = MAX(oldey, sel.ey);
  690. tsetdirt(starty, endy);
  691. }
  692. }
  693. }
  694. void
  695. die(const char *errstr, ...) {
  696. va_list ap;
  697. va_start(ap, errstr);
  698. vfprintf(stderr, errstr, ap);
  699. va_end(ap);
  700. exit(EXIT_FAILURE);
  701. }
  702. void
  703. execsh(void) {
  704. char **args;
  705. char *envshell = getenv("SHELL");
  706. unsetenv("COLUMNS");
  707. unsetenv("LINES");
  708. unsetenv("TERMCAP");
  709. signal(SIGCHLD, SIG_DFL);
  710. signal(SIGHUP, SIG_DFL);
  711. signal(SIGINT, SIG_DFL);
  712. signal(SIGQUIT, SIG_DFL);
  713. signal(SIGTERM, SIG_DFL);
  714. signal(SIGALRM, SIG_DFL);
  715. DEFAULT(envshell, SHELL);
  716. putenv("TERM="TNAME);
  717. args = opt_cmd ? opt_cmd : (char*[]){envshell, "-i", NULL};
  718. execvp(args[0], args);
  719. exit(EXIT_FAILURE);
  720. }
  721. void
  722. sigchld(int a) {
  723. int stat = 0;
  724. if(waitpid(pid, &stat, 0) < 0)
  725. die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
  726. if(WIFEXITED(stat))
  727. exit(WEXITSTATUS(stat));
  728. else
  729. exit(EXIT_FAILURE);
  730. }
  731. void
  732. ttynew(void) {
  733. int m, s;
  734. /* seems to work fine on linux, openbsd and freebsd */
  735. struct winsize w = {term.row, term.col, 0, 0};
  736. if(openpty(&m, &s, NULL, NULL, &w) < 0)
  737. die("openpty failed: %s\n", SERRNO);
  738. switch(pid = fork()) {
  739. case -1:
  740. die("fork failed\n");
  741. break;
  742. case 0:
  743. setsid(); /* create a new process group */
  744. dup2(s, STDIN_FILENO);
  745. dup2(s, STDOUT_FILENO);
  746. dup2(s, STDERR_FILENO);
  747. if(ioctl(s, TIOCSCTTY, NULL) < 0)
  748. die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
  749. close(s);
  750. close(m);
  751. execsh();
  752. break;
  753. default:
  754. close(s);
  755. cmdfd = m;
  756. signal(SIGCHLD, sigchld);
  757. if(opt_io) {
  758. if(!strcmp(opt_io, "-")) {
  759. iofd = STDOUT_FILENO;
  760. } else {
  761. if((iofd = open(opt_io, O_WRONLY | O_CREAT, 0666)) < 0) {
  762. fprintf(stderr, "Error opening %s:%s\n",
  763. opt_io, strerror(errno));
  764. }
  765. }
  766. }
  767. }
  768. }
  769. void
  770. dump(char c) {
  771. static int col;
  772. fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
  773. if(++col % 10 == 0)
  774. fprintf(stderr, "\n");
  775. }
  776. void
  777. ttyread(void) {
  778. static char buf[BUFSIZ];
  779. static int buflen = 0;
  780. char *ptr;
  781. char s[UTF_SIZ];
  782. int charsize; /* size of utf8 char in bytes */
  783. long utf8c;
  784. int ret;
  785. /* append read bytes to unprocessed bytes */
  786. if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
  787. die("Couldn't read from shell: %s\n", SERRNO);
  788. /* process every complete utf8 char */
  789. buflen += ret;
  790. ptr = buf;
  791. while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
  792. charsize = utf8decode(ptr, &utf8c);
  793. utf8encode(&utf8c, s);
  794. tputc(s);
  795. ptr += charsize;
  796. buflen -= charsize;
  797. }
  798. /* keep any uncomplete utf8 char for the next call */
  799. memmove(buf, ptr, buflen);
  800. }
  801. void
  802. ttywrite(const char *s, size_t n) {
  803. if(write(cmdfd, s, n) == -1)
  804. die("write error on tty: %s\n", SERRNO);
  805. }
  806. void
  807. ttyresize(void) {
  808. struct winsize w;
  809. w.ws_row = term.row;
  810. w.ws_col = term.col;
  811. w.ws_xpixel = xw.tw;
  812. w.ws_ypixel = xw.th;
  813. if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  814. fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
  815. }
  816. void
  817. tsetdirt(int top, int bot)
  818. {
  819. int i;
  820. LIMIT(top, 0, term.row-1);
  821. LIMIT(bot, 0, term.row-1);
  822. for(i = top; i <= bot; i++)
  823. term.dirty[i] = 1;
  824. }
  825. void
  826. tfulldirt(void)
  827. {
  828. tsetdirt(0, term.row-1);
  829. }
  830. void
  831. tcursor(int mode) {
  832. static TCursor c;
  833. if(mode == CURSOR_SAVE)
  834. c = term.c;
  835. else if(mode == CURSOR_LOAD)
  836. term.c = c, tmoveto(c.x, c.y);
  837. }
  838. void
  839. treset(void) {
  840. uint i;
  841. term.c = (TCursor){{
  842. .mode = ATTR_NULL,
  843. .fg = DefaultFG,
  844. .bg = DefaultBG
  845. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  846. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  847. for(i = TAB; i < term.col; i += TAB)
  848. term.tabs[i] = 1;
  849. term.top = 0, term.bot = term.row - 1;
  850. term.mode = MODE_WRAP;
  851. xclear(0, 0, xw.w, xw.h);
  852. tclearregion(0, 0, term.col-1, term.row-1);
  853. }
  854. void
  855. tnew(int col, int row) {
  856. /* set screen size */
  857. term.row = row, term.col = col;
  858. term.line = xmalloc(term.row * sizeof(Line));
  859. term.alt = xmalloc(term.row * sizeof(Line));
  860. term.dirty = xmalloc(term.row * sizeof(*term.dirty));
  861. term.tabs = xmalloc(term.col * sizeof(*term.tabs));
  862. for(row = 0; row < term.row; row++) {
  863. term.line[row] = xmalloc(term.col * sizeof(Glyph));
  864. term.alt [row] = xmalloc(term.col * sizeof(Glyph));
  865. term.dirty[row] = 0;
  866. }
  867. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  868. /* setup screen */
  869. treset();
  870. }
  871. void
  872. tswapscreen(void) {
  873. Line* tmp = term.line;
  874. term.line = term.alt;
  875. term.alt = tmp;
  876. term.mode ^= MODE_ALTSCREEN;
  877. tfulldirt();
  878. }
  879. void
  880. tscrolldown(int orig, int n) {
  881. int i;
  882. Line temp;
  883. LIMIT(n, 0, term.bot-orig+1);
  884. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  885. for(i = term.bot; i >= orig+n; i--) {
  886. temp = term.line[i];
  887. term.line[i] = term.line[i-n];
  888. term.line[i-n] = temp;
  889. term.dirty[i] = 1;
  890. term.dirty[i-n] = 1;
  891. }
  892. selscroll(orig, n);
  893. }
  894. void
  895. tscrollup(int orig, int n) {
  896. int i;
  897. Line temp;
  898. LIMIT(n, 0, term.bot-orig+1);
  899. tclearregion(0, orig, term.col-1, orig+n-1);
  900. for(i = orig; i <= term.bot-n; i++) {
  901. temp = term.line[i];
  902. term.line[i] = term.line[i+n];
  903. term.line[i+n] = temp;
  904. term.dirty[i] = 1;
  905. term.dirty[i+n] = 1;
  906. }
  907. selscroll(orig, -n);
  908. }
  909. void
  910. selscroll(int orig, int n) {
  911. if(sel.bx == -1)
  912. return;
  913. if(BETWEEN(sel.by, orig, term.bot) || BETWEEN(sel.ey, orig, term.bot)) {
  914. if((sel.by += n) > term.bot || (sel.ey += n) < term.top) {
  915. sel.bx = -1;
  916. return;
  917. }
  918. if(sel.by < term.top) {
  919. sel.by = term.top;
  920. sel.bx = 0;
  921. }
  922. if(sel.ey > term.bot) {
  923. sel.ey = term.bot;
  924. sel.ex = term.col;
  925. }
  926. sel.b.y = sel.by, sel.b.x = sel.bx;
  927. sel.e.y = sel.ey, sel.e.x = sel.ex;
  928. }
  929. }
  930. void
  931. tnewline(int first_col) {
  932. int y = term.c.y;
  933. if(y == term.bot)
  934. tscrollup(term.top, 1);
  935. else
  936. y++;
  937. tmoveto(first_col ? 0 : term.c.x, y);
  938. }
  939. void
  940. csiparse(void) {
  941. /* int noarg = 1; */
  942. char *p = csiescseq.buf;
  943. csiescseq.narg = 0;
  944. if(*p == '?')
  945. csiescseq.priv = 1, p++;
  946. while(p < csiescseq.buf+csiescseq.len) {
  947. while(isdigit(*p)) {
  948. csiescseq.arg[csiescseq.narg] *= 10;
  949. csiescseq.arg[csiescseq.narg] += *p++ - '0'/*, noarg = 0 */;
  950. }
  951. if(*p == ';' && csiescseq.narg+1 < ESC_ARG_SIZ)
  952. csiescseq.narg++, p++;
  953. else {
  954. csiescseq.mode = *p;
  955. csiescseq.narg++;
  956. return;
  957. }
  958. }
  959. }
  960. void
  961. tmoveto(int x, int y) {
  962. LIMIT(x, 0, term.col-1);
  963. LIMIT(y, 0, term.row-1);
  964. term.c.state &= ~CURSOR_WRAPNEXT;
  965. term.c.x = x;
  966. term.c.y = y;
  967. }
  968. void
  969. tsetchar(char *c) {
  970. term.dirty[term.c.y] = 1;
  971. term.line[term.c.y][term.c.x] = term.c.attr;
  972. memcpy(term.line[term.c.y][term.c.x].c, c, UTF_SIZ);
  973. term.line[term.c.y][term.c.x].state |= GLYPH_SET;
  974. }
  975. void
  976. tclearregion(int x1, int y1, int x2, int y2) {
  977. int x, y, temp;
  978. if(x1 > x2)
  979. temp = x1, x1 = x2, x2 = temp;
  980. if(y1 > y2)
  981. temp = y1, y1 = y2, y2 = temp;
  982. LIMIT(x1, 0, term.col-1);
  983. LIMIT(x2, 0, term.col-1);
  984. LIMIT(y1, 0, term.row-1);
  985. LIMIT(y2, 0, term.row-1);
  986. for(y = y1; y <= y2; y++) {
  987. term.dirty[y] = 1;
  988. for(x = x1; x <= x2; x++)
  989. term.line[y][x].state = 0;
  990. }
  991. }
  992. void
  993. tdeletechar(int n) {
  994. int src = term.c.x + n;
  995. int dst = term.c.x;
  996. int size = term.col - src;
  997. term.dirty[term.c.y] = 1;
  998. if(src >= term.col) {
  999. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1000. return;
  1001. }
  1002. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  1003. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  1004. }
  1005. void
  1006. tinsertblank(int n) {
  1007. int src = term.c.x;
  1008. int dst = src + n;
  1009. int size = term.col - dst;
  1010. term.dirty[term.c.y] = 1;
  1011. if(dst >= term.col) {
  1012. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1013. return;
  1014. }
  1015. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  1016. tclearregion(src, term.c.y, dst - 1, term.c.y);
  1017. }
  1018. void
  1019. tinsertblankline(int n) {
  1020. if(term.c.y < term.top || term.c.y > term.bot)
  1021. return;
  1022. tscrolldown(term.c.y, n);
  1023. }
  1024. void
  1025. tdeleteline(int n) {
  1026. if(term.c.y < term.top || term.c.y > term.bot)
  1027. return;
  1028. tscrollup(term.c.y, n);
  1029. }
  1030. void
  1031. tsetattr(int *attr, int l) {
  1032. int i;
  1033. for(i = 0; i < l; i++) {
  1034. switch(attr[i]) {
  1035. case 0:
  1036. term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD \
  1037. | ATTR_ITALIC | ATTR_BLINK);
  1038. term.c.attr.fg = DefaultFG;
  1039. term.c.attr.bg = DefaultBG;
  1040. break;
  1041. case 1:
  1042. term.c.attr.mode |= ATTR_BOLD;
  1043. break;
  1044. case 3: /* enter standout (highlight) */
  1045. term.c.attr.mode |= ATTR_ITALIC;
  1046. break;
  1047. case 4:
  1048. term.c.attr.mode |= ATTR_UNDERLINE;
  1049. break;
  1050. case 5:
  1051. term.c.attr.mode |= ATTR_BLINK;
  1052. break;
  1053. case 7:
  1054. term.c.attr.mode |= ATTR_REVERSE;
  1055. break;
  1056. case 21:
  1057. case 22:
  1058. term.c.attr.mode &= ~ATTR_BOLD;
  1059. break;
  1060. case 23: /* leave standout (highlight) mode */
  1061. term.c.attr.mode &= ~ATTR_ITALIC;
  1062. break;
  1063. case 24:
  1064. term.c.attr.mode &= ~ATTR_UNDERLINE;
  1065. break;
  1066. case 25:
  1067. term.c.attr.mode &= ~ATTR_BLINK;
  1068. break;
  1069. case 27:
  1070. term.c.attr.mode &= ~ATTR_REVERSE;
  1071. break;
  1072. case 38:
  1073. if(i + 2 < l && attr[i + 1] == 5) {
  1074. i += 2;
  1075. if(BETWEEN(attr[i], 0, 255))
  1076. term.c.attr.fg = attr[i];
  1077. else
  1078. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
  1079. }
  1080. else
  1081. fprintf(stderr, "erresc(38): gfx attr %d unknown\n", attr[i]);
  1082. break;
  1083. case 39:
  1084. term.c.attr.fg = DefaultFG;
  1085. break;
  1086. case 48:
  1087. if(i + 2 < l && attr[i + 1] == 5) {
  1088. i += 2;
  1089. if(BETWEEN(attr[i], 0, 255))
  1090. term.c.attr.bg = attr[i];
  1091. else
  1092. fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
  1093. }
  1094. else
  1095. fprintf(stderr, "erresc(48): gfx attr %d unknown\n", attr[i]);
  1096. break;
  1097. case 49:
  1098. term.c.attr.bg = DefaultBG;
  1099. break;
  1100. default:
  1101. if(BETWEEN(attr[i], 30, 37))
  1102. term.c.attr.fg = attr[i] - 30;
  1103. else if(BETWEEN(attr[i], 40, 47))
  1104. term.c.attr.bg = attr[i] - 40;
  1105. else if(BETWEEN(attr[i], 90, 97))
  1106. term.c.attr.fg = attr[i] - 90 + 8;
  1107. else if(BETWEEN(attr[i], 100, 107))
  1108. term.c.attr.bg = attr[i] - 100 + 8;
  1109. else
  1110. fprintf(stderr, "erresc(default): gfx attr %d unknown\n", attr[i]), csidump();
  1111. break;
  1112. }
  1113. }
  1114. }
  1115. void
  1116. tsetscroll(int t, int b) {
  1117. int temp;
  1118. LIMIT(t, 0, term.row-1);
  1119. LIMIT(b, 0, term.row-1);
  1120. if(t > b) {
  1121. temp = t;
  1122. t = b;
  1123. b = temp;
  1124. }
  1125. term.top = t;
  1126. term.bot = b;
  1127. }
  1128. #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
  1129. void
  1130. tsetmode(bool priv, bool set, int *args, int narg) {
  1131. int *lim, mode;
  1132. for(lim = args + narg; args < lim; ++args) {
  1133. if(priv) {
  1134. switch(*args) {
  1135. break;
  1136. case 1: /* DECCKM -- Cursor key */
  1137. MODBIT(term.mode, set, MODE_APPKEYPAD);
  1138. break;
  1139. case 5: /* DECSCNM -- Reverve video */
  1140. mode = term.mode;
  1141. MODBIT(term.mode,set, MODE_REVERSE);
  1142. if(mode != term.mode)
  1143. redraw();
  1144. break;
  1145. case 6: /* XXX: DECOM -- Origin */
  1146. break;
  1147. case 7: /* DECAWM -- Auto wrap */
  1148. MODBIT(term.mode, set, MODE_WRAP);
  1149. break;
  1150. case 8: /* XXX: DECARM -- Auto repeat */
  1151. break;
  1152. case 0: /* Error (IGNORED) */
  1153. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1154. break;
  1155. case 25:
  1156. MODBIT(term.c.state, !set, CURSOR_HIDE);
  1157. break;
  1158. case 1000: /* 1000,1002: enable xterm mouse report */
  1159. MODBIT(term.mode, set, MODE_MOUSEBTN);
  1160. break;
  1161. case 1002:
  1162. MODBIT(term.mode, set, MODE_MOUSEMOTION);
  1163. break;
  1164. case 1049: /* = 1047 and 1048 */
  1165. case 47:
  1166. case 1047:
  1167. if(IS_SET(MODE_ALTSCREEN))
  1168. tclearregion(0, 0, term.col-1, term.row-1);
  1169. if((set && !IS_SET(MODE_ALTSCREEN)) ||
  1170. (!set && IS_SET(MODE_ALTSCREEN))) {
  1171. tswapscreen();
  1172. }
  1173. if(*args != 1049)
  1174. break;
  1175. /* pass through */
  1176. case 1048:
  1177. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1178. break;
  1179. default:
  1180. /* case 2: DECANM -- ANSI/VT52 (NOT SUPPOURTED) */
  1181. /* case 3: DECCOLM -- Column (NOT SUPPORTED) */
  1182. /* case 4: DECSCLM -- Scroll (NOT SUPPORTED) */
  1183. /* case 18: DECPFF -- Printer feed (NOT SUPPORTED) */
  1184. /* case 19: DECPEX -- Printer extent (NOT SUPPORTED) */
  1185. /* case 42: DECNRCM -- National characters (NOT SUPPORTED) */
  1186. fprintf(stderr,
  1187. "erresc: unknown private set/reset mode %d\n",
  1188. *args);
  1189. break;
  1190. }
  1191. } else {
  1192. switch(*args) {
  1193. case 0: /* Error (IGNORED) */
  1194. break;
  1195. case 2: /* KAM -- keyboard action */
  1196. MODBIT(term.mode, set, MODE_KBDLOCK);
  1197. break;
  1198. case 4: /* IRM -- Insertion-replacement */
  1199. MODBIT(term.mode, set, MODE_INSERT);
  1200. break;
  1201. case 12: /* XXX: SRM -- Send/Receive */
  1202. break;
  1203. case 20: /* LNM -- Linefeed/new line */
  1204. MODBIT(term.mode, set, MODE_CRLF);
  1205. break;
  1206. default:
  1207. fprintf(stderr,
  1208. "erresc: unknown set/reset mode %d\n",
  1209. *args);
  1210. break;
  1211. }
  1212. }
  1213. }
  1214. }
  1215. #undef MODBIT
  1216. void
  1217. csihandle(void) {
  1218. switch(csiescseq.mode) {
  1219. default:
  1220. unknown:
  1221. fprintf(stderr, "erresc: unknown csi ");
  1222. csidump();
  1223. /* die(""); */
  1224. break;
  1225. case '@': /* ICH -- Insert <n> blank char */
  1226. DEFAULT(csiescseq.arg[0], 1);
  1227. tinsertblank(csiescseq.arg[0]);
  1228. break;
  1229. case 'A': /* CUU -- Cursor <n> Up */
  1230. case 'e':
  1231. DEFAULT(csiescseq.arg[0], 1);
  1232. tmoveto(term.c.x, term.c.y-csiescseq.arg[0]);
  1233. break;
  1234. case 'B': /* CUD -- Cursor <n> Down */
  1235. DEFAULT(csiescseq.arg[0], 1);
  1236. tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
  1237. break;
  1238. case 'C': /* CUF -- Cursor <n> Forward */
  1239. case 'a':
  1240. DEFAULT(csiescseq.arg[0], 1);
  1241. tmoveto(term.c.x+csiescseq.arg[0], term.c.y);
  1242. break;
  1243. case 'D': /* CUB -- Cursor <n> Backward */
  1244. DEFAULT(csiescseq.arg[0], 1);
  1245. tmoveto(term.c.x-csiescseq.arg[0], term.c.y);
  1246. break;
  1247. case 'E': /* CNL -- Cursor <n> Down and first col */
  1248. DEFAULT(csiescseq.arg[0], 1);
  1249. tmoveto(0, term.c.y+csiescseq.arg[0]);
  1250. break;
  1251. case 'F': /* CPL -- Cursor <n> Up and first col */
  1252. DEFAULT(csiescseq.arg[0], 1);
  1253. tmoveto(0, term.c.y-csiescseq.arg[0]);
  1254. break;
  1255. case 'g': /* TBC -- Tabulation clear */
  1256. switch (csiescseq.arg[0]) {
  1257. case 0: /* clear current tab stop */
  1258. term.tabs[term.c.x] = 0;
  1259. break;
  1260. case 3: /* clear all the tabs */
  1261. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  1262. break;
  1263. default:
  1264. goto unknown;
  1265. }
  1266. break;
  1267. case 'G': /* CHA -- Move to <col> */
  1268. case '`': /* HPA */
  1269. DEFAULT(csiescseq.arg[0], 1);
  1270. tmoveto(csiescseq.arg[0]-1, term.c.y);
  1271. break;
  1272. case 'H': /* CUP -- Move to <row> <col> */
  1273. case 'f': /* HVP */
  1274. DEFAULT(csiescseq.arg[0], 1);
  1275. DEFAULT(csiescseq.arg[1], 1);
  1276. tmoveto(csiescseq.arg[1]-1, csiescseq.arg[0]-1);
  1277. break;
  1278. case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
  1279. DEFAULT(csiescseq.arg[0], 1);
  1280. while(csiescseq.arg[0]--)
  1281. tputtab(1);
  1282. break;
  1283. case 'J': /* ED -- Clear screen */
  1284. sel.bx = -1;
  1285. switch(csiescseq.arg[0]) {
  1286. case 0: /* below */
  1287. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1288. if(term.c.y < term.row-1)
  1289. tclearregion(0, term.c.y+1, term.col-1, term.row-1);
  1290. break;
  1291. case 1: /* above */
  1292. if(term.c.y > 1)
  1293. tclearregion(0, 0, term.col-1, term.c.y-1);
  1294. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1295. break;
  1296. case 2: /* all */
  1297. tclearregion(0, 0, term.col-1, term.row-1);
  1298. break;
  1299. default:
  1300. goto unknown;
  1301. }
  1302. break;
  1303. case 'K': /* EL -- Clear line */
  1304. switch(csiescseq.arg[0]) {
  1305. case 0: /* right */
  1306. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1307. break;
  1308. case 1: /* left */
  1309. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1310. break;
  1311. case 2: /* all */
  1312. tclearregion(0, term.c.y, term.col-1, term.c.y);
  1313. break;
  1314. }
  1315. break;
  1316. case 'S': /* SU -- Scroll <n> line up */
  1317. DEFAULT(csiescseq.arg[0], 1);
  1318. tscrollup(term.top, csiescseq.arg[0]);
  1319. break;
  1320. case 'T': /* SD -- Scroll <n> line down */
  1321. DEFAULT(csiescseq.arg[0], 1);
  1322. tscrolldown(term.top, csiescseq.arg[0]);
  1323. break;
  1324. case 'L': /* IL -- Insert <n> blank lines */
  1325. DEFAULT(csiescseq.arg[0], 1);
  1326. tinsertblankline(csiescseq.arg[0]);
  1327. break;
  1328. case 'l': /* RM -- Reset Mode */
  1329. tsetmode(csiescseq.priv, 0, csiescseq.arg, csiescseq.narg);
  1330. break;
  1331. case 'M': /* DL -- Delete <n> lines */
  1332. DEFAULT(csiescseq.arg[0], 1);
  1333. tdeleteline(csiescseq.arg[0]);
  1334. break;
  1335. case 'X': /* ECH -- Erase <n> char */
  1336. DEFAULT(csiescseq.arg[0], 1);
  1337. tclearregion(term.c.x, term.c.y, term.c.x + csiescseq.arg[0], term.c.y);
  1338. break;
  1339. case 'P': /* DCH -- Delete <n> char */
  1340. DEFAULT(csiescseq.arg[0], 1);
  1341. tdeletechar(csiescseq.arg[0]);
  1342. break;
  1343. case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
  1344. DEFAULT(csiescseq.arg[0], 1);
  1345. while(csiescseq.arg[0]--)
  1346. tputtab(0);
  1347. break;
  1348. case 'd': /* VPA -- Move to <row> */
  1349. DEFAULT(csiescseq.arg[0], 1);
  1350. tmoveto(term.c.x, csiescseq.arg[0]-1);
  1351. break;
  1352. case 'h': /* SM -- Set terminal mode */
  1353. tsetmode(csiescseq.priv, 1, csiescseq.arg, csiescseq.narg);
  1354. break;
  1355. case 'm': /* SGR -- Terminal attribute (color) */
  1356. tsetattr(csiescseq.arg, csiescseq.narg);
  1357. break;
  1358. case 'r': /* DECSTBM -- Set Scrolling Region */
  1359. if(csiescseq.priv)
  1360. goto unknown;
  1361. else {
  1362. DEFAULT(csiescseq.arg[0], 1);
  1363. DEFAULT(csiescseq.arg[1], term.row);
  1364. tsetscroll(csiescseq.arg[0]-1, csiescseq.arg[1]-1);
  1365. tmoveto(0, 0);
  1366. }
  1367. break;
  1368. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  1369. tcursor(CURSOR_SAVE);
  1370. break;
  1371. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  1372. tcursor(CURSOR_LOAD);
  1373. break;
  1374. }
  1375. }
  1376. void
  1377. csidump(void) {
  1378. int i;
  1379. printf("ESC[");
  1380. for(i = 0; i < csiescseq.len; i++) {
  1381. uint c = csiescseq.buf[i] & 0xff;
  1382. if(isprint(c)) putchar(c);
  1383. else if(c == '\n') printf("(\\n)");
  1384. else if(c == '\r') printf("(\\r)");
  1385. else if(c == 0x1b) printf("(\\e)");
  1386. else printf("(%02x)", c);
  1387. }
  1388. putchar('\n');
  1389. }
  1390. void
  1391. csireset(void) {
  1392. memset(&csiescseq, 0, sizeof(csiescseq));
  1393. }
  1394. void
  1395. strhandle(void) {
  1396. char *p;
  1397. /*
  1398. * TODO: make this being useful in case of color palette change.
  1399. */
  1400. strparse();
  1401. p = strescseq.buf;
  1402. switch(strescseq.type) {
  1403. case ']': /* OSC -- Operating System Command */
  1404. switch(p[0]) {
  1405. case '0':
  1406. case '1':
  1407. case '2':
  1408. /*
  1409. * TODO: Handle special chars in string, like umlauts.
  1410. */
  1411. if(p[1] == ';') {
  1412. XStoreName(xw.dpy, xw.win, strescseq.buf+2);
  1413. }
  1414. break;
  1415. case ';':
  1416. XStoreName(xw.dpy, xw.win, strescseq.buf+1);
  1417. break;
  1418. case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
  1419. break;
  1420. default:
  1421. fprintf(stderr, "erresc: unknown str ");
  1422. strdump();
  1423. break;
  1424. }
  1425. break;
  1426. case 'k': /* old title set compatibility */
  1427. XStoreName(xw.dpy, xw.win, strescseq.buf);
  1428. break;
  1429. case 'P': /* DSC -- Device Control String */
  1430. case '_': /* APC -- Application Program Command */
  1431. case '^': /* PM -- Privacy Message */
  1432. default:
  1433. fprintf(stderr, "erresc: unknown str ");
  1434. strdump();
  1435. /* die(""); */
  1436. break;
  1437. }
  1438. }
  1439. void
  1440. strparse(void) {
  1441. /*
  1442. * TODO: Implement parsing like for CSI when required.
  1443. * Format: ESC type cmd ';' arg0 [';' argn] ESC \
  1444. */
  1445. return;
  1446. }
  1447. void
  1448. strdump(void) {
  1449. int i;
  1450. printf("ESC%c", strescseq.type);
  1451. for(i = 0; i < strescseq.len; i++) {
  1452. uint c = strescseq.buf[i] & 0xff;
  1453. if(isprint(c)) putchar(c);
  1454. else if(c == '\n') printf("(\\n)");
  1455. else if(c == '\r') printf("(\\r)");
  1456. else if(c == 0x1b) printf("(\\e)");
  1457. else printf("(%02x)", c);
  1458. }
  1459. printf("ESC\\\n");
  1460. }
  1461. void
  1462. strreset(void) {
  1463. memset(&strescseq, 0, sizeof(strescseq));
  1464. }
  1465. void
  1466. tputtab(bool forward) {
  1467. uint x = term.c.x;
  1468. if(forward) {
  1469. if(x == term.col)
  1470. return;
  1471. for(++x; x < term.col && !term.tabs[x]; ++x)
  1472. /* nothing */ ;
  1473. } else {
  1474. if(x == 0)
  1475. return;
  1476. for(--x; x > 0 && !term.tabs[x]; --x)
  1477. /* nothing */ ;
  1478. }
  1479. tmoveto(x, term.c.y);
  1480. }
  1481. void
  1482. tputc(char *c) {
  1483. char ascii = *c;
  1484. if(iofd != -1)
  1485. write(iofd, c, 1);
  1486. if(term.esc & ESC_START) {
  1487. if(term.esc & ESC_CSI) {
  1488. csiescseq.buf[csiescseq.len++] = ascii;
  1489. if(BETWEEN(ascii, 0x40, 0x7E) || csiescseq.len >= ESC_BUF_SIZ) {
  1490. term.esc = 0;
  1491. csiparse(), csihandle();
  1492. }
  1493. } else if(term.esc & ESC_STR) {
  1494. switch(ascii) {
  1495. case '\033':
  1496. term.esc = ESC_START | ESC_STR_END;
  1497. break;
  1498. case '\a': /* backwards compatibility to xterm */
  1499. term.esc = 0;
  1500. strhandle();
  1501. break;
  1502. default:
  1503. strescseq.buf[strescseq.len++] = ascii;
  1504. if(strescseq.len+1 >= STR_BUF_SIZ) {
  1505. term.esc = 0;
  1506. strhandle();
  1507. }
  1508. }
  1509. } else if(term.esc & ESC_STR_END) {
  1510. term.esc = 0;
  1511. if(ascii == '\\')
  1512. strhandle();
  1513. } else if(term.esc & ESC_ALTCHARSET) {
  1514. switch(ascii) {
  1515. case '0': /* Line drawing crap */
  1516. term.c.attr.mode |= ATTR_GFX;
  1517. break;
  1518. case 'B': /* Back to regular text */
  1519. term.c.attr.mode &= ~ATTR_GFX;
  1520. break;
  1521. default:
  1522. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  1523. }
  1524. term.esc = 0;
  1525. } else {
  1526. switch(ascii) {
  1527. case '[':
  1528. term.esc |= ESC_CSI;
  1529. break;
  1530. case 'P': /* DCS -- Device Control String */
  1531. case '_': /* APC -- Application Program Command */
  1532. case '^': /* PM -- Privacy Message */
  1533. case ']': /* OSC -- Operating System Command */
  1534. case 'k': /* old title set compatibility */
  1535. strreset();
  1536. strescseq.type = ascii;
  1537. term.esc |= ESC_STR;
  1538. break;
  1539. case '(':
  1540. term.esc |= ESC_ALTCHARSET;
  1541. break;
  1542. case 'D': /* IND -- Linefeed */
  1543. if(term.c.y == term.bot)
  1544. tscrollup(term.top, 1);
  1545. else
  1546. tmoveto(term.c.x, term.c.y+1);
  1547. term.esc = 0;
  1548. break;
  1549. case 'E': /* NEL -- Next line */
  1550. tnewline(1); /* always go to first col */
  1551. term.esc = 0;
  1552. break;
  1553. case 'H': /* HTS -- Horizontal tab stop */
  1554. term.tabs[term.c.x] = 1;
  1555. term.esc = 0;
  1556. break;
  1557. case 'M': /* RI -- Reverse index */
  1558. if(term.c.y == term.top)
  1559. tscrolldown(term.top, 1);
  1560. else
  1561. tmoveto(term.c.x, term.c.y-1);
  1562. term.esc = 0;
  1563. break;
  1564. case 'c': /* RIS -- Reset to inital state */
  1565. treset();
  1566. term.esc = 0;
  1567. xresettitle();
  1568. break;
  1569. case '=': /* DECPAM -- Application keypad */
  1570. term.mode |= MODE_APPKEYPAD;
  1571. term.esc = 0;
  1572. break;
  1573. case '>': /* DECPNM -- Normal keypad */
  1574. term.mode &= ~MODE_APPKEYPAD;
  1575. term.esc = 0;
  1576. break;
  1577. case '7': /* DECSC -- Save Cursor */
  1578. tcursor(CURSOR_SAVE);
  1579. term.esc = 0;
  1580. break;
  1581. case '8': /* DECRC -- Restore Cursor */
  1582. tcursor(CURSOR_LOAD);
  1583. term.esc = 0;
  1584. break;
  1585. case '\\': /* ST -- Stop */
  1586. term.esc = 0;
  1587. break;
  1588. default:
  1589. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  1590. (uchar) ascii, isprint(ascii)?ascii:'.');
  1591. term.esc = 0;
  1592. }
  1593. }
  1594. } else {
  1595. if(sel.bx != -1 && BETWEEN(term.c.y, sel.by, sel.ey))
  1596. sel.bx = -1;
  1597. switch(ascii) {
  1598. case '\0': /* padding character, do nothing */
  1599. break;
  1600. case '\t':
  1601. tputtab(1);
  1602. break;
  1603. case '\b':
  1604. tmoveto(term.c.x-1, term.c.y);
  1605. break;
  1606. case '\r':
  1607. tmoveto(0, term.c.y);
  1608. break;
  1609. case '\f':
  1610. case '\v':
  1611. case '\n':
  1612. /* go to first col if the mode is set */
  1613. tnewline(IS_SET(MODE_CRLF));
  1614. break;
  1615. case '\a':
  1616. if(!(xw.state & WIN_FOCUSED))
  1617. xseturgency(1);
  1618. break;
  1619. case '\033':
  1620. csireset();
  1621. term.esc = ESC_START;
  1622. break;
  1623. default:
  1624. if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
  1625. tnewline(1); /* always go to first col */
  1626. tsetchar(c);
  1627. if(term.c.x+1 < term.col)
  1628. tmoveto(term.c.x+1, term.c.y);
  1629. else
  1630. term.c.state |= CURSOR_WRAPNEXT;
  1631. }
  1632. }
  1633. }
  1634. int
  1635. tresize(int col, int row) {
  1636. int i, x;
  1637. int minrow = MIN(row, term.row);
  1638. int mincol = MIN(col, term.col);
  1639. int slide = term.c.y - row + 1;
  1640. if(col < 1 || row < 1)
  1641. return 0;
  1642. /* free unneeded rows */
  1643. i = 0;
  1644. if(slide > 0) {
  1645. /* slide screen to keep cursor where we expect it -
  1646. * tscrollup would work here, but we can optimize to
  1647. * memmove because we're freeing the earlier lines */
  1648. for(/* i = 0 */; i < slide; i++) {
  1649. free(term.line[i]);
  1650. free(term.alt[i]);
  1651. }
  1652. memmove(term.line, term.line + slide, row * sizeof(Line));
  1653. memmove(term.alt, term.alt + slide, row * sizeof(Line));
  1654. }
  1655. for(i += row; i < term.row; i++) {
  1656. free(term.line[i]);
  1657. free(term.alt[i]);
  1658. }
  1659. /* resize to new height */
  1660. term.line = xrealloc(term.line, row * sizeof(Line));
  1661. term.alt = xrealloc(term.alt, row * sizeof(Line));
  1662. term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
  1663. term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
  1664. /* resize each row to new width, zero-pad if needed */
  1665. for(i = 0; i < minrow; i++) {
  1666. term.dirty[i] = 1;
  1667. term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
  1668. term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
  1669. for(x = mincol; x < col; x++) {
  1670. term.line[i][x].state = 0;
  1671. term.alt[i][x].state = 0;
  1672. }
  1673. }
  1674. /* allocate any new rows */
  1675. for(/* i == minrow */; i < row; i++) {
  1676. term.dirty[i] = 1;
  1677. term.line[i] = xcalloc(col, sizeof(Glyph));
  1678. term.alt [i] = xcalloc(col, sizeof(Glyph));
  1679. }
  1680. if(col > term.col) {
  1681. bool *bp = term.tabs + term.col;
  1682. memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
  1683. while(--bp > term.tabs && !*bp)
  1684. /* nothing */ ;
  1685. for(bp += TAB; bp < term.tabs + col; bp += TAB)
  1686. *bp = 1;
  1687. }
  1688. /* update terminal size */
  1689. term.col = col, term.row = row;
  1690. /* make use of the LIMIT in tmoveto */
  1691. tmoveto(term.c.x, term.c.y);
  1692. /* reset scrolling region */
  1693. tsetscroll(0, row-1);
  1694. return (slide > 0);
  1695. }
  1696. void
  1697. xresize(int col, int row) {
  1698. xw.tw = MAX(1, 2*BORDER + col * xw.cw);
  1699. xw.th = MAX(1, 2*BORDER + row * xw.ch);
  1700. XftDrawChange(xw.xft_draw, xw.buf);
  1701. }
  1702. void
  1703. xloadcols(void) {
  1704. int i, r, g, b;
  1705. XRenderColor xft_color = { .alpha = 0 };
  1706. ulong white = WhitePixel(xw.dpy, xw.scr);
  1707. /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
  1708. for(i = 0; i < LEN(colorname); i++) {
  1709. if(!colorname[i])
  1710. continue;
  1711. if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, colorname[i], &dc.xft_col[i])) {
  1712. dc.col[i] = white;
  1713. fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
  1714. } else
  1715. dc.col[i] = dc.xft_col[i].pixel;
  1716. }
  1717. /* load colors [16-255] ; same colors as xterm */
  1718. for(i = 16, r = 0; r < 6; r++)
  1719. for(g = 0; g < 6; g++)
  1720. for(b = 0; b < 6; b++) {
  1721. xft_color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
  1722. xft_color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
  1723. xft_color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
  1724. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &xft_color, &dc.xft_col[i])) {
  1725. dc.col[i] = white;
  1726. fprintf(stderr, "Could not allocate color %d\n", i);
  1727. } else
  1728. dc.col[i] = dc.xft_col[i].pixel;
  1729. i++;
  1730. }
  1731. for(r = 0; r < 24; r++, i++) {
  1732. xft_color.red = xft_color.green = xft_color.blue = 0x0808 + 0x0a0a * r;
  1733. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &xft_color, &dc.xft_col[i])) {
  1734. dc.col[i] = white;
  1735. fprintf(stderr, "Could not allocate color %d\n", i);
  1736. } else
  1737. dc.col[i] = dc.xft_col[i].pixel;
  1738. }
  1739. }
  1740. void
  1741. xclear(int x1, int y1, int x2, int y2) {
  1742. XSetForeground(xw.dpy, dc.gc, dc.col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG]);
  1743. XFillRectangle(xw.dpy, xw.buf, dc.gc,
  1744. BORDER + x1 * xw.cw, BORDER + y1 * xw.ch,
  1745. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
  1746. }
  1747. void
  1748. xhints(void) {
  1749. XClassHint class = {opt_class ? opt_class : TNAME, TNAME};
  1750. XWMHints wm = {.flags = InputHint, .input = 1};
  1751. XSizeHints *sizeh = NULL;
  1752. sizeh = XAllocSizeHints();
  1753. if(xw.isfixed == False) {
  1754. sizeh->flags = PSize | PResizeInc | PBaseSize;
  1755. sizeh->height = xw.h;
  1756. sizeh->width = xw.w;
  1757. sizeh->height_inc = xw.ch;
  1758. sizeh->width_inc = xw.cw;
  1759. sizeh->base_height = 2*BORDER;
  1760. sizeh->base_width = 2*BORDER;
  1761. } else {
  1762. sizeh->flags = PMaxSize | PMinSize;
  1763. sizeh->min_width = sizeh->max_width = xw.fw;
  1764. sizeh->min_height = sizeh->max_height = xw.fh;
  1765. }
  1766. XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm, &class);
  1767. XFree(sizeh);
  1768. }
  1769. void
  1770. xinitfont(Font *f, char *fontstr) {
  1771. f->xft_set = XftFontOpenName(xw.dpy, xw.scr, fontstr);
  1772. if(!f->xft_set)
  1773. die("st: can't open font %s.\n", fontstr);
  1774. f->ascent = f->xft_set->ascent;
  1775. f->descent = f->xft_set->descent;
  1776. f->lbearing = 0;
  1777. f->rbearing = f->xft_set->max_advance_width;
  1778. }
  1779. void
  1780. initfonts(char *fontstr, char *bfontstr, char *ifontstr, char *ibfontstr) {
  1781. xinitfont(&dc.font, fontstr);
  1782. xinitfont(&dc.bfont, bfontstr);
  1783. xinitfont(&dc.ifont, ifontstr);
  1784. xinitfont(&dc.ibfont, ibfontstr);
  1785. }
  1786. void
  1787. xinit(void) {
  1788. XSetWindowAttributes attrs;
  1789. Cursor cursor;
  1790. Window parent;
  1791. int sw, sh, major, minor;
  1792. if(!(xw.dpy = XOpenDisplay(NULL)))
  1793. die("Can't open display\n");
  1794. xw.scr = XDefaultScreen(xw.dpy);
  1795. xw.vis = XDefaultVisual(xw.dpy, xw.scr);
  1796. /* font */
  1797. initfonts(FONT, BOLDFONT, ITALICFONT, ITALICBOLDFONT);
  1798. /* XXX: Assuming same size for bold font */
  1799. xw.cw = dc.font.rbearing - dc.font.lbearing;
  1800. xw.ch = dc.font.ascent + dc.font.descent;
  1801. /* colors */
  1802. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  1803. xloadcols();
  1804. /* adjust fixed window geometry */
  1805. if(xw.isfixed) {
  1806. sw = DisplayWidth(xw.dpy, xw.scr);
  1807. sh = DisplayHeight(xw.dpy, xw.scr);
  1808. if(xw.fx < 0)
  1809. xw.fx = sw + xw.fx - xw.fw - 1;
  1810. if(xw.fy < 0)
  1811. xw.fy = sh + xw.fy - xw.fh - 1;
  1812. xw.h = xw.fh;
  1813. xw.w = xw.fw;
  1814. } else {
  1815. /* window - default size */
  1816. xw.h = 2*BORDER + term.row * xw.ch;
  1817. xw.w = 2*BORDER + term.col * xw.cw;
  1818. xw.fx = 0;
  1819. xw.fy = 0;
  1820. }
  1821. attrs.background_pixel = dc.col[DefaultBG];
  1822. attrs.border_pixel = dc.col[DefaultBG];
  1823. attrs.bit_gravity = NorthWestGravity;
  1824. attrs.event_mask = FocusChangeMask | KeyPressMask
  1825. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  1826. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
  1827. attrs.colormap = xw.cmap;
  1828. parent = opt_embed ? strtol(opt_embed, NULL, 0) : XRootWindow(xw.dpy, xw.scr);
  1829. xw.win = XCreateWindow(xw.dpy, parent, xw.fx, xw.fy,
  1830. xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
  1831. xw.vis,
  1832. CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
  1833. | CWColormap,
  1834. &attrs);
  1835. /* double buffering */
  1836. if(!XdbeQueryExtension(xw.dpy, &major, &minor))
  1837. die("Xdbe extension is not present\n");
  1838. xw.buf = XdbeAllocateBackBufferName(xw.dpy, xw.win, XdbeCopied);
  1839. /* Xft rendering context */
  1840. xw.xft_draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
  1841. /* input methods */
  1842. xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
  1843. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  1844. | XIMStatusNothing, XNClientWindow, xw.win,
  1845. XNFocusWindow, xw.win, NULL);
  1846. /* gc */
  1847. dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
  1848. /* white cursor, black outline */
  1849. cursor = XCreateFontCursor(xw.dpy, XC_xterm);
  1850. XDefineCursor(xw.dpy, xw.win, cursor);
  1851. XRecolorCursor(xw.dpy, cursor,
  1852. &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
  1853. &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
  1854. xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
  1855. xresettitle();
  1856. XMapWindow(xw.dpy, xw.win);
  1857. xhints();
  1858. XSync(xw.dpy, 0);
  1859. }
  1860. void
  1861. xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
  1862. int fg = base.fg, bg = base.bg, temp;
  1863. int winx = BORDER+x*xw.cw, winy = BORDER+y*xw.ch + dc.font.ascent, width = charlen*xw.cw;
  1864. Font *font = &dc.font;
  1865. XGlyphInfo extents;
  1866. int i;
  1867. /* only switch default fg/bg if term is in RV mode */
  1868. if(IS_SET(MODE_REVERSE)) {
  1869. if(fg == DefaultFG)
  1870. fg = DefaultBG;
  1871. if(bg == DefaultBG)
  1872. bg = DefaultFG;
  1873. }
  1874. if(base.mode & ATTR_REVERSE)
  1875. temp = fg, fg = bg, bg = temp;
  1876. if(base.mode & ATTR_BOLD) {
  1877. fg += 8;
  1878. font = &dc.bfont;
  1879. }
  1880. if(base.mode & ATTR_ITALIC)
  1881. font = &dc.ifont;
  1882. if(base.mode & (ATTR_ITALIC|ATTR_ITALIC))
  1883. font = &dc.ibfont;
  1884. XSetBackground(xw.dpy, dc.gc, dc.col[bg]);
  1885. XSetForeground(xw.dpy, dc.gc, dc.col[fg]);
  1886. if(base.mode & ATTR_GFX) {
  1887. for(i = 0; i < bytelen; i++) {
  1888. char c = gfx[(uint)s[i] % 256];
  1889. if(c)
  1890. s[i] = c;
  1891. else if(s[i] > 0x5f)
  1892. s[i] -= 0x5f;
  1893. }
  1894. }
  1895. XftTextExtentsUtf8(xw.dpy, font->xft_set, (FcChar8 *)s, bytelen, &extents);
  1896. width = extents.xOff;
  1897. XftDrawRect(xw.xft_draw, &dc.xft_col[bg], winx, winy - font->ascent, width, xw.ch);
  1898. XftDrawStringUtf8(xw.xft_draw, &dc.xft_col[fg], font->xft_set, winx, winy, (FcChar8 *)s, bytelen);
  1899. if(base.mode & ATTR_UNDERLINE)
  1900. XDrawLine(xw.dpy, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
  1901. }
  1902. void
  1903. xdrawcursor(void) {
  1904. static int oldx = 0;
  1905. static int oldy = 0;
  1906. int sl;
  1907. Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
  1908. LIMIT(oldx, 0, term.col-1);
  1909. LIMIT(oldy, 0, term.row-1);
  1910. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  1911. memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
  1912. /* remove the old cursor */
  1913. if(term.line[oldy][oldx].state & GLYPH_SET) {
  1914. sl = utf8size(term.line[oldy][oldx].c);
  1915. xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1, sl);
  1916. } else
  1917. xclear(oldx, oldy, oldx, oldy);
  1918. /* draw the new one */
  1919. if(!(term.c.state & CURSOR_HIDE)) {
  1920. if(!(xw.state & WIN_FOCUSED))
  1921. g.bg = DefaultUCS;
  1922. if(IS_SET(MODE_REVERSE))
  1923. g.mode |= ATTR_REVERSE, g.fg = DefaultCS, g.bg = DefaultFG;
  1924. sl = utf8size(g.c);
  1925. xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
  1926. oldx = term.c.x, oldy = term.c.y;
  1927. }
  1928. }
  1929. void
  1930. xresettitle(void) {
  1931. XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
  1932. }
  1933. void
  1934. redraw(void) {
  1935. struct timespec tv = {0, REDRAW_TIMEOUT * 1000};
  1936. xclear(0, 0, xw.w, xw.h);
  1937. tfulldirt();
  1938. draw();
  1939. XSync(xw.dpy, False); /* necessary for a good tput flash */
  1940. nanosleep(&tv, NULL);
  1941. }
  1942. void
  1943. draw() {
  1944. XdbeSwapInfo swpinfo[1] = {{xw.win, XdbeCopied}};
  1945. drawregion(0, 0, term.col, term.row);
  1946. XdbeSwapBuffers(xw.dpy, swpinfo, 1);
  1947. }
  1948. void
  1949. drawregion(int x1, int y1, int x2, int y2) {
  1950. int ic, ib, x, y, ox, sl;
  1951. Glyph base, new;
  1952. char buf[DRAW_BUF_SIZ];
  1953. bool ena_sel = sel.bx != -1, alt = IS_SET(MODE_ALTSCREEN);
  1954. if((sel.alt && !alt) || (!sel.alt && alt))
  1955. ena_sel = 0;
  1956. if(!(xw.state & WIN_VISIBLE))
  1957. return;
  1958. for(y = y1; y < y2; y++) {
  1959. if(!term.dirty[y])
  1960. continue;
  1961. xclear(0, y, term.col, y);
  1962. term.dirty[y] = 0;
  1963. base = term.line[y][0];
  1964. ic = ib = ox = 0;
  1965. for(x = x1; x < x2; x++) {
  1966. new = term.line[y][x];
  1967. if(ena_sel && *(new.c) && selected(x, y))
  1968. new.mode ^= ATTR_REVERSE;
  1969. if(ib > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
  1970. ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
  1971. xdraws(buf, base, ox, y, ic, ib);
  1972. ic = ib = 0;
  1973. }
  1974. if(new.state & GLYPH_SET) {
  1975. if(ib == 0) {
  1976. ox = x;
  1977. base = new;
  1978. }
  1979. sl = utf8size(new.c);
  1980. memcpy(buf+ib, new.c, sl);
  1981. ib += sl;
  1982. ++ic;
  1983. }
  1984. }
  1985. if(ib > 0)
  1986. xdraws(buf, base, ox, y, ic, ib);
  1987. }
  1988. xdrawcursor();
  1989. }
  1990. void
  1991. expose(XEvent *ev) {
  1992. XExposeEvent *e = &ev->xexpose;
  1993. if(xw.state & WIN_REDRAW) {
  1994. if(!e->count)
  1995. xw.state &= ~WIN_REDRAW;
  1996. }
  1997. }
  1998. void
  1999. visibility(XEvent *ev) {
  2000. XVisibilityEvent *e = &ev->xvisibility;
  2001. if(e->state == VisibilityFullyObscured)
  2002. xw.state &= ~WIN_VISIBLE;
  2003. else if(!(xw.state & WIN_VISIBLE))
  2004. /* need a full redraw for next Expose, not just a buf copy */
  2005. xw.state |= WIN_VISIBLE | WIN_REDRAW;
  2006. }
  2007. void
  2008. unmap(XEvent *ev) {
  2009. xw.state &= ~WIN_VISIBLE;
  2010. }
  2011. void
  2012. xseturgency(int add) {
  2013. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  2014. h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
  2015. XSetWMHints(xw.dpy, xw.win, h);
  2016. XFree(h);
  2017. }
  2018. void
  2019. focus(XEvent *ev) {
  2020. if(ev->type == FocusIn) {
  2021. xw.state |= WIN_FOCUSED;
  2022. xseturgency(0);
  2023. } else
  2024. xw.state &= ~WIN_FOCUSED;
  2025. }
  2026. char*
  2027. kmap(KeySym k, uint state) {
  2028. int i;
  2029. state &= ~Mod2Mask;
  2030. for(i = 0; i < LEN(key); i++) {
  2031. uint mask = key[i].mask;
  2032. if(key[i].k == k && ((state & mask) == mask || (mask == XK_NO_MOD && !state)))
  2033. return (char*)key[i].s;
  2034. }
  2035. return NULL;
  2036. }
  2037. void
  2038. kpress(XEvent *ev) {
  2039. XKeyEvent *e = &ev->xkey;
  2040. KeySym ksym;
  2041. char buf[32];
  2042. char *customkey;
  2043. int len;
  2044. int meta;
  2045. int shift;
  2046. Status status;
  2047. if (IS_SET(MODE_KBDLOCK))
  2048. return;
  2049. meta = e->state & Mod1Mask;
  2050. shift = e->state & ShiftMask;
  2051. len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
  2052. /* 1. custom keys from config.h */
  2053. if((customkey = kmap(ksym, e->state)))
  2054. ttywrite(customkey, strlen(customkey));
  2055. /* 2. hardcoded (overrides X lookup) */
  2056. else
  2057. switch(ksym) {
  2058. case XK_Up:
  2059. case XK_Down:
  2060. case XK_Left:
  2061. case XK_Right:
  2062. /* XXX: shift up/down doesn't work */
  2063. sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', (shift ? "dacb":"DACB")[ksym - XK_Left]);
  2064. ttywrite(buf, 3);
  2065. break;
  2066. case XK_Insert:
  2067. if(shift)
  2068. selpaste();
  2069. break;
  2070. case XK_Return:
  2071. if(IS_SET(MODE_CRLF))
  2072. ttywrite("\r\n", 2);
  2073. else
  2074. ttywrite("\r", 1);
  2075. break;
  2076. /* 3. X lookup */
  2077. default:
  2078. if(len > 0) {
  2079. if(meta && len == 1)
  2080. ttywrite("\033", 1);
  2081. ttywrite(buf, len);
  2082. }
  2083. break;
  2084. }
  2085. }
  2086. void
  2087. cmessage(XEvent *e) {
  2088. /* See xembed specs
  2089. http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
  2090. if(e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
  2091. if(e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
  2092. xw.state |= WIN_FOCUSED;
  2093. xseturgency(0);
  2094. } else if(e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
  2095. xw.state &= ~WIN_FOCUSED;
  2096. }
  2097. }
  2098. }
  2099. void
  2100. resize(XEvent *e) {
  2101. int col, row;
  2102. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  2103. return;
  2104. xw.w = e->xconfigure.width;
  2105. xw.h = e->xconfigure.height;
  2106. col = (xw.w - 2*BORDER) / xw.cw;
  2107. row = (xw.h - 2*BORDER) / xw.ch;
  2108. if(col == term.col && row == term.row)
  2109. return;
  2110. xclear(0, 0, xw.w, xw.h);
  2111. tresize(col, row);
  2112. xresize(col, row);
  2113. ttyresize();
  2114. }
  2115. void
  2116. run(void) {
  2117. XEvent ev;
  2118. fd_set rfd;
  2119. int xfd = XConnectionNumber(xw.dpy), i;
  2120. struct timeval drawtimeout, *tv = NULL;
  2121. for(i = 0;; i++) {
  2122. FD_ZERO(&rfd);
  2123. FD_SET(cmdfd, &rfd);
  2124. FD_SET(xfd, &rfd);
  2125. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv) < 0) {
  2126. if(errno == EINTR)
  2127. continue;
  2128. die("select failed: %s\n", SERRNO);
  2129. }
  2130. /*
  2131. * Stop after a certain number of reads so the user does not
  2132. * feel like the system is stuttering.
  2133. */
  2134. if(i < 1000 && FD_ISSET(cmdfd, &rfd)) {
  2135. ttyread();
  2136. /*
  2137. * Just wait a bit so it isn't disturbing the
  2138. * user and the system is able to write something.
  2139. */
  2140. drawtimeout.tv_sec = 0;
  2141. drawtimeout.tv_usec = 5;
  2142. tv = &drawtimeout;
  2143. continue;
  2144. }
  2145. i = 0;
  2146. tv = NULL;
  2147. while(XPending(xw.dpy)) {
  2148. XNextEvent(xw.dpy, &ev);
  2149. if(XFilterEvent(&ev, xw.win))
  2150. continue;
  2151. if(handler[ev.type])
  2152. (handler[ev.type])(&ev);
  2153. }
  2154. draw();
  2155. XFlush(xw.dpy);
  2156. }
  2157. }
  2158. int
  2159. main(int argc, char *argv[]) {
  2160. int i, bitm, xr, yr;
  2161. uint wr, hr;
  2162. xw.fw = xw.fh = xw.fx = xw.fy = 0;
  2163. xw.isfixed = False;
  2164. for(i = 1; i < argc; i++) {
  2165. switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
  2166. case 't':
  2167. if(++i < argc) opt_title = argv[i];
  2168. break;
  2169. case 'c':
  2170. if(++i < argc) opt_class = argv[i];
  2171. break;
  2172. case 'w':
  2173. if(++i < argc) opt_embed = argv[i];
  2174. break;
  2175. case 'f':
  2176. if(++i < argc) opt_io = argv[i];
  2177. break;
  2178. case 'e':
  2179. /* eat every remaining arguments */
  2180. if(++i < argc) opt_cmd = &argv[i];
  2181. goto run;
  2182. case 'g':
  2183. if(++i >= argc)
  2184. break;
  2185. bitm = XParseGeometry(argv[i], &xr, &yr, &wr, &hr);
  2186. if(bitm & XValue)
  2187. xw.fx = xr;
  2188. if(bitm & YValue)
  2189. xw.fy = yr;
  2190. if(bitm & WidthValue)
  2191. xw.fw = (int)wr;
  2192. if(bitm & HeightValue)
  2193. xw.fh = (int)hr;
  2194. if(bitm & XNegative && xw.fx == 0)
  2195. xw.fx = -1;
  2196. if(bitm & XNegative && xw.fy == 0)
  2197. xw.fy = -1;
  2198. if(xw.fh != 0 && xw.fw != 0)
  2199. xw.isfixed = True;
  2200. break;
  2201. case 'v':
  2202. default:
  2203. die(USAGE);
  2204. }
  2205. }
  2206. run:
  2207. setlocale(LC_CTYPE, "");
  2208. xinit();
  2209. tnew(80, 24);
  2210. ttynew();
  2211. selinit();
  2212. run();
  2213. return 0;
  2214. }