st.c 83 KB

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