st.c 85 KB

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