wayland_thread.cpp 164 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215
  1. /**************************************************************************/
  2. /* wayland_thread.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 "wayland_thread.h"
  31. #include "core/config/engine.h"
  32. #ifdef WAYLAND_ENABLED
  33. #ifdef __FreeBSD__
  34. #include <dev/evdev/input-event-codes.h>
  35. #else
  36. // Assume Linux.
  37. #include <linux/input-event-codes.h>
  38. #endif
  39. // For the actual polling thread.
  40. #include <poll.h>
  41. // For shared memory buffer creation.
  42. #include <fcntl.h>
  43. #include <sys/mman.h>
  44. #include <unistd.h>
  45. // Fix the wl_array_for_each macro to work with C++. This is based on the
  46. // original from `wayland-util.h` in the Wayland client library.
  47. #undef wl_array_for_each
  48. #define wl_array_for_each(pos, array) \
  49. for (pos = (decltype(pos))(array)->data; (const char *)pos < ((const char *)(array)->data + (array)->size); (pos)++)
  50. #define WAYLAND_THREAD_DEBUG_LOGS_ENABLED
  51. #ifdef WAYLAND_THREAD_DEBUG_LOGS_ENABLED
  52. #define DEBUG_LOG_WAYLAND_THREAD(...) print_verbose(__VA_ARGS__)
  53. #else
  54. #define DEBUG_LOG_WAYLAND_THREAD(...)
  55. #endif
  56. // Since we're never going to use this interface directly, it's not worth
  57. // generating the whole deal.
  58. #define FIFO_INTERFACE_NAME "wp_fifo_manager_v1"
  59. // Read the content pointed by fd into a Vector<uint8_t>.
  60. Vector<uint8_t> WaylandThread::_read_fd(int fd) {
  61. // This is pretty much an arbitrary size.
  62. uint32_t chunk_size = 2048;
  63. LocalVector<uint8_t> data;
  64. data.resize(chunk_size);
  65. uint32_t bytes_read = 0;
  66. while (true) {
  67. ssize_t last_bytes_read = read(fd, data.ptr() + bytes_read, chunk_size);
  68. if (last_bytes_read < 0) {
  69. ERR_PRINT(vformat("Read error %d.", errno));
  70. data.clear();
  71. break;
  72. }
  73. if (last_bytes_read == 0) {
  74. // We're done, we've reached the EOF.
  75. DEBUG_LOG_WAYLAND_THREAD(vformat("Done reading %d bytes.", bytes_read));
  76. close(fd);
  77. data.resize(bytes_read);
  78. break;
  79. }
  80. DEBUG_LOG_WAYLAND_THREAD(vformat("Read chunk of %d bytes.", last_bytes_read));
  81. bytes_read += last_bytes_read;
  82. // Increase the buffer size by one chunk in preparation of the next read.
  83. data.resize(bytes_read + chunk_size);
  84. }
  85. return Vector<uint8_t>(data);
  86. }
  87. // Based on the wayland book's shared memory boilerplate (PD/CC0).
  88. // See: https://wayland-book.com/surfaces/shared-memory.html
  89. int WaylandThread::_allocate_shm_file(size_t size) {
  90. int retries = 100;
  91. do {
  92. // Generate a random name.
  93. char name[] = "/wl_shm-godot-XXXXXX";
  94. for (long unsigned int i = sizeof(name) - 7; i < sizeof(name) - 1; i++) {
  95. name[i] = Math::random('A', 'Z');
  96. }
  97. // Try to open a shared memory object with that name.
  98. int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
  99. if (fd >= 0) {
  100. // Success, unlink its name as we just need the file descriptor.
  101. shm_unlink(name);
  102. // Resize the file to the requested length.
  103. int ret;
  104. do {
  105. ret = ftruncate(fd, size);
  106. } while (ret < 0 && errno == EINTR);
  107. if (ret < 0) {
  108. close(fd);
  109. return -1;
  110. }
  111. return fd;
  112. }
  113. retries--;
  114. } while (retries > 0 && errno == EEXIST);
  115. return -1;
  116. }
  117. // Return the content of a wl_data_offer.
  118. Vector<uint8_t> WaylandThread::_wl_data_offer_read(struct wl_display *p_display, const char *p_mime, struct wl_data_offer *p_offer) {
  119. if (!p_offer) {
  120. return Vector<uint8_t>();
  121. }
  122. int fds[2];
  123. if (pipe(fds) == 0) {
  124. wl_data_offer_receive(p_offer, p_mime, fds[1]);
  125. // Let the compositor know about the pipe.
  126. // NOTE: It's important to just flush and not roundtrip here as we would risk
  127. // running some cleanup event, like for example `wl_data_device::leave`. We're
  128. // going to wait for the message anyways as the read will probably block if
  129. // the compositor doesn't read from the other end of the pipe.
  130. wl_display_flush(p_display);
  131. // Close the write end of the pipe, which we don't need and would otherwise
  132. // just stall our next `read`s.
  133. close(fds[1]);
  134. return _read_fd(fds[0]);
  135. }
  136. return Vector<uint8_t>();
  137. }
  138. // Read the content of a wp_primary_selection_offer.
  139. Vector<uint8_t> WaylandThread::_wp_primary_selection_offer_read(struct wl_display *p_display, const char *p_mime, struct zwp_primary_selection_offer_v1 *p_offer) {
  140. if (!p_offer) {
  141. return Vector<uint8_t>();
  142. }
  143. int fds[2];
  144. if (pipe(fds) == 0) {
  145. zwp_primary_selection_offer_v1_receive(p_offer, p_mime, fds[1]);
  146. // NOTE: It's important to just flush and not roundtrip here as we would risk
  147. // running some cleanup event, like for example `wl_data_device::leave`. We're
  148. // going to wait for the message anyways as the read will probably block if
  149. // the compositor doesn't read from the other end of the pipe.
  150. wl_display_flush(p_display);
  151. // Close the write end of the pipe, which we don't need and would otherwise
  152. // just stall our next `read`s.
  153. close(fds[1]);
  154. return _read_fd(fds[0]);
  155. }
  156. return Vector<uint8_t>();
  157. }
  158. Ref<InputEventKey> WaylandThread::_seat_state_get_key_event(SeatState *p_ss, xkb_keycode_t p_keycode, bool p_pressed) {
  159. Ref<InputEventKey> event;
  160. ERR_FAIL_NULL_V(p_ss, event);
  161. Key shifted_key = KeyMappingXKB::get_keycode(xkb_state_key_get_one_sym(p_ss->xkb_state, p_keycode));
  162. Key plain_key = Key::NONE;
  163. // NOTE: xkbcommon's API really encourages to apply the modifier state but we
  164. // only want a "plain" symbol so that we can convert it into a godot keycode.
  165. const xkb_keysym_t *syms = nullptr;
  166. int num_sys = xkb_keymap_key_get_syms_by_level(p_ss->xkb_keymap, p_keycode, p_ss->current_layout_index, 0, &syms);
  167. if (num_sys > 0 && syms) {
  168. plain_key = KeyMappingXKB::get_keycode(syms[0]);
  169. }
  170. Key physical_keycode = KeyMappingXKB::get_scancode(p_keycode);
  171. KeyLocation key_location = KeyMappingXKB::get_location(p_keycode);
  172. uint32_t unicode = xkb_state_key_get_utf32(p_ss->xkb_state, p_keycode);
  173. Key keycode = Key::NONE;
  174. if ((shifted_key & Key::SPECIAL) != Key::NONE || (plain_key & Key::SPECIAL) != Key::NONE) {
  175. keycode = shifted_key;
  176. }
  177. if (keycode == Key::NONE) {
  178. keycode = plain_key;
  179. }
  180. if (keycode == Key::NONE) {
  181. keycode = physical_keycode;
  182. }
  183. if (keycode >= Key::A + 32 && keycode <= Key::Z + 32) {
  184. keycode -= 'a' - 'A';
  185. }
  186. if (physical_keycode == Key::NONE && keycode == Key::NONE && unicode == 0) {
  187. return event;
  188. }
  189. event.instantiate();
  190. event->set_window_id(p_ss->focused_id);
  191. // Set all pressed modifiers.
  192. event->set_shift_pressed(p_ss->shift_pressed);
  193. event->set_ctrl_pressed(p_ss->ctrl_pressed);
  194. event->set_alt_pressed(p_ss->alt_pressed);
  195. event->set_meta_pressed(p_ss->meta_pressed);
  196. event->set_pressed(p_pressed);
  197. event->set_keycode(keycode);
  198. event->set_physical_keycode(physical_keycode);
  199. event->set_location(key_location);
  200. if (unicode != 0) {
  201. event->set_key_label(fix_key_label(unicode, keycode));
  202. } else {
  203. event->set_key_label(keycode);
  204. }
  205. if (p_pressed) {
  206. event->set_unicode(fix_unicode(unicode));
  207. }
  208. // Taken from DisplayServerX11.
  209. if (event->get_keycode() == Key::BACKTAB) {
  210. // Make it consistent across platforms.
  211. event->set_keycode(Key::TAB);
  212. event->set_physical_keycode(Key::TAB);
  213. event->set_shift_pressed(true);
  214. }
  215. return event;
  216. }
  217. // NOTE: Due to the nature of the way keys are encoded, there's an ambiguity
  218. // regarding "special" keys. In other words: there's no reliable way of
  219. // switching between a special key and a character key if not marking a
  220. // different Godot keycode, even if we're actually using the same XKB raw
  221. // keycode. This means that, during this switch, the old key will get "stuck",
  222. // as it will never receive a release event. This method returns the necessary
  223. // event to fix this if needed.
  224. Ref<InputEventKey> WaylandThread::_seat_state_get_unstuck_key_event(SeatState *p_ss, xkb_keycode_t p_keycode, bool p_pressed, Key p_key) {
  225. Ref<InputEventKey> event;
  226. if (p_pressed) {
  227. Key *old_key = p_ss->pressed_keycodes.getptr(p_keycode);
  228. if (old_key != nullptr && *old_key != p_key) {
  229. print_verbose(vformat("%s and %s have same keycode. Generating release event for %s", keycode_get_string(*old_key), keycode_get_string(p_key), keycode_get_string(*old_key)));
  230. event = _seat_state_get_key_event(p_ss, p_keycode, false);
  231. if (event.is_valid()) {
  232. event->set_keycode(*old_key);
  233. }
  234. }
  235. p_ss->pressed_keycodes[p_keycode] = p_key;
  236. } else {
  237. p_ss->pressed_keycodes.erase(p_keycode);
  238. }
  239. return event;
  240. }
  241. void WaylandThread::_set_current_seat(struct wl_seat *p_seat) {
  242. if (p_seat == wl_seat_current) {
  243. return;
  244. }
  245. SeatState *old_state = wl_seat_get_seat_state(wl_seat_current);
  246. if (old_state) {
  247. seat_state_unlock_pointer(old_state);
  248. }
  249. SeatState *new_state = wl_seat_get_seat_state(p_seat);
  250. seat_state_unlock_pointer(new_state);
  251. wl_seat_current = p_seat;
  252. pointer_set_constraint(pointer_constraint);
  253. }
  254. // Returns whether it loaded the theme or not.
  255. bool WaylandThread::_load_cursor_theme(int p_cursor_size) {
  256. if (wl_cursor_theme) {
  257. wl_cursor_theme_destroy(wl_cursor_theme);
  258. wl_cursor_theme = nullptr;
  259. }
  260. if (cursor_theme_name.is_empty()) {
  261. cursor_theme_name = "default";
  262. }
  263. print_verbose(vformat("Loading cursor theme \"%s\" size %d.", cursor_theme_name, p_cursor_size));
  264. wl_cursor_theme = wl_cursor_theme_load(cursor_theme_name.utf8().get_data(), p_cursor_size, registry.wl_shm);
  265. ERR_FAIL_NULL_V_MSG(wl_cursor_theme, false, "Can't load any cursor theme.");
  266. static const char *cursor_names[] = {
  267. "left_ptr",
  268. "xterm",
  269. "hand2",
  270. "cross",
  271. "watch",
  272. "left_ptr_watch",
  273. "fleur",
  274. "dnd-move",
  275. "crossed_circle",
  276. "v_double_arrow",
  277. "h_double_arrow",
  278. "size_bdiag",
  279. "size_fdiag",
  280. "move",
  281. "row_resize",
  282. "col_resize",
  283. "question_arrow"
  284. };
  285. static const char *cursor_names_fallback[] = {
  286. nullptr,
  287. nullptr,
  288. "pointer",
  289. "cross",
  290. "wait",
  291. "progress",
  292. "grabbing",
  293. "hand1",
  294. "forbidden",
  295. "ns-resize",
  296. "ew-resize",
  297. "fd_double_arrow",
  298. "bd_double_arrow",
  299. "fleur",
  300. "sb_v_double_arrow",
  301. "sb_h_double_arrow",
  302. "help"
  303. };
  304. for (int i = 0; i < DisplayServer::CURSOR_MAX; i++) {
  305. struct wl_cursor *cursor = wl_cursor_theme_get_cursor(wl_cursor_theme, cursor_names[i]);
  306. if (!cursor && cursor_names_fallback[i]) {
  307. cursor = wl_cursor_theme_get_cursor(wl_cursor_theme, cursor_names_fallback[i]);
  308. }
  309. if (cursor && cursor->image_count > 0) {
  310. wl_cursors[i] = cursor;
  311. } else {
  312. wl_cursors[i] = nullptr;
  313. print_verbose("Failed loading cursor: " + String(cursor_names[i]));
  314. }
  315. }
  316. return true;
  317. }
  318. void WaylandThread::_update_scale(int p_scale) {
  319. if (p_scale <= cursor_scale) {
  320. return;
  321. }
  322. print_verbose(vformat("Bumping cursor scale to %d", p_scale));
  323. // There's some display that's bigger than the cache, let's update it.
  324. cursor_scale = p_scale;
  325. if (wl_cursor_theme == nullptr) {
  326. // Ugh. Either we're still initializing (this must've been called from the
  327. // first roundtrips) or we had some error while doing so. We'll trust that it
  328. // will be updated for us if needed.
  329. return;
  330. }
  331. int cursor_size = unscaled_cursor_size * p_scale;
  332. if (_load_cursor_theme(cursor_size)) {
  333. for (struct wl_seat *wl_seat : registry.wl_seats) {
  334. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  335. ERR_FAIL_NULL(ss);
  336. seat_state_update_cursor(ss);
  337. }
  338. }
  339. }
  340. void WaylandThread::_wl_registry_on_global(void *data, struct wl_registry *wl_registry, uint32_t name, const char *interface, uint32_t version) {
  341. RegistryState *registry = (RegistryState *)data;
  342. ERR_FAIL_NULL(registry);
  343. if (strcmp(interface, wl_shm_interface.name) == 0) {
  344. registry->wl_shm = (struct wl_shm *)wl_registry_bind(wl_registry, name, &wl_shm_interface, 1);
  345. registry->wl_shm_name = name;
  346. return;
  347. }
  348. // NOTE: Deprecated.
  349. if (strcmp(interface, zxdg_exporter_v1_interface.name) == 0) {
  350. registry->xdg_exporter_v1 = (struct zxdg_exporter_v1 *)wl_registry_bind(wl_registry, name, &zxdg_exporter_v1_interface, 1);
  351. registry->xdg_exporter_v1_name = name;
  352. return;
  353. }
  354. if (strcmp(interface, zxdg_exporter_v2_interface.name) == 0) {
  355. registry->xdg_exporter_v2 = (struct zxdg_exporter_v2 *)wl_registry_bind(wl_registry, name, &zxdg_exporter_v2_interface, 1);
  356. registry->xdg_exporter_v2_name = name;
  357. return;
  358. }
  359. if (strcmp(interface, wl_compositor_interface.name) == 0) {
  360. registry->wl_compositor = (struct wl_compositor *)wl_registry_bind(wl_registry, name, &wl_compositor_interface, CLAMP((int)version, 1, 6));
  361. registry->wl_compositor_name = name;
  362. return;
  363. }
  364. if (strcmp(interface, wl_data_device_manager_interface.name) == 0) {
  365. registry->wl_data_device_manager = (struct wl_data_device_manager *)wl_registry_bind(wl_registry, name, &wl_data_device_manager_interface, CLAMP((int)version, 1, 3));
  366. registry->wl_data_device_manager_name = name;
  367. // This global creates some seat data. Let's do that for the ones already available.
  368. for (struct wl_seat *wl_seat : registry->wl_seats) {
  369. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  370. ERR_FAIL_NULL(ss);
  371. if (ss->wl_data_device == nullptr) {
  372. ss->wl_data_device = wl_data_device_manager_get_data_device(registry->wl_data_device_manager, wl_seat);
  373. wl_data_device_add_listener(ss->wl_data_device, &wl_data_device_listener, ss);
  374. }
  375. }
  376. return;
  377. }
  378. if (strcmp(interface, wl_output_interface.name) == 0) {
  379. struct wl_output *wl_output = (struct wl_output *)wl_registry_bind(wl_registry, name, &wl_output_interface, CLAMP((int)version, 1, 4));
  380. wl_proxy_tag_godot((struct wl_proxy *)wl_output);
  381. registry->wl_outputs.push_back(wl_output);
  382. ScreenState *ss = memnew(ScreenState);
  383. ss->wl_output_name = name;
  384. ss->wayland_thread = registry->wayland_thread;
  385. wl_proxy_tag_godot((struct wl_proxy *)wl_output);
  386. wl_output_add_listener(wl_output, &wl_output_listener, ss);
  387. return;
  388. }
  389. if (strcmp(interface, wl_seat_interface.name) == 0) {
  390. struct wl_seat *wl_seat = (struct wl_seat *)wl_registry_bind(wl_registry, name, &wl_seat_interface, CLAMP((int)version, 1, 9));
  391. wl_proxy_tag_godot((struct wl_proxy *)wl_seat);
  392. SeatState *ss = memnew(SeatState);
  393. ss->wl_seat = wl_seat;
  394. ss->wl_seat_name = name;
  395. ss->registry = registry;
  396. ss->wayland_thread = registry->wayland_thread;
  397. // Some extra stuff depends on other globals. We'll initialize them if the
  398. // globals are already there, otherwise we'll have to do that once and if they
  399. // get announced.
  400. //
  401. // NOTE: Don't forget to also bind/destroy with the respective global.
  402. if (!ss->wl_data_device && registry->wl_data_device_manager) {
  403. // Clipboard & DnD.
  404. ss->wl_data_device = wl_data_device_manager_get_data_device(registry->wl_data_device_manager, wl_seat);
  405. wl_data_device_add_listener(ss->wl_data_device, &wl_data_device_listener, ss);
  406. }
  407. if (!ss->wp_primary_selection_device && registry->wp_primary_selection_device_manager) {
  408. // Primary selection.
  409. ss->wp_primary_selection_device = zwp_primary_selection_device_manager_v1_get_device(registry->wp_primary_selection_device_manager, wl_seat);
  410. zwp_primary_selection_device_v1_add_listener(ss->wp_primary_selection_device, &wp_primary_selection_device_listener, ss);
  411. }
  412. if (!ss->wp_tablet_seat && registry->wp_tablet_manager) {
  413. // Tablet.
  414. ss->wp_tablet_seat = zwp_tablet_manager_v2_get_tablet_seat(registry->wp_tablet_manager, wl_seat);
  415. zwp_tablet_seat_v2_add_listener(ss->wp_tablet_seat, &wp_tablet_seat_listener, ss);
  416. }
  417. if (!ss->wp_text_input && registry->wp_text_input_manager) {
  418. // IME.
  419. ss->wp_text_input = zwp_text_input_manager_v3_get_text_input(registry->wp_text_input_manager, wl_seat);
  420. zwp_text_input_v3_add_listener(ss->wp_text_input, &wp_text_input_listener, ss);
  421. }
  422. registry->wl_seats.push_back(wl_seat);
  423. wl_seat_add_listener(wl_seat, &wl_seat_listener, ss);
  424. if (registry->wayland_thread->wl_seat_current == nullptr) {
  425. registry->wayland_thread->_set_current_seat(wl_seat);
  426. }
  427. return;
  428. }
  429. if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
  430. registry->xdg_wm_base = (struct xdg_wm_base *)wl_registry_bind(wl_registry, name, &xdg_wm_base_interface, CLAMP((int)version, 1, 6));
  431. registry->xdg_wm_base_name = name;
  432. xdg_wm_base_add_listener(registry->xdg_wm_base, &xdg_wm_base_listener, nullptr);
  433. return;
  434. }
  435. if (strcmp(interface, wp_viewporter_interface.name) == 0) {
  436. registry->wp_viewporter = (struct wp_viewporter *)wl_registry_bind(wl_registry, name, &wp_viewporter_interface, 1);
  437. registry->wp_viewporter_name = name;
  438. }
  439. if (strcmp(interface, wp_cursor_shape_manager_v1_interface.name) == 0) {
  440. registry->wp_cursor_shape_manager = (struct wp_cursor_shape_manager_v1 *)wl_registry_bind(wl_registry, name, &wp_cursor_shape_manager_v1_interface, 1);
  441. registry->wp_cursor_shape_manager_name = name;
  442. return;
  443. }
  444. if (strcmp(interface, wp_fractional_scale_manager_v1_interface.name) == 0) {
  445. registry->wp_fractional_scale_manager = (struct wp_fractional_scale_manager_v1 *)wl_registry_bind(wl_registry, name, &wp_fractional_scale_manager_v1_interface, 1);
  446. registry->wp_fractional_scale_manager_name = name;
  447. // NOTE: We're not mapping the fractional scale object here because this is
  448. // supposed to be a "startup global". If for some reason this isn't true (who
  449. // knows), add a conditional branch for creating the add-on object.
  450. }
  451. if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) {
  452. registry->xdg_decoration_manager = (struct zxdg_decoration_manager_v1 *)wl_registry_bind(wl_registry, name, &zxdg_decoration_manager_v1_interface, 1);
  453. registry->xdg_decoration_manager_name = name;
  454. return;
  455. }
  456. if (strcmp(interface, xdg_system_bell_v1_interface.name) == 0) {
  457. registry->xdg_system_bell = (struct xdg_system_bell_v1 *)wl_registry_bind(wl_registry, name, &xdg_system_bell_v1_interface, 1);
  458. registry->xdg_system_bell_name = name;
  459. return;
  460. }
  461. if (strcmp(interface, xdg_toplevel_icon_manager_v1_interface.name) == 0) {
  462. registry->xdg_toplevel_icon_manager = (struct xdg_toplevel_icon_manager_v1 *)wl_registry_bind(wl_registry, name, &xdg_toplevel_icon_manager_v1_interface, 1);
  463. registry->xdg_toplevel_icon_manager_name = name;
  464. return;
  465. }
  466. if (strcmp(interface, xdg_activation_v1_interface.name) == 0) {
  467. registry->xdg_activation = (struct xdg_activation_v1 *)wl_registry_bind(wl_registry, name, &xdg_activation_v1_interface, 1);
  468. registry->xdg_activation_name = name;
  469. return;
  470. }
  471. if (strcmp(interface, zwp_primary_selection_device_manager_v1_interface.name) == 0) {
  472. registry->wp_primary_selection_device_manager = (struct zwp_primary_selection_device_manager_v1 *)wl_registry_bind(wl_registry, name, &zwp_primary_selection_device_manager_v1_interface, 1);
  473. // This global creates some seat data. Let's do that for the ones already available.
  474. for (struct wl_seat *wl_seat : registry->wl_seats) {
  475. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  476. ERR_FAIL_NULL(ss);
  477. if (!ss->wp_primary_selection_device && registry->wp_primary_selection_device_manager) {
  478. ss->wp_primary_selection_device = zwp_primary_selection_device_manager_v1_get_device(registry->wp_primary_selection_device_manager, wl_seat);
  479. zwp_primary_selection_device_v1_add_listener(ss->wp_primary_selection_device, &wp_primary_selection_device_listener, ss);
  480. }
  481. }
  482. }
  483. if (strcmp(interface, zwp_relative_pointer_manager_v1_interface.name) == 0) {
  484. registry->wp_relative_pointer_manager = (struct zwp_relative_pointer_manager_v1 *)wl_registry_bind(wl_registry, name, &zwp_relative_pointer_manager_v1_interface, 1);
  485. registry->wp_relative_pointer_manager_name = name;
  486. return;
  487. }
  488. if (strcmp(interface, zwp_pointer_constraints_v1_interface.name) == 0) {
  489. registry->wp_pointer_constraints = (struct zwp_pointer_constraints_v1 *)wl_registry_bind(wl_registry, name, &zwp_pointer_constraints_v1_interface, 1);
  490. registry->wp_pointer_constraints_name = name;
  491. return;
  492. }
  493. if (strcmp(interface, zwp_pointer_gestures_v1_interface.name) == 0) {
  494. registry->wp_pointer_gestures = (struct zwp_pointer_gestures_v1 *)wl_registry_bind(wl_registry, name, &zwp_pointer_gestures_v1_interface, 1);
  495. registry->wp_pointer_gestures_name = name;
  496. return;
  497. }
  498. if (strcmp(interface, zwp_idle_inhibit_manager_v1_interface.name) == 0) {
  499. registry->wp_idle_inhibit_manager = (struct zwp_idle_inhibit_manager_v1 *)wl_registry_bind(wl_registry, name, &zwp_idle_inhibit_manager_v1_interface, 1);
  500. registry->wp_idle_inhibit_manager_name = name;
  501. return;
  502. }
  503. if (strcmp(interface, zwp_tablet_manager_v2_interface.name) == 0) {
  504. registry->wp_tablet_manager = (struct zwp_tablet_manager_v2 *)wl_registry_bind(wl_registry, name, &zwp_tablet_manager_v2_interface, 1);
  505. registry->wp_tablet_manager_name = name;
  506. // This global creates some seat data. Let's do that for the ones already available.
  507. for (struct wl_seat *wl_seat : registry->wl_seats) {
  508. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  509. ERR_FAIL_NULL(ss);
  510. ss->wp_tablet_seat = zwp_tablet_manager_v2_get_tablet_seat(registry->wp_tablet_manager, wl_seat);
  511. zwp_tablet_seat_v2_add_listener(ss->wp_tablet_seat, &wp_tablet_seat_listener, ss);
  512. }
  513. return;
  514. }
  515. if (strcmp(interface, zwp_text_input_manager_v3_interface.name) == 0) {
  516. registry->wp_text_input_manager = (struct zwp_text_input_manager_v3 *)wl_registry_bind(wl_registry, name, &zwp_text_input_manager_v3_interface, 1);
  517. registry->wp_text_input_manager_name = name;
  518. // This global creates some seat data. Let's do that for the ones already available.
  519. for (struct wl_seat *wl_seat : registry->wl_seats) {
  520. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  521. ERR_FAIL_NULL(ss);
  522. ss->wp_text_input = zwp_text_input_manager_v3_get_text_input(registry->wp_text_input_manager, wl_seat);
  523. zwp_text_input_v3_add_listener(ss->wp_text_input, &wp_text_input_listener, ss);
  524. }
  525. return;
  526. }
  527. if (strcmp(interface, FIFO_INTERFACE_NAME) == 0) {
  528. registry->wp_fifo_manager_name = name;
  529. }
  530. }
  531. void WaylandThread::_wl_registry_on_global_remove(void *data, struct wl_registry *wl_registry, uint32_t name) {
  532. RegistryState *registry = (RegistryState *)data;
  533. ERR_FAIL_NULL(registry);
  534. if (name == registry->wl_shm_name) {
  535. if (registry->wl_shm) {
  536. wl_shm_destroy(registry->wl_shm);
  537. registry->wl_shm = nullptr;
  538. }
  539. registry->wl_shm_name = 0;
  540. return;
  541. }
  542. // NOTE: Deprecated.
  543. if (name == registry->xdg_exporter_v1_name) {
  544. if (registry->xdg_exporter_v1) {
  545. zxdg_exporter_v1_destroy(registry->xdg_exporter_v1);
  546. registry->xdg_exporter_v1 = nullptr;
  547. }
  548. registry->xdg_exporter_v1_name = 0;
  549. return;
  550. }
  551. if (name == registry->xdg_exporter_v2_name) {
  552. if (registry->xdg_exporter_v2) {
  553. zxdg_exporter_v2_destroy(registry->xdg_exporter_v2);
  554. registry->xdg_exporter_v2 = nullptr;
  555. }
  556. registry->xdg_exporter_v2_name = 0;
  557. return;
  558. }
  559. if (name == registry->wl_compositor_name) {
  560. if (registry->wl_compositor) {
  561. wl_compositor_destroy(registry->wl_compositor);
  562. registry->wl_compositor = nullptr;
  563. }
  564. registry->wl_compositor_name = 0;
  565. return;
  566. }
  567. if (name == registry->wl_data_device_manager_name) {
  568. if (registry->wl_data_device_manager) {
  569. wl_data_device_manager_destroy(registry->wl_data_device_manager);
  570. registry->wl_data_device_manager = nullptr;
  571. }
  572. registry->wl_data_device_manager_name = 0;
  573. // This global is used to create some seat data. Let's clean it.
  574. for (struct wl_seat *wl_seat : registry->wl_seats) {
  575. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  576. ERR_FAIL_NULL(ss);
  577. if (ss->wl_data_device) {
  578. wl_data_device_destroy(ss->wl_data_device);
  579. ss->wl_data_device = nullptr;
  580. }
  581. ss->wl_data_device = nullptr;
  582. }
  583. return;
  584. }
  585. if (name == registry->xdg_wm_base_name) {
  586. if (registry->xdg_wm_base) {
  587. xdg_wm_base_destroy(registry->xdg_wm_base);
  588. registry->xdg_wm_base = nullptr;
  589. }
  590. registry->xdg_wm_base_name = 0;
  591. return;
  592. }
  593. if (name == registry->wp_viewporter_name) {
  594. for (KeyValue<DisplayServer::WindowID, WindowState> &pair : registry->wayland_thread->windows) {
  595. WindowState ws = pair.value;
  596. if (registry->wp_viewporter) {
  597. wp_viewporter_destroy(registry->wp_viewporter);
  598. registry->wp_viewporter = nullptr;
  599. }
  600. if (ws.wp_viewport) {
  601. wp_viewport_destroy(ws.wp_viewport);
  602. ws.wp_viewport = nullptr;
  603. }
  604. }
  605. registry->wp_viewporter_name = 0;
  606. return;
  607. }
  608. if (name == registry->wp_cursor_shape_manager_name) {
  609. if (registry->wp_cursor_shape_manager) {
  610. wp_cursor_shape_manager_v1_destroy(registry->wp_cursor_shape_manager);
  611. registry->wp_cursor_shape_manager = nullptr;
  612. }
  613. registry->wp_cursor_shape_manager_name = 0;
  614. for (struct wl_seat *wl_seat : registry->wl_seats) {
  615. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  616. ERR_FAIL_NULL(ss);
  617. if (ss->wp_cursor_shape_device) {
  618. wp_cursor_shape_device_v1_destroy(ss->wp_cursor_shape_device);
  619. ss->wp_cursor_shape_device = nullptr;
  620. }
  621. }
  622. }
  623. if (name == registry->wp_fractional_scale_manager_name) {
  624. for (KeyValue<DisplayServer::WindowID, WindowState> &pair : registry->wayland_thread->windows) {
  625. WindowState ws = pair.value;
  626. if (registry->wp_fractional_scale_manager) {
  627. wp_fractional_scale_manager_v1_destroy(registry->wp_fractional_scale_manager);
  628. registry->wp_fractional_scale_manager = nullptr;
  629. }
  630. if (ws.wp_fractional_scale) {
  631. wp_fractional_scale_v1_destroy(ws.wp_fractional_scale);
  632. ws.wp_fractional_scale = nullptr;
  633. }
  634. }
  635. registry->wp_fractional_scale_manager_name = 0;
  636. }
  637. if (name == registry->xdg_decoration_manager_name) {
  638. if (registry->xdg_decoration_manager) {
  639. zxdg_decoration_manager_v1_destroy(registry->xdg_decoration_manager);
  640. registry->xdg_decoration_manager = nullptr;
  641. }
  642. registry->xdg_decoration_manager_name = 0;
  643. return;
  644. }
  645. if (name == registry->xdg_system_bell_name) {
  646. if (registry->xdg_system_bell) {
  647. xdg_system_bell_v1_destroy(registry->xdg_system_bell);
  648. registry->xdg_system_bell = nullptr;
  649. }
  650. registry->xdg_system_bell_name = 0;
  651. return;
  652. }
  653. if (name == registry->xdg_toplevel_icon_manager_name) {
  654. if (registry->xdg_toplevel_icon_manager) {
  655. xdg_toplevel_icon_manager_v1_destroy(registry->xdg_toplevel_icon_manager);
  656. registry->xdg_toplevel_icon_manager = nullptr;
  657. }
  658. if (registry->wayland_thread->xdg_icon) {
  659. xdg_toplevel_icon_v1_destroy(registry->wayland_thread->xdg_icon);
  660. }
  661. if (registry->wayland_thread->icon_buffer) {
  662. wl_buffer_destroy(registry->wayland_thread->icon_buffer);
  663. }
  664. registry->xdg_toplevel_icon_manager_name = 0;
  665. return;
  666. }
  667. if (name == registry->xdg_activation_name) {
  668. if (registry->xdg_activation) {
  669. xdg_activation_v1_destroy(registry->xdg_activation);
  670. registry->xdg_activation = nullptr;
  671. }
  672. registry->xdg_activation_name = 0;
  673. return;
  674. }
  675. if (name == registry->wp_primary_selection_device_manager_name) {
  676. if (registry->wp_primary_selection_device_manager) {
  677. zwp_primary_selection_device_manager_v1_destroy(registry->wp_primary_selection_device_manager);
  678. registry->wp_primary_selection_device_manager = nullptr;
  679. }
  680. registry->wp_primary_selection_device_manager_name = 0;
  681. // This global is used to create some seat data. Let's clean it.
  682. for (struct wl_seat *wl_seat : registry->wl_seats) {
  683. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  684. ERR_FAIL_NULL(ss);
  685. if (ss->wp_primary_selection_device) {
  686. zwp_primary_selection_device_v1_destroy(ss->wp_primary_selection_device);
  687. ss->wp_primary_selection_device = nullptr;
  688. }
  689. if (ss->wp_primary_selection_source) {
  690. zwp_primary_selection_source_v1_destroy(ss->wp_primary_selection_source);
  691. ss->wp_primary_selection_source = nullptr;
  692. }
  693. if (ss->wp_primary_selection_offer) {
  694. memfree(wp_primary_selection_offer_get_offer_state(ss->wp_primary_selection_offer));
  695. zwp_primary_selection_offer_v1_destroy(ss->wp_primary_selection_offer);
  696. ss->wp_primary_selection_offer = nullptr;
  697. }
  698. }
  699. return;
  700. }
  701. if (name == registry->wp_relative_pointer_manager_name) {
  702. if (registry->wp_relative_pointer_manager) {
  703. zwp_relative_pointer_manager_v1_destroy(registry->wp_relative_pointer_manager);
  704. registry->wp_relative_pointer_manager = nullptr;
  705. }
  706. registry->wp_relative_pointer_manager_name = 0;
  707. // This global is used to create some seat data. Let's clean it.
  708. for (struct wl_seat *wl_seat : registry->wl_seats) {
  709. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  710. ERR_FAIL_NULL(ss);
  711. if (ss->wp_relative_pointer) {
  712. zwp_relative_pointer_v1_destroy(ss->wp_relative_pointer);
  713. ss->wp_relative_pointer = nullptr;
  714. }
  715. }
  716. return;
  717. }
  718. if (name == registry->wp_pointer_constraints_name) {
  719. if (registry->wp_pointer_constraints) {
  720. zwp_pointer_constraints_v1_destroy(registry->wp_pointer_constraints);
  721. registry->wp_pointer_constraints = nullptr;
  722. }
  723. registry->wp_pointer_constraints_name = 0;
  724. // This global is used to create some seat data. Let's clean it.
  725. for (struct wl_seat *wl_seat : registry->wl_seats) {
  726. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  727. ERR_FAIL_NULL(ss);
  728. if (ss->wp_relative_pointer) {
  729. zwp_relative_pointer_v1_destroy(ss->wp_relative_pointer);
  730. ss->wp_relative_pointer = nullptr;
  731. }
  732. if (ss->wp_locked_pointer) {
  733. zwp_locked_pointer_v1_destroy(ss->wp_locked_pointer);
  734. ss->wp_locked_pointer = nullptr;
  735. }
  736. if (ss->wp_confined_pointer) {
  737. zwp_confined_pointer_v1_destroy(ss->wp_confined_pointer);
  738. ss->wp_confined_pointer = nullptr;
  739. }
  740. }
  741. return;
  742. }
  743. if (name == registry->wp_pointer_gestures_name) {
  744. if (registry->wp_pointer_gestures) {
  745. zwp_pointer_gestures_v1_destroy(registry->wp_pointer_gestures);
  746. }
  747. registry->wp_pointer_gestures = nullptr;
  748. registry->wp_pointer_gestures_name = 0;
  749. // This global is used to create some seat data. Let's clean it.
  750. for (struct wl_seat *wl_seat : registry->wl_seats) {
  751. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  752. ERR_FAIL_NULL(ss);
  753. if (ss->wp_pointer_gesture_pinch) {
  754. zwp_pointer_gesture_pinch_v1_destroy(ss->wp_pointer_gesture_pinch);
  755. ss->wp_pointer_gesture_pinch = nullptr;
  756. }
  757. }
  758. return;
  759. }
  760. if (name == registry->wp_idle_inhibit_manager_name) {
  761. if (registry->wp_idle_inhibit_manager) {
  762. zwp_idle_inhibit_manager_v1_destroy(registry->wp_idle_inhibit_manager);
  763. registry->wp_idle_inhibit_manager = nullptr;
  764. }
  765. registry->wp_idle_inhibit_manager_name = 0;
  766. return;
  767. }
  768. if (name == registry->wp_tablet_manager_name) {
  769. if (registry->wp_tablet_manager) {
  770. zwp_tablet_manager_v2_destroy(registry->wp_tablet_manager);
  771. registry->wp_tablet_manager = nullptr;
  772. }
  773. registry->wp_tablet_manager_name = 0;
  774. // This global is used to create some seat data. Let's clean it.
  775. for (struct wl_seat *wl_seat : registry->wl_seats) {
  776. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  777. ERR_FAIL_NULL(ss);
  778. for (struct zwp_tablet_tool_v2 *tool : ss->tablet_tools) {
  779. TabletToolState *state = wp_tablet_tool_get_state(tool);
  780. if (state) {
  781. memdelete(state);
  782. }
  783. zwp_tablet_tool_v2_destroy(tool);
  784. }
  785. ss->tablet_tools.clear();
  786. }
  787. return;
  788. }
  789. if (name == registry->wp_text_input_manager_name) {
  790. if (registry->wp_text_input_manager) {
  791. zwp_text_input_manager_v3_destroy(registry->wp_text_input_manager);
  792. registry->wp_text_input_manager = nullptr;
  793. }
  794. registry->wp_text_input_manager_name = 0;
  795. for (struct wl_seat *wl_seat : registry->wl_seats) {
  796. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  797. ERR_FAIL_NULL(ss);
  798. zwp_text_input_v3_destroy(ss->wp_text_input);
  799. ss->wp_text_input = nullptr;
  800. }
  801. return;
  802. }
  803. {
  804. // Iterate through all of the seats to find if any got removed.
  805. List<struct wl_seat *>::Element *E = registry->wl_seats.front();
  806. while (E) {
  807. struct wl_seat *wl_seat = E->get();
  808. List<struct wl_seat *>::Element *N = E->next();
  809. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  810. ERR_FAIL_NULL(ss);
  811. if (ss->wl_seat_name == name) {
  812. if (wl_seat) {
  813. wl_seat_destroy(wl_seat);
  814. }
  815. if (ss->wl_data_device) {
  816. wl_data_device_destroy(ss->wl_data_device);
  817. }
  818. if (ss->wp_tablet_seat) {
  819. zwp_tablet_seat_v2_destroy(ss->wp_tablet_seat);
  820. for (struct zwp_tablet_tool_v2 *tool : ss->tablet_tools) {
  821. TabletToolState *state = wp_tablet_tool_get_state(tool);
  822. if (state) {
  823. memdelete(state);
  824. }
  825. zwp_tablet_tool_v2_destroy(tool);
  826. }
  827. }
  828. memdelete(ss);
  829. registry->wl_seats.erase(E);
  830. return;
  831. }
  832. E = N;
  833. }
  834. }
  835. {
  836. // Iterate through all of the outputs to find if any got removed.
  837. // FIXME: This is a very bruteforce approach.
  838. List<struct wl_output *>::Element *it = registry->wl_outputs.front();
  839. while (it) {
  840. // Iterate through all of the screens to find if any got removed.
  841. struct wl_output *wl_output = it->get();
  842. ERR_FAIL_NULL(wl_output);
  843. ScreenState *ss = wl_output_get_screen_state(wl_output);
  844. if (ss->wl_output_name == name) {
  845. registry->wl_outputs.erase(it);
  846. memdelete(ss);
  847. wl_output_destroy(wl_output);
  848. return;
  849. }
  850. it = it->next();
  851. }
  852. }
  853. if (name == registry->wp_fifo_manager_name) {
  854. registry->wp_fifo_manager_name = 0;
  855. }
  856. }
  857. void WaylandThread::_wl_surface_on_enter(void *data, struct wl_surface *wl_surface, struct wl_output *wl_output) {
  858. if (!wl_output || !wl_proxy_is_godot((struct wl_proxy *)wl_output)) {
  859. // This won't have the right data bound to it. Not worth it and would probably
  860. // just break everything.
  861. return;
  862. }
  863. WindowState *ws = (WindowState *)data;
  864. ERR_FAIL_NULL(ws);
  865. DEBUG_LOG_WAYLAND_THREAD(vformat("Window entered output %x.\n", (size_t)wl_output));
  866. ws->wl_outputs.insert(wl_output);
  867. // Workaround for buffer scaling as there's no guaranteed way of knowing the
  868. // preferred scale.
  869. // TODO: Skip this branch for newer `wl_surface`s once we add support for
  870. // `wl_surface::preferred_buffer_scale`
  871. if (ws->preferred_fractional_scale == 0) {
  872. window_state_update_size(ws, ws->rect.size.width, ws->rect.size.height);
  873. }
  874. }
  875. void WaylandThread::_frame_wl_callback_on_done(void *data, struct wl_callback *wl_callback, uint32_t callback_data) {
  876. wl_callback_destroy(wl_callback);
  877. WindowState *ws = (WindowState *)data;
  878. ERR_FAIL_NULL(ws);
  879. ERR_FAIL_NULL(ws->wayland_thread);
  880. ERR_FAIL_NULL(ws->wl_surface);
  881. ws->last_frame_time = OS::get_singleton()->get_ticks_usec();
  882. ws->wayland_thread->set_frame();
  883. ws->frame_callback = wl_surface_frame(ws->wl_surface);
  884. wl_callback_add_listener(ws->frame_callback, &frame_wl_callback_listener, ws);
  885. if (ws->wl_surface && ws->buffer_scale_changed) {
  886. // NOTE: We're only now setting the buffer scale as the idea is to get this
  887. // data committed together with the new frame, all by the rendering driver.
  888. // This is important because we might otherwise set an invalid combination of
  889. // buffer size and scale (e.g. odd size and 2x scale). We're pretty much
  890. // guaranteed to get a proper buffer in the next render loop as the rescaling
  891. // method also informs the engine of a "window rect change", triggering
  892. // rendering if needed.
  893. wl_surface_set_buffer_scale(ws->wl_surface, window_state_get_preferred_buffer_scale(ws));
  894. }
  895. }
  896. void WaylandThread::_wl_surface_on_leave(void *data, struct wl_surface *wl_surface, struct wl_output *wl_output) {
  897. if (!wl_output || !wl_proxy_is_godot((struct wl_proxy *)wl_output)) {
  898. // This won't have the right data bound to it. Not worth it and would probably
  899. // just break everything.
  900. return;
  901. }
  902. WindowState *ws = (WindowState *)data;
  903. ERR_FAIL_NULL(ws);
  904. ws->wl_outputs.erase(wl_output);
  905. DEBUG_LOG_WAYLAND_THREAD(vformat("Window left output %x.\n", (size_t)wl_output));
  906. }
  907. // TODO: Add support to this event.
  908. void WaylandThread::_wl_surface_on_preferred_buffer_scale(void *data, struct wl_surface *wl_surface, int32_t factor) {
  909. }
  910. // TODO: Add support to this event.
  911. void WaylandThread::_wl_surface_on_preferred_buffer_transform(void *data, struct wl_surface *wl_surface, uint32_t transform) {
  912. }
  913. void WaylandThread::_wl_output_on_geometry(void *data, struct wl_output *wl_output, int32_t x, int32_t y, int32_t physical_width, int32_t physical_height, int32_t subpixel, const char *make, const char *model, int32_t transform) {
  914. ScreenState *ss = (ScreenState *)data;
  915. ERR_FAIL_NULL(ss);
  916. ss->pending_data.position.x = x;
  917. ss->pending_data.position.x = x;
  918. ss->pending_data.position.y = y;
  919. ss->pending_data.physical_size.width = physical_width;
  920. ss->pending_data.physical_size.height = physical_height;
  921. ss->pending_data.make.clear();
  922. ss->pending_data.make.append_utf8(make);
  923. ss->pending_data.model.clear();
  924. ss->pending_data.model.append_utf8(model);
  925. // `wl_output::done` is a version 2 addition. We'll directly update the data
  926. // for compatibility.
  927. if (wl_output_get_version(wl_output) == 1) {
  928. ss->data = ss->pending_data;
  929. }
  930. }
  931. void WaylandThread::_wl_output_on_mode(void *data, struct wl_output *wl_output, uint32_t flags, int32_t width, int32_t height, int32_t refresh) {
  932. ScreenState *ss = (ScreenState *)data;
  933. ERR_FAIL_NULL(ss);
  934. ss->pending_data.size.width = width;
  935. ss->pending_data.size.height = height;
  936. ss->pending_data.refresh_rate = refresh ? refresh / 1000.0f : -1;
  937. // `wl_output::done` is a version 2 addition. We'll directly update the data
  938. // for compatibility.
  939. if (wl_output_get_version(wl_output) == 1) {
  940. ss->data = ss->pending_data;
  941. }
  942. }
  943. // NOTE: The following `wl_output` events are only for version 2 onwards, so we
  944. // can assume that they're "atomic" (i.e. rely on the `wl_output::done` event).
  945. void WaylandThread::_wl_output_on_done(void *data, struct wl_output *wl_output) {
  946. ScreenState *ss = (ScreenState *)data;
  947. ERR_FAIL_NULL(ss);
  948. ss->data = ss->pending_data;
  949. ss->wayland_thread->_update_scale(ss->data.scale);
  950. DEBUG_LOG_WAYLAND_THREAD(vformat("Output %x done.", (size_t)wl_output));
  951. }
  952. void WaylandThread::_wl_output_on_scale(void *data, struct wl_output *wl_output, int32_t factor) {
  953. ScreenState *ss = (ScreenState *)data;
  954. ERR_FAIL_NULL(ss);
  955. ss->pending_data.scale = factor;
  956. DEBUG_LOG_WAYLAND_THREAD(vformat("Output %x scale %d", (size_t)wl_output, factor));
  957. }
  958. void WaylandThread::_wl_output_on_name(void *data, struct wl_output *wl_output, const char *name) {
  959. }
  960. void WaylandThread::_wl_output_on_description(void *data, struct wl_output *wl_output, const char *description) {
  961. }
  962. void WaylandThread::_xdg_wm_base_on_ping(void *data, struct xdg_wm_base *xdg_wm_base, uint32_t serial) {
  963. xdg_wm_base_pong(xdg_wm_base, serial);
  964. }
  965. void WaylandThread::_xdg_surface_on_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial) {
  966. xdg_surface_ack_configure(xdg_surface, serial);
  967. WindowState *ws = (WindowState *)data;
  968. ERR_FAIL_NULL(ws);
  969. DEBUG_LOG_WAYLAND_THREAD(vformat("xdg surface on configure rect %s", ws->rect));
  970. }
  971. void WaylandThread::_xdg_toplevel_on_configure(void *data, struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height, struct wl_array *states) {
  972. WindowState *ws = (WindowState *)data;
  973. ERR_FAIL_NULL(ws);
  974. // Expect the window to be in a plain state. It will get properly set if the
  975. // compositor reports otherwise below.
  976. ws->mode = DisplayServer::WINDOW_MODE_WINDOWED;
  977. ws->suspended = false;
  978. uint32_t *state = nullptr;
  979. wl_array_for_each(state, states) {
  980. switch (*state) {
  981. case XDG_TOPLEVEL_STATE_MAXIMIZED: {
  982. ws->mode = DisplayServer::WINDOW_MODE_MAXIMIZED;
  983. } break;
  984. case XDG_TOPLEVEL_STATE_FULLSCREEN: {
  985. ws->mode = DisplayServer::WINDOW_MODE_FULLSCREEN;
  986. } break;
  987. case XDG_TOPLEVEL_STATE_SUSPENDED: {
  988. ws->suspended = true;
  989. } break;
  990. default: {
  991. // We don't care about the other states (for now).
  992. } break;
  993. }
  994. }
  995. if (width != 0 && height != 0) {
  996. window_state_update_size(ws, width, height);
  997. }
  998. DEBUG_LOG_WAYLAND_THREAD(vformat("XDG toplevel on configure width %d height %d.", width, height));
  999. }
  1000. void WaylandThread::_xdg_toplevel_on_close(void *data, struct xdg_toplevel *xdg_toplevel) {
  1001. WindowState *ws = (WindowState *)data;
  1002. ERR_FAIL_NULL(ws);
  1003. Ref<WindowEventMessage> msg;
  1004. msg.instantiate();
  1005. msg->id = ws->id;
  1006. msg->event = DisplayServer::WINDOW_EVENT_CLOSE_REQUEST;
  1007. ws->wayland_thread->push_message(msg);
  1008. }
  1009. void WaylandThread::_xdg_toplevel_on_configure_bounds(void *data, struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height) {
  1010. }
  1011. void WaylandThread::_xdg_toplevel_on_wm_capabilities(void *data, struct xdg_toplevel *xdg_toplevel, struct wl_array *capabilities) {
  1012. WindowState *ws = (WindowState *)data;
  1013. ERR_FAIL_NULL(ws);
  1014. ws->can_maximize = false;
  1015. ws->can_fullscreen = false;
  1016. ws->can_minimize = false;
  1017. uint32_t *capability = nullptr;
  1018. wl_array_for_each(capability, capabilities) {
  1019. switch (*capability) {
  1020. case XDG_TOPLEVEL_WM_CAPABILITIES_MAXIMIZE: {
  1021. ws->can_maximize = true;
  1022. } break;
  1023. case XDG_TOPLEVEL_WM_CAPABILITIES_FULLSCREEN: {
  1024. ws->can_fullscreen = true;
  1025. } break;
  1026. case XDG_TOPLEVEL_WM_CAPABILITIES_MINIMIZE: {
  1027. ws->can_minimize = true;
  1028. } break;
  1029. default: {
  1030. } break;
  1031. }
  1032. }
  1033. }
  1034. void WaylandThread::_xdg_popup_on_configure(void *data, struct xdg_popup *xdg_popup, int32_t x, int32_t y, int32_t width, int32_t height) {
  1035. WindowState *ws = (WindowState *)data;
  1036. ERR_FAIL_NULL(ws);
  1037. if (width != 0 && height != 0) {
  1038. window_state_update_size(ws, width, height);
  1039. }
  1040. WindowState *parent = ws->wayland_thread->window_get_state(ws->parent_id);
  1041. ERR_FAIL_NULL(parent);
  1042. Point2i pos = Point2i(x, y);
  1043. #ifdef LIBDECOR_ENABLED
  1044. if (parent->libdecor_frame) {
  1045. int translated_x = x;
  1046. int translated_y = y;
  1047. libdecor_frame_translate_coordinate(parent->libdecor_frame, x, y, &translated_x, &translated_y);
  1048. pos.x = translated_x;
  1049. pos.y = translated_y;
  1050. }
  1051. #endif
  1052. // Looks like the position returned here is relative to the parent. We have to
  1053. // accumulate it or there's gonna be a lot of confusion godot-side.
  1054. pos += parent->rect.position;
  1055. if (ws->rect.position != pos) {
  1056. DEBUG_LOG_WAYLAND_THREAD(vformat("Repositioning popup %d from %s to %s", ws->id, ws->rect.position, pos));
  1057. double parent_scale = window_state_get_scale_factor(parent);
  1058. ws->rect.position = pos;
  1059. Ref<WindowRectMessage> rect_msg;
  1060. rect_msg.instantiate();
  1061. rect_msg->id = ws->id;
  1062. rect_msg->rect.position = scale_vector2i(ws->rect.position, parent_scale);
  1063. rect_msg->rect.size = scale_vector2i(ws->rect.size, parent_scale);
  1064. ws->wayland_thread->push_message(rect_msg);
  1065. }
  1066. DEBUG_LOG_WAYLAND_THREAD(vformat("xdg popup on configure x%d y%d w%d h%d", x, y, width, height));
  1067. }
  1068. void WaylandThread::_xdg_popup_on_popup_done(void *data, struct xdg_popup *xdg_popup) {
  1069. WindowState *ws = (WindowState *)data;
  1070. ERR_FAIL_NULL(ws);
  1071. Ref<WindowEventMessage> ev_msg;
  1072. ev_msg.instantiate();
  1073. ev_msg->id = ws->id;
  1074. ev_msg->event = DisplayServer::WINDOW_EVENT_FORCE_CLOSE;
  1075. ws->wayland_thread->push_message(ev_msg);
  1076. }
  1077. void WaylandThread::_xdg_popup_on_repositioned(void *data, struct xdg_popup *xdg_popup, uint32_t token) {
  1078. DEBUG_LOG_WAYLAND_THREAD(vformat("stub xdg popup repositioned %x", token));
  1079. }
  1080. // NOTE: Deprecated.
  1081. void WaylandThread::_xdg_exported_v1_on_handle(void *data, zxdg_exported_v1 *exported, const char *handle) {
  1082. WindowState *ws = (WindowState *)data;
  1083. ERR_FAIL_NULL(ws);
  1084. ws->exported_handle = vformat("wayland:%s", String::utf8(handle));
  1085. }
  1086. void WaylandThread::_xdg_exported_v2_on_handle(void *data, zxdg_exported_v2 *exported, const char *handle) {
  1087. WindowState *ws = (WindowState *)data;
  1088. ERR_FAIL_NULL(ws);
  1089. ws->exported_handle = vformat("wayland:%s", String::utf8(handle));
  1090. }
  1091. void WaylandThread::_xdg_toplevel_decoration_on_configure(void *data, struct zxdg_toplevel_decoration_v1 *xdg_toplevel_decoration, uint32_t mode) {
  1092. if (mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE) {
  1093. #ifdef LIBDECOR_ENABLED
  1094. WARN_PRINT_ONCE("Native client side decorations are not yet supported without libdecor!");
  1095. #else
  1096. WARN_PRINT_ONCE("Native client side decorations are not yet supported!");
  1097. #endif // LIBDECOR_ENABLED
  1098. }
  1099. }
  1100. #ifdef LIBDECOR_ENABLED
  1101. void WaylandThread::libdecor_on_error(struct libdecor *context, enum libdecor_error error, const char *message) {
  1102. ERR_PRINT(vformat("libdecor error %d: %s", error, message));
  1103. }
  1104. // NOTE: This is pretty much a reimplementation of _xdg_surface_on_configure
  1105. // and _xdg_toplevel_on_configure. Libdecor really likes wrapping everything,
  1106. // forcing us to do stuff like this.
  1107. void WaylandThread::libdecor_frame_on_configure(struct libdecor_frame *frame, struct libdecor_configuration *configuration, void *user_data) {
  1108. WindowState *ws = (WindowState *)user_data;
  1109. ERR_FAIL_NULL(ws);
  1110. int width = 0;
  1111. int height = 0;
  1112. ws->pending_libdecor_configuration = configuration;
  1113. if (!libdecor_configuration_get_content_size(configuration, frame, &width, &height)) {
  1114. // The configuration doesn't have a size. We'll use the one already set in the window.
  1115. width = ws->rect.size.width;
  1116. height = ws->rect.size.height;
  1117. }
  1118. ERR_FAIL_COND_MSG(width == 0 || height == 0, "Window has invalid size.");
  1119. libdecor_window_state window_state = LIBDECOR_WINDOW_STATE_NONE;
  1120. // Expect the window to be in a plain state. It will get properly set if the
  1121. // compositor reports otherwise below.
  1122. ws->mode = DisplayServer::WINDOW_MODE_WINDOWED;
  1123. ws->suspended = false;
  1124. if (libdecor_configuration_get_window_state(configuration, &window_state)) {
  1125. if (window_state & LIBDECOR_WINDOW_STATE_MAXIMIZED) {
  1126. ws->mode = DisplayServer::WINDOW_MODE_MAXIMIZED;
  1127. }
  1128. if (window_state & LIBDECOR_WINDOW_STATE_FULLSCREEN) {
  1129. ws->mode = DisplayServer::WINDOW_MODE_FULLSCREEN;
  1130. }
  1131. if (window_state & LIBDECOR_WINDOW_STATE_SUSPENDED) {
  1132. ws->suspended = true;
  1133. }
  1134. }
  1135. window_state_update_size(ws, width, height);
  1136. DEBUG_LOG_WAYLAND_THREAD(vformat("libdecor frame on configure rect %s", ws->rect));
  1137. }
  1138. void WaylandThread::libdecor_frame_on_close(struct libdecor_frame *frame, void *user_data) {
  1139. WindowState *ws = (WindowState *)user_data;
  1140. ERR_FAIL_NULL(ws);
  1141. Ref<WindowEventMessage> winevent_msg;
  1142. winevent_msg.instantiate();
  1143. winevent_msg->id = ws->id;
  1144. winevent_msg->event = DisplayServer::WINDOW_EVENT_CLOSE_REQUEST;
  1145. ws->wayland_thread->push_message(winevent_msg);
  1146. DEBUG_LOG_WAYLAND_THREAD("libdecor frame on close");
  1147. }
  1148. void WaylandThread::libdecor_frame_on_commit(struct libdecor_frame *frame, void *user_data) {
  1149. // We're skipping this as we don't really care about libdecor's commit for
  1150. // atomicity reasons. See `_frame_wl_callback_on_done` for more info.
  1151. DEBUG_LOG_WAYLAND_THREAD("libdecor frame on commit");
  1152. }
  1153. void WaylandThread::libdecor_frame_on_dismiss_popup(struct libdecor_frame *frame, const char *seat_name, void *user_data) {
  1154. }
  1155. #endif // LIBDECOR_ENABLED
  1156. void WaylandThread::_wl_seat_on_capabilities(void *data, struct wl_seat *wl_seat, uint32_t capabilities) {
  1157. SeatState *ss = (SeatState *)data;
  1158. ERR_FAIL_NULL(ss);
  1159. // TODO: Handle touch.
  1160. // Pointer handling.
  1161. if (capabilities & WL_SEAT_CAPABILITY_POINTER) {
  1162. if (!ss->wl_pointer) {
  1163. ss->cursor_surface = wl_compositor_create_surface(ss->registry->wl_compositor);
  1164. wl_surface_commit(ss->cursor_surface);
  1165. ss->wl_pointer = wl_seat_get_pointer(wl_seat);
  1166. wl_pointer_add_listener(ss->wl_pointer, &wl_pointer_listener, ss);
  1167. if (ss->registry->wp_cursor_shape_manager) {
  1168. ss->wp_cursor_shape_device = wp_cursor_shape_manager_v1_get_pointer(ss->registry->wp_cursor_shape_manager, ss->wl_pointer);
  1169. }
  1170. if (ss->registry->wp_relative_pointer_manager) {
  1171. ss->wp_relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(ss->registry->wp_relative_pointer_manager, ss->wl_pointer);
  1172. zwp_relative_pointer_v1_add_listener(ss->wp_relative_pointer, &wp_relative_pointer_listener, ss);
  1173. }
  1174. if (ss->registry->wp_pointer_gestures) {
  1175. ss->wp_pointer_gesture_pinch = zwp_pointer_gestures_v1_get_pinch_gesture(ss->registry->wp_pointer_gestures, ss->wl_pointer);
  1176. zwp_pointer_gesture_pinch_v1_add_listener(ss->wp_pointer_gesture_pinch, &wp_pointer_gesture_pinch_listener, ss);
  1177. }
  1178. // TODO: Constrain new pointers if the global mouse mode is constrained.
  1179. }
  1180. } else {
  1181. if (ss->cursor_frame_callback) {
  1182. // Just in case. I got bitten by weird race-like conditions already.
  1183. wl_callback_set_user_data(ss->cursor_frame_callback, nullptr);
  1184. wl_callback_destroy(ss->cursor_frame_callback);
  1185. ss->cursor_frame_callback = nullptr;
  1186. }
  1187. if (ss->cursor_surface) {
  1188. wl_surface_destroy(ss->cursor_surface);
  1189. ss->cursor_surface = nullptr;
  1190. }
  1191. if (ss->wl_pointer) {
  1192. wl_pointer_destroy(ss->wl_pointer);
  1193. ss->wl_pointer = nullptr;
  1194. }
  1195. if (ss->wp_cursor_shape_device) {
  1196. wp_cursor_shape_device_v1_destroy(ss->wp_cursor_shape_device);
  1197. ss->wp_cursor_shape_device = nullptr;
  1198. }
  1199. if (ss->wp_relative_pointer) {
  1200. zwp_relative_pointer_v1_destroy(ss->wp_relative_pointer);
  1201. ss->wp_relative_pointer = nullptr;
  1202. }
  1203. if (ss->wp_confined_pointer) {
  1204. zwp_confined_pointer_v1_destroy(ss->wp_confined_pointer);
  1205. ss->wp_confined_pointer = nullptr;
  1206. }
  1207. if (ss->wp_locked_pointer) {
  1208. zwp_locked_pointer_v1_destroy(ss->wp_locked_pointer);
  1209. ss->wp_locked_pointer = nullptr;
  1210. }
  1211. }
  1212. // Keyboard handling.
  1213. if (capabilities & WL_SEAT_CAPABILITY_KEYBOARD) {
  1214. if (!ss->wl_keyboard) {
  1215. ss->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
  1216. ERR_FAIL_NULL(ss->xkb_context);
  1217. ss->wl_keyboard = wl_seat_get_keyboard(wl_seat);
  1218. wl_keyboard_add_listener(ss->wl_keyboard, &wl_keyboard_listener, ss);
  1219. }
  1220. } else {
  1221. if (ss->xkb_context) {
  1222. xkb_context_unref(ss->xkb_context);
  1223. ss->xkb_context = nullptr;
  1224. }
  1225. if (ss->wl_keyboard) {
  1226. wl_keyboard_destroy(ss->wl_keyboard);
  1227. ss->wl_keyboard = nullptr;
  1228. }
  1229. }
  1230. }
  1231. void WaylandThread::_wl_seat_on_name(void *data, struct wl_seat *wl_seat, const char *name) {
  1232. }
  1233. void WaylandThread::_cursor_frame_callback_on_done(void *data, struct wl_callback *wl_callback, uint32_t time_ms) {
  1234. wl_callback_destroy(wl_callback);
  1235. SeatState *ss = (SeatState *)data;
  1236. ERR_FAIL_NULL(ss);
  1237. ss->cursor_frame_callback = nullptr;
  1238. ss->cursor_time_ms = time_ms;
  1239. seat_state_update_cursor(ss);
  1240. }
  1241. void WaylandThread::_wl_pointer_on_enter(void *data, struct wl_pointer *wl_pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t surface_x, wl_fixed_t surface_y) {
  1242. WindowState *ws = wl_surface_get_window_state(surface);
  1243. if (!ws) {
  1244. return;
  1245. }
  1246. SeatState *ss = (SeatState *)data;
  1247. ERR_FAIL_NULL(ss);
  1248. ERR_FAIL_NULL(ss->cursor_surface);
  1249. PointerData &pd = ss->pointer_data_buffer;
  1250. ss->pointer_enter_serial = serial;
  1251. pd.pointed_id = ws->id;
  1252. pd.last_pointed_id = ws->id;
  1253. pd.position.x = wl_fixed_to_double(surface_x);
  1254. pd.position.y = wl_fixed_to_double(surface_y);
  1255. seat_state_update_cursor(ss);
  1256. DEBUG_LOG_WAYLAND_THREAD(vformat("Pointer entered window %d.", ws->id));
  1257. if (wl_pointer_get_version(wl_pointer) < WL_POINTER_FRAME_SINCE_VERSION) {
  1258. _wl_pointer_on_frame(data, wl_pointer);
  1259. }
  1260. }
  1261. void WaylandThread::_wl_pointer_on_leave(void *data, struct wl_pointer *wl_pointer, uint32_t serial, struct wl_surface *surface) {
  1262. // NOTE: `surface` will probably be null when the surface is destroyed.
  1263. // See: https://gitlab.freedesktop.org/wayland/wayland/-/issues/366
  1264. // See: https://gitlab.freedesktop.org/wayland/wayland/-/issues/465
  1265. SeatState *ss = (SeatState *)data;
  1266. ERR_FAIL_NULL(ss);
  1267. PointerData &pd = ss->pointer_data_buffer;
  1268. if (pd.pointed_id == DisplayServer::INVALID_WINDOW_ID) {
  1269. // We're probably on a decoration or some other third-party thing.
  1270. return;
  1271. }
  1272. DisplayServer::WindowID id = pd.pointed_id;
  1273. pd.pointed_id = DisplayServer::INVALID_WINDOW_ID;
  1274. pd.pressed_button_mask.clear();
  1275. DEBUG_LOG_WAYLAND_THREAD(vformat("Pointer left window %d.", id));
  1276. if (wl_pointer_get_version(wl_pointer) < WL_POINTER_FRAME_SINCE_VERSION) {
  1277. _wl_pointer_on_frame(data, wl_pointer);
  1278. }
  1279. }
  1280. void WaylandThread::_wl_pointer_on_motion(void *data, struct wl_pointer *wl_pointer, uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
  1281. SeatState *ss = (SeatState *)data;
  1282. ERR_FAIL_NULL(ss);
  1283. PointerData &pd = ss->pointer_data_buffer;
  1284. pd.position.x = wl_fixed_to_double(surface_x);
  1285. pd.position.y = wl_fixed_to_double(surface_y);
  1286. pd.motion_time = time;
  1287. if (wl_pointer_get_version(wl_pointer) < WL_POINTER_FRAME_SINCE_VERSION) {
  1288. _wl_pointer_on_frame(data, wl_pointer);
  1289. }
  1290. }
  1291. void WaylandThread::_wl_pointer_on_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
  1292. SeatState *ss = (SeatState *)data;
  1293. ERR_FAIL_NULL(ss);
  1294. PointerData &pd = ss->pointer_data_buffer;
  1295. MouseButton button_pressed = MouseButton::NONE;
  1296. switch (button) {
  1297. case BTN_LEFT:
  1298. button_pressed = MouseButton::LEFT;
  1299. break;
  1300. case BTN_MIDDLE:
  1301. button_pressed = MouseButton::MIDDLE;
  1302. break;
  1303. case BTN_RIGHT:
  1304. button_pressed = MouseButton::RIGHT;
  1305. break;
  1306. case BTN_EXTRA:
  1307. button_pressed = MouseButton::MB_XBUTTON1;
  1308. break;
  1309. case BTN_SIDE:
  1310. button_pressed = MouseButton::MB_XBUTTON2;
  1311. break;
  1312. default: {
  1313. }
  1314. }
  1315. MouseButtonMask mask = mouse_button_to_mask(button_pressed);
  1316. if (state & WL_POINTER_BUTTON_STATE_PRESSED) {
  1317. pd.pressed_button_mask.set_flag(mask);
  1318. pd.last_button_pressed = button_pressed;
  1319. pd.double_click_begun = true;
  1320. } else {
  1321. pd.pressed_button_mask.clear_flag(mask);
  1322. }
  1323. pd.button_time = time;
  1324. pd.button_serial = serial;
  1325. if (wl_pointer_get_version(wl_pointer) < WL_POINTER_FRAME_SINCE_VERSION) {
  1326. _wl_pointer_on_frame(data, wl_pointer);
  1327. }
  1328. }
  1329. void WaylandThread::_wl_pointer_on_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {
  1330. SeatState *ss = (SeatState *)data;
  1331. ERR_FAIL_NULL(ss);
  1332. PointerData &pd = ss->pointer_data_buffer;
  1333. switch (axis) {
  1334. case WL_POINTER_AXIS_VERTICAL_SCROLL: {
  1335. pd.scroll_vector.y = wl_fixed_to_double(value);
  1336. } break;
  1337. case WL_POINTER_AXIS_HORIZONTAL_SCROLL: {
  1338. pd.scroll_vector.x = wl_fixed_to_double(value);
  1339. } break;
  1340. }
  1341. pd.button_time = time;
  1342. if (wl_pointer_get_version(wl_pointer) < WL_POINTER_FRAME_SINCE_VERSION) {
  1343. _wl_pointer_on_frame(data, wl_pointer);
  1344. }
  1345. }
  1346. void WaylandThread::_wl_pointer_on_frame(void *data, struct wl_pointer *wl_pointer) {
  1347. SeatState *ss = (SeatState *)data;
  1348. ERR_FAIL_NULL(ss);
  1349. WaylandThread *wayland_thread = ss->wayland_thread;
  1350. ERR_FAIL_NULL(wayland_thread);
  1351. PointerData &old_pd = ss->pointer_data;
  1352. PointerData &pd = ss->pointer_data_buffer;
  1353. if (pd.pointed_id != old_pd.pointed_id) {
  1354. if (old_pd.pointed_id != DisplayServer::INVALID_WINDOW_ID) {
  1355. Ref<WindowEventMessage> msg;
  1356. msg.instantiate();
  1357. msg->id = old_pd.pointed_id;
  1358. msg->event = DisplayServer::WINDOW_EVENT_MOUSE_EXIT;
  1359. wayland_thread->push_message(msg);
  1360. }
  1361. if (pd.pointed_id != DisplayServer::INVALID_WINDOW_ID) {
  1362. Ref<WindowEventMessage> msg;
  1363. msg.instantiate();
  1364. msg->id = pd.pointed_id;
  1365. msg->event = DisplayServer::WINDOW_EVENT_MOUSE_ENTER;
  1366. wayland_thread->push_message(msg);
  1367. }
  1368. }
  1369. WindowState *ws = nullptr;
  1370. // NOTE: At least on sway, with wl_pointer version 5 or greater,
  1371. // wl_pointer::leave might be emitted with other events (like
  1372. // wl_pointer::button) within the same wl_pointer::frame. Because of this, we
  1373. // need to account for when the currently pointed window might be invalid
  1374. // (third-party or even none) and fall back to the old one.
  1375. if (pd.pointed_id != DisplayServer::INVALID_WINDOW_ID) {
  1376. ws = ss->wayland_thread->window_get_state(pd.pointed_id);
  1377. ERR_FAIL_NULL(ws);
  1378. } else if (old_pd.pointed_id != DisplayServer::INVALID_WINDOW_ID) {
  1379. ws = ss->wayland_thread->window_get_state(old_pd.pointed_id);
  1380. ERR_FAIL_NULL(ws);
  1381. }
  1382. if (ws == nullptr) {
  1383. // We're probably on a decoration or some other third-party thing. Let's
  1384. // "commit" the data and call it a day.
  1385. old_pd = pd;
  1386. return;
  1387. }
  1388. double scale = window_state_get_scale_factor(ws);
  1389. wayland_thread->_set_current_seat(ss->wl_seat);
  1390. if (old_pd.motion_time != pd.motion_time || old_pd.relative_motion_time != pd.relative_motion_time) {
  1391. Ref<InputEventMouseMotion> mm;
  1392. mm.instantiate();
  1393. // Set all pressed modifiers.
  1394. mm->set_shift_pressed(ss->shift_pressed);
  1395. mm->set_ctrl_pressed(ss->ctrl_pressed);
  1396. mm->set_alt_pressed(ss->alt_pressed);
  1397. mm->set_meta_pressed(ss->meta_pressed);
  1398. mm->set_window_id(ws->id);
  1399. mm->set_button_mask(pd.pressed_button_mask);
  1400. mm->set_position(pd.position * scale);
  1401. mm->set_global_position(pd.position * scale);
  1402. Vector2 pos_delta = (pd.position - old_pd.position) * scale;
  1403. if (old_pd.relative_motion_time != pd.relative_motion_time) {
  1404. uint32_t time_delta = pd.relative_motion_time - old_pd.relative_motion_time;
  1405. mm->set_relative(pd.relative_motion * scale);
  1406. mm->set_velocity((Vector2)pos_delta / time_delta);
  1407. } else {
  1408. // The spec includes the possibility of having motion events without an
  1409. // associated relative motion event. If that's the case, fallback to a
  1410. // simple delta of the position. The captured mouse won't report the
  1411. // relative speed anymore though.
  1412. uint32_t time_delta = pd.motion_time - old_pd.motion_time;
  1413. mm->set_relative(pos_delta);
  1414. mm->set_velocity((Vector2)pos_delta / time_delta);
  1415. }
  1416. mm->set_relative_screen_position(mm->get_relative());
  1417. mm->set_screen_velocity(mm->get_velocity());
  1418. Ref<InputEventMessage> msg;
  1419. msg.instantiate();
  1420. msg->event = mm;
  1421. wayland_thread->push_message(msg);
  1422. }
  1423. if (pd.discrete_scroll_vector_120 - old_pd.discrete_scroll_vector_120 != Vector2i()) {
  1424. // This is a discrete scroll (eg. from a scroll wheel), so we'll just emit
  1425. // scroll wheel buttons.
  1426. if (pd.scroll_vector.y != 0) {
  1427. MouseButton button = pd.scroll_vector.y > 0 ? MouseButton::WHEEL_DOWN : MouseButton::WHEEL_UP;
  1428. pd.pressed_button_mask.set_flag(mouse_button_to_mask(button));
  1429. }
  1430. if (pd.scroll_vector.x != 0) {
  1431. MouseButton button = pd.scroll_vector.x > 0 ? MouseButton::WHEEL_RIGHT : MouseButton::WHEEL_LEFT;
  1432. pd.pressed_button_mask.set_flag(mouse_button_to_mask(button));
  1433. }
  1434. } else {
  1435. if (pd.scroll_vector - old_pd.scroll_vector != Vector2()) {
  1436. // This is a continuous scroll, so we'll emit a pan gesture.
  1437. Ref<InputEventPanGesture> pg;
  1438. pg.instantiate();
  1439. // Set all pressed modifiers.
  1440. pg->set_shift_pressed(ss->shift_pressed);
  1441. pg->set_ctrl_pressed(ss->ctrl_pressed);
  1442. pg->set_alt_pressed(ss->alt_pressed);
  1443. pg->set_meta_pressed(ss->meta_pressed);
  1444. pg->set_position(pd.position * scale);
  1445. pg->set_window_id(ws->id);
  1446. pg->set_delta(pd.scroll_vector);
  1447. Ref<InputEventMessage> msg;
  1448. msg.instantiate();
  1449. msg->event = pg;
  1450. wayland_thread->push_message(msg);
  1451. }
  1452. }
  1453. if (old_pd.pressed_button_mask != pd.pressed_button_mask) {
  1454. BitField<MouseButtonMask> pressed_mask_delta = old_pd.pressed_button_mask.get_different(pd.pressed_button_mask);
  1455. const MouseButton buttons_to_test[] = {
  1456. MouseButton::LEFT,
  1457. MouseButton::MIDDLE,
  1458. MouseButton::RIGHT,
  1459. MouseButton::WHEEL_UP,
  1460. MouseButton::WHEEL_DOWN,
  1461. MouseButton::WHEEL_LEFT,
  1462. MouseButton::WHEEL_RIGHT,
  1463. MouseButton::MB_XBUTTON1,
  1464. MouseButton::MB_XBUTTON2,
  1465. };
  1466. for (MouseButton test_button : buttons_to_test) {
  1467. MouseButtonMask test_button_mask = mouse_button_to_mask(test_button);
  1468. if (pressed_mask_delta.has_flag(test_button_mask)) {
  1469. Ref<InputEventMouseButton> mb;
  1470. mb.instantiate();
  1471. // Set all pressed modifiers.
  1472. mb->set_shift_pressed(ss->shift_pressed);
  1473. mb->set_ctrl_pressed(ss->ctrl_pressed);
  1474. mb->set_alt_pressed(ss->alt_pressed);
  1475. mb->set_meta_pressed(ss->meta_pressed);
  1476. mb->set_window_id(ws->id);
  1477. mb->set_position(pd.position * scale);
  1478. mb->set_global_position(pd.position * scale);
  1479. if (test_button == MouseButton::WHEEL_UP || test_button == MouseButton::WHEEL_DOWN) {
  1480. // If this is a discrete scroll, specify how many "clicks" it did for this
  1481. // pointer frame.
  1482. mb->set_factor(Math::abs(pd.discrete_scroll_vector_120.y / (float)120));
  1483. }
  1484. if (test_button == MouseButton::WHEEL_RIGHT || test_button == MouseButton::WHEEL_LEFT) {
  1485. // If this is a discrete scroll, specify how many "clicks" it did for this
  1486. // pointer frame.
  1487. mb->set_factor(std::abs(pd.discrete_scroll_vector_120.x / (float)120));
  1488. }
  1489. mb->set_button_mask(pd.pressed_button_mask);
  1490. mb->set_button_index(test_button);
  1491. mb->set_pressed(pd.pressed_button_mask.has_flag(test_button_mask));
  1492. // We have to set the last position pressed here as we can't take for
  1493. // granted what the individual events might have seen due to them not having
  1494. // a guaranteed order.
  1495. if (mb->is_pressed()) {
  1496. pd.last_pressed_position = pd.position;
  1497. }
  1498. if (old_pd.double_click_begun && mb->is_pressed() && pd.last_button_pressed == old_pd.last_button_pressed && (pd.button_time - old_pd.button_time) < 400 && Vector2(old_pd.last_pressed_position * scale).distance_to(Vector2(pd.last_pressed_position * scale)) < 5) {
  1499. pd.double_click_begun = false;
  1500. mb->set_double_click(true);
  1501. }
  1502. Ref<InputEventMessage> msg;
  1503. msg.instantiate();
  1504. msg->event = mb;
  1505. wayland_thread->push_message(msg);
  1506. // Send an event resetting immediately the wheel key.
  1507. // Wayland specification defines axis_stop events as optional and says to
  1508. // treat all axis events as unterminated. As such, we have to manually do
  1509. // it ourselves.
  1510. if (test_button == MouseButton::WHEEL_UP || test_button == MouseButton::WHEEL_DOWN || test_button == MouseButton::WHEEL_LEFT || test_button == MouseButton::WHEEL_RIGHT) {
  1511. // FIXME: This is ugly, I can't find a clean way to clone an InputEvent.
  1512. // This works for now, despite being horrible.
  1513. Ref<InputEventMouseButton> wh_up;
  1514. wh_up.instantiate();
  1515. wh_up->set_window_id(ws->id);
  1516. wh_up->set_position(pd.position * scale);
  1517. wh_up->set_global_position(pd.position * scale);
  1518. // We have to unset the button to avoid it getting stuck.
  1519. pd.pressed_button_mask.clear_flag(test_button_mask);
  1520. wh_up->set_button_mask(pd.pressed_button_mask);
  1521. wh_up->set_button_index(test_button);
  1522. wh_up->set_pressed(false);
  1523. Ref<InputEventMessage> msg_up;
  1524. msg_up.instantiate();
  1525. msg_up->event = wh_up;
  1526. wayland_thread->push_message(msg_up);
  1527. }
  1528. }
  1529. }
  1530. }
  1531. // Reset the scroll vectors as we already handled them.
  1532. pd.scroll_vector = Vector2();
  1533. pd.discrete_scroll_vector_120 = Vector2i();
  1534. // Update the data all getters read. Wayland's specification requires us to do
  1535. // this, since all pointer actions are sent in individual events.
  1536. old_pd = pd;
  1537. }
  1538. void WaylandThread::_wl_pointer_on_axis_source(void *data, struct wl_pointer *wl_pointer, uint32_t axis_source) {
  1539. SeatState *ss = (SeatState *)data;
  1540. ERR_FAIL_NULL(ss);
  1541. ss->pointer_data_buffer.scroll_type = axis_source;
  1542. }
  1543. void WaylandThread::_wl_pointer_on_axis_stop(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis) {
  1544. }
  1545. // NOTE: This event is deprecated since version 8 and superseded by
  1546. // `wl_pointer::axis_value120`. This thus converts the data to its
  1547. // fraction-of-120 format.
  1548. void WaylandThread::_wl_pointer_on_axis_discrete(void *data, struct wl_pointer *wl_pointer, uint32_t axis, int32_t discrete) {
  1549. SeatState *ss = (SeatState *)data;
  1550. ERR_FAIL_NULL(ss);
  1551. PointerData &pd = ss->pointer_data_buffer;
  1552. // NOTE: We can allow ourselves to not accumulate this data (and thus just
  1553. // assign it) as the spec guarantees only one event per axis type.
  1554. if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
  1555. pd.discrete_scroll_vector_120.y = discrete * 120;
  1556. }
  1557. if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
  1558. pd.discrete_scroll_vector_120.x = discrete * 120;
  1559. }
  1560. }
  1561. // Supersedes `wl_pointer::axis_discrete` Since version 8.
  1562. void WaylandThread::_wl_pointer_on_axis_value120(void *data, struct wl_pointer *wl_pointer, uint32_t axis, int32_t value120) {
  1563. SeatState *ss = (SeatState *)data;
  1564. ERR_FAIL_NULL(ss);
  1565. PointerData &pd = ss->pointer_data_buffer;
  1566. if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
  1567. pd.discrete_scroll_vector_120.y += value120;
  1568. }
  1569. if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
  1570. pd.discrete_scroll_vector_120.x += value120;
  1571. }
  1572. }
  1573. // TODO: Add support to this event.
  1574. void WaylandThread::_wl_pointer_on_axis_relative_direction(void *data, struct wl_pointer *wl_pointer, uint32_t axis, uint32_t direction) {
  1575. }
  1576. void WaylandThread::_wl_keyboard_on_keymap(void *data, struct wl_keyboard *wl_keyboard, uint32_t format, int32_t fd, uint32_t size) {
  1577. ERR_FAIL_COND_MSG(format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, "Unsupported keymap format announced from the Wayland compositor.");
  1578. SeatState *ss = (SeatState *)data;
  1579. ERR_FAIL_NULL(ss);
  1580. if (ss->keymap_buffer) {
  1581. // We have already a mapped buffer, so we unmap it. There's no need to reset
  1582. // its pointer or size, as we're gonna set them below.
  1583. munmap((void *)ss->keymap_buffer, ss->keymap_buffer_size);
  1584. ss->keymap_buffer = nullptr;
  1585. }
  1586. ss->keymap_buffer = (const char *)mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0);
  1587. ss->keymap_buffer_size = size;
  1588. xkb_keymap_unref(ss->xkb_keymap);
  1589. ss->xkb_keymap = xkb_keymap_new_from_string(ss->xkb_context, ss->keymap_buffer,
  1590. XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
  1591. xkb_state_unref(ss->xkb_state);
  1592. ss->xkb_state = xkb_state_new(ss->xkb_keymap);
  1593. }
  1594. void WaylandThread::_wl_keyboard_on_enter(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys) {
  1595. WindowState *ws = wl_surface_get_window_state(surface);
  1596. if (!ws) {
  1597. return;
  1598. }
  1599. SeatState *ss = (SeatState *)data;
  1600. ERR_FAIL_NULL(ss);
  1601. WaylandThread *wayland_thread = ss->wayland_thread;
  1602. ERR_FAIL_NULL(wayland_thread);
  1603. ss->focused_id = ws->id;
  1604. wayland_thread->_set_current_seat(ss->wl_seat);
  1605. Ref<WindowEventMessage> msg;
  1606. msg.instantiate();
  1607. msg->id = ws->id;
  1608. msg->event = DisplayServer::WINDOW_EVENT_FOCUS_IN;
  1609. wayland_thread->push_message(msg);
  1610. DEBUG_LOG_WAYLAND_THREAD(vformat("Keyboard focused window %d.", ws->id));
  1611. }
  1612. void WaylandThread::_wl_keyboard_on_leave(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *surface) {
  1613. // NOTE: `surface` will probably be null when the surface is destroyed.
  1614. // See: https://gitlab.freedesktop.org/wayland/wayland/-/issues/366
  1615. // See: https://gitlab.freedesktop.org/wayland/wayland/-/issues/465
  1616. if (surface && !wl_proxy_is_godot((struct wl_proxy *)surface)) {
  1617. return;
  1618. }
  1619. SeatState *ss = (SeatState *)data;
  1620. ERR_FAIL_NULL(ss);
  1621. WaylandThread *wayland_thread = ss->wayland_thread;
  1622. ERR_FAIL_NULL(wayland_thread);
  1623. ss->repeating_keycode = XKB_KEYCODE_INVALID;
  1624. if (ss->focused_id == DisplayServer::INVALID_WINDOW_ID) {
  1625. // We're probably on a decoration or some other third-party thing.
  1626. return;
  1627. }
  1628. WindowState *ws = wayland_thread->window_get_state(ss->focused_id);
  1629. ERR_FAIL_NULL(ws);
  1630. ss->focused_id = DisplayServer::INVALID_WINDOW_ID;
  1631. Ref<WindowEventMessage> msg;
  1632. msg.instantiate();
  1633. msg->id = ws->id;
  1634. msg->event = DisplayServer::WINDOW_EVENT_FOCUS_OUT;
  1635. wayland_thread->push_message(msg);
  1636. DEBUG_LOG_WAYLAND_THREAD(vformat("Keyboard unfocused window %d.", ws->id));
  1637. }
  1638. void WaylandThread::_wl_keyboard_on_key(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) {
  1639. SeatState *ss = (SeatState *)data;
  1640. ERR_FAIL_NULL(ss);
  1641. if (ss->focused_id == DisplayServer::INVALID_WINDOW_ID) {
  1642. return;
  1643. }
  1644. WaylandThread *wayland_thread = ss->wayland_thread;
  1645. ERR_FAIL_NULL(wayland_thread);
  1646. // We have to add 8 to the scancode to get an XKB-compatible keycode.
  1647. xkb_keycode_t xkb_keycode = key + 8;
  1648. bool pressed = state & WL_KEYBOARD_KEY_STATE_PRESSED;
  1649. if (pressed) {
  1650. if (xkb_keymap_key_repeats(ss->xkb_keymap, xkb_keycode)) {
  1651. ss->last_repeat_start_msec = OS::get_singleton()->get_ticks_msec();
  1652. ss->repeating_keycode = xkb_keycode;
  1653. }
  1654. ss->last_key_pressed_serial = serial;
  1655. } else if (ss->repeating_keycode == xkb_keycode) {
  1656. ss->repeating_keycode = XKB_KEYCODE_INVALID;
  1657. }
  1658. Ref<InputEventKey> k = _seat_state_get_key_event(ss, xkb_keycode, pressed);
  1659. if (k.is_null()) {
  1660. return;
  1661. }
  1662. Ref<InputEventKey> uk = _seat_state_get_unstuck_key_event(ss, xkb_keycode, pressed, k->get_keycode());
  1663. if (uk.is_valid()) {
  1664. Ref<InputEventMessage> u_msg;
  1665. u_msg.instantiate();
  1666. u_msg->event = uk;
  1667. wayland_thread->push_message(u_msg);
  1668. }
  1669. Ref<InputEventMessage> msg;
  1670. msg.instantiate();
  1671. msg->event = k;
  1672. wayland_thread->push_message(msg);
  1673. }
  1674. void WaylandThread::_wl_keyboard_on_modifiers(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {
  1675. SeatState *ss = (SeatState *)data;
  1676. ERR_FAIL_NULL(ss);
  1677. xkb_state_update_mask(ss->xkb_state, mods_depressed, mods_latched, mods_locked, ss->current_layout_index, ss->current_layout_index, group);
  1678. ss->shift_pressed = xkb_state_mod_name_is_active(ss->xkb_state, XKB_MOD_NAME_SHIFT, XKB_STATE_MODS_DEPRESSED);
  1679. ss->ctrl_pressed = xkb_state_mod_name_is_active(ss->xkb_state, XKB_MOD_NAME_CTRL, XKB_STATE_MODS_DEPRESSED);
  1680. ss->alt_pressed = xkb_state_mod_name_is_active(ss->xkb_state, XKB_MOD_NAME_ALT, XKB_STATE_MODS_DEPRESSED);
  1681. ss->meta_pressed = xkb_state_mod_name_is_active(ss->xkb_state, XKB_MOD_NAME_LOGO, XKB_STATE_MODS_DEPRESSED);
  1682. ss->current_layout_index = group;
  1683. }
  1684. void WaylandThread::_wl_keyboard_on_repeat_info(void *data, struct wl_keyboard *wl_keyboard, int32_t rate, int32_t delay) {
  1685. SeatState *ss = (SeatState *)data;
  1686. ERR_FAIL_NULL(ss);
  1687. ss->repeat_key_delay_msec = 1000 / rate;
  1688. ss->repeat_start_delay_msec = delay;
  1689. }
  1690. // NOTE: Don't forget to `memfree` the offer's state.
  1691. void WaylandThread::_wl_data_device_on_data_offer(void *data, struct wl_data_device *wl_data_device, struct wl_data_offer *id) {
  1692. wl_proxy_tag_godot((struct wl_proxy *)id);
  1693. wl_data_offer_add_listener(id, &wl_data_offer_listener, memnew(OfferState));
  1694. }
  1695. void WaylandThread::_wl_data_device_on_enter(void *data, struct wl_data_device *wl_data_device, uint32_t serial, struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y, struct wl_data_offer *id) {
  1696. WindowState *ws = wl_surface_get_window_state(surface);
  1697. if (!ws) {
  1698. return;
  1699. }
  1700. SeatState *ss = (SeatState *)data;
  1701. ERR_FAIL_NULL(ss);
  1702. ss->dnd_id = ws->id;
  1703. ss->dnd_enter_serial = serial;
  1704. ss->wl_data_offer_dnd = id;
  1705. // Godot only supports DnD file copying for now.
  1706. wl_data_offer_accept(id, serial, "text/uri-list");
  1707. wl_data_offer_set_actions(id, WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY, WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY);
  1708. }
  1709. void WaylandThread::_wl_data_device_on_leave(void *data, struct wl_data_device *wl_data_device) {
  1710. SeatState *ss = (SeatState *)data;
  1711. ERR_FAIL_NULL(ss);
  1712. if (ss->wl_data_offer_dnd) {
  1713. memdelete(wl_data_offer_get_offer_state(ss->wl_data_offer_dnd));
  1714. wl_data_offer_destroy(ss->wl_data_offer_dnd);
  1715. ss->wl_data_offer_dnd = nullptr;
  1716. ss->dnd_id = DisplayServer::INVALID_WINDOW_ID;
  1717. }
  1718. }
  1719. void WaylandThread::_wl_data_device_on_motion(void *data, struct wl_data_device *wl_data_device, uint32_t time, wl_fixed_t x, wl_fixed_t y) {
  1720. }
  1721. void WaylandThread::_wl_data_device_on_drop(void *data, struct wl_data_device *wl_data_device) {
  1722. SeatState *ss = (SeatState *)data;
  1723. ERR_FAIL_NULL(ss);
  1724. WaylandThread *wayland_thread = ss->wayland_thread;
  1725. ERR_FAIL_NULL(wayland_thread);
  1726. OfferState *os = wl_data_offer_get_offer_state(ss->wl_data_offer_dnd);
  1727. ERR_FAIL_NULL(os);
  1728. if (os) {
  1729. Ref<DropFilesEventMessage> msg;
  1730. msg.instantiate();
  1731. msg->id = ss->dnd_id;
  1732. Vector<uint8_t> list_data = _wl_data_offer_read(wayland_thread->wl_display, "text/uri-list", ss->wl_data_offer_dnd);
  1733. msg->files = String::utf8((const char *)list_data.ptr(), list_data.size()).split("\r\n", false);
  1734. for (int i = 0; i < msg->files.size(); i++) {
  1735. msg->files.write[i] = msg->files[i].replace("file://", "").uri_file_decode();
  1736. }
  1737. wayland_thread->push_message(msg);
  1738. wl_data_offer_finish(ss->wl_data_offer_dnd);
  1739. }
  1740. memdelete(wl_data_offer_get_offer_state(ss->wl_data_offer_dnd));
  1741. wl_data_offer_destroy(ss->wl_data_offer_dnd);
  1742. ss->wl_data_offer_dnd = nullptr;
  1743. ss->dnd_id = DisplayServer::INVALID_WINDOW_ID;
  1744. }
  1745. void WaylandThread::_wl_data_device_on_selection(void *data, struct wl_data_device *wl_data_device, struct wl_data_offer *id) {
  1746. SeatState *ss = (SeatState *)data;
  1747. ERR_FAIL_NULL(ss);
  1748. if (ss->wl_data_offer_selection) {
  1749. memdelete(wl_data_offer_get_offer_state(ss->wl_data_offer_selection));
  1750. wl_data_offer_destroy(ss->wl_data_offer_selection);
  1751. }
  1752. ss->wl_data_offer_selection = id;
  1753. }
  1754. void WaylandThread::_wl_data_offer_on_offer(void *data, struct wl_data_offer *wl_data_offer, const char *mime_type) {
  1755. OfferState *os = (OfferState *)data;
  1756. ERR_FAIL_NULL(os);
  1757. if (os) {
  1758. os->mime_types.insert(String::utf8(mime_type));
  1759. }
  1760. }
  1761. void WaylandThread::_wl_data_offer_on_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions) {
  1762. }
  1763. void WaylandThread::_wl_data_offer_on_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action) {
  1764. }
  1765. void WaylandThread::_wl_data_source_on_target(void *data, struct wl_data_source *wl_data_source, const char *mime_type) {
  1766. }
  1767. void WaylandThread::_wl_data_source_on_send(void *data, struct wl_data_source *wl_data_source, const char *mime_type, int32_t fd) {
  1768. SeatState *ss = (SeatState *)data;
  1769. ERR_FAIL_NULL(ss);
  1770. Vector<uint8_t> *data_to_send = nullptr;
  1771. if (wl_data_source == ss->wl_data_source_selection) {
  1772. data_to_send = &ss->selection_data;
  1773. DEBUG_LOG_WAYLAND_THREAD("Clipboard: requested selection.");
  1774. }
  1775. if (data_to_send) {
  1776. ssize_t written_bytes = 0;
  1777. bool valid_mime = false;
  1778. if (strcmp(mime_type, "text/plain;charset=utf-8") == 0) {
  1779. valid_mime = true;
  1780. } else if (strcmp(mime_type, "text/plain") == 0) {
  1781. valid_mime = true;
  1782. }
  1783. if (valid_mime) {
  1784. written_bytes = write(fd, data_to_send->ptr(), data_to_send->size());
  1785. }
  1786. if (written_bytes > 0) {
  1787. DEBUG_LOG_WAYLAND_THREAD(vformat("Clipboard: sent %d bytes.", written_bytes));
  1788. } else if (written_bytes == 0) {
  1789. DEBUG_LOG_WAYLAND_THREAD("Clipboard: no bytes sent.");
  1790. } else {
  1791. ERR_PRINT(vformat("Clipboard: write error %d.", errno));
  1792. }
  1793. }
  1794. close(fd);
  1795. }
  1796. void WaylandThread::_wl_data_source_on_cancelled(void *data, struct wl_data_source *wl_data_source) {
  1797. SeatState *ss = (SeatState *)data;
  1798. ERR_FAIL_NULL(ss);
  1799. wl_data_source_destroy(wl_data_source);
  1800. if (wl_data_source == ss->wl_data_source_selection) {
  1801. ss->wl_data_source_selection = nullptr;
  1802. ss->selection_data.clear();
  1803. DEBUG_LOG_WAYLAND_THREAD("Clipboard: selection set by another program.");
  1804. return;
  1805. }
  1806. }
  1807. void WaylandThread::_wl_data_source_on_dnd_drop_performed(void *data, struct wl_data_source *wl_data_source) {
  1808. }
  1809. void WaylandThread::_wl_data_source_on_dnd_finished(void *data, struct wl_data_source *wl_data_source) {
  1810. }
  1811. void WaylandThread::_wl_data_source_on_action(void *data, struct wl_data_source *wl_data_source, uint32_t dnd_action) {
  1812. }
  1813. void WaylandThread::_wp_fractional_scale_on_preferred_scale(void *data, struct wp_fractional_scale_v1 *wp_fractional_scale_v1, uint32_t scale) {
  1814. WindowState *ws = (WindowState *)data;
  1815. ERR_FAIL_NULL(ws);
  1816. ws->preferred_fractional_scale = (double)scale / 120;
  1817. window_state_update_size(ws, ws->rect.size.width, ws->rect.size.height);
  1818. }
  1819. void WaylandThread::_wp_relative_pointer_on_relative_motion(void *data, struct zwp_relative_pointer_v1 *wp_relative_pointer, uint32_t uptime_hi, uint32_t uptime_lo, wl_fixed_t dx, wl_fixed_t dy, wl_fixed_t dx_unaccel, wl_fixed_t dy_unaccel) {
  1820. SeatState *ss = (SeatState *)data;
  1821. ERR_FAIL_NULL(ss);
  1822. PointerData &pd = ss->pointer_data_buffer;
  1823. pd.relative_motion.x = wl_fixed_to_double(dx);
  1824. pd.relative_motion.y = wl_fixed_to_double(dy);
  1825. pd.relative_motion_time = uptime_lo;
  1826. }
  1827. void WaylandThread::_wp_pointer_gesture_pinch_on_begin(void *data, struct zwp_pointer_gesture_pinch_v1 *wp_pointer_gesture_pinch_v1, uint32_t serial, uint32_t time, struct wl_surface *surface, uint32_t fingers) {
  1828. SeatState *ss = (SeatState *)data;
  1829. ERR_FAIL_NULL(ss);
  1830. if (fingers == 2) {
  1831. ss->old_pinch_scale = wl_fixed_from_int(1);
  1832. ss->active_gesture = Gesture::MAGNIFY;
  1833. }
  1834. }
  1835. void WaylandThread::_wp_pointer_gesture_pinch_on_update(void *data, struct zwp_pointer_gesture_pinch_v1 *wp_pointer_gesture_pinch_v1, uint32_t time, wl_fixed_t dx, wl_fixed_t dy, wl_fixed_t scale, wl_fixed_t rotation) {
  1836. SeatState *ss = (SeatState *)data;
  1837. ERR_FAIL_NULL(ss);
  1838. // NOTE: From what I can tell, this and all other pointer gestures are separate
  1839. // from the "frame" mechanism of regular pointers. Thus, let's just assume we
  1840. // can read from the "committed" state.
  1841. const PointerData &pd = ss->pointer_data;
  1842. WaylandThread *wayland_thread = ss->wayland_thread;
  1843. ERR_FAIL_NULL(wayland_thread);
  1844. WindowState *ws = wayland_thread->window_get_state(pd.pointed_id);
  1845. ERR_FAIL_NULL(ws);
  1846. double win_scale = window_state_get_scale_factor(ws);
  1847. if (ss->active_gesture == Gesture::MAGNIFY) {
  1848. Ref<InputEventMagnifyGesture> mg;
  1849. mg.instantiate();
  1850. mg->set_window_id(pd.pointed_id);
  1851. if (ws) {
  1852. mg->set_window_id(ws->id);
  1853. }
  1854. // Set all pressed modifiers.
  1855. mg->set_shift_pressed(ss->shift_pressed);
  1856. mg->set_ctrl_pressed(ss->ctrl_pressed);
  1857. mg->set_alt_pressed(ss->alt_pressed);
  1858. mg->set_meta_pressed(ss->meta_pressed);
  1859. mg->set_position(pd.position * win_scale);
  1860. wl_fixed_t scale_delta = scale - ss->old_pinch_scale;
  1861. mg->set_factor(1 + wl_fixed_to_double(scale_delta));
  1862. Ref<InputEventMessage> magnify_msg;
  1863. magnify_msg.instantiate();
  1864. magnify_msg->event = mg;
  1865. // Since Wayland allows only one gesture at a time and godot instead expects
  1866. // both of them, we'll have to create two separate input events: one for
  1867. // magnification and one for panning.
  1868. Ref<InputEventPanGesture> pg;
  1869. pg.instantiate();
  1870. // Set all pressed modifiers.
  1871. pg->set_shift_pressed(ss->shift_pressed);
  1872. pg->set_ctrl_pressed(ss->ctrl_pressed);
  1873. pg->set_alt_pressed(ss->alt_pressed);
  1874. pg->set_meta_pressed(ss->meta_pressed);
  1875. pg->set_position(pd.position * win_scale);
  1876. pg->set_delta(Vector2(wl_fixed_to_double(dx), wl_fixed_to_double(dy)));
  1877. Ref<InputEventMessage> pan_msg;
  1878. pan_msg.instantiate();
  1879. pan_msg->event = pg;
  1880. wayland_thread->push_message(magnify_msg);
  1881. wayland_thread->push_message(pan_msg);
  1882. ss->old_pinch_scale = scale;
  1883. }
  1884. }
  1885. void WaylandThread::_wp_pointer_gesture_pinch_on_end(void *data, struct zwp_pointer_gesture_pinch_v1 *wp_pointer_gesture_pinch_v1, uint32_t serial, uint32_t time, int32_t cancelled) {
  1886. SeatState *ss = (SeatState *)data;
  1887. ERR_FAIL_NULL(ss);
  1888. ss->active_gesture = Gesture::NONE;
  1889. }
  1890. // NOTE: Don't forget to `memfree` the offer's state.
  1891. void WaylandThread::_wp_primary_selection_device_on_data_offer(void *data, struct zwp_primary_selection_device_v1 *wp_primary_selection_device_v1, struct zwp_primary_selection_offer_v1 *offer) {
  1892. wl_proxy_tag_godot((struct wl_proxy *)offer);
  1893. zwp_primary_selection_offer_v1_add_listener(offer, &wp_primary_selection_offer_listener, memnew(OfferState));
  1894. }
  1895. void WaylandThread::_wp_primary_selection_device_on_selection(void *data, struct zwp_primary_selection_device_v1 *wp_primary_selection_device_v1, struct zwp_primary_selection_offer_v1 *id) {
  1896. SeatState *ss = (SeatState *)data;
  1897. ERR_FAIL_NULL(ss);
  1898. if (ss->wp_primary_selection_offer) {
  1899. memfree(wp_primary_selection_offer_get_offer_state(ss->wp_primary_selection_offer));
  1900. zwp_primary_selection_offer_v1_destroy(ss->wp_primary_selection_offer);
  1901. }
  1902. ss->wp_primary_selection_offer = id;
  1903. }
  1904. void WaylandThread::_wp_primary_selection_offer_on_offer(void *data, struct zwp_primary_selection_offer_v1 *wp_primary_selection_offer_v1, const char *mime_type) {
  1905. OfferState *os = (OfferState *)data;
  1906. ERR_FAIL_NULL(os);
  1907. if (os) {
  1908. os->mime_types.insert(String::utf8(mime_type));
  1909. }
  1910. }
  1911. void WaylandThread::_wp_primary_selection_source_on_send(void *data, struct zwp_primary_selection_source_v1 *wp_primary_selection_source_v1, const char *mime_type, int32_t fd) {
  1912. SeatState *ss = (SeatState *)data;
  1913. ERR_FAIL_NULL(ss);
  1914. Vector<uint8_t> *data_to_send = nullptr;
  1915. if (wp_primary_selection_source_v1 == ss->wp_primary_selection_source) {
  1916. data_to_send = &ss->primary_data;
  1917. DEBUG_LOG_WAYLAND_THREAD("Clipboard: requested primary selection.");
  1918. }
  1919. if (data_to_send) {
  1920. ssize_t written_bytes = 0;
  1921. if (strcmp(mime_type, "text/plain") == 0) {
  1922. written_bytes = write(fd, data_to_send->ptr(), data_to_send->size());
  1923. }
  1924. if (written_bytes > 0) {
  1925. DEBUG_LOG_WAYLAND_THREAD(vformat("Clipboard: sent %d bytes.", written_bytes));
  1926. } else if (written_bytes == 0) {
  1927. DEBUG_LOG_WAYLAND_THREAD("Clipboard: no bytes sent.");
  1928. } else {
  1929. ERR_PRINT(vformat("Clipboard: write error %d.", errno));
  1930. }
  1931. }
  1932. close(fd);
  1933. }
  1934. void WaylandThread::_wp_primary_selection_source_on_cancelled(void *data, struct zwp_primary_selection_source_v1 *wp_primary_selection_source_v1) {
  1935. SeatState *ss = (SeatState *)data;
  1936. ERR_FAIL_NULL(ss);
  1937. if (wp_primary_selection_source_v1 == ss->wp_primary_selection_source) {
  1938. zwp_primary_selection_source_v1_destroy(ss->wp_primary_selection_source);
  1939. ss->wp_primary_selection_source = nullptr;
  1940. ss->primary_data.clear();
  1941. DEBUG_LOG_WAYLAND_THREAD("Clipboard: primary selection set by another program.");
  1942. return;
  1943. }
  1944. }
  1945. void WaylandThread::_wp_tablet_seat_on_tablet_added(void *data, struct zwp_tablet_seat_v2 *wp_tablet_seat_v2, struct zwp_tablet_v2 *id) {
  1946. }
  1947. void WaylandThread::_wp_tablet_seat_on_tool_added(void *data, struct zwp_tablet_seat_v2 *wp_tablet_seat_v2, struct zwp_tablet_tool_v2 *id) {
  1948. SeatState *ss = (SeatState *)data;
  1949. ERR_FAIL_NULL(ss);
  1950. TabletToolState *state = memnew(TabletToolState);
  1951. state->wl_seat = ss->wl_seat;
  1952. wl_proxy_tag_godot((struct wl_proxy *)id);
  1953. zwp_tablet_tool_v2_add_listener(id, &wp_tablet_tool_listener, state);
  1954. ss->tablet_tools.push_back(id);
  1955. }
  1956. void WaylandThread::_wp_tablet_seat_on_pad_added(void *data, struct zwp_tablet_seat_v2 *wp_tablet_seat_v2, struct zwp_tablet_pad_v2 *id) {
  1957. }
  1958. void WaylandThread::_wp_tablet_tool_on_type(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t tool_type) {
  1959. TabletToolState *state = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  1960. if (state && tool_type == ZWP_TABLET_TOOL_V2_TYPE_ERASER) {
  1961. state->is_eraser = true;
  1962. }
  1963. }
  1964. void WaylandThread::_wp_tablet_tool_on_hardware_serial(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t hardware_serial_hi, uint32_t hardware_serial_lo) {
  1965. }
  1966. void WaylandThread::_wp_tablet_tool_on_hardware_id_wacom(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t hardware_id_hi, uint32_t hardware_id_lo) {
  1967. }
  1968. void WaylandThread::_wp_tablet_tool_on_capability(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t capability) {
  1969. }
  1970. void WaylandThread::_wp_tablet_tool_on_done(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2) {
  1971. }
  1972. void WaylandThread::_wp_tablet_tool_on_removed(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2) {
  1973. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  1974. if (!ts) {
  1975. return;
  1976. }
  1977. SeatState *ss = wl_seat_get_seat_state(ts->wl_seat);
  1978. if (!ss) {
  1979. return;
  1980. }
  1981. List<struct zwp_tablet_tool_v2 *>::Element *E = ss->tablet_tools.find(wp_tablet_tool_v2);
  1982. if (E && E->get()) {
  1983. struct zwp_tablet_tool_v2 *tool = E->get();
  1984. TabletToolState *state = wp_tablet_tool_get_state(tool);
  1985. if (state) {
  1986. memdelete(state);
  1987. }
  1988. zwp_tablet_tool_v2_destroy(tool);
  1989. ss->tablet_tools.erase(E);
  1990. }
  1991. }
  1992. void WaylandThread::_wp_tablet_tool_on_proximity_in(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t serial, struct zwp_tablet_v2 *tablet, struct wl_surface *surface) {
  1993. // NOTE: Works pretty much like wl_pointer::enter.
  1994. WindowState *ws = wl_surface_get_window_state(surface);
  1995. if (!ws) {
  1996. return;
  1997. }
  1998. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  1999. ERR_FAIL_NULL(ts);
  2000. ts->data_pending.proximity_serial = serial;
  2001. ts->data_pending.proximal_id = ws->id;
  2002. ts->data_pending.last_proximal_id = ws->id;
  2003. DEBUG_LOG_WAYLAND_THREAD(vformat("Tablet tool entered window %d.", ts->data_pending.proximal_id));
  2004. }
  2005. void WaylandThread::_wp_tablet_tool_on_proximity_out(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2) {
  2006. // NOTE: Works pretty much like wl_pointer::leave.
  2007. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  2008. ERR_FAIL_NULL(ts);
  2009. if (ts->data_pending.proximal_id == DisplayServer::INVALID_WINDOW_ID) {
  2010. // We're probably on a decoration or some other third-party thing.
  2011. return;
  2012. }
  2013. DisplayServer::WindowID id = ts->data_pending.proximal_id;
  2014. ts->data_pending.proximal_id = DisplayServer::INVALID_WINDOW_ID;
  2015. ts->data_pending.pressed_button_mask.clear();
  2016. DEBUG_LOG_WAYLAND_THREAD(vformat("Tablet tool left window %d.", id));
  2017. }
  2018. void WaylandThread::_wp_tablet_tool_on_down(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t serial) {
  2019. // NOTE: Works pretty much like wl_pointer::button but only for a pressed left
  2020. // button.
  2021. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  2022. ERR_FAIL_NULL(ts);
  2023. TabletToolData &td = ts->data_pending;
  2024. td.pressed_button_mask.set_flag(mouse_button_to_mask(MouseButton::LEFT));
  2025. td.last_button_pressed = MouseButton::LEFT;
  2026. td.double_click_begun = true;
  2027. // The protocol doesn't cover this, but we can use this funky hack to make
  2028. // double clicking work.
  2029. td.button_time = OS::get_singleton()->get_ticks_msec();
  2030. }
  2031. void WaylandThread::_wp_tablet_tool_on_up(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2) {
  2032. // NOTE: Works pretty much like wl_pointer::button but only for a released left
  2033. // button.
  2034. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  2035. ERR_FAIL_NULL(ts);
  2036. TabletToolData &td = ts->data_pending;
  2037. td.pressed_button_mask.clear_flag(mouse_button_to_mask(MouseButton::LEFT));
  2038. // The protocol doesn't cover this, but we can use this funky hack to make
  2039. // double clicking work.
  2040. td.button_time = OS::get_singleton()->get_ticks_msec();
  2041. }
  2042. void WaylandThread::_wp_tablet_tool_on_motion(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, wl_fixed_t x, wl_fixed_t y) {
  2043. // NOTE: Works pretty much like wl_pointer::motion.
  2044. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  2045. ERR_FAIL_NULL(ts);
  2046. TabletToolData &td = ts->data_pending;
  2047. td.position.x = wl_fixed_to_double(x);
  2048. td.position.y = wl_fixed_to_double(y);
  2049. }
  2050. void WaylandThread::_wp_tablet_tool_on_pressure(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t pressure) {
  2051. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  2052. ERR_FAIL_NULL(ts);
  2053. ts->data_pending.pressure = pressure;
  2054. }
  2055. void WaylandThread::_wp_tablet_tool_on_distance(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t distance) {
  2056. // Unsupported
  2057. }
  2058. void WaylandThread::_wp_tablet_tool_on_tilt(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, wl_fixed_t tilt_x, wl_fixed_t tilt_y) {
  2059. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  2060. ERR_FAIL_NULL(ts);
  2061. TabletToolData &td = ts->data_pending;
  2062. td.tilt.x = wl_fixed_to_double(tilt_x);
  2063. td.tilt.y = wl_fixed_to_double(tilt_y);
  2064. }
  2065. void WaylandThread::_wp_tablet_tool_on_rotation(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, wl_fixed_t degrees) {
  2066. // Unsupported.
  2067. }
  2068. void WaylandThread::_wp_tablet_tool_on_slider(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, int32_t position) {
  2069. // Unsupported.
  2070. }
  2071. void WaylandThread::_wp_tablet_tool_on_wheel(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, wl_fixed_t degrees, int32_t clicks) {
  2072. // TODO
  2073. }
  2074. void WaylandThread::_wp_tablet_tool_on_button(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t serial, uint32_t button, uint32_t state) {
  2075. // NOTE: Works pretty much like wl_pointer::button.
  2076. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  2077. ERR_FAIL_NULL(ts);
  2078. TabletToolData &td = ts->data_pending;
  2079. MouseButton mouse_button = MouseButton::NONE;
  2080. if (button == BTN_STYLUS) {
  2081. mouse_button = MouseButton::LEFT;
  2082. }
  2083. if (button == BTN_STYLUS2) {
  2084. mouse_button = MouseButton::RIGHT;
  2085. }
  2086. if (mouse_button != MouseButton::NONE) {
  2087. MouseButtonMask mask = mouse_button_to_mask(mouse_button);
  2088. if (state == ZWP_TABLET_TOOL_V2_BUTTON_STATE_PRESSED) {
  2089. td.pressed_button_mask.set_flag(mask);
  2090. td.last_button_pressed = mouse_button;
  2091. td.double_click_begun = true;
  2092. } else {
  2093. td.pressed_button_mask.clear_flag(mask);
  2094. }
  2095. // The protocol doesn't cover this, but we can use this funky hack to make
  2096. // double clicking work.
  2097. td.button_time = OS::get_singleton()->get_ticks_msec();
  2098. }
  2099. }
  2100. void WaylandThread::_wp_tablet_tool_on_frame(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t time) {
  2101. // NOTE: Works pretty much like wl_pointer::frame.
  2102. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  2103. ERR_FAIL_NULL(ts);
  2104. SeatState *ss = wl_seat_get_seat_state(ts->wl_seat);
  2105. ERR_FAIL_NULL(ss);
  2106. WaylandThread *wayland_thread = ss->wayland_thread;
  2107. ERR_FAIL_NULL(wayland_thread);
  2108. TabletToolData &old_td = ts->data;
  2109. TabletToolData &td = ts->data_pending;
  2110. if (td.proximal_id != old_td.proximal_id) {
  2111. if (old_td.proximal_id != DisplayServer::INVALID_WINDOW_ID) {
  2112. Ref<WindowEventMessage> msg;
  2113. msg.instantiate();
  2114. msg->id = old_td.proximal_id;
  2115. msg->event = DisplayServer::WINDOW_EVENT_MOUSE_EXIT;
  2116. wayland_thread->push_message(msg);
  2117. }
  2118. if (td.proximal_id != DisplayServer::INVALID_WINDOW_ID) {
  2119. Ref<WindowEventMessage> msg;
  2120. msg.instantiate();
  2121. msg->id = td.proximal_id;
  2122. msg->event = DisplayServer::WINDOW_EVENT_MOUSE_ENTER;
  2123. wayland_thread->push_message(msg);
  2124. }
  2125. }
  2126. if (td.proximal_id == DisplayServer::INVALID_WINDOW_ID) {
  2127. // We're probably on a decoration or some other third-party thing. Let's
  2128. // "commit" the data and call it a day.
  2129. old_td = td;
  2130. return;
  2131. }
  2132. WindowState *ws = wayland_thread->window_get_state(td.proximal_id);
  2133. ERR_FAIL_NULL(ws);
  2134. double scale = window_state_get_scale_factor(ws);
  2135. if (old_td.position != td.position || old_td.tilt != td.tilt || old_td.pressure != td.pressure) {
  2136. td.motion_time = time;
  2137. Ref<InputEventMouseMotion> mm;
  2138. mm.instantiate();
  2139. mm->set_window_id(td.proximal_id);
  2140. // Set all pressed modifiers.
  2141. mm->set_shift_pressed(ss->shift_pressed);
  2142. mm->set_ctrl_pressed(ss->ctrl_pressed);
  2143. mm->set_alt_pressed(ss->alt_pressed);
  2144. mm->set_meta_pressed(ss->meta_pressed);
  2145. mm->set_button_mask(td.pressed_button_mask);
  2146. mm->set_global_position(td.position * scale);
  2147. mm->set_position(td.position * scale);
  2148. // NOTE: The Godot API expects normalized values and we store them raw,
  2149. // straight from the compositor, so we have to normalize them here.
  2150. // According to the tablet proto spec, tilt is expressed in degrees relative
  2151. // to the Z axis of the tablet, so it shouldn't go over 90 degrees either way,
  2152. // I think. We'll clamp it just in case.
  2153. td.tilt = td.tilt.clampf(-90, 90);
  2154. mm->set_tilt(td.tilt / 90);
  2155. // The tablet proto spec explicitly says that pressure is defined as a value
  2156. // between 0 to 65535.
  2157. mm->set_pressure(td.pressure / (float)65535);
  2158. mm->set_pen_inverted(ts->is_eraser);
  2159. Vector2 pos_delta = (td.position - old_td.position) * scale;
  2160. mm->set_relative(pos_delta);
  2161. mm->set_relative_screen_position(pos_delta);
  2162. uint32_t time_delta = td.motion_time - old_td.motion_time;
  2163. mm->set_velocity((Vector2)pos_delta / time_delta);
  2164. Ref<InputEventMessage> inputev_msg;
  2165. inputev_msg.instantiate();
  2166. inputev_msg->event = mm;
  2167. wayland_thread->push_message(inputev_msg);
  2168. }
  2169. if (old_td.pressed_button_mask != td.pressed_button_mask) {
  2170. td.button_time = time;
  2171. BitField<MouseButtonMask> pressed_mask_delta = old_td.pressed_button_mask.get_different(td.pressed_button_mask);
  2172. for (MouseButton test_button : { MouseButton::LEFT, MouseButton::RIGHT }) {
  2173. MouseButtonMask test_button_mask = mouse_button_to_mask(test_button);
  2174. if (pressed_mask_delta.has_flag(test_button_mask)) {
  2175. Ref<InputEventMouseButton> mb;
  2176. mb.instantiate();
  2177. // Set all pressed modifiers.
  2178. mb->set_shift_pressed(ss->shift_pressed);
  2179. mb->set_ctrl_pressed(ss->ctrl_pressed);
  2180. mb->set_alt_pressed(ss->alt_pressed);
  2181. mb->set_meta_pressed(ss->meta_pressed);
  2182. mb->set_window_id(td.proximal_id);
  2183. mb->set_position(td.position * scale);
  2184. mb->set_global_position(td.position * scale);
  2185. mb->set_button_mask(td.pressed_button_mask);
  2186. mb->set_button_index(test_button);
  2187. mb->set_pressed(td.pressed_button_mask.has_flag(test_button_mask));
  2188. // We have to set the last position pressed here as we can't take for
  2189. // granted what the individual events might have seen due to them not having
  2190. // a garaunteed order.
  2191. if (mb->is_pressed()) {
  2192. td.last_pressed_position = td.position;
  2193. }
  2194. if (old_td.double_click_begun && mb->is_pressed() && td.last_button_pressed == old_td.last_button_pressed && (td.button_time - old_td.button_time) < 400 && Vector2(td.last_pressed_position * scale).distance_to(Vector2(old_td.last_pressed_position * scale)) < 5) {
  2195. td.double_click_begun = false;
  2196. mb->set_double_click(true);
  2197. }
  2198. Ref<InputEventMessage> msg;
  2199. msg.instantiate();
  2200. msg->event = mb;
  2201. wayland_thread->push_message(msg);
  2202. }
  2203. }
  2204. }
  2205. old_td = td;
  2206. }
  2207. void WaylandThread::_wp_text_input_on_enter(void *data, struct zwp_text_input_v3 *wp_text_input_v3, struct wl_surface *surface) {
  2208. SeatState *ss = (SeatState *)data;
  2209. if (!ss) {
  2210. return;
  2211. }
  2212. WindowState *ws = wl_surface_get_window_state(surface);
  2213. if (!ws) {
  2214. return;
  2215. }
  2216. ss->ime_window_id = ws->id;
  2217. ss->ime_enabled = true;
  2218. }
  2219. void WaylandThread::_wp_text_input_on_leave(void *data, struct zwp_text_input_v3 *wp_text_input_v3, struct wl_surface *surface) {
  2220. SeatState *ss = (SeatState *)data;
  2221. if (!ss) {
  2222. return;
  2223. }
  2224. Ref<IMEUpdateEventMessage> msg;
  2225. msg.instantiate();
  2226. msg->id = ss->ime_window_id;
  2227. msg->text = String();
  2228. msg->selection = Vector2i();
  2229. ss->wayland_thread->push_message(msg);
  2230. ss->ime_window_id = DisplayServer::INVALID_WINDOW_ID;
  2231. ss->ime_enabled = false;
  2232. ss->ime_active = false;
  2233. ss->ime_text = String();
  2234. ss->ime_text_commit = String();
  2235. ss->ime_cursor = Vector2i();
  2236. }
  2237. void WaylandThread::_wp_text_input_on_preedit_string(void *data, struct zwp_text_input_v3 *wp_text_input_v3, const char *text, int32_t cursor_begin, int32_t cursor_end) {
  2238. SeatState *ss = (SeatState *)data;
  2239. if (!ss) {
  2240. return;
  2241. }
  2242. ss->ime_text = String::utf8(text);
  2243. // Convert cursor positions from UTF-8 to UTF-32 offset.
  2244. int32_t cursor_begin_utf32 = 0;
  2245. int32_t cursor_end_utf32 = 0;
  2246. for (int i = 0; i < ss->ime_text.length(); i++) {
  2247. uint32_t c = ss->ime_text[i];
  2248. if (c <= 0x7f) { // 7 bits.
  2249. cursor_begin -= 1;
  2250. cursor_end -= 1;
  2251. } else if (c <= 0x7ff) { // 11 bits
  2252. cursor_begin -= 2;
  2253. cursor_end -= 2;
  2254. } else if (c <= 0xffff) { // 16 bits
  2255. cursor_begin -= 3;
  2256. cursor_end -= 3;
  2257. } else if (c <= 0x001fffff) { // 21 bits
  2258. cursor_begin -= 4;
  2259. cursor_end -= 4;
  2260. } else if (c <= 0x03ffffff) { // 26 bits
  2261. cursor_begin -= 5;
  2262. cursor_end -= 5;
  2263. } else if (c <= 0x7fffffff) { // 31 bits
  2264. cursor_begin -= 6;
  2265. cursor_end -= 6;
  2266. } else {
  2267. cursor_begin -= 1;
  2268. cursor_end -= 1;
  2269. }
  2270. if (cursor_begin == 0) {
  2271. cursor_begin_utf32 = i + 1;
  2272. }
  2273. if (cursor_end == 0) {
  2274. cursor_end_utf32 = i + 1;
  2275. }
  2276. if (cursor_begin <= 0 && cursor_end <= 0) {
  2277. break;
  2278. }
  2279. }
  2280. ss->ime_cursor = Vector2i(cursor_begin_utf32, cursor_end_utf32 - cursor_begin_utf32);
  2281. }
  2282. void WaylandThread::_wp_text_input_on_commit_string(void *data, struct zwp_text_input_v3 *wp_text_input_v3, const char *text) {
  2283. SeatState *ss = (SeatState *)data;
  2284. if (!ss) {
  2285. return;
  2286. }
  2287. ss->ime_text_commit = String::utf8(text);
  2288. }
  2289. void WaylandThread::_wp_text_input_on_delete_surrounding_text(void *data, struct zwp_text_input_v3 *wp_text_input_v3, uint32_t before_length, uint32_t after_length) {
  2290. // Not implemented.
  2291. }
  2292. void WaylandThread::_wp_text_input_on_done(void *data, struct zwp_text_input_v3 *wp_text_input_v3, uint32_t serial) {
  2293. SeatState *ss = (SeatState *)data;
  2294. if (!ss) {
  2295. return;
  2296. }
  2297. if (!ss->ime_text_commit.is_empty()) {
  2298. Ref<IMECommitEventMessage> msg;
  2299. msg.instantiate();
  2300. msg->id = ss->ime_window_id;
  2301. msg->text = ss->ime_text_commit;
  2302. ss->wayland_thread->push_message(msg);
  2303. } else {
  2304. Ref<IMEUpdateEventMessage> msg;
  2305. msg.instantiate();
  2306. msg->id = ss->ime_window_id;
  2307. msg->text = ss->ime_text;
  2308. msg->selection = ss->ime_cursor;
  2309. ss->wayland_thread->push_message(msg);
  2310. }
  2311. ss->ime_text = String();
  2312. ss->ime_text_commit = String();
  2313. ss->ime_cursor = Vector2i();
  2314. }
  2315. void WaylandThread::_xdg_activation_token_on_done(void *data, struct xdg_activation_token_v1 *xdg_activation_token, const char *token) {
  2316. WindowState *ws = (WindowState *)data;
  2317. ERR_FAIL_NULL(ws);
  2318. ERR_FAIL_NULL(ws->wayland_thread);
  2319. ERR_FAIL_NULL(ws->wl_surface);
  2320. xdg_activation_v1_activate(ws->wayland_thread->registry.xdg_activation, token, ws->wl_surface);
  2321. xdg_activation_token_v1_destroy(xdg_activation_token);
  2322. DEBUG_LOG_WAYLAND_THREAD(vformat("Received activation token and requested window activation."));
  2323. }
  2324. // NOTE: This must be started after a valid wl_display is loaded.
  2325. void WaylandThread::_poll_events_thread(void *p_data) {
  2326. ThreadData *data = (ThreadData *)p_data;
  2327. ERR_FAIL_NULL(data);
  2328. ERR_FAIL_NULL(data->wl_display);
  2329. struct pollfd poll_fd;
  2330. poll_fd.fd = wl_display_get_fd(data->wl_display);
  2331. poll_fd.events = POLLIN | POLLHUP;
  2332. while (true) {
  2333. // Empty the event queue while it's full.
  2334. while (wl_display_prepare_read(data->wl_display) != 0) {
  2335. // We aren't using wl_display_dispatch(), instead "manually" handling events
  2336. // through wl_display_dispatch_pending so that we can use a global mutex and
  2337. // be sure that this and the main thread won't race over stuff, as long as
  2338. // the main thread locks it too.
  2339. //
  2340. // Note that the main thread can still call wl_display_roundtrip as that
  2341. // method directly handles all events, effectively bypassing this polling
  2342. // loop and thus the mutex locking, avoiding a deadlock.
  2343. MutexLock mutex_lock(data->mutex);
  2344. if (wl_display_dispatch_pending(data->wl_display) == -1) {
  2345. // Oh no. We'll check and handle any display error below.
  2346. break;
  2347. }
  2348. }
  2349. int werror = wl_display_get_error(data->wl_display);
  2350. if (werror) {
  2351. if (werror == EPROTO) {
  2352. struct wl_interface *wl_interface = nullptr;
  2353. uint32_t id = 0;
  2354. int error_code = wl_display_get_protocol_error(data->wl_display, (const struct wl_interface **)&wl_interface, &id);
  2355. CRASH_NOW_MSG(vformat("Wayland protocol error %d on interface %s@%d.", error_code, wl_interface ? wl_interface->name : "unknown", id));
  2356. } else {
  2357. CRASH_NOW_MSG(vformat("Wayland client error code %d.", werror));
  2358. }
  2359. }
  2360. wl_display_flush(data->wl_display);
  2361. // Wait for the event file descriptor to have new data.
  2362. poll(&poll_fd, 1, -1);
  2363. if (data->thread_done.is_set()) {
  2364. wl_display_cancel_read(data->wl_display);
  2365. break;
  2366. }
  2367. if (poll_fd.revents | POLLIN) {
  2368. // Load the queues with fresh new data.
  2369. wl_display_read_events(data->wl_display);
  2370. } else {
  2371. // Oh well... Stop signaling that we want to read.
  2372. wl_display_cancel_read(data->wl_display);
  2373. }
  2374. // The docs advise to redispatch unconditionally and it looks like that if we
  2375. // don't do this we can't catch protocol errors, which is bad.
  2376. MutexLock mutex_lock(data->mutex);
  2377. wl_display_dispatch_pending(data->wl_display);
  2378. }
  2379. }
  2380. struct wl_display *WaylandThread::get_wl_display() const {
  2381. return wl_display;
  2382. }
  2383. // NOTE: Stuff like libdecor can (and will) register foreign proxies which
  2384. // aren't formatted as we like. This method is needed to detect whether a proxy
  2385. // has our tag. Also, be careful! The proxy has to be manually tagged or it
  2386. // won't be recognized.
  2387. bool WaylandThread::wl_proxy_is_godot(struct wl_proxy *p_proxy) {
  2388. ERR_FAIL_NULL_V(p_proxy, false);
  2389. return wl_proxy_get_tag(p_proxy) == &proxy_tag;
  2390. }
  2391. void WaylandThread::wl_proxy_tag_godot(struct wl_proxy *p_proxy) {
  2392. ERR_FAIL_NULL(p_proxy);
  2393. wl_proxy_set_tag(p_proxy, &proxy_tag);
  2394. }
  2395. // Returns the wl_surface's `WindowState`, otherwise `nullptr`.
  2396. // NOTE: This will fail if the surface isn't tagged as ours.
  2397. WaylandThread::WindowState *WaylandThread::wl_surface_get_window_state(struct wl_surface *p_surface) {
  2398. if (p_surface && wl_proxy_is_godot((wl_proxy *)p_surface)) {
  2399. return (WindowState *)wl_surface_get_user_data(p_surface);
  2400. }
  2401. return nullptr;
  2402. }
  2403. // Returns the wl_outputs's `ScreenState`, otherwise `nullptr`.
  2404. // NOTE: This will fail if the output isn't tagged as ours.
  2405. WaylandThread::ScreenState *WaylandThread::wl_output_get_screen_state(struct wl_output *p_output) {
  2406. if (p_output && wl_proxy_is_godot((wl_proxy *)p_output)) {
  2407. return (ScreenState *)wl_output_get_user_data(p_output);
  2408. }
  2409. return nullptr;
  2410. }
  2411. // Returns the wl_seat's `SeatState`, otherwise `nullptr`.
  2412. // NOTE: This will fail if the output isn't tagged as ours.
  2413. WaylandThread::SeatState *WaylandThread::wl_seat_get_seat_state(struct wl_seat *p_seat) {
  2414. if (p_seat && wl_proxy_is_godot((wl_proxy *)p_seat)) {
  2415. return (SeatState *)wl_seat_get_user_data(p_seat);
  2416. }
  2417. return nullptr;
  2418. }
  2419. // Returns the wp_tablet_tool's `TabletToolState`, otherwise `nullptr`.
  2420. // NOTE: This will fail if the output isn't tagged as ours.
  2421. WaylandThread::TabletToolState *WaylandThread::wp_tablet_tool_get_state(struct zwp_tablet_tool_v2 *p_tool) {
  2422. if (p_tool && wl_proxy_is_godot((wl_proxy *)p_tool)) {
  2423. return (TabletToolState *)zwp_tablet_tool_v2_get_user_data(p_tool);
  2424. }
  2425. return nullptr;
  2426. }
  2427. // Returns the wl_data_offer's `OfferState`, otherwise `nullptr`.
  2428. // NOTE: This will fail if the output isn't tagged as ours.
  2429. WaylandThread::OfferState *WaylandThread::wl_data_offer_get_offer_state(struct wl_data_offer *p_offer) {
  2430. if (p_offer && wl_proxy_is_godot((wl_proxy *)p_offer)) {
  2431. return (OfferState *)wl_data_offer_get_user_data(p_offer);
  2432. }
  2433. return nullptr;
  2434. }
  2435. // Returns the wl_data_offer's `OfferState`, otherwise `nullptr`.
  2436. // NOTE: This will fail if the output isn't tagged as ours.
  2437. WaylandThread::OfferState *WaylandThread::wp_primary_selection_offer_get_offer_state(struct zwp_primary_selection_offer_v1 *p_offer) {
  2438. if (p_offer && wl_proxy_is_godot((wl_proxy *)p_offer)) {
  2439. return (OfferState *)zwp_primary_selection_offer_v1_get_user_data(p_offer);
  2440. }
  2441. return nullptr;
  2442. }
  2443. // This is implemented as a method because this is the simplest way of
  2444. // accounting for dynamic output scale changes.
  2445. int WaylandThread::window_state_get_preferred_buffer_scale(WindowState *p_ws) {
  2446. ERR_FAIL_NULL_V(p_ws, 1);
  2447. if (p_ws->preferred_fractional_scale > 0) {
  2448. // We're scaling fractionally. Per spec, the buffer scale is always 1.
  2449. return 1;
  2450. }
  2451. if (p_ws->wl_outputs.is_empty()) {
  2452. DEBUG_LOG_WAYLAND_THREAD("Window has no output associated, returning buffer scale of 1.");
  2453. return 1;
  2454. }
  2455. // TODO: Cache value?
  2456. int max_size = 1;
  2457. // ================================ IMPORTANT =================================
  2458. // NOTE: Due to a Godot limitation, we can't really rescale the whole UI yet.
  2459. // Because of this reason, all platforms have resorted to forcing the highest
  2460. // scale possible of a system on any window, despite of what screen it's onto.
  2461. // On this backend everything's already in place for dynamic window scale
  2462. // handling, but in the meantime we'll just select the biggest _global_ output.
  2463. // To restore dynamic scale selection, simply iterate over `p_ws->wl_outputs`
  2464. // instead.
  2465. for (struct wl_output *wl_output : p_ws->registry->wl_outputs) {
  2466. ScreenState *ss = wl_output_get_screen_state(wl_output);
  2467. if (ss && ss->pending_data.scale > max_size) {
  2468. // NOTE: For some mystical reason, wl_output.done is emitted _after_ windows
  2469. // get resized but the scale event gets sent _before_ that. I'm still leaning
  2470. // towards the idea that rescaling when a window gets a resolution change is a
  2471. // pretty good approach, but this means that we'll have to use the screen data
  2472. // before it's "committed".
  2473. // FIXME: Use the committed data. Somehow.
  2474. max_size = ss->pending_data.scale;
  2475. }
  2476. }
  2477. return max_size;
  2478. }
  2479. double WaylandThread::window_state_get_scale_factor(WindowState *p_ws) {
  2480. ERR_FAIL_NULL_V(p_ws, 1);
  2481. if (p_ws->fractional_scale > 0) {
  2482. // The fractional scale amount takes priority.
  2483. return p_ws->fractional_scale;
  2484. }
  2485. return p_ws->buffer_scale;
  2486. }
  2487. void WaylandThread::window_state_update_size(WindowState *p_ws, int p_width, int p_height) {
  2488. ERR_FAIL_NULL(p_ws);
  2489. int preferred_buffer_scale = window_state_get_preferred_buffer_scale(p_ws);
  2490. bool using_fractional = p_ws->preferred_fractional_scale > 0;
  2491. // If neither is true we no-op.
  2492. bool scale_changed = true;
  2493. bool size_changed = true;
  2494. if (p_ws->rect.size.width != p_width || p_ws->rect.size.height != p_height) {
  2495. p_ws->rect.size.width = p_width;
  2496. p_ws->rect.size.height = p_height;
  2497. size_changed = true;
  2498. }
  2499. if (using_fractional && p_ws->fractional_scale != p_ws->preferred_fractional_scale) {
  2500. p_ws->fractional_scale = p_ws->preferred_fractional_scale;
  2501. scale_changed = true;
  2502. }
  2503. if (p_ws->buffer_scale != preferred_buffer_scale) {
  2504. // The buffer scale is always important, even if we use frac scaling.
  2505. p_ws->buffer_scale = preferred_buffer_scale;
  2506. p_ws->buffer_scale_changed = true;
  2507. if (!using_fractional) {
  2508. // We don't bother updating everything else if it's turned on though.
  2509. scale_changed = true;
  2510. }
  2511. }
  2512. if (p_ws->wl_surface) {
  2513. if (p_ws->wp_viewport) {
  2514. wp_viewport_set_destination(p_ws->wp_viewport, p_width, p_height);
  2515. }
  2516. if (p_ws->xdg_surface) {
  2517. xdg_surface_set_window_geometry(p_ws->xdg_surface, 0, 0, p_width, p_height);
  2518. }
  2519. }
  2520. #ifdef LIBDECOR_ENABLED
  2521. if (p_ws->libdecor_frame) {
  2522. struct libdecor_state *state = libdecor_state_new(p_width, p_height);
  2523. libdecor_frame_commit(p_ws->libdecor_frame, state, p_ws->pending_libdecor_configuration);
  2524. libdecor_state_free(state);
  2525. p_ws->pending_libdecor_configuration = nullptr;
  2526. }
  2527. #endif
  2528. if (size_changed || scale_changed) {
  2529. double win_scale = window_state_get_scale_factor(p_ws);
  2530. Size2i scaled_size = scale_vector2i(p_ws->rect.size, win_scale);
  2531. if (using_fractional) {
  2532. DEBUG_LOG_WAYLAND_THREAD(vformat("Resizing the window from %s to %s (fractional scale x%f).", p_ws->rect.size, scaled_size, p_ws->fractional_scale));
  2533. } else {
  2534. DEBUG_LOG_WAYLAND_THREAD(vformat("Resizing the window from %s to %s (buffer scale x%d).", p_ws->rect.size, scaled_size, p_ws->buffer_scale));
  2535. }
  2536. // FIXME: Actually resize the hint instead of centering it.
  2537. p_ws->wayland_thread->pointer_set_hint(scaled_size / 2);
  2538. Ref<WindowRectMessage> rect_msg;
  2539. rect_msg.instantiate();
  2540. rect_msg->id = p_ws->id;
  2541. rect_msg->rect.position = scale_vector2i(p_ws->rect.position, win_scale);
  2542. rect_msg->rect.size = scaled_size;
  2543. p_ws->wayland_thread->push_message(rect_msg);
  2544. }
  2545. if (scale_changed) {
  2546. Ref<WindowEventMessage> dpi_msg;
  2547. dpi_msg.instantiate();
  2548. dpi_msg->id = p_ws->id;
  2549. dpi_msg->event = DisplayServer::WINDOW_EVENT_DPI_CHANGE;
  2550. p_ws->wayland_thread->push_message(dpi_msg);
  2551. }
  2552. }
  2553. // Scales a vector according to wp_fractional_scale's rules, where coordinates
  2554. // must be scaled with away from zero half-rounding.
  2555. Vector2i WaylandThread::scale_vector2i(const Vector2i &p_vector, double p_amount) {
  2556. // This snippet is tiny, I know, but this is done a lot.
  2557. int x = std::round(p_vector.x * p_amount);
  2558. int y = std::round(p_vector.y * p_amount);
  2559. return Vector2i(x, y);
  2560. }
  2561. void WaylandThread::seat_state_unlock_pointer(SeatState *p_ss) {
  2562. ERR_FAIL_NULL(p_ss);
  2563. if (p_ss->wl_pointer == nullptr) {
  2564. return;
  2565. }
  2566. if (p_ss->wp_locked_pointer) {
  2567. zwp_locked_pointer_v1_destroy(p_ss->wp_locked_pointer);
  2568. p_ss->wp_locked_pointer = nullptr;
  2569. }
  2570. if (p_ss->wp_confined_pointer) {
  2571. zwp_confined_pointer_v1_destroy(p_ss->wp_confined_pointer);
  2572. p_ss->wp_confined_pointer = nullptr;
  2573. }
  2574. }
  2575. void WaylandThread::seat_state_lock_pointer(SeatState *p_ss) {
  2576. ERR_FAIL_NULL(p_ss);
  2577. if (p_ss->wl_pointer == nullptr) {
  2578. return;
  2579. }
  2580. if (registry.wp_pointer_constraints == nullptr) {
  2581. return;
  2582. }
  2583. if (p_ss->wp_locked_pointer == nullptr) {
  2584. struct wl_surface *locked_surface = window_get_wl_surface(p_ss->pointer_data.last_pointed_id);
  2585. ERR_FAIL_NULL(locked_surface);
  2586. p_ss->wp_locked_pointer = zwp_pointer_constraints_v1_lock_pointer(registry.wp_pointer_constraints, locked_surface, p_ss->wl_pointer, nullptr, ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
  2587. }
  2588. }
  2589. void WaylandThread::seat_state_set_hint(SeatState *p_ss, int p_x, int p_y) {
  2590. if (p_ss->wp_locked_pointer == nullptr) {
  2591. return;
  2592. }
  2593. zwp_locked_pointer_v1_set_cursor_position_hint(p_ss->wp_locked_pointer, wl_fixed_from_int(p_x), wl_fixed_from_int(p_y));
  2594. }
  2595. void WaylandThread::seat_state_confine_pointer(SeatState *p_ss) {
  2596. ERR_FAIL_NULL(p_ss);
  2597. if (p_ss->wl_pointer == nullptr) {
  2598. return;
  2599. }
  2600. if (registry.wp_pointer_constraints == nullptr) {
  2601. return;
  2602. }
  2603. if (p_ss->wp_confined_pointer == nullptr) {
  2604. struct wl_surface *confined_surface = window_get_wl_surface(p_ss->pointer_data.last_pointed_id);
  2605. ERR_FAIL_NULL(confined_surface);
  2606. p_ss->wp_confined_pointer = zwp_pointer_constraints_v1_confine_pointer(registry.wp_pointer_constraints, confined_surface, p_ss->wl_pointer, nullptr, ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
  2607. }
  2608. }
  2609. void WaylandThread::seat_state_update_cursor(SeatState *p_ss) {
  2610. ERR_FAIL_NULL(p_ss);
  2611. WaylandThread *thread = p_ss->wayland_thread;
  2612. ERR_FAIL_NULL(p_ss->wayland_thread);
  2613. if (!p_ss->wl_pointer || !p_ss->cursor_surface) {
  2614. return;
  2615. }
  2616. // NOTE: Those values are valid by default and will hide the cursor when
  2617. // unchanged.
  2618. struct wl_buffer *cursor_buffer = nullptr;
  2619. uint32_t hotspot_x = 0;
  2620. uint32_t hotspot_y = 0;
  2621. int scale = 1;
  2622. if (thread->cursor_visible) {
  2623. DisplayServer::CursorShape shape = thread->cursor_shape;
  2624. struct CustomCursor *custom_cursor = thread->custom_cursors.getptr(shape);
  2625. if (custom_cursor) {
  2626. cursor_buffer = custom_cursor->wl_buffer;
  2627. hotspot_x = custom_cursor->hotspot.x;
  2628. hotspot_y = custom_cursor->hotspot.y;
  2629. // We can't really reasonably scale custom cursors, so we'll let the
  2630. // compositor do it for us (badly).
  2631. scale = 1;
  2632. } else if (thread->registry.wp_cursor_shape_manager) {
  2633. wp_cursor_shape_device_v1_shape wp_shape = thread->standard_cursors[shape];
  2634. wp_cursor_shape_device_v1_set_shape(p_ss->wp_cursor_shape_device, p_ss->pointer_enter_serial, wp_shape);
  2635. // We should avoid calling the `wl_pointer_set_cursor` at the end of this method.
  2636. return;
  2637. } else {
  2638. struct wl_cursor *wl_cursor = thread->wl_cursors[shape];
  2639. if (!wl_cursor) {
  2640. return;
  2641. }
  2642. int frame_idx = 0;
  2643. if (wl_cursor->image_count > 1) {
  2644. // The cursor is animated.
  2645. frame_idx = wl_cursor_frame(wl_cursor, p_ss->cursor_time_ms);
  2646. if (!p_ss->cursor_frame_callback) {
  2647. // Since it's animated, we'll re-update it the next frame.
  2648. p_ss->cursor_frame_callback = wl_surface_frame(p_ss->cursor_surface);
  2649. wl_callback_add_listener(p_ss->cursor_frame_callback, &cursor_frame_callback_listener, p_ss);
  2650. }
  2651. }
  2652. struct wl_cursor_image *wl_cursor_image = wl_cursor->images[frame_idx];
  2653. scale = thread->cursor_scale;
  2654. cursor_buffer = wl_cursor_image_get_buffer(wl_cursor_image);
  2655. // As the surface's buffer is scaled (thus the surface is smaller) and the
  2656. // hotspot must be expressed in surface-local coordinates, we need to scale
  2657. // it down accordingly.
  2658. hotspot_x = wl_cursor_image->hotspot_x / scale;
  2659. hotspot_y = wl_cursor_image->hotspot_y / scale;
  2660. }
  2661. }
  2662. wl_pointer_set_cursor(p_ss->wl_pointer, p_ss->pointer_enter_serial, p_ss->cursor_surface, hotspot_x, hotspot_y);
  2663. wl_surface_set_buffer_scale(p_ss->cursor_surface, scale);
  2664. wl_surface_attach(p_ss->cursor_surface, cursor_buffer, 0, 0);
  2665. wl_surface_damage_buffer(p_ss->cursor_surface, 0, 0, INT_MAX, INT_MAX);
  2666. wl_surface_commit(p_ss->cursor_surface);
  2667. }
  2668. void WaylandThread::seat_state_echo_keys(SeatState *p_ss) {
  2669. ERR_FAIL_NULL(p_ss);
  2670. if (p_ss->wl_keyboard == nullptr) {
  2671. return;
  2672. }
  2673. // TODO: Comment and document out properly this block of code.
  2674. // In short, this implements key repeating.
  2675. if (p_ss->repeat_key_delay_msec && p_ss->repeating_keycode != XKB_KEYCODE_INVALID) {
  2676. uint64_t current_ticks = OS::get_singleton()->get_ticks_msec();
  2677. uint64_t delayed_start_ticks = p_ss->last_repeat_start_msec + p_ss->repeat_start_delay_msec;
  2678. if (p_ss->last_repeat_msec < delayed_start_ticks) {
  2679. p_ss->last_repeat_msec = delayed_start_ticks;
  2680. }
  2681. if (current_ticks >= delayed_start_ticks) {
  2682. uint64_t ticks_delta = current_ticks - p_ss->last_repeat_msec;
  2683. int keys_amount = (ticks_delta / p_ss->repeat_key_delay_msec);
  2684. for (int i = 0; i < keys_amount; i++) {
  2685. Ref<InputEventKey> k = _seat_state_get_key_event(p_ss, p_ss->repeating_keycode, true);
  2686. if (k.is_null()) {
  2687. continue;
  2688. }
  2689. k->set_echo(true);
  2690. Ref<InputEventKey> uk = _seat_state_get_unstuck_key_event(p_ss, p_ss->repeating_keycode, true, k->get_keycode());
  2691. if (uk.is_valid()) {
  2692. Input::get_singleton()->parse_input_event(uk);
  2693. }
  2694. Input::get_singleton()->parse_input_event(k);
  2695. }
  2696. p_ss->last_repeat_msec += ticks_delta - (ticks_delta % p_ss->repeat_key_delay_msec);
  2697. }
  2698. }
  2699. }
  2700. void WaylandThread::push_message(Ref<Message> message) {
  2701. messages.push_back(message);
  2702. }
  2703. bool WaylandThread::has_message() {
  2704. return messages.front() != nullptr;
  2705. }
  2706. Ref<WaylandThread::Message> WaylandThread::pop_message() {
  2707. if (messages.front() != nullptr) {
  2708. Ref<Message> msg = messages.front()->get();
  2709. messages.pop_front();
  2710. return msg;
  2711. }
  2712. // This method should only be called if `has_messages` returns true but if
  2713. // that isn't the case we'll just return an invalid `Ref`. After all, due to
  2714. // its `InputEvent`-like interface, we still have to dynamically cast and check
  2715. // the `Ref`'s validity anyways.
  2716. return Ref<Message>();
  2717. }
  2718. void WaylandThread::window_create(DisplayServer::WindowID p_window_id, const Size2i &p_size, DisplayServer::WindowID p_parent_id) {
  2719. ERR_FAIL_COND(windows.has(p_window_id));
  2720. WindowState &ws = windows[p_window_id];
  2721. ws.id = p_window_id;
  2722. ws.registry = &registry;
  2723. ws.wayland_thread = this;
  2724. ws.rect.size = p_size;
  2725. ws.wl_surface = wl_compositor_create_surface(registry.wl_compositor);
  2726. wl_proxy_tag_godot((struct wl_proxy *)ws.wl_surface);
  2727. wl_surface_add_listener(ws.wl_surface, &wl_surface_listener, &ws);
  2728. if (registry.wp_viewporter) {
  2729. ws.wp_viewport = wp_viewporter_get_viewport(registry.wp_viewporter, ws.wl_surface);
  2730. if (registry.wp_fractional_scale_manager) {
  2731. ws.wp_fractional_scale = wp_fractional_scale_manager_v1_get_fractional_scale(registry.wp_fractional_scale_manager, ws.wl_surface);
  2732. wp_fractional_scale_v1_add_listener(ws.wp_fractional_scale, &wp_fractional_scale_listener, &ws);
  2733. }
  2734. }
  2735. bool decorated = false;
  2736. #ifdef LIBDECOR_ENABLED
  2737. if (!decorated && libdecor_context) {
  2738. ws.libdecor_frame = libdecor_decorate(libdecor_context, ws.wl_surface, (struct libdecor_frame_interface *)&libdecor_frame_interface, &ws);
  2739. libdecor_frame_map(ws.libdecor_frame);
  2740. if (registry.xdg_toplevel_icon_manager) {
  2741. xdg_toplevel *toplevel = libdecor_frame_get_xdg_toplevel(ws.libdecor_frame);
  2742. if (toplevel != nullptr) {
  2743. xdg_toplevel_icon_manager_v1_set_icon(registry.xdg_toplevel_icon_manager, toplevel, xdg_icon);
  2744. }
  2745. }
  2746. decorated = true;
  2747. }
  2748. #endif
  2749. if (!decorated) {
  2750. // libdecor has failed loading or is disabled, we shall handle xdg_toplevel
  2751. // creation and decoration ourselves (and by decorating for now I just mean
  2752. // asking for SSDs and hoping for the best).
  2753. ws.xdg_surface = xdg_wm_base_get_xdg_surface(registry.xdg_wm_base, ws.wl_surface);
  2754. xdg_surface_add_listener(ws.xdg_surface, &xdg_surface_listener, &ws);
  2755. ws.xdg_toplevel = xdg_surface_get_toplevel(ws.xdg_surface);
  2756. xdg_toplevel_add_listener(ws.xdg_toplevel, &xdg_toplevel_listener, &ws);
  2757. if (registry.xdg_decoration_manager) {
  2758. ws.xdg_toplevel_decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(registry.xdg_decoration_manager, ws.xdg_toplevel);
  2759. zxdg_toplevel_decoration_v1_add_listener(ws.xdg_toplevel_decoration, &xdg_toplevel_decoration_listener, &ws);
  2760. decorated = true;
  2761. }
  2762. if (registry.xdg_toplevel_icon_manager) {
  2763. xdg_toplevel_icon_manager_v1_set_icon(registry.xdg_toplevel_icon_manager, ws.xdg_toplevel, xdg_icon);
  2764. }
  2765. }
  2766. if (p_parent_id != DisplayServer::INVALID_WINDOW_ID) {
  2767. // NOTE: It's important to set the parent ASAP to avoid misunderstandings with
  2768. // the compositor. For example, niri immediately resizes the window to full
  2769. // size as soon as it's configured if it's not parented to another toplevel.
  2770. window_set_parent(p_window_id, p_parent_id);
  2771. }
  2772. ws.frame_callback = wl_surface_frame(ws.wl_surface);
  2773. wl_callback_add_listener(ws.frame_callback, &frame_wl_callback_listener, &ws);
  2774. if (registry.xdg_exporter_v2) {
  2775. ws.xdg_exported_v2 = zxdg_exporter_v2_export_toplevel(registry.xdg_exporter_v2, ws.wl_surface);
  2776. zxdg_exported_v2_add_listener(ws.xdg_exported_v2, &xdg_exported_v2_listener, &ws);
  2777. } else if (registry.xdg_exporter_v1) {
  2778. ws.xdg_exported_v1 = zxdg_exporter_v1_export(registry.xdg_exporter_v1, ws.wl_surface);
  2779. zxdg_exported_v1_add_listener(ws.xdg_exported_v1, &xdg_exported_v1_listener, &ws);
  2780. }
  2781. wl_surface_commit(ws.wl_surface);
  2782. // Wait for the surface to be configured before continuing.
  2783. wl_display_roundtrip(wl_display);
  2784. window_state_update_size(&ws, ws.rect.size.width, ws.rect.size.height);
  2785. }
  2786. void WaylandThread::window_create_popup(DisplayServer::WindowID p_window_id, DisplayServer::WindowID p_parent_id, Rect2i p_rect) {
  2787. ERR_FAIL_COND(windows.has(p_window_id));
  2788. ERR_FAIL_COND(!windows.has(p_parent_id));
  2789. WindowState &ws = windows[p_window_id];
  2790. WindowState &parent = windows[p_parent_id];
  2791. double parent_scale = window_state_get_scale_factor(&parent);
  2792. p_rect.position = scale_vector2i(p_rect.position, 1.0 / parent_scale);
  2793. p_rect.size = scale_vector2i(p_rect.size, 1.0 / parent_scale);
  2794. ws.id = p_window_id;
  2795. ws.parent_id = p_parent_id;
  2796. ws.registry = &registry;
  2797. ws.wayland_thread = this;
  2798. ws.rect = p_rect;
  2799. ws.wl_surface = wl_compositor_create_surface(registry.wl_compositor);
  2800. wl_proxy_tag_godot((struct wl_proxy *)ws.wl_surface);
  2801. wl_surface_add_listener(ws.wl_surface, &wl_surface_listener, &ws);
  2802. if (registry.wp_viewporter) {
  2803. ws.wp_viewport = wp_viewporter_get_viewport(registry.wp_viewporter, ws.wl_surface);
  2804. if (registry.wp_fractional_scale_manager) {
  2805. ws.wp_fractional_scale = wp_fractional_scale_manager_v1_get_fractional_scale(registry.wp_fractional_scale_manager, ws.wl_surface);
  2806. wp_fractional_scale_v1_add_listener(ws.wp_fractional_scale, &wp_fractional_scale_listener, &ws);
  2807. }
  2808. }
  2809. ws.xdg_surface = xdg_wm_base_get_xdg_surface(registry.xdg_wm_base, ws.wl_surface);
  2810. xdg_surface_add_listener(ws.xdg_surface, &xdg_surface_listener, &ws);
  2811. Rect2i positioner_rect;
  2812. positioner_rect.size = parent.rect.size;
  2813. struct xdg_surface *parent_xdg_surface = parent.xdg_surface;
  2814. Point2i offset = ws.rect.position - parent.rect.position;
  2815. #ifdef LIBDECOR_ENABLED
  2816. if (!parent_xdg_surface && parent.libdecor_frame) {
  2817. parent_xdg_surface = libdecor_frame_get_xdg_surface(parent.libdecor_frame);
  2818. int corner_x = 0;
  2819. int corner_y = 0;
  2820. libdecor_frame_translate_coordinate(parent.libdecor_frame, 0, 0, &corner_x, &corner_y);
  2821. positioner_rect.position.x = corner_x;
  2822. positioner_rect.position.y = corner_y;
  2823. positioner_rect.size.width -= corner_x;
  2824. positioner_rect.size.height -= corner_y;
  2825. }
  2826. #endif
  2827. ERR_FAIL_NULL(parent_xdg_surface);
  2828. struct xdg_positioner *xdg_positioner = xdg_wm_base_create_positioner(registry.xdg_wm_base);
  2829. xdg_positioner_set_size(xdg_positioner, ws.rect.size.width, ws.rect.size.height);
  2830. xdg_positioner_set_anchor(xdg_positioner, XDG_POSITIONER_ANCHOR_TOP_LEFT);
  2831. xdg_positioner_set_gravity(xdg_positioner, XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT);
  2832. xdg_positioner_set_constraint_adjustment(xdg_positioner, XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X | XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_Y | XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_X | XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_Y);
  2833. xdg_positioner_set_anchor_rect(xdg_positioner, positioner_rect.position.x, positioner_rect.position.y, positioner_rect.size.width, positioner_rect.size.height);
  2834. xdg_positioner_set_offset(xdg_positioner, offset.x, offset.y);
  2835. ws.xdg_popup = xdg_surface_get_popup(ws.xdg_surface, parent_xdg_surface, xdg_positioner);
  2836. xdg_popup_add_listener(ws.xdg_popup, &xdg_popup_listener, &ws);
  2837. xdg_positioner_destroy(xdg_positioner);
  2838. ws.frame_callback = wl_surface_frame(ws.wl_surface);
  2839. wl_callback_add_listener(ws.frame_callback, &frame_wl_callback_listener, &ws);
  2840. wl_surface_commit(ws.wl_surface);
  2841. // Wait for the surface to be configured before continuing.
  2842. wl_display_roundtrip(wl_display);
  2843. }
  2844. void WaylandThread::window_destroy(DisplayServer::WindowID p_window_id) {
  2845. ERR_FAIL_COND(!windows.has(p_window_id));
  2846. WindowState &ws = windows[p_window_id];
  2847. if (ws.xdg_popup) {
  2848. xdg_popup_destroy(ws.xdg_popup);
  2849. }
  2850. if (ws.xdg_toplevel_decoration) {
  2851. zxdg_toplevel_decoration_v1_destroy(ws.xdg_toplevel_decoration);
  2852. }
  2853. if (ws.xdg_toplevel) {
  2854. xdg_toplevel_destroy(ws.xdg_toplevel);
  2855. }
  2856. #ifdef LIBDECOR_ENABLED
  2857. if (ws.libdecor_frame) {
  2858. libdecor_frame_unref(ws.libdecor_frame);
  2859. }
  2860. #endif // LIBDECOR_ENABLED
  2861. if (ws.wp_fractional_scale) {
  2862. wp_fractional_scale_v1_destroy(ws.wp_fractional_scale);
  2863. }
  2864. if (ws.wp_viewport) {
  2865. wp_viewport_destroy(ws.wp_viewport);
  2866. }
  2867. if (ws.frame_callback) {
  2868. wl_callback_destroy(ws.frame_callback);
  2869. }
  2870. if (ws.xdg_surface) {
  2871. xdg_surface_destroy(ws.xdg_surface);
  2872. }
  2873. if (ws.wl_surface) {
  2874. wl_surface_destroy(ws.wl_surface);
  2875. }
  2876. // Before continuing, let's handle any leftover event that might still refer to
  2877. // this window.
  2878. wl_display_roundtrip(wl_display);
  2879. // We can already clean up here, we're done.
  2880. windows.erase(p_window_id);
  2881. }
  2882. struct wl_surface *WaylandThread::window_get_wl_surface(DisplayServer::WindowID p_window_id) const {
  2883. ERR_FAIL_COND_V(!windows.has(p_window_id), nullptr);
  2884. const WindowState &ws = windows[p_window_id];
  2885. return ws.wl_surface;
  2886. }
  2887. WaylandThread::WindowState *WaylandThread::window_get_state(DisplayServer::WindowID p_window_id) {
  2888. return windows.getptr(p_window_id);
  2889. }
  2890. void WaylandThread::beep() const {
  2891. if (registry.xdg_system_bell) {
  2892. xdg_system_bell_v1_ring(registry.xdg_system_bell, nullptr);
  2893. }
  2894. }
  2895. void WaylandThread::window_start_drag(DisplayServer::WindowID p_window_id) {
  2896. ERR_FAIL_COND(!windows.has(p_window_id));
  2897. WindowState &ws = windows[p_window_id];
  2898. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  2899. if (ss && ws.xdg_toplevel) {
  2900. xdg_toplevel_move(ws.xdg_toplevel, ss->wl_seat, ss->pointer_data.button_serial);
  2901. }
  2902. #ifdef LIBDECOR_ENABLED
  2903. if (ws.libdecor_frame) {
  2904. libdecor_frame_move(ws.libdecor_frame, ss->wl_seat, ss->pointer_data.button_serial);
  2905. }
  2906. #endif
  2907. }
  2908. void WaylandThread::window_start_resize(DisplayServer::WindowResizeEdge p_edge, DisplayServer::WindowID p_window) {
  2909. ERR_FAIL_COND(!windows.has(p_window));
  2910. WindowState &ws = windows[p_window];
  2911. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  2912. if (ss && ws.xdg_toplevel) {
  2913. xdg_toplevel_resize_edge edge = XDG_TOPLEVEL_RESIZE_EDGE_NONE;
  2914. switch (p_edge) {
  2915. case DisplayServer::WINDOW_EDGE_TOP_LEFT: {
  2916. edge = XDG_TOPLEVEL_RESIZE_EDGE_TOP_LEFT;
  2917. } break;
  2918. case DisplayServer::WINDOW_EDGE_TOP: {
  2919. edge = XDG_TOPLEVEL_RESIZE_EDGE_TOP;
  2920. } break;
  2921. case DisplayServer::WINDOW_EDGE_TOP_RIGHT: {
  2922. edge = XDG_TOPLEVEL_RESIZE_EDGE_TOP_RIGHT;
  2923. } break;
  2924. case DisplayServer::WINDOW_EDGE_LEFT: {
  2925. edge = XDG_TOPLEVEL_RESIZE_EDGE_LEFT;
  2926. } break;
  2927. case DisplayServer::WINDOW_EDGE_RIGHT: {
  2928. edge = XDG_TOPLEVEL_RESIZE_EDGE_RIGHT;
  2929. } break;
  2930. case DisplayServer::WINDOW_EDGE_BOTTOM_LEFT: {
  2931. edge = XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_LEFT;
  2932. } break;
  2933. case DisplayServer::WINDOW_EDGE_BOTTOM: {
  2934. edge = XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM;
  2935. } break;
  2936. case DisplayServer::WINDOW_EDGE_BOTTOM_RIGHT: {
  2937. edge = XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_RIGHT;
  2938. } break;
  2939. default:
  2940. break;
  2941. }
  2942. xdg_toplevel_resize(ws.xdg_toplevel, ss->wl_seat, ss->pointer_data.button_serial, edge);
  2943. }
  2944. #ifdef LIBDECOR_ENABLED
  2945. if (ws.libdecor_frame) {
  2946. libdecor_resize_edge edge = LIBDECOR_RESIZE_EDGE_NONE;
  2947. switch (p_edge) {
  2948. case DisplayServer::WINDOW_EDGE_TOP_LEFT: {
  2949. edge = LIBDECOR_RESIZE_EDGE_TOP_LEFT;
  2950. } break;
  2951. case DisplayServer::WINDOW_EDGE_TOP: {
  2952. edge = LIBDECOR_RESIZE_EDGE_TOP;
  2953. } break;
  2954. case DisplayServer::WINDOW_EDGE_TOP_RIGHT: {
  2955. edge = LIBDECOR_RESIZE_EDGE_TOP_RIGHT;
  2956. } break;
  2957. case DisplayServer::WINDOW_EDGE_LEFT: {
  2958. edge = LIBDECOR_RESIZE_EDGE_LEFT;
  2959. } break;
  2960. case DisplayServer::WINDOW_EDGE_RIGHT: {
  2961. edge = LIBDECOR_RESIZE_EDGE_RIGHT;
  2962. } break;
  2963. case DisplayServer::WINDOW_EDGE_BOTTOM_LEFT: {
  2964. edge = LIBDECOR_RESIZE_EDGE_BOTTOM_LEFT;
  2965. } break;
  2966. case DisplayServer::WINDOW_EDGE_BOTTOM: {
  2967. edge = LIBDECOR_RESIZE_EDGE_BOTTOM;
  2968. } break;
  2969. case DisplayServer::WINDOW_EDGE_BOTTOM_RIGHT: {
  2970. edge = LIBDECOR_RESIZE_EDGE_BOTTOM_RIGHT;
  2971. } break;
  2972. default:
  2973. break;
  2974. }
  2975. libdecor_frame_resize(ws.libdecor_frame, ss->wl_seat, ss->pointer_data.button_serial, edge);
  2976. }
  2977. #endif
  2978. }
  2979. void WaylandThread::window_set_parent(DisplayServer::WindowID p_window_id, DisplayServer::WindowID p_parent_id) {
  2980. ERR_FAIL_COND(!windows.has(p_window_id));
  2981. ERR_FAIL_COND(!windows.has(p_parent_id));
  2982. WindowState &child = windows[p_window_id];
  2983. child.parent_id = p_parent_id;
  2984. WindowState &parent = windows[p_parent_id];
  2985. // NOTE: We can't really unparent as, at the time of writing, libdecor
  2986. // segfaults when trying to set a null parent. Hopefully unparenting is not
  2987. // that common. Bummer.
  2988. #ifdef LIBDECOR_ENABLED
  2989. if (child.libdecor_frame && parent.libdecor_frame) {
  2990. libdecor_frame_set_parent(child.libdecor_frame, parent.libdecor_frame);
  2991. return;
  2992. }
  2993. #endif
  2994. if (child.xdg_toplevel && parent.xdg_toplevel) {
  2995. xdg_toplevel_set_parent(child.xdg_toplevel, parent.xdg_toplevel);
  2996. }
  2997. }
  2998. void WaylandThread::window_set_max_size(DisplayServer::WindowID p_window_id, const Size2i &p_size) {
  2999. ERR_FAIL_COND(!windows.has(p_window_id));
  3000. WindowState &ws = windows[p_window_id];
  3001. Vector2i logical_max_size = scale_vector2i(p_size, 1 / window_state_get_scale_factor(&ws));
  3002. if (ws.wl_surface && ws.xdg_toplevel) {
  3003. xdg_toplevel_set_max_size(ws.xdg_toplevel, logical_max_size.width, logical_max_size.height);
  3004. }
  3005. #ifdef LIBDECOR_ENABLED
  3006. if (ws.libdecor_frame) {
  3007. libdecor_frame_set_max_content_size(ws.libdecor_frame, logical_max_size.width, logical_max_size.height);
  3008. }
  3009. // FIXME: I'm not sure whether we have to commit the surface for this to apply.
  3010. #endif
  3011. }
  3012. void WaylandThread::window_set_min_size(DisplayServer::WindowID p_window_id, const Size2i &p_size) {
  3013. ERR_FAIL_COND(!windows.has(p_window_id));
  3014. WindowState &ws = windows[p_window_id];
  3015. Size2i logical_min_size = scale_vector2i(p_size, 1 / window_state_get_scale_factor(&ws));
  3016. if (ws.wl_surface && ws.xdg_toplevel) {
  3017. xdg_toplevel_set_min_size(ws.xdg_toplevel, logical_min_size.width, logical_min_size.height);
  3018. }
  3019. #ifdef LIBDECOR_ENABLED
  3020. if (ws.libdecor_frame) {
  3021. libdecor_frame_set_min_content_size(ws.libdecor_frame, logical_min_size.width, logical_min_size.height);
  3022. }
  3023. // FIXME: I'm not sure whether we have to commit the surface for this to apply.
  3024. #endif
  3025. }
  3026. bool WaylandThread::window_can_set_mode(DisplayServer::WindowID p_window_id, DisplayServer::WindowMode p_window_mode) const {
  3027. ERR_FAIL_COND_V(!windows.has(p_window_id), false);
  3028. const WindowState &ws = windows[p_window_id];
  3029. switch (p_window_mode) {
  3030. case DisplayServer::WINDOW_MODE_WINDOWED: {
  3031. // Looks like it's guaranteed.
  3032. return true;
  3033. };
  3034. case DisplayServer::WINDOW_MODE_MINIMIZED: {
  3035. #ifdef LIBDECOR_ENABLED
  3036. if (ws.libdecor_frame) {
  3037. return libdecor_frame_has_capability(ws.libdecor_frame, LIBDECOR_ACTION_MINIMIZE);
  3038. }
  3039. #endif // LIBDECOR_ENABLED
  3040. return ws.can_minimize;
  3041. };
  3042. case DisplayServer::WINDOW_MODE_MAXIMIZED: {
  3043. // NOTE: libdecor doesn't seem to have a maximize capability query?
  3044. // The fact that there's a fullscreen one makes me suspicious.
  3045. return ws.can_maximize;
  3046. };
  3047. case DisplayServer::WINDOW_MODE_FULLSCREEN:
  3048. case DisplayServer::WINDOW_MODE_EXCLUSIVE_FULLSCREEN: {
  3049. #ifdef LIBDECOR_ENABLED
  3050. if (ws.libdecor_frame) {
  3051. return libdecor_frame_has_capability(ws.libdecor_frame, LIBDECOR_ACTION_FULLSCREEN);
  3052. }
  3053. #endif // LIBDECOR_ENABLED
  3054. return ws.can_fullscreen;
  3055. };
  3056. }
  3057. return false;
  3058. }
  3059. void WaylandThread::window_try_set_mode(DisplayServer::WindowID p_window_id, DisplayServer::WindowMode p_window_mode) {
  3060. ERR_FAIL_COND(!windows.has(p_window_id));
  3061. WindowState &ws = windows[p_window_id];
  3062. if (ws.mode == p_window_mode) {
  3063. return;
  3064. }
  3065. // Don't waste time with hidden windows and whatnot. Behave like it worked.
  3066. #ifdef LIBDECOR_ENABLED
  3067. if ((!ws.wl_surface || !ws.xdg_toplevel) && !ws.libdecor_frame) {
  3068. #else
  3069. if (!ws.wl_surface || !ws.xdg_toplevel) {
  3070. #endif // LIBDECOR_ENABLED
  3071. ws.mode = p_window_mode;
  3072. return;
  3073. }
  3074. // Return back to a windowed state so that we can apply what the user asked.
  3075. switch (ws.mode) {
  3076. case DisplayServer::WINDOW_MODE_WINDOWED: {
  3077. // Do nothing.
  3078. } break;
  3079. case DisplayServer::WINDOW_MODE_MINIMIZED: {
  3080. // We can't do much according to the xdg_shell protocol. I have no idea
  3081. // whether this implies that we should return or who knows what. For now
  3082. // we'll do nothing.
  3083. // TODO: Test this properly.
  3084. } break;
  3085. case DisplayServer::WINDOW_MODE_MAXIMIZED: {
  3086. // Try to unmaximize. This isn't garaunteed to work actually, so we'll have
  3087. // to check whether something changed.
  3088. if (ws.xdg_toplevel) {
  3089. xdg_toplevel_unset_maximized(ws.xdg_toplevel);
  3090. }
  3091. #ifdef LIBDECOR_ENABLED
  3092. if (ws.libdecor_frame) {
  3093. libdecor_frame_unset_maximized(ws.libdecor_frame);
  3094. }
  3095. #endif // LIBDECOR_ENABLED
  3096. } break;
  3097. case DisplayServer::WINDOW_MODE_FULLSCREEN:
  3098. case DisplayServer::WINDOW_MODE_EXCLUSIVE_FULLSCREEN: {
  3099. // Same thing as above, unset fullscreen and check later if it worked.
  3100. if (ws.xdg_toplevel) {
  3101. xdg_toplevel_unset_fullscreen(ws.xdg_toplevel);
  3102. }
  3103. #ifdef LIBDECOR_ENABLED
  3104. if (ws.libdecor_frame) {
  3105. libdecor_frame_unset_fullscreen(ws.libdecor_frame);
  3106. }
  3107. #endif // LIBDECOR_ENABLED
  3108. } break;
  3109. }
  3110. // Wait for a configure event and hope that something changed.
  3111. wl_display_roundtrip(wl_display);
  3112. if (ws.mode != DisplayServer::WINDOW_MODE_WINDOWED) {
  3113. // The compositor refused our "normalization" request. It'd be useless or
  3114. // unpredictable to attempt setting a new state. We're done.
  3115. return;
  3116. }
  3117. // Ask the compositor to set the state indicated by the new mode.
  3118. switch (p_window_mode) {
  3119. case DisplayServer::WINDOW_MODE_WINDOWED: {
  3120. // Do nothing. We're already windowed.
  3121. } break;
  3122. case DisplayServer::WINDOW_MODE_MINIMIZED: {
  3123. if (!window_can_set_mode(p_window_id, p_window_mode)) {
  3124. // Minimization is special (read below). Better not mess with it if the
  3125. // compositor explicitly announces that it doesn't support it.
  3126. break;
  3127. }
  3128. if (ws.xdg_toplevel) {
  3129. xdg_toplevel_set_minimized(ws.xdg_toplevel);
  3130. }
  3131. #ifdef LIBDECOR_ENABLED
  3132. if (ws.libdecor_frame) {
  3133. libdecor_frame_set_minimized(ws.libdecor_frame);
  3134. }
  3135. #endif // LIBDECOR_ENABLED
  3136. // We have no way to actually detect this state, so we'll have to report it
  3137. // manually to the engine (hoping that it worked). In the worst case it'll
  3138. // get reset by the next configure event.
  3139. ws.mode = DisplayServer::WINDOW_MODE_MINIMIZED;
  3140. } break;
  3141. case DisplayServer::WINDOW_MODE_MAXIMIZED: {
  3142. if (ws.xdg_toplevel) {
  3143. xdg_toplevel_set_maximized(ws.xdg_toplevel);
  3144. }
  3145. #ifdef LIBDECOR_ENABLED
  3146. if (ws.libdecor_frame) {
  3147. libdecor_frame_set_maximized(ws.libdecor_frame);
  3148. }
  3149. #endif // LIBDECOR_ENABLED
  3150. } break;
  3151. case DisplayServer::WINDOW_MODE_FULLSCREEN:
  3152. case DisplayServer::WINDOW_MODE_EXCLUSIVE_FULLSCREEN: {
  3153. if (ws.xdg_toplevel) {
  3154. xdg_toplevel_set_fullscreen(ws.xdg_toplevel, nullptr);
  3155. }
  3156. #ifdef LIBDECOR_ENABLED
  3157. if (ws.libdecor_frame) {
  3158. libdecor_frame_set_fullscreen(ws.libdecor_frame, nullptr);
  3159. }
  3160. #endif // LIBDECOR_ENABLED
  3161. } break;
  3162. default: {
  3163. } break;
  3164. }
  3165. }
  3166. void WaylandThread::window_set_borderless(DisplayServer::WindowID p_window_id, bool p_borderless) {
  3167. ERR_FAIL_COND(!windows.has(p_window_id));
  3168. WindowState &ws = windows[p_window_id];
  3169. if (ws.xdg_toplevel_decoration) {
  3170. if (p_borderless) {
  3171. // We implement borderless windows by simply asking the compositor to let
  3172. // us handle decorations (we don't).
  3173. zxdg_toplevel_decoration_v1_set_mode(ws.xdg_toplevel_decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE);
  3174. } else {
  3175. zxdg_toplevel_decoration_v1_set_mode(ws.xdg_toplevel_decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
  3176. }
  3177. }
  3178. #ifdef LIBDECOR_ENABLED
  3179. if (ws.libdecor_frame) {
  3180. bool visible_current = libdecor_frame_is_visible(ws.libdecor_frame);
  3181. bool visible_target = !p_borderless;
  3182. // NOTE: We have to do this otherwise we trip on a libdecor bug where it's
  3183. // possible to destroy the frame more than once, by setting the visibility
  3184. // to false multiple times and thus crashing.
  3185. if (visible_current != visible_target) {
  3186. print_verbose(vformat("Setting libdecor frame visibility to %d", visible_target));
  3187. libdecor_frame_set_visibility(ws.libdecor_frame, visible_target);
  3188. }
  3189. }
  3190. #endif // LIBDECOR_ENABLED
  3191. }
  3192. void WaylandThread::window_set_title(DisplayServer::WindowID p_window_id, const String &p_title) {
  3193. ERR_FAIL_COND(!windows.has(p_window_id));
  3194. WindowState &ws = windows[p_window_id];
  3195. #ifdef LIBDECOR_ENABLED
  3196. if (ws.libdecor_frame) {
  3197. libdecor_frame_set_title(ws.libdecor_frame, p_title.utf8().get_data());
  3198. }
  3199. #endif // LIBDECOR_ENABLE
  3200. if (ws.xdg_toplevel) {
  3201. xdg_toplevel_set_title(ws.xdg_toplevel, p_title.utf8().get_data());
  3202. }
  3203. }
  3204. void WaylandThread::window_set_app_id(DisplayServer::WindowID p_window_id, const String &p_app_id) {
  3205. ERR_FAIL_COND(!windows.has(p_window_id));
  3206. WindowState &ws = windows[p_window_id];
  3207. #ifdef LIBDECOR_ENABLED
  3208. if (ws.libdecor_frame) {
  3209. libdecor_frame_set_app_id(ws.libdecor_frame, p_app_id.utf8().get_data());
  3210. return;
  3211. }
  3212. #endif // LIBDECOR_ENABLED
  3213. if (ws.xdg_toplevel) {
  3214. xdg_toplevel_set_app_id(ws.xdg_toplevel, p_app_id.utf8().get_data());
  3215. return;
  3216. }
  3217. }
  3218. void WaylandThread::set_icon(const Ref<Image> &p_icon) {
  3219. ERR_FAIL_COND(p_icon.is_null());
  3220. Size2i icon_size = p_icon->get_size();
  3221. ERR_FAIL_COND(icon_size.width != icon_size.height);
  3222. if (!registry.xdg_toplevel_icon_manager) {
  3223. return;
  3224. }
  3225. if (xdg_icon) {
  3226. xdg_toplevel_icon_v1_destroy(xdg_icon);
  3227. }
  3228. if (icon_buffer) {
  3229. wl_buffer_destroy(icon_buffer);
  3230. }
  3231. // NOTE: The stride is the width of the icon in bytes.
  3232. uint32_t icon_stride = icon_size.width * 4;
  3233. uint32_t data_size = icon_stride * icon_size.height;
  3234. // We need a shared memory object file descriptor in order to create a
  3235. // wl_buffer through wl_shm.
  3236. int fd = WaylandThread::_allocate_shm_file(data_size);
  3237. ERR_FAIL_COND(fd == -1);
  3238. uint32_t *buffer_data = (uint32_t *)mmap(nullptr, data_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  3239. // Create the Wayland buffer.
  3240. struct wl_shm_pool *shm_pool = wl_shm_create_pool(registry.wl_shm, fd, data_size);
  3241. icon_buffer = wl_shm_pool_create_buffer(shm_pool, 0, icon_size.width, icon_size.height, icon_stride, WL_SHM_FORMAT_ARGB8888);
  3242. wl_shm_pool_destroy(shm_pool);
  3243. // Fill the cursor buffer with the image data.
  3244. for (uint32_t index = 0; index < (uint32_t)(icon_size.width * icon_size.height); index++) {
  3245. int row_index = index / icon_size.width;
  3246. int column_index = (index % icon_size.width);
  3247. buffer_data[index] = p_icon->get_pixel(column_index, row_index).to_argb32();
  3248. // Wayland buffers, unless specified, require associated alpha, so we'll just
  3249. // associate the alpha in-place.
  3250. uint8_t *pixel_data = (uint8_t *)&buffer_data[index];
  3251. pixel_data[0] = pixel_data[0] * pixel_data[3] / 255;
  3252. pixel_data[1] = pixel_data[1] * pixel_data[3] / 255;
  3253. pixel_data[2] = pixel_data[2] * pixel_data[3] / 255;
  3254. }
  3255. xdg_icon = xdg_toplevel_icon_manager_v1_create_icon(registry.xdg_toplevel_icon_manager);
  3256. xdg_toplevel_icon_v1_add_buffer(xdg_icon, icon_buffer, icon_size.width);
  3257. if (Engine::get_singleton()->is_editor_hint() || Engine::get_singleton()->is_project_manager_hint()) {
  3258. // Setting a name allows the godot icon to be overridden by a system theme.
  3259. // We only want the project manager and editor to get themed,
  3260. // Games will get icons with the protocol and themed icons with .desktop entries.
  3261. // NOTE: should be synced with the icon name in misc/dist/linuxbsd/Godot.desktop
  3262. xdg_toplevel_icon_v1_set_name(xdg_icon, "godot");
  3263. }
  3264. for (KeyValue<DisplayServer::WindowID, WindowState> &pair : windows) {
  3265. WindowState &ws = pair.value;
  3266. #ifdef LIBDECOR_ENABLED
  3267. if (ws.libdecor_frame) {
  3268. xdg_toplevel *toplevel = libdecor_frame_get_xdg_toplevel(ws.libdecor_frame);
  3269. ERR_FAIL_NULL(toplevel);
  3270. xdg_toplevel_icon_manager_v1_set_icon(registry.xdg_toplevel_icon_manager, toplevel, xdg_icon);
  3271. }
  3272. #endif
  3273. if (ws.xdg_toplevel) {
  3274. xdg_toplevel_icon_manager_v1_set_icon(registry.xdg_toplevel_icon_manager, ws.xdg_toplevel, xdg_icon);
  3275. }
  3276. }
  3277. }
  3278. DisplayServer::WindowMode WaylandThread::window_get_mode(DisplayServer::WindowID p_window_id) const {
  3279. ERR_FAIL_COND_V(!windows.has(p_window_id), DisplayServer::WINDOW_MODE_WINDOWED);
  3280. const WindowState &ws = windows[p_window_id];
  3281. return ws.mode;
  3282. }
  3283. void WaylandThread::window_request_attention(DisplayServer::WindowID p_window_id) {
  3284. ERR_FAIL_COND(!windows.has(p_window_id));
  3285. WindowState &ws = windows[p_window_id];
  3286. if (registry.xdg_activation) {
  3287. // Window attention requests are done through the XDG activation protocol.
  3288. xdg_activation_token_v1 *xdg_activation_token = xdg_activation_v1_get_activation_token(registry.xdg_activation);
  3289. xdg_activation_token_v1_add_listener(xdg_activation_token, &xdg_activation_token_listener, &ws);
  3290. xdg_activation_token_v1_commit(xdg_activation_token);
  3291. }
  3292. }
  3293. void WaylandThread::window_set_idle_inhibition(DisplayServer::WindowID p_window_id, bool p_enable) {
  3294. ERR_FAIL_COND(!windows.has(p_window_id));
  3295. WindowState &ws = windows[p_window_id];
  3296. if (p_enable) {
  3297. if (ws.registry->wp_idle_inhibit_manager && !ws.wp_idle_inhibitor) {
  3298. ERR_FAIL_NULL(ws.wl_surface);
  3299. ws.wp_idle_inhibitor = zwp_idle_inhibit_manager_v1_create_inhibitor(ws.registry->wp_idle_inhibit_manager, ws.wl_surface);
  3300. }
  3301. } else {
  3302. if (ws.wp_idle_inhibitor) {
  3303. zwp_idle_inhibitor_v1_destroy(ws.wp_idle_inhibitor);
  3304. ws.wp_idle_inhibitor = nullptr;
  3305. }
  3306. }
  3307. }
  3308. bool WaylandThread::window_get_idle_inhibition(DisplayServer::WindowID p_window_id) const {
  3309. ERR_FAIL_COND_V(!windows.has(p_window_id), false);
  3310. const WindowState &ws = windows[p_window_id];
  3311. return ws.wp_idle_inhibitor != nullptr;
  3312. }
  3313. WaylandThread::ScreenData WaylandThread::screen_get_data(int p_screen) const {
  3314. ERR_FAIL_INDEX_V(p_screen, registry.wl_outputs.size(), ScreenData());
  3315. return wl_output_get_screen_state(registry.wl_outputs.get(p_screen))->data;
  3316. }
  3317. int WaylandThread::get_screen_count() const {
  3318. return registry.wl_outputs.size();
  3319. }
  3320. DisplayServer::WindowID WaylandThread::pointer_get_pointed_window_id() const {
  3321. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3322. if (ss) {
  3323. // Let's determine the most recently used tablet tool.
  3324. TabletToolState *max_ts = nullptr;
  3325. for (struct zwp_tablet_tool_v2 *tool : ss->tablet_tools) {
  3326. TabletToolState *ts = wp_tablet_tool_get_state(tool);
  3327. ERR_CONTINUE(ts == nullptr);
  3328. TabletToolData &td = ts->data;
  3329. if (!max_ts) {
  3330. max_ts = ts;
  3331. continue;
  3332. }
  3333. if (MAX(td.button_time, td.motion_time) > MAX(max_ts->data.button_time, max_ts->data.motion_time)) {
  3334. max_ts = ts;
  3335. }
  3336. }
  3337. const PointerData &pd = ss->pointer_data;
  3338. if (max_ts) {
  3339. TabletToolData &td = max_ts->data;
  3340. if (MAX(td.button_time, td.motion_time) > MAX(pd.button_time, pd.motion_time)) {
  3341. return td.proximal_id;
  3342. }
  3343. }
  3344. return ss->pointer_data.pointed_id;
  3345. }
  3346. return DisplayServer::INVALID_WINDOW_ID;
  3347. }
  3348. DisplayServer::WindowID WaylandThread::pointer_get_last_pointed_window_id() const {
  3349. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3350. if (ss) {
  3351. // Let's determine the most recently used tablet tool.
  3352. TabletToolState *max_ts = nullptr;
  3353. for (struct zwp_tablet_tool_v2 *tool : ss->tablet_tools) {
  3354. TabletToolState *ts = wp_tablet_tool_get_state(tool);
  3355. ERR_CONTINUE(ts == nullptr);
  3356. TabletToolData &td = ts->data;
  3357. if (!max_ts) {
  3358. max_ts = ts;
  3359. continue;
  3360. }
  3361. if (MAX(td.button_time, td.motion_time) > MAX(max_ts->data.button_time, max_ts->data.motion_time)) {
  3362. max_ts = ts;
  3363. }
  3364. }
  3365. const PointerData &pd = ss->pointer_data;
  3366. if (max_ts) {
  3367. TabletToolData &td = max_ts->data;
  3368. if (MAX(td.button_time, td.motion_time) > MAX(pd.button_time, pd.motion_time)) {
  3369. return td.last_proximal_id;
  3370. }
  3371. }
  3372. return ss->pointer_data.last_pointed_id;
  3373. }
  3374. return DisplayServer::INVALID_WINDOW_ID;
  3375. }
  3376. void WaylandThread::pointer_set_constraint(PointerConstraint p_constraint) {
  3377. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3378. if (ss) {
  3379. seat_state_unlock_pointer(ss);
  3380. if (p_constraint == PointerConstraint::LOCKED) {
  3381. seat_state_lock_pointer(ss);
  3382. } else if (p_constraint == PointerConstraint::CONFINED) {
  3383. seat_state_confine_pointer(ss);
  3384. }
  3385. }
  3386. pointer_constraint = p_constraint;
  3387. }
  3388. void WaylandThread::pointer_set_hint(const Point2i &p_hint) {
  3389. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3390. if (!ss) {
  3391. return;
  3392. }
  3393. WindowState *ws = window_get_state(ss->pointer_data.pointed_id);
  3394. int hint_x = 0;
  3395. int hint_y = 0;
  3396. if (ws) {
  3397. // NOTE: It looks like it's not really recommended to convert from
  3398. // "godot-space" to "wayland-space" and in general I received mixed feelings
  3399. // discussing about this. I'm not really sure about the maths behind this but,
  3400. // oh well, we're setting a cursor hint. ¯\_(ツ)_/¯
  3401. // See: https://oftc.irclog.whitequark.org/wayland/2023-08-23#1692756914-1692816818
  3402. hint_x = std::round(p_hint.x / window_state_get_scale_factor(ws));
  3403. hint_y = std::round(p_hint.y / window_state_get_scale_factor(ws));
  3404. }
  3405. if (ss) {
  3406. seat_state_set_hint(ss, hint_x, hint_y);
  3407. }
  3408. }
  3409. WaylandThread::PointerConstraint WaylandThread::pointer_get_constraint() const {
  3410. return pointer_constraint;
  3411. }
  3412. BitField<MouseButtonMask> WaylandThread::pointer_get_button_mask() const {
  3413. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3414. if (ss) {
  3415. return ss->pointer_data.pressed_button_mask;
  3416. }
  3417. return BitField<MouseButtonMask>();
  3418. }
  3419. Error WaylandThread::init() {
  3420. #ifdef SOWRAP_ENABLED
  3421. #ifdef DEBUG_ENABLED
  3422. int dylibloader_verbose = 1;
  3423. #else
  3424. int dylibloader_verbose = 0;
  3425. #endif // DEBUG_ENABLED
  3426. if (initialize_wayland_client(dylibloader_verbose) != 0) {
  3427. WARN_PRINT("Can't load the Wayland client library.");
  3428. return ERR_CANT_CREATE;
  3429. }
  3430. if (initialize_wayland_cursor(dylibloader_verbose) != 0) {
  3431. WARN_PRINT("Can't load the Wayland cursor library.");
  3432. return ERR_CANT_CREATE;
  3433. }
  3434. if (initialize_xkbcommon(dylibloader_verbose) != 0) {
  3435. WARN_PRINT("Can't load the XKBcommon library.");
  3436. return ERR_CANT_CREATE;
  3437. }
  3438. #endif // SOWRAP_ENABLED
  3439. KeyMappingXKB::initialize();
  3440. wl_display = wl_display_connect(nullptr);
  3441. ERR_FAIL_NULL_V_MSG(wl_display, ERR_CANT_CREATE, "Can't connect to a Wayland display.");
  3442. thread_data.wl_display = wl_display;
  3443. events_thread.start(_poll_events_thread, &thread_data);
  3444. wl_registry = wl_display_get_registry(wl_display);
  3445. ERR_FAIL_NULL_V_MSG(wl_registry, ERR_UNAVAILABLE, "Can't obtain the Wayland registry global.");
  3446. registry.wayland_thread = this;
  3447. wl_registry_add_listener(wl_registry, &wl_registry_listener, &registry);
  3448. // Wait for registry to get notified from the compositor.
  3449. wl_display_roundtrip(wl_display);
  3450. ERR_FAIL_NULL_V_MSG(registry.wl_shm, ERR_UNAVAILABLE, "Can't obtain the Wayland shared memory global.");
  3451. ERR_FAIL_NULL_V_MSG(registry.wl_compositor, ERR_UNAVAILABLE, "Can't obtain the Wayland compositor global.");
  3452. ERR_FAIL_NULL_V_MSG(registry.xdg_wm_base, ERR_UNAVAILABLE, "Can't obtain the Wayland XDG shell global.");
  3453. if (!registry.xdg_decoration_manager) {
  3454. #ifdef LIBDECOR_ENABLED
  3455. WARN_PRINT("Can't obtain the XDG decoration manager. Libdecor will be used for drawing CSDs, if available.");
  3456. #else
  3457. WARN_PRINT("Can't obtain the XDG decoration manager. Decorations won't show up.");
  3458. #endif // LIBDECOR_ENABLED
  3459. }
  3460. if (!registry.xdg_activation) {
  3461. WARN_PRINT("Can't obtain the XDG activation global. Attention requesting won't work!");
  3462. }
  3463. #ifndef DBUS_ENABLED
  3464. if (!registry.wp_idle_inhibit_manager) {
  3465. WARN_PRINT("Can't obtain the idle inhibition manager. The screen might turn off even after calling screen_set_keep_on()!");
  3466. }
  3467. #endif // DBUS_ENABLED
  3468. if (!registry.wp_fifo_manager_name) {
  3469. WARN_PRINT("FIFO protocol not found! Frame pacing will be degraded.");
  3470. }
  3471. if (!registry.xdg_toplevel_icon_manager_name) {
  3472. WARN_PRINT("xdg-toplevel-icon protocol not found! Cannot set window icon.");
  3473. }
  3474. // Wait for seat capabilities.
  3475. wl_display_roundtrip(wl_display);
  3476. #ifdef LIBDECOR_ENABLED
  3477. bool libdecor_found = true;
  3478. #ifdef SOWRAP_ENABLED
  3479. if (initialize_libdecor(dylibloader_verbose) != 0) {
  3480. libdecor_found = false;
  3481. }
  3482. #endif // SOWRAP_ENABLED
  3483. if (libdecor_found) {
  3484. libdecor_context = libdecor_new(wl_display, (struct libdecor_interface *)&libdecor_interface);
  3485. } else {
  3486. print_verbose("libdecor not found. Client-side decorations disabled.");
  3487. }
  3488. #endif // LIBDECOR_ENABLED
  3489. cursor_theme_name = OS::get_singleton()->get_environment("XCURSOR_THEME");
  3490. unscaled_cursor_size = OS::get_singleton()->get_environment("XCURSOR_SIZE").to_int();
  3491. if (unscaled_cursor_size <= 0) {
  3492. print_verbose("Detected invalid cursor size preference, defaulting to 24.");
  3493. unscaled_cursor_size = 24;
  3494. }
  3495. // NOTE: The scale is useful here as it might've been updated by _update_scale.
  3496. bool cursor_theme_loaded = _load_cursor_theme(unscaled_cursor_size * cursor_scale);
  3497. if (!cursor_theme_loaded) {
  3498. return ERR_CANT_CREATE;
  3499. }
  3500. // Update the cursor.
  3501. cursor_set_shape(DisplayServer::CURSOR_ARROW);
  3502. initialized = true;
  3503. return OK;
  3504. }
  3505. void WaylandThread::cursor_set_visible(bool p_visible) {
  3506. cursor_visible = p_visible;
  3507. for (struct wl_seat *wl_seat : registry.wl_seats) {
  3508. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  3509. ERR_FAIL_NULL(ss);
  3510. seat_state_update_cursor(ss);
  3511. }
  3512. }
  3513. void WaylandThread::cursor_set_shape(DisplayServer::CursorShape p_cursor_shape) {
  3514. cursor_shape = p_cursor_shape;
  3515. for (struct wl_seat *wl_seat : registry.wl_seats) {
  3516. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  3517. ERR_FAIL_NULL(ss);
  3518. seat_state_update_cursor(ss);
  3519. }
  3520. }
  3521. void WaylandThread::cursor_shape_set_custom_image(DisplayServer::CursorShape p_cursor_shape, Ref<Image> p_image, const Point2i &p_hotspot) {
  3522. ERR_FAIL_COND(p_image.is_null());
  3523. Size2i image_size = p_image->get_size();
  3524. // NOTE: The stride is the width of the image in bytes.
  3525. unsigned int image_stride = image_size.width * 4;
  3526. unsigned int data_size = image_stride * image_size.height;
  3527. // We need a shared memory object file descriptor in order to create a
  3528. // wl_buffer through wl_shm.
  3529. int fd = WaylandThread::_allocate_shm_file(data_size);
  3530. ERR_FAIL_COND(fd == -1);
  3531. CustomCursor &cursor = custom_cursors[p_cursor_shape];
  3532. cursor.hotspot = p_hotspot;
  3533. if (cursor.wl_buffer) {
  3534. // Clean up the old Wayland buffer.
  3535. wl_buffer_destroy(cursor.wl_buffer);
  3536. }
  3537. if (cursor.buffer_data) {
  3538. // Clean up the old buffer data.
  3539. munmap(cursor.buffer_data, cursor.buffer_data_size);
  3540. }
  3541. cursor.buffer_data = (uint32_t *)mmap(nullptr, data_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  3542. cursor.buffer_data_size = data_size;
  3543. // Create the Wayland buffer.
  3544. struct wl_shm_pool *wl_shm_pool = wl_shm_create_pool(registry.wl_shm, fd, data_size);
  3545. // TODO: Make sure that WL_SHM_FORMAT_ARGB8888 format is supported. It
  3546. // technically isn't garaunteed to be supported, but I think that'd be a
  3547. // pretty unlikely thing to stumble upon.
  3548. cursor.wl_buffer = wl_shm_pool_create_buffer(wl_shm_pool, 0, image_size.width, image_size.height, image_stride, WL_SHM_FORMAT_ARGB8888);
  3549. wl_shm_pool_destroy(wl_shm_pool);
  3550. // Fill the cursor buffer with the image data.
  3551. for (unsigned int index = 0; index < (unsigned int)(image_size.width * image_size.height); index++) {
  3552. int row_index = std::floor(index / image_size.width);
  3553. int column_index = (index % int(image_size.width));
  3554. cursor.buffer_data[index] = p_image->get_pixel(column_index, row_index).to_argb32();
  3555. // Wayland buffers, unless specified, require associated alpha, so we'll just
  3556. // associate the alpha in-place.
  3557. uint8_t *pixel_data = (uint8_t *)&cursor.buffer_data[index];
  3558. pixel_data[0] = pixel_data[0] * pixel_data[3] / 255;
  3559. pixel_data[1] = pixel_data[1] * pixel_data[3] / 255;
  3560. pixel_data[2] = pixel_data[2] * pixel_data[3] / 255;
  3561. }
  3562. }
  3563. void WaylandThread::cursor_shape_clear_custom_image(DisplayServer::CursorShape p_cursor_shape) {
  3564. if (custom_cursors.has(p_cursor_shape)) {
  3565. CustomCursor cursor = custom_cursors[p_cursor_shape];
  3566. custom_cursors.erase(p_cursor_shape);
  3567. if (cursor.wl_buffer) {
  3568. wl_buffer_destroy(cursor.wl_buffer);
  3569. }
  3570. if (cursor.buffer_data) {
  3571. munmap(cursor.buffer_data, cursor.buffer_data_size);
  3572. }
  3573. }
  3574. }
  3575. void WaylandThread::window_set_ime_active(const bool p_active, DisplayServer::WindowID p_window_id) {
  3576. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3577. if (ss && ss->wp_text_input && ss->ime_enabled) {
  3578. if (p_active) {
  3579. ss->ime_active = true;
  3580. zwp_text_input_v3_enable(ss->wp_text_input);
  3581. zwp_text_input_v3_set_cursor_rectangle(ss->wp_text_input, ss->ime_rect.position.x, ss->ime_rect.position.y, ss->ime_rect.size.x, ss->ime_rect.size.y);
  3582. } else {
  3583. ss->ime_active = false;
  3584. ss->ime_text = String();
  3585. ss->ime_text_commit = String();
  3586. ss->ime_cursor = Vector2i();
  3587. zwp_text_input_v3_disable(ss->wp_text_input);
  3588. }
  3589. zwp_text_input_v3_commit(ss->wp_text_input);
  3590. }
  3591. }
  3592. void WaylandThread::window_set_ime_position(const Point2i &p_pos, DisplayServer::WindowID p_window_id) {
  3593. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3594. if (ss && ss->wp_text_input && ss->ime_enabled) {
  3595. ss->ime_rect = Rect2i(p_pos, Size2i(1, 10));
  3596. zwp_text_input_v3_set_cursor_rectangle(ss->wp_text_input, ss->ime_rect.position.x, ss->ime_rect.position.y, ss->ime_rect.size.x, ss->ime_rect.size.y);
  3597. zwp_text_input_v3_commit(ss->wp_text_input);
  3598. }
  3599. }
  3600. int WaylandThread::keyboard_get_layout_count() const {
  3601. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3602. if (ss && ss->xkb_keymap) {
  3603. return xkb_keymap_num_layouts(ss->xkb_keymap);
  3604. }
  3605. return 0;
  3606. }
  3607. int WaylandThread::keyboard_get_current_layout_index() const {
  3608. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3609. if (ss) {
  3610. return ss->current_layout_index;
  3611. }
  3612. return 0;
  3613. }
  3614. void WaylandThread::keyboard_set_current_layout_index(int p_index) {
  3615. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3616. if (ss) {
  3617. ss->current_layout_index = p_index;
  3618. }
  3619. }
  3620. String WaylandThread::keyboard_get_layout_name(int p_index) const {
  3621. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3622. if (ss && ss->xkb_keymap) {
  3623. return String::utf8(xkb_keymap_layout_get_name(ss->xkb_keymap, p_index));
  3624. }
  3625. return "";
  3626. }
  3627. Key WaylandThread::keyboard_get_key_from_physical(Key p_key) const {
  3628. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3629. if (ss && ss->xkb_state) {
  3630. xkb_keycode_t xkb_keycode = KeyMappingXKB::get_xkb_keycode(p_key);
  3631. return KeyMappingXKB::get_keycode(xkb_state_key_get_one_sym(ss->xkb_state, xkb_keycode));
  3632. }
  3633. return Key::NONE;
  3634. }
  3635. void WaylandThread::keyboard_echo_keys() {
  3636. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3637. if (ss) {
  3638. seat_state_echo_keys(ss);
  3639. }
  3640. }
  3641. void WaylandThread::selection_set_text(const String &p_text) {
  3642. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3643. if (registry.wl_data_device_manager == nullptr) {
  3644. DEBUG_LOG_WAYLAND_THREAD("Couldn't set selection, wl_data_device_manager global not available.");
  3645. return;
  3646. }
  3647. if (ss == nullptr) {
  3648. DEBUG_LOG_WAYLAND_THREAD("Couldn't set selection, current seat not set.");
  3649. return;
  3650. }
  3651. if (ss->wl_data_device == nullptr) {
  3652. DEBUG_LOG_WAYLAND_THREAD("Couldn't set selection, seat doesn't have wl_data_device.");
  3653. return;
  3654. }
  3655. ss->selection_data = p_text.to_utf8_buffer();
  3656. if (ss->wl_data_source_selection == nullptr) {
  3657. ss->wl_data_source_selection = wl_data_device_manager_create_data_source(registry.wl_data_device_manager);
  3658. wl_data_source_add_listener(ss->wl_data_source_selection, &wl_data_source_listener, ss);
  3659. wl_data_source_offer(ss->wl_data_source_selection, "text/plain;charset=utf-8");
  3660. wl_data_source_offer(ss->wl_data_source_selection, "text/plain");
  3661. // TODO: Implement a good way of getting the latest serial from the user.
  3662. wl_data_device_set_selection(ss->wl_data_device, ss->wl_data_source_selection, MAX(ss->pointer_data.button_serial, ss->last_key_pressed_serial));
  3663. }
  3664. // Wait for the message to get to the server before continuing, otherwise the
  3665. // clipboard update might come with a delay.
  3666. wl_display_roundtrip(wl_display);
  3667. }
  3668. bool WaylandThread::selection_has_mime(const String &p_mime) const {
  3669. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3670. if (ss == nullptr) {
  3671. DEBUG_LOG_WAYLAND_THREAD("Couldn't get selection, current seat not set.");
  3672. return false;
  3673. }
  3674. OfferState *os = wl_data_offer_get_offer_state(ss->wl_data_offer_selection);
  3675. if (!os) {
  3676. return false;
  3677. }
  3678. return os->mime_types.has(p_mime);
  3679. }
  3680. Vector<uint8_t> WaylandThread::selection_get_mime(const String &p_mime) const {
  3681. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3682. if (ss == nullptr) {
  3683. DEBUG_LOG_WAYLAND_THREAD("Couldn't get selection, current seat not set.");
  3684. return Vector<uint8_t>();
  3685. }
  3686. if (ss->wl_data_source_selection) {
  3687. // We have a source so the stuff we're pasting is ours. We'll have to pass the
  3688. // data directly or we'd stall waiting for Godot (ourselves) to send us the
  3689. // data :P
  3690. OfferState *os = wl_data_offer_get_offer_state(ss->wl_data_offer_selection);
  3691. ERR_FAIL_NULL_V(os, Vector<uint8_t>());
  3692. if (os->mime_types.has(p_mime)) {
  3693. // All righty, we're offering this type. Let's just return the data as is.
  3694. return ss->selection_data;
  3695. }
  3696. // ... we don't offer that type. Oh well.
  3697. return Vector<uint8_t>();
  3698. }
  3699. return _wl_data_offer_read(wl_display, p_mime.utf8().get_data(), ss->wl_data_offer_selection);
  3700. }
  3701. bool WaylandThread::primary_has_mime(const String &p_mime) const {
  3702. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3703. if (ss == nullptr) {
  3704. DEBUG_LOG_WAYLAND_THREAD("Couldn't get selection, current seat not set.");
  3705. return false;
  3706. }
  3707. OfferState *os = wp_primary_selection_offer_get_offer_state(ss->wp_primary_selection_offer);
  3708. if (!os) {
  3709. return false;
  3710. }
  3711. return os->mime_types.has(p_mime);
  3712. }
  3713. Vector<uint8_t> WaylandThread::primary_get_mime(const String &p_mime) const {
  3714. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3715. if (ss == nullptr) {
  3716. DEBUG_LOG_WAYLAND_THREAD("Couldn't get primary, current seat not set.");
  3717. return Vector<uint8_t>();
  3718. }
  3719. if (ss->wp_primary_selection_source) {
  3720. // We have a source so the stuff we're pasting is ours. We'll have to pass the
  3721. // data directly or we'd stall waiting for Godot (ourselves) to send us the
  3722. // data :P
  3723. OfferState *os = wp_primary_selection_offer_get_offer_state(ss->wp_primary_selection_offer);
  3724. ERR_FAIL_NULL_V(os, Vector<uint8_t>());
  3725. if (os->mime_types.has(p_mime)) {
  3726. // All righty, we're offering this type. Let's just return the data as is.
  3727. return ss->selection_data;
  3728. }
  3729. // ... we don't offer that type. Oh well.
  3730. return Vector<uint8_t>();
  3731. }
  3732. return _wp_primary_selection_offer_read(wl_display, p_mime.utf8().get_data(), ss->wp_primary_selection_offer);
  3733. }
  3734. void WaylandThread::primary_set_text(const String &p_text) {
  3735. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3736. if (registry.wp_primary_selection_device_manager == nullptr) {
  3737. DEBUG_LOG_WAYLAND_THREAD("Couldn't set primary, protocol not available.");
  3738. return;
  3739. }
  3740. if (ss == nullptr) {
  3741. DEBUG_LOG_WAYLAND_THREAD("Couldn't set primary, current seat not set.");
  3742. return;
  3743. }
  3744. if (ss->wp_primary_selection_device == nullptr) {
  3745. DEBUG_LOG_WAYLAND_THREAD("Couldn't set primary selection, seat doesn't have wp_primary_selection_device.");
  3746. return;
  3747. }
  3748. ss->primary_data = p_text.to_utf8_buffer();
  3749. if (ss->wp_primary_selection_source == nullptr) {
  3750. ss->wp_primary_selection_source = zwp_primary_selection_device_manager_v1_create_source(registry.wp_primary_selection_device_manager);
  3751. zwp_primary_selection_source_v1_add_listener(ss->wp_primary_selection_source, &wp_primary_selection_source_listener, ss);
  3752. zwp_primary_selection_source_v1_offer(ss->wp_primary_selection_source, "text/plain;charset=utf-8");
  3753. zwp_primary_selection_source_v1_offer(ss->wp_primary_selection_source, "text/plain");
  3754. // TODO: Implement a good way of getting the latest serial from the user.
  3755. zwp_primary_selection_device_v1_set_selection(ss->wp_primary_selection_device, ss->wp_primary_selection_source, MAX(ss->pointer_data.button_serial, ss->last_key_pressed_serial));
  3756. }
  3757. // Wait for the message to get to the server before continuing, otherwise the
  3758. // clipboard update might come with a delay.
  3759. wl_display_roundtrip(wl_display);
  3760. }
  3761. void WaylandThread::commit_surfaces() {
  3762. for (KeyValue<DisplayServer::WindowID, WindowState> &pair : windows) {
  3763. wl_surface_commit(pair.value.wl_surface);
  3764. }
  3765. }
  3766. void WaylandThread::set_frame() {
  3767. frame = true;
  3768. }
  3769. bool WaylandThread::get_reset_frame() {
  3770. bool old_frame = frame;
  3771. frame = false;
  3772. return old_frame;
  3773. }
  3774. // Dispatches events until a frame event is received, a window is reported as
  3775. // suspended or the timeout expires.
  3776. bool WaylandThread::wait_frame_suspend_ms(int p_timeout) {
  3777. // This is a bit of a chicken and egg thing... Looks like the main event loop
  3778. // has to call its rightfully forever-blocking poll right in between
  3779. // `wl_display_prepare_read` and `wl_display_read`. This means, that it will
  3780. // basically be guaranteed to stay stuck in a "prepare read" state, where it
  3781. // will block any other attempt at reading the display fd, such as ours. The
  3782. // solution? Let's make sure the mutex is locked (it should) and unblock the
  3783. // main thread with a roundtrip!
  3784. MutexLock mutex_lock(mutex);
  3785. wl_display_roundtrip(wl_display);
  3786. if (is_suspended()) {
  3787. // All windows are suspended! The compositor is telling us _explicitly_ that
  3788. // we don't need to draw, without letting us guess through the frame event's
  3789. // timing and stuff like that. Our job here is done.
  3790. return false;
  3791. }
  3792. if (frame) {
  3793. // We already have a frame! Probably it got there while the caller locked :D
  3794. frame = false;
  3795. return true;
  3796. }
  3797. struct pollfd poll_fd;
  3798. poll_fd.fd = wl_display_get_fd(wl_display);
  3799. poll_fd.events = POLLIN | POLLHUP;
  3800. int begin_ms = OS::get_singleton()->get_ticks_msec();
  3801. int remaining_ms = p_timeout;
  3802. while (remaining_ms > 0) {
  3803. // Empty the event queue while it's full.
  3804. while (wl_display_prepare_read(wl_display) != 0) {
  3805. if (wl_display_dispatch_pending(wl_display) == -1) {
  3806. // Oh no. We'll check and handle any display error below.
  3807. break;
  3808. }
  3809. if (is_suspended()) {
  3810. return false;
  3811. }
  3812. if (frame) {
  3813. // We had a frame event in the queue :D
  3814. frame = false;
  3815. return true;
  3816. }
  3817. }
  3818. int werror = wl_display_get_error(wl_display);
  3819. if (werror) {
  3820. if (werror == EPROTO) {
  3821. struct wl_interface *wl_interface = nullptr;
  3822. uint32_t id = 0;
  3823. int error_code = wl_display_get_protocol_error(wl_display, (const struct wl_interface **)&wl_interface, &id);
  3824. CRASH_NOW_MSG(vformat("Wayland protocol error %d on interface %s@%d.", error_code, wl_interface ? wl_interface->name : "unknown", id));
  3825. } else {
  3826. CRASH_NOW_MSG(vformat("Wayland client error code %d.", werror));
  3827. }
  3828. }
  3829. wl_display_flush(wl_display);
  3830. // Wait for the event file descriptor to have new data.
  3831. poll(&poll_fd, 1, remaining_ms);
  3832. if (poll_fd.revents | POLLIN) {
  3833. // Load the queues with fresh new data.
  3834. wl_display_read_events(wl_display);
  3835. } else {
  3836. // Oh well... Stop signaling that we want to read.
  3837. wl_display_cancel_read(wl_display);
  3838. // We've got no new events :(
  3839. // We won't even bother with checking the frame flag.
  3840. return false;
  3841. }
  3842. // Let's try dispatching now...
  3843. wl_display_dispatch_pending(wl_display);
  3844. if (is_suspended()) {
  3845. return false;
  3846. }
  3847. if (frame) {
  3848. frame = false;
  3849. return true;
  3850. }
  3851. remaining_ms -= OS::get_singleton()->get_ticks_msec() - begin_ms;
  3852. }
  3853. DEBUG_LOG_WAYLAND_THREAD("Frame timeout.");
  3854. return false;
  3855. }
  3856. uint64_t WaylandThread::window_get_last_frame_time(DisplayServer::WindowID p_window_id) const {
  3857. ERR_FAIL_COND_V(!windows.has(p_window_id), false);
  3858. return windows[p_window_id].last_frame_time;
  3859. }
  3860. bool WaylandThread::window_is_suspended(DisplayServer::WindowID p_window_id) const {
  3861. ERR_FAIL_COND_V(!windows.has(p_window_id), false);
  3862. return windows[p_window_id].suspended;
  3863. }
  3864. bool WaylandThread::is_fifo_available() const {
  3865. return registry.wp_fifo_manager_name != 0;
  3866. }
  3867. bool WaylandThread::is_suspended() const {
  3868. for (const KeyValue<DisplayServer::WindowID, WindowState> &E : windows) {
  3869. if (!E.value.suspended) {
  3870. return false;
  3871. }
  3872. }
  3873. return true;
  3874. }
  3875. void WaylandThread::destroy() {
  3876. if (!initialized) {
  3877. return;
  3878. }
  3879. if (wl_display && events_thread.is_started()) {
  3880. thread_data.thread_done.set();
  3881. // By sending a roundtrip message we're unblocking the polling thread so that
  3882. // it can realize that it's done and also handle every event that's left.
  3883. wl_display_roundtrip(wl_display);
  3884. events_thread.wait_to_finish();
  3885. }
  3886. for (KeyValue<DisplayServer::WindowID, WindowState> &pair : windows) {
  3887. WindowState &ws = pair.value;
  3888. if (ws.wp_fractional_scale) {
  3889. wp_fractional_scale_v1_destroy(ws.wp_fractional_scale);
  3890. }
  3891. if (ws.wp_viewport) {
  3892. wp_viewport_destroy(ws.wp_viewport);
  3893. }
  3894. if (ws.frame_callback) {
  3895. wl_callback_destroy(ws.frame_callback);
  3896. }
  3897. #ifdef LIBDECOR_ENABLED
  3898. if (ws.libdecor_frame) {
  3899. libdecor_frame_close(ws.libdecor_frame);
  3900. }
  3901. #endif // LIBDECOR_ENABLED
  3902. if (ws.xdg_toplevel) {
  3903. xdg_toplevel_destroy(ws.xdg_toplevel);
  3904. }
  3905. if (ws.xdg_surface) {
  3906. xdg_surface_destroy(ws.xdg_surface);
  3907. }
  3908. if (ws.wl_surface) {
  3909. wl_surface_destroy(ws.wl_surface);
  3910. }
  3911. }
  3912. for (struct wl_seat *wl_seat : registry.wl_seats) {
  3913. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  3914. ERR_FAIL_NULL(ss);
  3915. wl_seat_destroy(wl_seat);
  3916. xkb_context_unref(ss->xkb_context);
  3917. xkb_state_unref(ss->xkb_state);
  3918. xkb_keymap_unref(ss->xkb_keymap);
  3919. if (ss->wl_keyboard) {
  3920. wl_keyboard_destroy(ss->wl_keyboard);
  3921. }
  3922. if (ss->keymap_buffer) {
  3923. munmap((void *)ss->keymap_buffer, ss->keymap_buffer_size);
  3924. }
  3925. if (ss->wl_pointer) {
  3926. wl_pointer_destroy(ss->wl_pointer);
  3927. }
  3928. if (ss->cursor_frame_callback) {
  3929. // We don't need to set a null userdata for safety as the thread is done.
  3930. wl_callback_destroy(ss->cursor_frame_callback);
  3931. }
  3932. if (ss->cursor_surface) {
  3933. wl_surface_destroy(ss->cursor_surface);
  3934. }
  3935. if (ss->wl_data_device) {
  3936. wl_data_device_destroy(ss->wl_data_device);
  3937. }
  3938. if (ss->wp_cursor_shape_device) {
  3939. wp_cursor_shape_device_v1_destroy(ss->wp_cursor_shape_device);
  3940. }
  3941. if (ss->wp_relative_pointer) {
  3942. zwp_relative_pointer_v1_destroy(ss->wp_relative_pointer);
  3943. }
  3944. if (ss->wp_locked_pointer) {
  3945. zwp_locked_pointer_v1_destroy(ss->wp_locked_pointer);
  3946. }
  3947. if (ss->wp_confined_pointer) {
  3948. zwp_confined_pointer_v1_destroy(ss->wp_confined_pointer);
  3949. }
  3950. if (ss->wp_tablet_seat) {
  3951. zwp_tablet_seat_v2_destroy(ss->wp_tablet_seat);
  3952. }
  3953. for (struct zwp_tablet_tool_v2 *tool : ss->tablet_tools) {
  3954. TabletToolState *state = wp_tablet_tool_get_state(tool);
  3955. if (state) {
  3956. memdelete(state);
  3957. }
  3958. zwp_tablet_tool_v2_destroy(tool);
  3959. }
  3960. memdelete(ss);
  3961. }
  3962. for (struct wl_output *wl_output : registry.wl_outputs) {
  3963. ERR_FAIL_NULL(wl_output);
  3964. memdelete(wl_output_get_screen_state(wl_output));
  3965. wl_output_destroy(wl_output);
  3966. }
  3967. if (wl_cursor_theme) {
  3968. wl_cursor_theme_destroy(wl_cursor_theme);
  3969. }
  3970. if (registry.wp_idle_inhibit_manager) {
  3971. zwp_idle_inhibit_manager_v1_destroy(registry.wp_idle_inhibit_manager);
  3972. }
  3973. if (registry.wp_pointer_constraints) {
  3974. zwp_pointer_constraints_v1_destroy(registry.wp_pointer_constraints);
  3975. }
  3976. if (registry.wp_pointer_gestures) {
  3977. zwp_pointer_gestures_v1_destroy(registry.wp_pointer_gestures);
  3978. }
  3979. if (registry.wp_relative_pointer_manager) {
  3980. zwp_relative_pointer_manager_v1_destroy(registry.wp_relative_pointer_manager);
  3981. }
  3982. if (registry.xdg_activation) {
  3983. xdg_activation_v1_destroy(registry.xdg_activation);
  3984. }
  3985. if (registry.xdg_system_bell) {
  3986. xdg_system_bell_v1_destroy(registry.xdg_system_bell);
  3987. }
  3988. if (registry.xdg_toplevel_icon_manager) {
  3989. xdg_toplevel_icon_manager_v1_destroy(registry.xdg_toplevel_icon_manager);
  3990. if (xdg_icon) {
  3991. xdg_toplevel_icon_v1_destroy(xdg_icon);
  3992. }
  3993. if (icon_buffer) {
  3994. wl_buffer_destroy(icon_buffer);
  3995. }
  3996. }
  3997. if (registry.xdg_decoration_manager) {
  3998. zxdg_decoration_manager_v1_destroy(registry.xdg_decoration_manager);
  3999. }
  4000. if (registry.wp_cursor_shape_manager) {
  4001. wp_cursor_shape_manager_v1_destroy(registry.wp_cursor_shape_manager);
  4002. }
  4003. if (registry.wp_fractional_scale_manager) {
  4004. wp_fractional_scale_manager_v1_destroy(registry.wp_fractional_scale_manager);
  4005. }
  4006. if (registry.wp_viewporter) {
  4007. wp_viewporter_destroy(registry.wp_viewporter);
  4008. }
  4009. if (registry.xdg_wm_base) {
  4010. xdg_wm_base_destroy(registry.xdg_wm_base);
  4011. }
  4012. // NOTE: Deprecated.
  4013. if (registry.xdg_exporter_v1) {
  4014. zxdg_exporter_v1_destroy(registry.xdg_exporter_v1);
  4015. }
  4016. if (registry.xdg_exporter_v2) {
  4017. zxdg_exporter_v2_destroy(registry.xdg_exporter_v2);
  4018. }
  4019. if (registry.wl_shm) {
  4020. wl_shm_destroy(registry.wl_shm);
  4021. }
  4022. if (registry.wl_compositor) {
  4023. wl_compositor_destroy(registry.wl_compositor);
  4024. }
  4025. if (wl_registry) {
  4026. wl_registry_destroy(wl_registry);
  4027. }
  4028. if (wl_display) {
  4029. wl_display_disconnect(wl_display);
  4030. }
  4031. }
  4032. #endif // WAYLAND_ENABLED