st.c 85 KB

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