window.c 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <time.h>
  5. #include <assert.h>
  6. #define PUTTY_DO_GLOBALS /* actually _define_ globals */
  7. #include "putty.h"
  8. #include "terminal.h"
  9. #include "storage.h"
  10. #include "win_res.h"
  11. #ifndef NO_MULTIMON
  12. #if WINVER < 0x0500
  13. #define COMPILE_MULTIMON_STUBS
  14. #include <multimon.h>
  15. #endif
  16. #endif
  17. #include <imm.h>
  18. #include <commctrl.h>
  19. #include <richedit.h>
  20. #include <mmsystem.h>
  21. #define IDM_SHOWLOG 0x0010
  22. #define IDM_NEWSESS 0x0020
  23. #define IDM_DUPSESS 0x0030
  24. #define IDM_RESTART 0x0040
  25. #define IDM_RECONF 0x0050
  26. #define IDM_CLRSB 0x0060
  27. #define IDM_RESET 0x0070
  28. #define IDM_HELP 0x0140
  29. #define IDM_ABOUT 0x0150
  30. #define IDM_SAVEDSESS 0x0160
  31. #define IDM_COPYALL 0x0170
  32. #define IDM_FULLSCREEN 0x0180
  33. #define IDM_PASTE 0x0190
  34. #define IDM_SESSLGP 0x0250 /* log type printable */
  35. #define IDM_SESSLGA 0x0260 /* log type all chars */
  36. #define IDM_SESSLGE 0x0270 /* log end */
  37. #define IDM_SPECIAL_MIN 0x0400
  38. #define IDM_SPECIAL_MAX 0x0800
  39. #define IDM_SAVED_MIN 0x1000
  40. #define IDM_SAVED_MAX 0x2000
  41. #define WM_IGNORE_CLIP (WM_XUSER + 2)
  42. #define WM_FULLSCR_ON_MAX (WM_XUSER + 3)
  43. #define WM_AGENT_CALLBACK (WM_XUSER + 4)
  44. /* Needed for Chinese support and apparently not always defined. */
  45. #ifndef VK_PROCESSKEY
  46. #define VK_PROCESSKEY 0xE5
  47. #endif
  48. /* Mouse wheel support. */
  49. #ifndef WM_MOUSEWHEEL
  50. #define WM_MOUSEWHEEL 0x020A /* not defined in earlier SDKs */
  51. #endif
  52. #ifndef WHEEL_DELTA
  53. #define WHEEL_DELTA 120
  54. #endif
  55. static Mouse_Button translate_button(Mouse_Button button);
  56. static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  57. static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
  58. unsigned char *output);
  59. static void cfgtopalette(void);
  60. static void systopalette(void);
  61. static void init_palette(void);
  62. static void init_fonts(int, int);
  63. static void another_font(int);
  64. static void deinit_fonts(void);
  65. static void set_input_locale(HKL);
  66. static int is_full_screen(void);
  67. static void make_full_screen(void);
  68. static void clear_full_screen(void);
  69. static void flip_full_screen(void);
  70. /* Window layout information */
  71. static void reset_window(int);
  72. static int extra_width, extra_height;
  73. static int font_width, font_height, font_dualwidth;
  74. static int offset_width, offset_height;
  75. static int was_zoomed = 0;
  76. static int prev_rows, prev_cols;
  77. static int pending_netevent = 0;
  78. static WPARAM pend_netevent_wParam = 0;
  79. static LPARAM pend_netevent_lParam = 0;
  80. static void enact_pending_netevent(void);
  81. static void flash_window(int mode);
  82. static void sys_cursor_update(void);
  83. static int is_shift_pressed(void);
  84. static int get_fullscreen_rect(RECT * ss);
  85. static time_t last_movement = 0;
  86. static int caret_x = -1, caret_y = -1;
  87. static int kbd_codepage;
  88. static void *ldisc;
  89. static Backend *back;
  90. static void *backhandle;
  91. static struct unicode_data ucsdata;
  92. static int session_closed;
  93. static const struct telnet_special *specials;
  94. static int n_specials;
  95. static struct {
  96. HMENU menu;
  97. int specials_submenu_pos;
  98. } popup_menus[2];
  99. enum { SYSMENU, CTXMENU };
  100. Config cfg; /* exported to windlg.c */
  101. extern struct sesslist sesslist; /* imported from windlg.c */
  102. struct agent_callback {
  103. void (*callback)(void *, void *, int);
  104. void *callback_ctx;
  105. void *data;
  106. int len;
  107. };
  108. #define FONT_NORMAL 0
  109. #define FONT_BOLD 1
  110. #define FONT_UNDERLINE 2
  111. #define FONT_BOLDUND 3
  112. #define FONT_WIDE 0x04
  113. #define FONT_HIGH 0x08
  114. #define FONT_NARROW 0x10
  115. #define FONT_OEM 0x20
  116. #define FONT_OEMBOLD 0x21
  117. #define FONT_OEMUND 0x22
  118. #define FONT_OEMBOLDUND 0x23
  119. #define FONT_MAXNO 0x2F
  120. #define FONT_SHIFT 5
  121. static HFONT fonts[FONT_MAXNO];
  122. static LOGFONT lfont;
  123. static int fontflag[FONT_MAXNO];
  124. static enum {
  125. BOLD_COLOURS, BOLD_SHADOW, BOLD_FONT
  126. } bold_mode;
  127. static enum {
  128. UND_LINE, UND_FONT
  129. } und_mode;
  130. static int descent;
  131. #define NCOLOURS 24
  132. static COLORREF colours[NCOLOURS];
  133. static HPALETTE pal;
  134. static LPLOGPALETTE logpal;
  135. static RGBTRIPLE defpal[NCOLOURS];
  136. static HWND hwnd;
  137. static HBITMAP caretbm;
  138. static int dbltime, lasttime, lastact;
  139. static Mouse_Button lastbtn;
  140. /* this allows xterm-style mouse handling. */
  141. static int send_raw_mouse = 0;
  142. static int wheel_accumulator = 0;
  143. static char *window_name, *icon_name;
  144. static int compose_state = 0;
  145. static UINT wm_mousewheel = WM_MOUSEWHEEL;
  146. /* Dummy routine, only required in plink. */
  147. void ldisc_update(void *frontend, int echo, int edit)
  148. {
  149. }
  150. static void start_backend(void)
  151. {
  152. const char *error;
  153. char msg[1024], *title;
  154. char *realhost;
  155. int i;
  156. /*
  157. * Select protocol. This is farmed out into a table in a
  158. * separate file to enable an ssh-free variant.
  159. */
  160. back = NULL;
  161. for (i = 0; backends[i].backend != NULL; i++)
  162. if (backends[i].protocol == cfg.protocol) {
  163. back = backends[i].backend;
  164. break;
  165. }
  166. if (back == NULL) {
  167. char *str = dupprintf("%s Internal Error", appname);
  168. MessageBox(NULL, "Unsupported protocol number found",
  169. str, MB_OK | MB_ICONEXCLAMATION);
  170. sfree(str);
  171. cleanup_exit(1);
  172. }
  173. error = back->init(NULL, &backhandle, &cfg,
  174. cfg.host, cfg.port, &realhost, cfg.tcp_nodelay,
  175. cfg.tcp_keepalives);
  176. back->provide_logctx(backhandle, logctx);
  177. if (error) {
  178. char *str = dupprintf("%s Error", appname);
  179. sprintf(msg, "Unable to open connection to\n"
  180. "%.800s\n" "%s", cfg.host, error);
  181. MessageBox(NULL, msg, str, MB_ICONERROR | MB_OK);
  182. sfree(str);
  183. exit(0);
  184. }
  185. window_name = icon_name = NULL;
  186. if (*cfg.wintitle) {
  187. title = cfg.wintitle;
  188. } else {
  189. sprintf(msg, "%s - %s", realhost, appname);
  190. title = msg;
  191. }
  192. sfree(realhost);
  193. set_title(NULL, title);
  194. set_icon(NULL, title);
  195. /*
  196. * Connect the terminal to the backend for resize purposes.
  197. */
  198. term_provide_resize_fn(term, back->size, backhandle);
  199. /*
  200. * Set up a line discipline.
  201. */
  202. ldisc = ldisc_create(&cfg, term, back, backhandle, NULL);
  203. /*
  204. * Destroy the Restart Session menu item. (This will return
  205. * failure if it's already absent, as it will be the very first
  206. * time we call this function. We ignore that, because as long
  207. * as the menu item ends up not being there, we don't care
  208. * whether it was us who removed it or not!)
  209. */
  210. for (i = 0; i < lenof(popup_menus); i++) {
  211. DeleteMenu(popup_menus[i].menu, IDM_RESTART, MF_BYCOMMAND);
  212. }
  213. session_closed = FALSE;
  214. }
  215. static void close_session(void)
  216. {
  217. char morestuff[100];
  218. int i;
  219. session_closed = TRUE;
  220. sprintf(morestuff, "%.70s (inactive)", appname);
  221. set_icon(NULL, morestuff);
  222. set_title(NULL, morestuff);
  223. if (ldisc) {
  224. ldisc_free(ldisc);
  225. ldisc = NULL;
  226. }
  227. if (back) {
  228. back->free(backhandle);
  229. backhandle = NULL;
  230. back = NULL;
  231. update_specials_menu(NULL);
  232. }
  233. /*
  234. * Show the Restart Session menu item. Do a precautionary
  235. * delete first to ensure we never end up with more than one.
  236. */
  237. for (i = 0; i < lenof(popup_menus); i++) {
  238. DeleteMenu(popup_menus[i].menu, IDM_RESTART, MF_BYCOMMAND);
  239. InsertMenu(popup_menus[i].menu, IDM_DUPSESS, MF_BYCOMMAND | MF_ENABLED,
  240. IDM_RESTART, "&Restart Session");
  241. }
  242. }
  243. int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
  244. {
  245. WNDCLASS wndclass;
  246. MSG msg;
  247. int guess_width, guess_height;
  248. hinst = inst;
  249. flags = FLAG_VERBOSE | FLAG_INTERACTIVE;
  250. sk_init();
  251. InitCommonControls();
  252. /* Ensure a Maximize setting in Explorer doesn't maximise the
  253. * config box. */
  254. defuse_showwindow();
  255. if (!init_winver())
  256. {
  257. char *str = dupprintf("%s Fatal Error", appname);
  258. MessageBox(NULL, "Windows refuses to report a version",
  259. str, MB_OK | MB_ICONEXCLAMATION);
  260. sfree(str);
  261. return 1;
  262. }
  263. /*
  264. * If we're running a version of Windows that doesn't support
  265. * WM_MOUSEWHEEL, find out what message number we should be
  266. * using instead.
  267. */
  268. if (osVersion.dwMajorVersion < 4 ||
  269. (osVersion.dwMajorVersion == 4 &&
  270. osVersion.dwPlatformId != VER_PLATFORM_WIN32_NT))
  271. wm_mousewheel = RegisterWindowMessage("MSWHEEL_ROLLMSG");
  272. /*
  273. * See if we can find our Help file.
  274. */
  275. {
  276. char b[2048], *p, *q, *r;
  277. FILE *fp;
  278. GetModuleFileName(NULL, b, sizeof(b) - 1);
  279. r = b;
  280. p = strrchr(b, '\\');
  281. if (p && p >= r) r = p+1;
  282. q = strrchr(b, ':');
  283. if (q && q >= r) r = q+1;
  284. strcpy(r, "putty.hlp");
  285. if ( (fp = fopen(b, "r")) != NULL) {
  286. help_path = dupstr(b);
  287. fclose(fp);
  288. } else
  289. help_path = NULL;
  290. strcpy(r, "putty.cnt");
  291. if ( (fp = fopen(b, "r")) != NULL) {
  292. help_has_contents = TRUE;
  293. fclose(fp);
  294. } else
  295. help_has_contents = FALSE;
  296. }
  297. /*
  298. * Process the command line.
  299. */
  300. {
  301. char *p;
  302. int got_host = 0;
  303. default_protocol = be_default_protocol;
  304. /* Find the appropriate default port. */
  305. {
  306. int i;
  307. default_port = 0; /* illegal */
  308. for (i = 0; backends[i].backend != NULL; i++)
  309. if (backends[i].protocol == default_protocol) {
  310. default_port = backends[i].backend->default_port;
  311. break;
  312. }
  313. }
  314. cfg.logtype = LGTYP_NONE;
  315. do_defaults(NULL, &cfg);
  316. p = cmdline;
  317. /*
  318. * Process a couple of command-line options which are more
  319. * easily dealt with before the line is broken up into
  320. * words. These are the soon-to-be-defunct @sessionname and
  321. * the internal-use-only &sharedmemoryhandle, neither of
  322. * which are combined with anything else.
  323. */
  324. while (*p && isspace(*p))
  325. p++;
  326. if (*p == '@') {
  327. int i = strlen(p);
  328. while (i > 1 && isspace(p[i - 1]))
  329. i--;
  330. p[i] = '\0';
  331. do_defaults(p + 1, &cfg);
  332. if (!*cfg.host && !do_config()) {
  333. cleanup_exit(0);
  334. }
  335. } else if (*p == '&') {
  336. /*
  337. * An initial & means we've been given a command line
  338. * containing the hex value of a HANDLE for a file
  339. * mapping object, which we must then extract as a
  340. * config.
  341. */
  342. HANDLE filemap;
  343. Config *cp;
  344. if (sscanf(p + 1, "%p", &filemap) == 1 &&
  345. (cp = MapViewOfFile(filemap, FILE_MAP_READ,
  346. 0, 0, sizeof(Config))) != NULL) {
  347. cfg = *cp;
  348. UnmapViewOfFile(cp);
  349. CloseHandle(filemap);
  350. } else if (!do_config()) {
  351. cleanup_exit(0);
  352. }
  353. } else {
  354. /*
  355. * Otherwise, break up the command line and deal with
  356. * it sensibly.
  357. */
  358. int argc, i;
  359. char **argv;
  360. split_into_argv(cmdline, &argc, &argv, NULL);
  361. for (i = 0; i < argc; i++) {
  362. char *p = argv[i];
  363. int ret;
  364. ret = cmdline_process_param(p, i+1<argc?argv[i+1]:NULL,
  365. 1, &cfg);
  366. if (ret == -2) {
  367. cmdline_error("option \"%s\" requires an argument", p);
  368. } else if (ret == 2) {
  369. i++; /* skip next argument */
  370. } else if (ret == 1) {
  371. continue; /* nothing further needs doing */
  372. } else if (!strcmp(p, "-cleanup")) {
  373. /*
  374. * `putty -cleanup'. Remove all registry
  375. * entries associated with PuTTY, and also find
  376. * and delete the random seed file.
  377. */
  378. char *s1, *s2;
  379. s1 = dupprintf("This procedure will remove ALL Registry\n"
  380. "entries associated with %s, and will\n"
  381. "also remove the random seed file.\n"
  382. "\n"
  383. "THIS PROCESS WILL DESTROY YOUR SAVED\n"
  384. "SESSIONS. Are you really sure you want\n"
  385. "to continue?", appname);
  386. s2 = dupprintf("%s Warning", appname);
  387. if (MessageBox(NULL, s1, s2,
  388. MB_YESNO | MB_ICONWARNING) == IDYES) {
  389. cleanup_all();
  390. }
  391. sfree(s1);
  392. sfree(s2);
  393. exit(0);
  394. } else if (*p != '-') {
  395. char *q = p;
  396. if (got_host) {
  397. /*
  398. * If we already have a host name, treat
  399. * this argument as a port number. NB we
  400. * have to treat this as a saved -P
  401. * argument, so that it will be deferred
  402. * until it's a good moment to run it.
  403. */
  404. int ret = cmdline_process_param("-P", p, 1, &cfg);
  405. assert(ret == 2);
  406. } else if (!strncmp(q, "telnet:", 7)) {
  407. /*
  408. * If the hostname starts with "telnet:",
  409. * set the protocol to Telnet and process
  410. * the string as a Telnet URL.
  411. */
  412. char c;
  413. q += 7;
  414. if (q[0] == '/' && q[1] == '/')
  415. q += 2;
  416. cfg.protocol = PROT_TELNET;
  417. p = q;
  418. while (*p && *p != ':' && *p != '/')
  419. p++;
  420. c = *p;
  421. if (*p)
  422. *p++ = '\0';
  423. if (c == ':')
  424. cfg.port = atoi(p);
  425. else
  426. cfg.port = -1;
  427. strncpy(cfg.host, q, sizeof(cfg.host) - 1);
  428. cfg.host[sizeof(cfg.host) - 1] = '\0';
  429. got_host = 1;
  430. } else {
  431. /*
  432. * Otherwise, treat this argument as a host
  433. * name.
  434. */
  435. while (*p && !isspace(*p))
  436. p++;
  437. if (*p)
  438. *p++ = '\0';
  439. strncpy(cfg.host, q, sizeof(cfg.host) - 1);
  440. cfg.host[sizeof(cfg.host) - 1] = '\0';
  441. got_host = 1;
  442. }
  443. } else {
  444. cmdline_error("unknown option \"%s\"", p);
  445. }
  446. }
  447. }
  448. cmdline_run_saved(&cfg);
  449. if (!*cfg.host && !do_config()) {
  450. cleanup_exit(0);
  451. }
  452. /*
  453. * Trim leading whitespace off the hostname if it's there.
  454. */
  455. {
  456. int space = strspn(cfg.host, " \t");
  457. memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space);
  458. }
  459. /* See if host is of the form user@host */
  460. if (cfg.host[0] != '\0') {
  461. char *atsign = strrchr(cfg.host, '@');
  462. /* Make sure we're not overflowing the user field */
  463. if (atsign) {
  464. if (atsign - cfg.host < sizeof cfg.username) {
  465. strncpy(cfg.username, cfg.host, atsign - cfg.host);
  466. cfg.username[atsign - cfg.host] = '\0';
  467. }
  468. memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1));
  469. }
  470. }
  471. /*
  472. * Trim a colon suffix off the hostname if it's there.
  473. */
  474. cfg.host[strcspn(cfg.host, ":")] = '\0';
  475. /*
  476. * Remove any remaining whitespace from the hostname.
  477. */
  478. {
  479. int p1 = 0, p2 = 0;
  480. while (cfg.host[p2] != '\0') {
  481. if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') {
  482. cfg.host[p1] = cfg.host[p2];
  483. p1++;
  484. }
  485. p2++;
  486. }
  487. cfg.host[p1] = '\0';
  488. }
  489. }
  490. /* Check for invalid Port number (i.e. zero) */
  491. if (cfg.port == 0) {
  492. char *str = dupprintf("%s Internal Error", appname);
  493. MessageBox(NULL, "Invalid Port Number",
  494. str, MB_OK | MB_ICONEXCLAMATION);
  495. sfree(str);
  496. cleanup_exit(1);
  497. }
  498. if (!prev) {
  499. wndclass.style = 0;
  500. wndclass.lpfnWndProc = WndProc;
  501. wndclass.cbClsExtra = 0;
  502. wndclass.cbWndExtra = 0;
  503. wndclass.hInstance = inst;
  504. wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(IDI_MAINICON));
  505. wndclass.hCursor = LoadCursor(NULL, IDC_IBEAM);
  506. wndclass.hbrBackground = NULL;
  507. wndclass.lpszMenuName = NULL;
  508. wndclass.lpszClassName = appname;
  509. RegisterClass(&wndclass);
  510. }
  511. hwnd = NULL;
  512. memset(&ucsdata, 0, sizeof(ucsdata));
  513. term = term_init(&cfg, &ucsdata, NULL);
  514. logctx = log_init(NULL, &cfg);
  515. term_provide_logctx(term, logctx);
  516. cfgtopalette();
  517. /*
  518. * Guess some defaults for the window size. This all gets
  519. * updated later, so we don't really care too much. However, we
  520. * do want the font width/height guesses to correspond to a
  521. * large font rather than a small one...
  522. */
  523. font_width = 10;
  524. font_height = 20;
  525. extra_width = 25;
  526. extra_height = 28;
  527. term_size(term, cfg.height, cfg.width, cfg.savelines);
  528. guess_width = extra_width + font_width * term->cols;
  529. guess_height = extra_height + font_height * term->rows;
  530. {
  531. RECT r;
  532. get_fullscreen_rect(&r);
  533. if (guess_width > r.right - r.left)
  534. guess_width = r.right - r.left;
  535. if (guess_height > r.bottom - r.top)
  536. guess_height = r.bottom - r.top;
  537. }
  538. {
  539. int winmode = WS_OVERLAPPEDWINDOW | WS_VSCROLL;
  540. int exwinmode = 0;
  541. if (!cfg.scrollbar)
  542. winmode &= ~(WS_VSCROLL);
  543. if (cfg.resize_action == RESIZE_DISABLED)
  544. winmode &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX);
  545. if (cfg.alwaysontop)
  546. exwinmode |= WS_EX_TOPMOST;
  547. if (cfg.sunken_edge)
  548. exwinmode |= WS_EX_CLIENTEDGE;
  549. hwnd = CreateWindowEx(exwinmode, appname, appname,
  550. winmode, CW_USEDEFAULT, CW_USEDEFAULT,
  551. guess_width, guess_height,
  552. NULL, NULL, inst, NULL);
  553. }
  554. /*
  555. * Initialise the fonts, simultaneously correcting the guesses
  556. * for font_{width,height}.
  557. */
  558. init_fonts(0,0);
  559. /*
  560. * Correct the guesses for extra_{width,height}.
  561. */
  562. {
  563. RECT cr, wr;
  564. GetWindowRect(hwnd, &wr);
  565. GetClientRect(hwnd, &cr);
  566. offset_width = offset_height = cfg.window_border;
  567. extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
  568. extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
  569. }
  570. /*
  571. * Resize the window, now we know what size we _really_ want it
  572. * to be.
  573. */
  574. guess_width = extra_width + font_width * term->cols;
  575. guess_height = extra_height + font_height * term->rows;
  576. SetWindowPos(hwnd, NULL, 0, 0, guess_width, guess_height,
  577. SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER);
  578. /*
  579. * Set up a caret bitmap, with no content.
  580. */
  581. {
  582. char *bits;
  583. int size = (font_width + 15) / 16 * 2 * font_height;
  584. bits = snewn(size, char);
  585. memset(bits, 0, size);
  586. caretbm = CreateBitmap(font_width, font_height, 1, 1, bits);
  587. sfree(bits);
  588. }
  589. CreateCaret(hwnd, caretbm, font_width, font_height);
  590. /*
  591. * Initialise the scroll bar.
  592. */
  593. {
  594. SCROLLINFO si;
  595. si.cbSize = sizeof(si);
  596. si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
  597. si.nMin = 0;
  598. si.nMax = term->rows - 1;
  599. si.nPage = term->rows;
  600. si.nPos = 0;
  601. SetScrollInfo(hwnd, SB_VERT, &si, FALSE);
  602. }
  603. /*
  604. * Prepare the mouse handler.
  605. */
  606. lastact = MA_NOTHING;
  607. lastbtn = MBT_NOTHING;
  608. dbltime = GetDoubleClickTime();
  609. /*
  610. * Set up the session-control options on the system menu.
  611. */
  612. {
  613. HMENU s, m;
  614. int i, j;
  615. char *str;
  616. popup_menus[SYSMENU].menu = GetSystemMenu(hwnd, FALSE);
  617. popup_menus[CTXMENU].menu = CreatePopupMenu();
  618. AppendMenu(popup_menus[CTXMENU].menu, MF_ENABLED, IDM_PASTE, "&Paste");
  619. s = CreateMenu();
  620. get_sesslist(&sesslist, TRUE);
  621. for (i = 1;
  622. i < ((sesslist.nsessions < 256) ? sesslist.nsessions : 256);
  623. i++)
  624. AppendMenu(s, MF_ENABLED, IDM_SAVED_MIN + (16 * i),
  625. sesslist.sessions[i]);
  626. for (j = 0; j < lenof(popup_menus); j++) {
  627. m = popup_menus[j].menu;
  628. AppendMenu(m, MF_SEPARATOR, 0, 0);
  629. popup_menus[j].specials_submenu_pos = GetMenuItemCount(m);
  630. AppendMenu(m, MF_ENABLED, IDM_SHOWLOG, "&Event Log");
  631. AppendMenu(m, MF_SEPARATOR, 0, 0);
  632. AppendMenu(m, MF_ENABLED, IDM_NEWSESS, "Ne&w Session...");
  633. AppendMenu(m, MF_ENABLED, IDM_DUPSESS, "&Duplicate Session");
  634. AppendMenu(m, MF_POPUP | MF_ENABLED, (UINT) s, "Sa&ved Sessions");
  635. AppendMenu(m, MF_ENABLED, IDM_RECONF, "Chan&ge Settings...");
  636. AppendMenu(m, MF_SEPARATOR, 0, 0);
  637. AppendMenu(m, MF_ENABLED, IDM_COPYALL, "C&opy All to Clipboard");
  638. AppendMenu(m, MF_ENABLED, IDM_CLRSB, "C&lear Scrollback");
  639. AppendMenu(m, MF_ENABLED, IDM_RESET, "Rese&t Terminal");
  640. AppendMenu(m, MF_SEPARATOR, 0, 0);
  641. AppendMenu(m, (cfg.resize_action == RESIZE_DISABLED) ?
  642. MF_GRAYED : MF_ENABLED, IDM_FULLSCREEN, "&Full Screen");
  643. AppendMenu(m, MF_SEPARATOR, 0, 0);
  644. if (help_path)
  645. AppendMenu(m, MF_ENABLED, IDM_HELP, "&Help");
  646. str = dupprintf("&About %s", appname);
  647. AppendMenu(m, MF_ENABLED, IDM_ABOUT, str);
  648. sfree(str);
  649. }
  650. }
  651. start_backend();
  652. /*
  653. * Set up the initial input locale.
  654. */
  655. set_input_locale(GetKeyboardLayout(0));
  656. /*
  657. * Open the initial log file if there is one.
  658. */
  659. logfopen(logctx);
  660. /*
  661. * Finally show the window!
  662. */
  663. ShowWindow(hwnd, show);
  664. SetForegroundWindow(hwnd);
  665. /*
  666. * Set the palette up.
  667. */
  668. pal = NULL;
  669. logpal = NULL;
  670. init_palette();
  671. term->has_focus = (GetForegroundWindow() == hwnd);
  672. UpdateWindow(hwnd);
  673. if (GetMessage(&msg, NULL, 0, 0) == 1) {
  674. int timer_id = 0, long_timer = 0;
  675. while (msg.message != WM_QUIT) {
  676. /* Sometimes DispatchMessage calls routines that use their own
  677. * GetMessage loop, setup this timer so we get some control back.
  678. *
  679. * Also call term_update() from the timer so that if the host
  680. * is sending data flat out we still do redraws.
  681. */
  682. if (timer_id && long_timer) {
  683. KillTimer(hwnd, timer_id);
  684. long_timer = timer_id = 0;
  685. }
  686. if (!timer_id)
  687. timer_id = SetTimer(hwnd, 1, 20, NULL);
  688. if (!(IsWindow(logbox) && IsDialogMessage(logbox, &msg)))
  689. DispatchMessage(&msg);
  690. /* Make sure we blink everything that needs it. */
  691. term_blink(term, 0);
  692. /* Send the paste buffer if there's anything to send */
  693. term_paste(term);
  694. /* If there's nothing new in the queue then we can do everything
  695. * we've delayed, reading the socket, writing, and repainting
  696. * the window.
  697. */
  698. if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  699. continue;
  700. if (pending_netevent) {
  701. enact_pending_netevent();
  702. if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  703. continue;
  704. }
  705. /* Okay there is now nothing to do so we make sure the screen is
  706. * completely up to date then tell windows to call us in a little
  707. * while.
  708. */
  709. if (timer_id) {
  710. KillTimer(hwnd, timer_id);
  711. timer_id = 0;
  712. }
  713. HideCaret(hwnd);
  714. if (GetCapture() != hwnd ||
  715. (send_raw_mouse &&
  716. !(cfg.mouse_override && is_shift_pressed())))
  717. term_out(term);
  718. term_update(term);
  719. ShowCaret(hwnd);
  720. flash_window(1); /* maintain */
  721. /* The messages seem unreliable; especially if we're being tricky */
  722. term->has_focus = (GetForegroundWindow() == hwnd);
  723. if (term->in_vbell)
  724. /* Hmm, term_update didn't want to do an update too soon ... */
  725. timer_id = SetTimer(hwnd, 1, 50, NULL);
  726. else if (!term->has_focus)
  727. timer_id = SetTimer(hwnd, 1, 500, NULL);
  728. else
  729. timer_id = SetTimer(hwnd, 1, 100, NULL);
  730. long_timer = 1;
  731. /* There's no point rescanning everything in the message queue
  732. * so we do an apparently unnecessary wait here
  733. */
  734. WaitMessage();
  735. if (GetMessage(&msg, NULL, 0, 0) != 1)
  736. break;
  737. }
  738. }
  739. cleanup_exit(msg.wParam); /* this doesn't return... */
  740. return msg.wParam; /* ... but optimiser doesn't know */
  741. }
  742. /*
  743. * Clean up and exit.
  744. */
  745. void cleanup_exit(int code)
  746. {
  747. /*
  748. * Clean up.
  749. */
  750. deinit_fonts();
  751. sfree(logpal);
  752. if (pal)
  753. DeleteObject(pal);
  754. sk_cleanup();
  755. if (cfg.protocol == PROT_SSH) {
  756. random_save_seed();
  757. #ifdef MSCRYPTOAPI
  758. crypto_wrapup();
  759. #endif
  760. }
  761. exit(code);
  762. }
  763. /*
  764. * Set up, or shut down, an AsyncSelect. Called from winnet.c.
  765. */
  766. char *do_select(SOCKET skt, int startup)
  767. {
  768. int msg, events;
  769. if (startup) {
  770. msg = WM_NETEVENT;
  771. events = (FD_CONNECT | FD_READ | FD_WRITE |
  772. FD_OOB | FD_CLOSE | FD_ACCEPT);
  773. } else {
  774. msg = events = 0;
  775. }
  776. if (!hwnd)
  777. return "do_select(): internal error (hwnd==NULL)";
  778. if (p_WSAAsyncSelect(skt, hwnd, msg, events) == SOCKET_ERROR) {
  779. switch (p_WSAGetLastError()) {
  780. case WSAENETDOWN:
  781. return "Network is down";
  782. default:
  783. return "WSAAsyncSelect(): unknown error";
  784. }
  785. }
  786. return NULL;
  787. }
  788. /*
  789. * Update the Special Commands submenu.
  790. */
  791. void update_specials_menu(void *frontend)
  792. {
  793. HMENU p;
  794. int menu_already_exists = (specials != NULL);
  795. int i, j;
  796. if (back)
  797. specials = back->get_specials(backhandle);
  798. else
  799. specials = NULL;
  800. if (specials) {
  801. /* We can't use Windows to provide a stack for submenus, so
  802. * here's a lame "stack" that will do for now. */
  803. HMENU saved_menu = NULL;
  804. int nesting = 1;
  805. p = CreatePopupMenu();
  806. for (i = 0; nesting > 0; i++) {
  807. assert(IDM_SPECIAL_MIN + 0x10 * i < IDM_SPECIAL_MAX);
  808. switch (specials[i].code) {
  809. case TS_SEP:
  810. AppendMenu(p, MF_SEPARATOR, 0, 0);
  811. break;
  812. case TS_SUBMENU:
  813. assert(nesting < 2);
  814. nesting++;
  815. saved_menu = p; /* XXX lame stacking */
  816. p = CreatePopupMenu();
  817. AppendMenu(saved_menu, MF_POPUP | MF_ENABLED,
  818. (UINT) p, specials[i].name);
  819. break;
  820. case TS_EXITMENU:
  821. nesting--;
  822. if (nesting) {
  823. p = saved_menu; /* XXX lame stacking */
  824. saved_menu = NULL;
  825. }
  826. break;
  827. default:
  828. AppendMenu(p, MF_ENABLED, IDM_SPECIAL_MIN + 0x10 * i,
  829. specials[i].name);
  830. break;
  831. }
  832. }
  833. /* Squirrel the highest special. */
  834. n_specials = i - 1;
  835. } else {
  836. p = NULL;
  837. n_specials = 0;
  838. }
  839. for (j = 0; j < lenof(popup_menus); j++) {
  840. if (menu_already_exists) {
  841. /* XXX does this free up all submenus? */
  842. DeleteMenu(popup_menus[j].menu,
  843. popup_menus[j].specials_submenu_pos,
  844. MF_BYPOSITION);
  845. DeleteMenu(popup_menus[j].menu,
  846. popup_menus[j].specials_submenu_pos,
  847. MF_BYPOSITION);
  848. }
  849. if (specials) {
  850. InsertMenu(popup_menus[j].menu,
  851. popup_menus[j].specials_submenu_pos,
  852. MF_BYPOSITION | MF_SEPARATOR, 0, 0);
  853. InsertMenu(popup_menus[j].menu,
  854. popup_menus[j].specials_submenu_pos,
  855. MF_BYPOSITION | MF_POPUP | MF_ENABLED,
  856. (UINT) p, "S&pecial Command");
  857. }
  858. }
  859. }
  860. /*
  861. * set or clear the "raw mouse message" mode
  862. */
  863. void set_raw_mouse_mode(void *frontend, int activate)
  864. {
  865. activate = activate && !cfg.no_mouse_rep;
  866. send_raw_mouse = activate;
  867. SetCursor(LoadCursor(NULL, activate ? IDC_ARROW : IDC_IBEAM));
  868. }
  869. /*
  870. * Print a message box and close the connection.
  871. */
  872. void connection_fatal(void *frontend, char *fmt, ...)
  873. {
  874. va_list ap;
  875. char *stuff, morestuff[100];
  876. va_start(ap, fmt);
  877. stuff = dupvprintf(fmt, ap);
  878. va_end(ap);
  879. sprintf(morestuff, "%.70s Fatal Error", appname);
  880. MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
  881. sfree(stuff);
  882. if (cfg.close_on_exit == FORCE_ON)
  883. PostQuitMessage(1);
  884. else {
  885. close_session();
  886. }
  887. }
  888. /*
  889. * Report an error at the command-line parsing stage.
  890. */
  891. void cmdline_error(char *fmt, ...)
  892. {
  893. va_list ap;
  894. char *stuff, morestuff[100];
  895. va_start(ap, fmt);
  896. stuff = dupvprintf(fmt, ap);
  897. va_end(ap);
  898. sprintf(morestuff, "%.70s Command Line Error", appname);
  899. MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
  900. sfree(stuff);
  901. exit(1);
  902. }
  903. /*
  904. * Actually do the job requested by a WM_NETEVENT
  905. */
  906. static void enact_pending_netevent(void)
  907. {
  908. static int reentering = 0;
  909. extern int select_result(WPARAM, LPARAM);
  910. int ret;
  911. if (reentering)
  912. return; /* don't unpend the pending */
  913. pending_netevent = FALSE;
  914. reentering = 1;
  915. ret = select_result(pend_netevent_wParam, pend_netevent_lParam);
  916. reentering = 0;
  917. if (ret == 0 && !session_closed) {
  918. /* Abnormal exits will already have set session_closed and taken
  919. * appropriate action. */
  920. if (cfg.close_on_exit == FORCE_ON ||
  921. cfg.close_on_exit == AUTO) PostQuitMessage(0);
  922. else {
  923. close_session();
  924. session_closed = TRUE;
  925. MessageBox(hwnd, "Connection closed by remote host",
  926. appname, MB_OK | MB_ICONINFORMATION);
  927. }
  928. }
  929. }
  930. /*
  931. * Copy the colour palette from the configuration data into defpal.
  932. * This is non-trivial because the colour indices are different.
  933. */
  934. static void cfgtopalette(void)
  935. {
  936. int i;
  937. static const int ww[] = {
  938. 6, 7, 8, 9, 10, 11, 12, 13,
  939. 14, 15, 16, 17, 18, 19, 20, 21,
  940. 0, 1, 2, 3, 4, 4, 5, 5
  941. };
  942. for (i = 0; i < 24; i++) {
  943. int w = ww[i];
  944. defpal[i].rgbtRed = cfg.colours[w][0];
  945. defpal[i].rgbtGreen = cfg.colours[w][1];
  946. defpal[i].rgbtBlue = cfg.colours[w][2];
  947. }
  948. /* Override with system colours if appropriate */
  949. if (cfg.system_colour)
  950. systopalette();
  951. }
  952. /*
  953. * Override bit of defpal with colours from the system.
  954. * (NB that this takes a copy the system colours at the time this is called,
  955. * so subsequent colour scheme changes don't take effect. To fix that we'd
  956. * probably want to be using GetSysColorBrush() and the like.)
  957. */
  958. static void systopalette(void)
  959. {
  960. int i;
  961. static const struct { int nIndex; int norm; int bold; } or[] =
  962. {
  963. { COLOR_WINDOWTEXT, 16, 17 }, /* Default Foreground */
  964. { COLOR_WINDOW, 18, 19 }, /* Default Background */
  965. { COLOR_HIGHLIGHTTEXT, 20, 21 }, /* Cursor Text */
  966. { COLOR_HIGHLIGHT, 22, 23 }, /* Cursor Colour */
  967. };
  968. for (i = 0; i < (sizeof(or)/sizeof(or[0])); i++) {
  969. COLORREF colour = GetSysColor(or[i].nIndex);
  970. defpal[or[i].norm].rgbtRed =
  971. defpal[or[i].bold].rgbtRed = GetRValue(colour);
  972. defpal[or[i].norm].rgbtGreen =
  973. defpal[or[i].bold].rgbtGreen = GetGValue(colour);
  974. defpal[or[i].norm].rgbtBlue =
  975. defpal[or[i].bold].rgbtBlue = GetBValue(colour);
  976. }
  977. }
  978. /*
  979. * Set up the colour palette.
  980. */
  981. static void init_palette(void)
  982. {
  983. int i;
  984. HDC hdc = GetDC(hwnd);
  985. if (hdc) {
  986. if (cfg.try_palette && GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) {
  987. /*
  988. * This is a genuine case where we must use smalloc
  989. * because the snew macros can't cope.
  990. */
  991. logpal = smalloc(sizeof(*logpal)
  992. - sizeof(logpal->palPalEntry)
  993. + NCOLOURS * sizeof(PALETTEENTRY));
  994. logpal->palVersion = 0x300;
  995. logpal->palNumEntries = NCOLOURS;
  996. for (i = 0; i < NCOLOURS; i++) {
  997. logpal->palPalEntry[i].peRed = defpal[i].rgbtRed;
  998. logpal->palPalEntry[i].peGreen = defpal[i].rgbtGreen;
  999. logpal->palPalEntry[i].peBlue = defpal[i].rgbtBlue;
  1000. logpal->palPalEntry[i].peFlags = PC_NOCOLLAPSE;
  1001. }
  1002. pal = CreatePalette(logpal);
  1003. if (pal) {
  1004. SelectPalette(hdc, pal, FALSE);
  1005. RealizePalette(hdc);
  1006. SelectPalette(hdc, GetStockObject(DEFAULT_PALETTE), FALSE);
  1007. }
  1008. }
  1009. ReleaseDC(hwnd, hdc);
  1010. }
  1011. if (pal)
  1012. for (i = 0; i < NCOLOURS; i++)
  1013. colours[i] = PALETTERGB(defpal[i].rgbtRed,
  1014. defpal[i].rgbtGreen,
  1015. defpal[i].rgbtBlue);
  1016. else
  1017. for (i = 0; i < NCOLOURS; i++)
  1018. colours[i] = RGB(defpal[i].rgbtRed,
  1019. defpal[i].rgbtGreen, defpal[i].rgbtBlue);
  1020. }
  1021. /*
  1022. * Initialise all the fonts we will need initially. There may be as many as
  1023. * three or as few as one. The other (poentially) twentyone fonts are done
  1024. * if/when they are needed.
  1025. *
  1026. * We also:
  1027. *
  1028. * - check the font width and height, correcting our guesses if
  1029. * necessary.
  1030. *
  1031. * - verify that the bold font is the same width as the ordinary
  1032. * one, and engage shadow bolding if not.
  1033. *
  1034. * - verify that the underlined font is the same width as the
  1035. * ordinary one (manual underlining by means of line drawing can
  1036. * be done in a pinch).
  1037. */
  1038. static void init_fonts(int pick_width, int pick_height)
  1039. {
  1040. TEXTMETRIC tm;
  1041. CPINFO cpinfo;
  1042. int fontsize[3];
  1043. int i;
  1044. HDC hdc;
  1045. int fw_dontcare, fw_bold;
  1046. for (i = 0; i < FONT_MAXNO; i++)
  1047. fonts[i] = NULL;
  1048. bold_mode = cfg.bold_colour ? BOLD_COLOURS : BOLD_FONT;
  1049. und_mode = UND_FONT;
  1050. if (cfg.font.isbold) {
  1051. fw_dontcare = FW_BOLD;
  1052. fw_bold = FW_HEAVY;
  1053. } else {
  1054. fw_dontcare = FW_DONTCARE;
  1055. fw_bold = FW_BOLD;
  1056. }
  1057. hdc = GetDC(hwnd);
  1058. if (pick_height)
  1059. font_height = pick_height;
  1060. else {
  1061. font_height = cfg.font.height;
  1062. if (font_height > 0) {
  1063. font_height =
  1064. -MulDiv(font_height, GetDeviceCaps(hdc, LOGPIXELSY), 72);
  1065. }
  1066. }
  1067. font_width = pick_width;
  1068. #define f(i,c,w,u) \
  1069. fonts[i] = CreateFont (font_height, font_width, 0, 0, w, FALSE, u, FALSE, \
  1070. c, OUT_DEFAULT_PRECIS, \
  1071. CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, \
  1072. FIXED_PITCH | FF_DONTCARE, cfg.font.name)
  1073. f(FONT_NORMAL, cfg.font.charset, fw_dontcare, FALSE);
  1074. lfont.lfHeight = font_height;
  1075. lfont.lfWidth = font_width;
  1076. lfont.lfEscapement = 0;
  1077. lfont.lfOrientation = 0;
  1078. lfont.lfWeight = fw_dontcare;
  1079. lfont.lfItalic = FALSE;
  1080. lfont.lfUnderline = FALSE;
  1081. lfont.lfStrikeOut = FALSE;
  1082. lfont.lfCharSet = cfg.font.charset;
  1083. lfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
  1084. lfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  1085. lfont.lfQuality = DEFAULT_QUALITY;
  1086. lfont.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
  1087. strncpy(lfont.lfFaceName, cfg.font.name, LF_FACESIZE);
  1088. SelectObject(hdc, fonts[FONT_NORMAL]);
  1089. GetTextMetrics(hdc, &tm);
  1090. if (pick_width == 0 || pick_height == 0) {
  1091. font_height = tm.tmHeight;
  1092. font_width = tm.tmAveCharWidth;
  1093. }
  1094. font_dualwidth = (tm.tmAveCharWidth != tm.tmMaxCharWidth);
  1095. #ifdef RDB_DEBUG_PATCH
  1096. debug(23, "Primary font H=%d, AW=%d, MW=%d",
  1097. tm.tmHeight, tm.tmAveCharWidth, tm.tmMaxCharWidth);
  1098. #endif
  1099. {
  1100. CHARSETINFO info;
  1101. DWORD cset = tm.tmCharSet;
  1102. memset(&info, 0xFF, sizeof(info));
  1103. /* !!! Yes the next line is right */
  1104. if (cset == OEM_CHARSET)
  1105. ucsdata.font_codepage = GetOEMCP();
  1106. else
  1107. if (TranslateCharsetInfo ((DWORD *) cset, &info, TCI_SRCCHARSET))
  1108. ucsdata.font_codepage = info.ciACP;
  1109. else
  1110. ucsdata.font_codepage = -1;
  1111. GetCPInfo(ucsdata.font_codepage, &cpinfo);
  1112. ucsdata.dbcs_screenfont = (cpinfo.MaxCharSize > 1);
  1113. }
  1114. f(FONT_UNDERLINE, cfg.font.charset, fw_dontcare, TRUE);
  1115. /*
  1116. * Some fonts, e.g. 9-pt Courier, draw their underlines
  1117. * outside their character cell. We successfully prevent
  1118. * screen corruption by clipping the text output, but then
  1119. * we lose the underline completely. Here we try to work
  1120. * out whether this is such a font, and if it is, we set a
  1121. * flag that causes underlines to be drawn by hand.
  1122. *
  1123. * Having tried other more sophisticated approaches (such
  1124. * as examining the TEXTMETRIC structure or requesting the
  1125. * height of a string), I think we'll do this the brute
  1126. * force way: we create a small bitmap, draw an underlined
  1127. * space on it, and test to see whether any pixels are
  1128. * foreground-coloured. (Since we expect the underline to
  1129. * go all the way across the character cell, we only search
  1130. * down a single column of the bitmap, half way across.)
  1131. */
  1132. {
  1133. HDC und_dc;
  1134. HBITMAP und_bm, und_oldbm;
  1135. int i, gotit;
  1136. COLORREF c;
  1137. und_dc = CreateCompatibleDC(hdc);
  1138. und_bm = CreateCompatibleBitmap(hdc, font_width, font_height);
  1139. und_oldbm = SelectObject(und_dc, und_bm);
  1140. SelectObject(und_dc, fonts[FONT_UNDERLINE]);
  1141. SetTextAlign(und_dc, TA_TOP | TA_LEFT | TA_NOUPDATECP);
  1142. SetTextColor(und_dc, RGB(255, 255, 255));
  1143. SetBkColor(und_dc, RGB(0, 0, 0));
  1144. SetBkMode(und_dc, OPAQUE);
  1145. ExtTextOut(und_dc, 0, 0, ETO_OPAQUE, NULL, " ", 1, NULL);
  1146. gotit = FALSE;
  1147. for (i = 0; i < font_height; i++) {
  1148. c = GetPixel(und_dc, font_width / 2, i);
  1149. if (c != RGB(0, 0, 0))
  1150. gotit = TRUE;
  1151. }
  1152. SelectObject(und_dc, und_oldbm);
  1153. DeleteObject(und_bm);
  1154. DeleteDC(und_dc);
  1155. if (!gotit) {
  1156. und_mode = UND_LINE;
  1157. DeleteObject(fonts[FONT_UNDERLINE]);
  1158. fonts[FONT_UNDERLINE] = 0;
  1159. }
  1160. }
  1161. if (bold_mode == BOLD_FONT) {
  1162. f(FONT_BOLD, cfg.font.charset, fw_bold, FALSE);
  1163. }
  1164. #undef f
  1165. descent = tm.tmAscent + 1;
  1166. if (descent >= font_height)
  1167. descent = font_height - 1;
  1168. for (i = 0; i < 3; i++) {
  1169. if (fonts[i]) {
  1170. if (SelectObject(hdc, fonts[i]) && GetTextMetrics(hdc, &tm))
  1171. fontsize[i] = tm.tmAveCharWidth + 256 * tm.tmHeight;
  1172. else
  1173. fontsize[i] = -i;
  1174. } else
  1175. fontsize[i] = -i;
  1176. }
  1177. ReleaseDC(hwnd, hdc);
  1178. if (fontsize[FONT_UNDERLINE] != fontsize[FONT_NORMAL]) {
  1179. und_mode = UND_LINE;
  1180. DeleteObject(fonts[FONT_UNDERLINE]);
  1181. fonts[FONT_UNDERLINE] = 0;
  1182. }
  1183. if (bold_mode == BOLD_FONT &&
  1184. fontsize[FONT_BOLD] != fontsize[FONT_NORMAL]) {
  1185. bold_mode = BOLD_SHADOW;
  1186. DeleteObject(fonts[FONT_BOLD]);
  1187. fonts[FONT_BOLD] = 0;
  1188. }
  1189. fontflag[0] = fontflag[1] = fontflag[2] = 1;
  1190. init_ucs(&cfg, &ucsdata);
  1191. }
  1192. static void another_font(int fontno)
  1193. {
  1194. int basefont;
  1195. int fw_dontcare, fw_bold;
  1196. int c, u, w, x;
  1197. char *s;
  1198. if (fontno < 0 || fontno >= FONT_MAXNO || fontflag[fontno])
  1199. return;
  1200. basefont = (fontno & ~(FONT_BOLDUND));
  1201. if (basefont != fontno && !fontflag[basefont])
  1202. another_font(basefont);
  1203. if (cfg.font.isbold) {
  1204. fw_dontcare = FW_BOLD;
  1205. fw_bold = FW_HEAVY;
  1206. } else {
  1207. fw_dontcare = FW_DONTCARE;
  1208. fw_bold = FW_BOLD;
  1209. }
  1210. c = cfg.font.charset;
  1211. w = fw_dontcare;
  1212. u = FALSE;
  1213. s = cfg.font.name;
  1214. x = font_width;
  1215. if (fontno & FONT_WIDE)
  1216. x *= 2;
  1217. if (fontno & FONT_NARROW)
  1218. x = (x+1)/2;
  1219. if (fontno & FONT_OEM)
  1220. c = OEM_CHARSET;
  1221. if (fontno & FONT_BOLD)
  1222. w = fw_bold;
  1223. if (fontno & FONT_UNDERLINE)
  1224. u = TRUE;
  1225. fonts[fontno] =
  1226. CreateFont(font_height * (1 + !!(fontno & FONT_HIGH)), x, 0, 0, w,
  1227. FALSE, u, FALSE, c, OUT_DEFAULT_PRECIS,
  1228. CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  1229. FIXED_PITCH | FF_DONTCARE, s);
  1230. fontflag[fontno] = 1;
  1231. }
  1232. static void deinit_fonts(void)
  1233. {
  1234. int i;
  1235. for (i = 0; i < FONT_MAXNO; i++) {
  1236. if (fonts[i])
  1237. DeleteObject(fonts[i]);
  1238. fonts[i] = 0;
  1239. fontflag[i] = 0;
  1240. }
  1241. }
  1242. void request_resize(void *frontend, int w, int h)
  1243. {
  1244. int width, height;
  1245. /* If the window is maximized supress resizing attempts */
  1246. if (IsZoomed(hwnd)) {
  1247. if (cfg.resize_action == RESIZE_TERM)
  1248. return;
  1249. }
  1250. if (cfg.resize_action == RESIZE_DISABLED) return;
  1251. if (h == term->rows && w == term->cols) return;
  1252. /* Sanity checks ... */
  1253. {
  1254. static int first_time = 1;
  1255. static RECT ss;
  1256. switch (first_time) {
  1257. case 1:
  1258. /* Get the size of the screen */
  1259. if (get_fullscreen_rect(&ss))
  1260. /* first_time = 0 */ ;
  1261. else {
  1262. first_time = 2;
  1263. break;
  1264. }
  1265. case 0:
  1266. /* Make sure the values are sane */
  1267. width = (ss.right - ss.left - extra_width) / 4;
  1268. height = (ss.bottom - ss.top - extra_height) / 6;
  1269. if (w > width || h > height)
  1270. return;
  1271. if (w < 15)
  1272. w = 15;
  1273. if (h < 1)
  1274. h = 1;
  1275. }
  1276. }
  1277. term_size(term, h, w, cfg.savelines);
  1278. if (cfg.resize_action != RESIZE_FONT && !IsZoomed(hwnd)) {
  1279. width = extra_width + font_width * w;
  1280. height = extra_height + font_height * h;
  1281. SetWindowPos(hwnd, NULL, 0, 0, width, height,
  1282. SWP_NOACTIVATE | SWP_NOCOPYBITS |
  1283. SWP_NOMOVE | SWP_NOZORDER);
  1284. } else
  1285. reset_window(0);
  1286. InvalidateRect(hwnd, NULL, TRUE);
  1287. }
  1288. static void reset_window(int reinit) {
  1289. /*
  1290. * This function decides how to resize or redraw when the
  1291. * user changes something.
  1292. *
  1293. * This function doesn't like to change the terminal size but if the
  1294. * font size is locked that may be it's only soluion.
  1295. */
  1296. int win_width, win_height;
  1297. RECT cr, wr;
  1298. #ifdef RDB_DEBUG_PATCH
  1299. debug((27, "reset_window()"));
  1300. #endif
  1301. /* Current window sizes ... */
  1302. GetWindowRect(hwnd, &wr);
  1303. GetClientRect(hwnd, &cr);
  1304. win_width = cr.right - cr.left;
  1305. win_height = cr.bottom - cr.top;
  1306. if (cfg.resize_action == RESIZE_DISABLED) reinit = 2;
  1307. /* Are we being forced to reload the fonts ? */
  1308. if (reinit>1) {
  1309. #ifdef RDB_DEBUG_PATCH
  1310. debug((27, "reset_window() -- Forced deinit"));
  1311. #endif
  1312. deinit_fonts();
  1313. init_fonts(0,0);
  1314. }
  1315. /* Oh, looks like we're minimised */
  1316. if (win_width == 0 || win_height == 0)
  1317. return;
  1318. /* Is the window out of position ? */
  1319. if ( !reinit &&
  1320. (offset_width != (win_width-font_width*term->cols)/2 ||
  1321. offset_height != (win_height-font_height*term->rows)/2) ){
  1322. offset_width = (win_width-font_width*term->cols)/2;
  1323. offset_height = (win_height-font_height*term->rows)/2;
  1324. InvalidateRect(hwnd, NULL, TRUE);
  1325. #ifdef RDB_DEBUG_PATCH
  1326. debug((27, "reset_window() -> Reposition terminal"));
  1327. #endif
  1328. }
  1329. if (IsZoomed(hwnd)) {
  1330. /* We're fullscreen, this means we must not change the size of
  1331. * the window so it's the font size or the terminal itself.
  1332. */
  1333. extra_width = wr.right - wr.left - cr.right + cr.left;
  1334. extra_height = wr.bottom - wr.top - cr.bottom + cr.top;
  1335. if (cfg.resize_action != RESIZE_TERM) {
  1336. if ( font_width != win_width/term->cols ||
  1337. font_height != win_height/term->rows) {
  1338. deinit_fonts();
  1339. init_fonts(win_width/term->cols, win_height/term->rows);
  1340. offset_width = (win_width-font_width*term->cols)/2;
  1341. offset_height = (win_height-font_height*term->rows)/2;
  1342. InvalidateRect(hwnd, NULL, TRUE);
  1343. #ifdef RDB_DEBUG_PATCH
  1344. debug((25, "reset_window() -> Z font resize to (%d, %d)",
  1345. font_width, font_height));
  1346. #endif
  1347. }
  1348. } else {
  1349. if ( font_width != win_width/term->cols ||
  1350. font_height != win_height/term->rows) {
  1351. /* Our only choice at this point is to change the
  1352. * size of the terminal; Oh well.
  1353. */
  1354. term_size(term, win_height/font_height, win_width/font_width,
  1355. cfg.savelines);
  1356. offset_width = (win_width-font_width*term->cols)/2;
  1357. offset_height = (win_height-font_height*term->rows)/2;
  1358. InvalidateRect(hwnd, NULL, TRUE);
  1359. #ifdef RDB_DEBUG_PATCH
  1360. debug((27, "reset_window() -> Zoomed term_size"));
  1361. #endif
  1362. }
  1363. }
  1364. return;
  1365. }
  1366. /* Hmm, a force re-init means we should ignore the current window
  1367. * so we resize to the default font size.
  1368. */
  1369. if (reinit>0) {
  1370. #ifdef RDB_DEBUG_PATCH
  1371. debug((27, "reset_window() -> Forced re-init"));
  1372. #endif
  1373. offset_width = offset_height = cfg.window_border;
  1374. extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
  1375. extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
  1376. if (win_width != font_width*term->cols + offset_width*2 ||
  1377. win_height != font_height*term->rows + offset_height*2) {
  1378. /* If this is too large windows will resize it to the maximum
  1379. * allowed window size, we will then be back in here and resize
  1380. * the font or terminal to fit.
  1381. */
  1382. SetWindowPos(hwnd, NULL, 0, 0,
  1383. font_width*term->cols + extra_width,
  1384. font_height*term->rows + extra_height,
  1385. SWP_NOMOVE | SWP_NOZORDER);
  1386. }
  1387. InvalidateRect(hwnd, NULL, TRUE);
  1388. return;
  1389. }
  1390. /* Okay the user doesn't want us to change the font so we try the
  1391. * window. But that may be too big for the screen which forces us
  1392. * to change the terminal.
  1393. */
  1394. if ((cfg.resize_action == RESIZE_TERM && reinit<=0) ||
  1395. (cfg.resize_action == RESIZE_EITHER && reinit<0) ||
  1396. reinit>0) {
  1397. offset_width = offset_height = cfg.window_border;
  1398. extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
  1399. extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
  1400. if (win_width != font_width*term->cols + offset_width*2 ||
  1401. win_height != font_height*term->rows + offset_height*2) {
  1402. static RECT ss;
  1403. int width, height;
  1404. get_fullscreen_rect(&ss);
  1405. width = (ss.right - ss.left - extra_width) / font_width;
  1406. height = (ss.bottom - ss.top - extra_height) / font_height;
  1407. /* Grrr too big */
  1408. if ( term->rows > height || term->cols > width ) {
  1409. if (cfg.resize_action == RESIZE_EITHER) {
  1410. /* Make the font the biggest we can */
  1411. if (term->cols > width)
  1412. font_width = (ss.right - ss.left - extra_width)
  1413. / term->cols;
  1414. if (term->rows > height)
  1415. font_height = (ss.bottom - ss.top - extra_height)
  1416. / term->rows;
  1417. deinit_fonts();
  1418. init_fonts(font_width, font_height);
  1419. width = (ss.right - ss.left - extra_width) / font_width;
  1420. height = (ss.bottom - ss.top - extra_height) / font_height;
  1421. } else {
  1422. if ( height > term->rows ) height = term->rows;
  1423. if ( width > term->cols ) width = term->cols;
  1424. term_size(term, height, width, cfg.savelines);
  1425. #ifdef RDB_DEBUG_PATCH
  1426. debug((27, "reset_window() -> term resize to (%d,%d)",
  1427. height, width));
  1428. #endif
  1429. }
  1430. }
  1431. SetWindowPos(hwnd, NULL, 0, 0,
  1432. font_width*term->cols + extra_width,
  1433. font_height*term->rows + extra_height,
  1434. SWP_NOMOVE | SWP_NOZORDER);
  1435. InvalidateRect(hwnd, NULL, TRUE);
  1436. #ifdef RDB_DEBUG_PATCH
  1437. debug((27, "reset_window() -> window resize to (%d,%d)",
  1438. font_width*term->cols + extra_width,
  1439. font_height*term->rows + extra_height));
  1440. #endif
  1441. }
  1442. return;
  1443. }
  1444. /* We're allowed to or must change the font but do we want to ? */
  1445. if (font_width != (win_width-cfg.window_border*2)/term->cols ||
  1446. font_height != (win_height-cfg.window_border*2)/term->rows) {
  1447. deinit_fonts();
  1448. init_fonts((win_width-cfg.window_border*2)/term->cols,
  1449. (win_height-cfg.window_border*2)/term->rows);
  1450. offset_width = (win_width-font_width*term->cols)/2;
  1451. offset_height = (win_height-font_height*term->rows)/2;
  1452. extra_width = wr.right - wr.left - cr.right + cr.left +offset_width*2;
  1453. extra_height = wr.bottom - wr.top - cr.bottom + cr.top+offset_height*2;
  1454. InvalidateRect(hwnd, NULL, TRUE);
  1455. #ifdef RDB_DEBUG_PATCH
  1456. debug((25, "reset_window() -> font resize to (%d,%d)",
  1457. font_width, font_height));
  1458. #endif
  1459. }
  1460. }
  1461. static void set_input_locale(HKL kl)
  1462. {
  1463. char lbuf[20];
  1464. GetLocaleInfo(LOWORD(kl), LOCALE_IDEFAULTANSICODEPAGE,
  1465. lbuf, sizeof(lbuf));
  1466. kbd_codepage = atoi(lbuf);
  1467. }
  1468. static void click(Mouse_Button b, int x, int y, int shift, int ctrl, int alt)
  1469. {
  1470. int thistime = GetMessageTime();
  1471. if (send_raw_mouse && !(cfg.mouse_override && shift)) {
  1472. lastbtn = MBT_NOTHING;
  1473. term_mouse(term, b, translate_button(b), MA_CLICK,
  1474. x, y, shift, ctrl, alt);
  1475. return;
  1476. }
  1477. if (lastbtn == b && thistime - lasttime < dbltime) {
  1478. lastact = (lastact == MA_CLICK ? MA_2CLK :
  1479. lastact == MA_2CLK ? MA_3CLK :
  1480. lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
  1481. } else {
  1482. lastbtn = b;
  1483. lastact = MA_CLICK;
  1484. }
  1485. if (lastact != MA_NOTHING)
  1486. term_mouse(term, b, translate_button(b), lastact,
  1487. x, y, shift, ctrl, alt);
  1488. lasttime = thistime;
  1489. }
  1490. /*
  1491. * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
  1492. * into a cooked one (SELECT, EXTEND, PASTE).
  1493. */
  1494. static Mouse_Button translate_button(Mouse_Button button)
  1495. {
  1496. if (button == MBT_LEFT)
  1497. return MBT_SELECT;
  1498. if (button == MBT_MIDDLE)
  1499. return cfg.mouse_is_xterm == 1 ? MBT_PASTE : MBT_EXTEND;
  1500. if (button == MBT_RIGHT)
  1501. return cfg.mouse_is_xterm == 1 ? MBT_EXTEND : MBT_PASTE;
  1502. return 0; /* shouldn't happen */
  1503. }
  1504. static void show_mouseptr(int show)
  1505. {
  1506. static int cursor_visible = 1;
  1507. if (!cfg.hide_mouseptr) /* override if this feature disabled */
  1508. show = 1;
  1509. if (cursor_visible && !show)
  1510. ShowCursor(FALSE);
  1511. else if (!cursor_visible && show)
  1512. ShowCursor(TRUE);
  1513. cursor_visible = show;
  1514. }
  1515. static int is_alt_pressed(void)
  1516. {
  1517. BYTE keystate[256];
  1518. int r = GetKeyboardState(keystate);
  1519. if (!r)
  1520. return FALSE;
  1521. if (keystate[VK_MENU] & 0x80)
  1522. return TRUE;
  1523. if (keystate[VK_RMENU] & 0x80)
  1524. return TRUE;
  1525. return FALSE;
  1526. }
  1527. static int is_shift_pressed(void)
  1528. {
  1529. BYTE keystate[256];
  1530. int r = GetKeyboardState(keystate);
  1531. if (!r)
  1532. return FALSE;
  1533. if (keystate[VK_SHIFT] & 0x80)
  1534. return TRUE;
  1535. return FALSE;
  1536. }
  1537. static int resizing;
  1538. static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
  1539. WPARAM wParam, LPARAM lParam)
  1540. {
  1541. HDC hdc;
  1542. static int ignore_clip = FALSE;
  1543. static int need_backend_resize = FALSE;
  1544. static int fullscr_on_max = FALSE;
  1545. static UINT last_mousemove = 0;
  1546. switch (message) {
  1547. case WM_TIMER:
  1548. if (pending_netevent)
  1549. enact_pending_netevent();
  1550. if (GetCapture() != hwnd ||
  1551. (send_raw_mouse && !(cfg.mouse_override && is_shift_pressed())))
  1552. term_out(term);
  1553. noise_regular();
  1554. HideCaret(hwnd);
  1555. term_update(term);
  1556. ShowCaret(hwnd);
  1557. if (cfg.ping_interval > 0) {
  1558. time_t now;
  1559. time(&now);
  1560. if (now - last_movement > cfg.ping_interval) {
  1561. if (back)
  1562. back->special(backhandle, TS_PING);
  1563. last_movement = now;
  1564. }
  1565. }
  1566. net_pending_errors();
  1567. return 0;
  1568. case WM_CREATE:
  1569. break;
  1570. case WM_CLOSE:
  1571. {
  1572. char *str;
  1573. show_mouseptr(1);
  1574. str = dupprintf("%s Exit Confirmation", appname);
  1575. if (!cfg.warn_on_close || session_closed ||
  1576. MessageBox(hwnd,
  1577. "Are you sure you want to close this session?",
  1578. str, MB_ICONWARNING | MB_OKCANCEL) == IDOK)
  1579. DestroyWindow(hwnd);
  1580. sfree(str);
  1581. }
  1582. return 0;
  1583. case WM_DESTROY:
  1584. show_mouseptr(1);
  1585. PostQuitMessage(0);
  1586. return 0;
  1587. case WM_COMMAND:
  1588. case WM_SYSCOMMAND:
  1589. switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
  1590. case IDM_SHOWLOG:
  1591. showeventlog(hwnd);
  1592. break;
  1593. case IDM_NEWSESS:
  1594. case IDM_DUPSESS:
  1595. case IDM_SAVEDSESS:
  1596. {
  1597. char b[2048];
  1598. char c[30], *cl;
  1599. int freecl = FALSE;
  1600. STARTUPINFO si;
  1601. PROCESS_INFORMATION pi;
  1602. HANDLE filemap = NULL;
  1603. if (wParam == IDM_DUPSESS) {
  1604. /*
  1605. * Allocate a file-mapping memory chunk for the
  1606. * config structure.
  1607. */
  1608. SECURITY_ATTRIBUTES sa;
  1609. Config *p;
  1610. sa.nLength = sizeof(sa);
  1611. sa.lpSecurityDescriptor = NULL;
  1612. sa.bInheritHandle = TRUE;
  1613. filemap = CreateFileMapping((HANDLE) 0xFFFFFFFF,
  1614. &sa,
  1615. PAGE_READWRITE,
  1616. 0, sizeof(Config), NULL);
  1617. if (filemap) {
  1618. p = (Config *) MapViewOfFile(filemap,
  1619. FILE_MAP_WRITE,
  1620. 0, 0, sizeof(Config));
  1621. if (p) {
  1622. *p = cfg; /* structure copy */
  1623. UnmapViewOfFile(p);
  1624. }
  1625. }
  1626. sprintf(c, "putty &%p", filemap);
  1627. cl = c;
  1628. } else if (wParam == IDM_SAVEDSESS) {
  1629. if ((lParam - IDM_SAVED_MIN) / 16 < sesslist.nsessions) {
  1630. char *session =
  1631. sesslist.sessions[(lParam - IDM_SAVED_MIN) / 16];
  1632. cl = snewn(16 + strlen(session), char);
  1633. /* 8, but play safe */
  1634. if (!cl)
  1635. cl = NULL;
  1636. /* not a very important failure mode */
  1637. else {
  1638. sprintf(cl, "putty @%s", session);
  1639. freecl = TRUE;
  1640. }
  1641. } else
  1642. break;
  1643. } else
  1644. cl = NULL;
  1645. GetModuleFileName(NULL, b, sizeof(b) - 1);
  1646. si.cb = sizeof(si);
  1647. si.lpReserved = NULL;
  1648. si.lpDesktop = NULL;
  1649. si.lpTitle = NULL;
  1650. si.dwFlags = 0;
  1651. si.cbReserved2 = 0;
  1652. si.lpReserved2 = NULL;
  1653. CreateProcess(b, cl, NULL, NULL, TRUE,
  1654. NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
  1655. if (filemap)
  1656. CloseHandle(filemap);
  1657. if (freecl)
  1658. sfree(cl);
  1659. }
  1660. break;
  1661. case IDM_RESTART:
  1662. if (!back) {
  1663. logevent(NULL, "----- Session restarted -----");
  1664. start_backend();
  1665. }
  1666. break;
  1667. case IDM_RECONF:
  1668. {
  1669. Config prev_cfg;
  1670. int init_lvl = 1;
  1671. GetWindowText(hwnd, cfg.wintitle, sizeof(cfg.wintitle));
  1672. prev_cfg = cfg;
  1673. if (!do_reconfig(hwnd))
  1674. break;
  1675. {
  1676. /* Disable full-screen if resizing forbidden */
  1677. HMENU m = GetSystemMenu (hwnd, FALSE);
  1678. EnableMenuItem(m, IDM_FULLSCREEN, MF_BYCOMMAND |
  1679. (cfg.resize_action == RESIZE_DISABLED)
  1680. ? MF_GRAYED : MF_ENABLED);
  1681. /* Gracefully unzoom if necessary */
  1682. if (IsZoomed(hwnd) &&
  1683. (cfg.resize_action == RESIZE_DISABLED)) {
  1684. ShowWindow(hwnd, SW_RESTORE);
  1685. }
  1686. }
  1687. /* Pass new config data to the logging module */
  1688. log_reconfig(logctx, &cfg);
  1689. sfree(logpal);
  1690. /*
  1691. * Flush the line discipline's edit buffer in the
  1692. * case where local editing has just been disabled.
  1693. */
  1694. if (ldisc)
  1695. ldisc_send(ldisc, NULL, 0, 0);
  1696. if (pal)
  1697. DeleteObject(pal);
  1698. logpal = NULL;
  1699. pal = NULL;
  1700. cfgtopalette();
  1701. init_palette();
  1702. /* Pass new config data to the terminal */
  1703. term_reconfig(term, &cfg);
  1704. /* Pass new config data to the back end */
  1705. if (back)
  1706. back->reconfig(backhandle, &cfg);
  1707. /* Screen size changed ? */
  1708. if (cfg.height != prev_cfg.height ||
  1709. cfg.width != prev_cfg.width ||
  1710. cfg.savelines != prev_cfg.savelines ||
  1711. cfg.resize_action == RESIZE_FONT ||
  1712. (cfg.resize_action == RESIZE_EITHER && IsZoomed(hwnd)) ||
  1713. cfg.resize_action == RESIZE_DISABLED)
  1714. term_size(term, cfg.height, cfg.width, cfg.savelines);
  1715. /* Enable or disable the scroll bar, etc */
  1716. {
  1717. LONG nflg, flag = GetWindowLong(hwnd, GWL_STYLE);
  1718. LONG nexflag, exflag =
  1719. GetWindowLong(hwnd, GWL_EXSTYLE);
  1720. nexflag = exflag;
  1721. if (cfg.alwaysontop != prev_cfg.alwaysontop) {
  1722. if (cfg.alwaysontop) {
  1723. nexflag |= WS_EX_TOPMOST;
  1724. SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
  1725. SWP_NOMOVE | SWP_NOSIZE);
  1726. } else {
  1727. nexflag &= ~(WS_EX_TOPMOST);
  1728. SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
  1729. SWP_NOMOVE | SWP_NOSIZE);
  1730. }
  1731. }
  1732. if (cfg.sunken_edge)
  1733. nexflag |= WS_EX_CLIENTEDGE;
  1734. else
  1735. nexflag &= ~(WS_EX_CLIENTEDGE);
  1736. nflg = flag;
  1737. if (is_full_screen() ?
  1738. cfg.scrollbar_in_fullscreen : cfg.scrollbar)
  1739. nflg |= WS_VSCROLL;
  1740. else
  1741. nflg &= ~WS_VSCROLL;
  1742. if (cfg.resize_action == RESIZE_DISABLED ||
  1743. is_full_screen())
  1744. nflg &= ~WS_THICKFRAME;
  1745. else
  1746. nflg |= WS_THICKFRAME;
  1747. if (cfg.resize_action == RESIZE_DISABLED)
  1748. nflg &= ~WS_MAXIMIZEBOX;
  1749. else
  1750. nflg |= WS_MAXIMIZEBOX;
  1751. if (nflg != flag || nexflag != exflag) {
  1752. if (nflg != flag)
  1753. SetWindowLong(hwnd, GWL_STYLE, nflg);
  1754. if (nexflag != exflag)
  1755. SetWindowLong(hwnd, GWL_EXSTYLE, nexflag);
  1756. SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
  1757. SWP_NOACTIVATE | SWP_NOCOPYBITS |
  1758. SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
  1759. SWP_FRAMECHANGED);
  1760. init_lvl = 2;
  1761. }
  1762. }
  1763. /* Oops */
  1764. if (cfg.resize_action == RESIZE_DISABLED && IsZoomed(hwnd)) {
  1765. force_normal(hwnd);
  1766. init_lvl = 2;
  1767. }
  1768. set_title(NULL, cfg.wintitle);
  1769. if (IsIconic(hwnd)) {
  1770. SetWindowText(hwnd,
  1771. cfg.win_name_always ? window_name :
  1772. icon_name);
  1773. }
  1774. if (strcmp(cfg.font.name, prev_cfg.font.name) != 0 ||
  1775. strcmp(cfg.line_codepage, prev_cfg.line_codepage) != 0 ||
  1776. cfg.font.isbold != prev_cfg.font.isbold ||
  1777. cfg.font.height != prev_cfg.font.height ||
  1778. cfg.font.charset != prev_cfg.font.charset ||
  1779. cfg.vtmode != prev_cfg.vtmode ||
  1780. cfg.bold_colour != prev_cfg.bold_colour ||
  1781. cfg.resize_action == RESIZE_DISABLED ||
  1782. cfg.resize_action == RESIZE_EITHER ||
  1783. (cfg.resize_action != prev_cfg.resize_action))
  1784. init_lvl = 2;
  1785. InvalidateRect(hwnd, NULL, TRUE);
  1786. reset_window(init_lvl);
  1787. net_pending_errors();
  1788. }
  1789. break;
  1790. case IDM_COPYALL:
  1791. term_copyall(term);
  1792. break;
  1793. case IDM_PASTE:
  1794. term_do_paste(term);
  1795. break;
  1796. case IDM_CLRSB:
  1797. term_clrsb(term);
  1798. break;
  1799. case IDM_RESET:
  1800. term_pwron(term);
  1801. if (ldisc)
  1802. ldisc_send(ldisc, NULL, 0, 0);
  1803. break;
  1804. case IDM_ABOUT:
  1805. showabout(hwnd);
  1806. break;
  1807. case IDM_HELP:
  1808. WinHelp(hwnd, help_path,
  1809. help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
  1810. break;
  1811. case SC_MOUSEMENU:
  1812. /*
  1813. * We get this if the System menu has been activated
  1814. * using the mouse.
  1815. */
  1816. show_mouseptr(1);
  1817. break;
  1818. case SC_KEYMENU:
  1819. /*
  1820. * We get this if the System menu has been activated
  1821. * using the keyboard. This might happen from within
  1822. * TranslateKey, in which case it really wants to be
  1823. * followed by a `space' character to actually _bring
  1824. * the menu up_ rather than just sitting there in
  1825. * `ready to appear' state.
  1826. */
  1827. show_mouseptr(1); /* make sure pointer is visible */
  1828. if( lParam == 0 )
  1829. PostMessage(hwnd, WM_CHAR, ' ', 0);
  1830. break;
  1831. case IDM_FULLSCREEN:
  1832. flip_full_screen();
  1833. break;
  1834. default:
  1835. if (wParam >= IDM_SAVED_MIN && wParam <= IDM_SAVED_MAX) {
  1836. SendMessage(hwnd, WM_SYSCOMMAND, IDM_SAVEDSESS, wParam);
  1837. }
  1838. if (wParam >= IDM_SPECIAL_MIN && wParam <= IDM_SPECIAL_MAX) {
  1839. int i = (wParam - IDM_SPECIAL_MIN) / 0x10;
  1840. /*
  1841. * Ensure we haven't been sent a bogus SYSCOMMAND
  1842. * which would cause us to reference invalid memory
  1843. * and crash. Perhaps I'm just too paranoid here.
  1844. */
  1845. if (i >= n_specials)
  1846. break;
  1847. if (back)
  1848. back->special(backhandle, specials[i].code);
  1849. net_pending_errors();
  1850. }
  1851. }
  1852. break;
  1853. #define X_POS(l) ((int)(short)LOWORD(l))
  1854. #define Y_POS(l) ((int)(short)HIWORD(l))
  1855. #define TO_CHR_X(x) ((((x)<0 ? (x)-font_width+1 : (x))-offset_width) / font_width)
  1856. #define TO_CHR_Y(y) ((((y)<0 ? (y)-font_height+1: (y))-offset_height) / font_height)
  1857. case WM_LBUTTONDOWN:
  1858. case WM_MBUTTONDOWN:
  1859. case WM_RBUTTONDOWN:
  1860. case WM_LBUTTONUP:
  1861. case WM_MBUTTONUP:
  1862. case WM_RBUTTONUP:
  1863. if (message == WM_RBUTTONDOWN &&
  1864. ((wParam & MK_CONTROL) || (cfg.mouse_is_xterm == 2))) {
  1865. POINT cursorpos;
  1866. show_mouseptr(1); /* make sure pointer is visible */
  1867. GetCursorPos(&cursorpos);
  1868. TrackPopupMenu(popup_menus[CTXMENU].menu,
  1869. TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON,
  1870. cursorpos.x, cursorpos.y,
  1871. 0, hwnd, NULL);
  1872. break;
  1873. }
  1874. {
  1875. int button, press;
  1876. switch (message) {
  1877. case WM_LBUTTONDOWN:
  1878. button = MBT_LEFT;
  1879. press = 1;
  1880. break;
  1881. case WM_MBUTTONDOWN:
  1882. button = MBT_MIDDLE;
  1883. press = 1;
  1884. break;
  1885. case WM_RBUTTONDOWN:
  1886. button = MBT_RIGHT;
  1887. press = 1;
  1888. break;
  1889. case WM_LBUTTONUP:
  1890. button = MBT_LEFT;
  1891. press = 0;
  1892. break;
  1893. case WM_MBUTTONUP:
  1894. button = MBT_MIDDLE;
  1895. press = 0;
  1896. break;
  1897. case WM_RBUTTONUP:
  1898. button = MBT_RIGHT;
  1899. press = 0;
  1900. break;
  1901. default:
  1902. button = press = 0; /* shouldn't happen */
  1903. }
  1904. show_mouseptr(1);
  1905. /*
  1906. * Special case: in full-screen mode, if the left
  1907. * button is clicked in the very top left corner of the
  1908. * window, we put up the System menu instead of doing
  1909. * selection.
  1910. */
  1911. {
  1912. char mouse_on_hotspot = 0;
  1913. POINT pt;
  1914. GetCursorPos(&pt);
  1915. #ifndef NO_MULTIMON
  1916. {
  1917. HMONITOR mon;
  1918. MONITORINFO mi;
  1919. mon = MonitorFromPoint(pt, MONITOR_DEFAULTTONULL);
  1920. if (mon != NULL) {
  1921. mi.cbSize = sizeof(MONITORINFO);
  1922. GetMonitorInfo(mon, &mi);
  1923. if (mi.rcMonitor.left == pt.x &&
  1924. mi.rcMonitor.top == pt.y) {
  1925. mouse_on_hotspot = 1;
  1926. }
  1927. }
  1928. }
  1929. #else
  1930. if (pt.x == 0 && pt.y == 0) {
  1931. mouse_on_hotspot = 1;
  1932. }
  1933. #endif
  1934. if (is_full_screen() && press &&
  1935. button == MBT_LEFT && mouse_on_hotspot) {
  1936. SendMessage(hwnd, WM_SYSCOMMAND, SC_MOUSEMENU,
  1937. MAKELPARAM(pt.x, pt.y));
  1938. return 0;
  1939. }
  1940. }
  1941. if (press) {
  1942. click(button,
  1943. TO_CHR_X(X_POS(lParam)), TO_CHR_Y(Y_POS(lParam)),
  1944. wParam & MK_SHIFT, wParam & MK_CONTROL,
  1945. is_alt_pressed());
  1946. SetCapture(hwnd);
  1947. } else {
  1948. term_mouse(term, button, translate_button(button), MA_RELEASE,
  1949. TO_CHR_X(X_POS(lParam)),
  1950. TO_CHR_Y(Y_POS(lParam)), wParam & MK_SHIFT,
  1951. wParam & MK_CONTROL, is_alt_pressed());
  1952. ReleaseCapture();
  1953. }
  1954. }
  1955. return 0;
  1956. case WM_MOUSEMOVE:
  1957. {
  1958. /*
  1959. * Windows seems to like to occasionally send MOUSEMOVE
  1960. * events even if the mouse hasn't moved. Don't unhide
  1961. * the mouse pointer in this case.
  1962. */
  1963. static WPARAM wp = 0;
  1964. static LPARAM lp = 0;
  1965. if (wParam != wp || lParam != lp ||
  1966. last_mousemove != WM_MOUSEMOVE) {
  1967. show_mouseptr(1);
  1968. wp = wParam; lp = lParam;
  1969. last_mousemove = WM_MOUSEMOVE;
  1970. }
  1971. }
  1972. /*
  1973. * Add the mouse position and message time to the random
  1974. * number noise.
  1975. */
  1976. noise_ultralight(lParam);
  1977. if (wParam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON) &&
  1978. GetCapture() == hwnd) {
  1979. Mouse_Button b;
  1980. if (wParam & MK_LBUTTON)
  1981. b = MBT_LEFT;
  1982. else if (wParam & MK_MBUTTON)
  1983. b = MBT_MIDDLE;
  1984. else
  1985. b = MBT_RIGHT;
  1986. term_mouse(term, b, translate_button(b), MA_DRAG,
  1987. TO_CHR_X(X_POS(lParam)),
  1988. TO_CHR_Y(Y_POS(lParam)), wParam & MK_SHIFT,
  1989. wParam & MK_CONTROL, is_alt_pressed());
  1990. }
  1991. return 0;
  1992. case WM_NCMOUSEMOVE:
  1993. {
  1994. static WPARAM wp = 0;
  1995. static LPARAM lp = 0;
  1996. if (wParam != wp || lParam != lp ||
  1997. last_mousemove != WM_NCMOUSEMOVE) {
  1998. show_mouseptr(1);
  1999. wp = wParam; lp = lParam;
  2000. last_mousemove = WM_NCMOUSEMOVE;
  2001. }
  2002. }
  2003. noise_ultralight(lParam);
  2004. break;
  2005. case WM_IGNORE_CLIP:
  2006. ignore_clip = wParam; /* don't panic on DESTROYCLIPBOARD */
  2007. break;
  2008. case WM_DESTROYCLIPBOARD:
  2009. if (!ignore_clip)
  2010. term_deselect(term);
  2011. ignore_clip = FALSE;
  2012. return 0;
  2013. case WM_PAINT:
  2014. {
  2015. PAINTSTRUCT p;
  2016. HideCaret(hwnd);
  2017. hdc = BeginPaint(hwnd, &p);
  2018. if (pal) {
  2019. SelectPalette(hdc, pal, TRUE);
  2020. RealizePalette(hdc);
  2021. }
  2022. term_paint(term, hdc,
  2023. (p.rcPaint.left-offset_width)/font_width,
  2024. (p.rcPaint.top-offset_height)/font_height,
  2025. (p.rcPaint.right-offset_width-1)/font_width,
  2026. (p.rcPaint.bottom-offset_height-1)/font_height,
  2027. is_alt_pressed());
  2028. if (p.fErase ||
  2029. p.rcPaint.left < offset_width ||
  2030. p.rcPaint.top < offset_height ||
  2031. p.rcPaint.right >= offset_width + font_width*term->cols ||
  2032. p.rcPaint.bottom>= offset_height + font_height*term->rows)
  2033. {
  2034. HBRUSH fillcolour, oldbrush;
  2035. HPEN edge, oldpen;
  2036. fillcolour = CreateSolidBrush (
  2037. colours[(ATTR_DEFBG>>ATTR_BGSHIFT)*2]);
  2038. oldbrush = SelectObject(hdc, fillcolour);
  2039. edge = CreatePen(PS_SOLID, 0,
  2040. colours[(ATTR_DEFBG>>ATTR_BGSHIFT)*2]);
  2041. oldpen = SelectObject(hdc, edge);
  2042. /*
  2043. * Jordan Russell reports that this apparently
  2044. * ineffectual IntersectClipRect() call masks a
  2045. * Windows NT/2K bug causing strange display
  2046. * problems when the PuTTY window is taller than
  2047. * the primary monitor. It seems harmless enough...
  2048. */
  2049. IntersectClipRect(hdc,
  2050. p.rcPaint.left, p.rcPaint.top,
  2051. p.rcPaint.right, p.rcPaint.bottom);
  2052. ExcludeClipRect(hdc,
  2053. offset_width, offset_height,
  2054. offset_width+font_width*term->cols,
  2055. offset_height+font_height*term->rows);
  2056. Rectangle(hdc, p.rcPaint.left, p.rcPaint.top,
  2057. p.rcPaint.right, p.rcPaint.bottom);
  2058. // SelectClipRgn(hdc, NULL);
  2059. SelectObject(hdc, oldbrush);
  2060. DeleteObject(fillcolour);
  2061. SelectObject(hdc, oldpen);
  2062. DeleteObject(edge);
  2063. }
  2064. SelectObject(hdc, GetStockObject(SYSTEM_FONT));
  2065. SelectObject(hdc, GetStockObject(WHITE_PEN));
  2066. EndPaint(hwnd, &p);
  2067. ShowCaret(hwnd);
  2068. }
  2069. return 0;
  2070. case WM_NETEVENT:
  2071. /* Notice we can get multiple netevents, FD_READ, FD_WRITE etc
  2072. * but the only one that's likely to try to overload us is FD_READ.
  2073. * This means buffering just one is fine.
  2074. */
  2075. if (pending_netevent)
  2076. enact_pending_netevent();
  2077. pending_netevent = TRUE;
  2078. pend_netevent_wParam = wParam;
  2079. pend_netevent_lParam = lParam;
  2080. if (WSAGETSELECTEVENT(lParam) != FD_READ)
  2081. enact_pending_netevent();
  2082. time(&last_movement);
  2083. return 0;
  2084. case WM_SETFOCUS:
  2085. term->has_focus = TRUE;
  2086. CreateCaret(hwnd, caretbm, font_width, font_height);
  2087. ShowCaret(hwnd);
  2088. flash_window(0); /* stop */
  2089. compose_state = 0;
  2090. term_out(term);
  2091. term_update(term);
  2092. break;
  2093. case WM_KILLFOCUS:
  2094. show_mouseptr(1);
  2095. term->has_focus = FALSE;
  2096. DestroyCaret();
  2097. caret_x = caret_y = -1; /* ensure caret is replaced next time */
  2098. term_out(term);
  2099. term_update(term);
  2100. break;
  2101. case WM_ENTERSIZEMOVE:
  2102. #ifdef RDB_DEBUG_PATCH
  2103. debug((27, "WM_ENTERSIZEMOVE"));
  2104. #endif
  2105. EnableSizeTip(1);
  2106. resizing = TRUE;
  2107. need_backend_resize = FALSE;
  2108. break;
  2109. case WM_EXITSIZEMOVE:
  2110. EnableSizeTip(0);
  2111. resizing = FALSE;
  2112. #ifdef RDB_DEBUG_PATCH
  2113. debug((27, "WM_EXITSIZEMOVE"));
  2114. #endif
  2115. if (need_backend_resize) {
  2116. term_size(term, cfg.height, cfg.width, cfg.savelines);
  2117. InvalidateRect(hwnd, NULL, TRUE);
  2118. }
  2119. break;
  2120. case WM_SIZING:
  2121. /*
  2122. * This does two jobs:
  2123. * 1) Keep the sizetip uptodate
  2124. * 2) Make sure the window size is _stepped_ in units of the font size.
  2125. */
  2126. if (cfg.resize_action != RESIZE_FONT && !is_alt_pressed()) {
  2127. int width, height, w, h, ew, eh;
  2128. LPRECT r = (LPRECT) lParam;
  2129. if ( !need_backend_resize && cfg.resize_action == RESIZE_EITHER &&
  2130. (cfg.height != term->rows || cfg.width != term->cols )) {
  2131. /*
  2132. * Great! It seems that both the terminal size and the
  2133. * font size have been changed and the user is now dragging.
  2134. *
  2135. * It will now be difficult to get back to the configured
  2136. * font size!
  2137. *
  2138. * This would be easier but it seems to be too confusing.
  2139. term_size(term, cfg.height, cfg.width, cfg.savelines);
  2140. reset_window(2);
  2141. */
  2142. cfg.height=term->rows; cfg.width=term->cols;
  2143. InvalidateRect(hwnd, NULL, TRUE);
  2144. need_backend_resize = TRUE;
  2145. }
  2146. width = r->right - r->left - extra_width;
  2147. height = r->bottom - r->top - extra_height;
  2148. w = (width + font_width / 2) / font_width;
  2149. if (w < 1)
  2150. w = 1;
  2151. h = (height + font_height / 2) / font_height;
  2152. if (h < 1)
  2153. h = 1;
  2154. UpdateSizeTip(hwnd, w, h);
  2155. ew = width - w * font_width;
  2156. eh = height - h * font_height;
  2157. if (ew != 0) {
  2158. if (wParam == WMSZ_LEFT ||
  2159. wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_TOPLEFT)
  2160. r->left += ew;
  2161. else
  2162. r->right -= ew;
  2163. }
  2164. if (eh != 0) {
  2165. if (wParam == WMSZ_TOP ||
  2166. wParam == WMSZ_TOPRIGHT || wParam == WMSZ_TOPLEFT)
  2167. r->top += eh;
  2168. else
  2169. r->bottom -= eh;
  2170. }
  2171. if (ew || eh)
  2172. return 1;
  2173. else
  2174. return 0;
  2175. } else {
  2176. int width, height, w, h, rv = 0;
  2177. int ex_width = extra_width + (cfg.window_border - offset_width) * 2;
  2178. int ex_height = extra_height + (cfg.window_border - offset_height) * 2;
  2179. LPRECT r = (LPRECT) lParam;
  2180. width = r->right - r->left - ex_width;
  2181. height = r->bottom - r->top - ex_height;
  2182. w = (width + term->cols/2)/term->cols;
  2183. h = (height + term->rows/2)/term->rows;
  2184. if ( r->right != r->left + w*term->cols + ex_width)
  2185. rv = 1;
  2186. if (wParam == WMSZ_LEFT ||
  2187. wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_TOPLEFT)
  2188. r->left = r->right - w*term->cols - ex_width;
  2189. else
  2190. r->right = r->left + w*term->cols + ex_width;
  2191. if (r->bottom != r->top + h*term->rows + ex_height)
  2192. rv = 1;
  2193. if (wParam == WMSZ_TOP ||
  2194. wParam == WMSZ_TOPRIGHT || wParam == WMSZ_TOPLEFT)
  2195. r->top = r->bottom - h*term->rows - ex_height;
  2196. else
  2197. r->bottom = r->top + h*term->rows + ex_height;
  2198. return rv;
  2199. }
  2200. /* break; (never reached) */
  2201. case WM_FULLSCR_ON_MAX:
  2202. fullscr_on_max = TRUE;
  2203. break;
  2204. case WM_MOVE:
  2205. sys_cursor_update();
  2206. break;
  2207. case WM_SIZE:
  2208. #ifdef RDB_DEBUG_PATCH
  2209. debug((27, "WM_SIZE %s (%d,%d)",
  2210. (wParam == SIZE_MINIMIZED) ? "SIZE_MINIMIZED":
  2211. (wParam == SIZE_MAXIMIZED) ? "SIZE_MAXIMIZED":
  2212. (wParam == SIZE_RESTORED && resizing) ? "to":
  2213. (wParam == SIZE_RESTORED) ? "SIZE_RESTORED":
  2214. "...",
  2215. LOWORD(lParam), HIWORD(lParam)));
  2216. #endif
  2217. if (wParam == SIZE_MINIMIZED)
  2218. SetWindowText(hwnd,
  2219. cfg.win_name_always ? window_name : icon_name);
  2220. if (wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED)
  2221. SetWindowText(hwnd, window_name);
  2222. if (wParam == SIZE_RESTORED)
  2223. clear_full_screen();
  2224. if (wParam == SIZE_MAXIMIZED && fullscr_on_max) {
  2225. fullscr_on_max = FALSE;
  2226. make_full_screen();
  2227. }
  2228. if (cfg.resize_action == RESIZE_DISABLED) {
  2229. /* A resize, well it better be a minimize. */
  2230. reset_window(-1);
  2231. } else {
  2232. int width, height, w, h;
  2233. width = LOWORD(lParam);
  2234. height = HIWORD(lParam);
  2235. if (!resizing) {
  2236. if (wParam == SIZE_MAXIMIZED && !was_zoomed) {
  2237. was_zoomed = 1;
  2238. prev_rows = term->rows;
  2239. prev_cols = term->cols;
  2240. if (cfg.resize_action == RESIZE_TERM) {
  2241. w = width / font_width;
  2242. if (w < 1) w = 1;
  2243. h = height / font_height;
  2244. if (h < 1) h = 1;
  2245. term_size(term, h, w, cfg.savelines);
  2246. }
  2247. reset_window(0);
  2248. } else if (wParam == SIZE_RESTORED && was_zoomed) {
  2249. was_zoomed = 0;
  2250. if (cfg.resize_action == RESIZE_TERM)
  2251. term_size(term, prev_rows, prev_cols, cfg.savelines);
  2252. if (cfg.resize_action != RESIZE_FONT)
  2253. reset_window(2);
  2254. else
  2255. reset_window(0);
  2256. }
  2257. /* This is an unexpected resize, these will normally happen
  2258. * if the window is too large. Probably either the user
  2259. * selected a huge font or the screen size has changed.
  2260. *
  2261. * This is also called with minimize.
  2262. */
  2263. else reset_window(-1);
  2264. }
  2265. /*
  2266. * Don't call back->size in mid-resize. (To prevent
  2267. * massive numbers of resize events getting sent
  2268. * down the connection during an NT opaque drag.)
  2269. */
  2270. if (resizing) {
  2271. if (cfg.resize_action != RESIZE_FONT && !is_alt_pressed()) {
  2272. need_backend_resize = TRUE;
  2273. w = (width-cfg.window_border*2) / font_width;
  2274. if (w < 1) w = 1;
  2275. h = (height-cfg.window_border*2) / font_height;
  2276. if (h < 1) h = 1;
  2277. cfg.height = h;
  2278. cfg.width = w;
  2279. } else
  2280. reset_window(0);
  2281. }
  2282. }
  2283. sys_cursor_update();
  2284. return 0;
  2285. case WM_VSCROLL:
  2286. switch (LOWORD(wParam)) {
  2287. case SB_BOTTOM:
  2288. term_scroll(term, -1, 0);
  2289. break;
  2290. case SB_TOP:
  2291. term_scroll(term, +1, 0);
  2292. break;
  2293. case SB_LINEDOWN:
  2294. term_scroll(term, 0, +1);
  2295. break;
  2296. case SB_LINEUP:
  2297. term_scroll(term, 0, -1);
  2298. break;
  2299. case SB_PAGEDOWN:
  2300. term_scroll(term, 0, +term->rows / 2);
  2301. break;
  2302. case SB_PAGEUP:
  2303. term_scroll(term, 0, -term->rows / 2);
  2304. break;
  2305. case SB_THUMBPOSITION:
  2306. case SB_THUMBTRACK:
  2307. term_scroll(term, 1, HIWORD(wParam));
  2308. break;
  2309. }
  2310. break;
  2311. case WM_PALETTECHANGED:
  2312. if ((HWND) wParam != hwnd && pal != NULL) {
  2313. HDC hdc = get_ctx(NULL);
  2314. if (hdc) {
  2315. if (RealizePalette(hdc) > 0)
  2316. UpdateColors(hdc);
  2317. free_ctx(hdc);
  2318. }
  2319. }
  2320. break;
  2321. case WM_QUERYNEWPALETTE:
  2322. if (pal != NULL) {
  2323. HDC hdc = get_ctx(NULL);
  2324. if (hdc) {
  2325. if (RealizePalette(hdc) > 0)
  2326. UpdateColors(hdc);
  2327. free_ctx(hdc);
  2328. return TRUE;
  2329. }
  2330. }
  2331. return FALSE;
  2332. case WM_KEYDOWN:
  2333. case WM_SYSKEYDOWN:
  2334. case WM_KEYUP:
  2335. case WM_SYSKEYUP:
  2336. /*
  2337. * Add the scan code and keypress timing to the random
  2338. * number noise.
  2339. */
  2340. noise_ultralight(lParam);
  2341. /*
  2342. * We don't do TranslateMessage since it disassociates the
  2343. * resulting CHAR message from the KEYDOWN that sparked it,
  2344. * which we occasionally don't want. Instead, we process
  2345. * KEYDOWN, and call the Win32 translator functions so that
  2346. * we get the translations under _our_ control.
  2347. */
  2348. {
  2349. unsigned char buf[20];
  2350. int len;
  2351. if (wParam == VK_PROCESSKEY) {
  2352. MSG m;
  2353. m.hwnd = hwnd;
  2354. m.message = WM_KEYDOWN;
  2355. m.wParam = wParam;
  2356. m.lParam = lParam & 0xdfff;
  2357. TranslateMessage(&m);
  2358. } else {
  2359. len = TranslateKey(message, wParam, lParam, buf);
  2360. if (len == -1)
  2361. return DefWindowProc(hwnd, message, wParam, lParam);
  2362. if (len != 0) {
  2363. /*
  2364. * Interrupt an ongoing paste. I'm not sure
  2365. * this is sensible, but for the moment it's
  2366. * preferable to having to faff about buffering
  2367. * things.
  2368. */
  2369. term_nopaste(term);
  2370. /*
  2371. * We need not bother about stdin backlogs
  2372. * here, because in GUI PuTTY we can't do
  2373. * anything about it anyway; there's no means
  2374. * of asking Windows to hold off on KEYDOWN
  2375. * messages. We _have_ to buffer everything
  2376. * we're sent.
  2377. */
  2378. term_seen_key_event(term);
  2379. if (ldisc)
  2380. ldisc_send(ldisc, buf, len, 1);
  2381. show_mouseptr(0);
  2382. }
  2383. }
  2384. }
  2385. net_pending_errors();
  2386. return 0;
  2387. case WM_INPUTLANGCHANGE:
  2388. /* wParam == Font number */
  2389. /* lParam == Locale */
  2390. set_input_locale((HKL)lParam);
  2391. sys_cursor_update();
  2392. break;
  2393. case WM_IME_NOTIFY:
  2394. if(wParam == IMN_SETOPENSTATUS) {
  2395. HIMC hImc = ImmGetContext(hwnd);
  2396. ImmSetCompositionFont(hImc, &lfont);
  2397. ImmReleaseContext(hwnd, hImc);
  2398. return 0;
  2399. }
  2400. break;
  2401. case WM_IME_COMPOSITION:
  2402. {
  2403. HIMC hIMC;
  2404. int n;
  2405. char *buff;
  2406. if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ||
  2407. osVersion.dwPlatformId == VER_PLATFORM_WIN32s) break; /* no Unicode */
  2408. if ((lParam & GCS_RESULTSTR) == 0) /* Composition unfinished. */
  2409. break; /* fall back to DefWindowProc */
  2410. hIMC = ImmGetContext(hwnd);
  2411. n = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
  2412. if (n > 0) {
  2413. int i;
  2414. buff = snewn(n, char);
  2415. ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, buff, n);
  2416. /*
  2417. * Jaeyoun Chung reports that Korean character
  2418. * input doesn't work correctly if we do a single
  2419. * luni_send() covering the whole of buff. So
  2420. * instead we luni_send the characters one by one.
  2421. */
  2422. term_seen_key_event(term);
  2423. for (i = 0; i < n; i += 2) {
  2424. if (ldisc)
  2425. luni_send(ldisc, (unsigned short *)(buff+i), 1, 1);
  2426. }
  2427. free(buff);
  2428. }
  2429. ImmReleaseContext(hwnd, hIMC);
  2430. return 1;
  2431. }
  2432. case WM_IME_CHAR:
  2433. if (wParam & 0xFF00) {
  2434. unsigned char buf[2];
  2435. buf[1] = wParam;
  2436. buf[0] = wParam >> 8;
  2437. term_seen_key_event(term);
  2438. if (ldisc)
  2439. lpage_send(ldisc, kbd_codepage, buf, 2, 1);
  2440. } else {
  2441. char c = (unsigned char) wParam;
  2442. term_seen_key_event(term);
  2443. if (ldisc)
  2444. lpage_send(ldisc, kbd_codepage, &c, 1, 1);
  2445. }
  2446. return (0);
  2447. case WM_CHAR:
  2448. case WM_SYSCHAR:
  2449. /*
  2450. * Nevertheless, we are prepared to deal with WM_CHAR
  2451. * messages, should they crop up. So if someone wants to
  2452. * post the things to us as part of a macro manoeuvre,
  2453. * we're ready to cope.
  2454. */
  2455. {
  2456. char c = (unsigned char)wParam;
  2457. term_seen_key_event(term);
  2458. if (ldisc)
  2459. lpage_send(ldisc, CP_ACP, &c, 1, 1);
  2460. }
  2461. return 0;
  2462. case WM_SETCURSOR:
  2463. if (send_raw_mouse && LOWORD(lParam) == HTCLIENT) {
  2464. SetCursor(LoadCursor(NULL, IDC_ARROW));
  2465. return TRUE;
  2466. }
  2467. break;
  2468. case WM_SYSCOLORCHANGE:
  2469. if (cfg.system_colour) {
  2470. /* Refresh palette from system colours. */
  2471. /* XXX actually this zaps the entire palette. */
  2472. systopalette();
  2473. init_palette();
  2474. /* Force a repaint of the terminal window. */
  2475. term_invalidate(term);
  2476. }
  2477. break;
  2478. case WM_AGENT_CALLBACK:
  2479. {
  2480. struct agent_callback *c = (struct agent_callback *)lParam;
  2481. c->callback(c->callback_ctx, c->data, c->len);
  2482. sfree(c);
  2483. }
  2484. return 0;
  2485. default:
  2486. if (message == wm_mousewheel || message == WM_MOUSEWHEEL) {
  2487. int shift_pressed=0, control_pressed=0;
  2488. if (message == WM_MOUSEWHEEL) {
  2489. wheel_accumulator += (short)HIWORD(wParam);
  2490. shift_pressed=LOWORD(wParam) & MK_SHIFT;
  2491. control_pressed=LOWORD(wParam) & MK_CONTROL;
  2492. } else {
  2493. BYTE keys[256];
  2494. wheel_accumulator += (int)wParam;
  2495. if (GetKeyboardState(keys)!=0) {
  2496. shift_pressed=keys[VK_SHIFT]&0x80;
  2497. control_pressed=keys[VK_CONTROL]&0x80;
  2498. }
  2499. }
  2500. /* process events when the threshold is reached */
  2501. while (abs(wheel_accumulator) >= WHEEL_DELTA) {
  2502. int b;
  2503. /* reduce amount for next time */
  2504. if (wheel_accumulator > 0) {
  2505. b = MBT_WHEEL_UP;
  2506. wheel_accumulator -= WHEEL_DELTA;
  2507. } else if (wheel_accumulator < 0) {
  2508. b = MBT_WHEEL_DOWN;
  2509. wheel_accumulator += WHEEL_DELTA;
  2510. } else
  2511. break;
  2512. if (send_raw_mouse &&
  2513. !(cfg.mouse_override && shift_pressed)) {
  2514. /* send a mouse-down followed by a mouse up */
  2515. term_mouse(term, b, translate_button(b),
  2516. MA_CLICK,
  2517. TO_CHR_X(X_POS(lParam)),
  2518. TO_CHR_Y(Y_POS(lParam)), shift_pressed,
  2519. control_pressed, is_alt_pressed());
  2520. term_mouse(term, b, translate_button(b),
  2521. MA_RELEASE, TO_CHR_X(X_POS(lParam)),
  2522. TO_CHR_Y(Y_POS(lParam)), shift_pressed,
  2523. control_pressed, is_alt_pressed());
  2524. } else {
  2525. /* trigger a scroll */
  2526. term_scroll(term, 0,
  2527. b == MBT_WHEEL_UP ?
  2528. -term->rows / 2 : term->rows / 2);
  2529. }
  2530. }
  2531. return 0;
  2532. }
  2533. }
  2534. return DefWindowProc(hwnd, message, wParam, lParam);
  2535. }
  2536. /*
  2537. * Move the system caret. (We maintain one, even though it's
  2538. * invisible, for the benefit of blind people: apparently some
  2539. * helper software tracks the system caret, so we should arrange to
  2540. * have one.)
  2541. */
  2542. void sys_cursor(void *frontend, int x, int y)
  2543. {
  2544. int cx, cy;
  2545. if (!term->has_focus) return;
  2546. /*
  2547. * Avoid gratuitously re-updating the cursor position and IMM
  2548. * window if there's no actual change required.
  2549. */
  2550. cx = x * font_width + offset_width;
  2551. cy = y * font_height + offset_height;
  2552. if (cx == caret_x && cy == caret_y)
  2553. return;
  2554. caret_x = cx;
  2555. caret_y = cy;
  2556. sys_cursor_update();
  2557. }
  2558. static void sys_cursor_update(void)
  2559. {
  2560. COMPOSITIONFORM cf;
  2561. HIMC hIMC;
  2562. if (!term->has_focus) return;
  2563. if (caret_x < 0 || caret_y < 0)
  2564. return;
  2565. SetCaretPos(caret_x, caret_y);
  2566. /* IMM calls on Win98 and beyond only */
  2567. if(osVersion.dwPlatformId == VER_PLATFORM_WIN32s) return; /* 3.11 */
  2568. if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS &&
  2569. osVersion.dwMinorVersion == 0) return; /* 95 */
  2570. /* we should have the IMM functions */
  2571. hIMC = ImmGetContext(hwnd);
  2572. cf.dwStyle = CFS_POINT;
  2573. cf.ptCurrentPos.x = caret_x;
  2574. cf.ptCurrentPos.y = caret_y;
  2575. ImmSetCompositionWindow(hIMC, &cf);
  2576. ImmReleaseContext(hwnd, hIMC);
  2577. }
  2578. /*
  2579. * Draw a line of text in the window, at given character
  2580. * coordinates, in given attributes.
  2581. *
  2582. * We are allowed to fiddle with the contents of `text'.
  2583. */
  2584. void do_text(Context ctx, int x, int y, char *text, int len,
  2585. unsigned long attr, int lattr)
  2586. {
  2587. COLORREF fg, bg, t;
  2588. int nfg, nbg, nfont;
  2589. HDC hdc = ctx;
  2590. RECT line_box;
  2591. int force_manual_underline = 0;
  2592. int fnt_width = font_width * (1 + (lattr != LATTR_NORM));
  2593. int char_width = fnt_width;
  2594. int text_adjust = 0;
  2595. static int *IpDx = 0, IpDxLEN = 0;
  2596. if (attr & ATTR_WIDE)
  2597. char_width *= 2;
  2598. if (len > IpDxLEN || IpDx[0] != char_width) {
  2599. int i;
  2600. if (len > IpDxLEN) {
  2601. sfree(IpDx);
  2602. IpDx = snewn(len + 16, int);
  2603. IpDxLEN = (len + 16);
  2604. }
  2605. for (i = 0; i < IpDxLEN; i++)
  2606. IpDx[i] = char_width;
  2607. }
  2608. /* Only want the left half of double width lines */
  2609. if (lattr != LATTR_NORM && x*2 >= term->cols)
  2610. return;
  2611. x *= fnt_width;
  2612. y *= font_height;
  2613. x += offset_width;
  2614. y += offset_height;
  2615. if ((attr & TATTR_ACTCURS) && (cfg.cursor_type == 0 || term->big_cursor)) {
  2616. attr &= ATTR_CUR_AND | (bold_mode != BOLD_COLOURS ? ATTR_BOLD : 0);
  2617. attr ^= ATTR_CUR_XOR;
  2618. }
  2619. nfont = 0;
  2620. if (cfg.vtmode == VT_POORMAN && lattr != LATTR_NORM) {
  2621. /* Assume a poorman font is borken in other ways too. */
  2622. lattr = LATTR_WIDE;
  2623. } else
  2624. switch (lattr) {
  2625. case LATTR_NORM:
  2626. break;
  2627. case LATTR_WIDE:
  2628. nfont |= FONT_WIDE;
  2629. break;
  2630. default:
  2631. nfont |= FONT_WIDE + FONT_HIGH;
  2632. break;
  2633. }
  2634. if (attr & ATTR_NARROW)
  2635. nfont |= FONT_NARROW;
  2636. /* Special hack for the VT100 linedraw glyphs. */
  2637. if ((attr & CSET_MASK) == 0x2300) {
  2638. if (text[0] >= (char) 0xBA && text[0] <= (char) 0xBD) {
  2639. switch ((unsigned char) (text[0])) {
  2640. case 0xBA:
  2641. text_adjust = -2 * font_height / 5;
  2642. break;
  2643. case 0xBB:
  2644. text_adjust = -1 * font_height / 5;
  2645. break;
  2646. case 0xBC:
  2647. text_adjust = font_height / 5;
  2648. break;
  2649. case 0xBD:
  2650. text_adjust = 2 * font_height / 5;
  2651. break;
  2652. }
  2653. if (lattr == LATTR_TOP || lattr == LATTR_BOT)
  2654. text_adjust *= 2;
  2655. attr &= ~CSET_MASK;
  2656. text[0] = (char) (ucsdata.unitab_xterm['q'] & CHAR_MASK);
  2657. attr |= (ucsdata.unitab_xterm['q'] & CSET_MASK);
  2658. if (attr & ATTR_UNDER) {
  2659. attr &= ~ATTR_UNDER;
  2660. force_manual_underline = 1;
  2661. }
  2662. }
  2663. }
  2664. /* Anything left as an original character set is unprintable. */
  2665. if (DIRECT_CHAR(attr)) {
  2666. attr &= ~CSET_MASK;
  2667. attr |= 0xFF00;
  2668. memset(text, 0xFD, len);
  2669. }
  2670. /* OEM CP */
  2671. if ((attr & CSET_MASK) == ATTR_OEMCP)
  2672. nfont |= FONT_OEM;
  2673. nfg = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
  2674. nfg = 2 * (nfg & 0xF) + (nfg & 0x10 ? 1 : 0);
  2675. nbg = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
  2676. nbg = 2 * (nbg & 0xF) + (nbg & 0x10 ? 1 : 0);
  2677. if (bold_mode == BOLD_FONT && (attr & ATTR_BOLD))
  2678. nfont |= FONT_BOLD;
  2679. if (und_mode == UND_FONT && (attr & ATTR_UNDER))
  2680. nfont |= FONT_UNDERLINE;
  2681. another_font(nfont);
  2682. if (!fonts[nfont]) {
  2683. if (nfont & FONT_UNDERLINE)
  2684. force_manual_underline = 1;
  2685. /* Don't do the same for manual bold, it could be bad news. */
  2686. nfont &= ~(FONT_BOLD | FONT_UNDERLINE);
  2687. }
  2688. another_font(nfont);
  2689. if (!fonts[nfont])
  2690. nfont = FONT_NORMAL;
  2691. if (attr & ATTR_REVERSE) {
  2692. t = nfg;
  2693. nfg = nbg;
  2694. nbg = t;
  2695. }
  2696. if (bold_mode == BOLD_COLOURS && (attr & ATTR_BOLD))
  2697. nfg |= 1;
  2698. if (bold_mode == BOLD_COLOURS && (attr & ATTR_BLINK))
  2699. nbg |= 1;
  2700. fg = colours[nfg];
  2701. bg = colours[nbg];
  2702. SelectObject(hdc, fonts[nfont]);
  2703. SetTextColor(hdc, fg);
  2704. SetBkColor(hdc, bg);
  2705. SetBkMode(hdc, OPAQUE);
  2706. line_box.left = x;
  2707. line_box.top = y;
  2708. line_box.right = x + char_width * len;
  2709. line_box.bottom = y + font_height;
  2710. /* Only want the left half of double width lines */
  2711. if (line_box.right > font_width*term->cols+offset_width)
  2712. line_box.right = font_width*term->cols+offset_width;
  2713. /* We're using a private area for direct to font. (512 chars.) */
  2714. if (ucsdata.dbcs_screenfont && (attr & CSET_MASK) == ATTR_ACP) {
  2715. /* Ho Hum, dbcs fonts are a PITA! */
  2716. /* To display on W9x I have to convert to UCS */
  2717. static wchar_t *uni_buf = 0;
  2718. static int uni_len = 0;
  2719. int nlen, mptr;
  2720. if (len > uni_len) {
  2721. sfree(uni_buf);
  2722. uni_len = len;
  2723. uni_buf = snewn(uni_len, wchar_t);
  2724. }
  2725. for(nlen = mptr = 0; mptr<len; mptr++) {
  2726. uni_buf[nlen] = 0xFFFD;
  2727. if (IsDBCSLeadByteEx(ucsdata.font_codepage, (BYTE) text[mptr])) {
  2728. IpDx[nlen] += char_width;
  2729. MultiByteToWideChar(ucsdata.font_codepage, MB_USEGLYPHCHARS,
  2730. text+mptr, 2, uni_buf+nlen, 1);
  2731. mptr++;
  2732. }
  2733. else
  2734. {
  2735. MultiByteToWideChar(ucsdata.font_codepage, MB_USEGLYPHCHARS,
  2736. text+mptr, 1, uni_buf+nlen, 1);
  2737. }
  2738. nlen++;
  2739. }
  2740. if (nlen <= 0)
  2741. return; /* Eeek! */
  2742. ExtTextOutW(hdc, x,
  2743. y - font_height * (lattr == LATTR_BOT) + text_adjust,
  2744. ETO_CLIPPED | ETO_OPAQUE, &line_box, uni_buf, nlen, IpDx);
  2745. if (bold_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
  2746. SetBkMode(hdc, TRANSPARENT);
  2747. ExtTextOutW(hdc, x - 1,
  2748. y - font_height * (lattr ==
  2749. LATTR_BOT) + text_adjust,
  2750. ETO_CLIPPED, &line_box, uni_buf, nlen, IpDx);
  2751. }
  2752. IpDx[0] = -1;
  2753. } else if (DIRECT_FONT(attr)) {
  2754. ExtTextOut(hdc, x,
  2755. y - font_height * (lattr == LATTR_BOT) + text_adjust,
  2756. ETO_CLIPPED | ETO_OPAQUE, &line_box, text, len, IpDx);
  2757. if (bold_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
  2758. SetBkMode(hdc, TRANSPARENT);
  2759. /* GRR: This draws the character outside it's box and can leave
  2760. * 'droppings' even with the clip box! I suppose I could loop it
  2761. * one character at a time ... yuk.
  2762. *
  2763. * Or ... I could do a test print with "W", and use +1 or -1 for this
  2764. * shift depending on if the leftmost column is blank...
  2765. */
  2766. ExtTextOut(hdc, x - 1,
  2767. y - font_height * (lattr ==
  2768. LATTR_BOT) + text_adjust,
  2769. ETO_CLIPPED, &line_box, text, len, IpDx);
  2770. }
  2771. } else {
  2772. /* And 'normal' unicode characters */
  2773. static WCHAR *wbuf = NULL;
  2774. static int wlen = 0;
  2775. int i;
  2776. if (wlen < len) {
  2777. sfree(wbuf);
  2778. wlen = len;
  2779. wbuf = snewn(wlen, WCHAR);
  2780. }
  2781. for (i = 0; i < len; i++)
  2782. wbuf[i] = (WCHAR) ((attr & CSET_MASK) + (text[i] & CHAR_MASK));
  2783. ExtTextOutW(hdc, x,
  2784. y - font_height * (lattr == LATTR_BOT) + text_adjust,
  2785. ETO_CLIPPED | ETO_OPAQUE, &line_box, wbuf, len, IpDx);
  2786. /* And the shadow bold hack. */
  2787. if (bold_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
  2788. SetBkMode(hdc, TRANSPARENT);
  2789. ExtTextOutW(hdc, x - 1,
  2790. y - font_height * (lattr ==
  2791. LATTR_BOT) + text_adjust,
  2792. ETO_CLIPPED, &line_box, wbuf, len, IpDx);
  2793. }
  2794. }
  2795. if (lattr != LATTR_TOP && (force_manual_underline ||
  2796. (und_mode == UND_LINE
  2797. && (attr & ATTR_UNDER)))) {
  2798. HPEN oldpen;
  2799. int dec = descent;
  2800. if (lattr == LATTR_BOT)
  2801. dec = dec * 2 - font_height;
  2802. oldpen = SelectObject(hdc, CreatePen(PS_SOLID, 0, fg));
  2803. MoveToEx(hdc, x, y + dec, NULL);
  2804. LineTo(hdc, x + len * char_width, y + dec);
  2805. oldpen = SelectObject(hdc, oldpen);
  2806. DeleteObject(oldpen);
  2807. }
  2808. }
  2809. void do_cursor(Context ctx, int x, int y, char *text, int len,
  2810. unsigned long attr, int lattr)
  2811. {
  2812. int fnt_width;
  2813. int char_width;
  2814. HDC hdc = ctx;
  2815. int ctype = cfg.cursor_type;
  2816. if ((attr & TATTR_ACTCURS) && (ctype == 0 || term->big_cursor)) {
  2817. if (((attr & CSET_MASK) | (unsigned char) *text) != UCSWIDE) {
  2818. do_text(ctx, x, y, text, len, attr, lattr);
  2819. return;
  2820. }
  2821. ctype = 2;
  2822. attr |= TATTR_RIGHTCURS;
  2823. }
  2824. fnt_width = char_width = font_width * (1 + (lattr != LATTR_NORM));
  2825. if (attr & ATTR_WIDE)
  2826. char_width *= 2;
  2827. x *= fnt_width;
  2828. y *= font_height;
  2829. x += offset_width;
  2830. y += offset_height;
  2831. if ((attr & TATTR_PASCURS) && (ctype == 0 || term->big_cursor)) {
  2832. POINT pts[5];
  2833. HPEN oldpen;
  2834. pts[0].x = pts[1].x = pts[4].x = x;
  2835. pts[2].x = pts[3].x = x + char_width - 1;
  2836. pts[0].y = pts[3].y = pts[4].y = y;
  2837. pts[1].y = pts[2].y = y + font_height - 1;
  2838. oldpen = SelectObject(hdc, CreatePen(PS_SOLID, 0, colours[23]));
  2839. Polyline(hdc, pts, 5);
  2840. oldpen = SelectObject(hdc, oldpen);
  2841. DeleteObject(oldpen);
  2842. } else if ((attr & (TATTR_ACTCURS | TATTR_PASCURS)) && ctype != 0) {
  2843. int startx, starty, dx, dy, length, i;
  2844. if (ctype == 1) {
  2845. startx = x;
  2846. starty = y + descent;
  2847. dx = 1;
  2848. dy = 0;
  2849. length = char_width;
  2850. } else {
  2851. int xadjust = 0;
  2852. if (attr & TATTR_RIGHTCURS)
  2853. xadjust = char_width - 1;
  2854. startx = x + xadjust;
  2855. starty = y;
  2856. dx = 0;
  2857. dy = 1;
  2858. length = font_height;
  2859. }
  2860. if (attr & TATTR_ACTCURS) {
  2861. HPEN oldpen;
  2862. oldpen =
  2863. SelectObject(hdc, CreatePen(PS_SOLID, 0, colours[23]));
  2864. MoveToEx(hdc, startx, starty, NULL);
  2865. LineTo(hdc, startx + dx * length, starty + dy * length);
  2866. oldpen = SelectObject(hdc, oldpen);
  2867. DeleteObject(oldpen);
  2868. } else {
  2869. for (i = 0; i < length; i++) {
  2870. if (i % 2 == 0) {
  2871. SetPixel(hdc, startx, starty, colours[23]);
  2872. }
  2873. startx += dx;
  2874. starty += dy;
  2875. }
  2876. }
  2877. }
  2878. }
  2879. /* This function gets the actual width of a character in the normal font.
  2880. */
  2881. int char_width(Context ctx, int uc) {
  2882. HDC hdc = ctx;
  2883. int ibuf = 0;
  2884. /* If the font max is the same as the font ave width then this
  2885. * function is a no-op.
  2886. */
  2887. if (!font_dualwidth) return 1;
  2888. switch (uc & CSET_MASK) {
  2889. case ATTR_ASCII:
  2890. uc = ucsdata.unitab_line[uc & 0xFF];
  2891. break;
  2892. case ATTR_LINEDRW:
  2893. uc = ucsdata.unitab_xterm[uc & 0xFF];
  2894. break;
  2895. case ATTR_SCOACS:
  2896. uc = ucsdata.unitab_scoacs[uc & 0xFF];
  2897. break;
  2898. }
  2899. if (DIRECT_FONT(uc)) {
  2900. if (ucsdata.dbcs_screenfont) return 1;
  2901. /* Speedup, I know of no font where ascii is the wrong width */
  2902. if ((uc&CHAR_MASK) >= ' ' && (uc&CHAR_MASK)<= '~')
  2903. return 1;
  2904. if ( (uc & CSET_MASK) == ATTR_ACP ) {
  2905. SelectObject(hdc, fonts[FONT_NORMAL]);
  2906. } else if ( (uc & CSET_MASK) == ATTR_OEMCP ) {
  2907. another_font(FONT_OEM);
  2908. if (!fonts[FONT_OEM]) return 0;
  2909. SelectObject(hdc, fonts[FONT_OEM]);
  2910. } else
  2911. return 0;
  2912. if ( GetCharWidth32(hdc, uc&CHAR_MASK, uc&CHAR_MASK, &ibuf) != 1 &&
  2913. GetCharWidth(hdc, uc&CHAR_MASK, uc&CHAR_MASK, &ibuf) != 1)
  2914. return 0;
  2915. } else {
  2916. /* Speedup, I know of no font where ascii is the wrong width */
  2917. if (uc >= ' ' && uc <= '~') return 1;
  2918. SelectObject(hdc, fonts[FONT_NORMAL]);
  2919. if ( GetCharWidth32W(hdc, uc, uc, &ibuf) == 1 )
  2920. /* Okay that one worked */ ;
  2921. else if ( GetCharWidthW(hdc, uc, uc, &ibuf) == 1 )
  2922. /* This should work on 9x too, but it's "less accurate" */ ;
  2923. else
  2924. return 0;
  2925. }
  2926. ibuf += font_width / 2 -1;
  2927. ibuf /= font_width;
  2928. return ibuf;
  2929. }
  2930. /*
  2931. * Translate a WM_(SYS)?KEY(UP|DOWN) message into a string of ASCII
  2932. * codes. Returns number of bytes used or zero to drop the message
  2933. * or -1 to forward the message to windows.
  2934. */
  2935. static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
  2936. unsigned char *output)
  2937. {
  2938. BYTE keystate[256];
  2939. int scan, left_alt = 0, key_down, shift_state;
  2940. int r, i, code;
  2941. unsigned char *p = output;
  2942. static int alt_sum = 0;
  2943. HKL kbd_layout = GetKeyboardLayout(0);
  2944. /* keys is for ToAsciiEx. There's some ick here, see below. */
  2945. static WORD keys[3];
  2946. static int compose_char = 0;
  2947. static WPARAM compose_key = 0;
  2948. r = GetKeyboardState(keystate);
  2949. if (!r)
  2950. memset(keystate, 0, sizeof(keystate));
  2951. else {
  2952. #if 0
  2953. #define SHOW_TOASCII_RESULT
  2954. { /* Tell us all about key events */
  2955. static BYTE oldstate[256];
  2956. static int first = 1;
  2957. static int scan;
  2958. int ch;
  2959. if (first)
  2960. memcpy(oldstate, keystate, sizeof(oldstate));
  2961. first = 0;
  2962. if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT) {
  2963. debug(("+"));
  2964. } else if ((HIWORD(lParam) & KF_UP)
  2965. && scan == (HIWORD(lParam) & 0xFF)) {
  2966. debug((". U"));
  2967. } else {
  2968. debug((".\n"));
  2969. if (wParam >= VK_F1 && wParam <= VK_F20)
  2970. debug(("K_F%d", wParam + 1 - VK_F1));
  2971. else
  2972. switch (wParam) {
  2973. case VK_SHIFT:
  2974. debug(("SHIFT"));
  2975. break;
  2976. case VK_CONTROL:
  2977. debug(("CTRL"));
  2978. break;
  2979. case VK_MENU:
  2980. debug(("ALT"));
  2981. break;
  2982. default:
  2983. debug(("VK_%02x", wParam));
  2984. }
  2985. if (message == WM_SYSKEYDOWN || message == WM_SYSKEYUP)
  2986. debug(("*"));
  2987. debug((", S%02x", scan = (HIWORD(lParam) & 0xFF)));
  2988. ch = MapVirtualKeyEx(wParam, 2, kbd_layout);
  2989. if (ch >= ' ' && ch <= '~')
  2990. debug((", '%c'", ch));
  2991. else if (ch)
  2992. debug((", $%02x", ch));
  2993. if (keys[0])
  2994. debug((", KB0=%02x", keys[0]));
  2995. if (keys[1])
  2996. debug((", KB1=%02x", keys[1]));
  2997. if (keys[2])
  2998. debug((", KB2=%02x", keys[2]));
  2999. if ((keystate[VK_SHIFT] & 0x80) != 0)
  3000. debug((", S"));
  3001. if ((keystate[VK_CONTROL] & 0x80) != 0)
  3002. debug((", C"));
  3003. if ((HIWORD(lParam) & KF_EXTENDED))
  3004. debug((", E"));
  3005. if ((HIWORD(lParam) & KF_UP))
  3006. debug((", U"));
  3007. }
  3008. if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT);
  3009. else if ((HIWORD(lParam) & KF_UP))
  3010. oldstate[wParam & 0xFF] ^= 0x80;
  3011. else
  3012. oldstate[wParam & 0xFF] ^= 0x81;
  3013. for (ch = 0; ch < 256; ch++)
  3014. if (oldstate[ch] != keystate[ch])
  3015. debug((", M%02x=%02x", ch, keystate[ch]));
  3016. memcpy(oldstate, keystate, sizeof(oldstate));
  3017. }
  3018. #endif
  3019. if (wParam == VK_MENU && (HIWORD(lParam) & KF_EXTENDED)) {
  3020. keystate[VK_RMENU] = keystate[VK_MENU];
  3021. }
  3022. /* Nastyness with NUMLock - Shift-NUMLock is left alone though */
  3023. if ((cfg.funky_type == FUNKY_VT400 ||
  3024. (cfg.funky_type <= FUNKY_LINUX && term->app_keypad_keys &&
  3025. !cfg.no_applic_k))
  3026. && wParam == VK_NUMLOCK && !(keystate[VK_SHIFT] & 0x80)) {
  3027. wParam = VK_EXECUTE;
  3028. /* UnToggle NUMLock */
  3029. if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == 0)
  3030. keystate[VK_NUMLOCK] ^= 1;
  3031. }
  3032. /* And write back the 'adjusted' state */
  3033. SetKeyboardState(keystate);
  3034. }
  3035. /* Disable Auto repeat if required */
  3036. if (term->repeat_off &&
  3037. (HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT)
  3038. return 0;
  3039. if ((HIWORD(lParam) & KF_ALTDOWN) && (keystate[VK_RMENU] & 0x80) == 0)
  3040. left_alt = 1;
  3041. key_down = ((HIWORD(lParam) & KF_UP) == 0);
  3042. /* Make sure Ctrl-ALT is not the same as AltGr for ToAscii unless told. */
  3043. if (left_alt && (keystate[VK_CONTROL] & 0x80)) {
  3044. if (cfg.ctrlaltkeys)
  3045. keystate[VK_MENU] = 0;
  3046. else {
  3047. keystate[VK_RMENU] = 0x80;
  3048. left_alt = 0;
  3049. }
  3050. }
  3051. scan = (HIWORD(lParam) & (KF_UP | KF_EXTENDED | 0xFF));
  3052. shift_state = ((keystate[VK_SHIFT] & 0x80) != 0)
  3053. + ((keystate[VK_CONTROL] & 0x80) != 0) * 2;
  3054. /* Note if AltGr was pressed and if it was used as a compose key */
  3055. if (!compose_state) {
  3056. compose_key = 0x100;
  3057. if (cfg.compose_key) {
  3058. if (wParam == VK_MENU && (HIWORD(lParam) & KF_EXTENDED))
  3059. compose_key = wParam;
  3060. }
  3061. if (wParam == VK_APPS)
  3062. compose_key = wParam;
  3063. }
  3064. if (wParam == compose_key) {
  3065. if (compose_state == 0
  3066. && (HIWORD(lParam) & (KF_UP | KF_REPEAT)) == 0) compose_state =
  3067. 1;
  3068. else if (compose_state == 1 && (HIWORD(lParam) & KF_UP))
  3069. compose_state = 2;
  3070. else
  3071. compose_state = 0;
  3072. } else if (compose_state == 1 && wParam != VK_CONTROL)
  3073. compose_state = 0;
  3074. if (compose_state > 1 && left_alt)
  3075. compose_state = 0;
  3076. /* Sanitize the number pad if not using a PC NumPad */
  3077. if (left_alt || (term->app_keypad_keys && !cfg.no_applic_k
  3078. && cfg.funky_type != FUNKY_XTERM)
  3079. || cfg.funky_type == FUNKY_VT400 || cfg.nethack_keypad || compose_state) {
  3080. if ((HIWORD(lParam) & KF_EXTENDED) == 0) {
  3081. int nParam = 0;
  3082. switch (wParam) {
  3083. case VK_INSERT:
  3084. nParam = VK_NUMPAD0;
  3085. break;
  3086. case VK_END:
  3087. nParam = VK_NUMPAD1;
  3088. break;
  3089. case VK_DOWN:
  3090. nParam = VK_NUMPAD2;
  3091. break;
  3092. case VK_NEXT:
  3093. nParam = VK_NUMPAD3;
  3094. break;
  3095. case VK_LEFT:
  3096. nParam = VK_NUMPAD4;
  3097. break;
  3098. case VK_CLEAR:
  3099. nParam = VK_NUMPAD5;
  3100. break;
  3101. case VK_RIGHT:
  3102. nParam = VK_NUMPAD6;
  3103. break;
  3104. case VK_HOME:
  3105. nParam = VK_NUMPAD7;
  3106. break;
  3107. case VK_UP:
  3108. nParam = VK_NUMPAD8;
  3109. break;
  3110. case VK_PRIOR:
  3111. nParam = VK_NUMPAD9;
  3112. break;
  3113. case VK_DELETE:
  3114. nParam = VK_DECIMAL;
  3115. break;
  3116. }
  3117. if (nParam) {
  3118. if (keystate[VK_NUMLOCK] & 1)
  3119. shift_state |= 1;
  3120. wParam = nParam;
  3121. }
  3122. }
  3123. }
  3124. /* If a key is pressed and AltGr is not active */
  3125. if (key_down && (keystate[VK_RMENU] & 0x80) == 0 && !compose_state) {
  3126. /* Okay, prepare for most alts then ... */
  3127. if (left_alt)
  3128. *p++ = '\033';
  3129. /* Lets see if it's a pattern we know all about ... */
  3130. if (wParam == VK_PRIOR && shift_state == 1) {
  3131. SendMessage(hwnd, WM_VSCROLL, SB_PAGEUP, 0);
  3132. return 0;
  3133. }
  3134. if (wParam == VK_PRIOR && shift_state == 2) {
  3135. SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, 0);
  3136. return 0;
  3137. }
  3138. if (wParam == VK_NEXT && shift_state == 1) {
  3139. SendMessage(hwnd, WM_VSCROLL, SB_PAGEDOWN, 0);
  3140. return 0;
  3141. }
  3142. if (wParam == VK_NEXT && shift_state == 2) {
  3143. SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, 0);
  3144. return 0;
  3145. }
  3146. if (wParam == VK_INSERT && shift_state == 1) {
  3147. term_do_paste(term);
  3148. return 0;
  3149. }
  3150. if (left_alt && wParam == VK_F4 && cfg.alt_f4) {
  3151. return -1;
  3152. }
  3153. if (left_alt && wParam == VK_SPACE && cfg.alt_space) {
  3154. SendMessage(hwnd, WM_SYSCOMMAND, SC_KEYMENU, 0);
  3155. return -1;
  3156. }
  3157. if (left_alt && wParam == VK_RETURN && cfg.fullscreenonaltenter &&
  3158. (cfg.resize_action != RESIZE_DISABLED)) {
  3159. if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) != KF_REPEAT)
  3160. flip_full_screen();
  3161. return -1;
  3162. }
  3163. /* Control-Numlock for app-keypad mode switch */
  3164. if (wParam == VK_PAUSE && shift_state == 2) {
  3165. term->app_keypad_keys ^= 1;
  3166. return 0;
  3167. }
  3168. /* Nethack keypad */
  3169. if (cfg.nethack_keypad && !left_alt) {
  3170. switch (wParam) {
  3171. case VK_NUMPAD1:
  3172. *p++ = shift_state ? 'B' : 'b';
  3173. return p - output;
  3174. case VK_NUMPAD2:
  3175. *p++ = shift_state ? 'J' : 'j';
  3176. return p - output;
  3177. case VK_NUMPAD3:
  3178. *p++ = shift_state ? 'N' : 'n';
  3179. return p - output;
  3180. case VK_NUMPAD4:
  3181. *p++ = shift_state ? 'H' : 'h';
  3182. return p - output;
  3183. case VK_NUMPAD5:
  3184. *p++ = shift_state ? '.' : '.';
  3185. return p - output;
  3186. case VK_NUMPAD6:
  3187. *p++ = shift_state ? 'L' : 'l';
  3188. return p - output;
  3189. case VK_NUMPAD7:
  3190. *p++ = shift_state ? 'Y' : 'y';
  3191. return p - output;
  3192. case VK_NUMPAD8:
  3193. *p++ = shift_state ? 'K' : 'k';
  3194. return p - output;
  3195. case VK_NUMPAD9:
  3196. *p++ = shift_state ? 'U' : 'u';
  3197. return p - output;
  3198. }
  3199. }
  3200. /* Application Keypad */
  3201. if (!left_alt) {
  3202. int xkey = 0;
  3203. if (cfg.funky_type == FUNKY_VT400 ||
  3204. (cfg.funky_type <= FUNKY_LINUX &&
  3205. term->app_keypad_keys && !cfg.no_applic_k)) switch (wParam) {
  3206. case VK_EXECUTE:
  3207. xkey = 'P';
  3208. break;
  3209. case VK_DIVIDE:
  3210. xkey = 'Q';
  3211. break;
  3212. case VK_MULTIPLY:
  3213. xkey = 'R';
  3214. break;
  3215. case VK_SUBTRACT:
  3216. xkey = 'S';
  3217. break;
  3218. }
  3219. if (term->app_keypad_keys && !cfg.no_applic_k)
  3220. switch (wParam) {
  3221. case VK_NUMPAD0:
  3222. xkey = 'p';
  3223. break;
  3224. case VK_NUMPAD1:
  3225. xkey = 'q';
  3226. break;
  3227. case VK_NUMPAD2:
  3228. xkey = 'r';
  3229. break;
  3230. case VK_NUMPAD3:
  3231. xkey = 's';
  3232. break;
  3233. case VK_NUMPAD4:
  3234. xkey = 't';
  3235. break;
  3236. case VK_NUMPAD5:
  3237. xkey = 'u';
  3238. break;
  3239. case VK_NUMPAD6:
  3240. xkey = 'v';
  3241. break;
  3242. case VK_NUMPAD7:
  3243. xkey = 'w';
  3244. break;
  3245. case VK_NUMPAD8:
  3246. xkey = 'x';
  3247. break;
  3248. case VK_NUMPAD9:
  3249. xkey = 'y';
  3250. break;
  3251. case VK_DECIMAL:
  3252. xkey = 'n';
  3253. break;
  3254. case VK_ADD:
  3255. if (cfg.funky_type == FUNKY_XTERM) {
  3256. if (shift_state)
  3257. xkey = 'l';
  3258. else
  3259. xkey = 'k';
  3260. } else if (shift_state)
  3261. xkey = 'm';
  3262. else
  3263. xkey = 'l';
  3264. break;
  3265. case VK_DIVIDE:
  3266. if (cfg.funky_type == FUNKY_XTERM)
  3267. xkey = 'o';
  3268. break;
  3269. case VK_MULTIPLY:
  3270. if (cfg.funky_type == FUNKY_XTERM)
  3271. xkey = 'j';
  3272. break;
  3273. case VK_SUBTRACT:
  3274. if (cfg.funky_type == FUNKY_XTERM)
  3275. xkey = 'm';
  3276. break;
  3277. case VK_RETURN:
  3278. if (HIWORD(lParam) & KF_EXTENDED)
  3279. xkey = 'M';
  3280. break;
  3281. }
  3282. if (xkey) {
  3283. if (term->vt52_mode) {
  3284. if (xkey >= 'P' && xkey <= 'S')
  3285. p += sprintf((char *) p, "\x1B%c", xkey);
  3286. else
  3287. p += sprintf((char *) p, "\x1B?%c", xkey);
  3288. } else
  3289. p += sprintf((char *) p, "\x1BO%c", xkey);
  3290. return p - output;
  3291. }
  3292. }
  3293. if (wParam == VK_BACK && shift_state == 0) { /* Backspace */
  3294. *p++ = (cfg.bksp_is_delete ? 0x7F : 0x08);
  3295. *p++ = 0;
  3296. return -2;
  3297. }
  3298. if (wParam == VK_BACK && shift_state == 1) { /* Shift Backspace */
  3299. /* We do the opposite of what is configured */
  3300. *p++ = (cfg.bksp_is_delete ? 0x08 : 0x7F);
  3301. *p++ = 0;
  3302. return -2;
  3303. }
  3304. if (wParam == VK_TAB && shift_state == 1) { /* Shift tab */
  3305. *p++ = 0x1B;
  3306. *p++ = '[';
  3307. *p++ = 'Z';
  3308. return p - output;
  3309. }
  3310. if (wParam == VK_SPACE && shift_state == 2) { /* Ctrl-Space */
  3311. *p++ = 0;
  3312. return p - output;
  3313. }
  3314. if (wParam == VK_SPACE && shift_state == 3) { /* Ctrl-Shift-Space */
  3315. *p++ = 160;
  3316. return p - output;
  3317. }
  3318. if (wParam == VK_CANCEL && shift_state == 2) { /* Ctrl-Break */
  3319. *p++ = 3;
  3320. *p++ = 0;
  3321. return -2;
  3322. }
  3323. if (wParam == VK_PAUSE) { /* Break/Pause */
  3324. *p++ = 26;
  3325. *p++ = 0;
  3326. return -2;
  3327. }
  3328. /* Control-2 to Control-8 are special */
  3329. if (shift_state == 2 && wParam >= '2' && wParam <= '8') {
  3330. *p++ = "\000\033\034\035\036\037\177"[wParam - '2'];
  3331. return p - output;
  3332. }
  3333. if (shift_state == 2 && (wParam == 0xBD || wParam == 0xBF)) {
  3334. *p++ = 0x1F;
  3335. return p - output;
  3336. }
  3337. if (shift_state == 2 && wParam == 0xDF) {
  3338. *p++ = 0x1C;
  3339. return p - output;
  3340. }
  3341. if (shift_state == 3 && wParam == 0xDE) {
  3342. *p++ = 0x1E; /* Ctrl-~ == Ctrl-^ in xterm at least */
  3343. return p - output;
  3344. }
  3345. if (shift_state == 0 && wParam == VK_RETURN && term->cr_lf_return) {
  3346. *p++ = '\r';
  3347. *p++ = '\n';
  3348. return p - output;
  3349. }
  3350. /*
  3351. * Next, all the keys that do tilde codes. (ESC '[' nn '~',
  3352. * for integer decimal nn.)
  3353. *
  3354. * We also deal with the weird ones here. Linux VCs replace F1
  3355. * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
  3356. * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
  3357. * respectively.
  3358. */
  3359. code = 0;
  3360. switch (wParam) {
  3361. case VK_F1:
  3362. code = (keystate[VK_SHIFT] & 0x80 ? 23 : 11);
  3363. break;
  3364. case VK_F2:
  3365. code = (keystate[VK_SHIFT] & 0x80 ? 24 : 12);
  3366. break;
  3367. case VK_F3:
  3368. code = (keystate[VK_SHIFT] & 0x80 ? 25 : 13);
  3369. break;
  3370. case VK_F4:
  3371. code = (keystate[VK_SHIFT] & 0x80 ? 26 : 14);
  3372. break;
  3373. case VK_F5:
  3374. code = (keystate[VK_SHIFT] & 0x80 ? 28 : 15);
  3375. break;
  3376. case VK_F6:
  3377. code = (keystate[VK_SHIFT] & 0x80 ? 29 : 17);
  3378. break;
  3379. case VK_F7:
  3380. code = (keystate[VK_SHIFT] & 0x80 ? 31 : 18);
  3381. break;
  3382. case VK_F8:
  3383. code = (keystate[VK_SHIFT] & 0x80 ? 32 : 19);
  3384. break;
  3385. case VK_F9:
  3386. code = (keystate[VK_SHIFT] & 0x80 ? 33 : 20);
  3387. break;
  3388. case VK_F10:
  3389. code = (keystate[VK_SHIFT] & 0x80 ? 34 : 21);
  3390. break;
  3391. case VK_F11:
  3392. code = 23;
  3393. break;
  3394. case VK_F12:
  3395. code = 24;
  3396. break;
  3397. case VK_F13:
  3398. code = 25;
  3399. break;
  3400. case VK_F14:
  3401. code = 26;
  3402. break;
  3403. case VK_F15:
  3404. code = 28;
  3405. break;
  3406. case VK_F16:
  3407. code = 29;
  3408. break;
  3409. case VK_F17:
  3410. code = 31;
  3411. break;
  3412. case VK_F18:
  3413. code = 32;
  3414. break;
  3415. case VK_F19:
  3416. code = 33;
  3417. break;
  3418. case VK_F20:
  3419. code = 34;
  3420. break;
  3421. }
  3422. if ((shift_state&2) == 0) switch (wParam) {
  3423. case VK_HOME:
  3424. code = 1;
  3425. break;
  3426. case VK_INSERT:
  3427. code = 2;
  3428. break;
  3429. case VK_DELETE:
  3430. code = 3;
  3431. break;
  3432. case VK_END:
  3433. code = 4;
  3434. break;
  3435. case VK_PRIOR:
  3436. code = 5;
  3437. break;
  3438. case VK_NEXT:
  3439. code = 6;
  3440. break;
  3441. }
  3442. /* Reorder edit keys to physical order */
  3443. if (cfg.funky_type == FUNKY_VT400 && code <= 6)
  3444. code = "\0\2\1\4\5\3\6"[code];
  3445. if (term->vt52_mode && code > 0 && code <= 6) {
  3446. p += sprintf((char *) p, "\x1B%c", " HLMEIG"[code]);
  3447. return p - output;
  3448. }
  3449. if (cfg.funky_type == FUNKY_SCO && /* SCO function keys */
  3450. code >= 11 && code <= 34) {
  3451. char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
  3452. int index = 0;
  3453. switch (wParam) {
  3454. case VK_F1: index = 0; break;
  3455. case VK_F2: index = 1; break;
  3456. case VK_F3: index = 2; break;
  3457. case VK_F4: index = 3; break;
  3458. case VK_F5: index = 4; break;
  3459. case VK_F6: index = 5; break;
  3460. case VK_F7: index = 6; break;
  3461. case VK_F8: index = 7; break;
  3462. case VK_F9: index = 8; break;
  3463. case VK_F10: index = 9; break;
  3464. case VK_F11: index = 10; break;
  3465. case VK_F12: index = 11; break;
  3466. }
  3467. if (keystate[VK_SHIFT] & 0x80) index += 12;
  3468. if (keystate[VK_CONTROL] & 0x80) index += 24;
  3469. p += sprintf((char *) p, "\x1B[%c", codes[index]);
  3470. return p - output;
  3471. }
  3472. if (cfg.funky_type == FUNKY_SCO && /* SCO small keypad */
  3473. code >= 1 && code <= 6) {
  3474. char codes[] = "HL.FIG";
  3475. if (code == 3) {
  3476. *p++ = '\x7F';
  3477. } else {
  3478. p += sprintf((char *) p, "\x1B[%c", codes[code-1]);
  3479. }
  3480. return p - output;
  3481. }
  3482. if ((term->vt52_mode || cfg.funky_type == FUNKY_VT100P) && code >= 11 && code <= 24) {
  3483. int offt = 0;
  3484. if (code > 15)
  3485. offt++;
  3486. if (code > 21)
  3487. offt++;
  3488. if (term->vt52_mode)
  3489. p += sprintf((char *) p, "\x1B%c", code + 'P' - 11 - offt);
  3490. else
  3491. p +=
  3492. sprintf((char *) p, "\x1BO%c", code + 'P' - 11 - offt);
  3493. return p - output;
  3494. }
  3495. if (cfg.funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
  3496. p += sprintf((char *) p, "\x1B[[%c", code + 'A' - 11);
  3497. return p - output;
  3498. }
  3499. if (cfg.funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
  3500. if (term->vt52_mode)
  3501. p += sprintf((char *) p, "\x1B%c", code + 'P' - 11);
  3502. else
  3503. p += sprintf((char *) p, "\x1BO%c", code + 'P' - 11);
  3504. return p - output;
  3505. }
  3506. if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
  3507. p += sprintf((char *) p, code == 1 ? "\x1B[H" : "\x1BOw");
  3508. return p - output;
  3509. }
  3510. if (code) {
  3511. p += sprintf((char *) p, "\x1B[%d~", code);
  3512. return p - output;
  3513. }
  3514. /*
  3515. * Now the remaining keys (arrows and Keypad 5. Keypad 5 for
  3516. * some reason seems to send VK_CLEAR to Windows...).
  3517. */
  3518. {
  3519. char xkey = 0;
  3520. switch (wParam) {
  3521. case VK_UP:
  3522. xkey = 'A';
  3523. break;
  3524. case VK_DOWN:
  3525. xkey = 'B';
  3526. break;
  3527. case VK_RIGHT:
  3528. xkey = 'C';
  3529. break;
  3530. case VK_LEFT:
  3531. xkey = 'D';
  3532. break;
  3533. case VK_CLEAR:
  3534. xkey = 'G';
  3535. break;
  3536. }
  3537. if (xkey) {
  3538. if (term->vt52_mode)
  3539. p += sprintf((char *) p, "\x1B%c", xkey);
  3540. else {
  3541. int app_flg = (term->app_cursor_keys && !cfg.no_applic_c);
  3542. #if 0
  3543. /*
  3544. * RDB: VT100 & VT102 manuals both state the
  3545. * app cursor keys only work if the app keypad
  3546. * is on.
  3547. *
  3548. * SGT: That may well be true, but xterm
  3549. * disagrees and so does at least one
  3550. * application, so I've #if'ed this out and the
  3551. * behaviour is back to PuTTY's original: app
  3552. * cursor and app keypad are independently
  3553. * switchable modes. If anyone complains about
  3554. * _this_ I'll have to put in a configurable
  3555. * option.
  3556. */
  3557. if (!term->app_keypad_keys)
  3558. app_flg = 0;
  3559. #endif
  3560. /* Useful mapping of Ctrl-arrows */
  3561. if (shift_state == 2)
  3562. app_flg = !app_flg;
  3563. if (app_flg)
  3564. p += sprintf((char *) p, "\x1BO%c", xkey);
  3565. else
  3566. p += sprintf((char *) p, "\x1B[%c", xkey);
  3567. }
  3568. return p - output;
  3569. }
  3570. }
  3571. /*
  3572. * Finally, deal with Return ourselves. (Win95 seems to
  3573. * foul it up when Alt is pressed, for some reason.)
  3574. */
  3575. if (wParam == VK_RETURN) { /* Return */
  3576. *p++ = 0x0D;
  3577. *p++ = 0;
  3578. return -2;
  3579. }
  3580. if (left_alt && wParam >= VK_NUMPAD0 && wParam <= VK_NUMPAD9)
  3581. alt_sum = alt_sum * 10 + wParam - VK_NUMPAD0;
  3582. else
  3583. alt_sum = 0;
  3584. }
  3585. /* Okay we've done everything interesting; let windows deal with
  3586. * the boring stuff */
  3587. {
  3588. BOOL capsOn=0;
  3589. /* helg: clear CAPS LOCK state if caps lock switches to cyrillic */
  3590. if(cfg.xlat_capslockcyr && keystate[VK_CAPITAL] != 0) {
  3591. capsOn= !left_alt;
  3592. keystate[VK_CAPITAL] = 0;
  3593. }
  3594. /* XXX how do we know what the max size of the keys array should
  3595. * be is? There's indication on MS' website of an Inquire/InquireEx
  3596. * functioning returning a KBINFO structure which tells us. */
  3597. if (osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT) {
  3598. /* XXX 'keys' parameter is declared in MSDN documentation as
  3599. * 'LPWORD lpChar'.
  3600. * The experience of a French user indicates that on
  3601. * Win98, WORD[] should be passed in, but on Win2K, it should
  3602. * be BYTE[]. German WinXP and my Win2K with "US International"
  3603. * driver corroborate this.
  3604. * Experimentally I've conditionalised the behaviour on the
  3605. * Win9x/NT split, but I suspect it's worse than that.
  3606. * See wishlist item `win-dead-keys' for more horrible detail
  3607. * and speculations. */
  3608. BYTE keybs[3];
  3609. int i;
  3610. r = ToAsciiEx(wParam, scan, keystate, (LPWORD)keybs, 0, kbd_layout);
  3611. for (i=0; i<3; i++) keys[i] = keybs[i];
  3612. } else {
  3613. r = ToAsciiEx(wParam, scan, keystate, keys, 0, kbd_layout);
  3614. }
  3615. #ifdef SHOW_TOASCII_RESULT
  3616. if (r == 1 && !key_down) {
  3617. if (alt_sum) {
  3618. if (in_utf(term) || ucsdata.dbcs_screenfont)
  3619. debug((", (U+%04x)", alt_sum));
  3620. else
  3621. debug((", LCH(%d)", alt_sum));
  3622. } else {
  3623. debug((", ACH(%d)", keys[0]));
  3624. }
  3625. } else if (r > 0) {
  3626. int r1;
  3627. debug((", ASC("));
  3628. for (r1 = 0; r1 < r; r1++) {
  3629. debug(("%s%d", r1 ? "," : "", keys[r1]));
  3630. }
  3631. debug((")"));
  3632. }
  3633. #endif
  3634. if (r > 0) {
  3635. WCHAR keybuf;
  3636. /*
  3637. * Interrupt an ongoing paste. I'm not sure this is
  3638. * sensible, but for the moment it's preferable to
  3639. * having to faff about buffering things.
  3640. */
  3641. term_nopaste(term);
  3642. p = output;
  3643. for (i = 0; i < r; i++) {
  3644. unsigned char ch = (unsigned char) keys[i];
  3645. if (compose_state == 2 && (ch & 0x80) == 0 && ch > ' ') {
  3646. compose_char = ch;
  3647. compose_state++;
  3648. continue;
  3649. }
  3650. if (compose_state == 3 && (ch & 0x80) == 0 && ch > ' ') {
  3651. int nc;
  3652. compose_state = 0;
  3653. if ((nc = check_compose(compose_char, ch)) == -1) {
  3654. MessageBeep(MB_ICONHAND);
  3655. return 0;
  3656. }
  3657. keybuf = nc;
  3658. term_seen_key_event(term);
  3659. if (ldisc)
  3660. luni_send(ldisc, &keybuf, 1, 1);
  3661. continue;
  3662. }
  3663. compose_state = 0;
  3664. if (!key_down) {
  3665. if (alt_sum) {
  3666. if (in_utf(term) || ucsdata.dbcs_screenfont) {
  3667. keybuf = alt_sum;
  3668. term_seen_key_event(term);
  3669. if (ldisc)
  3670. luni_send(ldisc, &keybuf, 1, 1);
  3671. } else {
  3672. ch = (char) alt_sum;
  3673. /*
  3674. * We need not bother about stdin
  3675. * backlogs here, because in GUI PuTTY
  3676. * we can't do anything about it
  3677. * anyway; there's no means of asking
  3678. * Windows to hold off on KEYDOWN
  3679. * messages. We _have_ to buffer
  3680. * everything we're sent.
  3681. */
  3682. term_seen_key_event(term);
  3683. if (ldisc)
  3684. ldisc_send(ldisc, &ch, 1, 1);
  3685. }
  3686. alt_sum = 0;
  3687. } else {
  3688. term_seen_key_event(term);
  3689. if (ldisc)
  3690. lpage_send(ldisc, kbd_codepage, &ch, 1, 1);
  3691. }
  3692. } else {
  3693. if(capsOn && ch < 0x80) {
  3694. WCHAR cbuf[2];
  3695. cbuf[0] = 27;
  3696. cbuf[1] = xlat_uskbd2cyrllic(ch);
  3697. term_seen_key_event(term);
  3698. if (ldisc)
  3699. luni_send(ldisc, cbuf+!left_alt, 1+!!left_alt, 1);
  3700. } else {
  3701. char cbuf[2];
  3702. cbuf[0] = '\033';
  3703. cbuf[1] = ch;
  3704. term_seen_key_event(term);
  3705. if (ldisc)
  3706. lpage_send(ldisc, kbd_codepage,
  3707. cbuf+!left_alt, 1+!!left_alt, 1);
  3708. }
  3709. }
  3710. show_mouseptr(0);
  3711. }
  3712. /* This is so the ALT-Numpad and dead keys work correctly. */
  3713. keys[0] = 0;
  3714. return p - output;
  3715. }
  3716. /* If we're definitly not building up an ALT-54321 then clear it */
  3717. if (!left_alt)
  3718. keys[0] = 0;
  3719. /* If we will be using alt_sum fix the 256s */
  3720. else if (keys[0] && (in_utf(term) || ucsdata.dbcs_screenfont))
  3721. keys[0] = 10;
  3722. }
  3723. /*
  3724. * ALT alone may or may not want to bring up the System menu.
  3725. * If it's not meant to, we return 0 on presses or releases of
  3726. * ALT, to show that we've swallowed the keystroke. Otherwise
  3727. * we return -1, which means Windows will give the keystroke
  3728. * its default handling (i.e. bring up the System menu).
  3729. */
  3730. if (wParam == VK_MENU && !cfg.alt_only)
  3731. return 0;
  3732. return -1;
  3733. }
  3734. void request_paste(void *frontend)
  3735. {
  3736. /*
  3737. * In Windows, pasting is synchronous: we can read the
  3738. * clipboard with no difficulty, so request_paste() can just go
  3739. * ahead and paste.
  3740. */
  3741. term_do_paste(term);
  3742. }
  3743. void set_title(void *frontend, char *title)
  3744. {
  3745. sfree(window_name);
  3746. window_name = snewn(1 + strlen(title), char);
  3747. strcpy(window_name, title);
  3748. if (cfg.win_name_always || !IsIconic(hwnd))
  3749. SetWindowText(hwnd, title);
  3750. }
  3751. void set_icon(void *frontend, char *title)
  3752. {
  3753. sfree(icon_name);
  3754. icon_name = snewn(1 + strlen(title), char);
  3755. strcpy(icon_name, title);
  3756. if (!cfg.win_name_always && IsIconic(hwnd))
  3757. SetWindowText(hwnd, title);
  3758. }
  3759. void set_sbar(void *frontend, int total, int start, int page)
  3760. {
  3761. SCROLLINFO si;
  3762. if (is_full_screen() ? !cfg.scrollbar_in_fullscreen : !cfg.scrollbar)
  3763. return;
  3764. si.cbSize = sizeof(si);
  3765. si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
  3766. si.nMin = 0;
  3767. si.nMax = total - 1;
  3768. si.nPage = page;
  3769. si.nPos = start;
  3770. if (hwnd)
  3771. SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
  3772. }
  3773. Context get_ctx(void *frontend)
  3774. {
  3775. HDC hdc;
  3776. if (hwnd) {
  3777. hdc = GetDC(hwnd);
  3778. if (hdc && pal)
  3779. SelectPalette(hdc, pal, FALSE);
  3780. return hdc;
  3781. } else
  3782. return NULL;
  3783. }
  3784. void free_ctx(Context ctx)
  3785. {
  3786. SelectPalette(ctx, GetStockObject(DEFAULT_PALETTE), FALSE);
  3787. ReleaseDC(hwnd, ctx);
  3788. }
  3789. static void real_palette_set(int n, int r, int g, int b)
  3790. {
  3791. if (pal) {
  3792. logpal->palPalEntry[n].peRed = r;
  3793. logpal->palPalEntry[n].peGreen = g;
  3794. logpal->palPalEntry[n].peBlue = b;
  3795. logpal->palPalEntry[n].peFlags = PC_NOCOLLAPSE;
  3796. colours[n] = PALETTERGB(r, g, b);
  3797. SetPaletteEntries(pal, 0, NCOLOURS, logpal->palPalEntry);
  3798. } else
  3799. colours[n] = RGB(r, g, b);
  3800. }
  3801. void palette_set(void *frontend, int n, int r, int g, int b)
  3802. {
  3803. static const int first[21] = {
  3804. 0, 2, 4, 6, 8, 10, 12, 14,
  3805. 1, 3, 5, 7, 9, 11, 13, 15,
  3806. 16, 17, 18, 20, 22
  3807. };
  3808. real_palette_set(first[n], r, g, b);
  3809. if (first[n] >= 18)
  3810. real_palette_set(first[n] + 1, r, g, b);
  3811. if (pal) {
  3812. HDC hdc = get_ctx(frontend);
  3813. UnrealizeObject(pal);
  3814. RealizePalette(hdc);
  3815. free_ctx(hdc);
  3816. }
  3817. }
  3818. void palette_reset(void *frontend)
  3819. {
  3820. int i;
  3821. for (i = 0; i < NCOLOURS; i++) {
  3822. if (pal) {
  3823. logpal->palPalEntry[i].peRed = defpal[i].rgbtRed;
  3824. logpal->palPalEntry[i].peGreen = defpal[i].rgbtGreen;
  3825. logpal->palPalEntry[i].peBlue = defpal[i].rgbtBlue;
  3826. logpal->palPalEntry[i].peFlags = 0;
  3827. colours[i] = PALETTERGB(defpal[i].rgbtRed,
  3828. defpal[i].rgbtGreen,
  3829. defpal[i].rgbtBlue);
  3830. } else
  3831. colours[i] = RGB(defpal[i].rgbtRed,
  3832. defpal[i].rgbtGreen, defpal[i].rgbtBlue);
  3833. }
  3834. if (pal) {
  3835. HDC hdc;
  3836. SetPaletteEntries(pal, 0, NCOLOURS, logpal->palPalEntry);
  3837. hdc = get_ctx(frontend);
  3838. RealizePalette(hdc);
  3839. free_ctx(hdc);
  3840. }
  3841. }
  3842. void write_aclip(void *frontend, char *data, int len, int must_deselect)
  3843. {
  3844. HGLOBAL clipdata;
  3845. void *lock;
  3846. clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
  3847. if (!clipdata)
  3848. return;
  3849. lock = GlobalLock(clipdata);
  3850. if (!lock)
  3851. return;
  3852. memcpy(lock, data, len);
  3853. ((unsigned char *) lock)[len] = 0;
  3854. GlobalUnlock(clipdata);
  3855. if (!must_deselect)
  3856. SendMessage(hwnd, WM_IGNORE_CLIP, TRUE, 0);
  3857. if (OpenClipboard(hwnd)) {
  3858. EmptyClipboard();
  3859. SetClipboardData(CF_TEXT, clipdata);
  3860. CloseClipboard();
  3861. } else
  3862. GlobalFree(clipdata);
  3863. if (!must_deselect)
  3864. SendMessage(hwnd, WM_IGNORE_CLIP, FALSE, 0);
  3865. }
  3866. /*
  3867. * Note: unlike write_aclip() this will not append a nul.
  3868. */
  3869. void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
  3870. {
  3871. HGLOBAL clipdata, clipdata2, clipdata3;
  3872. int len2;
  3873. void *lock, *lock2, *lock3;
  3874. len2 = WideCharToMultiByte(CP_ACP, 0, data, len, 0, 0, NULL, NULL);
  3875. clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE,
  3876. len * sizeof(wchar_t));
  3877. clipdata2 = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len2);
  3878. if (!clipdata || !clipdata2) {
  3879. if (clipdata)
  3880. GlobalFree(clipdata);
  3881. if (clipdata2)
  3882. GlobalFree(clipdata2);
  3883. return;
  3884. }
  3885. if (!(lock = GlobalLock(clipdata)))
  3886. return;
  3887. if (!(lock2 = GlobalLock(clipdata2)))
  3888. return;
  3889. memcpy(lock, data, len * sizeof(wchar_t));
  3890. WideCharToMultiByte(CP_ACP, 0, data, len, lock2, len2, NULL, NULL);
  3891. if (cfg.rtf_paste) {
  3892. wchar_t unitab[256];
  3893. char *rtf = NULL;
  3894. unsigned char *tdata = (unsigned char *)lock2;
  3895. wchar_t *udata = (wchar_t *)lock;
  3896. int rtflen = 0, uindex = 0, tindex = 0;
  3897. int rtfsize = 0;
  3898. int multilen, blen, alen, totallen, i;
  3899. char before[16], after[4];
  3900. get_unitab(CP_ACP, unitab, 0);
  3901. rtfsize = 100 + strlen(cfg.font.name);
  3902. rtf = snewn(rtfsize, char);
  3903. sprintf(rtf, "{\\rtf1\\ansi%d{\\fonttbl\\f0\\fmodern %s;}\\f0",
  3904. GetACP(), cfg.font.name);
  3905. rtflen = strlen(rtf);
  3906. /*
  3907. * We want to construct a piece of RTF that specifies the
  3908. * same Unicode text. To do this we will read back in
  3909. * parallel from the Unicode data in `udata' and the
  3910. * non-Unicode data in `tdata'. For each character in
  3911. * `tdata' which becomes the right thing in `udata' when
  3912. * looked up in `unitab', we just copy straight over from
  3913. * tdata. For each one that doesn't, we must WCToMB it
  3914. * individually and produce a \u escape sequence.
  3915. *
  3916. * It would probably be more robust to just bite the bullet
  3917. * and WCToMB each individual Unicode character one by one,
  3918. * then MBToWC each one back to see if it was an accurate
  3919. * translation; but that strikes me as a horrifying number
  3920. * of Windows API calls so I want to see if this faster way
  3921. * will work. If it screws up badly we can always revert to
  3922. * the simple and slow way.
  3923. */
  3924. while (tindex < len2 && uindex < len &&
  3925. tdata[tindex] && udata[uindex]) {
  3926. if (tindex + 1 < len2 &&
  3927. tdata[tindex] == '\r' &&
  3928. tdata[tindex+1] == '\n') {
  3929. tindex++;
  3930. uindex++;
  3931. }
  3932. if (unitab[tdata[tindex]] == udata[uindex]) {
  3933. multilen = 1;
  3934. before[0] = '\0';
  3935. after[0] = '\0';
  3936. blen = alen = 0;
  3937. } else {
  3938. multilen = WideCharToMultiByte(CP_ACP, 0, unitab+uindex, 1,
  3939. NULL, 0, NULL, NULL);
  3940. if (multilen != 1) {
  3941. blen = sprintf(before, "{\\uc%d\\u%d", multilen,
  3942. udata[uindex]);
  3943. alen = 1; strcpy(after, "}");
  3944. } else {
  3945. blen = sprintf(before, "\\u%d", udata[uindex]);
  3946. alen = 0; after[0] = '\0';
  3947. }
  3948. }
  3949. assert(tindex + multilen <= len2);
  3950. totallen = blen + alen;
  3951. for (i = 0; i < multilen; i++) {
  3952. if (tdata[tindex+i] == '\\' ||
  3953. tdata[tindex+i] == '{' ||
  3954. tdata[tindex+i] == '}')
  3955. totallen += 2;
  3956. else if (tdata[tindex+i] == 0x0D || tdata[tindex+i] == 0x0A)
  3957. totallen += 6; /* \par\r\n */
  3958. else if (tdata[tindex+i] > 0x7E || tdata[tindex+i] < 0x20)
  3959. totallen += 4;
  3960. else
  3961. totallen++;
  3962. }
  3963. if (rtfsize < rtflen + totallen + 3) {
  3964. rtfsize = rtflen + totallen + 512;
  3965. rtf = sresize(rtf, rtfsize, char);
  3966. }
  3967. strcpy(rtf + rtflen, before); rtflen += blen;
  3968. for (i = 0; i < multilen; i++) {
  3969. if (tdata[tindex+i] == '\\' ||
  3970. tdata[tindex+i] == '{' ||
  3971. tdata[tindex+i] == '}') {
  3972. rtf[rtflen++] = '\\';
  3973. rtf[rtflen++] = tdata[tindex+i];
  3974. } else if (tdata[tindex+i] == 0x0D || tdata[tindex+i] == 0x0A) {
  3975. rtflen += sprintf(rtf+rtflen, "\\par\r\n");
  3976. } else if (tdata[tindex+i] > 0x7E || tdata[tindex+i] < 0x20) {
  3977. rtflen += sprintf(rtf+rtflen, "\\'%02x", tdata[tindex+i]);
  3978. } else {
  3979. rtf[rtflen++] = tdata[tindex+i];
  3980. }
  3981. }
  3982. strcpy(rtf + rtflen, after); rtflen += alen;
  3983. tindex += multilen;
  3984. uindex++;
  3985. }
  3986. strcpy(rtf + rtflen, "}");
  3987. rtflen += 2;
  3988. clipdata3 = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, rtflen);
  3989. if (clipdata3 && (lock3 = GlobalLock(clipdata3)) != NULL) {
  3990. strcpy(lock3, rtf);
  3991. GlobalUnlock(clipdata3);
  3992. }
  3993. sfree(rtf);
  3994. } else
  3995. clipdata3 = NULL;
  3996. GlobalUnlock(clipdata);
  3997. GlobalUnlock(clipdata2);
  3998. if (!must_deselect)
  3999. SendMessage(hwnd, WM_IGNORE_CLIP, TRUE, 0);
  4000. if (OpenClipboard(hwnd)) {
  4001. EmptyClipboard();
  4002. SetClipboardData(CF_UNICODETEXT, clipdata);
  4003. SetClipboardData(CF_TEXT, clipdata2);
  4004. if (clipdata3)
  4005. SetClipboardData(RegisterClipboardFormat(CF_RTF), clipdata3);
  4006. CloseClipboard();
  4007. } else {
  4008. GlobalFree(clipdata);
  4009. GlobalFree(clipdata2);
  4010. }
  4011. if (!must_deselect)
  4012. SendMessage(hwnd, WM_IGNORE_CLIP, FALSE, 0);
  4013. }
  4014. void get_clip(void *frontend, wchar_t ** p, int *len)
  4015. {
  4016. static HGLOBAL clipdata = NULL;
  4017. static wchar_t *converted = 0;
  4018. wchar_t *p2;
  4019. if (converted) {
  4020. sfree(converted);
  4021. converted = 0;
  4022. }
  4023. if (!p) {
  4024. if (clipdata)
  4025. GlobalUnlock(clipdata);
  4026. clipdata = NULL;
  4027. return;
  4028. } else if (OpenClipboard(NULL)) {
  4029. if ((clipdata = GetClipboardData(CF_UNICODETEXT))) {
  4030. CloseClipboard();
  4031. *p = GlobalLock(clipdata);
  4032. if (*p) {
  4033. for (p2 = *p; *p2; p2++);
  4034. *len = p2 - *p;
  4035. return;
  4036. }
  4037. } else if ( (clipdata = GetClipboardData(CF_TEXT)) ) {
  4038. char *s;
  4039. int i;
  4040. CloseClipboard();
  4041. s = GlobalLock(clipdata);
  4042. i = MultiByteToWideChar(CP_ACP, 0, s, strlen(s) + 1, 0, 0);
  4043. *p = converted = snewn(i, wchar_t);
  4044. MultiByteToWideChar(CP_ACP, 0, s, strlen(s) + 1, converted, i);
  4045. *len = i - 1;
  4046. return;
  4047. } else
  4048. CloseClipboard();
  4049. }
  4050. *p = NULL;
  4051. *len = 0;
  4052. }
  4053. #if 0
  4054. /*
  4055. * Move `lines' lines from position `from' to position `to' in the
  4056. * window.
  4057. */
  4058. void optimised_move(void *frontend, int to, int from, int lines)
  4059. {
  4060. RECT r;
  4061. int min, max;
  4062. min = (to < from ? to : from);
  4063. max = to + from - min;
  4064. r.left = offset_width;
  4065. r.right = offset_width + term->cols * font_width;
  4066. r.top = offset_height + min * font_height;
  4067. r.bottom = offset_height + (max + lines) * font_height;
  4068. ScrollWindow(hwnd, 0, (to - from) * font_height, &r, &r);
  4069. }
  4070. #endif
  4071. /*
  4072. * Print a message box and perform a fatal exit.
  4073. */
  4074. void fatalbox(char *fmt, ...)
  4075. {
  4076. va_list ap;
  4077. char *stuff, morestuff[100];
  4078. va_start(ap, fmt);
  4079. stuff = dupvprintf(fmt, ap);
  4080. va_end(ap);
  4081. sprintf(morestuff, "%.70s Fatal Error", appname);
  4082. MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
  4083. sfree(stuff);
  4084. cleanup_exit(1);
  4085. }
  4086. /*
  4087. * Print a modal (Really Bad) message box and perform a fatal exit.
  4088. */
  4089. void modalfatalbox(char *fmt, ...)
  4090. {
  4091. va_list ap;
  4092. char *stuff, morestuff[100];
  4093. va_start(ap, fmt);
  4094. stuff = dupvprintf(fmt, ap);
  4095. va_end(ap);
  4096. sprintf(morestuff, "%.70s Fatal Error", appname);
  4097. MessageBox(hwnd, stuff, morestuff,
  4098. MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
  4099. sfree(stuff);
  4100. cleanup_exit(1);
  4101. }
  4102. /*
  4103. * Manage window caption / taskbar flashing, if enabled.
  4104. * 0 = stop, 1 = maintain, 2 = start
  4105. */
  4106. static void flash_window(int mode)
  4107. {
  4108. static long last_flash = 0;
  4109. static int flashing = 0;
  4110. if ((mode == 0) || (cfg.beep_ind == B_IND_DISABLED)) {
  4111. /* stop */
  4112. if (flashing) {
  4113. FlashWindow(hwnd, FALSE);
  4114. flashing = 0;
  4115. }
  4116. } else if (mode == 2) {
  4117. /* start */
  4118. if (!flashing) {
  4119. last_flash = GetTickCount();
  4120. flashing = 1;
  4121. FlashWindow(hwnd, TRUE);
  4122. }
  4123. } else if ((mode == 1) && (cfg.beep_ind == B_IND_FLASH)) {
  4124. /* maintain */
  4125. if (flashing) {
  4126. long now = GetTickCount();
  4127. long fdiff = now - last_flash;
  4128. if (fdiff < 0 || fdiff > 450) {
  4129. last_flash = now;
  4130. FlashWindow(hwnd, TRUE); /* toggle */
  4131. }
  4132. }
  4133. }
  4134. }
  4135. /*
  4136. * Beep.
  4137. */
  4138. void beep(void *frontend, int mode)
  4139. {
  4140. if (mode == BELL_DEFAULT) {
  4141. /*
  4142. * For MessageBeep style bells, we want to be careful of
  4143. * timing, because they don't have the nice property of
  4144. * PlaySound bells that each one cancels the previous
  4145. * active one. So we limit the rate to one per 50ms or so.
  4146. */
  4147. static long lastbeep = 0;
  4148. long beepdiff;
  4149. beepdiff = GetTickCount() - lastbeep;
  4150. if (beepdiff >= 0 && beepdiff < 50)
  4151. return;
  4152. MessageBeep(MB_OK);
  4153. /*
  4154. * The above MessageBeep call takes time, so we record the
  4155. * time _after_ it finishes rather than before it starts.
  4156. */
  4157. lastbeep = GetTickCount();
  4158. } else if (mode == BELL_WAVEFILE) {
  4159. if (!PlaySound(cfg.bell_wavefile.path, NULL,
  4160. SND_ASYNC | SND_FILENAME)) {
  4161. char buf[sizeof(cfg.bell_wavefile.path) + 80];
  4162. char otherbuf[100];
  4163. sprintf(buf, "Unable to play sound file\n%s\n"
  4164. "Using default sound instead", cfg.bell_wavefile.path);
  4165. sprintf(otherbuf, "%.70s Sound Error", appname);
  4166. MessageBox(hwnd, buf, otherbuf,
  4167. MB_OK | MB_ICONEXCLAMATION);
  4168. cfg.beep = BELL_DEFAULT;
  4169. }
  4170. } else if (mode == BELL_PCSPEAKER) {
  4171. static long lastbeep = 0;
  4172. long beepdiff;
  4173. beepdiff = GetTickCount() - lastbeep;
  4174. if (beepdiff >= 0 && beepdiff < 50)
  4175. return;
  4176. /*
  4177. * We must beep in different ways depending on whether this
  4178. * is a 95-series or NT-series OS.
  4179. */
  4180. if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT)
  4181. Beep(800, 100);
  4182. else
  4183. MessageBeep(-1);
  4184. lastbeep = GetTickCount();
  4185. }
  4186. /* Otherwise, either visual bell or disabled; do nothing here */
  4187. if (!term->has_focus) {
  4188. flash_window(2); /* start */
  4189. }
  4190. }
  4191. /*
  4192. * Minimise or restore the window in response to a server-side
  4193. * request.
  4194. */
  4195. void set_iconic(void *frontend, int iconic)
  4196. {
  4197. if (IsIconic(hwnd)) {
  4198. if (!iconic)
  4199. ShowWindow(hwnd, SW_RESTORE);
  4200. } else {
  4201. if (iconic)
  4202. ShowWindow(hwnd, SW_MINIMIZE);
  4203. }
  4204. }
  4205. /*
  4206. * Move the window in response to a server-side request.
  4207. */
  4208. void move_window(void *frontend, int x, int y)
  4209. {
  4210. if (cfg.resize_action == RESIZE_DISABLED ||
  4211. cfg.resize_action == RESIZE_FONT ||
  4212. IsZoomed(hwnd))
  4213. return;
  4214. SetWindowPos(hwnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
  4215. }
  4216. /*
  4217. * Move the window to the top or bottom of the z-order in response
  4218. * to a server-side request.
  4219. */
  4220. void set_zorder(void *frontend, int top)
  4221. {
  4222. if (cfg.alwaysontop)
  4223. return; /* ignore */
  4224. SetWindowPos(hwnd, top ? HWND_TOP : HWND_BOTTOM, 0, 0, 0, 0,
  4225. SWP_NOMOVE | SWP_NOSIZE);
  4226. }
  4227. /*
  4228. * Refresh the window in response to a server-side request.
  4229. */
  4230. void refresh_window(void *frontend)
  4231. {
  4232. InvalidateRect(hwnd, NULL, TRUE);
  4233. }
  4234. /*
  4235. * Maximise or restore the window in response to a server-side
  4236. * request.
  4237. */
  4238. void set_zoomed(void *frontend, int zoomed)
  4239. {
  4240. if (IsZoomed(hwnd)) {
  4241. if (!zoomed)
  4242. ShowWindow(hwnd, SW_RESTORE);
  4243. } else {
  4244. if (zoomed)
  4245. ShowWindow(hwnd, SW_MAXIMIZE);
  4246. }
  4247. }
  4248. /*
  4249. * Report whether the window is iconic, for terminal reports.
  4250. */
  4251. int is_iconic(void *frontend)
  4252. {
  4253. return IsIconic(hwnd);
  4254. }
  4255. /*
  4256. * Report the window's position, for terminal reports.
  4257. */
  4258. void get_window_pos(void *frontend, int *x, int *y)
  4259. {
  4260. RECT r;
  4261. GetWindowRect(hwnd, &r);
  4262. *x = r.left;
  4263. *y = r.top;
  4264. }
  4265. /*
  4266. * Report the window's pixel size, for terminal reports.
  4267. */
  4268. void get_window_pixels(void *frontend, int *x, int *y)
  4269. {
  4270. RECT r;
  4271. GetWindowRect(hwnd, &r);
  4272. *x = r.right - r.left;
  4273. *y = r.bottom - r.top;
  4274. }
  4275. /*
  4276. * Return the window or icon title.
  4277. */
  4278. char *get_window_title(void *frontend, int icon)
  4279. {
  4280. return icon ? icon_name : window_name;
  4281. }
  4282. /*
  4283. * See if we're in full-screen mode.
  4284. */
  4285. static int is_full_screen()
  4286. {
  4287. if (!IsZoomed(hwnd))
  4288. return FALSE;
  4289. if (GetWindowLong(hwnd, GWL_STYLE) & WS_CAPTION)
  4290. return FALSE;
  4291. return TRUE;
  4292. }
  4293. /* Get the rect/size of a full screen window using the nearest available
  4294. * monitor in multimon systems; default to something sensible if only
  4295. * one monitor is present. */
  4296. static int get_fullscreen_rect(RECT * ss)
  4297. {
  4298. #if defined(MONITOR_DEFAULTTONEAREST) && !defined(NO_MULTIMON)
  4299. HMONITOR mon;
  4300. MONITORINFO mi;
  4301. mon = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
  4302. mi.cbSize = sizeof(mi);
  4303. GetMonitorInfo(mon, &mi);
  4304. /* structure copy */
  4305. *ss = mi.rcMonitor;
  4306. return TRUE;
  4307. #else
  4308. /* could also use code like this:
  4309. ss->left = ss->top = 0;
  4310. ss->right = GetSystemMetrics(SM_CXSCREEN);
  4311. ss->bottom = GetSystemMetrics(SM_CYSCREEN);
  4312. */
  4313. return GetClientRect(GetDesktopWindow(), ss);
  4314. #endif
  4315. }
  4316. /*
  4317. * Go full-screen. This should only be called when we are already
  4318. * maximised.
  4319. */
  4320. static void make_full_screen()
  4321. {
  4322. DWORD style;
  4323. RECT ss;
  4324. assert(IsZoomed(hwnd));
  4325. if (is_full_screen())
  4326. return;
  4327. /* Remove the window furniture. */
  4328. style = GetWindowLong(hwnd, GWL_STYLE);
  4329. style &= ~(WS_CAPTION | WS_BORDER | WS_THICKFRAME);
  4330. if (cfg.scrollbar_in_fullscreen)
  4331. style |= WS_VSCROLL;
  4332. else
  4333. style &= ~WS_VSCROLL;
  4334. SetWindowLong(hwnd, GWL_STYLE, style);
  4335. /* Resize ourselves to exactly cover the nearest monitor. */
  4336. get_fullscreen_rect(&ss);
  4337. SetWindowPos(hwnd, HWND_TOP, ss.left, ss.top,
  4338. ss.right - ss.left,
  4339. ss.bottom - ss.top,
  4340. SWP_FRAMECHANGED);
  4341. /* Tick the menu item in the System menu. */
  4342. CheckMenuItem(GetSystemMenu(hwnd, FALSE), IDM_FULLSCREEN,
  4343. MF_CHECKED);
  4344. }
  4345. /*
  4346. * Clear the full-screen attributes.
  4347. */
  4348. static void clear_full_screen()
  4349. {
  4350. DWORD oldstyle, style;
  4351. /* Reinstate the window furniture. */
  4352. style = oldstyle = GetWindowLong(hwnd, GWL_STYLE);
  4353. style |= WS_CAPTION | WS_BORDER;
  4354. if (cfg.resize_action == RESIZE_DISABLED)
  4355. style &= ~WS_THICKFRAME;
  4356. else
  4357. style |= WS_THICKFRAME;
  4358. if (cfg.scrollbar)
  4359. style |= WS_VSCROLL;
  4360. else
  4361. style &= ~WS_VSCROLL;
  4362. if (style != oldstyle) {
  4363. SetWindowLong(hwnd, GWL_STYLE, style);
  4364. SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
  4365. SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
  4366. SWP_FRAMECHANGED);
  4367. }
  4368. /* Untick the menu item in the System menu. */
  4369. CheckMenuItem(GetSystemMenu(hwnd, FALSE), IDM_FULLSCREEN,
  4370. MF_UNCHECKED);
  4371. }
  4372. /*
  4373. * Toggle full-screen mode.
  4374. */
  4375. static void flip_full_screen()
  4376. {
  4377. if (is_full_screen()) {
  4378. ShowWindow(hwnd, SW_RESTORE);
  4379. } else if (IsZoomed(hwnd)) {
  4380. make_full_screen();
  4381. } else {
  4382. SendMessage(hwnd, WM_FULLSCR_ON_MAX, 0, 0);
  4383. ShowWindow(hwnd, SW_MAXIMIZE);
  4384. }
  4385. }
  4386. void frontend_keypress(void *handle)
  4387. {
  4388. /*
  4389. * Keypress termination in non-Close-On-Exit mode is not
  4390. * currently supported in PuTTY proper, because the window
  4391. * always has a perfectly good Close button anyway. So we do
  4392. * nothing here.
  4393. */
  4394. return;
  4395. }
  4396. int from_backend(void *frontend, int is_stderr, const char *data, int len)
  4397. {
  4398. return term_data(term, is_stderr, data, len);
  4399. }
  4400. void agent_schedule_callback(void (*callback)(void *, void *, int),
  4401. void *callback_ctx, void *data, int len)
  4402. {
  4403. struct agent_callback *c = snew(struct agent_callback);
  4404. c->callback = callback;
  4405. c->callback_ctx = callback_ctx;
  4406. c->data = data;
  4407. c->len = len;
  4408. PostMessage(hwnd, WM_AGENT_CALLBACK, 0, (LPARAM)c);
  4409. }