st.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468
  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*, int);
  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, charsize);
  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. tclearregion(0, 0, term.col-1, term.row-1);
  852. }
  853. void
  854. tnew(int col, int row) {
  855. /* set screen size */
  856. term.row = row, term.col = col;
  857. term.line = xmalloc(term.row * sizeof(Line));
  858. term.alt = xmalloc(term.row * sizeof(Line));
  859. term.dirty = xmalloc(term.row * sizeof(*term.dirty));
  860. term.tabs = xmalloc(term.col * sizeof(*term.tabs));
  861. for(row = 0; row < term.row; row++) {
  862. term.line[row] = xmalloc(term.col * sizeof(Glyph));
  863. term.alt [row] = xmalloc(term.col * sizeof(Glyph));
  864. term.dirty[row] = 0;
  865. }
  866. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  867. /* setup screen */
  868. treset();
  869. }
  870. void
  871. tswapscreen(void) {
  872. Line* tmp = term.line;
  873. term.line = term.alt;
  874. term.alt = tmp;
  875. term.mode ^= MODE_ALTSCREEN;
  876. tfulldirt();
  877. }
  878. void
  879. tscrolldown(int orig, int n) {
  880. int i;
  881. Line temp;
  882. LIMIT(n, 0, term.bot-orig+1);
  883. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  884. for(i = term.bot; i >= orig+n; i--) {
  885. temp = term.line[i];
  886. term.line[i] = term.line[i-n];
  887. term.line[i-n] = temp;
  888. term.dirty[i] = 1;
  889. term.dirty[i-n] = 1;
  890. }
  891. selscroll(orig, n);
  892. }
  893. void
  894. tscrollup(int orig, int n) {
  895. int i;
  896. Line temp;
  897. LIMIT(n, 0, term.bot-orig+1);
  898. tclearregion(0, orig, term.col-1, orig+n-1);
  899. for(i = orig; i <= term.bot-n; i++) {
  900. temp = term.line[i];
  901. term.line[i] = term.line[i+n];
  902. term.line[i+n] = temp;
  903. term.dirty[i] = 1;
  904. term.dirty[i+n] = 1;
  905. }
  906. selscroll(orig, -n);
  907. }
  908. void
  909. selscroll(int orig, int n) {
  910. if(sel.bx == -1)
  911. return;
  912. if(BETWEEN(sel.by, orig, term.bot) || BETWEEN(sel.ey, orig, term.bot)) {
  913. if((sel.by += n) > term.bot || (sel.ey += n) < term.top) {
  914. sel.bx = -1;
  915. return;
  916. }
  917. if(sel.by < term.top) {
  918. sel.by = term.top;
  919. sel.bx = 0;
  920. }
  921. if(sel.ey > term.bot) {
  922. sel.ey = term.bot;
  923. sel.ex = term.col;
  924. }
  925. sel.b.y = sel.by, sel.b.x = sel.bx;
  926. sel.e.y = sel.ey, sel.e.x = sel.ex;
  927. }
  928. }
  929. void
  930. tnewline(int first_col) {
  931. int y = term.c.y;
  932. if(y == term.bot)
  933. tscrollup(term.top, 1);
  934. else
  935. y++;
  936. tmoveto(first_col ? 0 : term.c.x, y);
  937. }
  938. void
  939. csiparse(void) {
  940. /* int noarg = 1; */
  941. char *p = csiescseq.buf;
  942. csiescseq.narg = 0;
  943. if(*p == '?')
  944. csiescseq.priv = 1, p++;
  945. while(p < csiescseq.buf+csiescseq.len) {
  946. while(isdigit(*p)) {
  947. csiescseq.arg[csiescseq.narg] *= 10;
  948. csiescseq.arg[csiescseq.narg] += *p++ - '0'/*, noarg = 0 */;
  949. }
  950. if(*p == ';' && csiescseq.narg+1 < ESC_ARG_SIZ)
  951. csiescseq.narg++, p++;
  952. else {
  953. csiescseq.mode = *p;
  954. csiescseq.narg++;
  955. return;
  956. }
  957. }
  958. }
  959. void
  960. tmoveto(int x, int y) {
  961. LIMIT(x, 0, term.col-1);
  962. LIMIT(y, 0, term.row-1);
  963. term.c.state &= ~CURSOR_WRAPNEXT;
  964. term.c.x = x;
  965. term.c.y = y;
  966. }
  967. void
  968. tsetchar(char *c) {
  969. term.dirty[term.c.y] = 1;
  970. term.line[term.c.y][term.c.x] = term.c.attr;
  971. memcpy(term.line[term.c.y][term.c.x].c, c, UTF_SIZ);
  972. term.line[term.c.y][term.c.x].state |= GLYPH_SET;
  973. }
  974. void
  975. tclearregion(int x1, int y1, int x2, int y2) {
  976. int x, y, temp;
  977. if(x1 > x2)
  978. temp = x1, x1 = x2, x2 = temp;
  979. if(y1 > y2)
  980. temp = y1, y1 = y2, y2 = temp;
  981. LIMIT(x1, 0, term.col-1);
  982. LIMIT(x2, 0, term.col-1);
  983. LIMIT(y1, 0, term.row-1);
  984. LIMIT(y2, 0, term.row-1);
  985. for(y = y1; y <= y2; y++) {
  986. term.dirty[y] = 1;
  987. for(x = x1; x <= x2; x++)
  988. term.line[y][x].state = 0;
  989. }
  990. }
  991. void
  992. tdeletechar(int n) {
  993. int src = term.c.x + n;
  994. int dst = term.c.x;
  995. int size = term.col - src;
  996. term.dirty[term.c.y] = 1;
  997. if(src >= term.col) {
  998. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  999. return;
  1000. }
  1001. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  1002. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  1003. }
  1004. void
  1005. tinsertblank(int n) {
  1006. int src = term.c.x;
  1007. int dst = src + n;
  1008. int size = term.col - dst;
  1009. term.dirty[term.c.y] = 1;
  1010. if(dst >= term.col) {
  1011. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1012. return;
  1013. }
  1014. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  1015. tclearregion(src, term.c.y, dst - 1, term.c.y);
  1016. }
  1017. void
  1018. tinsertblankline(int n) {
  1019. if(term.c.y < term.top || term.c.y > term.bot)
  1020. return;
  1021. tscrolldown(term.c.y, n);
  1022. }
  1023. void
  1024. tdeleteline(int n) {
  1025. if(term.c.y < term.top || term.c.y > term.bot)
  1026. return;
  1027. tscrollup(term.c.y, n);
  1028. }
  1029. void
  1030. tsetattr(int *attr, int l) {
  1031. int i;
  1032. for(i = 0; i < l; i++) {
  1033. switch(attr[i]) {
  1034. case 0:
  1035. term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD \
  1036. | ATTR_ITALIC | ATTR_BLINK | ATTR_GFX);
  1037. term.c.attr.fg = DefaultFG;
  1038. term.c.attr.bg = DefaultBG;
  1039. break;
  1040. case 1:
  1041. term.c.attr.mode |= ATTR_BOLD;
  1042. break;
  1043. case 3: /* enter standout (highlight) */
  1044. term.c.attr.mode |= ATTR_ITALIC;
  1045. break;
  1046. case 4:
  1047. term.c.attr.mode |= ATTR_UNDERLINE;
  1048. break;
  1049. case 5:
  1050. term.c.attr.mode |= ATTR_BLINK;
  1051. break;
  1052. case 7:
  1053. term.c.attr.mode |= ATTR_REVERSE;
  1054. break;
  1055. case 21:
  1056. case 22:
  1057. term.c.attr.mode &= ~ATTR_BOLD;
  1058. break;
  1059. case 23: /* leave standout (highlight) mode */
  1060. term.c.attr.mode &= ~ATTR_ITALIC;
  1061. break;
  1062. case 24:
  1063. term.c.attr.mode &= ~ATTR_UNDERLINE;
  1064. break;
  1065. case 25:
  1066. term.c.attr.mode &= ~ATTR_BLINK;
  1067. break;
  1068. case 27:
  1069. term.c.attr.mode &= ~ATTR_REVERSE;
  1070. break;
  1071. case 38:
  1072. if(i + 2 < l && attr[i + 1] == 5) {
  1073. i += 2;
  1074. if(BETWEEN(attr[i], 0, 255))
  1075. term.c.attr.fg = attr[i];
  1076. else
  1077. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
  1078. }
  1079. else
  1080. fprintf(stderr, "erresc(38): gfx attr %d unknown\n", attr[i]);
  1081. break;
  1082. case 39:
  1083. term.c.attr.fg = DefaultFG;
  1084. break;
  1085. case 48:
  1086. if(i + 2 < l && attr[i + 1] == 5) {
  1087. i += 2;
  1088. if(BETWEEN(attr[i], 0, 255))
  1089. term.c.attr.bg = attr[i];
  1090. else
  1091. fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
  1092. }
  1093. else
  1094. fprintf(stderr, "erresc(48): gfx attr %d unknown\n", attr[i]);
  1095. break;
  1096. case 49:
  1097. term.c.attr.bg = DefaultBG;
  1098. break;
  1099. default:
  1100. if(BETWEEN(attr[i], 30, 37))
  1101. term.c.attr.fg = attr[i] - 30;
  1102. else if(BETWEEN(attr[i], 40, 47))
  1103. term.c.attr.bg = attr[i] - 40;
  1104. else if(BETWEEN(attr[i], 90, 97))
  1105. term.c.attr.fg = attr[i] - 90 + 8;
  1106. else if(BETWEEN(attr[i], 100, 107))
  1107. term.c.attr.bg = attr[i] - 100 + 8;
  1108. else
  1109. fprintf(stderr, "erresc(default): gfx attr %d unknown\n", attr[i]), csidump();
  1110. break;
  1111. }
  1112. }
  1113. }
  1114. void
  1115. tsetscroll(int t, int b) {
  1116. int temp;
  1117. LIMIT(t, 0, term.row-1);
  1118. LIMIT(b, 0, term.row-1);
  1119. if(t > b) {
  1120. temp = t;
  1121. t = b;
  1122. b = temp;
  1123. }
  1124. term.top = t;
  1125. term.bot = b;
  1126. }
  1127. #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
  1128. void
  1129. tsetmode(bool priv, bool set, int *args, int narg) {
  1130. int *lim, mode;
  1131. for(lim = args + narg; args < lim; ++args) {
  1132. if(priv) {
  1133. switch(*args) {
  1134. break;
  1135. case 1: /* DECCKM -- Cursor key */
  1136. MODBIT(term.mode, set, MODE_APPKEYPAD);
  1137. break;
  1138. case 5: /* DECSCNM -- Reverve video */
  1139. mode = term.mode;
  1140. MODBIT(term.mode,set, MODE_REVERSE);
  1141. if(mode != term.mode)
  1142. redraw();
  1143. break;
  1144. case 6: /* XXX: DECOM -- Origin */
  1145. break;
  1146. case 7: /* DECAWM -- Auto wrap */
  1147. MODBIT(term.mode, set, MODE_WRAP);
  1148. break;
  1149. case 8: /* XXX: DECARM -- Auto repeat */
  1150. break;
  1151. case 0: /* Error (IGNORED) */
  1152. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1153. break;
  1154. case 25:
  1155. MODBIT(term.c.state, !set, CURSOR_HIDE);
  1156. break;
  1157. case 1000: /* 1000,1002: enable xterm mouse report */
  1158. MODBIT(term.mode, set, MODE_MOUSEBTN);
  1159. break;
  1160. case 1002:
  1161. MODBIT(term.mode, set, MODE_MOUSEMOTION);
  1162. break;
  1163. case 1049: /* = 1047 and 1048 */
  1164. case 47:
  1165. case 1047:
  1166. if(IS_SET(MODE_ALTSCREEN))
  1167. tclearregion(0, 0, term.col-1, term.row-1);
  1168. if((set && !IS_SET(MODE_ALTSCREEN)) ||
  1169. (!set && IS_SET(MODE_ALTSCREEN))) {
  1170. tswapscreen();
  1171. }
  1172. if(*args != 1049)
  1173. break;
  1174. /* pass through */
  1175. case 1048:
  1176. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1177. break;
  1178. default:
  1179. /* case 2: DECANM -- ANSI/VT52 (NOT SUPPOURTED) */
  1180. /* case 3: DECCOLM -- Column (NOT SUPPORTED) */
  1181. /* case 4: DECSCLM -- Scroll (NOT SUPPORTED) */
  1182. /* case 18: DECPFF -- Printer feed (NOT SUPPORTED) */
  1183. /* case 19: DECPEX -- Printer extent (NOT SUPPORTED) */
  1184. /* case 42: DECNRCM -- National characters (NOT SUPPORTED) */
  1185. fprintf(stderr,
  1186. "erresc: unknown private set/reset mode %d\n",
  1187. *args);
  1188. break;
  1189. }
  1190. } else {
  1191. switch(*args) {
  1192. case 0: /* Error (IGNORED) */
  1193. break;
  1194. case 2: /* KAM -- keyboard action */
  1195. MODBIT(term.mode, set, MODE_KBDLOCK);
  1196. break;
  1197. case 4: /* IRM -- Insertion-replacement */
  1198. MODBIT(term.mode, set, MODE_INSERT);
  1199. break;
  1200. case 12: /* XXX: SRM -- Send/Receive */
  1201. break;
  1202. case 20: /* LNM -- Linefeed/new line */
  1203. MODBIT(term.mode, set, MODE_CRLF);
  1204. break;
  1205. default:
  1206. fprintf(stderr,
  1207. "erresc: unknown set/reset mode %d\n",
  1208. *args);
  1209. break;
  1210. }
  1211. }
  1212. }
  1213. }
  1214. #undef MODBIT
  1215. void
  1216. csihandle(void) {
  1217. switch(csiescseq.mode) {
  1218. default:
  1219. unknown:
  1220. fprintf(stderr, "erresc: unknown csi ");
  1221. csidump();
  1222. /* die(""); */
  1223. break;
  1224. case '@': /* ICH -- Insert <n> blank char */
  1225. DEFAULT(csiescseq.arg[0], 1);
  1226. tinsertblank(csiescseq.arg[0]);
  1227. break;
  1228. case 'A': /* CUU -- Cursor <n> Up */
  1229. case 'e':
  1230. DEFAULT(csiescseq.arg[0], 1);
  1231. tmoveto(term.c.x, term.c.y-csiescseq.arg[0]);
  1232. break;
  1233. case 'B': /* CUD -- Cursor <n> Down */
  1234. DEFAULT(csiescseq.arg[0], 1);
  1235. tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
  1236. break;
  1237. case 'C': /* CUF -- Cursor <n> Forward */
  1238. case 'a':
  1239. DEFAULT(csiescseq.arg[0], 1);
  1240. tmoveto(term.c.x+csiescseq.arg[0], term.c.y);
  1241. break;
  1242. case 'D': /* CUB -- Cursor <n> Backward */
  1243. DEFAULT(csiescseq.arg[0], 1);
  1244. tmoveto(term.c.x-csiescseq.arg[0], term.c.y);
  1245. break;
  1246. case 'E': /* CNL -- Cursor <n> Down and first col */
  1247. DEFAULT(csiescseq.arg[0], 1);
  1248. tmoveto(0, term.c.y+csiescseq.arg[0]);
  1249. break;
  1250. case 'F': /* CPL -- Cursor <n> Up and first col */
  1251. DEFAULT(csiescseq.arg[0], 1);
  1252. tmoveto(0, term.c.y-csiescseq.arg[0]);
  1253. break;
  1254. case 'g': /* TBC -- Tabulation clear */
  1255. switch (csiescseq.arg[0]) {
  1256. case 0: /* clear current tab stop */
  1257. term.tabs[term.c.x] = 0;
  1258. break;
  1259. case 3: /* clear all the tabs */
  1260. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  1261. break;
  1262. default:
  1263. goto unknown;
  1264. }
  1265. break;
  1266. case 'G': /* CHA -- Move to <col> */
  1267. case '`': /* HPA */
  1268. DEFAULT(csiescseq.arg[0], 1);
  1269. tmoveto(csiescseq.arg[0]-1, term.c.y);
  1270. break;
  1271. case 'H': /* CUP -- Move to <row> <col> */
  1272. case 'f': /* HVP */
  1273. DEFAULT(csiescseq.arg[0], 1);
  1274. DEFAULT(csiescseq.arg[1], 1);
  1275. tmoveto(csiescseq.arg[1]-1, csiescseq.arg[0]-1);
  1276. break;
  1277. case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
  1278. DEFAULT(csiescseq.arg[0], 1);
  1279. while(csiescseq.arg[0]--)
  1280. tputtab(1);
  1281. break;
  1282. case 'J': /* ED -- Clear screen */
  1283. sel.bx = -1;
  1284. switch(csiescseq.arg[0]) {
  1285. case 0: /* below */
  1286. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1287. if(term.c.y < term.row-1)
  1288. tclearregion(0, term.c.y+1, term.col-1, term.row-1);
  1289. break;
  1290. case 1: /* above */
  1291. if(term.c.y > 1)
  1292. tclearregion(0, 0, term.col-1, term.c.y-1);
  1293. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1294. break;
  1295. case 2: /* all */
  1296. tclearregion(0, 0, term.col-1, term.row-1);
  1297. break;
  1298. default:
  1299. goto unknown;
  1300. }
  1301. break;
  1302. case 'K': /* EL -- Clear line */
  1303. switch(csiescseq.arg[0]) {
  1304. case 0: /* right */
  1305. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1306. break;
  1307. case 1: /* left */
  1308. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1309. break;
  1310. case 2: /* all */
  1311. tclearregion(0, term.c.y, term.col-1, term.c.y);
  1312. break;
  1313. }
  1314. break;
  1315. case 'S': /* SU -- Scroll <n> line up */
  1316. DEFAULT(csiescseq.arg[0], 1);
  1317. tscrollup(term.top, csiescseq.arg[0]);
  1318. break;
  1319. case 'T': /* SD -- Scroll <n> line down */
  1320. DEFAULT(csiescseq.arg[0], 1);
  1321. tscrolldown(term.top, csiescseq.arg[0]);
  1322. break;
  1323. case 'L': /* IL -- Insert <n> blank lines */
  1324. DEFAULT(csiescseq.arg[0], 1);
  1325. tinsertblankline(csiescseq.arg[0]);
  1326. break;
  1327. case 'l': /* RM -- Reset Mode */
  1328. tsetmode(csiescseq.priv, 0, csiescseq.arg, csiescseq.narg);
  1329. break;
  1330. case 'M': /* DL -- Delete <n> lines */
  1331. DEFAULT(csiescseq.arg[0], 1);
  1332. tdeleteline(csiescseq.arg[0]);
  1333. break;
  1334. case 'X': /* ECH -- Erase <n> char */
  1335. DEFAULT(csiescseq.arg[0], 1);
  1336. tclearregion(term.c.x, term.c.y, term.c.x + csiescseq.arg[0], term.c.y);
  1337. break;
  1338. case 'P': /* DCH -- Delete <n> char */
  1339. DEFAULT(csiescseq.arg[0], 1);
  1340. tdeletechar(csiescseq.arg[0]);
  1341. break;
  1342. case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
  1343. DEFAULT(csiescseq.arg[0], 1);
  1344. while(csiescseq.arg[0]--)
  1345. tputtab(0);
  1346. break;
  1347. case 'd': /* VPA -- Move to <row> */
  1348. DEFAULT(csiescseq.arg[0], 1);
  1349. tmoveto(term.c.x, csiescseq.arg[0]-1);
  1350. break;
  1351. case 'h': /* SM -- Set terminal mode */
  1352. tsetmode(csiescseq.priv, 1, csiescseq.arg, csiescseq.narg);
  1353. break;
  1354. case 'm': /* SGR -- Terminal attribute (color) */
  1355. tsetattr(csiescseq.arg, csiescseq.narg);
  1356. break;
  1357. case 'r': /* DECSTBM -- Set Scrolling Region */
  1358. if(csiescseq.priv)
  1359. goto unknown;
  1360. else {
  1361. DEFAULT(csiescseq.arg[0], 1);
  1362. DEFAULT(csiescseq.arg[1], term.row);
  1363. tsetscroll(csiescseq.arg[0]-1, csiescseq.arg[1]-1);
  1364. tmoveto(0, 0);
  1365. }
  1366. break;
  1367. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  1368. tcursor(CURSOR_SAVE);
  1369. break;
  1370. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  1371. tcursor(CURSOR_LOAD);
  1372. break;
  1373. }
  1374. }
  1375. void
  1376. csidump(void) {
  1377. int i;
  1378. printf("ESC[");
  1379. for(i = 0; i < csiescseq.len; i++) {
  1380. uint c = csiescseq.buf[i] & 0xff;
  1381. if(isprint(c)) putchar(c);
  1382. else if(c == '\n') printf("(\\n)");
  1383. else if(c == '\r') printf("(\\r)");
  1384. else if(c == 0x1b) printf("(\\e)");
  1385. else printf("(%02x)", c);
  1386. }
  1387. putchar('\n');
  1388. }
  1389. void
  1390. csireset(void) {
  1391. memset(&csiescseq, 0, sizeof(csiescseq));
  1392. }
  1393. void
  1394. strhandle(void) {
  1395. char *p;
  1396. /*
  1397. * TODO: make this being useful in case of color palette change.
  1398. */
  1399. strparse();
  1400. p = strescseq.buf;
  1401. switch(strescseq.type) {
  1402. case ']': /* OSC -- Operating System Command */
  1403. switch(p[0]) {
  1404. case '0':
  1405. case '1':
  1406. case '2':
  1407. /*
  1408. * TODO: Handle special chars in string, like umlauts.
  1409. */
  1410. if(p[1] == ';') {
  1411. XStoreName(xw.dpy, xw.win, strescseq.buf+2);
  1412. }
  1413. break;
  1414. case ';':
  1415. XStoreName(xw.dpy, xw.win, strescseq.buf+1);
  1416. break;
  1417. case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
  1418. break;
  1419. default:
  1420. fprintf(stderr, "erresc: unknown str ");
  1421. strdump();
  1422. break;
  1423. }
  1424. break;
  1425. case 'k': /* old title set compatibility */
  1426. XStoreName(xw.dpy, xw.win, strescseq.buf);
  1427. break;
  1428. case 'P': /* DSC -- Device Control String */
  1429. case '_': /* APC -- Application Program Command */
  1430. case '^': /* PM -- Privacy Message */
  1431. default:
  1432. fprintf(stderr, "erresc: unknown str ");
  1433. strdump();
  1434. /* die(""); */
  1435. break;
  1436. }
  1437. }
  1438. void
  1439. strparse(void) {
  1440. /*
  1441. * TODO: Implement parsing like for CSI when required.
  1442. * Format: ESC type cmd ';' arg0 [';' argn] ESC \
  1443. */
  1444. return;
  1445. }
  1446. void
  1447. strdump(void) {
  1448. int i;
  1449. printf("ESC%c", strescseq.type);
  1450. for(i = 0; i < strescseq.len; i++) {
  1451. uint c = strescseq.buf[i] & 0xff;
  1452. if(isprint(c)) putchar(c);
  1453. else if(c == '\n') printf("(\\n)");
  1454. else if(c == '\r') printf("(\\r)");
  1455. else if(c == 0x1b) printf("(\\e)");
  1456. else printf("(%02x)", c);
  1457. }
  1458. printf("ESC\\\n");
  1459. }
  1460. void
  1461. strreset(void) {
  1462. memset(&strescseq, 0, sizeof(strescseq));
  1463. }
  1464. void
  1465. tputtab(bool forward) {
  1466. uint x = term.c.x;
  1467. if(forward) {
  1468. if(x == term.col)
  1469. return;
  1470. for(++x; x < term.col && !term.tabs[x]; ++x)
  1471. /* nothing */ ;
  1472. } else {
  1473. if(x == 0)
  1474. return;
  1475. for(--x; x > 0 && !term.tabs[x]; --x)
  1476. /* nothing */ ;
  1477. }
  1478. tmoveto(x, term.c.y);
  1479. }
  1480. void
  1481. tputc(char *c, int len) {
  1482. char ascii = *c;
  1483. if(iofd != -1)
  1484. write(iofd, c, len);
  1485. if(term.esc & ESC_START) {
  1486. if(term.esc & ESC_CSI) {
  1487. csiescseq.buf[csiescseq.len++] = ascii;
  1488. if(BETWEEN(ascii, 0x40, 0x7E) || csiescseq.len >= ESC_BUF_SIZ) {
  1489. term.esc = 0;
  1490. csiparse(), csihandle();
  1491. }
  1492. } else if(term.esc & ESC_STR) {
  1493. switch(ascii) {
  1494. case '\033':
  1495. term.esc = ESC_START | ESC_STR_END;
  1496. break;
  1497. case '\a': /* backwards compatibility to xterm */
  1498. term.esc = 0;
  1499. strhandle();
  1500. break;
  1501. default:
  1502. strescseq.buf[strescseq.len++] = ascii;
  1503. if(strescseq.len+1 >= STR_BUF_SIZ) {
  1504. term.esc = 0;
  1505. strhandle();
  1506. }
  1507. }
  1508. } else if(term.esc & ESC_STR_END) {
  1509. term.esc = 0;
  1510. if(ascii == '\\')
  1511. strhandle();
  1512. } else if(term.esc & ESC_ALTCHARSET) {
  1513. switch(ascii) {
  1514. case '0': /* Line drawing crap */
  1515. term.c.attr.mode |= ATTR_GFX;
  1516. break;
  1517. case 'B': /* Back to regular text */
  1518. term.c.attr.mode &= ~ATTR_GFX;
  1519. break;
  1520. default:
  1521. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  1522. }
  1523. term.esc = 0;
  1524. } else {
  1525. switch(ascii) {
  1526. case '[':
  1527. term.esc |= ESC_CSI;
  1528. break;
  1529. case 'P': /* DCS -- Device Control String */
  1530. case '_': /* APC -- Application Program Command */
  1531. case '^': /* PM -- Privacy Message */
  1532. case ']': /* OSC -- Operating System Command */
  1533. case 'k': /* old title set compatibility */
  1534. strreset();
  1535. strescseq.type = ascii;
  1536. term.esc |= ESC_STR;
  1537. break;
  1538. case ')':
  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. xclear(0, 0, xw.w, xw.h);
  1568. xresettitle();
  1569. break;
  1570. case '=': /* DECPAM -- Application keypad */
  1571. term.mode |= MODE_APPKEYPAD;
  1572. term.esc = 0;
  1573. break;
  1574. case '>': /* DECPNM -- Normal keypad */
  1575. term.mode &= ~MODE_APPKEYPAD;
  1576. term.esc = 0;
  1577. break;
  1578. case '7': /* DECSC -- Save Cursor */
  1579. tcursor(CURSOR_SAVE);
  1580. term.esc = 0;
  1581. break;
  1582. case '8': /* DECRC -- Restore Cursor */
  1583. tcursor(CURSOR_LOAD);
  1584. term.esc = 0;
  1585. break;
  1586. case '\\': /* ST -- Stop */
  1587. term.esc = 0;
  1588. break;
  1589. default:
  1590. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  1591. (uchar) ascii, isprint(ascii)?ascii:'.');
  1592. term.esc = 0;
  1593. }
  1594. }
  1595. } else {
  1596. if(sel.bx != -1 && BETWEEN(term.c.y, sel.by, sel.ey))
  1597. sel.bx = -1;
  1598. switch(ascii) {
  1599. case '\0': /* padding character, do nothing */
  1600. break;
  1601. case '\t':
  1602. tputtab(1);
  1603. break;
  1604. case '\b':
  1605. tmoveto(term.c.x-1, term.c.y);
  1606. break;
  1607. case '\r':
  1608. tmoveto(0, term.c.y);
  1609. break;
  1610. case '\f':
  1611. case '\v':
  1612. case '\n':
  1613. /* go to first col if the mode is set */
  1614. tnewline(IS_SET(MODE_CRLF));
  1615. break;
  1616. case '\a':
  1617. if(!(xw.state & WIN_FOCUSED))
  1618. xseturgency(1);
  1619. break;
  1620. case '\033':
  1621. csireset();
  1622. term.esc = ESC_START;
  1623. break;
  1624. default:
  1625. if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
  1626. tnewline(1); /* always go to first col */
  1627. tsetchar(c);
  1628. if(term.c.x+1 < term.col)
  1629. tmoveto(term.c.x+1, term.c.y);
  1630. else
  1631. term.c.state |= CURSOR_WRAPNEXT;
  1632. }
  1633. }
  1634. }
  1635. int
  1636. tresize(int col, int row) {
  1637. int i, x;
  1638. int minrow = MIN(row, term.row);
  1639. int mincol = MIN(col, term.col);
  1640. int slide = term.c.y - row + 1;
  1641. if(col < 1 || row < 1)
  1642. return 0;
  1643. /* free unneeded rows */
  1644. i = 0;
  1645. if(slide > 0) {
  1646. /* slide screen to keep cursor where we expect it -
  1647. * tscrollup would work here, but we can optimize to
  1648. * memmove because we're freeing the earlier lines */
  1649. for(/* i = 0 */; i < slide; i++) {
  1650. free(term.line[i]);
  1651. free(term.alt[i]);
  1652. }
  1653. memmove(term.line, term.line + slide, row * sizeof(Line));
  1654. memmove(term.alt, term.alt + slide, row * sizeof(Line));
  1655. }
  1656. for(i += row; i < term.row; i++) {
  1657. free(term.line[i]);
  1658. free(term.alt[i]);
  1659. }
  1660. /* resize to new height */
  1661. term.line = xrealloc(term.line, row * sizeof(Line));
  1662. term.alt = xrealloc(term.alt, row * sizeof(Line));
  1663. term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
  1664. term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
  1665. /* resize each row to new width, zero-pad if needed */
  1666. for(i = 0; i < minrow; i++) {
  1667. term.dirty[i] = 1;
  1668. term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
  1669. term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
  1670. for(x = mincol; x < col; x++) {
  1671. term.line[i][x].state = 0;
  1672. term.alt[i][x].state = 0;
  1673. }
  1674. }
  1675. /* allocate any new rows */
  1676. for(/* i == minrow */; i < row; i++) {
  1677. term.dirty[i] = 1;
  1678. term.line[i] = xcalloc(col, sizeof(Glyph));
  1679. term.alt [i] = xcalloc(col, sizeof(Glyph));
  1680. }
  1681. if(col > term.col) {
  1682. bool *bp = term.tabs + term.col;
  1683. memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
  1684. while(--bp > term.tabs && !*bp)
  1685. /* nothing */ ;
  1686. for(bp += TAB; bp < term.tabs + col; bp += TAB)
  1687. *bp = 1;
  1688. }
  1689. /* update terminal size */
  1690. term.col = col, term.row = row;
  1691. /* make use of the LIMIT in tmoveto */
  1692. tmoveto(term.c.x, term.c.y);
  1693. /* reset scrolling region */
  1694. tsetscroll(0, row-1);
  1695. return (slide > 0);
  1696. }
  1697. void
  1698. xresize(int col, int row) {
  1699. xw.tw = MAX(1, 2*BORDER + col * xw.cw);
  1700. xw.th = MAX(1, 2*BORDER + row * xw.ch);
  1701. XftDrawChange(xw.xft_draw, xw.buf);
  1702. }
  1703. void
  1704. xloadcols(void) {
  1705. int i, r, g, b;
  1706. XRenderColor xft_color = { .alpha = 0 };
  1707. ulong white = WhitePixel(xw.dpy, xw.scr);
  1708. /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
  1709. for(i = 0; i < LEN(colorname); i++) {
  1710. if(!colorname[i])
  1711. continue;
  1712. if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, colorname[i], &dc.xft_col[i])) {
  1713. dc.col[i] = white;
  1714. fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
  1715. } else
  1716. dc.col[i] = dc.xft_col[i].pixel;
  1717. }
  1718. /* load colors [16-255] ; same colors as xterm */
  1719. for(i = 16, r = 0; r < 6; r++)
  1720. for(g = 0; g < 6; g++)
  1721. for(b = 0; b < 6; b++) {
  1722. xft_color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
  1723. xft_color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
  1724. xft_color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
  1725. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &xft_color, &dc.xft_col[i])) {
  1726. dc.col[i] = white;
  1727. fprintf(stderr, "Could not allocate color %d\n", i);
  1728. } else
  1729. dc.col[i] = dc.xft_col[i].pixel;
  1730. i++;
  1731. }
  1732. for(r = 0; r < 24; r++, i++) {
  1733. xft_color.red = xft_color.green = xft_color.blue = 0x0808 + 0x0a0a * r;
  1734. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &xft_color, &dc.xft_col[i])) {
  1735. dc.col[i] = white;
  1736. fprintf(stderr, "Could not allocate color %d\n", i);
  1737. } else
  1738. dc.col[i] = dc.xft_col[i].pixel;
  1739. }
  1740. }
  1741. void
  1742. xclear(int x1, int y1, int x2, int y2) {
  1743. XSetForeground(xw.dpy, dc.gc, dc.col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG]);
  1744. XFillRectangle(xw.dpy, xw.buf, dc.gc,
  1745. BORDER + x1 * xw.cw, BORDER + y1 * xw.ch,
  1746. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
  1747. }
  1748. void
  1749. xhints(void) {
  1750. XClassHint class = {opt_class ? opt_class : TNAME, TNAME};
  1751. XWMHints wm = {.flags = InputHint, .input = 1};
  1752. XSizeHints *sizeh = NULL;
  1753. sizeh = XAllocSizeHints();
  1754. if(xw.isfixed == False) {
  1755. sizeh->flags = PSize | PResizeInc | PBaseSize;
  1756. sizeh->height = xw.h;
  1757. sizeh->width = xw.w;
  1758. sizeh->height_inc = xw.ch;
  1759. sizeh->width_inc = xw.cw;
  1760. sizeh->base_height = 2*BORDER;
  1761. sizeh->base_width = 2*BORDER;
  1762. } else {
  1763. sizeh->flags = PMaxSize | PMinSize;
  1764. sizeh->min_width = sizeh->max_width = xw.fw;
  1765. sizeh->min_height = sizeh->max_height = xw.fh;
  1766. }
  1767. XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm, &class);
  1768. XFree(sizeh);
  1769. }
  1770. void
  1771. xinitfont(Font *f, char *fontstr) {
  1772. f->xft_set = XftFontOpenName(xw.dpy, xw.scr, fontstr);
  1773. if(!f->xft_set)
  1774. die("st: can't open font %s.\n", fontstr);
  1775. f->ascent = f->xft_set->ascent;
  1776. f->descent = f->xft_set->descent;
  1777. f->lbearing = 0;
  1778. f->rbearing = f->xft_set->max_advance_width;
  1779. }
  1780. void
  1781. initfonts(char *fontstr, char *bfontstr, char *ifontstr, char *ibfontstr) {
  1782. xinitfont(&dc.font, fontstr);
  1783. xinitfont(&dc.bfont, bfontstr);
  1784. xinitfont(&dc.ifont, ifontstr);
  1785. xinitfont(&dc.ibfont, ibfontstr);
  1786. }
  1787. void
  1788. xinit(void) {
  1789. XSetWindowAttributes attrs;
  1790. Cursor cursor;
  1791. Window parent;
  1792. int sw, sh, major, minor;
  1793. if(!(xw.dpy = XOpenDisplay(NULL)))
  1794. die("Can't open display\n");
  1795. xw.scr = XDefaultScreen(xw.dpy);
  1796. xw.vis = XDefaultVisual(xw.dpy, xw.scr);
  1797. /* font */
  1798. initfonts(FONT, BOLDFONT, ITALICFONT, ITALICBOLDFONT);
  1799. /* XXX: Assuming same size for bold font */
  1800. xw.cw = dc.font.rbearing - dc.font.lbearing;
  1801. xw.ch = dc.font.ascent + dc.font.descent;
  1802. /* colors */
  1803. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  1804. xloadcols();
  1805. /* adjust fixed window geometry */
  1806. if(xw.isfixed) {
  1807. sw = DisplayWidth(xw.dpy, xw.scr);
  1808. sh = DisplayHeight(xw.dpy, xw.scr);
  1809. if(xw.fx < 0)
  1810. xw.fx = sw + xw.fx - xw.fw - 1;
  1811. if(xw.fy < 0)
  1812. xw.fy = sh + xw.fy - xw.fh - 1;
  1813. xw.h = xw.fh;
  1814. xw.w = xw.fw;
  1815. } else {
  1816. /* window - default size */
  1817. xw.h = 2*BORDER + term.row * xw.ch;
  1818. xw.w = 2*BORDER + term.col * xw.cw;
  1819. xw.fx = 0;
  1820. xw.fy = 0;
  1821. }
  1822. attrs.background_pixel = dc.col[DefaultBG];
  1823. attrs.border_pixel = dc.col[DefaultBG];
  1824. attrs.bit_gravity = NorthWestGravity;
  1825. attrs.event_mask = FocusChangeMask | KeyPressMask
  1826. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  1827. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
  1828. attrs.colormap = xw.cmap;
  1829. parent = opt_embed ? strtol(opt_embed, NULL, 0) : XRootWindow(xw.dpy, xw.scr);
  1830. xw.win = XCreateWindow(xw.dpy, parent, xw.fx, xw.fy,
  1831. xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
  1832. xw.vis,
  1833. CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
  1834. | CWColormap,
  1835. &attrs);
  1836. /* double buffering */
  1837. if(!XdbeQueryExtension(xw.dpy, &major, &minor))
  1838. die("Xdbe extension is not present\n");
  1839. xw.buf = XdbeAllocateBackBufferName(xw.dpy, xw.win, XdbeCopied);
  1840. /* Xft rendering context */
  1841. xw.xft_draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
  1842. /* input methods */
  1843. xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
  1844. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  1845. | XIMStatusNothing, XNClientWindow, xw.win,
  1846. XNFocusWindow, xw.win, NULL);
  1847. /* gc */
  1848. dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
  1849. /* white cursor, black outline */
  1850. cursor = XCreateFontCursor(xw.dpy, XC_xterm);
  1851. XDefineCursor(xw.dpy, xw.win, cursor);
  1852. XRecolorCursor(xw.dpy, cursor,
  1853. &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
  1854. &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
  1855. xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
  1856. xresettitle();
  1857. XMapWindow(xw.dpy, xw.win);
  1858. xhints();
  1859. XSync(xw.dpy, 0);
  1860. }
  1861. void
  1862. xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
  1863. int fg = base.fg, bg = base.bg, temp;
  1864. int winx = BORDER+x*xw.cw, winy = BORDER+y*xw.ch + dc.font.ascent, width = charlen*xw.cw;
  1865. Font *font = &dc.font;
  1866. XGlyphInfo extents;
  1867. int i;
  1868. /* only switch default fg/bg if term is in RV mode */
  1869. if(IS_SET(MODE_REVERSE)) {
  1870. if(fg == DefaultFG)
  1871. fg = DefaultBG;
  1872. if(bg == DefaultBG)
  1873. bg = DefaultFG;
  1874. }
  1875. if(base.mode & ATTR_REVERSE)
  1876. temp = fg, fg = bg, bg = temp;
  1877. if(base.mode & ATTR_BOLD) {
  1878. fg += 8;
  1879. font = &dc.bfont;
  1880. }
  1881. if(base.mode & ATTR_ITALIC)
  1882. font = &dc.ifont;
  1883. if(base.mode & (ATTR_ITALIC|ATTR_ITALIC))
  1884. font = &dc.ibfont;
  1885. XSetBackground(xw.dpy, dc.gc, dc.col[bg]);
  1886. XSetForeground(xw.dpy, dc.gc, dc.col[fg]);
  1887. if(base.mode & ATTR_GFX) {
  1888. for(i = 0; i < bytelen; i++) {
  1889. char c = gfx[(uint)s[i] % 256];
  1890. if(c)
  1891. s[i] = c;
  1892. else if(s[i] > 0x5f)
  1893. s[i] -= 0x5f;
  1894. }
  1895. }
  1896. XftTextExtentsUtf8(xw.dpy, font->xft_set, (FcChar8 *)s, bytelen, &extents);
  1897. width = extents.xOff;
  1898. XftDrawRect(xw.xft_draw, &dc.xft_col[bg], winx, winy - font->ascent, width, xw.ch);
  1899. XftDrawStringUtf8(xw.xft_draw, &dc.xft_col[fg], font->xft_set, winx, winy, (FcChar8 *)s, bytelen);
  1900. if(base.mode & ATTR_UNDERLINE)
  1901. XDrawLine(xw.dpy, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
  1902. }
  1903. void
  1904. xdrawcursor(void) {
  1905. static int oldx = 0;
  1906. static int oldy = 0;
  1907. int sl;
  1908. Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
  1909. LIMIT(oldx, 0, term.col-1);
  1910. LIMIT(oldy, 0, term.row-1);
  1911. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  1912. memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
  1913. /* remove the old cursor */
  1914. if(term.line[oldy][oldx].state & GLYPH_SET) {
  1915. sl = utf8size(term.line[oldy][oldx].c);
  1916. xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1, sl);
  1917. } else
  1918. xclear(oldx, oldy, oldx, oldy);
  1919. /* draw the new one */
  1920. if(!(term.c.state & CURSOR_HIDE)) {
  1921. if(!(xw.state & WIN_FOCUSED))
  1922. g.bg = DefaultUCS;
  1923. if(IS_SET(MODE_REVERSE))
  1924. g.mode |= ATTR_REVERSE, g.fg = DefaultCS, g.bg = DefaultFG;
  1925. sl = utf8size(g.c);
  1926. xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
  1927. oldx = term.c.x, oldy = term.c.y;
  1928. }
  1929. }
  1930. void
  1931. xresettitle(void) {
  1932. XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
  1933. }
  1934. void
  1935. redraw(void) {
  1936. struct timespec tv = {0, REDRAW_TIMEOUT * 1000};
  1937. xclear(0, 0, xw.w, xw.h);
  1938. tfulldirt();
  1939. draw();
  1940. XSync(xw.dpy, False); /* necessary for a good tput flash */
  1941. nanosleep(&tv, NULL);
  1942. }
  1943. void
  1944. draw() {
  1945. XdbeSwapInfo swpinfo[1] = {{xw.win, XdbeCopied}};
  1946. drawregion(0, 0, term.col, term.row);
  1947. XdbeSwapBuffers(xw.dpy, swpinfo, 1);
  1948. }
  1949. void
  1950. drawregion(int x1, int y1, int x2, int y2) {
  1951. int ic, ib, x, y, ox, sl;
  1952. Glyph base, new;
  1953. char buf[DRAW_BUF_SIZ];
  1954. bool ena_sel = sel.bx != -1, alt = IS_SET(MODE_ALTSCREEN);
  1955. if((sel.alt && !alt) || (!sel.alt && alt))
  1956. ena_sel = 0;
  1957. if(!(xw.state & WIN_VISIBLE))
  1958. return;
  1959. for(y = y1; y < y2; y++) {
  1960. if(!term.dirty[y])
  1961. continue;
  1962. xclear(0, y, term.col, y);
  1963. term.dirty[y] = 0;
  1964. base = term.line[y][0];
  1965. ic = ib = ox = 0;
  1966. for(x = x1; x < x2; x++) {
  1967. new = term.line[y][x];
  1968. if(ena_sel && *(new.c) && selected(x, y))
  1969. new.mode ^= ATTR_REVERSE;
  1970. if(ib > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
  1971. ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
  1972. xdraws(buf, base, ox, y, ic, ib);
  1973. ic = ib = 0;
  1974. }
  1975. if(new.state & GLYPH_SET) {
  1976. if(ib == 0) {
  1977. ox = x;
  1978. base = new;
  1979. }
  1980. sl = utf8size(new.c);
  1981. memcpy(buf+ib, new.c, sl);
  1982. ib += sl;
  1983. ++ic;
  1984. }
  1985. }
  1986. if(ib > 0)
  1987. xdraws(buf, base, ox, y, ic, ib);
  1988. }
  1989. xdrawcursor();
  1990. }
  1991. void
  1992. expose(XEvent *ev) {
  1993. XExposeEvent *e = &ev->xexpose;
  1994. if(xw.state & WIN_REDRAW) {
  1995. if(!e->count)
  1996. xw.state &= ~WIN_REDRAW;
  1997. }
  1998. }
  1999. void
  2000. visibility(XEvent *ev) {
  2001. XVisibilityEvent *e = &ev->xvisibility;
  2002. if(e->state == VisibilityFullyObscured)
  2003. xw.state &= ~WIN_VISIBLE;
  2004. else if(!(xw.state & WIN_VISIBLE))
  2005. /* need a full redraw for next Expose, not just a buf copy */
  2006. xw.state |= WIN_VISIBLE | WIN_REDRAW;
  2007. }
  2008. void
  2009. unmap(XEvent *ev) {
  2010. xw.state &= ~WIN_VISIBLE;
  2011. }
  2012. void
  2013. xseturgency(int add) {
  2014. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  2015. h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
  2016. XSetWMHints(xw.dpy, xw.win, h);
  2017. XFree(h);
  2018. }
  2019. void
  2020. focus(XEvent *ev) {
  2021. if(ev->type == FocusIn) {
  2022. xw.state |= WIN_FOCUSED;
  2023. xseturgency(0);
  2024. } else
  2025. xw.state &= ~WIN_FOCUSED;
  2026. }
  2027. char*
  2028. kmap(KeySym k, uint state) {
  2029. int i;
  2030. state &= ~Mod2Mask;
  2031. for(i = 0; i < LEN(key); i++) {
  2032. uint mask = key[i].mask;
  2033. if(key[i].k == k && ((state & mask) == mask || (mask == XK_NO_MOD && !state)))
  2034. return (char*)key[i].s;
  2035. }
  2036. return NULL;
  2037. }
  2038. void
  2039. kpress(XEvent *ev) {
  2040. XKeyEvent *e = &ev->xkey;
  2041. KeySym ksym;
  2042. char buf[32];
  2043. char *customkey;
  2044. int len;
  2045. int meta;
  2046. int shift;
  2047. Status status;
  2048. if (IS_SET(MODE_KBDLOCK))
  2049. return;
  2050. meta = e->state & Mod1Mask;
  2051. shift = e->state & ShiftMask;
  2052. len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
  2053. /* 1. custom keys from config.h */
  2054. if((customkey = kmap(ksym, e->state)))
  2055. ttywrite(customkey, strlen(customkey));
  2056. /* 2. hardcoded (overrides X lookup) */
  2057. else
  2058. switch(ksym) {
  2059. case XK_Up:
  2060. case XK_Down:
  2061. case XK_Left:
  2062. case XK_Right:
  2063. /* XXX: shift up/down doesn't work */
  2064. sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', (shift ? "dacb":"DACB")[ksym - XK_Left]);
  2065. ttywrite(buf, 3);
  2066. break;
  2067. case XK_Insert:
  2068. if(shift)
  2069. selpaste();
  2070. break;
  2071. case XK_Return:
  2072. if(IS_SET(MODE_CRLF))
  2073. ttywrite("\r\n", 2);
  2074. else
  2075. ttywrite("\r", 1);
  2076. break;
  2077. /* 3. X lookup */
  2078. default:
  2079. if(len > 0) {
  2080. if(meta && len == 1)
  2081. ttywrite("\033", 1);
  2082. ttywrite(buf, len);
  2083. }
  2084. break;
  2085. }
  2086. }
  2087. void
  2088. cmessage(XEvent *e) {
  2089. /* See xembed specs
  2090. http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
  2091. if(e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
  2092. if(e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
  2093. xw.state |= WIN_FOCUSED;
  2094. xseturgency(0);
  2095. } else if(e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
  2096. xw.state &= ~WIN_FOCUSED;
  2097. }
  2098. }
  2099. }
  2100. void
  2101. resize(XEvent *e) {
  2102. int col, row;
  2103. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  2104. return;
  2105. xw.w = e->xconfigure.width;
  2106. xw.h = e->xconfigure.height;
  2107. col = (xw.w - 2*BORDER) / xw.cw;
  2108. row = (xw.h - 2*BORDER) / xw.ch;
  2109. if(col == term.col && row == term.row)
  2110. return;
  2111. xclear(0, 0, xw.w, xw.h);
  2112. tresize(col, row);
  2113. xresize(col, row);
  2114. ttyresize();
  2115. }
  2116. void
  2117. run(void) {
  2118. XEvent ev;
  2119. fd_set rfd;
  2120. int xfd = XConnectionNumber(xw.dpy), i;
  2121. struct timeval drawtimeout, *tv = NULL;
  2122. for(i = 0;; i++) {
  2123. FD_ZERO(&rfd);
  2124. FD_SET(cmdfd, &rfd);
  2125. FD_SET(xfd, &rfd);
  2126. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv) < 0) {
  2127. if(errno == EINTR)
  2128. continue;
  2129. die("select failed: %s\n", SERRNO);
  2130. }
  2131. /*
  2132. * Stop after a certain number of reads so the user does not
  2133. * feel like the system is stuttering.
  2134. */
  2135. if(i < 1000 && FD_ISSET(cmdfd, &rfd)) {
  2136. ttyread();
  2137. /*
  2138. * Just wait a bit so it isn't disturbing the
  2139. * user and the system is able to write something.
  2140. */
  2141. drawtimeout.tv_sec = 0;
  2142. drawtimeout.tv_usec = 5;
  2143. tv = &drawtimeout;
  2144. continue;
  2145. }
  2146. i = 0;
  2147. tv = NULL;
  2148. while(XPending(xw.dpy)) {
  2149. XNextEvent(xw.dpy, &ev);
  2150. if(XFilterEvent(&ev, xw.win))
  2151. continue;
  2152. if(handler[ev.type])
  2153. (handler[ev.type])(&ev);
  2154. }
  2155. draw();
  2156. XFlush(xw.dpy);
  2157. }
  2158. }
  2159. int
  2160. main(int argc, char *argv[]) {
  2161. int i, bitm, xr, yr;
  2162. uint wr, hr;
  2163. xw.fw = xw.fh = xw.fx = xw.fy = 0;
  2164. xw.isfixed = False;
  2165. for(i = 1; i < argc; i++) {
  2166. switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
  2167. case 't':
  2168. if(++i < argc) opt_title = argv[i];
  2169. break;
  2170. case 'c':
  2171. if(++i < argc) opt_class = argv[i];
  2172. break;
  2173. case 'w':
  2174. if(++i < argc) opt_embed = argv[i];
  2175. break;
  2176. case 'f':
  2177. if(++i < argc) opt_io = argv[i];
  2178. break;
  2179. case 'e':
  2180. /* eat every remaining arguments */
  2181. if(++i < argc) opt_cmd = &argv[i];
  2182. goto run;
  2183. case 'g':
  2184. if(++i >= argc)
  2185. break;
  2186. bitm = XParseGeometry(argv[i], &xr, &yr, &wr, &hr);
  2187. if(bitm & XValue)
  2188. xw.fx = xr;
  2189. if(bitm & YValue)
  2190. xw.fy = yr;
  2191. if(bitm & WidthValue)
  2192. xw.fw = (int)wr;
  2193. if(bitm & HeightValue)
  2194. xw.fh = (int)hr;
  2195. if(bitm & XNegative && xw.fx == 0)
  2196. xw.fx = -1;
  2197. if(bitm & XNegative && xw.fy == 0)
  2198. xw.fy = -1;
  2199. if(xw.fh != 0 && xw.fw != 0)
  2200. xw.isfixed = True;
  2201. break;
  2202. case 'v':
  2203. default:
  2204. die(USAGE);
  2205. }
  2206. }
  2207. run:
  2208. setlocale(LC_CTYPE, "");
  2209. tnew(80, 24);
  2210. ttynew();
  2211. xinit();
  2212. selinit();
  2213. run();
  2214. return 0;
  2215. }