st.c 58 KB

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