wayland_thread.cpp 137 KB

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