wayland_thread.cpp 142 KB

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