wayland_thread.cpp 144 KB

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