st.c 90 KB

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