window.c 191 KB

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