st.c 85 KB

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