st.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261
  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 <pwd.h>
  9. #include <stdarg.h>
  10. #include <stdbool.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <signal.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/select.h>
  17. #include <sys/stat.h>
  18. #include <sys/time.h>
  19. #include <sys/types.h>
  20. #include <sys/wait.h>
  21. #include <time.h>
  22. #include <unistd.h>
  23. #include <X11/Xatom.h>
  24. #include <X11/Xlib.h>
  25. #include <X11/Xutil.h>
  26. #include <X11/cursorfont.h>
  27. #include <X11/keysym.h>
  28. #include <X11/extensions/Xdbe.h>
  29. #include <X11/Xft/Xft.h>
  30. #include <fontconfig/fontconfig.h>
  31. #define Glyph Glyph_
  32. #define Font Font_
  33. #define Draw XftDraw *
  34. #define Colour XftColor
  35. #define Colourmap Colormap
  36. #if defined(__linux)
  37. #include <pty.h>
  38. #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  39. #include <util.h>
  40. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  41. #include <libutil.h>
  42. #endif
  43. #define USAGE \
  44. "st " VERSION " (c) 2010-2013 st engineers\n" \
  45. "usage: st [-v] [-c class] [-f font] [-g geometry] [-o file]" \
  46. " [-t title] [-w windowid] [-e command ...]\n"
  47. /* XEMBED messages */
  48. #define XEMBED_FOCUS_IN 4
  49. #define XEMBED_FOCUS_OUT 5
  50. /* Arbitrary sizes */
  51. #define UTF_SIZ 4
  52. #define ESC_BUF_SIZ (128*UTF_SIZ)
  53. #define ESC_ARG_SIZ 16
  54. #define STR_BUF_SIZ ESC_BUF_SIZ
  55. #define STR_ARG_SIZ ESC_ARG_SIZ
  56. #define DRAW_BUF_SIZ 20*1024
  57. #define XK_ANY_MOD UINT_MAX
  58. #define XK_NO_MOD 0
  59. #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
  60. /* macros */
  61. #define SERRNO strerror(errno)
  62. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  63. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  64. #define LEN(a) (sizeof(a) / sizeof(a[0]))
  65. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  66. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  67. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  68. #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
  69. #define IS_SET(flag) ((term.mode & (flag)) != 0)
  70. #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
  71. #define VT102ID "\033[?6c"
  72. enum glyph_attribute {
  73. ATTR_NULL = 0,
  74. ATTR_REVERSE = 1,
  75. ATTR_UNDERLINE = 2,
  76. ATTR_BOLD = 4,
  77. ATTR_GFX = 8,
  78. ATTR_ITALIC = 16,
  79. ATTR_BLINK = 32,
  80. };
  81. enum cursor_movement {
  82. CURSOR_SAVE,
  83. CURSOR_LOAD
  84. };
  85. enum cursor_state {
  86. CURSOR_DEFAULT = 0,
  87. CURSOR_WRAPNEXT = 1,
  88. CURSOR_ORIGIN = 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. MODE_HIDE = 512,
  106. MODE_ECHO = 1024,
  107. MODE_APPCURSOR = 2048
  108. };
  109. enum escape_state {
  110. ESC_START = 1,
  111. ESC_CSI = 2,
  112. ESC_STR = 4, /* DSC, OSC, PM, APC */
  113. ESC_ALTCHARSET = 8,
  114. ESC_STR_END = 16, /* a final string was encountered */
  115. ESC_TEST = 32, /* Enter in test mode */
  116. };
  117. enum window_state {
  118. WIN_VISIBLE = 1,
  119. WIN_REDRAW = 2,
  120. WIN_FOCUSED = 4
  121. };
  122. /* bit macro */
  123. #undef B0
  124. enum { B0=1, B1=2, B2=4, B3=8, B4=16, B5=32, B6=64, B7=128 };
  125. typedef unsigned char uchar;
  126. typedef unsigned int uint;
  127. typedef unsigned long ulong;
  128. typedef unsigned short ushort;
  129. typedef struct {
  130. char c[UTF_SIZ]; /* character code */
  131. uchar mode; /* attribute flags */
  132. ushort fg; /* foreground */
  133. ushort bg; /* background */
  134. uchar state; /* state flags */
  135. } Glyph;
  136. typedef Glyph *Line;
  137. typedef struct {
  138. Glyph attr; /* current char attributes */
  139. int x;
  140. int y;
  141. char state;
  142. } TCursor;
  143. /* CSI Escape sequence structs */
  144. /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
  145. typedef struct {
  146. char buf[ESC_BUF_SIZ]; /* raw string */
  147. int len; /* raw string length */
  148. char priv;
  149. int arg[ESC_ARG_SIZ];
  150. int narg; /* nb of args */
  151. char mode;
  152. } CSIEscape;
  153. /* STR Escape sequence structs */
  154. /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
  155. typedef struct {
  156. char type; /* ESC type ... */
  157. char buf[STR_BUF_SIZ]; /* raw string */
  158. int len; /* raw string length */
  159. char *args[STR_ARG_SIZ];
  160. int narg; /* nb of args */
  161. } STREscape;
  162. /* Internal representation of the screen */
  163. typedef struct {
  164. int row; /* nb row */
  165. int col; /* nb col */
  166. Line *line; /* screen */
  167. Line *alt; /* alternate screen */
  168. bool *dirty; /* dirtyness of lines */
  169. TCursor c; /* cursor */
  170. int top; /* top scroll limit */
  171. int bot; /* bottom scroll limit */
  172. int mode; /* terminal mode flags */
  173. int esc; /* escape state flags */
  174. bool numlock; /* lock numbers in keyboard */
  175. bool *tabs;
  176. } Term;
  177. /* Purely graphic info */
  178. typedef struct {
  179. Display *dpy;
  180. Colourmap cmap;
  181. Window win;
  182. Drawable buf;
  183. Atom xembed, wmdeletewin;
  184. XIM xim;
  185. XIC xic;
  186. Draw draw;
  187. Visual *vis;
  188. int scr;
  189. bool isfixed; /* is fixed geometry? */
  190. int fx, fy, fw, fh; /* fixed geometry */
  191. int tw, th; /* tty width and height */
  192. int w, h; /* window width and height */
  193. int ch; /* char height */
  194. int cw; /* char width */
  195. char state; /* focus, redraw, visible */
  196. } XWindow;
  197. typedef struct {
  198. KeySym k;
  199. uint mask;
  200. char s[ESC_BUF_SIZ];
  201. /* three valued logic variables: 0 indifferent, 1 on, -1 off */
  202. signed char appkey; /* application keypad */
  203. signed char appcursor; /* application cursor */
  204. signed char crlf; /* crlf mode */
  205. } Key;
  206. /* TODO: use better name for vars... */
  207. typedef struct {
  208. int mode;
  209. int bx, by;
  210. int ex, ey;
  211. struct {
  212. int x, y;
  213. } b, e;
  214. char *clip;
  215. Atom xtarget;
  216. bool alt;
  217. struct timeval tclick1;
  218. struct timeval tclick2;
  219. } Selection;
  220. typedef union {
  221. int i;
  222. unsigned int ui;
  223. float f;
  224. const void *v;
  225. } Arg;
  226. typedef struct {
  227. unsigned int mod;
  228. KeySym keysym;
  229. void (*func)(const Arg *);
  230. const Arg arg;
  231. } Shortcut;
  232. /* function definitions used in config.h */
  233. static void xzoom(const Arg *);
  234. static void selpaste(const Arg *);
  235. static void numlock(const Arg *);
  236. /* Config.h for applying patches and the configuration. */
  237. #include "config.h"
  238. /* Font structure */
  239. typedef struct {
  240. int height;
  241. int width;
  242. int ascent;
  243. int descent;
  244. short lbearing;
  245. short rbearing;
  246. XftFont *match;
  247. FcFontSet *set;
  248. FcPattern *pattern;
  249. } Font;
  250. /* Drawing Context */
  251. typedef struct {
  252. Colour col[LEN(colorname) < 256 ? 256 : LEN(colorname)];
  253. Font font, bfont, ifont, ibfont;
  254. GC gc;
  255. } DC;
  256. static void die(const char *, ...);
  257. static void draw(void);
  258. static void redraw(int);
  259. static void drawregion(int, int, int, int);
  260. static void execsh(void);
  261. static void sigchld(int);
  262. static void run(void);
  263. static void csidump(void);
  264. static void csihandle(void);
  265. static void csiparse(void);
  266. static void csireset(void);
  267. static void strdump(void);
  268. static void strhandle(void);
  269. static void strparse(void);
  270. static void strreset(void);
  271. static void tclearregion(int, int, int, int, int);
  272. static void tcursor(int);
  273. static void tdeletechar(int);
  274. static void tdeleteline(int);
  275. static void tinsertblank(int);
  276. static void tinsertblankline(int);
  277. static void tmoveto(int, int);
  278. static void tmoveato(int x, int y);
  279. static void tnew(int, int);
  280. static void tnewline(int);
  281. static void tputtab(bool);
  282. static void tputc(char *, int);
  283. static void treset(void);
  284. static int tresize(int, int);
  285. static void tscrollup(int, int);
  286. static void tscrolldown(int, int);
  287. static void tsetattr(int*, int);
  288. static void tsetchar(char *, Glyph *, int, int);
  289. static void tsetscroll(int, int);
  290. static void tswapscreen(void);
  291. static void tsetdirt(int, int);
  292. static void tsetmode(bool, bool, int *, int);
  293. static void tfulldirt(void);
  294. static void techo(char *, int);
  295. static inline bool match(uint, uint);
  296. static void ttynew(void);
  297. static void ttyread(void);
  298. static void ttyresize(void);
  299. static void ttywrite(const char *, size_t);
  300. static void xdraws(char *, Glyph, int, int, int, int);
  301. static void xhints(void);
  302. static void xclear(int, int, int, int);
  303. static void xdrawcursor(void);
  304. static void xinit(void);
  305. static void xloadcols(void);
  306. static int xloadfont(Font *, FcPattern *);
  307. static void xloadfonts(char *, int);
  308. static void xresettitle(void);
  309. static void xseturgency(int);
  310. static void xsetsel(char*);
  311. static void xtermclear(int, int, int, int);
  312. static void xunloadfonts(void);
  313. static void xresize(int, int);
  314. static void expose(XEvent *);
  315. static void visibility(XEvent *);
  316. static void unmap(XEvent *);
  317. static char *kmap(KeySym, uint);
  318. static void kpress(XEvent *);
  319. static void cmessage(XEvent *);
  320. static void cresize(int, int);
  321. static void resize(XEvent *);
  322. static void focus(XEvent *);
  323. static void brelease(XEvent *);
  324. static void bpress(XEvent *);
  325. static void bmotion(XEvent *);
  326. static void selnotify(XEvent *);
  327. static void selclear(XEvent *);
  328. static void selrequest(XEvent *);
  329. static void selinit(void);
  330. static inline bool selected(int, int);
  331. static void selcopy(void);
  332. static void selscroll(int, int);
  333. static int utf8decode(char *, long *);
  334. static int utf8encode(long *, char *);
  335. static int utf8size(char *);
  336. static int isfullutf8(char *, int);
  337. static ssize_t xwrite(int, char *, size_t);
  338. static void *xmalloc(size_t);
  339. static void *xrealloc(void *, size_t);
  340. static void *xcalloc(size_t, size_t);
  341. static void (*handler[LASTEvent])(XEvent *) = {
  342. [KeyPress] = kpress,
  343. [ClientMessage] = cmessage,
  344. [ConfigureNotify] = resize,
  345. [VisibilityNotify] = visibility,
  346. [UnmapNotify] = unmap,
  347. [Expose] = expose,
  348. [FocusIn] = focus,
  349. [FocusOut] = focus,
  350. [MotionNotify] = bmotion,
  351. [ButtonPress] = bpress,
  352. [ButtonRelease] = brelease,
  353. [SelectionClear] = selclear,
  354. [SelectionNotify] = selnotify,
  355. [SelectionRequest] = selrequest,
  356. };
  357. /* Globals */
  358. static DC dc;
  359. static XWindow xw;
  360. static Term term;
  361. static CSIEscape csiescseq;
  362. static STREscape strescseq;
  363. static int cmdfd;
  364. static pid_t pid;
  365. static Selection sel;
  366. static int iofd = -1;
  367. static char **opt_cmd = NULL;
  368. static char *opt_io = NULL;
  369. static char *opt_title = NULL;
  370. static char *opt_embed = NULL;
  371. static char *opt_class = NULL;
  372. static char *opt_font = NULL;
  373. bool usedbe = False;
  374. static char *usedfont = NULL;
  375. static int usedfontsize = 0;
  376. /* Font Ring Cache */
  377. enum {
  378. FRC_NORMAL,
  379. FRC_ITALIC,
  380. FRC_BOLD,
  381. FRC_ITALICBOLD
  382. };
  383. typedef struct {
  384. XftFont *font;
  385. long c;
  386. int flags;
  387. } Fontcache;
  388. /*
  389. * Fontcache is a ring buffer, with frccur as current position and frclen as
  390. * the current length of used elements.
  391. */
  392. static Fontcache frc[1024];
  393. static int frccur = -1, frclen = 0;
  394. ssize_t
  395. xwrite(int fd, char *s, size_t len) {
  396. size_t aux = len;
  397. while(len > 0) {
  398. ssize_t r = write(fd, s, len);
  399. if(r < 0)
  400. return r;
  401. len -= r;
  402. s += r;
  403. }
  404. return aux;
  405. }
  406. void *
  407. xmalloc(size_t len) {
  408. void *p = malloc(len);
  409. if(!p)
  410. die("Out of memory\n");
  411. return p;
  412. }
  413. void *
  414. xrealloc(void *p, size_t len) {
  415. if((p = realloc(p, len)) == NULL)
  416. die("Out of memory\n");
  417. return p;
  418. }
  419. void *
  420. xcalloc(size_t nmemb, size_t size) {
  421. void *p = calloc(nmemb, size);
  422. if(!p)
  423. die("Out of memory\n");
  424. return p;
  425. }
  426. int
  427. utf8decode(char *s, long *u) {
  428. uchar c;
  429. int i, n, rtn;
  430. rtn = 1;
  431. c = *s;
  432. if(~c & B7) { /* 0xxxxxxx */
  433. *u = c;
  434. return rtn;
  435. } else if((c & (B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
  436. *u = c&(B4|B3|B2|B1|B0);
  437. n = 1;
  438. } else if((c & (B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
  439. *u = c&(B3|B2|B1|B0);
  440. n = 2;
  441. } else if((c & (B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
  442. *u = c & (B2|B1|B0);
  443. n = 3;
  444. } else {
  445. goto invalid;
  446. }
  447. for(i = n, ++s; i > 0; --i, ++rtn, ++s) {
  448. c = *s;
  449. if((c & (B7|B6)) != B7) /* 10xxxxxx */
  450. goto invalid;
  451. *u <<= 6;
  452. *u |= c & (B5|B4|B3|B2|B1|B0);
  453. }
  454. if((n == 1 && *u < 0x80) ||
  455. (n == 2 && *u < 0x800) ||
  456. (n == 3 && *u < 0x10000) ||
  457. (*u >= 0xD800 && *u <= 0xDFFF)) {
  458. goto invalid;
  459. }
  460. return rtn;
  461. invalid:
  462. *u = 0xFFFD;
  463. return rtn;
  464. }
  465. int
  466. utf8encode(long *u, char *s) {
  467. uchar *sp;
  468. ulong uc;
  469. int i, n;
  470. sp = (uchar *)s;
  471. uc = *u;
  472. if(uc < 0x80) {
  473. *sp = uc; /* 0xxxxxxx */
  474. return 1;
  475. } else if(*u < 0x800) {
  476. *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
  477. n = 1;
  478. } else if(uc < 0x10000) {
  479. *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
  480. n = 2;
  481. } else if(uc <= 0x10FFFF) {
  482. *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
  483. n = 3;
  484. } else {
  485. goto invalid;
  486. }
  487. for(i=n,++sp; i>0; --i,++sp)
  488. *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
  489. return n+1;
  490. invalid:
  491. /* U+FFFD */
  492. *s++ = '\xEF';
  493. *s++ = '\xBF';
  494. *s = '\xBD';
  495. return 3;
  496. }
  497. /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
  498. UTF-8 otherwise return 0 */
  499. int
  500. isfullutf8(char *s, int b) {
  501. uchar *c1, *c2, *c3;
  502. c1 = (uchar *)s;
  503. c2 = (uchar *)++s;
  504. c3 = (uchar *)++s;
  505. if(b < 1) {
  506. return 0;
  507. } else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1) {
  508. return 0;
  509. } else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
  510. ((b == 1) ||
  511. ((b == 2) && (*c2&(B7|B6)) == B7))) {
  512. return 0;
  513. } else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
  514. ((b == 1) ||
  515. ((b == 2) && (*c2&(B7|B6)) == B7) ||
  516. ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7))) {
  517. return 0;
  518. } else {
  519. return 1;
  520. }
  521. }
  522. int
  523. utf8size(char *s) {
  524. uchar c = *s;
  525. if(~c&B7) {
  526. return 1;
  527. } else if((c&(B7|B6|B5)) == (B7|B6)) {
  528. return 2;
  529. } else if((c&(B7|B6|B5|B4)) == (B7|B6|B5)) {
  530. return 3;
  531. } else {
  532. return 4;
  533. }
  534. }
  535. void
  536. selinit(void) {
  537. memset(&sel.tclick1, 0, sizeof(sel.tclick1));
  538. memset(&sel.tclick2, 0, sizeof(sel.tclick2));
  539. sel.mode = 0;
  540. sel.bx = -1;
  541. sel.clip = NULL;
  542. sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
  543. if(sel.xtarget == None)
  544. sel.xtarget = XA_STRING;
  545. }
  546. static int
  547. x2col(int x) {
  548. x -= borderpx;
  549. x /= xw.cw;
  550. return LIMIT(x, 0, term.col-1);
  551. }
  552. static int
  553. y2row(int y) {
  554. y -= borderpx;
  555. y /= xw.ch;
  556. return LIMIT(y, 0, term.row-1);
  557. }
  558. static inline bool
  559. selected(int x, int y) {
  560. int bx, ex;
  561. if(sel.ey == y && sel.by == y) {
  562. bx = MIN(sel.bx, sel.ex);
  563. ex = MAX(sel.bx, sel.ex);
  564. return BETWEEN(x, bx, ex);
  565. }
  566. return ((sel.b.y < y && y < sel.e.y)
  567. || (y == sel.e.y && x <= sel.e.x))
  568. || (y == sel.b.y && x >= sel.b.x
  569. && (x <= sel.e.x || sel.b.y != sel.e.y));
  570. }
  571. void
  572. getbuttoninfo(XEvent *e) {
  573. sel.alt = IS_SET(MODE_ALTSCREEN);
  574. sel.ex = x2col(e->xbutton.x);
  575. sel.ey = y2row(e->xbutton.y);
  576. sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
  577. sel.b.y = MIN(sel.by, sel.ey);
  578. sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
  579. sel.e.y = MAX(sel.by, sel.ey);
  580. }
  581. void
  582. mousereport(XEvent *e) {
  583. int x = x2col(e->xbutton.x);
  584. int y = y2row(e->xbutton.y);
  585. int button = e->xbutton.button;
  586. int state = e->xbutton.state;
  587. char buf[] = { '\033', '[', 'M', 0, 32+x+1, 32+y+1 };
  588. static int ob, ox, oy;
  589. /* from urxvt */
  590. if(e->xbutton.type == MotionNotify) {
  591. if(!IS_SET(MODE_MOUSEMOTION) || (x == ox && y == oy))
  592. return;
  593. button = ob + 32;
  594. ox = x, oy = y;
  595. } else if(e->xbutton.type == ButtonRelease || button == AnyButton) {
  596. button = 3;
  597. } else {
  598. button -= Button1;
  599. if(button >= 3)
  600. button += 64 - 3;
  601. if(e->xbutton.type == ButtonPress) {
  602. ob = button;
  603. ox = x, oy = y;
  604. }
  605. }
  606. buf[3] = 32 + button + (state & ShiftMask ? 4 : 0)
  607. + (state & Mod4Mask ? 8 : 0)
  608. + (state & ControlMask ? 16 : 0);
  609. ttywrite(buf, sizeof(buf));
  610. }
  611. void
  612. bpress(XEvent *e) {
  613. if(IS_SET(MODE_MOUSE)) {
  614. mousereport(e);
  615. } else if(e->xbutton.button == Button1) {
  616. if(sel.bx != -1) {
  617. sel.bx = -1;
  618. tsetdirt(sel.b.y, sel.e.y);
  619. draw();
  620. }
  621. sel.mode = 1;
  622. sel.ex = sel.bx = x2col(e->xbutton.x);
  623. sel.ey = sel.by = y2row(e->xbutton.y);
  624. } else if(e->xbutton.button == Button4) {
  625. ttywrite("\031", 1);
  626. } else if(e->xbutton.button == Button5) {
  627. ttywrite("\005", 1);
  628. }
  629. }
  630. void
  631. selcopy(void) {
  632. char *str, *ptr, *p;
  633. int x, y, bufsize, is_selected = 0, size;
  634. Glyph *gp, *last;
  635. if(sel.bx == -1) {
  636. str = NULL;
  637. } else {
  638. bufsize = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
  639. ptr = str = xmalloc(bufsize);
  640. /* append every set & selected glyph to the selection */
  641. for(y = 0; y < term.row; y++) {
  642. gp = &term.line[y][0];
  643. last = gp + term.col;
  644. while(--last >= gp && !(last->state & GLYPH_SET))
  645. /* nothing */;
  646. for(x = 0; gp <= last; x++, ++gp) {
  647. if(!(is_selected = selected(x, y)))
  648. continue;
  649. p = (gp->state & GLYPH_SET) ? gp->c : " ";
  650. size = utf8size(p);
  651. memcpy(ptr, p, size);
  652. ptr += size;
  653. }
  654. /* \n at the end of every selected line except for the last one */
  655. if(is_selected && y < sel.e.y)
  656. *ptr++ = '\n';
  657. }
  658. *ptr = 0;
  659. }
  660. xsetsel(str);
  661. }
  662. void
  663. selnotify(XEvent *e) {
  664. ulong nitems, ofs, rem;
  665. int format;
  666. uchar *data;
  667. Atom type;
  668. ofs = 0;
  669. do {
  670. if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
  671. False, AnyPropertyType, &type, &format,
  672. &nitems, &rem, &data)) {
  673. fprintf(stderr, "Clipboard allocation failed\n");
  674. return;
  675. }
  676. ttywrite((const char *) data, nitems * format / 8);
  677. XFree(data);
  678. /* number of 32-bit chunks returned */
  679. ofs += nitems * format / 32;
  680. } while(rem > 0);
  681. }
  682. void
  683. selpaste(const Arg *dummy) {
  684. XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY,
  685. xw.win, CurrentTime);
  686. }
  687. void selclear(XEvent *e) {
  688. if(sel.bx == -1)
  689. return;
  690. sel.bx = -1;
  691. tsetdirt(sel.b.y, sel.e.y);
  692. }
  693. void
  694. selrequest(XEvent *e) {
  695. fprintf(stderr, "selrequest\n");
  696. XSelectionRequestEvent *xsre;
  697. XSelectionEvent xev;
  698. Atom xa_targets, string;
  699. xsre = (XSelectionRequestEvent *) e;
  700. xev.type = SelectionNotify;
  701. xev.requestor = xsre->requestor;
  702. xev.selection = xsre->selection;
  703. xev.target = xsre->target;
  704. xev.time = xsre->time;
  705. /* reject */
  706. xev.property = None;
  707. xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
  708. if(xsre->target == xa_targets) {
  709. /* respond with the supported type */
  710. string = sel.xtarget;
  711. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  712. XA_ATOM, 32, PropModeReplace,
  713. (uchar *) &string, 1);
  714. xev.property = xsre->property;
  715. } else if(xsre->target == sel.xtarget && sel.clip != NULL) {
  716. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  717. xsre->target, 8, PropModeReplace,
  718. (uchar *) sel.clip, strlen(sel.clip));
  719. xev.property = xsre->property;
  720. }
  721. /* all done, send a notification to the listener */
  722. if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
  723. fprintf(stderr, "Error sending SelectionNotify event\n");
  724. }
  725. void
  726. xsetsel(char *str) {
  727. fprintf(stderr, "xsetsel: %s\n", str);
  728. /* register the selection for both the clipboard and the primary */
  729. Atom clipboard;
  730. free(sel.clip);
  731. sel.clip = str;
  732. XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
  733. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  734. XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
  735. }
  736. void
  737. brelease(XEvent *e) {
  738. fprintf(stderr, "brelease\n");
  739. struct timeval now;
  740. if(IS_SET(MODE_MOUSE)) {
  741. mousereport(e);
  742. return;
  743. }
  744. if(e->xbutton.button == Button2) {
  745. selpaste(NULL);
  746. } else if(e->xbutton.button == Button1) {
  747. sel.mode = 0;
  748. getbuttoninfo(e);
  749. term.dirty[sel.ey] = 1;
  750. if(sel.bx == sel.ex && sel.by == sel.ey) {
  751. sel.bx = -1;
  752. gettimeofday(&now, NULL);
  753. if(TIMEDIFF(now, sel.tclick2) <= tripleclicktimeout) {
  754. /* triple click on the line */
  755. sel.b.x = sel.bx = 0;
  756. sel.e.x = sel.ex = term.col;
  757. sel.b.y = sel.e.y = sel.ey;
  758. selcopy();
  759. } else if(TIMEDIFF(now, sel.tclick1) <= doubleclicktimeout) {
  760. /* double click to select word */
  761. sel.bx = sel.ex;
  762. while(sel.bx > 0 && term.line[sel.ey][sel.bx-1].state & GLYPH_SET &&
  763. term.line[sel.ey][sel.bx-1].c[0] != ' ') {
  764. sel.bx--;
  765. }
  766. sel.b.x = sel.bx;
  767. while(sel.ex < term.col-1 && term.line[sel.ey][sel.ex+1].state & GLYPH_SET &&
  768. term.line[sel.ey][sel.ex+1].c[0] != ' ') {
  769. sel.ex++;
  770. }
  771. sel.e.x = sel.ex;
  772. sel.b.y = sel.e.y = sel.ey;
  773. selcopy();
  774. }
  775. } else {
  776. selcopy();
  777. }
  778. }
  779. memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
  780. gettimeofday(&sel.tclick1, NULL);
  781. }
  782. void
  783. bmotion(XEvent *e) {
  784. int starty, endy, oldey, oldex;
  785. if(IS_SET(MODE_MOUSE)) {
  786. mousereport(e);
  787. return;
  788. }
  789. if(!sel.mode)
  790. return;
  791. oldey = sel.ey;
  792. oldex = sel.ex;
  793. getbuttoninfo(e);
  794. if(oldey != sel.ey || oldex != sel.ex) {
  795. starty = MIN(oldey, sel.ey);
  796. endy = MAX(oldey, sel.ey);
  797. tsetdirt(starty, endy);
  798. }
  799. }
  800. void
  801. die(const char *errstr, ...) {
  802. va_list ap;
  803. va_start(ap, errstr);
  804. vfprintf(stderr, errstr, ap);
  805. va_end(ap);
  806. exit(EXIT_FAILURE);
  807. }
  808. void
  809. execsh(void) {
  810. char **args;
  811. char *envshell = getenv("SHELL");
  812. const struct passwd *pass = getpwuid(getuid());
  813. char buf[sizeof(long) * 8 + 1];
  814. unsetenv("COLUMNS");
  815. unsetenv("LINES");
  816. unsetenv("TERMCAP");
  817. if(pass) {
  818. setenv("LOGNAME", pass->pw_name, 1);
  819. setenv("USER", pass->pw_name, 1);
  820. setenv("SHELL", pass->pw_shell, 0);
  821. setenv("HOME", pass->pw_dir, 0);
  822. }
  823. snprintf(buf, sizeof(buf), "%lu", xw.win);
  824. setenv("WINDOWID", buf, 1);
  825. signal(SIGCHLD, SIG_DFL);
  826. signal(SIGHUP, SIG_DFL);
  827. signal(SIGINT, SIG_DFL);
  828. signal(SIGQUIT, SIG_DFL);
  829. signal(SIGTERM, SIG_DFL);
  830. signal(SIGALRM, SIG_DFL);
  831. DEFAULT(envshell, shell);
  832. setenv("TERM", termname, 1);
  833. args = opt_cmd ? opt_cmd : (char *[]){envshell, "-i", NULL};
  834. execvp(args[0], args);
  835. exit(EXIT_FAILURE);
  836. }
  837. void
  838. sigchld(int a) {
  839. int stat = 0;
  840. if(waitpid(pid, &stat, 0) < 0)
  841. die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
  842. if(WIFEXITED(stat)) {
  843. exit(WEXITSTATUS(stat));
  844. } else {
  845. exit(EXIT_FAILURE);
  846. }
  847. }
  848. void
  849. ttynew(void) {
  850. int m, s;
  851. struct winsize w = {term.row, term.col, 0, 0};
  852. /* seems to work fine on linux, openbsd and freebsd */
  853. if(openpty(&m, &s, NULL, NULL, &w) < 0)
  854. die("openpty failed: %s\n", SERRNO);
  855. switch(pid = fork()) {
  856. case -1:
  857. die("fork failed\n");
  858. break;
  859. case 0:
  860. setsid(); /* create a new process group */
  861. dup2(s, STDIN_FILENO);
  862. dup2(s, STDOUT_FILENO);
  863. dup2(s, STDERR_FILENO);
  864. if(ioctl(s, TIOCSCTTY, NULL) < 0)
  865. die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
  866. close(s);
  867. close(m);
  868. execsh();
  869. break;
  870. default:
  871. close(s);
  872. cmdfd = m;
  873. signal(SIGCHLD, sigchld);
  874. if(opt_io) {
  875. iofd = (!strcmp(opt_io, "-")) ?
  876. STDOUT_FILENO :
  877. open(opt_io, O_WRONLY | O_CREAT, 0666);
  878. if(iofd < 0) {
  879. fprintf(stderr, "Error opening %s:%s\n",
  880. opt_io, strerror(errno));
  881. }
  882. }
  883. }
  884. }
  885. void
  886. dump(char c) {
  887. static int col;
  888. fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
  889. if(++col % 10 == 0)
  890. fprintf(stderr, "\n");
  891. }
  892. void
  893. ttyread(void) {
  894. static char buf[BUFSIZ];
  895. static int buflen = 0;
  896. char *ptr;
  897. char s[UTF_SIZ];
  898. int charsize; /* size of utf8 char in bytes */
  899. long utf8c;
  900. int ret;
  901. /* append read bytes to unprocessed bytes */
  902. if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
  903. die("Couldn't read from shell: %s\n", SERRNO);
  904. /* process every complete utf8 char */
  905. buflen += ret;
  906. ptr = buf;
  907. while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
  908. charsize = utf8decode(ptr, &utf8c);
  909. utf8encode(&utf8c, s);
  910. tputc(s, charsize);
  911. ptr += charsize;
  912. buflen -= charsize;
  913. }
  914. /* keep any uncomplete utf8 char for the next call */
  915. memmove(buf, ptr, buflen);
  916. }
  917. void
  918. ttywrite(const char *s, size_t n) {
  919. if(write(cmdfd, s, n) == -1)
  920. die("write error on tty: %s\n", SERRNO);
  921. }
  922. void
  923. ttyresize(void) {
  924. struct winsize w;
  925. w.ws_row = term.row;
  926. w.ws_col = term.col;
  927. w.ws_xpixel = xw.tw;
  928. w.ws_ypixel = xw.th;
  929. if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  930. fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
  931. }
  932. void
  933. tsetdirt(int top, int bot) {
  934. int i;
  935. LIMIT(top, 0, term.row-1);
  936. LIMIT(bot, 0, term.row-1);
  937. for(i = top; i <= bot; i++)
  938. term.dirty[i] = 1;
  939. }
  940. void
  941. tfulldirt(void) {
  942. tsetdirt(0, term.row-1);
  943. }
  944. void
  945. tcursor(int mode) {
  946. static TCursor c;
  947. if(mode == CURSOR_SAVE) {
  948. c = term.c;
  949. } else if(mode == CURSOR_LOAD) {
  950. term.c = c;
  951. tmoveto(c.x, c.y);
  952. }
  953. }
  954. void
  955. treset(void) {
  956. uint i;
  957. term.c = (TCursor){{
  958. .mode = ATTR_NULL,
  959. .fg = defaultfg,
  960. .bg = defaultbg
  961. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  962. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  963. for(i = tabspaces; i < term.col; i += tabspaces)
  964. term.tabs[i] = 1;
  965. term.top = 0;
  966. term.bot = term.row - 1;
  967. term.mode = MODE_WRAP;
  968. tclearregion(0, 0, term.col-1, term.row-1, 0);
  969. tmoveto(0, 0);
  970. tcursor(CURSOR_SAVE);
  971. }
  972. void
  973. tnew(int col, int row) {
  974. /* set screen size */
  975. term.row = row;
  976. term.col = col;
  977. term.line = xmalloc(term.row * sizeof(Line));
  978. term.alt = xmalloc(term.row * sizeof(Line));
  979. term.dirty = xmalloc(term.row * sizeof(*term.dirty));
  980. term.tabs = xmalloc(term.col * sizeof(*term.tabs));
  981. for(row = 0; row < term.row; row++) {
  982. term.line[row] = xmalloc(term.col * sizeof(Glyph));
  983. term.alt [row] = xmalloc(term.col * sizeof(Glyph));
  984. term.dirty[row] = 0;
  985. }
  986. term.numlock = 1;
  987. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  988. /* setup screen */
  989. treset();
  990. }
  991. void
  992. tswapscreen(void) {
  993. Line *tmp = term.line;
  994. term.line = term.alt;
  995. term.alt = tmp;
  996. term.mode ^= MODE_ALTSCREEN;
  997. tfulldirt();
  998. }
  999. void
  1000. tscrolldown(int orig, int n) {
  1001. int i;
  1002. Line temp;
  1003. LIMIT(n, 0, term.bot-orig+1);
  1004. tclearregion(0, term.bot-n+1, term.col-1, term.bot, 0);
  1005. for(i = term.bot; i >= orig+n; i--) {
  1006. temp = term.line[i];
  1007. term.line[i] = term.line[i-n];
  1008. term.line[i-n] = temp;
  1009. term.dirty[i] = 1;
  1010. term.dirty[i-n] = 1;
  1011. }
  1012. selscroll(orig, n);
  1013. }
  1014. void
  1015. tscrollup(int orig, int n) {
  1016. int i;
  1017. Line temp;
  1018. LIMIT(n, 0, term.bot-orig+1);
  1019. tclearregion(0, orig, term.col-1, orig+n-1, 0);
  1020. for(i = orig; i <= term.bot-n; i++) {
  1021. temp = term.line[i];
  1022. term.line[i] = term.line[i+n];
  1023. term.line[i+n] = temp;
  1024. term.dirty[i] = 1;
  1025. term.dirty[i+n] = 1;
  1026. }
  1027. selscroll(orig, -n);
  1028. }
  1029. void
  1030. selscroll(int orig, int n) {
  1031. if(sel.bx == -1)
  1032. return;
  1033. if(BETWEEN(sel.by, orig, term.bot) || BETWEEN(sel.ey, orig, term.bot)) {
  1034. if((sel.by += n) > term.bot || (sel.ey += n) < term.top) {
  1035. sel.bx = -1;
  1036. return;
  1037. }
  1038. if(sel.by < term.top) {
  1039. sel.by = term.top;
  1040. sel.bx = 0;
  1041. }
  1042. if(sel.ey > term.bot) {
  1043. sel.ey = term.bot;
  1044. sel.ex = term.col;
  1045. }
  1046. sel.b.y = sel.by, sel.b.x = sel.bx;
  1047. sel.e.y = sel.ey, sel.e.x = sel.ex;
  1048. }
  1049. }
  1050. void
  1051. tnewline(int first_col) {
  1052. int y = term.c.y;
  1053. if(y == term.bot) {
  1054. tscrollup(term.top, 1);
  1055. } else {
  1056. y++;
  1057. }
  1058. tmoveto(first_col ? 0 : term.c.x, y);
  1059. }
  1060. void
  1061. csiparse(void) {
  1062. /* int noarg = 1; */
  1063. char *p = csiescseq.buf;
  1064. csiescseq.narg = 0;
  1065. if(*p == '?')
  1066. csiescseq.priv = 1, p++;
  1067. while(p < csiescseq.buf+csiescseq.len) {
  1068. while(isdigit(*p)) {
  1069. csiescseq.arg[csiescseq.narg] *= 10;
  1070. csiescseq.arg[csiescseq.narg] += *p++ - '0'/*, noarg = 0 */;
  1071. }
  1072. if(*p == ';' && csiescseq.narg+1 < ESC_ARG_SIZ) {
  1073. csiescseq.narg++, p++;
  1074. } else {
  1075. csiescseq.mode = *p;
  1076. csiescseq.narg++;
  1077. return;
  1078. }
  1079. }
  1080. }
  1081. /* for absolute user moves, when decom is set */
  1082. void
  1083. tmoveato(int x, int y) {
  1084. tmoveto(x, y + ((term.c.state & CURSOR_ORIGIN) ? term.top: 0));
  1085. }
  1086. void
  1087. tmoveto(int x, int y) {
  1088. int miny, maxy;
  1089. if(term.c.state & CURSOR_ORIGIN) {
  1090. miny = term.top;
  1091. maxy = term.bot;
  1092. } else {
  1093. miny = 0;
  1094. maxy = term.row - 1;
  1095. }
  1096. LIMIT(x, 0, term.col-1);
  1097. LIMIT(y, miny, maxy);
  1098. term.c.state &= ~CURSOR_WRAPNEXT;
  1099. term.c.x = x;
  1100. term.c.y = y;
  1101. }
  1102. void
  1103. tsetchar(char *c, Glyph *attr, int x, int y) {
  1104. static char *vt100_0[62] = { /* 0x41 - 0x7e */
  1105. "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
  1106. 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
  1107. 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
  1108. 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
  1109. "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
  1110. "␤", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
  1111. "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
  1112. "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
  1113. };
  1114. /*
  1115. * The table is proudly stolen from rxvt.
  1116. */
  1117. if(attr->mode & ATTR_GFX) {
  1118. if(c[0] >= 0x41 && c[0] <= 0x7e
  1119. && vt100_0[c[0] - 0x41]) {
  1120. c = vt100_0[c[0] - 0x41];
  1121. }
  1122. }
  1123. term.dirty[y] = 1;
  1124. term.line[y][x] = *attr;
  1125. memcpy(term.line[y][x].c, c, UTF_SIZ);
  1126. term.line[y][x].state |= GLYPH_SET;
  1127. }
  1128. void
  1129. tclearregion(int x1, int y1, int x2, int y2, int bce) {
  1130. int x, y, temp;
  1131. if(x1 > x2)
  1132. temp = x1, x1 = x2, x2 = temp;
  1133. if(y1 > y2)
  1134. temp = y1, y1 = y2, y2 = temp;
  1135. LIMIT(x1, 0, term.col-1);
  1136. LIMIT(x2, 0, term.col-1);
  1137. LIMIT(y1, 0, term.row-1);
  1138. LIMIT(y2, 0, term.row-1);
  1139. for(y = y1; y <= y2; y++) {
  1140. term.dirty[y] = 1;
  1141. for(x = x1; x <= x2; x++) {
  1142. if(bce) {
  1143. term.line[y][x] = term.c.attr;
  1144. memcpy(term.line[y][x].c, " ", 2);
  1145. term.line[y][x].state |= GLYPH_SET;
  1146. } else {
  1147. term.line[y][x].state = 0;
  1148. }
  1149. }
  1150. }
  1151. }
  1152. void
  1153. tdeletechar(int n) {
  1154. int src = term.c.x + n;
  1155. int dst = term.c.x;
  1156. int size = term.col - src;
  1157. term.dirty[term.c.y] = 1;
  1158. if(src >= term.col) {
  1159. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y, 0);
  1160. return;
  1161. }
  1162. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src],
  1163. size * sizeof(Glyph));
  1164. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y, 0);
  1165. }
  1166. void
  1167. tinsertblank(int n) {
  1168. int src = term.c.x;
  1169. int dst = src + n;
  1170. int size = term.col - dst;
  1171. term.dirty[term.c.y] = 1;
  1172. if(dst >= term.col) {
  1173. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y, 0);
  1174. return;
  1175. }
  1176. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src],
  1177. size * sizeof(Glyph));
  1178. tclearregion(src, term.c.y, dst - 1, term.c.y, 0);
  1179. }
  1180. void
  1181. tinsertblankline(int n) {
  1182. if(term.c.y < term.top || term.c.y > term.bot)
  1183. return;
  1184. tscrolldown(term.c.y, n);
  1185. }
  1186. void
  1187. tdeleteline(int n) {
  1188. if(term.c.y < term.top || term.c.y > term.bot)
  1189. return;
  1190. tscrollup(term.c.y, n);
  1191. }
  1192. void
  1193. tsetattr(int *attr, int l) {
  1194. int i;
  1195. for(i = 0; i < l; i++) {
  1196. switch(attr[i]) {
  1197. case 0:
  1198. term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD \
  1199. | ATTR_ITALIC | ATTR_BLINK);
  1200. term.c.attr.fg = defaultfg;
  1201. term.c.attr.bg = defaultbg;
  1202. break;
  1203. case 1:
  1204. term.c.attr.mode |= ATTR_BOLD;
  1205. break;
  1206. case 3:
  1207. term.c.attr.mode |= ATTR_ITALIC;
  1208. break;
  1209. case 4:
  1210. term.c.attr.mode |= ATTR_UNDERLINE;
  1211. break;
  1212. case 5:
  1213. term.c.attr.mode |= ATTR_BLINK;
  1214. break;
  1215. case 7:
  1216. term.c.attr.mode |= ATTR_REVERSE;
  1217. break;
  1218. case 21:
  1219. case 22:
  1220. term.c.attr.mode &= ~ATTR_BOLD;
  1221. break;
  1222. case 23:
  1223. term.c.attr.mode &= ~ATTR_ITALIC;
  1224. break;
  1225. case 24:
  1226. term.c.attr.mode &= ~ATTR_UNDERLINE;
  1227. break;
  1228. case 25:
  1229. term.c.attr.mode &= ~ATTR_BLINK;
  1230. break;
  1231. case 27:
  1232. term.c.attr.mode &= ~ATTR_REVERSE;
  1233. break;
  1234. case 38:
  1235. if(i + 2 < l && attr[i + 1] == 5) {
  1236. i += 2;
  1237. if(BETWEEN(attr[i], 0, 255)) {
  1238. term.c.attr.fg = attr[i];
  1239. } else {
  1240. fprintf(stderr,
  1241. "erresc: bad fgcolor %d\n",
  1242. attr[i]);
  1243. }
  1244. } else {
  1245. fprintf(stderr,
  1246. "erresc(38): gfx attr %d unknown\n",
  1247. attr[i]);
  1248. }
  1249. break;
  1250. case 39:
  1251. term.c.attr.fg = defaultfg;
  1252. break;
  1253. case 48:
  1254. if(i + 2 < l && attr[i + 1] == 5) {
  1255. i += 2;
  1256. if(BETWEEN(attr[i], 0, 255)) {
  1257. term.c.attr.bg = attr[i];
  1258. } else {
  1259. fprintf(stderr,
  1260. "erresc: bad bgcolor %d\n",
  1261. attr[i]);
  1262. }
  1263. } else {
  1264. fprintf(stderr,
  1265. "erresc(48): gfx attr %d unknown\n",
  1266. attr[i]);
  1267. }
  1268. break;
  1269. case 49:
  1270. term.c.attr.bg = defaultbg;
  1271. break;
  1272. default:
  1273. if(BETWEEN(attr[i], 30, 37)) {
  1274. term.c.attr.fg = attr[i] - 30;
  1275. } else if(BETWEEN(attr[i], 40, 47)) {
  1276. term.c.attr.bg = attr[i] - 40;
  1277. } else if(BETWEEN(attr[i], 90, 97)) {
  1278. term.c.attr.fg = attr[i] - 90 + 8;
  1279. } else if(BETWEEN(attr[i], 100, 107)) {
  1280. term.c.attr.bg = attr[i] - 100 + 8;
  1281. } else {
  1282. fprintf(stderr,
  1283. "erresc(default): gfx attr %d unknown\n",
  1284. attr[i]), csidump();
  1285. }
  1286. break;
  1287. }
  1288. }
  1289. }
  1290. void
  1291. tsetscroll(int t, int b) {
  1292. int temp;
  1293. LIMIT(t, 0, term.row-1);
  1294. LIMIT(b, 0, term.row-1);
  1295. if(t > b) {
  1296. temp = t;
  1297. t = b;
  1298. b = temp;
  1299. }
  1300. term.top = t;
  1301. term.bot = b;
  1302. }
  1303. #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
  1304. void
  1305. tsetmode(bool priv, bool set, int *args, int narg) {
  1306. int *lim, mode;
  1307. bool alt;
  1308. for(lim = args + narg; args < lim; ++args) {
  1309. if(priv) {
  1310. switch(*args) {
  1311. break;
  1312. case 1: /* DECCKM -- Cursor key */
  1313. MODBIT(term.mode, set, MODE_APPCURSOR);
  1314. break;
  1315. case 5: /* DECSCNM -- Reverse video */
  1316. mode = term.mode;
  1317. MODBIT(term.mode, set, MODE_REVERSE);
  1318. if(mode != term.mode)
  1319. redraw(REDRAW_TIMEOUT);
  1320. break;
  1321. case 6: /* DECOM -- Origin */
  1322. MODBIT(term.c.state, set, CURSOR_ORIGIN);
  1323. tmoveato(0, 0);
  1324. break;
  1325. case 7: /* DECAWM -- Auto wrap */
  1326. MODBIT(term.mode, set, MODE_WRAP);
  1327. break;
  1328. case 0: /* Error (IGNORED) */
  1329. case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
  1330. case 3: /* DECCOLM -- Column (IGNORED) */
  1331. case 4: /* DECSCLM -- Scroll (IGNORED) */
  1332. case 8: /* DECARM -- Auto repeat (IGNORED) */
  1333. case 18: /* DECPFF -- Printer feed (IGNORED) */
  1334. case 19: /* DECPEX -- Printer extent (IGNORED) */
  1335. case 42: /* DECNRCM -- National characters (IGNORED) */
  1336. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1337. break;
  1338. case 25: /* DECTCEM -- Text Cursor Enable Mode */
  1339. MODBIT(term.mode, !set, MODE_HIDE);
  1340. break;
  1341. case 1000: /* 1000,1002: enable xterm mouse report */
  1342. MODBIT(term.mode, set, MODE_MOUSEBTN);
  1343. break;
  1344. case 1002:
  1345. MODBIT(term.mode, set, MODE_MOUSEMOTION);
  1346. break;
  1347. case 1049: /* = 1047 and 1048 */
  1348. case 47:
  1349. case 1047: {
  1350. alt = IS_SET(MODE_ALTSCREEN);
  1351. if(alt) {
  1352. tclearregion(0, 0, term.col-1,
  1353. term.row-1, 0);
  1354. }
  1355. if(set ^ alt) /* set is always 1 or 0 */
  1356. tswapscreen();
  1357. if(*args != 1049)
  1358. break;
  1359. }
  1360. /* pass through */
  1361. case 1048:
  1362. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1363. break;
  1364. default:
  1365. fprintf(stderr,
  1366. "erresc: unknown private set/reset mode %d\n",
  1367. *args);
  1368. break;
  1369. }
  1370. } else {
  1371. switch(*args) {
  1372. case 0: /* Error (IGNORED) */
  1373. break;
  1374. case 2: /* KAM -- keyboard action */
  1375. MODBIT(term.mode, set, MODE_KBDLOCK);
  1376. break;
  1377. case 4: /* IRM -- Insertion-replacement */
  1378. MODBIT(term.mode, set, MODE_INSERT);
  1379. break;
  1380. case 12: /* SRM -- Send/Receive */
  1381. MODBIT(term.mode, !set, MODE_ECHO);
  1382. break;
  1383. case 20: /* LNM -- Linefeed/new line */
  1384. MODBIT(term.mode, set, MODE_CRLF);
  1385. break;
  1386. default:
  1387. fprintf(stderr,
  1388. "erresc: unknown set/reset mode %d\n",
  1389. *args);
  1390. break;
  1391. }
  1392. }
  1393. }
  1394. }
  1395. #undef MODBIT
  1396. void
  1397. csihandle(void) {
  1398. switch(csiescseq.mode) {
  1399. default:
  1400. unknown:
  1401. fprintf(stderr, "erresc: unknown csi ");
  1402. csidump();
  1403. /* die(""); */
  1404. break;
  1405. case '@': /* ICH -- Insert <n> blank char */
  1406. DEFAULT(csiescseq.arg[0], 1);
  1407. tinsertblank(csiescseq.arg[0]);
  1408. break;
  1409. case 'A': /* CUU -- Cursor <n> Up */
  1410. DEFAULT(csiescseq.arg[0], 1);
  1411. tmoveto(term.c.x, term.c.y-csiescseq.arg[0]);
  1412. break;
  1413. case 'B': /* CUD -- Cursor <n> Down */
  1414. case 'e': /* VPR --Cursor <n> Down */
  1415. DEFAULT(csiescseq.arg[0], 1);
  1416. tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
  1417. break;
  1418. case 'c': /* DA -- Device Attributes */
  1419. if(csiescseq.arg[0] == 0)
  1420. ttywrite(VT102ID, sizeof(VT102ID) - 1);
  1421. break;
  1422. case 'C': /* CUF -- Cursor <n> Forward */
  1423. case 'a': /* HPR -- Cursor <n> Forward */
  1424. DEFAULT(csiescseq.arg[0], 1);
  1425. tmoveto(term.c.x+csiescseq.arg[0], term.c.y);
  1426. break;
  1427. case 'D': /* CUB -- Cursor <n> Backward */
  1428. DEFAULT(csiescseq.arg[0], 1);
  1429. tmoveto(term.c.x-csiescseq.arg[0], term.c.y);
  1430. break;
  1431. case 'E': /* CNL -- Cursor <n> Down and first col */
  1432. DEFAULT(csiescseq.arg[0], 1);
  1433. tmoveto(0, term.c.y+csiescseq.arg[0]);
  1434. break;
  1435. case 'F': /* CPL -- Cursor <n> Up and first col */
  1436. DEFAULT(csiescseq.arg[0], 1);
  1437. tmoveto(0, term.c.y-csiescseq.arg[0]);
  1438. break;
  1439. case 'g': /* TBC -- Tabulation clear */
  1440. switch (csiescseq.arg[0]) {
  1441. case 0: /* clear current tab stop */
  1442. term.tabs[term.c.x] = 0;
  1443. break;
  1444. case 3: /* clear all the tabs */
  1445. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  1446. break;
  1447. default:
  1448. goto unknown;
  1449. }
  1450. break;
  1451. case 'G': /* CHA -- Move to <col> */
  1452. case '`': /* HPA */
  1453. DEFAULT(csiescseq.arg[0], 1);
  1454. tmoveto(csiescseq.arg[0]-1, term.c.y);
  1455. break;
  1456. case 'H': /* CUP -- Move to <row> <col> */
  1457. case 'f': /* HVP */
  1458. DEFAULT(csiescseq.arg[0], 1);
  1459. DEFAULT(csiescseq.arg[1], 1);
  1460. tmoveato(csiescseq.arg[1]-1, csiescseq.arg[0]-1);
  1461. break;
  1462. case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
  1463. DEFAULT(csiescseq.arg[0], 1);
  1464. while(csiescseq.arg[0]--)
  1465. tputtab(1);
  1466. break;
  1467. case 'J': /* ED -- Clear screen */
  1468. sel.bx = -1;
  1469. switch(csiescseq.arg[0]) {
  1470. case 0: /* below */
  1471. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y, 1);
  1472. if(term.c.y < term.row-1) {
  1473. tclearregion(0, term.c.y+1, term.col-1,
  1474. term.row-1, 1);
  1475. }
  1476. break;
  1477. case 1: /* above */
  1478. if(term.c.y > 1)
  1479. tclearregion(0, 0, term.col-1, term.c.y-1, 1);
  1480. tclearregion(0, term.c.y, term.c.x, term.c.y, 1);
  1481. break;
  1482. case 2: /* all */
  1483. tclearregion(0, 0, term.col-1, term.row-1, 1);
  1484. break;
  1485. default:
  1486. goto unknown;
  1487. }
  1488. break;
  1489. case 'K': /* EL -- Clear line */
  1490. switch(csiescseq.arg[0]) {
  1491. case 0: /* right */
  1492. tclearregion(term.c.x, term.c.y, term.col-1,
  1493. term.c.y, 1);
  1494. break;
  1495. case 1: /* left */
  1496. tclearregion(0, term.c.y, term.c.x, term.c.y, 1);
  1497. break;
  1498. case 2: /* all */
  1499. tclearregion(0, term.c.y, term.col-1, term.c.y, 1);
  1500. break;
  1501. }
  1502. break;
  1503. case 'S': /* SU -- Scroll <n> line up */
  1504. DEFAULT(csiescseq.arg[0], 1);
  1505. tscrollup(term.top, csiescseq.arg[0]);
  1506. break;
  1507. case 'T': /* SD -- Scroll <n> line down */
  1508. DEFAULT(csiescseq.arg[0], 1);
  1509. tscrolldown(term.top, csiescseq.arg[0]);
  1510. break;
  1511. case 'L': /* IL -- Insert <n> blank lines */
  1512. DEFAULT(csiescseq.arg[0], 1);
  1513. tinsertblankline(csiescseq.arg[0]);
  1514. break;
  1515. case 'l': /* RM -- Reset Mode */
  1516. tsetmode(csiescseq.priv, 0, csiescseq.arg, csiescseq.narg);
  1517. break;
  1518. case 'M': /* DL -- Delete <n> lines */
  1519. DEFAULT(csiescseq.arg[0], 1);
  1520. tdeleteline(csiescseq.arg[0]);
  1521. break;
  1522. case 'X': /* ECH -- Erase <n> char */
  1523. DEFAULT(csiescseq.arg[0], 1);
  1524. tclearregion(term.c.x, term.c.y, term.c.x + csiescseq.arg[0],
  1525. term.c.y, 0);
  1526. break;
  1527. case 'P': /* DCH -- Delete <n> char */
  1528. DEFAULT(csiescseq.arg[0], 1);
  1529. tdeletechar(csiescseq.arg[0]);
  1530. break;
  1531. case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
  1532. DEFAULT(csiescseq.arg[0], 1);
  1533. while(csiescseq.arg[0]--)
  1534. tputtab(0);
  1535. break;
  1536. case 'd': /* VPA -- Move to <row> */
  1537. DEFAULT(csiescseq.arg[0], 1);
  1538. tmoveato(term.c.x, csiescseq.arg[0]-1);
  1539. break;
  1540. case 'h': /* SM -- Set terminal mode */
  1541. tsetmode(csiescseq.priv, 1, csiescseq.arg, csiescseq.narg);
  1542. break;
  1543. case 'm': /* SGR -- Terminal attribute (color) */
  1544. tsetattr(csiescseq.arg, csiescseq.narg);
  1545. break;
  1546. case 'r': /* DECSTBM -- Set Scrolling Region */
  1547. if(csiescseq.priv) {
  1548. goto unknown;
  1549. } else {
  1550. DEFAULT(csiescseq.arg[0], 1);
  1551. DEFAULT(csiescseq.arg[1], term.row);
  1552. tsetscroll(csiescseq.arg[0]-1, csiescseq.arg[1]-1);
  1553. tmoveato(0, 0);
  1554. }
  1555. break;
  1556. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  1557. tcursor(CURSOR_SAVE);
  1558. break;
  1559. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  1560. tcursor(CURSOR_LOAD);
  1561. break;
  1562. }
  1563. }
  1564. void
  1565. csidump(void) {
  1566. int i;
  1567. uint c;
  1568. printf("ESC[");
  1569. for(i = 0; i < csiescseq.len; i++) {
  1570. c = csiescseq.buf[i] & 0xff;
  1571. if(isprint(c)) {
  1572. putchar(c);
  1573. } else if(c == '\n') {
  1574. printf("(\\n)");
  1575. } else if(c == '\r') {
  1576. printf("(\\r)");
  1577. } else if(c == 0x1b) {
  1578. printf("(\\e)");
  1579. } else {
  1580. printf("(%02x)", c);
  1581. }
  1582. }
  1583. putchar('\n');
  1584. }
  1585. void
  1586. csireset(void) {
  1587. memset(&csiescseq, 0, sizeof(csiescseq));
  1588. }
  1589. void
  1590. strhandle(void) {
  1591. char *p;
  1592. /*
  1593. * TODO: make this being useful in case of color palette change.
  1594. */
  1595. strparse();
  1596. p = strescseq.buf;
  1597. switch(strescseq.type) {
  1598. case ']': /* OSC -- Operating System Command */
  1599. switch(p[0]) {
  1600. case '0':
  1601. case '1':
  1602. case '2':
  1603. /*
  1604. * TODO: Handle special chars in string, like umlauts.
  1605. */
  1606. if(p[1] == ';') {
  1607. XStoreName(xw.dpy, xw.win, strescseq.buf+2);
  1608. }
  1609. break;
  1610. case ';':
  1611. XStoreName(xw.dpy, xw.win, strescseq.buf+1);
  1612. break;
  1613. case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
  1614. break;
  1615. default:
  1616. fprintf(stderr, "erresc: unknown str ");
  1617. strdump();
  1618. break;
  1619. }
  1620. break;
  1621. case 'k': /* old title set compatibility */
  1622. XStoreName(xw.dpy, xw.win, strescseq.buf);
  1623. break;
  1624. case 'P': /* DSC -- Device Control String */
  1625. case '_': /* APC -- Application Program Command */
  1626. case '^': /* PM -- Privacy Message */
  1627. default:
  1628. fprintf(stderr, "erresc: unknown str ");
  1629. strdump();
  1630. /* die(""); */
  1631. break;
  1632. }
  1633. }
  1634. void
  1635. strparse(void) {
  1636. /*
  1637. * TODO: Implement parsing like for CSI when required.
  1638. * Format: ESC type cmd ';' arg0 [';' argn] ESC \
  1639. */
  1640. return;
  1641. }
  1642. void
  1643. strdump(void) {
  1644. int i;
  1645. uint c;
  1646. printf("ESC%c", strescseq.type);
  1647. for(i = 0; i < strescseq.len; i++) {
  1648. c = strescseq.buf[i] & 0xff;
  1649. if(isprint(c)) {
  1650. putchar(c);
  1651. } else if(c == '\n') {
  1652. printf("(\\n)");
  1653. } else if(c == '\r') {
  1654. printf("(\\r)");
  1655. } else if(c == 0x1b) {
  1656. printf("(\\e)");
  1657. } else {
  1658. printf("(%02x)", c);
  1659. }
  1660. }
  1661. printf("ESC\\\n");
  1662. }
  1663. void
  1664. strreset(void) {
  1665. memset(&strescseq, 0, sizeof(strescseq));
  1666. }
  1667. void
  1668. tputtab(bool forward) {
  1669. uint x = term.c.x;
  1670. if(forward) {
  1671. if(x == term.col)
  1672. return;
  1673. for(++x; x < term.col && !term.tabs[x]; ++x)
  1674. /* nothing */ ;
  1675. } else {
  1676. if(x == 0)
  1677. return;
  1678. for(--x; x > 0 && !term.tabs[x]; --x)
  1679. /* nothing */ ;
  1680. }
  1681. tmoveto(x, term.c.y);
  1682. }
  1683. void
  1684. techo(char *buf, int len) {
  1685. for(; len > 0; buf++, len--) {
  1686. char c = *buf;
  1687. if(c == '\033') { /* escape */
  1688. tputc("^", 1);
  1689. tputc("[", 1);
  1690. } else if (c < '\x20') { /* control code */
  1691. if(c != '\n' && c != '\r' && c != '\t') {
  1692. c |= '\x40';
  1693. tputc("^", 1);
  1694. }
  1695. tputc(&c, 1);
  1696. } else {
  1697. break;
  1698. }
  1699. }
  1700. if (len)
  1701. tputc(buf, len);
  1702. }
  1703. void
  1704. tputc(char *c, int len) {
  1705. uchar ascii = *c;
  1706. bool control = ascii < '\x20' || ascii == 0177;
  1707. if(iofd != -1) {
  1708. if (xwrite(iofd, c, len) < 0) {
  1709. fprintf(stderr, "Error writing in %s:%s\n",
  1710. opt_io, strerror(errno));
  1711. close(iofd);
  1712. iofd = -1;
  1713. }
  1714. }
  1715. /*
  1716. * STR sequences must be checked before anything else
  1717. * because it can use some control codes as part of the sequence.
  1718. */
  1719. if(term.esc & ESC_STR) {
  1720. switch(ascii) {
  1721. case '\033':
  1722. term.esc = ESC_START | ESC_STR_END;
  1723. break;
  1724. case '\a': /* backwards compatibility to xterm */
  1725. term.esc = 0;
  1726. strhandle();
  1727. break;
  1728. default:
  1729. if(strescseq.len + len < sizeof(strescseq.buf)) {
  1730. memmove(&strescseq.buf[strescseq.len], c, len);
  1731. strescseq.len += len;
  1732. } else {
  1733. /*
  1734. * Here is a bug in terminals. If the user never sends
  1735. * some code to stop the str or esc command, then st
  1736. * will stop responding. But this is better than
  1737. * silently failing with unknown characters. At least
  1738. * then users will report back.
  1739. *
  1740. * In the case users ever get fixed, here is the code:
  1741. */
  1742. /*
  1743. * term.esc = 0;
  1744. * strhandle();
  1745. */
  1746. }
  1747. }
  1748. return;
  1749. }
  1750. /*
  1751. * Actions of control codes must be performed as soon they arrive
  1752. * because they can be embedded inside a control sequence, and
  1753. * they must not cause conflicts with sequences.
  1754. */
  1755. if(control) {
  1756. switch(ascii) {
  1757. case '\t': /* HT */
  1758. tputtab(1);
  1759. return;
  1760. case '\b': /* BS */
  1761. tmoveto(term.c.x-1, term.c.y);
  1762. return;
  1763. case '\r': /* CR */
  1764. tmoveto(0, term.c.y);
  1765. return;
  1766. case '\f': /* LF */
  1767. case '\v': /* VT */
  1768. case '\n': /* LF */
  1769. /* go to first col if the mode is set */
  1770. tnewline(IS_SET(MODE_CRLF));
  1771. return;
  1772. case '\a': /* BEL */
  1773. if(!(xw.state & WIN_FOCUSED))
  1774. xseturgency(1);
  1775. return;
  1776. case '\033': /* ESC */
  1777. csireset();
  1778. term.esc = ESC_START;
  1779. return;
  1780. case '\016': /* SO */
  1781. case '\017': /* SI */
  1782. /*
  1783. * Different charsets are hard to handle. Applications
  1784. * should use the right alt charset escapes for the
  1785. * only reason they still exist: line drawing. The
  1786. * rest is incompatible history st should not support.
  1787. */
  1788. return;
  1789. case '\032': /* SUB */
  1790. case '\030': /* CAN */
  1791. csireset();
  1792. return;
  1793. case '\005': /* ENQ (IGNORED) */
  1794. case '\000': /* NUL (IGNORED) */
  1795. case '\021': /* XON (IGNORED) */
  1796. case '\023': /* XOFF (IGNORED) */
  1797. case 0177: /* DEL (IGNORED) */
  1798. return;
  1799. }
  1800. } else if(term.esc & ESC_START) {
  1801. if(term.esc & ESC_CSI) {
  1802. csiescseq.buf[csiescseq.len++] = ascii;
  1803. if(BETWEEN(ascii, 0x40, 0x7E)
  1804. || csiescseq.len >= ESC_BUF_SIZ) {
  1805. term.esc = 0;
  1806. csiparse(), csihandle();
  1807. }
  1808. } else if(term.esc & ESC_STR_END) {
  1809. term.esc = 0;
  1810. if(ascii == '\\')
  1811. strhandle();
  1812. } else if(term.esc & ESC_ALTCHARSET) {
  1813. switch(ascii) {
  1814. case '0': /* Line drawing set */
  1815. term.c.attr.mode |= ATTR_GFX;
  1816. break;
  1817. case 'B': /* USASCII */
  1818. term.c.attr.mode &= ~ATTR_GFX;
  1819. break;
  1820. case 'A': /* UK (IGNORED) */
  1821. case '<': /* multinational charset (IGNORED) */
  1822. case '5': /* Finnish (IGNORED) */
  1823. case 'C': /* Finnish (IGNORED) */
  1824. case 'K': /* German (IGNORED) */
  1825. break;
  1826. default:
  1827. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  1828. }
  1829. term.esc = 0;
  1830. } else if(term.esc & ESC_TEST) {
  1831. if(ascii == '8') { /* DEC screen alignment test. */
  1832. char E[UTF_SIZ] = "E";
  1833. int x, y;
  1834. for(x = 0; x < term.col; ++x) {
  1835. for(y = 0; y < term.row; ++y)
  1836. tsetchar(E, &term.c.attr, x, y);
  1837. }
  1838. }
  1839. term.esc = 0;
  1840. } else {
  1841. switch(ascii) {
  1842. case '[':
  1843. term.esc |= ESC_CSI;
  1844. break;
  1845. case '#':
  1846. term.esc |= ESC_TEST;
  1847. break;
  1848. case 'P': /* DCS -- Device Control String */
  1849. case '_': /* APC -- Application Program Command */
  1850. case '^': /* PM -- Privacy Message */
  1851. case ']': /* OSC -- Operating System Command */
  1852. case 'k': /* old title set compatibility */
  1853. strreset();
  1854. strescseq.type = ascii;
  1855. term.esc |= ESC_STR;
  1856. break;
  1857. case '(': /* set primary charset G0 */
  1858. term.esc |= ESC_ALTCHARSET;
  1859. break;
  1860. case ')': /* set secondary charset G1 (IGNORED) */
  1861. case '*': /* set tertiary charset G2 (IGNORED) */
  1862. case '+': /* set quaternary charset G3 (IGNORED) */
  1863. term.esc = 0;
  1864. break;
  1865. case 'D': /* IND -- Linefeed */
  1866. if(term.c.y == term.bot) {
  1867. tscrollup(term.top, 1);
  1868. } else {
  1869. tmoveto(term.c.x, term.c.y+1);
  1870. }
  1871. term.esc = 0;
  1872. break;
  1873. case 'E': /* NEL -- Next line */
  1874. tnewline(1); /* always go to first col */
  1875. term.esc = 0;
  1876. break;
  1877. case 'H': /* HTS -- Horizontal tab stop */
  1878. term.tabs[term.c.x] = 1;
  1879. term.esc = 0;
  1880. break;
  1881. case 'M': /* RI -- Reverse index */
  1882. if(term.c.y == term.top) {
  1883. tscrolldown(term.top, 1);
  1884. } else {
  1885. tmoveto(term.c.x, term.c.y-1);
  1886. }
  1887. term.esc = 0;
  1888. break;
  1889. case 'Z': /* DECID -- Identify Terminal */
  1890. ttywrite(VT102ID, sizeof(VT102ID) - 1);
  1891. term.esc = 0;
  1892. break;
  1893. case 'c': /* RIS -- Reset to inital state */
  1894. treset();
  1895. term.esc = 0;
  1896. xresettitle();
  1897. break;
  1898. case '=': /* DECPAM -- Application keypad */
  1899. term.mode |= MODE_APPKEYPAD;
  1900. term.esc = 0;
  1901. break;
  1902. case '>': /* DECPNM -- Normal keypad */
  1903. term.mode &= ~MODE_APPKEYPAD;
  1904. term.esc = 0;
  1905. break;
  1906. case '7': /* DECSC -- Save Cursor */
  1907. tcursor(CURSOR_SAVE);
  1908. term.esc = 0;
  1909. break;
  1910. case '8': /* DECRC -- Restore Cursor */
  1911. tcursor(CURSOR_LOAD);
  1912. term.esc = 0;
  1913. break;
  1914. case '\\': /* ST -- Stop */
  1915. term.esc = 0;
  1916. break;
  1917. default:
  1918. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  1919. (uchar) ascii, isprint(ascii)? ascii:'.');
  1920. term.esc = 0;
  1921. }
  1922. }
  1923. /*
  1924. * All characters which form part of a sequence are not
  1925. * printed
  1926. */
  1927. return;
  1928. }
  1929. /*
  1930. * Display control codes only if we are in graphic mode
  1931. */
  1932. if(control && !(term.c.attr.mode & ATTR_GFX))
  1933. return;
  1934. if(sel.bx != -1 && BETWEEN(term.c.y, sel.by, sel.ey))
  1935. sel.bx = -1;
  1936. if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
  1937. tnewline(1); /* always go to first col */
  1938. if(IS_SET(MODE_INSERT) && term.c.x+1 < term.col) {
  1939. memmove(&term.line[term.c.y][term.c.x+1],
  1940. &term.line[term.c.y][term.c.x],
  1941. (term.col - term.c.x - 1) * sizeof(Glyph));
  1942. }
  1943. tsetchar(c, &term.c.attr, term.c.x, term.c.y);
  1944. if(term.c.x+1 < term.col) {
  1945. tmoveto(term.c.x+1, term.c.y);
  1946. } else {
  1947. term.c.state |= CURSOR_WRAPNEXT;
  1948. }
  1949. }
  1950. int
  1951. tresize(int col, int row) {
  1952. int i, x;
  1953. int minrow = MIN(row, term.row);
  1954. int mincol = MIN(col, term.col);
  1955. int slide = term.c.y - row + 1;
  1956. bool *bp;
  1957. if(col < 1 || row < 1)
  1958. return 0;
  1959. /* free unneeded rows */
  1960. i = 0;
  1961. if(slide > 0) {
  1962. /* slide screen to keep cursor where we expect it -
  1963. * tscrollup would work here, but we can optimize to
  1964. * memmove because we're freeing the earlier lines */
  1965. for(/* i = 0 */; i < slide; i++) {
  1966. free(term.line[i]);
  1967. free(term.alt[i]);
  1968. }
  1969. memmove(term.line, term.line + slide, row * sizeof(Line));
  1970. memmove(term.alt, term.alt + slide, row * sizeof(Line));
  1971. }
  1972. for(i += row; i < term.row; i++) {
  1973. free(term.line[i]);
  1974. free(term.alt[i]);
  1975. }
  1976. /* resize to new height */
  1977. term.line = xrealloc(term.line, row * sizeof(Line));
  1978. term.alt = xrealloc(term.alt, row * sizeof(Line));
  1979. term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
  1980. term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
  1981. /* resize each row to new width, zero-pad if needed */
  1982. for(i = 0; i < minrow; i++) {
  1983. term.dirty[i] = 1;
  1984. term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
  1985. term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
  1986. for(x = mincol; x < col; x++) {
  1987. term.line[i][x].state = 0;
  1988. term.alt[i][x].state = 0;
  1989. }
  1990. }
  1991. /* allocate any new rows */
  1992. for(/* i == minrow */; i < row; i++) {
  1993. term.dirty[i] = 1;
  1994. term.line[i] = xcalloc(col, sizeof(Glyph));
  1995. term.alt [i] = xcalloc(col, sizeof(Glyph));
  1996. }
  1997. if(col > term.col) {
  1998. bp = term.tabs + term.col;
  1999. memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
  2000. while(--bp > term.tabs && !*bp)
  2001. /* nothing */ ;
  2002. for(bp += tabspaces; bp < term.tabs + col; bp += tabspaces)
  2003. *bp = 1;
  2004. }
  2005. /* update terminal size */
  2006. term.col = col;
  2007. term.row = row;
  2008. /* reset scrolling region */
  2009. tsetscroll(0, row-1);
  2010. /* make use of the LIMIT in tmoveto */
  2011. tmoveto(term.c.x, term.c.y);
  2012. return (slide > 0);
  2013. }
  2014. void
  2015. xresize(int col, int row) {
  2016. xw.tw = MAX(1, col * xw.cw);
  2017. xw.th = MAX(1, row * xw.ch);
  2018. if(!usedbe) {
  2019. XFreePixmap(xw.dpy, xw.buf);
  2020. xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.w, xw.h,
  2021. DefaultDepth(xw.dpy, xw.scr));
  2022. XSetForeground(xw.dpy, dc.gc, 0);
  2023. XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, xw.w, xw.h);
  2024. }
  2025. XftDrawChange(xw.draw, xw.buf);
  2026. }
  2027. void
  2028. xloadcols(void) {
  2029. int i, r, g, b;
  2030. XRenderColor color = { .alpha = 0 };
  2031. /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
  2032. for(i = 0; i < LEN(colorname); i++) {
  2033. if(!colorname[i])
  2034. continue;
  2035. if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, colorname[i], &dc.col[i])) {
  2036. die("Could not allocate color '%s'\n", colorname[i]);
  2037. }
  2038. }
  2039. /* load colors [16-255] ; same colors as xterm */
  2040. for(i = 16, r = 0; r < 6; r++) {
  2041. for(g = 0; g < 6; g++) {
  2042. for(b = 0; b < 6; b++) {
  2043. color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
  2044. color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
  2045. color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
  2046. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &dc.col[i])) {
  2047. die("Could not allocate color %d\n", i);
  2048. }
  2049. i++;
  2050. }
  2051. }
  2052. }
  2053. for(r = 0; r < 24; r++, i++) {
  2054. color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
  2055. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color,
  2056. &dc.col[i])) {
  2057. die("Could not allocate color %d\n", i);
  2058. }
  2059. }
  2060. }
  2061. void
  2062. xtermclear(int col1, int row1, int col2, int row2) {
  2063. XftDrawRect(xw.draw,
  2064. &dc.col[IS_SET(MODE_REVERSE) ? defaultfg : defaultbg],
  2065. borderpx + col1 * xw.cw,
  2066. borderpx + row1 * xw.ch,
  2067. (col2-col1+1) * xw.cw,
  2068. (row2-row1+1) * xw.ch);
  2069. }
  2070. /*
  2071. * Absolute coordinates.
  2072. */
  2073. void
  2074. xclear(int x1, int y1, int x2, int y2) {
  2075. XftDrawRect(xw.draw,
  2076. &dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg],
  2077. x1, y1, x2-x1, y2-y1);
  2078. }
  2079. void
  2080. xhints(void) {
  2081. XClassHint class = {opt_class ? opt_class : termname, termname};
  2082. XWMHints wm = {.flags = InputHint, .input = 1};
  2083. XSizeHints *sizeh = NULL;
  2084. sizeh = XAllocSizeHints();
  2085. if(xw.isfixed == False) {
  2086. sizeh->flags = PSize | PResizeInc | PBaseSize;
  2087. sizeh->height = xw.h;
  2088. sizeh->width = xw.w;
  2089. sizeh->height_inc = xw.ch;
  2090. sizeh->width_inc = xw.cw;
  2091. sizeh->base_height = 2 * borderpx;
  2092. sizeh->base_width = 2 * borderpx;
  2093. } else {
  2094. sizeh->flags = PMaxSize | PMinSize;
  2095. sizeh->min_width = sizeh->max_width = xw.fw;
  2096. sizeh->min_height = sizeh->max_height = xw.fh;
  2097. }
  2098. XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm, &class);
  2099. XFree(sizeh);
  2100. }
  2101. int
  2102. xloadfont(Font *f, FcPattern *pattern) {
  2103. FcPattern *match;
  2104. FcResult result;
  2105. match = FcFontMatch(NULL, pattern, &result);
  2106. if(!match)
  2107. return 1;
  2108. if(!(f->set = FcFontSort(0, match, FcTrue, 0, &result))) {
  2109. FcPatternDestroy(match);
  2110. return 1;
  2111. }
  2112. if(!(f->match = XftFontOpenPattern(xw.dpy, match))) {
  2113. FcPatternDestroy(match);
  2114. return 1;
  2115. }
  2116. f->pattern = FcPatternDuplicate(pattern);
  2117. f->ascent = f->match->ascent;
  2118. f->descent = f->match->descent;
  2119. f->lbearing = 0;
  2120. f->rbearing = f->match->max_advance_width;
  2121. f->height = f->match->height;
  2122. f->width = f->lbearing + f->rbearing;
  2123. return 0;
  2124. }
  2125. void
  2126. xloadfonts(char *fontstr, int fontsize) {
  2127. FcPattern *pattern;
  2128. FcResult result;
  2129. double fontval;
  2130. if(fontstr[0] == '-') {
  2131. pattern = XftXlfdParse(fontstr, False, False);
  2132. } else {
  2133. pattern = FcNameParse((FcChar8 *)fontstr);
  2134. }
  2135. if(!pattern)
  2136. die("st: can't open font %s\n", fontstr);
  2137. if(fontsize > 0) {
  2138. FcPatternDel(pattern, FC_PIXEL_SIZE);
  2139. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize);
  2140. usedfontsize = fontsize;
  2141. } else {
  2142. result = FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval);
  2143. if(result == FcResultMatch) {
  2144. usedfontsize = (int)fontval;
  2145. } else {
  2146. /*
  2147. * Default font size is 12, if none given. This is to
  2148. * have a known usedfontsize value.
  2149. */
  2150. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
  2151. usedfontsize = 12;
  2152. }
  2153. }
  2154. FcConfigSubstitute(0, pattern, FcMatchPattern);
  2155. FcDefaultSubstitute(pattern);
  2156. if(xloadfont(&dc.font, pattern))
  2157. die("st: can't open font %s\n", fontstr);
  2158. /* Setting character width and height. */
  2159. xw.cw = dc.font.width;
  2160. xw.ch = dc.font.height;
  2161. FcPatternDel(pattern, FC_WEIGHT);
  2162. FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
  2163. if(xloadfont(&dc.bfont, pattern))
  2164. die("st: can't open font %s\n", fontstr);
  2165. FcPatternDel(pattern, FC_SLANT);
  2166. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
  2167. if(xloadfont(&dc.ibfont, pattern))
  2168. die("st: can't open font %s\n", fontstr);
  2169. FcPatternDel(pattern, FC_WEIGHT);
  2170. if(xloadfont(&dc.ifont, pattern))
  2171. die("st: can't open font %s\n", fontstr);
  2172. FcPatternDestroy(pattern);
  2173. }
  2174. void
  2175. xunloadfonts(void)
  2176. {
  2177. int i, ip;
  2178. /*
  2179. * Free the loaded fonts in the font cache. This is done backwards
  2180. * from the frccur.
  2181. */
  2182. for (i = 0, ip = frccur; i < frclen; i++, ip--) {
  2183. if (ip < 0)
  2184. ip = LEN(frc) - 1;
  2185. XftFontClose(xw.dpy, frc[ip].font);
  2186. }
  2187. frccur = -1;
  2188. frclen = 0;
  2189. XftFontClose(xw.dpy, dc.font.match);
  2190. FcPatternDestroy(dc.font.pattern);
  2191. FcFontSetDestroy(dc.font.set);
  2192. XftFontClose(xw.dpy, dc.bfont.match);
  2193. FcPatternDestroy(dc.bfont.pattern);
  2194. FcFontSetDestroy(dc.bfont.set);
  2195. XftFontClose(xw.dpy, dc.ifont.match);
  2196. FcPatternDestroy(dc.ifont.pattern);
  2197. FcFontSetDestroy(dc.ifont.set);
  2198. XftFontClose(xw.dpy, dc.ibfont.match);
  2199. FcPatternDestroy(dc.ibfont.pattern);
  2200. FcFontSetDestroy(dc.ibfont.set);
  2201. }
  2202. void
  2203. xzoom(const Arg *arg)
  2204. {
  2205. xunloadfonts();
  2206. xloadfonts(usedfont, usedfontsize + arg->i);
  2207. cresize(0, 0);
  2208. redraw(0);
  2209. }
  2210. void
  2211. xinit(void) {
  2212. XSetWindowAttributes attrs;
  2213. XGCValues gcvalues;
  2214. Cursor cursor;
  2215. Window parent;
  2216. int sw, sh, major, minor;
  2217. if(!(xw.dpy = XOpenDisplay(NULL)))
  2218. die("Can't open display\n");
  2219. xw.scr = XDefaultScreen(xw.dpy);
  2220. xw.vis = XDefaultVisual(xw.dpy, xw.scr);
  2221. /* font */
  2222. if (!FcInit())
  2223. die("Could not init fontconfig.\n");
  2224. usedfont = (opt_font == NULL)? font : opt_font;
  2225. xloadfonts(usedfont, 0);
  2226. /* colors */
  2227. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  2228. xloadcols();
  2229. /* adjust fixed window geometry */
  2230. if(xw.isfixed) {
  2231. sw = DisplayWidth(xw.dpy, xw.scr);
  2232. sh = DisplayHeight(xw.dpy, xw.scr);
  2233. if(xw.fx < 0)
  2234. xw.fx = sw + xw.fx - xw.fw - 1;
  2235. if(xw.fy < 0)
  2236. xw.fy = sh + xw.fy - xw.fh - 1;
  2237. xw.h = xw.fh;
  2238. xw.w = xw.fw;
  2239. } else {
  2240. /* window - default size */
  2241. xw.h = 2 * borderpx + term.row * xw.ch;
  2242. xw.w = 2 * borderpx + term.col * xw.cw;
  2243. xw.fx = 0;
  2244. xw.fy = 0;
  2245. }
  2246. /* Events */
  2247. attrs.background_pixel = dc.col[defaultbg].pixel;
  2248. attrs.border_pixel = dc.col[defaultbg].pixel;
  2249. attrs.bit_gravity = NorthWestGravity;
  2250. attrs.event_mask = FocusChangeMask | KeyPressMask
  2251. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  2252. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
  2253. attrs.colormap = xw.cmap;
  2254. parent = opt_embed ? strtol(opt_embed, NULL, 0) : \
  2255. XRootWindow(xw.dpy, xw.scr);
  2256. xw.win = XCreateWindow(xw.dpy, parent, xw.fx, xw.fy,
  2257. xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
  2258. xw.vis,
  2259. CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
  2260. | CWColormap,
  2261. &attrs);
  2262. /* double buffering */
  2263. /*
  2264. if(XdbeQueryExtension(xw.dpy, &major, &minor)) {
  2265. xw.buf = XdbeAllocateBackBufferName(xw.dpy, xw.win,
  2266. XdbeBackground);
  2267. usedbe = True;
  2268. } else {
  2269. */
  2270. memset(&gcvalues, 0, sizeof(gcvalues));
  2271. gcvalues.graphics_exposures = False;
  2272. dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
  2273. &gcvalues);
  2274. xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.w, xw.h,
  2275. DefaultDepth(xw.dpy, xw.scr));
  2276. XSetForeground(xw.dpy, dc.gc, 0);
  2277. XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, xw.w, xw.h);
  2278. //xw.buf = xw.win;
  2279. /*
  2280. }
  2281. */
  2282. /* Xft rendering context */
  2283. xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
  2284. /* input methods */
  2285. if((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
  2286. XSetLocaleModifiers("@im=local");
  2287. if((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
  2288. XSetLocaleModifiers("@im=");
  2289. if((xw.xim = XOpenIM(xw.dpy,
  2290. NULL, NULL, NULL)) == NULL) {
  2291. die("XOpenIM failed. Could not open input"
  2292. " device.\n");
  2293. }
  2294. }
  2295. }
  2296. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  2297. | XIMStatusNothing, XNClientWindow, xw.win,
  2298. XNFocusWindow, xw.win, NULL);
  2299. if(xw.xic == NULL)
  2300. die("XCreateIC failed. Could not obtain input method.\n");
  2301. /* white cursor, black outline */
  2302. cursor = XCreateFontCursor(xw.dpy, XC_xterm);
  2303. XDefineCursor(xw.dpy, xw.win, cursor);
  2304. XRecolorCursor(xw.dpy, cursor,
  2305. &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
  2306. &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
  2307. xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
  2308. xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
  2309. XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
  2310. xresettitle();
  2311. XMapWindow(xw.dpy, xw.win);
  2312. xhints();
  2313. XSync(xw.dpy, 0);
  2314. }
  2315. void
  2316. xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
  2317. int winx = borderpx + x * xw.cw, winy = borderpx + y * xw.ch,
  2318. width = charlen * xw.cw, xp, i;
  2319. int frp, frcflags;
  2320. int u8fl, u8fblen, u8cblen, doesexist;
  2321. char *u8c, *u8fs;
  2322. long u8char;
  2323. Font *font = &dc.font;
  2324. FcResult fcres;
  2325. FcPattern *fcpattern, *fontpattern;
  2326. FcFontSet *fcsets[] = { NULL };
  2327. FcCharSet *fccharset;
  2328. Colour *fg = &dc.col[base.fg], *bg = &dc.col[base.bg],
  2329. *temp, revfg, revbg;
  2330. XRenderColor colfg, colbg;
  2331. frcflags = FRC_NORMAL;
  2332. if(base.mode & ATTR_BOLD) {
  2333. if(BETWEEN(base.fg, 0, 7)) {
  2334. /* basic system colors */
  2335. fg = &dc.col[base.fg + 8];
  2336. } else if(BETWEEN(base.fg, 16, 195)) {
  2337. /* 256 colors */
  2338. fg = &dc.col[base.fg + 36];
  2339. } else if(BETWEEN(base.fg, 232, 251)) {
  2340. /* greyscale */
  2341. fg = &dc.col[base.fg + 4];
  2342. }
  2343. /*
  2344. * Those ranges will not be brightened:
  2345. * 8 - 15 – bright system colors
  2346. * 196 - 231 – highest 256 color cube
  2347. * 252 - 255 – brightest colors in greyscale
  2348. */
  2349. font = &dc.bfont;
  2350. frcflags = FRC_BOLD;
  2351. }
  2352. if(base.mode & ATTR_ITALIC) {
  2353. font = &dc.ifont;
  2354. frcflags = FRC_ITALIC;
  2355. }
  2356. if((base.mode & ATTR_ITALIC) && (base.mode & ATTR_BOLD)) {
  2357. font = &dc.ibfont;
  2358. frcflags = FRC_ITALICBOLD;
  2359. }
  2360. if(IS_SET(MODE_REVERSE)) {
  2361. if(fg == &dc.col[defaultfg]) {
  2362. fg = &dc.col[defaultbg];
  2363. } else {
  2364. colfg.red = ~fg->color.red;
  2365. colfg.green = ~fg->color.green;
  2366. colfg.blue = ~fg->color.blue;
  2367. colfg.alpha = fg->color.alpha;
  2368. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
  2369. fg = &revfg;
  2370. }
  2371. if(bg == &dc.col[defaultbg]) {
  2372. bg = &dc.col[defaultfg];
  2373. } else {
  2374. colbg.red = ~bg->color.red;
  2375. colbg.green = ~bg->color.green;
  2376. colbg.blue = ~bg->color.blue;
  2377. colbg.alpha = bg->color.alpha;
  2378. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &revbg);
  2379. bg = &revbg;
  2380. }
  2381. }
  2382. if(base.mode & ATTR_REVERSE) {
  2383. temp = fg;
  2384. fg = bg;
  2385. bg = temp;
  2386. }
  2387. /* Intelligent cleaning up of the borders. */
  2388. if(x == 0) {
  2389. xclear(0, (y == 0)? 0 : winy, borderpx,
  2390. winy + xw.ch + ((y >= term.row-1)? xw.h : 0));
  2391. }
  2392. if(x + charlen >= term.col) {
  2393. xclear(winx + width, (y == 0)? 0 : winy, xw.w,
  2394. ((y >= term.row-1)? xw.h : (winy + xw.ch)));
  2395. }
  2396. if(y == 0)
  2397. xclear(winx, 0, winx + width, borderpx);
  2398. if(y == term.row-1)
  2399. xclear(winx, winy + xw.ch, winx + width, xw.h);
  2400. /* Clean up the region we want to draw to. */
  2401. XftDrawRect(xw.draw, bg, winx, winy, width, xw.ch);
  2402. fcsets[0] = font->set;
  2403. for (xp = winx; bytelen > 0;) {
  2404. /*
  2405. * Search for the range in the to be printed string of glyphs
  2406. * that are in the main font. Then print that range. If
  2407. * some glyph is found that is not in the font, do the
  2408. * fallback dance.
  2409. */
  2410. u8fs = s;
  2411. u8fblen = 0;
  2412. u8fl = 0;
  2413. for (;;) {
  2414. u8c = s;
  2415. u8cblen = utf8decode(s, &u8char);
  2416. s += u8cblen;
  2417. bytelen -= u8cblen;
  2418. doesexist = XftCharIndex(xw.dpy, font->match, u8char);
  2419. if (!doesexist || bytelen <= 0) {
  2420. if (bytelen <= 0) {
  2421. if (doesexist) {
  2422. u8fl++;
  2423. u8fblen += u8cblen;
  2424. }
  2425. }
  2426. if (u8fl > 0) {
  2427. XftDrawStringUtf8(xw.draw, fg,
  2428. font->match, xp,
  2429. winy + font->ascent,
  2430. (FcChar8 *)u8fs,
  2431. u8fblen);
  2432. xp += font->width * u8fl;
  2433. }
  2434. break;
  2435. }
  2436. u8fl++;
  2437. u8fblen += u8cblen;
  2438. }
  2439. if (doesexist)
  2440. break;
  2441. frp = frccur;
  2442. /* Search the font cache. */
  2443. for (i = 0; i < frclen; i++, frp--) {
  2444. if (frp <= 0)
  2445. frp = LEN(frc) - 1;
  2446. if (frc[frp].c == u8char
  2447. && frc[frp].flags == frcflags) {
  2448. break;
  2449. }
  2450. }
  2451. /* Nothing was found. */
  2452. if (i >= frclen) {
  2453. /*
  2454. * Nothing was found in the cache. Now use
  2455. * some dozen of Fontconfig calls to get the
  2456. * font for one single character.
  2457. */
  2458. fcpattern = FcPatternDuplicate(font->pattern);
  2459. fccharset = FcCharSetCreate();
  2460. FcCharSetAddChar(fccharset, u8char);
  2461. FcPatternAddCharSet(fcpattern, FC_CHARSET,
  2462. fccharset);
  2463. FcPatternAddBool(fcpattern, FC_SCALABLE,
  2464. FcTrue);
  2465. FcConfigSubstitute(0, fcpattern,
  2466. FcMatchPattern);
  2467. FcDefaultSubstitute(fcpattern);
  2468. fontpattern = FcFontSetMatch(0, fcsets,
  2469. FcTrue, fcpattern, &fcres);
  2470. /*
  2471. * Overwrite or create the new cache entry
  2472. * entry.
  2473. */
  2474. frccur++;
  2475. frclen++;
  2476. if (frccur >= LEN(frc))
  2477. frccur = 0;
  2478. if (frclen > LEN(frc)) {
  2479. frclen = LEN(frc);
  2480. XftFontClose(xw.dpy, frc[frccur].font);
  2481. }
  2482. frc[frccur].font = XftFontOpenPattern(xw.dpy,
  2483. fontpattern);
  2484. frc[frccur].c = u8char;
  2485. frc[frccur].flags = frcflags;
  2486. FcPatternDestroy(fcpattern);
  2487. FcCharSetDestroy(fccharset);
  2488. frp = frccur;
  2489. }
  2490. XftDrawStringUtf8(xw.draw, fg, frc[frp].font,
  2491. xp, winy + frc[frp].font->ascent,
  2492. (FcChar8 *)u8c, u8cblen);
  2493. xp += font->width;
  2494. }
  2495. /*
  2496. XftDrawStringUtf8(xw.draw, fg, font->set, winx,
  2497. winy + font->ascent, (FcChar8 *)s, bytelen);
  2498. */
  2499. if(base.mode & ATTR_UNDERLINE) {
  2500. XftDrawRect(xw.draw, fg, winx, winy + font->ascent + 1,
  2501. width, 1);
  2502. }
  2503. }
  2504. void
  2505. xdrawcursor(void) {
  2506. static int oldx = 0, oldy = 0;
  2507. int sl;
  2508. Glyph g = {{' '}, ATTR_NULL, defaultbg, defaultcs, 0};
  2509. LIMIT(oldx, 0, term.col-1);
  2510. LIMIT(oldy, 0, term.row-1);
  2511. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  2512. memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
  2513. /* remove the old cursor */
  2514. if(term.line[oldy][oldx].state & GLYPH_SET) {
  2515. sl = utf8size(term.line[oldy][oldx].c);
  2516. xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx,
  2517. oldy, 1, sl);
  2518. } else {
  2519. xtermclear(oldx, oldy, oldx, oldy);
  2520. }
  2521. /* draw the new one */
  2522. if(!(IS_SET(MODE_HIDE))) {
  2523. if(!(xw.state & WIN_FOCUSED))
  2524. g.bg = defaultucs;
  2525. if(IS_SET(MODE_REVERSE))
  2526. g.mode |= ATTR_REVERSE, g.fg = defaultcs, g.bg = defaultfg;
  2527. sl = utf8size(g.c);
  2528. xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
  2529. oldx = term.c.x, oldy = term.c.y;
  2530. }
  2531. }
  2532. void
  2533. xresettitle(void) {
  2534. XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
  2535. }
  2536. void
  2537. redraw(int timeout) {
  2538. struct timespec tv = {0, timeout * 1000};
  2539. tfulldirt();
  2540. draw();
  2541. if(timeout > 0) {
  2542. nanosleep(&tv, NULL);
  2543. XSync(xw.dpy, False); /* necessary for a good tput flash */
  2544. }
  2545. }
  2546. void
  2547. draw(void) {
  2548. XdbeSwapInfo swpinfo[1] = {{xw.win, XdbeCopied}};
  2549. drawregion(0, 0, term.col, term.row);
  2550. if(usedbe) {
  2551. XdbeSwapBuffers(xw.dpy, swpinfo, 1);
  2552. } else {
  2553. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.w,
  2554. xw.h, 0, 0);
  2555. XSetForeground(xw.dpy, dc.gc, 0);
  2556. }
  2557. }
  2558. void
  2559. drawregion(int x1, int y1, int x2, int y2) {
  2560. int ic, ib, x, y, ox, sl;
  2561. Glyph base, new;
  2562. char buf[DRAW_BUF_SIZ];
  2563. bool ena_sel = sel.bx != -1;
  2564. if(sel.alt ^ IS_SET(MODE_ALTSCREEN))
  2565. ena_sel = 0;
  2566. if(!(xw.state & WIN_VISIBLE))
  2567. return;
  2568. for(y = y1; y < y2; y++) {
  2569. if(!term.dirty[y])
  2570. continue;
  2571. xtermclear(0, y, term.col, y);
  2572. term.dirty[y] = 0;
  2573. base = term.line[y][0];
  2574. ic = ib = ox = 0;
  2575. for(x = x1; x < x2; x++) {
  2576. new = term.line[y][x];
  2577. if(ena_sel && *(new.c) && selected(x, y))
  2578. new.mode ^= ATTR_REVERSE;
  2579. if(ib > 0 && (!(new.state & GLYPH_SET)
  2580. || ATTRCMP(base, new)
  2581. || ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
  2582. xdraws(buf, base, ox, y, ic, ib);
  2583. ic = ib = 0;
  2584. }
  2585. if(new.state & GLYPH_SET) {
  2586. if(ib == 0) {
  2587. ox = x;
  2588. base = new;
  2589. }
  2590. sl = utf8size(new.c);
  2591. memcpy(buf+ib, new.c, sl);
  2592. ib += sl;
  2593. ++ic;
  2594. }
  2595. }
  2596. if(ib > 0)
  2597. xdraws(buf, base, ox, y, ic, ib);
  2598. }
  2599. xdrawcursor();
  2600. }
  2601. void
  2602. expose(XEvent *ev) {
  2603. XExposeEvent *e = &ev->xexpose;
  2604. if(xw.state & WIN_REDRAW) {
  2605. if(!e->count)
  2606. xw.state &= ~WIN_REDRAW;
  2607. }
  2608. redraw(0);
  2609. }
  2610. void
  2611. visibility(XEvent *ev) {
  2612. XVisibilityEvent *e = &ev->xvisibility;
  2613. if(e->state == VisibilityFullyObscured) {
  2614. xw.state &= ~WIN_VISIBLE;
  2615. } else if(!(xw.state & WIN_VISIBLE)) {
  2616. /* need a full redraw for next Expose, not just a buf copy */
  2617. xw.state |= WIN_VISIBLE | WIN_REDRAW;
  2618. }
  2619. }
  2620. void
  2621. unmap(XEvent *ev) {
  2622. xw.state &= ~WIN_VISIBLE;
  2623. }
  2624. void
  2625. xseturgency(int add) {
  2626. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  2627. h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
  2628. XSetWMHints(xw.dpy, xw.win, h);
  2629. XFree(h);
  2630. }
  2631. void
  2632. focus(XEvent *ev) {
  2633. if(ev->type == FocusIn) {
  2634. XSetICFocus(xw.xic);
  2635. xw.state |= WIN_FOCUSED;
  2636. xseturgency(0);
  2637. } else {
  2638. XUnsetICFocus(xw.xic);
  2639. xw.state &= ~WIN_FOCUSED;
  2640. }
  2641. }
  2642. inline bool
  2643. match(uint mask, uint state) {
  2644. if(mask == XK_NO_MOD && state)
  2645. return false;
  2646. if(mask != XK_ANY_MOD && mask != XK_NO_MOD && !state)
  2647. return false;
  2648. if((state & mask) != state)
  2649. return false;
  2650. return true;
  2651. }
  2652. void
  2653. numlock(const Arg *dummy) {
  2654. term.numlock ^= 1;
  2655. }
  2656. char*
  2657. kmap(KeySym k, uint state) {
  2658. uint mask;
  2659. Key *kp;
  2660. int i;
  2661. /* Check for mapped keys out of X11 function keys. */
  2662. for(i = 0; i < LEN(mappedkeys); i++) {
  2663. if(mappedkeys[i] == k)
  2664. break;
  2665. }
  2666. if(i == LEN(mappedkeys)) {
  2667. if((k & 0xFFFF) < 0xFD00)
  2668. return NULL;
  2669. }
  2670. for(kp = key; kp < key + LEN(key); kp++) {
  2671. mask = kp->mask;
  2672. if(kp->k != k)
  2673. continue;
  2674. if(!match(mask, state))
  2675. continue;
  2676. if(kp->appkey > 0) {
  2677. if(!IS_SET(MODE_APPKEYPAD))
  2678. continue;
  2679. if(term.numlock && kp->appkey == 2)
  2680. continue;
  2681. } else if(kp->appkey < 0 && IS_SET(MODE_APPKEYPAD)) {
  2682. continue;
  2683. }
  2684. if((kp->appcursor < 0 && IS_SET(MODE_APPCURSOR)) ||
  2685. (kp->appcursor > 0
  2686. && !IS_SET(MODE_APPCURSOR))) {
  2687. continue;
  2688. }
  2689. if((kp->crlf < 0 && IS_SET(MODE_CRLF)) ||
  2690. (kp->crlf > 0 && !IS_SET(MODE_CRLF))) {
  2691. continue;
  2692. }
  2693. return kp->s;
  2694. }
  2695. return NULL;
  2696. }
  2697. void
  2698. kpress(XEvent *ev) {
  2699. XKeyEvent *e = &ev->xkey;
  2700. KeySym ksym;
  2701. char xstr[31], buf[32], *customkey, *cp = buf;
  2702. int len;
  2703. Status status;
  2704. Shortcut *bp;
  2705. if (IS_SET(MODE_KBDLOCK))
  2706. return;
  2707. len = XmbLookupString(xw.xic, e, xstr, sizeof(xstr), &ksym, &status);
  2708. e->state &= ~Mod2Mask;
  2709. /* 1. shortcuts */
  2710. for(bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
  2711. if(ksym == bp->keysym && match(bp->mod, e->state)) {
  2712. bp->func(&(bp->arg));
  2713. return;
  2714. }
  2715. }
  2716. /* 2. custom keys from config.h */
  2717. if((customkey = kmap(ksym, e->state))) {
  2718. len = strlen(customkey);
  2719. memcpy(buf, customkey, len);
  2720. /* 2. hardcoded (overrides X lookup) */
  2721. } else {
  2722. if(len == 0)
  2723. return;
  2724. if (len == 1 && e->state & Mod1Mask)
  2725. *cp++ = '\033';
  2726. memcpy(cp, xstr, len);
  2727. len = cp - buf + len;
  2728. }
  2729. ttywrite(buf, len);
  2730. if(IS_SET(MODE_ECHO))
  2731. techo(buf, len);
  2732. }
  2733. void
  2734. cmessage(XEvent *e) {
  2735. /* See xembed specs
  2736. http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
  2737. if(e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
  2738. if(e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
  2739. xw.state |= WIN_FOCUSED;
  2740. xseturgency(0);
  2741. } else if(e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
  2742. xw.state &= ~WIN_FOCUSED;
  2743. }
  2744. } else if(e->xclient.data.l[0] == xw.wmdeletewin) {
  2745. /* Send SIGHUP to shell */
  2746. kill(pid, SIGHUP);
  2747. exit(EXIT_SUCCESS);
  2748. }
  2749. }
  2750. void
  2751. cresize(int width, int height)
  2752. {
  2753. int col, row;
  2754. if(width != 0)
  2755. xw.w = width;
  2756. if(height != 0)
  2757. xw.h = height;
  2758. col = (xw.w - 2 * borderpx) / xw.cw;
  2759. row = (xw.h - 2 * borderpx) / xw.ch;
  2760. tresize(col, row);
  2761. xresize(col, row);
  2762. ttyresize();
  2763. }
  2764. void
  2765. resize(XEvent *e) {
  2766. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  2767. return;
  2768. cresize(e->xconfigure.width, e->xconfigure.height);
  2769. }
  2770. void
  2771. run(void) {
  2772. XEvent ev;
  2773. fd_set rfd;
  2774. int xfd = XConnectionNumber(xw.dpy), i;
  2775. struct timeval drawtimeout, *tv = NULL;
  2776. for(i = 0;; i++) {
  2777. FD_ZERO(&rfd);
  2778. FD_SET(cmdfd, &rfd);
  2779. FD_SET(xfd, &rfd);
  2780. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv) < 0) {
  2781. if(errno == EINTR)
  2782. continue;
  2783. die("select failed: %s\n", SERRNO);
  2784. }
  2785. /*
  2786. * Stop after a certain number of reads so the user does not
  2787. * feel like the system is stuttering.
  2788. */
  2789. if(i < 1000 && FD_ISSET(cmdfd, &rfd)) {
  2790. ttyread();
  2791. /*
  2792. * Just wait a bit so it isn't disturbing the
  2793. * user and the system is able to write something.
  2794. */
  2795. drawtimeout.tv_sec = 0;
  2796. drawtimeout.tv_usec = 5;
  2797. tv = &drawtimeout;
  2798. continue;
  2799. }
  2800. i = 0;
  2801. tv = NULL;
  2802. while(XPending(xw.dpy)) {
  2803. XNextEvent(xw.dpy, &ev);
  2804. if(XFilterEvent(&ev, None))
  2805. continue;
  2806. fprintf(stderr, "ev.type = %d\n", ev.type);
  2807. if(handler[ev.type])
  2808. (handler[ev.type])(&ev);
  2809. }
  2810. draw();
  2811. XFlush(xw.dpy);
  2812. }
  2813. }
  2814. int
  2815. main(int argc, char *argv[]) {
  2816. int i, bitm, xr, yr;
  2817. uint wr, hr;
  2818. xw.fw = xw.fh = xw.fx = xw.fy = 0;
  2819. xw.isfixed = False;
  2820. for(i = 1; i < argc; i++) {
  2821. switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
  2822. case 'c':
  2823. if(++i < argc)
  2824. opt_class = argv[i];
  2825. break;
  2826. case 'e':
  2827. /* eat all remaining arguments */
  2828. if(++i < argc)
  2829. opt_cmd = &argv[i];
  2830. goto run;
  2831. case 'f':
  2832. if(++i < argc)
  2833. opt_font = argv[i];
  2834. break;
  2835. case 'g':
  2836. if(++i >= argc)
  2837. break;
  2838. bitm = XParseGeometry(argv[i], &xr, &yr, &wr, &hr);
  2839. if(bitm & XValue)
  2840. xw.fx = xr;
  2841. if(bitm & YValue)
  2842. xw.fy = yr;
  2843. if(bitm & WidthValue)
  2844. xw.fw = (int)wr;
  2845. if(bitm & HeightValue)
  2846. xw.fh = (int)hr;
  2847. if(bitm & XNegative && xw.fx == 0)
  2848. xw.fx = -1;
  2849. if(bitm & XNegative && xw.fy == 0)
  2850. xw.fy = -1;
  2851. if(xw.fh != 0 && xw.fw != 0)
  2852. xw.isfixed = True;
  2853. break;
  2854. case 'o':
  2855. if(++i < argc)
  2856. opt_io = argv[i];
  2857. break;
  2858. case 't':
  2859. if(++i < argc)
  2860. opt_title = argv[i];
  2861. break;
  2862. case 'v':
  2863. default:
  2864. die(USAGE);
  2865. case 'w':
  2866. if(++i < argc)
  2867. opt_embed = argv[i];
  2868. break;
  2869. }
  2870. }
  2871. run:
  2872. setlocale(LC_CTYPE, "");
  2873. XSetLocaleModifiers("");
  2874. tnew(80, 24);
  2875. xinit();
  2876. ttynew();
  2877. selinit();
  2878. run();
  2879. return 0;
  2880. }