st.c 85 KB

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