window.c 206 KB

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