st.c 96 KB

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