display_server_x11.cpp 226 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429
  1. /**************************************************************************/
  2. /* display_server_x11.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "display_server_x11.h"
  31. #ifdef X11_ENABLED
  32. #include "x11/detect_prime_x11.h"
  33. #include "x11/key_mapping_x11.h"
  34. #include "core/config/project_settings.h"
  35. #include "core/math/math_funcs.h"
  36. #include "core/string/print_string.h"
  37. #include "core/string/ustring.h"
  38. #include "core/version.h"
  39. #include "drivers/png/png_driver_common.h"
  40. #include "main/main.h"
  41. #include "servers/rendering/dummy/rasterizer_dummy.h"
  42. #if defined(VULKAN_ENABLED)
  43. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  44. #endif
  45. #if defined(GLES3_ENABLED)
  46. #include "drivers/gles3/rasterizer_gles3.h"
  47. #endif
  48. #ifdef ACCESSKIT_ENABLED
  49. #include "drivers/accesskit/accessibility_driver_accesskit.h"
  50. #endif
  51. #include <dlfcn.h>
  52. #include <limits.h>
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include <sys/stat.h>
  57. #include <sys/types.h>
  58. #include <unistd.h>
  59. #undef CursorShape
  60. #include <X11/XKBlib.h>
  61. // ICCCM
  62. #define WM_NormalState 1L // window normal state
  63. #define WM_IconicState 3L // window minimized
  64. // EWMH
  65. #define _NET_WM_STATE_REMOVE 0L // remove/unset property
  66. #define _NET_WM_STATE_ADD 1L // add/set property
  67. #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0L
  68. #define _NET_WM_MOVERESIZE_SIZE_TOP 1L
  69. #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2L
  70. #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3L
  71. #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4L
  72. #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5L
  73. #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6L
  74. #define _NET_WM_MOVERESIZE_SIZE_LEFT 7L
  75. #define _NET_WM_MOVERESIZE_MOVE 8L
  76. // 2.2 is the first release with multitouch
  77. #define XINPUT_CLIENT_VERSION_MAJOR 2
  78. #define XINPUT_CLIENT_VERSION_MINOR 2
  79. #define VALUATOR_ABSX 0
  80. #define VALUATOR_ABSY 1
  81. #define VALUATOR_PRESSURE 2
  82. #define VALUATOR_TILTX 3
  83. #define VALUATOR_TILTY 4
  84. //#define DISPLAY_SERVER_X11_DEBUG_LOGS_ENABLED
  85. #ifdef DISPLAY_SERVER_X11_DEBUG_LOGS_ENABLED
  86. #define DEBUG_LOG_X11(...) printf(__VA_ARGS__)
  87. #else
  88. #define DEBUG_LOG_X11(...)
  89. #endif
  90. static const double abs_resolution_mult = 10000.0;
  91. static const double abs_resolution_range_mult = 10.0;
  92. // Hints for X11 fullscreen
  93. struct Hints {
  94. unsigned long flags = 0;
  95. unsigned long functions = 0;
  96. unsigned long decorations = 0;
  97. long inputMode = 0;
  98. unsigned long status = 0;
  99. };
  100. static String get_atom_name(Display *p_disp, Atom p_atom) {
  101. char *name = XGetAtomName(p_disp, p_atom);
  102. ERR_FAIL_NULL_V_MSG(name, String(), "Atom is invalid.");
  103. String ret = String::utf8(name);
  104. XFree(name);
  105. return ret;
  106. }
  107. bool DisplayServerX11::has_feature(Feature p_feature) const {
  108. switch (p_feature) {
  109. #ifndef DISABLE_DEPRECATED
  110. case FEATURE_GLOBAL_MENU: {
  111. return (native_menu && native_menu->has_feature(NativeMenu::FEATURE_GLOBAL_MENU));
  112. } break;
  113. #endif
  114. case FEATURE_SUBWINDOWS:
  115. #ifdef TOUCH_ENABLED
  116. case FEATURE_TOUCHSCREEN:
  117. #endif
  118. case FEATURE_MOUSE:
  119. case FEATURE_MOUSE_WARP:
  120. case FEATURE_CLIPBOARD:
  121. case FEATURE_CURSOR_SHAPE:
  122. case FEATURE_CUSTOM_CURSOR_SHAPE:
  123. case FEATURE_IME:
  124. case FEATURE_WINDOW_TRANSPARENCY:
  125. //case FEATURE_HIDPI:
  126. case FEATURE_ICON:
  127. //case FEATURE_NATIVE_ICON:
  128. case FEATURE_SWAP_BUFFERS:
  129. #ifdef DBUS_ENABLED
  130. case FEATURE_KEEP_SCREEN_ON:
  131. #endif
  132. case FEATURE_CLIPBOARD_PRIMARY:
  133. case FEATURE_WINDOW_EMBEDDING:
  134. case FEATURE_WINDOW_DRAG: {
  135. return true;
  136. } break;
  137. //case FEATURE_NATIVE_DIALOG:
  138. //case FEATURE_NATIVE_DIALOG_INPUT:
  139. #ifdef DBUS_ENABLED
  140. case FEATURE_NATIVE_DIALOG_FILE:
  141. case FEATURE_NATIVE_DIALOG_FILE_EXTRA:
  142. case FEATURE_NATIVE_DIALOG_FILE_MIME: {
  143. return (portal_desktop && portal_desktop->is_supported() && portal_desktop->is_file_chooser_supported());
  144. } break;
  145. case FEATURE_NATIVE_COLOR_PICKER: {
  146. return (portal_desktop && portal_desktop->is_supported() && portal_desktop->is_screenshot_supported());
  147. } break;
  148. #endif
  149. case FEATURE_SCREEN_CAPTURE: {
  150. return !xwayland;
  151. } break;
  152. #ifdef SPEECHD_ENABLED
  153. case FEATURE_TEXT_TO_SPEECH: {
  154. return true;
  155. } break;
  156. #endif
  157. #ifdef ACCESSKIT_ENABLED
  158. case FEATURE_ACCESSIBILITY_SCREEN_READER: {
  159. return (accessibility_driver != nullptr);
  160. } break;
  161. #endif
  162. default: {
  163. return false;
  164. }
  165. }
  166. }
  167. String DisplayServerX11::get_name() const {
  168. return "X11";
  169. }
  170. void DisplayServerX11::_update_real_mouse_position(const WindowData &wd) {
  171. Window root_return, child_return;
  172. int root_x, root_y, win_x, win_y;
  173. unsigned int mask_return;
  174. Bool xquerypointer_result = XQueryPointer(x11_display, wd.x11_window, &root_return, &child_return, &root_x, &root_y,
  175. &win_x, &win_y, &mask_return);
  176. if (xquerypointer_result) {
  177. if (win_x > 0 && win_y > 0 && win_x <= wd.size.width && win_y <= wd.size.height) {
  178. last_mouse_pos.x = win_x;
  179. last_mouse_pos.y = win_y;
  180. last_mouse_pos_valid = true;
  181. Input::get_singleton()->set_mouse_position(last_mouse_pos);
  182. }
  183. }
  184. }
  185. bool DisplayServerX11::_refresh_device_info() {
  186. int event_base, error_base;
  187. print_verbose("XInput: Refreshing devices.");
  188. if (!XQueryExtension(x11_display, "XInputExtension", &xi.opcode, &event_base, &error_base)) {
  189. print_verbose("XInput extension not available. Please upgrade your distribution.");
  190. return false;
  191. }
  192. int xi_major_query = XINPUT_CLIENT_VERSION_MAJOR;
  193. int xi_minor_query = XINPUT_CLIENT_VERSION_MINOR;
  194. if (XIQueryVersion(x11_display, &xi_major_query, &xi_minor_query) != Success) {
  195. print_verbose(vformat("XInput 2 not available (server supports %d.%d).", xi_major_query, xi_minor_query));
  196. xi.opcode = 0;
  197. return false;
  198. }
  199. if (xi_major_query < XINPUT_CLIENT_VERSION_MAJOR || (xi_major_query == XINPUT_CLIENT_VERSION_MAJOR && xi_minor_query < XINPUT_CLIENT_VERSION_MINOR)) {
  200. print_verbose(vformat("XInput %d.%d not available (server supports %d.%d). Touch input unavailable.",
  201. XINPUT_CLIENT_VERSION_MAJOR, XINPUT_CLIENT_VERSION_MINOR, xi_major_query, xi_minor_query));
  202. }
  203. xi.absolute_devices.clear();
  204. xi.touch_devices.clear();
  205. xi.pen_inverted_devices.clear();
  206. xi.last_relative_time = 0;
  207. int dev_count;
  208. XIDeviceInfo *info = XIQueryDevice(x11_display, XIAllDevices, &dev_count);
  209. for (int i = 0; i < dev_count; i++) {
  210. XIDeviceInfo *dev = &info[i];
  211. if (!dev->enabled) {
  212. continue;
  213. }
  214. if (!(dev->use == XISlavePointer || dev->use == XIFloatingSlave)) {
  215. continue;
  216. }
  217. bool direct_touch = false;
  218. bool absolute_mode = false;
  219. int resolution_x = 0;
  220. int resolution_y = 0;
  221. double abs_x_min = 0;
  222. double abs_x_max = 0;
  223. double abs_y_min = 0;
  224. double abs_y_max = 0;
  225. double pressure_min = 0;
  226. double pressure_max = 0;
  227. double tilt_x_min = 0;
  228. double tilt_x_max = 0;
  229. double tilt_y_min = 0;
  230. double tilt_y_max = 0;
  231. for (int j = 0; j < dev->num_classes; j++) {
  232. #ifdef TOUCH_ENABLED
  233. if (dev->classes[j]->type == XITouchClass && ((XITouchClassInfo *)dev->classes[j])->mode == XIDirectTouch) {
  234. direct_touch = true;
  235. }
  236. #endif
  237. if (dev->classes[j]->type == XIValuatorClass) {
  238. XIValuatorClassInfo *class_info = (XIValuatorClassInfo *)dev->classes[j];
  239. if (class_info->number == VALUATOR_ABSX && class_info->mode == XIModeAbsolute) {
  240. resolution_x = class_info->resolution;
  241. abs_x_min = class_info->min;
  242. abs_x_max = class_info->max;
  243. absolute_mode = true;
  244. } else if (class_info->number == VALUATOR_ABSY && class_info->mode == XIModeAbsolute) {
  245. resolution_y = class_info->resolution;
  246. abs_y_min = class_info->min;
  247. abs_y_max = class_info->max;
  248. absolute_mode = true;
  249. } else if (class_info->number == VALUATOR_PRESSURE && class_info->mode == XIModeAbsolute) {
  250. pressure_min = class_info->min;
  251. pressure_max = class_info->max;
  252. } else if (class_info->number == VALUATOR_TILTX && class_info->mode == XIModeAbsolute) {
  253. tilt_x_min = class_info->min;
  254. tilt_x_max = class_info->max;
  255. } else if (class_info->number == VALUATOR_TILTY && class_info->mode == XIModeAbsolute) {
  256. tilt_y_min = class_info->min;
  257. tilt_y_max = class_info->max;
  258. }
  259. }
  260. }
  261. if (direct_touch) {
  262. xi.touch_devices.push_back(dev->deviceid);
  263. print_verbose("XInput: Using touch device: " + String(dev->name));
  264. }
  265. if (absolute_mode) {
  266. // If no resolution was reported, use the min/max ranges.
  267. if (resolution_x <= 0) {
  268. resolution_x = (abs_x_max - abs_x_min) * abs_resolution_range_mult;
  269. }
  270. if (resolution_y <= 0) {
  271. resolution_y = (abs_y_max - abs_y_min) * abs_resolution_range_mult;
  272. }
  273. xi.absolute_devices[dev->deviceid] = Vector2(abs_resolution_mult / resolution_x, abs_resolution_mult / resolution_y);
  274. print_verbose("XInput: Absolute pointing device: " + String(dev->name));
  275. }
  276. xi.pressure = 0;
  277. xi.pen_pressure_range[dev->deviceid] = Vector2(pressure_min, pressure_max);
  278. xi.pen_tilt_x_range[dev->deviceid] = Vector2(tilt_x_min, tilt_x_max);
  279. xi.pen_tilt_y_range[dev->deviceid] = Vector2(tilt_y_min, tilt_y_max);
  280. xi.pen_inverted_devices[dev->deviceid] = String(dev->name).findn("eraser") > 0;
  281. }
  282. XIFreeDeviceInfo(info);
  283. #ifdef TOUCH_ENABLED
  284. if (!xi.touch_devices.size()) {
  285. print_verbose("XInput: No touch devices found.");
  286. }
  287. #endif
  288. return true;
  289. }
  290. void DisplayServerX11::_flush_mouse_motion() {
  291. // Block events polling while flushing motion events.
  292. MutexLock mutex_lock(events_mutex);
  293. for (uint32_t event_index = 0; event_index < polled_events.size(); ++event_index) {
  294. XEvent &event = polled_events[event_index];
  295. if (XGetEventData(x11_display, &event.xcookie) && event.xcookie.type == GenericEvent && event.xcookie.extension == xi.opcode) {
  296. XIDeviceEvent *event_data = (XIDeviceEvent *)event.xcookie.data;
  297. if (event_data->evtype == XI_RawMotion) {
  298. XFreeEventData(x11_display, &event.xcookie);
  299. polled_events.remove_at(event_index--);
  300. continue;
  301. }
  302. XFreeEventData(x11_display, &event.xcookie);
  303. break;
  304. }
  305. }
  306. xi.relative_motion.x = 0;
  307. xi.relative_motion.y = 0;
  308. }
  309. #ifdef SPEECHD_ENABLED
  310. void DisplayServerX11::initialize_tts() const {
  311. const_cast<DisplayServerX11 *>(this)->tts = memnew(TTS_Linux);
  312. }
  313. bool DisplayServerX11::tts_is_speaking() const {
  314. if (unlikely(!tts)) {
  315. initialize_tts();
  316. }
  317. ERR_FAIL_NULL_V(tts, false);
  318. return tts->is_speaking();
  319. }
  320. bool DisplayServerX11::tts_is_paused() const {
  321. if (unlikely(!tts)) {
  322. initialize_tts();
  323. }
  324. ERR_FAIL_NULL_V(tts, false);
  325. return tts->is_paused();
  326. }
  327. TypedArray<Dictionary> DisplayServerX11::tts_get_voices() const {
  328. if (unlikely(!tts)) {
  329. initialize_tts();
  330. }
  331. ERR_FAIL_NULL_V(tts, TypedArray<Dictionary>());
  332. return tts->get_voices();
  333. }
  334. void DisplayServerX11::tts_speak(const String &p_text, const String &p_voice, int p_volume, float p_pitch, float p_rate, int p_utterance_id, bool p_interrupt) {
  335. if (unlikely(!tts)) {
  336. initialize_tts();
  337. }
  338. ERR_FAIL_NULL(tts);
  339. tts->speak(p_text, p_voice, p_volume, p_pitch, p_rate, p_utterance_id, p_interrupt);
  340. }
  341. void DisplayServerX11::tts_pause() {
  342. if (unlikely(!tts)) {
  343. initialize_tts();
  344. }
  345. ERR_FAIL_NULL(tts);
  346. tts->pause();
  347. }
  348. void DisplayServerX11::tts_resume() {
  349. if (unlikely(!tts)) {
  350. initialize_tts();
  351. }
  352. ERR_FAIL_NULL(tts);
  353. tts->resume();
  354. }
  355. void DisplayServerX11::tts_stop() {
  356. if (unlikely(!tts)) {
  357. initialize_tts();
  358. }
  359. ERR_FAIL_NULL(tts);
  360. tts->stop();
  361. }
  362. #endif
  363. #ifdef DBUS_ENABLED
  364. bool DisplayServerX11::is_dark_mode_supported() const {
  365. return portal_desktop && portal_desktop->is_supported() && portal_desktop->is_settings_supported();
  366. }
  367. bool DisplayServerX11::is_dark_mode() const {
  368. if (!is_dark_mode_supported()) {
  369. return false;
  370. }
  371. switch (portal_desktop->get_appearance_color_scheme()) {
  372. case 1:
  373. // Prefers dark theme.
  374. return true;
  375. case 2:
  376. // Prefers light theme.
  377. return false;
  378. default:
  379. // Preference unknown.
  380. return false;
  381. }
  382. }
  383. Color DisplayServerX11::get_accent_color() const {
  384. return portal_desktop->get_appearance_accent_color();
  385. }
  386. void DisplayServerX11::set_system_theme_change_callback(const Callable &p_callable) {
  387. portal_desktop->set_system_theme_change_callback(p_callable);
  388. }
  389. Error DisplayServerX11::file_dialog_show(const String &p_title, const String &p_current_directory, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const Callable &p_callback, WindowID p_window_id) {
  390. WindowID window_id = p_window_id;
  391. if (!windows.has(window_id) || windows[window_id].is_popup) {
  392. window_id = MAIN_WINDOW_ID;
  393. }
  394. String xid = vformat("x11:%x", (uint64_t)windows[window_id].x11_window);
  395. return portal_desktop->file_dialog_show(p_window_id, xid, p_title, p_current_directory, String(), p_filename, p_mode, p_filters, TypedArray<Dictionary>(), p_callback, false);
  396. }
  397. Error DisplayServerX11::file_dialog_with_options_show(const String &p_title, const String &p_current_directory, const String &p_root, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const TypedArray<Dictionary> &p_options, const Callable &p_callback, WindowID p_window_id) {
  398. WindowID window_id = p_window_id;
  399. if (!windows.has(window_id) || windows[window_id].is_popup) {
  400. window_id = MAIN_WINDOW_ID;
  401. }
  402. String xid = vformat("x11:%x", (uint64_t)windows[window_id].x11_window);
  403. return portal_desktop->file_dialog_show(p_window_id, xid, p_title, p_current_directory, p_root, p_filename, p_mode, p_filters, p_options, p_callback, true);
  404. }
  405. #endif
  406. void DisplayServerX11::beep() const {
  407. XBell(x11_display, 0);
  408. }
  409. void DisplayServerX11::_mouse_update_mode() {
  410. _THREAD_SAFE_METHOD_
  411. MouseMode wanted_mouse_mode = mouse_mode_override_enabled
  412. ? mouse_mode_override
  413. : mouse_mode_base;
  414. if (wanted_mouse_mode == mouse_mode) {
  415. return;
  416. }
  417. if (mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN) {
  418. XUngrabPointer(x11_display, CurrentTime);
  419. }
  420. // The only modes that show a cursor are VISIBLE and CONFINED
  421. bool show_cursor = (wanted_mouse_mode == MOUSE_MODE_VISIBLE || wanted_mouse_mode == MOUSE_MODE_CONFINED);
  422. bool previously_shown = (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED);
  423. if (show_cursor && !previously_shown) {
  424. WindowID window_id = get_window_at_screen_position(mouse_get_position());
  425. if (window_id != INVALID_WINDOW_ID && window_mouseover_id != window_id) {
  426. if (window_mouseover_id != INVALID_WINDOW_ID) {
  427. _send_window_event(windows[window_mouseover_id], WINDOW_EVENT_MOUSE_EXIT);
  428. }
  429. window_mouseover_id = window_id;
  430. _send_window_event(windows[window_id], WINDOW_EVENT_MOUSE_ENTER);
  431. }
  432. }
  433. for (const KeyValue<WindowID, WindowData> &E : windows) {
  434. if (show_cursor) {
  435. XDefineCursor(x11_display, E.value.x11_window, cursors[current_cursor]); // show cursor
  436. } else {
  437. XDefineCursor(x11_display, E.value.x11_window, null_cursor); // hide cursor
  438. }
  439. }
  440. mouse_mode = wanted_mouse_mode;
  441. if (mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN) {
  442. //flush pending motion events
  443. _flush_mouse_motion();
  444. WindowID window_id = _get_focused_window_or_popup();
  445. if (!windows.has(window_id)) {
  446. window_id = MAIN_WINDOW_ID;
  447. }
  448. WindowData &window = windows[window_id];
  449. if (XGrabPointer(
  450. x11_display, window.x11_window, True,
  451. ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
  452. GrabModeAsync, GrabModeAsync, window.x11_window, None, CurrentTime) != GrabSuccess) {
  453. ERR_PRINT("NO GRAB");
  454. }
  455. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  456. center.x = window.size.width / 2;
  457. center.y = window.size.height / 2;
  458. XWarpPointer(x11_display, None, window.x11_window,
  459. 0, 0, 0, 0, (int)center.x, (int)center.y);
  460. Input::get_singleton()->set_mouse_position(center);
  461. }
  462. } else {
  463. do_mouse_warp = false;
  464. }
  465. XFlush(x11_display);
  466. }
  467. void DisplayServerX11::mouse_set_mode(MouseMode p_mode) {
  468. ERR_FAIL_INDEX(p_mode, MouseMode::MOUSE_MODE_MAX);
  469. if (p_mode == mouse_mode_base) {
  470. return;
  471. }
  472. mouse_mode_base = p_mode;
  473. _mouse_update_mode();
  474. }
  475. DisplayServerX11::MouseMode DisplayServerX11::mouse_get_mode() const {
  476. return mouse_mode;
  477. }
  478. void DisplayServerX11::mouse_set_mode_override(MouseMode p_mode) {
  479. ERR_FAIL_INDEX(p_mode, MouseMode::MOUSE_MODE_MAX);
  480. if (p_mode == mouse_mode_override) {
  481. return;
  482. }
  483. mouse_mode_override = p_mode;
  484. _mouse_update_mode();
  485. }
  486. DisplayServerX11::MouseMode DisplayServerX11::mouse_get_mode_override() const {
  487. return mouse_mode_override;
  488. }
  489. void DisplayServerX11::mouse_set_mode_override_enabled(bool p_override_enabled) {
  490. if (p_override_enabled == mouse_mode_override_enabled) {
  491. return;
  492. }
  493. mouse_mode_override_enabled = p_override_enabled;
  494. _mouse_update_mode();
  495. }
  496. bool DisplayServerX11::mouse_is_mode_override_enabled() const {
  497. return mouse_mode_override_enabled;
  498. }
  499. void DisplayServerX11::warp_mouse(const Point2i &p_position) {
  500. _THREAD_SAFE_METHOD_
  501. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  502. last_mouse_pos = p_position;
  503. } else {
  504. WindowID window_id = _get_focused_window_or_popup();
  505. if (!windows.has(window_id)) {
  506. window_id = MAIN_WINDOW_ID;
  507. }
  508. XWarpPointer(x11_display, None, windows[window_id].x11_window,
  509. 0, 0, 0, 0, (int)p_position.x, (int)p_position.y);
  510. }
  511. }
  512. Point2i DisplayServerX11::mouse_get_position() const {
  513. int number_of_screens = XScreenCount(x11_display);
  514. for (int i = 0; i < number_of_screens; i++) {
  515. Window root, child;
  516. int root_x, root_y, win_x, win_y;
  517. unsigned int mask;
  518. if (XQueryPointer(x11_display, XRootWindow(x11_display, i), &root, &child, &root_x, &root_y, &win_x, &win_y, &mask)) {
  519. XWindowAttributes root_attrs;
  520. XGetWindowAttributes(x11_display, root, &root_attrs);
  521. return Vector2i(root_attrs.x + root_x, root_attrs.y + root_y);
  522. }
  523. }
  524. return Vector2i();
  525. }
  526. BitField<MouseButtonMask> DisplayServerX11::mouse_get_button_state() const {
  527. int number_of_screens = XScreenCount(x11_display);
  528. for (int i = 0; i < number_of_screens; i++) {
  529. Window root, child;
  530. int root_x, root_y, win_x, win_y;
  531. unsigned int mask;
  532. if (XQueryPointer(x11_display, XRootWindow(x11_display, i), &root, &child, &root_x, &root_y, &win_x, &win_y, &mask)) {
  533. BitField<MouseButtonMask> last_button_state = MouseButtonMask::NONE;
  534. if (mask & Button1Mask) {
  535. last_button_state.set_flag(MouseButtonMask::LEFT);
  536. }
  537. if (mask & Button2Mask) {
  538. last_button_state.set_flag(MouseButtonMask::MIDDLE);
  539. }
  540. if (mask & Button3Mask) {
  541. last_button_state.set_flag(MouseButtonMask::RIGHT);
  542. }
  543. if (mask & Button4Mask) {
  544. last_button_state.set_flag(MouseButtonMask::MB_XBUTTON1);
  545. }
  546. if (mask & Button5Mask) {
  547. last_button_state.set_flag(MouseButtonMask::MB_XBUTTON2);
  548. }
  549. return last_button_state;
  550. }
  551. }
  552. return MouseButtonMask::NONE;
  553. }
  554. void DisplayServerX11::clipboard_set(const String &p_text) {
  555. _THREAD_SAFE_METHOD_
  556. {
  557. // The clipboard content can be accessed while polling for events.
  558. MutexLock mutex_lock(events_mutex);
  559. internal_clipboard = p_text;
  560. }
  561. XSetSelectionOwner(x11_display, XA_PRIMARY, windows[MAIN_WINDOW_ID].x11_window, CurrentTime);
  562. XSetSelectionOwner(x11_display, XInternAtom(x11_display, "CLIPBOARD", 0), windows[MAIN_WINDOW_ID].x11_window, CurrentTime);
  563. }
  564. void DisplayServerX11::clipboard_set_primary(const String &p_text) {
  565. _THREAD_SAFE_METHOD_
  566. if (!p_text.is_empty()) {
  567. {
  568. // The clipboard content can be accessed while polling for events.
  569. MutexLock mutex_lock(events_mutex);
  570. internal_clipboard_primary = p_text;
  571. }
  572. XSetSelectionOwner(x11_display, XA_PRIMARY, windows[MAIN_WINDOW_ID].x11_window, CurrentTime);
  573. XSetSelectionOwner(x11_display, XInternAtom(x11_display, "PRIMARY", 0), windows[MAIN_WINDOW_ID].x11_window, CurrentTime);
  574. }
  575. }
  576. Bool DisplayServerX11::_predicate_clipboard_selection(Display *display, XEvent *event, XPointer arg) {
  577. if (event->type == SelectionNotify && event->xselection.requestor == *(Window *)arg) {
  578. return True;
  579. } else {
  580. return False;
  581. }
  582. }
  583. Bool DisplayServerX11::_predicate_clipboard_incr(Display *display, XEvent *event, XPointer arg) {
  584. if (event->type == PropertyNotify && event->xproperty.state == PropertyNewValue && event->xproperty.atom == *(Atom *)arg) {
  585. return True;
  586. } else {
  587. return False;
  588. }
  589. }
  590. String DisplayServerX11::_clipboard_get_impl(Atom p_source, Window x11_window, Atom target) const {
  591. String ret;
  592. Window selection_owner = XGetSelectionOwner(x11_display, p_source);
  593. if (selection_owner == x11_window) {
  594. static const char *target_type = "PRIMARY";
  595. if (p_source != None && get_atom_name(x11_display, p_source) == target_type) {
  596. return internal_clipboard_primary;
  597. } else {
  598. return internal_clipboard;
  599. }
  600. }
  601. if (selection_owner != None) {
  602. // Block events polling while processing selection events.
  603. MutexLock mutex_lock(events_mutex);
  604. Atom selection = XA_PRIMARY;
  605. XConvertSelection(x11_display, p_source, target, selection,
  606. x11_window, CurrentTime);
  607. XFlush(x11_display);
  608. // Blocking wait for predicate to be True and remove the event from the queue.
  609. XEvent event;
  610. XIfEvent(x11_display, &event, _predicate_clipboard_selection, (XPointer)&x11_window);
  611. // Do not get any data, see how much data is there.
  612. Atom type;
  613. int format, result;
  614. unsigned long len, bytes_left, dummy;
  615. unsigned char *data;
  616. XGetWindowProperty(x11_display, x11_window,
  617. selection, // Tricky..
  618. 0, 0, // offset - len
  619. 0, // Delete 0==FALSE
  620. AnyPropertyType, // flag
  621. &type, // return type
  622. &format, // return format
  623. &len, &bytes_left, // data length
  624. &data);
  625. if (data) {
  626. XFree(data);
  627. }
  628. if (type == XInternAtom(x11_display, "INCR", 0)) {
  629. // Data is going to be received incrementally.
  630. DEBUG_LOG_X11("INCR selection started.\n");
  631. LocalVector<uint8_t> incr_data;
  632. uint32_t data_size = 0;
  633. bool success = false;
  634. // Delete INCR property to notify the owner.
  635. XDeleteProperty(x11_display, x11_window, type);
  636. // Process events from the queue.
  637. bool done = false;
  638. while (!done) {
  639. if (!_wait_for_events()) {
  640. // Error or timeout, abort.
  641. break;
  642. }
  643. // Non-blocking wait for next event and remove it from the queue.
  644. XEvent ev;
  645. while (XCheckIfEvent(x11_display, &ev, _predicate_clipboard_incr, (XPointer)&selection)) {
  646. result = XGetWindowProperty(x11_display, x11_window,
  647. selection, // selection type
  648. 0, LONG_MAX, // offset - len
  649. True, // delete property to notify the owner
  650. AnyPropertyType, // flag
  651. &type, // return type
  652. &format, // return format
  653. &len, &bytes_left, // data length
  654. &data);
  655. DEBUG_LOG_X11("PropertyNotify: len=%lu, format=%i\n", len, format);
  656. if (result == Success) {
  657. if (data && (len > 0)) {
  658. uint32_t prev_size = incr_data.size();
  659. if (prev_size == 0) {
  660. // First property contains initial data size.
  661. unsigned long initial_size = *(unsigned long *)data;
  662. incr_data.resize(initial_size);
  663. } else {
  664. // New chunk, resize to be safe and append data.
  665. incr_data.resize(MAX(data_size + len, prev_size));
  666. memcpy(incr_data.ptr() + data_size, data, len);
  667. data_size += len;
  668. }
  669. } else {
  670. // Last chunk, process finished.
  671. done = true;
  672. success = true;
  673. }
  674. } else {
  675. print_verbose("Failed to get selection data chunk.");
  676. done = true;
  677. }
  678. if (data) {
  679. XFree(data);
  680. }
  681. if (done) {
  682. break;
  683. }
  684. }
  685. }
  686. if (success && (data_size > 0)) {
  687. ret.append_utf8((const char *)incr_data.ptr(), data_size);
  688. }
  689. } else if (bytes_left > 0) {
  690. // Data is ready and can be processed all at once.
  691. result = XGetWindowProperty(x11_display, x11_window,
  692. selection, 0, bytes_left, 0,
  693. AnyPropertyType, &type, &format,
  694. &len, &dummy, &data);
  695. if (result == Success) {
  696. ret.append_utf8((const char *)data);
  697. } else {
  698. print_verbose("Failed to get selection data.");
  699. }
  700. if (data) {
  701. XFree(data);
  702. }
  703. }
  704. }
  705. return ret;
  706. }
  707. Atom DisplayServerX11::_clipboard_get_image_target(Atom p_source, Window x11_window) const {
  708. Atom target = XInternAtom(x11_display, "TARGETS", 0);
  709. Atom png = XInternAtom(x11_display, "image/png", 0);
  710. Atom *valid_targets = nullptr;
  711. unsigned long atom_count = 0;
  712. Window selection_owner = XGetSelectionOwner(x11_display, p_source);
  713. if (selection_owner != None && selection_owner != x11_window) {
  714. // Block events polling while processing selection events.
  715. MutexLock mutex_lock(events_mutex);
  716. Atom selection = XA_PRIMARY;
  717. XConvertSelection(x11_display, p_source, target, selection, x11_window, CurrentTime);
  718. XFlush(x11_display);
  719. // Blocking wait for predicate to be True and remove the event from the queue.
  720. XEvent event;
  721. XIfEvent(x11_display, &event, _predicate_clipboard_selection, (XPointer)&x11_window);
  722. // Do not get any data, see how much data is there.
  723. Atom type;
  724. int format, result;
  725. unsigned long len, bytes_left, dummy;
  726. XGetWindowProperty(x11_display, x11_window,
  727. selection, // Tricky..
  728. 0, 0, // offset - len
  729. 0, // Delete 0==FALSE
  730. XA_ATOM, // flag
  731. &type, // return type
  732. &format, // return format
  733. &len, &bytes_left, // data length
  734. (unsigned char **)&valid_targets);
  735. if (valid_targets) {
  736. XFree(valid_targets);
  737. valid_targets = nullptr;
  738. }
  739. if (type == XA_ATOM && bytes_left > 0) {
  740. // Data is ready and can be processed all at once.
  741. result = XGetWindowProperty(x11_display, x11_window,
  742. selection, 0, bytes_left / 4, 0,
  743. XA_ATOM, &type, &format,
  744. &len, &dummy, (unsigned char **)&valid_targets);
  745. if (result == Success) {
  746. atom_count = len;
  747. } else {
  748. print_verbose("Failed to get selection data.");
  749. return None;
  750. }
  751. } else {
  752. return None;
  753. }
  754. } else {
  755. return None;
  756. }
  757. for (unsigned long i = 0; i < atom_count; i++) {
  758. Atom atom = valid_targets[i];
  759. if (atom == png) {
  760. XFree(valid_targets);
  761. return png;
  762. }
  763. }
  764. XFree(valid_targets);
  765. return None;
  766. }
  767. String DisplayServerX11::_clipboard_get(Atom p_source, Window x11_window) const {
  768. String ret;
  769. Atom utf8_atom = XInternAtom(x11_display, "UTF8_STRING", True);
  770. if (utf8_atom != None) {
  771. ret = _clipboard_get_impl(p_source, x11_window, utf8_atom);
  772. }
  773. if (ret.is_empty()) {
  774. ret = _clipboard_get_impl(p_source, x11_window, XA_STRING);
  775. }
  776. return ret;
  777. }
  778. String DisplayServerX11::clipboard_get() const {
  779. _THREAD_SAFE_METHOD_
  780. String ret;
  781. ret = _clipboard_get(XInternAtom(x11_display, "CLIPBOARD", 0), windows[MAIN_WINDOW_ID].x11_window);
  782. if (ret.is_empty()) {
  783. ret = _clipboard_get(XA_PRIMARY, windows[MAIN_WINDOW_ID].x11_window);
  784. }
  785. return ret;
  786. }
  787. String DisplayServerX11::clipboard_get_primary() const {
  788. _THREAD_SAFE_METHOD_
  789. String ret;
  790. ret = _clipboard_get(XInternAtom(x11_display, "PRIMARY", 0), windows[MAIN_WINDOW_ID].x11_window);
  791. if (ret.is_empty()) {
  792. ret = _clipboard_get(XA_PRIMARY, windows[MAIN_WINDOW_ID].x11_window);
  793. }
  794. return ret;
  795. }
  796. Ref<Image> DisplayServerX11::clipboard_get_image() const {
  797. _THREAD_SAFE_METHOD_
  798. Atom clipboard = XInternAtom(x11_display, "CLIPBOARD", 0);
  799. Window x11_window = windows[MAIN_WINDOW_ID].x11_window;
  800. Ref<Image> ret;
  801. Atom target = _clipboard_get_image_target(clipboard, x11_window);
  802. if (target == None) {
  803. return ret;
  804. }
  805. Window selection_owner = XGetSelectionOwner(x11_display, clipboard);
  806. if (selection_owner != None && selection_owner != x11_window) {
  807. // Block events polling while processing selection events.
  808. MutexLock mutex_lock(events_mutex);
  809. // Identifier for the property the other window
  810. // will send the converted data to.
  811. Atom transfer_prop = XA_PRIMARY;
  812. XConvertSelection(x11_display,
  813. clipboard, // source selection
  814. target, // format to convert to
  815. transfer_prop, // output property
  816. x11_window, CurrentTime);
  817. XFlush(x11_display);
  818. // Blocking wait for predicate to be True and remove the event from the queue.
  819. XEvent event;
  820. XIfEvent(x11_display, &event, _predicate_clipboard_selection, (XPointer)&x11_window);
  821. // Do not get any data, see how much data is there.
  822. Atom type;
  823. int format, result;
  824. unsigned long len, bytes_left, dummy;
  825. unsigned char *data;
  826. XGetWindowProperty(x11_display, x11_window,
  827. transfer_prop, // Property data is transferred through
  828. 0, 1, // offset, len (4 so we can get the size if INCR is used)
  829. 0, // Delete 0==FALSE
  830. AnyPropertyType, // flag
  831. &type, // return type
  832. &format, // return format
  833. &len, &bytes_left, // data length
  834. &data);
  835. if (type == XInternAtom(x11_display, "INCR", 0)) {
  836. ERR_FAIL_COND_V_MSG(len != 1, ret, "Incremental transfer initial value was not length.");
  837. // Data is going to be received incrementally.
  838. DEBUG_LOG_X11("INCR selection started.\n");
  839. LocalVector<uint8_t> incr_data;
  840. uint32_t data_size = 0;
  841. bool success = false;
  842. // Initial response is the lower bound of the length of the transferred data.
  843. incr_data.resize(*(unsigned long *)data);
  844. XFree(data);
  845. data = nullptr;
  846. // Delete INCR property to notify the owner.
  847. XDeleteProperty(x11_display, x11_window, transfer_prop);
  848. // Process events from the queue.
  849. bool done = false;
  850. while (!done) {
  851. if (!_wait_for_events()) {
  852. // Error or timeout, abort.
  853. break;
  854. }
  855. // Non-blocking wait for next event and remove it from the queue.
  856. XEvent ev;
  857. while (XCheckIfEvent(x11_display, &ev, _predicate_clipboard_incr, (XPointer)&transfer_prop)) {
  858. result = XGetWindowProperty(x11_display, x11_window,
  859. transfer_prop, // output property
  860. 0, LONG_MAX, // offset - len
  861. True, // delete property to notify the owner
  862. AnyPropertyType, // flag
  863. &type, // return type
  864. &format, // return format
  865. &len, &bytes_left, // data length
  866. &data);
  867. DEBUG_LOG_X11("PropertyNotify: len=%lu, format=%i\n", len, format);
  868. if (result == Success) {
  869. if (data && (len > 0)) {
  870. uint32_t prev_size = incr_data.size();
  871. // New chunk, resize to be safe and append data.
  872. incr_data.resize(MAX(data_size + len, prev_size));
  873. memcpy(incr_data.ptr() + data_size, data, len);
  874. data_size += len;
  875. } else if (!(format == 0 && len == 0)) {
  876. // For unclear reasons the first GetWindowProperty always returns a length and format of 0.
  877. // Otherwise, last chunk, process finished.
  878. done = true;
  879. success = true;
  880. }
  881. } else {
  882. print_verbose("Failed to get selection data chunk.");
  883. done = true;
  884. }
  885. if (data) {
  886. XFree(data);
  887. data = nullptr;
  888. }
  889. if (done) {
  890. break;
  891. }
  892. }
  893. }
  894. if (success && (data_size > 0)) {
  895. ret.instantiate();
  896. PNGDriverCommon::png_to_image(incr_data.ptr(), incr_data.size(), false, ret);
  897. }
  898. } else if (bytes_left > 0) {
  899. if (data) {
  900. XFree(data);
  901. data = nullptr;
  902. }
  903. // Data is ready and can be processed all at once.
  904. result = XGetWindowProperty(x11_display, x11_window,
  905. transfer_prop, 0, bytes_left + 4, 0,
  906. AnyPropertyType, &type, &format,
  907. &len, &dummy, &data);
  908. if (result == Success) {
  909. ret.instantiate();
  910. PNGDriverCommon::png_to_image((uint8_t *)data, bytes_left, false, ret);
  911. } else {
  912. print_verbose("Failed to get selection data.");
  913. }
  914. if (data) {
  915. XFree(data);
  916. }
  917. }
  918. }
  919. return ret;
  920. }
  921. bool DisplayServerX11::clipboard_has_image() const {
  922. Atom target = _clipboard_get_image_target(
  923. XInternAtom(x11_display, "CLIPBOARD", 0),
  924. windows[MAIN_WINDOW_ID].x11_window);
  925. return target != None;
  926. }
  927. Bool DisplayServerX11::_predicate_clipboard_save_targets(Display *display, XEvent *event, XPointer arg) {
  928. if (event->xany.window == *(Window *)arg) {
  929. return (event->type == SelectionRequest) ||
  930. (event->type == SelectionNotify);
  931. } else {
  932. return False;
  933. }
  934. }
  935. void DisplayServerX11::_clipboard_transfer_ownership(Atom p_source, Window x11_window) const {
  936. _THREAD_SAFE_METHOD_
  937. Window selection_owner = XGetSelectionOwner(x11_display, p_source);
  938. if (selection_owner != x11_window) {
  939. return;
  940. }
  941. // Block events polling while processing selection events.
  942. MutexLock mutex_lock(events_mutex);
  943. Atom clipboard_manager = XInternAtom(x11_display, "CLIPBOARD_MANAGER", False);
  944. Atom save_targets = XInternAtom(x11_display, "SAVE_TARGETS", False);
  945. XConvertSelection(x11_display, clipboard_manager, save_targets, None,
  946. x11_window, CurrentTime);
  947. // Process events from the queue.
  948. while (true) {
  949. if (!_wait_for_events()) {
  950. // Error or timeout, abort.
  951. break;
  952. }
  953. // Non-blocking wait for next event and remove it from the queue.
  954. XEvent ev;
  955. while (XCheckIfEvent(x11_display, &ev, _predicate_clipboard_save_targets, (XPointer)&x11_window)) {
  956. switch (ev.type) {
  957. case SelectionRequest:
  958. _handle_selection_request_event(&(ev.xselectionrequest));
  959. break;
  960. case SelectionNotify: {
  961. if (ev.xselection.target == save_targets) {
  962. // Once SelectionNotify is received, we're done whether it succeeded or not.
  963. return;
  964. }
  965. break;
  966. }
  967. }
  968. }
  969. }
  970. }
  971. int DisplayServerX11::get_screen_count() const {
  972. _THREAD_SAFE_METHOD_
  973. int count = 0;
  974. // Using Xinerama Extension
  975. int event_base, error_base;
  976. if (xinerama_ext_ok && XineramaQueryExtension(x11_display, &event_base, &error_base)) {
  977. XineramaScreenInfo *xsi = XineramaQueryScreens(x11_display, &count);
  978. XFree(xsi);
  979. }
  980. if (count == 0) {
  981. count = XScreenCount(x11_display);
  982. }
  983. return count;
  984. }
  985. int DisplayServerX11::get_primary_screen() const {
  986. int event_base, error_base;
  987. if (xinerama_ext_ok && XineramaQueryExtension(x11_display, &event_base, &error_base)) {
  988. return 0;
  989. } else {
  990. return XDefaultScreen(x11_display);
  991. }
  992. }
  993. int DisplayServerX11::get_keyboard_focus_screen() const {
  994. int count = get_screen_count();
  995. if (count < 2) {
  996. // Early exit with single monitor.
  997. return 0;
  998. }
  999. Window focus = 0;
  1000. int revert_to = 0;
  1001. XGetInputFocus(x11_display, &focus, &revert_to);
  1002. if (focus) {
  1003. Window focus_child = 0;
  1004. int x = 0, y = 0;
  1005. XTranslateCoordinates(x11_display, focus, DefaultRootWindow(x11_display), 0, 0, &x, &y, &focus_child);
  1006. XWindowAttributes xwa;
  1007. XGetWindowAttributes(x11_display, focus, &xwa);
  1008. Rect2i window_rect = Rect2i(x, y, xwa.width, xwa.height);
  1009. // Find which monitor has the largest overlap with the given window.
  1010. int screen_index = 0;
  1011. int max_area = 0;
  1012. for (int i = 0; i < count; i++) {
  1013. Rect2i screen_rect = _screen_get_rect(i);
  1014. Rect2i intersection = screen_rect.intersection(window_rect);
  1015. int area = intersection.get_area();
  1016. if (area > max_area) {
  1017. max_area = area;
  1018. screen_index = i;
  1019. }
  1020. }
  1021. return screen_index;
  1022. }
  1023. return get_primary_screen();
  1024. }
  1025. Rect2i DisplayServerX11::_screen_get_rect(int p_screen) const {
  1026. Rect2i rect(0, 0, 0, 0);
  1027. p_screen = _get_screen_index(p_screen);
  1028. ERR_FAIL_COND_V(p_screen < 0, rect);
  1029. // Using Xinerama Extension.
  1030. bool found = false;
  1031. int event_base, error_base;
  1032. if (xinerama_ext_ok && XineramaQueryExtension(x11_display, &event_base, &error_base)) {
  1033. int count;
  1034. XineramaScreenInfo *xsi = XineramaQueryScreens(x11_display, &count);
  1035. if (xsi) {
  1036. if (count > 0) {
  1037. // Check if screen is valid.
  1038. if (p_screen < count) {
  1039. rect.position.x = xsi[p_screen].x_org;
  1040. rect.position.y = xsi[p_screen].y_org;
  1041. rect.size.width = xsi[p_screen].width;
  1042. rect.size.height = xsi[p_screen].height;
  1043. found = true;
  1044. } else {
  1045. ERR_PRINT(vformat("Invalid screen index: %d (count: %d).", p_screen, count));
  1046. }
  1047. }
  1048. XFree(xsi);
  1049. }
  1050. }
  1051. if (!found) {
  1052. int count = XScreenCount(x11_display);
  1053. if (p_screen < count) {
  1054. Window root = XRootWindow(x11_display, p_screen);
  1055. XWindowAttributes xwa;
  1056. XGetWindowAttributes(x11_display, root, &xwa);
  1057. rect.position.x = xwa.x;
  1058. rect.position.y = xwa.y;
  1059. rect.size.width = xwa.width;
  1060. rect.size.height = xwa.height;
  1061. } else {
  1062. ERR_PRINT(vformat("Invalid screen index: %d (count: %d).", p_screen, count));
  1063. }
  1064. }
  1065. return rect;
  1066. }
  1067. Point2i DisplayServerX11::screen_get_position(int p_screen) const {
  1068. _THREAD_SAFE_METHOD_
  1069. return _screen_get_rect(p_screen).position;
  1070. }
  1071. Size2i DisplayServerX11::screen_get_size(int p_screen) const {
  1072. _THREAD_SAFE_METHOD_
  1073. return _screen_get_rect(p_screen).size;
  1074. }
  1075. // A Handler to avoid crashing on non-fatal X errors by default.
  1076. //
  1077. // The original X11 error formatter `_XPrintDefaultError` is defined here:
  1078. // https://gitlab.freedesktop.org/xorg/lib/libx11/-/blob/e45ca7b41dcd3ace7681d6897505f85d374640f2/src/XlibInt.c#L1322
  1079. // It is not exposed through the API, accesses X11 internals,
  1080. // and is much more complex, so this is a less complete simplified error X11 printer.
  1081. int default_window_error_handler(Display *display, XErrorEvent *error) {
  1082. static char message[1024];
  1083. XGetErrorText(display, error->error_code, message, sizeof(message));
  1084. ERR_PRINT(vformat("Unhandled XServer error: %s"
  1085. "\n Major opcode of failed request: %d"
  1086. "\n Serial number of failed request: %d"
  1087. "\n Current serial number in output stream: %d",
  1088. String::utf8(message), (uint64_t)error->request_code, (uint64_t)error->minor_code, (uint64_t)error->serial));
  1089. return 0;
  1090. }
  1091. bool g_bad_window = false;
  1092. int bad_window_error_handler(Display *display, XErrorEvent *error) {
  1093. if (error->error_code == BadWindow) {
  1094. g_bad_window = true;
  1095. } else {
  1096. return default_window_error_handler(display, error);
  1097. }
  1098. return 0;
  1099. }
  1100. Rect2i DisplayServerX11::screen_get_usable_rect(int p_screen) const {
  1101. _THREAD_SAFE_METHOD_
  1102. p_screen = _get_screen_index(p_screen);
  1103. int screen_count = get_screen_count();
  1104. // Check if screen is valid.
  1105. ERR_FAIL_INDEX_V(p_screen, screen_count, Rect2i(0, 0, 0, 0));
  1106. bool is_multiscreen = screen_count > 1;
  1107. // Use full monitor size as fallback.
  1108. Rect2i rect = _screen_get_rect(p_screen);
  1109. // There's generally only one screen reported by xlib even in multi-screen setup,
  1110. // in this case it's just one virtual screen composed of all physical monitors.
  1111. int x11_screen_count = ScreenCount(x11_display);
  1112. Window x11_window = RootWindow(x11_display, p_screen < x11_screen_count ? p_screen : 0);
  1113. Atom type;
  1114. int format = 0;
  1115. unsigned long remaining = 0;
  1116. // Find active desktop for the root window.
  1117. unsigned int desktop_index = 0;
  1118. Atom desktop_prop = XInternAtom(x11_display, "_NET_CURRENT_DESKTOP", True);
  1119. if (desktop_prop != None) {
  1120. unsigned long desktop_len = 0;
  1121. unsigned char *desktop_data = nullptr;
  1122. if (XGetWindowProperty(x11_display, x11_window, desktop_prop, 0, LONG_MAX, False, XA_CARDINAL, &type, &format, &desktop_len, &remaining, &desktop_data) == Success) {
  1123. if ((format == 32) && (desktop_len > 0) && desktop_data) {
  1124. desktop_index = (unsigned int)desktop_data[0];
  1125. }
  1126. if (desktop_data) {
  1127. XFree(desktop_data);
  1128. }
  1129. }
  1130. }
  1131. bool use_simple_method = true;
  1132. // First check for GTK work area, which is more accurate for multi-screen setup.
  1133. if (is_multiscreen) {
  1134. // Use already calculated work area when available.
  1135. Atom gtk_workareas_prop = XInternAtom(x11_display, "_GTK_WORKAREAS", False);
  1136. if (gtk_workareas_prop != None) {
  1137. char gtk_workarea_prop_name[32];
  1138. snprintf(gtk_workarea_prop_name, 32, "_GTK_WORKAREAS_D%d", desktop_index);
  1139. Atom gtk_workarea_prop = XInternAtom(x11_display, gtk_workarea_prop_name, True);
  1140. if (gtk_workarea_prop != None) {
  1141. unsigned long workarea_len = 0;
  1142. unsigned char *workarea_data = nullptr;
  1143. if (XGetWindowProperty(x11_display, x11_window, gtk_workarea_prop, 0, LONG_MAX, False, XA_CARDINAL, &type, &format, &workarea_len, &remaining, &workarea_data) == Success) {
  1144. if ((format == 32) && (workarea_len % 4 == 0) && workarea_data) {
  1145. long *rect_data = (long *)workarea_data;
  1146. for (uint32_t data_offset = 0; data_offset < workarea_len; data_offset += 4) {
  1147. Rect2i workarea_rect;
  1148. workarea_rect.position.x = rect_data[data_offset];
  1149. workarea_rect.position.y = rect_data[data_offset + 1];
  1150. workarea_rect.size.x = rect_data[data_offset + 2];
  1151. workarea_rect.size.y = rect_data[data_offset + 3];
  1152. // Intersect with actual monitor size to find the correct area,
  1153. // because areas are not in the same order as screens from Xinerama.
  1154. if (rect.grow(-1).intersects(workarea_rect)) {
  1155. rect = rect.intersection(workarea_rect);
  1156. XFree(workarea_data);
  1157. return rect;
  1158. }
  1159. }
  1160. }
  1161. }
  1162. if (workarea_data) {
  1163. XFree(workarea_data);
  1164. }
  1165. }
  1166. }
  1167. // Fallback to calculating work area by hand from struts.
  1168. Atom client_list_prop = XInternAtom(x11_display, "_NET_CLIENT_LIST", True);
  1169. if (client_list_prop != None) {
  1170. unsigned long clients_len = 0;
  1171. unsigned char *clients_data = nullptr;
  1172. if (XGetWindowProperty(x11_display, x11_window, client_list_prop, 0, LONG_MAX, False, XA_WINDOW, &type, &format, &clients_len, &remaining, &clients_data) == Success) {
  1173. if ((format == 32) && (clients_len > 0) && clients_data) {
  1174. Window *windows_data = (Window *)clients_data;
  1175. Rect2i desktop_rect;
  1176. bool desktop_valid = false;
  1177. // Get full desktop size.
  1178. {
  1179. Atom desktop_geometry_prop = XInternAtom(x11_display, "_NET_DESKTOP_GEOMETRY", True);
  1180. if (desktop_geometry_prop != None) {
  1181. unsigned long geom_len = 0;
  1182. unsigned char *geom_data = nullptr;
  1183. if (XGetWindowProperty(x11_display, x11_window, desktop_geometry_prop, 0, LONG_MAX, False, XA_CARDINAL, &type, &format, &geom_len, &remaining, &geom_data) == Success) {
  1184. if ((format == 32) && (geom_len >= 2) && geom_data) {
  1185. desktop_valid = true;
  1186. long *size_data = (long *)geom_data;
  1187. desktop_rect.size.x = size_data[0];
  1188. desktop_rect.size.y = size_data[1];
  1189. }
  1190. }
  1191. if (geom_data) {
  1192. XFree(geom_data);
  1193. }
  1194. }
  1195. }
  1196. // Get full desktop position.
  1197. if (desktop_valid) {
  1198. Atom desktop_viewport_prop = XInternAtom(x11_display, "_NET_DESKTOP_VIEWPORT", True);
  1199. if (desktop_viewport_prop != None) {
  1200. unsigned long viewport_len = 0;
  1201. unsigned char *viewport_data = nullptr;
  1202. if (XGetWindowProperty(x11_display, x11_window, desktop_viewport_prop, 0, LONG_MAX, False, XA_CARDINAL, &type, &format, &viewport_len, &remaining, &viewport_data) == Success) {
  1203. if ((format == 32) && (viewport_len >= 2) && viewport_data) {
  1204. desktop_valid = true;
  1205. long *pos_data = (long *)viewport_data;
  1206. desktop_rect.position.x = pos_data[0];
  1207. desktop_rect.position.y = pos_data[1];
  1208. }
  1209. }
  1210. if (viewport_data) {
  1211. XFree(viewport_data);
  1212. }
  1213. }
  1214. }
  1215. if (desktop_valid) {
  1216. // Handle bad window errors silently because there's no other way to check
  1217. // that one of the windows has been destroyed in the meantime.
  1218. int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&bad_window_error_handler);
  1219. for (unsigned long win_index = 0; win_index < clients_len; ++win_index) {
  1220. g_bad_window = false;
  1221. // Remove strut size from desktop size to get a more accurate result.
  1222. bool strut_found = false;
  1223. unsigned long strut_len = 0;
  1224. unsigned char *strut_data = nullptr;
  1225. Atom strut_partial_prop = XInternAtom(x11_display, "_NET_WM_STRUT_PARTIAL", True);
  1226. if (strut_partial_prop != None) {
  1227. if (XGetWindowProperty(x11_display, windows_data[win_index], strut_partial_prop, 0, LONG_MAX, False, XA_CARDINAL, &type, &format, &strut_len, &remaining, &strut_data) == Success) {
  1228. strut_found = true;
  1229. }
  1230. }
  1231. // Fallback to older strut property.
  1232. if (!g_bad_window && !strut_found) {
  1233. Atom strut_prop = XInternAtom(x11_display, "_NET_WM_STRUT", True);
  1234. if (strut_prop != None) {
  1235. if (XGetWindowProperty(x11_display, windows_data[win_index], strut_prop, 0, LONG_MAX, False, XA_CARDINAL, &type, &format, &strut_len, &remaining, &strut_data) == Success) {
  1236. strut_found = true;
  1237. }
  1238. }
  1239. }
  1240. if (!g_bad_window && strut_found && (format == 32) && (strut_len >= 4) && strut_data) {
  1241. use_simple_method = false;
  1242. long *struts = (long *)strut_data;
  1243. long left = struts[0];
  1244. long right = struts[1];
  1245. long top = struts[2];
  1246. long bottom = struts[3];
  1247. long left_start_y, left_end_y, right_start_y, right_end_y;
  1248. long top_start_x, top_end_x, bottom_start_x, bottom_end_x;
  1249. if (strut_len >= 12) {
  1250. left_start_y = struts[4];
  1251. left_end_y = struts[5];
  1252. right_start_y = struts[6];
  1253. right_end_y = struts[7];
  1254. top_start_x = struts[8];
  1255. top_end_x = struts[9];
  1256. bottom_start_x = struts[10];
  1257. bottom_end_x = struts[11];
  1258. } else {
  1259. left_start_y = 0;
  1260. left_end_y = desktop_rect.size.y;
  1261. right_start_y = 0;
  1262. right_end_y = desktop_rect.size.y;
  1263. top_start_x = 0;
  1264. top_end_x = desktop_rect.size.x;
  1265. bottom_start_x = 0;
  1266. bottom_end_x = desktop_rect.size.x;
  1267. }
  1268. const Point2i &pos = desktop_rect.position;
  1269. const Size2i &size = desktop_rect.size;
  1270. Rect2i left_rect(pos.x, pos.y + left_start_y, left, left_end_y - left_start_y);
  1271. if (left_rect.size.x > 0) {
  1272. Rect2i intersection = rect.intersection(left_rect);
  1273. if (intersection.has_area() && intersection.size.x < rect.size.x) {
  1274. rect.position.x = left_rect.size.x;
  1275. rect.size.x = rect.size.x - intersection.size.x;
  1276. }
  1277. }
  1278. Rect2i right_rect(pos.x + size.x - right, pos.y + right_start_y, right, right_end_y - right_start_y);
  1279. if (right_rect.size.x > 0) {
  1280. Rect2i intersection = rect.intersection(right_rect);
  1281. if (intersection.has_area() && right_rect.size.x < rect.size.x) {
  1282. rect.size.x = intersection.position.x - rect.position.x;
  1283. }
  1284. }
  1285. Rect2i top_rect(pos.x + top_start_x, pos.y, top_end_x - top_start_x, top);
  1286. if (top_rect.size.y > 0) {
  1287. Rect2i intersection = rect.intersection(top_rect);
  1288. if (intersection.has_area() && intersection.size.y < rect.size.y) {
  1289. rect.position.y = top_rect.size.y;
  1290. rect.size.y = rect.size.y - intersection.size.y;
  1291. }
  1292. }
  1293. Rect2i bottom_rect(pos.x + bottom_start_x, pos.y + size.y - bottom, bottom_end_x - bottom_start_x, bottom);
  1294. if (bottom_rect.size.y > 0) {
  1295. Rect2i intersection = rect.intersection(bottom_rect);
  1296. if (intersection.has_area() && right_rect.size.y < rect.size.y) {
  1297. rect.size.y = intersection.position.y - rect.position.y;
  1298. }
  1299. }
  1300. }
  1301. if (strut_data) {
  1302. XFree(strut_data);
  1303. }
  1304. }
  1305. // Restore default error handler.
  1306. XSetErrorHandler(oldHandler);
  1307. }
  1308. }
  1309. }
  1310. if (clients_data) {
  1311. XFree(clients_data);
  1312. }
  1313. }
  1314. }
  1315. // Single screen or fallback for multi screen.
  1316. if (use_simple_method) {
  1317. // Get desktop available size from the global work area.
  1318. Atom workarea_prop = XInternAtom(x11_display, "_NET_WORKAREA", True);
  1319. if (workarea_prop != None) {
  1320. unsigned long workarea_len = 0;
  1321. unsigned char *workarea_data = nullptr;
  1322. if (XGetWindowProperty(x11_display, x11_window, workarea_prop, 0, LONG_MAX, False, XA_CARDINAL, &type, &format, &workarea_len, &remaining, &workarea_data) == Success) {
  1323. if ((format == 32) && (workarea_len >= ((desktop_index + 1) * 4)) && workarea_data) {
  1324. long *rect_data = (long *)workarea_data;
  1325. int data_offset = desktop_index * 4;
  1326. Rect2i workarea_rect;
  1327. workarea_rect.position.x = rect_data[data_offset];
  1328. workarea_rect.position.y = rect_data[data_offset + 1];
  1329. workarea_rect.size.x = rect_data[data_offset + 2];
  1330. workarea_rect.size.y = rect_data[data_offset + 3];
  1331. // Intersect with actual monitor size to get a proper approximation in multi-screen setup.
  1332. if (!is_multiscreen) {
  1333. rect = workarea_rect;
  1334. } else if (rect.intersects(workarea_rect)) {
  1335. rect = rect.intersection(workarea_rect);
  1336. }
  1337. }
  1338. }
  1339. if (workarea_data) {
  1340. XFree(workarea_data);
  1341. }
  1342. }
  1343. }
  1344. return rect;
  1345. }
  1346. Rect2i DisplayServerX11::_screens_get_full_rect() const {
  1347. Rect2i full_rect;
  1348. int count = get_screen_count();
  1349. for (int i = 0; i < count; i++) {
  1350. if (i == 0) {
  1351. full_rect = _screen_get_rect(i);
  1352. continue;
  1353. }
  1354. Rect2i screen_rect = _screen_get_rect(i);
  1355. if (full_rect.position.x > screen_rect.position.x) {
  1356. full_rect.size.x += full_rect.position.x - screen_rect.position.x;
  1357. full_rect.position.x = screen_rect.position.x;
  1358. }
  1359. if (full_rect.position.y > screen_rect.position.y) {
  1360. full_rect.size.y += full_rect.position.y - screen_rect.position.y;
  1361. full_rect.position.y = screen_rect.position.y;
  1362. }
  1363. if (full_rect.position.x + full_rect.size.x < screen_rect.position.x + screen_rect.size.x) {
  1364. full_rect.size.x = screen_rect.position.x + screen_rect.size.x - full_rect.position.x;
  1365. }
  1366. if (full_rect.position.y + full_rect.size.y < screen_rect.position.y + screen_rect.size.y) {
  1367. full_rect.size.y = screen_rect.position.y + screen_rect.size.y - full_rect.position.y;
  1368. }
  1369. }
  1370. return full_rect;
  1371. }
  1372. int DisplayServerX11::screen_get_dpi(int p_screen) const {
  1373. _THREAD_SAFE_METHOD_
  1374. p_screen = _get_screen_index(p_screen);
  1375. ERR_FAIL_INDEX_V(p_screen, get_screen_count(), 0);
  1376. //Get physical monitor Dimensions through XRandR and calculate dpi
  1377. Size2i sc = screen_get_size(p_screen);
  1378. if (xrandr_ext_ok) {
  1379. int count = 0;
  1380. if (xrr_get_monitors) {
  1381. xrr_monitor_info *monitors = xrr_get_monitors(x11_display, windows[MAIN_WINDOW_ID].x11_window, true, &count);
  1382. if (p_screen < count) {
  1383. double xdpi = sc.width / (double)monitors[p_screen].mwidth * 25.4;
  1384. double ydpi = sc.height / (double)monitors[p_screen].mheight * 25.4;
  1385. xrr_free_monitors(monitors);
  1386. return (xdpi + ydpi) / 2;
  1387. }
  1388. xrr_free_monitors(monitors);
  1389. } else if (p_screen == 0) {
  1390. XRRScreenSize *sizes = XRRSizes(x11_display, 0, &count);
  1391. if (sizes) {
  1392. double xdpi = sc.width / (double)sizes[0].mwidth * 25.4;
  1393. double ydpi = sc.height / (double)sizes[0].mheight * 25.4;
  1394. return (xdpi + ydpi) / 2;
  1395. }
  1396. }
  1397. }
  1398. int width_mm = DisplayWidthMM(x11_display, p_screen);
  1399. int height_mm = DisplayHeightMM(x11_display, p_screen);
  1400. double xdpi = (width_mm ? sc.width / (double)width_mm * 25.4 : 0);
  1401. double ydpi = (height_mm ? sc.height / (double)height_mm * 25.4 : 0);
  1402. if (xdpi || ydpi) {
  1403. return (xdpi + ydpi) / (xdpi && ydpi ? 2 : 1);
  1404. }
  1405. //could not get dpi
  1406. return 96;
  1407. }
  1408. int get_image_errorhandler(Display *dpy, XErrorEvent *ev) {
  1409. return 0;
  1410. }
  1411. Color DisplayServerX11::screen_get_pixel(const Point2i &p_position) const {
  1412. Point2i pos = p_position;
  1413. if (xwayland) {
  1414. return Color();
  1415. }
  1416. int (*old_handler)(Display *, XErrorEvent *) = XSetErrorHandler(&get_image_errorhandler);
  1417. Color color;
  1418. int number_of_screens = XScreenCount(x11_display);
  1419. for (int i = 0; i < number_of_screens; i++) {
  1420. Window root = XRootWindow(x11_display, i);
  1421. XWindowAttributes root_attrs;
  1422. XGetWindowAttributes(x11_display, root, &root_attrs);
  1423. if ((pos.x >= root_attrs.x) && (pos.x <= root_attrs.x + root_attrs.width) && (pos.y >= root_attrs.y) && (pos.y <= root_attrs.y + root_attrs.height)) {
  1424. XImage *image = XGetImage(x11_display, root, pos.x, pos.y, 1, 1, AllPlanes, XYPixmap);
  1425. if (image) {
  1426. XColor c;
  1427. c.pixel = XGetPixel(image, 0, 0);
  1428. XDestroyImage(image);
  1429. XQueryColor(x11_display, XDefaultColormap(x11_display, i), &c);
  1430. color = Color(float(c.red) / 65535.0, float(c.green) / 65535.0, float(c.blue) / 65535.0, 1.0);
  1431. break;
  1432. }
  1433. }
  1434. }
  1435. XSetErrorHandler(old_handler);
  1436. return color;
  1437. }
  1438. Ref<Image> DisplayServerX11::screen_get_image(int p_screen) const {
  1439. ERR_FAIL_INDEX_V(p_screen, get_screen_count(), Ref<Image>());
  1440. switch (p_screen) {
  1441. case SCREEN_PRIMARY: {
  1442. p_screen = get_primary_screen();
  1443. } break;
  1444. case SCREEN_OF_MAIN_WINDOW: {
  1445. p_screen = window_get_current_screen(MAIN_WINDOW_ID);
  1446. } break;
  1447. default:
  1448. break;
  1449. }
  1450. ERR_FAIL_COND_V(p_screen < 0, Ref<Image>());
  1451. if (xwayland) {
  1452. return Ref<Image>();
  1453. }
  1454. int (*old_handler)(Display *, XErrorEvent *) = XSetErrorHandler(&get_image_errorhandler);
  1455. XImage *image = nullptr;
  1456. bool found = false;
  1457. int event_base, error_base;
  1458. if (xinerama_ext_ok && XineramaQueryExtension(x11_display, &event_base, &error_base)) {
  1459. int xin_count;
  1460. XineramaScreenInfo *xsi = XineramaQueryScreens(x11_display, &xin_count);
  1461. if (xsi) {
  1462. if (xin_count > 0) {
  1463. if (p_screen < xin_count) {
  1464. int x_count = XScreenCount(x11_display);
  1465. for (int i = 0; i < x_count; i++) {
  1466. Window root = XRootWindow(x11_display, i);
  1467. XWindowAttributes root_attrs;
  1468. XGetWindowAttributes(x11_display, root, &root_attrs);
  1469. if ((xsi[p_screen].x_org >= root_attrs.x) && (xsi[p_screen].x_org <= root_attrs.x + root_attrs.width) && (xsi[p_screen].y_org >= root_attrs.y) && (xsi[p_screen].y_org <= root_attrs.y + root_attrs.height)) {
  1470. found = true;
  1471. image = XGetImage(x11_display, root, xsi[p_screen].x_org, xsi[p_screen].y_org, xsi[p_screen].width, xsi[p_screen].height, AllPlanes, ZPixmap);
  1472. break;
  1473. }
  1474. }
  1475. } else {
  1476. ERR_PRINT(vformat("Invalid screen index: %d (count: %d).", p_screen, xin_count));
  1477. }
  1478. }
  1479. XFree(xsi);
  1480. }
  1481. }
  1482. if (!found) {
  1483. int x_count = XScreenCount(x11_display);
  1484. if (p_screen < x_count) {
  1485. Window root = XRootWindow(x11_display, p_screen);
  1486. XWindowAttributes root_attrs;
  1487. XGetWindowAttributes(x11_display, root, &root_attrs);
  1488. image = XGetImage(x11_display, root, root_attrs.x, root_attrs.y, root_attrs.width, root_attrs.height, AllPlanes, ZPixmap);
  1489. } else {
  1490. ERR_PRINT(vformat("Invalid screen index: %d (count: %d).", p_screen, x_count));
  1491. }
  1492. }
  1493. XSetErrorHandler(old_handler);
  1494. Ref<Image> img;
  1495. if (image) {
  1496. int width = image->width;
  1497. int height = image->height;
  1498. Vector<uint8_t> img_data;
  1499. img_data.resize(height * width * 4);
  1500. uint8_t *sr = (uint8_t *)image->data;
  1501. uint8_t *wr = (uint8_t *)img_data.ptrw();
  1502. if (image->bits_per_pixel == 24 && image->red_mask == 0xff0000 && image->green_mask == 0x00ff00 && image->blue_mask == 0x0000ff) {
  1503. for (int y = 0; y < height; y++) {
  1504. for (int x = 0; x < width; x++) {
  1505. wr[(y * width + x) * 4 + 0] = sr[(y * width + x) * 3 + 2];
  1506. wr[(y * width + x) * 4 + 1] = sr[(y * width + x) * 3 + 1];
  1507. wr[(y * width + x) * 4 + 2] = sr[(y * width + x) * 3 + 0];
  1508. wr[(y * width + x) * 4 + 3] = 255;
  1509. }
  1510. }
  1511. } else if (image->bits_per_pixel == 24 && image->red_mask == 0x0000ff && image->green_mask == 0x00ff00 && image->blue_mask == 0xff0000) {
  1512. for (int y = 0; y < height; y++) {
  1513. for (int x = 0; x < width; x++) {
  1514. wr[(y * width + x) * 4 + 0] = sr[(y * width + x) * 3 + 2];
  1515. wr[(y * width + x) * 4 + 1] = sr[(y * width + x) * 3 + 1];
  1516. wr[(y * width + x) * 4 + 2] = sr[(y * width + x) * 3 + 0];
  1517. wr[(y * width + x) * 4 + 3] = 255;
  1518. }
  1519. }
  1520. } else if (image->bits_per_pixel == 32 && image->red_mask == 0xff0000 && image->green_mask == 0x00ff00 && image->blue_mask == 0x0000ff) {
  1521. for (int y = 0; y < height; y++) {
  1522. for (int x = 0; x < width; x++) {
  1523. wr[(y * width + x) * 4 + 0] = sr[(y * width + x) * 4 + 2];
  1524. wr[(y * width + x) * 4 + 1] = sr[(y * width + x) * 4 + 1];
  1525. wr[(y * width + x) * 4 + 2] = sr[(y * width + x) * 4 + 0];
  1526. wr[(y * width + x) * 4 + 3] = 255;
  1527. }
  1528. }
  1529. } else {
  1530. String msg = vformat("XImage with RGB mask %x %x %x and depth %d is not supported.", (uint64_t)image->red_mask, (uint64_t)image->green_mask, (uint64_t)image->blue_mask, (int64_t)image->bits_per_pixel);
  1531. XDestroyImage(image);
  1532. ERR_FAIL_V_MSG(Ref<Image>(), msg);
  1533. }
  1534. img = Image::create_from_data(width, height, false, Image::FORMAT_RGBA8, img_data);
  1535. XDestroyImage(image);
  1536. }
  1537. return img;
  1538. }
  1539. float DisplayServerX11::screen_get_refresh_rate(int p_screen) const {
  1540. _THREAD_SAFE_METHOD_
  1541. p_screen = _get_screen_index(p_screen);
  1542. ERR_FAIL_INDEX_V(p_screen, get_screen_count(), SCREEN_REFRESH_RATE_FALLBACK);
  1543. //Use xrandr to get screen refresh rate.
  1544. if (xrandr_ext_ok) {
  1545. XRRScreenResources *screen_info = XRRGetScreenResourcesCurrent(x11_display, windows[MAIN_WINDOW_ID].x11_window);
  1546. if (screen_info) {
  1547. RRMode current_mode = 0;
  1548. xrr_monitor_info *monitors = nullptr;
  1549. if (xrr_get_monitors) {
  1550. int count = 0;
  1551. monitors = xrr_get_monitors(x11_display, windows[MAIN_WINDOW_ID].x11_window, true, &count);
  1552. ERR_FAIL_INDEX_V(p_screen, count, SCREEN_REFRESH_RATE_FALLBACK);
  1553. } else {
  1554. ERR_PRINT("An error occurred while trying to get the screen refresh rate.");
  1555. return SCREEN_REFRESH_RATE_FALLBACK;
  1556. }
  1557. bool found_active_mode = false;
  1558. for (int crtc = 0; crtc < screen_info->ncrtc; crtc++) { // Loop through outputs to find which one is currently outputting.
  1559. XRRCrtcInfo *monitor_info = XRRGetCrtcInfo(x11_display, screen_info, screen_info->crtcs[crtc]);
  1560. if (monitor_info->x != monitors[p_screen].x || monitor_info->y != monitors[p_screen].y) { // If X and Y aren't the same as the monitor we're looking for, this isn't the right monitor. Continue.
  1561. continue;
  1562. }
  1563. if (monitor_info->mode != None) {
  1564. current_mode = monitor_info->mode;
  1565. found_active_mode = true;
  1566. break;
  1567. }
  1568. }
  1569. if (found_active_mode) {
  1570. for (int mode = 0; mode < screen_info->nmode; mode++) {
  1571. XRRModeInfo m_info = screen_info->modes[mode];
  1572. if (m_info.id == current_mode) {
  1573. // Snap to nearest 0.01 to stay consistent with other platforms.
  1574. return Math::snapped((float)m_info.dotClock / ((float)m_info.hTotal * (float)m_info.vTotal), 0.01);
  1575. }
  1576. }
  1577. }
  1578. ERR_PRINT("An error occurred while trying to get the screen refresh rate."); // We should have returned the refresh rate by now. An error must have occurred.
  1579. return SCREEN_REFRESH_RATE_FALLBACK;
  1580. } else {
  1581. ERR_PRINT("An error occurred while trying to get the screen refresh rate.");
  1582. return SCREEN_REFRESH_RATE_FALLBACK;
  1583. }
  1584. }
  1585. ERR_PRINT("An error occurred while trying to get the screen refresh rate.");
  1586. return SCREEN_REFRESH_RATE_FALLBACK;
  1587. }
  1588. #ifdef DBUS_ENABLED
  1589. void DisplayServerX11::screen_set_keep_on(bool p_enable) {
  1590. if (screen_is_kept_on() == p_enable) {
  1591. return;
  1592. }
  1593. if (p_enable) {
  1594. screensaver->inhibit();
  1595. } else {
  1596. screensaver->uninhibit();
  1597. }
  1598. keep_screen_on = p_enable;
  1599. }
  1600. bool DisplayServerX11::screen_is_kept_on() const {
  1601. return keep_screen_on;
  1602. }
  1603. #endif
  1604. Vector<DisplayServer::WindowID> DisplayServerX11::get_window_list() const {
  1605. _THREAD_SAFE_METHOD_
  1606. Vector<int> ret;
  1607. for (const KeyValue<WindowID, WindowData> &E : windows) {
  1608. ret.push_back(E.key);
  1609. }
  1610. return ret;
  1611. }
  1612. DisplayServer::WindowID DisplayServerX11::create_sub_window(WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Rect2i &p_rect, bool p_exclusive, WindowID p_transient_parent) {
  1613. _THREAD_SAFE_METHOD_
  1614. WindowID id = _create_window(p_mode, p_vsync_mode, p_flags, p_rect, 0);
  1615. for (int i = 0; i < WINDOW_FLAG_MAX; i++) {
  1616. if (p_flags & (1 << i)) {
  1617. window_set_flag(WindowFlags(i), true, id);
  1618. }
  1619. }
  1620. #ifdef RD_ENABLED
  1621. if (rendering_device) {
  1622. rendering_device->screen_create(id);
  1623. }
  1624. #endif
  1625. if (p_transient_parent != INVALID_WINDOW_ID) {
  1626. window_set_transient(id, p_transient_parent);
  1627. }
  1628. return id;
  1629. }
  1630. void DisplayServerX11::show_window(WindowID p_id) {
  1631. _THREAD_SAFE_METHOD_
  1632. const WindowData &wd = windows[p_id];
  1633. popup_open(p_id);
  1634. DEBUG_LOG_X11("show_window: %lu (%u) \n", wd.x11_window, p_id);
  1635. XMapWindow(x11_display, wd.x11_window);
  1636. XSync(x11_display, False);
  1637. _validate_mode_on_map(p_id);
  1638. }
  1639. void DisplayServerX11::delete_sub_window(WindowID p_id) {
  1640. _THREAD_SAFE_METHOD_
  1641. ERR_FAIL_COND(!windows.has(p_id));
  1642. ERR_FAIL_COND_MSG(p_id == MAIN_WINDOW_ID, "Main window can't be deleted");
  1643. popup_close(p_id);
  1644. WindowData &wd = windows[p_id];
  1645. DEBUG_LOG_X11("delete_sub_window: %lu (%u) \n", wd.x11_window, p_id);
  1646. if (window_mouseover_id == p_id) {
  1647. window_mouseover_id = INVALID_WINDOW_ID;
  1648. _send_window_event(windows[p_id], WINDOW_EVENT_MOUSE_EXIT);
  1649. }
  1650. while (wd.transient_children.size()) {
  1651. window_set_transient(*wd.transient_children.begin(), INVALID_WINDOW_ID);
  1652. }
  1653. if (wd.transient_parent != INVALID_WINDOW_ID) {
  1654. window_set_transient(p_id, INVALID_WINDOW_ID);
  1655. }
  1656. #if defined(RD_ENABLED)
  1657. if (rendering_device) {
  1658. rendering_device->screen_free(p_id);
  1659. }
  1660. if (rendering_context) {
  1661. rendering_context->window_destroy(p_id);
  1662. }
  1663. #endif
  1664. #ifdef GLES3_ENABLED
  1665. if (gl_manager) {
  1666. gl_manager->window_destroy(p_id);
  1667. }
  1668. if (gl_manager_egl) {
  1669. gl_manager_egl->window_destroy(p_id);
  1670. }
  1671. #endif
  1672. #ifdef ACCESSKIT_ENABLED
  1673. if (accessibility_driver) {
  1674. accessibility_driver->window_destroy(p_id);
  1675. }
  1676. #endif
  1677. if (wd.xic) {
  1678. XDestroyIC(wd.xic);
  1679. wd.xic = nullptr;
  1680. }
  1681. XDestroyWindow(x11_display, wd.x11_xim_window);
  1682. #ifdef XKB_ENABLED
  1683. if (xkb_loaded_v05p) {
  1684. if (wd.xkb_state) {
  1685. xkb_compose_state_unref(wd.xkb_state);
  1686. wd.xkb_state = nullptr;
  1687. }
  1688. }
  1689. #endif
  1690. XUnmapWindow(x11_display, wd.x11_window);
  1691. XDestroyWindow(x11_display, wd.x11_window);
  1692. window_set_rect_changed_callback(Callable(), p_id);
  1693. window_set_window_event_callback(Callable(), p_id);
  1694. window_set_input_event_callback(Callable(), p_id);
  1695. window_set_input_text_callback(Callable(), p_id);
  1696. window_set_drop_files_callback(Callable(), p_id);
  1697. windows.erase(p_id);
  1698. if (last_focused_window == p_id) {
  1699. last_focused_window = INVALID_WINDOW_ID;
  1700. }
  1701. }
  1702. int64_t DisplayServerX11::window_get_native_handle(HandleType p_handle_type, WindowID p_window) const {
  1703. ERR_FAIL_COND_V(!windows.has(p_window), 0);
  1704. switch (p_handle_type) {
  1705. case DISPLAY_HANDLE: {
  1706. return (int64_t)x11_display;
  1707. }
  1708. case WINDOW_HANDLE: {
  1709. return (int64_t)windows[p_window].x11_window;
  1710. }
  1711. case WINDOW_VIEW: {
  1712. return 0; // Not supported.
  1713. }
  1714. #ifdef GLES3_ENABLED
  1715. case OPENGL_CONTEXT: {
  1716. if (gl_manager) {
  1717. return (int64_t)gl_manager->get_glx_context(p_window);
  1718. }
  1719. if (gl_manager_egl) {
  1720. return (int64_t)gl_manager_egl->get_context(p_window);
  1721. }
  1722. return 0;
  1723. }
  1724. case EGL_DISPLAY: {
  1725. if (gl_manager_egl) {
  1726. return (int64_t)gl_manager_egl->get_display(p_window);
  1727. }
  1728. return 0;
  1729. }
  1730. case EGL_CONFIG: {
  1731. if (gl_manager_egl) {
  1732. return (int64_t)gl_manager_egl->get_config(p_window);
  1733. }
  1734. return 0;
  1735. }
  1736. #endif
  1737. default: {
  1738. return 0;
  1739. }
  1740. }
  1741. }
  1742. void DisplayServerX11::window_attach_instance_id(ObjectID p_instance, WindowID p_window) {
  1743. ERR_FAIL_COND(!windows.has(p_window));
  1744. WindowData &wd = windows[p_window];
  1745. wd.instance_id = p_instance;
  1746. }
  1747. ObjectID DisplayServerX11::window_get_attached_instance_id(WindowID p_window) const {
  1748. ERR_FAIL_COND_V(!windows.has(p_window), ObjectID());
  1749. const WindowData &wd = windows[p_window];
  1750. return wd.instance_id;
  1751. }
  1752. DisplayServerX11::WindowID DisplayServerX11::get_window_at_screen_position(const Point2i &p_position) const {
  1753. WindowID found_window = INVALID_WINDOW_ID;
  1754. WindowID parent_window = INVALID_WINDOW_ID;
  1755. unsigned int focus_order = 0;
  1756. for (const KeyValue<WindowID, WindowData> &E : windows) {
  1757. const WindowData &wd = E.value;
  1758. // Discard windows with no focus.
  1759. if (wd.focus_order == 0) {
  1760. continue;
  1761. }
  1762. // Find topmost window which contains the given position.
  1763. WindowID window_id = E.key;
  1764. Rect2i win_rect = Rect2i(window_get_position(window_id), window_get_size(window_id));
  1765. if (win_rect.has_point(p_position)) {
  1766. // For siblings, pick the window which was focused last.
  1767. if ((parent_window != wd.transient_parent) || (wd.focus_order > focus_order)) {
  1768. found_window = window_id;
  1769. parent_window = wd.transient_parent;
  1770. focus_order = wd.focus_order;
  1771. }
  1772. }
  1773. }
  1774. return found_window;
  1775. }
  1776. void DisplayServerX11::window_set_title(const String &p_title, WindowID p_window) {
  1777. _THREAD_SAFE_METHOD_
  1778. ERR_FAIL_COND(!windows.has(p_window));
  1779. WindowData &wd = windows[p_window];
  1780. XStoreName(x11_display, wd.x11_window, p_title.utf8().get_data());
  1781. Atom _net_wm_name = XInternAtom(x11_display, "_NET_WM_NAME", false);
  1782. Atom utf8_string = XInternAtom(x11_display, "UTF8_STRING", false);
  1783. if (_net_wm_name != None && utf8_string != None) {
  1784. CharString utf8_title = p_title.utf8();
  1785. XChangeProperty(x11_display, wd.x11_window, _net_wm_name, utf8_string, 8, PropModeReplace, (unsigned char *)utf8_title.get_data(), utf8_title.length());
  1786. }
  1787. }
  1788. void DisplayServerX11::window_set_mouse_passthrough(const Vector<Vector2> &p_region, WindowID p_window) {
  1789. _THREAD_SAFE_METHOD_
  1790. ERR_FAIL_COND(!windows.has(p_window));
  1791. windows[p_window].mpath = p_region;
  1792. _update_window_mouse_passthrough(p_window);
  1793. }
  1794. void DisplayServerX11::_update_window_mouse_passthrough(WindowID p_window) {
  1795. ERR_FAIL_COND(!windows.has(p_window));
  1796. ERR_FAIL_COND(!xshaped_ext_ok);
  1797. const Vector<Vector2> region_path = windows[p_window].mpath;
  1798. int event_base, error_base;
  1799. const Bool ext_okay = XShapeQueryExtension(x11_display, &event_base, &error_base);
  1800. if (ext_okay) {
  1801. if (windows[p_window].mpass) {
  1802. Region region = XCreateRegion();
  1803. XShapeCombineRegion(x11_display, windows[p_window].x11_window, ShapeInput, 0, 0, region, ShapeSet);
  1804. XDestroyRegion(region);
  1805. } else if (region_path.is_empty()) {
  1806. XShapeCombineMask(x11_display, windows[p_window].x11_window, ShapeInput, 0, 0, None, ShapeSet);
  1807. } else {
  1808. XPoint *points = (XPoint *)memalloc(sizeof(XPoint) * region_path.size());
  1809. for (int i = 0; i < region_path.size(); i++) {
  1810. points[i].x = region_path[i].x;
  1811. points[i].y = region_path[i].y;
  1812. }
  1813. Region region = XPolygonRegion(points, region_path.size(), EvenOddRule);
  1814. memfree(points);
  1815. XShapeCombineRegion(x11_display, windows[p_window].x11_window, ShapeInput, 0, 0, region, ShapeSet);
  1816. XDestroyRegion(region);
  1817. }
  1818. }
  1819. }
  1820. void DisplayServerX11::window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window) {
  1821. _THREAD_SAFE_METHOD_
  1822. ERR_FAIL_COND(!windows.has(p_window));
  1823. WindowData &wd = windows[p_window];
  1824. wd.rect_changed_callback = p_callable;
  1825. }
  1826. void DisplayServerX11::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) {
  1827. _THREAD_SAFE_METHOD_
  1828. ERR_FAIL_COND(!windows.has(p_window));
  1829. WindowData &wd = windows[p_window];
  1830. wd.event_callback = p_callable;
  1831. }
  1832. void DisplayServerX11::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) {
  1833. _THREAD_SAFE_METHOD_
  1834. ERR_FAIL_COND(!windows.has(p_window));
  1835. WindowData &wd = windows[p_window];
  1836. wd.input_event_callback = p_callable;
  1837. }
  1838. void DisplayServerX11::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) {
  1839. _THREAD_SAFE_METHOD_
  1840. ERR_FAIL_COND(!windows.has(p_window));
  1841. WindowData &wd = windows[p_window];
  1842. wd.input_text_callback = p_callable;
  1843. }
  1844. void DisplayServerX11::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) {
  1845. _THREAD_SAFE_METHOD_
  1846. ERR_FAIL_COND(!windows.has(p_window));
  1847. WindowData &wd = windows[p_window];
  1848. wd.drop_files_callback = p_callable;
  1849. }
  1850. int DisplayServerX11::window_get_current_screen(WindowID p_window) const {
  1851. _THREAD_SAFE_METHOD_
  1852. int count = get_screen_count();
  1853. if (count < 2) {
  1854. // Early exit with single monitor.
  1855. return 0;
  1856. }
  1857. ERR_FAIL_COND_V(!windows.has(p_window), 0);
  1858. const WindowData &wd = windows[p_window];
  1859. const Rect2i window_rect(wd.position, wd.size);
  1860. // Find which monitor has the largest overlap with the given window.
  1861. int screen_index = 0;
  1862. int max_area = 0;
  1863. for (int i = 0; i < count; i++) {
  1864. Rect2i screen_rect = _screen_get_rect(i);
  1865. Rect2i intersection = screen_rect.intersection(window_rect);
  1866. int area = intersection.get_area();
  1867. if (area > max_area) {
  1868. max_area = area;
  1869. screen_index = i;
  1870. }
  1871. }
  1872. return screen_index;
  1873. }
  1874. void DisplayServerX11::gl_window_make_current(DisplayServer::WindowID p_window_id) {
  1875. #if defined(GLES3_ENABLED)
  1876. if (gl_manager) {
  1877. gl_manager->window_make_current(p_window_id);
  1878. }
  1879. if (gl_manager_egl) {
  1880. gl_manager_egl->window_make_current(p_window_id);
  1881. }
  1882. #endif
  1883. }
  1884. void DisplayServerX11::window_set_current_screen(int p_screen, WindowID p_window) {
  1885. _THREAD_SAFE_METHOD_
  1886. ERR_FAIL_COND(!windows.has(p_window));
  1887. WindowData &wd = windows[p_window];
  1888. p_screen = _get_screen_index(p_screen);
  1889. ERR_FAIL_INDEX(p_screen, get_screen_count());
  1890. if (window_get_current_screen(p_window) == p_screen) {
  1891. return;
  1892. }
  1893. if (wd.embed_parent) {
  1894. print_line("Embedded window can't be moved to another screen.");
  1895. return;
  1896. }
  1897. if (window_get_mode(p_window) == WINDOW_MODE_FULLSCREEN || window_get_mode(p_window) == WINDOW_MODE_MAXIMIZED) {
  1898. Point2i position = screen_get_position(p_screen);
  1899. Size2i size = screen_get_size(p_screen);
  1900. XMoveResizeWindow(x11_display, wd.x11_window, position.x, position.y, size.x, size.y);
  1901. } else {
  1902. Rect2i srect = screen_get_usable_rect(p_screen);
  1903. Point2i wpos = window_get_position(p_window) - screen_get_position(window_get_current_screen(p_window));
  1904. Size2i wsize = window_get_size(p_window);
  1905. wpos += srect.position;
  1906. if (srect != Rect2i()) {
  1907. wpos = wpos.clamp(srect.position, srect.position + srect.size - wsize / 3);
  1908. }
  1909. window_set_position(wpos, p_window);
  1910. }
  1911. }
  1912. void DisplayServerX11::window_set_transient(WindowID p_window, WindowID p_parent) {
  1913. _THREAD_SAFE_METHOD_
  1914. ERR_FAIL_COND(p_window == p_parent);
  1915. ERR_FAIL_COND(!windows.has(p_window));
  1916. WindowData &wd_window = windows[p_window];
  1917. WindowID prev_parent = wd_window.transient_parent;
  1918. ERR_FAIL_COND(prev_parent == p_parent);
  1919. DEBUG_LOG_X11("window_set_transient: %lu (%u), prev_parent=%u, parent=%u\n", wd_window.x11_window, p_window, prev_parent, p_parent);
  1920. ERR_FAIL_COND_MSG(wd_window.on_top, "Windows with the 'on top' can't become transient.");
  1921. if (p_parent == INVALID_WINDOW_ID) {
  1922. //remove transient
  1923. ERR_FAIL_COND(prev_parent == INVALID_WINDOW_ID);
  1924. ERR_FAIL_COND(!windows.has(prev_parent));
  1925. WindowData &wd_parent = windows[prev_parent];
  1926. wd_window.transient_parent = INVALID_WINDOW_ID;
  1927. wd_parent.transient_children.erase(p_window);
  1928. XSetTransientForHint(x11_display, wd_window.x11_window, None);
  1929. XWindowAttributes xwa;
  1930. XSync(x11_display, False);
  1931. XGetWindowAttributes(x11_display, wd_parent.x11_window, &xwa);
  1932. // Set focus to parent sub window to avoid losing all focus when closing a nested sub-menu.
  1933. // RevertToPointerRoot is used to make sure we don't lose all focus in case
  1934. // a subwindow and its parent are both destroyed.
  1935. if (!wd_window.no_focus && !wd_window.is_popup && wd_window.focused) {
  1936. if ((xwa.map_state == IsViewable) && !wd_parent.no_focus && !wd_window.is_popup && _window_focus_check()) {
  1937. _set_input_focus(wd_parent.x11_window, RevertToPointerRoot);
  1938. }
  1939. }
  1940. } else {
  1941. ERR_FAIL_COND(!windows.has(p_parent));
  1942. ERR_FAIL_COND_MSG(prev_parent != INVALID_WINDOW_ID, "Window already has a transient parent");
  1943. WindowData &wd_parent = windows[p_parent];
  1944. wd_window.transient_parent = p_parent;
  1945. wd_parent.transient_children.insert(p_window);
  1946. XSetTransientForHint(x11_display, wd_window.x11_window, wd_parent.x11_window);
  1947. }
  1948. }
  1949. // Helper method. Assumes that the window id has already been checked and exists.
  1950. void DisplayServerX11::_update_size_hints(WindowID p_window) {
  1951. WindowData &wd = windows[p_window];
  1952. WindowMode window_mode = window_get_mode(p_window);
  1953. XSizeHints *xsh = XAllocSizeHints();
  1954. // Always set the position and size hints - they should be synchronized with the actual values after the window is mapped anyway
  1955. xsh->flags |= PPosition | PSize;
  1956. xsh->x = wd.position.x;
  1957. xsh->y = wd.position.y;
  1958. xsh->width = wd.size.width;
  1959. xsh->height = wd.size.height;
  1960. if (window_mode == WINDOW_MODE_FULLSCREEN || window_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN) {
  1961. // Do not set any other hints to prevent the window manager from ignoring the fullscreen flags
  1962. } else if (window_get_flag(WINDOW_FLAG_RESIZE_DISABLED, p_window)) {
  1963. // If resizing is disabled, use the forced size
  1964. xsh->flags |= PMinSize | PMaxSize;
  1965. xsh->min_width = wd.size.x;
  1966. xsh->max_width = wd.size.x;
  1967. xsh->min_height = wd.size.y;
  1968. xsh->max_height = wd.size.y;
  1969. } else {
  1970. // Otherwise, just respect min_size and max_size
  1971. if (wd.min_size != Size2i()) {
  1972. xsh->flags |= PMinSize;
  1973. xsh->min_width = wd.min_size.x;
  1974. xsh->min_height = wd.min_size.y;
  1975. }
  1976. if (wd.max_size != Size2i()) {
  1977. xsh->flags |= PMaxSize;
  1978. xsh->max_width = wd.max_size.x;
  1979. xsh->max_height = wd.max_size.y;
  1980. }
  1981. }
  1982. XSetWMNormalHints(x11_display, wd.x11_window, xsh);
  1983. XFree(xsh);
  1984. }
  1985. void DisplayServerX11::_update_actions_hints(WindowID p_window) {
  1986. WindowData &wd = windows[p_window];
  1987. Atom prop = XInternAtom(x11_display, "_NET_WM_ALLOWED_ACTIONS", False);
  1988. if (prop != None) {
  1989. Atom wm_act_max_horz = XInternAtom(x11_display, "_NET_WM_ACTION_MAXIMIZE_HORZ", False);
  1990. Atom wm_act_max_vert = XInternAtom(x11_display, "_NET_WM_ACTION_MAXIMIZE_VERT", False);
  1991. Atom wm_act_min = XInternAtom(x11_display, "_NET_WM_ACTION_MINIMIZE", False);
  1992. Atom type;
  1993. int format;
  1994. unsigned long len;
  1995. unsigned long remaining;
  1996. unsigned char *data = nullptr;
  1997. if (XGetWindowProperty(x11_display, wd.x11_window, prop, 0, 1024, False, XA_ATOM, &type, &format, &len, &remaining, &data) == Success) {
  1998. Atom *atoms = (Atom *)data;
  1999. Vector<Atom> new_atoms;
  2000. for (uint64_t i = 0; i < len; i++) {
  2001. if (atoms[i] != wm_act_max_horz && atoms[i] != wm_act_max_vert && atoms[i] != wm_act_min) {
  2002. new_atoms.push_back(atoms[i]);
  2003. }
  2004. }
  2005. if (!wd.no_max_btn) {
  2006. new_atoms.push_back(wm_act_max_horz);
  2007. new_atoms.push_back(wm_act_max_vert);
  2008. }
  2009. if (!wd.no_min_btn) {
  2010. new_atoms.push_back(wm_act_min);
  2011. }
  2012. XChangeProperty(x11_display, wd.x11_window, prop, XA_ATOM, 32, PropModeReplace, (unsigned char *)new_atoms.ptrw(), new_atoms.size());
  2013. XFree(data);
  2014. }
  2015. }
  2016. }
  2017. Point2i DisplayServerX11::window_get_position(WindowID p_window) const {
  2018. _THREAD_SAFE_METHOD_
  2019. ERR_FAIL_COND_V(!windows.has(p_window), Point2i());
  2020. const WindowData &wd = windows[p_window];
  2021. return wd.position;
  2022. }
  2023. Point2i DisplayServerX11::window_get_position_with_decorations(WindowID p_window) const {
  2024. _THREAD_SAFE_METHOD_
  2025. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  2026. const WindowData &wd = windows[p_window];
  2027. if (wd.fullscreen) {
  2028. return wd.position;
  2029. }
  2030. XWindowAttributes xwa;
  2031. XSync(x11_display, False);
  2032. XGetWindowAttributes(x11_display, wd.x11_window, &xwa);
  2033. int x = wd.position.x;
  2034. int y = wd.position.y;
  2035. Atom prop = XInternAtom(x11_display, "_NET_FRAME_EXTENTS", True);
  2036. if (prop != None) {
  2037. Atom type;
  2038. int format;
  2039. unsigned long len;
  2040. unsigned long remaining;
  2041. unsigned char *data = nullptr;
  2042. if (XGetWindowProperty(x11_display, wd.x11_window, prop, 0, 4, False, AnyPropertyType, &type, &format, &len, &remaining, &data) == Success) {
  2043. if (format == 32 && len == 4 && data) {
  2044. long *extents = (long *)data;
  2045. x -= extents[0]; // left
  2046. y -= extents[2]; // top
  2047. }
  2048. XFree(data);
  2049. }
  2050. }
  2051. return Size2i(x, y);
  2052. }
  2053. void DisplayServerX11::window_set_position(const Point2i &p_position, WindowID p_window) {
  2054. _THREAD_SAFE_METHOD_
  2055. ERR_FAIL_COND(!windows.has(p_window));
  2056. WindowData &wd = windows[p_window];
  2057. if (wd.embed_parent) {
  2058. print_line("Embedded window can't be moved.");
  2059. return;
  2060. }
  2061. wd.position = p_position;
  2062. int x = 0;
  2063. int y = 0;
  2064. if (!window_get_flag(WINDOW_FLAG_BORDERLESS, p_window)) {
  2065. //exclude window decorations
  2066. XSync(x11_display, False);
  2067. Atom prop = XInternAtom(x11_display, "_NET_FRAME_EXTENTS", True);
  2068. if (prop != None) {
  2069. Atom type;
  2070. int format;
  2071. unsigned long len;
  2072. unsigned long remaining;
  2073. unsigned char *data = nullptr;
  2074. if (XGetWindowProperty(x11_display, wd.x11_window, prop, 0, 4, False, AnyPropertyType, &type, &format, &len, &remaining, &data) == Success) {
  2075. if (format == 32 && len == 4 && data) {
  2076. long *extents = (long *)data;
  2077. x = extents[0];
  2078. y = extents[2];
  2079. }
  2080. XFree(data);
  2081. }
  2082. }
  2083. }
  2084. XMoveWindow(x11_display, wd.x11_window, p_position.x - x, p_position.y - y);
  2085. _update_real_mouse_position(wd);
  2086. }
  2087. void DisplayServerX11::window_set_max_size(const Size2i p_size, WindowID p_window) {
  2088. _THREAD_SAFE_METHOD_
  2089. ERR_FAIL_COND(!windows.has(p_window));
  2090. WindowData &wd = windows[p_window];
  2091. if (wd.embed_parent) {
  2092. print_line("Embedded windows can't have a maximum size.");
  2093. return;
  2094. }
  2095. if ((p_size != Size2i()) && ((p_size.x < wd.min_size.x) || (p_size.y < wd.min_size.y))) {
  2096. ERR_PRINT("Maximum window size can't be smaller than minimum window size!");
  2097. return;
  2098. }
  2099. wd.max_size = p_size;
  2100. _update_size_hints(p_window);
  2101. XFlush(x11_display);
  2102. }
  2103. Size2i DisplayServerX11::window_get_max_size(WindowID p_window) const {
  2104. _THREAD_SAFE_METHOD_
  2105. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  2106. const WindowData &wd = windows[p_window];
  2107. return wd.max_size;
  2108. }
  2109. void DisplayServerX11::window_set_min_size(const Size2i p_size, WindowID p_window) {
  2110. _THREAD_SAFE_METHOD_
  2111. ERR_FAIL_COND(!windows.has(p_window));
  2112. WindowData &wd = windows[p_window];
  2113. if (wd.embed_parent) {
  2114. print_line("Embedded windows can't have a minimum size.");
  2115. return;
  2116. }
  2117. if ((p_size != Size2i()) && (wd.max_size != Size2i()) && ((p_size.x > wd.max_size.x) || (p_size.y > wd.max_size.y))) {
  2118. ERR_PRINT("Minimum window size can't be larger than maximum window size!");
  2119. return;
  2120. }
  2121. wd.min_size = p_size;
  2122. _update_size_hints(p_window);
  2123. XFlush(x11_display);
  2124. }
  2125. Size2i DisplayServerX11::window_get_min_size(WindowID p_window) const {
  2126. _THREAD_SAFE_METHOD_
  2127. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  2128. const WindowData &wd = windows[p_window];
  2129. return wd.min_size;
  2130. }
  2131. void DisplayServerX11::window_set_size(const Size2i p_size, WindowID p_window) {
  2132. _THREAD_SAFE_METHOD_
  2133. ERR_FAIL_COND(!windows.has(p_window));
  2134. Size2i size = p_size;
  2135. size = size.maxi(1);
  2136. WindowData &wd = windows[p_window];
  2137. if (wd.embed_parent) {
  2138. print_line("Embedded window can't be resized.");
  2139. return;
  2140. }
  2141. if (wd.size.width == size.width && wd.size.height == size.height) {
  2142. return;
  2143. }
  2144. XWindowAttributes xwa;
  2145. XSync(x11_display, False);
  2146. XGetWindowAttributes(x11_display, wd.x11_window, &xwa);
  2147. int old_w = xwa.width;
  2148. int old_h = xwa.height;
  2149. // Update our videomode width and height
  2150. wd.size = size;
  2151. // Update the size hints first to make sure the window size can be set
  2152. _update_size_hints(p_window);
  2153. // Resize the window
  2154. XResizeWindow(x11_display, wd.x11_window, size.x, size.y);
  2155. for (int timeout = 0; timeout < 50; ++timeout) {
  2156. XSync(x11_display, False);
  2157. XGetWindowAttributes(x11_display, wd.x11_window, &xwa);
  2158. if (old_w != xwa.width || old_h != xwa.height) {
  2159. break;
  2160. }
  2161. OS::get_singleton()->delay_usec(10'000);
  2162. }
  2163. // Keep rendering context window size in sync
  2164. #if defined(RD_ENABLED)
  2165. if (rendering_context) {
  2166. rendering_context->window_set_size(p_window, xwa.width, xwa.height);
  2167. }
  2168. #endif
  2169. #if defined(GLES3_ENABLED)
  2170. if (gl_manager) {
  2171. gl_manager->window_resize(p_window, xwa.width, xwa.height);
  2172. }
  2173. if (gl_manager_egl) {
  2174. gl_manager_egl->window_resize(p_window, xwa.width, xwa.height);
  2175. }
  2176. #endif
  2177. }
  2178. Size2i DisplayServerX11::window_get_size(WindowID p_window) const {
  2179. _THREAD_SAFE_METHOD_
  2180. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  2181. const WindowData &wd = windows[p_window];
  2182. return wd.size;
  2183. }
  2184. Size2i DisplayServerX11::window_get_size_with_decorations(WindowID p_window) const {
  2185. _THREAD_SAFE_METHOD_
  2186. ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
  2187. const WindowData &wd = windows[p_window];
  2188. if (wd.fullscreen) {
  2189. return wd.size;
  2190. }
  2191. XWindowAttributes xwa;
  2192. XSync(x11_display, False);
  2193. XGetWindowAttributes(x11_display, wd.x11_window, &xwa);
  2194. int w = xwa.width;
  2195. int h = xwa.height;
  2196. Atom prop = XInternAtom(x11_display, "_NET_FRAME_EXTENTS", True);
  2197. if (prop != None) {
  2198. Atom type;
  2199. int format;
  2200. unsigned long len;
  2201. unsigned long remaining;
  2202. unsigned char *data = nullptr;
  2203. if (XGetWindowProperty(x11_display, wd.x11_window, prop, 0, 4, False, AnyPropertyType, &type, &format, &len, &remaining, &data) == Success) {
  2204. if (format == 32 && len == 4 && data) {
  2205. long *extents = (long *)data;
  2206. w += extents[0] + extents[1]; // left, right
  2207. h += extents[2] + extents[3]; // top, bottom
  2208. }
  2209. XFree(data);
  2210. }
  2211. }
  2212. return Size2i(w, h);
  2213. }
  2214. // Just a helper to reduce code duplication in `window_is_maximize_allowed`
  2215. // and `_set_wm_maximized`.
  2216. bool DisplayServerX11::_window_maximize_check(WindowID p_window, const char *p_atom_name) const {
  2217. ERR_FAIL_COND_V(!windows.has(p_window), false);
  2218. const WindowData &wd = windows[p_window];
  2219. Atom property = XInternAtom(x11_display, p_atom_name, False);
  2220. Atom type;
  2221. int format;
  2222. unsigned long len;
  2223. unsigned long remaining;
  2224. unsigned char *data = nullptr;
  2225. bool retval = false;
  2226. if (property == None) {
  2227. return false;
  2228. }
  2229. int result = XGetWindowProperty(
  2230. x11_display,
  2231. wd.x11_window,
  2232. property,
  2233. 0,
  2234. 1024,
  2235. False,
  2236. XA_ATOM,
  2237. &type,
  2238. &format,
  2239. &len,
  2240. &remaining,
  2241. &data);
  2242. if (result == Success && data) {
  2243. Atom *atoms = (Atom *)data;
  2244. Atom wm_act_max_horz;
  2245. Atom wm_act_max_vert;
  2246. bool checking_state = strcmp(p_atom_name, "_NET_WM_STATE") == 0;
  2247. if (checking_state) {
  2248. wm_act_max_horz = XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
  2249. wm_act_max_vert = XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
  2250. } else {
  2251. wm_act_max_horz = XInternAtom(x11_display, "_NET_WM_ACTION_MAXIMIZE_HORZ", False);
  2252. wm_act_max_vert = XInternAtom(x11_display, "_NET_WM_ACTION_MAXIMIZE_VERT", False);
  2253. }
  2254. bool found_wm_act_max_horz = false;
  2255. bool found_wm_act_max_vert = false;
  2256. for (uint64_t i = 0; i < len; i++) {
  2257. if (atoms[i] == wm_act_max_horz) {
  2258. found_wm_act_max_horz = true;
  2259. }
  2260. if (atoms[i] == wm_act_max_vert) {
  2261. found_wm_act_max_vert = true;
  2262. }
  2263. if (checking_state) {
  2264. if (found_wm_act_max_horz && found_wm_act_max_vert) {
  2265. retval = true;
  2266. break;
  2267. }
  2268. } else {
  2269. if (found_wm_act_max_horz || found_wm_act_max_vert) {
  2270. retval = true;
  2271. break;
  2272. }
  2273. }
  2274. }
  2275. XFree(data);
  2276. }
  2277. return retval;
  2278. }
  2279. bool DisplayServerX11::_window_minimize_check(WindowID p_window) const {
  2280. const WindowData &wd = windows[p_window];
  2281. // Using EWMH instead of ICCCM, might work better for Wayland users.
  2282. Atom property = XInternAtom(x11_display, "_NET_WM_STATE", True);
  2283. Atom hidden = XInternAtom(x11_display, "_NET_WM_STATE_HIDDEN", True);
  2284. if (property == None || hidden == None) {
  2285. return false;
  2286. }
  2287. Atom type;
  2288. int format;
  2289. unsigned long len;
  2290. unsigned long remaining;
  2291. Atom *atoms = nullptr;
  2292. int result = XGetWindowProperty(
  2293. x11_display,
  2294. wd.x11_window,
  2295. property,
  2296. 0,
  2297. 32,
  2298. False,
  2299. XA_ATOM,
  2300. &type,
  2301. &format,
  2302. &len,
  2303. &remaining,
  2304. (unsigned char **)&atoms);
  2305. if (result == Success && atoms) {
  2306. for (unsigned int i = 0; i < len; i++) {
  2307. if (atoms[i] == hidden) {
  2308. XFree(atoms);
  2309. return true;
  2310. }
  2311. }
  2312. XFree(atoms);
  2313. }
  2314. return false;
  2315. }
  2316. bool DisplayServerX11::_window_fullscreen_check(WindowID p_window) const {
  2317. ERR_FAIL_COND_V(!windows.has(p_window), false);
  2318. const WindowData &wd = windows[p_window];
  2319. // Using EWMH -- Extended Window Manager Hints
  2320. Atom property = XInternAtom(x11_display, "_NET_WM_STATE", False);
  2321. Atom type;
  2322. int format;
  2323. unsigned long len;
  2324. unsigned long remaining;
  2325. unsigned char *data = nullptr;
  2326. bool retval = false;
  2327. if (property == None) {
  2328. return retval;
  2329. }
  2330. int result = XGetWindowProperty(
  2331. x11_display,
  2332. wd.x11_window,
  2333. property,
  2334. 0,
  2335. 1024,
  2336. False,
  2337. XA_ATOM,
  2338. &type,
  2339. &format,
  2340. &len,
  2341. &remaining,
  2342. &data);
  2343. if (result == Success) {
  2344. Atom *atoms = (Atom *)data;
  2345. Atom wm_fullscreen = XInternAtom(x11_display, "_NET_WM_STATE_FULLSCREEN", False);
  2346. for (uint64_t i = 0; i < len; i++) {
  2347. if (atoms[i] == wm_fullscreen) {
  2348. retval = true;
  2349. break;
  2350. }
  2351. }
  2352. XFree(data);
  2353. }
  2354. return retval;
  2355. }
  2356. void DisplayServerX11::_validate_mode_on_map(WindowID p_window) {
  2357. // Check if we applied any window modes that didn't take effect while unmapped
  2358. const WindowData &wd = windows[p_window];
  2359. if (wd.fullscreen && !_window_fullscreen_check(p_window)) {
  2360. _set_wm_fullscreen(p_window, true, wd.exclusive_fullscreen);
  2361. } else if (wd.maximized && !_window_maximize_check(p_window, "_NET_WM_STATE")) {
  2362. _set_wm_maximized(p_window, true);
  2363. } else if (wd.minimized && !_window_minimize_check(p_window)) {
  2364. _set_wm_minimized(p_window, true);
  2365. }
  2366. if (wd.on_top) {
  2367. Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
  2368. Atom wm_above = XInternAtom(x11_display, "_NET_WM_STATE_ABOVE", False);
  2369. XClientMessageEvent xev;
  2370. memset(&xev, 0, sizeof(xev));
  2371. xev.type = ClientMessage;
  2372. xev.window = wd.x11_window;
  2373. xev.message_type = wm_state;
  2374. xev.format = 32;
  2375. xev.data.l[0] = _NET_WM_STATE_ADD;
  2376. xev.data.l[1] = wm_above;
  2377. xev.data.l[3] = 1;
  2378. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&xev);
  2379. }
  2380. }
  2381. bool DisplayServerX11::window_is_maximize_allowed(WindowID p_window) const {
  2382. _THREAD_SAFE_METHOD_
  2383. return _window_maximize_check(p_window, "_NET_WM_ALLOWED_ACTIONS");
  2384. }
  2385. void DisplayServerX11::_set_wm_maximized(WindowID p_window, bool p_enabled) {
  2386. ERR_FAIL_COND(!windows.has(p_window));
  2387. WindowData &wd = windows[p_window];
  2388. // Using EWMH -- Extended Window Manager Hints
  2389. XEvent xev;
  2390. Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
  2391. Atom wm_max_horz = XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
  2392. Atom wm_max_vert = XInternAtom(x11_display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
  2393. memset(&xev, 0, sizeof(xev));
  2394. xev.type = ClientMessage;
  2395. xev.xclient.window = wd.x11_window;
  2396. xev.xclient.message_type = wm_state;
  2397. xev.xclient.format = 32;
  2398. xev.xclient.data.l[0] = p_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
  2399. xev.xclient.data.l[1] = wm_max_horz;
  2400. xev.xclient.data.l[2] = wm_max_vert;
  2401. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  2402. if (p_enabled && window_is_maximize_allowed(p_window)) {
  2403. // Wait for effective resizing (so the GLX context is too).
  2404. // Give up after 0.5s, it's not going to happen on this WM.
  2405. // https://github.com/godotengine/godot/issues/19978
  2406. for (int attempt = 0; window_get_mode(p_window) != WINDOW_MODE_MAXIMIZED && attempt < 50; attempt++) {
  2407. OS::get_singleton()->delay_usec(10'000);
  2408. }
  2409. }
  2410. wd.maximized = p_enabled;
  2411. }
  2412. void DisplayServerX11::_set_wm_minimized(WindowID p_window, bool p_enabled) {
  2413. WindowData &wd = windows[p_window];
  2414. // Using ICCCM -- Inter-Client Communication Conventions Manual
  2415. XEvent xev;
  2416. Atom wm_change = XInternAtom(x11_display, "WM_CHANGE_STATE", False);
  2417. memset(&xev, 0, sizeof(xev));
  2418. xev.type = ClientMessage;
  2419. xev.xclient.window = wd.x11_window;
  2420. xev.xclient.message_type = wm_change;
  2421. xev.xclient.format = 32;
  2422. xev.xclient.data.l[0] = p_enabled ? WM_IconicState : WM_NormalState;
  2423. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  2424. Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
  2425. Atom wm_hidden = XInternAtom(x11_display, "_NET_WM_STATE_HIDDEN", False);
  2426. memset(&xev, 0, sizeof(xev));
  2427. xev.type = ClientMessage;
  2428. xev.xclient.window = wd.x11_window;
  2429. xev.xclient.message_type = wm_state;
  2430. xev.xclient.format = 32;
  2431. xev.xclient.data.l[0] = p_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
  2432. xev.xclient.data.l[1] = wm_hidden;
  2433. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  2434. wd.minimized = p_enabled;
  2435. }
  2436. void DisplayServerX11::_set_wm_fullscreen(WindowID p_window, bool p_enabled, bool p_exclusive) {
  2437. ERR_FAIL_COND(!windows.has(p_window));
  2438. WindowData &wd = windows[p_window];
  2439. if (p_enabled && !window_get_flag(WINDOW_FLAG_BORDERLESS, p_window)) {
  2440. // remove decorations if the window is not already borderless
  2441. Hints hints;
  2442. Atom property;
  2443. hints.flags = 2;
  2444. hints.decorations = 0;
  2445. property = XInternAtom(x11_display, "_MOTIF_WM_HINTS", True);
  2446. if (property != None) {
  2447. XChangeProperty(x11_display, wd.x11_window, property, property, 32, PropModeReplace, (unsigned char *)&hints, 5);
  2448. }
  2449. }
  2450. if (p_enabled) {
  2451. // Set the window as resizable to prevent window managers to ignore the fullscreen state flag.
  2452. _update_size_hints(p_window);
  2453. }
  2454. // Using EWMH -- Extended Window Manager Hints
  2455. XEvent xev;
  2456. Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
  2457. Atom wm_fullscreen = XInternAtom(x11_display, "_NET_WM_STATE_FULLSCREEN", False);
  2458. memset(&xev, 0, sizeof(xev));
  2459. xev.type = ClientMessage;
  2460. xev.xclient.window = wd.x11_window;
  2461. xev.xclient.message_type = wm_state;
  2462. xev.xclient.format = 32;
  2463. xev.xclient.data.l[0] = p_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
  2464. xev.xclient.data.l[1] = wm_fullscreen;
  2465. xev.xclient.data.l[2] = 0;
  2466. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  2467. // set bypass compositor hint
  2468. Atom bypass_compositor = XInternAtom(x11_display, "_NET_WM_BYPASS_COMPOSITOR", False);
  2469. unsigned long compositing_disable_on = 0; // Use default.
  2470. if (p_enabled) {
  2471. if (p_exclusive) {
  2472. compositing_disable_on = 1; // Force composition OFF to reduce overhead.
  2473. } else {
  2474. compositing_disable_on = 2; // Force composition ON to allow popup windows.
  2475. }
  2476. }
  2477. if (bypass_compositor != None) {
  2478. XChangeProperty(x11_display, wd.x11_window, bypass_compositor, XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&compositing_disable_on, 1);
  2479. }
  2480. XFlush(x11_display);
  2481. if (!p_enabled) {
  2482. // Reset the non-resizable flags if we un-set these before.
  2483. _update_size_hints(p_window);
  2484. // put back or remove decorations according to the last set borderless state
  2485. Hints hints;
  2486. Atom property;
  2487. hints.flags = 2;
  2488. hints.decorations = wd.borderless ? 0 : 1;
  2489. property = XInternAtom(x11_display, "_MOTIF_WM_HINTS", True);
  2490. if (property != None) {
  2491. XChangeProperty(x11_display, wd.x11_window, property, property, 32, PropModeReplace, (unsigned char *)&hints, 5);
  2492. }
  2493. }
  2494. }
  2495. void DisplayServerX11::window_set_mode(WindowMode p_mode, WindowID p_window) {
  2496. _THREAD_SAFE_METHOD_
  2497. ERR_FAIL_COND(!windows.has(p_window));
  2498. WindowData &wd = windows[p_window];
  2499. WindowMode old_mode = window_get_mode(p_window);
  2500. if (old_mode == p_mode) {
  2501. return; // do nothing
  2502. }
  2503. if (p_mode != WINDOW_MODE_WINDOWED && wd.embed_parent) {
  2504. print_line("Embedded window only supports Windowed mode.");
  2505. return;
  2506. }
  2507. // Remove all "extra" modes.
  2508. switch (old_mode) {
  2509. case WINDOW_MODE_WINDOWED: {
  2510. //do nothing
  2511. } break;
  2512. case WINDOW_MODE_MINIMIZED: {
  2513. _set_wm_minimized(p_window, false);
  2514. } break;
  2515. case WINDOW_MODE_EXCLUSIVE_FULLSCREEN:
  2516. case WINDOW_MODE_FULLSCREEN: {
  2517. //Remove full-screen
  2518. wd.fullscreen = false;
  2519. wd.exclusive_fullscreen = false;
  2520. _set_wm_fullscreen(p_window, false, false);
  2521. //un-maximize required for always on top
  2522. bool on_top = window_get_flag(WINDOW_FLAG_ALWAYS_ON_TOP, p_window);
  2523. window_set_position(wd.last_position_before_fs, p_window);
  2524. if (on_top) {
  2525. _set_wm_maximized(p_window, false);
  2526. }
  2527. } break;
  2528. case WINDOW_MODE_MAXIMIZED: {
  2529. _set_wm_maximized(p_window, false);
  2530. } break;
  2531. }
  2532. switch (p_mode) {
  2533. case WINDOW_MODE_WINDOWED: {
  2534. //do nothing
  2535. } break;
  2536. case WINDOW_MODE_MINIMIZED: {
  2537. _set_wm_minimized(p_window, true);
  2538. } break;
  2539. case WINDOW_MODE_EXCLUSIVE_FULLSCREEN:
  2540. case WINDOW_MODE_FULLSCREEN: {
  2541. wd.last_position_before_fs = wd.position;
  2542. if (window_get_flag(WINDOW_FLAG_ALWAYS_ON_TOP, p_window)) {
  2543. _set_wm_maximized(p_window, true);
  2544. }
  2545. wd.fullscreen = true;
  2546. if (p_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN) {
  2547. wd.exclusive_fullscreen = true;
  2548. _set_wm_fullscreen(p_window, true, true);
  2549. } else {
  2550. wd.exclusive_fullscreen = false;
  2551. _set_wm_fullscreen(p_window, true, false);
  2552. }
  2553. } break;
  2554. case WINDOW_MODE_MAXIMIZED: {
  2555. _set_wm_maximized(p_window, true);
  2556. } break;
  2557. }
  2558. }
  2559. DisplayServer::WindowMode DisplayServerX11::window_get_mode(WindowID p_window) const {
  2560. _THREAD_SAFE_METHOD_
  2561. ERR_FAIL_COND_V(!windows.has(p_window), WINDOW_MODE_WINDOWED);
  2562. const WindowData &wd = windows[p_window];
  2563. if (wd.fullscreen) { //if fullscreen, it's not in another mode
  2564. if (wd.exclusive_fullscreen) {
  2565. return WINDOW_MODE_EXCLUSIVE_FULLSCREEN;
  2566. } else {
  2567. return WINDOW_MODE_FULLSCREEN;
  2568. }
  2569. }
  2570. // Test maximized.
  2571. // Using EWMH -- Extended Window Manager Hints
  2572. if (_window_maximize_check(p_window, "_NET_WM_STATE")) {
  2573. return WINDOW_MODE_MAXIMIZED;
  2574. }
  2575. {
  2576. if (_window_minimize_check(p_window)) {
  2577. return WINDOW_MODE_MINIMIZED;
  2578. }
  2579. }
  2580. // All other discarded, return windowed.
  2581. return WINDOW_MODE_WINDOWED;
  2582. }
  2583. void DisplayServerX11::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) {
  2584. _THREAD_SAFE_METHOD_
  2585. ERR_FAIL_COND(!windows.has(p_window));
  2586. WindowData &wd = windows[p_window];
  2587. switch (p_flag) {
  2588. case WINDOW_FLAG_MAXIMIZE_DISABLED: {
  2589. wd.no_max_btn = p_enabled;
  2590. _update_actions_hints(p_window);
  2591. XFlush(x11_display);
  2592. } break;
  2593. case WINDOW_FLAG_MINIMIZE_DISABLED: {
  2594. wd.no_min_btn = p_enabled;
  2595. _update_actions_hints(p_window);
  2596. XFlush(x11_display);
  2597. } break;
  2598. case WINDOW_FLAG_RESIZE_DISABLED: {
  2599. if (p_enabled && wd.embed_parent) {
  2600. print_line("Embedded window resize can't be disabled.");
  2601. return;
  2602. }
  2603. wd.resize_disabled = p_enabled;
  2604. _update_size_hints(p_window);
  2605. _update_actions_hints(p_window);
  2606. XFlush(x11_display);
  2607. } break;
  2608. case WINDOW_FLAG_BORDERLESS: {
  2609. Hints hints;
  2610. Atom property;
  2611. hints.flags = 2;
  2612. hints.decorations = p_enabled ? 0 : 1;
  2613. property = XInternAtom(x11_display, "_MOTIF_WM_HINTS", True);
  2614. if (property != None) {
  2615. XChangeProperty(x11_display, wd.x11_window, property, property, 32, PropModeReplace, (unsigned char *)&hints, 5);
  2616. }
  2617. // Preserve window size
  2618. if (!wd.embed_parent) {
  2619. window_set_size(window_get_size(p_window), p_window);
  2620. }
  2621. wd.borderless = p_enabled;
  2622. _update_window_mouse_passthrough(p_window);
  2623. } break;
  2624. case WINDOW_FLAG_ALWAYS_ON_TOP: {
  2625. ERR_FAIL_COND_MSG(wd.transient_parent != INVALID_WINDOW_ID, "Can't make a window transient if the 'on top' flag is active.");
  2626. if (p_enabled && wd.embed_parent) {
  2627. print_line("Embedded window can't become on top.");
  2628. return;
  2629. }
  2630. if (p_enabled && wd.fullscreen) {
  2631. _set_wm_maximized(p_window, true);
  2632. }
  2633. Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
  2634. Atom wm_above = XInternAtom(x11_display, "_NET_WM_STATE_ABOVE", False);
  2635. XClientMessageEvent xev;
  2636. memset(&xev, 0, sizeof(xev));
  2637. xev.type = ClientMessage;
  2638. xev.window = wd.x11_window;
  2639. xev.message_type = wm_state;
  2640. xev.format = 32;
  2641. xev.data.l[0] = p_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
  2642. xev.data.l[1] = wm_above;
  2643. xev.data.l[3] = 1;
  2644. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&xev);
  2645. if (!p_enabled && !wd.fullscreen) {
  2646. _set_wm_maximized(p_window, false);
  2647. }
  2648. wd.on_top = p_enabled;
  2649. } break;
  2650. case WINDOW_FLAG_TRANSPARENT: {
  2651. wd.layered_window = p_enabled;
  2652. } break;
  2653. case WINDOW_FLAG_NO_FOCUS: {
  2654. wd.no_focus = p_enabled;
  2655. } break;
  2656. case WINDOW_FLAG_MOUSE_PASSTHROUGH: {
  2657. wd.mpass = p_enabled;
  2658. _update_window_mouse_passthrough(p_window);
  2659. } break;
  2660. case WINDOW_FLAG_POPUP: {
  2661. XWindowAttributes xwa;
  2662. XSync(x11_display, False);
  2663. XGetWindowAttributes(x11_display, wd.x11_window, &xwa);
  2664. ERR_FAIL_COND_MSG(p_window == MAIN_WINDOW_ID, "Main window can't be popup.");
  2665. ERR_FAIL_COND_MSG((xwa.map_state == IsViewable) && (wd.is_popup != p_enabled), "Popup flag can't changed while window is opened.");
  2666. if (p_enabled && wd.embed_parent) {
  2667. print_line("Embedded window can't be popup.");
  2668. return;
  2669. }
  2670. wd.is_popup = p_enabled;
  2671. } break;
  2672. default: {
  2673. }
  2674. }
  2675. }
  2676. bool DisplayServerX11::window_get_flag(WindowFlags p_flag, WindowID p_window) const {
  2677. _THREAD_SAFE_METHOD_
  2678. ERR_FAIL_COND_V(!windows.has(p_window), false);
  2679. const WindowData &wd = windows[p_window];
  2680. switch (p_flag) {
  2681. case WINDOW_FLAG_MAXIMIZE_DISABLED: {
  2682. return wd.no_max_btn;
  2683. } break;
  2684. case WINDOW_FLAG_MINIMIZE_DISABLED: {
  2685. return wd.no_min_btn;
  2686. } break;
  2687. case WINDOW_FLAG_RESIZE_DISABLED: {
  2688. return wd.resize_disabled;
  2689. } break;
  2690. case WINDOW_FLAG_BORDERLESS: {
  2691. bool borderless = wd.borderless;
  2692. Atom prop = XInternAtom(x11_display, "_MOTIF_WM_HINTS", True);
  2693. if (prop != None) {
  2694. Atom type;
  2695. int format;
  2696. unsigned long len;
  2697. unsigned long remaining;
  2698. unsigned char *data = nullptr;
  2699. if (XGetWindowProperty(x11_display, wd.x11_window, prop, 0, sizeof(Hints), False, AnyPropertyType, &type, &format, &len, &remaining, &data) == Success) {
  2700. if (data && (format == 32) && (len >= 5)) {
  2701. borderless = !(reinterpret_cast<Hints *>(data)->decorations);
  2702. }
  2703. if (data) {
  2704. XFree(data);
  2705. }
  2706. }
  2707. }
  2708. return borderless;
  2709. } break;
  2710. case WINDOW_FLAG_ALWAYS_ON_TOP: {
  2711. return wd.on_top;
  2712. } break;
  2713. case WINDOW_FLAG_TRANSPARENT: {
  2714. return wd.layered_window;
  2715. } break;
  2716. case WINDOW_FLAG_NO_FOCUS: {
  2717. return wd.no_focus;
  2718. } break;
  2719. case WINDOW_FLAG_MOUSE_PASSTHROUGH: {
  2720. return wd.mpass;
  2721. } break;
  2722. case WINDOW_FLAG_POPUP: {
  2723. return wd.is_popup;
  2724. } break;
  2725. default: {
  2726. }
  2727. }
  2728. return false;
  2729. }
  2730. void DisplayServerX11::window_request_attention(WindowID p_window) {
  2731. _THREAD_SAFE_METHOD_
  2732. ERR_FAIL_COND(!windows.has(p_window));
  2733. const WindowData &wd = windows[p_window];
  2734. // Using EWMH -- Extended Window Manager Hints
  2735. //
  2736. // Sets the _NET_WM_STATE_DEMANDS_ATTENTION atom for WM_STATE
  2737. // Will be unset by the window manager after user react on the request for attention
  2738. XEvent xev;
  2739. Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
  2740. Atom wm_attention = XInternAtom(x11_display, "_NET_WM_STATE_DEMANDS_ATTENTION", False);
  2741. memset(&xev, 0, sizeof(xev));
  2742. xev.type = ClientMessage;
  2743. xev.xclient.window = wd.x11_window;
  2744. xev.xclient.message_type = wm_state;
  2745. xev.xclient.format = 32;
  2746. xev.xclient.data.l[0] = _NET_WM_STATE_ADD;
  2747. xev.xclient.data.l[1] = wm_attention;
  2748. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  2749. XFlush(x11_display);
  2750. }
  2751. void DisplayServerX11::window_move_to_foreground(WindowID p_window) {
  2752. _THREAD_SAFE_METHOD_
  2753. ERR_FAIL_COND(!windows.has(p_window));
  2754. const WindowData &wd = windows[p_window];
  2755. XEvent xev;
  2756. Atom net_active_window = XInternAtom(x11_display, "_NET_ACTIVE_WINDOW", False);
  2757. memset(&xev, 0, sizeof(xev));
  2758. xev.type = ClientMessage;
  2759. xev.xclient.window = wd.x11_window;
  2760. xev.xclient.message_type = net_active_window;
  2761. xev.xclient.format = 32;
  2762. xev.xclient.data.l[0] = 1;
  2763. xev.xclient.data.l[1] = CurrentTime;
  2764. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  2765. XFlush(x11_display);
  2766. }
  2767. DisplayServerX11::WindowID DisplayServerX11::get_focused_window() const {
  2768. return last_focused_window;
  2769. }
  2770. bool DisplayServerX11::window_is_focused(WindowID p_window) const {
  2771. _THREAD_SAFE_METHOD_
  2772. ERR_FAIL_COND_V(!windows.has(p_window), false);
  2773. const WindowData &wd = windows[p_window];
  2774. return wd.focused;
  2775. }
  2776. bool DisplayServerX11::window_can_draw(WindowID p_window) const {
  2777. //this seems to be all that is provided by X11
  2778. return window_get_mode(p_window) != WINDOW_MODE_MINIMIZED;
  2779. }
  2780. bool DisplayServerX11::can_any_window_draw() const {
  2781. _THREAD_SAFE_METHOD_
  2782. for (const KeyValue<WindowID, WindowData> &E : windows) {
  2783. if (window_get_mode(E.key) != WINDOW_MODE_MINIMIZED) {
  2784. return true;
  2785. }
  2786. }
  2787. return false;
  2788. }
  2789. void DisplayServerX11::window_set_ime_active(const bool p_active, WindowID p_window) {
  2790. _THREAD_SAFE_METHOD_
  2791. ERR_FAIL_COND(!windows.has(p_window));
  2792. WindowData &wd = windows[p_window];
  2793. if (!wd.xic) {
  2794. return;
  2795. }
  2796. if (!wd.focused) {
  2797. wd.ime_active = false;
  2798. im_text = String();
  2799. im_selection = Vector2i();
  2800. return;
  2801. }
  2802. // Block events polling while changing input focus
  2803. // because it triggers some event polling internally.
  2804. if (p_active) {
  2805. MutexLock mutex_lock(events_mutex);
  2806. wd.ime_active = true;
  2807. XMapWindow(x11_display, wd.x11_xim_window);
  2808. XWindowAttributes xwa;
  2809. XSync(x11_display, False);
  2810. XGetWindowAttributes(x11_display, wd.x11_xim_window, &xwa);
  2811. if (xwa.map_state == IsViewable && _window_focus_check()) {
  2812. _set_input_focus(wd.x11_xim_window, RevertToParent);
  2813. }
  2814. XSetICFocus(wd.xic);
  2815. } else {
  2816. MutexLock mutex_lock(events_mutex);
  2817. XUnsetICFocus(wd.xic);
  2818. XUnmapWindow(x11_display, wd.x11_xim_window);
  2819. wd.ime_active = false;
  2820. im_text = String();
  2821. im_selection = Vector2i();
  2822. }
  2823. }
  2824. void DisplayServerX11::window_set_ime_position(const Point2i &p_pos, WindowID p_window) {
  2825. _THREAD_SAFE_METHOD_
  2826. ERR_FAIL_COND(!windows.has(p_window));
  2827. WindowData &wd = windows[p_window];
  2828. if (!wd.xic) {
  2829. return;
  2830. }
  2831. if (!wd.focused) {
  2832. return;
  2833. }
  2834. if (wd.ime_active) {
  2835. XWindowAttributes xwa;
  2836. XSync(x11_display, False);
  2837. XGetWindowAttributes(x11_display, wd.x11_xim_window, &xwa);
  2838. if (xwa.map_state == IsViewable) {
  2839. XMoveWindow(x11_display, wd.x11_xim_window, p_pos.x, p_pos.y);
  2840. }
  2841. }
  2842. }
  2843. int DisplayServerX11::accessibility_should_increase_contrast() const {
  2844. #ifdef DBUS_ENABLED
  2845. return portal_desktop->get_high_contrast();
  2846. #endif
  2847. return -1;
  2848. }
  2849. int DisplayServerX11::accessibility_screen_reader_active() const {
  2850. #ifdef DBUS_ENABLED
  2851. if (atspi_monitor->is_supported()) {
  2852. return atspi_monitor->is_active();
  2853. }
  2854. #endif
  2855. return -1;
  2856. }
  2857. Point2i DisplayServerX11::ime_get_selection() const {
  2858. return im_selection;
  2859. }
  2860. String DisplayServerX11::ime_get_text() const {
  2861. return im_text;
  2862. }
  2863. void DisplayServerX11::cursor_set_shape(CursorShape p_shape) {
  2864. _THREAD_SAFE_METHOD_
  2865. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  2866. if (p_shape == current_cursor) {
  2867. return;
  2868. }
  2869. if (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED) {
  2870. if (cursors[p_shape] != None) {
  2871. for (const KeyValue<WindowID, WindowData> &E : windows) {
  2872. XDefineCursor(x11_display, E.value.x11_window, cursors[p_shape]);
  2873. }
  2874. } else if (cursors[CURSOR_ARROW] != None) {
  2875. for (const KeyValue<WindowID, WindowData> &E : windows) {
  2876. XDefineCursor(x11_display, E.value.x11_window, cursors[CURSOR_ARROW]);
  2877. }
  2878. }
  2879. }
  2880. current_cursor = p_shape;
  2881. }
  2882. DisplayServerX11::CursorShape DisplayServerX11::cursor_get_shape() const {
  2883. return current_cursor;
  2884. }
  2885. void DisplayServerX11::cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  2886. _THREAD_SAFE_METHOD_
  2887. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  2888. if (p_cursor.is_valid()) {
  2889. HashMap<CursorShape, Vector<Variant>>::Iterator cursor_c = cursors_cache.find(p_shape);
  2890. if (cursor_c) {
  2891. if (cursor_c->value[0] == p_cursor && cursor_c->value[1] == p_hotspot) {
  2892. cursor_set_shape(p_shape);
  2893. return;
  2894. }
  2895. cursors_cache.erase(p_shape);
  2896. }
  2897. Ref<Image> image = _get_cursor_image_from_resource(p_cursor, p_hotspot);
  2898. ERR_FAIL_COND(image.is_null());
  2899. Vector2i texture_size = image->get_size();
  2900. // Create the cursor structure
  2901. XcursorImage *cursor_image = XcursorImageCreate(texture_size.width, texture_size.height);
  2902. XcursorUInt image_size = texture_size.width * texture_size.height;
  2903. XcursorDim size = sizeof(XcursorPixel) * image_size;
  2904. cursor_image->version = 1;
  2905. cursor_image->size = size;
  2906. cursor_image->xhot = p_hotspot.x;
  2907. cursor_image->yhot = p_hotspot.y;
  2908. // allocate memory to contain the whole file
  2909. cursor_image->pixels = (XcursorPixel *)memalloc(size);
  2910. for (XcursorPixel index = 0; index < image_size; index++) {
  2911. int row_index = floor(index / texture_size.width);
  2912. int column_index = index % int(texture_size.width);
  2913. *(cursor_image->pixels + index) = image->get_pixel(column_index, row_index).to_argb32();
  2914. }
  2915. ERR_FAIL_NULL(cursor_image->pixels);
  2916. // Save it for a further usage
  2917. cursors[p_shape] = XcursorImageLoadCursor(x11_display, cursor_image);
  2918. Vector<Variant> params;
  2919. params.push_back(p_cursor);
  2920. params.push_back(p_hotspot);
  2921. cursors_cache.insert(p_shape, params);
  2922. if (p_shape == current_cursor) {
  2923. if (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED) {
  2924. for (const KeyValue<WindowID, WindowData> &E : windows) {
  2925. XDefineCursor(x11_display, E.value.x11_window, cursors[p_shape]);
  2926. }
  2927. }
  2928. }
  2929. memfree(cursor_image->pixels);
  2930. XcursorImageDestroy(cursor_image);
  2931. } else {
  2932. // Reset to default system cursor
  2933. if (cursor_img[p_shape]) {
  2934. cursors[p_shape] = XcursorImageLoadCursor(x11_display, cursor_img[p_shape]);
  2935. }
  2936. cursors_cache.erase(p_shape);
  2937. CursorShape c = current_cursor;
  2938. current_cursor = CURSOR_MAX;
  2939. cursor_set_shape(c);
  2940. }
  2941. }
  2942. bool DisplayServerX11::get_swap_cancel_ok() {
  2943. return swap_cancel_ok;
  2944. }
  2945. int DisplayServerX11::keyboard_get_layout_count() const {
  2946. int _group_count = 0;
  2947. XkbDescRec *kbd = XkbAllocKeyboard();
  2948. if (kbd) {
  2949. kbd->dpy = x11_display;
  2950. XkbGetControls(x11_display, XkbAllControlsMask, kbd);
  2951. XkbGetNames(x11_display, XkbSymbolsNameMask, kbd);
  2952. const Atom *groups = kbd->names->groups;
  2953. if (kbd->ctrls != nullptr) {
  2954. _group_count = kbd->ctrls->num_groups;
  2955. } else {
  2956. while (_group_count < XkbNumKbdGroups && groups[_group_count] != None) {
  2957. _group_count++;
  2958. }
  2959. }
  2960. XkbFreeKeyboard(kbd, 0, true);
  2961. }
  2962. return _group_count;
  2963. }
  2964. int DisplayServerX11::keyboard_get_current_layout() const {
  2965. XkbStateRec state;
  2966. XkbGetState(x11_display, XkbUseCoreKbd, &state);
  2967. return state.group;
  2968. }
  2969. void DisplayServerX11::keyboard_set_current_layout(int p_index) {
  2970. ERR_FAIL_INDEX(p_index, keyboard_get_layout_count());
  2971. XkbLockGroup(x11_display, XkbUseCoreKbd, p_index);
  2972. }
  2973. String DisplayServerX11::keyboard_get_layout_language(int p_index) const {
  2974. String ret;
  2975. XkbDescRec *kbd = XkbAllocKeyboard();
  2976. if (kbd) {
  2977. kbd->dpy = x11_display;
  2978. XkbGetControls(x11_display, XkbAllControlsMask, kbd);
  2979. XkbGetNames(x11_display, XkbSymbolsNameMask, kbd);
  2980. XkbGetNames(x11_display, XkbGroupNamesMask, kbd);
  2981. int _group_count = 0;
  2982. const Atom *groups = kbd->names->groups;
  2983. if (kbd->ctrls != nullptr) {
  2984. _group_count = kbd->ctrls->num_groups;
  2985. } else {
  2986. while (_group_count < XkbNumKbdGroups && groups[_group_count] != None) {
  2987. _group_count++;
  2988. }
  2989. }
  2990. Atom names = kbd->names->symbols;
  2991. if (names != None) {
  2992. Vector<String> info = get_atom_name(x11_display, names).split("+");
  2993. if (p_index >= 0 && p_index < _group_count) {
  2994. if (p_index + 1 < info.size()) {
  2995. ret = info[p_index + 1]; // Skip "pc" at the start and "inet"/"group" at the end of symbols.
  2996. } else {
  2997. ret = "en"; // No symbol for layout fallback to "en".
  2998. }
  2999. } else {
  3000. ERR_PRINT("Index " + itos(p_index) + "is out of bounds (" + itos(_group_count) + ").");
  3001. }
  3002. }
  3003. XkbFreeKeyboard(kbd, 0, true);
  3004. }
  3005. return ret.substr(0, 2);
  3006. }
  3007. String DisplayServerX11::keyboard_get_layout_name(int p_index) const {
  3008. String ret;
  3009. XkbDescRec *kbd = XkbAllocKeyboard();
  3010. if (kbd) {
  3011. kbd->dpy = x11_display;
  3012. XkbGetControls(x11_display, XkbAllControlsMask, kbd);
  3013. XkbGetNames(x11_display, XkbSymbolsNameMask, kbd);
  3014. XkbGetNames(x11_display, XkbGroupNamesMask, kbd);
  3015. int _group_count = 0;
  3016. const Atom *groups = kbd->names->groups;
  3017. if (kbd->ctrls != nullptr) {
  3018. _group_count = kbd->ctrls->num_groups;
  3019. } else {
  3020. while (_group_count < XkbNumKbdGroups && groups[_group_count] != None) {
  3021. _group_count++;
  3022. }
  3023. }
  3024. if (p_index >= 0 && p_index < _group_count) {
  3025. ret = get_atom_name(x11_display, groups[p_index]);
  3026. } else {
  3027. ERR_PRINT("Index " + itos(p_index) + "is out of bounds (" + itos(_group_count) + ").");
  3028. }
  3029. XkbFreeKeyboard(kbd, 0, true);
  3030. }
  3031. return ret;
  3032. }
  3033. Key DisplayServerX11::keyboard_get_keycode_from_physical(Key p_keycode) const {
  3034. Key modifiers = p_keycode & KeyModifierMask::MODIFIER_MASK;
  3035. Key keycode_no_mod = p_keycode & KeyModifierMask::CODE_MASK;
  3036. unsigned int xkeycode = KeyMappingX11::get_xlibcode(keycode_no_mod);
  3037. KeySym xkeysym = XkbKeycodeToKeysym(x11_display, xkeycode, keyboard_get_current_layout(), 0);
  3038. if (is_ascii_lower_case(xkeysym)) {
  3039. xkeysym -= ('a' - 'A');
  3040. }
  3041. Key key = KeyMappingX11::get_keycode(xkeysym);
  3042. // If not found, fallback to QWERTY.
  3043. // This should match the behavior of the event pump
  3044. if (key == Key::NONE) {
  3045. return p_keycode;
  3046. }
  3047. return (Key)(key | modifiers);
  3048. }
  3049. Key DisplayServerX11::keyboard_get_label_from_physical(Key p_keycode) const {
  3050. Key modifiers = p_keycode & KeyModifierMask::MODIFIER_MASK;
  3051. Key keycode_no_mod = p_keycode & KeyModifierMask::CODE_MASK;
  3052. unsigned int xkeycode = KeyMappingX11::get_xlibcode(keycode_no_mod);
  3053. KeySym xkeysym = XkbKeycodeToKeysym(x11_display, xkeycode, keyboard_get_current_layout(), 0);
  3054. if (is_ascii_lower_case(xkeysym)) {
  3055. xkeysym -= ('a' - 'A');
  3056. }
  3057. Key key = KeyMappingX11::get_keycode(xkeysym);
  3058. #ifdef XKB_ENABLED
  3059. if (xkb_loaded_v08p) {
  3060. String keysym = String::chr(xkb_keysym_to_utf32(xkb_keysym_to_upper(xkeysym)));
  3061. key = fix_key_label(keysym[0], KeyMappingX11::get_keycode(xkeysym));
  3062. }
  3063. #endif
  3064. // If not found, fallback to QWERTY.
  3065. // This should match the behavior of the event pump
  3066. if (key == Key::NONE) {
  3067. return p_keycode;
  3068. }
  3069. return (Key)(key | modifiers);
  3070. }
  3071. bool DisplayServerX11::color_picker(const Callable &p_callback) {
  3072. WindowID window_id = last_focused_window;
  3073. if (!windows.has(window_id)) {
  3074. window_id = MAIN_WINDOW_ID;
  3075. }
  3076. String xid = vformat("x11:%x", (uint64_t)windows[window_id].x11_window);
  3077. return portal_desktop->color_picker(xid, p_callback);
  3078. }
  3079. DisplayServerX11::Property DisplayServerX11::_read_property(Display *p_display, Window p_window, Atom p_property) {
  3080. Atom actual_type = None;
  3081. int actual_format = 0;
  3082. unsigned long nitems = 0;
  3083. unsigned long bytes_after = 0;
  3084. unsigned char *ret = nullptr;
  3085. // Keep trying to read the property until there are no bytes unread.
  3086. if (p_property != None) {
  3087. int read_bytes = 1024;
  3088. do {
  3089. if (ret != nullptr) {
  3090. XFree(ret);
  3091. }
  3092. XGetWindowProperty(p_display, p_window, p_property, 0, read_bytes, False, AnyPropertyType,
  3093. &actual_type, &actual_format, &nitems, &bytes_after,
  3094. &ret);
  3095. read_bytes *= 2;
  3096. } while (bytes_after != 0);
  3097. }
  3098. Property p = { ret, actual_format, (int)nitems, actual_type };
  3099. return p;
  3100. }
  3101. static Atom pick_target_from_list(Display *p_display, const Atom *p_list, int p_count) {
  3102. static const char *target_type = "text/uri-list";
  3103. for (int i = 0; i < p_count; i++) {
  3104. Atom atom = p_list[i];
  3105. if (atom != None && get_atom_name(p_display, atom) == target_type) {
  3106. return atom;
  3107. }
  3108. }
  3109. return None;
  3110. }
  3111. static Atom pick_target_from_atoms(Display *p_disp, Atom p_t1, Atom p_t2, Atom p_t3) {
  3112. static const char *target_type = "text/uri-list";
  3113. if (p_t1 != None && get_atom_name(p_disp, p_t1) == target_type) {
  3114. return p_t1;
  3115. }
  3116. if (p_t2 != None && get_atom_name(p_disp, p_t2) == target_type) {
  3117. return p_t2;
  3118. }
  3119. if (p_t3 != None && get_atom_name(p_disp, p_t3) == target_type) {
  3120. return p_t3;
  3121. }
  3122. return None;
  3123. }
  3124. void DisplayServerX11::_get_key_modifier_state(unsigned int p_x11_state, Ref<InputEventWithModifiers> state) {
  3125. state->set_shift_pressed((p_x11_state & ShiftMask));
  3126. state->set_ctrl_pressed((p_x11_state & ControlMask));
  3127. state->set_alt_pressed((p_x11_state & Mod1Mask /*|| p_x11_state&Mod5Mask*/)); //altgr should not count as alt
  3128. state->set_meta_pressed((p_x11_state & Mod4Mask));
  3129. }
  3130. void DisplayServerX11::_handle_key_event(WindowID p_window, XKeyEvent *p_event, LocalVector<XEvent> &p_events, uint32_t &p_event_index, bool p_echo) {
  3131. WindowData &wd = windows[p_window];
  3132. // X11 functions don't know what const is
  3133. XKeyEvent *xkeyevent = p_event;
  3134. if (wd.ime_in_progress) {
  3135. return;
  3136. }
  3137. if (wd.ime_suppress_next_keyup) {
  3138. wd.ime_suppress_next_keyup = false;
  3139. if (xkeyevent->type != KeyPress) {
  3140. return;
  3141. }
  3142. }
  3143. // This code was pretty difficult to write.
  3144. // The docs stink and every toolkit seems to
  3145. // do it in a different way.
  3146. /* Phase 1, obtain a proper keysym */
  3147. // This was also very difficult to figure out.
  3148. // You'd expect you could just use Keysym provided by
  3149. // XKeycodeToKeysym to obtain internationalized
  3150. // input.. WRONG!!
  3151. // you must use XLookupString (???) which not only wastes
  3152. // cycles generating an unnecessary string, but also
  3153. // still works in half the cases. (won't handle deadkeys)
  3154. // For more complex input methods (deadkeys and more advanced)
  3155. // you have to use XmbLookupString (??).
  3156. // So then you have to choose which of both results
  3157. // you want to keep.
  3158. // This is a real bizarreness and cpu waster.
  3159. KeySym keysym_keycode = 0; // keysym used to find a keycode
  3160. KeySym keysym_unicode = 0; // keysym used to find unicode
  3161. // XLookupString returns keysyms usable as nice keycodes.
  3162. char str[256] = {};
  3163. XKeyEvent xkeyevent_no_mod = *xkeyevent;
  3164. xkeyevent_no_mod.state &= 0xFF00;
  3165. XLookupString(xkeyevent, str, 255, &keysym_unicode, nullptr);
  3166. XLookupString(&xkeyevent_no_mod, nullptr, 0, &keysym_keycode, nullptr);
  3167. String keysym;
  3168. #ifdef XKB_ENABLED
  3169. if (xkb_loaded_v08p) {
  3170. KeySym keysym_unicode_nm = 0; // keysym used to find unicode
  3171. XLookupString(&xkeyevent_no_mod, nullptr, 0, &keysym_unicode_nm, nullptr);
  3172. keysym = String::chr(xkb_keysym_to_utf32(xkb_keysym_to_upper(keysym_unicode_nm)));
  3173. }
  3174. #endif
  3175. // Meanwhile, XLookupString returns keysyms useful for unicode.
  3176. if (!xmbstring) {
  3177. // keep a temporary buffer for the string
  3178. xmbstring = (char *)memalloc(sizeof(char) * 8);
  3179. xmblen = 8;
  3180. }
  3181. if (xkeyevent->type == KeyPress && wd.xic) {
  3182. Status status;
  3183. #ifdef X_HAVE_UTF8_STRING
  3184. int utf8len = 8;
  3185. char *utf8string = (char *)memalloc(sizeof(char) * utf8len);
  3186. int utf8bytes = Xutf8LookupString(wd.xic, xkeyevent, utf8string,
  3187. utf8len - 1, &keysym_unicode, &status);
  3188. if (status == XBufferOverflow) {
  3189. utf8len = utf8bytes + 1;
  3190. utf8string = (char *)memrealloc(utf8string, utf8len);
  3191. utf8bytes = Xutf8LookupString(wd.xic, xkeyevent, utf8string,
  3192. utf8len - 1, &keysym_unicode, &status);
  3193. }
  3194. utf8string[utf8bytes] = '\0';
  3195. if (status == XLookupChars) {
  3196. bool keypress = xkeyevent->type == KeyPress;
  3197. Key keycode = Key::NONE;
  3198. if (KeyMappingX11::is_sym_numpad(keysym_unicode)) {
  3199. // Special case for numpad keys.
  3200. keycode = KeyMappingX11::get_keycode(keysym_unicode);
  3201. }
  3202. if (keycode == Key::NONE) {
  3203. keycode = KeyMappingX11::get_keycode(keysym_keycode);
  3204. }
  3205. Key physical_keycode = KeyMappingX11::get_scancode(xkeyevent->keycode);
  3206. if (keycode >= Key::A + 32 && keycode <= Key::Z + 32) {
  3207. keycode -= 'a' - 'A';
  3208. }
  3209. String tmp = String::utf8(utf8string, utf8bytes);
  3210. for (int i = 0; i < tmp.length(); i++) {
  3211. Ref<InputEventKey> k;
  3212. k.instantiate();
  3213. if (physical_keycode == Key::NONE && keycode == Key::NONE && tmp[i] == 0) {
  3214. continue;
  3215. }
  3216. if (keycode == Key::NONE) {
  3217. keycode = (Key)physical_keycode;
  3218. }
  3219. _get_key_modifier_state(xkeyevent->state, k);
  3220. k->set_window_id(p_window);
  3221. k->set_pressed(keypress);
  3222. k->set_keycode(keycode);
  3223. k->set_physical_keycode(physical_keycode);
  3224. if (!keysym.is_empty()) {
  3225. k->set_key_label(fix_key_label(keysym[0], keycode));
  3226. } else {
  3227. k->set_key_label(keycode);
  3228. }
  3229. if (keypress) {
  3230. k->set_unicode(fix_unicode(tmp[i]));
  3231. }
  3232. k->set_echo(false);
  3233. if (k->get_keycode() == Key::BACKTAB) {
  3234. //make it consistent across platforms.
  3235. k->set_keycode(Key::TAB);
  3236. k->set_physical_keycode(Key::TAB);
  3237. k->set_shift_pressed(true);
  3238. }
  3239. Input::get_singleton()->parse_input_event(k);
  3240. }
  3241. memfree(utf8string);
  3242. return;
  3243. }
  3244. memfree(utf8string);
  3245. #else
  3246. do {
  3247. int mnbytes = XmbLookupString(xic, xkeyevent, xmbstring, xmblen - 1, &keysym_unicode, &status);
  3248. xmbstring[mnbytes] = '\0';
  3249. if (status == XBufferOverflow) {
  3250. xmblen = mnbytes + 1;
  3251. xmbstring = (char *)memrealloc(xmbstring, xmblen);
  3252. }
  3253. } while (status == XBufferOverflow);
  3254. #endif
  3255. #ifdef XKB_ENABLED
  3256. } else if (xkeyevent->type == KeyPress && wd.xkb_state && xkb_loaded_v05p) {
  3257. xkb_compose_feed_result res = xkb_compose_state_feed(wd.xkb_state, keysym_unicode);
  3258. if (res == XKB_COMPOSE_FEED_ACCEPTED) {
  3259. if (xkb_compose_state_get_status(wd.xkb_state) == XKB_COMPOSE_COMPOSED) {
  3260. bool keypress = xkeyevent->type == KeyPress;
  3261. Key physical_keycode = KeyMappingX11::get_scancode(xkeyevent->keycode);
  3262. KeyLocation key_location = KeyMappingX11::get_location(xkeyevent->keycode);
  3263. Key keycode = Key::NONE;
  3264. if (KeyMappingX11::is_sym_numpad(keysym_unicode)) {
  3265. // Special case for numpad keys.
  3266. keycode = KeyMappingX11::get_keycode(keysym_unicode);
  3267. }
  3268. if (keycode == Key::NONE) {
  3269. keycode = KeyMappingX11::get_keycode(keysym_keycode);
  3270. }
  3271. if (keycode >= Key::A + 32 && keycode <= Key::Z + 32) {
  3272. keycode -= 'a' - 'A';
  3273. }
  3274. char str_xkb[256] = {};
  3275. int str_xkb_size = xkb_compose_state_get_utf8(wd.xkb_state, str_xkb, 255);
  3276. String tmp = String::utf8(str_xkb, str_xkb_size);
  3277. for (int i = 0; i < tmp.length(); i++) {
  3278. Ref<InputEventKey> k;
  3279. k.instantiate();
  3280. if (physical_keycode == Key::NONE && keycode == Key::NONE && tmp[i] == 0) {
  3281. continue;
  3282. }
  3283. if (keycode == Key::NONE) {
  3284. keycode = (Key)physical_keycode;
  3285. }
  3286. _get_key_modifier_state(xkeyevent->state, k);
  3287. k->set_window_id(p_window);
  3288. k->set_pressed(keypress);
  3289. k->set_keycode(keycode);
  3290. k->set_physical_keycode(physical_keycode);
  3291. if (!keysym.is_empty()) {
  3292. k->set_key_label(fix_key_label(keysym[0], keycode));
  3293. } else {
  3294. k->set_key_label(keycode);
  3295. }
  3296. if (keypress) {
  3297. k->set_unicode(fix_unicode(tmp[i]));
  3298. }
  3299. k->set_location(key_location);
  3300. k->set_echo(false);
  3301. if (k->get_keycode() == Key::BACKTAB) {
  3302. //make it consistent across platforms.
  3303. k->set_keycode(Key::TAB);
  3304. k->set_physical_keycode(Key::TAB);
  3305. k->set_shift_pressed(true);
  3306. }
  3307. Input::get_singleton()->parse_input_event(k);
  3308. }
  3309. return;
  3310. }
  3311. }
  3312. #endif
  3313. }
  3314. /* Phase 2, obtain a Godot keycode from the keysym */
  3315. // KeyMappingX11 just translated the X11 keysym to a PIGUI
  3316. // keysym, so it works in all platforms the same.
  3317. Key keycode = Key::NONE;
  3318. if (KeyMappingX11::is_sym_numpad(keysym_unicode) || KeyMappingX11::is_sym_numpad(keysym_keycode)) {
  3319. // Special case for numpad keys.
  3320. keycode = KeyMappingX11::get_keycode(keysym_unicode);
  3321. }
  3322. if (keycode == Key::NONE) {
  3323. keycode = KeyMappingX11::get_keycode(keysym_keycode);
  3324. }
  3325. Key physical_keycode = KeyMappingX11::get_scancode(xkeyevent->keycode);
  3326. KeyLocation key_location = KeyMappingX11::get_location(xkeyevent->keycode);
  3327. /* Phase 3, obtain a unicode character from the keysym */
  3328. // KeyMappingX11 also translates keysym to unicode.
  3329. // It does a binary search on a table to translate
  3330. // most properly.
  3331. char32_t unicode = keysym_unicode > 0 ? KeyMappingX11::get_unicode_from_keysym(keysym_unicode) : 0;
  3332. /* Phase 4, determine if event must be filtered */
  3333. // This seems to be a side-effect of using XIM.
  3334. // XFilterEvent looks like a core X11 function,
  3335. // but it's actually just used to see if we must
  3336. // ignore a deadkey, or events XIM determines
  3337. // must not reach the actual gui.
  3338. // Guess it was a design problem of the extension
  3339. bool keypress = xkeyevent->type == KeyPress;
  3340. if (physical_keycode == Key::NONE && keycode == Key::NONE && unicode == 0) {
  3341. return;
  3342. }
  3343. if (keycode == Key::NONE) {
  3344. keycode = (Key)physical_keycode;
  3345. }
  3346. /* Phase 5, determine modifier mask */
  3347. // No problems here, except I had no way to
  3348. // know Mod1 was ALT and Mod4 was META (applekey/winkey)
  3349. // just tried Mods until i found them.
  3350. //print_verbose("mod1: "+itos(xkeyevent->state&Mod1Mask)+" mod 5: "+itos(xkeyevent->state&Mod5Mask));
  3351. Ref<InputEventKey> k;
  3352. k.instantiate();
  3353. k->set_window_id(p_window);
  3354. _get_key_modifier_state(xkeyevent->state, k);
  3355. /* Phase 6, determine echo character */
  3356. // Echo characters in X11 are a keyrelease and a keypress
  3357. // one after the other with the (almot) same timestamp.
  3358. // To detect them, i compare to the next event in list and
  3359. // check that their difference in time is below a threshold.
  3360. if (xkeyevent->type != KeyPress) {
  3361. p_echo = false;
  3362. // make sure there are events pending,
  3363. // so this call won't block.
  3364. if (p_event_index + 1 < p_events.size()) {
  3365. XEvent &peek_event = p_events[p_event_index + 1];
  3366. // I'm using a threshold of 5 msecs,
  3367. // since sometimes there seems to be a little
  3368. // jitter. I'm still not convinced that all this approach
  3369. // is correct, but the xorg developers are
  3370. // not very helpful today.
  3371. #define ABSDIFF(x, y) (((x) < (y)) ? ((y) - (x)) : ((x) - (y)))
  3372. ::Time threshold = ABSDIFF(peek_event.xkey.time, xkeyevent->time);
  3373. #undef ABSDIFF
  3374. if (peek_event.type == KeyPress && threshold < 5) {
  3375. KeySym rk;
  3376. XLookupString((XKeyEvent *)&peek_event, str, 256, &rk, nullptr);
  3377. if (rk == keysym_keycode) {
  3378. // Consume to next event.
  3379. ++p_event_index;
  3380. _handle_key_event(p_window, (XKeyEvent *)&peek_event, p_events, p_event_index, true);
  3381. return; //ignore current, echo next
  3382. }
  3383. }
  3384. // use the time from peek_event so it always works
  3385. }
  3386. // save the time to check for echo when keypress happens
  3387. }
  3388. /* Phase 7, send event to Window */
  3389. k->set_pressed(keypress);
  3390. if (keycode >= Key::A + 32 && keycode <= Key::Z + 32) {
  3391. keycode -= int('a' - 'A');
  3392. }
  3393. k->set_keycode(keycode);
  3394. k->set_physical_keycode((Key)physical_keycode);
  3395. if (!keysym.is_empty()) {
  3396. k->set_key_label(fix_key_label(keysym[0], keycode));
  3397. } else {
  3398. k->set_key_label(keycode);
  3399. }
  3400. if (keypress) {
  3401. k->set_unicode(fix_unicode(unicode));
  3402. }
  3403. k->set_location(key_location);
  3404. k->set_echo(p_echo);
  3405. if (k->get_keycode() == Key::BACKTAB) {
  3406. //make it consistent across platforms.
  3407. k->set_keycode(Key::TAB);
  3408. k->set_physical_keycode(Key::TAB);
  3409. k->set_shift_pressed(true);
  3410. }
  3411. //don't set mod state if modifier keys are released by themselves
  3412. //else event.is_action() will not work correctly here
  3413. if (!k->is_pressed()) {
  3414. if (k->get_keycode() == Key::SHIFT) {
  3415. k->set_shift_pressed(false);
  3416. } else if (k->get_keycode() == Key::CTRL) {
  3417. k->set_ctrl_pressed(false);
  3418. } else if (k->get_keycode() == Key::ALT) {
  3419. k->set_alt_pressed(false);
  3420. } else if (k->get_keycode() == Key::META) {
  3421. k->set_meta_pressed(false);
  3422. }
  3423. }
  3424. bool last_is_pressed = Input::get_singleton()->is_key_pressed(k->get_keycode());
  3425. if (k->is_pressed()) {
  3426. if (last_is_pressed) {
  3427. k->set_echo(true);
  3428. }
  3429. }
  3430. Input::get_singleton()->parse_input_event(k);
  3431. }
  3432. Atom DisplayServerX11::_process_selection_request_target(Atom p_target, Window p_requestor, Atom p_property, Atom p_selection) const {
  3433. if (p_target == XInternAtom(x11_display, "TARGETS", 0)) {
  3434. // Request to list all supported targets.
  3435. Atom data[9];
  3436. data[0] = XInternAtom(x11_display, "TARGETS", 0);
  3437. data[1] = XInternAtom(x11_display, "SAVE_TARGETS", 0);
  3438. data[2] = XInternAtom(x11_display, "MULTIPLE", 0);
  3439. data[3] = XInternAtom(x11_display, "UTF8_STRING", 0);
  3440. data[4] = XInternAtom(x11_display, "COMPOUND_TEXT", 0);
  3441. data[5] = XInternAtom(x11_display, "TEXT", 0);
  3442. data[6] = XA_STRING;
  3443. data[7] = XInternAtom(x11_display, "text/plain;charset=utf-8", 0);
  3444. data[8] = XInternAtom(x11_display, "text/plain", 0);
  3445. XChangeProperty(x11_display,
  3446. p_requestor,
  3447. p_property,
  3448. XA_ATOM,
  3449. 32,
  3450. PropModeReplace,
  3451. (unsigned char *)&data,
  3452. std::size(data));
  3453. return p_property;
  3454. } else if (p_target == XInternAtom(x11_display, "SAVE_TARGETS", 0)) {
  3455. // Request to check if SAVE_TARGETS is supported, nothing special to do.
  3456. XChangeProperty(x11_display,
  3457. p_requestor,
  3458. p_property,
  3459. XInternAtom(x11_display, "NULL", False),
  3460. 32,
  3461. PropModeReplace,
  3462. nullptr,
  3463. 0);
  3464. return p_property;
  3465. } else if (p_target == XInternAtom(x11_display, "UTF8_STRING", 0) ||
  3466. p_target == XInternAtom(x11_display, "COMPOUND_TEXT", 0) ||
  3467. p_target == XInternAtom(x11_display, "TEXT", 0) ||
  3468. p_target == XA_STRING ||
  3469. p_target == XInternAtom(x11_display, "text/plain;charset=utf-8", 0) ||
  3470. p_target == XInternAtom(x11_display, "text/plain", 0)) {
  3471. // Directly using internal clipboard because we know our window
  3472. // is the owner during a selection request.
  3473. CharString clip;
  3474. static const char *target_type = "PRIMARY";
  3475. if (p_selection != None && get_atom_name(x11_display, p_selection) == target_type) {
  3476. clip = internal_clipboard_primary.utf8();
  3477. } else {
  3478. clip = internal_clipboard.utf8();
  3479. }
  3480. XChangeProperty(x11_display,
  3481. p_requestor,
  3482. p_property,
  3483. p_target,
  3484. 8,
  3485. PropModeReplace,
  3486. (unsigned char *)clip.get_data(),
  3487. clip.length());
  3488. return p_property;
  3489. } else {
  3490. char *target_name = XGetAtomName(x11_display, p_target);
  3491. print_verbose(vformat("Target '%s' not supported.", target_name));
  3492. if (target_name) {
  3493. XFree(target_name);
  3494. }
  3495. return None;
  3496. }
  3497. }
  3498. void DisplayServerX11::_handle_selection_request_event(XSelectionRequestEvent *p_event) const {
  3499. XEvent respond;
  3500. if (p_event->target == XInternAtom(x11_display, "MULTIPLE", 0)) {
  3501. // Request for multiple target conversions at once.
  3502. Atom atom_pair = XInternAtom(x11_display, "ATOM_PAIR", False);
  3503. respond.xselection.property = None;
  3504. Atom type;
  3505. int format;
  3506. unsigned long len;
  3507. unsigned long remaining;
  3508. unsigned char *data = nullptr;
  3509. if (XGetWindowProperty(x11_display, p_event->requestor, p_event->property, 0, LONG_MAX, False, atom_pair, &type, &format, &len, &remaining, &data) == Success) {
  3510. if ((len >= 2) && data) {
  3511. Atom *targets = (Atom *)data;
  3512. for (uint64_t i = 0; i < len; i += 2) {
  3513. Atom target = targets[i];
  3514. Atom &property = targets[i + 1];
  3515. property = _process_selection_request_target(target, p_event->requestor, property, p_event->selection);
  3516. }
  3517. XChangeProperty(x11_display,
  3518. p_event->requestor,
  3519. p_event->property,
  3520. atom_pair,
  3521. 32,
  3522. PropModeReplace,
  3523. (unsigned char *)targets,
  3524. len);
  3525. respond.xselection.property = p_event->property;
  3526. }
  3527. XFree(data);
  3528. }
  3529. } else {
  3530. // Request for target conversion.
  3531. respond.xselection.property = _process_selection_request_target(p_event->target, p_event->requestor, p_event->property, p_event->selection);
  3532. }
  3533. respond.xselection.type = SelectionNotify;
  3534. respond.xselection.display = p_event->display;
  3535. respond.xselection.requestor = p_event->requestor;
  3536. respond.xselection.selection = p_event->selection;
  3537. respond.xselection.target = p_event->target;
  3538. respond.xselection.time = p_event->time;
  3539. XSendEvent(x11_display, p_event->requestor, True, NoEventMask, &respond);
  3540. XFlush(x11_display);
  3541. }
  3542. int DisplayServerX11::_xim_preedit_start_callback(::XIM xim, ::XPointer client_data,
  3543. ::XPointer call_data) {
  3544. DisplayServerX11 *ds = reinterpret_cast<DisplayServerX11 *>(client_data);
  3545. WindowID window_id = ds->_get_focused_window_or_popup();
  3546. WindowData &wd = ds->windows[window_id];
  3547. if (wd.ime_active) {
  3548. wd.ime_in_progress = true;
  3549. }
  3550. return -1; // Allow preedit strings of any length (no limit).
  3551. }
  3552. void DisplayServerX11::_xim_preedit_done_callback(::XIM xim, ::XPointer client_data,
  3553. ::XPointer call_data) {
  3554. DisplayServerX11 *ds = reinterpret_cast<DisplayServerX11 *>(client_data);
  3555. WindowID window_id = ds->_get_focused_window_or_popup();
  3556. WindowData &wd = ds->windows[window_id];
  3557. if (wd.ime_active) {
  3558. wd.ime_in_progress = false;
  3559. wd.ime_suppress_next_keyup = true;
  3560. }
  3561. }
  3562. void DisplayServerX11::_xim_preedit_draw_callback(::XIM xim, ::XPointer client_data,
  3563. ::XIMPreeditDrawCallbackStruct *call_data) {
  3564. DisplayServerX11 *ds = reinterpret_cast<DisplayServerX11 *>(client_data);
  3565. WindowID window_id = ds->_get_focused_window_or_popup();
  3566. WindowData &wd = ds->windows[window_id];
  3567. XIMText *xim_text = call_data->text;
  3568. if (wd.ime_active) {
  3569. if (xim_text != nullptr) {
  3570. String changed_text;
  3571. if (xim_text->encoding_is_wchar) {
  3572. changed_text = String(xim_text->string.wide_char);
  3573. } else {
  3574. changed_text.append_utf8(xim_text->string.multi_byte);
  3575. }
  3576. if (call_data->chg_length < 0) {
  3577. ds->im_text = ds->im_text.substr(0, call_data->chg_first) + changed_text;
  3578. } else {
  3579. ds->im_text = ds->im_text.substr(0, call_data->chg_first) + changed_text + ds->im_text.substr(call_data->chg_length);
  3580. }
  3581. // Find the start and end of the selection.
  3582. int start = 0, count = 0;
  3583. for (int i = 0; i < xim_text->length; i++) {
  3584. if (xim_text->feedback[i] & XIMReverse) {
  3585. if (count == 0) {
  3586. start = i;
  3587. count = 1;
  3588. } else {
  3589. count++;
  3590. }
  3591. }
  3592. }
  3593. if (count > 0) {
  3594. ds->im_selection = Point2i(start + call_data->chg_first, count);
  3595. } else {
  3596. ds->im_selection = Point2i(call_data->caret, 0);
  3597. }
  3598. } else {
  3599. ds->im_text = String();
  3600. ds->im_selection = Point2i();
  3601. }
  3602. callable_mp((Object *)OS_Unix::get_singleton()->get_main_loop(), &Object::notification).call_deferred(MainLoop::NOTIFICATION_OS_IME_UPDATE, false);
  3603. }
  3604. }
  3605. void DisplayServerX11::_xim_preedit_caret_callback(::XIM xim, ::XPointer client_data,
  3606. ::XIMPreeditCaretCallbackStruct *call_data) {
  3607. }
  3608. void DisplayServerX11::_xim_destroy_callback(::XIM im, ::XPointer client_data,
  3609. ::XPointer call_data) {
  3610. WARN_PRINT("Input method stopped");
  3611. DisplayServerX11 *ds = reinterpret_cast<DisplayServerX11 *>(client_data);
  3612. ds->xim = nullptr;
  3613. for (KeyValue<WindowID, WindowData> &E : ds->windows) {
  3614. E.value.xic = nullptr;
  3615. }
  3616. }
  3617. void DisplayServerX11::_window_changed(XEvent *event) {
  3618. WindowID window_id = MAIN_WINDOW_ID;
  3619. // Assign the event to the relevant window
  3620. for (const KeyValue<WindowID, WindowData> &E : windows) {
  3621. if (event->xany.window == E.value.x11_window) {
  3622. window_id = E.key;
  3623. break;
  3624. }
  3625. }
  3626. Rect2i new_rect;
  3627. WindowData &wd = windows[window_id];
  3628. if (wd.x11_window != event->xany.window) { // Check if the correct window, in case it was not main window or anything else
  3629. return;
  3630. }
  3631. // Query display server about a possible new window state.
  3632. wd.fullscreen = _window_fullscreen_check(window_id);
  3633. wd.maximized = _window_maximize_check(window_id, "_NET_WM_STATE") && !wd.fullscreen;
  3634. wd.minimized = _window_minimize_check(window_id) && !wd.fullscreen && !wd.maximized;
  3635. // Readjusting the window position if the window is being reparented by the window manager for decoration
  3636. Window root, parent, *children;
  3637. unsigned int nchildren;
  3638. if (XQueryTree(x11_display, wd.x11_window, &root, &parent, &children, &nchildren) && wd.parent != parent) {
  3639. wd.parent = parent;
  3640. if (!wd.embed_parent) {
  3641. window_set_position(wd.position, window_id);
  3642. }
  3643. }
  3644. XFree(children);
  3645. {
  3646. //the position in xconfigure is not useful here, obtain it manually
  3647. int x = 0, y = 0;
  3648. Window child;
  3649. XTranslateCoordinates(x11_display, wd.x11_window, DefaultRootWindow(x11_display), 0, 0, &x, &y, &child);
  3650. new_rect.position.x = x;
  3651. new_rect.position.y = y;
  3652. new_rect.size.width = event->xconfigure.width;
  3653. new_rect.size.height = event->xconfigure.height;
  3654. }
  3655. if (new_rect == Rect2i(wd.position, wd.size)) {
  3656. return;
  3657. }
  3658. wd.position = new_rect.position;
  3659. wd.size = new_rect.size;
  3660. #if defined(RD_ENABLED)
  3661. if (rendering_context) {
  3662. rendering_context->window_set_size(window_id, wd.size.width, wd.size.height);
  3663. }
  3664. #endif
  3665. #if defined(GLES3_ENABLED)
  3666. if (gl_manager) {
  3667. gl_manager->window_resize(window_id, wd.size.width, wd.size.height);
  3668. }
  3669. if (gl_manager_egl) {
  3670. gl_manager_egl->window_resize(window_id, wd.size.width, wd.size.height);
  3671. }
  3672. #endif
  3673. if (wd.rect_changed_callback.is_valid()) {
  3674. wd.rect_changed_callback.call(new_rect);
  3675. }
  3676. }
  3677. DisplayServer::WindowID DisplayServerX11::_get_focused_window_or_popup() const {
  3678. const List<WindowID>::Element *E = popup_list.back();
  3679. if (E) {
  3680. return E->get();
  3681. }
  3682. return last_focused_window;
  3683. }
  3684. void DisplayServerX11::_dispatch_input_events(const Ref<InputEvent> &p_event) {
  3685. static_cast<DisplayServerX11 *>(get_singleton())->_dispatch_input_event(p_event);
  3686. }
  3687. void DisplayServerX11::_dispatch_input_event(const Ref<InputEvent> &p_event) {
  3688. {
  3689. List<WindowID>::Element *E = popup_list.back();
  3690. if (E && Object::cast_to<InputEventKey>(*p_event)) {
  3691. // Redirect keyboard input to active popup.
  3692. if (windows.has(E->get())) {
  3693. Callable callable = windows[E->get()].input_event_callback;
  3694. if (callable.is_valid()) {
  3695. callable.call(p_event);
  3696. }
  3697. }
  3698. return;
  3699. }
  3700. }
  3701. Ref<InputEventFromWindow> event_from_window = p_event;
  3702. if (event_from_window.is_valid() && event_from_window->get_window_id() != INVALID_WINDOW_ID) {
  3703. // Send to a single window.
  3704. if (windows.has(event_from_window->get_window_id())) {
  3705. Callable callable = windows[event_from_window->get_window_id()].input_event_callback;
  3706. if (callable.is_valid()) {
  3707. callable.call(p_event);
  3708. }
  3709. }
  3710. } else {
  3711. // Send to all windows. Copy all pending callbacks, since callback can erase window.
  3712. Vector<Callable> cbs;
  3713. for (KeyValue<WindowID, WindowData> &E : windows) {
  3714. Callable callable = E.value.input_event_callback;
  3715. if (callable.is_valid()) {
  3716. cbs.push_back(callable);
  3717. }
  3718. }
  3719. for (const Callable &cb : cbs) {
  3720. cb.call(p_event);
  3721. }
  3722. }
  3723. }
  3724. void DisplayServerX11::_send_window_event(const WindowData &wd, WindowEvent p_event) {
  3725. if (wd.event_callback.is_valid()) {
  3726. Variant event = int(p_event);
  3727. wd.event_callback.call(event);
  3728. }
  3729. }
  3730. void DisplayServerX11::_set_input_focus(Window p_window, int p_revert_to) {
  3731. Window focused_window;
  3732. int focus_ret_state;
  3733. XGetInputFocus(x11_display, &focused_window, &focus_ret_state);
  3734. // Only attempt to change focus if the window isn't already focused, in order to
  3735. // prevent issues with Godot stealing input focus with alternative window managers.
  3736. if (p_window != focused_window) {
  3737. XSetInputFocus(x11_display, p_window, p_revert_to, CurrentTime);
  3738. }
  3739. }
  3740. void DisplayServerX11::_poll_events_thread(void *ud) {
  3741. DisplayServerX11 *display_server = static_cast<DisplayServerX11 *>(ud);
  3742. display_server->_poll_events();
  3743. }
  3744. Bool DisplayServerX11::_predicate_all_events(Display *display, XEvent *event, XPointer arg) {
  3745. // Just accept all events.
  3746. return True;
  3747. }
  3748. bool DisplayServerX11::_wait_for_events() const {
  3749. int x11_fd = ConnectionNumber(x11_display);
  3750. fd_set in_fds;
  3751. XFlush(x11_display);
  3752. FD_ZERO(&in_fds);
  3753. FD_SET(x11_fd, &in_fds);
  3754. struct timeval tv;
  3755. tv.tv_usec = 0;
  3756. tv.tv_sec = 1;
  3757. // Wait for next event or timeout.
  3758. int num_ready_fds = select(x11_fd + 1, &in_fds, nullptr, nullptr, &tv);
  3759. if (num_ready_fds > 0) {
  3760. // Event received.
  3761. return true;
  3762. } else {
  3763. // Error or timeout.
  3764. if (num_ready_fds < 0) {
  3765. ERR_PRINT("_wait_for_events: select error: " + itos(errno));
  3766. }
  3767. return false;
  3768. }
  3769. }
  3770. void DisplayServerX11::_poll_events() {
  3771. while (!events_thread_done.is_set()) {
  3772. _wait_for_events();
  3773. // Process events from the queue.
  3774. {
  3775. MutexLock mutex_lock(events_mutex);
  3776. _check_pending_events(polled_events);
  3777. }
  3778. }
  3779. }
  3780. void DisplayServerX11::_check_pending_events(LocalVector<XEvent> &r_events) {
  3781. // Flush to make sure to gather all pending events.
  3782. XFlush(x11_display);
  3783. // Non-blocking wait for next event and remove it from the queue.
  3784. XEvent ev = {};
  3785. while (XCheckIfEvent(x11_display, &ev, _predicate_all_events, nullptr)) {
  3786. // Check if the input manager wants to process the event.
  3787. if (XFilterEvent(&ev, None)) {
  3788. // Event has been filtered by the Input Manager,
  3789. // it has to be ignored and a new one will be received.
  3790. continue;
  3791. }
  3792. // Handle selection request events directly in the event thread, because
  3793. // communication through the x server takes several events sent back and forth
  3794. // and we don't want to block other programs while processing only one each frame.
  3795. if (ev.type == SelectionRequest) {
  3796. _handle_selection_request_event(&(ev.xselectionrequest));
  3797. continue;
  3798. }
  3799. r_events.push_back(ev);
  3800. }
  3801. }
  3802. DisplayServer::WindowID DisplayServerX11::window_get_active_popup() const {
  3803. const List<WindowID>::Element *E = popup_list.back();
  3804. if (E) {
  3805. return E->get();
  3806. } else {
  3807. return INVALID_WINDOW_ID;
  3808. }
  3809. }
  3810. void DisplayServerX11::window_set_popup_safe_rect(WindowID p_window, const Rect2i &p_rect) {
  3811. _THREAD_SAFE_METHOD_
  3812. ERR_FAIL_COND(!windows.has(p_window));
  3813. WindowData &wd = windows[p_window];
  3814. wd.parent_safe_rect = p_rect;
  3815. }
  3816. Rect2i DisplayServerX11::window_get_popup_safe_rect(WindowID p_window) const {
  3817. _THREAD_SAFE_METHOD_
  3818. ERR_FAIL_COND_V(!windows.has(p_window), Rect2i());
  3819. const WindowData &wd = windows[p_window];
  3820. return wd.parent_safe_rect;
  3821. }
  3822. void DisplayServerX11::popup_open(WindowID p_window) {
  3823. _THREAD_SAFE_METHOD_
  3824. bool has_popup_ancestor = false;
  3825. WindowID transient_root = p_window;
  3826. while (true) {
  3827. WindowID parent = windows[transient_root].transient_parent;
  3828. if (parent == INVALID_WINDOW_ID) {
  3829. break;
  3830. } else {
  3831. transient_root = parent;
  3832. if (windows[parent].is_popup) {
  3833. has_popup_ancestor = true;
  3834. break;
  3835. }
  3836. }
  3837. }
  3838. // Detect tooltips and other similar popups that shouldn't block input to their parent.
  3839. bool ignores_input = window_get_flag(WINDOW_FLAG_NO_FOCUS, p_window) && window_get_flag(WINDOW_FLAG_MOUSE_PASSTHROUGH, p_window);
  3840. WindowData &wd = windows[p_window];
  3841. if (wd.is_popup || (has_popup_ancestor && !ignores_input)) {
  3842. // Find current popup parent, or root popup if new window is not transient.
  3843. List<WindowID>::Element *C = nullptr;
  3844. List<WindowID>::Element *E = popup_list.back();
  3845. while (E) {
  3846. if (wd.transient_parent != E->get() || wd.transient_parent == INVALID_WINDOW_ID) {
  3847. C = E;
  3848. E = E->prev();
  3849. } else {
  3850. break;
  3851. }
  3852. }
  3853. if (C) {
  3854. _send_window_event(windows[C->get()], DisplayServerX11::WINDOW_EVENT_CLOSE_REQUEST);
  3855. }
  3856. time_since_popup = OS::get_singleton()->get_ticks_msec();
  3857. popup_list.push_back(p_window);
  3858. }
  3859. }
  3860. void DisplayServerX11::popup_close(WindowID p_window) {
  3861. _THREAD_SAFE_METHOD_
  3862. List<WindowID>::Element *E = popup_list.find(p_window);
  3863. while (E) {
  3864. List<WindowID>::Element *F = E->next();
  3865. WindowID win_id = E->get();
  3866. popup_list.erase(E);
  3867. if (win_id != p_window) {
  3868. // Only request close on related windows, not this window. We are already processing it.
  3869. _send_window_event(windows[win_id], DisplayServerX11::WINDOW_EVENT_CLOSE_REQUEST);
  3870. }
  3871. E = F;
  3872. }
  3873. }
  3874. bool DisplayServerX11::mouse_process_popups() {
  3875. _THREAD_SAFE_METHOD_
  3876. if (popup_list.is_empty()) {
  3877. return false;
  3878. }
  3879. uint64_t delta = OS::get_singleton()->get_ticks_msec() - time_since_popup;
  3880. if (delta < 250) {
  3881. return false;
  3882. }
  3883. int number_of_screens = XScreenCount(x11_display);
  3884. bool closed = false;
  3885. for (int i = 0; i < number_of_screens; i++) {
  3886. Window root, child;
  3887. int root_x, root_y, win_x, win_y;
  3888. unsigned int mask;
  3889. if (XQueryPointer(x11_display, XRootWindow(x11_display, i), &root, &child, &root_x, &root_y, &win_x, &win_y, &mask)) {
  3890. XWindowAttributes root_attrs;
  3891. XGetWindowAttributes(x11_display, root, &root_attrs);
  3892. Vector2i pos = Vector2i(root_attrs.x + root_x, root_attrs.y + root_y);
  3893. if (mask != last_mouse_monitor_mask) {
  3894. if (((mask & Button1Mask) || (mask & Button2Mask) || (mask & Button3Mask) || (mask & Button4Mask) || (mask & Button5Mask))) {
  3895. List<WindowID>::Element *C = nullptr;
  3896. List<WindowID>::Element *E = popup_list.back();
  3897. // Find top popup to close.
  3898. while (E) {
  3899. // Popup window area.
  3900. Rect2i win_rect = Rect2i(window_get_position_with_decorations(E->get()), window_get_size_with_decorations(E->get()));
  3901. // Area of the parent window, which responsible for opening sub-menu.
  3902. Rect2i safe_rect = window_get_popup_safe_rect(E->get());
  3903. if (win_rect.has_point(pos)) {
  3904. break;
  3905. } else if (safe_rect != Rect2i() && safe_rect.has_point(pos)) {
  3906. break;
  3907. } else {
  3908. C = E;
  3909. E = E->prev();
  3910. }
  3911. }
  3912. if (C) {
  3913. _send_window_event(windows[C->get()], DisplayServerX11::WINDOW_EVENT_CLOSE_REQUEST);
  3914. closed = true;
  3915. }
  3916. }
  3917. }
  3918. last_mouse_monitor_mask = mask;
  3919. }
  3920. }
  3921. return closed;
  3922. }
  3923. bool DisplayServerX11::_window_focus_check() {
  3924. Window focused_window;
  3925. int focus_ret_state;
  3926. XGetInputFocus(x11_display, &focused_window, &focus_ret_state);
  3927. bool has_focus = false;
  3928. for (const KeyValue<int, DisplayServerX11::WindowData> &wid : windows) {
  3929. if (wid.value.x11_window == focused_window || (wid.value.xic && wid.value.ime_active && wid.value.x11_xim_window == focused_window)) {
  3930. has_focus = true;
  3931. break;
  3932. }
  3933. }
  3934. return has_focus;
  3935. }
  3936. void DisplayServerX11::process_events() {
  3937. ERR_FAIL_COND(!Thread::is_main_thread());
  3938. _THREAD_SAFE_LOCK_
  3939. #ifdef DISPLAY_SERVER_X11_DEBUG_LOGS_ENABLED
  3940. static int frame = 0;
  3941. ++frame;
  3942. #endif
  3943. bool ignore_events = mouse_process_popups();
  3944. if (app_focused) {
  3945. //verify that one of the windows has focus, else send focus out notification
  3946. bool focus_found = false;
  3947. for (const KeyValue<WindowID, WindowData> &E : windows) {
  3948. if (E.value.focused) {
  3949. focus_found = true;
  3950. break;
  3951. }
  3952. }
  3953. if (!focus_found) {
  3954. uint64_t delta = OS::get_singleton()->get_ticks_msec() - time_since_no_focus;
  3955. if (delta > 250) {
  3956. //X11 can go between windows and have no focus for a while, when creating them or something else. Use this as safety to avoid unnecessary focus in/outs.
  3957. if (OS::get_singleton()->get_main_loop()) {
  3958. DEBUG_LOG_X11("All focus lost, triggering NOTIFICATION_APPLICATION_FOCUS_OUT\n");
  3959. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT);
  3960. }
  3961. app_focused = false;
  3962. }
  3963. } else {
  3964. time_since_no_focus = OS::get_singleton()->get_ticks_msec();
  3965. }
  3966. }
  3967. do_mouse_warp = false;
  3968. // Is the current mouse mode one where it needs to be grabbed.
  3969. bool mouse_mode_grab = mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN;
  3970. xi.pressure = 0;
  3971. xi.tilt = Vector2();
  3972. xi.pressure_supported = false;
  3973. LocalVector<XEvent> events;
  3974. {
  3975. // Block events polling while flushing events.
  3976. MutexLock mutex_lock(events_mutex);
  3977. events = polled_events;
  3978. polled_events.clear();
  3979. // Check for more pending events to avoid an extra frame delay.
  3980. _check_pending_events(events);
  3981. }
  3982. for (uint32_t event_index = 0; event_index < events.size(); ++event_index) {
  3983. XEvent &event = events[event_index];
  3984. bool ime_window_event = false;
  3985. WindowID window_id = MAIN_WINDOW_ID;
  3986. // Assign the event to the relevant window
  3987. for (const KeyValue<WindowID, WindowData> &E : windows) {
  3988. if (event.xany.window == E.value.x11_window) {
  3989. window_id = E.key;
  3990. break;
  3991. }
  3992. if (event.xany.window == E.value.x11_xim_window) {
  3993. window_id = E.key;
  3994. ime_window_event = true;
  3995. break;
  3996. }
  3997. }
  3998. if (XGetEventData(x11_display, &event.xcookie)) {
  3999. if (event.xcookie.type == GenericEvent && event.xcookie.extension == xi.opcode) {
  4000. XIDeviceEvent *event_data = (XIDeviceEvent *)event.xcookie.data;
  4001. switch (event_data->evtype) {
  4002. case XI_HierarchyChanged:
  4003. case XI_DeviceChanged: {
  4004. _refresh_device_info();
  4005. } break;
  4006. case XI_RawMotion: {
  4007. if (ime_window_event || ignore_events) {
  4008. break;
  4009. }
  4010. XIRawEvent *raw_event = (XIRawEvent *)event_data;
  4011. int device_id = raw_event->sourceid;
  4012. // Determine the axis used (called valuators in XInput for some forsaken reason)
  4013. // Mask is a bitmask indicating which axes are involved.
  4014. // We are interested in the values of axes 0 and 1.
  4015. if (raw_event->valuators.mask_len <= 0) {
  4016. break;
  4017. }
  4018. const double *values = raw_event->raw_values;
  4019. double rel_x = 0.0;
  4020. double rel_y = 0.0;
  4021. if (XIMaskIsSet(raw_event->valuators.mask, VALUATOR_ABSX)) {
  4022. rel_x = *values;
  4023. values++;
  4024. }
  4025. if (XIMaskIsSet(raw_event->valuators.mask, VALUATOR_ABSY)) {
  4026. rel_y = *values;
  4027. values++;
  4028. }
  4029. if (XIMaskIsSet(raw_event->valuators.mask, VALUATOR_PRESSURE)) {
  4030. HashMap<int, Vector2>::Iterator pen_pressure = xi.pen_pressure_range.find(device_id);
  4031. if (pen_pressure) {
  4032. Vector2 pen_pressure_range = pen_pressure->value;
  4033. if (pen_pressure_range != Vector2()) {
  4034. xi.pressure_supported = true;
  4035. xi.pressure = (*values - pen_pressure_range[0]) /
  4036. (pen_pressure_range[1] - pen_pressure_range[0]);
  4037. }
  4038. }
  4039. values++;
  4040. }
  4041. if (XIMaskIsSet(raw_event->valuators.mask, VALUATOR_TILTX)) {
  4042. HashMap<int, Vector2>::Iterator pen_tilt_x = xi.pen_tilt_x_range.find(device_id);
  4043. if (pen_tilt_x) {
  4044. Vector2 pen_tilt_x_range = pen_tilt_x->value;
  4045. if (pen_tilt_x_range[0] != 0 && *values < 0) {
  4046. xi.tilt.x = *values / -pen_tilt_x_range[0];
  4047. } else if (pen_tilt_x_range[1] != 0) {
  4048. xi.tilt.x = *values / pen_tilt_x_range[1];
  4049. }
  4050. }
  4051. values++;
  4052. }
  4053. if (XIMaskIsSet(raw_event->valuators.mask, VALUATOR_TILTY)) {
  4054. HashMap<int, Vector2>::Iterator pen_tilt_y = xi.pen_tilt_y_range.find(device_id);
  4055. if (pen_tilt_y) {
  4056. Vector2 pen_tilt_y_range = pen_tilt_y->value;
  4057. if (pen_tilt_y_range[0] != 0 && *values < 0) {
  4058. xi.tilt.y = *values / -pen_tilt_y_range[0];
  4059. } else if (pen_tilt_y_range[1] != 0) {
  4060. xi.tilt.y = *values / pen_tilt_y_range[1];
  4061. }
  4062. }
  4063. values++;
  4064. }
  4065. HashMap<int, bool>::Iterator pen_inverted = xi.pen_inverted_devices.find(device_id);
  4066. if (pen_inverted) {
  4067. xi.pen_inverted = pen_inverted->value;
  4068. }
  4069. // https://bugs.freedesktop.org/show_bug.cgi?id=71609
  4070. // http://lists.libsdl.org/pipermail/commits-libsdl.org/2015-June/000282.html
  4071. if (raw_event->time == xi.last_relative_time && rel_x == xi.relative_motion.x && rel_y == xi.relative_motion.y) {
  4072. break; // Flush duplicate to avoid overly fast motion
  4073. }
  4074. xi.old_raw_pos.x = xi.raw_pos.x;
  4075. xi.old_raw_pos.y = xi.raw_pos.y;
  4076. xi.raw_pos.x = rel_x;
  4077. xi.raw_pos.y = rel_y;
  4078. HashMap<int, Vector2>::Iterator abs_info = xi.absolute_devices.find(device_id);
  4079. if (abs_info) {
  4080. // Absolute mode device
  4081. Vector2 mult = abs_info->value;
  4082. xi.relative_motion.x += (xi.raw_pos.x - xi.old_raw_pos.x) * mult.x;
  4083. xi.relative_motion.y += (xi.raw_pos.y - xi.old_raw_pos.y) * mult.y;
  4084. } else {
  4085. // Relative mode device
  4086. xi.relative_motion.x = xi.raw_pos.x;
  4087. xi.relative_motion.y = xi.raw_pos.y;
  4088. }
  4089. xi.last_relative_time = raw_event->time;
  4090. } break;
  4091. #ifdef TOUCH_ENABLED
  4092. case XI_TouchBegin:
  4093. case XI_TouchEnd: {
  4094. if (ime_window_event || ignore_events) {
  4095. break;
  4096. }
  4097. bool is_begin = event_data->evtype == XI_TouchBegin;
  4098. int index = event_data->detail;
  4099. Vector2 pos = Vector2(event_data->event_x, event_data->event_y);
  4100. Ref<InputEventScreenTouch> st;
  4101. st.instantiate();
  4102. st->set_window_id(window_id);
  4103. st->set_index(index);
  4104. st->set_position(pos);
  4105. st->set_pressed(is_begin);
  4106. if (is_begin) {
  4107. if (xi.state.has(index)) { // Defensive
  4108. break;
  4109. }
  4110. xi.state[index] = pos;
  4111. if (xi.state.size() == 1) {
  4112. // X11 may send a motion event when a touch gesture begins, that would result
  4113. // in a spurious mouse motion event being sent to Godot; remember it to be able to filter it out
  4114. xi.mouse_pos_to_filter = pos;
  4115. }
  4116. Input::get_singleton()->parse_input_event(st);
  4117. } else {
  4118. if (!xi.state.has(index)) { // Defensive
  4119. break;
  4120. }
  4121. xi.state.erase(index);
  4122. Input::get_singleton()->parse_input_event(st);
  4123. }
  4124. } break;
  4125. case XI_TouchUpdate: {
  4126. if (ime_window_event || ignore_events) {
  4127. break;
  4128. }
  4129. int index = event_data->detail;
  4130. Vector2 pos = Vector2(event_data->event_x, event_data->event_y);
  4131. HashMap<int, Vector2>::Iterator curr_pos_elem = xi.state.find(index);
  4132. if (!curr_pos_elem) { // Defensive
  4133. break;
  4134. }
  4135. if (curr_pos_elem->value != pos) {
  4136. Ref<InputEventScreenDrag> sd;
  4137. sd.instantiate();
  4138. sd->set_window_id(window_id);
  4139. sd->set_index(index);
  4140. sd->set_position(pos);
  4141. sd->set_relative(pos - curr_pos_elem->value);
  4142. sd->set_relative_screen_position(sd->get_relative());
  4143. Input::get_singleton()->parse_input_event(sd);
  4144. curr_pos_elem->value = pos;
  4145. }
  4146. } break;
  4147. #endif
  4148. }
  4149. }
  4150. }
  4151. XFreeEventData(x11_display, &event.xcookie);
  4152. switch (event.type) {
  4153. case MapNotify: {
  4154. DEBUG_LOG_X11("[%u] MapNotify window=%lu (%u) \n", frame, event.xmap.window, window_id);
  4155. if (ime_window_event) {
  4156. break;
  4157. }
  4158. const WindowData &wd = windows[window_id];
  4159. XWindowAttributes xwa;
  4160. XSync(x11_display, False);
  4161. XGetWindowAttributes(x11_display, wd.x11_window, &xwa);
  4162. _update_actions_hints(window_id);
  4163. XFlush(x11_display);
  4164. // Set focus when menu window is started.
  4165. // RevertToPointerRoot is used to make sure we don't lose all focus in case
  4166. // a subwindow and its parent are both destroyed.
  4167. if ((xwa.map_state == IsViewable) && !wd.no_focus && !wd.is_popup && _window_focus_check()) {
  4168. _set_input_focus(wd.x11_window, RevertToPointerRoot);
  4169. }
  4170. // Have we failed to set fullscreen while the window was unmapped?
  4171. _validate_mode_on_map(window_id);
  4172. // On KDE Plasma, when the parent window of an embedded process is restored after being minimized,
  4173. // only the embedded window receives the Map notification, causing it to
  4174. // appear without its parent.
  4175. if (wd.embed_parent) {
  4176. XMapWindow(x11_display, wd.embed_parent);
  4177. }
  4178. } break;
  4179. case Expose: {
  4180. DEBUG_LOG_X11("[%u] Expose window=%lu (%u), count='%u' \n", frame, event.xexpose.window, window_id, event.xexpose.count);
  4181. if (ime_window_event) {
  4182. break;
  4183. }
  4184. windows[window_id].fullscreen = _window_fullscreen_check(window_id);
  4185. Main::force_redraw();
  4186. } break;
  4187. case NoExpose: {
  4188. DEBUG_LOG_X11("[%u] NoExpose drawable=%lu (%u) \n", frame, event.xnoexpose.drawable, window_id);
  4189. if (ime_window_event) {
  4190. break;
  4191. }
  4192. windows[window_id].minimized = true;
  4193. } break;
  4194. case VisibilityNotify: {
  4195. DEBUG_LOG_X11("[%u] VisibilityNotify window=%lu (%u), state=%u \n", frame, event.xvisibility.window, window_id, event.xvisibility.state);
  4196. if (ime_window_event) {
  4197. break;
  4198. }
  4199. windows[window_id].minimized = _window_minimize_check(window_id);
  4200. } break;
  4201. case LeaveNotify: {
  4202. DEBUG_LOG_X11("[%u] LeaveNotify window=%lu (%u), mode='%u' \n", frame, event.xcrossing.window, window_id, event.xcrossing.mode);
  4203. if (ime_window_event) {
  4204. break;
  4205. }
  4206. if (!mouse_mode_grab && window_mouseover_id == window_id) {
  4207. window_mouseover_id = INVALID_WINDOW_ID;
  4208. _send_window_event(windows[window_id], WINDOW_EVENT_MOUSE_EXIT);
  4209. }
  4210. } break;
  4211. case EnterNotify: {
  4212. DEBUG_LOG_X11("[%u] EnterNotify window=%lu (%u), mode='%u' \n", frame, event.xcrossing.window, window_id, event.xcrossing.mode);
  4213. if (ime_window_event) {
  4214. break;
  4215. }
  4216. if (!mouse_mode_grab && window_mouseover_id != window_id) {
  4217. if (window_mouseover_id != INVALID_WINDOW_ID) {
  4218. _send_window_event(windows[window_mouseover_id], WINDOW_EVENT_MOUSE_EXIT);
  4219. }
  4220. window_mouseover_id = window_id;
  4221. _send_window_event(windows[window_id], WINDOW_EVENT_MOUSE_ENTER);
  4222. }
  4223. } break;
  4224. case FocusIn: {
  4225. DEBUG_LOG_X11("[%u] FocusIn window=%lu (%u), mode='%u' \n", frame, event.xfocus.window, window_id, event.xfocus.mode);
  4226. if (ime_window_event || (event.xfocus.detail == NotifyInferior)) {
  4227. break;
  4228. }
  4229. WindowData &wd = windows[window_id];
  4230. last_focused_window = window_id;
  4231. wd.focused = true;
  4232. // Keep track of focus order for overlapping windows.
  4233. static unsigned int focus_order = 0;
  4234. wd.focus_order = ++focus_order;
  4235. #ifdef ACCESSKIT_ENABLED
  4236. if (accessibility_driver) {
  4237. accessibility_driver->accessibility_set_window_focused(window_id, true);
  4238. }
  4239. #endif
  4240. _send_window_event(wd, WINDOW_EVENT_FOCUS_IN);
  4241. if (mouse_mode_grab) {
  4242. // Show and update the cursor if confined and the window regained focus.
  4243. for (const KeyValue<WindowID, WindowData> &E : windows) {
  4244. if (mouse_mode == MOUSE_MODE_CONFINED) {
  4245. XUndefineCursor(x11_display, E.value.x11_window);
  4246. } else if (mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN) { // Or re-hide it.
  4247. XDefineCursor(x11_display, E.value.x11_window, null_cursor);
  4248. }
  4249. XGrabPointer(
  4250. x11_display, E.value.x11_window, True,
  4251. ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
  4252. GrabModeAsync, GrabModeAsync, E.value.x11_window, None, CurrentTime);
  4253. }
  4254. }
  4255. #ifdef TOUCH_ENABLED
  4256. // Grab touch devices to avoid OS gesture interference
  4257. /*for (int i = 0; i < xi.touch_devices.size(); ++i) {
  4258. XIGrabDevice(x11_display, xi.touch_devices[i], x11_window, CurrentTime, None, XIGrabModeAsync, XIGrabModeAsync, False, &xi.touch_event_mask);
  4259. }*/
  4260. #endif
  4261. if (!app_focused) {
  4262. if (OS::get_singleton()->get_main_loop()) {
  4263. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN);
  4264. }
  4265. app_focused = true;
  4266. }
  4267. } break;
  4268. case FocusOut: {
  4269. DEBUG_LOG_X11("[%u] FocusOut window=%lu (%u), mode='%u' \n", frame, event.xfocus.window, window_id, event.xfocus.mode);
  4270. WindowData &wd = windows[window_id];
  4271. if (ime_window_event || (event.xfocus.detail == NotifyInferior)) {
  4272. break;
  4273. }
  4274. if (wd.ime_active) {
  4275. MutexLock mutex_lock(events_mutex);
  4276. XUnsetICFocus(wd.xic);
  4277. XUnmapWindow(x11_display, wd.x11_xim_window);
  4278. wd.ime_active = false;
  4279. im_text = String();
  4280. im_selection = Vector2i();
  4281. OS_Unix::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_IME_UPDATE);
  4282. }
  4283. wd.focused = false;
  4284. Input::get_singleton()->release_pressed_events();
  4285. #ifdef ACCESSKIT_ENABLED
  4286. if (accessibility_driver) {
  4287. accessibility_driver->accessibility_set_window_focused(window_id, false);
  4288. }
  4289. #endif
  4290. _send_window_event(wd, WINDOW_EVENT_FOCUS_OUT);
  4291. if (mouse_mode_grab) {
  4292. for (const KeyValue<WindowID, WindowData> &E : windows) {
  4293. //dear X11, I try, I really try, but you never work, you do whatever you want.
  4294. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  4295. // Show the cursor if we're in captured mode so it doesn't look weird.
  4296. XUndefineCursor(x11_display, E.value.x11_window);
  4297. }
  4298. }
  4299. XUngrabPointer(x11_display, CurrentTime);
  4300. }
  4301. #ifdef TOUCH_ENABLED
  4302. // Ungrab touch devices so input works as usual while we are unfocused
  4303. /*for (int i = 0; i < xi.touch_devices.size(); ++i) {
  4304. XIUngrabDevice(x11_display, xi.touch_devices[i], CurrentTime);
  4305. }*/
  4306. // Release every pointer to avoid sticky points
  4307. for (const KeyValue<int, Vector2> &E : xi.state) {
  4308. Ref<InputEventScreenTouch> st;
  4309. st.instantiate();
  4310. st->set_index(E.key);
  4311. st->set_window_id(window_id);
  4312. st->set_position(E.value);
  4313. Input::get_singleton()->parse_input_event(st);
  4314. }
  4315. xi.state.clear();
  4316. #endif
  4317. } break;
  4318. case ConfigureNotify: {
  4319. DEBUG_LOG_X11("[%u] ConfigureNotify window=%lu (%u), event=%lu, above=%lu, override_redirect=%u \n", frame, event.xconfigure.window, window_id, event.xconfigure.event, event.xconfigure.above, event.xconfigure.override_redirect);
  4320. if (event.xconfigure.window == windows[window_id].x11_xim_window) {
  4321. break;
  4322. }
  4323. _window_changed(&event);
  4324. } break;
  4325. case ButtonPress:
  4326. case ButtonRelease: {
  4327. if (ime_window_event || ignore_events) {
  4328. break;
  4329. }
  4330. /* exit in case of a mouse button press */
  4331. last_timestamp = event.xbutton.time;
  4332. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  4333. event.xbutton.x = last_mouse_pos.x;
  4334. event.xbutton.y = last_mouse_pos.y;
  4335. }
  4336. Ref<InputEventMouseButton> mb;
  4337. mb.instantiate();
  4338. mb->set_window_id(window_id);
  4339. _get_key_modifier_state(event.xbutton.state, mb);
  4340. mb->set_button_index((MouseButton)event.xbutton.button);
  4341. if (mb->get_button_index() == MouseButton::RIGHT) {
  4342. mb->set_button_index(MouseButton::MIDDLE);
  4343. } else if (mb->get_button_index() == MouseButton::MIDDLE) {
  4344. mb->set_button_index(MouseButton::RIGHT);
  4345. }
  4346. mb->set_position(Vector2(event.xbutton.x, event.xbutton.y));
  4347. mb->set_global_position(mb->get_position());
  4348. mb->set_pressed((event.type == ButtonPress));
  4349. if (mb->is_pressed() && mb->get_button_index() >= MouseButton::WHEEL_UP && mb->get_button_index() <= MouseButton::WHEEL_RIGHT) {
  4350. MouseButtonMask mask = mouse_button_to_mask(mb->get_button_index());
  4351. BitField<MouseButtonMask> scroll_mask = mouse_get_button_state();
  4352. scroll_mask.set_flag(mask);
  4353. mb->set_button_mask(scroll_mask);
  4354. } else {
  4355. mb->set_button_mask(mouse_get_button_state());
  4356. }
  4357. const WindowData &wd = windows[window_id];
  4358. if (event.type == ButtonPress) {
  4359. DEBUG_LOG_X11("[%u] ButtonPress window=%lu (%u), button_index=%u \n", frame, event.xbutton.window, window_id, mb->get_button_index());
  4360. // Ensure window focus on click.
  4361. // RevertToPointerRoot is used to make sure we don't lose all focus in case
  4362. // a subwindow and its parent are both destroyed.
  4363. if (!wd.no_focus && !wd.is_popup) {
  4364. _set_input_focus(wd.x11_window, RevertToPointerRoot);
  4365. }
  4366. uint64_t diff = OS::get_singleton()->get_ticks_usec() / 1000 - last_click_ms;
  4367. if (mb->get_button_index() == last_click_button_index) {
  4368. if (diff < 400 && Vector2(last_click_pos).distance_to(Vector2(event.xbutton.x, event.xbutton.y)) < 5) {
  4369. last_click_ms = 0;
  4370. last_click_pos = Point2i(-100, -100);
  4371. last_click_button_index = MouseButton::NONE;
  4372. mb->set_double_click(true);
  4373. }
  4374. } else if (mb->get_button_index() < MouseButton::WHEEL_UP || mb->get_button_index() > MouseButton::WHEEL_RIGHT) {
  4375. last_click_button_index = mb->get_button_index();
  4376. }
  4377. if (!mb->is_double_click()) {
  4378. last_click_ms += diff;
  4379. last_click_pos = Point2i(event.xbutton.x, event.xbutton.y);
  4380. }
  4381. } else {
  4382. DEBUG_LOG_X11("[%u] ButtonRelease window=%lu (%u), button_index=%u \n", frame, event.xbutton.window, window_id, mb->get_button_index());
  4383. WindowID window_id_other = INVALID_WINDOW_ID;
  4384. Window wd_other_x11_window;
  4385. if (!wd.focused) {
  4386. // Propagate the event to the focused window,
  4387. // because it's received only on the topmost window.
  4388. // Note: This is needed for drag & drop to work between windows,
  4389. // because the engine expects events to keep being processed
  4390. // on the same window dragging started.
  4391. for (const KeyValue<WindowID, WindowData> &E : windows) {
  4392. if (E.value.focused) {
  4393. if (E.key != window_id) {
  4394. window_id_other = E.key;
  4395. wd_other_x11_window = E.value.x11_window;
  4396. }
  4397. break;
  4398. }
  4399. }
  4400. }
  4401. if (window_id_other != INVALID_WINDOW_ID) {
  4402. int x, y;
  4403. Window child;
  4404. XTranslateCoordinates(x11_display, wd.x11_window, wd_other_x11_window, event.xbutton.x, event.xbutton.y, &x, &y, &child);
  4405. mb->set_window_id(window_id_other);
  4406. mb->set_position(Vector2(x, y));
  4407. mb->set_global_position(mb->get_position());
  4408. }
  4409. }
  4410. Input::get_singleton()->parse_input_event(mb);
  4411. } break;
  4412. case MotionNotify: {
  4413. if (ime_window_event || ignore_events) {
  4414. break;
  4415. }
  4416. // The X11 API requires filtering one-by-one through the motion
  4417. // notify events, in order to figure out which event is the one
  4418. // generated by warping the mouse pointer.
  4419. WindowID focused_window_id = _get_focused_window_or_popup();
  4420. if (!windows.has(focused_window_id)) {
  4421. focused_window_id = MAIN_WINDOW_ID;
  4422. }
  4423. while (true) {
  4424. if (mouse_mode == MOUSE_MODE_CAPTURED && event.xmotion.x == windows[focused_window_id].size.width / 2 && event.xmotion.y == windows[focused_window_id].size.height / 2) {
  4425. //this is likely the warp event since it was warped here
  4426. center = Vector2(event.xmotion.x, event.xmotion.y);
  4427. break;
  4428. }
  4429. if (event_index + 1 < events.size()) {
  4430. const XEvent &next_event = events[event_index + 1];
  4431. if (next_event.type == MotionNotify) {
  4432. ++event_index;
  4433. event = next_event;
  4434. } else {
  4435. break;
  4436. }
  4437. } else {
  4438. break;
  4439. }
  4440. }
  4441. last_timestamp = event.xmotion.time;
  4442. // Motion is also simple.
  4443. // A little hack is in order
  4444. // to be able to send relative motion events.
  4445. Point2i pos(event.xmotion.x, event.xmotion.y);
  4446. // Avoidance of spurious mouse motion (see handling of touch)
  4447. bool filter = false;
  4448. // Adding some tolerance to match better Point2i to Vector2
  4449. if (xi.state.size() && Vector2(pos).distance_squared_to(xi.mouse_pos_to_filter) < 2) {
  4450. filter = true;
  4451. }
  4452. // Invalidate to avoid filtering a possible legitimate similar event coming later
  4453. xi.mouse_pos_to_filter = Vector2(1e10, 1e10);
  4454. if (filter) {
  4455. break;
  4456. }
  4457. const WindowData &wd = windows[window_id];
  4458. bool focused = wd.focused;
  4459. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  4460. if (xi.relative_motion.x == 0 && xi.relative_motion.y == 0) {
  4461. break;
  4462. }
  4463. Point2i new_center = pos;
  4464. pos = last_mouse_pos + xi.relative_motion;
  4465. center = new_center;
  4466. do_mouse_warp = focused; // warp the cursor if we're focused in
  4467. }
  4468. if (!last_mouse_pos_valid) {
  4469. last_mouse_pos = pos;
  4470. last_mouse_pos_valid = true;
  4471. }
  4472. // Hackish but relative mouse motion is already handled in the RawMotion event.
  4473. // RawMotion does not provide the absolute mouse position (whereas MotionNotify does).
  4474. // Therefore, RawMotion cannot be the authority on absolute mouse position.
  4475. // RawMotion provides more precision than MotionNotify, which doesn't sense subpixel motion.
  4476. // Therefore, MotionNotify cannot be the authority on relative mouse motion.
  4477. // This means we need to take a combined approach...
  4478. Point2i rel;
  4479. // Only use raw input if in capture mode. Otherwise use the classic behavior.
  4480. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  4481. rel = xi.relative_motion;
  4482. } else {
  4483. rel = pos - last_mouse_pos;
  4484. }
  4485. // Reset to prevent lingering motion
  4486. xi.relative_motion.x = 0;
  4487. xi.relative_motion.y = 0;
  4488. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  4489. pos = Point2i(windows[focused_window_id].size.width / 2, windows[focused_window_id].size.height / 2);
  4490. }
  4491. BitField<MouseButtonMask> last_button_state = MouseButtonMask::NONE;
  4492. if (event.xmotion.state & Button1Mask) {
  4493. last_button_state.set_flag(MouseButtonMask::LEFT);
  4494. }
  4495. if (event.xmotion.state & Button2Mask) {
  4496. last_button_state.set_flag(MouseButtonMask::MIDDLE);
  4497. }
  4498. if (event.xmotion.state & Button3Mask) {
  4499. last_button_state.set_flag(MouseButtonMask::RIGHT);
  4500. }
  4501. if (event.xmotion.state & Button4Mask) {
  4502. last_button_state.set_flag(MouseButtonMask::MB_XBUTTON1);
  4503. }
  4504. if (event.xmotion.state & Button5Mask) {
  4505. last_button_state.set_flag(MouseButtonMask::MB_XBUTTON2);
  4506. }
  4507. Ref<InputEventMouseMotion> mm;
  4508. mm.instantiate();
  4509. mm->set_window_id(window_id);
  4510. if (xi.pressure_supported) {
  4511. mm->set_pressure(xi.pressure);
  4512. } else {
  4513. mm->set_pressure(bool(last_button_state.has_flag(MouseButtonMask::LEFT)) ? 1.0f : 0.0f);
  4514. }
  4515. mm->set_tilt(xi.tilt);
  4516. mm->set_pen_inverted(xi.pen_inverted);
  4517. _get_key_modifier_state(event.xmotion.state, mm);
  4518. mm->set_button_mask(last_button_state);
  4519. mm->set_position(pos);
  4520. mm->set_global_position(pos);
  4521. mm->set_velocity(Input::get_singleton()->get_last_mouse_velocity());
  4522. mm->set_screen_velocity(mm->get_velocity());
  4523. mm->set_relative(rel);
  4524. mm->set_relative_screen_position(rel);
  4525. last_mouse_pos = pos;
  4526. // printf("rel: %d,%d\n", rel.x, rel.y );
  4527. // Don't propagate the motion event unless we have focus
  4528. // this is so that the relative motion doesn't get messed up
  4529. // after we regain focus.
  4530. // Adjusted to parse the input event if the window is not focused allowing mouse hovering on the editor
  4531. // the embedding process has focus.
  4532. if (!focused) {
  4533. // Propagate the event to the focused window,
  4534. // because it's received only on the topmost window.
  4535. // Note: This is needed for drag & drop to work between windows,
  4536. // because the engine expects events to keep being processed
  4537. // on the same window dragging started.
  4538. for (const KeyValue<WindowID, WindowData> &E : windows) {
  4539. const WindowData &wd_other = E.value;
  4540. if (wd_other.focused) {
  4541. int x, y;
  4542. Window child;
  4543. XTranslateCoordinates(x11_display, wd.x11_window, wd_other.x11_window, event.xmotion.x, event.xmotion.y, &x, &y, &child);
  4544. Point2i pos_focused(x, y);
  4545. mm->set_window_id(E.key);
  4546. mm->set_position(pos_focused);
  4547. mm->set_global_position(pos_focused);
  4548. mm->set_velocity(Input::get_singleton()->get_last_mouse_velocity());
  4549. break;
  4550. }
  4551. }
  4552. }
  4553. Input::get_singleton()->parse_input_event(mm);
  4554. } break;
  4555. case KeyPress:
  4556. case KeyRelease: {
  4557. if (ignore_events) {
  4558. break;
  4559. }
  4560. #ifdef DISPLAY_SERVER_X11_DEBUG_LOGS_ENABLED
  4561. if (event.type == KeyPress) {
  4562. DEBUG_LOG_X11("[%u] KeyPress window=%lu (%u), keycode=%u, time=%lu \n", frame, event.xkey.window, window_id, event.xkey.keycode, event.xkey.time);
  4563. } else {
  4564. DEBUG_LOG_X11("[%u] KeyRelease window=%lu (%u), keycode=%u, time=%lu \n", frame, event.xkey.window, window_id, event.xkey.keycode, event.xkey.time);
  4565. }
  4566. #endif
  4567. last_timestamp = event.xkey.time;
  4568. // key event is a little complex, so
  4569. // it will be handled in its own function.
  4570. _handle_key_event(window_id, &event.xkey, events, event_index);
  4571. } break;
  4572. case SelectionNotify:
  4573. if (ime_window_event) {
  4574. break;
  4575. }
  4576. if (event.xselection.target == requested) {
  4577. Property p = _read_property(x11_display, windows[window_id].x11_window, XInternAtom(x11_display, "PRIMARY", 0));
  4578. Vector<String> files = String((char *)p.data).split("\r\n", false);
  4579. XFree(p.data);
  4580. for (int i = 0; i < files.size(); i++) {
  4581. files.write[i] = files[i].replace("file://", "").uri_file_decode();
  4582. }
  4583. if (windows[window_id].drop_files_callback.is_valid()) {
  4584. Variant v_files = files;
  4585. const Variant *v_args[1] = { &v_files };
  4586. Variant ret;
  4587. Callable::CallError ce;
  4588. windows[window_id].drop_files_callback.callp((const Variant **)&v_args, 1, ret, ce);
  4589. if (ce.error != Callable::CallError::CALL_OK) {
  4590. ERR_PRINT(vformat("Failed to execute drop files callback: %s.", Variant::get_callable_error_text(windows[window_id].drop_files_callback, v_args, 1, ce)));
  4591. }
  4592. }
  4593. //Reply that all is well.
  4594. XClientMessageEvent m;
  4595. memset(&m, 0, sizeof(m));
  4596. m.type = ClientMessage;
  4597. m.display = x11_display;
  4598. m.window = xdnd_source_window;
  4599. m.message_type = xdnd_finished;
  4600. m.format = 32;
  4601. m.data.l[0] = windows[window_id].x11_window;
  4602. m.data.l[1] = 1;
  4603. m.data.l[2] = xdnd_action_copy; //We only ever copy.
  4604. XSendEvent(x11_display, xdnd_source_window, False, NoEventMask, (XEvent *)&m);
  4605. }
  4606. break;
  4607. case ClientMessage:
  4608. if (ime_window_event) {
  4609. break;
  4610. }
  4611. if ((unsigned int)event.xclient.data.l[0] == (unsigned int)wm_delete) {
  4612. _send_window_event(windows[window_id], WINDOW_EVENT_CLOSE_REQUEST);
  4613. }
  4614. else if ((unsigned int)event.xclient.message_type == (unsigned int)xdnd_enter) {
  4615. //File(s) have been dragged over the window, check for supported target (text/uri-list)
  4616. xdnd_version = (event.xclient.data.l[1] >> 24);
  4617. Window source = event.xclient.data.l[0];
  4618. bool more_than_3 = event.xclient.data.l[1] & 1;
  4619. if (more_than_3) {
  4620. Property p = _read_property(x11_display, source, XInternAtom(x11_display, "XdndTypeList", False));
  4621. requested = pick_target_from_list(x11_display, (Atom *)p.data, p.nitems);
  4622. XFree(p.data);
  4623. } else {
  4624. requested = pick_target_from_atoms(x11_display, event.xclient.data.l[2], event.xclient.data.l[3], event.xclient.data.l[4]);
  4625. }
  4626. } else if ((unsigned int)event.xclient.message_type == (unsigned int)xdnd_position) {
  4627. //xdnd position event, reply with an XDND status message
  4628. //just depending on type of data for now
  4629. XClientMessageEvent m;
  4630. memset(&m, 0, sizeof(m));
  4631. m.type = ClientMessage;
  4632. m.display = event.xclient.display;
  4633. m.window = event.xclient.data.l[0];
  4634. m.message_type = xdnd_status;
  4635. m.format = 32;
  4636. m.data.l[0] = windows[window_id].x11_window;
  4637. m.data.l[1] = (requested != None);
  4638. m.data.l[2] = 0; //empty rectangle
  4639. m.data.l[3] = 0;
  4640. m.data.l[4] = xdnd_action_copy;
  4641. XSendEvent(x11_display, event.xclient.data.l[0], False, NoEventMask, (XEvent *)&m);
  4642. XFlush(x11_display);
  4643. } else if ((unsigned int)event.xclient.message_type == (unsigned int)xdnd_drop) {
  4644. if (requested != None) {
  4645. xdnd_source_window = event.xclient.data.l[0];
  4646. if (xdnd_version >= 1) {
  4647. XConvertSelection(x11_display, xdnd_selection, requested, XInternAtom(x11_display, "PRIMARY", 0), windows[window_id].x11_window, event.xclient.data.l[2]);
  4648. } else {
  4649. XConvertSelection(x11_display, xdnd_selection, requested, XInternAtom(x11_display, "PRIMARY", 0), windows[window_id].x11_window, CurrentTime);
  4650. }
  4651. } else {
  4652. //Reply that we're not interested.
  4653. XClientMessageEvent m;
  4654. memset(&m, 0, sizeof(m));
  4655. m.type = ClientMessage;
  4656. m.display = event.xclient.display;
  4657. m.window = event.xclient.data.l[0];
  4658. m.message_type = xdnd_finished;
  4659. m.format = 32;
  4660. m.data.l[0] = windows[window_id].x11_window;
  4661. m.data.l[1] = 0;
  4662. m.data.l[2] = None; //Failed.
  4663. XSendEvent(x11_display, event.xclient.data.l[0], False, NoEventMask, (XEvent *)&m);
  4664. }
  4665. }
  4666. break;
  4667. default:
  4668. break;
  4669. }
  4670. }
  4671. XFlush(x11_display);
  4672. if (do_mouse_warp) {
  4673. XWarpPointer(x11_display, None, windows[MAIN_WINDOW_ID].x11_window,
  4674. 0, 0, 0, 0, (int)windows[MAIN_WINDOW_ID].size.width / 2, (int)windows[MAIN_WINDOW_ID].size.height / 2);
  4675. /*
  4676. Window root, child;
  4677. int root_x, root_y;
  4678. int win_x, win_y;
  4679. unsigned int mask;
  4680. XQueryPointer( x11_display, x11_window, &root, &child, &root_x, &root_y, &win_x, &win_y, &mask );
  4681. printf("Root: %d,%d\n", root_x, root_y);
  4682. printf("Win: %d,%d\n", win_x, win_y);
  4683. */
  4684. }
  4685. #ifdef DBUS_ENABLED
  4686. if (portal_desktop) {
  4687. portal_desktop->process_callbacks();
  4688. }
  4689. #endif
  4690. _THREAD_SAFE_UNLOCK_
  4691. Input::get_singleton()->flush_buffered_events();
  4692. }
  4693. void DisplayServerX11::release_rendering_thread() {
  4694. #if defined(GLES3_ENABLED)
  4695. if (gl_manager) {
  4696. gl_manager->release_current();
  4697. }
  4698. if (gl_manager_egl) {
  4699. gl_manager_egl->release_current();
  4700. }
  4701. #endif
  4702. }
  4703. void DisplayServerX11::swap_buffers() {
  4704. #if defined(GLES3_ENABLED)
  4705. if (gl_manager) {
  4706. gl_manager->swap_buffers();
  4707. }
  4708. if (gl_manager_egl) {
  4709. gl_manager_egl->swap_buffers();
  4710. }
  4711. #endif
  4712. }
  4713. void DisplayServerX11::_update_context(WindowData &wd) {
  4714. XClassHint *classHint = XAllocClassHint();
  4715. if (classHint) {
  4716. CharString name_str;
  4717. switch (context) {
  4718. case CONTEXT_EDITOR:
  4719. name_str = "Godot_Editor";
  4720. break;
  4721. case CONTEXT_PROJECTMAN:
  4722. name_str = "Godot_ProjectList";
  4723. break;
  4724. case CONTEXT_ENGINE:
  4725. name_str = "Godot_Engine";
  4726. break;
  4727. }
  4728. CharString class_str;
  4729. if (context == CONTEXT_ENGINE) {
  4730. String config_name = GLOBAL_GET("application/config/name");
  4731. if (config_name.length() == 0) {
  4732. class_str = "Godot_Engine";
  4733. } else {
  4734. class_str = config_name.utf8();
  4735. }
  4736. } else {
  4737. class_str = "Godot";
  4738. }
  4739. classHint->res_class = class_str.ptrw();
  4740. classHint->res_name = name_str.ptrw();
  4741. XSetClassHint(x11_display, wd.x11_window, classHint);
  4742. XFree(classHint);
  4743. }
  4744. }
  4745. void DisplayServerX11::set_context(Context p_context) {
  4746. _THREAD_SAFE_METHOD_
  4747. context = p_context;
  4748. for (KeyValue<WindowID, WindowData> &E : windows) {
  4749. _update_context(E.value);
  4750. }
  4751. }
  4752. bool DisplayServerX11::is_window_transparency_available() const {
  4753. CharString net_wm_cm_name = vformat("_NET_WM_CM_S%d", XDefaultScreen(x11_display)).ascii();
  4754. Atom net_wm_cm = XInternAtom(x11_display, net_wm_cm_name.get_data(), False);
  4755. if (net_wm_cm == None) {
  4756. return false;
  4757. }
  4758. if (XGetSelectionOwner(x11_display, net_wm_cm) == None) {
  4759. return false;
  4760. }
  4761. #if defined(RD_ENABLED)
  4762. if (rendering_device && !rendering_device->is_composite_alpha_supported()) {
  4763. return false;
  4764. }
  4765. #endif
  4766. return OS::get_singleton()->is_layered_allowed();
  4767. }
  4768. void DisplayServerX11::set_native_icon(const String &p_filename) {
  4769. WARN_PRINT("Native icon not supported by this display server.");
  4770. }
  4771. bool g_set_icon_error = false;
  4772. int set_icon_errorhandler(Display *dpy, XErrorEvent *ev) {
  4773. g_set_icon_error = true;
  4774. return 0;
  4775. }
  4776. void DisplayServerX11::set_icon(const Ref<Image> &p_icon) {
  4777. _THREAD_SAFE_METHOD_
  4778. WindowData &wd = windows[MAIN_WINDOW_ID];
  4779. int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&set_icon_errorhandler);
  4780. Atom net_wm_icon = XInternAtom(x11_display, "_NET_WM_ICON", False);
  4781. if (p_icon.is_valid()) {
  4782. ERR_FAIL_COND(p_icon->get_width() <= 0 || p_icon->get_height() <= 0);
  4783. Ref<Image> img = p_icon->duplicate();
  4784. img->convert(Image::FORMAT_RGBA8);
  4785. while (true) {
  4786. int w = img->get_width();
  4787. int h = img->get_height();
  4788. if (g_set_icon_error) {
  4789. g_set_icon_error = false;
  4790. WARN_PRINT(vformat("Icon too large (%dx%d), attempting to downscale icon.", w, h));
  4791. int new_width, new_height;
  4792. if (w > h) {
  4793. new_width = w / 2;
  4794. new_height = h * new_width / w;
  4795. } else {
  4796. new_height = h / 2;
  4797. new_width = w * new_height / h;
  4798. }
  4799. w = new_width;
  4800. h = new_height;
  4801. if (!w || !h) {
  4802. WARN_PRINT("Unable to set icon.");
  4803. break;
  4804. }
  4805. img->resize(w, h, Image::INTERPOLATE_CUBIC);
  4806. }
  4807. // We're using long to have wordsize (32Bit build -> 32 Bits, 64 Bit build -> 64 Bits
  4808. Vector<long> pd;
  4809. pd.resize(2 + w * h);
  4810. pd.write[0] = w;
  4811. pd.write[1] = h;
  4812. const uint8_t *r = img->get_data().ptr();
  4813. long *wr = &pd.write[2];
  4814. uint8_t const *pr = r;
  4815. for (int i = 0; i < w * h; i++) {
  4816. long v = 0;
  4817. // A R G B
  4818. v |= pr[3] << 24 | pr[0] << 16 | pr[1] << 8 | pr[2];
  4819. *wr++ = v;
  4820. pr += 4;
  4821. }
  4822. if (net_wm_icon != None) {
  4823. XChangeProperty(x11_display, wd.x11_window, net_wm_icon, XA_CARDINAL, 32, PropModeReplace, (unsigned char *)pd.ptr(), pd.size());
  4824. }
  4825. if (!g_set_icon_error) {
  4826. break;
  4827. }
  4828. }
  4829. } else {
  4830. XDeleteProperty(x11_display, wd.x11_window, net_wm_icon);
  4831. }
  4832. XFlush(x11_display);
  4833. XSetErrorHandler(oldHandler);
  4834. }
  4835. void DisplayServerX11::window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window) {
  4836. _THREAD_SAFE_METHOD_
  4837. #if defined(RD_ENABLED)
  4838. if (rendering_context) {
  4839. rendering_context->window_set_vsync_mode(p_window, p_vsync_mode);
  4840. }
  4841. #endif
  4842. #if defined(GLES3_ENABLED)
  4843. if (gl_manager) {
  4844. gl_manager->set_use_vsync(p_vsync_mode != DisplayServer::VSYNC_DISABLED);
  4845. }
  4846. if (gl_manager_egl) {
  4847. gl_manager_egl->set_use_vsync(p_vsync_mode != DisplayServer::VSYNC_DISABLED);
  4848. }
  4849. #endif
  4850. }
  4851. DisplayServer::VSyncMode DisplayServerX11::window_get_vsync_mode(WindowID p_window) const {
  4852. _THREAD_SAFE_METHOD_
  4853. #if defined(RD_ENABLED)
  4854. if (rendering_context) {
  4855. return rendering_context->window_get_vsync_mode(p_window);
  4856. }
  4857. #endif
  4858. #if defined(GLES3_ENABLED)
  4859. if (gl_manager) {
  4860. return gl_manager->is_using_vsync() ? DisplayServer::VSYNC_ENABLED : DisplayServer::VSYNC_DISABLED;
  4861. }
  4862. if (gl_manager_egl) {
  4863. return gl_manager_egl->is_using_vsync() ? DisplayServer::VSYNC_ENABLED : DisplayServer::VSYNC_DISABLED;
  4864. }
  4865. #endif
  4866. return DisplayServer::VSYNC_ENABLED;
  4867. }
  4868. void DisplayServerX11::window_start_drag(WindowID p_window) {
  4869. _THREAD_SAFE_METHOD_
  4870. ERR_FAIL_COND(!windows.has(p_window));
  4871. WindowData &wd = windows[p_window];
  4872. if (wd.embed_parent) {
  4873. return; // Embedded window.
  4874. }
  4875. XClientMessageEvent m;
  4876. memset(&m, 0, sizeof(m));
  4877. XUngrabPointer(x11_display, CurrentTime);
  4878. Window root_return, child_return;
  4879. int root_x, root_y, win_x, win_y;
  4880. unsigned int mask_return;
  4881. Bool xquerypointer_result = XQueryPointer(x11_display, wd.x11_window, &root_return, &child_return, &root_x, &root_y, &win_x, &win_y, &mask_return);
  4882. m.type = ClientMessage;
  4883. m.window = wd.x11_window;
  4884. m.message_type = XInternAtom(x11_display, "_NET_WM_MOVERESIZE", True);
  4885. m.format = 32;
  4886. if (xquerypointer_result) {
  4887. m.data.l[0] = root_x;
  4888. m.data.l[1] = root_y;
  4889. m.data.l[3] = Button1;
  4890. }
  4891. m.data.l[2] = _NET_WM_MOVERESIZE_MOVE;
  4892. m.data.l[4] = 1; // Source - normal application.
  4893. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&m);
  4894. XSync(x11_display, 0);
  4895. }
  4896. void DisplayServerX11::window_start_resize(WindowResizeEdge p_edge, WindowID p_window) {
  4897. _THREAD_SAFE_METHOD_
  4898. ERR_FAIL_INDEX(int(p_edge), WINDOW_EDGE_MAX);
  4899. ERR_FAIL_COND(!windows.has(p_window));
  4900. WindowData &wd = windows[p_window];
  4901. if (wd.embed_parent) {
  4902. return; // Embedded window.
  4903. }
  4904. XClientMessageEvent m;
  4905. memset(&m, 0, sizeof(m));
  4906. XUngrabPointer(x11_display, CurrentTime);
  4907. Window root_return, child_return;
  4908. int root_x, root_y, win_x, win_y;
  4909. unsigned int mask_return;
  4910. Bool xquerypointer_result = XQueryPointer(x11_display, wd.x11_window, &root_return, &child_return, &root_x, &root_y, &win_x, &win_y, &mask_return);
  4911. m.type = ClientMessage;
  4912. m.window = wd.x11_window;
  4913. m.message_type = XInternAtom(x11_display, "_NET_WM_MOVERESIZE", True);
  4914. m.format = 32;
  4915. if (xquerypointer_result) {
  4916. m.data.l[0] = root_x;
  4917. m.data.l[1] = root_y;
  4918. m.data.l[3] = Button1;
  4919. }
  4920. switch (p_edge) {
  4921. case DisplayServer::WINDOW_EDGE_TOP_LEFT: {
  4922. m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_TOPLEFT;
  4923. } break;
  4924. case DisplayServer::WINDOW_EDGE_TOP: {
  4925. m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_TOP;
  4926. } break;
  4927. case DisplayServer::WINDOW_EDGE_TOP_RIGHT: {
  4928. m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT;
  4929. } break;
  4930. case DisplayServer::WINDOW_EDGE_LEFT: {
  4931. m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_LEFT;
  4932. } break;
  4933. case DisplayServer::WINDOW_EDGE_RIGHT: {
  4934. m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_RIGHT;
  4935. } break;
  4936. case DisplayServer::WINDOW_EDGE_BOTTOM_LEFT: {
  4937. m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT;
  4938. } break;
  4939. case DisplayServer::WINDOW_EDGE_BOTTOM: {
  4940. m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_BOTTOM;
  4941. } break;
  4942. case DisplayServer::WINDOW_EDGE_BOTTOM_RIGHT: {
  4943. m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT;
  4944. } break;
  4945. default:
  4946. break;
  4947. }
  4948. m.data.l[4] = 1; // Source - normal application.
  4949. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&m);
  4950. XSync(x11_display, 0);
  4951. }
  4952. pid_t get_window_pid(Display *p_display, Window p_window) {
  4953. Atom atom = XInternAtom(p_display, "_NET_WM_PID", False);
  4954. Atom actualType;
  4955. int actualFormat;
  4956. unsigned long nItems, bytesAfter;
  4957. unsigned char *prop = nullptr;
  4958. if (XGetWindowProperty(p_display, p_window, atom, 0, sizeof(pid_t), False, AnyPropertyType,
  4959. &actualType, &actualFormat, &nItems, &bytesAfter, &prop) == Success) {
  4960. if (nItems > 0) {
  4961. pid_t pid = *(pid_t *)prop;
  4962. XFree(prop);
  4963. return pid;
  4964. }
  4965. }
  4966. return 0; // PID not found.
  4967. }
  4968. Window find_window_from_process_id_internal(Display *p_display, pid_t p_process_id, Window p_window) {
  4969. Window dummy;
  4970. Window *children;
  4971. unsigned int num_children;
  4972. if (!XQueryTree(p_display, p_window, &dummy, &dummy, &children, &num_children)) {
  4973. return 0;
  4974. }
  4975. for (unsigned int i = 0; i < num_children; i++) {
  4976. pid_t pid = get_window_pid(p_display, children[i]);
  4977. if (pid == p_process_id) {
  4978. return children[i];
  4979. }
  4980. }
  4981. // Then check children of children.
  4982. for (unsigned int i = 0; i < num_children; i++) {
  4983. Window wnd = find_window_from_process_id_internal(p_display, p_process_id, children[i]);
  4984. if (wnd != 0) {
  4985. return wnd;
  4986. }
  4987. }
  4988. if (children) {
  4989. XFree(children);
  4990. }
  4991. return 0;
  4992. }
  4993. Window find_window_from_process_id(Display *p_display, pid_t p_process_id) {
  4994. // Handle bad window errors silently because while looping
  4995. // windows can be destroyed, resulting in BadWindow errors.
  4996. int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&bad_window_error_handler);
  4997. const int screencount = XScreenCount(p_display);
  4998. Window process_window = 0;
  4999. for (int screen_index = 0; screen_index < screencount; screen_index++) {
  5000. Window root = RootWindow(p_display, screen_index);
  5001. Window wnd = find_window_from_process_id_internal(p_display, p_process_id, root);
  5002. if (wnd != 0) {
  5003. process_window = wnd;
  5004. break;
  5005. }
  5006. }
  5007. // Restore default error handler.
  5008. XSetErrorHandler(oldHandler);
  5009. return process_window;
  5010. }
  5011. Point2i DisplayServerX11::_get_window_position(Window p_window) const {
  5012. int x = 0, y = 0;
  5013. Window child;
  5014. XTranslateCoordinates(x11_display, p_window, DefaultRootWindow(x11_display), 0, 0, &x, &y, &child);
  5015. return Point2i(x, y);
  5016. }
  5017. Rect2i DisplayServerX11::_get_window_rect(Window p_window) const {
  5018. XWindowAttributes xwa;
  5019. XGetWindowAttributes(x11_display, p_window, &xwa);
  5020. return Rect2i(xwa.x, xwa.y, xwa.width, xwa.height);
  5021. }
  5022. void DisplayServerX11::_set_window_taskbar_pager_enabled(Window p_window, bool p_enabled) {
  5023. Atom wmState = XInternAtom(x11_display, "_NET_WM_STATE", False);
  5024. Atom skipTaskbar = XInternAtom(x11_display, "_NET_WM_STATE_SKIP_TASKBAR", False);
  5025. Atom skipPager = XInternAtom(x11_display, "_NET_WM_STATE_SKIP_PAGER", False);
  5026. XClientMessageEvent xev;
  5027. memset(&xev, 0, sizeof(xev));
  5028. xev.type = ClientMessage;
  5029. xev.window = p_window;
  5030. xev.message_type = wmState;
  5031. xev.format = 32;
  5032. xev.data.l[0] = p_enabled ? _NET_WM_STATE_REMOVE : _NET_WM_STATE_ADD; // When enabled, we must remove the skip.
  5033. xev.data.l[1] = skipTaskbar;
  5034. xev.data.l[2] = skipPager;
  5035. xev.data.l[3] = 0;
  5036. xev.data.l[4] = 0;
  5037. // Send the client message to the root window.
  5038. XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&xev);
  5039. }
  5040. Error DisplayServerX11::embed_process(WindowID p_window, OS::ProcessID p_pid, const Rect2i &p_rect, bool p_visible, bool p_grab_focus) {
  5041. _THREAD_SAFE_METHOD_
  5042. ERR_FAIL_COND_V(!windows.has(p_window), FAILED);
  5043. const WindowData &wd = windows[p_window];
  5044. DEBUG_LOG_X11("Starting embedding %ld to window %lu \n", p_pid, wd.x11_window);
  5045. EmbeddedProcessData *ep = nullptr;
  5046. if (embedded_processes.has(p_pid)) {
  5047. ep = embedded_processes.get(p_pid);
  5048. } else {
  5049. // New process, trying to find the window.
  5050. Window process_window = find_window_from_process_id(x11_display, p_pid);
  5051. if (!process_window) {
  5052. return ERR_DOES_NOT_EXIST;
  5053. }
  5054. DEBUG_LOG_X11("Process %ld window found: %lu \n", p_pid, process_window);
  5055. ep = memnew(EmbeddedProcessData);
  5056. ep->process_window = process_window;
  5057. ep->visible = true;
  5058. XSetTransientForHint(x11_display, process_window, wd.x11_window);
  5059. _set_window_taskbar_pager_enabled(process_window, false);
  5060. embedded_processes.insert(p_pid, ep);
  5061. }
  5062. // Handle bad window errors silently because just in case the embedded window was closed.
  5063. int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&bad_window_error_handler);
  5064. if (p_visible) {
  5065. // Resize and move the window to match the desired rectangle.
  5066. // X11 does not allow moving the window entirely outside the screen boundaries.
  5067. // To ensure the window remains visible, we will resize it to fit within both the screen and the specified rectangle.
  5068. Rect2i desired_rect = p_rect;
  5069. // First resize the desired rect to fit inside all the screens without considering the
  5070. // working area.
  5071. Rect2i screens_full_rect = _screens_get_full_rect();
  5072. Vector2i screens_full_end = screens_full_rect.get_end();
  5073. if (desired_rect.position.x < screens_full_rect.position.x) {
  5074. desired_rect.size.x = MAX(desired_rect.size.x - (screens_full_rect.position.x - desired_rect.position.x), 0);
  5075. desired_rect.position.x = screens_full_rect.position.x;
  5076. }
  5077. if (desired_rect.position.x + desired_rect.size.x > screens_full_end.x) {
  5078. desired_rect.size.x = MAX(screens_full_end.x - desired_rect.position.x, 0);
  5079. }
  5080. if (desired_rect.position.y < screens_full_rect.position.y) {
  5081. desired_rect.size.y = MAX(desired_rect.size.y - (screens_full_rect.position.y - desired_rect.position.y), 0);
  5082. desired_rect.position.y = screens_full_rect.position.y;
  5083. }
  5084. if (desired_rect.position.y + desired_rect.size.y > screens_full_end.y) {
  5085. desired_rect.size.y = MAX(screens_full_end.y - desired_rect.position.y, 0);
  5086. }
  5087. // Second, for each screen, check if the desired rectangle is within a portion of the screen
  5088. // that is outside the working area. Each screen can have a different working area
  5089. // depending on top, bottom, or side panels.
  5090. int desired_area = desired_rect.get_area();
  5091. int count = get_screen_count();
  5092. for (int i = 0; i < count; i++) {
  5093. Rect2i screen_rect = _screen_get_rect(i);
  5094. if (screen_rect.intersection(desired_rect).get_area() == 0) {
  5095. continue;
  5096. }
  5097. // The desired rect is inside this screen.
  5098. Rect2i screen_usable_rect = screen_get_usable_rect(i);
  5099. int screen_usable_area = screen_usable_rect.intersection(desired_rect).get_area();
  5100. if (screen_usable_area == desired_area) {
  5101. // The desired rect is fulling inside the usable rect of the screen. No need to resize.
  5102. continue;
  5103. }
  5104. if (desired_rect.position.x >= screen_rect.position.x && desired_rect.position.x < screen_usable_rect.position.x) {
  5105. int offset = screen_usable_rect.position.x - desired_rect.position.x;
  5106. desired_rect.size.x = MAX(desired_rect.size.x - offset, 0);
  5107. desired_rect.position.x += offset;
  5108. }
  5109. if (desired_rect.position.y >= screen_rect.position.y && desired_rect.position.y < screen_usable_rect.position.y) {
  5110. int offset = screen_usable_rect.position.y - desired_rect.position.y;
  5111. desired_rect.size.y = MAX(desired_rect.size.y - offset, 0);
  5112. desired_rect.position.y += offset;
  5113. }
  5114. Vector2i desired_end = desired_rect.get_end();
  5115. Vector2i screen_end = screen_rect.get_end();
  5116. Vector2i screen_usable_end = screen_usable_rect.get_end();
  5117. if (desired_end.x > screen_usable_end.x && desired_end.x <= screen_end.x) {
  5118. desired_rect.size.x = MAX(desired_rect.size.x - (desired_end.x - screen_usable_end.x), 0);
  5119. }
  5120. if (desired_end.y > screen_usable_end.y && desired_end.y <= screen_end.y) {
  5121. desired_rect.size.y = MAX(desired_rect.size.y - (desired_end.y - screen_usable_end.y), 0);
  5122. }
  5123. }
  5124. if (desired_rect.size.x <= 100 || desired_rect.size.y <= 100) {
  5125. p_visible = false;
  5126. }
  5127. if (p_visible) {
  5128. Rect2i current_process_window_rect = _get_window_rect(ep->process_window);
  5129. if (current_process_window_rect != desired_rect) {
  5130. DEBUG_LOG_X11("Embedding XMoveResizeWindow process %ld, window %lu to %d, %d, %d, %d \n", p_pid, wd.x11_window, desired_rect.position.x, desired_rect.position.y, desired_rect.size.x, desired_rect.size.y);
  5131. XMoveResizeWindow(x11_display, ep->process_window, desired_rect.position.x, desired_rect.position.y, desired_rect.size.x, desired_rect.size.y);
  5132. }
  5133. }
  5134. }
  5135. if (ep->visible != p_visible) {
  5136. if (p_visible) {
  5137. XMapWindow(x11_display, ep->process_window);
  5138. } else {
  5139. XUnmapWindow(x11_display, ep->process_window);
  5140. }
  5141. ep->visible = p_visible;
  5142. }
  5143. if (p_grab_focus && p_visible) {
  5144. Window focused_window = 0;
  5145. int revert_to = 0;
  5146. XGetInputFocus(x11_display, &focused_window, &revert_to);
  5147. if (focused_window != ep->process_window) {
  5148. // Be sure that the window is visible to prevent BadMatch error when calling XSetInputFocus on a not viewable window.
  5149. XWindowAttributes attr;
  5150. if (XGetWindowAttributes(x11_display, ep->process_window, &attr) && attr.map_state == IsViewable) {
  5151. XSetInputFocus(x11_display, ep->process_window, RevertToParent, CurrentTime);
  5152. }
  5153. }
  5154. }
  5155. // Restore default error handler.
  5156. XSetErrorHandler(oldHandler);
  5157. return OK;
  5158. }
  5159. Error DisplayServerX11::request_close_embedded_process(OS::ProcessID p_pid) {
  5160. _THREAD_SAFE_METHOD_
  5161. if (!embedded_processes.has(p_pid)) {
  5162. return ERR_DOES_NOT_EXIST;
  5163. }
  5164. EmbeddedProcessData *ep = embedded_processes.get(p_pid);
  5165. // Handle bad window errors silently because just in case the embedded window was closed.
  5166. int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&bad_window_error_handler);
  5167. // Check if the window is still valid.
  5168. XWindowAttributes attr;
  5169. if (XGetWindowAttributes(x11_display, ep->process_window, &attr)) {
  5170. // Send the message to gracefully close the window.
  5171. XEvent ev;
  5172. memset(&ev, 0, sizeof(ev));
  5173. ev.xclient.type = ClientMessage;
  5174. ev.xclient.window = ep->process_window;
  5175. ev.xclient.message_type = XInternAtom(x11_display, "WM_PROTOCOLS", True);
  5176. ev.xclient.format = 32;
  5177. ev.xclient.data.l[0] = XInternAtom(x11_display, "WM_DELETE_WINDOW", False);
  5178. ev.xclient.data.l[1] = CurrentTime;
  5179. XSendEvent(x11_display, ep->process_window, False, NoEventMask, &ev);
  5180. }
  5181. // Restore default error handler.
  5182. XSetErrorHandler(oldHandler);
  5183. return OK;
  5184. }
  5185. Error DisplayServerX11::remove_embedded_process(OS::ProcessID p_pid) {
  5186. _THREAD_SAFE_METHOD_
  5187. if (!embedded_processes.has(p_pid)) {
  5188. return ERR_DOES_NOT_EXIST;
  5189. }
  5190. EmbeddedProcessData *ep = embedded_processes.get(p_pid);
  5191. request_close_embedded_process(p_pid);
  5192. embedded_processes.erase(p_pid);
  5193. memdelete(ep);
  5194. return OK;
  5195. }
  5196. OS::ProcessID DisplayServerX11::get_focused_process_id() {
  5197. Window focused_window = 0;
  5198. int revert_to = 0;
  5199. XGetInputFocus(x11_display, &focused_window, &revert_to);
  5200. if (focused_window == None) {
  5201. return 0;
  5202. }
  5203. return get_window_pid(x11_display, focused_window);
  5204. }
  5205. Vector<String> DisplayServerX11::get_rendering_drivers_func() {
  5206. Vector<String> drivers;
  5207. #ifdef VULKAN_ENABLED
  5208. drivers.push_back("vulkan");
  5209. #endif
  5210. #ifdef GLES3_ENABLED
  5211. drivers.push_back("opengl3");
  5212. drivers.push_back("opengl3_es");
  5213. #endif
  5214. drivers.push_back("dummy");
  5215. return drivers;
  5216. }
  5217. DisplayServer *DisplayServerX11::create_func(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, int64_t p_parent_window, Error &r_error) {
  5218. DisplayServer *ds = memnew(DisplayServerX11(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_position, p_resolution, p_screen, p_context, p_parent_window, r_error));
  5219. return ds;
  5220. }
  5221. DisplayServerX11::WindowID DisplayServerX11::_create_window(WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Rect2i &p_rect, Window p_parent_window) {
  5222. //Create window
  5223. XVisualInfo visualInfo;
  5224. bool vi_selected = false;
  5225. #ifdef GLES3_ENABLED
  5226. if (gl_manager) {
  5227. Error err;
  5228. visualInfo = gl_manager->get_vi(x11_display, err);
  5229. ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Can't acquire visual info from display.");
  5230. vi_selected = true;
  5231. }
  5232. if (gl_manager_egl) {
  5233. XVisualInfo visual_info_template;
  5234. int visual_id = gl_manager_egl->display_get_native_visual_id(x11_display);
  5235. ERR_FAIL_COND_V_MSG(visual_id < 0, INVALID_WINDOW_ID, "Unable to get a visual id.");
  5236. visual_info_template.visualid = (VisualID)visual_id;
  5237. int number_of_visuals = 0;
  5238. XVisualInfo *vi_list = XGetVisualInfo(x11_display, VisualIDMask, &visual_info_template, &number_of_visuals);
  5239. ERR_FAIL_COND_V(number_of_visuals <= 0, INVALID_WINDOW_ID);
  5240. visualInfo = vi_list[0];
  5241. XFree(vi_list);
  5242. }
  5243. #endif
  5244. if (!vi_selected) {
  5245. long visualMask = VisualScreenMask;
  5246. int numberOfVisuals;
  5247. XVisualInfo vInfoTemplate = {};
  5248. vInfoTemplate.screen = DefaultScreen(x11_display);
  5249. XVisualInfo *vi_list = XGetVisualInfo(x11_display, visualMask, &vInfoTemplate, &numberOfVisuals);
  5250. ERR_FAIL_NULL_V(vi_list, INVALID_WINDOW_ID);
  5251. visualInfo = vi_list[0];
  5252. if (OS::get_singleton()->is_layered_allowed()) {
  5253. for (int i = 0; i < numberOfVisuals; i++) {
  5254. XRenderPictFormat *pict_format = XRenderFindVisualFormat(x11_display, vi_list[i].visual);
  5255. if (!pict_format) {
  5256. continue;
  5257. }
  5258. visualInfo = vi_list[i];
  5259. if (pict_format->direct.alphaMask > 0) {
  5260. break;
  5261. }
  5262. }
  5263. }
  5264. XFree(vi_list);
  5265. }
  5266. Colormap colormap = XCreateColormap(x11_display, RootWindow(x11_display, visualInfo.screen), visualInfo.visual, AllocNone);
  5267. XSetWindowAttributes windowAttributes = {};
  5268. windowAttributes.colormap = colormap;
  5269. windowAttributes.background_pixel = 0xFFFFFFFF;
  5270. windowAttributes.border_pixel = 0;
  5271. windowAttributes.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
  5272. unsigned long valuemask = CWBorderPixel | CWColormap | CWEventMask;
  5273. if (OS::get_singleton()->is_layered_allowed()) {
  5274. windowAttributes.background_pixmap = None;
  5275. windowAttributes.background_pixel = 0;
  5276. windowAttributes.border_pixmap = None;
  5277. valuemask |= CWBackPixel;
  5278. }
  5279. WindowID id = window_id_counter++;
  5280. WindowData &wd = windows[id];
  5281. if (p_flags & WINDOW_FLAG_NO_FOCUS_BIT) {
  5282. wd.no_focus = true;
  5283. }
  5284. if (p_flags & WINDOW_FLAG_POPUP_BIT) {
  5285. wd.is_popup = true;
  5286. }
  5287. // Setup for menu subwindows:
  5288. // - override_redirect forces the WM not to interfere with the window, to avoid delays due to
  5289. // handling decorations and placement.
  5290. // On the other hand, focus changes need to be handled manually when this is set.
  5291. // - save_under is a hint for the WM to keep the content of windows behind to avoid repaint.
  5292. if (wd.no_focus) {
  5293. windowAttributes.override_redirect = True;
  5294. windowAttributes.save_under = True;
  5295. valuemask |= CWOverrideRedirect | CWSaveUnder;
  5296. }
  5297. int rq_screen = get_screen_from_rect(p_rect);
  5298. if (rq_screen < 0) {
  5299. rq_screen = get_primary_screen(); // Requested window rect is outside any screen bounds.
  5300. }
  5301. Rect2i win_rect = p_rect;
  5302. if (!p_parent_window) {
  5303. // No parent.
  5304. if (p_mode == WINDOW_MODE_FULLSCREEN || p_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN) {
  5305. Rect2i screen_rect = Rect2i(screen_get_position(rq_screen), screen_get_size(rq_screen));
  5306. win_rect = screen_rect;
  5307. } else {
  5308. Rect2i srect = screen_get_usable_rect(rq_screen);
  5309. Point2i wpos = p_rect.position;
  5310. wpos = wpos.clamp(srect.position, srect.position + srect.size - p_rect.size / 3);
  5311. win_rect.position = wpos;
  5312. }
  5313. }
  5314. // Position and size hints are set from these values before they are updated to the actual
  5315. // window size, so we need to initialize them here.
  5316. wd.position = win_rect.position;
  5317. wd.size = win_rect.size;
  5318. {
  5319. wd.x11_window = XCreateWindow(x11_display, RootWindow(x11_display, visualInfo.screen), win_rect.position.x, win_rect.position.y, win_rect.size.width > 0 ? win_rect.size.width : 1, win_rect.size.height > 0 ? win_rect.size.height : 1, 0, visualInfo.depth, InputOutput, visualInfo.visual, valuemask, &windowAttributes);
  5320. wd.parent = RootWindow(x11_display, visualInfo.screen);
  5321. DEBUG_LOG_X11("CreateWindow window=%lu, parent: %lu \n", wd.x11_window, wd.parent);
  5322. if (p_parent_window) {
  5323. wd.embed_parent = p_parent_window;
  5324. XSetTransientForHint(x11_display, wd.x11_window, p_parent_window);
  5325. }
  5326. XSetWindowAttributes window_attributes_ime = {};
  5327. window_attributes_ime.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
  5328. wd.x11_xim_window = XCreateWindow(x11_display, wd.x11_window, 0, 0, 1, 1, 0, CopyFromParent, InputOnly, CopyFromParent, CWEventMask, &window_attributes_ime);
  5329. #ifdef XKB_ENABLED
  5330. if (dead_tbl && xkb_loaded_v05p) {
  5331. wd.xkb_state = xkb_compose_state_new(dead_tbl, XKB_COMPOSE_STATE_NO_FLAGS);
  5332. }
  5333. #endif
  5334. #ifdef ACCESSKIT_ENABLED
  5335. if (accessibility_driver && !accessibility_driver->window_create(id, nullptr)) {
  5336. if (OS::get_singleton()->is_stdout_verbose()) {
  5337. ERR_PRINT("Can't create an accessibility adapter for window, accessibility support disabled!");
  5338. }
  5339. memdelete(accessibility_driver);
  5340. accessibility_driver = nullptr;
  5341. }
  5342. #endif
  5343. // Enable receiving notification when the window is initialized (MapNotify)
  5344. // so the focus can be set at the right time.
  5345. if (!wd.no_focus && !wd.is_popup) {
  5346. XSelectInput(x11_display, wd.x11_window, StructureNotifyMask);
  5347. }
  5348. //associate PID
  5349. // make PID known to X11
  5350. {
  5351. const long pid = OS::get_singleton()->get_process_id();
  5352. Atom net_wm_pid = XInternAtom(x11_display, "_NET_WM_PID", False);
  5353. if (net_wm_pid != None) {
  5354. XChangeProperty(x11_display, wd.x11_window, net_wm_pid, XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&pid, 1);
  5355. }
  5356. }
  5357. long im_event_mask = 0;
  5358. {
  5359. XIEventMask all_event_mask;
  5360. XSetWindowAttributes new_attr;
  5361. new_attr.event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask |
  5362. ButtonReleaseMask | EnterWindowMask |
  5363. LeaveWindowMask | PointerMotionMask |
  5364. Button1MotionMask |
  5365. Button2MotionMask | Button3MotionMask |
  5366. Button4MotionMask | Button5MotionMask |
  5367. ButtonMotionMask | KeymapStateMask |
  5368. ExposureMask | VisibilityChangeMask |
  5369. StructureNotifyMask |
  5370. SubstructureNotifyMask | SubstructureRedirectMask |
  5371. FocusChangeMask | PropertyChangeMask |
  5372. ColormapChangeMask | OwnerGrabButtonMask |
  5373. im_event_mask;
  5374. XChangeWindowAttributes(x11_display, wd.x11_window, CWEventMask, &new_attr);
  5375. static unsigned char all_mask_data[XIMaskLen(XI_LASTEVENT)] = {};
  5376. all_event_mask.deviceid = XIAllDevices;
  5377. all_event_mask.mask_len = sizeof(all_mask_data);
  5378. all_event_mask.mask = all_mask_data;
  5379. XISetMask(all_event_mask.mask, XI_HierarchyChanged);
  5380. #ifdef TOUCH_ENABLED
  5381. if (xi.touch_devices.size()) {
  5382. XISetMask(all_event_mask.mask, XI_TouchBegin);
  5383. XISetMask(all_event_mask.mask, XI_TouchUpdate);
  5384. XISetMask(all_event_mask.mask, XI_TouchEnd);
  5385. XISetMask(all_event_mask.mask, XI_TouchOwnership);
  5386. }
  5387. #endif
  5388. XISelectEvents(x11_display, wd.x11_window, &all_event_mask, 1);
  5389. }
  5390. /* set the titlebar name */
  5391. XStoreName(x11_display, wd.x11_window, "Godot");
  5392. XSetWMProtocols(x11_display, wd.x11_window, &wm_delete, 1);
  5393. if (xdnd_aware != None) {
  5394. XChangeProperty(x11_display, wd.x11_window, xdnd_aware, XA_ATOM, 32, PropModeReplace, (unsigned char *)&xdnd_version, 1);
  5395. }
  5396. if (xim && xim_style) {
  5397. // Block events polling while changing input focus
  5398. // because it triggers some event polling internally.
  5399. MutexLock mutex_lock(events_mutex);
  5400. // Force on-the-spot for the over-the-spot style.
  5401. if ((xim_style & XIMPreeditPosition) != 0) {
  5402. xim_style &= ~XIMPreeditPosition;
  5403. xim_style |= XIMPreeditCallbacks;
  5404. }
  5405. if ((xim_style & XIMPreeditCallbacks) != 0) {
  5406. ::XIMCallback preedit_start_callback;
  5407. preedit_start_callback.client_data = (::XPointer)(this);
  5408. preedit_start_callback.callback = (::XIMProc)(void *)(_xim_preedit_start_callback);
  5409. ::XIMCallback preedit_done_callback;
  5410. preedit_done_callback.client_data = (::XPointer)(this);
  5411. preedit_done_callback.callback = (::XIMProc)(_xim_preedit_done_callback);
  5412. ::XIMCallback preedit_draw_callback;
  5413. preedit_draw_callback.client_data = (::XPointer)(this);
  5414. preedit_draw_callback.callback = (::XIMProc)(_xim_preedit_draw_callback);
  5415. ::XIMCallback preedit_caret_callback;
  5416. preedit_caret_callback.client_data = (::XPointer)(this);
  5417. preedit_caret_callback.callback = (::XIMProc)(_xim_preedit_caret_callback);
  5418. ::XVaNestedList preedit_attributes = XVaCreateNestedList(0,
  5419. XNPreeditStartCallback, &preedit_start_callback,
  5420. XNPreeditDoneCallback, &preedit_done_callback,
  5421. XNPreeditDrawCallback, &preedit_draw_callback,
  5422. XNPreeditCaretCallback, &preedit_caret_callback,
  5423. (char *)nullptr);
  5424. wd.xic = XCreateIC(xim,
  5425. XNInputStyle, xim_style,
  5426. XNClientWindow, wd.x11_xim_window,
  5427. XNFocusWindow, wd.x11_xim_window,
  5428. XNPreeditAttributes, preedit_attributes,
  5429. (char *)nullptr);
  5430. XFree(preedit_attributes);
  5431. } else {
  5432. wd.xic = XCreateIC(xim,
  5433. XNInputStyle, xim_style,
  5434. XNClientWindow, wd.x11_xim_window,
  5435. XNFocusWindow, wd.x11_xim_window,
  5436. (char *)nullptr);
  5437. }
  5438. if (XGetICValues(wd.xic, XNFilterEvents, &im_event_mask, nullptr) != nullptr) {
  5439. WARN_PRINT("XGetICValues couldn't obtain XNFilterEvents value");
  5440. XDestroyIC(wd.xic);
  5441. wd.xic = nullptr;
  5442. }
  5443. if (wd.xic) {
  5444. XUnsetICFocus(wd.xic);
  5445. } else {
  5446. WARN_PRINT("XCreateIC couldn't create wd.xic");
  5447. }
  5448. } else {
  5449. wd.xic = nullptr;
  5450. WARN_PRINT("XCreateIC couldn't create wd.xic");
  5451. }
  5452. _update_context(wd);
  5453. if (p_flags & WINDOW_FLAG_BORDERLESS_BIT) {
  5454. Hints hints;
  5455. Atom property;
  5456. hints.flags = 2;
  5457. hints.decorations = 0;
  5458. property = XInternAtom(x11_display, "_MOTIF_WM_HINTS", True);
  5459. if (property != None) {
  5460. XChangeProperty(x11_display, wd.x11_window, property, property, 32, PropModeReplace, (unsigned char *)&hints, 5);
  5461. }
  5462. }
  5463. if (wd.is_popup || wd.no_focus || (wd.embed_parent && !kde5_embed_workaround)) {
  5464. // Set Utility type to disable fade animations.
  5465. Atom type_atom = XInternAtom(x11_display, "_NET_WM_WINDOW_TYPE_UTILITY", False);
  5466. Atom wt_atom = XInternAtom(x11_display, "_NET_WM_WINDOW_TYPE", False);
  5467. if (wt_atom != None && type_atom != None) {
  5468. XChangeProperty(x11_display, wd.x11_window, wt_atom, XA_ATOM, 32, PropModeReplace, (unsigned char *)&type_atom, 1);
  5469. }
  5470. } else {
  5471. Atom type_atom = XInternAtom(x11_display, "_NET_WM_WINDOW_TYPE_NORMAL", False);
  5472. Atom wt_atom = XInternAtom(x11_display, "_NET_WM_WINDOW_TYPE", False);
  5473. if (wt_atom != None && type_atom != None) {
  5474. XChangeProperty(x11_display, wd.x11_window, wt_atom, XA_ATOM, 32, PropModeReplace, (unsigned char *)&type_atom, 1);
  5475. }
  5476. }
  5477. if (p_parent_window) {
  5478. // Disable the window in the taskbar and alt-tab.
  5479. _set_window_taskbar_pager_enabled(wd.x11_window, false);
  5480. }
  5481. _update_size_hints(id);
  5482. #if defined(RD_ENABLED)
  5483. if (rendering_context) {
  5484. union {
  5485. #ifdef VULKAN_ENABLED
  5486. RenderingContextDriverVulkanX11::WindowPlatformData vulkan;
  5487. #endif
  5488. } wpd;
  5489. #ifdef VULKAN_ENABLED
  5490. if (rendering_driver == "vulkan") {
  5491. wpd.vulkan.window = wd.x11_window;
  5492. wpd.vulkan.display = x11_display;
  5493. }
  5494. #endif
  5495. Error err = rendering_context->window_create(id, &wpd);
  5496. ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, vformat("Can't create a %s window", rendering_driver));
  5497. rendering_context->window_set_size(id, win_rect.size.width, win_rect.size.height);
  5498. rendering_context->window_set_vsync_mode(id, p_vsync_mode);
  5499. }
  5500. #endif
  5501. #ifdef GLES3_ENABLED
  5502. if (gl_manager) {
  5503. Error err = gl_manager->window_create(id, wd.x11_window, x11_display, win_rect.size.width, win_rect.size.height);
  5504. ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Can't create an OpenGL window");
  5505. }
  5506. if (gl_manager_egl) {
  5507. Error err = gl_manager_egl->window_create(id, x11_display, &wd.x11_window, win_rect.size.width, win_rect.size.height);
  5508. ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Failed to create an OpenGLES window.");
  5509. }
  5510. window_set_vsync_mode(p_vsync_mode, id);
  5511. #endif
  5512. //set_class_hint(x11_display, wd.x11_window);
  5513. XFlush(x11_display);
  5514. XSync(x11_display, False);
  5515. //XSetErrorHandler(oldHandler);
  5516. }
  5517. window_set_mode(p_mode, id);
  5518. //sync size
  5519. {
  5520. XWindowAttributes xwa;
  5521. XSync(x11_display, False);
  5522. XGetWindowAttributes(x11_display, wd.x11_window, &xwa);
  5523. wd.position.x = xwa.x;
  5524. wd.position.y = xwa.y;
  5525. wd.size.width = xwa.width;
  5526. wd.size.height = xwa.height;
  5527. }
  5528. //set cursor
  5529. if (cursors[current_cursor] != None) {
  5530. XDefineCursor(x11_display, wd.x11_window, cursors[current_cursor]);
  5531. }
  5532. return id;
  5533. }
  5534. static bool _is_xim_style_supported(const ::XIMStyle &p_style) {
  5535. const ::XIMStyle supported_preedit = XIMPreeditCallbacks | XIMPreeditPosition | XIMPreeditNothing | XIMPreeditNone;
  5536. const ::XIMStyle supported_status = XIMStatusNothing | XIMStatusNone;
  5537. // Check preedit style is supported
  5538. if ((p_style & supported_preedit) == 0) {
  5539. return false;
  5540. }
  5541. // Check status style is supported
  5542. if ((p_style & supported_status) == 0) {
  5543. return false;
  5544. }
  5545. return true;
  5546. }
  5547. static ::XIMStyle _get_best_xim_style(const ::XIMStyle &p_style_a, const ::XIMStyle &p_style_b) {
  5548. if (p_style_a == 0) {
  5549. return p_style_b;
  5550. }
  5551. if (p_style_b == 0) {
  5552. return p_style_a;
  5553. }
  5554. const ::XIMStyle preedit = XIMPreeditArea | XIMPreeditCallbacks | XIMPreeditPosition | XIMPreeditNothing | XIMPreeditNone;
  5555. const ::XIMStyle status = XIMStatusArea | XIMStatusCallbacks | XIMStatusNothing | XIMStatusNone;
  5556. ::XIMStyle a = p_style_a & preedit;
  5557. ::XIMStyle b = p_style_b & preedit;
  5558. if (a != b) {
  5559. // Compare preedit styles.
  5560. if ((a | b) & XIMPreeditCallbacks) {
  5561. return a == XIMPreeditCallbacks ? p_style_a : p_style_b;
  5562. } else if ((a | b) & XIMPreeditPosition) {
  5563. return a == XIMPreeditPosition ? p_style_a : p_style_b;
  5564. } else if ((a | b) & XIMPreeditArea) {
  5565. return a == XIMPreeditArea ? p_style_a : p_style_b;
  5566. } else if ((a | b) & XIMPreeditNothing) {
  5567. return a == XIMPreeditNothing ? p_style_a : p_style_b;
  5568. }
  5569. } else {
  5570. // Preedit styles are the same, compare status styles.
  5571. a = p_style_a & status;
  5572. b = p_style_b & status;
  5573. if ((a | b) & XIMStatusCallbacks) {
  5574. return a == XIMStatusCallbacks ? p_style_a : p_style_b;
  5575. } else if ((a | b) & XIMStatusArea) {
  5576. return a == XIMStatusArea ? p_style_a : p_style_b;
  5577. } else if ((a | b) & XIMStatusNothing) {
  5578. return a == XIMStatusNothing ? p_style_a : p_style_b;
  5579. }
  5580. }
  5581. return p_style_a;
  5582. }
  5583. DisplayServerX11::DisplayServerX11(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, int64_t p_parent_window, Error &r_error) {
  5584. KeyMappingX11::initialize();
  5585. String current_desk = OS::get_singleton()->get_environment("XDG_CURRENT_DESKTOP").to_lower();
  5586. String session_desk = OS::get_singleton()->get_environment("XDG_SESSION_DESKTOP").to_lower();
  5587. swap_cancel_ok = (current_desk.contains("kde") || session_desk.contains("kde") || current_desk.contains("lxqt") || session_desk.contains("lxqt"));
  5588. xwayland = OS::get_singleton()->get_environment("XDG_SESSION_TYPE").to_lower() == "wayland";
  5589. kde5_embed_workaround = current_desk == "kde" && OS::get_singleton()->get_environment("KDE_SESSION_VERSION") == "5";
  5590. native_menu = memnew(NativeMenu);
  5591. context = p_context;
  5592. #ifdef SOWRAP_ENABLED
  5593. #ifdef DEBUG_ENABLED
  5594. int dylibloader_verbose = 1;
  5595. #else
  5596. int dylibloader_verbose = 0;
  5597. #endif
  5598. if (initialize_xlib(dylibloader_verbose) != 0) {
  5599. r_error = ERR_UNAVAILABLE;
  5600. ERR_FAIL_MSG("Can't load Xlib dynamically.");
  5601. }
  5602. if (initialize_xcursor(dylibloader_verbose) != 0) {
  5603. r_error = ERR_UNAVAILABLE;
  5604. ERR_FAIL_MSG("Can't load XCursor dynamically.");
  5605. }
  5606. #ifdef XKB_ENABLED
  5607. bool xkb_loaded = (initialize_xkbcommon(dylibloader_verbose) == 0);
  5608. xkb_loaded_v05p = xkb_loaded;
  5609. if (!xkb_context_new || !xkb_compose_table_new_from_locale || !xkb_compose_table_unref || !xkb_context_unref || !xkb_compose_state_feed || !xkb_compose_state_unref || !xkb_compose_state_new || !xkb_compose_state_get_status || !xkb_compose_state_get_utf8) {
  5610. xkb_loaded_v05p = false;
  5611. print_verbose("Detected XKBcommon library version older than 0.5, dead key composition and Unicode key labels disabled.");
  5612. }
  5613. xkb_loaded_v08p = xkb_loaded;
  5614. if (!xkb_keysym_to_utf32 || !xkb_keysym_to_upper) {
  5615. xkb_loaded_v08p = false;
  5616. print_verbose("Detected XKBcommon library version older than 0.8, Unicode key labels disabled.");
  5617. }
  5618. #endif
  5619. if (initialize_xext(dylibloader_verbose) != 0) {
  5620. r_error = ERR_UNAVAILABLE;
  5621. ERR_FAIL_MSG("Can't load Xext dynamically.");
  5622. }
  5623. if (initialize_xinerama(dylibloader_verbose) != 0) {
  5624. xinerama_ext_ok = false;
  5625. }
  5626. if (initialize_xrandr(dylibloader_verbose) != 0) {
  5627. xrandr_ext_ok = false;
  5628. }
  5629. if (initialize_xrender(dylibloader_verbose) != 0) {
  5630. r_error = ERR_UNAVAILABLE;
  5631. ERR_FAIL_MSG("Can't load Xrender dynamically.");
  5632. }
  5633. if (initialize_xinput2(dylibloader_verbose) != 0) {
  5634. r_error = ERR_UNAVAILABLE;
  5635. ERR_FAIL_MSG("Can't load Xinput2 dynamically.");
  5636. }
  5637. #else
  5638. #ifdef XKB_ENABLED
  5639. bool xkb_loaded = true;
  5640. xkb_loaded_v05p = true;
  5641. xkb_loaded_v08p = true;
  5642. #endif
  5643. #endif
  5644. #ifdef XKB_ENABLED
  5645. if (xkb_loaded) {
  5646. xkb_ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
  5647. if (xkb_ctx) {
  5648. const char *locale = getenv("LC_ALL");
  5649. if (!locale || !*locale) {
  5650. locale = getenv("LC_CTYPE");
  5651. }
  5652. if (!locale || !*locale) {
  5653. locale = getenv("LANG");
  5654. }
  5655. if (!locale || !*locale) {
  5656. locale = "C";
  5657. }
  5658. dead_tbl = xkb_compose_table_new_from_locale(xkb_ctx, locale, XKB_COMPOSE_COMPILE_NO_FLAGS);
  5659. }
  5660. }
  5661. #endif
  5662. Input::get_singleton()->set_event_dispatch_function(_dispatch_input_events);
  5663. r_error = OK;
  5664. #ifdef SOWRAP_ENABLED
  5665. {
  5666. if (!XcursorImageCreate || !XcursorImageLoadCursor || !XcursorImageDestroy || !XcursorGetDefaultSize || !XcursorGetTheme || !XcursorLibraryLoadImage) {
  5667. // There's no API to check version, check if functions are available instead.
  5668. ERR_PRINT("Unsupported Xcursor library version.");
  5669. r_error = ERR_UNAVAILABLE;
  5670. return;
  5671. }
  5672. }
  5673. #endif
  5674. for (int i = 0; i < CURSOR_MAX; i++) {
  5675. cursors[i] = None;
  5676. cursor_img[i] = nullptr;
  5677. }
  5678. XInitThreads(); //always use threads
  5679. /** XLIB INITIALIZATION **/
  5680. x11_display = XOpenDisplay(nullptr);
  5681. if (!x11_display) {
  5682. ERR_PRINT("X11 Display is not available");
  5683. r_error = ERR_UNAVAILABLE;
  5684. return;
  5685. }
  5686. if (xshaped_ext_ok) {
  5687. int version_major = 0;
  5688. int version_minor = 0;
  5689. int rc = XShapeQueryVersion(x11_display, &version_major, &version_minor);
  5690. print_verbose(vformat("Xshape %d.%d detected.", version_major, version_minor));
  5691. if (rc != 1 || version_major < 1) {
  5692. xshaped_ext_ok = false;
  5693. print_verbose("Unsupported Xshape library version.");
  5694. }
  5695. }
  5696. if (xinerama_ext_ok) {
  5697. int version_major = 0;
  5698. int version_minor = 0;
  5699. int rc = XineramaQueryVersion(x11_display, &version_major, &version_minor);
  5700. print_verbose(vformat("Xinerama %d.%d detected.", version_major, version_minor));
  5701. if (rc != 1 || version_major < 1) {
  5702. xinerama_ext_ok = false;
  5703. print_verbose("Unsupported Xinerama library version.");
  5704. }
  5705. }
  5706. if (xrandr_ext_ok) {
  5707. int version_major = 0;
  5708. int version_minor = 0;
  5709. int rc = XRRQueryVersion(x11_display, &version_major, &version_minor);
  5710. print_verbose(vformat("Xrandr %d.%d detected.", version_major, version_minor));
  5711. if (rc != 1 || (version_major == 1 && version_minor < 3) || (version_major < 1)) {
  5712. xrandr_ext_ok = false;
  5713. print_verbose("Unsupported Xrandr library version.");
  5714. }
  5715. }
  5716. {
  5717. int version_major = 0;
  5718. int version_minor = 0;
  5719. int rc = XRenderQueryVersion(x11_display, &version_major, &version_minor);
  5720. print_verbose(vformat("Xrender %d.%d detected.", version_major, version_minor));
  5721. if (rc != 1 || (version_major == 0 && version_minor < 11)) {
  5722. ERR_PRINT("Unsupported Xrender library version.");
  5723. r_error = ERR_UNAVAILABLE;
  5724. XCloseDisplay(x11_display);
  5725. return;
  5726. }
  5727. }
  5728. {
  5729. int version_major = 2; // Report 2.2 as supported by engine, but should work with 2.1 or 2.0 library as well.
  5730. int version_minor = 2;
  5731. int rc = XIQueryVersion(x11_display, &version_major, &version_minor);
  5732. print_verbose(vformat("Xinput %d.%d detected.", version_major, version_minor));
  5733. if (rc != Success || (version_major < 2)) {
  5734. ERR_PRINT("Unsupported Xinput2 library version.");
  5735. r_error = ERR_UNAVAILABLE;
  5736. XCloseDisplay(x11_display);
  5737. return;
  5738. }
  5739. }
  5740. char *modifiers = nullptr;
  5741. Bool xkb_dar = False;
  5742. XAutoRepeatOn(x11_display);
  5743. xkb_dar = XkbSetDetectableAutoRepeat(x11_display, True, nullptr);
  5744. // Try to support IME if detectable auto-repeat is supported
  5745. if (xkb_dar == True) {
  5746. #ifdef X_HAVE_UTF8_STRING
  5747. // Xutf8LookupString will be used later instead of XmbLookupString before
  5748. // the multibyte sequences can be converted to unicode string.
  5749. modifiers = XSetLocaleModifiers("");
  5750. #endif
  5751. }
  5752. if (modifiers == nullptr) {
  5753. if (OS::get_singleton()->is_stdout_verbose()) {
  5754. WARN_PRINT("IME is disabled");
  5755. }
  5756. XSetLocaleModifiers("@im=none");
  5757. WARN_PRINT("Error setting locale modifiers");
  5758. }
  5759. const char *err;
  5760. int xrandr_major = 0;
  5761. int xrandr_minor = 0;
  5762. int event_base, error_base;
  5763. xrandr_ext_ok = XRRQueryExtension(x11_display, &event_base, &error_base);
  5764. xrandr_handle = dlopen("libXrandr.so.2", RTLD_LAZY);
  5765. if (!xrandr_handle) {
  5766. err = dlerror();
  5767. // For some arcane reason, NetBSD now ships libXrandr.so.3 while the rest of the world has libXrandr.so.2...
  5768. // In case this happens for other X11 platforms in the future, let's give it a try too before failing.
  5769. xrandr_handle = dlopen("libXrandr.so.3", RTLD_LAZY);
  5770. if (!xrandr_handle) {
  5771. fprintf(stderr, "could not load libXrandr.so.2, Error: %s\n", err);
  5772. }
  5773. }
  5774. if (xrandr_handle) {
  5775. XRRQueryVersion(x11_display, &xrandr_major, &xrandr_minor);
  5776. if (((xrandr_major << 8) | xrandr_minor) >= 0x0105) {
  5777. xrr_get_monitors = (xrr_get_monitors_t)dlsym(xrandr_handle, "XRRGetMonitors");
  5778. if (!xrr_get_monitors) {
  5779. err = dlerror();
  5780. fprintf(stderr, "could not find symbol XRRGetMonitors\nError: %s\n", err);
  5781. } else {
  5782. xrr_free_monitors = (xrr_free_monitors_t)dlsym(xrandr_handle, "XRRFreeMonitors");
  5783. if (!xrr_free_monitors) {
  5784. err = dlerror();
  5785. fprintf(stderr, "could not find XRRFreeMonitors\nError: %s\n", err);
  5786. xrr_get_monitors = nullptr;
  5787. }
  5788. }
  5789. }
  5790. }
  5791. if (!_refresh_device_info()) {
  5792. OS::get_singleton()->alert("Your system does not support XInput 2.\n"
  5793. "Please upgrade your distribution.",
  5794. "Unable to initialize XInput");
  5795. r_error = ERR_UNAVAILABLE;
  5796. return;
  5797. }
  5798. xim = XOpenIM(x11_display, nullptr, nullptr, nullptr);
  5799. if (xim == nullptr) {
  5800. WARN_PRINT("XOpenIM failed");
  5801. xim_style = 0L;
  5802. } else {
  5803. ::XIMCallback im_destroy_callback;
  5804. im_destroy_callback.client_data = (::XPointer)(this);
  5805. im_destroy_callback.callback = (::XIMProc)(_xim_destroy_callback);
  5806. if (XSetIMValues(xim, XNDestroyCallback, &im_destroy_callback,
  5807. nullptr) != nullptr) {
  5808. WARN_PRINT("Error setting XIM destroy callback");
  5809. }
  5810. ::XIMStyles *xim_styles = nullptr;
  5811. xim_style = 0L;
  5812. char *imvalret = XGetIMValues(xim, XNQueryInputStyle, &xim_styles, nullptr);
  5813. if (imvalret != nullptr || xim_styles == nullptr) {
  5814. fprintf(stderr, "Input method doesn't support any styles\n");
  5815. }
  5816. if (xim_styles) {
  5817. xim_style = 0L;
  5818. for (int i = 0; i < xim_styles->count_styles; i++) {
  5819. const ::XIMStyle &style = xim_styles->supported_styles[i];
  5820. if (!_is_xim_style_supported(style)) {
  5821. continue;
  5822. }
  5823. xim_style = _get_best_xim_style(xim_style, style);
  5824. }
  5825. XFree(xim_styles);
  5826. }
  5827. XFree(imvalret);
  5828. }
  5829. /* Atom internment */
  5830. wm_delete = XInternAtom(x11_display, "WM_DELETE_WINDOW", true);
  5831. // Set Xdnd (drag & drop) support.
  5832. xdnd_aware = XInternAtom(x11_display, "XdndAware", False);
  5833. xdnd_enter = XInternAtom(x11_display, "XdndEnter", False);
  5834. xdnd_position = XInternAtom(x11_display, "XdndPosition", False);
  5835. xdnd_status = XInternAtom(x11_display, "XdndStatus", False);
  5836. xdnd_action_copy = XInternAtom(x11_display, "XdndActionCopy", False);
  5837. xdnd_drop = XInternAtom(x11_display, "XdndDrop", False);
  5838. xdnd_finished = XInternAtom(x11_display, "XdndFinished", False);
  5839. xdnd_selection = XInternAtom(x11_display, "XdndSelection", False);
  5840. #ifdef SPEECHD_ENABLED
  5841. // Init TTS
  5842. bool tts_enabled = GLOBAL_GET("audio/general/text_to_speech");
  5843. if (tts_enabled) {
  5844. initialize_tts();
  5845. }
  5846. #endif
  5847. #ifdef ACCESSKIT_ENABLED
  5848. if (accessibility_get_mode() != DisplayServer::AccessibilityMode::ACCESSIBILITY_DISABLED) {
  5849. accessibility_driver = memnew(AccessibilityDriverAccessKit);
  5850. if (accessibility_driver->init() != OK) {
  5851. memdelete(accessibility_driver);
  5852. accessibility_driver = nullptr;
  5853. }
  5854. }
  5855. #endif
  5856. //!!!!!!!!!!!!!!!!!!!!!!!!!!
  5857. //TODO - do Vulkan and OpenGL support checks, driver selection and fallback
  5858. rendering_driver = p_rendering_driver;
  5859. bool driver_found = false;
  5860. String executable_name = OS::get_singleton()->get_executable_path().get_file();
  5861. // Initialize context and rendering device.
  5862. if (rendering_driver == "dummy") {
  5863. RasterizerDummy::make_current();
  5864. driver_found = true;
  5865. }
  5866. #if defined(RD_ENABLED)
  5867. #if defined(VULKAN_ENABLED)
  5868. if (rendering_driver == "vulkan") {
  5869. rendering_context = memnew(RenderingContextDriverVulkanX11);
  5870. }
  5871. #endif // VULKAN_ENABLED
  5872. if (rendering_context) {
  5873. if (rendering_context->initialize() != OK) {
  5874. memdelete(rendering_context);
  5875. rendering_context = nullptr;
  5876. #if defined(GLES3_ENABLED)
  5877. bool fallback_to_opengl3 = GLOBAL_GET("rendering/rendering_device/fallback_to_opengl3");
  5878. if (fallback_to_opengl3 && rendering_driver != "opengl3") {
  5879. WARN_PRINT("Your video card drivers seem not to support the required Vulkan version, switching to OpenGL 3.");
  5880. rendering_driver = "opengl3";
  5881. OS::get_singleton()->set_current_rendering_method("gl_compatibility");
  5882. OS::get_singleton()->set_current_rendering_driver_name(rendering_driver);
  5883. } else
  5884. #endif // GLES3_ENABLED
  5885. {
  5886. r_error = ERR_CANT_CREATE;
  5887. if (p_rendering_driver == "vulkan") {
  5888. OS::get_singleton()->alert(
  5889. vformat("Your video card drivers seem not to support the required Vulkan version.\n\n"
  5890. "If possible, consider updating your video card drivers or using the OpenGL 3 driver.\n\n"
  5891. "You can enable the OpenGL 3 driver by starting the engine from the\n"
  5892. "command line with the command:\n\n \"%s\" --rendering-driver opengl3\n\n"
  5893. "If you recently updated your video card drivers, try rebooting.",
  5894. executable_name),
  5895. "Unable to initialize Vulkan video driver");
  5896. }
  5897. ERR_FAIL_MSG(vformat("Could not initialize %s", rendering_driver));
  5898. }
  5899. }
  5900. driver_found = true;
  5901. }
  5902. #endif // RD_ENABLED
  5903. #if defined(GLES3_ENABLED)
  5904. if (rendering_driver == "opengl3" || rendering_driver == "opengl3_es") {
  5905. if (getenv("DRI_PRIME") == nullptr) {
  5906. int use_prime = -1;
  5907. if (getenv("PRIMUS_DISPLAY") ||
  5908. getenv("PRIMUS_libGLd") ||
  5909. getenv("PRIMUS_libGLa") ||
  5910. getenv("PRIMUS_libGL") ||
  5911. getenv("PRIMUS_LOAD_GLOBAL") ||
  5912. getenv("BUMBLEBEE_SOCKET")) {
  5913. print_verbose("Optirun/primusrun detected. Skipping GPU detection");
  5914. use_prime = 0;
  5915. }
  5916. // Some tools use fake libGL libraries and have them override the real one using
  5917. // LD_LIBRARY_PATH, so we skip them. *But* Steam also sets LD_LIBRARY_PATH for its
  5918. // runtime and includes system `/lib` and `/lib64`... so ignore Steam.
  5919. if (use_prime == -1 && getenv("LD_LIBRARY_PATH") && !getenv("STEAM_RUNTIME_LIBRARY_PATH")) {
  5920. String ld_library_path(getenv("LD_LIBRARY_PATH"));
  5921. Vector<String> libraries = ld_library_path.split(":");
  5922. for (int i = 0; i < libraries.size(); ++i) {
  5923. if (FileAccess::exists(libraries[i] + "/libGL.so.1") ||
  5924. FileAccess::exists(libraries[i] + "/libGL.so")) {
  5925. print_verbose("Custom libGL override detected. Skipping GPU detection");
  5926. use_prime = 0;
  5927. }
  5928. }
  5929. }
  5930. if (use_prime == -1) {
  5931. print_verbose("Detecting GPUs, set DRI_PRIME in the environment to override GPU detection logic.");
  5932. use_prime = DetectPrimeX11::detect_prime();
  5933. }
  5934. if (use_prime) {
  5935. print_line("Found discrete GPU, setting DRI_PRIME=1 to use it.");
  5936. print_line("Note: Set DRI_PRIME=0 in the environment to disable Godot from using the discrete GPU.");
  5937. setenv("DRI_PRIME", "1", 1);
  5938. }
  5939. }
  5940. }
  5941. if (rendering_driver == "opengl3") {
  5942. gl_manager = memnew(GLManager_X11(p_resolution, GLManager_X11::GLES_3_0_COMPATIBLE));
  5943. if (gl_manager->initialize(x11_display) != OK || gl_manager->open_display(x11_display) != OK) {
  5944. memdelete(gl_manager);
  5945. gl_manager = nullptr;
  5946. bool fallback = GLOBAL_GET("rendering/gl_compatibility/fallback_to_gles");
  5947. if (fallback) {
  5948. WARN_PRINT("Your video card drivers seem not to support the required OpenGL version, switching to OpenGLES.");
  5949. rendering_driver = "opengl3_es";
  5950. OS::get_singleton()->set_current_rendering_driver_name(rendering_driver);
  5951. } else {
  5952. r_error = ERR_UNAVAILABLE;
  5953. OS::get_singleton()->alert(
  5954. vformat("Your video card drivers seem not to support the required OpenGL 3.3 version.\n\n"
  5955. "If possible, consider updating your video card drivers or using the Vulkan driver.\n\n"
  5956. "You can enable the Vulkan driver by starting the engine from the\n"
  5957. "command line with the command:\n\n \"%s\" --rendering-driver vulkan\n\n"
  5958. "If you recently updated your video card drivers, try rebooting.",
  5959. executable_name),
  5960. "Unable to initialize OpenGL video driver");
  5961. ERR_FAIL_MSG("Could not initialize OpenGL.");
  5962. }
  5963. } else {
  5964. driver_found = true;
  5965. RasterizerGLES3::make_current(true);
  5966. }
  5967. }
  5968. if (rendering_driver == "opengl3_es") {
  5969. gl_manager_egl = memnew(GLManagerEGL_X11);
  5970. if (gl_manager_egl->initialize() != OK || gl_manager_egl->open_display(x11_display) != OK) {
  5971. memdelete(gl_manager_egl);
  5972. gl_manager_egl = nullptr;
  5973. r_error = ERR_UNAVAILABLE;
  5974. OS::get_singleton()->alert(
  5975. "Your video card drivers seem not to support the required OpenGL ES 3.0 version.\n\n"
  5976. "If possible, consider updating your video card drivers.\n\n"
  5977. "If you recently updated your video card drivers, try rebooting.",
  5978. "Unable to initialize OpenGL ES video driver");
  5979. ERR_FAIL_MSG("Could not initialize OpenGL ES.");
  5980. }
  5981. driver_found = true;
  5982. RasterizerGLES3::make_current(false);
  5983. }
  5984. #endif // GLES3_ENABLED
  5985. if (!driver_found) {
  5986. r_error = ERR_UNAVAILABLE;
  5987. ERR_FAIL_MSG("Video driver not found.");
  5988. }
  5989. Point2i window_position;
  5990. if (p_position != nullptr) {
  5991. window_position = *p_position;
  5992. } else {
  5993. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  5994. p_screen = SCREEN_PRIMARY;
  5995. }
  5996. Rect2i scr_rect = screen_get_usable_rect(p_screen);
  5997. window_position = scr_rect.position + (scr_rect.size - p_resolution) / 2;
  5998. }
  5999. WindowID main_window = _create_window(p_mode, p_vsync_mode, p_flags, Rect2i(window_position, p_resolution), p_parent_window);
  6000. if (main_window == INVALID_WINDOW_ID) {
  6001. r_error = ERR_CANT_CREATE;
  6002. return;
  6003. }
  6004. for (int i = 0; i < WINDOW_FLAG_MAX; i++) {
  6005. if (p_flags & (1 << i)) {
  6006. window_set_flag(WindowFlags(i), true, main_window);
  6007. }
  6008. }
  6009. #if defined(RD_ENABLED)
  6010. if (rendering_context) {
  6011. rendering_device = memnew(RenderingDevice);
  6012. if (rendering_device->initialize(rendering_context, MAIN_WINDOW_ID) != OK) {
  6013. memdelete(rendering_device);
  6014. rendering_device = nullptr;
  6015. memdelete(rendering_context);
  6016. rendering_context = nullptr;
  6017. r_error = ERR_UNAVAILABLE;
  6018. return;
  6019. }
  6020. rendering_device->screen_create(MAIN_WINDOW_ID);
  6021. RendererCompositorRD::make_current();
  6022. }
  6023. #endif // RD_ENABLED
  6024. {
  6025. //set all event master mask
  6026. XIEventMask all_master_event_mask;
  6027. static unsigned char all_master_mask_data[XIMaskLen(XI_LASTEVENT)] = {};
  6028. all_master_event_mask.deviceid = XIAllMasterDevices;
  6029. all_master_event_mask.mask_len = sizeof(all_master_mask_data);
  6030. all_master_event_mask.mask = all_master_mask_data;
  6031. XISetMask(all_master_event_mask.mask, XI_DeviceChanged);
  6032. XISetMask(all_master_event_mask.mask, XI_RawMotion);
  6033. XISelectEvents(x11_display, DefaultRootWindow(x11_display), &all_master_event_mask, 1);
  6034. }
  6035. cursor_size = XcursorGetDefaultSize(x11_display);
  6036. cursor_theme = XcursorGetTheme(x11_display);
  6037. if (!cursor_theme) {
  6038. print_verbose("XcursorGetTheme could not get cursor theme");
  6039. cursor_theme = "default";
  6040. }
  6041. for (int i = 0; i < CURSOR_MAX; i++) {
  6042. static const char *cursor_file[] = {
  6043. "left_ptr",
  6044. "xterm",
  6045. "hand2",
  6046. "cross",
  6047. "watch",
  6048. "left_ptr_watch",
  6049. "fleur",
  6050. "dnd-move",
  6051. "crossed_circle",
  6052. "v_double_arrow",
  6053. "h_double_arrow",
  6054. "size_bdiag",
  6055. "size_fdiag",
  6056. "move",
  6057. "row_resize",
  6058. "col_resize",
  6059. "question_arrow"
  6060. };
  6061. cursor_img[i] = XcursorLibraryLoadImage(cursor_file[i], cursor_theme, cursor_size);
  6062. if (!cursor_img[i]) {
  6063. const char *fallback = nullptr;
  6064. switch (i) {
  6065. case CURSOR_POINTING_HAND:
  6066. fallback = "pointer";
  6067. break;
  6068. case CURSOR_CROSS:
  6069. fallback = "crosshair";
  6070. break;
  6071. case CURSOR_WAIT:
  6072. fallback = "wait";
  6073. break;
  6074. case CURSOR_BUSY:
  6075. fallback = "progress";
  6076. break;
  6077. case CURSOR_DRAG:
  6078. fallback = "grabbing";
  6079. break;
  6080. case CURSOR_CAN_DROP:
  6081. fallback = "hand1";
  6082. break;
  6083. case CURSOR_FORBIDDEN:
  6084. fallback = "forbidden";
  6085. break;
  6086. case CURSOR_VSIZE:
  6087. fallback = "ns-resize";
  6088. break;
  6089. case CURSOR_HSIZE:
  6090. fallback = "ew-resize";
  6091. break;
  6092. case CURSOR_BDIAGSIZE:
  6093. fallback = "fd_double_arrow";
  6094. break;
  6095. case CURSOR_FDIAGSIZE:
  6096. fallback = "bd_double_arrow";
  6097. break;
  6098. case CURSOR_MOVE:
  6099. cursor_img[i] = cursor_img[CURSOR_DRAG];
  6100. break;
  6101. case CURSOR_VSPLIT:
  6102. fallback = "sb_v_double_arrow";
  6103. break;
  6104. case CURSOR_HSPLIT:
  6105. fallback = "sb_h_double_arrow";
  6106. break;
  6107. case CURSOR_HELP:
  6108. fallback = "help";
  6109. break;
  6110. }
  6111. if (fallback != nullptr) {
  6112. cursor_img[i] = XcursorLibraryLoadImage(fallback, cursor_theme, cursor_size);
  6113. }
  6114. }
  6115. if (cursor_img[i]) {
  6116. cursors[i] = XcursorImageLoadCursor(x11_display, cursor_img[i]);
  6117. } else {
  6118. print_verbose("Failed loading custom cursor: " + String(cursor_file[i]));
  6119. }
  6120. }
  6121. {
  6122. // Creating an empty/transparent cursor
  6123. // Create 1x1 bitmap
  6124. Pixmap cursormask = XCreatePixmap(x11_display,
  6125. RootWindow(x11_display, DefaultScreen(x11_display)), 1, 1, 1);
  6126. // Fill with zero
  6127. XGCValues xgc;
  6128. xgc.function = GXclear;
  6129. GC gc = XCreateGC(x11_display, cursormask, GCFunction, &xgc);
  6130. XFillRectangle(x11_display, cursormask, gc, 0, 0, 1, 1);
  6131. // Color value doesn't matter. Mask zero means no foreground or background will be drawn
  6132. XColor col = {};
  6133. Cursor cursor = XCreatePixmapCursor(x11_display,
  6134. cursormask, // source (using cursor mask as placeholder, since it'll all be ignored)
  6135. cursormask, // mask
  6136. &col, &col, 0, 0);
  6137. XFreePixmap(x11_display, cursormask);
  6138. XFreeGC(x11_display, gc);
  6139. if (cursor == None) {
  6140. ERR_PRINT("FAILED CREATING CURSOR");
  6141. }
  6142. null_cursor = cursor;
  6143. }
  6144. cursor_set_shape(CURSOR_BUSY);
  6145. // Search the X11 event queue for ConfigureNotify events and process all
  6146. // that are currently queued early, so we can get the final window size
  6147. // for correctly drawing of the bootsplash.
  6148. XEvent config_event;
  6149. while (XCheckTypedEvent(x11_display, ConfigureNotify, &config_event)) {
  6150. _window_changed(&config_event);
  6151. }
  6152. events_thread.start(_poll_events_thread, this);
  6153. _update_real_mouse_position(windows[MAIN_WINDOW_ID]);
  6154. #ifdef DBUS_ENABLED
  6155. screensaver = memnew(FreeDesktopScreenSaver);
  6156. screen_set_keep_on(GLOBAL_GET("display/window/energy_saving/keep_screen_on"));
  6157. portal_desktop = memnew(FreeDesktopPortalDesktop);
  6158. atspi_monitor = memnew(FreeDesktopAtSPIMonitor);
  6159. #endif // DBUS_ENABLED
  6160. XSetErrorHandler(&default_window_error_handler);
  6161. r_error = OK;
  6162. }
  6163. DisplayServerX11::~DisplayServerX11() {
  6164. // Send owned clipboard data to clipboard manager before exit.
  6165. Window x11_main_window = windows[MAIN_WINDOW_ID].x11_window;
  6166. _clipboard_transfer_ownership(XA_PRIMARY, x11_main_window);
  6167. _clipboard_transfer_ownership(XInternAtom(x11_display, "CLIPBOARD", 0), x11_main_window);
  6168. events_thread_done.set();
  6169. events_thread.wait_to_finish();
  6170. if (native_menu) {
  6171. memdelete(native_menu);
  6172. native_menu = nullptr;
  6173. }
  6174. //destroy all windows
  6175. for (KeyValue<WindowID, WindowData> &E : windows) {
  6176. #if defined(RD_ENABLED)
  6177. if (rendering_device) {
  6178. rendering_device->screen_free(E.key);
  6179. }
  6180. if (rendering_context) {
  6181. rendering_context->window_destroy(E.key);
  6182. }
  6183. #endif
  6184. #ifdef GLES3_ENABLED
  6185. if (gl_manager) {
  6186. gl_manager->window_destroy(E.key);
  6187. }
  6188. if (gl_manager_egl) {
  6189. gl_manager_egl->window_destroy(E.key);
  6190. }
  6191. #endif
  6192. #ifdef ACCESSKIT_ENABLED
  6193. if (accessibility_driver) {
  6194. accessibility_driver->window_destroy(E.key);
  6195. }
  6196. #endif
  6197. WindowData &wd = E.value;
  6198. if (wd.xic) {
  6199. XDestroyIC(wd.xic);
  6200. wd.xic = nullptr;
  6201. }
  6202. XDestroyWindow(x11_display, wd.x11_xim_window);
  6203. #ifdef XKB_ENABLED
  6204. if (xkb_loaded_v05p) {
  6205. if (wd.xkb_state) {
  6206. xkb_compose_state_unref(wd.xkb_state);
  6207. wd.xkb_state = nullptr;
  6208. }
  6209. }
  6210. #endif
  6211. XUnmapWindow(x11_display, wd.x11_window);
  6212. XDestroyWindow(x11_display, wd.x11_window);
  6213. }
  6214. #ifdef XKB_ENABLED
  6215. if (xkb_loaded_v05p) {
  6216. if (dead_tbl) {
  6217. xkb_compose_table_unref(dead_tbl);
  6218. }
  6219. if (xkb_ctx) {
  6220. xkb_context_unref(xkb_ctx);
  6221. }
  6222. }
  6223. #endif
  6224. //destroy drivers
  6225. #if defined(RD_ENABLED)
  6226. if (rendering_device) {
  6227. memdelete(rendering_device);
  6228. rendering_device = nullptr;
  6229. }
  6230. if (rendering_context) {
  6231. memdelete(rendering_context);
  6232. rendering_context = nullptr;
  6233. }
  6234. #endif
  6235. #ifdef GLES3_ENABLED
  6236. if (gl_manager) {
  6237. memdelete(gl_manager);
  6238. gl_manager = nullptr;
  6239. }
  6240. if (gl_manager_egl) {
  6241. memdelete(gl_manager_egl);
  6242. gl_manager_egl = nullptr;
  6243. }
  6244. #endif
  6245. if (xrandr_handle) {
  6246. dlclose(xrandr_handle);
  6247. }
  6248. for (int i = 0; i < CURSOR_MAX; i++) {
  6249. if (cursors[i] != None) {
  6250. XFreeCursor(x11_display, cursors[i]);
  6251. }
  6252. if (cursor_img[i] != nullptr) {
  6253. XcursorImageDestroy(cursor_img[i]);
  6254. }
  6255. }
  6256. if (xim) {
  6257. XCloseIM(xim);
  6258. }
  6259. XCloseDisplay(x11_display);
  6260. if (xmbstring) {
  6261. memfree(xmbstring);
  6262. }
  6263. #ifdef ACCESSKIT_ENABLED
  6264. if (accessibility_driver) {
  6265. memdelete(accessibility_driver);
  6266. }
  6267. #endif
  6268. #ifdef SPEECHD_ENABLED
  6269. if (tts) {
  6270. memdelete(tts);
  6271. }
  6272. #endif
  6273. #ifdef DBUS_ENABLED
  6274. memdelete(screensaver);
  6275. memdelete(portal_desktop);
  6276. memdelete(atspi_monitor);
  6277. #endif
  6278. }
  6279. void DisplayServerX11::register_x11_driver() {
  6280. register_create_function("x11", create_func, get_rendering_drivers_func);
  6281. }
  6282. #endif // X11 enabled