wayland_thread.cpp 137 KB

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