st.c 83 KB

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