window.c 209 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003
  1. /*
  2. * window.c - the PuTTY(tel)/pterm main program, which runs a PuTTY
  3. * terminal emulator and backend in a window.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include <time.h>
  9. #include <limits.h>
  10. #include <assert.h>
  11. #include <wchar.h>
  12. #define COMPILE_MULTIMON_STUBS
  13. #include "putty.h"
  14. #include "ssh.h"
  15. #include "terminal.h"
  16. #include "storage.h"
  17. #include "putty-rc.h"
  18. #include "security-api.h"
  19. #include "win-gui-seat.h"
  20. #include "tree234.h"
  21. #ifdef NO_MULTIMON
  22. #include <multimon.h>
  23. #endif
  24. #include <imm.h>
  25. #include <commctrl.h>
  26. #include <richedit.h>
  27. #include <mmsystem.h>
  28. /* From MSDN: In the WM_SYSCOMMAND message, the four low-order bits of
  29. * wParam are used by Windows, and should be masked off, so we shouldn't
  30. * attempt to store information in them. Hence all these identifiers have
  31. * the low 4 bits clear. Also, identifiers should < 0xF000. */
  32. #define IDM_SHOWLOG 0x0010
  33. #define IDM_NEWSESS 0x0020
  34. #define IDM_DUPSESS 0x0030
  35. #define IDM_RESTART 0x0040
  36. #define IDM_RECONF 0x0050
  37. #define IDM_CLRSB 0x0060
  38. #define IDM_RESET 0x0070
  39. #define IDM_HELP 0x0140
  40. #define IDM_ABOUT 0x0150
  41. #define IDM_SAVEDSESS 0x0160
  42. #define IDM_COPYALL 0x0170
  43. #define IDM_FULLSCREEN 0x0180
  44. #define IDM_COPY 0x0190
  45. #define IDM_PASTE 0x01A0
  46. #define IDM_SPECIALSEP 0x0200
  47. #define IDM_SPECIAL_MIN 0x0400
  48. #define IDM_SPECIAL_MAX 0x0800
  49. #define IDM_SAVED_MIN 0x1000
  50. #define IDM_SAVED_MAX 0x5000
  51. #define MENU_SAVED_STEP 16
  52. /* Maximum number of sessions on saved-session submenu */
  53. #define MENU_SAVED_MAX ((IDM_SAVED_MAX-IDM_SAVED_MIN) / MENU_SAVED_STEP)
  54. #define WM_IGNORE_CLIP (WM_APP + 2)
  55. #define WM_FULLSCR_ON_MAX (WM_APP + 3)
  56. #define WM_GOT_CLIPDATA (WM_APP + 4)
  57. /* Needed for Chinese support and apparently not always defined. */
  58. #ifndef VK_PROCESSKEY
  59. #define VK_PROCESSKEY 0xE5
  60. #endif
  61. /* Mouse wheel support. */
  62. #ifndef WM_MOUSEWHEEL
  63. #define WM_MOUSEWHEEL 0x020A /* not defined in earlier SDKs */
  64. #endif
  65. #ifndef WM_MOUSEHWHEEL
  66. #define WM_MOUSEHWHEEL 0x020E /* not defined in earlier SDKs */
  67. #endif
  68. #ifndef WHEEL_DELTA
  69. #define WHEEL_DELTA 120
  70. #endif
  71. /* DPI awareness support */
  72. #ifndef WM_DPICHANGED
  73. #define WM_DPICHANGED 0x02E0
  74. #define WM_DPICHANGED_BEFOREPARENT 0x02E2
  75. #define WM_DPICHANGED_AFTERPARENT 0x02E3
  76. #define WM_GETDPISCALEDSIZE 0x02E4
  77. #endif
  78. /* VK_PACKET, used to send Unicode characters in WM_KEYDOWNs */
  79. #ifndef VK_PACKET
  80. #define VK_PACKET 0xE7
  81. #endif
  82. static Mouse_Button translate_button(WinGuiSeat *wgs, Mouse_Button button);
  83. static void show_mouseptr(WinGuiSeat *wgs, bool show);
  84. static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  85. static int TranslateKey(WinGuiSeat *wgs, UINT message, WPARAM wParam,
  86. LPARAM lParam, unsigned char *output);
  87. static void init_palette(WinGuiSeat *wgs);
  88. static void init_fonts(WinGuiSeat *wgs, int, int);
  89. static void init_dpi_info(WinGuiSeat *wgs);
  90. static void another_font(WinGuiSeat *wgs, int);
  91. static void deinit_fonts(WinGuiSeat *wgs);
  92. static void set_input_locale(WinGuiSeat *wgs, HKL);
  93. static void update_savedsess_menu(WinGuiSeat *wgs);
  94. static void init_winfuncs(void);
  95. static bool is_full_screen(WinGuiSeat *wgs);
  96. static void make_full_screen(WinGuiSeat *wgs);
  97. static void clear_full_screen(WinGuiSeat *wgs);
  98. static void flip_full_screen(WinGuiSeat *wgs);
  99. static void process_clipdata(WinGuiSeat *wgs, HGLOBAL clipdata, bool unicode);
  100. static void setup_clipboards(Terminal *, Conf *);
  101. /* Window layout information */
  102. static void reset_window(WinGuiSeat *wgs, int reinit);
  103. static void flash_window(WinGuiSeat *wgs, int mode);
  104. static void sys_cursor_update(WinGuiSeat *wgs);
  105. static bool get_fullscreen_rect(WinGuiSeat *wgs, RECT *ss);
  106. static bool get_workingarea_rect(WinGuiSeat *wgs, RECT *ss);
  107. static void conf_cache_data(WinGuiSeat *wgs);
  108. static struct sesslist sesslist; /* for saved-session menu */
  109. enum MONITOR_DPI_TYPE { MDT_EFFECTIVE_DPI, MDT_ANGULAR_DPI, MDT_RAW_DPI, MDT_DEFAULT };
  110. DECL_WINDOWS_FUNCTION(static, BOOL, GetMonitorInfoA, (HMONITOR, LPMONITORINFO));
  111. DECL_WINDOWS_FUNCTION(static, HMONITOR, MonitorFromPoint, (POINT, DWORD));
  112. DECL_WINDOWS_FUNCTION(static, HMONITOR, MonitorFromWindow, (HWND, DWORD));
  113. DECL_WINDOWS_FUNCTION(static, HRESULT, GetDpiForMonitor, (HMONITOR hmonitor, enum MONITOR_DPI_TYPE dpiType, UINT *dpiX, UINT *dpiY));
  114. DECL_WINDOWS_FUNCTION(static, HRESULT, GetSystemMetricsForDpi, (int nIndex, UINT dpi));
  115. DECL_WINDOWS_FUNCTION(static, HRESULT, AdjustWindowRectExForDpi, (LPRECT lpRect, DWORD dwStyle, BOOL bMenu, DWORD dwExStyle, UINT dpi));
  116. static UINT wm_mousewheel = WM_MOUSEWHEEL;
  117. struct WinGuiSeatListNode wgslisthead = {
  118. .next = &wgslisthead, .prev = &wgslisthead,
  119. };
  120. #define IS_HIGH_VARSEL(wch1, wch2) \
  121. ((wch1) == 0xDB40 && ((wch2) >= 0xDD00 && (wch2) <= 0xDDEF))
  122. #define IS_LOW_VARSEL(wch) \
  123. (((wch) >= 0x180B && (wch) <= 0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR */ \
  124. ((wch) >= 0xFE00 && (wch) <= 0xFE0F)) /* VARIATION SELECTOR 1-16 */
  125. static bool wintw_setup_draw_ctx(TermWin *);
  126. static void wintw_draw_text(TermWin *, int x, int y, wchar_t *text, int len,
  127. unsigned long attrs, int lattrs, truecolour tc);
  128. static void wintw_draw_cursor(TermWin *, int x, int y, wchar_t *text, int len,
  129. unsigned long attrs, int lattrs, truecolour tc);
  130. static void wintw_draw_trust_sigil(TermWin *, int x, int y);
  131. static int wintw_char_width(TermWin *, int uc);
  132. static void wintw_free_draw_ctx(TermWin *);
  133. static void wintw_set_cursor_pos(TermWin *, int x, int y);
  134. static void wintw_set_raw_mouse_mode(TermWin *, bool enable);
  135. static void wintw_set_raw_mouse_mode_pointer(TermWin *, bool enable);
  136. static void wintw_set_scrollbar(TermWin *, int total, int start, int page);
  137. static void wintw_bell(TermWin *, int mode);
  138. static void wintw_clip_write(
  139. TermWin *, int clipboard, wchar_t *text, int *attrs,
  140. truecolour *colours, int len, bool must_deselect);
  141. static void wintw_clip_request_paste(TermWin *, int clipboard);
  142. static void wintw_refresh(TermWin *);
  143. static void wintw_request_resize(TermWin *, int w, int h);
  144. static void wintw_set_title(TermWin *, const char *title, int codepage);
  145. static void wintw_set_icon_title(TermWin *, const char *icontitle,
  146. int codepage);
  147. static void wintw_set_minimised(TermWin *, bool minimised);
  148. static void wintw_set_maximised(TermWin *, bool maximised);
  149. static void wintw_move(TermWin *, int x, int y);
  150. static void wintw_set_zorder(TermWin *, bool top);
  151. static void wintw_palette_set(TermWin *, unsigned, unsigned, const rgb *);
  152. static void wintw_palette_get_overrides(TermWin *, Terminal *);
  153. static void wintw_unthrottle(TermWin *win, size_t bufsize);
  154. static const TermWinVtable windows_termwin_vt = {
  155. .setup_draw_ctx = wintw_setup_draw_ctx,
  156. .draw_text = wintw_draw_text,
  157. .draw_cursor = wintw_draw_cursor,
  158. .draw_trust_sigil = wintw_draw_trust_sigil,
  159. .char_width = wintw_char_width,
  160. .free_draw_ctx = wintw_free_draw_ctx,
  161. .set_cursor_pos = wintw_set_cursor_pos,
  162. .set_raw_mouse_mode = wintw_set_raw_mouse_mode,
  163. .set_raw_mouse_mode_pointer = wintw_set_raw_mouse_mode_pointer,
  164. .set_scrollbar = wintw_set_scrollbar,
  165. .bell = wintw_bell,
  166. .clip_write = wintw_clip_write,
  167. .clip_request_paste = wintw_clip_request_paste,
  168. .refresh = wintw_refresh,
  169. .request_resize = wintw_request_resize,
  170. .set_title = wintw_set_title,
  171. .set_icon_title = wintw_set_icon_title,
  172. .set_minimised = wintw_set_minimised,
  173. .set_maximised = wintw_set_maximised,
  174. .move = wintw_move,
  175. .set_zorder = wintw_set_zorder,
  176. .palette_set = wintw_palette_set,
  177. .palette_get_overrides = wintw_palette_get_overrides,
  178. .unthrottle = wintw_unthrottle,
  179. };
  180. static HICON trust_icon = INVALID_HANDLE_VALUE;
  181. const bool share_can_be_downstream = true;
  182. const bool share_can_be_upstream = true;
  183. static bool is_utf8(WinGuiSeat *wgs)
  184. {
  185. return wgs->ucsdata.line_codepage == CP_UTF8;
  186. }
  187. static bool win_seat_is_utf8(Seat *seat)
  188. {
  189. WinGuiSeat *wgs = container_of(seat, WinGuiSeat, seat);
  190. return is_utf8(wgs);
  191. }
  192. static char *win_seat_get_ttymode(Seat *seat, const char *mode)
  193. {
  194. WinGuiSeat *wgs = container_of(seat, WinGuiSeat, seat);
  195. return term_get_ttymode(wgs->term, mode);
  196. }
  197. static StripCtrlChars *win_seat_stripctrl_new(
  198. Seat *seat, BinarySink *bs_out, SeatInteractionContext sic)
  199. {
  200. WinGuiSeat *wgs = container_of(seat, WinGuiSeat, seat);
  201. return stripctrl_new_term(bs_out, false, 0, wgs->term);
  202. }
  203. static size_t win_seat_output(
  204. Seat *seat, SeatOutputType type, const void *, size_t);
  205. static bool win_seat_eof(Seat *seat);
  206. static SeatPromptResult win_seat_get_userpass_input(Seat *seat, prompts_t *p);
  207. static void win_seat_notify_remote_exit(Seat *seat);
  208. static void win_seat_connection_fatal(Seat *seat, const char *msg);
  209. static void win_seat_nonfatal(Seat *seat, const char *msg);
  210. static void win_seat_update_specials_menu(Seat *seat);
  211. static void win_seat_set_busy_status(Seat *seat, BusyStatus status);
  212. static void win_seat_set_trust_status(Seat *seat, bool trusted);
  213. static bool win_seat_can_set_trust_status(Seat *seat);
  214. static bool win_seat_get_cursor_position(Seat *seat, int *x, int *y);
  215. static bool win_seat_get_window_pixel_size(Seat *seat, int *x, int *y);
  216. static const SeatVtable win_seat_vt = {
  217. .output = win_seat_output,
  218. .eof = win_seat_eof,
  219. .sent = nullseat_sent,
  220. .banner = nullseat_banner_to_stderr,
  221. .get_userpass_input = win_seat_get_userpass_input,
  222. .notify_session_started = nullseat_notify_session_started,
  223. .notify_remote_exit = win_seat_notify_remote_exit,
  224. .notify_remote_disconnect = nullseat_notify_remote_disconnect,
  225. .connection_fatal = win_seat_connection_fatal,
  226. .nonfatal = win_seat_nonfatal,
  227. .update_specials_menu = win_seat_update_specials_menu,
  228. .get_ttymode = win_seat_get_ttymode,
  229. .set_busy_status = win_seat_set_busy_status,
  230. .confirm_ssh_host_key = win_seat_confirm_ssh_host_key,
  231. .confirm_weak_crypto_primitive = win_seat_confirm_weak_crypto_primitive,
  232. .confirm_weak_cached_hostkey = win_seat_confirm_weak_cached_hostkey,
  233. .prompt_descriptions = win_seat_prompt_descriptions,
  234. .is_utf8 = win_seat_is_utf8,
  235. .echoedit_update = nullseat_echoedit_update,
  236. .get_x_display = nullseat_get_x_display,
  237. .get_windowid = nullseat_get_windowid,
  238. .get_window_pixel_size = win_seat_get_window_pixel_size,
  239. .stripctrl_new = win_seat_stripctrl_new,
  240. .set_trust_status = win_seat_set_trust_status,
  241. .can_set_trust_status = win_seat_can_set_trust_status,
  242. .has_mixed_input_stream = nullseat_has_mixed_input_stream_yes,
  243. .verbose = nullseat_verbose_yes,
  244. .interactive = nullseat_interactive_yes,
  245. .get_cursor_position = win_seat_get_cursor_position,
  246. };
  247. static void start_backend(WinGuiSeat *wgs)
  248. {
  249. const struct BackendVtable *vt;
  250. char *error, *realhost;
  251. int i;
  252. wgs->cmdline_get_passwd_state = cmdline_get_passwd_input_state_new;
  253. vt = backend_vt_from_conf(wgs->conf);
  254. seat_set_trust_status(&wgs->seat, true);
  255. error = backend_init(vt, &wgs->seat, &wgs->backend, wgs->logctx, wgs->conf,
  256. conf_get_str(wgs->conf, CONF_host),
  257. conf_get_int(wgs->conf, CONF_port),
  258. &realhost,
  259. conf_get_bool(wgs->conf, CONF_tcp_nodelay),
  260. conf_get_bool(wgs->conf, CONF_tcp_keepalives));
  261. if (error) {
  262. char *str = dupprintf("%s Error", appname);
  263. char *msg;
  264. if (cmdline_tooltype & TOOLTYPE_NONNETWORK) {
  265. /* Special case for pterm. */
  266. msg = dupprintf("Unable to open terminal:\n%s", error);
  267. } else {
  268. msg = dupprintf("Unable to open connection to\n%s\n%s",
  269. conf_dest(wgs->conf), error);
  270. }
  271. sfree(error);
  272. MessageBox(NULL, msg, str, MB_ICONERROR | MB_OK);
  273. sfree(str);
  274. sfree(msg);
  275. exit(0);
  276. }
  277. term_setup_window_titles(wgs->term, realhost);
  278. sfree(realhost);
  279. /*
  280. * Connect the terminal to the backend for resize purposes.
  281. */
  282. term_provide_backend(wgs->term, wgs->backend);
  283. /*
  284. * Set up a line discipline.
  285. */
  286. wgs->ldisc = ldisc_create(wgs->conf, wgs->term, wgs->backend, &wgs->seat);
  287. /*
  288. * Destroy the Restart Session menu item. (This will return
  289. * failure if it's already absent, as it will be the very first
  290. * time we call this function. We ignore that, because as long
  291. * as the menu item ends up not being there, we don't care
  292. * whether it was us who removed it or not!)
  293. */
  294. for (i = 0; i < lenof(wgs->popup_menus); i++) {
  295. DeleteMenu(wgs->popup_menus[i].menu, IDM_RESTART, MF_BYCOMMAND);
  296. }
  297. wgs->session_closed = false;
  298. }
  299. static void close_session(void *vctx)
  300. {
  301. WinGuiSeat *wgs = (WinGuiSeat *)vctx;
  302. char *newtitle;
  303. int i;
  304. wgs->session_closed = true;
  305. newtitle = dupprintf("%s (inactive)", appname);
  306. win_set_icon_title(&wgs->termwin, newtitle, DEFAULT_CODEPAGE);
  307. win_set_title(&wgs->termwin, newtitle, DEFAULT_CODEPAGE);
  308. sfree(newtitle);
  309. if (wgs->ldisc) {
  310. ldisc_free(wgs->ldisc);
  311. wgs->ldisc = NULL;
  312. }
  313. if (wgs->backend) {
  314. backend_free(wgs->backend);
  315. wgs->backend = NULL;
  316. term_provide_backend(wgs->term, NULL);
  317. seat_update_specials_menu(&wgs->seat);
  318. }
  319. /*
  320. * Show the Restart Session menu item. Do a precautionary
  321. * delete first to ensure we never end up with more than one.
  322. */
  323. for (i = 0; i < lenof(wgs->popup_menus); i++) {
  324. DeleteMenu(wgs->popup_menus[i].menu, IDM_RESTART, MF_BYCOMMAND);
  325. InsertMenu(wgs->popup_menus[i].menu, IDM_DUPSESS,
  326. MF_BYCOMMAND | MF_ENABLED, IDM_RESTART, "&Restart Session");
  327. }
  328. }
  329. /*
  330. * Some machinery to deal with switching the window type between ANSI
  331. * and Unicode. We prefer Unicode, but some PuTTY builds still try to
  332. * run on machines so old that they don't support that mode. So we're
  333. * prepared to fall back to an ANSI window if we have to. For this
  334. * purpose, we swap out a few Windows API functions, and wrap
  335. * SetWindowText so that if we're not in Unicode mode we first convert
  336. * the wide string we're given.
  337. */
  338. static bool unicode_window;
  339. static BOOL (WINAPI *sw_PeekMessage)(LPMSG, HWND, UINT, UINT, UINT);
  340. static LRESULT (WINAPI *sw_DispatchMessage)(const MSG *);
  341. static LRESULT (WINAPI *sw_DefWindowProc)(HWND, UINT, WPARAM, LPARAM);
  342. static void sw_SetWindowText(HWND hwnd, wchar_t *text)
  343. {
  344. if (unicode_window) {
  345. SetWindowTextW(hwnd, text);
  346. } else {
  347. char *mb = dup_wc_to_mb(DEFAULT_CODEPAGE, text, "?");
  348. SetWindowTextA(hwnd, mb);
  349. sfree(mb);
  350. }
  351. }
  352. static HINSTANCE hprev;
  353. /*
  354. * Also, registering window classes has to be done in a fiddly way.
  355. */
  356. #define SETUP_WNDCLASS(wndclass, classname) do { \
  357. wndclass.style = 0; \
  358. wndclass.lpfnWndProc = WndProc; \
  359. wndclass.cbClsExtra = 0; \
  360. wndclass.cbWndExtra = 0; \
  361. wndclass.hInstance = hinst; \
  362. wndclass.hIcon = LoadIcon(hinst, MAKEINTRESOURCE(IDI_MAINICON)); \
  363. wndclass.hCursor = LoadCursor(NULL, IDC_IBEAM); \
  364. wndclass.hbrBackground = NULL; \
  365. wndclass.lpszMenuName = NULL; \
  366. wndclass.lpszClassName = classname; \
  367. } while (0)
  368. wchar_t *terminal_window_class_w(void)
  369. {
  370. static wchar_t *classname = NULL;
  371. if (!classname)
  372. classname = dup_mb_to_wc(DEFAULT_CODEPAGE, appname);
  373. if (!hprev) {
  374. WNDCLASSW wndclassw;
  375. SETUP_WNDCLASS(wndclassw, classname);
  376. RegisterClassW(&wndclassw);
  377. }
  378. return classname;
  379. }
  380. char *terminal_window_class_a(void)
  381. {
  382. static char *classname = NULL;
  383. if (!classname)
  384. classname = dupcat(appname, ".ansi");
  385. if (!hprev) {
  386. WNDCLASSA wndclassa;
  387. SETUP_WNDCLASS(wndclassa, classname);
  388. RegisterClassA(&wndclassa);
  389. }
  390. return classname;
  391. }
  392. HINSTANCE hinst;
  393. int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
  394. {
  395. MSG msg;
  396. HRESULT hr;
  397. int guess_width, guess_height;
  398. dll_hijacking_protection();
  399. hinst = inst;
  400. hprev = prev;
  401. sk_init();
  402. init_common_controls();
  403. /* Set Explicit App User Model Id so that jump lists don't cause
  404. PuTTY to hang on to removable media. */
  405. set_explicit_app_user_model_id();
  406. /* Ensure a Maximize setting in Explorer doesn't maximise the
  407. * config box. */
  408. defuse_showwindow();
  409. init_winver();
  410. /*
  411. * If we're running a version of Windows that doesn't support
  412. * WM_MOUSEWHEEL, find out what message number we should be
  413. * using instead.
  414. */
  415. if (osMajorVersion < 4 ||
  416. (osMajorVersion == 4 && osPlatformId != VER_PLATFORM_WIN32_NT))
  417. wm_mousewheel = RegisterWindowMessage("MSWHEEL_ROLLMSG");
  418. init_help();
  419. init_winfuncs();
  420. setup_gui_timing();
  421. WinGuiSeat *wgs = snew(WinGuiSeat);
  422. memset(wgs, 0, sizeof(*wgs));
  423. wgs_link(wgs);
  424. wgs->seat.vt = &win_seat_vt;
  425. wgs->logpolicy.vt = &win_gui_logpolicy_vt;
  426. wgs->termwin.vt = &windows_termwin_vt;
  427. wgs->caret_x = wgs->caret_y = -1;
  428. wgs->busy_status = BUSY_NOT;
  429. wgs->conf = conf_new();
  430. /*
  431. * Initialize COM.
  432. */
  433. hr = CoInitialize(NULL);
  434. if (hr != S_OK && hr != S_FALSE) {
  435. char *str = dupprintf("%s Fatal Error", appname);
  436. MessageBox(NULL, "Failed to initialize COM subsystem",
  437. str, MB_OK | MB_ICONEXCLAMATION);
  438. sfree(str);
  439. return 1;
  440. }
  441. /*
  442. * Process the command line.
  443. * (If the command line doesn't provide enough info to start a
  444. * session, this will detour via the config box.)
  445. */
  446. gui_term_process_cmdline(wgs->conf, cmdline);
  447. memset(&wgs->ucsdata, 0, sizeof(wgs->ucsdata));
  448. conf_cache_data(wgs);
  449. /*
  450. * Guess some defaults for the window size. This all gets
  451. * updated later, so we don't really care too much. However, we
  452. * do want the font width/height guesses to correspond to a
  453. * large font rather than a small one...
  454. */
  455. wgs->font_width = 10;
  456. wgs->font_height = 20;
  457. wgs->extra_width = 25;
  458. wgs->extra_height = 28;
  459. guess_width = wgs->extra_width + wgs->font_width * conf_get_int(
  460. wgs->conf, CONF_width);
  461. guess_height = wgs->extra_height + wgs->font_height * conf_get_int(
  462. wgs->conf, CONF_height);
  463. {
  464. RECT r;
  465. get_fullscreen_rect(wgs, &r);
  466. if (guess_width > r.right - r.left)
  467. guess_width = r.right - r.left;
  468. if (guess_height > r.bottom - r.top)
  469. guess_height = r.bottom - r.top;
  470. }
  471. {
  472. int winmode = WS_OVERLAPPEDWINDOW | WS_VSCROLL;
  473. int exwinmode = 0;
  474. const struct BackendVtable *vt =
  475. backend_vt_from_proto(be_default_protocol);
  476. bool resize_forbidden = false;
  477. if (vt && vt->flags & BACKEND_RESIZE_FORBIDDEN)
  478. resize_forbidden = true;
  479. wchar_t *uappname = dup_mb_to_wc(DEFAULT_CODEPAGE, appname);
  480. wgs->window_name = dup_mb_to_wc(DEFAULT_CODEPAGE, appname);
  481. wgs->icon_name = dup_mb_to_wc(DEFAULT_CODEPAGE, appname);
  482. if (!conf_get_bool(wgs->conf, CONF_scrollbar))
  483. winmode &= ~(WS_VSCROLL);
  484. if (conf_get_int(wgs->conf, CONF_resize_action) == RESIZE_DISABLED ||
  485. resize_forbidden)
  486. winmode &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX);
  487. if (conf_get_bool(wgs->conf, CONF_alwaysontop))
  488. exwinmode |= WS_EX_TOPMOST;
  489. if (conf_get_bool(wgs->conf, CONF_sunken_edge))
  490. exwinmode |= WS_EX_CLIENTEDGE;
  491. #ifdef TEST_ANSI_WINDOW
  492. /* For developer testing of ANSI window support, pretend
  493. * CreateWindowExW failed */
  494. wgs->term_hwnd = NULL;
  495. SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  496. #else
  497. unicode_window = true;
  498. sw_PeekMessage = PeekMessageW;
  499. sw_DispatchMessage = DispatchMessageW;
  500. sw_DefWindowProc = DefWindowProcW;
  501. wgs->term_hwnd = CreateWindowExW(
  502. exwinmode, terminal_window_class_w(), uappname,
  503. winmode, CW_USEDEFAULT, CW_USEDEFAULT,
  504. guess_width, guess_height, NULL, NULL, inst, NULL);
  505. #endif
  506. #if defined LEGACY_WINDOWS || defined TEST_ANSI_WINDOW
  507. if (!wgs->term_hwnd && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
  508. /* Fall back to an ANSI window, swapping in all the ANSI
  509. * window message handling functions */
  510. unicode_window = false;
  511. sw_PeekMessage = PeekMessageA;
  512. sw_DispatchMessage = DispatchMessageA;
  513. sw_DefWindowProc = DefWindowProcA;
  514. wgs->term_hwnd = CreateWindowExA(
  515. exwinmode, terminal_window_class_a(), appname,
  516. winmode, CW_USEDEFAULT, CW_USEDEFAULT,
  517. guess_width, guess_height, NULL, NULL, inst, NULL);
  518. }
  519. #endif
  520. if (!wgs->term_hwnd) {
  521. modalfatalbox("Unable to create terminal window: %s",
  522. win_strerror(GetLastError()));
  523. }
  524. memset(&wgs->dpi_info, 0, sizeof(struct _dpi_info));
  525. init_dpi_info(wgs);
  526. sfree(uappname);
  527. }
  528. SetWindowLongPtr(wgs->term_hwnd, GWLP_USERDATA, (LONG_PTR)wgs);
  529. /*
  530. * Initialise the fonts, simultaneously correcting the guesses
  531. * for font_{width,height}.
  532. */
  533. init_fonts(wgs, 0, 0);
  534. /*
  535. * Prepare a logical palette.
  536. */
  537. init_palette(wgs);
  538. /*
  539. * Initialise the terminal. (We have to do this _after_
  540. * creating the window, since the terminal is the first thing
  541. * which will call schedule_timer(), which will in turn call
  542. * timer_change_notify() which will expect hwnd to exist.)
  543. */
  544. wgs->term = term_init(wgs->conf, &wgs->ucsdata, &wgs->termwin);
  545. setup_clipboards(wgs->term, wgs->conf);
  546. wgs->logctx = log_init(&wgs->logpolicy, wgs->conf);
  547. term_provide_logctx(wgs->term, wgs->logctx);
  548. term_size(wgs->term, conf_get_int(wgs->conf, CONF_height),
  549. conf_get_int(wgs->conf, CONF_width),
  550. conf_get_int(wgs->conf, CONF_savelines));
  551. /*
  552. * Correct the guesses for extra_{width,height}.
  553. */
  554. {
  555. RECT cr, wr;
  556. GetWindowRect(wgs->term_hwnd, &wr);
  557. GetClientRect(wgs->term_hwnd, &cr);
  558. wgs->offset_width = wgs->offset_height =
  559. conf_get_int(wgs->conf, CONF_window_border);
  560. wgs->extra_width =
  561. wr.right - wr.left - cr.right + cr.left + wgs->offset_width*2;
  562. wgs->extra_height =
  563. wr.bottom - wr.top - cr.bottom + cr.top +wgs->offset_height*2;
  564. }
  565. /*
  566. * Compute what size we _really_ want the window to be.
  567. */
  568. guess_width = wgs->extra_width + wgs->font_width * wgs->term->cols;
  569. guess_height = wgs->extra_height + wgs->font_height * wgs->term->rows;
  570. /*
  571. * Resize the window to that size, also repositioning it if it's extended
  572. * off the edge of a monitor.
  573. */
  574. {
  575. /* Find the previous coordinates of the window */
  576. RECT winr;
  577. GetWindowRect(wgs->term_hwnd, &winr);
  578. int x = winr.left;
  579. int y = winr.top;
  580. /* Adjust them if necessary */
  581. RECT war;
  582. if (get_workingarea_rect(wgs, &war)) {
  583. /*
  584. * Try to ensure the window is entirely within the monitor's
  585. * working area, by adjusting its position if not.
  586. *
  587. * We first move it left, if it overlaps off the right side. Then
  588. * we move it right if it overlaps off the left side. This means
  589. * that if it's wider than the working area (so that some overlap
  590. * is unavoidable), we prefer to get its left edge in bounds than
  591. * its right edge. Similarly, we do the y checks in the same
  592. * order, privileging the top edge over the bottom.
  593. */
  594. if (x + guess_width > war.right)
  595. x = war.right - guess_width;
  596. if (x < war.left)
  597. x = war.left;
  598. if (y + guess_height > war.bottom)
  599. y = war.bottom - guess_height;
  600. if (y < war.top)
  601. y = war.top;
  602. }
  603. /* And set the window to the final size and position we've chosen */
  604. SetWindowPos(wgs->term_hwnd, NULL, x, y, guess_width, guess_height,
  605. SWP_NOREDRAW | SWP_NOZORDER);
  606. }
  607. /*
  608. * Set up a caret bitmap, with no content.
  609. */
  610. {
  611. char *bits;
  612. int size = (wgs->font_width + 15) / 16 * 2 * wgs->font_height;
  613. bits = snewn(size, char);
  614. memset(bits, 0, size);
  615. wgs->caretbm = CreateBitmap(wgs->font_width, wgs->font_height,
  616. 1, 1, bits);
  617. sfree(bits);
  618. }
  619. CreateCaret(wgs->term_hwnd, wgs->caretbm,
  620. wgs->font_width, wgs->font_height);
  621. /*
  622. * Initialise the scroll bar.
  623. */
  624. {
  625. SCROLLINFO si;
  626. si.cbSize = sizeof(si);
  627. si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
  628. si.nMin = 0;
  629. si.nMax = wgs->term->rows - 1;
  630. si.nPage = wgs->term->rows;
  631. si.nPos = 0;
  632. SetScrollInfo(wgs->term_hwnd, SB_VERT, &si, false);
  633. }
  634. /*
  635. * Prepare the mouse handler.
  636. */
  637. wgs->lastact = MA_NOTHING;
  638. wgs->lastbtn = MBT_NOTHING;
  639. wgs->dbltime = GetDoubleClickTime();
  640. /*
  641. * Set up the session-control options on the system menu.
  642. */
  643. {
  644. HMENU m;
  645. int j;
  646. char *str;
  647. wgs->popup_menus[SYSMENU].menu = GetSystemMenu(wgs->term_hwnd, false);
  648. wgs->popup_menus[CTXMENU].menu = CreatePopupMenu();
  649. for (j = 0; j < lenof(wgs->popup_menus); j++) {
  650. m = wgs->popup_menus[j].menu;
  651. AppendMenu(m, MF_ENABLED, IDM_COPY, "&Copy");
  652. AppendMenu(m, MF_ENABLED, IDM_PASTE, "&Paste");
  653. }
  654. wgs->savedsess_menu = CreateMenu();
  655. get_sesslist(&sesslist, true);
  656. update_savedsess_menu(wgs);
  657. for (j = 0; j < lenof(wgs->popup_menus); j++) {
  658. m = wgs->popup_menus[j].menu;
  659. AppendMenu(m, MF_SEPARATOR, 0, 0);
  660. AppendMenu(m, MF_ENABLED, IDM_SHOWLOG, "&Event Log");
  661. AppendMenu(m, MF_SEPARATOR, 0, 0);
  662. AppendMenu(m, MF_ENABLED, IDM_NEWSESS, "Ne&w Session...");
  663. AppendMenu(m, MF_ENABLED, IDM_DUPSESS, "&Duplicate Session");
  664. AppendMenu(m, MF_POPUP | MF_ENABLED, (UINT_PTR)wgs->savedsess_menu,
  665. "Sa&ved Sessions");
  666. AppendMenu(m, MF_ENABLED, IDM_RECONF, "Chan&ge Settings...");
  667. AppendMenu(m, MF_SEPARATOR, 0, 0);
  668. AppendMenu(m, MF_ENABLED, IDM_COPYALL, "C&opy All to Clipboard");
  669. AppendMenu(m, MF_ENABLED, IDM_CLRSB, "C&lear Scrollback");
  670. AppendMenu(m, MF_ENABLED, IDM_RESET, "Rese&t Terminal");
  671. AppendMenu(m, MF_SEPARATOR, 0, 0);
  672. AppendMenu(m, (conf_get_int(wgs->conf, CONF_resize_action)
  673. == RESIZE_DISABLED) ? MF_GRAYED : MF_ENABLED,
  674. IDM_FULLSCREEN, "&Full Screen");
  675. AppendMenu(m, MF_SEPARATOR, 0, 0);
  676. if (has_help())
  677. AppendMenu(m, MF_ENABLED, IDM_HELP, "&Help");
  678. str = dupprintf("&About %s", appname);
  679. AppendMenu(m, MF_ENABLED, IDM_ABOUT, str);
  680. sfree(str);
  681. }
  682. }
  683. if (restricted_acl()) {
  684. lp_eventlog(&wgs->logpolicy, "Running with restricted process ACL");
  685. }
  686. winselgui_set_hwnd(wgs->term_hwnd);
  687. start_backend(wgs);
  688. /*
  689. * Set up the initial input locale.
  690. */
  691. set_input_locale(wgs, GetKeyboardLayout(0));
  692. /*
  693. * Finally show the window!
  694. */
  695. ShowWindow(wgs->term_hwnd, show);
  696. SetForegroundWindow(wgs->term_hwnd);
  697. term_set_focus(wgs->term, GetForegroundWindow() == wgs->term_hwnd);
  698. UpdateWindow(wgs->term_hwnd);
  699. gui_terminal_ready(wgs->term_hwnd, &wgs->seat, wgs->backend);
  700. while (1) {
  701. int n;
  702. DWORD timeout;
  703. if (toplevel_callback_pending() ||
  704. PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
  705. /*
  706. * If we have anything we'd like to do immediately, set
  707. * the timeout for MsgWaitForMultipleObjects to zero so
  708. * that we'll only do a quick check of our handles and
  709. * then get on with whatever that was.
  710. *
  711. * One such option is a pending toplevel callback. The
  712. * other is a non-empty Windows message queue, which you'd
  713. * think we could leave to MsgWaitForMultipleObjects to
  714. * check for us along with all the handles, but in fact we
  715. * can't because once PeekMessage in one iteration of this
  716. * loop has removed a message from the queue, the whole
  717. * queue is considered uninteresting by the next
  718. * invocation of MWFMO. So we check ourselves whether the
  719. * message queue is non-empty, and if so, set this timeout
  720. * to zero to ensure MWFMO doesn't block.
  721. */
  722. timeout = 0;
  723. } else {
  724. timeout = INFINITE;
  725. /* The messages seem unreliable; especially if we're being tricky */
  726. term_set_focus(wgs->term, GetForegroundWindow() == wgs->term_hwnd);
  727. }
  728. HandleWaitList *hwl = get_handle_wait_list();
  729. n = MsgWaitForMultipleObjects(hwl->nhandles, hwl->handles, false,
  730. timeout, QS_ALLINPUT);
  731. if ((unsigned)(n - WAIT_OBJECT_0) < (unsigned)hwl->nhandles)
  732. handle_wait_activate(hwl, n - WAIT_OBJECT_0);
  733. handle_wait_list_free(hwl);
  734. while (sw_PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  735. if (msg.message == WM_QUIT)
  736. goto finished; /* two-level break */
  737. HWND logbox = event_log_window();
  738. if (!(IsWindow(logbox) && IsDialogMessage(logbox, &msg)))
  739. sw_DispatchMessage(&msg);
  740. /*
  741. * WM_NETEVENT messages seem to jump ahead of others in
  742. * the message queue. I'm not sure why; the docs for
  743. * PeekMessage mention that messages are prioritised in
  744. * some way, but I'm unclear on which priorities go where.
  745. *
  746. * Anyway, in practice I observe that WM_NETEVENT seems to
  747. * jump to the head of the queue, which means that if we
  748. * were to only process one message every time round this
  749. * loop, we'd get nothing but NETEVENTs if the server
  750. * flooded us with data, and stop responding to any other
  751. * kind of window message. So instead, we keep on round
  752. * this loop until we've consumed at least one message
  753. * that _isn't_ a NETEVENT, or run out of messages
  754. * completely (whichever comes first). And we don't go to
  755. * run_toplevel_callbacks (which is where the netevents
  756. * are actually processed, causing fresh NETEVENT messages
  757. * to appear) until we've done this.
  758. */
  759. if (msg.message != WM_NETEVENT)
  760. break;
  761. }
  762. run_toplevel_callbacks();
  763. }
  764. finished:
  765. cleanup_exit(msg.wParam); /* this doesn't return... */
  766. return msg.wParam; /* ... but optimiser doesn't know */
  767. }
  768. static void wgs_cleanup(WinGuiSeat *wgs)
  769. {
  770. deinit_fonts(wgs);
  771. sfree(wgs->logpal);
  772. if (wgs->pal)
  773. DeleteObject(wgs->pal);
  774. wgs_unlink(wgs);
  775. sfree(wgs);
  776. }
  777. char *handle_restrict_acl_cmdline_prefix(char *p)
  778. {
  779. /*
  780. * Process the &R prefix on a command line, which is equivalent to
  781. * -restrict-acl but lexically easier to prepend when another
  782. * instance of ourself automatically constructs a command line.
  783. *
  784. * If successful, restricts the process ACL and advances the input
  785. * pointer past the prefix. Returns the updated pointer (whether
  786. * it moved or not).
  787. */
  788. while (*p && isspace((unsigned char)*p))
  789. p++;
  790. if (*p == '&' && p[1] == 'R' &&
  791. (!p[2] || p[2] == '@' || p[2] == '&')) {
  792. /* &R restrict-acl prefix */
  793. restrict_process_acl();
  794. p += 2;
  795. }
  796. return p;
  797. }
  798. bool handle_special_sessionname_cmdline(char *p, Conf *conf)
  799. {
  800. /*
  801. * Process the special form of command line with an initial @
  802. * followed by the name of a saved session with _no quoting or
  803. * escaping_. This is a very convenient means of automated
  804. * saved-session launching, via IDM_SAVEDSESS or Windows 7 jump
  805. * lists.
  806. *
  807. * If successful, the whole command line has been interpreted in
  808. * this way, so there's nothing left to parse into other arguments.
  809. */
  810. if (*p != '@')
  811. return false;
  812. ptrlen sessionname = ptrlen_from_asciz(p + 1);
  813. while (sessionname.len > 0 &&
  814. isspace(((unsigned char *)sessionname.ptr)[sessionname.len-1]))
  815. sessionname.len--;
  816. char *dup = mkstr(sessionname);
  817. bool loaded = do_defaults(dup, conf);
  818. sfree(dup);
  819. return loaded;
  820. }
  821. bool handle_special_filemapping_cmdline(char *p, Conf *conf)
  822. {
  823. /*
  824. * Process the special form of command line with an initial &
  825. * followed by the hex value of a HANDLE for a file mapping object
  826. * and the size of the data contained in it, which we must
  827. * interpret as a serialised Conf.
  828. *
  829. * If successful, the whole command line has been interpreted in
  830. * this way, so there's nothing left to parse into other arguments.
  831. */
  832. if (*p != '&')
  833. return false;
  834. HANDLE filemap;
  835. unsigned cpsize;
  836. if (sscanf(p + 1, "%p:%u", &filemap, &cpsize) != 2)
  837. return false;
  838. void *cp = MapViewOfFile(filemap, FILE_MAP_READ, 0, 0, cpsize);
  839. if (!cp)
  840. return false;
  841. BinarySource src[1];
  842. BinarySource_BARE_INIT(src, cp, cpsize);
  843. if (!conf_deserialise(conf, src))
  844. modalfatalbox("Serialised configuration data was invalid");
  845. UnmapViewOfFile(cp);
  846. CloseHandle(filemap);
  847. return true;
  848. }
  849. static void setup_clipboards(Terminal *term, Conf *conf)
  850. {
  851. assert(term->mouse_select_clipboards[0] == CLIP_LOCAL);
  852. term->n_mouse_select_clipboards = 1;
  853. if (conf_get_bool(conf, CONF_mouseautocopy)) {
  854. term->mouse_select_clipboards[
  855. term->n_mouse_select_clipboards++] = CLIP_SYSTEM;
  856. }
  857. switch (conf_get_int(conf, CONF_mousepaste)) {
  858. case CLIPUI_IMPLICIT:
  859. term->mouse_paste_clipboard = CLIP_LOCAL;
  860. break;
  861. case CLIPUI_EXPLICIT:
  862. term->mouse_paste_clipboard = CLIP_SYSTEM;
  863. break;
  864. default:
  865. term->mouse_paste_clipboard = CLIP_NULL;
  866. break;
  867. }
  868. }
  869. /*
  870. * Clean up and exit.
  871. */
  872. void cleanup_exit(int code)
  873. {
  874. /*
  875. * Clean up.
  876. */
  877. while (wgslisthead.next != &wgslisthead) {
  878. WinGuiSeat *wgs = container_of(
  879. wgslisthead.next, WinGuiSeat, wgslistnode);
  880. wgs_cleanup(wgs);
  881. }
  882. sk_cleanup();
  883. random_save_seed();
  884. shutdown_help();
  885. /* Clean up COM. */
  886. CoUninitialize();
  887. exit(code);
  888. }
  889. /*
  890. * Refresh the saved-session submenu from `sesslist'.
  891. */
  892. static void update_savedsess_menu(WinGuiSeat *wgs)
  893. {
  894. int i;
  895. while (DeleteMenu(wgs->savedsess_menu, 0, MF_BYPOSITION)) ;
  896. /* skip sesslist.sessions[0] == Default Settings */
  897. for (i = 1;
  898. i < ((sesslist.nsessions <= MENU_SAVED_MAX+1) ? sesslist.nsessions
  899. : MENU_SAVED_MAX+1);
  900. i++)
  901. AppendMenu(wgs->savedsess_menu, MF_ENABLED,
  902. IDM_SAVED_MIN + (i-1)*MENU_SAVED_STEP,
  903. sesslist.sessions[i]);
  904. if (sesslist.nsessions <= 1)
  905. AppendMenu(wgs->savedsess_menu, MF_GRAYED, IDM_SAVED_MIN,
  906. "(No sessions)");
  907. }
  908. /*
  909. * Update the Special Commands submenu.
  910. */
  911. static void win_seat_update_specials_menu(Seat *seat)
  912. {
  913. WinGuiSeat *wgs = container_of(seat, WinGuiSeat, seat);
  914. HMENU new_menu;
  915. int i, j;
  916. if (wgs->backend)
  917. wgs->specials = backend_get_specials(wgs->backend);
  918. else
  919. wgs->specials = NULL;
  920. if (wgs->specials) {
  921. /* We can't use Windows to provide a stack for submenus, so
  922. * here's a lame "stack" that will do for now. */
  923. HMENU saved_menu = NULL;
  924. int nesting = 1;
  925. new_menu = CreatePopupMenu();
  926. for (i = 0; nesting > 0; i++) {
  927. assert(IDM_SPECIAL_MIN + 0x10 * i < IDM_SPECIAL_MAX);
  928. switch (wgs->specials[i].code) {
  929. case SS_SEP:
  930. AppendMenu(new_menu, MF_SEPARATOR, 0, 0);
  931. break;
  932. case SS_SUBMENU:
  933. assert(nesting < 2);
  934. nesting++;
  935. saved_menu = new_menu; /* XXX lame stacking */
  936. new_menu = CreatePopupMenu();
  937. AppendMenu(saved_menu, MF_POPUP | MF_ENABLED,
  938. (UINT_PTR) new_menu, wgs->specials[i].name);
  939. break;
  940. case SS_EXITMENU:
  941. nesting--;
  942. if (nesting) {
  943. new_menu = saved_menu; /* XXX lame stacking */
  944. saved_menu = NULL;
  945. }
  946. break;
  947. default:
  948. AppendMenu(new_menu, MF_ENABLED, IDM_SPECIAL_MIN + 0x10 * i,
  949. wgs->specials[i].name);
  950. break;
  951. }
  952. }
  953. /* Squirrel the highest special. */
  954. wgs->n_specials = i - 1;
  955. } else {
  956. new_menu = NULL;
  957. wgs->n_specials = 0;
  958. }
  959. for (j = 0; j < lenof(wgs->popup_menus); j++) {
  960. if (wgs->specials_menu) {
  961. /* XXX does this free up all submenus? */
  962. DeleteMenu(wgs->popup_menus[j].menu, (UINT_PTR)wgs->specials_menu,
  963. MF_BYCOMMAND);
  964. DeleteMenu(wgs->popup_menus[j].menu, IDM_SPECIALSEP, MF_BYCOMMAND);
  965. }
  966. if (new_menu) {
  967. InsertMenu(wgs->popup_menus[j].menu, IDM_SHOWLOG,
  968. MF_BYCOMMAND | MF_POPUP | MF_ENABLED,
  969. (UINT_PTR) new_menu, "S&pecial Command");
  970. InsertMenu(wgs->popup_menus[j].menu, IDM_SHOWLOG,
  971. MF_BYCOMMAND | MF_SEPARATOR, IDM_SPECIALSEP, 0);
  972. }
  973. }
  974. wgs->specials_menu = new_menu;
  975. }
  976. static void update_mouse_pointer(WinGuiSeat *wgs)
  977. {
  978. LPTSTR curstype = NULL;
  979. bool force_visible = false;
  980. static bool forced_visible = false;
  981. switch (wgs->busy_status) {
  982. case BUSY_NOT:
  983. if (wgs->pointer_indicates_raw_mouse)
  984. curstype = IDC_ARROW;
  985. else
  986. curstype = IDC_IBEAM;
  987. break;
  988. case BUSY_WAITING:
  989. curstype = IDC_APPSTARTING; /* this may be an abuse */
  990. force_visible = true;
  991. break;
  992. case BUSY_CPU:
  993. curstype = IDC_WAIT;
  994. force_visible = true;
  995. break;
  996. default:
  997. unreachable("Bad busy_status");
  998. }
  999. {
  1000. HCURSOR cursor = LoadCursor(NULL, curstype);
  1001. SetClassLongPtr(wgs->term_hwnd, GCLP_HCURSOR, (LONG_PTR)cursor);
  1002. SetCursor(cursor); /* force redraw of cursor at current posn */
  1003. }
  1004. if (force_visible != forced_visible) {
  1005. /* We want some cursor shapes to be visible always.
  1006. * Along with show_mouseptr(), this manages the ShowCursor()
  1007. * counter such that if we switch back to a non-force_visible
  1008. * cursor, the previous visibility state is restored. */
  1009. ShowCursor(force_visible);
  1010. forced_visible = force_visible;
  1011. }
  1012. }
  1013. static void win_seat_set_busy_status(Seat *seat, BusyStatus status)
  1014. {
  1015. WinGuiSeat *wgs = container_of(seat, WinGuiSeat, seat);
  1016. wgs->busy_status = status;
  1017. update_mouse_pointer(wgs);
  1018. }
  1019. static void wintw_set_raw_mouse_mode(TermWin *tw, bool activate)
  1020. {
  1021. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  1022. wgs->send_raw_mouse = activate;
  1023. }
  1024. static void wintw_set_raw_mouse_mode_pointer(TermWin *tw, bool activate)
  1025. {
  1026. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  1027. wgs->pointer_indicates_raw_mouse = activate;
  1028. update_mouse_pointer(wgs);
  1029. }
  1030. /*
  1031. * Print a message box and close the connection.
  1032. */
  1033. static void win_seat_connection_fatal(Seat *seat, const char *msg)
  1034. {
  1035. WinGuiSeat *wgs = container_of(seat, WinGuiSeat, seat);
  1036. char *title = dupprintf("%s Fatal Error", appname);
  1037. show_mouseptr(wgs, true);
  1038. MessageBox(wgs->term_hwnd, msg, title, MB_ICONERROR | MB_OK);
  1039. sfree(title);
  1040. if (conf_get_int(wgs->conf, CONF_close_on_exit) == FORCE_ON)
  1041. PostQuitMessage(1);
  1042. else {
  1043. queue_toplevel_callback(close_session, wgs);
  1044. }
  1045. }
  1046. /*
  1047. * Print a message box and don't close the connection.
  1048. */
  1049. static void win_seat_nonfatal(Seat *seat, const char *msg)
  1050. {
  1051. WinGuiSeat *wgs = container_of(seat, WinGuiSeat, seat);
  1052. char *title = dupprintf("%s Error", appname);
  1053. show_mouseptr(wgs, true);
  1054. MessageBox(wgs->term_hwnd, msg, title, MB_ICONERROR | MB_OK);
  1055. sfree(title);
  1056. }
  1057. static HWND find_window_for_msgbox(void)
  1058. {
  1059. if (wgslisthead.next != &wgslisthead) {
  1060. WinGuiSeat *wgs = container_of(
  1061. wgslisthead.next, WinGuiSeat, wgslistnode);
  1062. return wgs->term_hwnd;
  1063. }
  1064. return NULL;
  1065. }
  1066. /*
  1067. * Report an error at the command-line parsing stage.
  1068. */
  1069. void cmdline_error(const char *fmt, ...)
  1070. {
  1071. va_list ap;
  1072. char *message, *title;
  1073. va_start(ap, fmt);
  1074. message = dupvprintf(fmt, ap);
  1075. va_end(ap);
  1076. title = dupprintf("%s Command Line Error", appname);
  1077. MessageBox(find_window_for_msgbox(), message, title, MB_ICONERROR | MB_OK);
  1078. sfree(message);
  1079. sfree(title);
  1080. exit(1);
  1081. }
  1082. static inline rgb rgb_from_colorref(COLORREF cr)
  1083. {
  1084. rgb toret;
  1085. toret.r = GetRValue(cr);
  1086. toret.g = GetGValue(cr);
  1087. toret.b = GetBValue(cr);
  1088. return toret;
  1089. }
  1090. static void wintw_palette_get_overrides(TermWin *tw, Terminal *term)
  1091. {
  1092. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  1093. if (conf_get_bool(wgs->conf, CONF_system_colour)) {
  1094. rgb rgb;
  1095. rgb = rgb_from_colorref(GetSysColor(COLOR_WINDOWTEXT));
  1096. term_palette_override(term, OSC4_COLOUR_fg, rgb);
  1097. term_palette_override(term, OSC4_COLOUR_fg_bold, rgb);
  1098. rgb = rgb_from_colorref(GetSysColor(COLOR_WINDOW));
  1099. term_palette_override(term, OSC4_COLOUR_bg, rgb);
  1100. term_palette_override(term, OSC4_COLOUR_bg_bold, rgb);
  1101. rgb = rgb_from_colorref(GetSysColor(COLOR_HIGHLIGHTTEXT));
  1102. term_palette_override(term, OSC4_COLOUR_cursor_fg, rgb);
  1103. rgb = rgb_from_colorref(GetSysColor(COLOR_HIGHLIGHT));
  1104. term_palette_override(term, OSC4_COLOUR_cursor_bg, rgb);
  1105. }
  1106. }
  1107. /*
  1108. * This is a wrapper to ExtTextOut() to force Windows to display
  1109. * the precise glyphs we give it. Otherwise it would do its own
  1110. * bidi and Arabic shaping, and we would end up uncertain which
  1111. * characters it had put where.
  1112. */
  1113. static void exact_textout(HDC hdc, int x, int y, CONST RECT *lprc,
  1114. unsigned short *lpString, UINT cbCount,
  1115. CONST INT *lpDx, bool opaque)
  1116. {
  1117. #if HAVE_GCP_RESULTSW
  1118. GCP_RESULTSW gcpr;
  1119. #else
  1120. /*
  1121. * If building against old enough headers that the GCP_RESULTSW
  1122. * type isn't available, we can make do with GCP_RESULTS proper:
  1123. * the differences aren't important to us (the only variable-width
  1124. * string parameter is one we don't use anyway).
  1125. */
  1126. GCP_RESULTS gcpr;
  1127. #endif
  1128. char *buffer = snewn(cbCount*2+2, char);
  1129. char *classbuffer = snewn(cbCount, char);
  1130. memset(&gcpr, 0, sizeof(gcpr));
  1131. memset(buffer, 0, cbCount*2+2);
  1132. memset(classbuffer, GCPCLASS_NEUTRAL, cbCount);
  1133. gcpr.lStructSize = sizeof(gcpr);
  1134. gcpr.lpGlyphs = (void *)buffer;
  1135. gcpr.lpClass = (void *)classbuffer;
  1136. gcpr.nGlyphs = cbCount;
  1137. GetCharacterPlacementW(hdc, lpString, cbCount, 0, &gcpr,
  1138. FLI_MASK | GCP_CLASSIN | GCP_DIACRITIC);
  1139. ExtTextOut(hdc, x, y,
  1140. ETO_GLYPH_INDEX | ETO_CLIPPED | (opaque ? ETO_OPAQUE : 0),
  1141. lprc, buffer, cbCount, lpDx);
  1142. }
  1143. /*
  1144. * The exact_textout() wrapper, unfortunately, destroys the useful
  1145. * Windows `font linking' behaviour: automatic handling of Unicode
  1146. * code points not supported in this font by falling back to a font
  1147. * which does contain them. Therefore, we adopt a multi-layered
  1148. * approach: for any potentially-bidi text, we use exact_textout(),
  1149. * and for everything else we use a simple ExtTextOut as we did
  1150. * before exact_textout() was introduced.
  1151. */
  1152. static void general_textout(
  1153. WinGuiSeat *wgs, HDC hdc, int x, int y, CONST RECT *lprc,
  1154. unsigned short *lpString, UINT cbCount, CONST INT *lpDx, bool opaque)
  1155. {
  1156. int i, j, xp, xn;
  1157. int bkmode = 0;
  1158. bool got_bkmode = false;
  1159. xp = xn = x;
  1160. for (i = 0; i < (int)cbCount ;) {
  1161. bool rtl = is_rtl(lpString[i]);
  1162. xn += lpDx[i];
  1163. for (j = i+1; j < (int)cbCount; j++) {
  1164. if (rtl != is_rtl(lpString[j]))
  1165. break;
  1166. xn += lpDx[j];
  1167. }
  1168. /*
  1169. * Now [i,j) indicates a maximal substring of lpString
  1170. * which should be displayed using the same textout
  1171. * function.
  1172. */
  1173. if (rtl) {
  1174. exact_textout(hdc, xp, y, lprc, lpString+i, j-i,
  1175. wgs->font_varpitch ? NULL : lpDx+i, opaque);
  1176. } else {
  1177. ExtTextOutW(hdc, xp, y, ETO_CLIPPED | (opaque ? ETO_OPAQUE : 0),
  1178. lprc, lpString+i, j-i,
  1179. wgs->font_varpitch ? NULL : lpDx+i);
  1180. }
  1181. i = j;
  1182. xp = xn;
  1183. bkmode = GetBkMode(hdc);
  1184. got_bkmode = true;
  1185. SetBkMode(hdc, TRANSPARENT);
  1186. opaque = false;
  1187. }
  1188. if (got_bkmode)
  1189. SetBkMode(hdc, bkmode);
  1190. }
  1191. static int get_font_width(WinGuiSeat *wgs, HDC hdc, const TEXTMETRIC *tm)
  1192. {
  1193. int ret;
  1194. /* Note that the TMPF_FIXED_PITCH bit is defined upside down :-( */
  1195. if (!(tm->tmPitchAndFamily & TMPF_FIXED_PITCH)) {
  1196. ret = tm->tmAveCharWidth;
  1197. } else {
  1198. #define FIRST '0'
  1199. #define LAST '9'
  1200. ABCFLOAT widths[LAST-FIRST + 1];
  1201. int j;
  1202. wgs->font_varpitch = true;
  1203. wgs->font_dualwidth = true;
  1204. if (GetCharABCWidthsFloat(hdc, FIRST, LAST, widths)) {
  1205. ret = 0;
  1206. for (j = 0; j < lenof(widths); j++) {
  1207. int width = (int)(0.5 + widths[j].abcfA +
  1208. widths[j].abcfB + widths[j].abcfC);
  1209. if (ret < width)
  1210. ret = width;
  1211. }
  1212. } else {
  1213. ret = tm->tmMaxCharWidth;
  1214. }
  1215. #undef FIRST
  1216. #undef LAST
  1217. }
  1218. return ret;
  1219. }
  1220. static void init_dpi_info(WinGuiSeat *wgs)
  1221. {
  1222. if (wgs->dpi_info.cur_dpi.x == 0 || wgs->dpi_info.cur_dpi.y == 0) {
  1223. if (p_GetDpiForMonitor && p_MonitorFromWindow) {
  1224. UINT dpiX, dpiY;
  1225. HMONITOR currentMonitor = p_MonitorFromWindow(
  1226. wgs->term_hwnd, MONITOR_DEFAULTTOPRIMARY);
  1227. if (p_GetDpiForMonitor(currentMonitor, MDT_EFFECTIVE_DPI,
  1228. &dpiX, &dpiY) == S_OK) {
  1229. wgs->dpi_info.cur_dpi.x = (int)dpiX;
  1230. wgs->dpi_info.cur_dpi.y = (int)dpiY;
  1231. }
  1232. }
  1233. /* Fall back to system DPI */
  1234. if (wgs->dpi_info.cur_dpi.x == 0 || wgs->dpi_info.cur_dpi.y == 0) {
  1235. HDC hdc = GetDC(wgs->term_hwnd);
  1236. wgs->dpi_info.cur_dpi.x = GetDeviceCaps(hdc, LOGPIXELSX);
  1237. wgs->dpi_info.cur_dpi.y = GetDeviceCaps(hdc, LOGPIXELSY);
  1238. ReleaseDC(wgs->term_hwnd, hdc);
  1239. }
  1240. }
  1241. }
  1242. /*
  1243. * Initialise all the fonts we will need initially. There may be as many as
  1244. * three or as few as one. The other (potentially) twenty-one fonts are done
  1245. * if/when they are needed.
  1246. *
  1247. * We also:
  1248. *
  1249. * - check the font width and height, correcting our guesses if
  1250. * necessary.
  1251. *
  1252. * - verify that the bold font is the same width as the ordinary
  1253. * one, and engage shadow bolding if not.
  1254. *
  1255. * - verify that the underlined font is the same width as the
  1256. * ordinary one (manual underlining by means of line drawing can
  1257. * be done in a pinch).
  1258. *
  1259. * - find a trust sigil icon that will look OK with the chosen font.
  1260. */
  1261. static void init_fonts(WinGuiSeat *wgs, int pick_width, int pick_height)
  1262. {
  1263. TEXTMETRIC tm;
  1264. OUTLINETEXTMETRIC otm;
  1265. CPINFO cpinfo;
  1266. FontSpec *font;
  1267. int fontsize[3];
  1268. int i;
  1269. int quality;
  1270. HDC hdc;
  1271. int fw_dontcare, fw_bold;
  1272. for (i = 0; i < FONT_MAXNO; i++)
  1273. wgs->fonts[i] = NULL;
  1274. wgs->bold_font_mode =
  1275. conf_get_int(wgs->conf, CONF_bold_style) & BOLD_STYLE_FONT ?
  1276. BOLD_FONT : BOLD_NONE;
  1277. wgs->bold_colours =
  1278. conf_get_int(wgs->conf, CONF_bold_style) & BOLD_STYLE_COLOUR ?
  1279. true : false;
  1280. wgs->und_mode = UND_FONT;
  1281. font = conf_get_fontspec(wgs->conf, CONF_font);
  1282. if (font->isbold) {
  1283. fw_dontcare = FW_BOLD;
  1284. fw_bold = FW_HEAVY;
  1285. } else {
  1286. fw_dontcare = FW_DONTCARE;
  1287. fw_bold = FW_BOLD;
  1288. }
  1289. hdc = GetDC(wgs->term_hwnd);
  1290. if (pick_height)
  1291. wgs->font_height = pick_height;
  1292. else {
  1293. wgs->font_height = font->height;
  1294. if (wgs->font_height > 0) {
  1295. wgs->font_height = -MulDiv(
  1296. wgs->font_height, wgs->dpi_info.cur_dpi.y, 72);
  1297. }
  1298. }
  1299. wgs->font_width = pick_width;
  1300. quality = conf_get_int(wgs->conf, CONF_font_quality);
  1301. #define f(i,c,w,u) \
  1302. wgs->fonts[i] = CreateFont( \
  1303. wgs->font_height, wgs->font_width, 0, 0, w, false, u, false, c, \
  1304. OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, FONT_QUALITY(quality), \
  1305. FIXED_PITCH | FF_DONTCARE, font->name)
  1306. f(FONT_NORMAL, font->charset, fw_dontcare, false);
  1307. SelectObject(hdc, wgs->fonts[FONT_NORMAL]);
  1308. GetTextMetrics(hdc, &tm);
  1309. if (GetOutlineTextMetrics(hdc, sizeof(otm), &otm))
  1310. wgs->font_strikethrough_y = tm.tmAscent - otm.otmsStrikeoutPosition;
  1311. else
  1312. wgs->font_strikethrough_y = tm.tmAscent - (tm.tmAscent * 3 / 8);
  1313. GetObject(wgs->fonts[FONT_NORMAL], sizeof(LOGFONT), &wgs->lfont);
  1314. /* Note that the TMPF_FIXED_PITCH bit is defined upside down :-( */
  1315. if (!(tm.tmPitchAndFamily & TMPF_FIXED_PITCH)) {
  1316. wgs->font_varpitch = false;
  1317. wgs->font_dualwidth = (tm.tmAveCharWidth != tm.tmMaxCharWidth);
  1318. } else {
  1319. wgs->font_varpitch = true;
  1320. wgs->font_dualwidth = true;
  1321. }
  1322. if (pick_width == 0 || pick_height == 0) {
  1323. wgs->font_height = tm.tmHeight;
  1324. wgs->font_width = get_font_width(wgs, hdc, &tm);
  1325. }
  1326. #ifdef RDB_DEBUG_PATCH
  1327. debug("Primary font H=%d, AW=%d, MW=%d\n",
  1328. tm.tmHeight, tm.tmAveCharWidth, tm.tmMaxCharWidth);
  1329. #endif
  1330. {
  1331. CHARSETINFO info;
  1332. DWORD cset = tm.tmCharSet;
  1333. memset(&info, 0xFF, sizeof(info));
  1334. /* !!! Yes the next line is right */
  1335. if (cset == OEM_CHARSET)
  1336. wgs->ucsdata.font_codepage = GetOEMCP();
  1337. else if (TranslateCharsetInfo ((DWORD *)(ULONG_PTR)cset,
  1338. &info, TCI_SRCCHARSET))
  1339. wgs->ucsdata.font_codepage = info.ciACP;
  1340. else
  1341. wgs->ucsdata.font_codepage = -1;
  1342. GetCPInfo(wgs->ucsdata.font_codepage, &cpinfo);
  1343. wgs->ucsdata.dbcs_screenfont = (cpinfo.MaxCharSize > 1);
  1344. }
  1345. f(FONT_UNDERLINE, font->charset, fw_dontcare, true);
  1346. /*
  1347. * Some fonts, e.g. 9-pt Courier, draw their underlines
  1348. * outside their character cell. We successfully prevent
  1349. * screen corruption by clipping the text output, but then
  1350. * we lose the underline completely. Here we try to work
  1351. * out whether this is such a font, and if it is, we set a
  1352. * flag that causes underlines to be drawn by hand.
  1353. *
  1354. * Having tried other more sophisticated approaches (such
  1355. * as examining the TEXTMETRIC structure or requesting the
  1356. * height of a string), I think we'll do this the brute
  1357. * force way: we create a small bitmap, draw an underlined
  1358. * space on it, and test to see whether any pixels are
  1359. * foreground-coloured. (Since we expect the underline to
  1360. * go all the way across the character cell, we only search
  1361. * down a single column of the bitmap, half way across.)
  1362. */
  1363. {
  1364. HDC und_dc;
  1365. HBITMAP und_bm, und_oldbm;
  1366. int i;
  1367. bool gotit;
  1368. COLORREF c;
  1369. und_dc = CreateCompatibleDC(hdc);
  1370. und_bm = CreateCompatibleBitmap(
  1371. hdc, wgs->font_width, wgs->font_height);
  1372. und_oldbm = SelectObject(und_dc, und_bm);
  1373. SelectObject(und_dc, wgs->fonts[FONT_UNDERLINE]);
  1374. SetTextAlign(und_dc, TA_TOP | TA_LEFT | TA_NOUPDATECP);
  1375. SetTextColor(und_dc, RGB(255, 255, 255));
  1376. SetBkColor(und_dc, RGB(0, 0, 0));
  1377. SetBkMode(und_dc, OPAQUE);
  1378. ExtTextOut(und_dc, 0, 0, ETO_OPAQUE, NULL, " ", 1, NULL);
  1379. gotit = false;
  1380. for (i = 0; i < wgs->font_height; i++) {
  1381. c = GetPixel(und_dc, wgs->font_width / 2, i);
  1382. if (c != RGB(0, 0, 0))
  1383. gotit = true;
  1384. }
  1385. SelectObject(und_dc, und_oldbm);
  1386. DeleteObject(und_bm);
  1387. DeleteDC(und_dc);
  1388. if (!gotit) {
  1389. wgs->und_mode = UND_LINE;
  1390. DeleteObject(wgs->fonts[FONT_UNDERLINE]);
  1391. wgs->fonts[FONT_UNDERLINE] = 0;
  1392. }
  1393. }
  1394. if (wgs->bold_font_mode == BOLD_FONT) {
  1395. f(FONT_BOLD, font->charset, fw_bold, false);
  1396. }
  1397. #undef f
  1398. wgs->descent = tm.tmAscent + 1;
  1399. if (wgs->descent >= wgs->font_height)
  1400. wgs->descent = wgs->font_height - 1;
  1401. for (i = 0; i < 3; i++) {
  1402. if (wgs->fonts[i]) {
  1403. if (SelectObject(hdc, wgs->fonts[i]) && GetTextMetrics(hdc, &tm))
  1404. fontsize[i] = (get_font_width(wgs, hdc, &tm) +
  1405. 256 * tm.tmHeight);
  1406. else
  1407. fontsize[i] = -i;
  1408. } else
  1409. fontsize[i] = -i;
  1410. }
  1411. ReleaseDC(wgs->term_hwnd, hdc);
  1412. if (trust_icon != INVALID_HANDLE_VALUE) {
  1413. DestroyIcon(trust_icon);
  1414. }
  1415. trust_icon = LoadImage(hinst, MAKEINTRESOURCE(IDI_MAINICON),
  1416. IMAGE_ICON, wgs->font_width*2, wgs->font_height,
  1417. LR_DEFAULTCOLOR);
  1418. if (fontsize[FONT_UNDERLINE] != fontsize[FONT_NORMAL]) {
  1419. wgs->und_mode = UND_LINE;
  1420. DeleteObject(wgs->fonts[FONT_UNDERLINE]);
  1421. wgs->fonts[FONT_UNDERLINE] = 0;
  1422. }
  1423. if (wgs->bold_font_mode == BOLD_FONT &&
  1424. fontsize[FONT_BOLD] != fontsize[FONT_NORMAL]) {
  1425. wgs->bold_font_mode = BOLD_SHADOW;
  1426. DeleteObject(wgs->fonts[FONT_BOLD]);
  1427. wgs->fonts[FONT_BOLD] = 0;
  1428. }
  1429. wgs->fontflag[0] = true;
  1430. wgs->fontflag[1] = true;
  1431. wgs->fontflag[2] = true;
  1432. init_ucs(wgs->conf, &wgs->ucsdata);
  1433. }
  1434. static void another_font(WinGuiSeat *wgs, int fontno)
  1435. {
  1436. int basefont;
  1437. int fw_dontcare, fw_bold, quality;
  1438. int c, w, x;
  1439. bool u;
  1440. char *s;
  1441. FontSpec *font;
  1442. if (fontno < 0 || fontno >= FONT_MAXNO || wgs->fontflag[fontno])
  1443. return;
  1444. basefont = (fontno & ~(FONT_BOLDUND));
  1445. if (basefont != fontno && !wgs->fontflag[basefont])
  1446. another_font(wgs, basefont);
  1447. font = conf_get_fontspec(wgs->conf, CONF_font);
  1448. if (font->isbold) {
  1449. fw_dontcare = FW_BOLD;
  1450. fw_bold = FW_HEAVY;
  1451. } else {
  1452. fw_dontcare = FW_DONTCARE;
  1453. fw_bold = FW_BOLD;
  1454. }
  1455. c = font->charset;
  1456. w = fw_dontcare;
  1457. u = false;
  1458. s = font->name;
  1459. x = wgs->font_width;
  1460. if (fontno & FONT_WIDE)
  1461. x *= 2;
  1462. if (fontno & FONT_NARROW)
  1463. x = (x+1)/2;
  1464. if (fontno & FONT_OEM)
  1465. c = OEM_CHARSET;
  1466. if (fontno & FONT_BOLD)
  1467. w = fw_bold;
  1468. if (fontno & FONT_UNDERLINE)
  1469. u = true;
  1470. quality = conf_get_int(wgs->conf, CONF_font_quality);
  1471. wgs->fonts[fontno] =
  1472. CreateFont(wgs->font_height * (1 + !!(fontno & FONT_HIGH)), x, 0, 0, w,
  1473. false, u, false, c, OUT_DEFAULT_PRECIS,
  1474. CLIP_DEFAULT_PRECIS, FONT_QUALITY(quality),
  1475. DEFAULT_PITCH | FF_DONTCARE, s);
  1476. wgs->fontflag[fontno] = true;
  1477. }
  1478. static void deinit_fonts(WinGuiSeat *wgs)
  1479. {
  1480. int i;
  1481. for (i = 0; i < FONT_MAXNO; i++) {
  1482. if (wgs->fonts[i])
  1483. DeleteObject(wgs->fonts[i]);
  1484. wgs->fonts[i] = 0;
  1485. wgs->fontflag[i] = false;
  1486. }
  1487. if (trust_icon != INVALID_HANDLE_VALUE) {
  1488. DestroyIcon(trust_icon);
  1489. }
  1490. trust_icon = INVALID_HANDLE_VALUE;
  1491. }
  1492. static void wintw_request_resize(TermWin *tw, int w, int h)
  1493. {
  1494. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  1495. const struct BackendVtable *vt;
  1496. int width, height;
  1497. int resize_action = conf_get_int(wgs->conf, CONF_resize_action);
  1498. bool deny_resize = false;
  1499. /* Suppress server-originated resizing attempts if local resizing
  1500. * is disabled entirely, or if it's supposed to change
  1501. * rows/columns but the window is maximised. */
  1502. if (resize_action == RESIZE_DISABLED
  1503. || (resize_action == RESIZE_TERM && IsZoomed(wgs->term_hwnd))) {
  1504. deny_resize = true;
  1505. }
  1506. vt = backend_vt_from_proto(be_default_protocol);
  1507. if (vt && vt->flags & BACKEND_RESIZE_FORBIDDEN)
  1508. deny_resize = true;
  1509. if (h == wgs->term->rows && w == wgs->term->cols) deny_resize = true;
  1510. /* We still need to acknowledge a suppressed resize attempt. */
  1511. if (deny_resize) {
  1512. term_resize_request_completed(wgs->term);
  1513. return;
  1514. }
  1515. /* Sanity checks ... */
  1516. {
  1517. RECT ss;
  1518. if (get_fullscreen_rect(wgs, &ss)) {
  1519. /* Make sure the values aren't too big */
  1520. width = (ss.right - ss.left - wgs->extra_width) / 4;
  1521. height = (ss.bottom - ss.top - wgs->extra_height) / 6;
  1522. if (w > width || h > height) {
  1523. term_resize_request_completed(wgs->term);
  1524. return;
  1525. }
  1526. if (w < 15)
  1527. w = 15;
  1528. if (h < 1)
  1529. h = 1;
  1530. }
  1531. }
  1532. if (resize_action != RESIZE_FONT && !IsZoomed(wgs->term_hwnd)) {
  1533. width = wgs->extra_width + wgs->font_width * w;
  1534. height = wgs->extra_height + wgs->font_height * h;
  1535. SetWindowPos(wgs->term_hwnd, NULL, 0, 0, width, height,
  1536. SWP_NOACTIVATE | SWP_NOCOPYBITS |
  1537. SWP_NOMOVE | SWP_NOZORDER);
  1538. } else {
  1539. /*
  1540. * If we're resizing by changing the font, we must tell the
  1541. * terminal the new size immediately, so that reset_window
  1542. * will know what to do.
  1543. */
  1544. term_size(wgs->term, h, w, conf_get_int(wgs->conf, CONF_savelines));
  1545. reset_window(wgs, 0);
  1546. }
  1547. term_resize_request_completed(wgs->term);
  1548. InvalidateRect(wgs->term_hwnd, NULL, true);
  1549. }
  1550. static void recompute_window_offset(WinGuiSeat *wgs)
  1551. {
  1552. RECT cr;
  1553. GetClientRect(wgs->term_hwnd, &cr);
  1554. int win_width = cr.right - cr.left;
  1555. int win_height = cr.bottom - cr.top;
  1556. int new_offset_width = (win_width-wgs->font_width*wgs->term->cols)/2;
  1557. int new_offset_height = (win_height-wgs->font_height*wgs->term->rows)/2;
  1558. if (wgs->offset_width != new_offset_width ||
  1559. wgs->offset_height != new_offset_height) {
  1560. wgs->offset_width = new_offset_width;
  1561. wgs->offset_height = new_offset_height;
  1562. InvalidateRect(wgs->term_hwnd, NULL, true);
  1563. }
  1564. }
  1565. static void reset_window(WinGuiSeat *wgs, int reinit)
  1566. {
  1567. /*
  1568. * This function decides how to resize or redraw when the
  1569. * user changes something.
  1570. *
  1571. * This function doesn't like to change the terminal size but if the
  1572. * font size is locked that may be it's only soluion.
  1573. */
  1574. int win_width, win_height, resize_action, window_border;
  1575. RECT cr, wr;
  1576. #ifdef RDB_DEBUG_PATCH
  1577. debug("reset_window()\n");
  1578. #endif
  1579. /* Current window sizes ... */
  1580. GetWindowRect(wgs->term_hwnd, &wr);
  1581. GetClientRect(wgs->term_hwnd, &cr);
  1582. win_width = cr.right - cr.left;
  1583. win_height = cr.bottom - cr.top;
  1584. resize_action = conf_get_int(wgs->conf, CONF_resize_action);
  1585. window_border = conf_get_int(wgs->conf, CONF_window_border);
  1586. if (resize_action == RESIZE_DISABLED)
  1587. reinit = 2;
  1588. /* Are we being forced to reload the fonts ? */
  1589. if (reinit>1) {
  1590. #ifdef RDB_DEBUG_PATCH
  1591. debug("reset_window() -- Forced deinit\n");
  1592. #endif
  1593. deinit_fonts(wgs);
  1594. init_fonts(wgs, 0, 0);
  1595. }
  1596. /* Oh, looks like we're minimised */
  1597. if (win_width == 0 || win_height == 0)
  1598. return;
  1599. /* Is the window out of position ? */
  1600. if (!reinit) {
  1601. recompute_window_offset(wgs);
  1602. #ifdef RDB_DEBUG_PATCH
  1603. debug("reset_window() -> Reposition terminal\n");
  1604. #endif
  1605. }
  1606. if (IsZoomed(wgs->term_hwnd)) {
  1607. /* We're fullscreen, this means we must not change the size of
  1608. * the window so it's the font size or the terminal itself.
  1609. */
  1610. wgs->extra_width = wr.right - wr.left - cr.right + cr.left;
  1611. wgs->extra_height = wr.bottom - wr.top - cr.bottom + cr.top;
  1612. if (resize_action != RESIZE_TERM) {
  1613. if (wgs->font_width != win_width/wgs->term->cols ||
  1614. wgs->font_height != win_height/wgs->term->rows) {
  1615. int fw = (win_width - 2*window_border) / wgs->term->cols;
  1616. int fh = (win_height - 2*window_border) / wgs->term->rows;
  1617. /* In case that subtraction made the font size go
  1618. * negative in an edge case, bound it below by 1 */
  1619. if (fw < 1) fw = 1;
  1620. if (fh < 1) fh = 1;
  1621. deinit_fonts(wgs);
  1622. init_fonts(wgs, fw, fh);
  1623. wgs->offset_width =
  1624. (win_width - wgs->font_width*wgs->term->cols) / 2;
  1625. wgs->offset_height =
  1626. (win_height - wgs->font_height*wgs->term->rows) / 2;
  1627. InvalidateRect(wgs->term_hwnd, NULL, true);
  1628. #ifdef RDB_DEBUG_PATCH
  1629. debug("reset_window() -> Z font resize to (%d, %d)\n",
  1630. wgs->font_width, wgs->font_height);
  1631. #endif
  1632. }
  1633. } else {
  1634. if (wgs->font_width * wgs->term->cols != win_width ||
  1635. wgs->font_height * wgs->term->rows != win_height) {
  1636. /* Our only choice at this point is to change the
  1637. * size of the terminal; Oh well.
  1638. */
  1639. term_size(wgs->term,
  1640. (win_height - 2*window_border) / wgs->font_height,
  1641. (win_width - 2*window_border) / wgs->font_width,
  1642. conf_get_int(wgs->conf, CONF_savelines));
  1643. wgs->offset_width =
  1644. (win_width - window_border - wgs->font_width*wgs->term->cols) / 2;
  1645. wgs->offset_height =
  1646. (win_height - window_border - wgs->font_height*wgs->term->rows) / 2;
  1647. InvalidateRect(wgs->term_hwnd, NULL, true);
  1648. #ifdef RDB_DEBUG_PATCH
  1649. debug("reset_window() -> Zoomed term_size\n");
  1650. #endif
  1651. }
  1652. }
  1653. return;
  1654. }
  1655. /* Resize window after DPI change */
  1656. if (reinit == 3 && p_GetSystemMetricsForDpi && p_AdjustWindowRectExForDpi) {
  1657. RECT rect;
  1658. rect.left = rect.top = 0;
  1659. rect.right = (wgs->font_width * wgs->term->cols);
  1660. if (conf_get_bool(wgs->conf, CONF_scrollbar))
  1661. rect.right += p_GetSystemMetricsForDpi(SM_CXVSCROLL,
  1662. wgs->dpi_info.cur_dpi.x);
  1663. rect.bottom = (wgs->font_height * wgs->term->rows);
  1664. p_AdjustWindowRectExForDpi(
  1665. &rect, GetWindowLongPtr(wgs->term_hwnd, GWL_STYLE),
  1666. FALSE, GetWindowLongPtr(wgs->term_hwnd, GWL_EXSTYLE),
  1667. wgs->dpi_info.cur_dpi.x);
  1668. rect.right += (window_border * 2);
  1669. rect.bottom += (window_border * 2);
  1670. OffsetRect(&wgs->dpi_info.new_wnd_rect,
  1671. ((wgs->dpi_info.new_wnd_rect.right -
  1672. wgs->dpi_info.new_wnd_rect.left) -
  1673. (rect.right - rect.left)) / 2,
  1674. ((wgs->dpi_info.new_wnd_rect.bottom -
  1675. wgs->dpi_info.new_wnd_rect.top) -
  1676. (rect.bottom - rect.top)) / 2);
  1677. SetWindowPos(wgs->term_hwnd, NULL,
  1678. wgs->dpi_info.new_wnd_rect.left,
  1679. wgs->dpi_info.new_wnd_rect.top,
  1680. rect.right - rect.left, rect.bottom - rect.top,
  1681. SWP_NOZORDER);
  1682. InvalidateRect(wgs->term_hwnd, NULL, true);
  1683. return;
  1684. }
  1685. /* Hmm, a force re-init means we should ignore the current window
  1686. * so we resize to the default font size.
  1687. */
  1688. if (reinit>0) {
  1689. #ifdef RDB_DEBUG_PATCH
  1690. debug("reset_window() -> Forced re-init\n");
  1691. #endif
  1692. wgs->offset_width = wgs->offset_height = window_border;
  1693. wgs->extra_width =
  1694. wr.right - wr.left - cr.right + cr.left + wgs->offset_width*2;
  1695. wgs->extra_height =
  1696. wr.bottom - wr.top - cr.bottom + cr.top + wgs->offset_height*2;
  1697. if (win_width != (wgs->font_width*wgs->term->cols +
  1698. wgs->offset_width*2) ||
  1699. win_height != (wgs->font_height*wgs->term->rows +
  1700. wgs->offset_height*2)) {
  1701. /* If this is too large windows will resize it to the maximum
  1702. * allowed window size, we will then be back in here and resize
  1703. * the font or terminal to fit.
  1704. */
  1705. SetWindowPos(wgs->term_hwnd, NULL, 0, 0,
  1706. wgs->font_width*wgs->term->cols + wgs->extra_width,
  1707. wgs->font_height*wgs->term->rows + wgs->extra_height,
  1708. SWP_NOMOVE | SWP_NOZORDER);
  1709. }
  1710. InvalidateRect(wgs->term_hwnd, NULL, true);
  1711. return;
  1712. }
  1713. /* Okay the user doesn't want us to change the font so we try the
  1714. * window. But that may be too big for the screen which forces us
  1715. * to change the terminal.
  1716. */
  1717. if ((resize_action == RESIZE_TERM && reinit<=0) ||
  1718. (resize_action == RESIZE_EITHER && reinit<0) ||
  1719. reinit>0) {
  1720. wgs->offset_width = wgs->offset_height = window_border;
  1721. wgs->extra_width =
  1722. wr.right - wr.left - cr.right + cr.left + wgs->offset_width*2;
  1723. wgs->extra_height =
  1724. wr.bottom - wr.top - cr.bottom + cr.top + wgs->offset_height*2;
  1725. if (win_width != (wgs->font_width*wgs->term->cols +
  1726. wgs->offset_width*2) ||
  1727. win_height != (wgs->font_height*wgs->term->rows +
  1728. wgs->offset_height*2)) {
  1729. RECT ss;
  1730. int width, height;
  1731. get_fullscreen_rect(wgs, &ss);
  1732. width = (ss.right - ss.left - wgs->extra_width) / wgs->font_width;
  1733. height = (ss.bottom - ss.top - wgs->extra_height)/wgs->font_height;
  1734. /* Grrr too big */
  1735. if ( wgs->term->rows > height || wgs->term->cols > width ) {
  1736. if (resize_action == RESIZE_EITHER) {
  1737. /* Make the font the biggest we can */
  1738. if (wgs->term->cols > width)
  1739. wgs->font_width =
  1740. (ss.right - ss.left - wgs->extra_width) /
  1741. wgs->term->cols;
  1742. if (wgs->term->rows > height)
  1743. wgs->font_height =
  1744. (ss.bottom - ss.top - wgs->extra_height) /
  1745. wgs->term->rows;
  1746. deinit_fonts(wgs);
  1747. init_fonts(wgs, wgs->font_width, wgs->font_height);
  1748. width = (ss.right - ss.left - wgs->extra_width) /
  1749. wgs->font_width;
  1750. height = (ss.bottom - ss.top - wgs->extra_height) /
  1751. wgs->font_height;
  1752. } else {
  1753. if ( height > wgs->term->rows ) height = wgs->term->rows;
  1754. if ( width > wgs->term->cols ) width = wgs->term->cols;
  1755. term_size(wgs->term, height, width,
  1756. conf_get_int(wgs->conf, CONF_savelines));
  1757. #ifdef RDB_DEBUG_PATCH
  1758. debug("reset_window() -> term resize to (%d,%d)\n",
  1759. height, width);
  1760. #endif
  1761. }
  1762. }
  1763. SetWindowPos(wgs->term_hwnd, NULL, 0, 0,
  1764. wgs->font_width*wgs->term->cols + wgs->extra_width,
  1765. wgs->font_height*wgs->term->rows + wgs->extra_height,
  1766. SWP_NOMOVE | SWP_NOZORDER);
  1767. InvalidateRect(wgs->term_hwnd, NULL, true);
  1768. #ifdef RDB_DEBUG_PATCH
  1769. debug("reset_window() -> window resize to (%d,%d)\n",
  1770. wgs->font_width*term->cols + wgs->extra_width,
  1771. wgs->font_height*term->rows + wgs->extra_height);
  1772. #endif
  1773. }
  1774. return;
  1775. }
  1776. /* We're allowed to or must change the font but do we want to ? */
  1777. if (wgs->font_width != (win_width-window_border*2)/wgs->term->cols ||
  1778. wgs->font_height != (win_height-window_border*2)/wgs->term->rows) {
  1779. deinit_fonts(wgs);
  1780. init_fonts(wgs, (win_width-window_border*2)/wgs->term->cols,
  1781. (win_height-window_border*2)/wgs->term->rows);
  1782. wgs->offset_width = (win_width-wgs->font_width*wgs->term->cols)/2;
  1783. wgs->offset_height = (win_height-wgs->font_height*wgs->term->rows)/2;
  1784. wgs->extra_width =
  1785. wr.right - wr.left - cr.right + cr.left + wgs->offset_width*2;
  1786. wgs->extra_height =
  1787. wr.bottom - wr.top - cr.bottom + cr.top + wgs->offset_height*2;
  1788. InvalidateRect(wgs->term_hwnd, NULL, true);
  1789. #ifdef RDB_DEBUG_PATCH
  1790. debug("reset_window() -> font resize to (%d,%d)\n",
  1791. wgs->font_width, wgs->font_height);
  1792. #endif
  1793. }
  1794. }
  1795. static void set_input_locale(WinGuiSeat *wgs, HKL kl)
  1796. {
  1797. char lbuf[20];
  1798. GetLocaleInfo(LOWORD(kl), LOCALE_IDEFAULTANSICODEPAGE,
  1799. lbuf, sizeof(lbuf));
  1800. wgs->kbd_codepage = atoi(lbuf);
  1801. }
  1802. static void click(WinGuiSeat *wgs, Mouse_Button b, int x, int y,
  1803. bool shift, bool ctrl, bool alt)
  1804. {
  1805. int thistime = GetMessageTime();
  1806. if (wgs->send_raw_mouse &&
  1807. !(shift && conf_get_bool(wgs->conf, CONF_mouse_override))) {
  1808. wgs->lastbtn = MBT_NOTHING;
  1809. term_mouse(wgs->term, b, translate_button(wgs, b), MA_CLICK,
  1810. x, y, shift, ctrl, alt);
  1811. return;
  1812. }
  1813. if (wgs->lastbtn == b && thistime - wgs->lasttime < wgs->dbltime) {
  1814. wgs->lastact = (wgs->lastact == MA_CLICK ? MA_2CLK :
  1815. wgs->lastact == MA_2CLK ? MA_3CLK :
  1816. wgs->lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
  1817. } else {
  1818. wgs->lastbtn = b;
  1819. wgs->lastact = MA_CLICK;
  1820. }
  1821. if (wgs->lastact != MA_NOTHING)
  1822. term_mouse(wgs->term, b, translate_button(wgs, b), wgs->lastact,
  1823. x, y, shift, ctrl, alt);
  1824. wgs->lasttime = thistime;
  1825. }
  1826. /*
  1827. * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
  1828. * into a cooked one (SELECT, EXTEND, PASTE).
  1829. */
  1830. static Mouse_Button translate_button(WinGuiSeat *wgs, Mouse_Button button)
  1831. {
  1832. if (button == MBT_LEFT)
  1833. return MBT_SELECT;
  1834. if (button == MBT_MIDDLE)
  1835. return conf_get_int(wgs->conf, CONF_mouse_is_xterm) == MOUSE_XTERM ?
  1836. MBT_PASTE : MBT_EXTEND;
  1837. if (button == MBT_RIGHT)
  1838. return conf_get_int(wgs->conf, CONF_mouse_is_xterm) == MOUSE_XTERM ?
  1839. MBT_EXTEND : MBT_PASTE;
  1840. return 0; /* shouldn't happen */
  1841. }
  1842. static void show_mouseptr(WinGuiSeat *wgs, bool show)
  1843. {
  1844. /* NB that the counter in ShowCursor() is also frobbed by
  1845. * update_mouse_pointer() */
  1846. static bool cursor_visible = true;
  1847. if (wgs) {
  1848. if (!conf_get_bool(wgs->conf, CONF_hide_mouseptr))
  1849. show = true; /* hiding mouse pointer disabled in Conf */
  1850. } else {
  1851. /*
  1852. * You can pass wgs==NULL if you want to _show_ the pointer
  1853. * rather than hiding it, because that's never disallowed.
  1854. */
  1855. assert(show);
  1856. }
  1857. if (cursor_visible && !show)
  1858. ShowCursor(false);
  1859. else if (!cursor_visible && show)
  1860. ShowCursor(true);
  1861. cursor_visible = show;
  1862. }
  1863. static bool is_alt_pressed(void)
  1864. {
  1865. BYTE keystate[256];
  1866. int r = GetKeyboardState(keystate);
  1867. if (!r)
  1868. return false;
  1869. if (keystate[VK_MENU] & 0x80)
  1870. return true;
  1871. if (keystate[VK_RMENU] & 0x80)
  1872. return true;
  1873. return false;
  1874. }
  1875. static void exit_callback(void *vctx)
  1876. {
  1877. WinGuiSeat *wgs = (WinGuiSeat *)vctx;
  1878. int exitcode, close_on_exit;
  1879. if (!wgs->session_closed &&
  1880. (exitcode = backend_exitcode(wgs->backend)) >= 0) {
  1881. close_on_exit = conf_get_int(wgs->conf, CONF_close_on_exit);
  1882. /* Abnormal exits will already have set session_closed and taken
  1883. * appropriate action. */
  1884. if (close_on_exit == FORCE_ON ||
  1885. (close_on_exit == AUTO && exitcode != INT_MAX)) {
  1886. PostQuitMessage(0);
  1887. } else {
  1888. queue_toplevel_callback(close_session, wgs);
  1889. wgs->session_closed = true;
  1890. /* exitcode == INT_MAX indicates that the connection was closed
  1891. * by a fatal error, so an error box will be coming our way and
  1892. * we should not generate this informational one. */
  1893. if (exitcode != INT_MAX) {
  1894. show_mouseptr(wgs, true);
  1895. MessageBox(wgs->term_hwnd, "Connection closed by remote host",
  1896. appname, MB_OK | MB_ICONINFORMATION);
  1897. }
  1898. }
  1899. }
  1900. }
  1901. static void win_seat_notify_remote_exit(Seat *seat)
  1902. {
  1903. WinGuiSeat *wgs = container_of(seat, WinGuiSeat, seat);
  1904. queue_toplevel_callback(exit_callback, wgs);
  1905. }
  1906. static void conf_cache_data(WinGuiSeat *wgs)
  1907. {
  1908. /* Cache some items from conf to speed lookups in very hot code */
  1909. wgs->cursor_type = conf_get_int(wgs->conf, CONF_cursor_type);
  1910. wgs->vtmode = conf_get_int(wgs->conf, CONF_vtmode);
  1911. }
  1912. static const int clips_system[] = { CLIP_SYSTEM };
  1913. static HDC make_hdc(WinGuiSeat *wgs)
  1914. {
  1915. HDC hdc;
  1916. if (!wgs->term_hwnd)
  1917. return NULL;
  1918. hdc = GetDC(wgs->term_hwnd);
  1919. if (!hdc)
  1920. return NULL;
  1921. SelectPalette(hdc, wgs->pal, false);
  1922. return hdc;
  1923. }
  1924. static void free_hdc(WinGuiSeat *wgs, HDC hdc)
  1925. {
  1926. assert(wgs->term_hwnd);
  1927. SelectPalette(hdc, GetStockObject(DEFAULT_PALETTE), false);
  1928. ReleaseDC(wgs->term_hwnd, hdc);
  1929. }
  1930. static void wm_size_resize_term(WinGuiSeat *wgs, LPARAM lParam, bool border)
  1931. {
  1932. int width = LOWORD(lParam);
  1933. int height = HIWORD(lParam);
  1934. int border_size = border ? conf_get_int(wgs->conf, CONF_window_border) : 0;
  1935. int w = (width - border_size*2) / wgs->font_width;
  1936. int h = (height - border_size*2) / wgs->font_height;
  1937. if (w < 1) w = 1;
  1938. if (h < 1) h = 1;
  1939. if (wgs->resizing) {
  1940. /*
  1941. * If we're in the middle of an interactive resize, we don't
  1942. * call term_size. This means that, firstly, the user can drag
  1943. * the size back and forth indecisively without wiping out any
  1944. * actual terminal contents, and secondly, the Terminal
  1945. * doesn't call back->size in turn for each increment of the
  1946. * resizing drag, so we don't spam the server with huge
  1947. * numbers of resize events.
  1948. */
  1949. wgs->need_backend_resize = true;
  1950. conf_set_int(wgs->conf, CONF_height, h);
  1951. conf_set_int(wgs->conf, CONF_width, w);
  1952. } else {
  1953. term_size(wgs->term, h, w,
  1954. conf_get_int(wgs->conf, CONF_savelines));
  1955. }
  1956. }
  1957. static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
  1958. WPARAM wParam, LPARAM lParam)
  1959. {
  1960. HDC hdc;
  1961. int resize_action;
  1962. WinGuiSeat *wgs = (WinGuiSeat *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  1963. switch (message) {
  1964. case WM_CREATE:
  1965. break;
  1966. case WM_CLOSE: {
  1967. char *title, *msg, *additional = NULL;
  1968. show_mouseptr(wgs, true);
  1969. title = dupprintf("%s Exit Confirmation", appname);
  1970. if (wgs->backend && wgs->backend->vt->close_warn_text) {
  1971. additional = wgs->backend->vt->close_warn_text(wgs->backend);
  1972. }
  1973. msg = dupprintf("Are you sure you want to close this session?%s%s",
  1974. additional ? "\n" : "",
  1975. additional ? additional : "");
  1976. if (wgs->session_closed ||
  1977. !conf_get_bool(wgs->conf, CONF_warn_on_close) ||
  1978. MessageBox(hwnd, msg, title,
  1979. MB_ICONWARNING | MB_OKCANCEL | MB_DEFBUTTON1)
  1980. == IDOK)
  1981. DestroyWindow(hwnd);
  1982. sfree(title);
  1983. sfree(msg);
  1984. sfree(additional);
  1985. return 0;
  1986. }
  1987. case WM_DESTROY:
  1988. show_mouseptr(wgs, true);
  1989. PostQuitMessage(0);
  1990. return 0;
  1991. case WM_INITMENUPOPUP:
  1992. if ((HMENU)wParam == wgs->savedsess_menu) {
  1993. /* About to pop up Saved Sessions sub-menu.
  1994. * Refresh the session list. */
  1995. get_sesslist(&sesslist, false); /* free */
  1996. get_sesslist(&sesslist, true);
  1997. update_savedsess_menu(wgs);
  1998. return 0;
  1999. }
  2000. break;
  2001. case WM_COMMAND:
  2002. case WM_SYSCOMMAND:
  2003. switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
  2004. case SC_VSCROLL:
  2005. case SC_HSCROLL:
  2006. if (message == WM_SYSCOMMAND) {
  2007. /* As per the long comment in WM_VSCROLL handler: give
  2008. * this message the default handling, which starts a
  2009. * subsidiary message loop, but set a flag so that
  2010. * when we're re-entered from that loop, scroll events
  2011. * within an interactive scrollbar-drag can be handled
  2012. * differently. */
  2013. wgs->in_scrollbar_loop = true;
  2014. LRESULT result = sw_DefWindowProc(
  2015. hwnd, message, wParam, lParam);
  2016. wgs->in_scrollbar_loop = false;
  2017. return result;
  2018. }
  2019. break;
  2020. case IDM_SHOWLOG:
  2021. showeventlog(hwnd);
  2022. break;
  2023. case IDM_NEWSESS:
  2024. case IDM_DUPSESS:
  2025. case IDM_SAVEDSESS: {
  2026. char b[2048];
  2027. char *cl;
  2028. const char *argprefix;
  2029. bool inherit_handles;
  2030. STARTUPINFO si;
  2031. PROCESS_INFORMATION pi;
  2032. HANDLE filemap = NULL;
  2033. if (restricted_acl())
  2034. argprefix = "&R";
  2035. else
  2036. argprefix = "";
  2037. if (wParam == IDM_DUPSESS) {
  2038. /*
  2039. * Allocate a file-mapping memory chunk for the
  2040. * config structure.
  2041. */
  2042. SECURITY_ATTRIBUTES sa;
  2043. strbuf *serbuf;
  2044. void *p;
  2045. int size;
  2046. serbuf = strbuf_new();
  2047. conf_serialise(BinarySink_UPCAST(serbuf), wgs->conf);
  2048. size = serbuf->len;
  2049. sa.nLength = sizeof(sa);
  2050. sa.lpSecurityDescriptor = NULL;
  2051. sa.bInheritHandle = true;
  2052. filemap = CreateFileMapping(INVALID_HANDLE_VALUE,
  2053. &sa,
  2054. PAGE_READWRITE,
  2055. 0, size, NULL);
  2056. if (filemap && filemap != INVALID_HANDLE_VALUE) {
  2057. p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, size);
  2058. if (p) {
  2059. memcpy(p, serbuf->s, size);
  2060. UnmapViewOfFile(p);
  2061. }
  2062. }
  2063. strbuf_free(serbuf);
  2064. inherit_handles = true;
  2065. cl = dupprintf("putty %s&%p:%u", argprefix,
  2066. filemap, (unsigned)size);
  2067. } else if (wParam == IDM_SAVEDSESS) {
  2068. unsigned int sessno = ((lParam - IDM_SAVED_MIN)
  2069. / MENU_SAVED_STEP) + 1;
  2070. if (sessno < (unsigned)sesslist.nsessions) {
  2071. const char *session = sesslist.sessions[sessno];
  2072. cl = dupprintf("putty %s@%s", argprefix, session);
  2073. inherit_handles = false;
  2074. } else
  2075. break;
  2076. } else /* IDM_NEWSESS */ {
  2077. cl = dupprintf("putty%s%s",
  2078. *argprefix ? " " : "",
  2079. argprefix);
  2080. inherit_handles = false;
  2081. }
  2082. GetModuleFileName(NULL, b, sizeof(b) - 1);
  2083. si.cb = sizeof(si);
  2084. si.lpReserved = NULL;
  2085. si.lpDesktop = NULL;
  2086. si.lpTitle = NULL;
  2087. si.dwFlags = 0;
  2088. si.cbReserved2 = 0;
  2089. si.lpReserved2 = NULL;
  2090. CreateProcess(b, cl, NULL, NULL, inherit_handles,
  2091. NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
  2092. CloseHandle(pi.hProcess);
  2093. CloseHandle(pi.hThread);
  2094. if (filemap)
  2095. CloseHandle(filemap);
  2096. sfree(cl);
  2097. break;
  2098. }
  2099. case IDM_RESTART:
  2100. if (!wgs->backend) {
  2101. lp_eventlog(&wgs->logpolicy, "----- Session restarted -----");
  2102. term_pwron(wgs->term, false);
  2103. start_backend(wgs);
  2104. }
  2105. break;
  2106. case IDM_RECONF: {
  2107. Conf *prev_conf;
  2108. int init_lvl = 1;
  2109. bool reconfig_result;
  2110. if (wgs->reconfiguring)
  2111. break;
  2112. else
  2113. wgs->reconfiguring = true;
  2114. term_pre_reconfig(wgs->term, wgs->conf);
  2115. prev_conf = conf_copy(wgs->conf);
  2116. reconfig_result = do_reconfig(
  2117. hwnd, wgs->conf,
  2118. wgs->backend ? backend_cfg_info(wgs->backend) : 0);
  2119. wgs->reconfiguring = false;
  2120. if (!reconfig_result) {
  2121. conf_free(prev_conf);
  2122. break;
  2123. }
  2124. conf_cache_data(wgs);
  2125. resize_action = conf_get_int(wgs->conf, CONF_resize_action);
  2126. {
  2127. /* Disable full-screen if resizing forbidden */
  2128. int i;
  2129. for (i = 0; i < lenof(wgs->popup_menus); i++)
  2130. EnableMenuItem(wgs->popup_menus[i].menu, IDM_FULLSCREEN,
  2131. MF_BYCOMMAND |
  2132. (resize_action == RESIZE_DISABLED
  2133. ? MF_GRAYED : MF_ENABLED));
  2134. /* Gracefully unzoom if necessary */
  2135. if (IsZoomed(hwnd) && (resize_action == RESIZE_DISABLED))
  2136. ShowWindow(hwnd, SW_RESTORE);
  2137. }
  2138. /* Pass new config data to the logging module */
  2139. log_reconfig(wgs->logctx, wgs->conf);
  2140. sfree(wgs->logpal);
  2141. /*
  2142. * Flush the line discipline's edit buffer in the
  2143. * case where local editing has just been disabled.
  2144. */
  2145. if (wgs->ldisc) {
  2146. ldisc_configure(wgs->ldisc, wgs->conf);
  2147. ldisc_echoedit_update(wgs->ldisc);
  2148. }
  2149. if (conf_get_bool(wgs->conf, CONF_system_colour) !=
  2150. conf_get_bool(prev_conf, CONF_system_colour))
  2151. term_notify_palette_changed(wgs->term);
  2152. /* Pass new config data to the terminal */
  2153. term_reconfig(wgs->term, wgs->conf);
  2154. setup_clipboards(wgs->term, wgs->conf);
  2155. /* Reinitialise the colour palette, in case the terminal
  2156. * just read new settings out of Conf */
  2157. if (wgs->pal)
  2158. DeleteObject(wgs->pal);
  2159. wgs->logpal = NULL;
  2160. wgs->pal = NULL;
  2161. init_palette(wgs);
  2162. /* Pass new config data to the back end */
  2163. if (wgs->backend)
  2164. backend_reconfig(wgs->backend, wgs->conf);
  2165. /* Screen size changed ? */
  2166. if (conf_get_int(wgs->conf, CONF_height) !=
  2167. conf_get_int(prev_conf, CONF_height) ||
  2168. conf_get_int(wgs->conf, CONF_width) !=
  2169. conf_get_int(prev_conf, CONF_width) ||
  2170. conf_get_int(wgs->conf, CONF_savelines) !=
  2171. conf_get_int(prev_conf, CONF_savelines) ||
  2172. resize_action == RESIZE_FONT ||
  2173. (resize_action == RESIZE_EITHER && IsZoomed(hwnd)) ||
  2174. resize_action == RESIZE_DISABLED)
  2175. term_size(wgs->term, conf_get_int(wgs->conf, CONF_height),
  2176. conf_get_int(wgs->conf, CONF_width),
  2177. conf_get_int(wgs->conf, CONF_savelines));
  2178. /* Enable or disable the scroll bar, etc */
  2179. {
  2180. LONG nflg, flag = GetWindowLongPtr(hwnd, GWL_STYLE);
  2181. LONG nexflag, exflag =
  2182. GetWindowLongPtr(hwnd, GWL_EXSTYLE);
  2183. nexflag = exflag;
  2184. if (conf_get_bool(wgs->conf, CONF_alwaysontop) !=
  2185. conf_get_bool(prev_conf, CONF_alwaysontop)) {
  2186. if (conf_get_bool(wgs->conf, CONF_alwaysontop)) {
  2187. nexflag |= WS_EX_TOPMOST;
  2188. SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
  2189. SWP_NOMOVE | SWP_NOSIZE);
  2190. } else {
  2191. nexflag &= ~(WS_EX_TOPMOST);
  2192. SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
  2193. SWP_NOMOVE | SWP_NOSIZE);
  2194. }
  2195. }
  2196. if (conf_get_bool(wgs->conf, CONF_sunken_edge))
  2197. nexflag |= WS_EX_CLIENTEDGE;
  2198. else
  2199. nexflag &= ~(WS_EX_CLIENTEDGE);
  2200. nflg = flag;
  2201. if (conf_get_bool(wgs->conf, is_full_screen(wgs) ?
  2202. CONF_scrollbar_in_fullscreen :
  2203. CONF_scrollbar))
  2204. nflg |= WS_VSCROLL;
  2205. else
  2206. nflg &= ~WS_VSCROLL;
  2207. if (resize_action == RESIZE_DISABLED ||
  2208. is_full_screen(wgs))
  2209. nflg &= ~WS_THICKFRAME;
  2210. else
  2211. nflg |= WS_THICKFRAME;
  2212. if (resize_action == RESIZE_DISABLED)
  2213. nflg &= ~WS_MAXIMIZEBOX;
  2214. else
  2215. nflg |= WS_MAXIMIZEBOX;
  2216. if (nflg != flag || nexflag != exflag) {
  2217. if (nflg != flag)
  2218. SetWindowLongPtr(hwnd, GWL_STYLE, nflg);
  2219. if (nexflag != exflag)
  2220. SetWindowLongPtr(hwnd, GWL_EXSTYLE, nexflag);
  2221. SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
  2222. SWP_NOACTIVATE | SWP_NOCOPYBITS |
  2223. SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
  2224. SWP_FRAMECHANGED);
  2225. init_lvl = 2;
  2226. }
  2227. }
  2228. /* Oops */
  2229. if (resize_action == RESIZE_DISABLED && IsZoomed(hwnd)) {
  2230. force_normal(hwnd);
  2231. init_lvl = 2;
  2232. }
  2233. {
  2234. FontSpec *font = conf_get_fontspec(wgs->conf, CONF_font);
  2235. FontSpec *prev_font = conf_get_fontspec(prev_conf,
  2236. CONF_font);
  2237. if (!strcmp(font->name, prev_font->name) ||
  2238. !strcmp(conf_get_str(wgs->conf, CONF_line_codepage),
  2239. conf_get_str(prev_conf, CONF_line_codepage)) ||
  2240. font->isbold != prev_font->isbold ||
  2241. font->height != prev_font->height ||
  2242. font->charset != prev_font->charset ||
  2243. conf_get_int(wgs->conf, CONF_font_quality) !=
  2244. conf_get_int(prev_conf, CONF_font_quality) ||
  2245. conf_get_int(wgs->conf, CONF_vtmode) !=
  2246. conf_get_int(prev_conf, CONF_vtmode) ||
  2247. conf_get_int(wgs->conf, CONF_bold_style) !=
  2248. conf_get_int(prev_conf, CONF_bold_style) ||
  2249. resize_action == RESIZE_DISABLED ||
  2250. resize_action == RESIZE_EITHER ||
  2251. resize_action != conf_get_int(prev_conf,
  2252. CONF_resize_action))
  2253. init_lvl = 2;
  2254. }
  2255. InvalidateRect(hwnd, NULL, true);
  2256. reset_window(wgs, init_lvl);
  2257. conf_free(prev_conf);
  2258. break;
  2259. }
  2260. case IDM_COPYALL:
  2261. term_copyall(wgs->term, clips_system, lenof(clips_system));
  2262. break;
  2263. case IDM_COPY:
  2264. term_request_copy(wgs->term, clips_system, lenof(clips_system));
  2265. break;
  2266. case IDM_PASTE:
  2267. term_request_paste(wgs->term, CLIP_SYSTEM);
  2268. break;
  2269. case IDM_CLRSB:
  2270. term_clrsb(wgs->term);
  2271. break;
  2272. case IDM_RESET:
  2273. term_pwron(wgs->term, true);
  2274. if (wgs->ldisc)
  2275. ldisc_echoedit_update(wgs->ldisc);
  2276. break;
  2277. case IDM_ABOUT:
  2278. showabout(hwnd);
  2279. break;
  2280. case IDM_HELP:
  2281. launch_help(hwnd, NULL);
  2282. break;
  2283. case SC_MOUSEMENU:
  2284. /*
  2285. * We get this if the System menu has been activated
  2286. * using the mouse.
  2287. */
  2288. show_mouseptr(wgs, true);
  2289. break;
  2290. case SC_KEYMENU:
  2291. /*
  2292. * We get this if the System menu has been activated
  2293. * using the keyboard. This might happen from within
  2294. * TranslateKey, in which case it really wants to be
  2295. * followed by a `space' character to actually _bring
  2296. * the menu up_ rather than just sitting there in
  2297. * `ready to appear' state.
  2298. */
  2299. show_mouseptr(wgs, true); /* make sure pointer is visible */
  2300. if (lParam == 0)
  2301. PostMessage(hwnd, WM_CHAR, ' ', 0);
  2302. break;
  2303. case IDM_FULLSCREEN:
  2304. flip_full_screen(wgs);
  2305. break;
  2306. default:
  2307. if (wParam >= IDM_SAVED_MIN && wParam < IDM_SAVED_MAX) {
  2308. SendMessage(hwnd, WM_SYSCOMMAND, IDM_SAVEDSESS, wParam);
  2309. }
  2310. if (wParam >= IDM_SPECIAL_MIN && wParam <= IDM_SPECIAL_MAX) {
  2311. int i = (wParam - IDM_SPECIAL_MIN) / 0x10;
  2312. /*
  2313. * Ensure we haven't been sent a bogus SYSCOMMAND
  2314. * which would cause us to reference invalid memory
  2315. * and crash. Perhaps I'm just too paranoid here.
  2316. */
  2317. if (i >= wgs->n_specials)
  2318. break;
  2319. if (wgs->backend)
  2320. backend_special(wgs->backend, wgs->specials[i].code,
  2321. wgs->specials[i].arg);
  2322. }
  2323. }
  2324. break;
  2325. #define X_POS(l) ((int)(short)LOWORD(l))
  2326. #define Y_POS(l) ((int)(short)HIWORD(l))
  2327. #define TO_CHR_X(x) ((((x)<0 ? (x)-wgs->font_width+1 : \
  2328. (x))-wgs->offset_width) / wgs->font_width)
  2329. #define TO_CHR_Y(y) ((((y)<0 ? (y)-wgs->font_height+1 : \
  2330. (y))-wgs->offset_height) / wgs->font_height)
  2331. case WM_LBUTTONDOWN:
  2332. case WM_MBUTTONDOWN:
  2333. case WM_RBUTTONDOWN:
  2334. case WM_LBUTTONUP:
  2335. case WM_MBUTTONUP:
  2336. case WM_RBUTTONUP:
  2337. if (message == WM_RBUTTONDOWN &&
  2338. ((wParam & MK_CONTROL) ||
  2339. (conf_get_int(wgs->conf, CONF_mouse_is_xterm) == MOUSE_WINDOWS))) {
  2340. POINT cursorpos;
  2341. /* Just in case this happened in mid-select */
  2342. term_cancel_selection_drag(wgs->term);
  2343. show_mouseptr(wgs, true); /* make sure pointer is visible */
  2344. GetCursorPos(&cursorpos);
  2345. TrackPopupMenu(wgs->popup_menus[CTXMENU].menu,
  2346. TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON,
  2347. cursorpos.x, cursorpos.y,
  2348. 0, hwnd, NULL);
  2349. break;
  2350. }
  2351. {
  2352. int button;
  2353. bool press;
  2354. switch (message) {
  2355. case WM_LBUTTONDOWN:
  2356. button = MBT_LEFT;
  2357. wParam |= MK_LBUTTON;
  2358. press = true;
  2359. break;
  2360. case WM_MBUTTONDOWN:
  2361. button = MBT_MIDDLE;
  2362. wParam |= MK_MBUTTON;
  2363. press = true;
  2364. break;
  2365. case WM_RBUTTONDOWN:
  2366. button = MBT_RIGHT;
  2367. wParam |= MK_RBUTTON;
  2368. press = true;
  2369. break;
  2370. case WM_LBUTTONUP:
  2371. button = MBT_LEFT;
  2372. wParam &= ~MK_LBUTTON;
  2373. press = false;
  2374. break;
  2375. case WM_MBUTTONUP:
  2376. button = MBT_MIDDLE;
  2377. wParam &= ~MK_MBUTTON;
  2378. press = false;
  2379. break;
  2380. case WM_RBUTTONUP:
  2381. button = MBT_RIGHT;
  2382. wParam &= ~MK_RBUTTON;
  2383. press = false;
  2384. break;
  2385. default: /* shouldn't happen */
  2386. button = 0;
  2387. press = false;
  2388. }
  2389. show_mouseptr(wgs, true);
  2390. /*
  2391. * Special case: in full-screen mode, if the left
  2392. * button is clicked in the very top left corner of the
  2393. * window, we put up the System menu instead of doing
  2394. * selection.
  2395. */
  2396. {
  2397. bool mouse_on_hotspot = false;
  2398. POINT pt;
  2399. GetCursorPos(&pt);
  2400. #ifndef NO_MULTIMON
  2401. if (p_GetMonitorInfoA && p_MonitorFromPoint) {
  2402. HMONITOR mon;
  2403. MONITORINFO mi;
  2404. mon = p_MonitorFromPoint(pt, MONITOR_DEFAULTTONULL);
  2405. if (mon != NULL) {
  2406. mi.cbSize = sizeof(MONITORINFO);
  2407. p_GetMonitorInfoA(mon, &mi);
  2408. if (mi.rcMonitor.left == pt.x &&
  2409. mi.rcMonitor.top == pt.y) {
  2410. mouse_on_hotspot = true;
  2411. }
  2412. }
  2413. } else
  2414. #endif
  2415. if (pt.x == 0 && pt.y == 0) {
  2416. mouse_on_hotspot = true;
  2417. }
  2418. if (is_full_screen(wgs) && press &&
  2419. button == MBT_LEFT && mouse_on_hotspot) {
  2420. SendMessage(hwnd, WM_SYSCOMMAND, SC_MOUSEMENU,
  2421. MAKELPARAM(pt.x, pt.y));
  2422. return 0;
  2423. }
  2424. }
  2425. if (press) {
  2426. click(wgs, button,
  2427. TO_CHR_X(X_POS(lParam)), TO_CHR_Y(Y_POS(lParam)),
  2428. wParam & MK_SHIFT, wParam & MK_CONTROL,
  2429. is_alt_pressed());
  2430. SetCapture(hwnd);
  2431. } else {
  2432. term_mouse(wgs->term, button, translate_button(wgs, button),
  2433. MA_RELEASE, TO_CHR_X(X_POS(lParam)),
  2434. TO_CHR_Y(Y_POS(lParam)), wParam & MK_SHIFT,
  2435. wParam & MK_CONTROL, is_alt_pressed());
  2436. if (!(wParam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON)))
  2437. ReleaseCapture();
  2438. }
  2439. }
  2440. return 0;
  2441. case WM_MOUSEMOVE:
  2442. /*
  2443. * Windows seems to like to occasionally send MOUSEMOVE
  2444. * events even if the mouse hasn't moved. Don't unhide
  2445. * the mouse pointer in this case.
  2446. */
  2447. if (wgs->last_mousemove != WM_MOUSEMOVE ||
  2448. wParam != wgs->last_wm_mousemove_wParam ||
  2449. lParam != wgs->last_wm_mousemove_lParam) {
  2450. show_mouseptr(wgs, true);
  2451. wgs->last_mousemove = WM_MOUSEMOVE;
  2452. wgs->last_wm_mousemove_wParam = wParam;
  2453. wgs->last_wm_mousemove_lParam = lParam;
  2454. }
  2455. /*
  2456. * Add the mouse position and message time to the random
  2457. * number noise.
  2458. */
  2459. noise_ultralight(NOISE_SOURCE_MOUSEPOS, lParam);
  2460. if (wParam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON) &&
  2461. GetCapture() == hwnd) {
  2462. Mouse_Button b;
  2463. if (wParam & MK_LBUTTON)
  2464. b = MBT_LEFT;
  2465. else if (wParam & MK_MBUTTON)
  2466. b = MBT_MIDDLE;
  2467. else
  2468. b = MBT_RIGHT;
  2469. term_mouse(wgs->term, b, translate_button(wgs, b), MA_DRAG,
  2470. TO_CHR_X(X_POS(lParam)),
  2471. TO_CHR_Y(Y_POS(lParam)), wParam & MK_SHIFT,
  2472. wParam & MK_CONTROL, is_alt_pressed());
  2473. } else {
  2474. term_mouse(wgs->term, MBT_NOTHING, MBT_NOTHING, MA_MOVE,
  2475. TO_CHR_X(X_POS(lParam)),
  2476. TO_CHR_Y(Y_POS(lParam)), false,
  2477. false, false);
  2478. }
  2479. return 0;
  2480. case WM_NCMOUSEMOVE:
  2481. if (wgs->last_mousemove != WM_NCMOUSEMOVE ||
  2482. wParam != wgs->last_wm_ncmousemove_wParam ||
  2483. lParam != wgs->last_wm_ncmousemove_lParam) {
  2484. show_mouseptr(wgs, true);
  2485. wgs->last_mousemove = WM_NCMOUSEMOVE;
  2486. wgs->last_wm_ncmousemove_wParam = wParam;
  2487. wgs->last_wm_ncmousemove_lParam = lParam;
  2488. }
  2489. noise_ultralight(NOISE_SOURCE_MOUSEPOS, lParam);
  2490. break;
  2491. case WM_IGNORE_CLIP:
  2492. wgs->ignore_clip = wParam; /* don't panic on DESTROYCLIPBOARD */
  2493. break;
  2494. case WM_DESTROYCLIPBOARD:
  2495. if (!wgs->ignore_clip)
  2496. term_lost_clipboard_ownership(wgs->term, CLIP_SYSTEM);
  2497. wgs->ignore_clip = false;
  2498. return 0;
  2499. case WM_PAINT: {
  2500. PAINTSTRUCT p;
  2501. HideCaret(hwnd);
  2502. hdc = BeginPaint(hwnd, &p);
  2503. if (wgs->pal) {
  2504. SelectPalette(hdc, wgs->pal, true);
  2505. RealizePalette(hdc);
  2506. }
  2507. /*
  2508. * We have to be careful about term_paint(). It will
  2509. * set a bunch of character cells to INVALID and then
  2510. * call do_paint(), which will redraw those cells and
  2511. * _then mark them as done_. This may not be accurate:
  2512. * when painting in WM_PAINT context we are restricted
  2513. * to the rectangle which has just been exposed - so if
  2514. * that only covers _part_ of a character cell and the
  2515. * rest of it was already visible, that remainder will
  2516. * not be redrawn at all. Accordingly, we must not
  2517. * paint any character cell in a WM_PAINT context which
  2518. * already has a pending update due to terminal output.
  2519. * The simplest solution to this - and many, many
  2520. * thanks to Hung-Te Lin for working all this out - is
  2521. * not to do any actual painting at _all_ if there's a
  2522. * pending terminal update: just mark the relevant
  2523. * character cells as INVALID and wait for the
  2524. * scheduled full update to sort it out.
  2525. *
  2526. * I have a suspicion this isn't the _right_ solution.
  2527. * An alternative approach would be to have terminal.c
  2528. * separately track what _should_ be on the terminal
  2529. * screen and what _is_ on the terminal screen, and
  2530. * have two completely different types of redraw (one
  2531. * for full updates, which syncs the former with the
  2532. * terminal itself, and one for WM_PAINT which syncs
  2533. * the latter with the former); yet another possibility
  2534. * would be to have the Windows front end do what the
  2535. * GTK one already does, and maintain a bitmap of the
  2536. * current terminal appearance so that WM_PAINT becomes
  2537. * completely trivial. However, this should do for now.
  2538. */
  2539. assert(!wgs->wintw_hdc);
  2540. wgs->wintw_hdc = hdc;
  2541. term_paint(wgs->term,
  2542. (p.rcPaint.left-wgs->offset_width)/wgs->font_width,
  2543. (p.rcPaint.top-wgs->offset_height)/wgs->font_height,
  2544. (p.rcPaint.right-wgs->offset_width-1)/wgs->font_width,
  2545. (p.rcPaint.bottom-wgs->offset_height-1)/wgs->font_height,
  2546. !wgs->term->window_update_pending);
  2547. wgs->wintw_hdc = NULL;
  2548. if (p.fErase ||
  2549. p.rcPaint.left < wgs->offset_width ||
  2550. p.rcPaint.top < wgs->offset_height ||
  2551. p.rcPaint.right >= (wgs->offset_width +
  2552. wgs->font_width*wgs->term->cols) ||
  2553. p.rcPaint.bottom>= (wgs->offset_height +
  2554. wgs->font_height*wgs->term->rows)) {
  2555. HBRUSH fillcolour, oldbrush;
  2556. HPEN edge, oldpen;
  2557. fillcolour = CreateSolidBrush (
  2558. wgs->colours[ATTR_DEFBG>>ATTR_BGSHIFT]);
  2559. oldbrush = SelectObject(hdc, fillcolour);
  2560. edge = CreatePen(PS_SOLID, 0,
  2561. wgs->colours[ATTR_DEFBG>>ATTR_BGSHIFT]);
  2562. oldpen = SelectObject(hdc, edge);
  2563. /*
  2564. * Jordan Russell reports that this apparently
  2565. * ineffectual IntersectClipRect() call masks a
  2566. * Windows NT/2K bug causing strange display
  2567. * problems when the PuTTY window is taller than
  2568. * the primary monitor. It seems harmless enough...
  2569. */
  2570. IntersectClipRect(hdc,
  2571. p.rcPaint.left, p.rcPaint.top,
  2572. p.rcPaint.right, p.rcPaint.bottom);
  2573. ExcludeClipRect(
  2574. hdc, wgs->offset_width, wgs->offset_height,
  2575. wgs->offset_width+wgs->font_width*wgs->term->cols,
  2576. wgs->offset_height+wgs->font_height*wgs->term->rows);
  2577. Rectangle(hdc, p.rcPaint.left, p.rcPaint.top,
  2578. p.rcPaint.right, p.rcPaint.bottom);
  2579. /* SelectClipRgn(hdc, NULL); */
  2580. SelectObject(hdc, oldbrush);
  2581. DeleteObject(fillcolour);
  2582. SelectObject(hdc, oldpen);
  2583. DeleteObject(edge);
  2584. }
  2585. SelectObject(hdc, GetStockObject(SYSTEM_FONT));
  2586. SelectObject(hdc, GetStockObject(WHITE_PEN));
  2587. EndPaint(hwnd, &p);
  2588. ShowCaret(hwnd);
  2589. return 0;
  2590. }
  2591. case WM_NETEVENT:
  2592. winselgui_response(wParam, lParam);
  2593. return 0;
  2594. case WM_SETFOCUS:
  2595. term_set_focus(wgs->term, true);
  2596. CreateCaret(hwnd, wgs->caretbm, wgs->font_width, wgs->font_height);
  2597. ShowCaret(hwnd);
  2598. flash_window(wgs, 0); /* stop */
  2599. wgs->compose_state = 0;
  2600. term_update(wgs->term);
  2601. break;
  2602. case WM_KILLFOCUS:
  2603. show_mouseptr(wgs, true);
  2604. term_set_focus(wgs->term, false);
  2605. DestroyCaret();
  2606. wgs->caret_x = wgs->caret_y = -1; /* ensure caret replaced next time */
  2607. term_update(wgs->term);
  2608. break;
  2609. case WM_ENTERSIZEMOVE:
  2610. #ifdef RDB_DEBUG_PATCH
  2611. debug("WM_ENTERSIZEMOVE\n");
  2612. #endif
  2613. EnableSizeTip(true);
  2614. wgs->resizing = true;
  2615. wgs->need_backend_resize = false;
  2616. break;
  2617. case WM_EXITSIZEMOVE:
  2618. EnableSizeTip(false);
  2619. wgs->resizing = false;
  2620. #ifdef RDB_DEBUG_PATCH
  2621. debug("WM_EXITSIZEMOVE\n");
  2622. #endif
  2623. if (wgs->need_backend_resize) {
  2624. term_size(wgs->term, conf_get_int(wgs->conf, CONF_height),
  2625. conf_get_int(wgs->conf, CONF_width),
  2626. conf_get_int(wgs->conf, CONF_savelines));
  2627. InvalidateRect(hwnd, NULL, true);
  2628. }
  2629. recompute_window_offset(wgs);
  2630. break;
  2631. case WM_SIZING:
  2632. /*
  2633. * This does two jobs:
  2634. * 1) Keep the sizetip uptodate
  2635. * 2) Make sure the window size is _stepped_ in units of the font size.
  2636. */
  2637. resize_action = conf_get_int(wgs->conf, CONF_resize_action);
  2638. if (resize_action == RESIZE_TERM ||
  2639. (resize_action == RESIZE_EITHER && !is_alt_pressed())) {
  2640. int width, height, w, h, ew, eh;
  2641. LPRECT r = (LPRECT) lParam;
  2642. if (!wgs->need_backend_resize && resize_action == RESIZE_EITHER &&
  2643. (conf_get_int(wgs->conf, CONF_height) != wgs->term->rows ||
  2644. conf_get_int(wgs->conf, CONF_width) != wgs->term->cols)) {
  2645. /*
  2646. * Great! It seems that both the terminal size and the
  2647. * font size have been changed and the user is now dragging.
  2648. *
  2649. * It will now be difficult to get back to the configured
  2650. * font size!
  2651. *
  2652. * This would be easier but it seems to be too confusing.
  2653. */
  2654. conf_set_int(wgs->conf, CONF_height, wgs->term->rows);
  2655. conf_set_int(wgs->conf, CONF_width, wgs->term->cols);
  2656. InvalidateRect(hwnd, NULL, true);
  2657. wgs->need_backend_resize = true;
  2658. }
  2659. width = r->right - r->left - wgs->extra_width;
  2660. height = r->bottom - r->top - wgs->extra_height;
  2661. w = (width + wgs->font_width / 2) / wgs->font_width;
  2662. if (w < 1)
  2663. w = 1;
  2664. h = (height + wgs->font_height / 2) / wgs->font_height;
  2665. if (h < 1)
  2666. h = 1;
  2667. UpdateSizeTip(hwnd, w, h);
  2668. ew = width - w * wgs->font_width;
  2669. eh = height - h * wgs->font_height;
  2670. if (ew != 0) {
  2671. if (wParam == WMSZ_LEFT ||
  2672. wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_TOPLEFT)
  2673. r->left += ew;
  2674. else
  2675. r->right -= ew;
  2676. }
  2677. if (eh != 0) {
  2678. if (wParam == WMSZ_TOP ||
  2679. wParam == WMSZ_TOPRIGHT || wParam == WMSZ_TOPLEFT)
  2680. r->top += eh;
  2681. else
  2682. r->bottom -= eh;
  2683. }
  2684. if (ew || eh)
  2685. return 1;
  2686. else
  2687. return 0;
  2688. } else {
  2689. int width, height, w, h, rv = 0;
  2690. int window_border = conf_get_int(wgs->conf, CONF_window_border);
  2691. int ex_width = wgs->extra_width +
  2692. (window_border - wgs->offset_width) * 2;
  2693. int ex_height = wgs->extra_height +
  2694. (window_border - wgs->offset_height) * 2;
  2695. LPRECT r = (LPRECT) lParam;
  2696. width = r->right - r->left - ex_width;
  2697. height = r->bottom - r->top - ex_height;
  2698. w = (width + wgs->term->cols/2)/wgs->term->cols;
  2699. h = (height + wgs->term->rows/2)/wgs->term->rows;
  2700. if ( r->right != r->left + w*wgs->term->cols + ex_width)
  2701. rv = 1;
  2702. if (wParam == WMSZ_LEFT ||
  2703. wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_TOPLEFT)
  2704. r->left = r->right - w*wgs->term->cols - ex_width;
  2705. else
  2706. r->right = r->left + w*wgs->term->cols + ex_width;
  2707. if (r->bottom != r->top + h*wgs->term->rows + ex_height)
  2708. rv = 1;
  2709. if (wParam == WMSZ_TOP ||
  2710. wParam == WMSZ_TOPRIGHT || wParam == WMSZ_TOPLEFT)
  2711. r->top = r->bottom - h*wgs->term->rows - ex_height;
  2712. else
  2713. r->bottom = r->top + h*wgs->term->rows + ex_height;
  2714. return rv;
  2715. }
  2716. /* break; (never reached) */
  2717. case WM_FULLSCR_ON_MAX:
  2718. wgs->fullscr_on_max = true;
  2719. break;
  2720. case WM_MOVE:
  2721. term_notify_window_pos(wgs->term, LOWORD(lParam), HIWORD(lParam));
  2722. sys_cursor_update(wgs);
  2723. break;
  2724. case WM_SIZE:
  2725. resize_action = conf_get_int(wgs->conf, CONF_resize_action);
  2726. #ifdef RDB_DEBUG_PATCH
  2727. debug("WM_SIZE %s (%d,%d)\n",
  2728. (wParam == SIZE_MINIMIZED) ? "SIZE_MINIMIZED":
  2729. (wParam == SIZE_MAXIMIZED) ? "SIZE_MAXIMIZED":
  2730. (wParam == SIZE_RESTORED && resizing) ? "to":
  2731. (wParam == SIZE_RESTORED) ? "SIZE_RESTORED":
  2732. "...",
  2733. LOWORD(lParam), HIWORD(lParam));
  2734. #endif
  2735. term_notify_minimised(wgs->term, wParam == SIZE_MINIMIZED);
  2736. {
  2737. /*
  2738. * WM_SIZE's lParam tells us the size of the client area.
  2739. * But historic PuTTY practice is that we want to tell the
  2740. * terminal the size of the overall window.
  2741. */
  2742. RECT r;
  2743. GetWindowRect(hwnd, &r);
  2744. term_notify_window_size_pixels(
  2745. wgs->term, r.right - r.left, r.bottom - r.top);
  2746. }
  2747. if (wParam == SIZE_MINIMIZED)
  2748. sw_SetWindowText(hwnd,
  2749. conf_get_bool(wgs->conf, CONF_win_name_always) ?
  2750. wgs->window_name : wgs->icon_name);
  2751. if (wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED)
  2752. sw_SetWindowText(hwnd, wgs->window_name);
  2753. if (wParam == SIZE_RESTORED) {
  2754. wgs->processed_resize = false;
  2755. clear_full_screen(wgs);
  2756. if (wgs->processed_resize) {
  2757. /*
  2758. * Inhibit normal processing of this WM_SIZE; a
  2759. * secondary one was triggered just now by
  2760. * clear_full_screen which contained the correct
  2761. * client area size.
  2762. */
  2763. return 0;
  2764. }
  2765. }
  2766. if (wParam == SIZE_MAXIMIZED && wgs->fullscr_on_max) {
  2767. wgs->fullscr_on_max = false;
  2768. wgs->processed_resize = false;
  2769. make_full_screen(wgs);
  2770. if (wgs->processed_resize) {
  2771. /*
  2772. * Inhibit normal processing of this WM_SIZE; a
  2773. * secondary one was triggered just now by
  2774. * make_full_screen which contained the correct client
  2775. * area size.
  2776. */
  2777. return 0;
  2778. }
  2779. }
  2780. wgs->processed_resize = true;
  2781. if (resize_action == RESIZE_DISABLED) {
  2782. /* A resize, well it better be a minimize. */
  2783. reset_window(wgs, -1);
  2784. } else {
  2785. if (wParam == SIZE_MAXIMIZED) {
  2786. wgs->was_zoomed = true;
  2787. wgs->prev_rows = wgs->term->rows;
  2788. wgs->prev_cols = wgs->term->cols;
  2789. if (resize_action == RESIZE_TERM)
  2790. wm_size_resize_term(wgs, lParam, false);
  2791. reset_window(wgs, 0);
  2792. } else if (wParam == SIZE_RESTORED && wgs->was_zoomed) {
  2793. wgs->was_zoomed = false;
  2794. if (resize_action == RESIZE_TERM) {
  2795. wm_size_resize_term(wgs, lParam, true);
  2796. reset_window(wgs, 2);
  2797. } else if (resize_action != RESIZE_FONT)
  2798. reset_window(wgs, 2);
  2799. else
  2800. reset_window(wgs, 0);
  2801. } else if (wParam == SIZE_MINIMIZED) {
  2802. /* do nothing */
  2803. } else if (resize_action == RESIZE_TERM ||
  2804. (resize_action == RESIZE_EITHER &&
  2805. !is_alt_pressed())) {
  2806. wm_size_resize_term(wgs, lParam, true);
  2807. /*
  2808. * Sometimes, we can get a spontaneous resize event
  2809. * outside a WM_SIZING interactive drag which wants to
  2810. * set us to a new specific SIZE_RESTORED size. An
  2811. * example is what happens if you press Windows+Right
  2812. * and then Windows+Up: the first operation fits the
  2813. * window to the right-hand half of the screen, and
  2814. * the second one changes that for the top right
  2815. * quadrant. In that situation, if we've responded
  2816. * here by resizing the terminal, we may still need to
  2817. * recompute the border around the window and do a
  2818. * full redraw to clear the new border.
  2819. */
  2820. if (!wgs->resizing)
  2821. recompute_window_offset(wgs);
  2822. } else {
  2823. reset_window(wgs, 0);
  2824. }
  2825. }
  2826. sys_cursor_update(wgs);
  2827. return 0;
  2828. case WM_DPICHANGED:
  2829. wgs->dpi_info.cur_dpi.x = LOWORD(wParam);
  2830. wgs->dpi_info.cur_dpi.y = HIWORD(wParam);
  2831. wgs->dpi_info.new_wnd_rect = *(RECT*)(lParam);
  2832. reset_window(wgs, 3);
  2833. return 0;
  2834. case WM_VSCROLL:
  2835. switch (LOWORD(wParam)) {
  2836. case SB_BOTTOM:
  2837. term_scroll(wgs->term, -1, 0);
  2838. break;
  2839. case SB_TOP:
  2840. term_scroll(wgs->term, +1, 0);
  2841. break;
  2842. case SB_LINEDOWN:
  2843. term_scroll(wgs->term, 0, +1);
  2844. break;
  2845. case SB_LINEUP:
  2846. term_scroll(wgs->term, 0, -1);
  2847. break;
  2848. case SB_PAGEDOWN:
  2849. term_scroll(wgs->term, 0, +wgs->term->rows / 2);
  2850. break;
  2851. case SB_PAGEUP:
  2852. term_scroll(wgs->term, 0, -wgs->term->rows / 2);
  2853. break;
  2854. case SB_THUMBPOSITION:
  2855. case SB_THUMBTRACK: {
  2856. /*
  2857. * Use GetScrollInfo instead of HIWORD(wParam) to get
  2858. * 32-bit scroll position.
  2859. */
  2860. SCROLLINFO si;
  2861. si.cbSize = sizeof(si);
  2862. si.fMask = SIF_TRACKPOS;
  2863. if (GetScrollInfo(hwnd, SB_VERT, &si) == 0)
  2864. si.nTrackPos = HIWORD(wParam);
  2865. term_scroll(wgs->term, 1, si.nTrackPos);
  2866. break;
  2867. }
  2868. }
  2869. if (wgs->in_scrollbar_loop) {
  2870. /*
  2871. * Allow window updates to happen during interactive
  2872. * scroll.
  2873. *
  2874. * When the user takes hold of our window's scrollbar and
  2875. * wobbles it interactively back and forth, or presses on
  2876. * one of the arrow buttons at the ends, the first thing
  2877. * that happens is that this window procedure receives
  2878. * WM_SYSCOMMAND / SC_VSCROLL. [1] The default handler for
  2879. * that window message starts a subsidiary message loop,
  2880. * which continues to run until the user lets go of the
  2881. * scrollbar again. All WM_VSCROLL / SB_THUMBTRACK
  2882. * messages are generated by the handlers within that
  2883. * subsidiary message loop.
  2884. *
  2885. * So, during that time, _our_ message loop is not
  2886. * running, which means toplevel callbacks and timers and
  2887. * so forth are not happening, which means that when we
  2888. * redraw the window and set a timer to clear the cooldown
  2889. * flag 20ms later, that timer never fires, and we aren't
  2890. * able to keep redrawing the window.
  2891. *
  2892. * The 'obvious' answer would be to seize that SYSCOMMAND
  2893. * ourselves and inhibit the default handler, so that our
  2894. * message loop carries on running. But that would mean
  2895. * we'd have to reimplement the whole of the scrollbar
  2896. * handler!
  2897. *
  2898. * So instead we apply a bodge: set a static variable that
  2899. * indicates that we're _in_ that sub-loop, and if so,
  2900. * decide it's OK to manually call term_update() proper,
  2901. * bypassing the timer and cooldown and rate-limiting
  2902. * systems completely, whenever we see an SB_THUMBTRACK.
  2903. * This shouldn't cause a rate overload, because we're
  2904. * only doing it once per UI event!
  2905. *
  2906. * [1] Actually, there's an extra oddity where SC_HSCROLL
  2907. * and SC_VSCROLL have their documented values the wrong
  2908. * way round. Many people on the Internet have noticed
  2909. * this, e.g. https://stackoverflow.com/q/55528397
  2910. */
  2911. term_update(wgs->term);
  2912. }
  2913. break;
  2914. case WM_PALETTECHANGED:
  2915. if ((HWND) wParam != hwnd && wgs->pal != NULL) {
  2916. HDC hdc = make_hdc(wgs);
  2917. if (hdc) {
  2918. if (RealizePalette(hdc) > 0)
  2919. UpdateColors(hdc);
  2920. free_hdc(wgs, hdc);
  2921. }
  2922. }
  2923. break;
  2924. case WM_QUERYNEWPALETTE:
  2925. if (wgs->pal != NULL) {
  2926. HDC hdc = make_hdc(wgs);
  2927. if (hdc) {
  2928. if (RealizePalette(hdc) > 0)
  2929. UpdateColors(hdc);
  2930. free_hdc(wgs, hdc);
  2931. return true;
  2932. }
  2933. }
  2934. return false;
  2935. case WM_KEYDOWN:
  2936. case WM_SYSKEYDOWN:
  2937. case WM_KEYUP:
  2938. case WM_SYSKEYUP:
  2939. /*
  2940. * Add the scan code and keypress timing to the random
  2941. * number noise.
  2942. */
  2943. noise_ultralight(NOISE_SOURCE_KEY, lParam);
  2944. /*
  2945. * We don't do TranslateMessage since it disassociates the
  2946. * resulting CHAR message from the KEYDOWN that sparked it,
  2947. * which we occasionally don't want. Instead, we process
  2948. * KEYDOWN, and call the Win32 translator functions so that
  2949. * we get the translations under _our_ control.
  2950. */
  2951. {
  2952. unsigned char buf[20];
  2953. int len;
  2954. if (wParam == VK_PROCESSKEY || /* IME PROCESS key */
  2955. wParam == VK_PACKET) { /* 'this key is a Unicode char' */
  2956. if (message == WM_KEYDOWN) {
  2957. MSG m;
  2958. m.hwnd = hwnd;
  2959. m.message = WM_KEYDOWN;
  2960. m.wParam = wParam;
  2961. m.lParam = lParam & 0xdfff;
  2962. TranslateMessage(&m);
  2963. } else break; /* pass to Windows for default processing */
  2964. } else {
  2965. len = TranslateKey(wgs, message, wParam, lParam, buf);
  2966. if (len == -1)
  2967. return sw_DefWindowProc(hwnd, message, wParam, lParam);
  2968. if (len != 0) {
  2969. /*
  2970. * We need not bother about stdin backlogs
  2971. * here, because in GUI PuTTY we can't do
  2972. * anything about it anyway; there's no means
  2973. * of asking Windows to hold off on KEYDOWN
  2974. * messages. We _have_ to buffer everything
  2975. * we're sent.
  2976. */
  2977. term_keyinput(wgs->term, -1, buf, len);
  2978. show_mouseptr(wgs, false);
  2979. }
  2980. }
  2981. }
  2982. return 0;
  2983. case WM_INPUTLANGCHANGE:
  2984. /* wParam == Font number */
  2985. /* lParam == Locale */
  2986. set_input_locale(wgs, (HKL)lParam);
  2987. sys_cursor_update(wgs);
  2988. break;
  2989. case WM_IME_STARTCOMPOSITION: {
  2990. HIMC hImc = ImmGetContext(hwnd);
  2991. ImmSetCompositionFont(hImc, &wgs->lfont);
  2992. ImmReleaseContext(hwnd, hImc);
  2993. break;
  2994. }
  2995. case WM_IME_COMPOSITION: {
  2996. HIMC hIMC;
  2997. int n;
  2998. char *buff;
  2999. if (osPlatformId == VER_PLATFORM_WIN32_WINDOWS ||
  3000. osPlatformId == VER_PLATFORM_WIN32s)
  3001. break; /* no Unicode */
  3002. if ((lParam & GCS_RESULTSTR) == 0) /* Composition unfinished. */
  3003. break; /* fall back to DefWindowProc */
  3004. hIMC = ImmGetContext(hwnd);
  3005. n = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
  3006. if (n > 0) {
  3007. int i;
  3008. buff = snewn(n, char);
  3009. ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, buff, n);
  3010. /*
  3011. * Jaeyoun Chung reports that Korean character
  3012. * input doesn't work correctly if we do a single
  3013. * term_keyinputw covering the whole of buff. So
  3014. * instead we send the characters one by one.
  3015. */
  3016. /* don't divide SURROGATE PAIR */
  3017. if (wgs->ldisc) {
  3018. for (i = 0; i < n; i += 2) {
  3019. WCHAR hs = *(unsigned short *)(buff+i);
  3020. if (IS_HIGH_SURROGATE(hs) && i+2 < n) {
  3021. WCHAR ls = *(unsigned short *)(buff+i+2);
  3022. if (IS_LOW_SURROGATE(ls)) {
  3023. term_keyinputw(
  3024. wgs->term, (unsigned short *)(buff+i), 2);
  3025. i += 2;
  3026. continue;
  3027. }
  3028. }
  3029. term_keyinputw(
  3030. wgs->term, (unsigned short *)(buff+i), 1);
  3031. }
  3032. }
  3033. free(buff);
  3034. }
  3035. ImmReleaseContext(hwnd, hIMC);
  3036. return 1;
  3037. }
  3038. case WM_IME_CHAR:
  3039. if (wParam & 0xFF00) {
  3040. char buf[2];
  3041. buf[1] = wParam;
  3042. buf[0] = wParam >> 8;
  3043. term_keyinput(wgs->term, wgs->kbd_codepage, buf, 2);
  3044. } else {
  3045. char c = (unsigned char) wParam;
  3046. term_seen_key_event(wgs->term);
  3047. term_keyinput(wgs->term, wgs->kbd_codepage, &c, 1);
  3048. }
  3049. return (0);
  3050. case WM_CHAR:
  3051. case WM_SYSCHAR:
  3052. /*
  3053. * Nevertheless, we are prepared to deal with WM_CHAR
  3054. * messages, should they crop up. So if someone wants to
  3055. * post the things to us as part of a macro manoeuvre,
  3056. * we're ready to cope.
  3057. */
  3058. if (unicode_window) {
  3059. wchar_t c = wParam;
  3060. if (IS_HIGH_SURROGATE(c)) {
  3061. wgs->pending_surrogate = c;
  3062. } else if (IS_SURROGATE_PAIR(wgs->pending_surrogate, c)) {
  3063. wchar_t pair[2];
  3064. pair[0] = wgs->pending_surrogate;
  3065. pair[1] = c;
  3066. term_keyinputw(wgs->term, pair, 2);
  3067. } else if (!IS_SURROGATE(c)) {
  3068. term_keyinputw(wgs->term, &c, 1);
  3069. }
  3070. } else {
  3071. char c = (unsigned char)wParam;
  3072. term_seen_key_event(wgs->term);
  3073. if (wgs->ldisc)
  3074. term_keyinput(wgs->term, CP_ACP, &c, 1);
  3075. }
  3076. return 0;
  3077. case WM_SYSCOLORCHANGE:
  3078. if (conf_get_bool(wgs->conf, CONF_system_colour)) {
  3079. /* Refresh palette from system colours. */
  3080. term_notify_palette_changed(wgs->term);
  3081. init_palette(wgs);
  3082. /* Force a repaint of the terminal window. */
  3083. term_invalidate(wgs->term);
  3084. }
  3085. break;
  3086. case WM_GOT_CLIPDATA:
  3087. process_clipdata(wgs, (HGLOBAL)lParam, wParam);
  3088. return 0;
  3089. default:
  3090. if (message == wm_mousewheel || message == WM_MOUSEWHEEL
  3091. || message == WM_MOUSEHWHEEL) {
  3092. bool shift_pressed = false, control_pressed = false;
  3093. if (message == WM_MOUSEWHEEL || message == WM_MOUSEHWHEEL) {
  3094. wgs->wheel_accumulator += (short)HIWORD(wParam);
  3095. shift_pressed=LOWORD(wParam) & MK_SHIFT;
  3096. control_pressed=LOWORD(wParam) & MK_CONTROL;
  3097. } else {
  3098. BYTE keys[256];
  3099. wgs->wheel_accumulator += (int)wParam;
  3100. if (GetKeyboardState(keys)!=0) {
  3101. shift_pressed=keys[VK_SHIFT]&0x80;
  3102. control_pressed=keys[VK_CONTROL]&0x80;
  3103. }
  3104. }
  3105. /* process events when the threshold is reached */
  3106. while (abs(wgs->wheel_accumulator) >= WHEEL_DELTA) {
  3107. int b;
  3108. /* reduce amount for next time */
  3109. if (wgs->wheel_accumulator > 0) {
  3110. b = message == WM_MOUSEHWHEEL ? MBT_WHEEL_RIGHT : MBT_WHEEL_UP;
  3111. wgs->wheel_accumulator -= WHEEL_DELTA;
  3112. } else if (wgs->wheel_accumulator < 0) {
  3113. b = message == WM_MOUSEHWHEEL ? MBT_WHEEL_LEFT : MBT_WHEEL_DOWN;
  3114. wgs->wheel_accumulator += WHEEL_DELTA;
  3115. } else
  3116. break;
  3117. if (wgs->send_raw_mouse &&
  3118. !(conf_get_bool(wgs->conf, CONF_mouse_override) &&
  3119. shift_pressed)) {
  3120. /* Mouse wheel position is in screen coordinates for
  3121. * some reason */
  3122. POINT p;
  3123. p.x = X_POS(lParam); p.y = Y_POS(lParam);
  3124. if (ScreenToClient(hwnd, &p)) {
  3125. /* send a mouse-down followed by a mouse up */
  3126. term_mouse(wgs->term, b, translate_button(wgs, b),
  3127. MA_CLICK,
  3128. TO_CHR_X(p.x),
  3129. TO_CHR_Y(p.y), shift_pressed,
  3130. control_pressed, is_alt_pressed());
  3131. } /* else: not sure when this can fail */
  3132. } else if (message != WM_MOUSEHWHEEL) {
  3133. /* trigger a scroll */
  3134. term_scroll(wgs->term, 0,
  3135. b == MBT_WHEEL_UP ?
  3136. -wgs->term->rows / 2 : wgs->term->rows / 2);
  3137. }
  3138. }
  3139. return 0;
  3140. }
  3141. }
  3142. /*
  3143. * Any messages we don't process completely above are passed through to
  3144. * DefWindowProc() for default processing.
  3145. */
  3146. return sw_DefWindowProc(hwnd, message, wParam, lParam);
  3147. }
  3148. /*
  3149. * Move the system caret. (We maintain one, even though it's
  3150. * invisible, for the benefit of blind people: apparently some
  3151. * helper software tracks the system caret, so we should arrange to
  3152. * have one.)
  3153. */
  3154. static void wintw_set_cursor_pos(TermWin *tw, int x, int y)
  3155. {
  3156. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  3157. int cx, cy;
  3158. if (!wgs->term->has_focus) return;
  3159. /*
  3160. * Avoid gratuitously re-updating the cursor position and IMM
  3161. * window if there's no actual change required.
  3162. */
  3163. cx = x * wgs->font_width + wgs->offset_width;
  3164. cy = y * wgs->font_height + wgs->offset_height;
  3165. if (cx == wgs->caret_x && cy == wgs->caret_y)
  3166. return;
  3167. wgs->caret_x = cx;
  3168. wgs->caret_y = cy;
  3169. sys_cursor_update(wgs);
  3170. }
  3171. static void sys_cursor_update(WinGuiSeat *wgs)
  3172. {
  3173. COMPOSITIONFORM cf;
  3174. HIMC hIMC;
  3175. if (!wgs->term->has_focus) return;
  3176. if (wgs->caret_x < 0 || wgs->caret_y < 0)
  3177. return;
  3178. SetCaretPos(wgs->caret_x, wgs->caret_y);
  3179. /* IMM calls on Win98 and beyond only */
  3180. if (osPlatformId == VER_PLATFORM_WIN32s) return; /* 3.11 */
  3181. if (osPlatformId == VER_PLATFORM_WIN32_WINDOWS &&
  3182. osMinorVersion == 0) return; /* 95 */
  3183. /* we should have the IMM functions */
  3184. hIMC = ImmGetContext(wgs->term_hwnd);
  3185. cf.dwStyle = CFS_POINT;
  3186. cf.ptCurrentPos.x = wgs->caret_x;
  3187. cf.ptCurrentPos.y = wgs->caret_y;
  3188. ImmSetCompositionWindow(hIMC, &cf);
  3189. ImmReleaseContext(wgs->term_hwnd, hIMC);
  3190. }
  3191. static void draw_horizontal_line_on_text(
  3192. WinGuiSeat *wgs, int y, int lattr, RECT line_box, COLORREF colour)
  3193. {
  3194. if (lattr == LATTR_TOP || lattr == LATTR_BOT) {
  3195. y *= 2;
  3196. if (lattr == LATTR_BOT)
  3197. y -= wgs->font_height;
  3198. }
  3199. if (!(0 <= y && y < wgs->font_height))
  3200. return;
  3201. HPEN oldpen = SelectObject(wgs->wintw_hdc, CreatePen(PS_SOLID, 0, colour));
  3202. MoveToEx(wgs->wintw_hdc, line_box.left, line_box.top + y, NULL);
  3203. LineTo(wgs->wintw_hdc, line_box.right, line_box.top + y);
  3204. oldpen = SelectObject(wgs->wintw_hdc, oldpen);
  3205. DeleteObject(oldpen);
  3206. }
  3207. /*
  3208. * Draw a line of text in the window, at given character
  3209. * coordinates, in given attributes.
  3210. *
  3211. * We are allowed to fiddle with the contents of `text'.
  3212. */
  3213. static void do_text_internal(
  3214. WinGuiSeat *wgs, int x, int y, wchar_t *text, int len,
  3215. unsigned long attr, int lattr, truecolour truecolour)
  3216. {
  3217. COLORREF fg, bg, t;
  3218. int nfg, nbg, nfont;
  3219. RECT line_box;
  3220. bool force_manual_underline = false;
  3221. int fnt_width, char_width;
  3222. int text_adjust = 0;
  3223. int xoffset = 0;
  3224. int maxlen, remaining;
  3225. bool opaque;
  3226. bool is_cursor = false;
  3227. int *lpDx = NULL;
  3228. size_t lpDx_len = 0;
  3229. bool use_lpDx;
  3230. wchar_t *wbuf = NULL;
  3231. char *cbuf = NULL;
  3232. size_t wbuflen = 0, cbuflen = 0;
  3233. int len2; /* for SURROGATE PAIR */
  3234. lattr &= LATTR_MODE;
  3235. char_width = fnt_width = wgs->font_width * (1 + (lattr != LATTR_NORM));
  3236. if (attr & ATTR_WIDE)
  3237. char_width *= 2;
  3238. /* Only want the left half of double width lines */
  3239. if (lattr != LATTR_NORM && x*2 >= wgs->term->cols)
  3240. return;
  3241. x *= fnt_width;
  3242. y *= wgs->font_height;
  3243. x += wgs->offset_width;
  3244. y += wgs->offset_height;
  3245. if ((attr & TATTR_ACTCURS) &&
  3246. (wgs->cursor_type == CURSOR_BLOCK || wgs->term->big_cursor)) {
  3247. truecolour.fg = truecolour.bg = optionalrgb_none;
  3248. attr &= ~(ATTR_REVERSE|ATTR_BLINK|ATTR_COLOURS|ATTR_DIM);
  3249. /* cursor fg and bg */
  3250. attr |= (260 << ATTR_FGSHIFT) | (261 << ATTR_BGSHIFT);
  3251. is_cursor = true;
  3252. }
  3253. nfont = 0;
  3254. if (wgs->vtmode == VT_POORMAN && lattr != LATTR_NORM) {
  3255. /* Assume a poorman font is borken in other ways too. */
  3256. lattr = LATTR_WIDE;
  3257. } else
  3258. switch (lattr) {
  3259. case LATTR_NORM:
  3260. break;
  3261. case LATTR_WIDE:
  3262. nfont |= FONT_WIDE;
  3263. break;
  3264. default:
  3265. nfont |= FONT_WIDE + FONT_HIGH;
  3266. break;
  3267. }
  3268. if (attr & ATTR_NARROW)
  3269. nfont |= FONT_NARROW;
  3270. #ifdef USES_VTLINE_HACK
  3271. /* Special hack for the VT100 linedraw glyphs. */
  3272. if (text[0] >= 0x23BA && text[0] <= 0x23BD) {
  3273. switch ((unsigned char) (text[0])) {
  3274. case 0xBA:
  3275. text_adjust = -2 * wgs->font_height / 5;
  3276. break;
  3277. case 0xBB:
  3278. text_adjust = -1 * wgs->font_height / 5;
  3279. break;
  3280. case 0xBC:
  3281. text_adjust = wgs->font_height / 5;
  3282. break;
  3283. case 0xBD:
  3284. text_adjust = 2 * wgs->font_height / 5;
  3285. break;
  3286. }
  3287. if (lattr == LATTR_TOP || lattr == LATTR_BOT)
  3288. text_adjust *= 2;
  3289. text[0] = wgs->ucsdata.unitab_xterm['q'];
  3290. if (attr & ATTR_UNDER) {
  3291. attr &= ~ATTR_UNDER;
  3292. force_manual_underline = true;
  3293. }
  3294. }
  3295. #endif
  3296. /* Anything left as an original character set is unprintable. */
  3297. if (DIRECT_CHAR(text[0]) &&
  3298. (len < 2 || !IS_SURROGATE_PAIR(text[0], text[1]))) {
  3299. int i;
  3300. for (i = 0; i < len; i++)
  3301. text[i] = 0xFFFD;
  3302. }
  3303. /* OEM CP */
  3304. if ((text[0] & CSET_MASK) == CSET_OEMCP)
  3305. nfont |= FONT_OEM;
  3306. nfg = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
  3307. nbg = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
  3308. if (wgs->bold_font_mode == BOLD_FONT && (attr & ATTR_BOLD))
  3309. nfont |= FONT_BOLD;
  3310. if (wgs->und_mode == UND_FONT && (attr & ATTR_UNDER))
  3311. nfont |= FONT_UNDERLINE;
  3312. another_font(wgs, nfont);
  3313. if (!wgs->fonts[nfont]) {
  3314. if (nfont & FONT_UNDERLINE)
  3315. force_manual_underline = true;
  3316. /* Don't do the same for manual bold, it could be bad news. */
  3317. nfont &= ~(FONT_BOLD | FONT_UNDERLINE);
  3318. }
  3319. another_font(wgs, nfont);
  3320. if (!wgs->fonts[nfont])
  3321. nfont = FONT_NORMAL;
  3322. if (attr & ATTR_REVERSE) {
  3323. struct optionalrgb trgb;
  3324. t = nfg;
  3325. nfg = nbg;
  3326. nbg = t;
  3327. trgb = truecolour.fg;
  3328. truecolour.fg = truecolour.bg;
  3329. truecolour.bg = trgb;
  3330. }
  3331. if (wgs->bold_colours && (attr & ATTR_BOLD) && !is_cursor) {
  3332. if (nfg < 16) nfg |= 8;
  3333. else if (nfg >= 256) nfg |= 1;
  3334. }
  3335. if (wgs->bold_colours && (attr & ATTR_BLINK)) {
  3336. if (nbg < 16) nbg |= 8;
  3337. else if (nbg >= 256) nbg |= 1;
  3338. }
  3339. if (!wgs->pal && truecolour.fg.enabled)
  3340. fg = RGB(truecolour.fg.r, truecolour.fg.g, truecolour.fg.b);
  3341. else
  3342. fg = wgs->colours[nfg];
  3343. if (!wgs->pal && truecolour.bg.enabled)
  3344. bg = RGB(truecolour.bg.r, truecolour.bg.g, truecolour.bg.b);
  3345. else
  3346. bg = wgs->colours[nbg];
  3347. if (!wgs->pal && (attr & ATTR_DIM)) {
  3348. fg = RGB(GetRValue(fg) * 2 / 3,
  3349. GetGValue(fg) * 2 / 3,
  3350. GetBValue(fg) * 2 / 3);
  3351. }
  3352. SelectObject(wgs->wintw_hdc, wgs->fonts[nfont]);
  3353. SetTextColor(wgs->wintw_hdc, fg);
  3354. SetBkColor(wgs->wintw_hdc, bg);
  3355. if (attr & TATTR_COMBINING)
  3356. SetBkMode(wgs->wintw_hdc, TRANSPARENT);
  3357. else
  3358. SetBkMode(wgs->wintw_hdc, OPAQUE);
  3359. line_box.left = x;
  3360. line_box.top = y;
  3361. line_box.right = x + char_width * len;
  3362. line_box.bottom = y + wgs->font_height;
  3363. /* adjust line_box.right for SURROGATE PAIR & VARIATION SELECTOR */
  3364. {
  3365. int i;
  3366. int rc_width = 0;
  3367. for (i = 0; i < len ; i++) {
  3368. if (i+1 < len && IS_HIGH_VARSEL(text[i], text[i+1])) {
  3369. i++;
  3370. } else if (i+1 < len && IS_SURROGATE_PAIR(text[i], text[i+1])) {
  3371. rc_width += char_width;
  3372. i++;
  3373. } else if (IS_LOW_VARSEL(text[i])) {
  3374. /* do nothing */
  3375. } else {
  3376. rc_width += char_width;
  3377. }
  3378. }
  3379. line_box.right = line_box.left + rc_width;
  3380. }
  3381. /* Only want the left half of double width lines */
  3382. if (line_box.right > wgs->font_width*wgs->term->cols+wgs->offset_width)
  3383. line_box.right = wgs->font_width*wgs->term->cols+wgs->offset_width;
  3384. if (wgs->font_varpitch) {
  3385. /*
  3386. * If we're using a variable-pitch font, we unconditionally
  3387. * draw the glyphs one at a time and centre them in their
  3388. * character cells (which means in particular that we must
  3389. * disable the lpDx mechanism). This gives slightly odd but
  3390. * generally reasonable results.
  3391. */
  3392. xoffset = char_width / 2;
  3393. SetTextAlign(wgs->wintw_hdc, TA_TOP | TA_CENTER | TA_NOUPDATECP);
  3394. use_lpDx = false;
  3395. maxlen = 1;
  3396. } else {
  3397. /*
  3398. * In a fixed-pitch font, we draw the whole string in one go
  3399. * in the normal way.
  3400. */
  3401. xoffset = 0;
  3402. SetTextAlign(wgs->wintw_hdc, TA_TOP | TA_LEFT | TA_NOUPDATECP);
  3403. use_lpDx = true;
  3404. maxlen = len;
  3405. }
  3406. opaque = true; /* start by erasing the rectangle */
  3407. for (remaining = len; remaining > 0;
  3408. text += len, remaining -= len, x += char_width * len2) {
  3409. len = (maxlen < remaining ? maxlen : remaining);
  3410. /* don't divide SURROGATE PAIR and VARIATION SELECTOR */
  3411. len2 = len;
  3412. if (maxlen == 1) {
  3413. if (remaining >= 1 && IS_SURROGATE_PAIR(text[0], text[1]))
  3414. len++;
  3415. if (remaining-len >= 1 && IS_LOW_VARSEL(text[len]))
  3416. len++;
  3417. else if (remaining-len >= 2 &&
  3418. IS_HIGH_VARSEL(text[len], text[len+1]))
  3419. len += 2;
  3420. }
  3421. if (len > lpDx_len)
  3422. sgrowarray(lpDx, lpDx_len, len);
  3423. {
  3424. int i;
  3425. /* only last char has dx width in SURROGATE PAIR and
  3426. * VARIATION sequence */
  3427. for (i = 0; i < len; i++) {
  3428. lpDx[i] = char_width;
  3429. if (i+1 < len && IS_HIGH_VARSEL(text[i], text[i+1])) {
  3430. if (i > 0) lpDx[i-1] = 0;
  3431. lpDx[i] = 0;
  3432. i++;
  3433. lpDx[i] = char_width;
  3434. } else if (i+1 < len && IS_SURROGATE_PAIR(text[i],text[i+1])) {
  3435. lpDx[i] = 0;
  3436. i++;
  3437. lpDx[i] = char_width;
  3438. } else if (IS_LOW_VARSEL(text[i])) {
  3439. if (i > 0) lpDx[i-1] = 0;
  3440. lpDx[i] = char_width;
  3441. }
  3442. }
  3443. }
  3444. /* We're using a private area for direct to font. (512 chars.) */
  3445. if (wgs->ucsdata.dbcs_screenfont &&
  3446. (text[0] & CSET_MASK) == CSET_ACP) {
  3447. /* Ho Hum, dbcs fonts are a PITA! */
  3448. /* To display on W9x I have to convert to UCS */
  3449. int nlen, mptr;
  3450. sgrowarray(wbuf, wbuflen, len);
  3451. for (nlen = mptr = 0; mptr<len; mptr++) {
  3452. wbuf[nlen] = 0xFFFD;
  3453. if (IsDBCSLeadByteEx(wgs->ucsdata.font_codepage,
  3454. (BYTE) text[mptr])) {
  3455. char dbcstext[2];
  3456. dbcstext[0] = text[mptr] & 0xFF;
  3457. dbcstext[1] = text[mptr+1] & 0xFF;
  3458. lpDx[nlen] += char_width;
  3459. MultiByteToWideChar(
  3460. wgs->ucsdata.font_codepage, MB_USEGLYPHCHARS,
  3461. dbcstext, 2, wbuf+nlen, 1);
  3462. mptr++;
  3463. } else {
  3464. char dbcstext[1];
  3465. dbcstext[0] = text[mptr] & 0xFF;
  3466. MultiByteToWideChar(
  3467. wgs->ucsdata.font_codepage, MB_USEGLYPHCHARS,
  3468. dbcstext, 1, wbuf+nlen, 1);
  3469. }
  3470. nlen++;
  3471. }
  3472. if (nlen <= 0)
  3473. goto out; /* Eeek! */
  3474. ExtTextOutW(
  3475. wgs->wintw_hdc, x + xoffset,
  3476. y - wgs->font_height * (lattr == LATTR_BOT) + text_adjust,
  3477. ETO_CLIPPED | (opaque ? ETO_OPAQUE : 0),
  3478. &line_box, wbuf, nlen, (use_lpDx ? lpDx : NULL));
  3479. if (wgs->bold_font_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
  3480. SetBkMode(wgs->wintw_hdc, TRANSPARENT);
  3481. ExtTextOutW(
  3482. wgs->wintw_hdc, x + xoffset - 1,
  3483. y - wgs->font_height * (lattr == LATTR_BOT) + text_adjust,
  3484. ETO_CLIPPED, &line_box, wbuf, nlen,
  3485. (use_lpDx ? lpDx : NULL));
  3486. }
  3487. lpDx[0] = -1;
  3488. } else if (DIRECT_FONT(text[0])) {
  3489. sgrowarray(cbuf, cbuflen, len);
  3490. for (size_t i = 0; i < len; i++)
  3491. cbuf[i] = text[i] & 0xFF;
  3492. ExtTextOut(
  3493. wgs->wintw_hdc, x + xoffset,
  3494. y - wgs->font_height * (lattr == LATTR_BOT) + text_adjust,
  3495. ETO_CLIPPED | (opaque ? ETO_OPAQUE : 0),
  3496. &line_box, cbuf, len, (use_lpDx ? lpDx : NULL));
  3497. if (wgs->bold_font_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
  3498. SetBkMode(wgs->wintw_hdc, TRANSPARENT);
  3499. /* GRR: This draws the character outside its box and
  3500. * can leave 'droppings' even with the clip box! I
  3501. * suppose I could loop it one character at a time ...
  3502. * yuk.
  3503. *
  3504. * Or ... I could do a test print with "W", and use +1
  3505. * or -1 for this shift depending on if the leftmost
  3506. * column is blank...
  3507. */
  3508. ExtTextOut(
  3509. wgs->wintw_hdc, x + xoffset - 1,
  3510. y - wgs->font_height * (lattr == LATTR_BOT) + text_adjust,
  3511. ETO_CLIPPED, &line_box, cbuf, len,
  3512. (use_lpDx ? lpDx : NULL));
  3513. }
  3514. } else {
  3515. /* And 'normal' unicode characters */
  3516. sgrowarray(wbuf, wbuflen, len);
  3517. for (int i = 0; i < len; i++)
  3518. wbuf[i] = text[i];
  3519. /* print Glyphs as they are, without Windows' Shaping*/
  3520. general_textout(
  3521. wgs, wgs->wintw_hdc, x + xoffset,
  3522. y - wgs->font_height * (lattr==LATTR_BOT) + text_adjust,
  3523. &line_box, wbuf, len, lpDx,
  3524. opaque && !(attr & TATTR_COMBINING));
  3525. /* And the shadow bold hack. */
  3526. if (wgs->bold_font_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
  3527. SetBkMode(wgs->wintw_hdc, TRANSPARENT);
  3528. ExtTextOutW(
  3529. wgs->wintw_hdc, x + xoffset - 1,
  3530. y - wgs->font_height * (lattr == LATTR_BOT) + text_adjust,
  3531. ETO_CLIPPED, &line_box, wbuf, len,
  3532. (use_lpDx ? lpDx : NULL));
  3533. }
  3534. }
  3535. /*
  3536. * If we're looping round again, stop erasing the background
  3537. * rectangle.
  3538. */
  3539. SetBkMode(wgs->wintw_hdc, TRANSPARENT);
  3540. opaque = false;
  3541. }
  3542. if (lattr != LATTR_TOP &&
  3543. (force_manual_underline || (wgs->und_mode == UND_LINE &&
  3544. (attr & ATTR_UNDER))))
  3545. draw_horizontal_line_on_text(wgs, wgs->descent, lattr, line_box, fg);
  3546. if (attr & ATTR_STRIKE)
  3547. draw_horizontal_line_on_text(wgs, wgs->font_strikethrough_y, lattr,
  3548. line_box, fg);
  3549. out:
  3550. sfree(lpDx);
  3551. sfree(wbuf);
  3552. sfree(cbuf);
  3553. }
  3554. /*
  3555. * Wrapper that handles combining characters.
  3556. */
  3557. static void wintw_draw_text(
  3558. TermWin *tw, int x, int y, wchar_t *text, int len,
  3559. unsigned long attr, int lattr, truecolour truecolour)
  3560. {
  3561. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  3562. if (attr & TATTR_COMBINING) {
  3563. unsigned long a = 0;
  3564. int len0 = 1;
  3565. /* don't divide SURROGATE PAIR and VARIATION SELECTOR */
  3566. if (len >= 2 && IS_SURROGATE_PAIR(text[0], text[1]))
  3567. len0 = 2;
  3568. if (len-len0 >= 1 && IS_LOW_VARSEL(text[len0])) {
  3569. attr &= ~TATTR_COMBINING;
  3570. do_text_internal(wgs, x, y, text, len0+1, attr, lattr, truecolour);
  3571. text += len0+1;
  3572. len -= len0+1;
  3573. a = TATTR_COMBINING;
  3574. } else if (len-len0 >= 2 && IS_HIGH_VARSEL(text[len0], text[len0+1])) {
  3575. attr &= ~TATTR_COMBINING;
  3576. do_text_internal(wgs, x, y, text, len0+2, attr, lattr, truecolour);
  3577. text += len0+2;
  3578. len -= len0+2;
  3579. a = TATTR_COMBINING;
  3580. } else {
  3581. attr &= ~TATTR_COMBINING;
  3582. }
  3583. while (len--) {
  3584. if (len >= 1 && IS_SURROGATE_PAIR(text[0], text[1])) {
  3585. do_text_internal(wgs, x, y, text, 2, attr | a, lattr,
  3586. truecolour);
  3587. len--;
  3588. text++;
  3589. } else
  3590. do_text_internal(wgs, x, y, text, 1, attr | a, lattr,
  3591. truecolour);
  3592. text++;
  3593. a = TATTR_COMBINING;
  3594. }
  3595. } else
  3596. do_text_internal(wgs, x, y, text, len, attr, lattr, truecolour);
  3597. }
  3598. static void wintw_draw_cursor(
  3599. TermWin *tw, int x, int y, wchar_t *text, int len,
  3600. unsigned long attr, int lattr, truecolour truecolour)
  3601. {
  3602. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  3603. int fnt_width;
  3604. int char_width;
  3605. int ctype = wgs->cursor_type;
  3606. lattr &= LATTR_MODE;
  3607. if ((attr & TATTR_ACTCURS) &&
  3608. (ctype == CURSOR_BLOCK || wgs->term->big_cursor)) {
  3609. if (*text != UCSWIDE) {
  3610. win_draw_text(tw, x, y, text, len, attr, lattr, truecolour);
  3611. return;
  3612. }
  3613. ctype = CURSOR_VERTICAL_LINE;
  3614. attr |= TATTR_RIGHTCURS;
  3615. }
  3616. fnt_width = char_width = wgs->font_width * (1 + (lattr != LATTR_NORM));
  3617. if (attr & ATTR_WIDE)
  3618. char_width *= 2;
  3619. x *= fnt_width;
  3620. y *= wgs->font_height;
  3621. x += wgs->offset_width;
  3622. y += wgs->offset_height;
  3623. if ((attr & TATTR_PASCURS) &&
  3624. (ctype == CURSOR_BLOCK || wgs->term->big_cursor)) {
  3625. POINT pts[5];
  3626. HPEN oldpen;
  3627. pts[0].x = pts[1].x = pts[4].x = x;
  3628. pts[2].x = pts[3].x = x + char_width - 1;
  3629. pts[0].y = pts[3].y = pts[4].y = y;
  3630. pts[1].y = pts[2].y = y + wgs->font_height - 1;
  3631. oldpen = SelectObject(wgs->wintw_hdc,
  3632. CreatePen(PS_SOLID, 0, wgs->colours[261]));
  3633. Polyline(wgs->wintw_hdc, pts, 5);
  3634. oldpen = SelectObject(wgs->wintw_hdc, oldpen);
  3635. DeleteObject(oldpen);
  3636. } else if ((attr & (TATTR_ACTCURS | TATTR_PASCURS)) &&
  3637. ctype != CURSOR_BLOCK) {
  3638. int startx, starty, dx, dy, length, i;
  3639. if (ctype == CURSOR_UNDERLINE) {
  3640. startx = x;
  3641. starty = y + wgs->descent;
  3642. dx = 1;
  3643. dy = 0;
  3644. length = char_width;
  3645. } else /* ctype == CURSOR_VERTICAL_LINE */ {
  3646. int xadjust = 0;
  3647. if (attr & TATTR_RIGHTCURS)
  3648. xadjust = char_width - 1;
  3649. startx = x + xadjust;
  3650. starty = y;
  3651. dx = 0;
  3652. dy = 1;
  3653. length = wgs->font_height;
  3654. }
  3655. if (attr & TATTR_ACTCURS) {
  3656. HPEN oldpen;
  3657. oldpen =
  3658. SelectObject(wgs->wintw_hdc,
  3659. CreatePen(PS_SOLID, 0, wgs->colours[261]));
  3660. MoveToEx(wgs->wintw_hdc, startx, starty, NULL);
  3661. LineTo(wgs->wintw_hdc, startx + dx * length, starty + dy * length);
  3662. oldpen = SelectObject(wgs->wintw_hdc, oldpen);
  3663. DeleteObject(oldpen);
  3664. } else {
  3665. for (i = 0; i < length; i++) {
  3666. if (i % 2 == 0) {
  3667. SetPixel(wgs->wintw_hdc, startx, starty,
  3668. wgs->colours[261]);
  3669. }
  3670. startx += dx;
  3671. starty += dy;
  3672. }
  3673. }
  3674. }
  3675. }
  3676. static void wintw_draw_trust_sigil(TermWin *tw, int x, int y)
  3677. {
  3678. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  3679. x *= wgs->font_width;
  3680. y *= wgs->font_height;
  3681. x += wgs->offset_width;
  3682. y += wgs->offset_height;
  3683. DrawIconEx(wgs->wintw_hdc, x, y, trust_icon,
  3684. wgs->font_width * 2, wgs->font_height, 0, NULL, DI_NORMAL);
  3685. }
  3686. /* This function gets the actual width of a character in the normal font.
  3687. */
  3688. static int wintw_char_width(TermWin *tw, int uc)
  3689. {
  3690. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  3691. int ibuf = 0;
  3692. /* If the font max is the same as the font ave width then this
  3693. * function is a no-op.
  3694. */
  3695. if (!wgs->font_dualwidth) return 1;
  3696. switch (uc & CSET_MASK) {
  3697. case CSET_ASCII:
  3698. uc = wgs->ucsdata.unitab_line[uc & 0xFF];
  3699. break;
  3700. case CSET_LINEDRW:
  3701. uc = wgs->ucsdata.unitab_xterm[uc & 0xFF];
  3702. break;
  3703. case CSET_SCOACS:
  3704. uc = wgs->ucsdata.unitab_scoacs[uc & 0xFF];
  3705. break;
  3706. }
  3707. if (DIRECT_FONT(uc)) {
  3708. if (wgs->ucsdata.dbcs_screenfont) return 1;
  3709. /* Speedup, I know of no font where ascii is the wrong width */
  3710. if ((uc&~CSET_MASK) >= ' ' && (uc&~CSET_MASK)<= '~')
  3711. return 1;
  3712. if ( (uc & CSET_MASK) == CSET_ACP ) {
  3713. SelectObject(wgs->wintw_hdc, wgs->fonts[FONT_NORMAL]);
  3714. } else if ( (uc & CSET_MASK) == CSET_OEMCP ) {
  3715. another_font(wgs, FONT_OEM);
  3716. if (!wgs->fonts[FONT_OEM]) return 0;
  3717. SelectObject(wgs->wintw_hdc, wgs->fonts[FONT_OEM]);
  3718. } else
  3719. return 0;
  3720. if (GetCharWidth32(wgs->wintw_hdc, uc & ~CSET_MASK,
  3721. uc & ~CSET_MASK, &ibuf) != 1 &&
  3722. GetCharWidth(wgs->wintw_hdc, uc & ~CSET_MASK,
  3723. uc & ~CSET_MASK, &ibuf) != 1)
  3724. return 0;
  3725. } else {
  3726. /* Speedup, I know of no font where ascii is the wrong width */
  3727. if (uc >= ' ' && uc <= '~') return 1;
  3728. SelectObject(wgs->wintw_hdc, wgs->fonts[FONT_NORMAL]);
  3729. if (GetCharWidth32W(wgs->wintw_hdc, uc, uc, &ibuf) == 1)
  3730. /* Okay that one worked */ ;
  3731. else if (GetCharWidthW(wgs->wintw_hdc, uc, uc, &ibuf) == 1)
  3732. /* This should work on 9x too, but it's "less accurate" */ ;
  3733. else
  3734. return 0;
  3735. }
  3736. ibuf += wgs->font_width / 2 -1;
  3737. ibuf /= wgs->font_width;
  3738. return ibuf;
  3739. }
  3740. DECL_WINDOWS_FUNCTION(static, BOOL, FlashWindowEx, (PFLASHWINFO));
  3741. DECL_WINDOWS_FUNCTION(static, BOOL, ToUnicodeEx,
  3742. (UINT, UINT, const BYTE *, LPWSTR, int, UINT, HKL));
  3743. DECL_WINDOWS_FUNCTION(static, BOOL, PlaySoundW, (LPCWSTR, HMODULE, DWORD));
  3744. DECL_WINDOWS_FUNCTION(static, BOOL, PlaySoundA, (LPCSTR, HMODULE, DWORD));
  3745. static void init_winfuncs(void)
  3746. {
  3747. HMODULE user32_module = load_system32_dll("user32.dll");
  3748. HMODULE winmm_module = load_system32_dll("winmm.dll");
  3749. HMODULE shcore_module = load_system32_dll("shcore.dll");
  3750. GET_WINDOWS_FUNCTION(user32_module, FlashWindowEx);
  3751. GET_WINDOWS_FUNCTION(user32_module, ToUnicodeEx);
  3752. GET_WINDOWS_FUNCTION(winmm_module, PlaySoundW);
  3753. GET_WINDOWS_FUNCTION(winmm_module, PlaySoundA);
  3754. GET_WINDOWS_FUNCTION_NO_TYPECHECK(user32_module, GetMonitorInfoA);
  3755. GET_WINDOWS_FUNCTION_NO_TYPECHECK(user32_module, MonitorFromPoint);
  3756. GET_WINDOWS_FUNCTION_NO_TYPECHECK(user32_module, MonitorFromWindow);
  3757. GET_WINDOWS_FUNCTION_NO_TYPECHECK(shcore_module, GetDpiForMonitor);
  3758. GET_WINDOWS_FUNCTION_NO_TYPECHECK(user32_module, GetSystemMetricsForDpi);
  3759. GET_WINDOWS_FUNCTION_NO_TYPECHECK(user32_module, AdjustWindowRectExForDpi);
  3760. }
  3761. /*
  3762. * Translate a WM_(SYS)?KEY(UP|DOWN) message into a string of ASCII
  3763. * codes. Returns number of bytes used, zero to drop the message,
  3764. * -1 to forward the message to Windows, or another negative number
  3765. * to indicate a NUL-terminated "special" string.
  3766. */
  3767. static int TranslateKey(WinGuiSeat *wgs, UINT message, WPARAM wParam,
  3768. LPARAM lParam, unsigned char *output)
  3769. {
  3770. BYTE keystate[256];
  3771. int scan, shift_state;
  3772. bool left_alt = false, key_down;
  3773. int r, i;
  3774. unsigned char *p = output;
  3775. int funky_type = conf_get_int(wgs->conf, CONF_funky_type);
  3776. bool no_applic_k = conf_get_bool(wgs->conf, CONF_no_applic_k);
  3777. bool ctrlaltkeys = conf_get_bool(wgs->conf, CONF_ctrlaltkeys);
  3778. bool nethack_keypad = conf_get_bool(wgs->conf, CONF_nethack_keypad);
  3779. char keypad_key = '\0';
  3780. HKL kbd_layout = GetKeyboardLayout(0);
  3781. r = GetKeyboardState(keystate);
  3782. if (!r)
  3783. memset(keystate, 0, sizeof(keystate));
  3784. else {
  3785. #if 0
  3786. #define SHOW_TOASCII_RESULT
  3787. { /* Tell us all about key events */
  3788. static BYTE oldstate[256];
  3789. static int first = 1;
  3790. static int scan;
  3791. int ch;
  3792. if (first)
  3793. memcpy(oldstate, keystate, sizeof(oldstate));
  3794. first = 0;
  3795. if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT) {
  3796. debug("+");
  3797. } else if ((HIWORD(lParam) & KF_UP)
  3798. && scan == (HIWORD(lParam) & 0xFF)) {
  3799. debug(". U");
  3800. } else {
  3801. debug(".\n");
  3802. if (wParam >= VK_F1 && wParam <= VK_F20)
  3803. debug("K_F%d", wParam + 1 - VK_F1);
  3804. else
  3805. switch (wParam) {
  3806. case VK_SHIFT:
  3807. debug("SHIFT");
  3808. break;
  3809. case VK_CONTROL:
  3810. debug("CTRL");
  3811. break;
  3812. case VK_MENU:
  3813. debug("ALT");
  3814. break;
  3815. default:
  3816. debug("VK_%02x", wParam);
  3817. }
  3818. if (message == WM_SYSKEYDOWN || message == WM_SYSKEYUP)
  3819. debug("*");
  3820. debug(", S%02x", scan = (HIWORD(lParam) & 0xFF));
  3821. ch = MapVirtualKeyEx(wParam, 2, kbd_layout);
  3822. if (ch >= ' ' && ch <= '~')
  3823. debug(", '%c'", ch);
  3824. else if (ch)
  3825. debug(", $%02x", ch);
  3826. if ((keystate[VK_SHIFT] & 0x80) != 0)
  3827. debug(", S");
  3828. if ((keystate[VK_CONTROL] & 0x80) != 0)
  3829. debug(", C");
  3830. if ((HIWORD(lParam) & KF_EXTENDED))
  3831. debug(", E");
  3832. if ((HIWORD(lParam) & KF_UP))
  3833. debug(", U");
  3834. }
  3835. if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT);
  3836. else if ((HIWORD(lParam) & KF_UP))
  3837. oldstate[wParam & 0xFF] ^= 0x80;
  3838. else
  3839. oldstate[wParam & 0xFF] ^= 0x81;
  3840. for (ch = 0; ch < 256; ch++)
  3841. if (oldstate[ch] != keystate[ch])
  3842. debug(", M%02x=%02x", ch, keystate[ch]);
  3843. memcpy(oldstate, keystate, sizeof(oldstate));
  3844. }
  3845. #endif
  3846. if (wParam == VK_MENU && (HIWORD(lParam) & KF_EXTENDED)) {
  3847. keystate[VK_RMENU] = keystate[VK_MENU];
  3848. }
  3849. /* Nastiness with NUMLock - Shift-NUMLock is left alone though */
  3850. if ((funky_type == FUNKY_VT400 ||
  3851. (funky_type <= FUNKY_LINUX && wgs->term->app_keypad_keys &&
  3852. !no_applic_k))
  3853. && wParam == VK_NUMLOCK && !(keystate[VK_SHIFT] & 0x80)) {
  3854. wParam = VK_EXECUTE;
  3855. /* UnToggle NUMLock */
  3856. if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == 0)
  3857. keystate[VK_NUMLOCK] ^= 1;
  3858. }
  3859. /* And write back the 'adjusted' state */
  3860. SetKeyboardState(keystate);
  3861. }
  3862. /* Disable Auto repeat if required */
  3863. if (wgs->term->repeat_off &&
  3864. (HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT)
  3865. return 0;
  3866. if ((HIWORD(lParam) & KF_ALTDOWN) && (keystate[VK_RMENU] & 0x80) == 0)
  3867. left_alt = true;
  3868. key_down = ((HIWORD(lParam) & KF_UP) == 0);
  3869. /* Make sure Ctrl-ALT is not the same as AltGr for ToAscii unless told. */
  3870. if (left_alt && (keystate[VK_CONTROL] & 0x80)) {
  3871. if (ctrlaltkeys)
  3872. keystate[VK_MENU] = 0;
  3873. else {
  3874. keystate[VK_RMENU] = 0x80;
  3875. left_alt = false;
  3876. }
  3877. }
  3878. scan = (HIWORD(lParam) & (KF_UP | KF_EXTENDED | 0xFF));
  3879. shift_state = ((keystate[VK_SHIFT] & 0x80) != 0)
  3880. + ((keystate[VK_CONTROL] & 0x80) != 0) * 2;
  3881. /* Note if AltGr was pressed and if it was used as a compose key */
  3882. if (!wgs->compose_state) {
  3883. wgs->compose_keycode = 0x100;
  3884. if (conf_get_bool(wgs->conf, CONF_compose_key)) {
  3885. if (wParam == VK_MENU && (HIWORD(lParam) & KF_EXTENDED))
  3886. wgs->compose_keycode = wParam;
  3887. }
  3888. if (wParam == VK_APPS)
  3889. wgs->compose_keycode = wParam;
  3890. }
  3891. if (wParam == wgs->compose_keycode) {
  3892. if (wgs->compose_state == 0 &&
  3893. (HIWORD(lParam) & (KF_UP | KF_REPEAT)) == 0)
  3894. wgs->compose_state = 1;
  3895. else if (wgs->compose_state == 1 && (HIWORD(lParam) & KF_UP))
  3896. wgs->compose_state = 2;
  3897. else
  3898. wgs->compose_state = 0;
  3899. } else if (wgs->compose_state == 1 && wParam != VK_CONTROL)
  3900. wgs->compose_state = 0;
  3901. if (wgs->compose_state > 1 && left_alt)
  3902. wgs->compose_state = 0;
  3903. /* Sanitize the number pad if not using a PC NumPad */
  3904. if (left_alt || (wgs->term->app_keypad_keys && !no_applic_k
  3905. && funky_type != FUNKY_XTERM) ||
  3906. funky_type == FUNKY_VT400 || nethack_keypad || wgs->compose_state) {
  3907. if ((HIWORD(lParam) & KF_EXTENDED) == 0) {
  3908. int nParam = 0;
  3909. switch (wParam) {
  3910. case VK_INSERT:
  3911. nParam = VK_NUMPAD0;
  3912. break;
  3913. case VK_END:
  3914. nParam = VK_NUMPAD1;
  3915. break;
  3916. case VK_DOWN:
  3917. nParam = VK_NUMPAD2;
  3918. break;
  3919. case VK_NEXT:
  3920. nParam = VK_NUMPAD3;
  3921. break;
  3922. case VK_LEFT:
  3923. nParam = VK_NUMPAD4;
  3924. break;
  3925. case VK_CLEAR:
  3926. nParam = VK_NUMPAD5;
  3927. break;
  3928. case VK_RIGHT:
  3929. nParam = VK_NUMPAD6;
  3930. break;
  3931. case VK_HOME:
  3932. nParam = VK_NUMPAD7;
  3933. break;
  3934. case VK_UP:
  3935. nParam = VK_NUMPAD8;
  3936. break;
  3937. case VK_PRIOR:
  3938. nParam = VK_NUMPAD9;
  3939. break;
  3940. case VK_DELETE:
  3941. nParam = VK_DECIMAL;
  3942. break;
  3943. }
  3944. if (nParam) {
  3945. if (keystate[VK_NUMLOCK] & 1)
  3946. shift_state |= 1;
  3947. wParam = nParam;
  3948. }
  3949. }
  3950. }
  3951. /* If a key is pressed and AltGr is not active */
  3952. if (key_down && (keystate[VK_RMENU] & 0x80) == 0 && !wgs->compose_state) {
  3953. /* Okay, prepare for most alts then ... */
  3954. if (left_alt)
  3955. *p++ = '\033';
  3956. /* Lets see if it's a pattern we know all about ... */
  3957. if (wParam == VK_PRIOR && shift_state == 1) {
  3958. SendMessage(wgs->term_hwnd, WM_VSCROLL, SB_PAGEUP, 0);
  3959. return 0;
  3960. }
  3961. if (wParam == VK_PRIOR && shift_state == 3) { /* ctrl-shift-pageup */
  3962. SendMessage(wgs->term_hwnd, WM_VSCROLL, SB_TOP, 0);
  3963. return 0;
  3964. }
  3965. if (wParam == VK_NEXT && shift_state == 3) { /* ctrl-shift-pagedown */
  3966. SendMessage(wgs->term_hwnd, WM_VSCROLL, SB_BOTTOM, 0);
  3967. return 0;
  3968. }
  3969. if (wParam == VK_PRIOR && shift_state == 2) {
  3970. SendMessage(wgs->term_hwnd, WM_VSCROLL, SB_LINEUP, 0);
  3971. return 0;
  3972. }
  3973. if (wParam == VK_NEXT && shift_state == 1) {
  3974. SendMessage(wgs->term_hwnd, WM_VSCROLL, SB_PAGEDOWN, 0);
  3975. return 0;
  3976. }
  3977. if (wParam == VK_NEXT && shift_state == 2) {
  3978. SendMessage(wgs->term_hwnd, WM_VSCROLL, SB_LINEDOWN, 0);
  3979. return 0;
  3980. }
  3981. if ((wParam == VK_PRIOR || wParam == VK_NEXT) && shift_state == 3) {
  3982. term_scroll_to_selection(wgs->term, (wParam == VK_PRIOR ? 0 : 1));
  3983. return 0;
  3984. }
  3985. if (wParam == VK_INSERT && shift_state == 2) {
  3986. switch (conf_get_int(wgs->conf, CONF_ctrlshiftins)) {
  3987. case CLIPUI_IMPLICIT:
  3988. break; /* no need to re-copy to CLIP_LOCAL */
  3989. case CLIPUI_EXPLICIT:
  3990. term_request_copy(wgs->term, clips_system,
  3991. lenof(clips_system));
  3992. break;
  3993. default:
  3994. break;
  3995. }
  3996. return 0;
  3997. }
  3998. if (wParam == VK_INSERT && shift_state == 1) {
  3999. switch (conf_get_int(wgs->conf, CONF_ctrlshiftins)) {
  4000. case CLIPUI_IMPLICIT:
  4001. term_request_paste(wgs->term, CLIP_LOCAL);
  4002. break;
  4003. case CLIPUI_EXPLICIT:
  4004. term_request_paste(wgs->term, CLIP_SYSTEM);
  4005. break;
  4006. default:
  4007. break;
  4008. }
  4009. return 0;
  4010. }
  4011. if (wParam == 'C' && shift_state == 3) {
  4012. switch (conf_get_int(wgs->conf, CONF_ctrlshiftcv)) {
  4013. case CLIPUI_IMPLICIT:
  4014. break; /* no need to re-copy to CLIP_LOCAL */
  4015. case CLIPUI_EXPLICIT:
  4016. term_request_copy(wgs->term, clips_system,
  4017. lenof(clips_system));
  4018. break;
  4019. default:
  4020. break;
  4021. }
  4022. return 0;
  4023. }
  4024. if (wParam == 'V' && shift_state == 3) {
  4025. switch (conf_get_int(wgs->conf, CONF_ctrlshiftcv)) {
  4026. case CLIPUI_IMPLICIT:
  4027. term_request_paste(wgs->term, CLIP_LOCAL);
  4028. break;
  4029. case CLIPUI_EXPLICIT:
  4030. term_request_paste(wgs->term, CLIP_SYSTEM);
  4031. break;
  4032. default:
  4033. break;
  4034. }
  4035. return 0;
  4036. }
  4037. if (left_alt && wParam == VK_F4 &&
  4038. conf_get_bool(wgs->conf, CONF_alt_f4)) {
  4039. return -1;
  4040. }
  4041. if (left_alt && wParam == VK_SPACE &&
  4042. conf_get_bool(wgs->conf, CONF_alt_space)) {
  4043. SendMessage(wgs->term_hwnd, WM_SYSCOMMAND, SC_KEYMENU, 0);
  4044. return -1;
  4045. }
  4046. if (left_alt && wParam == VK_RETURN &&
  4047. conf_get_bool(wgs->conf, CONF_fullscreenonaltenter) &&
  4048. (conf_get_int(wgs->conf, CONF_resize_action) != RESIZE_DISABLED)) {
  4049. if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) != KF_REPEAT)
  4050. flip_full_screen(wgs);
  4051. return -1;
  4052. }
  4053. /* Control-Numlock for app-keypad mode switch */
  4054. if (wParam == VK_PAUSE && shift_state == 2) {
  4055. wgs->term->app_keypad_keys = !wgs->term->app_keypad_keys;
  4056. return 0;
  4057. }
  4058. if (wParam == VK_BACK && shift_state == 0) { /* Backspace */
  4059. *p++ = (conf_get_bool(wgs->conf, CONF_bksp_is_delete) ?
  4060. 0x7F : 0x08);
  4061. *p++ = 0;
  4062. return -2;
  4063. }
  4064. if (wParam == VK_BACK && shift_state == 1) { /* Shift Backspace */
  4065. /* We do the opposite of what is configured */
  4066. *p++ = (conf_get_bool(wgs->conf, CONF_bksp_is_delete) ?
  4067. 0x08 : 0x7F);
  4068. *p++ = 0;
  4069. return -2;
  4070. }
  4071. if (wParam == VK_TAB && shift_state == 1) { /* Shift tab */
  4072. *p++ = 0x1B;
  4073. *p++ = '[';
  4074. *p++ = 'Z';
  4075. return p - output;
  4076. }
  4077. if (wParam == VK_SPACE && shift_state == 2) { /* Ctrl-Space */
  4078. *p++ = 0;
  4079. return p - output;
  4080. }
  4081. if (wParam == VK_SPACE && shift_state == 3) { /* Ctrl-Shift-Space */
  4082. *p++ = 160;
  4083. return p - output;
  4084. }
  4085. if (wParam == VK_CANCEL && shift_state == 2) { /* Ctrl-Break */
  4086. if (wgs->backend)
  4087. backend_special(wgs->backend, SS_BRK, 0);
  4088. return 0;
  4089. }
  4090. if (wParam == VK_PAUSE) { /* Break/Pause */
  4091. *p++ = 26;
  4092. *p++ = 0;
  4093. return -2;
  4094. }
  4095. /* Control-2 to Control-8 are special */
  4096. if (shift_state == 2 && wParam >= '2' && wParam <= '8') {
  4097. *p++ = "\000\033\034\035\036\037\177"[wParam - '2'];
  4098. return p - output;
  4099. }
  4100. if (shift_state == 2 && (wParam == 0xBD || wParam == 0xBF)) {
  4101. *p++ = 0x1F;
  4102. return p - output;
  4103. }
  4104. if (shift_state == 2 && (wParam == 0xDF || wParam == 0xDC)) {
  4105. *p++ = 0x1C;
  4106. return p - output;
  4107. }
  4108. if (shift_state == 3 && wParam == 0xDE) {
  4109. *p++ = 0x1E; /* Ctrl-~ == Ctrl-^ in xterm at least */
  4110. return p - output;
  4111. }
  4112. switch (wParam) {
  4113. bool consumed_alt;
  4114. case VK_NUMPAD0: keypad_key = '0'; goto numeric_keypad;
  4115. case VK_NUMPAD1: keypad_key = '1'; goto numeric_keypad;
  4116. case VK_NUMPAD2: keypad_key = '2'; goto numeric_keypad;
  4117. case VK_NUMPAD3: keypad_key = '3'; goto numeric_keypad;
  4118. case VK_NUMPAD4: keypad_key = '4'; goto numeric_keypad;
  4119. case VK_NUMPAD5: keypad_key = '5'; goto numeric_keypad;
  4120. case VK_NUMPAD6: keypad_key = '6'; goto numeric_keypad;
  4121. case VK_NUMPAD7: keypad_key = '7'; goto numeric_keypad;
  4122. case VK_NUMPAD8: keypad_key = '8'; goto numeric_keypad;
  4123. case VK_NUMPAD9: keypad_key = '9'; goto numeric_keypad;
  4124. case VK_DECIMAL: keypad_key = '.'; goto numeric_keypad;
  4125. case VK_ADD: keypad_key = '+'; goto numeric_keypad;
  4126. case VK_SUBTRACT: keypad_key = '-'; goto numeric_keypad;
  4127. case VK_MULTIPLY: keypad_key = '*'; goto numeric_keypad;
  4128. case VK_DIVIDE: keypad_key = '/'; goto numeric_keypad;
  4129. case VK_EXECUTE: keypad_key = 'G'; goto numeric_keypad;
  4130. /* also the case for VK_RETURN below can sometimes come here */
  4131. numeric_keypad:
  4132. /* Left Alt overrides all numeric keypad usage to act as
  4133. * numeric character code input */
  4134. if (left_alt) {
  4135. if (keypad_key >= '0' && keypad_key <= '9')
  4136. wgs->alt_numberpad_accumulator =
  4137. wgs->alt_numberpad_accumulator * 10 + keypad_key - '0';
  4138. else
  4139. wgs->alt_numberpad_accumulator = 0;
  4140. break;
  4141. }
  4142. {
  4143. int nchars = format_numeric_keypad_key(
  4144. (char *)p, wgs->term, keypad_key,
  4145. shift_state & 1, shift_state & 2);
  4146. if (!nchars) {
  4147. /*
  4148. * If we didn't get an escape sequence out of the
  4149. * numeric keypad key, then that must be because
  4150. * we're in Num Lock mode without application
  4151. * keypad enabled. In that situation we leave this
  4152. * keypress to the ToUnicode/ToAsciiEx handler
  4153. * below, which will translate it according to the
  4154. * appropriate keypad layout (e.g. so that what a
  4155. * Brit thinks of as keypad '.' can become ',' in
  4156. * the German layout).
  4157. *
  4158. * An exception is the keypad Return key: if we
  4159. * didn't get an escape sequence for that, we
  4160. * treat it like ordinary Return, taking into
  4161. * account Telnet special new line codes and
  4162. * config options.
  4163. */
  4164. if (keypad_key == '\r')
  4165. goto ordinary_return_key;
  4166. break;
  4167. }
  4168. p += nchars;
  4169. return p - output;
  4170. }
  4171. int fkey_number;
  4172. case VK_F1: fkey_number = 1; goto numbered_function_key;
  4173. case VK_F2: fkey_number = 2; goto numbered_function_key;
  4174. case VK_F3: fkey_number = 3; goto numbered_function_key;
  4175. case VK_F4: fkey_number = 4; goto numbered_function_key;
  4176. case VK_F5: fkey_number = 5; goto numbered_function_key;
  4177. case VK_F6: fkey_number = 6; goto numbered_function_key;
  4178. case VK_F7: fkey_number = 7; goto numbered_function_key;
  4179. case VK_F8: fkey_number = 8; goto numbered_function_key;
  4180. case VK_F9: fkey_number = 9; goto numbered_function_key;
  4181. case VK_F10: fkey_number = 10; goto numbered_function_key;
  4182. case VK_F11: fkey_number = 11; goto numbered_function_key;
  4183. case VK_F12: fkey_number = 12; goto numbered_function_key;
  4184. case VK_F13: fkey_number = 13; goto numbered_function_key;
  4185. case VK_F14: fkey_number = 14; goto numbered_function_key;
  4186. case VK_F15: fkey_number = 15; goto numbered_function_key;
  4187. case VK_F16: fkey_number = 16; goto numbered_function_key;
  4188. case VK_F17: fkey_number = 17; goto numbered_function_key;
  4189. case VK_F18: fkey_number = 18; goto numbered_function_key;
  4190. case VK_F19: fkey_number = 19; goto numbered_function_key;
  4191. case VK_F20: fkey_number = 20; goto numbered_function_key;
  4192. numbered_function_key:
  4193. consumed_alt = false;
  4194. p += format_function_key((char *)p, wgs->term, fkey_number,
  4195. shift_state & 1, shift_state & 2,
  4196. left_alt, &consumed_alt);
  4197. if (consumed_alt) {
  4198. /* supersedes the usual prefixing of Esc */
  4199. p -= 1;
  4200. memmove(output, output + 1, p - output);
  4201. }
  4202. return p - output;
  4203. SmallKeypadKey sk_key;
  4204. case VK_HOME: sk_key = SKK_HOME; goto small_keypad_key;
  4205. case VK_END: sk_key = SKK_END; goto small_keypad_key;
  4206. case VK_INSERT: sk_key = SKK_INSERT; goto small_keypad_key;
  4207. case VK_DELETE: sk_key = SKK_DELETE; goto small_keypad_key;
  4208. case VK_PRIOR: sk_key = SKK_PGUP; goto small_keypad_key;
  4209. case VK_NEXT: sk_key = SKK_PGDN; goto small_keypad_key;
  4210. small_keypad_key:
  4211. /* These keys don't generate terminal input with Ctrl */
  4212. if (shift_state & 2)
  4213. break;
  4214. consumed_alt = false;
  4215. p += format_small_keypad_key((char *)p, wgs->term, sk_key,
  4216. shift_state & 1, shift_state & 2,
  4217. left_alt, &consumed_alt);
  4218. if (consumed_alt) {
  4219. /* supersedes the usual prefixing of Esc */
  4220. p -= 1;
  4221. memmove(output, output + 1, p - output);
  4222. }
  4223. return p - output;
  4224. char xkey;
  4225. case VK_UP: xkey = 'A'; goto arrow_key;
  4226. case VK_DOWN: xkey = 'B'; goto arrow_key;
  4227. case VK_RIGHT: xkey = 'C'; goto arrow_key;
  4228. case VK_LEFT: xkey = 'D'; goto arrow_key;
  4229. case VK_CLEAR: xkey = 'G'; goto arrow_key; /* close enough */
  4230. arrow_key:
  4231. consumed_alt = false;
  4232. p += format_arrow_key((char *)p, wgs->term, xkey, shift_state & 1,
  4233. shift_state & 2, left_alt, &consumed_alt);
  4234. if (consumed_alt) {
  4235. /* supersedes the usual prefixing of Esc */
  4236. p -= 1;
  4237. memmove(output, output + 1, p - output);
  4238. }
  4239. return p - output;
  4240. case VK_RETURN:
  4241. if (HIWORD(lParam) & KF_EXTENDED) {
  4242. keypad_key = '\r';
  4243. goto numeric_keypad;
  4244. }
  4245. ordinary_return_key:
  4246. if (shift_state == 0 && wgs->term->cr_lf_return) {
  4247. *p++ = '\r';
  4248. *p++ = '\n';
  4249. return p - output;
  4250. } else {
  4251. *p++ = 0x0D;
  4252. *p++ = 0;
  4253. return -2;
  4254. }
  4255. }
  4256. }
  4257. /* Okay we've done everything interesting; let windows deal with
  4258. * the boring stuff */
  4259. {
  4260. bool capsOn = false;
  4261. /* helg: clear CAPS LOCK state if caps lock switches to cyrillic */
  4262. if (keystate[VK_CAPITAL] != 0 &&
  4263. conf_get_bool(wgs->conf, CONF_xlat_capslockcyr)) {
  4264. capsOn = !left_alt;
  4265. keystate[VK_CAPITAL] = 0;
  4266. }
  4267. wchar_t keys_unicode[3];
  4268. /* XXX how do we know what the max size of the keys array should
  4269. * be is? There's indication on MS' website of an Inquire/InquireEx
  4270. * functioning returning a KBINFO structure which tells us. */
  4271. if (osPlatformId == VER_PLATFORM_WIN32_NT && p_ToUnicodeEx) {
  4272. r = p_ToUnicodeEx(wParam, scan, keystate, keys_unicode,
  4273. lenof(keys_unicode), 0, kbd_layout);
  4274. } else {
  4275. /* XXX 'keys' parameter is declared in MSDN documentation as
  4276. * 'LPWORD lpChar'.
  4277. * The experience of a French user indicates that on
  4278. * Win98, WORD[] should be passed in, but on Win2K, it should
  4279. * be BYTE[]. German WinXP and my Win2K with "US International"
  4280. * driver corroborate this.
  4281. * Experimentally I've conditionalised the behaviour on the
  4282. * Win9x/NT split, but I suspect it's worse than that.
  4283. * See wishlist item `win-dead-keys' for more horrible detail
  4284. * and speculations. */
  4285. int i;
  4286. WORD keys[3];
  4287. BYTE keysb[3];
  4288. r = ToAsciiEx(wParam, scan, keystate, keys, 0, kbd_layout);
  4289. if (r > 0) {
  4290. for (i = 0; i < r; i++) {
  4291. keysb[i] = (BYTE)keys[i];
  4292. }
  4293. MultiByteToWideChar(CP_ACP, 0, (LPCSTR)keysb, r,
  4294. keys_unicode, lenof(keys_unicode));
  4295. }
  4296. }
  4297. #ifdef SHOW_TOASCII_RESULT
  4298. if (r == 1 && !key_down) {
  4299. if (wgs->alt_numberpad_accumulator) {
  4300. if (in_utf(term) || ucsdata.dbcs_screenfont)
  4301. debug(", (U+%04x)", wgs->alt_numberpad_accumulator);
  4302. else
  4303. debug(", LCH(%d)", wgs->alt_numberpad_accumulator);
  4304. } else {
  4305. debug(", ACH(%d)", keys_unicode[0]);
  4306. }
  4307. } else if (r > 0) {
  4308. int r1;
  4309. debug(", ASC(");
  4310. for (r1 = 0; r1 < r; r1++) {
  4311. debug("%s%d", r1 ? "," : "", keys_unicode[r1]);
  4312. }
  4313. debug(")");
  4314. }
  4315. #endif
  4316. if (r > 0) {
  4317. WCHAR keybuf;
  4318. p = output;
  4319. for (i = 0; i < r; i++) {
  4320. wchar_t wch = keys_unicode[i];
  4321. if (wgs->compose_state == 2 && wch >= ' ' && wch < 0x80) {
  4322. wgs->compose_char = wch;
  4323. wgs->compose_state++;
  4324. continue;
  4325. }
  4326. if (wgs->compose_state == 3 && wch >= ' ' && wch < 0x80) {
  4327. int nc;
  4328. wgs->compose_state = 0;
  4329. if ((nc = check_compose(wgs->compose_char, wch)) == -1) {
  4330. MessageBeep(MB_ICONHAND);
  4331. return 0;
  4332. }
  4333. keybuf = nc;
  4334. term_keyinputw(wgs->term, &keybuf, 1);
  4335. continue;
  4336. }
  4337. wgs->compose_state = 0;
  4338. if (!key_down) {
  4339. if (wgs->alt_numberpad_accumulator) {
  4340. if (in_utf(wgs->term) ||
  4341. wgs->ucsdata.dbcs_screenfont) {
  4342. keybuf = wgs->alt_numberpad_accumulator;
  4343. term_keyinputw(wgs->term, &keybuf, 1);
  4344. } else {
  4345. char ch = (char) wgs->alt_numberpad_accumulator;
  4346. /*
  4347. * We need not bother about stdin
  4348. * backlogs here, because in GUI PuTTY
  4349. * we can't do anything about it
  4350. * anyway; there's no means of asking
  4351. * Windows to hold off on KEYDOWN
  4352. * messages. We _have_ to buffer
  4353. * everything we're sent.
  4354. */
  4355. term_keyinput(wgs->term, -1, &ch, 1);
  4356. }
  4357. wgs->alt_numberpad_accumulator = 0;
  4358. } else {
  4359. term_keyinputw(wgs->term, &wch, 1);
  4360. }
  4361. } else {
  4362. if (capsOn && wch < 0x80) {
  4363. WCHAR cbuf[2];
  4364. cbuf[0] = 27;
  4365. cbuf[1] = xlat_uskbd2cyrllic(wch);
  4366. term_keyinputw(
  4367. wgs->term, cbuf+!left_alt, 1+!!left_alt);
  4368. } else {
  4369. WCHAR cbuf[2];
  4370. cbuf[0] = '\033';
  4371. cbuf[1] = wch;
  4372. term_keyinputw(
  4373. wgs->term, cbuf +!left_alt, 1+!!left_alt);
  4374. }
  4375. }
  4376. show_mouseptr(wgs, false);
  4377. }
  4378. return p - output;
  4379. }
  4380. }
  4381. /*
  4382. * ALT alone may or may not want to bring up the System menu.
  4383. * If it's not meant to, we return 0 on presses or releases of
  4384. * ALT, to show that we've swallowed the keystroke. Otherwise
  4385. * we return -1, which means Windows will give the keystroke
  4386. * its default handling (i.e. bring up the System menu).
  4387. */
  4388. if (wParam == VK_MENU && !conf_get_bool(wgs->conf, CONF_alt_only))
  4389. return 0;
  4390. return -1;
  4391. }
  4392. static void wintw_set_title(TermWin *tw, const char *title, int codepage)
  4393. {
  4394. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  4395. wchar_t *new_window_name = dup_mb_to_wc(codepage, title);
  4396. if (!wcscmp(new_window_name, wgs->window_name)) {
  4397. sfree(new_window_name);
  4398. return;
  4399. }
  4400. sfree(wgs->window_name);
  4401. wgs->window_name = new_window_name;
  4402. if (conf_get_bool(wgs->conf, CONF_win_name_always) ||
  4403. !IsIconic(wgs->term_hwnd))
  4404. sw_SetWindowText(wgs->term_hwnd, wgs->window_name);
  4405. }
  4406. static void wintw_set_icon_title(TermWin *tw, const char *title, int codepage)
  4407. {
  4408. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  4409. wchar_t *new_icon_name = dup_mb_to_wc(codepage, title);
  4410. if (!wcscmp(new_icon_name, wgs->icon_name)) {
  4411. sfree(new_icon_name);
  4412. return;
  4413. }
  4414. sfree(wgs->icon_name);
  4415. wgs->icon_name = new_icon_name;
  4416. if (!conf_get_bool(wgs->conf, CONF_win_name_always) &&
  4417. IsIconic(wgs->term_hwnd))
  4418. sw_SetWindowText(wgs->term_hwnd, wgs->icon_name);
  4419. }
  4420. static void wintw_set_scrollbar(TermWin *tw, int total, int start, int page)
  4421. {
  4422. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  4423. SCROLLINFO si;
  4424. if (!conf_get_bool(wgs->conf, is_full_screen(wgs) ?
  4425. CONF_scrollbar_in_fullscreen : CONF_scrollbar))
  4426. return;
  4427. si.cbSize = sizeof(si);
  4428. si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
  4429. si.nMin = 0;
  4430. si.nMax = total - 1;
  4431. si.nPage = page;
  4432. si.nPos = start;
  4433. if (wgs->term_hwnd)
  4434. SetScrollInfo(wgs->term_hwnd, SB_VERT, &si, true);
  4435. }
  4436. static bool wintw_setup_draw_ctx(TermWin *tw)
  4437. {
  4438. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  4439. assert(!wgs->wintw_hdc);
  4440. wgs->wintw_hdc = make_hdc(wgs);
  4441. return wgs->wintw_hdc != NULL;
  4442. }
  4443. static void wintw_free_draw_ctx(TermWin *tw)
  4444. {
  4445. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  4446. assert(wgs->wintw_hdc);
  4447. free_hdc(wgs, wgs->wintw_hdc);
  4448. wgs->wintw_hdc = NULL;
  4449. }
  4450. /*
  4451. * Set up the colour palette.
  4452. */
  4453. static void init_palette(WinGuiSeat *wgs)
  4454. {
  4455. wgs->pal = NULL;
  4456. wgs->logpal = snew_plus(
  4457. LOGPALETTE, (OSC4_NCOLOURS - 1) * sizeof(PALETTEENTRY));
  4458. wgs->logpal->palVersion = 0x300;
  4459. wgs->logpal->palNumEntries = OSC4_NCOLOURS;
  4460. for (unsigned i = 0; i < OSC4_NCOLOURS; i++)
  4461. wgs->logpal->palPalEntry[i].peFlags = PC_NOCOLLAPSE;
  4462. }
  4463. static void wintw_palette_set(TermWin *tw, unsigned start,
  4464. unsigned ncolours, const rgb *colours_in)
  4465. {
  4466. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  4467. assert(start <= OSC4_NCOLOURS);
  4468. assert(ncolours <= OSC4_NCOLOURS - start);
  4469. for (unsigned i = 0; i < ncolours; i++) {
  4470. const rgb *in = &colours_in[i];
  4471. PALETTEENTRY *out = &wgs->logpal->palPalEntry[i + start];
  4472. out->peRed = in->r;
  4473. out->peGreen = in->g;
  4474. out->peBlue = in->b;
  4475. wgs->colours[i + start] =
  4476. RGB(in->r, in->g, in->b) ^ wgs->colorref_modifier;
  4477. }
  4478. bool got_new_palette = false;
  4479. if (!wgs->tried_pal && conf_get_bool(wgs->conf, CONF_try_palette)) {
  4480. HDC hdc = GetDC(wgs->term_hwnd);
  4481. if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) {
  4482. wgs->pal = CreatePalette(wgs->logpal);
  4483. if (wgs->pal) {
  4484. SelectPalette(hdc, wgs->pal, false);
  4485. RealizePalette(hdc);
  4486. SelectPalette(hdc, GetStockObject(DEFAULT_PALETTE), false);
  4487. /* Convert all RGB() values in colours[] into PALETTERGB(),
  4488. * and ensure we stick to that later */
  4489. wgs->colorref_modifier = PALETTERGB(0, 0, 0) ^ RGB(0, 0, 0);
  4490. for (unsigned i = 0; i < OSC4_NCOLOURS; i++)
  4491. wgs->colours[i] ^= wgs->colorref_modifier;
  4492. /* Inhibit the SetPaletteEntries call below */
  4493. got_new_palette = true;
  4494. }
  4495. }
  4496. ReleaseDC(wgs->term_hwnd, hdc);
  4497. wgs->tried_pal = true;
  4498. }
  4499. if (wgs->pal && !got_new_palette) {
  4500. /* We already had a palette, so replace the changed colours in the
  4501. * existing one. */
  4502. SetPaletteEntries(wgs->pal, start, ncolours,
  4503. wgs->logpal->palPalEntry + start);
  4504. HDC hdc = make_hdc(wgs);
  4505. UnrealizeObject(wgs->pal);
  4506. RealizePalette(hdc);
  4507. free_hdc(wgs, hdc);
  4508. }
  4509. if (start <= OSC4_COLOUR_bg && OSC4_COLOUR_bg < start + ncolours) {
  4510. /* If Default Background changes, we need to ensure any space between
  4511. * the text area and the window border is redrawn. */
  4512. InvalidateRect(wgs->term_hwnd, NULL, true);
  4513. }
  4514. }
  4515. void write_aclip(HWND hwnd, int clipboard, char *data, int len)
  4516. {
  4517. HGLOBAL clipdata;
  4518. void *lock;
  4519. if (clipboard != CLIP_SYSTEM)
  4520. return;
  4521. clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
  4522. if (!clipdata)
  4523. return;
  4524. lock = GlobalLock(clipdata);
  4525. if (!lock)
  4526. return;
  4527. memcpy(lock, data, len);
  4528. ((unsigned char *) lock)[len] = 0;
  4529. GlobalUnlock(clipdata);
  4530. if (OpenClipboard(hwnd)) {
  4531. EmptyClipboard();
  4532. SetClipboardData(CF_TEXT, clipdata);
  4533. CloseClipboard();
  4534. } else
  4535. GlobalFree(clipdata);
  4536. }
  4537. typedef struct _rgbindex {
  4538. int index;
  4539. COLORREF ref;
  4540. } rgbindex;
  4541. int cmpCOLORREF(void *va, void *vb)
  4542. {
  4543. COLORREF a = ((rgbindex *)va)->ref;
  4544. COLORREF b = ((rgbindex *)vb)->ref;
  4545. return (a < b) ? -1 : (a > b) ? +1 : 0;
  4546. }
  4547. /*
  4548. * Note: unlike write_aclip() this will not append a nul.
  4549. */
  4550. static void wintw_clip_write(
  4551. TermWin *tw, int clipboard, wchar_t *data, int *attr,
  4552. truecolour *truecolour, int len, bool must_deselect)
  4553. {
  4554. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  4555. HGLOBAL clipdata, clipdata2, clipdata3;
  4556. int len2;
  4557. void *lock, *lock2, *lock3;
  4558. if (clipboard != CLIP_SYSTEM)
  4559. return;
  4560. len2 = WideCharToMultiByte(CP_ACP, 0, data, len, 0, 0, NULL, NULL);
  4561. clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE,
  4562. len * sizeof(wchar_t));
  4563. clipdata2 = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len2);
  4564. if (!clipdata || !clipdata2) {
  4565. if (clipdata)
  4566. GlobalFree(clipdata);
  4567. if (clipdata2)
  4568. GlobalFree(clipdata2);
  4569. return;
  4570. }
  4571. if (!(lock = GlobalLock(clipdata))) {
  4572. GlobalFree(clipdata);
  4573. GlobalFree(clipdata2);
  4574. return;
  4575. }
  4576. if (!(lock2 = GlobalLock(clipdata2))) {
  4577. GlobalUnlock(clipdata);
  4578. GlobalFree(clipdata);
  4579. GlobalFree(clipdata2);
  4580. return;
  4581. }
  4582. memcpy(lock, data, len * sizeof(wchar_t));
  4583. WideCharToMultiByte(CP_ACP, 0, data, len, lock2, len2, NULL, NULL);
  4584. if (conf_get_bool(wgs->conf, CONF_rtf_paste)) {
  4585. wchar_t unitab[256];
  4586. strbuf *rtf = strbuf_new();
  4587. unsigned char *tdata = (unsigned char *)lock2;
  4588. wchar_t *udata = (wchar_t *)lock;
  4589. int uindex = 0, tindex = 0;
  4590. int multilen, blen, alen, i;
  4591. char before[16], after[4];
  4592. int fgcolour, lastfgcolour = -1;
  4593. int bgcolour, lastbgcolour = -1;
  4594. COLORREF fg, lastfg = -1;
  4595. COLORREF bg, lastbg = -1;
  4596. int attrBold, lastAttrBold = 0;
  4597. int attrUnder, lastAttrUnder = 0;
  4598. int palette[OSC4_NCOLOURS];
  4599. int numcolours;
  4600. tree234 *rgbtree = NULL;
  4601. FontSpec *font = conf_get_fontspec(wgs->conf, CONF_font);
  4602. get_unitab(CP_ACP, unitab, 0);
  4603. put_fmt(
  4604. rtf, "{\\rtf1\\ansi\\deff0{\\fonttbl\\f0\\fmodern %s;}\\f0\\fs%d",
  4605. font->name, font->height*2);
  4606. /*
  4607. * Add colour palette
  4608. * {\colortbl ;\red255\green0\blue0;\red0\green0\blue128;}
  4609. */
  4610. /*
  4611. * First - Determine all colours in use
  4612. * o Foregound and background colours share the same palette
  4613. */
  4614. if (attr) {
  4615. memset(palette, 0, sizeof(palette));
  4616. for (i = 0; i < (len-1); i++) {
  4617. fgcolour = ((attr[i] & ATTR_FGMASK) >> ATTR_FGSHIFT);
  4618. bgcolour = ((attr[i] & ATTR_BGMASK) >> ATTR_BGSHIFT);
  4619. if (attr[i] & ATTR_REVERSE) {
  4620. int tmpcolour = fgcolour; /* Swap foreground and background */
  4621. fgcolour = bgcolour;
  4622. bgcolour = tmpcolour;
  4623. }
  4624. if (wgs->bold_colours && (attr[i] & ATTR_BOLD)) {
  4625. if (fgcolour < 8) /* ANSI colours */
  4626. fgcolour += 8;
  4627. else if (fgcolour >= 256) /* Default colours */
  4628. fgcolour ++;
  4629. }
  4630. if ((attr[i] & ATTR_BLINK)) {
  4631. if (bgcolour < 8) /* ANSI colours */
  4632. bgcolour += 8;
  4633. else if (bgcolour >= 256) /* Default colours */
  4634. bgcolour ++;
  4635. }
  4636. palette[fgcolour]++;
  4637. palette[bgcolour]++;
  4638. }
  4639. if (truecolour) {
  4640. rgbtree = newtree234(cmpCOLORREF);
  4641. for (i = 0; i < (len-1); i++) {
  4642. if (truecolour[i].fg.enabled) {
  4643. rgbindex *rgbp = snew(rgbindex);
  4644. rgbp->ref = RGB(truecolour[i].fg.r,
  4645. truecolour[i].fg.g,
  4646. truecolour[i].fg.b);
  4647. if (add234(rgbtree, rgbp) != rgbp)
  4648. sfree(rgbp);
  4649. }
  4650. if (truecolour[i].bg.enabled) {
  4651. rgbindex *rgbp = snew(rgbindex);
  4652. rgbp->ref = RGB(truecolour[i].bg.r,
  4653. truecolour[i].bg.g,
  4654. truecolour[i].bg.b);
  4655. if (add234(rgbtree, rgbp) != rgbp)
  4656. sfree(rgbp);
  4657. }
  4658. }
  4659. }
  4660. /*
  4661. * Next - Create a reduced palette
  4662. */
  4663. numcolours = 0;
  4664. for (i = 0; i < OSC4_NCOLOURS; i++) {
  4665. if (palette[i] != 0)
  4666. palette[i] = ++numcolours;
  4667. }
  4668. if (rgbtree) {
  4669. rgbindex *rgbp;
  4670. for (i = 0; (rgbp = index234(rgbtree, i)) != NULL; i++)
  4671. rgbp->index = ++numcolours;
  4672. }
  4673. /*
  4674. * Finally - Write the colour table
  4675. */
  4676. put_datapl(rtf, PTRLEN_LITERAL("{\\colortbl ;"));
  4677. for (i = 0; i < OSC4_NCOLOURS; i++) {
  4678. if (palette[i] != 0) {
  4679. const PALETTEENTRY *pe = &wgs->logpal->palPalEntry[i];
  4680. put_fmt(rtf, "\\red%d\\green%d\\blue%d;",
  4681. pe->peRed, pe->peGreen, pe->peBlue);
  4682. }
  4683. }
  4684. if (rgbtree) {
  4685. rgbindex *rgbp;
  4686. for (i = 0; (rgbp = index234(rgbtree, i)) != NULL; i++)
  4687. put_fmt(rtf, "\\red%d\\green%d\\blue%d;",
  4688. GetRValue(rgbp->ref), GetGValue(rgbp->ref),
  4689. GetBValue(rgbp->ref));
  4690. }
  4691. put_datapl(rtf, PTRLEN_LITERAL("}"));
  4692. }
  4693. /*
  4694. * We want to construct a piece of RTF that specifies the
  4695. * same Unicode text. To do this we will read back in
  4696. * parallel from the Unicode data in `udata' and the
  4697. * non-Unicode data in `tdata'. For each character in
  4698. * `tdata' which becomes the right thing in `udata' when
  4699. * looked up in `unitab', we just copy straight over from
  4700. * tdata. For each one that doesn't, we must WCToMB it
  4701. * individually and produce a \u escape sequence.
  4702. *
  4703. * It would probably be more robust to just bite the bullet
  4704. * and WCToMB each individual Unicode character one by one,
  4705. * then MBToWC each one back to see if it was an accurate
  4706. * translation; but that strikes me as a horrifying number
  4707. * of Windows API calls so I want to see if this faster way
  4708. * will work. If it screws up badly we can always revert to
  4709. * the simple and slow way.
  4710. */
  4711. while (tindex < len2 && uindex < len &&
  4712. tdata[tindex] && udata[uindex]) {
  4713. if (tindex + 1 < len2 &&
  4714. tdata[tindex] == '\r' &&
  4715. tdata[tindex+1] == '\n') {
  4716. tindex++;
  4717. uindex++;
  4718. }
  4719. /*
  4720. * Set text attributes
  4721. */
  4722. if (attr) {
  4723. /*
  4724. * Determine foreground and background colours
  4725. */
  4726. if (truecolour && truecolour[tindex].fg.enabled) {
  4727. fgcolour = -1;
  4728. fg = RGB(truecolour[tindex].fg.r,
  4729. truecolour[tindex].fg.g,
  4730. truecolour[tindex].fg.b);
  4731. } else {
  4732. fgcolour = ((attr[tindex] & ATTR_FGMASK) >> ATTR_FGSHIFT);
  4733. fg = -1;
  4734. }
  4735. if (truecolour && truecolour[tindex].bg.enabled) {
  4736. bgcolour = -1;
  4737. bg = RGB(truecolour[tindex].bg.r,
  4738. truecolour[tindex].bg.g,
  4739. truecolour[tindex].bg.b);
  4740. } else {
  4741. bgcolour = ((attr[tindex] & ATTR_BGMASK) >> ATTR_BGSHIFT);
  4742. bg = -1;
  4743. }
  4744. if (attr[tindex] & ATTR_REVERSE) {
  4745. int tmpcolour = fgcolour; /* Swap foreground and background */
  4746. fgcolour = bgcolour;
  4747. bgcolour = tmpcolour;
  4748. COLORREF tmpref = fg;
  4749. fg = bg;
  4750. bg = tmpref;
  4751. }
  4752. if (wgs->bold_colours && (attr[tindex] & ATTR_BOLD) &&
  4753. (fgcolour >= 0)) {
  4754. if (fgcolour < 8) /* ANSI colours */
  4755. fgcolour += 8;
  4756. else if (fgcolour >= 256) /* Default colours */
  4757. fgcolour ++;
  4758. }
  4759. if ((attr[tindex] & ATTR_BLINK) && (bgcolour >= 0)) {
  4760. if (bgcolour < 8) /* ANSI colours */
  4761. bgcolour += 8;
  4762. else if (bgcolour >= 256) /* Default colours */
  4763. bgcolour ++;
  4764. }
  4765. /*
  4766. * Collect other attributes
  4767. */
  4768. if (wgs->bold_font_mode != BOLD_NONE)
  4769. attrBold = attr[tindex] & ATTR_BOLD;
  4770. else
  4771. attrBold = 0;
  4772. attrUnder = attr[tindex] & ATTR_UNDER;
  4773. /*
  4774. * Reverse video
  4775. * o If video isn't reversed, ignore colour attributes for default foregound
  4776. * or background.
  4777. * o Special case where bolded text is displayed using the default foregound
  4778. * and background colours - force to bolded RTF.
  4779. */
  4780. if (!(attr[tindex] & ATTR_REVERSE)) {
  4781. if (bgcolour >= 256) /* Default color */
  4782. bgcolour = -1; /* No coloring */
  4783. if (fgcolour >= 256) { /* Default colour */
  4784. if (wgs->bold_colours && (fgcolour & 1) &&
  4785. bgcolour == -1)
  4786. attrBold = ATTR_BOLD; /* Emphasize text with bold attribute */
  4787. fgcolour = -1; /* No coloring */
  4788. }
  4789. }
  4790. /*
  4791. * Write RTF text attributes
  4792. */
  4793. if ((lastfgcolour != fgcolour) || (lastfg != fg)) {
  4794. lastfgcolour = fgcolour;
  4795. lastfg = fg;
  4796. if (fg == -1) {
  4797. put_fmt(rtf, "\\cf%d ",
  4798. (fgcolour >= 0) ? palette[fgcolour] : 0);
  4799. } else {
  4800. rgbindex rgb, *rgbp;
  4801. rgb.ref = fg;
  4802. if ((rgbp = find234(rgbtree, &rgb, NULL)) != NULL)
  4803. put_fmt(rtf, "\\cf%d ", rgbp->index);
  4804. }
  4805. }
  4806. if ((lastbgcolour != bgcolour) || (lastbg != bg)) {
  4807. lastbgcolour = bgcolour;
  4808. lastbg = bg;
  4809. if (bg == -1)
  4810. put_fmt(rtf, "\\highlight%d ",
  4811. (bgcolour >= 0) ? palette[bgcolour] : 0);
  4812. else {
  4813. rgbindex rgb, *rgbp;
  4814. rgb.ref = bg;
  4815. if ((rgbp = find234(rgbtree, &rgb, NULL)) != NULL)
  4816. put_fmt(rtf, "\\highlight%d ", rgbp->index);
  4817. }
  4818. }
  4819. if (lastAttrBold != attrBold) {
  4820. lastAttrBold = attrBold;
  4821. put_datapl(rtf, attrBold ?
  4822. PTRLEN_LITERAL("\\b ") :
  4823. PTRLEN_LITERAL("\\b0 "));
  4824. }
  4825. if (lastAttrUnder != attrUnder) {
  4826. lastAttrUnder = attrUnder;
  4827. put_datapl(rtf, attrUnder ?
  4828. PTRLEN_LITERAL("\\ul ") :
  4829. PTRLEN_LITERAL("\\ulnone "));
  4830. }
  4831. }
  4832. if (unitab[tdata[tindex]] == udata[uindex]) {
  4833. multilen = 1;
  4834. before[0] = '\0';
  4835. after[0] = '\0';
  4836. blen = alen = 0;
  4837. } else {
  4838. multilen = WideCharToMultiByte(CP_ACP, 0, unitab+uindex, 1,
  4839. NULL, 0, NULL, NULL);
  4840. if (multilen != 1) {
  4841. blen = sprintf(before, "{\\uc%d\\u%d", (int)multilen,
  4842. (int)udata[uindex]);
  4843. alen = 1; strcpy(after, "}");
  4844. } else {
  4845. blen = sprintf(before, "\\u%d", (int)udata[uindex]);
  4846. alen = 0; after[0] = '\0';
  4847. }
  4848. }
  4849. assert(tindex + multilen <= len2);
  4850. put_data(rtf, before, blen);
  4851. for (i = 0; i < multilen; i++) {
  4852. if (tdata[tindex+i] == '\\' ||
  4853. tdata[tindex+i] == '{' ||
  4854. tdata[tindex+i] == '}') {
  4855. put_byte(rtf, '\\');
  4856. put_byte(rtf, tdata[tindex+i]);
  4857. } else if (tdata[tindex+i] == 0x0D || tdata[tindex+i] == 0x0A) {
  4858. put_datapl(rtf, PTRLEN_LITERAL("\\par\r\n"));
  4859. } else if (tdata[tindex+i] > 0x7E || tdata[tindex+i] < 0x20) {
  4860. put_fmt(rtf, "\\'%02x", tdata[tindex+i]);
  4861. } else {
  4862. put_byte(rtf, tdata[tindex+i]);
  4863. }
  4864. }
  4865. put_data(rtf, after, alen);
  4866. tindex += multilen;
  4867. uindex++;
  4868. }
  4869. put_datapl(rtf, PTRLEN_LITERAL("}\0\0")); /* Terminate RTF stream */
  4870. clipdata3 = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, rtf->len);
  4871. if (clipdata3 && (lock3 = GlobalLock(clipdata3)) != NULL) {
  4872. memcpy(lock3, rtf->u, rtf->len);
  4873. GlobalUnlock(clipdata3);
  4874. }
  4875. strbuf_free(rtf);
  4876. if (rgbtree) {
  4877. rgbindex *rgbp;
  4878. while ((rgbp = delpos234(rgbtree, 0)) != NULL)
  4879. sfree(rgbp);
  4880. freetree234(rgbtree);
  4881. }
  4882. } else
  4883. clipdata3 = NULL;
  4884. GlobalUnlock(clipdata);
  4885. GlobalUnlock(clipdata2);
  4886. if (!must_deselect)
  4887. SendMessage(wgs->term_hwnd, WM_IGNORE_CLIP, true, 0);
  4888. if (OpenClipboard(wgs->term_hwnd)) {
  4889. EmptyClipboard();
  4890. SetClipboardData(CF_UNICODETEXT, clipdata);
  4891. SetClipboardData(CF_TEXT, clipdata2);
  4892. if (clipdata3)
  4893. SetClipboardData(RegisterClipboardFormat(CF_RTF), clipdata3);
  4894. CloseClipboard();
  4895. } else {
  4896. GlobalFree(clipdata);
  4897. GlobalFree(clipdata2);
  4898. }
  4899. if (!must_deselect)
  4900. SendMessage(wgs->term_hwnd, WM_IGNORE_CLIP, false, 0);
  4901. }
  4902. static DWORD WINAPI clipboard_read_threadfunc(void *param)
  4903. {
  4904. HWND hwnd = (HWND)param;
  4905. HGLOBAL clipdata;
  4906. if (OpenClipboard(NULL)) {
  4907. if ((clipdata = GetClipboardData(CF_UNICODETEXT))) {
  4908. SendMessage(hwnd, WM_GOT_CLIPDATA,
  4909. (WPARAM)true, (LPARAM)clipdata);
  4910. } else if ((clipdata = GetClipboardData(CF_TEXT))) {
  4911. SendMessage(hwnd, WM_GOT_CLIPDATA,
  4912. (WPARAM)false, (LPARAM)clipdata);
  4913. }
  4914. CloseClipboard();
  4915. }
  4916. return 0;
  4917. }
  4918. static void process_clipdata(WinGuiSeat *wgs, HGLOBAL clipdata, bool unicode)
  4919. {
  4920. wchar_t *clipboard_contents = NULL;
  4921. size_t clipboard_length = 0;
  4922. if (unicode) {
  4923. wchar_t *p = GlobalLock(clipdata);
  4924. wchar_t *p2;
  4925. if (p) {
  4926. /* Unwilling to rely on Windows having wcslen() */
  4927. for (p2 = p; *p2; p2++);
  4928. clipboard_length = p2 - p;
  4929. clipboard_contents = snewn(clipboard_length + 1, wchar_t);
  4930. memcpy(clipboard_contents, p, clipboard_length * sizeof(wchar_t));
  4931. clipboard_contents[clipboard_length] = L'\0';
  4932. term_do_paste(wgs->term, clipboard_contents, clipboard_length);
  4933. }
  4934. } else {
  4935. char *s = GlobalLock(clipdata);
  4936. int i;
  4937. if (s) {
  4938. i = MultiByteToWideChar(CP_ACP, 0, s, strlen(s) + 1, 0, 0);
  4939. clipboard_contents = snewn(i, wchar_t);
  4940. MultiByteToWideChar(CP_ACP, 0, s, strlen(s) + 1,
  4941. clipboard_contents, i);
  4942. clipboard_length = i - 1;
  4943. clipboard_contents[clipboard_length] = L'\0';
  4944. term_do_paste(wgs->term, clipboard_contents, clipboard_length);
  4945. }
  4946. }
  4947. sfree(clipboard_contents);
  4948. }
  4949. static void wintw_clip_request_paste(TermWin *tw, int clipboard)
  4950. {
  4951. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  4952. assert(clipboard == CLIP_SYSTEM);
  4953. /*
  4954. * I always thought pasting was synchronous in Windows; the
  4955. * clipboard access functions certainly _look_ synchronous,
  4956. * unlike the X ones. But in fact it seems that in some
  4957. * situations the contents of the clipboard might not be
  4958. * immediately available, and the clipboard-reading functions
  4959. * may block. This leads to trouble if the application
  4960. * delivering the clipboard data has to get hold of it by -
  4961. * for example - talking over a network connection which is
  4962. * forwarded through this very PuTTY.
  4963. *
  4964. * Hence, we spawn a subthread to read the clipboard, and do
  4965. * our paste when it's finished. The thread will send a
  4966. * message back to our main window when it terminates, and
  4967. * that tells us it's OK to paste.
  4968. */
  4969. DWORD in_threadid; /* required for Win9x */
  4970. HANDLE hThread = CreateThread(NULL, 0, clipboard_read_threadfunc,
  4971. wgs->term_hwnd, 0, &in_threadid);
  4972. if (hThread)
  4973. CloseHandle(hThread); /* we don't need the thread handle */
  4974. }
  4975. /*
  4976. * Print a modal (Really Bad) message box and perform a fatal exit.
  4977. */
  4978. void modalfatalbox(const char *fmt, ...)
  4979. {
  4980. va_list ap;
  4981. char *message, *title;
  4982. va_start(ap, fmt);
  4983. message = dupvprintf(fmt, ap);
  4984. va_end(ap);
  4985. show_mouseptr(NULL, true);
  4986. title = dupprintf("%s Fatal Error", appname);
  4987. MessageBox(find_window_for_msgbox(), message, title,
  4988. MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
  4989. sfree(message);
  4990. sfree(title);
  4991. cleanup_exit(1);
  4992. }
  4993. /*
  4994. * Print a message box and don't close the connection.
  4995. */
  4996. void nonfatal(const char *fmt, ...)
  4997. {
  4998. va_list ap;
  4999. char *message, *title;
  5000. va_start(ap, fmt);
  5001. message = dupvprintf(fmt, ap);
  5002. va_end(ap);
  5003. show_mouseptr(NULL, true);
  5004. title = dupprintf("%s Error", appname);
  5005. MessageBox(find_window_for_msgbox(), message, title, MB_ICONERROR | MB_OK);
  5006. sfree(message);
  5007. sfree(title);
  5008. }
  5009. static bool flash_window_ex(WinGuiSeat *wgs, DWORD dwFlags,
  5010. UINT uCount, DWORD dwTimeout)
  5011. {
  5012. if (p_FlashWindowEx) {
  5013. FLASHWINFO fi;
  5014. fi.cbSize = sizeof(fi);
  5015. fi.hwnd = wgs->term_hwnd;
  5016. fi.dwFlags = dwFlags;
  5017. fi.uCount = uCount;
  5018. fi.dwTimeout = dwTimeout;
  5019. return (*p_FlashWindowEx)(&fi);
  5020. }
  5021. else
  5022. return false; /* shrug */
  5023. }
  5024. /*
  5025. * Timer for platforms where we must maintain window flashing manually
  5026. * (e.g., Win95).
  5027. */
  5028. static void flash_window_timer(void *vctx, unsigned long now)
  5029. {
  5030. WinGuiSeat *wgs = (WinGuiSeat *)vctx;
  5031. if (wgs->flashing && now == wgs->next_flash) {
  5032. flash_window(wgs, 1);
  5033. }
  5034. }
  5035. /*
  5036. * Manage window caption / taskbar flashing, if enabled.
  5037. * 0 = stop, 1 = maintain, 2 = start
  5038. */
  5039. static void flash_window(WinGuiSeat *wgs, int mode)
  5040. {
  5041. int beep_ind = conf_get_int(wgs->conf, CONF_beep_ind);
  5042. if ((mode == 0) || (beep_ind == B_IND_DISABLED)) {
  5043. /* stop */
  5044. if (wgs->flashing) {
  5045. wgs->flashing = false;
  5046. if (p_FlashWindowEx)
  5047. flash_window_ex(wgs, FLASHW_STOP, 0, 0);
  5048. else
  5049. FlashWindow(wgs->term_hwnd, false);
  5050. }
  5051. } else if (mode == 2) {
  5052. /* start */
  5053. if (!wgs->flashing) {
  5054. wgs->flashing = true;
  5055. if (p_FlashWindowEx) {
  5056. /* For so-called "steady" mode, we use uCount=2, which
  5057. * seems to be the traditional number of flashes used
  5058. * by user notifications (e.g., by Explorer).
  5059. * uCount=0 appears to enable continuous flashing, per
  5060. * "flashing" mode, although I haven't seen this
  5061. * documented. */
  5062. flash_window_ex(wgs, FLASHW_ALL | FLASHW_TIMER,
  5063. (beep_ind == B_IND_FLASH ? 0 : 2),
  5064. 0 /* system cursor blink rate */);
  5065. /* No need to schedule timer */
  5066. } else {
  5067. FlashWindow(wgs->term_hwnd, true);
  5068. wgs->next_flash = schedule_timer(450, flash_window_timer, wgs);
  5069. }
  5070. }
  5071. } else if ((mode == 1) && (beep_ind == B_IND_FLASH)) {
  5072. /* maintain */
  5073. if (wgs->flashing && !p_FlashWindowEx) {
  5074. FlashWindow(wgs->term_hwnd, true); /* toggle */
  5075. wgs->next_flash = schedule_timer(450, flash_window_timer, wgs);
  5076. }
  5077. }
  5078. }
  5079. /*
  5080. * Beep.
  5081. */
  5082. static void wintw_bell(TermWin *tw, int mode)
  5083. {
  5084. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  5085. if (mode == BELL_DEFAULT) {
  5086. /*
  5087. * For MessageBeep style bells, we want to be careful of
  5088. * timing, because they don't have the nice property of
  5089. * PlaySound bells that each one cancels the previous
  5090. * active one. So we limit the rate to one per 50ms or so.
  5091. */
  5092. long beepdiff;
  5093. beepdiff = GetTickCount() - wgs->last_beep_time;
  5094. if (beepdiff >= 0 && beepdiff < 50)
  5095. return;
  5096. MessageBeep(MB_OK);
  5097. /*
  5098. * The above MessageBeep call takes time, so we record the
  5099. * time _after_ it finishes rather than before it starts.
  5100. */
  5101. wgs->last_beep_time = GetTickCount();
  5102. } else if (mode == BELL_WAVEFILE) {
  5103. Filename *bell_wavefile = conf_get_filename(
  5104. wgs->conf, CONF_bell_wavefile);
  5105. bool success = (
  5106. p_PlaySoundW ? p_PlaySoundW(bell_wavefile->wpath, NULL,
  5107. SND_ASYNC | SND_FILENAME) :
  5108. p_PlaySoundA ? p_PlaySoundA(bell_wavefile->cpath, NULL,
  5109. SND_ASYNC | SND_FILENAME) : false);
  5110. if (!success) {
  5111. char *buf, *otherbuf;
  5112. show_mouseptr(wgs, true);
  5113. buf = dupprintf(
  5114. "Unable to play sound file\n%s\nUsing default sound instead",
  5115. bell_wavefile->utf8path);
  5116. otherbuf = dupprintf("%s Sound Error", appname);
  5117. message_box(wgs->term_hwnd, buf, otherbuf,
  5118. MB_OK | MB_ICONEXCLAMATION, true, 0);
  5119. sfree(buf);
  5120. sfree(otherbuf);
  5121. conf_set_int(wgs->conf, CONF_beep, BELL_DEFAULT);
  5122. }
  5123. } else if (mode == BELL_PCSPEAKER) {
  5124. long beepdiff;
  5125. beepdiff = GetTickCount() - wgs->last_beep_time;
  5126. if (beepdiff >= 0 && beepdiff < 50)
  5127. return;
  5128. /*
  5129. * We must beep in different ways depending on whether this
  5130. * is a 95-series or NT-series OS.
  5131. */
  5132. if (osPlatformId == VER_PLATFORM_WIN32_NT)
  5133. Beep(800, 100);
  5134. else
  5135. MessageBeep(-1);
  5136. wgs->last_beep_time = GetTickCount();
  5137. }
  5138. /* Otherwise, either visual bell or disabled; do nothing here */
  5139. if (!wgs->term->has_focus) {
  5140. flash_window(wgs, 2); /* start */
  5141. }
  5142. }
  5143. /*
  5144. * Minimise or restore the window in response to a server-side
  5145. * request.
  5146. */
  5147. static void wintw_set_minimised(TermWin *tw, bool minimised)
  5148. {
  5149. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  5150. if (IsIconic(wgs->term_hwnd)) {
  5151. if (!minimised)
  5152. ShowWindow(wgs->term_hwnd, SW_RESTORE);
  5153. } else {
  5154. if (minimised)
  5155. ShowWindow(wgs->term_hwnd, SW_MINIMIZE);
  5156. }
  5157. }
  5158. /*
  5159. * Move the window in response to a server-side request.
  5160. */
  5161. static void wintw_move(TermWin *tw, int x, int y)
  5162. {
  5163. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  5164. int resize_action = conf_get_int(wgs->conf, CONF_resize_action);
  5165. if (resize_action == RESIZE_DISABLED ||
  5166. resize_action == RESIZE_FONT ||
  5167. IsZoomed(wgs->term_hwnd))
  5168. return;
  5169. SetWindowPos(wgs->term_hwnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
  5170. }
  5171. /*
  5172. * Move the window to the top or bottom of the z-order in response
  5173. * to a server-side request.
  5174. */
  5175. static void wintw_set_zorder(TermWin *tw, bool top)
  5176. {
  5177. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  5178. if (conf_get_bool(wgs->conf, CONF_alwaysontop))
  5179. return; /* ignore */
  5180. SetWindowPos(wgs->term_hwnd, top ? HWND_TOP : HWND_BOTTOM, 0, 0, 0, 0,
  5181. SWP_NOMOVE | SWP_NOSIZE);
  5182. }
  5183. /*
  5184. * Refresh the window in response to a server-side request.
  5185. */
  5186. static void wintw_refresh(TermWin *tw)
  5187. {
  5188. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  5189. InvalidateRect(wgs->term_hwnd, NULL, true);
  5190. }
  5191. /*
  5192. * Maximise or restore the window in response to a server-side
  5193. * request.
  5194. */
  5195. static void wintw_set_maximised(TermWin *tw, bool maximised)
  5196. {
  5197. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  5198. if (IsZoomed(wgs->term_hwnd)) {
  5199. if (!maximised)
  5200. ShowWindow(wgs->term_hwnd, SW_RESTORE);
  5201. } else {
  5202. if (maximised)
  5203. ShowWindow(wgs->term_hwnd, SW_MAXIMIZE);
  5204. }
  5205. }
  5206. /*
  5207. * See if we're in full-screen mode.
  5208. */
  5209. static bool is_full_screen(WinGuiSeat *wgs)
  5210. {
  5211. if (!IsZoomed(wgs->term_hwnd))
  5212. return false;
  5213. if (GetWindowLongPtr(wgs->term_hwnd, GWL_STYLE) & WS_CAPTION)
  5214. return false;
  5215. return true;
  5216. }
  5217. /* Get a MONITORINFO structure for the nearest available monitor, if the
  5218. * multimon API is available and returns success. Shared subroutine between
  5219. * get_fullscreen_rect() and get_workingarea_rect(). */
  5220. static bool get_monitor_info(WinGuiSeat *wgs, MONITORINFO *mi)
  5221. {
  5222. #if defined(MONITOR_DEFAULTTONEAREST) && !defined(NO_MULTIMON)
  5223. if (p_GetMonitorInfoA && p_MonitorFromWindow) {
  5224. HMONITOR mon;
  5225. mon = p_MonitorFromWindow(wgs->term_hwnd, MONITOR_DEFAULTTONEAREST);
  5226. mi->cbSize = sizeof(*mi);
  5227. p_GetMonitorInfoA(mon, mi);
  5228. return true;
  5229. }
  5230. #endif
  5231. return false;
  5232. }
  5233. /* Get the rect/size of a full-screen window on the nearest available
  5234. * monitor in multimon systems; default to something sensible if only
  5235. * one monitor is present. */
  5236. static bool get_fullscreen_rect(WinGuiSeat *wgs, RECT *ss)
  5237. {
  5238. #if defined(MONITOR_DEFAULTTONEAREST) && !defined(NO_MULTIMON)
  5239. MONITORINFO mi;
  5240. if (get_monitor_info(wgs, &mi)) {
  5241. /* structure copy */
  5242. *ss = mi.rcMonitor;
  5243. return true;
  5244. }
  5245. #endif
  5246. /* could also use code like this:
  5247. ss->left = ss->top = 0;
  5248. ss->right = GetSystemMetrics(SM_CXSCREEN);
  5249. ss->bottom = GetSystemMetrics(SM_CYSCREEN);
  5250. */
  5251. return GetClientRect(GetDesktopWindow(), ss);
  5252. }
  5253. /* Similar to get_fullscreen_rect, but retrieves the working area of the
  5254. * monitor (minus the taskbar) instead of its full extent. */
  5255. static bool get_workingarea_rect(WinGuiSeat *wgs, RECT *ss)
  5256. {
  5257. #if defined(MONITOR_DEFAULTTONEAREST) && !defined(NO_MULTIMON)
  5258. MONITORINFO mi;
  5259. if (get_monitor_info(wgs, &mi)) {
  5260. /* structure copy */
  5261. *ss = mi.rcWork;
  5262. return true;
  5263. }
  5264. #endif
  5265. /* Fallback is the same as get_monitor_rect, which is good _enough_:
  5266. * if the window overlaps the taskbar, that's not too bad a failure. */
  5267. return GetClientRect(GetDesktopWindow(), ss);
  5268. }
  5269. /*
  5270. * Go full-screen. This should only be called when we are already
  5271. * maximised.
  5272. */
  5273. static void make_full_screen(WinGuiSeat *wgs)
  5274. {
  5275. DWORD style;
  5276. RECT ss;
  5277. assert(IsZoomed(wgs->term_hwnd));
  5278. if (is_full_screen(wgs))
  5279. return;
  5280. /* Remove the window furniture. */
  5281. style = GetWindowLongPtr(wgs->term_hwnd, GWL_STYLE);
  5282. style &= ~(WS_CAPTION | WS_BORDER | WS_THICKFRAME);
  5283. if (conf_get_bool(wgs->conf, CONF_scrollbar_in_fullscreen))
  5284. style |= WS_VSCROLL;
  5285. else
  5286. style &= ~WS_VSCROLL;
  5287. SetWindowLongPtr(wgs->term_hwnd, GWL_STYLE, style);
  5288. /* Resize ourselves to exactly cover the nearest monitor. */
  5289. get_fullscreen_rect(wgs, &ss);
  5290. SetWindowPos(wgs->term_hwnd, HWND_TOP, ss.left, ss.top,
  5291. ss.right - ss.left, ss.bottom - ss.top, SWP_FRAMECHANGED);
  5292. /* We may have changed size as a result */
  5293. reset_window(wgs, 0);
  5294. /* Tick the menu item in the System and context menus. */
  5295. {
  5296. int i;
  5297. for (i = 0; i < lenof(wgs->popup_menus); i++)
  5298. CheckMenuItem(wgs->popup_menus[i].menu,
  5299. IDM_FULLSCREEN, MF_CHECKED);
  5300. }
  5301. }
  5302. /*
  5303. * Clear the full-screen attributes.
  5304. */
  5305. static void clear_full_screen(WinGuiSeat *wgs)
  5306. {
  5307. DWORD oldstyle, style;
  5308. /* Reinstate the window furniture. */
  5309. style = oldstyle = GetWindowLongPtr(wgs->term_hwnd, GWL_STYLE);
  5310. style |= WS_CAPTION | WS_BORDER;
  5311. if (conf_get_int(wgs->conf, CONF_resize_action) == RESIZE_DISABLED)
  5312. style &= ~WS_THICKFRAME;
  5313. else
  5314. style |= WS_THICKFRAME;
  5315. if (conf_get_bool(wgs->conf, CONF_scrollbar))
  5316. style |= WS_VSCROLL;
  5317. else
  5318. style &= ~WS_VSCROLL;
  5319. if (style != oldstyle) {
  5320. SetWindowLongPtr(wgs->term_hwnd, GWL_STYLE, style);
  5321. SetWindowPos(wgs->term_hwnd, NULL, 0, 0, 0, 0,
  5322. SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
  5323. SWP_FRAMECHANGED);
  5324. }
  5325. /* Untick the menu item in the System and context menus. */
  5326. {
  5327. int i;
  5328. for (i = 0; i < lenof(wgs->popup_menus); i++)
  5329. CheckMenuItem(wgs->popup_menus[i].menu,
  5330. IDM_FULLSCREEN, MF_UNCHECKED);
  5331. }
  5332. }
  5333. /*
  5334. * Toggle full-screen mode.
  5335. */
  5336. static void flip_full_screen(WinGuiSeat *wgs)
  5337. {
  5338. if (is_full_screen(wgs)) {
  5339. ShowWindow(wgs->term_hwnd, SW_RESTORE);
  5340. } else if (IsZoomed(wgs->term_hwnd)) {
  5341. make_full_screen(wgs);
  5342. } else {
  5343. SendMessage(wgs->term_hwnd, WM_FULLSCR_ON_MAX, 0, 0);
  5344. ShowWindow(wgs->term_hwnd, SW_MAXIMIZE);
  5345. }
  5346. }
  5347. static size_t win_seat_output(Seat *seat, SeatOutputType type,
  5348. const void *data, size_t len)
  5349. {
  5350. WinGuiSeat *wgs = container_of(seat, WinGuiSeat, seat);
  5351. return term_data(wgs->term, data, len);
  5352. }
  5353. static void wintw_unthrottle(TermWin *tw, size_t bufsize)
  5354. {
  5355. WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin);
  5356. if (wgs->backend)
  5357. backend_unthrottle(wgs->backend, bufsize);
  5358. }
  5359. static bool win_seat_eof(Seat *seat)
  5360. {
  5361. return true; /* do respond to incoming EOF with outgoing */
  5362. }
  5363. static SeatPromptResult win_seat_get_userpass_input(Seat *seat, prompts_t *p)
  5364. {
  5365. WinGuiSeat *wgs = container_of(seat, WinGuiSeat, seat);
  5366. SeatPromptResult spr;
  5367. spr = cmdline_get_passwd_input(p, &wgs->cmdline_get_passwd_state, true);
  5368. if (spr.kind == SPRK_INCOMPLETE)
  5369. spr = term_get_userpass_input(wgs->term, p);
  5370. return spr;
  5371. }
  5372. static void win_seat_set_trust_status(Seat *seat, bool trusted)
  5373. {
  5374. WinGuiSeat *wgs = container_of(seat, WinGuiSeat, seat);
  5375. term_set_trust_status(wgs->term, trusted);
  5376. }
  5377. static bool win_seat_can_set_trust_status(Seat *seat)
  5378. {
  5379. return true;
  5380. }
  5381. static bool win_seat_get_cursor_position(Seat *seat, int *x, int *y)
  5382. {
  5383. WinGuiSeat *wgs = container_of(seat, WinGuiSeat, seat);
  5384. term_get_cursor_position(wgs->term, x, y);
  5385. return true;
  5386. }
  5387. static bool win_seat_get_window_pixel_size(Seat *seat, int *x, int *y)
  5388. {
  5389. WinGuiSeat *wgs = container_of(seat, WinGuiSeat, seat);
  5390. RECT r;
  5391. GetWindowRect(wgs->term_hwnd, &r);
  5392. *x = r.right - r.left;
  5393. *y = r.bottom - r.top;
  5394. return true;
  5395. }