st.c 85 KB

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