wayland_thread.cpp 138 KB

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