st.c 72 KB

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