st.c 84 KB

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