st.c 85 KB

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