st.c 85 KB

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