window.c 191 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709
  1. /*
  2. * window.c: the main code that runs a PuTTY terminal emulator and
  3. * backend in a GTK window.
  4. */
  5. #define _GNU_SOURCE
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <signal.h>
  11. #include <stdio.h>
  12. #include <time.h>
  13. #include <errno.h>
  14. #include <locale.h>
  15. #include <fcntl.h>
  16. #include <unistd.h>
  17. #include <sys/types.h>
  18. #include <sys/wait.h>
  19. #include <gtk/gtk.h>
  20. #if !GTK_CHECK_VERSION(3,0,0)
  21. #include <gdk/gdkkeysyms.h>
  22. #endif
  23. #if GTK_CHECK_VERSION(2,0,0)
  24. #include <gtk/gtkimmodule.h>
  25. #endif
  26. #define MAY_REFER_TO_GTK_IN_HEADERS
  27. #include "putty.h"
  28. #include "terminal.h"
  29. #include "gtkcompat.h"
  30. #include "unifont.h"
  31. #include "gtkmisc.h"
  32. #ifndef NOT_X_WINDOWS
  33. #include <gdk/gdkx.h>
  34. #include <X11/Xlib.h>
  35. #include <X11/Xutil.h>
  36. #include <X11/Xatom.h>
  37. #endif
  38. #include "x11misc.h"
  39. GdkAtom compound_text_atom, utf8_string_atom;
  40. static GdkAtom clipboard_atom
  41. #if GTK_CHECK_VERSION(2,0,0) /* GTK1 will have to fill this in at startup */
  42. = GDK_SELECTION_CLIPBOARD
  43. #endif
  44. ;
  45. #ifdef JUST_USE_GTK_CLIPBOARD_UTF8
  46. /*
  47. * Because calling gtk_clipboard_set_with_data triggers a call to the
  48. * clipboard_clear function from the last time, we need to arrange a
  49. * way to distinguish a real call to clipboard_clear for the _new_
  50. * instance of the clipboard data from the leftover call for the
  51. * outgoing one. We do this by setting the user data field in our
  52. * gtk_clipboard_set_with_data() call, instead of the obvious pointer
  53. * to 'inst', to one of these.
  54. */
  55. struct clipboard_data_instance {
  56. char *pasteout_data_utf8;
  57. int pasteout_data_utf8_len;
  58. struct clipboard_state *state;
  59. struct clipboard_data_instance *next, *prev;
  60. };
  61. #endif
  62. struct clipboard_state {
  63. GtkFrontend *inst;
  64. int clipboard;
  65. GdkAtom atom;
  66. #ifdef JUST_USE_GTK_CLIPBOARD_UTF8
  67. GtkClipboard *gtkclipboard;
  68. struct clipboard_data_instance *current_cdi;
  69. #else
  70. char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
  71. int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
  72. #endif
  73. };
  74. typedef struct XpmHolder XpmHolder; /* only used for GTK 1 */
  75. struct GtkFrontend {
  76. GtkWidget *window, *area, *sbar;
  77. gboolean sbar_visible;
  78. gboolean drawing_area_got_size, drawing_area_realised;
  79. gboolean drawing_area_setup_needed;
  80. bool drawing_area_setup_called;
  81. GtkBox *hbox;
  82. GtkAdjustment *sbar_adjust;
  83. GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
  84. *restartitem;
  85. GtkWidget *sessionsmenu;
  86. #ifndef NOT_X_WINDOWS
  87. Display *disp;
  88. #endif
  89. #ifndef NO_BACKING_PIXMAPS
  90. /*
  91. * Server-side pixmap which we use to cache the terminal window's
  92. * contents. When we draw text in the terminal, we draw it to this
  93. * pixmap first, and then blit from there to the actual window;
  94. * this way, X expose events can be handled with an absolute
  95. * minimum of network traffic, by just sending a command to
  96. * re-blit an appropriate rectangle from this pixmap.
  97. */
  98. GdkPixmap *pixmap;
  99. #else
  100. /*
  101. * If we're avoiding GdkPixmaps, we cache the same image in a
  102. * Cairo surface. Under X11, that surface will probably be a
  103. * server-side pixmap.
  104. */
  105. cairo_surface_t *surface;
  106. #endif
  107. int backing_w, backing_h;
  108. #if GTK_CHECK_VERSION(2,0,0)
  109. GtkIMContext *imc;
  110. #endif
  111. unifont *fonts[4]; /* normal, bold, wide, widebold */
  112. int xpos, ypos, gravity;
  113. bool gotpos;
  114. GdkCursor *rawcursor, *textcursor, *blankcursor, *waitcursor, *currcursor;
  115. GdkColor cols[OSC4_NCOLOURS]; /* indexed by xterm colour indices */
  116. #if !GTK_CHECK_VERSION(3,0,0)
  117. GdkColormap *colmap;
  118. #endif
  119. bool direct_to_font;
  120. struct clipboard_state clipstates[N_CLIPBOARDS];
  121. #ifdef JUST_USE_GTK_CLIPBOARD_UTF8
  122. /* Remember all clipboard_data_instance structures currently
  123. * associated with this GtkFrontend, in case they're still around
  124. * when it gets destroyed */
  125. struct clipboard_data_instance cdi_headtail;
  126. #endif
  127. int clipboard_ctrlshiftins, clipboard_ctrlshiftcv;
  128. int font_width, font_height;
  129. int width, height, scale;
  130. bool ignore_sbar;
  131. bool mouseptr_visible;
  132. BusyStatus busy_status;
  133. int alt_keycode;
  134. int alt_digits;
  135. char *wintitle;
  136. char *icontitle;
  137. int master_fd, master_func_id;
  138. Ldisc *ldisc;
  139. Backend *backend;
  140. Terminal *term;
  141. cmdline_get_passwd_input_state cmdline_get_passwd_state;
  142. LogContext *logctx;
  143. bool exited;
  144. struct unicode_data ucsdata;
  145. Conf *conf;
  146. eventlog_stuff *eventlogstuff;
  147. guint32 input_event_time; /* Timestamp of the most recent input event. */
  148. GtkWidget *dialogs[DIALOG_SLOT_LIMIT];
  149. #if GTK_CHECK_VERSION(3,4,0)
  150. gdouble cumulative_hscroll, cumulative_vscroll;
  151. #endif
  152. /* Cached things out of conf that we refer to a lot */
  153. int bold_style;
  154. int window_border;
  155. int cursor_type;
  156. int drawtype;
  157. int meta_mod_mask;
  158. #ifdef OSX_META_KEY_CONFIG
  159. int system_mod_mask;
  160. #endif
  161. bool send_raw_mouse;
  162. bool pointer_indicates_raw_mouse;
  163. unifont_drawctx uctx;
  164. #if GTK_CHECK_VERSION(2,0,0)
  165. GdkPixbuf *trust_sigil_pb;
  166. #else
  167. GdkPixmap *trust_sigil_pm;
  168. #endif
  169. int trust_sigil_w, trust_sigil_h;
  170. /*
  171. * Not every GDK backend can be relied on 100% to reply to a
  172. * resize request in a timely manner. (In X11 it's all
  173. * asynchronous and goes via the window manager, and if your
  174. * window manager is seriously unwell, you'd rather not have
  175. * terminal windows start becoming unusable as a knock-on effect,
  176. * since those are just the thing you might need to use for
  177. * emergency WM maintenance!)
  178. *
  179. * So when we ask GTK to resize our terminal window, we also set a
  180. * 5-second timer, after which we'll regretfully conclude that a
  181. * resize (or ConfigureNotify telling us no resize took place) is
  182. * probably not going to happen after all.
  183. */
  184. bool win_resize_pending, term_resize_notification_required;
  185. long win_resize_timeout;
  186. #define WIN_RESIZE_TIMEOUT (TICKSPERSEC*5)
  187. Seat seat;
  188. TermWin termwin;
  189. LogPolicy logpolicy;
  190. };
  191. static void cache_conf_values(GtkFrontend *inst)
  192. {
  193. inst->bold_style = conf_get_int(inst->conf, CONF_bold_style);
  194. inst->window_border = conf_get_int(inst->conf, CONF_window_border);
  195. inst->cursor_type = conf_get_int(inst->conf, CONF_cursor_type);
  196. #ifdef OSX_META_KEY_CONFIG
  197. inst->meta_mod_mask = 0;
  198. if (conf_get_bool(inst->conf, CONF_osx_option_meta))
  199. inst->meta_mod_mask |= GDK_MOD1_MASK;
  200. if (conf_get_bool(inst->conf, CONF_osx_command_meta))
  201. inst->meta_mod_mask |= GDK_MOD2_MASK;
  202. inst->system_mod_mask = GDK_MOD2_MASK & ~inst->meta_mod_mask;
  203. #else
  204. inst->meta_mod_mask = GDK_MOD1_MASK;
  205. #endif
  206. }
  207. static void start_backend(GtkFrontend *inst);
  208. static void exit_callback(void *vinst);
  209. static void destroy_inst_connection(GtkFrontend *inst);
  210. static void delete_inst(GtkFrontend *inst);
  211. static void post_fatal_message_box_toplevel(void *vctx)
  212. {
  213. GtkFrontend *inst = (GtkFrontend *)vctx;
  214. gtk_widget_destroy(inst->window);
  215. }
  216. static void post_fatal_message_box(void *vctx, int result)
  217. {
  218. GtkFrontend *inst = (GtkFrontend *)vctx;
  219. unregister_dialog(&inst->seat, DIALOG_SLOT_CONNECTION_FATAL);
  220. queue_toplevel_callback(post_fatal_message_box_toplevel, inst);
  221. }
  222. static void common_connfatal_message_box(
  223. GtkFrontend *inst, const char *msg, post_dialog_fn_t postfn)
  224. {
  225. char *title = dupcat(appname, " Fatal Error");
  226. GtkWidget *dialog = create_message_box(
  227. inst->window, title, msg,
  228. string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
  229. false, &buttons_ok, postfn, inst);
  230. register_dialog(&inst->seat, DIALOG_SLOT_CONNECTION_FATAL, dialog);
  231. sfree(title);
  232. }
  233. void fatal_message_box(GtkFrontend *inst, const char *msg)
  234. {
  235. common_connfatal_message_box(inst, msg, post_fatal_message_box);
  236. }
  237. static void connection_fatal_callback(void *vctx)
  238. {
  239. GtkFrontend *inst = (GtkFrontend *)vctx;
  240. destroy_inst_connection(inst);
  241. }
  242. static void post_nonfatal_message_box(void *vctx, int result)
  243. {
  244. GtkFrontend *inst = (GtkFrontend *)vctx;
  245. unregister_dialog(&inst->seat, DIALOG_SLOT_CONNECTION_FATAL);
  246. }
  247. static void gtk_seat_connection_fatal(Seat *seat, const char *msg)
  248. {
  249. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  250. if (conf_get_int(inst->conf, CONF_close_on_exit) == FORCE_ON) {
  251. fatal_message_box(inst, msg);
  252. } else {
  253. common_connfatal_message_box(inst, msg, post_nonfatal_message_box);
  254. }
  255. inst->exited = true; /* suppress normal exit handling */
  256. queue_toplevel_callback(connection_fatal_callback, inst);
  257. }
  258. static void gtk_seat_nonfatal(Seat *seat, const char *msg)
  259. {
  260. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  261. nonfatal_message_box(inst->window, msg);
  262. }
  263. /*
  264. * Default settings that are specific to pterm.
  265. */
  266. FontSpec *platform_default_fontspec(const char *name)
  267. {
  268. if (!strcmp(name, "Font"))
  269. return fontspec_new(DEFAULT_GTK_FONT);
  270. else
  271. return fontspec_new_default();
  272. }
  273. Filename *platform_default_filename(const char *name)
  274. {
  275. if (!strcmp(name, "LogFileName"))
  276. return filename_from_str("putty.log");
  277. else
  278. return filename_from_str("");
  279. }
  280. char *platform_default_s(const char *name)
  281. {
  282. if (!strcmp(name, "SerialLine"))
  283. return dupstr("/dev/ttyS0");
  284. return NULL;
  285. }
  286. bool platform_default_b(const char *name, bool def)
  287. {
  288. if (!strcmp(name, "WinNameAlways")) {
  289. /* X natively supports icon titles, so use 'em by default */
  290. return false;
  291. }
  292. return def;
  293. }
  294. int platform_default_i(const char *name, int def)
  295. {
  296. if (!strcmp(name, "CloseOnExit"))
  297. return 2; /* maps to FORCE_ON after painful rearrangement :-( */
  298. return def;
  299. }
  300. static char *gtk_seat_get_ttymode(Seat *seat, const char *mode)
  301. {
  302. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  303. return term_get_ttymode(inst->term, mode);
  304. }
  305. static size_t gtk_seat_output(Seat *seat, SeatOutputType type,
  306. const void *data, size_t len)
  307. {
  308. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  309. return term_data(inst->term, data, len);
  310. }
  311. static void gtkwin_unthrottle(TermWin *win, size_t bufsize)
  312. {
  313. GtkFrontend *inst = container_of(win, GtkFrontend, termwin);
  314. if (inst->backend)
  315. backend_unthrottle(inst->backend, bufsize);
  316. }
  317. static bool gtk_seat_eof(Seat *seat)
  318. {
  319. /* GtkFrontend *inst = container_of(seat, GtkFrontend, seat); */
  320. return true; /* do respond to incoming EOF with outgoing */
  321. }
  322. static SeatPromptResult gtk_seat_get_userpass_input(Seat *seat, prompts_t *p)
  323. {
  324. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  325. SeatPromptResult spr;
  326. spr = cmdline_get_passwd_input(p, &inst->cmdline_get_passwd_state, true);
  327. if (spr.kind == SPRK_INCOMPLETE)
  328. spr = term_get_userpass_input(inst->term, p);
  329. return spr;
  330. }
  331. static bool gtk_seat_is_utf8(Seat *seat)
  332. {
  333. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  334. return inst->ucsdata.line_codepage == CS_UTF8;
  335. }
  336. static void get_window_pixel_size(GtkFrontend *inst, int *w, int *h)
  337. {
  338. /*
  339. * I assume that when the GTK version of this call is available
  340. * we should use it. Not sure how it differs from the GDK one,
  341. * though.
  342. */
  343. #if GTK_CHECK_VERSION(2,0,0)
  344. gtk_window_get_size(GTK_WINDOW(inst->window), w, h);
  345. #else
  346. gdk_window_get_size(gtk_widget_get_window(inst->window), w, h);
  347. #endif
  348. }
  349. static bool gtk_seat_get_window_pixel_size(Seat *seat, int *w, int *h)
  350. {
  351. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  352. get_window_pixel_size(inst, w, h);
  353. return true;
  354. }
  355. StripCtrlChars *gtk_seat_stripctrl_new(
  356. Seat *seat, BinarySink *bs_out, SeatInteractionContext sic)
  357. {
  358. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  359. return stripctrl_new_term(bs_out, false, 0, inst->term);
  360. }
  361. static void gtk_seat_notify_remote_exit(Seat *seat);
  362. static void gtk_seat_update_specials_menu(Seat *seat);
  363. static void gtk_seat_set_busy_status(Seat *seat, BusyStatus status);
  364. #ifndef NOT_X_WINDOWS
  365. static const char *gtk_seat_get_display(Seat *seat, SeatDisplayType dtype);
  366. static bool gtk_seat_get_windowid(Seat *seat, long *id);
  367. #endif
  368. static void gtk_seat_set_trust_status(Seat *seat, bool trusted);
  369. static bool gtk_seat_can_set_trust_status(Seat *seat);
  370. static bool gtk_seat_get_cursor_position(Seat *seat, int *x, int *y);
  371. static const SeatVtable gtk_seat_vt = {
  372. .output = gtk_seat_output,
  373. .eof = gtk_seat_eof,
  374. .sent = nullseat_sent,
  375. .banner = nullseat_banner_to_stderr,
  376. .get_userpass_input = gtk_seat_get_userpass_input,
  377. .notify_session_started = nullseat_notify_session_started,
  378. .notify_remote_exit = gtk_seat_notify_remote_exit,
  379. .notify_remote_disconnect = nullseat_notify_remote_disconnect,
  380. .connection_fatal = gtk_seat_connection_fatal,
  381. .nonfatal = gtk_seat_nonfatal,
  382. .update_specials_menu = gtk_seat_update_specials_menu,
  383. .get_ttymode = gtk_seat_get_ttymode,
  384. .set_busy_status = gtk_seat_set_busy_status,
  385. .confirm_ssh_host_key = gtk_seat_confirm_ssh_host_key,
  386. .confirm_weak_crypto_primitive = gtk_seat_confirm_weak_crypto_primitive,
  387. .confirm_weak_cached_hostkey = gtk_seat_confirm_weak_cached_hostkey,
  388. .prompt_descriptions = gtk_seat_prompt_descriptions,
  389. .is_utf8 = gtk_seat_is_utf8,
  390. .echoedit_update = nullseat_echoedit_update,
  391. #ifdef NOT_X_WINDOWS
  392. .get_display = nullseat_get_display,
  393. .get_windowid = nullseat_get_windowid,
  394. #else
  395. .get_display = gtk_seat_get_display,
  396. .get_windowid = gtk_seat_get_windowid,
  397. #endif
  398. .get_window_pixel_size = gtk_seat_get_window_pixel_size,
  399. .stripctrl_new = gtk_seat_stripctrl_new,
  400. .set_trust_status = gtk_seat_set_trust_status,
  401. .can_set_trust_status = gtk_seat_can_set_trust_status,
  402. .has_mixed_input_stream = nullseat_has_mixed_input_stream_yes,
  403. .verbose = nullseat_verbose_yes,
  404. .interactive = nullseat_interactive_yes,
  405. .get_cursor_position = gtk_seat_get_cursor_position,
  406. };
  407. static void gtk_eventlog(LogPolicy *lp, const char *string)
  408. {
  409. GtkFrontend *inst = container_of(lp, GtkFrontend, logpolicy);
  410. logevent_dlg(inst->eventlogstuff, string);
  411. }
  412. static int gtk_askappend(LogPolicy *lp, Filename *filename,
  413. void (*callback)(void *ctx, int result), void *ctx)
  414. {
  415. GtkFrontend *inst = container_of(lp, GtkFrontend, logpolicy);
  416. return gtkdlg_askappend(&inst->seat, filename, callback, ctx);
  417. }
  418. static void gtk_logging_error(LogPolicy *lp, const char *event)
  419. {
  420. GtkFrontend *inst = container_of(lp, GtkFrontend, logpolicy);
  421. /* Send 'can't open log file' errors to the terminal window.
  422. * (Marked as stderr, although terminal.c won't care.) */
  423. seat_stderr_pl(&inst->seat, ptrlen_from_asciz(event));
  424. seat_stderr_pl(&inst->seat, PTRLEN_LITERAL("\r\n"));
  425. }
  426. static const LogPolicyVtable gtk_logpolicy_vt = {
  427. .eventlog = gtk_eventlog,
  428. .askappend = gtk_askappend,
  429. .logging_error = gtk_logging_error,
  430. .verbose = null_lp_verbose_yes,
  431. };
  432. /*
  433. * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
  434. * into a cooked one (SELECT, EXTEND, PASTE).
  435. *
  436. * In Unix, this is not configurable; the X button arrangement is
  437. * rock-solid across all applications, everyone has a three-button
  438. * mouse or a means of faking it, and there is no need to switch
  439. * buttons around at all.
  440. */
  441. static Mouse_Button translate_button(Mouse_Button button)
  442. {
  443. if (button == MBT_LEFT)
  444. return MBT_SELECT;
  445. if (button == MBT_MIDDLE)
  446. return MBT_PASTE;
  447. if (button == MBT_RIGHT)
  448. return MBT_EXTEND;
  449. if (button == MBT_NOTHING)
  450. return MBT_NOTHING;
  451. return 0; /* shouldn't happen */
  452. }
  453. /*
  454. * Return the top-level GtkWindow associated with a particular
  455. * front end instance.
  456. */
  457. GtkWidget *gtk_seat_get_window(Seat *seat)
  458. {
  459. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  460. return inst->window;
  461. }
  462. /*
  463. * Set and clear a pointer to a dialog box created as a result of the
  464. * network code wanting to ask an asynchronous user question (e.g.
  465. * 'what about this dodgy host key, then?').
  466. */
  467. void register_dialog(Seat *seat, enum DialogSlot slot, GtkWidget *dialog)
  468. {
  469. GtkFrontend *inst;
  470. assert(seat->vt == &gtk_seat_vt);
  471. inst = container_of(seat, GtkFrontend, seat);
  472. assert(slot < DIALOG_SLOT_LIMIT);
  473. assert(!inst->dialogs[slot]);
  474. inst->dialogs[slot] = dialog;
  475. }
  476. void unregister_dialog(Seat *seat, enum DialogSlot slot)
  477. {
  478. GtkFrontend *inst;
  479. assert(seat->vt == &gtk_seat_vt);
  480. inst = container_of(seat, GtkFrontend, seat);
  481. assert(slot < DIALOG_SLOT_LIMIT);
  482. assert(inst->dialogs[slot]);
  483. inst->dialogs[slot] = NULL;
  484. }
  485. /*
  486. * Minimise or restore the window in response to a server-side
  487. * request.
  488. */
  489. static void gtkwin_set_minimised(TermWin *tw, bool minimised)
  490. {
  491. /*
  492. * GTK 1.2 doesn't know how to do this.
  493. */
  494. #if GTK_CHECK_VERSION(2,0,0)
  495. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  496. if (minimised)
  497. gtk_window_iconify(GTK_WINDOW(inst->window));
  498. else
  499. gtk_window_deiconify(GTK_WINDOW(inst->window));
  500. #endif
  501. }
  502. /*
  503. * Move the window in response to a server-side request.
  504. */
  505. static void gtkwin_move(TermWin *tw, int x, int y)
  506. {
  507. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  508. /*
  509. * I assume that when the GTK version of this call is available
  510. * we should use it. Not sure how it differs from the GDK one,
  511. * though.
  512. */
  513. #if GTK_CHECK_VERSION(2,0,0)
  514. /* in case we reset this at startup due to a geometry string */
  515. gtk_window_set_gravity(GTK_WINDOW(inst->window), GDK_GRAVITY_NORTH_EAST);
  516. gtk_window_move(GTK_WINDOW(inst->window), x, y);
  517. #else
  518. gdk_window_move(gtk_widget_get_window(inst->window), x, y);
  519. #endif
  520. }
  521. /*
  522. * Move the window to the top or bottom of the z-order in response
  523. * to a server-side request.
  524. */
  525. static void gtkwin_set_zorder(TermWin *tw, bool top)
  526. {
  527. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  528. if (top)
  529. gdk_window_raise(gtk_widget_get_window(inst->window));
  530. else
  531. gdk_window_lower(gtk_widget_get_window(inst->window));
  532. }
  533. /*
  534. * Refresh the window in response to a server-side request.
  535. */
  536. static void gtkwin_refresh(TermWin *tw)
  537. {
  538. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  539. term_invalidate(inst->term);
  540. }
  541. /*
  542. * Maximise or restore the window in response to a server-side
  543. * request.
  544. */
  545. static void gtkwin_set_maximised(TermWin *tw, bool maximised)
  546. {
  547. /*
  548. * GTK 1.2 doesn't know how to do this.
  549. */
  550. #if GTK_CHECK_VERSION(2,0,0)
  551. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  552. if (maximised)
  553. gtk_window_maximize(GTK_WINDOW(inst->window));
  554. else
  555. gtk_window_unmaximize(GTK_WINDOW(inst->window));
  556. #endif
  557. }
  558. /*
  559. * Find out whether a dialog box already exists for this window in a
  560. * particular DialogSlot. If it does, uniconify it (if we can) and
  561. * raise it, so that the user realises they've already been asked this
  562. * question.
  563. */
  564. static bool find_and_raise_dialog(GtkFrontend *inst, enum DialogSlot slot)
  565. {
  566. GtkWidget *dialog = inst->dialogs[slot];
  567. if (!dialog)
  568. return false;
  569. #if GTK_CHECK_VERSION(2,0,0)
  570. gtk_window_deiconify(GTK_WINDOW(dialog));
  571. #endif
  572. gdk_window_raise(gtk_widget_get_window(dialog));
  573. return true;
  574. }
  575. static void warn_on_close_callback(void *vctx, int result)
  576. {
  577. GtkFrontend *inst = (GtkFrontend *)vctx;
  578. unregister_dialog(&inst->seat, DIALOG_SLOT_WARN_ON_CLOSE);
  579. if (result)
  580. gtk_widget_destroy(inst->window);
  581. }
  582. /*
  583. * Handle the 'delete window' event (e.g. user clicking the WM close
  584. * button). The return value false means the window should close, and
  585. * true means it shouldn't.
  586. *
  587. * (That's counterintuitive, but really, in GTK terms, true means 'I
  588. * have done everything necessary to handle this event, so the default
  589. * handler need not do anything', i.e. 'suppress default handler',
  590. * i.e. 'do not close the window'.)
  591. */
  592. gint delete_window(GtkWidget *widget, GdkEvent *event, GtkFrontend *inst)
  593. {
  594. if (!inst->exited && conf_get_bool(inst->conf, CONF_warn_on_close)) {
  595. /*
  596. * We're not going to exit right now. We must put up a
  597. * warn-on-close dialog, unless one already exists, in which
  598. * case we'll just re-emphasise that one.
  599. */
  600. if (!find_and_raise_dialog(inst, DIALOG_SLOT_WARN_ON_CLOSE)) {
  601. char *title = dupcat(appname, " Exit Confirmation");
  602. char *msg, *additional = NULL;
  603. if (inst->backend && inst->backend->vt->close_warn_text) {
  604. additional = inst->backend->vt->close_warn_text(inst->backend);
  605. }
  606. msg = dupprintf("Are you sure you want to close this session?%s%s",
  607. additional ? "\n" : "",
  608. additional ? additional : "");
  609. GtkWidget *dialog = create_message_box(
  610. inst->window, title, msg,
  611. string_width("Most of the width of the above text"),
  612. false, &buttons_yn, warn_on_close_callback, inst);
  613. register_dialog(&inst->seat, DIALOG_SLOT_WARN_ON_CLOSE, dialog);
  614. sfree(title);
  615. sfree(msg);
  616. sfree(additional);
  617. }
  618. return true;
  619. }
  620. return false;
  621. }
  622. #if GTK_CHECK_VERSION(2,0,0)
  623. static gboolean window_state_event(
  624. GtkWidget *widget, GdkEventWindowState *event, gpointer user_data)
  625. {
  626. GtkFrontend *inst = (GtkFrontend *)user_data;
  627. term_notify_minimised(
  628. inst->term, event->new_window_state & GDK_WINDOW_STATE_ICONIFIED);
  629. return false;
  630. }
  631. #endif
  632. static void update_mouseptr(GtkFrontend *inst)
  633. {
  634. switch (inst->busy_status) {
  635. case BUSY_NOT:
  636. if (!inst->mouseptr_visible) {
  637. gdk_window_set_cursor(gtk_widget_get_window(inst->area),
  638. inst->blankcursor);
  639. } else if (inst->pointer_indicates_raw_mouse) {
  640. gdk_window_set_cursor(gtk_widget_get_window(inst->area),
  641. inst->rawcursor);
  642. } else {
  643. gdk_window_set_cursor(gtk_widget_get_window(inst->area),
  644. inst->textcursor);
  645. }
  646. break;
  647. case BUSY_WAITING: /* XXX can we do better? */
  648. case BUSY_CPU:
  649. /* We always display these cursors. */
  650. gdk_window_set_cursor(gtk_widget_get_window(inst->area),
  651. inst->waitcursor);
  652. break;
  653. default:
  654. unreachable("Bad busy_status");
  655. }
  656. }
  657. static void show_mouseptr(GtkFrontend *inst, bool show)
  658. {
  659. if (!conf_get_bool(inst->conf, CONF_hide_mouseptr))
  660. show = true;
  661. inst->mouseptr_visible = show;
  662. update_mouseptr(inst);
  663. }
  664. static void draw_backing_rect(GtkFrontend *inst);
  665. static void drawing_area_setup(GtkFrontend *inst, int width, int height)
  666. {
  667. int w, h, new_scale;
  668. /*
  669. * See if the terminal size has changed.
  670. */
  671. w = (width - 2*inst->window_border) / inst->font_width;
  672. h = (height - 2*inst->window_border) / inst->font_height;
  673. if (w != inst->width || h != inst->height) {
  674. /*
  675. * Update conf.
  676. */
  677. inst->width = w;
  678. inst->height = h;
  679. conf_set_int(inst->conf, CONF_width, inst->width);
  680. conf_set_int(inst->conf, CONF_height, inst->height);
  681. /*
  682. * We must refresh the window's backing image.
  683. */
  684. inst->drawing_area_setup_needed = true;
  685. }
  686. #if GTK_CHECK_VERSION(3,10,0)
  687. new_scale = gtk_widget_get_scale_factor(inst->area);
  688. if (new_scale != inst->scale)
  689. inst->drawing_area_setup_needed = true;
  690. #else
  691. new_scale = 1;
  692. #endif
  693. int new_backing_w = width;
  694. int new_backing_h = height;
  695. if (inst->backing_w != new_backing_w || inst->backing_h != new_backing_h
  696. || inst->scale != new_scale)
  697. inst->drawing_area_setup_needed = true;
  698. /*
  699. * GTK will sometimes send us configure events when nothing about
  700. * the window size has actually changed. In some situations this
  701. * can happen quite often, so it's a worthwhile optimisation to
  702. * detect that situation and avoid the expensive reinitialisation
  703. * of the backing surface / image, and so on.
  704. *
  705. * However, we must still communicate to the terminal that we
  706. * received a resize event, because sometimes a trivial resize
  707. * event (to the same size we already were) is a signal from the
  708. * window system that a _nontrivial_ resize we recently asked for
  709. * has failed to happen.
  710. */
  711. inst->drawing_area_setup_called = true;
  712. if (inst->term)
  713. term_size(inst->term, h, w, conf_get_int(inst->conf, CONF_savelines));
  714. if (inst->win_resize_pending) {
  715. if (inst->term_resize_notification_required)
  716. term_resize_request_completed(inst->term);
  717. inst->win_resize_pending = false;
  718. }
  719. if (!inst->drawing_area_setup_needed)
  720. return;
  721. inst->drawing_area_setup_needed = false;
  722. inst->scale = new_scale;
  723. inst->backing_w = new_backing_w;
  724. inst->backing_h = new_backing_h;
  725. #ifndef NO_BACKING_PIXMAPS
  726. if (inst->pixmap) {
  727. gdk_pixmap_unref(inst->pixmap);
  728. inst->pixmap = NULL;
  729. }
  730. inst->pixmap = gdk_pixmap_new(gtk_widget_get_window(inst->area),
  731. inst->backing_w, inst->backing_h, -1);
  732. #else
  733. if (inst->surface) {
  734. cairo_surface_destroy(inst->surface);
  735. inst->surface = NULL;
  736. }
  737. #if GTK_CHECK_VERSION(2,22,0)
  738. inst->surface = gdk_window_create_similar_surface(
  739. gtk_widget_get_window(inst->area),
  740. CAIRO_CONTENT_COLOR, inst->backing_w, inst->backing_h);
  741. #else
  742. cairo_t *tmp_cr = gdk_cairo_create(gtk_widget_get_window(inst->area));
  743. inst->surface = cairo_surface_create_similar(
  744. cairo_get_target(tmp_cr),
  745. CAIRO_CONTENT_COLOR, inst->backing_w, inst->backing_h);
  746. cairo_destroy(tmp_cr);
  747. #endif
  748. #endif
  749. draw_backing_rect(inst);
  750. if (inst->term)
  751. term_invalidate(inst->term);
  752. #if GTK_CHECK_VERSION(2,0,0)
  753. gtk_im_context_set_client_window(
  754. inst->imc, gtk_widget_get_window(inst->area));
  755. #endif
  756. }
  757. static void drawing_area_setup_simple(GtkFrontend *inst)
  758. {
  759. /*
  760. * Wrapper on drawing_area_setup which fetches the width and
  761. * height of the drawing area. We go directly to the inner version
  762. * in the case where a new size allocation comes in (just in case
  763. * GTK hasn't installed it in the normal place yet).
  764. */
  765. #if GTK_CHECK_VERSION(2,0,0)
  766. GdkRectangle alloc;
  767. gtk_widget_get_allocation(inst->area, &alloc);
  768. #else
  769. GtkAllocation alloc = inst->area->allocation;
  770. #endif
  771. drawing_area_setup(inst, alloc.width, alloc.height);
  772. }
  773. static void drawing_area_setup_cb(void *vctx)
  774. {
  775. GtkFrontend *inst = (GtkFrontend *)vctx;
  776. if (!inst->drawing_area_setup_called)
  777. drawing_area_setup_simple(inst);
  778. }
  779. static void area_realised(GtkWidget *widget, GtkFrontend *inst)
  780. {
  781. inst->drawing_area_realised = true;
  782. if (inst->drawing_area_realised && inst->drawing_area_got_size &&
  783. inst->drawing_area_setup_needed)
  784. drawing_area_setup_simple(inst);
  785. }
  786. static void area_size_allocate(
  787. GtkWidget *widget, GdkRectangle *alloc, GtkFrontend *inst)
  788. {
  789. inst->drawing_area_got_size = true;
  790. if (inst->drawing_area_realised && inst->drawing_area_got_size)
  791. drawing_area_setup(inst, alloc->width, alloc->height);
  792. }
  793. #if GTK_CHECK_VERSION(3,10,0)
  794. static void area_check_scale(GtkFrontend *inst)
  795. {
  796. if (!inst->drawing_area_setup_needed &&
  797. inst->scale != gtk_widget_get_scale_factor(inst->area)) {
  798. drawing_area_setup_simple(inst);
  799. if (inst->term) {
  800. term_invalidate(inst->term);
  801. term_update(inst->term);
  802. }
  803. }
  804. }
  805. #endif
  806. static gboolean window_configured(
  807. GtkWidget *widget, GdkEventConfigure *event, gpointer data)
  808. {
  809. GtkFrontend *inst = (GtkFrontend *)data;
  810. if (inst->term) {
  811. term_notify_window_pos(inst->term, event->x, event->y);
  812. term_notify_window_size_pixels(
  813. inst->term, event->width, event->height);
  814. if (inst->drawing_area_realised && inst->drawing_area_got_size) {
  815. inst->drawing_area_setup_called = false;
  816. queue_toplevel_callback(drawing_area_setup_cb, inst);
  817. }
  818. }
  819. return false;
  820. }
  821. #if GTK_CHECK_VERSION(3,10,0)
  822. static gboolean area_configured(
  823. GtkWidget *widget, GdkEventConfigure *event, gpointer data)
  824. {
  825. GtkFrontend *inst = (GtkFrontend *)data;
  826. area_check_scale(inst);
  827. return false;
  828. }
  829. #endif
  830. #ifdef DRAW_TEXT_CAIRO
  831. static void cairo_setup_draw_ctx(GtkFrontend *inst)
  832. {
  833. cairo_set_line_width(inst->uctx.u.cairo.cr, 1.0);
  834. cairo_set_line_cap(inst->uctx.u.cairo.cr, CAIRO_LINE_CAP_SQUARE);
  835. cairo_set_line_join(inst->uctx.u.cairo.cr, CAIRO_LINE_JOIN_MITER);
  836. /*
  837. * This antialiasing setting doesn't affect Pango font rendering
  838. * but does affect stroking and filling paths; I don't quite
  839. * understand the logic of that, but I won't complain since it's
  840. * exactly what I happen to want.
  841. */
  842. cairo_set_antialias(inst->uctx.u.cairo.cr, CAIRO_ANTIALIAS_NONE);
  843. }
  844. #endif
  845. #if GTK_CHECK_VERSION(3,0,0)
  846. static gint draw_area(GtkWidget *widget, cairo_t *cr, gpointer data)
  847. {
  848. GtkFrontend *inst = (GtkFrontend *)data;
  849. #if GTK_CHECK_VERSION(3,10,0)
  850. /*
  851. * This may be the first we hear of the window scale having
  852. * changed, in which case we must hastily reconstruct our backing
  853. * surface before we copy the wrong one into the newly resized
  854. * real window.
  855. */
  856. area_check_scale(inst);
  857. #endif
  858. /*
  859. * GTK3 window redraw: we always expect Cairo to be enabled, so
  860. * that inst->surface exists, and pixmaps to be disabled, so that
  861. * inst->pixmap does not exist. Hence, we just blit from
  862. * inst->surface to the window.
  863. */
  864. if (inst->surface) {
  865. cairo_set_source_surface(cr, inst->surface, 0, 0);
  866. cairo_paint(cr);
  867. cairo_surface_flush(cairo_get_target(cr));
  868. }
  869. return true;
  870. }
  871. #else
  872. gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
  873. {
  874. GtkFrontend *inst = (GtkFrontend *)data;
  875. #ifndef NO_BACKING_PIXMAPS
  876. /*
  877. * Draw to the exposed part of the window from the server-side
  878. * backing pixmap.
  879. */
  880. if (inst->pixmap) {
  881. gdk_draw_pixmap(gtk_widget_get_window(widget),
  882. (gtk_widget_get_style(widget)->fg_gc
  883. [gtk_widget_get_state(widget)]),
  884. inst->pixmap,
  885. event->area.x, event->area.y,
  886. event->area.x, event->area.y,
  887. event->area.width, event->area.height);
  888. }
  889. #else
  890. /*
  891. * Failing that, draw from the backing Cairo surface. (We
  892. * should never be compiled in a context where we have _neither_
  893. * inst->surface nor inst->pixmap.)
  894. */
  895. if (inst->surface) {
  896. cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(widget));
  897. cairo_set_source_surface(cr, inst->surface, 0, 0);
  898. cairo_rectangle(cr, event->area.x, event->area.y,
  899. event->area.width, event->area.height);
  900. cairo_fill(cr);
  901. cairo_destroy(cr);
  902. }
  903. #endif
  904. return true;
  905. }
  906. #endif
  907. #define KEY_PRESSED(k) \
  908. (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
  909. #ifdef KEY_EVENT_DIAGNOSTICS
  910. char *dup_keyval_name(guint keyval)
  911. {
  912. const char *name = gdk_keyval_name(keyval);
  913. if (name)
  914. return dupstr(name);
  915. else
  916. return dupprintf("UNKNOWN[%u]", (unsigned)keyval);
  917. }
  918. #endif
  919. static void change_font_size(GtkFrontend *inst, int increment);
  920. static void key_pressed(GtkFrontend *inst);
  921. /* Subroutine used in key_event */
  922. static int return_key(GtkFrontend *inst, char *output, bool *special)
  923. {
  924. int end;
  925. /* Ugly label so we can come here as a fallback from
  926. * numeric keypad Enter handling */
  927. if (inst->term->cr_lf_return) {
  928. #ifdef KEY_EVENT_DIAGNOSTICS
  929. debug(" - Return in cr_lf_return mode, translating as 0d 0a\n");
  930. #endif
  931. output[1] = '\015';
  932. output[2] = '\012';
  933. end = 3;
  934. } else {
  935. #ifdef KEY_EVENT_DIAGNOSTICS
  936. debug(" - Return special case, translating as 0d + special\n");
  937. #endif
  938. output[1] = '\015';
  939. end = 2;
  940. *special = true;
  941. }
  942. return end;
  943. }
  944. gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
  945. {
  946. GtkFrontend *inst = (GtkFrontend *)data;
  947. char output[256];
  948. wchar_t ucsoutput[2];
  949. int ucsval, start, end, output_charset;
  950. bool special, use_ucsoutput;
  951. bool force_format_numeric_keypad = false;
  952. bool generated_something = false;
  953. char num_keypad_key = '\0';
  954. const char *event_string = event->string ? event->string : "";
  955. noise_ultralight(NOISE_SOURCE_KEY, event->keyval);
  956. #ifdef OSX_META_KEY_CONFIG
  957. if (event->state & inst->system_mod_mask)
  958. return false; /* let GTK process OS X Command key */
  959. #endif
  960. /* Remember the timestamp. */
  961. inst->input_event_time = event->time;
  962. /* By default, nothing is generated. */
  963. end = start = 0;
  964. special = use_ucsoutput = false;
  965. output_charset = CS_ISO8859_1;
  966. #ifdef KEY_EVENT_DIAGNOSTICS
  967. {
  968. char *type_string, *state_string, *keyval_string, *string_string;
  969. type_string = (event->type == GDK_KEY_PRESS ? dupstr("PRESS") :
  970. event->type == GDK_KEY_RELEASE ? dupstr("RELEASE") :
  971. dupprintf("UNKNOWN[%d]", (int)event->type));
  972. {
  973. static const struct {
  974. int mod_bit;
  975. const char *name;
  976. } mod_bits[] = {
  977. {GDK_SHIFT_MASK, "SHIFT"},
  978. {GDK_LOCK_MASK, "LOCK"},
  979. {GDK_CONTROL_MASK, "CONTROL"},
  980. {GDK_MOD1_MASK, "MOD1"},
  981. {GDK_MOD2_MASK, "MOD2"},
  982. {GDK_MOD3_MASK, "MOD3"},
  983. {GDK_MOD4_MASK, "MOD4"},
  984. {GDK_MOD5_MASK, "MOD5"},
  985. {GDK_SUPER_MASK, "SUPER"},
  986. {GDK_HYPER_MASK, "HYPER"},
  987. {GDK_META_MASK, "META"},
  988. };
  989. int i;
  990. int val = event->state;
  991. state_string = dupstr("");
  992. for (i = 0; i < lenof(mod_bits); i++) {
  993. if (val & mod_bits[i].mod_bit) {
  994. char *old = state_string;
  995. state_string = dupcat(state_string,
  996. state_string[0] ? "|" : "",
  997. mod_bits[i].name);
  998. sfree(old);
  999. val &= ~mod_bits[i].mod_bit;
  1000. }
  1001. }
  1002. if (val || !state_string[0]) {
  1003. char *old = state_string;
  1004. state_string = dupprintf("%s%s%d", state_string,
  1005. state_string[0] ? "|" : "", val);
  1006. sfree(old);
  1007. }
  1008. }
  1009. keyval_string = dup_keyval_name(event->keyval);
  1010. string_string = dupstr("");
  1011. {
  1012. int i;
  1013. for (i = 0; event_string[i]; i++) {
  1014. char *old = string_string;
  1015. string_string = dupprintf("%s%s%02x", string_string,
  1016. string_string[0] ? " " : "",
  1017. (unsigned)event_string[i] & 0xFF);
  1018. sfree(old);
  1019. }
  1020. }
  1021. debug("key_event: type=%s keyval=%s state=%s "
  1022. "hardware_keycode=%d is_modifier=%s string=[%s]\n",
  1023. type_string, keyval_string, state_string,
  1024. (int)event->hardware_keycode,
  1025. event->is_modifier ? "true" : "false",
  1026. string_string);
  1027. sfree(type_string);
  1028. sfree(state_string);
  1029. sfree(keyval_string);
  1030. sfree(string_string);
  1031. }
  1032. #endif /* KEY_EVENT_DIAGNOSTICS */
  1033. /*
  1034. * If Alt is being released after typing an Alt+numberpad
  1035. * sequence, we should generate the code that was typed.
  1036. *
  1037. * Note that we only do this if more than one key was actually
  1038. * pressed - I don't think Alt+NumPad4 should be ^D or that
  1039. * Alt+NumPad3 should be ^C, for example. There's no serious
  1040. * inconvenience in having to type a zero before a single-digit
  1041. * character code.
  1042. */
  1043. if (event->type == GDK_KEY_RELEASE) {
  1044. if ((event->keyval == GDK_KEY_Meta_L ||
  1045. event->keyval == GDK_KEY_Meta_R ||
  1046. event->keyval == GDK_KEY_Alt_L ||
  1047. event->keyval == GDK_KEY_Alt_R) &&
  1048. inst->alt_keycode >= 0 && inst->alt_digits > 1) {
  1049. #ifdef KEY_EVENT_DIAGNOSTICS
  1050. debug(" - modifier release terminates Alt+numberpad input, "
  1051. "keycode = %d\n", inst->alt_keycode);
  1052. #endif
  1053. /*
  1054. * FIXME: we might usefully try to do something clever here
  1055. * about interpreting the generated key code in a way that's
  1056. * appropriate to the line code page.
  1057. */
  1058. output[0] = inst->alt_keycode;
  1059. end = 1;
  1060. goto done;
  1061. }
  1062. #if GTK_CHECK_VERSION(2,0,0)
  1063. #ifdef KEY_EVENT_DIAGNOSTICS
  1064. debug(" - key release, passing to IM\n");
  1065. #endif
  1066. if (gtk_im_context_filter_keypress(inst->imc, event)) {
  1067. #ifdef KEY_EVENT_DIAGNOSTICS
  1068. debug(" - key release accepted by IM\n");
  1069. #endif
  1070. return true;
  1071. } else {
  1072. #ifdef KEY_EVENT_DIAGNOSTICS
  1073. debug(" - key release not accepted by IM\n");
  1074. #endif
  1075. }
  1076. #endif
  1077. }
  1078. if (event->type == GDK_KEY_PRESS) {
  1079. /*
  1080. * If Alt has just been pressed, we start potentially
  1081. * accumulating an Alt+numberpad code. We do this by
  1082. * setting alt_keycode to -1 (nothing yet but plausible).
  1083. */
  1084. if ((event->keyval == GDK_KEY_Meta_L ||
  1085. event->keyval == GDK_KEY_Meta_R ||
  1086. event->keyval == GDK_KEY_Alt_L ||
  1087. event->keyval == GDK_KEY_Alt_R)) {
  1088. #ifdef KEY_EVENT_DIAGNOSTICS
  1089. debug(" - modifier press potentially begins Alt+numberpad "
  1090. "input\n");
  1091. #endif
  1092. inst->alt_keycode = -1;
  1093. inst->alt_digits = 0;
  1094. goto done; /* this generates nothing else */
  1095. }
  1096. /*
  1097. * If we're seeing a numberpad key press with Meta down,
  1098. * consider adding it to alt_keycode if that's sensible.
  1099. * Anything _else_ with Meta down cancels any possibility
  1100. * of an ALT keycode: we set alt_keycode to -2.
  1101. */
  1102. if ((event->state & inst->meta_mod_mask) && inst->alt_keycode != -2) {
  1103. int digit = -1;
  1104. switch (event->keyval) {
  1105. case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: digit = 0; break;
  1106. case GDK_KEY_KP_1: case GDK_KEY_KP_End: digit = 1; break;
  1107. case GDK_KEY_KP_2: case GDK_KEY_KP_Down: digit = 2; break;
  1108. case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: digit = 3; break;
  1109. case GDK_KEY_KP_4: case GDK_KEY_KP_Left: digit = 4; break;
  1110. case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: digit = 5; break;
  1111. case GDK_KEY_KP_6: case GDK_KEY_KP_Right: digit = 6; break;
  1112. case GDK_KEY_KP_7: case GDK_KEY_KP_Home: digit = 7; break;
  1113. case GDK_KEY_KP_8: case GDK_KEY_KP_Up: digit = 8; break;
  1114. case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: digit = 9; break;
  1115. }
  1116. if (digit < 0)
  1117. inst->alt_keycode = -2; /* it's invalid */
  1118. else {
  1119. #if defined(DEBUG) && defined(KEY_EVENT_DIAGNOSTICS)
  1120. int old_keycode = inst->alt_keycode;
  1121. #endif
  1122. if (inst->alt_keycode == -1)
  1123. inst->alt_keycode = digit; /* one-digit code */
  1124. else
  1125. inst->alt_keycode = inst->alt_keycode * 10 + digit;
  1126. inst->alt_digits++;
  1127. #ifdef KEY_EVENT_DIAGNOSTICS
  1128. debug(" - Alt+numberpad digit %d added to keycode %d"
  1129. " gives %d\n", digit, old_keycode, inst->alt_keycode);
  1130. #endif
  1131. /* Having used this digit, we now do nothing more with it. */
  1132. goto done;
  1133. }
  1134. }
  1135. if (event->keyval == GDK_KEY_greater &&
  1136. (event->state & GDK_CONTROL_MASK)) {
  1137. #ifdef KEY_EVENT_DIAGNOSTICS
  1138. debug(" - Ctrl->: increase font size\n");
  1139. #endif
  1140. if (!inst->win_resize_pending)
  1141. change_font_size(inst, +1);
  1142. return true;
  1143. }
  1144. if (event->keyval == GDK_KEY_less &&
  1145. (event->state & GDK_CONTROL_MASK)) {
  1146. #ifdef KEY_EVENT_DIAGNOSTICS
  1147. debug(" - Ctrl-<: increase font size\n");
  1148. #endif
  1149. if (!inst->win_resize_pending)
  1150. change_font_size(inst, -1);
  1151. return true;
  1152. }
  1153. /*
  1154. * Shift-PgUp and Shift-PgDn don't even generate keystrokes
  1155. * at all.
  1156. */
  1157. if (event->keyval == GDK_KEY_Page_Up &&
  1158. ((event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) ==
  1159. (GDK_CONTROL_MASK | GDK_SHIFT_MASK))) {
  1160. #ifdef KEY_EVENT_DIAGNOSTICS
  1161. debug(" - Ctrl-Shift-PgUp scroll\n");
  1162. #endif
  1163. term_scroll(inst->term, 1, 0);
  1164. return true;
  1165. }
  1166. if (event->keyval == GDK_KEY_Page_Up &&
  1167. (event->state & GDK_SHIFT_MASK)) {
  1168. #ifdef KEY_EVENT_DIAGNOSTICS
  1169. debug(" - Shift-PgUp scroll\n");
  1170. #endif
  1171. term_scroll(inst->term, 0, -inst->height/2);
  1172. return true;
  1173. }
  1174. if (event->keyval == GDK_KEY_Page_Up &&
  1175. (event->state & GDK_CONTROL_MASK)) {
  1176. #ifdef KEY_EVENT_DIAGNOSTICS
  1177. debug(" - Ctrl-PgUp scroll\n");
  1178. #endif
  1179. term_scroll(inst->term, 0, -1);
  1180. return true;
  1181. }
  1182. if (event->keyval == GDK_KEY_Page_Down &&
  1183. ((event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) ==
  1184. (GDK_CONTROL_MASK | GDK_SHIFT_MASK))) {
  1185. #ifdef KEY_EVENT_DIAGNOSTICS
  1186. debug(" - Ctrl-shift-PgDn scroll\n");
  1187. #endif
  1188. term_scroll(inst->term, -1, 0);
  1189. return true;
  1190. }
  1191. if (event->keyval == GDK_KEY_Page_Down &&
  1192. (event->state & GDK_SHIFT_MASK)) {
  1193. #ifdef KEY_EVENT_DIAGNOSTICS
  1194. debug(" - Shift-PgDn scroll\n");
  1195. #endif
  1196. term_scroll(inst->term, 0, +inst->height/2);
  1197. return true;
  1198. }
  1199. if (event->keyval == GDK_KEY_Page_Down &&
  1200. (event->state & GDK_CONTROL_MASK)) {
  1201. #ifdef KEY_EVENT_DIAGNOSTICS
  1202. debug(" - Ctrl-PgDn scroll\n");
  1203. #endif
  1204. term_scroll(inst->term, 0, +1);
  1205. return true;
  1206. }
  1207. /*
  1208. * Neither do Shift-Ins or Ctrl-Ins (if enabled).
  1209. */
  1210. if (event->keyval == GDK_KEY_Insert &&
  1211. (event->state & GDK_SHIFT_MASK)) {
  1212. int cfgval = conf_get_int(inst->conf, CONF_ctrlshiftins);
  1213. switch (cfgval) {
  1214. case CLIPUI_IMPLICIT:
  1215. #ifdef KEY_EVENT_DIAGNOSTICS
  1216. debug(" - Shift-Insert: paste from PRIMARY\n");
  1217. #endif
  1218. term_request_paste(inst->term, CLIP_PRIMARY);
  1219. return true;
  1220. case CLIPUI_EXPLICIT:
  1221. #ifdef KEY_EVENT_DIAGNOSTICS
  1222. debug(" - Shift-Insert: paste from CLIPBOARD\n");
  1223. #endif
  1224. term_request_paste(inst->term, CLIP_CLIPBOARD);
  1225. return true;
  1226. case CLIPUI_CUSTOM:
  1227. #ifdef KEY_EVENT_DIAGNOSTICS
  1228. debug(" - Shift-Insert: paste from custom clipboard\n");
  1229. #endif
  1230. term_request_paste(inst->term, inst->clipboard_ctrlshiftins);
  1231. return true;
  1232. default:
  1233. #ifdef KEY_EVENT_DIAGNOSTICS
  1234. debug(" - Shift-Insert: no paste action\n");
  1235. #endif
  1236. break;
  1237. }
  1238. }
  1239. if (event->keyval == GDK_KEY_Insert &&
  1240. (event->state & GDK_CONTROL_MASK)) {
  1241. static const int clips_clipboard[] = { CLIP_CLIPBOARD };
  1242. int cfgval = conf_get_int(inst->conf, CONF_ctrlshiftins);
  1243. switch (cfgval) {
  1244. case CLIPUI_IMPLICIT:
  1245. /* do nothing; re-copy to PRIMARY is not needed */
  1246. #ifdef KEY_EVENT_DIAGNOSTICS
  1247. debug(" - Ctrl-Insert: non-copy to PRIMARY\n");
  1248. #endif
  1249. return true;
  1250. case CLIPUI_EXPLICIT:
  1251. #ifdef KEY_EVENT_DIAGNOSTICS
  1252. debug(" - Ctrl-Insert: copy to CLIPBOARD\n");
  1253. #endif
  1254. term_request_copy(inst->term,
  1255. clips_clipboard, lenof(clips_clipboard));
  1256. return true;
  1257. case CLIPUI_CUSTOM:
  1258. #ifdef KEY_EVENT_DIAGNOSTICS
  1259. debug(" - Ctrl-Insert: copy to custom clipboard\n");
  1260. #endif
  1261. term_request_copy(inst->term,
  1262. &inst->clipboard_ctrlshiftins, 1);
  1263. return true;
  1264. default:
  1265. #ifdef KEY_EVENT_DIAGNOSTICS
  1266. debug(" - Ctrl-Insert: no copy action\n");
  1267. #endif
  1268. break;
  1269. }
  1270. }
  1271. /*
  1272. * Another pair of copy-paste keys.
  1273. */
  1274. if ((event->state & GDK_SHIFT_MASK) &&
  1275. (event->state & GDK_CONTROL_MASK) &&
  1276. (event->keyval == GDK_KEY_C || event->keyval == GDK_KEY_c ||
  1277. event->keyval == GDK_KEY_V || event->keyval == GDK_KEY_v)) {
  1278. int cfgval = conf_get_int(inst->conf, CONF_ctrlshiftcv);
  1279. bool paste = (event->keyval == GDK_KEY_V ||
  1280. event->keyval == GDK_KEY_v);
  1281. switch (cfgval) {
  1282. case CLIPUI_IMPLICIT:
  1283. if (paste) {
  1284. #ifdef KEY_EVENT_DIAGNOSTICS
  1285. debug(" - Ctrl-Shift-V: paste from PRIMARY\n");
  1286. #endif
  1287. term_request_paste(inst->term, CLIP_PRIMARY);
  1288. } else {
  1289. #ifdef KEY_EVENT_DIAGNOSTICS
  1290. debug(" - Ctrl-Shift-C: non-copy to PRIMARY\n");
  1291. #endif
  1292. }
  1293. return true;
  1294. case CLIPUI_EXPLICIT:
  1295. if (paste) {
  1296. #ifdef KEY_EVENT_DIAGNOSTICS
  1297. debug(" - Ctrl-Shift-V: paste from CLIPBOARD\n");
  1298. #endif
  1299. term_request_paste(inst->term, CLIP_CLIPBOARD);
  1300. } else {
  1301. static const int clips[] = { CLIP_CLIPBOARD };
  1302. #ifdef KEY_EVENT_DIAGNOSTICS
  1303. debug(" - Ctrl-Shift-C: copy to CLIPBOARD\n");
  1304. #endif
  1305. term_request_copy(inst->term, clips, lenof(clips));
  1306. }
  1307. return true;
  1308. case CLIPUI_CUSTOM:
  1309. if (paste) {
  1310. #ifdef KEY_EVENT_DIAGNOSTICS
  1311. debug(" - Ctrl-Shift-V: paste from custom clipboard\n");
  1312. #endif
  1313. term_request_paste(inst->term,
  1314. inst->clipboard_ctrlshiftcv);
  1315. } else {
  1316. #ifdef KEY_EVENT_DIAGNOSTICS
  1317. debug(" - Ctrl-Shift-C: copy to custom clipboard\n");
  1318. #endif
  1319. term_request_copy(inst->term,
  1320. &inst->clipboard_ctrlshiftcv, 1);
  1321. }
  1322. return true;
  1323. }
  1324. }
  1325. special = false;
  1326. use_ucsoutput = false;
  1327. /* ALT+things gives leading Escape. */
  1328. output[0] = '\033';
  1329. #if !GTK_CHECK_VERSION(2,0,0)
  1330. /*
  1331. * In vanilla X, and hence also GDK 1.2, the string received
  1332. * as part of a keyboard event is assumed to be in
  1333. * ISO-8859-1. (Seems woefully shortsighted in i18n terms,
  1334. * but it's true: see the man page for XLookupString(3) for
  1335. * confirmation.)
  1336. */
  1337. output_charset = CS_ISO8859_1;
  1338. strncpy(output+1, event_string, lenof(output)-1);
  1339. #else /* !GTK_CHECK_VERSION(2,0,0) */
  1340. /*
  1341. * Most things can now be passed to
  1342. * gtk_im_context_filter_keypress without breaking anything
  1343. * below this point. An exception is the numeric keypad if
  1344. * we're in Nethack or application mode: the IM will eat
  1345. * numeric keypad presses if Num Lock is on, but we don't want
  1346. * it to.
  1347. */
  1348. bool numeric = false;
  1349. bool nethack_mode = conf_get_bool(inst->conf, CONF_nethack_keypad);
  1350. bool app_keypad_mode = (inst->term->app_keypad_keys &&
  1351. !conf_get_bool(inst->conf, CONF_no_applic_k));
  1352. switch (event->keyval) {
  1353. case GDK_KEY_Num_Lock: num_keypad_key = 'G'; break;
  1354. case GDK_KEY_KP_Divide: num_keypad_key = '/'; break;
  1355. case GDK_KEY_KP_Multiply: num_keypad_key = '*'; break;
  1356. case GDK_KEY_KP_Subtract: num_keypad_key = '-'; break;
  1357. case GDK_KEY_KP_Add: num_keypad_key = '+'; break;
  1358. case GDK_KEY_KP_Enter: num_keypad_key = '\r'; break;
  1359. case GDK_KEY_KP_0: num_keypad_key = '0'; numeric = true; break;
  1360. case GDK_KEY_KP_Insert: num_keypad_key = '0'; break;
  1361. case GDK_KEY_KP_1: num_keypad_key = '1'; numeric = true; break;
  1362. case GDK_KEY_KP_End: num_keypad_key = '1'; break;
  1363. case GDK_KEY_KP_2: num_keypad_key = '2'; numeric = true; break;
  1364. case GDK_KEY_KP_Down: num_keypad_key = '2'; break;
  1365. case GDK_KEY_KP_3: num_keypad_key = '3'; numeric = true; break;
  1366. case GDK_KEY_KP_Page_Down: num_keypad_key = '3'; break;
  1367. case GDK_KEY_KP_4: num_keypad_key = '4'; numeric = true; break;
  1368. case GDK_KEY_KP_Left: num_keypad_key = '4'; break;
  1369. case GDK_KEY_KP_5: num_keypad_key = '5'; numeric = true; break;
  1370. case GDK_KEY_KP_Begin: num_keypad_key = '5'; break;
  1371. case GDK_KEY_KP_6: num_keypad_key = '6'; numeric = true; break;
  1372. case GDK_KEY_KP_Right: num_keypad_key = '6'; break;
  1373. case GDK_KEY_KP_7: num_keypad_key = '7'; numeric = true; break;
  1374. case GDK_KEY_KP_Home: num_keypad_key = '7'; break;
  1375. case GDK_KEY_KP_8: num_keypad_key = '8'; numeric = true; break;
  1376. case GDK_KEY_KP_Up: num_keypad_key = '8'; break;
  1377. case GDK_KEY_KP_9: num_keypad_key = '9'; numeric = true; break;
  1378. case GDK_KEY_KP_Page_Up: num_keypad_key = '9'; break;
  1379. case GDK_KEY_KP_Decimal: num_keypad_key = '.'; numeric = true; break;
  1380. case GDK_KEY_KP_Delete: num_keypad_key = '.'; break;
  1381. }
  1382. if ((app_keypad_mode && num_keypad_key &&
  1383. (numeric || inst->term->funky_type != FUNKY_XTERM)) ||
  1384. (nethack_mode && num_keypad_key >= '1' && num_keypad_key <= '9')) {
  1385. /* In these modes, we override the keypad handling:
  1386. * regardless of Num Lock, the keys are handled by
  1387. * format_numeric_keypad_key below. */
  1388. force_format_numeric_keypad = true;
  1389. } else {
  1390. bool try_filter = true;
  1391. #ifdef META_MANUAL_MASK
  1392. if (event->state & META_MANUAL_MASK & inst->meta_mod_mask) {
  1393. /*
  1394. * If this key event had a Meta modifier bit set which
  1395. * is also in META_MANUAL_MASK, that means passing
  1396. * such an event to the GtkIMContext will be unhelpful
  1397. * (it will eat the keystroke and turn it into
  1398. * something not what we wanted).
  1399. */
  1400. #ifdef KEY_EVENT_DIAGNOSTICS
  1401. debug(" - Meta modifier requiring manual intervention, "
  1402. "suppressing IM filtering\n");
  1403. #endif
  1404. try_filter = false;
  1405. }
  1406. #endif
  1407. if (try_filter) {
  1408. #ifdef KEY_EVENT_DIAGNOSTICS
  1409. debug(" - general key press, passing to IM\n");
  1410. #endif
  1411. if (gtk_im_context_filter_keypress(inst->imc, event)) {
  1412. #ifdef KEY_EVENT_DIAGNOSTICS
  1413. debug(" - key press accepted by IM\n");
  1414. #endif
  1415. return true;
  1416. } else {
  1417. #ifdef KEY_EVENT_DIAGNOSTICS
  1418. debug(" - key press not accepted by IM\n");
  1419. #endif
  1420. }
  1421. }
  1422. }
  1423. /*
  1424. * GDK 2.0 arranges to have done some translation for us: in
  1425. * GDK 2.0, event->string is encoded in the current locale.
  1426. *
  1427. * So we use the standard C library function mbstowcs() to
  1428. * convert from the current locale into Unicode; from there
  1429. * we can convert to whatever PuTTY is currently working in.
  1430. * (In fact I convert straight back to UTF-8 from
  1431. * wide-character Unicode, for the sake of simplicity: that
  1432. * way we can still use exactly the same code to manipulate
  1433. * the string, such as prefixing ESC.)
  1434. */
  1435. output_charset = CS_UTF8;
  1436. {
  1437. wchar_t widedata[32];
  1438. const wchar_t *wp;
  1439. int wlen;
  1440. int ulen;
  1441. buffer_sink bs[1];
  1442. buffer_sink_init(bs, widedata, sizeof(widedata) - sizeof(wchar_t));
  1443. put_mb_to_wc(bs, DEFAULT_CODEPAGE,
  1444. event_string, strlen(event_string));
  1445. wlen = (wchar_t *)bs->out - widedata;
  1446. #ifdef KEY_EVENT_DIAGNOSTICS
  1447. {
  1448. char *string_string = dupstr("");
  1449. int i;
  1450. for (i = 0; i < wlen; i++) {
  1451. char *old = string_string;
  1452. string_string = dupprintf("%s%s%04x", string_string,
  1453. string_string[0] ? " " : "",
  1454. (unsigned)widedata[i]);
  1455. sfree(old);
  1456. }
  1457. debug(" - string translated into Unicode = [%s]\n",
  1458. string_string);
  1459. sfree(string_string);
  1460. }
  1461. #endif
  1462. wp = widedata;
  1463. ulen = charset_from_unicode(&wp, &wlen, output+1, lenof(output)-2,
  1464. CS_UTF8, NULL, NULL, 0);
  1465. #ifdef KEY_EVENT_DIAGNOSTICS
  1466. {
  1467. char *string_string = dupstr("");
  1468. int i;
  1469. for (i = 0; i < ulen; i++) {
  1470. char *old = string_string;
  1471. string_string = dupprintf("%s%s%02x", string_string,
  1472. string_string[0] ? " " : "",
  1473. (unsigned)output[i+1] & 0xFF);
  1474. sfree(old);
  1475. }
  1476. debug(" - string translated into UTF-8 = [%s]\n",
  1477. string_string);
  1478. sfree(string_string);
  1479. }
  1480. #endif
  1481. output[1+ulen] = '\0';
  1482. }
  1483. #endif /* !GTK_CHECK_VERSION(2,0,0) */
  1484. if (!output[1] &&
  1485. (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
  1486. ucsoutput[0] = '\033';
  1487. ucsoutput[1] = ucsval;
  1488. #ifdef KEY_EVENT_DIAGNOSTICS
  1489. debug(" - keysym_to_unicode gave %04x\n",
  1490. (unsigned)ucsoutput[1]);
  1491. #endif
  1492. use_ucsoutput = true;
  1493. end = 2;
  1494. } else {
  1495. output[lenof(output)-1] = '\0';
  1496. end = strlen(output);
  1497. }
  1498. if (event->state & inst->meta_mod_mask) {
  1499. start = 0;
  1500. if (end == 1) end = 0;
  1501. #ifdef META_MANUAL_MASK
  1502. if (event->state & META_MANUAL_MASK) {
  1503. /*
  1504. * Key events which have a META_MANUAL_MASK meta bit
  1505. * set may have a keyval reflecting that, e.g. on OS X
  1506. * the Option key acts as an AltGr-like modifier and
  1507. * causes different Unicode characters to be output.
  1508. *
  1509. * To work around this, we clear the dangerous
  1510. * modifier bit and retranslate from the hardware
  1511. * keycode as if the key had been pressed without that
  1512. * modifier. Then we prefix Esc to *that*.
  1513. */
  1514. guint new_keyval;
  1515. GdkModifierType consumed;
  1516. if (gdk_keymap_translate_keyboard_state(
  1517. gdk_keymap_get_for_display(gdk_display_get_default()),
  1518. event->hardware_keycode,
  1519. event->state & ~META_MANUAL_MASK,
  1520. 0, &new_keyval, NULL, NULL, &consumed)) {
  1521. ucsoutput[0] = '\033';
  1522. ucsoutput[1] = gdk_keyval_to_unicode(new_keyval);
  1523. #ifdef KEY_EVENT_DIAGNOSTICS
  1524. {
  1525. char *keyval_name = dup_keyval_name(new_keyval);
  1526. debug(" - retranslation for manual Meta: "
  1527. "new keyval = %s, Unicode = %04x\n",
  1528. keyval_name, (unsigned)ucsoutput[1]);
  1529. sfree(keyval_name);
  1530. }
  1531. #endif
  1532. use_ucsoutput = true;
  1533. end = 2;
  1534. }
  1535. }
  1536. #endif
  1537. } else
  1538. start = 1;
  1539. /* Control-` is the same as Control-\ (unless gtk has a better idea) */
  1540. if (!output[1] && event->keyval == '`' &&
  1541. (event->state & GDK_CONTROL_MASK)) {
  1542. #ifdef KEY_EVENT_DIAGNOSTICS
  1543. debug(" - Ctrl-` special case, translating as 1c\n");
  1544. #endif
  1545. output[1] = '\x1C';
  1546. use_ucsoutput = false;
  1547. end = 2;
  1548. }
  1549. /* Some GTK backends (e.g. Quartz) do not change event->string
  1550. * in response to the Control modifier. So we do it ourselves
  1551. * here, if it's not already happened.
  1552. *
  1553. * The translations below are in line with X11 policy as far
  1554. * as I know. */
  1555. if ((event->state & GDK_CONTROL_MASK) && end == 2) {
  1556. int orig = use_ucsoutput ? ucsoutput[1] : output[1];
  1557. int new = orig;
  1558. if (new >= '3' && new <= '7') {
  1559. /* ^3,...,^7 map to 0x1B,...,0x1F */
  1560. new += '\x1B' - '3';
  1561. } else if (new == '2' || new == ' ') {
  1562. /* ^2 and ^Space are both ^@, i.e. \0 */
  1563. new = '\0';
  1564. } else if (new == '8') {
  1565. /* ^8 is DEL */
  1566. new = '\x7F';
  1567. } else if (new == '/') {
  1568. /* ^/ is the same as ^_ */
  1569. new = '\x1F';
  1570. } else if (new >= 0x40 && new < 0x7F) {
  1571. /* Everything anywhere near the alphabetics just gets
  1572. * masked. */
  1573. new &= 0x1F;
  1574. }
  1575. /* Anything else, e.g. '0', is unchanged. */
  1576. if (orig == new) {
  1577. #ifdef KEY_EVENT_DIAGNOSTICS
  1578. debug(" - manual Ctrl key handling did nothing\n");
  1579. #endif
  1580. } else {
  1581. #ifdef KEY_EVENT_DIAGNOSTICS
  1582. debug(" - manual Ctrl key handling: %02x -> %02x\n",
  1583. (unsigned)orig, (unsigned)new);
  1584. #endif
  1585. output[1] = new;
  1586. use_ucsoutput = false;
  1587. }
  1588. }
  1589. /* Control-Break sends a Break special to the backend */
  1590. if (event->keyval == GDK_KEY_Break &&
  1591. (event->state & GDK_CONTROL_MASK)) {
  1592. #ifdef KEY_EVENT_DIAGNOSTICS
  1593. debug(" - Ctrl-Break special case, sending SS_BRK\n");
  1594. #endif
  1595. if (inst->backend)
  1596. backend_special(inst->backend, SS_BRK, 0);
  1597. return true;
  1598. }
  1599. /* We handle Return ourselves, because it needs to be flagged as
  1600. * special to ldisc. */
  1601. if (event->keyval == GDK_KEY_Return) {
  1602. end = return_key(inst, output, &special);
  1603. use_ucsoutput = false;
  1604. }
  1605. /* Control-2, Control-Space and Control-@ are NUL */
  1606. if (!output[1] &&
  1607. (event->keyval == ' ' || event->keyval == '2' ||
  1608. event->keyval == '@') &&
  1609. (event->state & (GDK_SHIFT_MASK |
  1610. GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
  1611. #ifdef KEY_EVENT_DIAGNOSTICS
  1612. debug(" - Ctrl-{space,2,@} special case, translating as 00\n");
  1613. #endif
  1614. output[1] = '\0';
  1615. use_ucsoutput = false;
  1616. end = 2;
  1617. }
  1618. /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
  1619. if (!output[1] && event->keyval == ' ' &&
  1620. (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
  1621. (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
  1622. #ifdef KEY_EVENT_DIAGNOSTICS
  1623. debug(" - Ctrl-Shift-space special case, translating as 00a0\n");
  1624. #endif
  1625. output[1] = '\240';
  1626. output_charset = CS_ISO8859_1;
  1627. use_ucsoutput = false;
  1628. end = 2;
  1629. }
  1630. /* We don't let GTK tell us what Backspace is! We know better. */
  1631. if (event->keyval == GDK_KEY_BackSpace &&
  1632. !(event->state & GDK_SHIFT_MASK)) {
  1633. output[1] = conf_get_bool(inst->conf, CONF_bksp_is_delete) ?
  1634. '\x7F' : '\x08';
  1635. #ifdef KEY_EVENT_DIAGNOSTICS
  1636. debug(" - Backspace, translating as %02x\n",
  1637. (unsigned)output[1]);
  1638. #endif
  1639. use_ucsoutput = false;
  1640. end = 2;
  1641. special = true;
  1642. }
  1643. /* For Shift Backspace, do opposite of what is configured. */
  1644. if (event->keyval == GDK_KEY_BackSpace &&
  1645. (event->state & GDK_SHIFT_MASK)) {
  1646. output[1] = conf_get_bool(inst->conf, CONF_bksp_is_delete) ?
  1647. '\x08' : '\x7F';
  1648. #ifdef KEY_EVENT_DIAGNOSTICS
  1649. debug(" - Shift-Backspace, translating as %02x\n",
  1650. (unsigned)output[1]);
  1651. #endif
  1652. use_ucsoutput = false;
  1653. end = 2;
  1654. special = true;
  1655. }
  1656. /* Shift-Tab is ESC [ Z */
  1657. if (event->keyval == GDK_KEY_ISO_Left_Tab ||
  1658. (event->keyval == GDK_KEY_Tab &&
  1659. (event->state & GDK_SHIFT_MASK))) {
  1660. #ifdef KEY_EVENT_DIAGNOSTICS
  1661. debug(" - Shift-Tab, translating as ESC [ Z\n");
  1662. #endif
  1663. end = 1 + sprintf(output+1, "\033[Z");
  1664. use_ucsoutput = false;
  1665. }
  1666. /* And normal Tab is Tab, if the keymap hasn't already told us.
  1667. * (Curiously, at least one version of the MacOS 10.5 X server
  1668. * doesn't translate Tab for us. */
  1669. if (event->keyval == GDK_KEY_Tab && end <= 1) {
  1670. #ifdef KEY_EVENT_DIAGNOSTICS
  1671. debug(" - Tab, translating as 09\n");
  1672. #endif
  1673. output[1] = '\t';
  1674. end = 2;
  1675. }
  1676. if (num_keypad_key && force_format_numeric_keypad) {
  1677. end = 1 + format_numeric_keypad_key(
  1678. output+1, inst->term, num_keypad_key,
  1679. event->state & GDK_SHIFT_MASK,
  1680. event->state & GDK_CONTROL_MASK);
  1681. #ifdef KEY_EVENT_DIAGNOSTICS
  1682. debug(" - numeric keypad key");
  1683. #endif
  1684. use_ucsoutput = false;
  1685. goto done;
  1686. }
  1687. switch (event->keyval) {
  1688. int fkey_number;
  1689. bool consumed_meta_key;
  1690. case GDK_KEY_F1: fkey_number = 1; goto numbered_function_key;
  1691. case GDK_KEY_F2: fkey_number = 2; goto numbered_function_key;
  1692. case GDK_KEY_F3: fkey_number = 3; goto numbered_function_key;
  1693. case GDK_KEY_F4: fkey_number = 4; goto numbered_function_key;
  1694. case GDK_KEY_F5: fkey_number = 5; goto numbered_function_key;
  1695. case GDK_KEY_F6: fkey_number = 6; goto numbered_function_key;
  1696. case GDK_KEY_F7: fkey_number = 7; goto numbered_function_key;
  1697. case GDK_KEY_F8: fkey_number = 8; goto numbered_function_key;
  1698. case GDK_KEY_F9: fkey_number = 9; goto numbered_function_key;
  1699. case GDK_KEY_F10: fkey_number = 10; goto numbered_function_key;
  1700. case GDK_KEY_F11: fkey_number = 11; goto numbered_function_key;
  1701. case GDK_KEY_F12: fkey_number = 12; goto numbered_function_key;
  1702. case GDK_KEY_F13: fkey_number = 13; goto numbered_function_key;
  1703. case GDK_KEY_F14: fkey_number = 14; goto numbered_function_key;
  1704. case GDK_KEY_F15: fkey_number = 15; goto numbered_function_key;
  1705. case GDK_KEY_F16: fkey_number = 16; goto numbered_function_key;
  1706. case GDK_KEY_F17: fkey_number = 17; goto numbered_function_key;
  1707. case GDK_KEY_F18: fkey_number = 18; goto numbered_function_key;
  1708. case GDK_KEY_F19: fkey_number = 19; goto numbered_function_key;
  1709. case GDK_KEY_F20: fkey_number = 20; goto numbered_function_key;
  1710. numbered_function_key:
  1711. consumed_meta_key = false;
  1712. end = 1 + format_function_key(output+1, inst->term, fkey_number,
  1713. event->state & GDK_SHIFT_MASK,
  1714. event->state & GDK_CONTROL_MASK,
  1715. event->state & inst->meta_mod_mask,
  1716. &consumed_meta_key);
  1717. if (consumed_meta_key)
  1718. start = 1; /* supersedes the usual prefixing of Esc */
  1719. #ifdef KEY_EVENT_DIAGNOSTICS
  1720. debug(" - function key F%d", fkey_number);
  1721. #endif
  1722. use_ucsoutput = false;
  1723. goto done;
  1724. SmallKeypadKey sk_key;
  1725. case GDK_KEY_Home: case GDK_KEY_KP_Home:
  1726. sk_key = SKK_HOME; goto small_keypad_key;
  1727. case GDK_KEY_Insert: case GDK_KEY_KP_Insert:
  1728. sk_key = SKK_INSERT; goto small_keypad_key;
  1729. case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
  1730. sk_key = SKK_DELETE; goto small_keypad_key;
  1731. case GDK_KEY_End: case GDK_KEY_KP_End:
  1732. sk_key = SKK_END; goto small_keypad_key;
  1733. case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
  1734. sk_key = SKK_PGUP; goto small_keypad_key;
  1735. case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
  1736. sk_key = SKK_PGDN; goto small_keypad_key;
  1737. small_keypad_key:
  1738. /* These keys don't generate terminal input with Ctrl */
  1739. if (event->state & GDK_CONTROL_MASK)
  1740. break;
  1741. consumed_meta_key = false;
  1742. end = 1 + format_small_keypad_key(
  1743. output+1, inst->term, sk_key, event->state & GDK_SHIFT_MASK,
  1744. event->state & GDK_CONTROL_MASK,
  1745. event->state & inst->meta_mod_mask, &consumed_meta_key);
  1746. if (consumed_meta_key)
  1747. start = 1; /* supersedes the usual prefixing of Esc */
  1748. #ifdef KEY_EVENT_DIAGNOSTICS
  1749. debug(" - small keypad key");
  1750. #endif
  1751. use_ucsoutput = false;
  1752. goto done;
  1753. int xkey;
  1754. case GDK_KEY_Up: case GDK_KEY_KP_Up:
  1755. xkey = 'A'; goto arrow_key;
  1756. case GDK_KEY_Down: case GDK_KEY_KP_Down:
  1757. xkey = 'B'; goto arrow_key;
  1758. case GDK_KEY_Right: case GDK_KEY_KP_Right:
  1759. xkey = 'C'; goto arrow_key;
  1760. case GDK_KEY_Left: case GDK_KEY_KP_Left:
  1761. xkey = 'D'; goto arrow_key;
  1762. case GDK_KEY_Begin: case GDK_KEY_KP_Begin:
  1763. xkey = 'G'; goto arrow_key;
  1764. arrow_key:
  1765. consumed_meta_key = false;
  1766. end = 1 + format_arrow_key(
  1767. output+1, inst->term, xkey, event->state & GDK_SHIFT_MASK,
  1768. event->state & GDK_CONTROL_MASK,
  1769. event->state & inst->meta_mod_mask, &consumed_meta_key);
  1770. if (consumed_meta_key)
  1771. start = 1; /* supersedes the usual prefixing of Esc */
  1772. #ifdef KEY_EVENT_DIAGNOSTICS
  1773. debug(" - arrow key");
  1774. #endif
  1775. use_ucsoutput = false;
  1776. goto done;
  1777. }
  1778. if (num_keypad_key) {
  1779. end = 1 + format_numeric_keypad_key(
  1780. output+1, inst->term, num_keypad_key,
  1781. event->state & GDK_SHIFT_MASK,
  1782. event->state & GDK_CONTROL_MASK);
  1783. #ifdef KEY_EVENT_DIAGNOSTICS
  1784. debug(" - numeric keypad key");
  1785. #endif
  1786. if (end == 1 && num_keypad_key == '\r') {
  1787. /* Keypad Enter, lacking any other translation,
  1788. * becomes the same special Return code as normal
  1789. * Return. */
  1790. end = return_key(inst, output, &special);
  1791. use_ucsoutput = false;
  1792. }
  1793. use_ucsoutput = false;
  1794. goto done;
  1795. }
  1796. goto done;
  1797. }
  1798. done:
  1799. if (end-start > 0) {
  1800. if (special) {
  1801. #ifdef KEY_EVENT_DIAGNOSTICS
  1802. char *string_string = dupstr("");
  1803. int i;
  1804. for (i = start; i < end; i++) {
  1805. char *old = string_string;
  1806. string_string = dupprintf("%s%s%02x", string_string,
  1807. string_string[0] ? " " : "",
  1808. (unsigned)output[i] & 0xFF);
  1809. sfree(old);
  1810. }
  1811. debug(" - final output, special, generic encoding = [%s]\n",
  1812. string_string);
  1813. sfree(string_string);
  1814. #endif
  1815. /*
  1816. * For special control characters, the character set
  1817. * should never matter.
  1818. */
  1819. output[end] = '\0'; /* NUL-terminate */
  1820. generated_something = true;
  1821. term_keyinput(inst->term, -1, output+start, -2);
  1822. } else if (!inst->direct_to_font) {
  1823. if (!use_ucsoutput) {
  1824. #ifdef KEY_EVENT_DIAGNOSTICS
  1825. char *string_string = dupstr("");
  1826. int i;
  1827. for (i = start; i < end; i++) {
  1828. char *old = string_string;
  1829. string_string = dupprintf("%s%s%02x", string_string,
  1830. string_string[0] ? " " : "",
  1831. (unsigned)output[i] & 0xFF);
  1832. sfree(old);
  1833. }
  1834. debug(" - final output in %s = [%s]\n",
  1835. charset_to_localenc(output_charset), string_string);
  1836. sfree(string_string);
  1837. #endif
  1838. generated_something = true;
  1839. term_keyinput(inst->term, output_charset,
  1840. output+start, end-start);
  1841. } else {
  1842. #ifdef KEY_EVENT_DIAGNOSTICS
  1843. char *string_string = dupstr("");
  1844. int i;
  1845. for (i = start; i < end; i++) {
  1846. char *old = string_string;
  1847. string_string = dupprintf("%s%s%04x", string_string,
  1848. string_string[0] ? " " : "",
  1849. (unsigned)ucsoutput[i]);
  1850. sfree(old);
  1851. }
  1852. debug(" - final output in Unicode = [%s]\n",
  1853. string_string);
  1854. sfree(string_string);
  1855. #endif
  1856. /*
  1857. * We generated our own Unicode key data from the
  1858. * keysym, so use that instead.
  1859. */
  1860. generated_something = true;
  1861. term_keyinputw(inst->term, ucsoutput+start, end-start);
  1862. }
  1863. } else {
  1864. /*
  1865. * In direct-to-font mode, we just send the string
  1866. * exactly as we received it.
  1867. */
  1868. #ifdef KEY_EVENT_DIAGNOSTICS
  1869. char *string_string = dupstr("");
  1870. int i;
  1871. for (i = start; i < end; i++) {
  1872. char *old = string_string;
  1873. string_string = dupprintf("%s%s%02x", string_string,
  1874. string_string[0] ? " " : "",
  1875. (unsigned)output[i] & 0xFF);
  1876. sfree(old);
  1877. }
  1878. debug(" - final output in direct-to-font encoding = [%s]\n",
  1879. string_string);
  1880. sfree(string_string);
  1881. #endif
  1882. generated_something = true;
  1883. term_keyinput(inst->term, -1, output+start, end-start);
  1884. }
  1885. show_mouseptr(inst, false);
  1886. }
  1887. if (generated_something)
  1888. key_pressed(inst);
  1889. return true;
  1890. }
  1891. #if GTK_CHECK_VERSION(2,0,0)
  1892. void input_method_commit_event(GtkIMContext *imc, gchar *str, gpointer data)
  1893. {
  1894. GtkFrontend *inst = (GtkFrontend *)data;
  1895. #ifdef KEY_EVENT_DIAGNOSTICS
  1896. char *string_string = dupstr("");
  1897. int i;
  1898. for (i = 0; str[i]; i++) {
  1899. char *old = string_string;
  1900. string_string = dupprintf("%s%s%02x", string_string,
  1901. string_string[0] ? " " : "",
  1902. (unsigned)str[i] & 0xFF);
  1903. sfree(old);
  1904. }
  1905. debug(" - IM commit event in UTF-8 = [%s]\n", string_string);
  1906. sfree(string_string);
  1907. #endif
  1908. term_keyinput(inst->term, CS_UTF8, str, strlen(str));
  1909. show_mouseptr(inst, false);
  1910. key_pressed(inst);
  1911. }
  1912. void input_method_preedit_start_event(GtkIMContext *imc, gpointer data)
  1913. {
  1914. #ifdef KEY_EVENT_DIAGNOSTICS
  1915. debug(" - IM preedit-start event\n");
  1916. #endif
  1917. }
  1918. void input_method_preedit_changed_event(GtkIMContext *imc, gpointer data)
  1919. {
  1920. GtkFrontend *inst = (GtkFrontend *)data;
  1921. gint cursor_pos;
  1922. gchar *preedit_string;
  1923. #ifdef KEY_EVENT_DIAGNOSTICS
  1924. char *string_string = dupstr("");
  1925. int i;
  1926. #endif
  1927. gtk_im_context_get_preedit_string(imc, &preedit_string, NULL, &cursor_pos);
  1928. #ifdef KEY_EVENT_DIAGNOSTICS
  1929. for (i = 0; preedit_string[i]; i++) {
  1930. char *old = string_string;
  1931. string_string = dupprintf("%s%s%02x", string_string,
  1932. string_string[0] ? " " : "",
  1933. (unsigned)preedit_string[i] & 0xFF);
  1934. sfree(old);
  1935. }
  1936. debug(" - IM preedit-changed event in UTF-8 = [%s] cursor_pos=%d\n",
  1937. string_string, (int)cursor_pos);
  1938. sfree(string_string);
  1939. #endif
  1940. term_set_preedit_text(inst->term, preedit_string);
  1941. g_free(preedit_string);
  1942. }
  1943. void input_method_preedit_end_event(GtkIMContext *imc, gpointer data)
  1944. {
  1945. GtkFrontend *inst = (GtkFrontend *)data;
  1946. #ifdef KEY_EVENT_DIAGNOSTICS
  1947. debug(" - IM preedit-end event\n");
  1948. #endif
  1949. term_set_preedit_text(inst->term, NULL);
  1950. }
  1951. #endif
  1952. #define SCROLL_INCREMENT_LINES 5
  1953. #if GTK_CHECK_VERSION(3,4,0)
  1954. gboolean scroll_internal(GtkFrontend *inst, gdouble xdelta, gdouble ydelta,
  1955. guint state, gdouble ex, gdouble ey)
  1956. {
  1957. int x, y;
  1958. bool shift, ctrl, alt, raw_mouse_mode;
  1959. show_mouseptr(inst, true);
  1960. shift = state & GDK_SHIFT_MASK;
  1961. ctrl = state & GDK_CONTROL_MASK;
  1962. alt = state & inst->meta_mod_mask;
  1963. x = (ex - inst->window_border) / inst->font_width;
  1964. y = (ey - inst->window_border) / inst->font_height;
  1965. raw_mouse_mode = (inst->send_raw_mouse &&
  1966. !(shift && conf_get_bool(inst->conf,
  1967. CONF_mouse_override)));
  1968. inst->cumulative_vscroll += ydelta * SCROLL_INCREMENT_LINES;
  1969. if (!raw_mouse_mode) {
  1970. int scroll_lines = (int)inst->cumulative_vscroll; /* rounds toward 0 */
  1971. if (scroll_lines) {
  1972. term_scroll(inst->term, 0, scroll_lines);
  1973. inst->cumulative_vscroll -= scroll_lines;
  1974. }
  1975. return true;
  1976. } else {
  1977. int scroll_events = (int)(inst->cumulative_vscroll /
  1978. SCROLL_INCREMENT_LINES);
  1979. if (scroll_events) {
  1980. int button;
  1981. inst->cumulative_vscroll -= scroll_events * SCROLL_INCREMENT_LINES;
  1982. if (scroll_events > 0) {
  1983. button = MBT_WHEEL_DOWN;
  1984. } else {
  1985. button = MBT_WHEEL_UP;
  1986. scroll_events = -scroll_events;
  1987. }
  1988. while (scroll_events-- > 0) {
  1989. term_mouse(inst->term, button, translate_button(button),
  1990. MA_CLICK, x, y, shift, ctrl, alt);
  1991. }
  1992. }
  1993. /*
  1994. * Now do the same for horizontal scrolling. But because we
  1995. * _only_ use that for passing through to mouse reporting, we
  1996. * don't even collect the scroll deltas while not in
  1997. * raw_mouse_mode. (Otherwise there would likely be a huge
  1998. * unexpected lurch when raw_mouse_mode was enabled!)
  1999. */
  2000. inst->cumulative_hscroll += xdelta * SCROLL_INCREMENT_LINES;
  2001. scroll_events = (int)(inst->cumulative_hscroll /
  2002. SCROLL_INCREMENT_LINES);
  2003. if (scroll_events) {
  2004. int button;
  2005. inst->cumulative_hscroll -= scroll_events * SCROLL_INCREMENT_LINES;
  2006. if (scroll_events > 0) {
  2007. button = MBT_WHEEL_RIGHT;
  2008. } else {
  2009. button = MBT_WHEEL_LEFT;
  2010. scroll_events = -scroll_events;
  2011. }
  2012. while (scroll_events-- > 0) {
  2013. term_mouse(inst->term, button, translate_button(button),
  2014. MA_CLICK, x, y, shift, ctrl, alt);
  2015. }
  2016. }
  2017. return true;
  2018. }
  2019. }
  2020. #endif
  2021. static gboolean button_internal(GtkFrontend *inst, GdkEventButton *event)
  2022. {
  2023. bool shift, ctrl, alt, raw_mouse_mode;
  2024. int x, y, button, act;
  2025. /* Remember the timestamp. */
  2026. inst->input_event_time = event->time;
  2027. noise_ultralight(NOISE_SOURCE_MOUSEBUTTON, event->button);
  2028. show_mouseptr(inst, true);
  2029. shift = event->state & GDK_SHIFT_MASK;
  2030. ctrl = event->state & GDK_CONTROL_MASK;
  2031. alt = event->state & inst->meta_mod_mask;
  2032. raw_mouse_mode = (inst->send_raw_mouse &&
  2033. !(shift && conf_get_bool(inst->conf,
  2034. CONF_mouse_override)));
  2035. if (!raw_mouse_mode) {
  2036. if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
  2037. term_scroll(inst->term, 0, -SCROLL_INCREMENT_LINES);
  2038. return true;
  2039. }
  2040. if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
  2041. term_scroll(inst->term, 0, +SCROLL_INCREMENT_LINES);
  2042. return true;
  2043. }
  2044. }
  2045. if (event->button == 3 && ctrl) {
  2046. /* Just in case this happened in mid-select */
  2047. term_cancel_selection_drag(inst->term);
  2048. #if GTK_CHECK_VERSION(3,22,0)
  2049. gtk_menu_popup_at_pointer(GTK_MENU(inst->menu), (GdkEvent *)event);
  2050. #else
  2051. gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
  2052. event->button, event->time);
  2053. #endif
  2054. return true;
  2055. }
  2056. if (event->button == 1)
  2057. button = MBT_LEFT;
  2058. else if (event->button == 2)
  2059. button = MBT_MIDDLE;
  2060. else if (event->button == 3)
  2061. button = MBT_RIGHT;
  2062. else if (event->button == 4)
  2063. button = MBT_WHEEL_UP;
  2064. else if (event->button == 5)
  2065. button = MBT_WHEEL_DOWN;
  2066. else
  2067. return false; /* don't even know what button! */
  2068. switch (event->type) {
  2069. case GDK_BUTTON_PRESS: act = MA_CLICK; break;
  2070. case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
  2071. case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
  2072. case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
  2073. default: return false; /* don't know this event type */
  2074. }
  2075. if (raw_mouse_mode && act != MA_CLICK && act != MA_RELEASE)
  2076. return true; /* we ignore these in raw mouse mode */
  2077. x = (event->x - inst->window_border) / inst->font_width;
  2078. y = (event->y - inst->window_border) / inst->font_height;
  2079. term_mouse(inst->term, button, translate_button(button), act,
  2080. x, y, shift, ctrl, alt);
  2081. return true;
  2082. }
  2083. gboolean button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
  2084. {
  2085. GtkFrontend *inst = (GtkFrontend *)data;
  2086. return button_internal(inst, event);
  2087. }
  2088. #if GTK_CHECK_VERSION(2,0,0)
  2089. /*
  2090. * In GTK 2, mouse wheel events have become a new type of event.
  2091. * This handler translates them back into button-4 and button-5
  2092. * presses so that I don't have to change my old code too much :-)
  2093. */
  2094. gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event, gpointer data)
  2095. {
  2096. GtkFrontend *inst = (GtkFrontend *)data;
  2097. GdkScrollDirection dir;
  2098. #if GTK_CHECK_VERSION(3,4,0)
  2099. gdouble dx, dy;
  2100. if (gdk_event_get_scroll_deltas((GdkEvent *)event, &dx, &dy)) {
  2101. return scroll_internal(inst, dx, dy, event->state, event->x, event->y);
  2102. } else if (!gdk_event_get_scroll_direction((GdkEvent *)event, &dir)) {
  2103. return false;
  2104. }
  2105. #else
  2106. dir = event->direction;
  2107. #endif
  2108. guint button;
  2109. GdkEventButton *event_button;
  2110. gboolean ret;
  2111. if (dir == GDK_SCROLL_UP)
  2112. button = 4;
  2113. else if (dir == GDK_SCROLL_DOWN)
  2114. button = 5;
  2115. else
  2116. return false;
  2117. event_button = (GdkEventButton *)gdk_event_new(GDK_BUTTON_PRESS);
  2118. event_button->window = g_object_ref(event->window);
  2119. event_button->send_event = event->send_event;
  2120. event_button->time = event->time;
  2121. event_button->x = event->x;
  2122. event_button->y = event->y;
  2123. event_button->axes = NULL;
  2124. event_button->state = event->state;
  2125. event_button->button = button;
  2126. event_button->device = g_object_ref(event->device);
  2127. event_button->x_root = event->x_root;
  2128. event_button->y_root = event->y_root;
  2129. ret = button_internal(inst, event_button);
  2130. gdk_event_free((GdkEvent *)event_button);
  2131. return ret;
  2132. }
  2133. #endif
  2134. gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
  2135. {
  2136. GtkFrontend *inst = (GtkFrontend *)data;
  2137. bool shift, ctrl, alt;
  2138. Mouse_Action action = MA_DRAG;
  2139. Mouse_Button button = MBT_NOTHING;
  2140. int x, y;
  2141. /* Remember the timestamp. */
  2142. inst->input_event_time = event->time;
  2143. noise_ultralight(NOISE_SOURCE_MOUSEPOS,
  2144. ((uint32_t)event->x << 16) | (uint32_t)event->y);
  2145. show_mouseptr(inst, true);
  2146. shift = event->state & GDK_SHIFT_MASK;
  2147. ctrl = event->state & GDK_CONTROL_MASK;
  2148. alt = event->state & inst->meta_mod_mask;
  2149. if (event->state & GDK_BUTTON1_MASK)
  2150. button = MBT_LEFT;
  2151. else if (event->state & GDK_BUTTON2_MASK)
  2152. button = MBT_MIDDLE;
  2153. else if (event->state & GDK_BUTTON3_MASK)
  2154. button = MBT_RIGHT;
  2155. else
  2156. action = MA_MOVE;
  2157. x = (event->x - inst->window_border) / inst->font_width;
  2158. y = (event->y - inst->window_border) / inst->font_height;
  2159. term_mouse(inst->term, button, translate_button(button), action,
  2160. x, y, shift, ctrl, alt);
  2161. return true;
  2162. }
  2163. static void key_pressed(GtkFrontend *inst)
  2164. {
  2165. /*
  2166. * If our child process has exited but not closed, terminate on
  2167. * any keypress.
  2168. *
  2169. * This is a UI feature specific to GTK PuTTY, because GTK PuTTY
  2170. * will (at least sometimes) be running under X, and under X the
  2171. * window manager is sometimes absent (very occasionally on
  2172. * purpose, more usually temporarily because it's crashed). So
  2173. * it's useful to have a way to close an application window
  2174. * without depending on protocols like WM_DELETE_WINDOW that are
  2175. * typically generated by the WM (e.g. in response to a close
  2176. * button in the window frame).
  2177. */
  2178. if (inst->exited)
  2179. gtk_widget_destroy(inst->window);
  2180. }
  2181. static void exit_callback(void *vctx)
  2182. {
  2183. GtkFrontend *inst = (GtkFrontend *)vctx;
  2184. int exitcode, close_on_exit;
  2185. if (!inst->exited &&
  2186. (exitcode = backend_exitcode(inst->backend)) >= 0) {
  2187. destroy_inst_connection(inst);
  2188. close_on_exit = conf_get_int(inst->conf, CONF_close_on_exit);
  2189. if (close_on_exit == FORCE_ON ||
  2190. (close_on_exit == AUTO && exitcode == 0)) {
  2191. gtk_widget_destroy(inst->window);
  2192. }
  2193. }
  2194. }
  2195. static void gtk_seat_notify_remote_exit(Seat *seat)
  2196. {
  2197. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  2198. queue_toplevel_callback(exit_callback, inst);
  2199. }
  2200. static void destroy_inst_connection(GtkFrontend *inst)
  2201. {
  2202. inst->exited = true;
  2203. if (inst->ldisc) {
  2204. ldisc_free(inst->ldisc);
  2205. inst->ldisc = NULL;
  2206. }
  2207. if (inst->backend) {
  2208. backend_free(inst->backend);
  2209. inst->backend = NULL;
  2210. }
  2211. if (inst->term)
  2212. term_provide_backend(inst->term, NULL);
  2213. if (inst->menu) {
  2214. seat_update_specials_menu(&inst->seat);
  2215. gtk_widget_set_sensitive(inst->restartitem, true);
  2216. }
  2217. }
  2218. static void delete_inst(GtkFrontend *inst)
  2219. {
  2220. int dialog_slot;
  2221. for (dialog_slot = 0; dialog_slot < DIALOG_SLOT_LIMIT; dialog_slot++) {
  2222. if (inst->dialogs[dialog_slot]) {
  2223. gtk_widget_destroy(inst->dialogs[dialog_slot]);
  2224. inst->dialogs[dialog_slot] = NULL;
  2225. }
  2226. }
  2227. if (inst->window) {
  2228. gtk_widget_destroy(inst->window);
  2229. inst->window = NULL;
  2230. }
  2231. if (inst->menu) {
  2232. gtk_widget_destroy(inst->menu);
  2233. inst->menu = NULL;
  2234. }
  2235. destroy_inst_connection(inst);
  2236. if (inst->term) {
  2237. term_free(inst->term);
  2238. inst->term = NULL;
  2239. }
  2240. if (inst->conf) {
  2241. conf_free(inst->conf);
  2242. inst->conf = NULL;
  2243. }
  2244. if (inst->logctx) {
  2245. log_free(inst->logctx);
  2246. inst->logctx = NULL;
  2247. }
  2248. #if GTK_CHECK_VERSION(2,0,0)
  2249. if (inst->trust_sigil_pb) {
  2250. g_object_unref(G_OBJECT(inst->trust_sigil_pb));
  2251. inst->trust_sigil_pb = NULL;
  2252. }
  2253. #else
  2254. if (inst->trust_sigil_pm) {
  2255. gdk_pixmap_unref(inst->trust_sigil_pm);
  2256. inst->trust_sigil_pm = NULL;
  2257. }
  2258. #endif
  2259. #ifdef JUST_USE_GTK_CLIPBOARD_UTF8
  2260. /*
  2261. * Clear up any in-flight clipboard_data_instances. We can't
  2262. * actually _free_ them, but we detach them from the inst that's
  2263. * about to be destroyed.
  2264. */
  2265. while (inst->cdi_headtail.next != &inst->cdi_headtail) {
  2266. struct clipboard_data_instance *cdi = inst->cdi_headtail.next;
  2267. cdi->state = NULL;
  2268. cdi->next->prev = cdi->prev;
  2269. cdi->prev->next = cdi->next;
  2270. cdi->next = cdi->prev = cdi;
  2271. }
  2272. #endif
  2273. /*
  2274. * Delete any top-level callbacks associated with inst, which
  2275. * would otherwise become stale-pointer dereferences waiting to
  2276. * happen. We do this last, because some of the above cleanups
  2277. * (notably shutting down the backend) might themelves queue such
  2278. * callbacks, so we need to make sure they don't do that _after_
  2279. * we're supposed to have cleaned everything up.
  2280. */
  2281. delete_callbacks_for_context(inst);
  2282. eventlogstuff_free(inst->eventlogstuff);
  2283. sfree(inst);
  2284. }
  2285. void destroy(GtkWidget *widget, gpointer data)
  2286. {
  2287. GtkFrontend *inst = (GtkFrontend *)data;
  2288. inst->window = NULL;
  2289. delete_inst(inst);
  2290. session_window_closed();
  2291. }
  2292. gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
  2293. {
  2294. GtkFrontend *inst = (GtkFrontend *)data;
  2295. #if GTK_CHECK_VERSION(2,0,0)
  2296. if (event->in)
  2297. gtk_im_context_focus_in(inst->imc);
  2298. else
  2299. gtk_im_context_focus_out(inst->imc);
  2300. #endif
  2301. term_set_focus(inst->term, event->in);
  2302. term_update(inst->term);
  2303. show_mouseptr(inst, true);
  2304. return false;
  2305. }
  2306. static void gtk_seat_set_busy_status(Seat *seat, BusyStatus status)
  2307. {
  2308. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  2309. inst->busy_status = status;
  2310. update_mouseptr(inst);
  2311. }
  2312. static void gtkwin_set_raw_mouse_mode(TermWin *tw, bool activate)
  2313. {
  2314. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  2315. inst->send_raw_mouse = activate;
  2316. }
  2317. static void gtkwin_set_raw_mouse_mode_pointer(TermWin *tw, bool activate)
  2318. {
  2319. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  2320. inst->pointer_indicates_raw_mouse = activate;
  2321. update_mouseptr(inst);
  2322. }
  2323. #if GTK_CHECK_VERSION(2,0,0)
  2324. static void compute_whole_window_size(GtkFrontend *inst,
  2325. int wchars, int hchars,
  2326. int *wpix, int *hpix);
  2327. static void gtkwin_deny_term_resize(void *vctx)
  2328. {
  2329. GtkFrontend *inst = (GtkFrontend *)vctx;
  2330. drawing_area_setup_simple(inst);
  2331. }
  2332. #endif
  2333. static void gtkwin_timer(void *vctx, unsigned long now)
  2334. {
  2335. GtkFrontend *inst = (GtkFrontend *)vctx;
  2336. if (inst->win_resize_pending && now == inst->win_resize_timeout) {
  2337. if (inst->term_resize_notification_required)
  2338. term_resize_request_completed(inst->term);
  2339. inst->win_resize_pending = false;
  2340. }
  2341. }
  2342. static void request_resize_internal(GtkFrontend *inst, bool from_terminal,
  2343. int w, int h)
  2344. {
  2345. #if GTK_CHECK_VERSION(2,0,0)
  2346. /*
  2347. * Initial check: don't even try to resize a window if it's in one
  2348. * of the states that make that impossible. This includes being
  2349. * maximised; being full-screen (if we ever implement that); or
  2350. * being in various tiled states.
  2351. *
  2352. * On X11, the effect of trying to resize the window when it can't
  2353. * be resized should be that the window manager sends us a
  2354. * synthetic ConfigureNotify event restating our existing size
  2355. * (ICCCM section 4.1.5 "Configuring the Window"). That's awkward
  2356. * to deal with, but not impossible; so for X11 alone, we might
  2357. * not bother with this check.
  2358. *
  2359. * (In any case, X11 has other reasons why a window resize might
  2360. * be rejected, which this enumeration can't be aware of in any
  2361. * case. For example, if your window manager is a tiling one, then
  2362. * all your windows are _de facto_ tiled, but not _de jure_ in a
  2363. * way that GDK will know about. So we have to handle those
  2364. * synthetic ConfigureNotifies in any case.)
  2365. *
  2366. * On Wayland, as of GTK 3.24.20, the effects are much worse: it
  2367. * looks to me as if GTK stops ever sending us "draw" events, or
  2368. * even a size_allocate, so the display locks up completely until
  2369. * you toggle the maximised state of the window by some other
  2370. * means. So it's worth checking!
  2371. */
  2372. GdkWindow *gdkwin = gtk_widget_get_window(inst->window);
  2373. if (gdkwin) {
  2374. GdkWindowState state = gdk_window_get_state(gdkwin);
  2375. if (state & (GDK_WINDOW_STATE_MAXIMIZED |
  2376. GDK_WINDOW_STATE_FULLSCREEN |
  2377. #if GTK_CHECK_VERSION(3,10,0)
  2378. GDK_WINDOW_STATE_TILED |
  2379. #endif
  2380. #if GTK_CHECK_VERSION(3,22,23)
  2381. GDK_WINDOW_STATE_TOP_TILED |
  2382. GDK_WINDOW_STATE_RIGHT_TILED |
  2383. GDK_WINDOW_STATE_BOTTOM_TILED |
  2384. GDK_WINDOW_STATE_LEFT_TILED |
  2385. #endif
  2386. 0)) {
  2387. queue_toplevel_callback(gtkwin_deny_term_resize, inst);
  2388. if (from_terminal)
  2389. term_resize_request_completed(inst->term);
  2390. return;
  2391. }
  2392. }
  2393. #endif
  2394. #if !GTK_CHECK_VERSION(3,0,0)
  2395. int large_x, large_y;
  2396. int offset_x, offset_y;
  2397. int area_x, area_y;
  2398. GtkRequisition inner, outer;
  2399. /*
  2400. * This is a heinous hack dreamed up by the gnome-terminal
  2401. * people to get around a limitation in gtk. The problem is
  2402. * that in order to set the size correctly we really need to be
  2403. * calling gtk_window_resize - but that needs to know the size
  2404. * of the _whole window_, not the drawing area. So what we do
  2405. * is to set an artificially huge size request on the drawing
  2406. * area, recompute the resulting size request on the window,
  2407. * and look at the difference between the two. That gives us
  2408. * the x and y offsets we need to translate drawing area size
  2409. * into window size for real, and then we call
  2410. * gtk_window_resize.
  2411. */
  2412. /*
  2413. * We start by retrieving the current size of the whole window.
  2414. * Adding a bit to _that_ will give us a value we can use as a
  2415. * bogus size request which guarantees to be bigger than the
  2416. * current size of the drawing area.
  2417. */
  2418. get_window_pixel_size(inst, &large_x, &large_y);
  2419. large_x += 32;
  2420. large_y += 32;
  2421. gtk_widget_set_size_request(inst->area, large_x, large_y);
  2422. gtk_widget_size_request(inst->area, &inner);
  2423. gtk_widget_size_request(inst->window, &outer);
  2424. offset_x = outer.width - inner.width;
  2425. offset_y = outer.height - inner.height;
  2426. area_x = inst->font_width * w + 2*inst->window_border;
  2427. area_y = inst->font_height * h + 2*inst->window_border;
  2428. /*
  2429. * Now we must set the size request on the drawing area back to
  2430. * something sensible before we commit the real resize. Best
  2431. * way to do this, I think, is to set it to what the size is
  2432. * really going to end up being.
  2433. */
  2434. gtk_widget_set_size_request(inst->area, area_x, area_y);
  2435. #if GTK_CHECK_VERSION(2,0,0)
  2436. gtk_window_resize(GTK_WINDOW(inst->window),
  2437. area_x + offset_x, area_y + offset_y);
  2438. #else
  2439. gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
  2440. /*
  2441. * I can no longer remember what this call to
  2442. * gtk_container_dequeue_resize_handler is for. It was
  2443. * introduced in r3092 with no comment, and the commit log
  2444. * message was uninformative. I'm _guessing_ its purpose is to
  2445. * prevent gratuitous resize processing on the window given
  2446. * that we're about to resize it anyway, but I have no idea
  2447. * why that's so incredibly vital.
  2448. *
  2449. * I've tried removing the call, and nothing seems to go
  2450. * wrong. I've backtracked to r3092 and tried removing the
  2451. * call there, and still nothing goes wrong. So I'm going to
  2452. * adopt the working hypothesis that it's superfluous; I won't
  2453. * actually remove it from the GTK 1.2 code, but I won't
  2454. * attempt to replicate its functionality in the GTK 2 code
  2455. * above.
  2456. */
  2457. gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
  2458. gdk_window_resize(gtk_widget_get_window(inst->window),
  2459. area_x + offset_x, area_y + offset_y);
  2460. #endif
  2461. #else /* GTK_CHECK_VERSION(3,0,0) */
  2462. int wp, hp;
  2463. compute_whole_window_size(inst, w, h, &wp, &hp);
  2464. gtk_window_resize(GTK_WINDOW(inst->window), wp, hp);
  2465. #endif
  2466. inst->win_resize_pending = true;
  2467. inst->term_resize_notification_required = from_terminal;
  2468. inst->win_resize_timeout = schedule_timer(
  2469. WIN_RESIZE_TIMEOUT, gtkwin_timer, inst);
  2470. }
  2471. static void gtkwin_request_resize(TermWin *tw, int w, int h)
  2472. {
  2473. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  2474. request_resize_internal(inst, true, w, h);
  2475. }
  2476. #if GTK_CHECK_VERSION(3,0,0)
  2477. char *colour_to_css(const GdkColor *col)
  2478. {
  2479. GdkRGBA rgba;
  2480. rgba.red = col->red / 65535.0;
  2481. rgba.green = col->green / 65535.0;
  2482. rgba.blue = col->blue / 65535.0;
  2483. rgba.alpha = 1.0;
  2484. return gdk_rgba_to_string(&rgba);
  2485. }
  2486. #endif
  2487. void set_gtk_widget_background(GtkWidget *widget, const GdkColor *col)
  2488. {
  2489. #if GTK_CHECK_VERSION(3,0,0)
  2490. GtkCssProvider *provider = gtk_css_provider_new();
  2491. char *col_css = colour_to_css(col);
  2492. char *data = dupprintf(
  2493. "#drawing-area, #top-level { background-color: %s; }\n", col_css);
  2494. gtk_css_provider_load_from_data(provider, data, -1, NULL);
  2495. GtkStyleContext *context = gtk_widget_get_style_context(widget);
  2496. gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER(provider),
  2497. GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  2498. free(data);
  2499. free(col_css);
  2500. #else
  2501. if (gtk_widget_get_window(widget)) {
  2502. /* For GTK1, which doesn't have a 'const' on
  2503. * gdk_window_set_background's second parameter type. */
  2504. GdkColor col_mutable = *col;
  2505. gdk_window_set_background(gtk_widget_get_window(widget), &col_mutable);
  2506. }
  2507. #endif
  2508. }
  2509. void set_window_background(GtkFrontend *inst)
  2510. {
  2511. if (inst->area)
  2512. set_gtk_widget_background(GTK_WIDGET(inst->area), &inst->cols[258]);
  2513. if (inst->window)
  2514. set_gtk_widget_background(GTK_WIDGET(inst->window), &inst->cols[258]);
  2515. }
  2516. static void gtkwin_palette_set(TermWin *tw, unsigned start, unsigned ncolours,
  2517. const rgb *colours)
  2518. {
  2519. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  2520. assert(start <= OSC4_NCOLOURS);
  2521. assert(ncolours <= OSC4_NCOLOURS - start);
  2522. #if !GTK_CHECK_VERSION(3,0,0)
  2523. if (!inst->colmap) {
  2524. inst->colmap = gdk_colormap_get_system();
  2525. } else {
  2526. gdk_colormap_free_colors(inst->colmap, inst->cols, OSC4_NCOLOURS);
  2527. }
  2528. #endif
  2529. for (unsigned i = 0; i < ncolours; i++) {
  2530. const rgb *in = &colours[i];
  2531. GdkColor *out = &inst->cols[start + i];
  2532. out->red = in->r * 0x0101;
  2533. out->green = in->g * 0x0101;
  2534. out->blue = in->b * 0x0101;
  2535. }
  2536. #if !GTK_CHECK_VERSION(3,0,0)
  2537. {
  2538. gboolean success[OSC4_NCOLOURS];
  2539. gdk_colormap_alloc_colors(inst->colmap, inst->cols + start,
  2540. ncolours, false, true, success);
  2541. for (unsigned i = 0; i < ncolours; i++) {
  2542. if (!success[i])
  2543. g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
  2544. appname, start + i,
  2545. conf_get_int_int(inst->conf, CONF_colours, i*3+0),
  2546. conf_get_int_int(inst->conf, CONF_colours, i*3+1),
  2547. conf_get_int_int(inst->conf, CONF_colours, i*3+2));
  2548. }
  2549. }
  2550. #endif
  2551. if (start <= OSC4_COLOUR_bg && OSC4_COLOUR_bg < start + ncolours) {
  2552. /* Default Background has changed, so ensure that space between text
  2553. * area and window border is refreshed. */
  2554. set_window_background(inst);
  2555. if (inst->area && gtk_widget_get_window(inst->area)) {
  2556. draw_backing_rect(inst);
  2557. gtk_widget_queue_draw(inst->area);
  2558. }
  2559. }
  2560. }
  2561. static void gtkwin_palette_get_overrides(TermWin *tw, Terminal *term)
  2562. {
  2563. /* GTK has no analogue of Windows's 'standard system colours', so GTK PuTTY
  2564. * has no config option to override the normally configured colours from
  2565. * it */
  2566. }
  2567. static struct clipboard_state *clipboard_from_atom(
  2568. GtkFrontend *inst, GdkAtom atom)
  2569. {
  2570. int i;
  2571. for (i = 0; i < N_CLIPBOARDS; i++) {
  2572. struct clipboard_state *state = &inst->clipstates[i];
  2573. if (state->inst == inst && state->atom == atom)
  2574. return state;
  2575. }
  2576. return NULL;
  2577. }
  2578. #ifdef JUST_USE_GTK_CLIPBOARD_UTF8
  2579. /* ----------------------------------------------------------------------
  2580. * Clipboard handling, using the high-level GtkClipboard interface in
  2581. * as hands-off a way as possible. We write and read the clipboard as
  2582. * UTF-8 text, and let GTK deal with converting to any other text
  2583. * formats it feels like.
  2584. */
  2585. void set_clipboard_atom(GtkFrontend *inst, int clipboard, GdkAtom atom)
  2586. {
  2587. struct clipboard_state *state = &inst->clipstates[clipboard];
  2588. state->inst = inst;
  2589. state->clipboard = clipboard;
  2590. state->atom = atom;
  2591. if (state->atom != GDK_NONE) {
  2592. state->gtkclipboard = gtk_clipboard_get_for_display(
  2593. gdk_display_get_default(), state->atom);
  2594. g_object_set_data(G_OBJECT(state->gtkclipboard), "user-data", state);
  2595. } else {
  2596. state->gtkclipboard = NULL;
  2597. }
  2598. }
  2599. int init_clipboard(GtkFrontend *inst)
  2600. {
  2601. set_clipboard_atom(inst, CLIP_PRIMARY, GDK_SELECTION_PRIMARY);
  2602. set_clipboard_atom(inst, CLIP_CLIPBOARD, clipboard_atom);
  2603. return true;
  2604. }
  2605. static void clipboard_provide_data(GtkClipboard *clipboard,
  2606. GtkSelectionData *selection_data,
  2607. guint info, gpointer data)
  2608. {
  2609. struct clipboard_data_instance *cdi =
  2610. (struct clipboard_data_instance *)data;
  2611. if (cdi->state && cdi->state->current_cdi == cdi) {
  2612. gtk_selection_data_set_text(selection_data, cdi->pasteout_data_utf8,
  2613. cdi->pasteout_data_utf8_len);
  2614. }
  2615. }
  2616. static void clipboard_clear(GtkClipboard *clipboard, gpointer data)
  2617. {
  2618. struct clipboard_data_instance *cdi =
  2619. (struct clipboard_data_instance *)data;
  2620. if (cdi->state && cdi->state->current_cdi == cdi) {
  2621. if (cdi->state->inst && cdi->state->inst->term) {
  2622. term_lost_clipboard_ownership(cdi->state->inst->term,
  2623. cdi->state->clipboard);
  2624. }
  2625. cdi->state->current_cdi = NULL;
  2626. }
  2627. sfree(cdi->pasteout_data_utf8);
  2628. cdi->next->prev = cdi->prev;
  2629. cdi->prev->next = cdi->next;
  2630. sfree(cdi);
  2631. }
  2632. static void gtkwin_clip_write(
  2633. TermWin *tw, int clipboard, wchar_t *data, int *attr,
  2634. truecolour *truecolour, int len, bool must_deselect)
  2635. {
  2636. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  2637. struct clipboard_state *state = &inst->clipstates[clipboard];
  2638. struct clipboard_data_instance *cdi;
  2639. if (inst->direct_to_font) {
  2640. /* In this clipboard mode, we just can't paste if we're in
  2641. * direct-to-font mode. Fortunately, that shouldn't be
  2642. * important, because we'll only use this clipboard handling
  2643. * code on systems where that kind of font doesn't exist
  2644. * anyway. */
  2645. return;
  2646. }
  2647. if (!state->gtkclipboard)
  2648. return;
  2649. cdi = snew(struct clipboard_data_instance);
  2650. cdi->state = state;
  2651. state->current_cdi = cdi;
  2652. cdi->pasteout_data_utf8 = snewn(len*6, char);
  2653. cdi->prev = inst->cdi_headtail.prev;
  2654. cdi->next = &inst->cdi_headtail;
  2655. cdi->next->prev = cdi;
  2656. cdi->prev->next = cdi;
  2657. {
  2658. const wchar_t *tmp = data;
  2659. int tmplen = len;
  2660. cdi->pasteout_data_utf8_len =
  2661. charset_from_unicode(&tmp, &tmplen, cdi->pasteout_data_utf8,
  2662. len*6, CS_UTF8, NULL, NULL, 0);
  2663. }
  2664. /*
  2665. * It would be nice to just call gtk_clipboard_set_text() in place
  2666. * of all of the faffing below. Unfortunately, that won't give me
  2667. * access to the clipboard-clear event, which we use to visually
  2668. * deselect text in the terminal.
  2669. */
  2670. {
  2671. GtkTargetList *targetlist;
  2672. GtkTargetEntry *targettable;
  2673. gint n_targets;
  2674. targetlist = gtk_target_list_new(NULL, 0);
  2675. gtk_target_list_add_text_targets(targetlist, 0);
  2676. targettable = gtk_target_table_new_from_list(targetlist, &n_targets);
  2677. gtk_clipboard_set_with_data(state->gtkclipboard, targettable,
  2678. n_targets, clipboard_provide_data,
  2679. clipboard_clear, cdi);
  2680. gtk_target_table_free(targettable, n_targets);
  2681. gtk_target_list_unref(targetlist);
  2682. }
  2683. }
  2684. static void clipboard_text_received(GtkClipboard *clipboard,
  2685. const gchar *text, gpointer data)
  2686. {
  2687. GtkFrontend *inst = (GtkFrontend *)data;
  2688. wchar_t *paste;
  2689. size_t paste_len;
  2690. if (!text)
  2691. return;
  2692. paste = dup_mb_to_wc(CS_UTF8, text, length, &paste_len);
  2693. term_do_paste(inst->term, paste, paste_len);
  2694. sfree(paste);
  2695. }
  2696. static void gtkwin_clip_request_paste(TermWin *tw, int clipboard)
  2697. {
  2698. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  2699. struct clipboard_state *state = &inst->clipstates[clipboard];
  2700. if (!state->gtkclipboard)
  2701. return;
  2702. gtk_clipboard_request_text(state->gtkclipboard,
  2703. clipboard_text_received, inst);
  2704. }
  2705. #else /* JUST_USE_GTK_CLIPBOARD_UTF8 */
  2706. /* ----------------------------------------------------------------------
  2707. * Clipboard handling for X, using the low-level gtk_selection_*
  2708. * interface, handling conversions to fiddly things like compound text
  2709. * ourselves, and storing in X cut buffers too.
  2710. *
  2711. * This version of the clipboard code has to be kept around for GTK1,
  2712. * which doesn't have the higher-level GtkClipboard interface at all.
  2713. * And since it works on GTK2 and GTK3 too and has had a good few
  2714. * years of shakedown and bug fixing, we might as well keep using it
  2715. * where it's applicable.
  2716. *
  2717. * It's _possible_ that we might be able to replicate all the
  2718. * important wrinkles of this code in GtkClipboard. (In particular,
  2719. * cut buffers or local analogue look as if they might be accessible
  2720. * via gtk_clipboard_set_can_store(), and delivering text in
  2721. * non-Unicode formats only in the direct-to-font case ought to be
  2722. * possible if we can figure out the right set of things to put in the
  2723. * GtkTargetList.) But that work can wait until there's a need for it!
  2724. */
  2725. #ifndef NOT_X_WINDOWS
  2726. /* Store the data in a cut-buffer. */
  2727. static void store_cutbuffer(GtkFrontend *inst, char *ptr, int len)
  2728. {
  2729. if (inst->disp) {
  2730. /* ICCCM says we must rotate the buffers before storing to buffer 0. */
  2731. XRotateBuffers(inst->disp, 1);
  2732. XStoreBytes(inst->disp, ptr, len);
  2733. }
  2734. }
  2735. /* Retrieve data from a cut-buffer.
  2736. * Returned data needs to be freed with XFree().
  2737. */
  2738. static char *retrieve_cutbuffer(GtkFrontend *inst, int *nbytes)
  2739. {
  2740. char *ptr;
  2741. if (!inst->disp) {
  2742. *nbytes = 0;
  2743. return NULL;
  2744. }
  2745. ptr = XFetchBytes(inst->disp, nbytes);
  2746. if (*nbytes <= 0 && ptr != 0) {
  2747. XFree(ptr);
  2748. ptr = 0;
  2749. }
  2750. return ptr;
  2751. }
  2752. #endif /* NOT_X_WINDOWS */
  2753. static void gtkwin_clip_write(
  2754. TermWin *tw, int clipboard, wchar_t *data, int *attr,
  2755. truecolour *truecolour, int len, bool must_deselect)
  2756. {
  2757. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  2758. struct clipboard_state *state = &inst->clipstates[clipboard];
  2759. if (state->pasteout_data)
  2760. sfree(state->pasteout_data);
  2761. if (state->pasteout_data_ctext)
  2762. sfree(state->pasteout_data_ctext);
  2763. if (state->pasteout_data_utf8)
  2764. sfree(state->pasteout_data_utf8);
  2765. /*
  2766. * Set up UTF-8 and compound text paste data. This only happens
  2767. * if we aren't in direct-to-font mode using the D800 hack.
  2768. */
  2769. if (!inst->direct_to_font) {
  2770. const wchar_t *tmp = data;
  2771. int tmplen = len;
  2772. #ifndef NOT_X_WINDOWS
  2773. XTextProperty tp;
  2774. char *list[1];
  2775. #endif
  2776. state->pasteout_data_utf8 = snewn(len*6, char);
  2777. state->pasteout_data_utf8_len = len*6;
  2778. state->pasteout_data_utf8_len =
  2779. charset_from_unicode(&tmp, &tmplen, state->pasteout_data_utf8,
  2780. state->pasteout_data_utf8_len,
  2781. CS_UTF8, NULL, NULL, 0);
  2782. if (state->pasteout_data_utf8_len == 0) {
  2783. sfree(state->pasteout_data_utf8);
  2784. state->pasteout_data_utf8 = NULL;
  2785. } else {
  2786. state->pasteout_data_utf8 =
  2787. sresize(state->pasteout_data_utf8,
  2788. state->pasteout_data_utf8_len + 1, char);
  2789. state->pasteout_data_utf8[state->pasteout_data_utf8_len] = '\0';
  2790. }
  2791. /*
  2792. * Now let Xlib convert our UTF-8 data into compound text.
  2793. */
  2794. #ifndef NOT_X_WINDOWS
  2795. list[0] = state->pasteout_data_utf8;
  2796. if (inst->disp && Xutf8TextListToTextProperty(
  2797. inst->disp, list, 1, XCompoundTextStyle, &tp) == 0) {
  2798. state->pasteout_data_ctext = snewn(tp.nitems+1, char);
  2799. memcpy(state->pasteout_data_ctext, tp.value, tp.nitems);
  2800. state->pasteout_data_ctext_len = tp.nitems;
  2801. XFree(tp.value);
  2802. } else
  2803. #endif
  2804. {
  2805. state->pasteout_data_ctext = NULL;
  2806. state->pasteout_data_ctext_len = 0;
  2807. }
  2808. } else {
  2809. state->pasteout_data_utf8 = NULL;
  2810. state->pasteout_data_utf8_len = 0;
  2811. state->pasteout_data_ctext = NULL;
  2812. state->pasteout_data_ctext_len = 0;
  2813. }
  2814. {
  2815. size_t outlen;
  2816. state->pasteout_data = dup_wc_to_mb_c(
  2817. inst->ucsdata.line_codepage, data, len, "", &outlen);
  2818. /* We can't handle pastes larger than INT_MAX, because
  2819. * gtk_selection_data_set_text's length parameter is a gint */
  2820. if (outlen > INT_MAX)
  2821. outlen = INT_MAX;
  2822. state->pasteout_data_len = outlen;
  2823. }
  2824. #ifndef NOT_X_WINDOWS
  2825. /* The legacy X cut buffers go with PRIMARY, not any other clipboard */
  2826. if (state->atom == GDK_SELECTION_PRIMARY)
  2827. store_cutbuffer(inst, state->pasteout_data, state->pasteout_data_len);
  2828. #endif
  2829. if (gtk_selection_owner_set(inst->area, state->atom,
  2830. inst->input_event_time)) {
  2831. #if GTK_CHECK_VERSION(2,0,0)
  2832. gtk_selection_clear_targets(inst->area, state->atom);
  2833. #endif
  2834. gtk_selection_add_target(inst->area, state->atom,
  2835. GDK_SELECTION_TYPE_STRING, 1);
  2836. if (state->pasteout_data_ctext)
  2837. gtk_selection_add_target(inst->area, state->atom,
  2838. compound_text_atom, 1);
  2839. if (state->pasteout_data_utf8)
  2840. gtk_selection_add_target(inst->area, state->atom,
  2841. utf8_string_atom, 1);
  2842. }
  2843. if (must_deselect)
  2844. term_lost_clipboard_ownership(inst->term, clipboard);
  2845. }
  2846. static void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
  2847. guint info, guint time_stamp, gpointer data)
  2848. {
  2849. GtkFrontend *inst = (GtkFrontend *)data;
  2850. GdkAtom target = gtk_selection_data_get_target(seldata);
  2851. struct clipboard_state *state = clipboard_from_atom(
  2852. inst, gtk_selection_data_get_selection(seldata));
  2853. if (!state)
  2854. return;
  2855. if (target == utf8_string_atom)
  2856. gtk_selection_data_set(seldata, target, 8,
  2857. (unsigned char *)state->pasteout_data_utf8,
  2858. state->pasteout_data_utf8_len);
  2859. else if (target == compound_text_atom)
  2860. gtk_selection_data_set(seldata, target, 8,
  2861. (unsigned char *)state->pasteout_data_ctext,
  2862. state->pasteout_data_ctext_len);
  2863. else
  2864. gtk_selection_data_set(seldata, target, 8,
  2865. (unsigned char *)state->pasteout_data,
  2866. state->pasteout_data_len);
  2867. }
  2868. static gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
  2869. gpointer data)
  2870. {
  2871. GtkFrontend *inst = (GtkFrontend *)data;
  2872. struct clipboard_state *state = clipboard_from_atom(
  2873. inst, seldata->selection);
  2874. if (!state)
  2875. return true;
  2876. term_lost_clipboard_ownership(inst->term, state->clipboard);
  2877. if (state->pasteout_data)
  2878. sfree(state->pasteout_data);
  2879. if (state->pasteout_data_ctext)
  2880. sfree(state->pasteout_data_ctext);
  2881. if (state->pasteout_data_utf8)
  2882. sfree(state->pasteout_data_utf8);
  2883. state->pasteout_data = NULL;
  2884. state->pasteout_data_len = 0;
  2885. state->pasteout_data_ctext = NULL;
  2886. state->pasteout_data_ctext_len = 0;
  2887. state->pasteout_data_utf8 = NULL;
  2888. state->pasteout_data_utf8_len = 0;
  2889. return true;
  2890. }
  2891. static void gtkwin_clip_request_paste(TermWin *tw, int clipboard)
  2892. {
  2893. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  2894. struct clipboard_state *state = &inst->clipstates[clipboard];
  2895. /*
  2896. * In Unix, pasting is asynchronous: all we can do at the
  2897. * moment is to call gtk_selection_convert(), and when the data
  2898. * comes back _then_ we can call term_do_paste().
  2899. */
  2900. if (!inst->direct_to_font) {
  2901. /*
  2902. * First we attempt to retrieve the selection as a UTF-8
  2903. * string (which we will convert to the correct code page
  2904. * before sending to the session, of course). If that
  2905. * fails, selection_received() will be informed and will
  2906. * fall back to an ordinary string.
  2907. */
  2908. gtk_selection_convert(inst->area, state->atom, utf8_string_atom,
  2909. inst->input_event_time);
  2910. } else {
  2911. /*
  2912. * If we're in direct-to-font mode, we disable UTF-8
  2913. * pasting, and go straight to ordinary string data.
  2914. */
  2915. gtk_selection_convert(inst->area, state->atom,
  2916. GDK_SELECTION_TYPE_STRING,
  2917. inst->input_event_time);
  2918. }
  2919. }
  2920. static void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
  2921. guint time, gpointer data)
  2922. {
  2923. GtkFrontend *inst = (GtkFrontend *)data;
  2924. char *text;
  2925. int length;
  2926. #ifndef NOT_X_WINDOWS
  2927. char **list;
  2928. bool free_list_required = false;
  2929. bool free_required = false;
  2930. #endif
  2931. int charset;
  2932. GdkAtom seldata_target = gtk_selection_data_get_target(seldata);
  2933. GdkAtom seldata_type = gtk_selection_data_get_data_type(seldata);
  2934. const guchar *seldata_data = gtk_selection_data_get_data(seldata);
  2935. gint seldata_length = gtk_selection_data_get_length(seldata);
  2936. wchar_t *paste;
  2937. size_t paste_len;
  2938. struct clipboard_state *state = clipboard_from_atom(
  2939. inst, gtk_selection_data_get_selection(seldata));
  2940. if (!state)
  2941. return;
  2942. if (seldata_target == utf8_string_atom && seldata_length <= 0) {
  2943. /*
  2944. * Failed to get a UTF-8 selection string. Try compound
  2945. * text next.
  2946. */
  2947. gtk_selection_convert(inst->area, state->atom,
  2948. compound_text_atom,
  2949. inst->input_event_time);
  2950. return;
  2951. }
  2952. if (seldata_target == compound_text_atom && seldata_length <= 0) {
  2953. /*
  2954. * Failed to get UTF-8 or compound text. Try an ordinary
  2955. * string.
  2956. */
  2957. gtk_selection_convert(inst->area, state->atom,
  2958. GDK_SELECTION_TYPE_STRING,
  2959. inst->input_event_time);
  2960. return;
  2961. }
  2962. /*
  2963. * If we have data, but it's not of a type we can deal with,
  2964. * we have to ignore the data.
  2965. */
  2966. if (seldata_length > 0 &&
  2967. seldata_type != GDK_SELECTION_TYPE_STRING &&
  2968. seldata_type != compound_text_atom &&
  2969. seldata_type != utf8_string_atom)
  2970. return;
  2971. /*
  2972. * If we have no data, try looking in a cut buffer.
  2973. */
  2974. if (seldata_length <= 0) {
  2975. #ifndef NOT_X_WINDOWS
  2976. text = retrieve_cutbuffer(inst, &length);
  2977. if (length == 0)
  2978. return;
  2979. /* Xterm is rumoured to expect Latin-1, though I haven't checked the
  2980. * source, so use that as a de-facto standard. */
  2981. charset = CS_ISO8859_1;
  2982. free_required = true;
  2983. #else
  2984. return;
  2985. #endif
  2986. } else {
  2987. /*
  2988. * Convert COMPOUND_TEXT into UTF-8.
  2989. */
  2990. if (seldata_type == compound_text_atom) {
  2991. #ifndef NOT_X_WINDOWS
  2992. XTextProperty tp;
  2993. int ret, count;
  2994. tp.value = (unsigned char *)seldata_data;
  2995. tp.encoding = (Atom) seldata_type;
  2996. tp.format = gtk_selection_data_get_format(seldata);
  2997. tp.nitems = seldata_length;
  2998. ret = inst->disp == NULL ? -1 :
  2999. Xutf8TextPropertyToTextList(inst->disp, &tp, &list, &count);
  3000. if (ret == 0 && count == 1) {
  3001. text = list[0];
  3002. length = strlen(list[0]);
  3003. charset = CS_UTF8;
  3004. free_list_required = true;
  3005. } else
  3006. #endif
  3007. {
  3008. /*
  3009. * Compound text failed; fall back to STRING.
  3010. */
  3011. gtk_selection_convert(inst->area, state->atom,
  3012. GDK_SELECTION_TYPE_STRING,
  3013. inst->input_event_time);
  3014. return;
  3015. }
  3016. } else {
  3017. text = (char *)seldata_data;
  3018. length = seldata_length;
  3019. charset = (seldata_type == utf8_string_atom ?
  3020. CS_UTF8 : inst->ucsdata.line_codepage);
  3021. }
  3022. }
  3023. paste = dup_mb_to_wc_c(charset, text, length, &paste_len);
  3024. term_do_paste(inst->term, paste, paste_len);
  3025. sfree(paste);
  3026. #ifndef NOT_X_WINDOWS
  3027. if (free_list_required)
  3028. XFreeStringList(list);
  3029. if (free_required)
  3030. XFree(text);
  3031. #endif
  3032. }
  3033. static void init_one_clipboard(GtkFrontend *inst, int clipboard)
  3034. {
  3035. struct clipboard_state *state = &inst->clipstates[clipboard];
  3036. state->inst = inst;
  3037. state->clipboard = clipboard;
  3038. }
  3039. void set_clipboard_atom(GtkFrontend *inst, int clipboard, GdkAtom atom)
  3040. {
  3041. struct clipboard_state *state = &inst->clipstates[clipboard];
  3042. state->inst = inst;
  3043. state->clipboard = clipboard;
  3044. state->atom = atom;
  3045. }
  3046. void init_clipboard(GtkFrontend *inst)
  3047. {
  3048. #ifndef NOT_X_WINDOWS
  3049. /*
  3050. * Ensure that all the cut buffers exist - according to the ICCCM,
  3051. * we must do this before we start using cut buffers.
  3052. */
  3053. if (inst->disp) {
  3054. unsigned char empty[] = "";
  3055. x11_ignore_error(inst->disp, BadMatch);
  3056. XChangeProperty(inst->disp, GDK_ROOT_WINDOW(), XA_CUT_BUFFER0,
  3057. XA_STRING, 8, PropModeAppend, empty, 0);
  3058. x11_ignore_error(inst->disp, BadMatch);
  3059. XChangeProperty(inst->disp, GDK_ROOT_WINDOW(), XA_CUT_BUFFER1,
  3060. XA_STRING, 8, PropModeAppend, empty, 0);
  3061. x11_ignore_error(inst->disp, BadMatch);
  3062. XChangeProperty(inst->disp, GDK_ROOT_WINDOW(), XA_CUT_BUFFER2,
  3063. XA_STRING, 8, PropModeAppend, empty, 0);
  3064. x11_ignore_error(inst->disp, BadMatch);
  3065. XChangeProperty(inst->disp, GDK_ROOT_WINDOW(), XA_CUT_BUFFER3,
  3066. XA_STRING, 8, PropModeAppend, empty, 0);
  3067. x11_ignore_error(inst->disp, BadMatch);
  3068. XChangeProperty(inst->disp, GDK_ROOT_WINDOW(), XA_CUT_BUFFER4,
  3069. XA_STRING, 8, PropModeAppend, empty, 0);
  3070. x11_ignore_error(inst->disp, BadMatch);
  3071. XChangeProperty(inst->disp, GDK_ROOT_WINDOW(), XA_CUT_BUFFER5,
  3072. XA_STRING, 8, PropModeAppend, empty, 0);
  3073. x11_ignore_error(inst->disp, BadMatch);
  3074. XChangeProperty(inst->disp, GDK_ROOT_WINDOW(), XA_CUT_BUFFER6,
  3075. XA_STRING, 8, PropModeAppend, empty, 0);
  3076. x11_ignore_error(inst->disp, BadMatch);
  3077. XChangeProperty(inst->disp, GDK_ROOT_WINDOW(), XA_CUT_BUFFER7,
  3078. XA_STRING, 8, PropModeAppend, empty, 0);
  3079. }
  3080. #endif
  3081. inst->clipstates[CLIP_PRIMARY].atom = GDK_SELECTION_PRIMARY;
  3082. inst->clipstates[CLIP_CLIPBOARD].atom = clipboard_atom;
  3083. init_one_clipboard(inst, CLIP_PRIMARY);
  3084. init_one_clipboard(inst, CLIP_CLIPBOARD);
  3085. g_signal_connect(G_OBJECT(inst->area), "selection_received",
  3086. G_CALLBACK(selection_received), inst);
  3087. g_signal_connect(G_OBJECT(inst->area), "selection_get",
  3088. G_CALLBACK(selection_get), inst);
  3089. g_signal_connect(G_OBJECT(inst->area), "selection_clear_event",
  3090. G_CALLBACK(selection_clear), inst);
  3091. }
  3092. /*
  3093. * End of selection/clipboard handling.
  3094. * ----------------------------------------------------------------------
  3095. */
  3096. #endif /* JUST_USE_GTK_CLIPBOARD_UTF8 */
  3097. static void set_window_titles(GtkFrontend *inst)
  3098. {
  3099. /*
  3100. * We must always call set_icon_name after calling set_title,
  3101. * since set_title will write both names. Irritating, but such
  3102. * is life.
  3103. */
  3104. gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
  3105. if (!conf_get_bool(inst->conf, CONF_win_name_always))
  3106. gdk_window_set_icon_name(gtk_widget_get_window(inst->window),
  3107. inst->icontitle);
  3108. }
  3109. static void gtkwin_set_title(TermWin *tw, const char *title, int codepage)
  3110. {
  3111. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  3112. sfree(inst->wintitle);
  3113. if (codepage != CP_UTF8) {
  3114. wchar_t *title_w = dup_mb_to_wc(codepage, title);
  3115. inst->wintitle = encode_wide_string_as_utf8(title_w);
  3116. sfree(title_w);
  3117. } else {
  3118. inst->wintitle = dupstr(title);
  3119. }
  3120. set_window_titles(inst);
  3121. }
  3122. static void gtkwin_set_icon_title(TermWin *tw, const char *title, int codepage)
  3123. {
  3124. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  3125. sfree(inst->icontitle);
  3126. if (codepage != CP_UTF8) {
  3127. wchar_t *title_w = dup_mb_to_wc(codepage, title);
  3128. inst->icontitle = encode_wide_string_as_utf8(title_w);
  3129. sfree(title_w);
  3130. } else {
  3131. inst->icontitle = dupstr(title);
  3132. }
  3133. set_window_titles(inst);
  3134. }
  3135. static void gtkwin_set_scrollbar(TermWin *tw, int total, int start, int page)
  3136. {
  3137. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  3138. if (!conf_get_bool(inst->conf, CONF_scrollbar))
  3139. return;
  3140. inst->ignore_sbar = true;
  3141. gtk_adjustment_set_lower(inst->sbar_adjust, 0);
  3142. gtk_adjustment_set_upper(inst->sbar_adjust, total);
  3143. gtk_adjustment_set_value(inst->sbar_adjust, start);
  3144. gtk_adjustment_set_page_size(inst->sbar_adjust, page);
  3145. gtk_adjustment_set_step_increment(inst->sbar_adjust, 1);
  3146. gtk_adjustment_set_page_increment(inst->sbar_adjust, page/2);
  3147. #if !GTK_CHECK_VERSION(3,18,0)
  3148. gtk_adjustment_changed(inst->sbar_adjust);
  3149. #endif
  3150. inst->ignore_sbar = false;
  3151. }
  3152. void scrollbar_moved(GtkAdjustment *adj, GtkFrontend *inst)
  3153. {
  3154. if (!conf_get_bool(inst->conf, CONF_scrollbar))
  3155. return;
  3156. if (!inst->ignore_sbar)
  3157. term_scroll(inst->term, 1, (int)gtk_adjustment_get_value(adj));
  3158. }
  3159. static void show_scrollbar(GtkFrontend *inst, gboolean visible)
  3160. {
  3161. inst->sbar_visible = visible;
  3162. if (visible)
  3163. gtk_widget_show(inst->sbar);
  3164. else
  3165. gtk_widget_hide(inst->sbar);
  3166. }
  3167. static void gtkwin_set_cursor_pos(TermWin *tw, int x, int y)
  3168. {
  3169. /*
  3170. * This is meaningless under X.
  3171. */
  3172. }
  3173. /*
  3174. * This is still called when mode==BELL_VISUAL, even though the
  3175. * visual bell is handled entirely within terminal.c, because we
  3176. * may want to perform additional actions on any kind of bell (for
  3177. * example, taskbar flashing in Windows).
  3178. */
  3179. static void gtkwin_bell(TermWin *tw, int mode)
  3180. {
  3181. /* GtkFrontend *inst = container_of(tw, GtkFrontend, termwin); */
  3182. if (mode == BELL_DEFAULT)
  3183. gdk_display_beep(gdk_display_get_default());
  3184. }
  3185. static int gtkwin_char_width(TermWin *tw, int uc)
  3186. {
  3187. /*
  3188. * In this front end, double-width characters are handled using a
  3189. * separate font, so this can safely just return 1 always.
  3190. */
  3191. return 1;
  3192. }
  3193. static bool gtkwin_setup_draw_ctx(TermWin *tw)
  3194. {
  3195. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  3196. if (!gtk_widget_get_window(inst->area))
  3197. return false;
  3198. inst->uctx.type = inst->drawtype;
  3199. #ifdef DRAW_TEXT_GDK
  3200. if (inst->uctx.type == DRAWTYPE_GDK) {
  3201. /* If we're doing GDK-based drawing, then we also expect
  3202. * inst->pixmap to exist. */
  3203. inst->uctx.u.gdk.target = inst->pixmap;
  3204. inst->uctx.u.gdk.gc = gdk_gc_new(gtk_widget_get_window(inst->area));
  3205. }
  3206. #endif
  3207. #ifdef DRAW_TEXT_CAIRO
  3208. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3209. inst->uctx.u.cairo.widget = GTK_WIDGET(inst->area);
  3210. /*
  3211. * If we're doing Cairo drawing, we draw to the target pixmap
  3212. * if there is one, and otherwise to the backing surface.
  3213. */
  3214. #ifdef NO_BACKING_PIXMAPS
  3215. inst->uctx.u.cairo.cr = cairo_create(inst->surface);
  3216. #else
  3217. inst->uctx.u.cairo.cr = gdk_cairo_create(inst->pixmap);
  3218. #endif
  3219. cairo_setup_draw_ctx(inst);
  3220. }
  3221. #endif
  3222. return true;
  3223. }
  3224. static void gtkwin_free_draw_ctx(TermWin *tw)
  3225. {
  3226. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  3227. #ifdef DRAW_TEXT_GDK
  3228. if (inst->uctx.type == DRAWTYPE_GDK) {
  3229. gdk_gc_unref(inst->uctx.u.gdk.gc);
  3230. }
  3231. #endif
  3232. #ifdef DRAW_TEXT_CAIRO
  3233. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3234. cairo_destroy(inst->uctx.u.cairo.cr);
  3235. }
  3236. #endif
  3237. }
  3238. static void draw_update(GtkFrontend *inst, int x, int y, int w, int h)
  3239. {
  3240. /*
  3241. * Now we just queue a window redraw, which will cause
  3242. * inst->surface or inst->pixmap (whichever is appropriate for our
  3243. * compile mode) to be copied to the real window when we receive
  3244. * the resulting "expose" or "draw" event.
  3245. *
  3246. * Amazingly, this one API call is actually valid in all versions
  3247. * of GTK :-)
  3248. */
  3249. gtk_widget_queue_draw_area(inst->area, x, y, w, h);
  3250. }
  3251. #ifdef DRAW_TEXT_CAIRO
  3252. static void cairo_set_source_rgb_dim(cairo_t *cr, double r, double g, double b,
  3253. bool dim)
  3254. {
  3255. if (dim)
  3256. cairo_set_source_rgb(cr, r * 2 / 3, g * 2 / 3, b * 2 / 3);
  3257. else
  3258. cairo_set_source_rgb(cr, r, g, b);
  3259. }
  3260. #endif
  3261. static void draw_set_colour(GtkFrontend *inst, int col, bool dim)
  3262. {
  3263. #ifdef DRAW_TEXT_GDK
  3264. if (inst->uctx.type == DRAWTYPE_GDK) {
  3265. if (dim) {
  3266. #if GTK_CHECK_VERSION(2,0,0)
  3267. GdkColor color;
  3268. color.red = inst->cols[col].red * 2 / 3;
  3269. color.green = inst->cols[col].green * 2 / 3;
  3270. color.blue = inst->cols[col].blue * 2 / 3;
  3271. gdk_gc_set_rgb_fg_color(inst->uctx.u.gdk.gc, &color);
  3272. #else
  3273. /* Poor GTK1 fallback */
  3274. gdk_gc_set_foreground(inst->uctx.u.gdk.gc, &inst->cols[col]);
  3275. #endif
  3276. } else {
  3277. gdk_gc_set_foreground(inst->uctx.u.gdk.gc, &inst->cols[col]);
  3278. }
  3279. }
  3280. #endif
  3281. #ifdef DRAW_TEXT_CAIRO
  3282. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3283. cairo_set_source_rgb_dim(inst->uctx.u.cairo.cr,
  3284. inst->cols[col].red / 65535.0,
  3285. inst->cols[col].green / 65535.0,
  3286. inst->cols[col].blue / 65535.0, dim);
  3287. }
  3288. #endif
  3289. }
  3290. static void draw_set_colour_rgb(GtkFrontend *inst, optionalrgb orgb, bool dim)
  3291. {
  3292. #ifdef DRAW_TEXT_GDK
  3293. if (inst->uctx.type == DRAWTYPE_GDK) {
  3294. #if GTK_CHECK_VERSION(2,0,0)
  3295. GdkColor color;
  3296. color.red = orgb.r * 256;
  3297. color.green = orgb.g * 256;
  3298. color.blue = orgb.b * 256;
  3299. if (dim) {
  3300. color.red = color.red * 2 / 3;
  3301. color.green = color.green * 2 / 3;
  3302. color.blue = color.blue * 2 / 3;
  3303. }
  3304. gdk_gc_set_rgb_fg_color(inst->uctx.u.gdk.gc, &color);
  3305. #else
  3306. /* Poor GTK1 fallback */
  3307. gdk_gc_set_foreground(inst->uctx.u.gdk.gc, &inst->cols[256]);
  3308. #endif
  3309. }
  3310. #endif
  3311. #ifdef DRAW_TEXT_CAIRO
  3312. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3313. cairo_set_source_rgb_dim(inst->uctx.u.cairo.cr, orgb.r / 255.0,
  3314. orgb.g / 255.0, orgb.b / 255.0, dim);
  3315. }
  3316. #endif
  3317. }
  3318. static void draw_rectangle(GtkFrontend *inst, bool filled,
  3319. int x, int y, int w, int h)
  3320. {
  3321. #ifdef DRAW_TEXT_GDK
  3322. if (inst->uctx.type == DRAWTYPE_GDK) {
  3323. gdk_draw_rectangle(inst->uctx.u.gdk.target, inst->uctx.u.gdk.gc,
  3324. filled, x, y, w, h);
  3325. }
  3326. #endif
  3327. #ifdef DRAW_TEXT_CAIRO
  3328. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3329. cairo_new_path(inst->uctx.u.cairo.cr);
  3330. if (filled) {
  3331. cairo_rectangle(inst->uctx.u.cairo.cr, x, y, w, h);
  3332. cairo_fill(inst->uctx.u.cairo.cr);
  3333. } else {
  3334. cairo_rectangle(inst->uctx.u.cairo.cr,
  3335. x + 0.5, y + 0.5, w, h);
  3336. cairo_close_path(inst->uctx.u.cairo.cr);
  3337. cairo_stroke(inst->uctx.u.cairo.cr);
  3338. }
  3339. }
  3340. #endif
  3341. }
  3342. static void draw_clip(GtkFrontend *inst, int x, int y, int w, int h)
  3343. {
  3344. #ifdef DRAW_TEXT_GDK
  3345. if (inst->uctx.type == DRAWTYPE_GDK) {
  3346. GdkRectangle r;
  3347. r.x = x;
  3348. r.y = y;
  3349. r.width = w;
  3350. r.height = h;
  3351. gdk_gc_set_clip_rectangle(inst->uctx.u.gdk.gc, &r);
  3352. }
  3353. #endif
  3354. #ifdef DRAW_TEXT_CAIRO
  3355. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3356. cairo_reset_clip(inst->uctx.u.cairo.cr);
  3357. cairo_new_path(inst->uctx.u.cairo.cr);
  3358. cairo_rectangle(inst->uctx.u.cairo.cr, x, y, w, h);
  3359. cairo_clip(inst->uctx.u.cairo.cr);
  3360. }
  3361. #endif
  3362. }
  3363. static void draw_point(GtkFrontend *inst, int x, int y)
  3364. {
  3365. #ifdef DRAW_TEXT_GDK
  3366. if (inst->uctx.type == DRAWTYPE_GDK) {
  3367. gdk_draw_point(inst->uctx.u.gdk.target, inst->uctx.u.gdk.gc, x, y);
  3368. }
  3369. #endif
  3370. #ifdef DRAW_TEXT_CAIRO
  3371. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3372. cairo_new_path(inst->uctx.u.cairo.cr);
  3373. cairo_rectangle(inst->uctx.u.cairo.cr, x, y, 1, 1);
  3374. cairo_fill(inst->uctx.u.cairo.cr);
  3375. }
  3376. #endif
  3377. }
  3378. static void draw_line(GtkFrontend *inst, int x0, int y0, int x1, int y1)
  3379. {
  3380. #ifdef DRAW_TEXT_GDK
  3381. if (inst->uctx.type == DRAWTYPE_GDK) {
  3382. gdk_draw_line(inst->uctx.u.gdk.target, inst->uctx.u.gdk.gc,
  3383. x0, y0, x1, y1);
  3384. }
  3385. #endif
  3386. #ifdef DRAW_TEXT_CAIRO
  3387. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3388. cairo_new_path(inst->uctx.u.cairo.cr);
  3389. cairo_move_to(inst->uctx.u.cairo.cr, x0 + 0.5, y0 + 0.5);
  3390. cairo_line_to(inst->uctx.u.cairo.cr, x1 + 0.5, y1 + 0.5);
  3391. cairo_stroke(inst->uctx.u.cairo.cr);
  3392. }
  3393. #endif
  3394. }
  3395. static void draw_stretch_before(GtkFrontend *inst, int x, int y,
  3396. int w, bool wdouble,
  3397. int h, bool hdouble, bool hbothalf)
  3398. {
  3399. #ifdef DRAW_TEXT_CAIRO
  3400. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3401. cairo_save(inst->uctx.u.cairo.cr);
  3402. cairo_translate(inst->uctx.u.cairo.cr,
  3403. -x * wdouble, -y * hdouble - h * hbothalf);
  3404. cairo_scale(inst->uctx.u.cairo.cr, 1 + wdouble, 1 + hdouble);
  3405. }
  3406. #endif
  3407. }
  3408. static void draw_stretch_after(GtkFrontend *inst, int x, int y,
  3409. int w, bool wdouble,
  3410. int h, bool hdouble, bool hbothalf)
  3411. {
  3412. #ifdef DRAW_TEXT_GDK
  3413. #ifndef NO_BACKING_PIXMAPS
  3414. if (inst->uctx.type == DRAWTYPE_GDK) {
  3415. /*
  3416. * I can't find any plausible StretchBlt equivalent in the X
  3417. * server, so I'm going to do this the slow and painful way.
  3418. * This will involve repeated calls to gdk_draw_pixmap() to
  3419. * stretch the text horizontally. It's O(N^2) in time and O(N)
  3420. * in network bandwidth, but you try thinking of a better way.
  3421. * :-(
  3422. */
  3423. int i;
  3424. if (wdouble) {
  3425. for (i = 0; i < w; i++) {
  3426. gdk_draw_pixmap(inst->uctx.u.gdk.target,
  3427. inst->uctx.u.gdk.gc,
  3428. inst->uctx.u.gdk.target,
  3429. x + 2*i, y,
  3430. x + 2*i+1, y,
  3431. w - i, h);
  3432. }
  3433. w *= 2;
  3434. }
  3435. if (hdouble) {
  3436. int dt, db;
  3437. /* Now stretch vertically, in the same way. */
  3438. if (hbothalf)
  3439. dt = 0, db = 1;
  3440. else
  3441. dt = 1, db = 0;
  3442. for (i = 0; i < h; i += 2) {
  3443. gdk_draw_pixmap(inst->uctx.u.gdk.target,
  3444. inst->uctx.u.gdk.gc,
  3445. inst->uctx.u.gdk.target,
  3446. x, y + dt*i + db,
  3447. x, y + dt*(i+1),
  3448. w, h-i-1);
  3449. }
  3450. }
  3451. }
  3452. #else
  3453. #error No way to implement stretching in GDK without a reliable backing pixmap
  3454. #endif
  3455. #endif /* DRAW_TEXT_GDK */
  3456. #ifdef DRAW_TEXT_CAIRO
  3457. if (inst->uctx.type == DRAWTYPE_CAIRO)
  3458. cairo_restore(inst->uctx.u.cairo.cr);
  3459. #endif
  3460. }
  3461. static void draw_backing_rect(GtkFrontend *inst)
  3462. {
  3463. if (!win_setup_draw_ctx(&inst->termwin))
  3464. return;
  3465. draw_set_colour(inst, 258, false);
  3466. draw_rectangle(inst, true, 0, 0, inst->backing_w, inst->backing_h);
  3467. draw_update(inst, 0, 0, inst->backing_w, inst->backing_h);
  3468. win_free_draw_ctx(&inst->termwin);
  3469. }
  3470. /*
  3471. * Draw a line of text in the window, at given character
  3472. * coordinates, in given attributes.
  3473. *
  3474. * We are allowed to fiddle with the contents of `text'.
  3475. */
  3476. static void do_text_internal(
  3477. GtkFrontend *inst, int x, int y, wchar_t *text, int len,
  3478. unsigned long attr, int lattr, truecolour truecolour)
  3479. {
  3480. int ncombining;
  3481. int nfg, nbg, t, fontid, rlen, widefactor;
  3482. bool bold;
  3483. bool monochrome =
  3484. gdk_visual_get_depth(gtk_widget_get_visual(inst->area)) == 1;
  3485. if (attr & TATTR_COMBINING) {
  3486. ncombining = len;
  3487. len = 1;
  3488. } else
  3489. ncombining = 1;
  3490. if (monochrome)
  3491. truecolour.fg = truecolour.bg = optionalrgb_none;
  3492. nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
  3493. nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
  3494. if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & ATTR_ACTCURS))) {
  3495. struct optionalrgb trgb;
  3496. t = nfg;
  3497. nfg = nbg;
  3498. nbg = t;
  3499. trgb = truecolour.fg;
  3500. truecolour.fg = truecolour.bg;
  3501. truecolour.bg = trgb;
  3502. }
  3503. if ((inst->bold_style & BOLD_STYLE_COLOUR) && (attr & ATTR_BOLD)) {
  3504. if (nfg < 16) nfg |= 8;
  3505. else if (nfg >= 256) nfg |= 1;
  3506. }
  3507. if ((inst->bold_style & BOLD_STYLE_COLOUR) && (attr & ATTR_BLINK)) {
  3508. if (nbg < 16) nbg |= 8;
  3509. else if (nbg >= 256) nbg |= 1;
  3510. }
  3511. if ((attr & ATTR_ACTCURS) && !monochrome) {
  3512. truecolour.fg = truecolour.bg = optionalrgb_none;
  3513. nfg = 260;
  3514. nbg = 261;
  3515. attr &= ~ATTR_DIM; /* don't dim the cursor */
  3516. }
  3517. fontid = 0;
  3518. if (attr & ATTR_WIDE) {
  3519. widefactor = 2;
  3520. fontid |= 2;
  3521. } else {
  3522. widefactor = 1;
  3523. }
  3524. if ((attr & ATTR_BOLD) && (inst->bold_style & BOLD_STYLE_FONT)) {
  3525. bold = true;
  3526. fontid |= 1;
  3527. } else {
  3528. bold = false;
  3529. }
  3530. if (!inst->fonts[fontid]) {
  3531. int i;
  3532. /*
  3533. * Fall back through font ids with subsets of this one's
  3534. * set bits, in order.
  3535. */
  3536. for (i = fontid; i-- > 0 ;) {
  3537. if (i & ~fontid)
  3538. continue; /* some other bit is set */
  3539. if (inst->fonts[i]) {
  3540. fontid = i;
  3541. break;
  3542. }
  3543. }
  3544. assert(inst->fonts[fontid]); /* we should at least have hit zero */
  3545. }
  3546. if ((lattr & LATTR_MODE) != LATTR_NORM) {
  3547. x *= 2;
  3548. if (x >= inst->term->cols)
  3549. return;
  3550. if (x + len*2*widefactor > inst->term->cols) {
  3551. len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
  3552. if (len == 0)
  3553. return; /* rounded down half a double-width char to zero */
  3554. }
  3555. rlen = len * 2;
  3556. } else
  3557. rlen = len;
  3558. draw_clip(inst,
  3559. x*inst->font_width+inst->window_border,
  3560. y*inst->font_height+inst->window_border,
  3561. rlen*widefactor*inst->font_width,
  3562. inst->font_height);
  3563. if ((lattr & LATTR_MODE) != LATTR_NORM) {
  3564. draw_stretch_before(inst,
  3565. x*inst->font_width+inst->window_border,
  3566. y*inst->font_height+inst->window_border,
  3567. rlen*widefactor*inst->font_width, true,
  3568. inst->font_height,
  3569. ((lattr & LATTR_MODE) != LATTR_WIDE),
  3570. ((lattr & LATTR_MODE) == LATTR_BOT));
  3571. }
  3572. if (truecolour.bg.enabled)
  3573. draw_set_colour_rgb(inst, truecolour.bg, attr & ATTR_DIM);
  3574. else
  3575. draw_set_colour(inst, nbg, attr & ATTR_DIM);
  3576. draw_rectangle(inst, true,
  3577. x*inst->font_width+inst->window_border,
  3578. y*inst->font_height+inst->window_border,
  3579. rlen*widefactor*inst->font_width, inst->font_height);
  3580. if (truecolour.fg.enabled)
  3581. draw_set_colour_rgb(inst, truecolour.fg, attr & ATTR_DIM);
  3582. else
  3583. draw_set_colour(inst, nfg, attr & ATTR_DIM);
  3584. if (ncombining > 1) {
  3585. assert(len == 1);
  3586. unifont_draw_combining(&inst->uctx, inst->fonts[fontid],
  3587. x*inst->font_width+inst->window_border,
  3588. (y*inst->font_height+inst->window_border+
  3589. inst->fonts[0]->ascent),
  3590. text, ncombining, widefactor > 1,
  3591. bold, inst->font_width);
  3592. } else {
  3593. unifont_draw_text(&inst->uctx, inst->fonts[fontid],
  3594. x*inst->font_width+inst->window_border,
  3595. (y*inst->font_height+inst->window_border+
  3596. inst->fonts[0]->ascent),
  3597. text, len, widefactor > 1,
  3598. bold, inst->font_width);
  3599. }
  3600. if (attr & ATTR_UNDER) {
  3601. int uheight = inst->fonts[0]->ascent + 1;
  3602. if (uheight >= inst->font_height)
  3603. uheight = inst->font_height - 1;
  3604. draw_line(inst, x*inst->font_width+inst->window_border,
  3605. y*inst->font_height + uheight + inst->window_border,
  3606. (x+len)*widefactor*inst->font_width-1+inst->window_border,
  3607. y*inst->font_height + uheight + inst->window_border);
  3608. }
  3609. if (attr & ATTR_STRIKE) {
  3610. int sheight = inst->fonts[fontid]->strikethrough_y;
  3611. draw_line(inst, x*inst->font_width+inst->window_border,
  3612. y*inst->font_height + sheight + inst->window_border,
  3613. (x+len)*widefactor*inst->font_width-1+inst->window_border,
  3614. y*inst->font_height + sheight + inst->window_border);
  3615. }
  3616. if ((lattr & LATTR_MODE) != LATTR_NORM) {
  3617. draw_stretch_after(inst,
  3618. x*inst->font_width+inst->window_border,
  3619. y*inst->font_height+inst->window_border,
  3620. rlen*widefactor*inst->font_width, true,
  3621. inst->font_height,
  3622. ((lattr & LATTR_MODE) != LATTR_WIDE),
  3623. ((lattr & LATTR_MODE) == LATTR_BOT));
  3624. }
  3625. }
  3626. static void gtkwin_draw_text(
  3627. TermWin *tw, int x, int y, wchar_t *text, int len,
  3628. unsigned long attr, int lattr, truecolour truecolour)
  3629. {
  3630. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  3631. int widefactor;
  3632. do_text_internal(inst, x, y, text, len, attr, lattr, truecolour);
  3633. if (attr & ATTR_WIDE) {
  3634. widefactor = 2;
  3635. } else {
  3636. widefactor = 1;
  3637. }
  3638. if ((lattr & LATTR_MODE) != LATTR_NORM) {
  3639. x *= 2;
  3640. if (x >= inst->term->cols)
  3641. return;
  3642. if (x + len*2*widefactor > inst->term->cols)
  3643. len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
  3644. len *= 2;
  3645. }
  3646. draw_update(inst,
  3647. x*inst->font_width+inst->window_border,
  3648. y*inst->font_height+inst->window_border,
  3649. len*widefactor*inst->font_width, inst->font_height);
  3650. }
  3651. static void gtkwin_draw_cursor(
  3652. TermWin *tw, int x, int y, wchar_t *text, int len,
  3653. unsigned long attr, int lattr, truecolour truecolour)
  3654. {
  3655. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  3656. bool active, passive;
  3657. int widefactor;
  3658. if (attr & ATTR_PASCURS) {
  3659. attr &= ~ATTR_PASCURS;
  3660. passive = true;
  3661. } else
  3662. passive = false;
  3663. if ((attr & ATTR_ACTCURS) && inst->cursor_type != CURSOR_BLOCK) {
  3664. attr &= ~ATTR_ACTCURS;
  3665. active = true;
  3666. } else
  3667. active = false;
  3668. do_text_internal(inst, x, y, text, len, attr, lattr, truecolour);
  3669. if (attr & TATTR_COMBINING)
  3670. len = 1;
  3671. if (attr & ATTR_WIDE) {
  3672. widefactor = 2;
  3673. } else {
  3674. widefactor = 1;
  3675. }
  3676. if ((lattr & LATTR_MODE) != LATTR_NORM) {
  3677. x *= 2;
  3678. if (x >= inst->term->cols)
  3679. return;
  3680. if (x + len*2*widefactor > inst->term->cols)
  3681. len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
  3682. len *= 2;
  3683. }
  3684. if (inst->cursor_type == CURSOR_BLOCK) {
  3685. /*
  3686. * An active block cursor will already have been done by
  3687. * the above do_text call, so we only need to do anything
  3688. * if it's passive.
  3689. */
  3690. if (passive) {
  3691. draw_set_colour(inst, 261, false);
  3692. draw_rectangle(inst, false,
  3693. x*inst->font_width+inst->window_border,
  3694. y*inst->font_height+inst->window_border,
  3695. len*widefactor*inst->font_width-1,
  3696. inst->font_height-1);
  3697. }
  3698. } else {
  3699. int uheight;
  3700. int startx, starty, dx, dy, length, i;
  3701. int char_width;
  3702. if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
  3703. char_width = 2*inst->font_width;
  3704. else
  3705. char_width = inst->font_width;
  3706. if (inst->cursor_type == CURSOR_UNDERLINE) {
  3707. uheight = inst->fonts[0]->ascent + 1;
  3708. if (uheight >= inst->font_height)
  3709. uheight = inst->font_height - 1;
  3710. startx = x * inst->font_width + inst->window_border;
  3711. starty = y * inst->font_height + inst->window_border + uheight;
  3712. dx = 1;
  3713. dy = 0;
  3714. length = len * widefactor * char_width;
  3715. } else /* inst->cursor_type == CURSOR_VERTICAL_LINE */ {
  3716. int xadjust = 0;
  3717. if (attr & ATTR_RIGHTCURS)
  3718. xadjust = char_width - 1;
  3719. startx = x * inst->font_width + inst->window_border + xadjust;
  3720. starty = y * inst->font_height + inst->window_border;
  3721. dx = 0;
  3722. dy = 1;
  3723. length = inst->font_height;
  3724. }
  3725. draw_set_colour(inst, 261, false);
  3726. if (passive) {
  3727. for (i = 0; i < length; i++) {
  3728. if (i % 2 == 0) {
  3729. draw_point(inst, startx, starty);
  3730. }
  3731. startx += dx;
  3732. starty += dy;
  3733. }
  3734. } else if (active) {
  3735. draw_line(inst, startx, starty,
  3736. startx + (length-1) * dx, starty + (length-1) * dy);
  3737. } /* else no cursor (e.g., blinked off) */
  3738. }
  3739. draw_update(inst,
  3740. x*inst->font_width+inst->window_border,
  3741. y*inst->font_height+inst->window_border,
  3742. len*widefactor*inst->font_width, inst->font_height);
  3743. #if GTK_CHECK_VERSION(2,0,0)
  3744. {
  3745. GdkRectangle cursorrect;
  3746. cursorrect.x = x*inst->font_width+inst->window_border;
  3747. cursorrect.y = y*inst->font_height+inst->window_border;
  3748. cursorrect.width = len*widefactor*inst->font_width;
  3749. cursorrect.height = inst->font_height;
  3750. gtk_im_context_set_cursor_location(inst->imc, &cursorrect);
  3751. }
  3752. #endif
  3753. }
  3754. #if !GTK_CHECK_VERSION(2,0,0)
  3755. /*
  3756. * For GTK 1, manual code to scale an in-memory XPM, producing a new
  3757. * one as output. It will be ugly, but good enough to use as a trust
  3758. * sigil.
  3759. */
  3760. struct XpmHolder {
  3761. char **strings;
  3762. size_t nstrings;
  3763. };
  3764. static void xpmholder_free(XpmHolder *xh)
  3765. {
  3766. for (size_t i = 0; i < xh->nstrings; i++)
  3767. sfree(xh->strings[i]);
  3768. sfree(xh->strings);
  3769. sfree(xh);
  3770. }
  3771. static XpmHolder *xpm_scale(const char *const *xpm, int wo, int ho)
  3772. {
  3773. /* Get image dimensions, # colours, and chars-per-pixel */
  3774. int wi = 0, hi = 0, nc = 0, cpp = 0;
  3775. int retd = sscanf(xpm[0], "%d %d %d %d", &wi, &hi, &nc, &cpp);
  3776. assert(retd == 4);
  3777. /* Make output XpmHolder */
  3778. XpmHolder *xh = snew(XpmHolder);
  3779. xh->nstrings = 1 + nc + ho;
  3780. xh->strings = snewn(xh->nstrings, char *);
  3781. /* Set up header */
  3782. xh->strings[0] = dupprintf("%d %d %d %d", wo, ho, nc, cpp);
  3783. for (int i = 0; i < nc; i++)
  3784. xh->strings[1 + i] = dupstr(xpm[1 + i]);
  3785. /* Scale image */
  3786. for (int yo = 0; yo < ho; yo++) {
  3787. int yi = yo * hi / ho;
  3788. char *ro = snewn(cpp * wo + 1, char);
  3789. ro[cpp * wo] = '\0';
  3790. xh->strings[1 + nc + yo] = ro;
  3791. const char *ri = xpm[1 + nc + yi];
  3792. for (int xo = 0; xo < wo; xo++) {
  3793. int xi = xo * wi / wo;
  3794. memcpy(ro + cpp * xo, ri + cpp * xi, cpp);
  3795. }
  3796. }
  3797. return xh;
  3798. }
  3799. #endif /* !GTK_CHECK_VERSION(2,0,0) */
  3800. static void gtkwin_draw_trust_sigil(TermWin *tw, int cx, int cy)
  3801. {
  3802. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  3803. int x = cx * inst->font_width + inst->window_border;
  3804. int y = cy * inst->font_height + inst->window_border;
  3805. int w = 2*inst->font_width, h = inst->font_height;
  3806. if (inst->trust_sigil_w != w || inst->trust_sigil_h != h ||
  3807. #if GTK_CHECK_VERSION(2,0,0)
  3808. !inst->trust_sigil_pb
  3809. #else
  3810. !inst->trust_sigil_pm
  3811. #endif
  3812. ) {
  3813. #if GTK_CHECK_VERSION(2,0,0)
  3814. if (inst->trust_sigil_pb)
  3815. g_object_unref(G_OBJECT(inst->trust_sigil_pb));
  3816. #else
  3817. if (inst->trust_sigil_pm)
  3818. gdk_pixmap_unref(inst->trust_sigil_pm);
  3819. #endif
  3820. int best_icon_index = 0;
  3821. unsigned score = UINT_MAX;
  3822. for (int i = 0; i < n_main_icon; i++) {
  3823. int iw, ih;
  3824. if (sscanf(main_icon[i][0], "%d %d", &iw, &ih) == 2) {
  3825. int this_excess = (iw + ih) - (w + h);
  3826. unsigned this_score = (abs(this_excess) |
  3827. (this_excess > 0 ? 0 : 0x80000000U));
  3828. if (this_score < score) {
  3829. best_icon_index = i;
  3830. score = this_score;
  3831. }
  3832. }
  3833. }
  3834. #if GTK_CHECK_VERSION(2,0,0)
  3835. GdkPixbuf *icon_unscaled = gdk_pixbuf_new_from_xpm_data(
  3836. (const gchar **)main_icon[best_icon_index]);
  3837. inst->trust_sigil_pb = gdk_pixbuf_scale_simple(
  3838. icon_unscaled, w, h, GDK_INTERP_BILINEAR);
  3839. g_object_unref(G_OBJECT(icon_unscaled));
  3840. #else
  3841. XpmHolder *xh = xpm_scale(main_icon[best_icon_index], w, h);
  3842. inst->trust_sigil_pm = gdk_pixmap_create_from_xpm_d(
  3843. gtk_widget_get_window(inst->window), NULL,
  3844. &inst->cols[258], xh->strings);
  3845. xpmholder_free(xh);
  3846. #endif
  3847. inst->trust_sigil_w = w;
  3848. inst->trust_sigil_h = h;
  3849. }
  3850. #ifdef DRAW_TEXT_GDK
  3851. if (inst->uctx.type == DRAWTYPE_GDK) {
  3852. #if GTK_CHECK_VERSION(2,0,0)
  3853. gdk_draw_pixbuf(inst->uctx.u.gdk.target, inst->uctx.u.gdk.gc,
  3854. inst->trust_sigil_pb, 0, 0, x, y, w, h,
  3855. GDK_RGB_DITHER_NORMAL, 0, 0);
  3856. #else
  3857. gdk_draw_pixmap(inst->uctx.u.gdk.target, inst->uctx.u.gdk.gc,
  3858. inst->trust_sigil_pm, 0, 0, x, y, w, h);
  3859. #endif
  3860. }
  3861. #endif
  3862. #ifdef DRAW_TEXT_CAIRO
  3863. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3864. inst->uctx.u.cairo.widget = GTK_WIDGET(inst->area);
  3865. cairo_save(inst->uctx.u.cairo.cr);
  3866. cairo_translate(inst->uctx.u.cairo.cr, x, y);
  3867. gdk_cairo_set_source_pixbuf(inst->uctx.u.cairo.cr,
  3868. inst->trust_sigil_pb, 0, 0);
  3869. cairo_rectangle(inst->uctx.u.cairo.cr, 0, 0, w, h);
  3870. cairo_fill(inst->uctx.u.cairo.cr);
  3871. cairo_restore(inst->uctx.u.cairo.cr);
  3872. }
  3873. #endif
  3874. draw_update(inst, x, y, w, h);
  3875. }
  3876. GdkCursor *make_mouse_ptr(GtkFrontend *inst, int cursor_val)
  3877. {
  3878. if (cursor_val == -1) {
  3879. #if GTK_CHECK_VERSION(2,16,0)
  3880. cursor_val = GDK_BLANK_CURSOR;
  3881. #else
  3882. /*
  3883. * Work around absence of GDK_BLANK_CURSOR by inventing a
  3884. * blank pixmap.
  3885. */
  3886. GdkCursor *ret;
  3887. GdkColor bg = { 0, 0, 0, 0 };
  3888. GdkPixmap *pm = gdk_pixmap_new(NULL, 1, 1, 1);
  3889. GdkGC *gc = gdk_gc_new(pm);
  3890. gdk_gc_set_foreground(gc, &bg);
  3891. gdk_draw_rectangle(pm, gc, 1, 0, 0, 1, 1);
  3892. gdk_gc_unref(gc);
  3893. ret = gdk_cursor_new_from_pixmap(pm, pm, &bg, &bg, 1, 1);
  3894. gdk_pixmap_unref(pm);
  3895. return ret;
  3896. #endif
  3897. }
  3898. return gdk_cursor_new(cursor_val);
  3899. }
  3900. void modalfatalbox(const char *p, ...)
  3901. {
  3902. va_list ap;
  3903. fprintf(stderr, "FATAL ERROR: ");
  3904. va_start(ap, p);
  3905. vfprintf(stderr, p, ap);
  3906. va_end(ap);
  3907. fputc('\n', stderr);
  3908. exit(1);
  3909. }
  3910. #ifndef NOT_X_WINDOWS
  3911. static const char *gtk_seat_get_display(Seat *seat, SeatDisplayType dtype)
  3912. {
  3913. if (dtype == SDISP_X11 && !GDK_IS_X11_DISPLAY(gdk_display_get_default()))
  3914. return NULL;
  3915. return gdk_get_display();
  3916. }
  3917. static bool gtk_seat_get_windowid(Seat *seat, long *id)
  3918. {
  3919. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  3920. GdkWindow *window = gtk_widget_get_window(inst->area);
  3921. if (!GDK_IS_X11_WINDOW(window))
  3922. return false;
  3923. *id = GDK_WINDOW_XID(window);
  3924. return true;
  3925. }
  3926. #endif
  3927. char *setup_fonts_ucs(GtkFrontend *inst, Conf *conf)
  3928. {
  3929. bool shadowbold = conf_get_bool(conf, CONF_shadowbold);
  3930. int shadowboldoffset = conf_get_int(conf, CONF_shadowboldoffset);
  3931. FontSpec *fs;
  3932. unifont *fonts[4];
  3933. int i;
  3934. fs = conf_get_fontspec(conf, CONF_font);
  3935. fonts[0] = multifont_create(inst->area, fs->name, false, false,
  3936. shadowboldoffset, shadowbold);
  3937. if (!fonts[0]) {
  3938. return dupprintf("unable to load font \"%s\"", fs->name);
  3939. }
  3940. fs = conf_get_fontspec(conf, CONF_boldfont);
  3941. if (shadowbold || !fs->name[0]) {
  3942. fonts[1] = NULL;
  3943. } else {
  3944. fonts[1] = multifont_create(inst->area, fs->name, false, true,
  3945. shadowboldoffset, shadowbold);
  3946. if (!fonts[1]) {
  3947. if (fonts[0])
  3948. unifont_destroy(fonts[0]);
  3949. return dupprintf("unable to load bold font \"%s\"", fs->name);
  3950. }
  3951. }
  3952. fs = conf_get_fontspec(conf, CONF_widefont);
  3953. if (fs->name[0]) {
  3954. fonts[2] = multifont_create(inst->area, fs->name, true, false,
  3955. shadowboldoffset, shadowbold);
  3956. if (!fonts[2]) {
  3957. for (i = 0; i < 2; i++)
  3958. if (fonts[i])
  3959. unifont_destroy(fonts[i]);
  3960. return dupprintf("unable to load wide font \"%s\"", fs->name);
  3961. }
  3962. } else {
  3963. fonts[2] = NULL;
  3964. }
  3965. fs = conf_get_fontspec(conf, CONF_wideboldfont);
  3966. if (shadowbold || !fs->name[0]) {
  3967. fonts[3] = NULL;
  3968. } else {
  3969. fonts[3] = multifont_create(inst->area, fs->name, true, true,
  3970. shadowboldoffset, shadowbold);
  3971. if (!fonts[3]) {
  3972. for (i = 0; i < 3; i++)
  3973. if (fonts[i])
  3974. unifont_destroy(fonts[i]);
  3975. return dupprintf("unable to load wide bold font \"%s\"", fs->name);
  3976. }
  3977. }
  3978. /*
  3979. * Now we've got past all the possible error conditions, we can
  3980. * actually update our state.
  3981. */
  3982. for (i = 0; i < 4; i++) {
  3983. if (inst->fonts[i])
  3984. unifont_destroy(inst->fonts[i]);
  3985. inst->fonts[i] = fonts[i];
  3986. }
  3987. if (inst->font_width != inst->fonts[0]->width ||
  3988. inst->font_height != inst->fonts[0]->height) {
  3989. inst->font_width = inst->fonts[0]->width;
  3990. inst->font_height = inst->fonts[0]->height;
  3991. /*
  3992. * The font size has changed, so force the next call to
  3993. * drawing_area_setup to regenerate the backing surface.
  3994. */
  3995. inst->drawing_area_setup_needed = true;
  3996. }
  3997. inst->direct_to_font = init_ucs(&inst->ucsdata,
  3998. conf_get_str(inst->conf, CONF_line_codepage),
  3999. conf_get_bool(inst->conf, CONF_utf8_override),
  4000. inst->fonts[0]->public_charset,
  4001. conf_get_int(inst->conf, CONF_vtmode));
  4002. inst->drawtype = inst->fonts[0]->preferred_drawtype;
  4003. return NULL;
  4004. }
  4005. #if GTK_CHECK_VERSION(3,0,0)
  4006. struct find_app_menu_bar_ctx {
  4007. GtkWidget *area, *menubar;
  4008. };
  4009. static void find_app_menu_bar(GtkWidget *widget, gpointer data)
  4010. {
  4011. struct find_app_menu_bar_ctx *ctx = (struct find_app_menu_bar_ctx *)data;
  4012. if (widget != ctx->area && GTK_IS_MENU_BAR(widget))
  4013. ctx->menubar = widget;
  4014. }
  4015. #endif
  4016. static void compute_geom_hints(GtkFrontend *inst, GdkGeometry *geom)
  4017. {
  4018. /*
  4019. * Unused fields in geom.
  4020. */
  4021. geom->max_width = geom->max_height = -1;
  4022. geom->min_aspect = geom->max_aspect = 0;
  4023. /*
  4024. * Set up the geometry fields we care about, with reference to
  4025. * just the drawing area. We'll correct for other widgets in a
  4026. * moment.
  4027. */
  4028. geom->min_width = inst->font_width + 2*inst->window_border;
  4029. geom->min_height = inst->font_height + 2*inst->window_border;
  4030. geom->base_width = 2*inst->window_border;
  4031. geom->base_height = 2*inst->window_border;
  4032. geom->width_inc = inst->font_width;
  4033. geom->height_inc = inst->font_height;
  4034. /*
  4035. * If we've got a scrollbar visible, then we must include its
  4036. * width as part of the base and min width, and also ensure that
  4037. * our window's minimum height is at least the height required by
  4038. * the scrollbar.
  4039. *
  4040. * In the latter case, we must also take care to arrange that
  4041. * (geom->min_height - geom->base_height) is an integer multiple of
  4042. * geom->height_inc, because if it's not, then some window managers
  4043. * (we know of xfwm4) get confused, with the effect that they
  4044. * resize our window to a height based on min_height instead of
  4045. * base_height, which we then round down and the window ends up
  4046. * too short.
  4047. */
  4048. if (inst->sbar_visible) {
  4049. GtkRequisition req;
  4050. int min_sb_height;
  4051. #if GTK_CHECK_VERSION(3,0,0)
  4052. gtk_widget_get_preferred_size(inst->sbar, &req, NULL);
  4053. #else
  4054. gtk_widget_size_request(inst->sbar, &req);
  4055. #endif
  4056. /* Compute rounded-up scrollbar height. */
  4057. min_sb_height = req.height;
  4058. min_sb_height += geom->height_inc - 1;
  4059. min_sb_height -= ((min_sb_height - geom->base_height%geom->height_inc)
  4060. % geom->height_inc);
  4061. geom->min_width += req.width;
  4062. geom->base_width += req.width;
  4063. if (geom->min_height < min_sb_height)
  4064. geom->min_height = min_sb_height;
  4065. }
  4066. #if GTK_CHECK_VERSION(3,0,0)
  4067. /*
  4068. * And if we're running a main-gtk-application.c based program and
  4069. * GtkApplicationWindow has given us a menu bar inside the window,
  4070. * then we must take that into account as well.
  4071. *
  4072. * In its unbounded wisdom, GtkApplicationWindow doesn't actually
  4073. * give us a direct function call to _find_ the menu bar widget.
  4074. * Fortunately, we can find it by enumerating the children of the
  4075. * top-level window and looking for one we didn't put there
  4076. * ourselves.
  4077. */
  4078. {
  4079. struct find_app_menu_bar_ctx ctx[1];
  4080. ctx->area = inst->area;
  4081. ctx->menubar = NULL;
  4082. gtk_container_foreach(GTK_CONTAINER(inst->window),
  4083. find_app_menu_bar, ctx);
  4084. if (ctx->menubar) {
  4085. GtkRequisition req;
  4086. int min_menu_width;
  4087. gtk_widget_get_preferred_size(ctx->menubar, NULL, &req);
  4088. /*
  4089. * This time, the height adjustment is easy (the menu bar
  4090. * sits above everything), but we have to take care with
  4091. * the _width_ to ensure we keep min_width and base_width
  4092. * congruent modulo width_inc.
  4093. */
  4094. geom->min_height += req.height;
  4095. geom->base_height += req.height;
  4096. min_menu_width = req.width;
  4097. min_menu_width += geom->width_inc - 1;
  4098. min_menu_width -=
  4099. ((min_menu_width - geom->base_width%geom->width_inc)
  4100. % geom->width_inc);
  4101. if (geom->min_width < min_menu_width)
  4102. geom->min_width = min_menu_width;
  4103. }
  4104. }
  4105. #endif
  4106. }
  4107. void set_geom_hints(GtkFrontend *inst)
  4108. {
  4109. /*
  4110. * 2021-12-20: I've found that on Ubuntu 20.04 Wayland (using GTK
  4111. * 3.24.20), setting geometry hints causes the window size to come
  4112. * out wrong. As far as I can tell, that's because the GDK Wayland
  4113. * backend internally considers windows to be a lot larger than
  4114. * their obvious display size (*even* considering visible window
  4115. * furniture like title bars), with an extra margin on every side
  4116. * to account for surrounding effects like shadows. And the
  4117. * geometry hints like base size and resize increment are applied
  4118. * to that larger size rather than the more obvious 'client area'
  4119. * size. So when we ask for a window of exactly the size we want,
  4120. * it gets modified by GDK based on the geometry hints, but
  4121. * applying this extra margin, which causes the size to be a
  4122. * little bit too small.
  4123. *
  4124. * I don't know how you can sensibly find out the size of that
  4125. * margin. If I did, I could account for it in the geometry hints.
  4126. * But I also see that gtk_window_set_geometry_hints is removed in
  4127. * GTK 4, which suggests that probably doing a lot of hard work to
  4128. * fix this is not the way forward.
  4129. *
  4130. * So instead, I simply avoid setting geometry hints at all on any
  4131. * GDK backend other than X11, and hopefully that's a workaround.
  4132. */
  4133. #if GTK_CHECK_VERSION(3,0,0) && !defined NOT_X_WINDOWS
  4134. if (!GDK_IS_X11_DISPLAY(gdk_display_get_default()))
  4135. return;
  4136. #endif
  4137. const struct BackendVtable *vt;
  4138. GdkGeometry geom;
  4139. gint flags = GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE | GDK_HINT_RESIZE_INC;
  4140. compute_geom_hints(inst, &geom);
  4141. #if GTK_CHECK_VERSION(2,0,0)
  4142. if (inst->gotpos)
  4143. flags |= GDK_HINT_USER_POS;
  4144. #endif
  4145. vt = backend_vt_from_proto(conf_get_int(inst->conf, CONF_protocol));
  4146. if (vt && vt->flags & BACKEND_RESIZE_FORBIDDEN) {
  4147. /* Window resizing forbidden. Set both minimum and maximum
  4148. * dimensions to be the initial size. */
  4149. geom.min_width = geom.base_width + geom.width_inc * inst->width;
  4150. geom.min_height = geom.base_height + geom.height_inc * inst->height;
  4151. geom.max_width = geom.min_width;
  4152. geom.max_height = geom.min_height;
  4153. flags |= GDK_HINT_MAX_SIZE;
  4154. }
  4155. gtk_window_set_geometry_hints(GTK_WINDOW(inst->window),
  4156. NULL, &geom, flags);
  4157. }
  4158. #if GTK_CHECK_VERSION(2,0,0)
  4159. static void compute_whole_window_size(GtkFrontend *inst,
  4160. int wchars, int hchars,
  4161. int *wpix, int *hpix)
  4162. {
  4163. GdkGeometry geom;
  4164. compute_geom_hints(inst, &geom);
  4165. if (wpix) *wpix = geom.base_width + wchars * geom.width_inc;
  4166. if (hpix) *hpix = geom.base_height + hchars * geom.height_inc;
  4167. }
  4168. #endif
  4169. void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
  4170. {
  4171. GtkFrontend *inst = (GtkFrontend *)data;
  4172. term_clrsb(inst->term);
  4173. }
  4174. void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
  4175. {
  4176. GtkFrontend *inst = (GtkFrontend *)data;
  4177. term_pwron(inst->term, true);
  4178. if (inst->ldisc)
  4179. ldisc_echoedit_update(inst->ldisc);
  4180. }
  4181. void copy_clipboard_menuitem(GtkMenuItem *item, gpointer data)
  4182. {
  4183. GtkFrontend *inst = (GtkFrontend *)data;
  4184. static const int clips[] = { MENU_CLIPBOARD };
  4185. term_request_copy(inst->term, clips, lenof(clips));
  4186. }
  4187. void paste_clipboard_menuitem(GtkMenuItem *item, gpointer data)
  4188. {
  4189. GtkFrontend *inst = (GtkFrontend *)data;
  4190. term_request_paste(inst->term, MENU_CLIPBOARD);
  4191. }
  4192. void copy_all_menuitem(GtkMenuItem *item, gpointer data)
  4193. {
  4194. GtkFrontend *inst = (GtkFrontend *)data;
  4195. static const int clips[] = { COPYALL_CLIPBOARDS };
  4196. term_copyall(inst->term, clips, lenof(clips));
  4197. }
  4198. void special_menuitem(GtkMenuItem *item, gpointer data)
  4199. {
  4200. GtkFrontend *inst = (GtkFrontend *)data;
  4201. SessionSpecial *sc = g_object_get_data(G_OBJECT(item), "user-data");
  4202. if (inst->backend)
  4203. backend_special(inst->backend, sc->code, sc->arg);
  4204. }
  4205. void about_menuitem(GtkMenuItem *item, gpointer data)
  4206. {
  4207. GtkFrontend *inst = (GtkFrontend *)data;
  4208. about_box(inst->window);
  4209. }
  4210. void event_log_menuitem(GtkMenuItem *item, gpointer data)
  4211. {
  4212. GtkFrontend *inst = (GtkFrontend *)data;
  4213. showeventlog(inst->eventlogstuff, inst->window);
  4214. }
  4215. void setup_clipboards(GtkFrontend *inst, Terminal *term, Conf *conf)
  4216. {
  4217. assert(term->mouse_select_clipboards[0] == CLIP_LOCAL);
  4218. term->n_mouse_select_clipboards = 1;
  4219. term->mouse_select_clipboards[
  4220. term->n_mouse_select_clipboards++] = MOUSE_SELECT_CLIPBOARD;
  4221. if (conf_get_bool(conf, CONF_mouseautocopy)) {
  4222. term->mouse_select_clipboards[
  4223. term->n_mouse_select_clipboards++] = CLIP_CLIPBOARD;
  4224. }
  4225. set_clipboard_atom(inst, CLIP_CUSTOM_1, GDK_NONE);
  4226. set_clipboard_atom(inst, CLIP_CUSTOM_2, GDK_NONE);
  4227. set_clipboard_atom(inst, CLIP_CUSTOM_3, GDK_NONE);
  4228. switch (conf_get_int(conf, CONF_mousepaste)) {
  4229. case CLIPUI_IMPLICIT:
  4230. term->mouse_paste_clipboard = MOUSE_PASTE_CLIPBOARD;
  4231. break;
  4232. case CLIPUI_EXPLICIT:
  4233. term->mouse_paste_clipboard = CLIP_CLIPBOARD;
  4234. break;
  4235. case CLIPUI_CUSTOM:
  4236. term->mouse_paste_clipboard = CLIP_CUSTOM_1;
  4237. set_clipboard_atom(inst, CLIP_CUSTOM_1,
  4238. gdk_atom_intern(
  4239. conf_get_str(conf, CONF_mousepaste_custom),
  4240. false));
  4241. break;
  4242. default:
  4243. term->mouse_paste_clipboard = CLIP_NULL;
  4244. break;
  4245. }
  4246. if (conf_get_int(conf, CONF_ctrlshiftins) == CLIPUI_CUSTOM) {
  4247. GdkAtom atom = gdk_atom_intern(
  4248. conf_get_str(conf, CONF_ctrlshiftins_custom), false);
  4249. struct clipboard_state *state = clipboard_from_atom(inst, atom);
  4250. if (state) {
  4251. inst->clipboard_ctrlshiftins = state->clipboard;
  4252. } else {
  4253. inst->clipboard_ctrlshiftins = CLIP_CUSTOM_2;
  4254. set_clipboard_atom(inst, CLIP_CUSTOM_2, atom);
  4255. }
  4256. }
  4257. if (conf_get_int(conf, CONF_ctrlshiftcv) == CLIPUI_CUSTOM) {
  4258. GdkAtom atom = gdk_atom_intern(
  4259. conf_get_str(conf, CONF_ctrlshiftcv_custom), false);
  4260. struct clipboard_state *state = clipboard_from_atom(inst, atom);
  4261. if (state) {
  4262. inst->clipboard_ctrlshiftins = state->clipboard;
  4263. } else {
  4264. inst->clipboard_ctrlshiftcv = CLIP_CUSTOM_3;
  4265. set_clipboard_atom(inst, CLIP_CUSTOM_3, atom);
  4266. }
  4267. }
  4268. }
  4269. struct after_change_settings_dialog_ctx {
  4270. GtkFrontend *inst;
  4271. Conf *newconf;
  4272. };
  4273. static void after_change_settings_dialog(void *vctx, int retval);
  4274. void change_settings_menuitem(GtkMenuItem *item, gpointer data)
  4275. {
  4276. GtkFrontend *inst = (GtkFrontend *)data;
  4277. struct after_change_settings_dialog_ctx *ctx;
  4278. GtkWidget *dialog;
  4279. char *title;
  4280. if (find_and_raise_dialog(inst, DIALOG_SLOT_RECONFIGURE))
  4281. return;
  4282. title = dupcat(appname, " Reconfiguration");
  4283. ctx = snew(struct after_change_settings_dialog_ctx);
  4284. ctx->inst = inst;
  4285. ctx->newconf = conf_copy(inst->conf);
  4286. term_pre_reconfig(inst->term, ctx->newconf);
  4287. dialog = create_config_box(
  4288. title, ctx->newconf, true,
  4289. inst->backend ? backend_cfg_info(inst->backend) : 0,
  4290. after_change_settings_dialog, ctx);
  4291. register_dialog(&inst->seat, DIALOG_SLOT_RECONFIGURE, dialog);
  4292. sfree(title);
  4293. }
  4294. static void after_change_settings_dialog(void *vctx, int retval)
  4295. {
  4296. struct after_change_settings_dialog_ctx ctx =
  4297. *(struct after_change_settings_dialog_ctx *)vctx;
  4298. GtkFrontend *inst = ctx.inst;
  4299. Conf *oldconf = inst->conf, *newconf = ctx.newconf;
  4300. bool need_size;
  4301. sfree(vctx); /* we've copied this already */
  4302. unregister_dialog(&inst->seat, DIALOG_SLOT_RECONFIGURE);
  4303. if (retval > 0) {
  4304. inst->conf = newconf;
  4305. /* Pass new config data to the logging module */
  4306. log_reconfig(inst->logctx, inst->conf);
  4307. /*
  4308. * Flush the line discipline's edit buffer in the case
  4309. * where local editing has just been disabled.
  4310. */
  4311. if (inst->ldisc) {
  4312. ldisc_configure(inst->ldisc, inst->conf);
  4313. ldisc_echoedit_update(inst->ldisc);
  4314. }
  4315. /* Pass new config data to the terminal */
  4316. term_reconfig(inst->term, inst->conf);
  4317. setup_clipboards(inst, inst->term, inst->conf);
  4318. /* Pass new config data to the back end */
  4319. if (inst->backend)
  4320. backend_reconfig(inst->backend, inst->conf);
  4321. cache_conf_values(inst);
  4322. need_size = false;
  4323. /*
  4324. * If the scrollbar needs to be shown, hidden, or moved
  4325. * from one end to the other of the window, do so now.
  4326. */
  4327. if (conf_get_bool(oldconf, CONF_scrollbar) !=
  4328. conf_get_bool(newconf, CONF_scrollbar)) {
  4329. show_scrollbar(inst, conf_get_bool(newconf, CONF_scrollbar));
  4330. need_size = true;
  4331. }
  4332. if (conf_get_bool(oldconf, CONF_scrollbar_on_left) !=
  4333. conf_get_bool(newconf, CONF_scrollbar_on_left)) {
  4334. gtk_box_reorder_child(inst->hbox, inst->sbar,
  4335. conf_get_bool(newconf, CONF_scrollbar_on_left)
  4336. ? 0 : 1);
  4337. }
  4338. /*
  4339. * Redo the whole tangled fonts and Unicode mess if
  4340. * necessary.
  4341. */
  4342. if (strcmp(conf_get_fontspec(oldconf, CONF_font)->name,
  4343. conf_get_fontspec(newconf, CONF_font)->name) ||
  4344. strcmp(conf_get_fontspec(oldconf, CONF_boldfont)->name,
  4345. conf_get_fontspec(newconf, CONF_boldfont)->name) ||
  4346. strcmp(conf_get_fontspec(oldconf, CONF_widefont)->name,
  4347. conf_get_fontspec(newconf, CONF_widefont)->name) ||
  4348. strcmp(conf_get_fontspec(oldconf, CONF_wideboldfont)->name,
  4349. conf_get_fontspec(newconf, CONF_wideboldfont)->name) ||
  4350. strcmp(conf_get_str(oldconf, CONF_line_codepage),
  4351. conf_get_str(newconf, CONF_line_codepage)) ||
  4352. conf_get_bool(oldconf, CONF_utf8_override) !=
  4353. conf_get_bool(newconf, CONF_utf8_override) ||
  4354. conf_get_int(oldconf, CONF_vtmode) !=
  4355. conf_get_int(newconf, CONF_vtmode) ||
  4356. conf_get_bool(oldconf, CONF_shadowbold) !=
  4357. conf_get_bool(newconf, CONF_shadowbold) ||
  4358. conf_get_int(oldconf, CONF_shadowboldoffset) !=
  4359. conf_get_int(newconf, CONF_shadowboldoffset)) {
  4360. char *errmsg = setup_fonts_ucs(inst, inst->conf);
  4361. if (errmsg) {
  4362. char *msgboxtext =
  4363. dupprintf("Could not change fonts in terminal window: %s\n",
  4364. errmsg);
  4365. create_message_box(
  4366. inst->window, "Font setup error", msgboxtext,
  4367. string_width("Could not change fonts in terminal window:"),
  4368. false, &buttons_ok, trivial_post_dialog_fn, NULL);
  4369. sfree(msgboxtext);
  4370. sfree(errmsg);
  4371. } else {
  4372. need_size = true;
  4373. }
  4374. }
  4375. /*
  4376. * Resize the window.
  4377. */
  4378. if (conf_get_int(oldconf, CONF_width) !=
  4379. conf_get_int(newconf, CONF_width) ||
  4380. conf_get_int(oldconf, CONF_height) !=
  4381. conf_get_int(newconf, CONF_height) ||
  4382. conf_get_int(oldconf, CONF_window_border) !=
  4383. conf_get_int(newconf, CONF_window_border) ||
  4384. need_size) {
  4385. set_geom_hints(inst);
  4386. request_resize_internal(inst, false,
  4387. conf_get_int(newconf, CONF_width),
  4388. conf_get_int(newconf, CONF_height));
  4389. } else {
  4390. /*
  4391. * The above will have caused a call to term_size() for
  4392. * us if it happened. If the user has fiddled with only
  4393. * the scrollback size, the above will not have
  4394. * happened and we will need an explicit term_size()
  4395. * here.
  4396. */
  4397. if (conf_get_int(oldconf, CONF_savelines) !=
  4398. conf_get_int(newconf, CONF_savelines))
  4399. term_size(inst->term, inst->term->rows, inst->term->cols,
  4400. conf_get_int(newconf, CONF_savelines));
  4401. }
  4402. term_invalidate(inst->term);
  4403. /*
  4404. * We do an explicit full redraw here to ensure the window
  4405. * border has been redrawn as well as the text area.
  4406. */
  4407. gtk_widget_queue_draw(inst->area);
  4408. conf_free(oldconf);
  4409. } else {
  4410. conf_free(newconf);
  4411. }
  4412. }
  4413. static void change_font_size(GtkFrontend *inst, int increment)
  4414. {
  4415. static const int conf_keys[lenof(inst->fonts)] = {
  4416. CONF_font, CONF_boldfont, CONF_widefont, CONF_wideboldfont,
  4417. };
  4418. FontSpec *oldfonts[lenof(inst->fonts)];
  4419. FontSpec *newfonts[lenof(inst->fonts)];
  4420. char *errmsg = NULL;
  4421. int i;
  4422. for (i = 0; i < lenof(newfonts); i++)
  4423. oldfonts[i] = newfonts[i] = NULL;
  4424. for (i = 0; i < lenof(inst->fonts); i++) {
  4425. if (inst->fonts[i]) {
  4426. char *newname = unifont_size_increment(inst->fonts[i], increment);
  4427. if (!newname)
  4428. goto cleanup;
  4429. newfonts[i] = fontspec_new(newname);
  4430. sfree(newname);
  4431. }
  4432. }
  4433. for (i = 0; i < lenof(newfonts); i++) {
  4434. if (newfonts[i]) {
  4435. oldfonts[i] = fontspec_copy(
  4436. conf_get_fontspec(inst->conf, conf_keys[i]));
  4437. conf_set_fontspec(inst->conf, conf_keys[i], newfonts[i]);
  4438. }
  4439. }
  4440. errmsg = setup_fonts_ucs(inst, inst->conf);
  4441. if (errmsg)
  4442. goto cleanup;
  4443. /* Success, so suppress putting everything back */
  4444. for (i = 0; i < lenof(newfonts); i++) {
  4445. if (oldfonts[i]) {
  4446. fontspec_free(oldfonts[i]);
  4447. oldfonts[i] = NULL;
  4448. }
  4449. }
  4450. set_geom_hints(inst);
  4451. request_resize_internal(inst, false, conf_get_int(inst->conf, CONF_width),
  4452. conf_get_int(inst->conf, CONF_height));
  4453. term_invalidate(inst->term);
  4454. gtk_widget_queue_draw(inst->area);
  4455. cleanup:
  4456. for (i = 0; i < lenof(oldfonts); i++) {
  4457. if (oldfonts[i]) {
  4458. conf_set_fontspec(inst->conf, conf_keys[i], oldfonts[i]);
  4459. fontspec_free(oldfonts[i]);
  4460. }
  4461. if (newfonts[i])
  4462. fontspec_free(newfonts[i]);
  4463. }
  4464. sfree(errmsg);
  4465. }
  4466. void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
  4467. {
  4468. GtkFrontend *inst = (GtkFrontend *)gdata;
  4469. launch_duplicate_session(inst->conf);
  4470. }
  4471. void new_session_menuitem(GtkMenuItem *item, gpointer data)
  4472. {
  4473. launch_new_session();
  4474. }
  4475. void restart_session_menuitem(GtkMenuItem *item, gpointer data)
  4476. {
  4477. GtkFrontend *inst = (GtkFrontend *)data;
  4478. if (!inst->backend) {
  4479. logevent(inst->logctx, "----- Session restarted -----");
  4480. term_pwron(inst->term, false);
  4481. start_backend(inst);
  4482. inst->exited = false;
  4483. }
  4484. }
  4485. void saved_session_menuitem(GtkMenuItem *item, gpointer data)
  4486. {
  4487. char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
  4488. launch_saved_session(str);
  4489. }
  4490. void saved_session_freedata(GtkMenuItem *item, gpointer data)
  4491. {
  4492. char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
  4493. sfree(str);
  4494. }
  4495. void app_menu_action(GtkFrontend *frontend, enum MenuAction action)
  4496. {
  4497. GtkFrontend *inst = (GtkFrontend *)frontend;
  4498. switch (action) {
  4499. case MA_COPY:
  4500. copy_clipboard_menuitem(NULL, inst);
  4501. break;
  4502. case MA_PASTE:
  4503. paste_clipboard_menuitem(NULL, inst);
  4504. break;
  4505. case MA_COPY_ALL:
  4506. copy_all_menuitem(NULL, inst);
  4507. break;
  4508. case MA_DUPLICATE_SESSION:
  4509. dup_session_menuitem(NULL, inst);
  4510. break;
  4511. case MA_RESTART_SESSION:
  4512. restart_session_menuitem(NULL, inst);
  4513. break;
  4514. case MA_CHANGE_SETTINGS:
  4515. change_settings_menuitem(NULL, inst);
  4516. break;
  4517. case MA_CLEAR_SCROLLBACK:
  4518. clear_scrollback_menuitem(NULL, inst);
  4519. break;
  4520. case MA_RESET_TERMINAL:
  4521. reset_terminal_menuitem(NULL, inst);
  4522. break;
  4523. case MA_EVENT_LOG:
  4524. event_log_menuitem(NULL, inst);
  4525. break;
  4526. }
  4527. }
  4528. static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
  4529. {
  4530. GtkFrontend *inst = (GtkFrontend *)data;
  4531. struct sesslist sesslist;
  4532. int i;
  4533. gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
  4534. (GtkCallback)gtk_widget_destroy, NULL);
  4535. get_sesslist(&sesslist, true);
  4536. /* skip sesslist.sessions[0] == Default Settings */
  4537. for (i = 1; i < sesslist.nsessions; i++) {
  4538. GtkWidget *menuitem =
  4539. gtk_menu_item_new_with_label(sesslist.sessions[i]);
  4540. gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
  4541. gtk_widget_show(menuitem);
  4542. g_object_set_data(G_OBJECT(menuitem), "user-data",
  4543. dupstr(sesslist.sessions[i]));
  4544. g_signal_connect(G_OBJECT(menuitem), "activate",
  4545. G_CALLBACK(saved_session_menuitem),
  4546. inst);
  4547. g_signal_connect(G_OBJECT(menuitem), "destroy",
  4548. G_CALLBACK(saved_session_freedata),
  4549. inst);
  4550. }
  4551. if (sesslist.nsessions <= 1) {
  4552. GtkWidget *menuitem =
  4553. gtk_menu_item_new_with_label("(No sessions)");
  4554. gtk_widget_set_sensitive(menuitem, false);
  4555. gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
  4556. gtk_widget_show(menuitem);
  4557. }
  4558. get_sesslist(&sesslist, false); /* free up */
  4559. }
  4560. void set_window_icon(GtkWidget *window, const char *const *const *icon,
  4561. int n_icon)
  4562. {
  4563. #if GTK_CHECK_VERSION(2,0,0)
  4564. GList *iconlist;
  4565. int n;
  4566. #else
  4567. GdkPixmap *iconpm;
  4568. GdkBitmap *iconmask;
  4569. #endif
  4570. if (!n_icon)
  4571. return;
  4572. gtk_widget_realize(window);
  4573. #if GTK_CHECK_VERSION(2,0,0)
  4574. gtk_window_set_icon(GTK_WINDOW(window),
  4575. gdk_pixbuf_new_from_xpm_data((const gchar **)icon[0]));
  4576. #else
  4577. iconpm = gdk_pixmap_create_from_xpm_d(gtk_widget_get_window(window),
  4578. &iconmask, NULL, (gchar **)icon[0]);
  4579. gdk_window_set_icon(gtk_widget_get_window(window), NULL, iconpm, iconmask);
  4580. #endif
  4581. #if GTK_CHECK_VERSION(2,0,0)
  4582. iconlist = NULL;
  4583. for (n = 0; n < n_icon; n++) {
  4584. iconlist =
  4585. g_list_append(iconlist,
  4586. gdk_pixbuf_new_from_xpm_data((const gchar **)
  4587. icon[n]));
  4588. }
  4589. gtk_window_set_icon_list(GTK_WINDOW(window), iconlist);
  4590. #endif
  4591. }
  4592. static void free_special_cmd(gpointer data) { sfree(data); }
  4593. static void gtk_seat_update_specials_menu(Seat *seat)
  4594. {
  4595. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  4596. const SessionSpecial *specials;
  4597. if (inst->backend)
  4598. specials = backend_get_specials(inst->backend);
  4599. else
  4600. specials = NULL;
  4601. /* I believe this disposes of submenus too. */
  4602. gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
  4603. (GtkCallback)gtk_widget_destroy, NULL);
  4604. if (specials) {
  4605. int i;
  4606. GtkWidget *menu = inst->specialsmenu;
  4607. /* A lame "stack" for submenus that will do for now. */
  4608. GtkWidget *saved_menu = NULL;
  4609. int nesting = 1;
  4610. for (i = 0; nesting > 0; i++) {
  4611. GtkWidget *menuitem = NULL;
  4612. switch (specials[i].code) {
  4613. case SS_SUBMENU:
  4614. assert (nesting < 2);
  4615. saved_menu = menu; /* XXX lame stacking */
  4616. menu = gtk_menu_new();
  4617. menuitem = gtk_menu_item_new_with_label(specials[i].name);
  4618. gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
  4619. gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
  4620. gtk_widget_show(menuitem);
  4621. menuitem = NULL;
  4622. nesting++;
  4623. break;
  4624. case SS_EXITMENU:
  4625. nesting--;
  4626. if (nesting) {
  4627. menu = saved_menu; /* XXX lame stacking */
  4628. saved_menu = NULL;
  4629. }
  4630. break;
  4631. case SS_SEP:
  4632. menuitem = gtk_menu_item_new();
  4633. break;
  4634. default: {
  4635. menuitem = gtk_menu_item_new_with_label(specials[i].name);
  4636. SessionSpecial *sc = snew(SessionSpecial);
  4637. *sc = specials[i]; /* structure copy */
  4638. g_object_set_data_full(G_OBJECT(menuitem), "user-data",
  4639. sc, free_special_cmd);
  4640. g_signal_connect(G_OBJECT(menuitem), "activate",
  4641. G_CALLBACK(special_menuitem), inst);
  4642. break;
  4643. }
  4644. }
  4645. if (menuitem) {
  4646. gtk_container_add(GTK_CONTAINER(menu), menuitem);
  4647. gtk_widget_show(menuitem);
  4648. }
  4649. }
  4650. gtk_widget_show(inst->specialsitem1);
  4651. gtk_widget_show(inst->specialsitem2);
  4652. } else {
  4653. gtk_widget_hide(inst->specialsitem1);
  4654. gtk_widget_hide(inst->specialsitem2);
  4655. }
  4656. }
  4657. static void start_backend(GtkFrontend *inst)
  4658. {
  4659. const struct BackendVtable *vt;
  4660. char *error, *realhost;
  4661. inst->cmdline_get_passwd_state = cmdline_get_passwd_input_state_new;
  4662. vt = select_backend(inst->conf);
  4663. seat_set_trust_status(&inst->seat, true);
  4664. error = backend_init(vt, &inst->seat, &inst->backend,
  4665. inst->logctx, inst->conf,
  4666. conf_get_str(inst->conf, CONF_host),
  4667. conf_get_int(inst->conf, CONF_port),
  4668. &realhost,
  4669. conf_get_bool(inst->conf, CONF_tcp_nodelay),
  4670. conf_get_bool(inst->conf, CONF_tcp_keepalives));
  4671. if (error) {
  4672. if (cmdline_tooltype & TOOLTYPE_NONNETWORK) {
  4673. /* Special case for pterm. */
  4674. seat_connection_fatal(&inst->seat,
  4675. "Unable to open terminal:\n%s",
  4676. error);
  4677. } else {
  4678. seat_connection_fatal(&inst->seat,
  4679. "Unable to open connection to %s:\n%s",
  4680. conf_dest(inst->conf), error);
  4681. }
  4682. sfree(error);
  4683. inst->exited = true;
  4684. return;
  4685. }
  4686. term_setup_window_titles(inst->term, realhost);
  4687. sfree(realhost);
  4688. term_provide_backend(inst->term, inst->backend);
  4689. inst->ldisc = ldisc_create(inst->conf, inst->term, inst->backend,
  4690. &inst->seat);
  4691. gtk_widget_set_sensitive(inst->restartitem, false);
  4692. }
  4693. #if GTK_CHECK_VERSION(2,0,0)
  4694. static void get_monitor_geometry(GtkWidget *widget, GdkRectangle *geometry)
  4695. {
  4696. #if GTK_CHECK_VERSION(3,4,0)
  4697. GdkDisplay *display = gtk_widget_get_display(widget);
  4698. GdkWindow *gdkwindow = gtk_widget_get_window(widget);
  4699. # if GTK_CHECK_VERSION(3,22,0)
  4700. GdkMonitor *monitor;
  4701. if (gdkwindow)
  4702. monitor = gdk_display_get_monitor_at_window(display, gdkwindow);
  4703. else
  4704. monitor = gdk_display_get_monitor(display, 0);
  4705. gdk_monitor_get_geometry(monitor, geometry);
  4706. # else
  4707. GdkScreen *screen = gdk_display_get_default_screen(display);
  4708. gint monitor_num = gdk_screen_get_monitor_at_window(screen, gdkwindow);
  4709. gdk_screen_get_monitor_geometry(screen, monitor_num, geometry);
  4710. # endif
  4711. #else
  4712. geometry->x = geometry->y = 0;
  4713. geometry->width = gdk_screen_width();
  4714. geometry->height = gdk_screen_height();
  4715. #endif
  4716. }
  4717. #endif
  4718. static const TermWinVtable gtk_termwin_vt = {
  4719. .setup_draw_ctx = gtkwin_setup_draw_ctx,
  4720. .draw_text = gtkwin_draw_text,
  4721. .draw_cursor = gtkwin_draw_cursor,
  4722. .draw_trust_sigil = gtkwin_draw_trust_sigil,
  4723. .char_width = gtkwin_char_width,
  4724. .free_draw_ctx = gtkwin_free_draw_ctx,
  4725. .set_cursor_pos = gtkwin_set_cursor_pos,
  4726. .set_raw_mouse_mode = gtkwin_set_raw_mouse_mode,
  4727. .set_raw_mouse_mode_pointer = gtkwin_set_raw_mouse_mode_pointer,
  4728. .set_scrollbar = gtkwin_set_scrollbar,
  4729. .bell = gtkwin_bell,
  4730. .clip_write = gtkwin_clip_write,
  4731. .clip_request_paste = gtkwin_clip_request_paste,
  4732. .refresh = gtkwin_refresh,
  4733. .request_resize = gtkwin_request_resize,
  4734. .set_title = gtkwin_set_title,
  4735. .set_icon_title = gtkwin_set_icon_title,
  4736. .set_minimised = gtkwin_set_minimised,
  4737. .set_maximised = gtkwin_set_maximised,
  4738. .move = gtkwin_move,
  4739. .set_zorder = gtkwin_set_zorder,
  4740. .palette_set = gtkwin_palette_set,
  4741. .palette_get_overrides = gtkwin_palette_get_overrides,
  4742. .unthrottle = gtkwin_unthrottle,
  4743. };
  4744. void new_session_window(Conf *conf, const char *geometry_string)
  4745. {
  4746. GtkFrontend *inst;
  4747. prepare_session(conf);
  4748. /*
  4749. * Create an instance structure and initialise to zeroes
  4750. */
  4751. inst = snew(GtkFrontend);
  4752. memset(inst, 0, sizeof(*inst));
  4753. #ifdef JUST_USE_GTK_CLIPBOARD_UTF8
  4754. inst->cdi_headtail.next = inst->cdi_headtail.prev = &inst->cdi_headtail;
  4755. #endif
  4756. inst->alt_keycode = -1; /* this one needs _not_ to be zero */
  4757. inst->busy_status = BUSY_NOT;
  4758. inst->conf = conf;
  4759. inst->wintitle = inst->icontitle = NULL;
  4760. inst->drawtype = DRAWTYPE_DEFAULT;
  4761. #if GTK_CHECK_VERSION(3,4,0)
  4762. inst->cumulative_vscroll = 0.0;
  4763. inst->cumulative_hscroll = 0.0;
  4764. #endif
  4765. inst->drawing_area_setup_needed = true;
  4766. inst->termwin.vt = &gtk_termwin_vt;
  4767. inst->seat.vt = &gtk_seat_vt;
  4768. inst->logpolicy.vt = &gtk_logpolicy_vt;
  4769. #ifndef NOT_X_WINDOWS
  4770. inst->disp = get_x11_display();
  4771. if (geometry_string) {
  4772. int flags, x, y;
  4773. unsigned int w, h;
  4774. flags = XParseGeometry(geometry_string, &x, &y, &w, &h);
  4775. if (flags & WidthValue)
  4776. conf_set_int(conf, CONF_width, w);
  4777. if (flags & HeightValue)
  4778. conf_set_int(conf, CONF_height, h);
  4779. if (flags & (XValue | YValue)) {
  4780. inst->xpos = x;
  4781. inst->ypos = y;
  4782. inst->gotpos = true;
  4783. inst->gravity = ((flags & XNegative ? 1 : 0) |
  4784. (flags & YNegative ? 2 : 0));
  4785. }
  4786. }
  4787. #endif
  4788. if (!compound_text_atom)
  4789. compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", false);
  4790. if (!utf8_string_atom)
  4791. utf8_string_atom = gdk_atom_intern("UTF8_STRING", false);
  4792. if (!clipboard_atom)
  4793. clipboard_atom = gdk_atom_intern("CLIPBOARD", false);
  4794. inst->area = gtk_drawing_area_new();
  4795. gtk_widget_set_name(GTK_WIDGET(inst->area), "drawing-area");
  4796. #if GTK_CHECK_VERSION(2,0,0) && !GTK_CHECK_VERSION(3,0,0)
  4797. /*
  4798. * PuTTY does its own double-buffering, so we don't really need
  4799. * GTK to do it as well. GTK documentation says this is probably
  4800. * a bad idea from GTK 3.10 onwards, and it's officially
  4801. * deprecated from 3.14. But it definitely helps in GTK 2.
  4802. */
  4803. gtk_widget_set_double_buffered(GTK_WIDGET(inst->area), false);
  4804. #endif
  4805. /*
  4806. * Try to create the fonts for use in the window. If this fails,
  4807. * we'll try again with some fallback settings, and only abort
  4808. * completely if we can't find any fonts at all.
  4809. */
  4810. {
  4811. char *errmsg_main = setup_fonts_ucs(inst, inst->conf);
  4812. if (!errmsg_main)
  4813. goto fonts_ok;
  4814. static const char *const fallbacks[] = {
  4815. DEFAULT_GTK_CLIENT_FONT,
  4816. DEFAULT_GTK_SERVER_FONT,
  4817. };
  4818. for (size_t i = 0; i < lenof(fallbacks); i++) {
  4819. Conf *fallback_conf = conf_new();
  4820. do_defaults(NULL, fallback_conf);
  4821. FontSpec *fs = fontspec_new(fallbacks[i]);
  4822. conf_set_fontspec(fallback_conf, CONF_font, fs);
  4823. fontspec_free(fs);
  4824. char *errmsg_fallback = setup_fonts_ucs(inst, fallback_conf);
  4825. conf_free(fallback_conf);
  4826. if (!errmsg_fallback) {
  4827. fprintf(stderr, "%s; falling back to default font '%s'\n",
  4828. errmsg_main, fallbacks[i]);
  4829. sfree(errmsg_main);
  4830. goto fonts_ok;
  4831. }
  4832. sfree(errmsg_fallback);
  4833. }
  4834. window_setup_error(errmsg_main);
  4835. sfree(errmsg_main);
  4836. gtk_widget_destroy(inst->area);
  4837. sfree(inst);
  4838. return;
  4839. fonts_ok:;
  4840. }
  4841. #if GTK_CHECK_VERSION(2,0,0)
  4842. inst->imc = gtk_im_multicontext_new();
  4843. #endif
  4844. inst->window = make_gtk_toplevel_window(inst);
  4845. gtk_widget_set_name(GTK_WIDGET(inst->window), "top-level");
  4846. {
  4847. const char *winclass = conf_get_str(inst->conf, CONF_winclass);
  4848. if (*winclass) {
  4849. #if GTK_CHECK_VERSION(3,22,0)
  4850. #ifndef NOT_X_WINDOWS
  4851. GdkWindow *gdkwin;
  4852. gtk_widget_realize(GTK_WIDGET(inst->window));
  4853. gdkwin = gtk_widget_get_window(GTK_WIDGET(inst->window));
  4854. if (inst->disp && gdk_window_ensure_native(gdkwin)) {
  4855. XClassHint *xch = XAllocClassHint();
  4856. xch->res_name = (char *)winclass;
  4857. xch->res_class = (char *)winclass;
  4858. XSetClassHint(inst->disp, GDK_WINDOW_XID(gdkwin), xch);
  4859. XFree(xch);
  4860. }
  4861. #endif
  4862. /*
  4863. * If we do have NOT_X_WINDOWS set, then we don't have any
  4864. * function in GTK 3.22 equivalent to the above. But then,
  4865. * surely in that situation the deprecated
  4866. * gtk_window_set_wmclass wouldn't have done anything
  4867. * meaningful in previous GTKs either.
  4868. */
  4869. #else
  4870. gtk_window_set_wmclass(GTK_WINDOW(inst->window),
  4871. winclass, winclass);
  4872. #endif
  4873. }
  4874. }
  4875. #if GTK_CHECK_VERSION(2,0,0)
  4876. {
  4877. const BackendVtable *vt = select_backend(inst->conf);
  4878. if (vt && vt->flags & BACKEND_RESIZE_FORBIDDEN)
  4879. gtk_window_set_resizable(GTK_WINDOW(inst->window), false);
  4880. }
  4881. #endif
  4882. inst->width = conf_get_int(inst->conf, CONF_width);
  4883. inst->height = conf_get_int(inst->conf, CONF_height);
  4884. cache_conf_values(inst);
  4885. init_clipboard(inst);
  4886. inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
  4887. inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
  4888. inst->hbox = GTK_BOX(gtk_hbox_new(false, 0));
  4889. /*
  4890. * We always create the scrollbar; it remains invisible if
  4891. * unwanted, so we can pop it up quickly if it suddenly becomes
  4892. * desirable.
  4893. */
  4894. if (conf_get_bool(inst->conf, CONF_scrollbar_on_left))
  4895. gtk_box_pack_start(inst->hbox, inst->sbar, false, false, 0);
  4896. gtk_box_pack_start(inst->hbox, inst->area, true, true, 0);
  4897. if (!conf_get_bool(inst->conf, CONF_scrollbar_on_left))
  4898. gtk_box_pack_start(inst->hbox, inst->sbar, false, false, 0);
  4899. gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
  4900. gtk_widget_show(inst->area);
  4901. show_scrollbar(inst, conf_get_bool(inst->conf, CONF_scrollbar));
  4902. gtk_widget_show(GTK_WIDGET(inst->hbox));
  4903. /*
  4904. * We must call gtk_widget_realize before setting up the geometry
  4905. * hints, so that GtkApplicationWindow will have actually created
  4906. * its menu bar (if it's going to) and hence compute_geom_hints
  4907. * can find it to take its size into account.
  4908. */
  4909. gtk_widget_realize(inst->window);
  4910. set_geom_hints(inst);
  4911. #if GTK_CHECK_VERSION(3,0,0)
  4912. {
  4913. int wp, hp;
  4914. compute_whole_window_size(inst, inst->width, inst->height, &wp, &hp);
  4915. gtk_window_set_default_size(GTK_WINDOW(inst->window), wp, hp);
  4916. }
  4917. #else
  4918. {
  4919. int w = inst->font_width * inst->width + 2*inst->window_border;
  4920. int h = inst->font_height * inst->height + 2*inst->window_border;
  4921. #if GTK_CHECK_VERSION(2,0,0)
  4922. gtk_widget_set_size_request(inst->area, w, h);
  4923. #else
  4924. gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), w, h);
  4925. #endif
  4926. }
  4927. #endif
  4928. #if GTK_CHECK_VERSION(2,0,0)
  4929. if (inst->gotpos) {
  4930. static const GdkGravity gravities[] = {
  4931. GDK_GRAVITY_NORTH_WEST,
  4932. GDK_GRAVITY_NORTH_EAST,
  4933. GDK_GRAVITY_SOUTH_WEST,
  4934. GDK_GRAVITY_SOUTH_EAST,
  4935. };
  4936. int x = inst->xpos, y = inst->ypos;
  4937. int wp, hp;
  4938. GdkRectangle monitor_geometry;
  4939. compute_whole_window_size(inst, inst->width, inst->height, &wp, &hp);
  4940. get_monitor_geometry(GTK_WIDGET(inst->window), &monitor_geometry);
  4941. if (inst->gravity & 1) x += (monitor_geometry.width - wp);
  4942. if (inst->gravity & 2) y += (monitor_geometry.height - hp);
  4943. gtk_window_set_gravity(GTK_WINDOW(inst->window),
  4944. gravities[inst->gravity & 3]);
  4945. gtk_window_move(GTK_WINDOW(inst->window), x, y);
  4946. }
  4947. #else
  4948. if (inst->gotpos) {
  4949. int x = inst->xpos, y = inst->ypos;
  4950. GtkRequisition req;
  4951. gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
  4952. if (inst->gravity & 1) x += gdk_screen_width() - req.width;
  4953. if (inst->gravity & 2) y += gdk_screen_height() - req.height;
  4954. gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
  4955. gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
  4956. }
  4957. #endif
  4958. g_signal_connect(G_OBJECT(inst->window), "destroy",
  4959. G_CALLBACK(destroy), inst);
  4960. g_signal_connect(G_OBJECT(inst->window), "delete_event",
  4961. G_CALLBACK(delete_window), inst);
  4962. g_signal_connect(G_OBJECT(inst->window), "key_press_event",
  4963. G_CALLBACK(key_event), inst);
  4964. g_signal_connect(G_OBJECT(inst->window), "key_release_event",
  4965. G_CALLBACK(key_event), inst);
  4966. g_signal_connect(G_OBJECT(inst->window), "focus_in_event",
  4967. G_CALLBACK(focus_event), inst);
  4968. g_signal_connect(G_OBJECT(inst->window), "focus_out_event",
  4969. G_CALLBACK(focus_event), inst);
  4970. g_signal_connect(G_OBJECT(inst->area), "realize",
  4971. G_CALLBACK(area_realised), inst);
  4972. g_signal_connect(G_OBJECT(inst->area), "size_allocate",
  4973. G_CALLBACK(area_size_allocate), inst);
  4974. g_signal_connect(G_OBJECT(inst->window), "configure_event",
  4975. G_CALLBACK(window_configured), inst);
  4976. #if GTK_CHECK_VERSION(3,10,0)
  4977. g_signal_connect(G_OBJECT(inst->area), "configure_event",
  4978. G_CALLBACK(area_configured), inst);
  4979. #endif
  4980. #if GTK_CHECK_VERSION(3,0,0)
  4981. g_signal_connect(G_OBJECT(inst->area), "draw",
  4982. G_CALLBACK(draw_area), inst);
  4983. #else
  4984. g_signal_connect(G_OBJECT(inst->area), "expose_event",
  4985. G_CALLBACK(expose_area), inst);
  4986. #endif
  4987. g_signal_connect(G_OBJECT(inst->area), "button_press_event",
  4988. G_CALLBACK(button_event), inst);
  4989. g_signal_connect(G_OBJECT(inst->area), "button_release_event",
  4990. G_CALLBACK(button_event), inst);
  4991. #if GTK_CHECK_VERSION(2,0,0)
  4992. g_signal_connect(G_OBJECT(inst->area), "scroll_event",
  4993. G_CALLBACK(scroll_event), inst);
  4994. #endif
  4995. g_signal_connect(G_OBJECT(inst->area), "motion_notify_event",
  4996. G_CALLBACK(motion_event), inst);
  4997. #if GTK_CHECK_VERSION(2,0,0)
  4998. g_signal_connect(G_OBJECT(inst->imc), "commit",
  4999. G_CALLBACK(input_method_commit_event), inst);
  5000. g_signal_connect(G_OBJECT(inst->imc), "preedit-start",
  5001. G_CALLBACK(input_method_preedit_start_event), inst);
  5002. g_signal_connect(G_OBJECT(inst->imc), "preedit-changed",
  5003. G_CALLBACK(input_method_preedit_changed_event), inst);
  5004. g_signal_connect(G_OBJECT(inst->imc), "preedit-end",
  5005. G_CALLBACK(input_method_preedit_end_event), inst);
  5006. #endif
  5007. if (conf_get_bool(inst->conf, CONF_scrollbar))
  5008. g_signal_connect(G_OBJECT(inst->sbar_adjust), "value_changed",
  5009. G_CALLBACK(scrollbar_moved), inst);
  5010. gtk_widget_add_events(GTK_WIDGET(inst->area),
  5011. GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
  5012. GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
  5013. GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK
  5014. #if GTK_CHECK_VERSION(3,4,0)
  5015. | GDK_SMOOTH_SCROLL_MASK
  5016. #endif
  5017. );
  5018. set_window_icon(inst->window, main_icon, n_main_icon);
  5019. gtk_widget_show(inst->window);
  5020. set_window_background(inst);
  5021. /*
  5022. * Set up the Ctrl+rightclick context menu.
  5023. */
  5024. {
  5025. GtkWidget *menuitem;
  5026. char *s;
  5027. inst->menu = gtk_menu_new();
  5028. #define MKMENUITEM(title, func) do \
  5029. { \
  5030. menuitem = gtk_menu_item_new_with_label(title); \
  5031. gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
  5032. gtk_widget_show(menuitem); \
  5033. g_signal_connect(G_OBJECT(menuitem), "activate", \
  5034. G_CALLBACK(func), inst); \
  5035. } while (0)
  5036. #define MKSUBMENU(title) do \
  5037. { \
  5038. menuitem = gtk_menu_item_new_with_label(title); \
  5039. gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
  5040. gtk_widget_show(menuitem); \
  5041. } while (0)
  5042. #define MKSEP() do \
  5043. { \
  5044. menuitem = gtk_menu_item_new(); \
  5045. gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
  5046. gtk_widget_show(menuitem); \
  5047. } while (0)
  5048. if (new_session)
  5049. MKMENUITEM("New Session...", new_session_menuitem);
  5050. MKMENUITEM("Restart Session", restart_session_menuitem);
  5051. inst->restartitem = menuitem;
  5052. gtk_widget_set_sensitive(inst->restartitem, false);
  5053. MKMENUITEM("Duplicate Session", dup_session_menuitem);
  5054. if (saved_sessions) {
  5055. inst->sessionsmenu = gtk_menu_new();
  5056. /* sessionsmenu will be updated when it's invoked */
  5057. /* XXX is this the right way to do dynamic menus in Gtk? */
  5058. MKMENUITEM("Saved Sessions", update_savedsess_menu);
  5059. gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
  5060. inst->sessionsmenu);
  5061. }
  5062. MKSEP();
  5063. MKMENUITEM("Change Settings...", change_settings_menuitem);
  5064. MKSEP();
  5065. if (use_event_log)
  5066. MKMENUITEM("Event Log", event_log_menuitem);
  5067. MKSUBMENU("Special Commands");
  5068. inst->specialsmenu = gtk_menu_new();
  5069. gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
  5070. inst->specialsitem1 = menuitem;
  5071. MKSEP();
  5072. inst->specialsitem2 = menuitem;
  5073. gtk_widget_hide(inst->specialsitem1);
  5074. gtk_widget_hide(inst->specialsitem2);
  5075. MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
  5076. MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
  5077. MKSEP();
  5078. MKMENUITEM("Copy to " CLIPNAME_EXPLICIT_OBJECT,
  5079. copy_clipboard_menuitem);
  5080. MKMENUITEM("Paste from " CLIPNAME_EXPLICIT_OBJECT,
  5081. paste_clipboard_menuitem);
  5082. MKMENUITEM("Copy All", copy_all_menuitem);
  5083. MKSEP();
  5084. s = dupcat("About ", appname);
  5085. MKMENUITEM(s, about_menuitem);
  5086. sfree(s);
  5087. #undef MKMENUITEM
  5088. #undef MKSUBMENU
  5089. #undef MKSEP
  5090. }
  5091. inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
  5092. inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
  5093. inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
  5094. inst->blankcursor = make_mouse_ptr(inst, -1);
  5095. inst->currcursor = inst->textcursor;
  5096. show_mouseptr(inst, true);
  5097. inst->eventlogstuff = eventlogstuff_new();
  5098. inst->term = term_init(inst->conf, &inst->ucsdata, &inst->termwin);
  5099. setup_clipboards(inst, inst->term, inst->conf);
  5100. inst->logctx = log_init(&inst->logpolicy, inst->conf);
  5101. term_provide_logctx(inst->term, inst->logctx);
  5102. term_size(inst->term, inst->height, inst->width,
  5103. conf_get_int(inst->conf, CONF_savelines));
  5104. #if GTK_CHECK_VERSION(2,0,0)
  5105. /* Delay this signal connection until after inst->term exists */
  5106. g_signal_connect(G_OBJECT(inst->window), "window_state_event",
  5107. G_CALLBACK(window_state_event), inst);
  5108. #endif
  5109. inst->exited = false;
  5110. start_backend(inst);
  5111. if (inst->ldisc) /* early backend failure might make this NULL already */
  5112. ldisc_echoedit_update(inst->ldisc); /* cause ldisc to notice changes */
  5113. }
  5114. static void gtk_seat_set_trust_status(Seat *seat, bool trusted)
  5115. {
  5116. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  5117. term_set_trust_status(inst->term, trusted);
  5118. }
  5119. static bool gtk_seat_can_set_trust_status(Seat *seat)
  5120. {
  5121. return true;
  5122. }
  5123. static bool gtk_seat_get_cursor_position(Seat *seat, int *x, int *y)
  5124. {
  5125. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  5126. if (inst->term) {
  5127. term_get_cursor_position(inst->term, x, y);
  5128. return true;
  5129. }
  5130. return false;
  5131. }