st.c 87 KB

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