st.c 85 KB

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