window.c 192 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713
  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. term_set_focus(inst->term, event->in);
  2288. term_update(inst->term);
  2289. show_mouseptr(inst, true);
  2290. return false;
  2291. }
  2292. static void gtk_seat_set_busy_status(Seat *seat, BusyStatus status)
  2293. {
  2294. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  2295. inst->busy_status = status;
  2296. update_mouseptr(inst);
  2297. }
  2298. static void gtkwin_set_raw_mouse_mode(TermWin *tw, bool activate)
  2299. {
  2300. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  2301. inst->send_raw_mouse = activate;
  2302. }
  2303. static void gtkwin_set_raw_mouse_mode_pointer(TermWin *tw, bool activate)
  2304. {
  2305. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  2306. inst->pointer_indicates_raw_mouse = activate;
  2307. update_mouseptr(inst);
  2308. }
  2309. #if GTK_CHECK_VERSION(2,0,0)
  2310. static void compute_whole_window_size(GtkFrontend *inst,
  2311. int wchars, int hchars,
  2312. int *wpix, int *hpix);
  2313. static void gtkwin_deny_term_resize(void *vctx)
  2314. {
  2315. GtkFrontend *inst = (GtkFrontend *)vctx;
  2316. drawing_area_setup_simple(inst);
  2317. }
  2318. #endif
  2319. static void gtkwin_timer(void *vctx, unsigned long now)
  2320. {
  2321. GtkFrontend *inst = (GtkFrontend *)vctx;
  2322. if (inst->win_resize_pending && now == inst->win_resize_timeout) {
  2323. if (inst->term_resize_notification_required)
  2324. term_resize_request_completed(inst->term);
  2325. inst->win_resize_pending = false;
  2326. }
  2327. }
  2328. static void request_resize_internal(GtkFrontend *inst, bool from_terminal,
  2329. int w, int h)
  2330. {
  2331. #if GTK_CHECK_VERSION(2,0,0)
  2332. /*
  2333. * Initial check: don't even try to resize a window if it's in one
  2334. * of the states that make that impossible. This includes being
  2335. * maximised; being full-screen (if we ever implement that); or
  2336. * being in various tiled states.
  2337. *
  2338. * On X11, the effect of trying to resize the window when it can't
  2339. * be resized should be that the window manager sends us a
  2340. * synthetic ConfigureNotify event restating our existing size
  2341. * (ICCCM section 4.1.5 "Configuring the Window"). That's awkward
  2342. * to deal with, but not impossible; so for X11 alone, we might
  2343. * not bother with this check.
  2344. *
  2345. * (In any case, X11 has other reasons why a window resize might
  2346. * be rejected, which this enumeration can't be aware of in any
  2347. * case. For example, if your window manager is a tiling one, then
  2348. * all your windows are _de facto_ tiled, but not _de jure_ in a
  2349. * way that GDK will know about. So we have to handle those
  2350. * synthetic ConfigureNotifies in any case.)
  2351. *
  2352. * On Wayland, as of GTK 3.24.20, the effects are much worse: it
  2353. * looks to me as if GTK stops ever sending us "draw" events, or
  2354. * even a size_allocate, so the display locks up completely until
  2355. * you toggle the maximised state of the window by some other
  2356. * means. So it's worth checking!
  2357. */
  2358. GdkWindow *gdkwin = gtk_widget_get_window(inst->window);
  2359. if (gdkwin) {
  2360. GdkWindowState state = gdk_window_get_state(gdkwin);
  2361. if (state & (GDK_WINDOW_STATE_MAXIMIZED |
  2362. GDK_WINDOW_STATE_FULLSCREEN |
  2363. #if GTK_CHECK_VERSION(3,10,0)
  2364. GDK_WINDOW_STATE_TILED |
  2365. #endif
  2366. #if GTK_CHECK_VERSION(3,22,23)
  2367. GDK_WINDOW_STATE_TOP_TILED |
  2368. GDK_WINDOW_STATE_RIGHT_TILED |
  2369. GDK_WINDOW_STATE_BOTTOM_TILED |
  2370. GDK_WINDOW_STATE_LEFT_TILED |
  2371. #endif
  2372. 0)) {
  2373. queue_toplevel_callback(gtkwin_deny_term_resize, inst);
  2374. if (from_terminal)
  2375. term_resize_request_completed(inst->term);
  2376. return;
  2377. }
  2378. }
  2379. #endif
  2380. #if !GTK_CHECK_VERSION(3,0,0)
  2381. int large_x, large_y;
  2382. int offset_x, offset_y;
  2383. int area_x, area_y;
  2384. GtkRequisition inner, outer;
  2385. /*
  2386. * This is a heinous hack dreamed up by the gnome-terminal
  2387. * people to get around a limitation in gtk. The problem is
  2388. * that in order to set the size correctly we really need to be
  2389. * calling gtk_window_resize - but that needs to know the size
  2390. * of the _whole window_, not the drawing area. So what we do
  2391. * is to set an artificially huge size request on the drawing
  2392. * area, recompute the resulting size request on the window,
  2393. * and look at the difference between the two. That gives us
  2394. * the x and y offsets we need to translate drawing area size
  2395. * into window size for real, and then we call
  2396. * gtk_window_resize.
  2397. */
  2398. /*
  2399. * We start by retrieving the current size of the whole window.
  2400. * Adding a bit to _that_ will give us a value we can use as a
  2401. * bogus size request which guarantees to be bigger than the
  2402. * current size of the drawing area.
  2403. */
  2404. get_window_pixel_size(inst, &large_x, &large_y);
  2405. large_x += 32;
  2406. large_y += 32;
  2407. gtk_widget_set_size_request(inst->area, large_x, large_y);
  2408. gtk_widget_size_request(inst->area, &inner);
  2409. gtk_widget_size_request(inst->window, &outer);
  2410. offset_x = outer.width - inner.width;
  2411. offset_y = outer.height - inner.height;
  2412. area_x = inst->font_width * w + 2*inst->window_border;
  2413. area_y = inst->font_height * h + 2*inst->window_border;
  2414. /*
  2415. * Now we must set the size request on the drawing area back to
  2416. * something sensible before we commit the real resize. Best
  2417. * way to do this, I think, is to set it to what the size is
  2418. * really going to end up being.
  2419. */
  2420. gtk_widget_set_size_request(inst->area, area_x, area_y);
  2421. #if GTK_CHECK_VERSION(2,0,0)
  2422. gtk_window_resize(GTK_WINDOW(inst->window),
  2423. area_x + offset_x, area_y + offset_y);
  2424. #else
  2425. gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
  2426. /*
  2427. * I can no longer remember what this call to
  2428. * gtk_container_dequeue_resize_handler is for. It was
  2429. * introduced in r3092 with no comment, and the commit log
  2430. * message was uninformative. I'm _guessing_ its purpose is to
  2431. * prevent gratuitous resize processing on the window given
  2432. * that we're about to resize it anyway, but I have no idea
  2433. * why that's so incredibly vital.
  2434. *
  2435. * I've tried removing the call, and nothing seems to go
  2436. * wrong. I've backtracked to r3092 and tried removing the
  2437. * call there, and still nothing goes wrong. So I'm going to
  2438. * adopt the working hypothesis that it's superfluous; I won't
  2439. * actually remove it from the GTK 1.2 code, but I won't
  2440. * attempt to replicate its functionality in the GTK 2 code
  2441. * above.
  2442. */
  2443. gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
  2444. gdk_window_resize(gtk_widget_get_window(inst->window),
  2445. area_x + offset_x, area_y + offset_y);
  2446. #endif
  2447. #else /* GTK_CHECK_VERSION(3,0,0) */
  2448. int wp, hp;
  2449. compute_whole_window_size(inst, w, h, &wp, &hp);
  2450. gtk_window_resize(GTK_WINDOW(inst->window), wp, hp);
  2451. #endif
  2452. inst->win_resize_pending = true;
  2453. inst->term_resize_notification_required = from_terminal;
  2454. inst->win_resize_timeout = schedule_timer(
  2455. WIN_RESIZE_TIMEOUT, gtkwin_timer, inst);
  2456. }
  2457. static void gtkwin_request_resize(TermWin *tw, int w, int h)
  2458. {
  2459. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  2460. request_resize_internal(inst, true, w, h);
  2461. }
  2462. #if GTK_CHECK_VERSION(3,0,0)
  2463. char *colour_to_css(const GdkColor *col)
  2464. {
  2465. GdkRGBA rgba;
  2466. rgba.red = col->red / 65535.0;
  2467. rgba.green = col->green / 65535.0;
  2468. rgba.blue = col->blue / 65535.0;
  2469. rgba.alpha = 1.0;
  2470. return gdk_rgba_to_string(&rgba);
  2471. }
  2472. #endif
  2473. void set_gtk_widget_background(GtkWidget *widget, const GdkColor *col)
  2474. {
  2475. #if GTK_CHECK_VERSION(3,0,0)
  2476. GtkCssProvider *provider = gtk_css_provider_new();
  2477. char *col_css = colour_to_css(col);
  2478. char *data = dupprintf(
  2479. "#drawing-area, #top-level { background-color: %s; }\n", col_css);
  2480. gtk_css_provider_load_from_data(provider, data, -1, NULL);
  2481. GtkStyleContext *context = gtk_widget_get_style_context(widget);
  2482. gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER(provider),
  2483. GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  2484. free(data);
  2485. free(col_css);
  2486. #else
  2487. if (gtk_widget_get_window(widget)) {
  2488. /* For GTK1, which doesn't have a 'const' on
  2489. * gdk_window_set_background's second parameter type. */
  2490. GdkColor col_mutable = *col;
  2491. gdk_window_set_background(gtk_widget_get_window(widget), &col_mutable);
  2492. }
  2493. #endif
  2494. }
  2495. void set_window_background(GtkFrontend *inst)
  2496. {
  2497. if (inst->area)
  2498. set_gtk_widget_background(GTK_WIDGET(inst->area), &inst->cols[258]);
  2499. if (inst->window)
  2500. set_gtk_widget_background(GTK_WIDGET(inst->window), &inst->cols[258]);
  2501. }
  2502. static void gtkwin_palette_set(TermWin *tw, unsigned start, unsigned ncolours,
  2503. const rgb *colours)
  2504. {
  2505. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  2506. assert(start <= OSC4_NCOLOURS);
  2507. assert(ncolours <= OSC4_NCOLOURS - start);
  2508. #if !GTK_CHECK_VERSION(3,0,0)
  2509. if (!inst->colmap) {
  2510. inst->colmap = gdk_colormap_get_system();
  2511. } else {
  2512. gdk_colormap_free_colors(inst->colmap, inst->cols, OSC4_NCOLOURS);
  2513. }
  2514. #endif
  2515. for (unsigned i = 0; i < ncolours; i++) {
  2516. const rgb *in = &colours[i];
  2517. GdkColor *out = &inst->cols[start + i];
  2518. out->red = in->r * 0x0101;
  2519. out->green = in->g * 0x0101;
  2520. out->blue = in->b * 0x0101;
  2521. }
  2522. #if !GTK_CHECK_VERSION(3,0,0)
  2523. {
  2524. gboolean success[OSC4_NCOLOURS];
  2525. gdk_colormap_alloc_colors(inst->colmap, inst->cols + start,
  2526. ncolours, false, true, success);
  2527. for (unsigned i = 0; i < ncolours; i++) {
  2528. if (!success[i])
  2529. g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
  2530. appname, start + i,
  2531. conf_get_int_int(inst->conf, CONF_colours, i*3+0),
  2532. conf_get_int_int(inst->conf, CONF_colours, i*3+1),
  2533. conf_get_int_int(inst->conf, CONF_colours, i*3+2));
  2534. }
  2535. }
  2536. #endif
  2537. if (start <= OSC4_COLOUR_bg && OSC4_COLOUR_bg < start + ncolours) {
  2538. /* Default Background has changed, so ensure that space between text
  2539. * area and window border is refreshed. */
  2540. set_window_background(inst);
  2541. if (inst->area && gtk_widget_get_window(inst->area)) {
  2542. draw_backing_rect(inst);
  2543. gtk_widget_queue_draw(inst->area);
  2544. }
  2545. }
  2546. }
  2547. static void gtkwin_palette_get_overrides(TermWin *tw, Terminal *term)
  2548. {
  2549. /* GTK has no analogue of Windows's 'standard system colours', so GTK PuTTY
  2550. * has no config option to override the normally configured colours from
  2551. * it */
  2552. }
  2553. static struct clipboard_state *clipboard_from_atom(
  2554. GtkFrontend *inst, GdkAtom atom)
  2555. {
  2556. int i;
  2557. for (i = 0; i < N_CLIPBOARDS; i++) {
  2558. struct clipboard_state *state = &inst->clipstates[i];
  2559. if (state->inst == inst && state->atom == atom)
  2560. return state;
  2561. }
  2562. return NULL;
  2563. }
  2564. #ifdef JUST_USE_GTK_CLIPBOARD_UTF8
  2565. /* ----------------------------------------------------------------------
  2566. * Clipboard handling, using the high-level GtkClipboard interface in
  2567. * as hands-off a way as possible. We write and read the clipboard as
  2568. * UTF-8 text, and let GTK deal with converting to any other text
  2569. * formats it feels like.
  2570. */
  2571. void set_clipboard_atom(GtkFrontend *inst, int clipboard, GdkAtom atom)
  2572. {
  2573. struct clipboard_state *state = &inst->clipstates[clipboard];
  2574. state->inst = inst;
  2575. state->clipboard = clipboard;
  2576. state->atom = atom;
  2577. if (state->atom != GDK_NONE) {
  2578. state->gtkclipboard = gtk_clipboard_get_for_display(
  2579. gdk_display_get_default(), state->atom);
  2580. g_object_set_data(G_OBJECT(state->gtkclipboard), "user-data", state);
  2581. } else {
  2582. state->gtkclipboard = NULL;
  2583. }
  2584. }
  2585. int init_clipboard(GtkFrontend *inst)
  2586. {
  2587. set_clipboard_atom(inst, CLIP_PRIMARY, GDK_SELECTION_PRIMARY);
  2588. set_clipboard_atom(inst, CLIP_CLIPBOARD, clipboard_atom);
  2589. return true;
  2590. }
  2591. static void clipboard_provide_data(GtkClipboard *clipboard,
  2592. GtkSelectionData *selection_data,
  2593. guint info, gpointer data)
  2594. {
  2595. struct clipboard_data_instance *cdi =
  2596. (struct clipboard_data_instance *)data;
  2597. if (cdi->state && cdi->state->current_cdi == cdi) {
  2598. gtk_selection_data_set_text(selection_data, cdi->pasteout_data_utf8,
  2599. cdi->pasteout_data_utf8_len);
  2600. }
  2601. }
  2602. static void clipboard_clear(GtkClipboard *clipboard, gpointer data)
  2603. {
  2604. struct clipboard_data_instance *cdi =
  2605. (struct clipboard_data_instance *)data;
  2606. if (cdi->state && cdi->state->current_cdi == cdi) {
  2607. if (cdi->state->inst && cdi->state->inst->term) {
  2608. term_lost_clipboard_ownership(cdi->state->inst->term,
  2609. cdi->state->clipboard);
  2610. }
  2611. cdi->state->current_cdi = NULL;
  2612. }
  2613. sfree(cdi->pasteout_data_utf8);
  2614. cdi->next->prev = cdi->prev;
  2615. cdi->prev->next = cdi->next;
  2616. sfree(cdi);
  2617. }
  2618. static void gtkwin_clip_write(
  2619. TermWin *tw, int clipboard, wchar_t *data, int *attr,
  2620. truecolour *truecolour, int len, bool must_deselect)
  2621. {
  2622. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  2623. struct clipboard_state *state = &inst->clipstates[clipboard];
  2624. struct clipboard_data_instance *cdi;
  2625. if (inst->direct_to_font) {
  2626. /* In this clipboard mode, we just can't paste if we're in
  2627. * direct-to-font mode. Fortunately, that shouldn't be
  2628. * important, because we'll only use this clipboard handling
  2629. * code on systems where that kind of font doesn't exist
  2630. * anyway. */
  2631. return;
  2632. }
  2633. if (!state->gtkclipboard)
  2634. return;
  2635. cdi = snew(struct clipboard_data_instance);
  2636. cdi->state = state;
  2637. state->current_cdi = cdi;
  2638. cdi->pasteout_data_utf8 = snewn(len*6, char);
  2639. cdi->prev = inst->cdi_headtail.prev;
  2640. cdi->next = &inst->cdi_headtail;
  2641. cdi->next->prev = cdi;
  2642. cdi->prev->next = cdi;
  2643. {
  2644. const wchar_t *tmp = data;
  2645. int tmplen = len;
  2646. cdi->pasteout_data_utf8_len =
  2647. charset_from_unicode(&tmp, &tmplen, cdi->pasteout_data_utf8,
  2648. len*6, CS_UTF8, NULL, NULL, 0);
  2649. }
  2650. /*
  2651. * It would be nice to just call gtk_clipboard_set_text() in place
  2652. * of all of the faffing below. Unfortunately, that won't give me
  2653. * access to the clipboard-clear event, which we use to visually
  2654. * deselect text in the terminal.
  2655. */
  2656. {
  2657. GtkTargetList *targetlist;
  2658. GtkTargetEntry *targettable;
  2659. gint n_targets;
  2660. targetlist = gtk_target_list_new(NULL, 0);
  2661. gtk_target_list_add_text_targets(targetlist, 0);
  2662. targettable = gtk_target_table_new_from_list(targetlist, &n_targets);
  2663. gtk_clipboard_set_with_data(state->gtkclipboard, targettable,
  2664. n_targets, clipboard_provide_data,
  2665. clipboard_clear, cdi);
  2666. gtk_target_table_free(targettable, n_targets);
  2667. gtk_target_list_unref(targetlist);
  2668. }
  2669. }
  2670. static void clipboard_text_received(GtkClipboard *clipboard,
  2671. const gchar *text, gpointer data)
  2672. {
  2673. GtkFrontend *inst = (GtkFrontend *)data;
  2674. wchar_t *paste;
  2675. size_t paste_len;
  2676. if (!text)
  2677. return;
  2678. paste = dup_mb_to_wc(CS_UTF8, text, length, &paste_len);
  2679. term_do_paste(inst->term, paste, paste_len);
  2680. sfree(paste);
  2681. }
  2682. static void gtkwin_clip_request_paste(TermWin *tw, int clipboard)
  2683. {
  2684. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  2685. struct clipboard_state *state = &inst->clipstates[clipboard];
  2686. if (!state->gtkclipboard)
  2687. return;
  2688. gtk_clipboard_request_text(state->gtkclipboard,
  2689. clipboard_text_received, inst);
  2690. }
  2691. #else /* JUST_USE_GTK_CLIPBOARD_UTF8 */
  2692. /* ----------------------------------------------------------------------
  2693. * Clipboard handling for X, using the low-level gtk_selection_*
  2694. * interface, handling conversions to fiddly things like compound text
  2695. * ourselves, and storing in X cut buffers too.
  2696. *
  2697. * This version of the clipboard code has to be kept around for GTK1,
  2698. * which doesn't have the higher-level GtkClipboard interface at all.
  2699. * And since it works on GTK2 and GTK3 too and has had a good few
  2700. * years of shakedown and bug fixing, we might as well keep using it
  2701. * where it's applicable.
  2702. *
  2703. * It's _possible_ that we might be able to replicate all the
  2704. * important wrinkles of this code in GtkClipboard. (In particular,
  2705. * cut buffers or local analogue look as if they might be accessible
  2706. * via gtk_clipboard_set_can_store(), and delivering text in
  2707. * non-Unicode formats only in the direct-to-font case ought to be
  2708. * possible if we can figure out the right set of things to put in the
  2709. * GtkTargetList.) But that work can wait until there's a need for it!
  2710. */
  2711. #ifndef NOT_X_WINDOWS
  2712. /* Store the data in a cut-buffer. */
  2713. static void store_cutbuffer(GtkFrontend *inst, char *ptr, int len)
  2714. {
  2715. if (inst->disp) {
  2716. /* ICCCM says we must rotate the buffers before storing to buffer 0. */
  2717. XRotateBuffers(inst->disp, 1);
  2718. XStoreBytes(inst->disp, ptr, len);
  2719. }
  2720. }
  2721. /* Retrieve data from a cut-buffer.
  2722. * Returned data needs to be freed with XFree().
  2723. */
  2724. static char *retrieve_cutbuffer(GtkFrontend *inst, int *nbytes)
  2725. {
  2726. char *ptr;
  2727. if (!inst->disp) {
  2728. *nbytes = 0;
  2729. return NULL;
  2730. }
  2731. ptr = XFetchBytes(inst->disp, nbytes);
  2732. if (*nbytes <= 0 && ptr != 0) {
  2733. XFree(ptr);
  2734. ptr = 0;
  2735. }
  2736. return ptr;
  2737. }
  2738. #endif /* NOT_X_WINDOWS */
  2739. static void gtkwin_clip_write(
  2740. TermWin *tw, int clipboard, wchar_t *data, int *attr,
  2741. truecolour *truecolour, int len, bool must_deselect)
  2742. {
  2743. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  2744. struct clipboard_state *state = &inst->clipstates[clipboard];
  2745. if (state->pasteout_data)
  2746. sfree(state->pasteout_data);
  2747. if (state->pasteout_data_ctext)
  2748. sfree(state->pasteout_data_ctext);
  2749. if (state->pasteout_data_utf8)
  2750. sfree(state->pasteout_data_utf8);
  2751. /*
  2752. * Set up UTF-8 and compound text paste data. This only happens
  2753. * if we aren't in direct-to-font mode using the D800 hack.
  2754. */
  2755. if (!inst->direct_to_font) {
  2756. const wchar_t *tmp = data;
  2757. int tmplen = len;
  2758. #ifndef NOT_X_WINDOWS
  2759. XTextProperty tp;
  2760. char *list[1];
  2761. #endif
  2762. state->pasteout_data_utf8 = snewn(len*6, char);
  2763. state->pasteout_data_utf8_len = len*6;
  2764. state->pasteout_data_utf8_len =
  2765. charset_from_unicode(&tmp, &tmplen, state->pasteout_data_utf8,
  2766. state->pasteout_data_utf8_len,
  2767. CS_UTF8, NULL, NULL, 0);
  2768. if (state->pasteout_data_utf8_len == 0) {
  2769. sfree(state->pasteout_data_utf8);
  2770. state->pasteout_data_utf8 = NULL;
  2771. } else {
  2772. state->pasteout_data_utf8 =
  2773. sresize(state->pasteout_data_utf8,
  2774. state->pasteout_data_utf8_len + 1, char);
  2775. state->pasteout_data_utf8[state->pasteout_data_utf8_len] = '\0';
  2776. }
  2777. /*
  2778. * Now let Xlib convert our UTF-8 data into compound text.
  2779. */
  2780. #ifndef NOT_X_WINDOWS
  2781. list[0] = state->pasteout_data_utf8;
  2782. if (inst->disp && Xutf8TextListToTextProperty(
  2783. inst->disp, list, 1, XCompoundTextStyle, &tp) == 0) {
  2784. state->pasteout_data_ctext = snewn(tp.nitems+1, char);
  2785. memcpy(state->pasteout_data_ctext, tp.value, tp.nitems);
  2786. state->pasteout_data_ctext_len = tp.nitems;
  2787. XFree(tp.value);
  2788. } else
  2789. #endif
  2790. {
  2791. state->pasteout_data_ctext = NULL;
  2792. state->pasteout_data_ctext_len = 0;
  2793. }
  2794. } else {
  2795. state->pasteout_data_utf8 = NULL;
  2796. state->pasteout_data_utf8_len = 0;
  2797. state->pasteout_data_ctext = NULL;
  2798. state->pasteout_data_ctext_len = 0;
  2799. }
  2800. {
  2801. size_t outlen;
  2802. state->pasteout_data = dup_wc_to_mb_c(
  2803. inst->ucsdata.line_codepage, data, len, "", &outlen);
  2804. /* We can't handle pastes larger than INT_MAX, because
  2805. * gtk_selection_data_set_text's length parameter is a gint */
  2806. if (outlen > INT_MAX)
  2807. outlen = INT_MAX;
  2808. state->pasteout_data_len = outlen;
  2809. }
  2810. #ifndef NOT_X_WINDOWS
  2811. /* The legacy X cut buffers go with PRIMARY, not any other clipboard */
  2812. if (state->atom == GDK_SELECTION_PRIMARY)
  2813. store_cutbuffer(inst, state->pasteout_data, state->pasteout_data_len);
  2814. #endif
  2815. if (gtk_selection_owner_set(inst->area, state->atom,
  2816. inst->input_event_time)) {
  2817. #if GTK_CHECK_VERSION(2,0,0)
  2818. gtk_selection_clear_targets(inst->area, state->atom);
  2819. #endif
  2820. gtk_selection_add_target(inst->area, state->atom,
  2821. GDK_SELECTION_TYPE_STRING, 1);
  2822. if (state->pasteout_data_ctext)
  2823. gtk_selection_add_target(inst->area, state->atom,
  2824. compound_text_atom, 1);
  2825. if (state->pasteout_data_utf8)
  2826. gtk_selection_add_target(inst->area, state->atom,
  2827. utf8_string_atom, 1);
  2828. }
  2829. if (must_deselect)
  2830. term_lost_clipboard_ownership(inst->term, clipboard);
  2831. }
  2832. static void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
  2833. guint info, guint time_stamp, gpointer data)
  2834. {
  2835. GtkFrontend *inst = (GtkFrontend *)data;
  2836. GdkAtom target = gtk_selection_data_get_target(seldata);
  2837. struct clipboard_state *state = clipboard_from_atom(
  2838. inst, gtk_selection_data_get_selection(seldata));
  2839. if (!state)
  2840. return;
  2841. if (target == utf8_string_atom)
  2842. gtk_selection_data_set(seldata, target, 8,
  2843. (unsigned char *)state->pasteout_data_utf8,
  2844. state->pasteout_data_utf8_len);
  2845. else if (target == compound_text_atom)
  2846. gtk_selection_data_set(seldata, target, 8,
  2847. (unsigned char *)state->pasteout_data_ctext,
  2848. state->pasteout_data_ctext_len);
  2849. else
  2850. gtk_selection_data_set(seldata, target, 8,
  2851. (unsigned char *)state->pasteout_data,
  2852. state->pasteout_data_len);
  2853. }
  2854. static gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
  2855. gpointer data)
  2856. {
  2857. GtkFrontend *inst = (GtkFrontend *)data;
  2858. struct clipboard_state *state = clipboard_from_atom(
  2859. inst, seldata->selection);
  2860. if (!state)
  2861. return true;
  2862. term_lost_clipboard_ownership(inst->term, state->clipboard);
  2863. if (state->pasteout_data)
  2864. sfree(state->pasteout_data);
  2865. if (state->pasteout_data_ctext)
  2866. sfree(state->pasteout_data_ctext);
  2867. if (state->pasteout_data_utf8)
  2868. sfree(state->pasteout_data_utf8);
  2869. state->pasteout_data = NULL;
  2870. state->pasteout_data_len = 0;
  2871. state->pasteout_data_ctext = NULL;
  2872. state->pasteout_data_ctext_len = 0;
  2873. state->pasteout_data_utf8 = NULL;
  2874. state->pasteout_data_utf8_len = 0;
  2875. return true;
  2876. }
  2877. static void gtkwin_clip_request_paste(TermWin *tw, int clipboard)
  2878. {
  2879. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  2880. struct clipboard_state *state = &inst->clipstates[clipboard];
  2881. /*
  2882. * In Unix, pasting is asynchronous: all we can do at the
  2883. * moment is to call gtk_selection_convert(), and when the data
  2884. * comes back _then_ we can call term_do_paste().
  2885. */
  2886. if (!inst->direct_to_font) {
  2887. /*
  2888. * First we attempt to retrieve the selection as a UTF-8
  2889. * string (which we will convert to the correct code page
  2890. * before sending to the session, of course). If that
  2891. * fails, selection_received() will be informed and will
  2892. * fall back to an ordinary string.
  2893. */
  2894. gtk_selection_convert(inst->area, state->atom, utf8_string_atom,
  2895. inst->input_event_time);
  2896. } else {
  2897. /*
  2898. * If we're in direct-to-font mode, we disable UTF-8
  2899. * pasting, and go straight to ordinary string data.
  2900. */
  2901. gtk_selection_convert(inst->area, state->atom,
  2902. GDK_SELECTION_TYPE_STRING,
  2903. inst->input_event_time);
  2904. }
  2905. }
  2906. static void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
  2907. guint time, gpointer data)
  2908. {
  2909. GtkFrontend *inst = (GtkFrontend *)data;
  2910. char *text;
  2911. int length;
  2912. #ifndef NOT_X_WINDOWS
  2913. char **list;
  2914. bool free_list_required = false;
  2915. bool free_required = false;
  2916. #endif
  2917. int charset;
  2918. GdkAtom seldata_target = gtk_selection_data_get_target(seldata);
  2919. GdkAtom seldata_type = gtk_selection_data_get_data_type(seldata);
  2920. const guchar *seldata_data = gtk_selection_data_get_data(seldata);
  2921. gint seldata_length = gtk_selection_data_get_length(seldata);
  2922. wchar_t *paste;
  2923. size_t paste_len;
  2924. struct clipboard_state *state = clipboard_from_atom(
  2925. inst, gtk_selection_data_get_selection(seldata));
  2926. if (!state)
  2927. return;
  2928. if (seldata_target == utf8_string_atom && seldata_length <= 0) {
  2929. /*
  2930. * Failed to get a UTF-8 selection string. Try compound
  2931. * text next.
  2932. */
  2933. gtk_selection_convert(inst->area, state->atom,
  2934. compound_text_atom,
  2935. inst->input_event_time);
  2936. return;
  2937. }
  2938. if (seldata_target == compound_text_atom && seldata_length <= 0) {
  2939. /*
  2940. * Failed to get UTF-8 or compound text. Try an ordinary
  2941. * string.
  2942. */
  2943. gtk_selection_convert(inst->area, state->atom,
  2944. GDK_SELECTION_TYPE_STRING,
  2945. inst->input_event_time);
  2946. return;
  2947. }
  2948. /*
  2949. * If we have data, but it's not of a type we can deal with,
  2950. * we have to ignore the data.
  2951. */
  2952. if (seldata_length > 0 &&
  2953. seldata_type != GDK_SELECTION_TYPE_STRING &&
  2954. seldata_type != compound_text_atom &&
  2955. seldata_type != utf8_string_atom)
  2956. return;
  2957. /*
  2958. * If we have no data, try looking in a cut buffer.
  2959. */
  2960. if (seldata_length <= 0) {
  2961. #ifndef NOT_X_WINDOWS
  2962. text = retrieve_cutbuffer(inst, &length);
  2963. if (length == 0)
  2964. return;
  2965. /* Xterm is rumoured to expect Latin-1, though I haven't checked the
  2966. * source, so use that as a de-facto standard. */
  2967. charset = CS_ISO8859_1;
  2968. free_required = true;
  2969. #else
  2970. return;
  2971. #endif
  2972. } else {
  2973. /*
  2974. * Convert COMPOUND_TEXT into UTF-8.
  2975. */
  2976. if (seldata_type == compound_text_atom) {
  2977. #ifndef NOT_X_WINDOWS
  2978. XTextProperty tp;
  2979. int ret, count;
  2980. tp.value = (unsigned char *)seldata_data;
  2981. tp.encoding = (Atom) seldata_type;
  2982. tp.format = gtk_selection_data_get_format(seldata);
  2983. tp.nitems = seldata_length;
  2984. ret = inst->disp == NULL ? -1 :
  2985. Xutf8TextPropertyToTextList(inst->disp, &tp, &list, &count);
  2986. if (ret == 0 && count == 1) {
  2987. text = list[0];
  2988. length = strlen(list[0]);
  2989. charset = CS_UTF8;
  2990. free_list_required = true;
  2991. } else
  2992. #endif
  2993. {
  2994. /*
  2995. * Compound text failed; fall back to STRING.
  2996. */
  2997. gtk_selection_convert(inst->area, state->atom,
  2998. GDK_SELECTION_TYPE_STRING,
  2999. inst->input_event_time);
  3000. return;
  3001. }
  3002. } else {
  3003. text = (char *)seldata_data;
  3004. length = seldata_length;
  3005. charset = (seldata_type == utf8_string_atom ?
  3006. CS_UTF8 : inst->ucsdata.line_codepage);
  3007. }
  3008. }
  3009. paste = dup_mb_to_wc_c(charset, text, length, &paste_len);
  3010. term_do_paste(inst->term, paste, paste_len);
  3011. sfree(paste);
  3012. #ifndef NOT_X_WINDOWS
  3013. if (free_list_required)
  3014. XFreeStringList(list);
  3015. if (free_required)
  3016. XFree(text);
  3017. #endif
  3018. }
  3019. static void init_one_clipboard(GtkFrontend *inst, int clipboard)
  3020. {
  3021. struct clipboard_state *state = &inst->clipstates[clipboard];
  3022. state->inst = inst;
  3023. state->clipboard = clipboard;
  3024. }
  3025. void set_clipboard_atom(GtkFrontend *inst, int clipboard, GdkAtom atom)
  3026. {
  3027. struct clipboard_state *state = &inst->clipstates[clipboard];
  3028. state->inst = inst;
  3029. state->clipboard = clipboard;
  3030. state->atom = atom;
  3031. }
  3032. void init_clipboard(GtkFrontend *inst)
  3033. {
  3034. #ifndef NOT_X_WINDOWS
  3035. /*
  3036. * Ensure that all the cut buffers exist - according to the ICCCM,
  3037. * we must do this before we start using cut buffers.
  3038. */
  3039. if (inst->disp) {
  3040. unsigned char empty[] = "";
  3041. x11_ignore_error(inst->disp, BadMatch);
  3042. XChangeProperty(inst->disp, GDK_ROOT_WINDOW(), XA_CUT_BUFFER0,
  3043. XA_STRING, 8, PropModeAppend, empty, 0);
  3044. x11_ignore_error(inst->disp, BadMatch);
  3045. XChangeProperty(inst->disp, GDK_ROOT_WINDOW(), XA_CUT_BUFFER1,
  3046. XA_STRING, 8, PropModeAppend, empty, 0);
  3047. x11_ignore_error(inst->disp, BadMatch);
  3048. XChangeProperty(inst->disp, GDK_ROOT_WINDOW(), XA_CUT_BUFFER2,
  3049. XA_STRING, 8, PropModeAppend, empty, 0);
  3050. x11_ignore_error(inst->disp, BadMatch);
  3051. XChangeProperty(inst->disp, GDK_ROOT_WINDOW(), XA_CUT_BUFFER3,
  3052. XA_STRING, 8, PropModeAppend, empty, 0);
  3053. x11_ignore_error(inst->disp, BadMatch);
  3054. XChangeProperty(inst->disp, GDK_ROOT_WINDOW(), XA_CUT_BUFFER4,
  3055. XA_STRING, 8, PropModeAppend, empty, 0);
  3056. x11_ignore_error(inst->disp, BadMatch);
  3057. XChangeProperty(inst->disp, GDK_ROOT_WINDOW(), XA_CUT_BUFFER5,
  3058. XA_STRING, 8, PropModeAppend, empty, 0);
  3059. x11_ignore_error(inst->disp, BadMatch);
  3060. XChangeProperty(inst->disp, GDK_ROOT_WINDOW(), XA_CUT_BUFFER6,
  3061. XA_STRING, 8, PropModeAppend, empty, 0);
  3062. x11_ignore_error(inst->disp, BadMatch);
  3063. XChangeProperty(inst->disp, GDK_ROOT_WINDOW(), XA_CUT_BUFFER7,
  3064. XA_STRING, 8, PropModeAppend, empty, 0);
  3065. }
  3066. #endif
  3067. inst->clipstates[CLIP_PRIMARY].atom = GDK_SELECTION_PRIMARY;
  3068. inst->clipstates[CLIP_CLIPBOARD].atom = clipboard_atom;
  3069. init_one_clipboard(inst, CLIP_PRIMARY);
  3070. init_one_clipboard(inst, CLIP_CLIPBOARD);
  3071. g_signal_connect(G_OBJECT(inst->area), "selection_received",
  3072. G_CALLBACK(selection_received), inst);
  3073. g_signal_connect(G_OBJECT(inst->area), "selection_get",
  3074. G_CALLBACK(selection_get), inst);
  3075. g_signal_connect(G_OBJECT(inst->area), "selection_clear_event",
  3076. G_CALLBACK(selection_clear), inst);
  3077. }
  3078. /*
  3079. * End of selection/clipboard handling.
  3080. * ----------------------------------------------------------------------
  3081. */
  3082. #endif /* JUST_USE_GTK_CLIPBOARD_UTF8 */
  3083. static void set_window_titles(GtkFrontend *inst)
  3084. {
  3085. /*
  3086. * We must always call set_icon_name after calling set_title,
  3087. * since set_title will write both names. Irritating, but such
  3088. * is life.
  3089. */
  3090. gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
  3091. if (!conf_get_bool(inst->conf, CONF_win_name_always))
  3092. gdk_window_set_icon_name(gtk_widget_get_window(inst->window),
  3093. inst->icontitle);
  3094. }
  3095. static void gtkwin_set_title(TermWin *tw, const char *title, int codepage)
  3096. {
  3097. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  3098. sfree(inst->wintitle);
  3099. if (codepage != CP_UTF8) {
  3100. wchar_t *title_w = dup_mb_to_wc(codepage, title);
  3101. inst->wintitle = encode_wide_string_as_utf8(title_w);
  3102. sfree(title_w);
  3103. } else {
  3104. inst->wintitle = dupstr(title);
  3105. }
  3106. set_window_titles(inst);
  3107. }
  3108. static void gtkwin_set_icon_title(TermWin *tw, const char *title, int codepage)
  3109. {
  3110. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  3111. sfree(inst->icontitle);
  3112. if (codepage != CP_UTF8) {
  3113. wchar_t *title_w = dup_mb_to_wc(codepage, title);
  3114. inst->icontitle = encode_wide_string_as_utf8(title_w);
  3115. sfree(title_w);
  3116. } else {
  3117. inst->icontitle = dupstr(title);
  3118. }
  3119. set_window_titles(inst);
  3120. }
  3121. static void gtkwin_set_scrollbar(TermWin *tw, int total, int start, int page)
  3122. {
  3123. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  3124. if (!conf_get_bool(inst->conf, CONF_scrollbar))
  3125. return;
  3126. inst->ignore_sbar = true;
  3127. gtk_adjustment_set_lower(inst->sbar_adjust, 0);
  3128. gtk_adjustment_set_upper(inst->sbar_adjust, total);
  3129. gtk_adjustment_set_value(inst->sbar_adjust, start);
  3130. gtk_adjustment_set_page_size(inst->sbar_adjust, page);
  3131. gtk_adjustment_set_step_increment(inst->sbar_adjust, 1);
  3132. gtk_adjustment_set_page_increment(inst->sbar_adjust, page/2);
  3133. #if !GTK_CHECK_VERSION(3,18,0)
  3134. gtk_adjustment_changed(inst->sbar_adjust);
  3135. #endif
  3136. inst->ignore_sbar = false;
  3137. }
  3138. void scrollbar_moved(GtkAdjustment *adj, GtkFrontend *inst)
  3139. {
  3140. if (!conf_get_bool(inst->conf, CONF_scrollbar))
  3141. return;
  3142. if (!inst->ignore_sbar)
  3143. term_scroll(inst->term, 1, (int)gtk_adjustment_get_value(adj));
  3144. }
  3145. static void show_scrollbar(GtkFrontend *inst, gboolean visible)
  3146. {
  3147. inst->sbar_visible = visible;
  3148. if (visible)
  3149. gtk_widget_show(inst->sbar);
  3150. else
  3151. gtk_widget_hide(inst->sbar);
  3152. }
  3153. static void gtkwin_set_cursor_pos(TermWin *tw, int x, int y)
  3154. {
  3155. /*
  3156. * This is meaningless under X.
  3157. */
  3158. }
  3159. /*
  3160. * This is still called when mode==BELL_VISUAL, even though the
  3161. * visual bell is handled entirely within terminal.c, because we
  3162. * may want to perform additional actions on any kind of bell (for
  3163. * example, taskbar flashing in Windows).
  3164. */
  3165. static void gtkwin_bell(TermWin *tw, int mode)
  3166. {
  3167. /* GtkFrontend *inst = container_of(tw, GtkFrontend, termwin); */
  3168. if (mode == BELL_DEFAULT)
  3169. gdk_display_beep(gdk_display_get_default());
  3170. }
  3171. static int gtkwin_char_width(TermWin *tw, int uc)
  3172. {
  3173. /*
  3174. * In this front end, double-width characters are handled using a
  3175. * separate font, so this can safely just return 1 always.
  3176. */
  3177. return 1;
  3178. }
  3179. static bool gtkwin_setup_draw_ctx(TermWin *tw)
  3180. {
  3181. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  3182. if (!gtk_widget_get_window(inst->area))
  3183. return false;
  3184. inst->uctx.type = inst->drawtype;
  3185. #ifdef DRAW_TEXT_GDK
  3186. if (inst->uctx.type == DRAWTYPE_GDK) {
  3187. /* If we're doing GDK-based drawing, then we also expect
  3188. * inst->pixmap to exist. */
  3189. inst->uctx.u.gdk.target = inst->pixmap;
  3190. inst->uctx.u.gdk.gc = gdk_gc_new(gtk_widget_get_window(inst->area));
  3191. }
  3192. #endif
  3193. #ifdef DRAW_TEXT_CAIRO
  3194. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3195. inst->uctx.u.cairo.widget = GTK_WIDGET(inst->area);
  3196. /* If we're doing Cairo drawing, we expect inst->surface to
  3197. * exist, and we draw to that first, regardless of whether we
  3198. * subsequently copy the results to inst->pixmap. */
  3199. inst->uctx.u.cairo.cr = cairo_create(inst->surface);
  3200. cairo_scale(inst->uctx.u.cairo.cr, inst->scale, inst->scale);
  3201. cairo_setup_draw_ctx(inst);
  3202. }
  3203. #endif
  3204. return true;
  3205. }
  3206. static void gtkwin_free_draw_ctx(TermWin *tw)
  3207. {
  3208. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  3209. #ifdef DRAW_TEXT_GDK
  3210. if (inst->uctx.type == DRAWTYPE_GDK) {
  3211. gdk_gc_unref(inst->uctx.u.gdk.gc);
  3212. }
  3213. #endif
  3214. #ifdef DRAW_TEXT_CAIRO
  3215. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3216. cairo_destroy(inst->uctx.u.cairo.cr);
  3217. }
  3218. #endif
  3219. }
  3220. static void draw_update(GtkFrontend *inst, int x, int y, int w, int h)
  3221. {
  3222. #if defined DRAW_TEXT_CAIRO && !defined NO_BACKING_PIXMAPS
  3223. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3224. /*
  3225. * If inst->surface and inst->pixmap both exist, then we've
  3226. * just drawn new content to the former which we must copy to
  3227. * the latter.
  3228. */
  3229. cairo_t *cr = gdk_cairo_create(inst->pixmap);
  3230. cairo_set_source_surface(cr, inst->surface, 0, 0);
  3231. cairo_rectangle(cr, x, y, w, h);
  3232. cairo_fill(cr);
  3233. cairo_destroy(cr);
  3234. }
  3235. #endif
  3236. /*
  3237. * Now we just queue a window redraw, which will cause
  3238. * inst->surface or inst->pixmap (whichever is appropriate for our
  3239. * compile mode) to be copied to the real window when we receive
  3240. * the resulting "expose" or "draw" event.
  3241. *
  3242. * Amazingly, this one API call is actually valid in all versions
  3243. * of GTK :-)
  3244. */
  3245. gtk_widget_queue_draw_area(inst->area, x, y, w, h);
  3246. }
  3247. #ifdef DRAW_TEXT_CAIRO
  3248. static void cairo_set_source_rgb_dim(cairo_t *cr, double r, double g, double b,
  3249. bool dim)
  3250. {
  3251. if (dim)
  3252. cairo_set_source_rgb(cr, r * 2 / 3, g * 2 / 3, b * 2 / 3);
  3253. else
  3254. cairo_set_source_rgb(cr, r, g, b);
  3255. }
  3256. #endif
  3257. static void draw_set_colour(GtkFrontend *inst, int col, bool dim)
  3258. {
  3259. #ifdef DRAW_TEXT_GDK
  3260. if (inst->uctx.type == DRAWTYPE_GDK) {
  3261. if (dim) {
  3262. #if GTK_CHECK_VERSION(2,0,0)
  3263. GdkColor color;
  3264. color.red = inst->cols[col].red * 2 / 3;
  3265. color.green = inst->cols[col].green * 2 / 3;
  3266. color.blue = inst->cols[col].blue * 2 / 3;
  3267. gdk_gc_set_rgb_fg_color(inst->uctx.u.gdk.gc, &color);
  3268. #else
  3269. /* Poor GTK1 fallback */
  3270. gdk_gc_set_foreground(inst->uctx.u.gdk.gc, &inst->cols[col]);
  3271. #endif
  3272. } else {
  3273. gdk_gc_set_foreground(inst->uctx.u.gdk.gc, &inst->cols[col]);
  3274. }
  3275. }
  3276. #endif
  3277. #ifdef DRAW_TEXT_CAIRO
  3278. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3279. cairo_set_source_rgb_dim(inst->uctx.u.cairo.cr,
  3280. inst->cols[col].red / 65535.0,
  3281. inst->cols[col].green / 65535.0,
  3282. inst->cols[col].blue / 65535.0, dim);
  3283. }
  3284. #endif
  3285. }
  3286. static void draw_set_colour_rgb(GtkFrontend *inst, optionalrgb orgb, bool dim)
  3287. {
  3288. #ifdef DRAW_TEXT_GDK
  3289. if (inst->uctx.type == DRAWTYPE_GDK) {
  3290. #if GTK_CHECK_VERSION(2,0,0)
  3291. GdkColor color;
  3292. color.red = orgb.r * 256;
  3293. color.green = orgb.g * 256;
  3294. color.blue = orgb.b * 256;
  3295. if (dim) {
  3296. color.red = color.red * 2 / 3;
  3297. color.green = color.green * 2 / 3;
  3298. color.blue = color.blue * 2 / 3;
  3299. }
  3300. gdk_gc_set_rgb_fg_color(inst->uctx.u.gdk.gc, &color);
  3301. #else
  3302. /* Poor GTK1 fallback */
  3303. gdk_gc_set_foreground(inst->uctx.u.gdk.gc, &inst->cols[256]);
  3304. #endif
  3305. }
  3306. #endif
  3307. #ifdef DRAW_TEXT_CAIRO
  3308. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3309. cairo_set_source_rgb_dim(inst->uctx.u.cairo.cr, orgb.r / 255.0,
  3310. orgb.g / 255.0, orgb.b / 255.0, dim);
  3311. }
  3312. #endif
  3313. }
  3314. static void draw_rectangle(GtkFrontend *inst, bool filled,
  3315. int x, int y, int w, int h)
  3316. {
  3317. #ifdef DRAW_TEXT_GDK
  3318. if (inst->uctx.type == DRAWTYPE_GDK) {
  3319. gdk_draw_rectangle(inst->uctx.u.gdk.target, inst->uctx.u.gdk.gc,
  3320. filled, x, y, w, h);
  3321. }
  3322. #endif
  3323. #ifdef DRAW_TEXT_CAIRO
  3324. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3325. cairo_new_path(inst->uctx.u.cairo.cr);
  3326. if (filled) {
  3327. cairo_rectangle(inst->uctx.u.cairo.cr, x, y, w, h);
  3328. cairo_fill(inst->uctx.u.cairo.cr);
  3329. } else {
  3330. cairo_rectangle(inst->uctx.u.cairo.cr,
  3331. x + 0.5, y + 0.5, w, h);
  3332. cairo_close_path(inst->uctx.u.cairo.cr);
  3333. cairo_stroke(inst->uctx.u.cairo.cr);
  3334. }
  3335. }
  3336. #endif
  3337. }
  3338. static void draw_clip(GtkFrontend *inst, int x, int y, int w, int h)
  3339. {
  3340. #ifdef DRAW_TEXT_GDK
  3341. if (inst->uctx.type == DRAWTYPE_GDK) {
  3342. GdkRectangle r;
  3343. r.x = x;
  3344. r.y = y;
  3345. r.width = w;
  3346. r.height = h;
  3347. gdk_gc_set_clip_rectangle(inst->uctx.u.gdk.gc, &r);
  3348. }
  3349. #endif
  3350. #ifdef DRAW_TEXT_CAIRO
  3351. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3352. cairo_reset_clip(inst->uctx.u.cairo.cr);
  3353. cairo_new_path(inst->uctx.u.cairo.cr);
  3354. cairo_rectangle(inst->uctx.u.cairo.cr, x, y, w, h);
  3355. cairo_clip(inst->uctx.u.cairo.cr);
  3356. }
  3357. #endif
  3358. }
  3359. static void draw_point(GtkFrontend *inst, int x, int y)
  3360. {
  3361. #ifdef DRAW_TEXT_GDK
  3362. if (inst->uctx.type == DRAWTYPE_GDK) {
  3363. gdk_draw_point(inst->uctx.u.gdk.target, inst->uctx.u.gdk.gc, x, y);
  3364. }
  3365. #endif
  3366. #ifdef DRAW_TEXT_CAIRO
  3367. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3368. cairo_new_path(inst->uctx.u.cairo.cr);
  3369. cairo_rectangle(inst->uctx.u.cairo.cr, x, y, 1, 1);
  3370. cairo_fill(inst->uctx.u.cairo.cr);
  3371. }
  3372. #endif
  3373. }
  3374. static void draw_line(GtkFrontend *inst, int x0, int y0, int x1, int y1)
  3375. {
  3376. #ifdef DRAW_TEXT_GDK
  3377. if (inst->uctx.type == DRAWTYPE_GDK) {
  3378. gdk_draw_line(inst->uctx.u.gdk.target, inst->uctx.u.gdk.gc,
  3379. x0, y0, x1, y1);
  3380. }
  3381. #endif
  3382. #ifdef DRAW_TEXT_CAIRO
  3383. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3384. cairo_new_path(inst->uctx.u.cairo.cr);
  3385. cairo_move_to(inst->uctx.u.cairo.cr, x0 + 0.5, y0 + 0.5);
  3386. cairo_line_to(inst->uctx.u.cairo.cr, x1 + 0.5, y1 + 0.5);
  3387. cairo_stroke(inst->uctx.u.cairo.cr);
  3388. }
  3389. #endif
  3390. }
  3391. static void draw_stretch_before(GtkFrontend *inst, int x, int y,
  3392. int w, bool wdouble,
  3393. int h, bool hdouble, bool hbothalf)
  3394. {
  3395. #ifdef DRAW_TEXT_CAIRO
  3396. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3397. cairo_matrix_t matrix;
  3398. matrix.xy = 0;
  3399. matrix.yx = 0;
  3400. if (wdouble) {
  3401. matrix.xx = 2;
  3402. matrix.x0 = -x;
  3403. } else {
  3404. matrix.xx = 1;
  3405. matrix.x0 = 0;
  3406. }
  3407. if (hdouble) {
  3408. matrix.yy = 2;
  3409. if (hbothalf) {
  3410. matrix.y0 = -(y+h);
  3411. } else {
  3412. matrix.y0 = -y;
  3413. }
  3414. } else {
  3415. matrix.yy = 1;
  3416. matrix.y0 = 0;
  3417. }
  3418. cairo_transform(inst->uctx.u.cairo.cr, &matrix);
  3419. }
  3420. #endif
  3421. }
  3422. static void draw_stretch_after(GtkFrontend *inst, int x, int y,
  3423. int w, bool wdouble,
  3424. int h, bool hdouble, bool hbothalf)
  3425. {
  3426. #ifdef DRAW_TEXT_GDK
  3427. #ifndef NO_BACKING_PIXMAPS
  3428. if (inst->uctx.type == DRAWTYPE_GDK) {
  3429. /*
  3430. * I can't find any plausible StretchBlt equivalent in the X
  3431. * server, so I'm going to do this the slow and painful way.
  3432. * This will involve repeated calls to gdk_draw_pixmap() to
  3433. * stretch the text horizontally. It's O(N^2) in time and O(N)
  3434. * in network bandwidth, but you try thinking of a better way.
  3435. * :-(
  3436. */
  3437. int i;
  3438. if (wdouble) {
  3439. for (i = 0; i < w; i++) {
  3440. gdk_draw_pixmap(inst->uctx.u.gdk.target,
  3441. inst->uctx.u.gdk.gc,
  3442. inst->uctx.u.gdk.target,
  3443. x + 2*i, y,
  3444. x + 2*i+1, y,
  3445. w - i, h);
  3446. }
  3447. w *= 2;
  3448. }
  3449. if (hdouble) {
  3450. int dt, db;
  3451. /* Now stretch vertically, in the same way. */
  3452. if (hbothalf)
  3453. dt = 0, db = 1;
  3454. else
  3455. dt = 1, db = 0;
  3456. for (i = 0; i < h; i += 2) {
  3457. gdk_draw_pixmap(inst->uctx.u.gdk.target,
  3458. inst->uctx.u.gdk.gc,
  3459. inst->uctx.u.gdk.target,
  3460. x, y + dt*i + db,
  3461. x, y + dt*(i+1),
  3462. w, h-i-1);
  3463. }
  3464. }
  3465. }
  3466. #else
  3467. #error No way to implement stretching in GDK without a reliable backing pixmap
  3468. #endif
  3469. #endif /* DRAW_TEXT_GDK */
  3470. #ifdef DRAW_TEXT_CAIRO
  3471. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3472. cairo_set_matrix(inst->uctx.u.cairo.cr,
  3473. &inst->uctx.u.cairo.origmatrix);
  3474. }
  3475. #endif
  3476. }
  3477. static void draw_backing_rect(GtkFrontend *inst)
  3478. {
  3479. if (!win_setup_draw_ctx(&inst->termwin))
  3480. return;
  3481. draw_set_colour(inst, 258, false);
  3482. draw_rectangle(inst, true, 0, 0, inst->backing_w, inst->backing_h);
  3483. draw_update(inst, 0, 0, inst->backing_w, inst->backing_h);
  3484. win_free_draw_ctx(&inst->termwin);
  3485. }
  3486. /*
  3487. * Draw a line of text in the window, at given character
  3488. * coordinates, in given attributes.
  3489. *
  3490. * We are allowed to fiddle with the contents of `text'.
  3491. */
  3492. static void do_text_internal(
  3493. GtkFrontend *inst, int x, int y, wchar_t *text, int len,
  3494. unsigned long attr, int lattr, truecolour truecolour)
  3495. {
  3496. int ncombining;
  3497. int nfg, nbg, t, fontid, rlen, widefactor;
  3498. bool bold;
  3499. bool monochrome =
  3500. gdk_visual_get_depth(gtk_widget_get_visual(inst->area)) == 1;
  3501. if (attr & TATTR_COMBINING) {
  3502. ncombining = len;
  3503. len = 1;
  3504. } else
  3505. ncombining = 1;
  3506. if (monochrome)
  3507. truecolour.fg = truecolour.bg = optionalrgb_none;
  3508. nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
  3509. nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
  3510. if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
  3511. struct optionalrgb trgb;
  3512. t = nfg;
  3513. nfg = nbg;
  3514. nbg = t;
  3515. trgb = truecolour.fg;
  3516. truecolour.fg = truecolour.bg;
  3517. truecolour.bg = trgb;
  3518. }
  3519. if ((inst->bold_style & BOLD_STYLE_COLOUR) && (attr & ATTR_BOLD)) {
  3520. if (nfg < 16) nfg |= 8;
  3521. else if (nfg >= 256) nfg |= 1;
  3522. }
  3523. if ((inst->bold_style & BOLD_STYLE_COLOUR) && (attr & ATTR_BLINK)) {
  3524. if (nbg < 16) nbg |= 8;
  3525. else if (nbg >= 256) nbg |= 1;
  3526. }
  3527. if ((attr & TATTR_ACTCURS) && !monochrome) {
  3528. truecolour.fg = truecolour.bg = optionalrgb_none;
  3529. nfg = 260;
  3530. nbg = 261;
  3531. attr &= ~ATTR_DIM; /* don't dim the cursor */
  3532. }
  3533. fontid = 0;
  3534. if (attr & ATTR_WIDE) {
  3535. widefactor = 2;
  3536. fontid |= 2;
  3537. } else {
  3538. widefactor = 1;
  3539. }
  3540. if ((attr & ATTR_BOLD) && (inst->bold_style & BOLD_STYLE_FONT)) {
  3541. bold = true;
  3542. fontid |= 1;
  3543. } else {
  3544. bold = false;
  3545. }
  3546. if (!inst->fonts[fontid]) {
  3547. int i;
  3548. /*
  3549. * Fall back through font ids with subsets of this one's
  3550. * set bits, in order.
  3551. */
  3552. for (i = fontid; i-- > 0 ;) {
  3553. if (i & ~fontid)
  3554. continue; /* some other bit is set */
  3555. if (inst->fonts[i]) {
  3556. fontid = i;
  3557. break;
  3558. }
  3559. }
  3560. assert(inst->fonts[fontid]); /* we should at least have hit zero */
  3561. }
  3562. if ((lattr & LATTR_MODE) != LATTR_NORM) {
  3563. x *= 2;
  3564. if (x >= inst->term->cols)
  3565. return;
  3566. if (x + len*2*widefactor > inst->term->cols) {
  3567. len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
  3568. if (len == 0)
  3569. return; /* rounded down half a double-width char to zero */
  3570. }
  3571. rlen = len * 2;
  3572. } else
  3573. rlen = len;
  3574. draw_clip(inst,
  3575. x*inst->font_width+inst->window_border,
  3576. y*inst->font_height+inst->window_border,
  3577. rlen*widefactor*inst->font_width,
  3578. inst->font_height);
  3579. if ((lattr & LATTR_MODE) != LATTR_NORM) {
  3580. draw_stretch_before(inst,
  3581. x*inst->font_width+inst->window_border,
  3582. y*inst->font_height+inst->window_border,
  3583. rlen*widefactor*inst->font_width, true,
  3584. inst->font_height,
  3585. ((lattr & LATTR_MODE) != LATTR_WIDE),
  3586. ((lattr & LATTR_MODE) == LATTR_BOT));
  3587. }
  3588. if (truecolour.bg.enabled)
  3589. draw_set_colour_rgb(inst, truecolour.bg, attr & ATTR_DIM);
  3590. else
  3591. draw_set_colour(inst, nbg, attr & ATTR_DIM);
  3592. draw_rectangle(inst, true,
  3593. x*inst->font_width+inst->window_border,
  3594. y*inst->font_height+inst->window_border,
  3595. rlen*widefactor*inst->font_width, inst->font_height);
  3596. if (truecolour.fg.enabled)
  3597. draw_set_colour_rgb(inst, truecolour.fg, attr & ATTR_DIM);
  3598. else
  3599. draw_set_colour(inst, nfg, attr & ATTR_DIM);
  3600. if (ncombining > 1) {
  3601. assert(len == 1);
  3602. unifont_draw_combining(&inst->uctx, inst->fonts[fontid],
  3603. x*inst->font_width+inst->window_border,
  3604. (y*inst->font_height+inst->window_border+
  3605. inst->fonts[0]->ascent),
  3606. text, ncombining, widefactor > 1,
  3607. bold, inst->font_width);
  3608. } else {
  3609. unifont_draw_text(&inst->uctx, inst->fonts[fontid],
  3610. x*inst->font_width+inst->window_border,
  3611. (y*inst->font_height+inst->window_border+
  3612. inst->fonts[0]->ascent),
  3613. text, len, widefactor > 1,
  3614. bold, inst->font_width);
  3615. }
  3616. if (attr & ATTR_UNDER) {
  3617. int uheight = inst->fonts[0]->ascent + 1;
  3618. if (uheight >= inst->font_height)
  3619. uheight = inst->font_height - 1;
  3620. draw_line(inst, x*inst->font_width+inst->window_border,
  3621. y*inst->font_height + uheight + inst->window_border,
  3622. (x+len)*widefactor*inst->font_width-1+inst->window_border,
  3623. y*inst->font_height + uheight + inst->window_border);
  3624. }
  3625. if (attr & ATTR_STRIKE) {
  3626. int sheight = inst->fonts[fontid]->strikethrough_y;
  3627. draw_line(inst, x*inst->font_width+inst->window_border,
  3628. y*inst->font_height + sheight + inst->window_border,
  3629. (x+len)*widefactor*inst->font_width-1+inst->window_border,
  3630. y*inst->font_height + sheight + inst->window_border);
  3631. }
  3632. if ((lattr & LATTR_MODE) != LATTR_NORM) {
  3633. draw_stretch_after(inst,
  3634. x*inst->font_width+inst->window_border,
  3635. y*inst->font_height+inst->window_border,
  3636. rlen*widefactor*inst->font_width, true,
  3637. inst->font_height,
  3638. ((lattr & LATTR_MODE) != LATTR_WIDE),
  3639. ((lattr & LATTR_MODE) == LATTR_BOT));
  3640. }
  3641. }
  3642. static void gtkwin_draw_text(
  3643. TermWin *tw, int x, int y, wchar_t *text, int len,
  3644. unsigned long attr, int lattr, truecolour truecolour)
  3645. {
  3646. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  3647. int widefactor;
  3648. do_text_internal(inst, x, y, text, len, attr, lattr, truecolour);
  3649. if (attr & ATTR_WIDE) {
  3650. widefactor = 2;
  3651. } else {
  3652. widefactor = 1;
  3653. }
  3654. if ((lattr & LATTR_MODE) != LATTR_NORM) {
  3655. x *= 2;
  3656. if (x >= inst->term->cols)
  3657. return;
  3658. if (x + len*2*widefactor > inst->term->cols)
  3659. len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
  3660. len *= 2;
  3661. }
  3662. draw_update(inst,
  3663. x*inst->font_width+inst->window_border,
  3664. y*inst->font_height+inst->window_border,
  3665. len*widefactor*inst->font_width, inst->font_height);
  3666. }
  3667. static void gtkwin_draw_cursor(
  3668. TermWin *tw, int x, int y, wchar_t *text, int len,
  3669. unsigned long attr, int lattr, truecolour truecolour)
  3670. {
  3671. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  3672. bool active, passive;
  3673. int widefactor;
  3674. if (attr & TATTR_PASCURS) {
  3675. attr &= ~TATTR_PASCURS;
  3676. passive = true;
  3677. } else
  3678. passive = false;
  3679. if ((attr & TATTR_ACTCURS) && inst->cursor_type != CURSOR_BLOCK) {
  3680. attr &= ~TATTR_ACTCURS;
  3681. active = true;
  3682. } else
  3683. active = false;
  3684. do_text_internal(inst, x, y, text, len, attr, lattr, truecolour);
  3685. if (attr & TATTR_COMBINING)
  3686. len = 1;
  3687. if (attr & ATTR_WIDE) {
  3688. widefactor = 2;
  3689. } else {
  3690. widefactor = 1;
  3691. }
  3692. if ((lattr & LATTR_MODE) != LATTR_NORM) {
  3693. x *= 2;
  3694. if (x >= inst->term->cols)
  3695. return;
  3696. if (x + len*2*widefactor > inst->term->cols)
  3697. len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
  3698. len *= 2;
  3699. }
  3700. if (inst->cursor_type == CURSOR_BLOCK) {
  3701. /*
  3702. * An active block cursor will already have been done by
  3703. * the above do_text call, so we only need to do anything
  3704. * if it's passive.
  3705. */
  3706. if (passive) {
  3707. draw_set_colour(inst, 261, false);
  3708. draw_rectangle(inst, false,
  3709. x*inst->font_width+inst->window_border,
  3710. y*inst->font_height+inst->window_border,
  3711. len*widefactor*inst->font_width-1,
  3712. inst->font_height-1);
  3713. }
  3714. } else {
  3715. int uheight;
  3716. int startx, starty, dx, dy, length, i;
  3717. int char_width;
  3718. if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
  3719. char_width = 2*inst->font_width;
  3720. else
  3721. char_width = inst->font_width;
  3722. if (inst->cursor_type == CURSOR_UNDERLINE) {
  3723. uheight = inst->fonts[0]->ascent + 1;
  3724. if (uheight >= inst->font_height)
  3725. uheight = inst->font_height - 1;
  3726. startx = x * inst->font_width + inst->window_border;
  3727. starty = y * inst->font_height + inst->window_border + uheight;
  3728. dx = 1;
  3729. dy = 0;
  3730. length = len * widefactor * char_width;
  3731. } else /* inst->cursor_type == CURSOR_VERTICAL_LINE */ {
  3732. int xadjust = 0;
  3733. if (attr & TATTR_RIGHTCURS)
  3734. xadjust = char_width - 1;
  3735. startx = x * inst->font_width + inst->window_border + xadjust;
  3736. starty = y * inst->font_height + inst->window_border;
  3737. dx = 0;
  3738. dy = 1;
  3739. length = inst->font_height;
  3740. }
  3741. draw_set_colour(inst, 261, false);
  3742. if (passive) {
  3743. for (i = 0; i < length; i++) {
  3744. if (i % 2 == 0) {
  3745. draw_point(inst, startx, starty);
  3746. }
  3747. startx += dx;
  3748. starty += dy;
  3749. }
  3750. } else if (active) {
  3751. draw_line(inst, startx, starty,
  3752. startx + (length-1) * dx, starty + (length-1) * dy);
  3753. } /* else no cursor (e.g., blinked off) */
  3754. }
  3755. draw_update(inst,
  3756. x*inst->font_width+inst->window_border,
  3757. y*inst->font_height+inst->window_border,
  3758. len*widefactor*inst->font_width, inst->font_height);
  3759. #if GTK_CHECK_VERSION(2,0,0)
  3760. {
  3761. GdkRectangle cursorrect;
  3762. cursorrect.x = x*inst->font_width+inst->window_border;
  3763. cursorrect.y = y*inst->font_height+inst->window_border;
  3764. cursorrect.width = len*widefactor*inst->font_width;
  3765. cursorrect.height = inst->font_height;
  3766. gtk_im_context_set_cursor_location(inst->imc, &cursorrect);
  3767. }
  3768. #endif
  3769. }
  3770. #if !GTK_CHECK_VERSION(2,0,0)
  3771. /*
  3772. * For GTK 1, manual code to scale an in-memory XPM, producing a new
  3773. * one as output. It will be ugly, but good enough to use as a trust
  3774. * sigil.
  3775. */
  3776. struct XpmHolder {
  3777. char **strings;
  3778. size_t nstrings;
  3779. };
  3780. static void xpmholder_free(XpmHolder *xh)
  3781. {
  3782. for (size_t i = 0; i < xh->nstrings; i++)
  3783. sfree(xh->strings[i]);
  3784. sfree(xh->strings);
  3785. sfree(xh);
  3786. }
  3787. static XpmHolder *xpm_scale(const char *const *xpm, int wo, int ho)
  3788. {
  3789. /* Get image dimensions, # colours, and chars-per-pixel */
  3790. int wi = 0, hi = 0, nc = 0, cpp = 0;
  3791. int retd = sscanf(xpm[0], "%d %d %d %d", &wi, &hi, &nc, &cpp);
  3792. assert(retd == 4);
  3793. /* Make output XpmHolder */
  3794. XpmHolder *xh = snew(XpmHolder);
  3795. xh->nstrings = 1 + nc + ho;
  3796. xh->strings = snewn(xh->nstrings, char *);
  3797. /* Set up header */
  3798. xh->strings[0] = dupprintf("%d %d %d %d", wo, ho, nc, cpp);
  3799. for (int i = 0; i < nc; i++)
  3800. xh->strings[1 + i] = dupstr(xpm[1 + i]);
  3801. /* Scale image */
  3802. for (int yo = 0; yo < ho; yo++) {
  3803. int yi = yo * hi / ho;
  3804. char *ro = snewn(cpp * wo + 1, char);
  3805. ro[cpp * wo] = '\0';
  3806. xh->strings[1 + nc + yo] = ro;
  3807. const char *ri = xpm[1 + nc + yi];
  3808. for (int xo = 0; xo < wo; xo++) {
  3809. int xi = xo * wi / wo;
  3810. memcpy(ro + cpp * xo, ri + cpp * xi, cpp);
  3811. }
  3812. }
  3813. return xh;
  3814. }
  3815. #endif /* !GTK_CHECK_VERSION(2,0,0) */
  3816. static void gtkwin_draw_trust_sigil(TermWin *tw, int cx, int cy)
  3817. {
  3818. GtkFrontend *inst = container_of(tw, GtkFrontend, termwin);
  3819. int x = cx * inst->font_width + inst->window_border;
  3820. int y = cy * inst->font_height + inst->window_border;
  3821. int w = 2*inst->font_width, h = inst->font_height;
  3822. if (inst->trust_sigil_w != w || inst->trust_sigil_h != h ||
  3823. #if GTK_CHECK_VERSION(2,0,0)
  3824. !inst->trust_sigil_pb
  3825. #else
  3826. !inst->trust_sigil_pm
  3827. #endif
  3828. ) {
  3829. #if GTK_CHECK_VERSION(2,0,0)
  3830. if (inst->trust_sigil_pb)
  3831. g_object_unref(G_OBJECT(inst->trust_sigil_pb));
  3832. #else
  3833. if (inst->trust_sigil_pm)
  3834. gdk_pixmap_unref(inst->trust_sigil_pm);
  3835. #endif
  3836. int best_icon_index = 0;
  3837. unsigned score = UINT_MAX;
  3838. for (int i = 0; i < n_main_icon; i++) {
  3839. int iw, ih;
  3840. if (sscanf(main_icon[i][0], "%d %d", &iw, &ih) == 2) {
  3841. int this_excess = (iw + ih) - (w + h);
  3842. unsigned this_score = (abs(this_excess) |
  3843. (this_excess > 0 ? 0 : 0x80000000U));
  3844. if (this_score < score) {
  3845. best_icon_index = i;
  3846. score = this_score;
  3847. }
  3848. }
  3849. }
  3850. #if GTK_CHECK_VERSION(2,0,0)
  3851. GdkPixbuf *icon_unscaled = gdk_pixbuf_new_from_xpm_data(
  3852. (const gchar **)main_icon[best_icon_index]);
  3853. inst->trust_sigil_pb = gdk_pixbuf_scale_simple(
  3854. icon_unscaled, w, h, GDK_INTERP_BILINEAR);
  3855. g_object_unref(G_OBJECT(icon_unscaled));
  3856. #else
  3857. XpmHolder *xh = xpm_scale(main_icon[best_icon_index], w, h);
  3858. inst->trust_sigil_pm = gdk_pixmap_create_from_xpm_d(
  3859. gtk_widget_get_window(inst->window), NULL,
  3860. &inst->cols[258], xh->strings);
  3861. xpmholder_free(xh);
  3862. #endif
  3863. inst->trust_sigil_w = w;
  3864. inst->trust_sigil_h = h;
  3865. }
  3866. #ifdef DRAW_TEXT_GDK
  3867. if (inst->uctx.type == DRAWTYPE_GDK) {
  3868. #if GTK_CHECK_VERSION(2,0,0)
  3869. gdk_draw_pixbuf(inst->uctx.u.gdk.target, inst->uctx.u.gdk.gc,
  3870. inst->trust_sigil_pb, 0, 0, x, y, w, h,
  3871. GDK_RGB_DITHER_NORMAL, 0, 0);
  3872. #else
  3873. gdk_draw_pixmap(inst->uctx.u.gdk.target, inst->uctx.u.gdk.gc,
  3874. inst->trust_sigil_pm, 0, 0, x, y, w, h);
  3875. #endif
  3876. }
  3877. #endif
  3878. #ifdef DRAW_TEXT_CAIRO
  3879. if (inst->uctx.type == DRAWTYPE_CAIRO) {
  3880. inst->uctx.u.cairo.widget = GTK_WIDGET(inst->area);
  3881. cairo_save(inst->uctx.u.cairo.cr);
  3882. cairo_translate(inst->uctx.u.cairo.cr, x, y);
  3883. gdk_cairo_set_source_pixbuf(inst->uctx.u.cairo.cr,
  3884. inst->trust_sigil_pb, 0, 0);
  3885. cairo_rectangle(inst->uctx.u.cairo.cr, 0, 0, w, h);
  3886. cairo_fill(inst->uctx.u.cairo.cr);
  3887. cairo_restore(inst->uctx.u.cairo.cr);
  3888. }
  3889. #endif
  3890. draw_update(inst, x, y, w, h);
  3891. }
  3892. GdkCursor *make_mouse_ptr(GtkFrontend *inst, int cursor_val)
  3893. {
  3894. if (cursor_val == -1) {
  3895. #if GTK_CHECK_VERSION(2,16,0)
  3896. cursor_val = GDK_BLANK_CURSOR;
  3897. #else
  3898. /*
  3899. * Work around absence of GDK_BLANK_CURSOR by inventing a
  3900. * blank pixmap.
  3901. */
  3902. GdkCursor *ret;
  3903. GdkColor bg = { 0, 0, 0, 0 };
  3904. GdkPixmap *pm = gdk_pixmap_new(NULL, 1, 1, 1);
  3905. GdkGC *gc = gdk_gc_new(pm);
  3906. gdk_gc_set_foreground(gc, &bg);
  3907. gdk_draw_rectangle(pm, gc, 1, 0, 0, 1, 1);
  3908. gdk_gc_unref(gc);
  3909. ret = gdk_cursor_new_from_pixmap(pm, pm, &bg, &bg, 1, 1);
  3910. gdk_pixmap_unref(pm);
  3911. return ret;
  3912. #endif
  3913. }
  3914. return gdk_cursor_new(cursor_val);
  3915. }
  3916. void modalfatalbox(const char *p, ...)
  3917. {
  3918. va_list ap;
  3919. fprintf(stderr, "FATAL ERROR: ");
  3920. va_start(ap, p);
  3921. vfprintf(stderr, p, ap);
  3922. va_end(ap);
  3923. fputc('\n', stderr);
  3924. exit(1);
  3925. }
  3926. #ifndef NOT_X_WINDOWS
  3927. static const char *gtk_seat_get_x_display(Seat *seat)
  3928. {
  3929. if (GDK_IS_X11_DISPLAY(gdk_display_get_default()))
  3930. return gdk_get_display();
  3931. return NULL;
  3932. }
  3933. static bool gtk_seat_get_windowid(Seat *seat, long *id)
  3934. {
  3935. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  3936. GdkWindow *window = gtk_widget_get_window(inst->area);
  3937. if (!GDK_IS_X11_WINDOW(window))
  3938. return false;
  3939. *id = GDK_WINDOW_XID(window);
  3940. return true;
  3941. }
  3942. #endif
  3943. char *setup_fonts_ucs(GtkFrontend *inst, Conf *conf)
  3944. {
  3945. bool shadowbold = conf_get_bool(conf, CONF_shadowbold);
  3946. int shadowboldoffset = conf_get_int(conf, CONF_shadowboldoffset);
  3947. FontSpec *fs;
  3948. unifont *fonts[4];
  3949. int i;
  3950. fs = conf_get_fontspec(conf, CONF_font);
  3951. fonts[0] = multifont_create(inst->area, fs->name, false, false,
  3952. shadowboldoffset, shadowbold);
  3953. if (!fonts[0]) {
  3954. return dupprintf("unable to load font \"%s\"", fs->name);
  3955. }
  3956. fs = conf_get_fontspec(conf, CONF_boldfont);
  3957. if (shadowbold || !fs->name[0]) {
  3958. fonts[1] = NULL;
  3959. } else {
  3960. fonts[1] = multifont_create(inst->area, fs->name, false, true,
  3961. shadowboldoffset, shadowbold);
  3962. if (!fonts[1]) {
  3963. if (fonts[0])
  3964. unifont_destroy(fonts[0]);
  3965. return dupprintf("unable to load bold font \"%s\"", fs->name);
  3966. }
  3967. }
  3968. fs = conf_get_fontspec(conf, CONF_widefont);
  3969. if (fs->name[0]) {
  3970. fonts[2] = multifont_create(inst->area, fs->name, true, false,
  3971. shadowboldoffset, shadowbold);
  3972. if (!fonts[2]) {
  3973. for (i = 0; i < 2; i++)
  3974. if (fonts[i])
  3975. unifont_destroy(fonts[i]);
  3976. return dupprintf("unable to load wide font \"%s\"", fs->name);
  3977. }
  3978. } else {
  3979. fonts[2] = NULL;
  3980. }
  3981. fs = conf_get_fontspec(conf, CONF_wideboldfont);
  3982. if (shadowbold || !fs->name[0]) {
  3983. fonts[3] = NULL;
  3984. } else {
  3985. fonts[3] = multifont_create(inst->area, fs->name, true, true,
  3986. shadowboldoffset, shadowbold);
  3987. if (!fonts[3]) {
  3988. for (i = 0; i < 3; i++)
  3989. if (fonts[i])
  3990. unifont_destroy(fonts[i]);
  3991. return dupprintf("unable to load wide bold font \"%s\"", fs->name);
  3992. }
  3993. }
  3994. /*
  3995. * Now we've got past all the possible error conditions, we can
  3996. * actually update our state.
  3997. */
  3998. for (i = 0; i < 4; i++) {
  3999. if (inst->fonts[i])
  4000. unifont_destroy(inst->fonts[i]);
  4001. inst->fonts[i] = fonts[i];
  4002. }
  4003. if (inst->font_width != inst->fonts[0]->width ||
  4004. inst->font_height != inst->fonts[0]->height) {
  4005. inst->font_width = inst->fonts[0]->width;
  4006. inst->font_height = inst->fonts[0]->height;
  4007. /*
  4008. * The font size has changed, so force the next call to
  4009. * drawing_area_setup to regenerate the backing surface.
  4010. */
  4011. inst->drawing_area_setup_needed = true;
  4012. }
  4013. inst->direct_to_font = init_ucs(&inst->ucsdata,
  4014. conf_get_str(inst->conf, CONF_line_codepage),
  4015. conf_get_bool(inst->conf, CONF_utf8_override),
  4016. inst->fonts[0]->public_charset,
  4017. conf_get_int(inst->conf, CONF_vtmode));
  4018. inst->drawtype = inst->fonts[0]->preferred_drawtype;
  4019. return NULL;
  4020. }
  4021. #if GTK_CHECK_VERSION(3,0,0)
  4022. struct find_app_menu_bar_ctx {
  4023. GtkWidget *area, *menubar;
  4024. };
  4025. static void find_app_menu_bar(GtkWidget *widget, gpointer data)
  4026. {
  4027. struct find_app_menu_bar_ctx *ctx = (struct find_app_menu_bar_ctx *)data;
  4028. if (widget != ctx->area && GTK_IS_MENU_BAR(widget))
  4029. ctx->menubar = widget;
  4030. }
  4031. #endif
  4032. static void compute_geom_hints(GtkFrontend *inst, GdkGeometry *geom)
  4033. {
  4034. /*
  4035. * Unused fields in geom.
  4036. */
  4037. geom->max_width = geom->max_height = -1;
  4038. geom->min_aspect = geom->max_aspect = 0;
  4039. /*
  4040. * Set up the geometry fields we care about, with reference to
  4041. * just the drawing area. We'll correct for other widgets in a
  4042. * moment.
  4043. */
  4044. geom->min_width = inst->font_width + 2*inst->window_border;
  4045. geom->min_height = inst->font_height + 2*inst->window_border;
  4046. geom->base_width = 2*inst->window_border;
  4047. geom->base_height = 2*inst->window_border;
  4048. geom->width_inc = inst->font_width;
  4049. geom->height_inc = inst->font_height;
  4050. /*
  4051. * If we've got a scrollbar visible, then we must include its
  4052. * width as part of the base and min width, and also ensure that
  4053. * our window's minimum height is at least the height required by
  4054. * the scrollbar.
  4055. *
  4056. * In the latter case, we must also take care to arrange that
  4057. * (geom->min_height - geom->base_height) is an integer multiple of
  4058. * geom->height_inc, because if it's not, then some window managers
  4059. * (we know of xfwm4) get confused, with the effect that they
  4060. * resize our window to a height based on min_height instead of
  4061. * base_height, which we then round down and the window ends up
  4062. * too short.
  4063. */
  4064. if (inst->sbar_visible) {
  4065. GtkRequisition req;
  4066. int min_sb_height;
  4067. #if GTK_CHECK_VERSION(3,0,0)
  4068. gtk_widget_get_preferred_size(inst->sbar, &req, NULL);
  4069. #else
  4070. gtk_widget_size_request(inst->sbar, &req);
  4071. #endif
  4072. /* Compute rounded-up scrollbar height. */
  4073. min_sb_height = req.height;
  4074. min_sb_height += geom->height_inc - 1;
  4075. min_sb_height -= ((min_sb_height - geom->base_height%geom->height_inc)
  4076. % geom->height_inc);
  4077. geom->min_width += req.width;
  4078. geom->base_width += req.width;
  4079. if (geom->min_height < min_sb_height)
  4080. geom->min_height = min_sb_height;
  4081. }
  4082. #if GTK_CHECK_VERSION(3,0,0)
  4083. /*
  4084. * And if we're running a main-gtk-application.c based program and
  4085. * GtkApplicationWindow has given us a menu bar inside the window,
  4086. * then we must take that into account as well.
  4087. *
  4088. * In its unbounded wisdom, GtkApplicationWindow doesn't actually
  4089. * give us a direct function call to _find_ the menu bar widget.
  4090. * Fortunately, we can find it by enumerating the children of the
  4091. * top-level window and looking for one we didn't put there
  4092. * ourselves.
  4093. */
  4094. {
  4095. struct find_app_menu_bar_ctx ctx[1];
  4096. ctx->area = inst->area;
  4097. ctx->menubar = NULL;
  4098. gtk_container_foreach(GTK_CONTAINER(inst->window),
  4099. find_app_menu_bar, ctx);
  4100. if (ctx->menubar) {
  4101. GtkRequisition req;
  4102. int min_menu_width;
  4103. gtk_widget_get_preferred_size(ctx->menubar, NULL, &req);
  4104. /*
  4105. * This time, the height adjustment is easy (the menu bar
  4106. * sits above everything), but we have to take care with
  4107. * the _width_ to ensure we keep min_width and base_width
  4108. * congruent modulo width_inc.
  4109. */
  4110. geom->min_height += req.height;
  4111. geom->base_height += req.height;
  4112. min_menu_width = req.width;
  4113. min_menu_width += geom->width_inc - 1;
  4114. min_menu_width -=
  4115. ((min_menu_width - geom->base_width%geom->width_inc)
  4116. % geom->width_inc);
  4117. if (geom->min_width < min_menu_width)
  4118. geom->min_width = min_menu_width;
  4119. }
  4120. }
  4121. #endif
  4122. }
  4123. void set_geom_hints(GtkFrontend *inst)
  4124. {
  4125. /*
  4126. * 2021-12-20: I've found that on Ubuntu 20.04 Wayland (using GTK
  4127. * 3.24.20), setting geometry hints causes the window size to come
  4128. * out wrong. As far as I can tell, that's because the GDK Wayland
  4129. * backend internally considers windows to be a lot larger than
  4130. * their obvious display size (*even* considering visible window
  4131. * furniture like title bars), with an extra margin on every side
  4132. * to account for surrounding effects like shadows. And the
  4133. * geometry hints like base size and resize increment are applied
  4134. * to that larger size rather than the more obvious 'client area'
  4135. * size. So when we ask for a window of exactly the size we want,
  4136. * it gets modified by GDK based on the geometry hints, but
  4137. * applying this extra margin, which causes the size to be a
  4138. * little bit too small.
  4139. *
  4140. * I don't know how you can sensibly find out the size of that
  4141. * margin. If I did, I could account for it in the geometry hints.
  4142. * But I also see that gtk_window_set_geometry_hints is removed in
  4143. * GTK 4, which suggests that probably doing a lot of hard work to
  4144. * fix this is not the way forward.
  4145. *
  4146. * So instead, I simply avoid setting geometry hints at all on any
  4147. * GDK backend other than X11, and hopefully that's a workaround.
  4148. */
  4149. #if GTK_CHECK_VERSION(3,0,0) && !defined NOT_X_WINDOWS
  4150. if (!GDK_IS_X11_DISPLAY(gdk_display_get_default()))
  4151. return;
  4152. #endif
  4153. const struct BackendVtable *vt;
  4154. GdkGeometry geom;
  4155. gint flags = GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE | GDK_HINT_RESIZE_INC;
  4156. compute_geom_hints(inst, &geom);
  4157. #if GTK_CHECK_VERSION(2,0,0)
  4158. if (inst->gotpos)
  4159. flags |= GDK_HINT_USER_POS;
  4160. #endif
  4161. vt = backend_vt_from_proto(conf_get_int(inst->conf, CONF_protocol));
  4162. if (vt && vt->flags & BACKEND_RESIZE_FORBIDDEN) {
  4163. /* Window resizing forbidden. Set both minimum and maximum
  4164. * dimensions to be the initial size. */
  4165. geom.min_width = geom.base_width + geom.width_inc * inst->width;
  4166. geom.min_height = geom.base_height + geom.height_inc * inst->height;
  4167. geom.max_width = geom.min_width;
  4168. geom.max_height = geom.min_height;
  4169. flags |= GDK_HINT_MAX_SIZE;
  4170. }
  4171. gtk_window_set_geometry_hints(GTK_WINDOW(inst->window),
  4172. NULL, &geom, flags);
  4173. }
  4174. #if GTK_CHECK_VERSION(2,0,0)
  4175. static void compute_whole_window_size(GtkFrontend *inst,
  4176. int wchars, int hchars,
  4177. int *wpix, int *hpix)
  4178. {
  4179. GdkGeometry geom;
  4180. compute_geom_hints(inst, &geom);
  4181. if (wpix) *wpix = geom.base_width + wchars * geom.width_inc;
  4182. if (hpix) *hpix = geom.base_height + hchars * geom.height_inc;
  4183. }
  4184. #endif
  4185. void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
  4186. {
  4187. GtkFrontend *inst = (GtkFrontend *)data;
  4188. term_clrsb(inst->term);
  4189. }
  4190. void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
  4191. {
  4192. GtkFrontend *inst = (GtkFrontend *)data;
  4193. term_pwron(inst->term, true);
  4194. if (inst->ldisc)
  4195. ldisc_echoedit_update(inst->ldisc);
  4196. }
  4197. void copy_clipboard_menuitem(GtkMenuItem *item, gpointer data)
  4198. {
  4199. GtkFrontend *inst = (GtkFrontend *)data;
  4200. static const int clips[] = { MENU_CLIPBOARD };
  4201. term_request_copy(inst->term, clips, lenof(clips));
  4202. }
  4203. void paste_clipboard_menuitem(GtkMenuItem *item, gpointer data)
  4204. {
  4205. GtkFrontend *inst = (GtkFrontend *)data;
  4206. term_request_paste(inst->term, MENU_CLIPBOARD);
  4207. }
  4208. void copy_all_menuitem(GtkMenuItem *item, gpointer data)
  4209. {
  4210. GtkFrontend *inst = (GtkFrontend *)data;
  4211. static const int clips[] = { COPYALL_CLIPBOARDS };
  4212. term_copyall(inst->term, clips, lenof(clips));
  4213. }
  4214. void special_menuitem(GtkMenuItem *item, gpointer data)
  4215. {
  4216. GtkFrontend *inst = (GtkFrontend *)data;
  4217. SessionSpecial *sc = g_object_get_data(G_OBJECT(item), "user-data");
  4218. if (inst->backend)
  4219. backend_special(inst->backend, sc->code, sc->arg);
  4220. }
  4221. void about_menuitem(GtkMenuItem *item, gpointer data)
  4222. {
  4223. GtkFrontend *inst = (GtkFrontend *)data;
  4224. about_box(inst->window);
  4225. }
  4226. void event_log_menuitem(GtkMenuItem *item, gpointer data)
  4227. {
  4228. GtkFrontend *inst = (GtkFrontend *)data;
  4229. showeventlog(inst->eventlogstuff, inst->window);
  4230. }
  4231. void setup_clipboards(GtkFrontend *inst, Terminal *term, Conf *conf)
  4232. {
  4233. assert(term->mouse_select_clipboards[0] == CLIP_LOCAL);
  4234. term->n_mouse_select_clipboards = 1;
  4235. term->mouse_select_clipboards[
  4236. term->n_mouse_select_clipboards++] = MOUSE_SELECT_CLIPBOARD;
  4237. if (conf_get_bool(conf, CONF_mouseautocopy)) {
  4238. term->mouse_select_clipboards[
  4239. term->n_mouse_select_clipboards++] = CLIP_CLIPBOARD;
  4240. }
  4241. set_clipboard_atom(inst, CLIP_CUSTOM_1, GDK_NONE);
  4242. set_clipboard_atom(inst, CLIP_CUSTOM_2, GDK_NONE);
  4243. set_clipboard_atom(inst, CLIP_CUSTOM_3, GDK_NONE);
  4244. switch (conf_get_int(conf, CONF_mousepaste)) {
  4245. case CLIPUI_IMPLICIT:
  4246. term->mouse_paste_clipboard = MOUSE_PASTE_CLIPBOARD;
  4247. break;
  4248. case CLIPUI_EXPLICIT:
  4249. term->mouse_paste_clipboard = CLIP_CLIPBOARD;
  4250. break;
  4251. case CLIPUI_CUSTOM:
  4252. term->mouse_paste_clipboard = CLIP_CUSTOM_1;
  4253. set_clipboard_atom(inst, CLIP_CUSTOM_1,
  4254. gdk_atom_intern(
  4255. conf_get_str(conf, CONF_mousepaste_custom),
  4256. false));
  4257. break;
  4258. default:
  4259. term->mouse_paste_clipboard = CLIP_NULL;
  4260. break;
  4261. }
  4262. if (conf_get_int(conf, CONF_ctrlshiftins) == CLIPUI_CUSTOM) {
  4263. GdkAtom atom = gdk_atom_intern(
  4264. conf_get_str(conf, CONF_ctrlshiftins_custom), false);
  4265. struct clipboard_state *state = clipboard_from_atom(inst, atom);
  4266. if (state) {
  4267. inst->clipboard_ctrlshiftins = state->clipboard;
  4268. } else {
  4269. inst->clipboard_ctrlshiftins = CLIP_CUSTOM_2;
  4270. set_clipboard_atom(inst, CLIP_CUSTOM_2, atom);
  4271. }
  4272. }
  4273. if (conf_get_int(conf, CONF_ctrlshiftcv) == CLIPUI_CUSTOM) {
  4274. GdkAtom atom = gdk_atom_intern(
  4275. conf_get_str(conf, CONF_ctrlshiftcv_custom), false);
  4276. struct clipboard_state *state = clipboard_from_atom(inst, atom);
  4277. if (state) {
  4278. inst->clipboard_ctrlshiftins = state->clipboard;
  4279. } else {
  4280. inst->clipboard_ctrlshiftcv = CLIP_CUSTOM_3;
  4281. set_clipboard_atom(inst, CLIP_CUSTOM_3, atom);
  4282. }
  4283. }
  4284. }
  4285. struct after_change_settings_dialog_ctx {
  4286. GtkFrontend *inst;
  4287. Conf *newconf;
  4288. };
  4289. static void after_change_settings_dialog(void *vctx, int retval);
  4290. void change_settings_menuitem(GtkMenuItem *item, gpointer data)
  4291. {
  4292. GtkFrontend *inst = (GtkFrontend *)data;
  4293. struct after_change_settings_dialog_ctx *ctx;
  4294. GtkWidget *dialog;
  4295. char *title;
  4296. if (find_and_raise_dialog(inst, DIALOG_SLOT_RECONFIGURE))
  4297. return;
  4298. title = dupcat(appname, " Reconfiguration");
  4299. ctx = snew(struct after_change_settings_dialog_ctx);
  4300. ctx->inst = inst;
  4301. ctx->newconf = conf_copy(inst->conf);
  4302. term_pre_reconfig(inst->term, ctx->newconf);
  4303. dialog = create_config_box(
  4304. title, ctx->newconf, true,
  4305. inst->backend ? backend_cfg_info(inst->backend) : 0,
  4306. after_change_settings_dialog, ctx);
  4307. register_dialog(&inst->seat, DIALOG_SLOT_RECONFIGURE, dialog);
  4308. sfree(title);
  4309. }
  4310. static void after_change_settings_dialog(void *vctx, int retval)
  4311. {
  4312. struct after_change_settings_dialog_ctx ctx =
  4313. *(struct after_change_settings_dialog_ctx *)vctx;
  4314. GtkFrontend *inst = ctx.inst;
  4315. Conf *oldconf = inst->conf, *newconf = ctx.newconf;
  4316. bool need_size;
  4317. sfree(vctx); /* we've copied this already */
  4318. unregister_dialog(&inst->seat, DIALOG_SLOT_RECONFIGURE);
  4319. if (retval > 0) {
  4320. inst->conf = newconf;
  4321. /* Pass new config data to the logging module */
  4322. log_reconfig(inst->logctx, inst->conf);
  4323. /*
  4324. * Flush the line discipline's edit buffer in the case
  4325. * where local editing has just been disabled.
  4326. */
  4327. if (inst->ldisc) {
  4328. ldisc_configure(inst->ldisc, inst->conf);
  4329. ldisc_echoedit_update(inst->ldisc);
  4330. }
  4331. /* Pass new config data to the terminal */
  4332. term_reconfig(inst->term, inst->conf);
  4333. setup_clipboards(inst, inst->term, inst->conf);
  4334. /* Pass new config data to the back end */
  4335. if (inst->backend)
  4336. backend_reconfig(inst->backend, inst->conf);
  4337. cache_conf_values(inst);
  4338. need_size = false;
  4339. /*
  4340. * If the scrollbar needs to be shown, hidden, or moved
  4341. * from one end to the other of the window, do so now.
  4342. */
  4343. if (conf_get_bool(oldconf, CONF_scrollbar) !=
  4344. conf_get_bool(newconf, CONF_scrollbar)) {
  4345. show_scrollbar(inst, conf_get_bool(newconf, CONF_scrollbar));
  4346. need_size = true;
  4347. }
  4348. if (conf_get_bool(oldconf, CONF_scrollbar_on_left) !=
  4349. conf_get_bool(newconf, CONF_scrollbar_on_left)) {
  4350. gtk_box_reorder_child(inst->hbox, inst->sbar,
  4351. conf_get_bool(newconf, CONF_scrollbar_on_left)
  4352. ? 0 : 1);
  4353. }
  4354. /*
  4355. * Redo the whole tangled fonts and Unicode mess if
  4356. * necessary.
  4357. */
  4358. if (strcmp(conf_get_fontspec(oldconf, CONF_font)->name,
  4359. conf_get_fontspec(newconf, CONF_font)->name) ||
  4360. strcmp(conf_get_fontspec(oldconf, CONF_boldfont)->name,
  4361. conf_get_fontspec(newconf, CONF_boldfont)->name) ||
  4362. strcmp(conf_get_fontspec(oldconf, CONF_widefont)->name,
  4363. conf_get_fontspec(newconf, CONF_widefont)->name) ||
  4364. strcmp(conf_get_fontspec(oldconf, CONF_wideboldfont)->name,
  4365. conf_get_fontspec(newconf, CONF_wideboldfont)->name) ||
  4366. strcmp(conf_get_str(oldconf, CONF_line_codepage),
  4367. conf_get_str(newconf, CONF_line_codepage)) ||
  4368. conf_get_bool(oldconf, CONF_utf8_override) !=
  4369. conf_get_bool(newconf, CONF_utf8_override) ||
  4370. conf_get_int(oldconf, CONF_vtmode) !=
  4371. conf_get_int(newconf, CONF_vtmode) ||
  4372. conf_get_bool(oldconf, CONF_shadowbold) !=
  4373. conf_get_bool(newconf, CONF_shadowbold) ||
  4374. conf_get_int(oldconf, CONF_shadowboldoffset) !=
  4375. conf_get_int(newconf, CONF_shadowboldoffset)) {
  4376. char *errmsg = setup_fonts_ucs(inst, inst->conf);
  4377. if (errmsg) {
  4378. char *msgboxtext =
  4379. dupprintf("Could not change fonts in terminal window: %s\n",
  4380. errmsg);
  4381. create_message_box(
  4382. inst->window, "Font setup error", msgboxtext,
  4383. string_width("Could not change fonts in terminal window:"),
  4384. false, &buttons_ok, trivial_post_dialog_fn, NULL);
  4385. sfree(msgboxtext);
  4386. sfree(errmsg);
  4387. } else {
  4388. need_size = true;
  4389. }
  4390. }
  4391. /*
  4392. * Resize the window.
  4393. */
  4394. if (conf_get_int(oldconf, CONF_width) !=
  4395. conf_get_int(newconf, CONF_width) ||
  4396. conf_get_int(oldconf, CONF_height) !=
  4397. conf_get_int(newconf, CONF_height) ||
  4398. conf_get_int(oldconf, CONF_window_border) !=
  4399. conf_get_int(newconf, CONF_window_border) ||
  4400. need_size) {
  4401. set_geom_hints(inst);
  4402. request_resize_internal(inst, false,
  4403. conf_get_int(newconf, CONF_width),
  4404. conf_get_int(newconf, CONF_height));
  4405. } else {
  4406. /*
  4407. * The above will have caused a call to term_size() for
  4408. * us if it happened. If the user has fiddled with only
  4409. * the scrollback size, the above will not have
  4410. * happened and we will need an explicit term_size()
  4411. * here.
  4412. */
  4413. if (conf_get_int(oldconf, CONF_savelines) !=
  4414. conf_get_int(newconf, CONF_savelines))
  4415. term_size(inst->term, inst->term->rows, inst->term->cols,
  4416. conf_get_int(newconf, CONF_savelines));
  4417. }
  4418. term_invalidate(inst->term);
  4419. /*
  4420. * We do an explicit full redraw here to ensure the window
  4421. * border has been redrawn as well as the text area.
  4422. */
  4423. gtk_widget_queue_draw(inst->area);
  4424. conf_free(oldconf);
  4425. } else {
  4426. conf_free(newconf);
  4427. }
  4428. }
  4429. static void change_font_size(GtkFrontend *inst, int increment)
  4430. {
  4431. static const int conf_keys[lenof(inst->fonts)] = {
  4432. CONF_font, CONF_boldfont, CONF_widefont, CONF_wideboldfont,
  4433. };
  4434. FontSpec *oldfonts[lenof(inst->fonts)];
  4435. FontSpec *newfonts[lenof(inst->fonts)];
  4436. char *errmsg = NULL;
  4437. int i;
  4438. for (i = 0; i < lenof(newfonts); i++)
  4439. oldfonts[i] = newfonts[i] = NULL;
  4440. for (i = 0; i < lenof(inst->fonts); i++) {
  4441. if (inst->fonts[i]) {
  4442. char *newname = unifont_size_increment(inst->fonts[i], increment);
  4443. if (!newname)
  4444. goto cleanup;
  4445. newfonts[i] = fontspec_new(newname);
  4446. sfree(newname);
  4447. }
  4448. }
  4449. for (i = 0; i < lenof(newfonts); i++) {
  4450. if (newfonts[i]) {
  4451. oldfonts[i] = fontspec_copy(
  4452. conf_get_fontspec(inst->conf, conf_keys[i]));
  4453. conf_set_fontspec(inst->conf, conf_keys[i], newfonts[i]);
  4454. }
  4455. }
  4456. errmsg = setup_fonts_ucs(inst, inst->conf);
  4457. if (errmsg)
  4458. goto cleanup;
  4459. /* Success, so suppress putting everything back */
  4460. for (i = 0; i < lenof(newfonts); i++) {
  4461. if (oldfonts[i]) {
  4462. fontspec_free(oldfonts[i]);
  4463. oldfonts[i] = NULL;
  4464. }
  4465. }
  4466. set_geom_hints(inst);
  4467. request_resize_internal(inst, false, conf_get_int(inst->conf, CONF_width),
  4468. conf_get_int(inst->conf, CONF_height));
  4469. term_invalidate(inst->term);
  4470. gtk_widget_queue_draw(inst->area);
  4471. cleanup:
  4472. for (i = 0; i < lenof(oldfonts); i++) {
  4473. if (oldfonts[i]) {
  4474. conf_set_fontspec(inst->conf, conf_keys[i], oldfonts[i]);
  4475. fontspec_free(oldfonts[i]);
  4476. }
  4477. if (newfonts[i])
  4478. fontspec_free(newfonts[i]);
  4479. }
  4480. sfree(errmsg);
  4481. }
  4482. void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
  4483. {
  4484. GtkFrontend *inst = (GtkFrontend *)gdata;
  4485. launch_duplicate_session(inst->conf);
  4486. }
  4487. void new_session_menuitem(GtkMenuItem *item, gpointer data)
  4488. {
  4489. launch_new_session();
  4490. }
  4491. void restart_session_menuitem(GtkMenuItem *item, gpointer data)
  4492. {
  4493. GtkFrontend *inst = (GtkFrontend *)data;
  4494. if (!inst->backend) {
  4495. logevent(inst->logctx, "----- Session restarted -----");
  4496. term_pwron(inst->term, false);
  4497. start_backend(inst);
  4498. inst->exited = false;
  4499. }
  4500. }
  4501. void saved_session_menuitem(GtkMenuItem *item, gpointer data)
  4502. {
  4503. char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
  4504. launch_saved_session(str);
  4505. }
  4506. void saved_session_freedata(GtkMenuItem *item, gpointer data)
  4507. {
  4508. char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
  4509. sfree(str);
  4510. }
  4511. void app_menu_action(GtkFrontend *frontend, enum MenuAction action)
  4512. {
  4513. GtkFrontend *inst = (GtkFrontend *)frontend;
  4514. switch (action) {
  4515. case MA_COPY:
  4516. copy_clipboard_menuitem(NULL, inst);
  4517. break;
  4518. case MA_PASTE:
  4519. paste_clipboard_menuitem(NULL, inst);
  4520. break;
  4521. case MA_COPY_ALL:
  4522. copy_all_menuitem(NULL, inst);
  4523. break;
  4524. case MA_DUPLICATE_SESSION:
  4525. dup_session_menuitem(NULL, inst);
  4526. break;
  4527. case MA_RESTART_SESSION:
  4528. restart_session_menuitem(NULL, inst);
  4529. break;
  4530. case MA_CHANGE_SETTINGS:
  4531. change_settings_menuitem(NULL, inst);
  4532. break;
  4533. case MA_CLEAR_SCROLLBACK:
  4534. clear_scrollback_menuitem(NULL, inst);
  4535. break;
  4536. case MA_RESET_TERMINAL:
  4537. reset_terminal_menuitem(NULL, inst);
  4538. break;
  4539. case MA_EVENT_LOG:
  4540. event_log_menuitem(NULL, inst);
  4541. break;
  4542. }
  4543. }
  4544. static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
  4545. {
  4546. GtkFrontend *inst = (GtkFrontend *)data;
  4547. struct sesslist sesslist;
  4548. int i;
  4549. gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
  4550. (GtkCallback)gtk_widget_destroy, NULL);
  4551. get_sesslist(&sesslist, true);
  4552. /* skip sesslist.sessions[0] == Default Settings */
  4553. for (i = 1; i < sesslist.nsessions; i++) {
  4554. GtkWidget *menuitem =
  4555. gtk_menu_item_new_with_label(sesslist.sessions[i]);
  4556. gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
  4557. gtk_widget_show(menuitem);
  4558. g_object_set_data(G_OBJECT(menuitem), "user-data",
  4559. dupstr(sesslist.sessions[i]));
  4560. g_signal_connect(G_OBJECT(menuitem), "activate",
  4561. G_CALLBACK(saved_session_menuitem),
  4562. inst);
  4563. g_signal_connect(G_OBJECT(menuitem), "destroy",
  4564. G_CALLBACK(saved_session_freedata),
  4565. inst);
  4566. }
  4567. if (sesslist.nsessions <= 1) {
  4568. GtkWidget *menuitem =
  4569. gtk_menu_item_new_with_label("(No sessions)");
  4570. gtk_widget_set_sensitive(menuitem, false);
  4571. gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
  4572. gtk_widget_show(menuitem);
  4573. }
  4574. get_sesslist(&sesslist, false); /* free up */
  4575. }
  4576. void set_window_icon(GtkWidget *window, const char *const *const *icon,
  4577. int n_icon)
  4578. {
  4579. #if GTK_CHECK_VERSION(2,0,0)
  4580. GList *iconlist;
  4581. int n;
  4582. #else
  4583. GdkPixmap *iconpm;
  4584. GdkBitmap *iconmask;
  4585. #endif
  4586. if (!n_icon)
  4587. return;
  4588. gtk_widget_realize(window);
  4589. #if GTK_CHECK_VERSION(2,0,0)
  4590. gtk_window_set_icon(GTK_WINDOW(window),
  4591. gdk_pixbuf_new_from_xpm_data((const gchar **)icon[0]));
  4592. #else
  4593. iconpm = gdk_pixmap_create_from_xpm_d(gtk_widget_get_window(window),
  4594. &iconmask, NULL, (gchar **)icon[0]);
  4595. gdk_window_set_icon(gtk_widget_get_window(window), NULL, iconpm, iconmask);
  4596. #endif
  4597. #if GTK_CHECK_VERSION(2,0,0)
  4598. iconlist = NULL;
  4599. for (n = 0; n < n_icon; n++) {
  4600. iconlist =
  4601. g_list_append(iconlist,
  4602. gdk_pixbuf_new_from_xpm_data((const gchar **)
  4603. icon[n]));
  4604. }
  4605. gtk_window_set_icon_list(GTK_WINDOW(window), iconlist);
  4606. #endif
  4607. }
  4608. static void free_special_cmd(gpointer data) { sfree(data); }
  4609. static void gtk_seat_update_specials_menu(Seat *seat)
  4610. {
  4611. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  4612. const SessionSpecial *specials;
  4613. if (inst->backend)
  4614. specials = backend_get_specials(inst->backend);
  4615. else
  4616. specials = NULL;
  4617. /* I believe this disposes of submenus too. */
  4618. gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
  4619. (GtkCallback)gtk_widget_destroy, NULL);
  4620. if (specials) {
  4621. int i;
  4622. GtkWidget *menu = inst->specialsmenu;
  4623. /* A lame "stack" for submenus that will do for now. */
  4624. GtkWidget *saved_menu = NULL;
  4625. int nesting = 1;
  4626. for (i = 0; nesting > 0; i++) {
  4627. GtkWidget *menuitem = NULL;
  4628. switch (specials[i].code) {
  4629. case SS_SUBMENU:
  4630. assert (nesting < 2);
  4631. saved_menu = menu; /* XXX lame stacking */
  4632. menu = gtk_menu_new();
  4633. menuitem = gtk_menu_item_new_with_label(specials[i].name);
  4634. gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
  4635. gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
  4636. gtk_widget_show(menuitem);
  4637. menuitem = NULL;
  4638. nesting++;
  4639. break;
  4640. case SS_EXITMENU:
  4641. nesting--;
  4642. if (nesting) {
  4643. menu = saved_menu; /* XXX lame stacking */
  4644. saved_menu = NULL;
  4645. }
  4646. break;
  4647. case SS_SEP:
  4648. menuitem = gtk_menu_item_new();
  4649. break;
  4650. default: {
  4651. menuitem = gtk_menu_item_new_with_label(specials[i].name);
  4652. SessionSpecial *sc = snew(SessionSpecial);
  4653. *sc = specials[i]; /* structure copy */
  4654. g_object_set_data_full(G_OBJECT(menuitem), "user-data",
  4655. sc, free_special_cmd);
  4656. g_signal_connect(G_OBJECT(menuitem), "activate",
  4657. G_CALLBACK(special_menuitem), inst);
  4658. break;
  4659. }
  4660. }
  4661. if (menuitem) {
  4662. gtk_container_add(GTK_CONTAINER(menu), menuitem);
  4663. gtk_widget_show(menuitem);
  4664. }
  4665. }
  4666. gtk_widget_show(inst->specialsitem1);
  4667. gtk_widget_show(inst->specialsitem2);
  4668. } else {
  4669. gtk_widget_hide(inst->specialsitem1);
  4670. gtk_widget_hide(inst->specialsitem2);
  4671. }
  4672. }
  4673. static void start_backend(GtkFrontend *inst)
  4674. {
  4675. const struct BackendVtable *vt;
  4676. char *error, *realhost;
  4677. inst->cmdline_get_passwd_state = cmdline_get_passwd_input_state_new;
  4678. vt = select_backend(inst->conf);
  4679. seat_set_trust_status(&inst->seat, true);
  4680. error = backend_init(vt, &inst->seat, &inst->backend,
  4681. inst->logctx, inst->conf,
  4682. conf_get_str(inst->conf, CONF_host),
  4683. conf_get_int(inst->conf, CONF_port),
  4684. &realhost,
  4685. conf_get_bool(inst->conf, CONF_tcp_nodelay),
  4686. conf_get_bool(inst->conf, CONF_tcp_keepalives));
  4687. if (error) {
  4688. if (cmdline_tooltype & TOOLTYPE_NONNETWORK) {
  4689. /* Special case for pterm. */
  4690. seat_connection_fatal(&inst->seat,
  4691. "Unable to open terminal:\n%s",
  4692. error);
  4693. } else {
  4694. seat_connection_fatal(&inst->seat,
  4695. "Unable to open connection to %s:\n%s",
  4696. conf_dest(inst->conf), error);
  4697. }
  4698. sfree(error);
  4699. inst->exited = true;
  4700. return;
  4701. }
  4702. term_setup_window_titles(inst->term, realhost);
  4703. sfree(realhost);
  4704. term_provide_backend(inst->term, inst->backend);
  4705. inst->ldisc = ldisc_create(inst->conf, inst->term, inst->backend,
  4706. &inst->seat);
  4707. gtk_widget_set_sensitive(inst->restartitem, false);
  4708. }
  4709. #if GTK_CHECK_VERSION(2,0,0)
  4710. static void get_monitor_geometry(GtkWidget *widget, GdkRectangle *geometry)
  4711. {
  4712. #if GTK_CHECK_VERSION(3,4,0)
  4713. GdkDisplay *display = gtk_widget_get_display(widget);
  4714. GdkWindow *gdkwindow = gtk_widget_get_window(widget);
  4715. # if GTK_CHECK_VERSION(3,22,0)
  4716. GdkMonitor *monitor;
  4717. if (gdkwindow)
  4718. monitor = gdk_display_get_monitor_at_window(display, gdkwindow);
  4719. else
  4720. monitor = gdk_display_get_monitor(display, 0);
  4721. gdk_monitor_get_geometry(monitor, geometry);
  4722. # else
  4723. GdkScreen *screen = gdk_display_get_default_screen(display);
  4724. gint monitor_num = gdk_screen_get_monitor_at_window(screen, gdkwindow);
  4725. gdk_screen_get_monitor_geometry(screen, monitor_num, geometry);
  4726. # endif
  4727. #else
  4728. geometry->x = geometry->y = 0;
  4729. geometry->width = gdk_screen_width();
  4730. geometry->height = gdk_screen_height();
  4731. #endif
  4732. }
  4733. #endif
  4734. static const TermWinVtable gtk_termwin_vt = {
  4735. .setup_draw_ctx = gtkwin_setup_draw_ctx,
  4736. .draw_text = gtkwin_draw_text,
  4737. .draw_cursor = gtkwin_draw_cursor,
  4738. .draw_trust_sigil = gtkwin_draw_trust_sigil,
  4739. .char_width = gtkwin_char_width,
  4740. .free_draw_ctx = gtkwin_free_draw_ctx,
  4741. .set_cursor_pos = gtkwin_set_cursor_pos,
  4742. .set_raw_mouse_mode = gtkwin_set_raw_mouse_mode,
  4743. .set_raw_mouse_mode_pointer = gtkwin_set_raw_mouse_mode_pointer,
  4744. .set_scrollbar = gtkwin_set_scrollbar,
  4745. .bell = gtkwin_bell,
  4746. .clip_write = gtkwin_clip_write,
  4747. .clip_request_paste = gtkwin_clip_request_paste,
  4748. .refresh = gtkwin_refresh,
  4749. .request_resize = gtkwin_request_resize,
  4750. .set_title = gtkwin_set_title,
  4751. .set_icon_title = gtkwin_set_icon_title,
  4752. .set_minimised = gtkwin_set_minimised,
  4753. .set_maximised = gtkwin_set_maximised,
  4754. .move = gtkwin_move,
  4755. .set_zorder = gtkwin_set_zorder,
  4756. .palette_set = gtkwin_palette_set,
  4757. .palette_get_overrides = gtkwin_palette_get_overrides,
  4758. .unthrottle = gtkwin_unthrottle,
  4759. };
  4760. void new_session_window(Conf *conf, const char *geometry_string)
  4761. {
  4762. GtkFrontend *inst;
  4763. prepare_session(conf);
  4764. /*
  4765. * Create an instance structure and initialise to zeroes
  4766. */
  4767. inst = snew(GtkFrontend);
  4768. memset(inst, 0, sizeof(*inst));
  4769. #ifdef JUST_USE_GTK_CLIPBOARD_UTF8
  4770. inst->cdi_headtail.next = inst->cdi_headtail.prev = &inst->cdi_headtail;
  4771. #endif
  4772. inst->alt_keycode = -1; /* this one needs _not_ to be zero */
  4773. inst->busy_status = BUSY_NOT;
  4774. inst->conf = conf;
  4775. inst->wintitle = inst->icontitle = NULL;
  4776. inst->drawtype = DRAWTYPE_DEFAULT;
  4777. #if GTK_CHECK_VERSION(3,4,0)
  4778. inst->cumulative_vscroll = 0.0;
  4779. inst->cumulative_hscroll = 0.0;
  4780. #endif
  4781. inst->drawing_area_setup_needed = true;
  4782. inst->termwin.vt = &gtk_termwin_vt;
  4783. inst->seat.vt = &gtk_seat_vt;
  4784. inst->logpolicy.vt = &gtk_logpolicy_vt;
  4785. #ifndef NOT_X_WINDOWS
  4786. inst->disp = get_x11_display();
  4787. if (geometry_string) {
  4788. int flags, x, y;
  4789. unsigned int w, h;
  4790. flags = XParseGeometry(geometry_string, &x, &y, &w, &h);
  4791. if (flags & WidthValue)
  4792. conf_set_int(conf, CONF_width, w);
  4793. if (flags & HeightValue)
  4794. conf_set_int(conf, CONF_height, h);
  4795. if (flags & (XValue | YValue)) {
  4796. inst->xpos = x;
  4797. inst->ypos = y;
  4798. inst->gotpos = true;
  4799. inst->gravity = ((flags & XNegative ? 1 : 0) |
  4800. (flags & YNegative ? 2 : 0));
  4801. }
  4802. }
  4803. #endif
  4804. if (!compound_text_atom)
  4805. compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", false);
  4806. if (!utf8_string_atom)
  4807. utf8_string_atom = gdk_atom_intern("UTF8_STRING", false);
  4808. if (!clipboard_atom)
  4809. clipboard_atom = gdk_atom_intern("CLIPBOARD", false);
  4810. inst->area = gtk_drawing_area_new();
  4811. gtk_widget_set_name(GTK_WIDGET(inst->area), "drawing-area");
  4812. /*
  4813. * Try to create the fonts for use in the window. If this fails,
  4814. * we'll try again with some fallback settings, and only abort
  4815. * completely if we can't find any fonts at all.
  4816. */
  4817. {
  4818. char *errmsg_main = setup_fonts_ucs(inst, inst->conf);
  4819. if (!errmsg_main)
  4820. goto fonts_ok;
  4821. static const char *const fallbacks[] = {
  4822. DEFAULT_GTK_CLIENT_FONT,
  4823. DEFAULT_GTK_SERVER_FONT,
  4824. };
  4825. for (size_t i = 0; i < lenof(fallbacks); i++) {
  4826. Conf *fallback_conf = conf_new();
  4827. do_defaults(NULL, fallback_conf);
  4828. FontSpec *fs = fontspec_new(fallbacks[i]);
  4829. conf_set_fontspec(fallback_conf, CONF_font, fs);
  4830. fontspec_free(fs);
  4831. char *errmsg_fallback = setup_fonts_ucs(inst, fallback_conf);
  4832. conf_free(fallback_conf);
  4833. if (!errmsg_fallback) {
  4834. fprintf(stderr, "%s; falling back to default font '%s'\n",
  4835. errmsg_main, fallbacks[i]);
  4836. sfree(errmsg_main);
  4837. goto fonts_ok;
  4838. }
  4839. sfree(errmsg_fallback);
  4840. }
  4841. window_setup_error(errmsg_main);
  4842. sfree(errmsg_main);
  4843. gtk_widget_destroy(inst->area);
  4844. sfree(inst);
  4845. return;
  4846. fonts_ok:;
  4847. }
  4848. #if GTK_CHECK_VERSION(2,0,0)
  4849. inst->imc = gtk_im_multicontext_new();
  4850. #endif
  4851. inst->window = make_gtk_toplevel_window(inst);
  4852. gtk_widget_set_name(GTK_WIDGET(inst->window), "top-level");
  4853. {
  4854. const char *winclass = conf_get_str(inst->conf, CONF_winclass);
  4855. if (*winclass) {
  4856. #if GTK_CHECK_VERSION(3,22,0)
  4857. #ifndef NOT_X_WINDOWS
  4858. GdkWindow *gdkwin;
  4859. gtk_widget_realize(GTK_WIDGET(inst->window));
  4860. gdkwin = gtk_widget_get_window(GTK_WIDGET(inst->window));
  4861. if (inst->disp && gdk_window_ensure_native(gdkwin)) {
  4862. XClassHint *xch = XAllocClassHint();
  4863. xch->res_name = (char *)winclass;
  4864. xch->res_class = (char *)winclass;
  4865. XSetClassHint(inst->disp, GDK_WINDOW_XID(gdkwin), xch);
  4866. XFree(xch);
  4867. }
  4868. #endif
  4869. /*
  4870. * If we do have NOT_X_WINDOWS set, then we don't have any
  4871. * function in GTK 3.22 equivalent to the above. But then,
  4872. * surely in that situation the deprecated
  4873. * gtk_window_set_wmclass wouldn't have done anything
  4874. * meaningful in previous GTKs either.
  4875. */
  4876. #else
  4877. gtk_window_set_wmclass(GTK_WINDOW(inst->window),
  4878. winclass, winclass);
  4879. #endif
  4880. }
  4881. }
  4882. #if GTK_CHECK_VERSION(2,0,0)
  4883. {
  4884. const BackendVtable *vt = select_backend(inst->conf);
  4885. if (vt && vt->flags & BACKEND_RESIZE_FORBIDDEN)
  4886. gtk_window_set_resizable(GTK_WINDOW(inst->window), false);
  4887. }
  4888. #endif
  4889. inst->width = conf_get_int(inst->conf, CONF_width);
  4890. inst->height = conf_get_int(inst->conf, CONF_height);
  4891. cache_conf_values(inst);
  4892. init_clipboard(inst);
  4893. inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
  4894. inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
  4895. inst->hbox = GTK_BOX(gtk_hbox_new(false, 0));
  4896. /*
  4897. * We always create the scrollbar; it remains invisible if
  4898. * unwanted, so we can pop it up quickly if it suddenly becomes
  4899. * desirable.
  4900. */
  4901. if (conf_get_bool(inst->conf, CONF_scrollbar_on_left))
  4902. gtk_box_pack_start(inst->hbox, inst->sbar, false, false, 0);
  4903. gtk_box_pack_start(inst->hbox, inst->area, true, true, 0);
  4904. if (!conf_get_bool(inst->conf, CONF_scrollbar_on_left))
  4905. gtk_box_pack_start(inst->hbox, inst->sbar, false, false, 0);
  4906. gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
  4907. gtk_widget_show(inst->area);
  4908. show_scrollbar(inst, conf_get_bool(inst->conf, CONF_scrollbar));
  4909. gtk_widget_show(GTK_WIDGET(inst->hbox));
  4910. /*
  4911. * We must call gtk_widget_realize before setting up the geometry
  4912. * hints, so that GtkApplicationWindow will have actually created
  4913. * its menu bar (if it's going to) and hence compute_geom_hints
  4914. * can find it to take its size into account.
  4915. */
  4916. gtk_widget_realize(inst->window);
  4917. set_geom_hints(inst);
  4918. #if GTK_CHECK_VERSION(3,0,0)
  4919. {
  4920. int wp, hp;
  4921. compute_whole_window_size(inst, inst->width, inst->height, &wp, &hp);
  4922. gtk_window_set_default_size(GTK_WINDOW(inst->window), wp, hp);
  4923. }
  4924. #else
  4925. {
  4926. int w = inst->font_width * inst->width + 2*inst->window_border;
  4927. int h = inst->font_height * inst->height + 2*inst->window_border;
  4928. #if GTK_CHECK_VERSION(2,0,0)
  4929. gtk_widget_set_size_request(inst->area, w, h);
  4930. #else
  4931. gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), w, h);
  4932. #endif
  4933. }
  4934. #endif
  4935. #if GTK_CHECK_VERSION(2,0,0)
  4936. if (inst->gotpos) {
  4937. static const GdkGravity gravities[] = {
  4938. GDK_GRAVITY_NORTH_WEST,
  4939. GDK_GRAVITY_NORTH_EAST,
  4940. GDK_GRAVITY_SOUTH_WEST,
  4941. GDK_GRAVITY_SOUTH_EAST,
  4942. };
  4943. int x = inst->xpos, y = inst->ypos;
  4944. int wp, hp;
  4945. GdkRectangle monitor_geometry;
  4946. compute_whole_window_size(inst, inst->width, inst->height, &wp, &hp);
  4947. get_monitor_geometry(GTK_WIDGET(inst->window), &monitor_geometry);
  4948. if (inst->gravity & 1) x += (monitor_geometry.width - wp);
  4949. if (inst->gravity & 2) y += (monitor_geometry.height - hp);
  4950. gtk_window_set_gravity(GTK_WINDOW(inst->window),
  4951. gravities[inst->gravity & 3]);
  4952. gtk_window_move(GTK_WINDOW(inst->window), x, y);
  4953. }
  4954. #else
  4955. if (inst->gotpos) {
  4956. int x = inst->xpos, y = inst->ypos;
  4957. GtkRequisition req;
  4958. gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
  4959. if (inst->gravity & 1) x += gdk_screen_width() - req.width;
  4960. if (inst->gravity & 2) y += gdk_screen_height() - req.height;
  4961. gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
  4962. gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
  4963. }
  4964. #endif
  4965. g_signal_connect(G_OBJECT(inst->window), "destroy",
  4966. G_CALLBACK(destroy), inst);
  4967. g_signal_connect(G_OBJECT(inst->window), "delete_event",
  4968. G_CALLBACK(delete_window), inst);
  4969. g_signal_connect(G_OBJECT(inst->window), "key_press_event",
  4970. G_CALLBACK(key_event), inst);
  4971. g_signal_connect(G_OBJECT(inst->window), "key_release_event",
  4972. G_CALLBACK(key_event), inst);
  4973. g_signal_connect(G_OBJECT(inst->window), "focus_in_event",
  4974. G_CALLBACK(focus_event), inst);
  4975. g_signal_connect(G_OBJECT(inst->window), "focus_out_event",
  4976. G_CALLBACK(focus_event), inst);
  4977. g_signal_connect(G_OBJECT(inst->area), "realize",
  4978. G_CALLBACK(area_realised), inst);
  4979. g_signal_connect(G_OBJECT(inst->area), "size_allocate",
  4980. G_CALLBACK(area_size_allocate), inst);
  4981. g_signal_connect(G_OBJECT(inst->window), "configure_event",
  4982. G_CALLBACK(window_configured), inst);
  4983. #if GTK_CHECK_VERSION(3,10,0)
  4984. g_signal_connect(G_OBJECT(inst->area), "configure_event",
  4985. G_CALLBACK(area_configured), inst);
  4986. #endif
  4987. #if GTK_CHECK_VERSION(3,0,0)
  4988. g_signal_connect(G_OBJECT(inst->area), "draw",
  4989. G_CALLBACK(draw_area), inst);
  4990. #else
  4991. g_signal_connect(G_OBJECT(inst->area), "expose_event",
  4992. G_CALLBACK(expose_area), inst);
  4993. #endif
  4994. g_signal_connect(G_OBJECT(inst->area), "button_press_event",
  4995. G_CALLBACK(button_event), inst);
  4996. g_signal_connect(G_OBJECT(inst->area), "button_release_event",
  4997. G_CALLBACK(button_event), inst);
  4998. #if GTK_CHECK_VERSION(2,0,0)
  4999. g_signal_connect(G_OBJECT(inst->area), "scroll_event",
  5000. G_CALLBACK(scroll_event), inst);
  5001. #endif
  5002. g_signal_connect(G_OBJECT(inst->area), "motion_notify_event",
  5003. G_CALLBACK(motion_event), inst);
  5004. #if GTK_CHECK_VERSION(2,0,0)
  5005. g_signal_connect(G_OBJECT(inst->imc), "commit",
  5006. G_CALLBACK(input_method_commit_event), inst);
  5007. #endif
  5008. if (conf_get_bool(inst->conf, CONF_scrollbar))
  5009. g_signal_connect(G_OBJECT(inst->sbar_adjust), "value_changed",
  5010. G_CALLBACK(scrollbar_moved), inst);
  5011. gtk_widget_add_events(GTK_WIDGET(inst->area),
  5012. GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
  5013. GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
  5014. GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK
  5015. #if GTK_CHECK_VERSION(3,4,0)
  5016. | GDK_SMOOTH_SCROLL_MASK
  5017. #endif
  5018. );
  5019. set_window_icon(inst->window, main_icon, n_main_icon);
  5020. gtk_widget_show(inst->window);
  5021. set_window_background(inst);
  5022. /*
  5023. * Set up the Ctrl+rightclick context menu.
  5024. */
  5025. {
  5026. GtkWidget *menuitem;
  5027. char *s;
  5028. inst->menu = gtk_menu_new();
  5029. #define MKMENUITEM(title, func) do \
  5030. { \
  5031. menuitem = gtk_menu_item_new_with_label(title); \
  5032. gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
  5033. gtk_widget_show(menuitem); \
  5034. g_signal_connect(G_OBJECT(menuitem), "activate", \
  5035. G_CALLBACK(func), inst); \
  5036. } while (0)
  5037. #define MKSUBMENU(title) do \
  5038. { \
  5039. menuitem = gtk_menu_item_new_with_label(title); \
  5040. gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
  5041. gtk_widget_show(menuitem); \
  5042. } while (0)
  5043. #define MKSEP() do \
  5044. { \
  5045. menuitem = gtk_menu_item_new(); \
  5046. gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
  5047. gtk_widget_show(menuitem); \
  5048. } while (0)
  5049. if (new_session)
  5050. MKMENUITEM("New Session...", new_session_menuitem);
  5051. MKMENUITEM("Restart Session", restart_session_menuitem);
  5052. inst->restartitem = menuitem;
  5053. gtk_widget_set_sensitive(inst->restartitem, false);
  5054. MKMENUITEM("Duplicate Session", dup_session_menuitem);
  5055. if (saved_sessions) {
  5056. inst->sessionsmenu = gtk_menu_new();
  5057. /* sessionsmenu will be updated when it's invoked */
  5058. /* XXX is this the right way to do dynamic menus in Gtk? */
  5059. MKMENUITEM("Saved Sessions", update_savedsess_menu);
  5060. gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
  5061. inst->sessionsmenu);
  5062. }
  5063. MKSEP();
  5064. MKMENUITEM("Change Settings...", change_settings_menuitem);
  5065. MKSEP();
  5066. if (use_event_log)
  5067. MKMENUITEM("Event Log", event_log_menuitem);
  5068. MKSUBMENU("Special Commands");
  5069. inst->specialsmenu = gtk_menu_new();
  5070. gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
  5071. inst->specialsitem1 = menuitem;
  5072. MKSEP();
  5073. inst->specialsitem2 = menuitem;
  5074. gtk_widget_hide(inst->specialsitem1);
  5075. gtk_widget_hide(inst->specialsitem2);
  5076. MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
  5077. MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
  5078. MKSEP();
  5079. MKMENUITEM("Copy to " CLIPNAME_EXPLICIT_OBJECT,
  5080. copy_clipboard_menuitem);
  5081. MKMENUITEM("Paste from " CLIPNAME_EXPLICIT_OBJECT,
  5082. paste_clipboard_menuitem);
  5083. MKMENUITEM("Copy All", copy_all_menuitem);
  5084. MKSEP();
  5085. s = dupcat("About ", appname);
  5086. MKMENUITEM(s, about_menuitem);
  5087. sfree(s);
  5088. #undef MKMENUITEM
  5089. #undef MKSUBMENU
  5090. #undef MKSEP
  5091. }
  5092. inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
  5093. inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
  5094. inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
  5095. inst->blankcursor = make_mouse_ptr(inst, -1);
  5096. inst->currcursor = inst->textcursor;
  5097. show_mouseptr(inst, true);
  5098. inst->eventlogstuff = eventlogstuff_new();
  5099. inst->term = term_init(inst->conf, &inst->ucsdata, &inst->termwin);
  5100. setup_clipboards(inst, inst->term, inst->conf);
  5101. inst->logctx = log_init(&inst->logpolicy, inst->conf);
  5102. term_provide_logctx(inst->term, inst->logctx);
  5103. term_size(inst->term, inst->height, inst->width,
  5104. conf_get_int(inst->conf, CONF_savelines));
  5105. #if GTK_CHECK_VERSION(2,0,0)
  5106. /* Delay this signal connection until after inst->term exists */
  5107. g_signal_connect(G_OBJECT(inst->window), "window_state_event",
  5108. G_CALLBACK(window_state_event), inst);
  5109. #endif
  5110. inst->exited = false;
  5111. start_backend(inst);
  5112. if (inst->ldisc) /* early backend failure might make this NULL already */
  5113. ldisc_echoedit_update(inst->ldisc); /* cause ldisc to notice changes */
  5114. }
  5115. static void gtk_seat_set_trust_status(Seat *seat, bool trusted)
  5116. {
  5117. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  5118. term_set_trust_status(inst->term, trusted);
  5119. }
  5120. static bool gtk_seat_can_set_trust_status(Seat *seat)
  5121. {
  5122. return true;
  5123. }
  5124. static bool gtk_seat_get_cursor_position(Seat *seat, int *x, int *y)
  5125. {
  5126. GtkFrontend *inst = container_of(seat, GtkFrontend, seat);
  5127. if (inst->term) {
  5128. term_get_cursor_position(inst->term, x, y);
  5129. return true;
  5130. }
  5131. return false;
  5132. }