window.c 192 KB

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