st.c 83 KB

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