st.c 84 KB

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