wayland_thread.cpp 131 KB

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