st.c 70 KB

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