st.c 96 KB

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