st.c 85 KB

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