st.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467
  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. 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);
  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) {
  1482. char ascii = *c;
  1483. if(iofd != -1)
  1484. write(iofd, c, 1);
  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. term.esc |= ESC_ALTCHARSET;
  1540. break;
  1541. case 'D': /* IND -- Linefeed */
  1542. if(term.c.y == term.bot)
  1543. tscrollup(term.top, 1);
  1544. else
  1545. tmoveto(term.c.x, term.c.y+1);
  1546. term.esc = 0;
  1547. break;
  1548. case 'E': /* NEL -- Next line */
  1549. tnewline(1); /* always go to first col */
  1550. term.esc = 0;
  1551. break;
  1552. case 'H': /* HTS -- Horizontal tab stop */
  1553. term.tabs[term.c.x] = 1;
  1554. term.esc = 0;
  1555. break;
  1556. case 'M': /* RI -- Reverse index */
  1557. if(term.c.y == term.top)
  1558. tscrolldown(term.top, 1);
  1559. else
  1560. tmoveto(term.c.x, term.c.y-1);
  1561. term.esc = 0;
  1562. break;
  1563. case 'c': /* RIS -- Reset to inital state */
  1564. treset();
  1565. term.esc = 0;
  1566. xclear(0, 0, xw.w, xw.h);
  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. tnew(80, 24);
  2209. ttynew();
  2210. xinit();
  2211. selinit();
  2212. run();
  2213. return 0;
  2214. }