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