st.c 70 KB

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