wayland_thread.cpp 156 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997
  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(p_ss->focused_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. for (KeyValue<DisplayServer::WindowID, WindowState> &pair : registry->wayland_thread->windows) {
  578. WindowState ws = pair.value;
  579. if (registry->wp_viewporter) {
  580. wp_viewporter_destroy(registry->wp_viewporter);
  581. registry->wp_viewporter = nullptr;
  582. }
  583. if (ws.wp_viewport) {
  584. wp_viewport_destroy(ws.wp_viewport);
  585. ws.wp_viewport = nullptr;
  586. }
  587. }
  588. registry->wp_viewporter_name = 0;
  589. return;
  590. }
  591. if (name == registry->wp_fractional_scale_manager_name) {
  592. for (KeyValue<DisplayServer::WindowID, WindowState> &pair : registry->wayland_thread->windows) {
  593. WindowState ws = pair.value;
  594. if (registry->wp_fractional_scale_manager) {
  595. wp_fractional_scale_manager_v1_destroy(registry->wp_fractional_scale_manager);
  596. registry->wp_fractional_scale_manager = nullptr;
  597. }
  598. if (ws.wp_fractional_scale) {
  599. wp_fractional_scale_v1_destroy(ws.wp_fractional_scale);
  600. ws.wp_fractional_scale = nullptr;
  601. }
  602. }
  603. registry->wp_fractional_scale_manager_name = 0;
  604. }
  605. if (name == registry->xdg_decoration_manager_name) {
  606. if (registry->xdg_decoration_manager) {
  607. zxdg_decoration_manager_v1_destroy(registry->xdg_decoration_manager);
  608. registry->xdg_decoration_manager = nullptr;
  609. }
  610. registry->xdg_decoration_manager_name = 0;
  611. return;
  612. }
  613. if (name == registry->xdg_system_bell_name) {
  614. if (registry->xdg_system_bell) {
  615. xdg_system_bell_v1_destroy(registry->xdg_system_bell);
  616. registry->xdg_system_bell = nullptr;
  617. }
  618. registry->xdg_system_bell_name = 0;
  619. return;
  620. }
  621. if (name == registry->xdg_activation_name) {
  622. if (registry->xdg_activation) {
  623. xdg_activation_v1_destroy(registry->xdg_activation);
  624. registry->xdg_activation = nullptr;
  625. }
  626. registry->xdg_activation_name = 0;
  627. return;
  628. }
  629. if (name == registry->wp_primary_selection_device_manager_name) {
  630. if (registry->wp_primary_selection_device_manager) {
  631. zwp_primary_selection_device_manager_v1_destroy(registry->wp_primary_selection_device_manager);
  632. registry->wp_primary_selection_device_manager = nullptr;
  633. }
  634. registry->wp_primary_selection_device_manager_name = 0;
  635. // This global is used to create some seat data. Let's clean it.
  636. for (struct wl_seat *wl_seat : registry->wl_seats) {
  637. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  638. ERR_FAIL_NULL(ss);
  639. if (ss->wp_primary_selection_device) {
  640. zwp_primary_selection_device_v1_destroy(ss->wp_primary_selection_device);
  641. ss->wp_primary_selection_device = nullptr;
  642. }
  643. if (ss->wp_primary_selection_source) {
  644. zwp_primary_selection_source_v1_destroy(ss->wp_primary_selection_source);
  645. ss->wp_primary_selection_source = nullptr;
  646. }
  647. if (ss->wp_primary_selection_offer) {
  648. memfree(wp_primary_selection_offer_get_offer_state(ss->wp_primary_selection_offer));
  649. zwp_primary_selection_offer_v1_destroy(ss->wp_primary_selection_offer);
  650. ss->wp_primary_selection_offer = nullptr;
  651. }
  652. }
  653. return;
  654. }
  655. if (name == registry->wp_relative_pointer_manager_name) {
  656. if (registry->wp_relative_pointer_manager) {
  657. zwp_relative_pointer_manager_v1_destroy(registry->wp_relative_pointer_manager);
  658. registry->wp_relative_pointer_manager = nullptr;
  659. }
  660. registry->wp_relative_pointer_manager_name = 0;
  661. // This global is used to create some seat data. Let's clean it.
  662. for (struct wl_seat *wl_seat : registry->wl_seats) {
  663. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  664. ERR_FAIL_NULL(ss);
  665. if (ss->wp_relative_pointer) {
  666. zwp_relative_pointer_v1_destroy(ss->wp_relative_pointer);
  667. ss->wp_relative_pointer = nullptr;
  668. }
  669. }
  670. return;
  671. }
  672. if (name == registry->wp_pointer_constraints_name) {
  673. if (registry->wp_pointer_constraints) {
  674. zwp_pointer_constraints_v1_destroy(registry->wp_pointer_constraints);
  675. registry->wp_pointer_constraints = nullptr;
  676. }
  677. registry->wp_pointer_constraints_name = 0;
  678. // This global is used to create some seat data. Let's clean it.
  679. for (struct wl_seat *wl_seat : registry->wl_seats) {
  680. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  681. ERR_FAIL_NULL(ss);
  682. if (ss->wp_relative_pointer) {
  683. zwp_relative_pointer_v1_destroy(ss->wp_relative_pointer);
  684. ss->wp_relative_pointer = nullptr;
  685. }
  686. if (ss->wp_locked_pointer) {
  687. zwp_locked_pointer_v1_destroy(ss->wp_locked_pointer);
  688. ss->wp_locked_pointer = nullptr;
  689. }
  690. if (ss->wp_confined_pointer) {
  691. zwp_confined_pointer_v1_destroy(ss->wp_confined_pointer);
  692. ss->wp_confined_pointer = nullptr;
  693. }
  694. }
  695. return;
  696. }
  697. if (name == registry->wp_pointer_gestures_name) {
  698. if (registry->wp_pointer_gestures) {
  699. zwp_pointer_gestures_v1_destroy(registry->wp_pointer_gestures);
  700. }
  701. registry->wp_pointer_gestures = nullptr;
  702. registry->wp_pointer_gestures_name = 0;
  703. // This global is used to create some seat data. Let's clean it.
  704. for (struct wl_seat *wl_seat : registry->wl_seats) {
  705. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  706. ERR_FAIL_NULL(ss);
  707. if (ss->wp_pointer_gesture_pinch) {
  708. zwp_pointer_gesture_pinch_v1_destroy(ss->wp_pointer_gesture_pinch);
  709. ss->wp_pointer_gesture_pinch = nullptr;
  710. }
  711. }
  712. return;
  713. }
  714. if (name == registry->wp_idle_inhibit_manager_name) {
  715. if (registry->wp_idle_inhibit_manager) {
  716. zwp_idle_inhibit_manager_v1_destroy(registry->wp_idle_inhibit_manager);
  717. registry->wp_idle_inhibit_manager = nullptr;
  718. }
  719. registry->wp_idle_inhibit_manager_name = 0;
  720. return;
  721. }
  722. if (name == registry->wp_tablet_manager_name) {
  723. if (registry->wp_tablet_manager) {
  724. zwp_tablet_manager_v2_destroy(registry->wp_tablet_manager);
  725. registry->wp_tablet_manager = nullptr;
  726. }
  727. registry->wp_tablet_manager_name = 0;
  728. // This global is used to create some seat data. Let's clean it.
  729. for (struct wl_seat *wl_seat : registry->wl_seats) {
  730. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  731. ERR_FAIL_NULL(ss);
  732. for (struct zwp_tablet_tool_v2 *tool : ss->tablet_tools) {
  733. TabletToolState *state = wp_tablet_tool_get_state(tool);
  734. if (state) {
  735. memdelete(state);
  736. }
  737. zwp_tablet_tool_v2_destroy(tool);
  738. }
  739. ss->tablet_tools.clear();
  740. }
  741. return;
  742. }
  743. if (name == registry->wp_text_input_manager_name) {
  744. if (registry->wp_text_input_manager) {
  745. zwp_text_input_manager_v3_destroy(registry->wp_text_input_manager);
  746. registry->wp_text_input_manager = nullptr;
  747. }
  748. registry->wp_text_input_manager_name = 0;
  749. for (struct wl_seat *wl_seat : registry->wl_seats) {
  750. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  751. ERR_FAIL_NULL(ss);
  752. zwp_text_input_v3_destroy(ss->wp_text_input);
  753. ss->wp_text_input = nullptr;
  754. }
  755. return;
  756. }
  757. {
  758. // Iterate through all of the seats to find if any got removed.
  759. List<struct wl_seat *>::Element *E = registry->wl_seats.front();
  760. while (E) {
  761. struct wl_seat *wl_seat = E->get();
  762. List<struct wl_seat *>::Element *N = E->next();
  763. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  764. ERR_FAIL_NULL(ss);
  765. if (ss->wl_seat_name == name) {
  766. if (wl_seat) {
  767. wl_seat_destroy(wl_seat);
  768. }
  769. if (ss->wl_data_device) {
  770. wl_data_device_destroy(ss->wl_data_device);
  771. }
  772. if (ss->wp_tablet_seat) {
  773. zwp_tablet_seat_v2_destroy(ss->wp_tablet_seat);
  774. for (struct zwp_tablet_tool_v2 *tool : ss->tablet_tools) {
  775. TabletToolState *state = wp_tablet_tool_get_state(tool);
  776. if (state) {
  777. memdelete(state);
  778. }
  779. zwp_tablet_tool_v2_destroy(tool);
  780. }
  781. }
  782. memdelete(ss);
  783. registry->wl_seats.erase(E);
  784. return;
  785. }
  786. E = N;
  787. }
  788. }
  789. {
  790. // Iterate through all of the outputs to find if any got removed.
  791. // FIXME: This is a very bruteforce approach.
  792. List<struct wl_output *>::Element *it = registry->wl_outputs.front();
  793. while (it) {
  794. // Iterate through all of the screens to find if any got removed.
  795. struct wl_output *wl_output = it->get();
  796. ERR_FAIL_NULL(wl_output);
  797. ScreenState *ss = wl_output_get_screen_state(wl_output);
  798. if (ss->wl_output_name == name) {
  799. registry->wl_outputs.erase(it);
  800. memdelete(ss);
  801. wl_output_destroy(wl_output);
  802. return;
  803. }
  804. it = it->next();
  805. }
  806. }
  807. }
  808. void WaylandThread::_wl_surface_on_enter(void *data, struct wl_surface *wl_surface, struct wl_output *wl_output) {
  809. if (!wl_output || !wl_proxy_is_godot((struct wl_proxy *)wl_output)) {
  810. // This won't have the right data bound to it. Not worth it and would probably
  811. // just break everything.
  812. return;
  813. }
  814. WindowState *ws = (WindowState *)data;
  815. ERR_FAIL_NULL(ws);
  816. DEBUG_LOG_WAYLAND_THREAD(vformat("Window entered output %x.\n", (size_t)wl_output));
  817. ws->wl_outputs.insert(wl_output);
  818. // Workaround for buffer scaling as there's no guaranteed way of knowing the
  819. // preferred scale.
  820. // TODO: Skip this branch for newer `wl_surface`s once we add support for
  821. // `wl_surface::preferred_buffer_scale`
  822. if (ws->preferred_fractional_scale == 0) {
  823. window_state_update_size(ws, ws->rect.size.width, ws->rect.size.height);
  824. }
  825. }
  826. void WaylandThread::_frame_wl_callback_on_done(void *data, struct wl_callback *wl_callback, uint32_t callback_data) {
  827. wl_callback_destroy(wl_callback);
  828. WindowState *ws = (WindowState *)data;
  829. ERR_FAIL_NULL(ws);
  830. ERR_FAIL_NULL(ws->wayland_thread);
  831. ERR_FAIL_NULL(ws->wl_surface);
  832. ws->last_frame_time = OS::get_singleton()->get_ticks_usec();
  833. ws->wayland_thread->set_frame();
  834. ws->frame_callback = wl_surface_frame(ws->wl_surface);
  835. wl_callback_add_listener(ws->frame_callback, &frame_wl_callback_listener, ws);
  836. if (ws->wl_surface && ws->buffer_scale_changed) {
  837. // NOTE: We're only now setting the buffer scale as the idea is to get this
  838. // data committed together with the new frame, all by the rendering driver.
  839. // This is important because we might otherwise set an invalid combination of
  840. // buffer size and scale (e.g. odd size and 2x scale). We're pretty much
  841. // guaranteed to get a proper buffer in the next render loop as the rescaling
  842. // method also informs the engine of a "window rect change", triggering
  843. // rendering if needed.
  844. wl_surface_set_buffer_scale(ws->wl_surface, window_state_get_preferred_buffer_scale(ws));
  845. }
  846. }
  847. void WaylandThread::_wl_surface_on_leave(void *data, struct wl_surface *wl_surface, struct wl_output *wl_output) {
  848. if (!wl_output || !wl_proxy_is_godot((struct wl_proxy *)wl_output)) {
  849. // This won't have the right data bound to it. Not worth it and would probably
  850. // just break everything.
  851. return;
  852. }
  853. WindowState *ws = (WindowState *)data;
  854. ERR_FAIL_NULL(ws);
  855. ws->wl_outputs.erase(wl_output);
  856. DEBUG_LOG_WAYLAND_THREAD(vformat("Window left output %x.\n", (size_t)wl_output));
  857. }
  858. // TODO: Add support to this event.
  859. void WaylandThread::_wl_surface_on_preferred_buffer_scale(void *data, struct wl_surface *wl_surface, int32_t factor) {
  860. }
  861. // TODO: Add support to this event.
  862. void WaylandThread::_wl_surface_on_preferred_buffer_transform(void *data, struct wl_surface *wl_surface, uint32_t transform) {
  863. }
  864. 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) {
  865. ScreenState *ss = (ScreenState *)data;
  866. ERR_FAIL_NULL(ss);
  867. ss->pending_data.position.x = x;
  868. ss->pending_data.position.x = x;
  869. ss->pending_data.position.y = y;
  870. ss->pending_data.physical_size.width = physical_width;
  871. ss->pending_data.physical_size.height = physical_height;
  872. ss->pending_data.make.clear();
  873. ss->pending_data.make.append_utf8(make);
  874. ss->pending_data.model.clear();
  875. ss->pending_data.model.append_utf8(model);
  876. // `wl_output::done` is a version 2 addition. We'll directly update the data
  877. // for compatibility.
  878. if (wl_output_get_version(wl_output) == 1) {
  879. ss->data = ss->pending_data;
  880. }
  881. }
  882. 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) {
  883. ScreenState *ss = (ScreenState *)data;
  884. ERR_FAIL_NULL(ss);
  885. ss->pending_data.size.width = width;
  886. ss->pending_data.size.height = height;
  887. ss->pending_data.refresh_rate = refresh ? refresh / 1000.0f : -1;
  888. // `wl_output::done` is a version 2 addition. We'll directly update the data
  889. // for compatibility.
  890. if (wl_output_get_version(wl_output) == 1) {
  891. ss->data = ss->pending_data;
  892. }
  893. }
  894. // NOTE: The following `wl_output` events are only for version 2 onwards, so we
  895. // can assume that they're "atomic" (i.e. rely on the `wl_output::done` event).
  896. void WaylandThread::_wl_output_on_done(void *data, struct wl_output *wl_output) {
  897. ScreenState *ss = (ScreenState *)data;
  898. ERR_FAIL_NULL(ss);
  899. ss->data = ss->pending_data;
  900. ss->wayland_thread->_update_scale(ss->data.scale);
  901. DEBUG_LOG_WAYLAND_THREAD(vformat("Output %x done.", (size_t)wl_output));
  902. }
  903. void WaylandThread::_wl_output_on_scale(void *data, struct wl_output *wl_output, int32_t factor) {
  904. ScreenState *ss = (ScreenState *)data;
  905. ERR_FAIL_NULL(ss);
  906. ss->pending_data.scale = factor;
  907. DEBUG_LOG_WAYLAND_THREAD(vformat("Output %x scale %d", (size_t)wl_output, factor));
  908. }
  909. void WaylandThread::_wl_output_on_name(void *data, struct wl_output *wl_output, const char *name) {
  910. }
  911. void WaylandThread::_wl_output_on_description(void *data, struct wl_output *wl_output, const char *description) {
  912. }
  913. void WaylandThread::_xdg_wm_base_on_ping(void *data, struct xdg_wm_base *xdg_wm_base, uint32_t serial) {
  914. xdg_wm_base_pong(xdg_wm_base, serial);
  915. }
  916. void WaylandThread::_xdg_surface_on_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial) {
  917. xdg_surface_ack_configure(xdg_surface, serial);
  918. WindowState *ws = (WindowState *)data;
  919. ERR_FAIL_NULL(ws);
  920. DEBUG_LOG_WAYLAND_THREAD(vformat("xdg surface on configure rect %s", ws->rect));
  921. }
  922. void WaylandThread::_xdg_toplevel_on_configure(void *data, struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height, struct wl_array *states) {
  923. WindowState *ws = (WindowState *)data;
  924. ERR_FAIL_NULL(ws);
  925. // Expect the window to be in a plain state. It will get properly set if the
  926. // compositor reports otherwise below.
  927. ws->mode = DisplayServer::WINDOW_MODE_WINDOWED;
  928. ws->suspended = false;
  929. uint32_t *state = nullptr;
  930. wl_array_for_each(state, states) {
  931. switch (*state) {
  932. case XDG_TOPLEVEL_STATE_MAXIMIZED: {
  933. ws->mode = DisplayServer::WINDOW_MODE_MAXIMIZED;
  934. } break;
  935. case XDG_TOPLEVEL_STATE_FULLSCREEN: {
  936. ws->mode = DisplayServer::WINDOW_MODE_FULLSCREEN;
  937. } break;
  938. case XDG_TOPLEVEL_STATE_SUSPENDED: {
  939. ws->suspended = true;
  940. } break;
  941. default: {
  942. // We don't care about the other states (for now).
  943. } break;
  944. }
  945. }
  946. if (width != 0 && height != 0) {
  947. window_state_update_size(ws, width, height);
  948. }
  949. DEBUG_LOG_WAYLAND_THREAD(vformat("XDG toplevel on configure width %d height %d.", width, height));
  950. }
  951. void WaylandThread::_xdg_toplevel_on_close(void *data, struct xdg_toplevel *xdg_toplevel) {
  952. WindowState *ws = (WindowState *)data;
  953. ERR_FAIL_NULL(ws);
  954. Ref<WindowEventMessage> msg;
  955. msg.instantiate();
  956. msg->id = ws->id;
  957. msg->event = DisplayServer::WINDOW_EVENT_CLOSE_REQUEST;
  958. ws->wayland_thread->push_message(msg);
  959. }
  960. void WaylandThread::_xdg_toplevel_on_configure_bounds(void *data, struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height) {
  961. }
  962. void WaylandThread::_xdg_toplevel_on_wm_capabilities(void *data, struct xdg_toplevel *xdg_toplevel, struct wl_array *capabilities) {
  963. WindowState *ws = (WindowState *)data;
  964. ERR_FAIL_NULL(ws);
  965. ws->can_maximize = false;
  966. ws->can_fullscreen = false;
  967. ws->can_minimize = false;
  968. uint32_t *capability = nullptr;
  969. wl_array_for_each(capability, capabilities) {
  970. switch (*capability) {
  971. case XDG_TOPLEVEL_WM_CAPABILITIES_MAXIMIZE: {
  972. ws->can_maximize = true;
  973. } break;
  974. case XDG_TOPLEVEL_WM_CAPABILITIES_FULLSCREEN: {
  975. ws->can_fullscreen = true;
  976. } break;
  977. case XDG_TOPLEVEL_WM_CAPABILITIES_MINIMIZE: {
  978. ws->can_minimize = true;
  979. } break;
  980. default: {
  981. } break;
  982. }
  983. }
  984. }
  985. void WaylandThread::_xdg_popup_on_configure(void *data, struct xdg_popup *xdg_popup, int32_t x, int32_t y, int32_t width, int32_t height) {
  986. WindowState *ws = (WindowState *)data;
  987. ERR_FAIL_NULL(ws);
  988. if (width != 0 && height != 0) {
  989. window_state_update_size(ws, width, height);
  990. }
  991. WindowState *parent = ws->wayland_thread->window_get_state(ws->parent_id);
  992. ERR_FAIL_NULL(parent);
  993. Point2i pos = Point2i(x, y);
  994. if (parent->libdecor_frame) {
  995. int translated_x = x;
  996. int translated_y = y;
  997. libdecor_frame_translate_coordinate(parent->libdecor_frame, x, y, &translated_x, &translated_y);
  998. pos.x = translated_x;
  999. pos.y = translated_y;
  1000. }
  1001. // Looks like the position returned here is relative to the parent. We have to
  1002. // accumulate it or there's gonna be a lot of confusion godot-side.
  1003. pos += parent->rect.position;
  1004. if (ws->rect.position != pos) {
  1005. DEBUG_LOG_WAYLAND_THREAD(vformat("Repositioning popup %d from %s to %s", ws->id, ws->rect.position, pos));
  1006. double parent_scale = window_state_get_scale_factor(parent);
  1007. ws->rect.position = pos;
  1008. Ref<WindowRectMessage> rect_msg;
  1009. rect_msg.instantiate();
  1010. rect_msg->id = ws->id;
  1011. rect_msg->rect.position = scale_vector2i(ws->rect.position, parent_scale);
  1012. rect_msg->rect.size = scale_vector2i(ws->rect.size, parent_scale);
  1013. ws->wayland_thread->push_message(rect_msg);
  1014. }
  1015. DEBUG_LOG_WAYLAND_THREAD(vformat("xdg popup on configure x%d y%d w%d h%d", x, y, width, height));
  1016. }
  1017. void WaylandThread::_xdg_popup_on_popup_done(void *data, struct xdg_popup *xdg_popup) {
  1018. WindowState *ws = (WindowState *)data;
  1019. ERR_FAIL_NULL(ws);
  1020. Ref<WindowEventMessage> ev_msg;
  1021. ev_msg.instantiate();
  1022. ev_msg->id = ws->id;
  1023. ev_msg->event = DisplayServer::WINDOW_EVENT_FORCE_CLOSE;
  1024. ws->wayland_thread->push_message(ev_msg);
  1025. }
  1026. void WaylandThread::_xdg_popup_on_repositioned(void *data, struct xdg_popup *xdg_popup, uint32_t token) {
  1027. DEBUG_LOG_WAYLAND_THREAD(vformat("stub xdg popup repositioned %x", token));
  1028. }
  1029. // NOTE: Deprecated.
  1030. void WaylandThread::_xdg_exported_v1_on_handle(void *data, zxdg_exported_v1 *exported, const char *handle) {
  1031. WindowState *ws = (WindowState *)data;
  1032. ERR_FAIL_NULL(ws);
  1033. ws->exported_handle = vformat("wayland:%s", String::utf8(handle));
  1034. }
  1035. void WaylandThread::_xdg_exported_v2_on_handle(void *data, zxdg_exported_v2 *exported, const char *handle) {
  1036. WindowState *ws = (WindowState *)data;
  1037. ERR_FAIL_NULL(ws);
  1038. ws->exported_handle = vformat("wayland:%s", String::utf8(handle));
  1039. }
  1040. void WaylandThread::_xdg_toplevel_decoration_on_configure(void *data, struct zxdg_toplevel_decoration_v1 *xdg_toplevel_decoration, uint32_t mode) {
  1041. if (mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE) {
  1042. #ifdef LIBDECOR_ENABLED
  1043. WARN_PRINT_ONCE("Native client side decorations are not yet supported without libdecor!");
  1044. #else
  1045. WARN_PRINT_ONCE("Native client side decorations are not yet supported!");
  1046. #endif // LIBDECOR_ENABLED
  1047. }
  1048. }
  1049. #ifdef LIBDECOR_ENABLED
  1050. void WaylandThread::libdecor_on_error(struct libdecor *context, enum libdecor_error error, const char *message) {
  1051. ERR_PRINT(vformat("libdecor error %d: %s", error, message));
  1052. }
  1053. // NOTE: This is pretty much a reimplementation of _xdg_surface_on_configure
  1054. // and _xdg_toplevel_on_configure. Libdecor really likes wrapping everything,
  1055. // forcing us to do stuff like this.
  1056. void WaylandThread::libdecor_frame_on_configure(struct libdecor_frame *frame, struct libdecor_configuration *configuration, void *user_data) {
  1057. WindowState *ws = (WindowState *)user_data;
  1058. ERR_FAIL_NULL(ws);
  1059. int width = 0;
  1060. int height = 0;
  1061. ws->pending_libdecor_configuration = configuration;
  1062. if (!libdecor_configuration_get_content_size(configuration, frame, &width, &height)) {
  1063. // The configuration doesn't have a size. We'll use the one already set in the window.
  1064. width = ws->rect.size.width;
  1065. height = ws->rect.size.height;
  1066. }
  1067. ERR_FAIL_COND_MSG(width == 0 || height == 0, "Window has invalid size.");
  1068. libdecor_window_state window_state = LIBDECOR_WINDOW_STATE_NONE;
  1069. // Expect the window to be in a plain state. It will get properly set if the
  1070. // compositor reports otherwise below.
  1071. ws->mode = DisplayServer::WINDOW_MODE_WINDOWED;
  1072. ws->suspended = false;
  1073. if (libdecor_configuration_get_window_state(configuration, &window_state)) {
  1074. if (window_state & LIBDECOR_WINDOW_STATE_MAXIMIZED) {
  1075. ws->mode = DisplayServer::WINDOW_MODE_MAXIMIZED;
  1076. }
  1077. if (window_state & LIBDECOR_WINDOW_STATE_FULLSCREEN) {
  1078. ws->mode = DisplayServer::WINDOW_MODE_FULLSCREEN;
  1079. }
  1080. if (window_state & LIBDECOR_WINDOW_STATE_SUSPENDED) {
  1081. ws->suspended = true;
  1082. }
  1083. }
  1084. window_state_update_size(ws, width, height);
  1085. DEBUG_LOG_WAYLAND_THREAD(vformat("libdecor frame on configure rect %s", ws->rect));
  1086. }
  1087. void WaylandThread::libdecor_frame_on_close(struct libdecor_frame *frame, void *user_data) {
  1088. WindowState *ws = (WindowState *)user_data;
  1089. ERR_FAIL_NULL(ws);
  1090. Ref<WindowEventMessage> winevent_msg;
  1091. winevent_msg.instantiate();
  1092. winevent_msg->id = ws->id;
  1093. winevent_msg->event = DisplayServer::WINDOW_EVENT_CLOSE_REQUEST;
  1094. ws->wayland_thread->push_message(winevent_msg);
  1095. DEBUG_LOG_WAYLAND_THREAD("libdecor frame on close");
  1096. }
  1097. void WaylandThread::libdecor_frame_on_commit(struct libdecor_frame *frame, void *user_data) {
  1098. // We're skipping this as we don't really care about libdecor's commit for
  1099. // atomicity reasons. See `_frame_wl_callback_on_done` for more info.
  1100. DEBUG_LOG_WAYLAND_THREAD("libdecor frame on commit");
  1101. }
  1102. void WaylandThread::libdecor_frame_on_dismiss_popup(struct libdecor_frame *frame, const char *seat_name, void *user_data) {
  1103. }
  1104. #endif // LIBDECOR_ENABLED
  1105. void WaylandThread::_wl_seat_on_capabilities(void *data, struct wl_seat *wl_seat, uint32_t capabilities) {
  1106. SeatState *ss = (SeatState *)data;
  1107. ERR_FAIL_NULL(ss);
  1108. // TODO: Handle touch.
  1109. // Pointer handling.
  1110. if (capabilities & WL_SEAT_CAPABILITY_POINTER) {
  1111. if (!ss->wl_pointer) {
  1112. ss->cursor_surface = wl_compositor_create_surface(ss->registry->wl_compositor);
  1113. wl_surface_commit(ss->cursor_surface);
  1114. ss->wl_pointer = wl_seat_get_pointer(wl_seat);
  1115. wl_pointer_add_listener(ss->wl_pointer, &wl_pointer_listener, ss);
  1116. if (ss->registry->wp_relative_pointer_manager) {
  1117. ss->wp_relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(ss->registry->wp_relative_pointer_manager, ss->wl_pointer);
  1118. zwp_relative_pointer_v1_add_listener(ss->wp_relative_pointer, &wp_relative_pointer_listener, ss);
  1119. }
  1120. if (ss->registry->wp_pointer_gestures) {
  1121. ss->wp_pointer_gesture_pinch = zwp_pointer_gestures_v1_get_pinch_gesture(ss->registry->wp_pointer_gestures, ss->wl_pointer);
  1122. zwp_pointer_gesture_pinch_v1_add_listener(ss->wp_pointer_gesture_pinch, &wp_pointer_gesture_pinch_listener, ss);
  1123. }
  1124. // TODO: Constrain new pointers if the global mouse mode is constrained.
  1125. }
  1126. } else {
  1127. if (ss->cursor_frame_callback) {
  1128. // Just in case. I got bitten by weird race-like conditions already.
  1129. wl_callback_set_user_data(ss->cursor_frame_callback, nullptr);
  1130. wl_callback_destroy(ss->cursor_frame_callback);
  1131. ss->cursor_frame_callback = nullptr;
  1132. }
  1133. if (ss->cursor_surface) {
  1134. wl_surface_destroy(ss->cursor_surface);
  1135. ss->cursor_surface = nullptr;
  1136. }
  1137. if (ss->wl_pointer) {
  1138. wl_pointer_destroy(ss->wl_pointer);
  1139. ss->wl_pointer = nullptr;
  1140. }
  1141. if (ss->wp_relative_pointer) {
  1142. zwp_relative_pointer_v1_destroy(ss->wp_relative_pointer);
  1143. ss->wp_relative_pointer = nullptr;
  1144. }
  1145. if (ss->wp_confined_pointer) {
  1146. zwp_confined_pointer_v1_destroy(ss->wp_confined_pointer);
  1147. ss->wp_confined_pointer = nullptr;
  1148. }
  1149. if (ss->wp_locked_pointer) {
  1150. zwp_locked_pointer_v1_destroy(ss->wp_locked_pointer);
  1151. ss->wp_locked_pointer = nullptr;
  1152. }
  1153. }
  1154. // Keyboard handling.
  1155. if (capabilities & WL_SEAT_CAPABILITY_KEYBOARD) {
  1156. if (!ss->wl_keyboard) {
  1157. ss->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
  1158. ERR_FAIL_NULL(ss->xkb_context);
  1159. ss->wl_keyboard = wl_seat_get_keyboard(wl_seat);
  1160. wl_keyboard_add_listener(ss->wl_keyboard, &wl_keyboard_listener, ss);
  1161. }
  1162. } else {
  1163. if (ss->xkb_context) {
  1164. xkb_context_unref(ss->xkb_context);
  1165. ss->xkb_context = nullptr;
  1166. }
  1167. if (ss->wl_keyboard) {
  1168. wl_keyboard_destroy(ss->wl_keyboard);
  1169. ss->wl_keyboard = nullptr;
  1170. }
  1171. }
  1172. }
  1173. void WaylandThread::_wl_seat_on_name(void *data, struct wl_seat *wl_seat, const char *name) {
  1174. }
  1175. void WaylandThread::_cursor_frame_callback_on_done(void *data, struct wl_callback *wl_callback, uint32_t time_ms) {
  1176. wl_callback_destroy(wl_callback);
  1177. SeatState *ss = (SeatState *)data;
  1178. ERR_FAIL_NULL(ss);
  1179. ss->cursor_frame_callback = nullptr;
  1180. ss->cursor_time_ms = time_ms;
  1181. seat_state_update_cursor(ss);
  1182. }
  1183. 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) {
  1184. WindowState *ws = wl_surface_get_window_state(surface);
  1185. if (!ws) {
  1186. return;
  1187. }
  1188. SeatState *ss = (SeatState *)data;
  1189. ERR_FAIL_NULL(ss);
  1190. ERR_FAIL_NULL(ss->cursor_surface);
  1191. PointerData &pd = ss->pointer_data_buffer;
  1192. ss->pointer_enter_serial = serial;
  1193. pd.pointed_id = ws->id;
  1194. pd.last_pointed_id = ws->id;
  1195. pd.position.x = wl_fixed_to_double(surface_x);
  1196. pd.position.y = wl_fixed_to_double(surface_y);
  1197. seat_state_update_cursor(ss);
  1198. DEBUG_LOG_WAYLAND_THREAD(vformat("Pointer entered window %d.", ws->id));
  1199. if (wl_pointer_get_version(wl_pointer) < WL_POINTER_FRAME_SINCE_VERSION) {
  1200. _wl_pointer_on_frame(data, wl_pointer);
  1201. }
  1202. }
  1203. void WaylandThread::_wl_pointer_on_leave(void *data, struct wl_pointer *wl_pointer, uint32_t serial, struct wl_surface *surface) {
  1204. // NOTE: `surface` will probably be null when the surface is destroyed.
  1205. // See: https://gitlab.freedesktop.org/wayland/wayland/-/issues/366
  1206. // See: https://gitlab.freedesktop.org/wayland/wayland/-/issues/465
  1207. SeatState *ss = (SeatState *)data;
  1208. ERR_FAIL_NULL(ss);
  1209. PointerData &pd = ss->pointer_data_buffer;
  1210. if (pd.pointed_id == DisplayServer::INVALID_WINDOW_ID) {
  1211. // We're probably on a decoration or some other third-party thing.
  1212. return;
  1213. }
  1214. DisplayServer::WindowID id = pd.pointed_id;
  1215. pd.pointed_id = DisplayServer::INVALID_WINDOW_ID;
  1216. pd.pressed_button_mask.clear();
  1217. DEBUG_LOG_WAYLAND_THREAD(vformat("Pointer left window %d.", id));
  1218. if (wl_pointer_get_version(wl_pointer) < WL_POINTER_FRAME_SINCE_VERSION) {
  1219. _wl_pointer_on_frame(data, wl_pointer);
  1220. }
  1221. }
  1222. 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) {
  1223. SeatState *ss = (SeatState *)data;
  1224. ERR_FAIL_NULL(ss);
  1225. PointerData &pd = ss->pointer_data_buffer;
  1226. pd.position.x = wl_fixed_to_double(surface_x);
  1227. pd.position.y = wl_fixed_to_double(surface_y);
  1228. pd.motion_time = time;
  1229. if (wl_pointer_get_version(wl_pointer) < WL_POINTER_FRAME_SINCE_VERSION) {
  1230. _wl_pointer_on_frame(data, wl_pointer);
  1231. }
  1232. }
  1233. 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) {
  1234. SeatState *ss = (SeatState *)data;
  1235. ERR_FAIL_NULL(ss);
  1236. PointerData &pd = ss->pointer_data_buffer;
  1237. MouseButton button_pressed = MouseButton::NONE;
  1238. switch (button) {
  1239. case BTN_LEFT:
  1240. button_pressed = MouseButton::LEFT;
  1241. break;
  1242. case BTN_MIDDLE:
  1243. button_pressed = MouseButton::MIDDLE;
  1244. break;
  1245. case BTN_RIGHT:
  1246. button_pressed = MouseButton::RIGHT;
  1247. break;
  1248. case BTN_EXTRA:
  1249. button_pressed = MouseButton::MB_XBUTTON1;
  1250. break;
  1251. case BTN_SIDE:
  1252. button_pressed = MouseButton::MB_XBUTTON2;
  1253. break;
  1254. default: {
  1255. }
  1256. }
  1257. MouseButtonMask mask = mouse_button_to_mask(button_pressed);
  1258. if (state & WL_POINTER_BUTTON_STATE_PRESSED) {
  1259. pd.pressed_button_mask.set_flag(mask);
  1260. pd.last_button_pressed = button_pressed;
  1261. pd.double_click_begun = true;
  1262. } else {
  1263. pd.pressed_button_mask.clear_flag(mask);
  1264. }
  1265. pd.button_time = time;
  1266. pd.button_serial = serial;
  1267. if (wl_pointer_get_version(wl_pointer) < WL_POINTER_FRAME_SINCE_VERSION) {
  1268. _wl_pointer_on_frame(data, wl_pointer);
  1269. }
  1270. }
  1271. void WaylandThread::_wl_pointer_on_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {
  1272. SeatState *ss = (SeatState *)data;
  1273. ERR_FAIL_NULL(ss);
  1274. PointerData &pd = ss->pointer_data_buffer;
  1275. switch (axis) {
  1276. case WL_POINTER_AXIS_VERTICAL_SCROLL: {
  1277. pd.scroll_vector.y = wl_fixed_to_double(value);
  1278. } break;
  1279. case WL_POINTER_AXIS_HORIZONTAL_SCROLL: {
  1280. pd.scroll_vector.x = wl_fixed_to_double(value);
  1281. } break;
  1282. }
  1283. pd.button_time = time;
  1284. if (wl_pointer_get_version(wl_pointer) < WL_POINTER_FRAME_SINCE_VERSION) {
  1285. _wl_pointer_on_frame(data, wl_pointer);
  1286. }
  1287. }
  1288. void WaylandThread::_wl_pointer_on_frame(void *data, struct wl_pointer *wl_pointer) {
  1289. SeatState *ss = (SeatState *)data;
  1290. ERR_FAIL_NULL(ss);
  1291. WaylandThread *wayland_thread = ss->wayland_thread;
  1292. ERR_FAIL_NULL(wayland_thread);
  1293. PointerData &old_pd = ss->pointer_data;
  1294. PointerData &pd = ss->pointer_data_buffer;
  1295. if (pd.pointed_id != old_pd.pointed_id) {
  1296. if (old_pd.pointed_id != DisplayServer::INVALID_WINDOW_ID) {
  1297. Ref<WindowEventMessage> msg;
  1298. msg.instantiate();
  1299. msg->id = old_pd.pointed_id;
  1300. msg->event = DisplayServer::WINDOW_EVENT_MOUSE_EXIT;
  1301. wayland_thread->push_message(msg);
  1302. }
  1303. if (pd.pointed_id != DisplayServer::INVALID_WINDOW_ID) {
  1304. Ref<WindowEventMessage> msg;
  1305. msg.instantiate();
  1306. msg->id = pd.pointed_id;
  1307. msg->event = DisplayServer::WINDOW_EVENT_MOUSE_ENTER;
  1308. wayland_thread->push_message(msg);
  1309. }
  1310. }
  1311. if (pd.pointed_id == DisplayServer::INVALID_WINDOW_ID) {
  1312. // We're probably on a decoration or some other third-party thing. Let's
  1313. // "commit" the data and call it a day.
  1314. old_pd = pd;
  1315. return;
  1316. }
  1317. WindowState *ws = ss->wayland_thread->window_get_state(pd.pointed_id);
  1318. ERR_FAIL_NULL(ws);
  1319. double scale = window_state_get_scale_factor(ws);
  1320. wayland_thread->_set_current_seat(ss->wl_seat);
  1321. if (old_pd.motion_time != pd.motion_time || old_pd.relative_motion_time != pd.relative_motion_time) {
  1322. Ref<InputEventMouseMotion> mm;
  1323. mm.instantiate();
  1324. // Set all pressed modifiers.
  1325. mm->set_shift_pressed(ss->shift_pressed);
  1326. mm->set_ctrl_pressed(ss->ctrl_pressed);
  1327. mm->set_alt_pressed(ss->alt_pressed);
  1328. mm->set_meta_pressed(ss->meta_pressed);
  1329. mm->set_window_id(ws->id);
  1330. mm->set_button_mask(pd.pressed_button_mask);
  1331. mm->set_position(pd.position * scale);
  1332. mm->set_global_position(pd.position * scale);
  1333. Vector2 pos_delta = (pd.position - old_pd.position) * scale;
  1334. if (old_pd.relative_motion_time != pd.relative_motion_time) {
  1335. uint32_t time_delta = pd.relative_motion_time - old_pd.relative_motion_time;
  1336. mm->set_relative(pd.relative_motion * scale);
  1337. mm->set_velocity((Vector2)pos_delta / time_delta);
  1338. } else {
  1339. // The spec includes the possibility of having motion events without an
  1340. // associated relative motion event. If that's the case, fallback to a
  1341. // simple delta of the position. The captured mouse won't report the
  1342. // relative speed anymore though.
  1343. uint32_t time_delta = pd.motion_time - old_pd.motion_time;
  1344. mm->set_relative(pos_delta);
  1345. mm->set_velocity((Vector2)pos_delta / time_delta);
  1346. }
  1347. mm->set_relative_screen_position(mm->get_relative());
  1348. mm->set_screen_velocity(mm->get_velocity());
  1349. Ref<InputEventMessage> msg;
  1350. msg.instantiate();
  1351. msg->event = mm;
  1352. wayland_thread->push_message(msg);
  1353. }
  1354. if (pd.discrete_scroll_vector_120 - old_pd.discrete_scroll_vector_120 != Vector2i()) {
  1355. // This is a discrete scroll (eg. from a scroll wheel), so we'll just emit
  1356. // scroll wheel buttons.
  1357. if (pd.scroll_vector.y != 0) {
  1358. MouseButton button = pd.scroll_vector.y > 0 ? MouseButton::WHEEL_DOWN : MouseButton::WHEEL_UP;
  1359. pd.pressed_button_mask.set_flag(mouse_button_to_mask(button));
  1360. }
  1361. if (pd.scroll_vector.x != 0) {
  1362. MouseButton button = pd.scroll_vector.x > 0 ? MouseButton::WHEEL_RIGHT : MouseButton::WHEEL_LEFT;
  1363. pd.pressed_button_mask.set_flag(mouse_button_to_mask(button));
  1364. }
  1365. } else {
  1366. if (pd.scroll_vector - old_pd.scroll_vector != Vector2()) {
  1367. // This is a continuous scroll, so we'll emit a pan gesture.
  1368. Ref<InputEventPanGesture> pg;
  1369. pg.instantiate();
  1370. // Set all pressed modifiers.
  1371. pg->set_shift_pressed(ss->shift_pressed);
  1372. pg->set_ctrl_pressed(ss->ctrl_pressed);
  1373. pg->set_alt_pressed(ss->alt_pressed);
  1374. pg->set_meta_pressed(ss->meta_pressed);
  1375. pg->set_position(pd.position * scale);
  1376. pg->set_window_id(ws->id);
  1377. pg->set_delta(pd.scroll_vector);
  1378. Ref<InputEventMessage> msg;
  1379. msg.instantiate();
  1380. msg->event = pg;
  1381. wayland_thread->push_message(msg);
  1382. }
  1383. }
  1384. if (old_pd.pressed_button_mask != pd.pressed_button_mask) {
  1385. BitField<MouseButtonMask> pressed_mask_delta = old_pd.pressed_button_mask.get_different(pd.pressed_button_mask);
  1386. const MouseButton buttons_to_test[] = {
  1387. MouseButton::LEFT,
  1388. MouseButton::MIDDLE,
  1389. MouseButton::RIGHT,
  1390. MouseButton::WHEEL_UP,
  1391. MouseButton::WHEEL_DOWN,
  1392. MouseButton::WHEEL_LEFT,
  1393. MouseButton::WHEEL_RIGHT,
  1394. MouseButton::MB_XBUTTON1,
  1395. MouseButton::MB_XBUTTON2,
  1396. };
  1397. for (MouseButton test_button : buttons_to_test) {
  1398. MouseButtonMask test_button_mask = mouse_button_to_mask(test_button);
  1399. if (pressed_mask_delta.has_flag(test_button_mask)) {
  1400. Ref<InputEventMouseButton> mb;
  1401. mb.instantiate();
  1402. // Set all pressed modifiers.
  1403. mb->set_shift_pressed(ss->shift_pressed);
  1404. mb->set_ctrl_pressed(ss->ctrl_pressed);
  1405. mb->set_alt_pressed(ss->alt_pressed);
  1406. mb->set_meta_pressed(ss->meta_pressed);
  1407. mb->set_window_id(ws->id);
  1408. mb->set_position(pd.position * scale);
  1409. mb->set_global_position(pd.position * scale);
  1410. if (test_button == MouseButton::WHEEL_UP || test_button == MouseButton::WHEEL_DOWN) {
  1411. // If this is a discrete scroll, specify how many "clicks" it did for this
  1412. // pointer frame.
  1413. mb->set_factor(Math::abs(pd.discrete_scroll_vector_120.y / (float)120));
  1414. }
  1415. if (test_button == MouseButton::WHEEL_RIGHT || test_button == MouseButton::WHEEL_LEFT) {
  1416. // If this is a discrete scroll, specify how many "clicks" it did for this
  1417. // pointer frame.
  1418. mb->set_factor(fabs(pd.discrete_scroll_vector_120.x / (float)120));
  1419. }
  1420. mb->set_button_mask(pd.pressed_button_mask);
  1421. mb->set_button_index(test_button);
  1422. mb->set_pressed(pd.pressed_button_mask.has_flag(test_button_mask));
  1423. // We have to set the last position pressed here as we can't take for
  1424. // granted what the individual events might have seen due to them not having
  1425. // a guaranteed order.
  1426. if (mb->is_pressed()) {
  1427. pd.last_pressed_position = pd.position;
  1428. }
  1429. 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 * scale).distance_to(Vector2(pd.last_pressed_position * scale)) < 5) {
  1430. pd.double_click_begun = false;
  1431. mb->set_double_click(true);
  1432. }
  1433. Ref<InputEventMessage> msg;
  1434. msg.instantiate();
  1435. msg->event = mb;
  1436. wayland_thread->push_message(msg);
  1437. // Send an event resetting immediately the wheel key.
  1438. // Wayland specification defines axis_stop events as optional and says to
  1439. // treat all axis events as unterminated. As such, we have to manually do
  1440. // it ourselves.
  1441. if (test_button == MouseButton::WHEEL_UP || test_button == MouseButton::WHEEL_DOWN || test_button == MouseButton::WHEEL_LEFT || test_button == MouseButton::WHEEL_RIGHT) {
  1442. // FIXME: This is ugly, I can't find a clean way to clone an InputEvent.
  1443. // This works for now, despite being horrible.
  1444. Ref<InputEventMouseButton> wh_up;
  1445. wh_up.instantiate();
  1446. wh_up->set_window_id(ws->id);
  1447. wh_up->set_position(pd.position * scale);
  1448. wh_up->set_global_position(pd.position * scale);
  1449. // We have to unset the button to avoid it getting stuck.
  1450. pd.pressed_button_mask.clear_flag(test_button_mask);
  1451. wh_up->set_button_mask(pd.pressed_button_mask);
  1452. wh_up->set_button_index(test_button);
  1453. wh_up->set_pressed(false);
  1454. Ref<InputEventMessage> msg_up;
  1455. msg_up.instantiate();
  1456. msg_up->event = wh_up;
  1457. wayland_thread->push_message(msg_up);
  1458. }
  1459. }
  1460. }
  1461. }
  1462. // Reset the scroll vectors as we already handled them.
  1463. pd.scroll_vector = Vector2();
  1464. pd.discrete_scroll_vector_120 = Vector2i();
  1465. // Update the data all getters read. Wayland's specification requires us to do
  1466. // this, since all pointer actions are sent in individual events.
  1467. old_pd = pd;
  1468. }
  1469. void WaylandThread::_wl_pointer_on_axis_source(void *data, struct wl_pointer *wl_pointer, uint32_t axis_source) {
  1470. SeatState *ss = (SeatState *)data;
  1471. ERR_FAIL_NULL(ss);
  1472. ss->pointer_data_buffer.scroll_type = axis_source;
  1473. }
  1474. void WaylandThread::_wl_pointer_on_axis_stop(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis) {
  1475. }
  1476. // NOTE: This event is deprecated since version 8 and superseded by
  1477. // `wl_pointer::axis_value120`. This thus converts the data to its
  1478. // fraction-of-120 format.
  1479. void WaylandThread::_wl_pointer_on_axis_discrete(void *data, struct wl_pointer *wl_pointer, uint32_t axis, int32_t discrete) {
  1480. SeatState *ss = (SeatState *)data;
  1481. ERR_FAIL_NULL(ss);
  1482. PointerData &pd = ss->pointer_data_buffer;
  1483. // NOTE: We can allow ourselves to not accumulate this data (and thus just
  1484. // assign it) as the spec guarantees only one event per axis type.
  1485. if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
  1486. pd.discrete_scroll_vector_120.y = discrete * 120;
  1487. }
  1488. if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
  1489. pd.discrete_scroll_vector_120.x = discrete * 120;
  1490. }
  1491. }
  1492. // Supersedes `wl_pointer::axis_discrete` Since version 8.
  1493. void WaylandThread::_wl_pointer_on_axis_value120(void *data, struct wl_pointer *wl_pointer, uint32_t axis, int32_t value120) {
  1494. SeatState *ss = (SeatState *)data;
  1495. ERR_FAIL_NULL(ss);
  1496. PointerData &pd = ss->pointer_data_buffer;
  1497. if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
  1498. pd.discrete_scroll_vector_120.y += value120;
  1499. }
  1500. if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
  1501. pd.discrete_scroll_vector_120.x += value120;
  1502. }
  1503. }
  1504. // TODO: Add support to this event.
  1505. void WaylandThread::_wl_pointer_on_axis_relative_direction(void *data, struct wl_pointer *wl_pointer, uint32_t axis, uint32_t direction) {
  1506. }
  1507. void WaylandThread::_wl_keyboard_on_keymap(void *data, struct wl_keyboard *wl_keyboard, uint32_t format, int32_t fd, uint32_t size) {
  1508. ERR_FAIL_COND_MSG(format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, "Unsupported keymap format announced from the Wayland compositor.");
  1509. SeatState *ss = (SeatState *)data;
  1510. ERR_FAIL_NULL(ss);
  1511. if (ss->keymap_buffer) {
  1512. // We have already a mapped buffer, so we unmap it. There's no need to reset
  1513. // its pointer or size, as we're gonna set them below.
  1514. munmap((void *)ss->keymap_buffer, ss->keymap_buffer_size);
  1515. ss->keymap_buffer = nullptr;
  1516. }
  1517. ss->keymap_buffer = (const char *)mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0);
  1518. ss->keymap_buffer_size = size;
  1519. xkb_keymap_unref(ss->xkb_keymap);
  1520. ss->xkb_keymap = xkb_keymap_new_from_string(ss->xkb_context, ss->keymap_buffer,
  1521. XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
  1522. xkb_state_unref(ss->xkb_state);
  1523. ss->xkb_state = xkb_state_new(ss->xkb_keymap);
  1524. }
  1525. void WaylandThread::_wl_keyboard_on_enter(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys) {
  1526. WindowState *ws = wl_surface_get_window_state(surface);
  1527. if (!ws) {
  1528. return;
  1529. }
  1530. SeatState *ss = (SeatState *)data;
  1531. ERR_FAIL_NULL(ss);
  1532. WaylandThread *wayland_thread = ss->wayland_thread;
  1533. ERR_FAIL_NULL(wayland_thread);
  1534. ss->focused_id = ws->id;
  1535. wayland_thread->_set_current_seat(ss->wl_seat);
  1536. Ref<WindowEventMessage> msg;
  1537. msg.instantiate();
  1538. msg->id = ws->id;
  1539. msg->event = DisplayServer::WINDOW_EVENT_FOCUS_IN;
  1540. wayland_thread->push_message(msg);
  1541. DEBUG_LOG_WAYLAND_THREAD(vformat("Keyboard focused window %d.", ws->id));
  1542. }
  1543. void WaylandThread::_wl_keyboard_on_leave(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *surface) {
  1544. // NOTE: `surface` will probably be null when the surface is destroyed.
  1545. // See: https://gitlab.freedesktop.org/wayland/wayland/-/issues/366
  1546. // See: https://gitlab.freedesktop.org/wayland/wayland/-/issues/465
  1547. if (surface && !wl_proxy_is_godot((struct wl_proxy *)surface)) {
  1548. return;
  1549. }
  1550. SeatState *ss = (SeatState *)data;
  1551. ERR_FAIL_NULL(ss);
  1552. WaylandThread *wayland_thread = ss->wayland_thread;
  1553. ERR_FAIL_NULL(wayland_thread);
  1554. ss->repeating_keycode = XKB_KEYCODE_INVALID;
  1555. if (ss->focused_id == DisplayServer::INVALID_WINDOW_ID) {
  1556. // We're probably on a decoration or some other third-party thing.
  1557. return;
  1558. }
  1559. WindowState *ws = wayland_thread->window_get_state(ss->focused_id);
  1560. ERR_FAIL_NULL(ws);
  1561. ss->focused_id = DisplayServer::INVALID_WINDOW_ID;
  1562. Ref<WindowEventMessage> msg;
  1563. msg.instantiate();
  1564. msg->id = ws->id;
  1565. msg->event = DisplayServer::WINDOW_EVENT_FOCUS_OUT;
  1566. wayland_thread->push_message(msg);
  1567. DEBUG_LOG_WAYLAND_THREAD(vformat("Keyboard unfocused window %d.", ws->id));
  1568. }
  1569. 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) {
  1570. SeatState *ss = (SeatState *)data;
  1571. ERR_FAIL_NULL(ss);
  1572. if (ss->focused_id == DisplayServer::INVALID_WINDOW_ID) {
  1573. return;
  1574. }
  1575. WaylandThread *wayland_thread = ss->wayland_thread;
  1576. ERR_FAIL_NULL(wayland_thread);
  1577. // We have to add 8 to the scancode to get an XKB-compatible keycode.
  1578. xkb_keycode_t xkb_keycode = key + 8;
  1579. bool pressed = state & WL_KEYBOARD_KEY_STATE_PRESSED;
  1580. if (pressed) {
  1581. if (xkb_keymap_key_repeats(ss->xkb_keymap, xkb_keycode)) {
  1582. ss->last_repeat_start_msec = OS::get_singleton()->get_ticks_msec();
  1583. ss->repeating_keycode = xkb_keycode;
  1584. }
  1585. ss->last_key_pressed_serial = serial;
  1586. } else if (ss->repeating_keycode == xkb_keycode) {
  1587. ss->repeating_keycode = XKB_KEYCODE_INVALID;
  1588. }
  1589. Ref<InputEventKey> k = _seat_state_get_key_event(ss, xkb_keycode, pressed);
  1590. if (k.is_null()) {
  1591. return;
  1592. }
  1593. Ref<InputEventKey> uk = _seat_state_get_unstuck_key_event(ss, xkb_keycode, pressed, k->get_keycode());
  1594. if (uk.is_valid()) {
  1595. Ref<InputEventMessage> u_msg;
  1596. u_msg.instantiate();
  1597. u_msg->event = uk;
  1598. wayland_thread->push_message(u_msg);
  1599. }
  1600. Ref<InputEventMessage> msg;
  1601. msg.instantiate();
  1602. msg->event = k;
  1603. wayland_thread->push_message(msg);
  1604. }
  1605. 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) {
  1606. SeatState *ss = (SeatState *)data;
  1607. ERR_FAIL_NULL(ss);
  1608. xkb_state_update_mask(ss->xkb_state, mods_depressed, mods_latched, mods_locked, ss->current_layout_index, ss->current_layout_index, group);
  1609. ss->shift_pressed = xkb_state_mod_name_is_active(ss->xkb_state, XKB_MOD_NAME_SHIFT, XKB_STATE_MODS_DEPRESSED);
  1610. ss->ctrl_pressed = xkb_state_mod_name_is_active(ss->xkb_state, XKB_MOD_NAME_CTRL, XKB_STATE_MODS_DEPRESSED);
  1611. ss->alt_pressed = xkb_state_mod_name_is_active(ss->xkb_state, XKB_MOD_NAME_ALT, XKB_STATE_MODS_DEPRESSED);
  1612. ss->meta_pressed = xkb_state_mod_name_is_active(ss->xkb_state, XKB_MOD_NAME_LOGO, XKB_STATE_MODS_DEPRESSED);
  1613. ss->current_layout_index = group;
  1614. }
  1615. void WaylandThread::_wl_keyboard_on_repeat_info(void *data, struct wl_keyboard *wl_keyboard, int32_t rate, int32_t delay) {
  1616. SeatState *ss = (SeatState *)data;
  1617. ERR_FAIL_NULL(ss);
  1618. ss->repeat_key_delay_msec = 1000 / rate;
  1619. ss->repeat_start_delay_msec = delay;
  1620. }
  1621. // NOTE: Don't forget to `memfree` the offer's state.
  1622. void WaylandThread::_wl_data_device_on_data_offer(void *data, struct wl_data_device *wl_data_device, struct wl_data_offer *id) {
  1623. wl_proxy_tag_godot((struct wl_proxy *)id);
  1624. wl_data_offer_add_listener(id, &wl_data_offer_listener, memnew(OfferState));
  1625. }
  1626. 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) {
  1627. WindowState *ws = wl_surface_get_window_state(surface);
  1628. if (!ws) {
  1629. return;
  1630. }
  1631. SeatState *ss = (SeatState *)data;
  1632. ERR_FAIL_NULL(ss);
  1633. ss->dnd_id = ws->id;
  1634. ss->dnd_enter_serial = serial;
  1635. ss->wl_data_offer_dnd = id;
  1636. // Godot only supports DnD file copying for now.
  1637. wl_data_offer_accept(id, serial, "text/uri-list");
  1638. wl_data_offer_set_actions(id, WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY, WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY);
  1639. }
  1640. void WaylandThread::_wl_data_device_on_leave(void *data, struct wl_data_device *wl_data_device) {
  1641. SeatState *ss = (SeatState *)data;
  1642. ERR_FAIL_NULL(ss);
  1643. if (ss->wl_data_offer_dnd) {
  1644. memdelete(wl_data_offer_get_offer_state(ss->wl_data_offer_dnd));
  1645. wl_data_offer_destroy(ss->wl_data_offer_dnd);
  1646. ss->wl_data_offer_dnd = nullptr;
  1647. ss->dnd_id = DisplayServer::INVALID_WINDOW_ID;
  1648. }
  1649. }
  1650. 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) {
  1651. }
  1652. void WaylandThread::_wl_data_device_on_drop(void *data, struct wl_data_device *wl_data_device) {
  1653. SeatState *ss = (SeatState *)data;
  1654. ERR_FAIL_NULL(ss);
  1655. WaylandThread *wayland_thread = ss->wayland_thread;
  1656. ERR_FAIL_NULL(wayland_thread);
  1657. OfferState *os = wl_data_offer_get_offer_state(ss->wl_data_offer_dnd);
  1658. ERR_FAIL_NULL(os);
  1659. if (os) {
  1660. Ref<DropFilesEventMessage> msg;
  1661. msg.instantiate();
  1662. msg->id = ss->dnd_id;
  1663. Vector<uint8_t> list_data = _wl_data_offer_read(wayland_thread->wl_display, "text/uri-list", ss->wl_data_offer_dnd);
  1664. msg->files = String::utf8((const char *)list_data.ptr(), list_data.size()).split("\r\n", false);
  1665. for (int i = 0; i < msg->files.size(); i++) {
  1666. msg->files.write[i] = msg->files[i].replace("file://", "").uri_file_decode();
  1667. }
  1668. wayland_thread->push_message(msg);
  1669. wl_data_offer_finish(ss->wl_data_offer_dnd);
  1670. }
  1671. memdelete(wl_data_offer_get_offer_state(ss->wl_data_offer_dnd));
  1672. wl_data_offer_destroy(ss->wl_data_offer_dnd);
  1673. ss->wl_data_offer_dnd = nullptr;
  1674. ss->dnd_id = DisplayServer::INVALID_WINDOW_ID;
  1675. }
  1676. void WaylandThread::_wl_data_device_on_selection(void *data, struct wl_data_device *wl_data_device, struct wl_data_offer *id) {
  1677. SeatState *ss = (SeatState *)data;
  1678. ERR_FAIL_NULL(ss);
  1679. if (ss->wl_data_offer_selection) {
  1680. memdelete(wl_data_offer_get_offer_state(ss->wl_data_offer_selection));
  1681. wl_data_offer_destroy(ss->wl_data_offer_selection);
  1682. }
  1683. ss->wl_data_offer_selection = id;
  1684. }
  1685. void WaylandThread::_wl_data_offer_on_offer(void *data, struct wl_data_offer *wl_data_offer, const char *mime_type) {
  1686. OfferState *os = (OfferState *)data;
  1687. ERR_FAIL_NULL(os);
  1688. if (os) {
  1689. os->mime_types.insert(String::utf8(mime_type));
  1690. }
  1691. }
  1692. void WaylandThread::_wl_data_offer_on_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions) {
  1693. }
  1694. void WaylandThread::_wl_data_offer_on_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action) {
  1695. }
  1696. void WaylandThread::_wl_data_source_on_target(void *data, struct wl_data_source *wl_data_source, const char *mime_type) {
  1697. }
  1698. void WaylandThread::_wl_data_source_on_send(void *data, struct wl_data_source *wl_data_source, const char *mime_type, int32_t fd) {
  1699. SeatState *ss = (SeatState *)data;
  1700. ERR_FAIL_NULL(ss);
  1701. Vector<uint8_t> *data_to_send = nullptr;
  1702. if (wl_data_source == ss->wl_data_source_selection) {
  1703. data_to_send = &ss->selection_data;
  1704. DEBUG_LOG_WAYLAND_THREAD("Clipboard: requested selection.");
  1705. }
  1706. if (data_to_send) {
  1707. ssize_t written_bytes = 0;
  1708. bool valid_mime = false;
  1709. if (strcmp(mime_type, "text/plain;charset=utf-8") == 0) {
  1710. valid_mime = true;
  1711. } else if (strcmp(mime_type, "text/plain") == 0) {
  1712. valid_mime = true;
  1713. }
  1714. if (valid_mime) {
  1715. written_bytes = write(fd, data_to_send->ptr(), data_to_send->size());
  1716. }
  1717. if (written_bytes > 0) {
  1718. DEBUG_LOG_WAYLAND_THREAD(vformat("Clipboard: sent %d bytes.", written_bytes));
  1719. } else if (written_bytes == 0) {
  1720. DEBUG_LOG_WAYLAND_THREAD("Clipboard: no bytes sent.");
  1721. } else {
  1722. ERR_PRINT(vformat("Clipboard: write error %d.", errno));
  1723. }
  1724. }
  1725. close(fd);
  1726. }
  1727. void WaylandThread::_wl_data_source_on_cancelled(void *data, struct wl_data_source *wl_data_source) {
  1728. SeatState *ss = (SeatState *)data;
  1729. ERR_FAIL_NULL(ss);
  1730. wl_data_source_destroy(wl_data_source);
  1731. if (wl_data_source == ss->wl_data_source_selection) {
  1732. ss->wl_data_source_selection = nullptr;
  1733. ss->selection_data.clear();
  1734. DEBUG_LOG_WAYLAND_THREAD("Clipboard: selection set by another program.");
  1735. return;
  1736. }
  1737. }
  1738. void WaylandThread::_wl_data_source_on_dnd_drop_performed(void *data, struct wl_data_source *wl_data_source) {
  1739. }
  1740. void WaylandThread::_wl_data_source_on_dnd_finished(void *data, struct wl_data_source *wl_data_source) {
  1741. }
  1742. void WaylandThread::_wl_data_source_on_action(void *data, struct wl_data_source *wl_data_source, uint32_t dnd_action) {
  1743. }
  1744. void WaylandThread::_wp_fractional_scale_on_preferred_scale(void *data, struct wp_fractional_scale_v1 *wp_fractional_scale_v1, uint32_t scale) {
  1745. WindowState *ws = (WindowState *)data;
  1746. ERR_FAIL_NULL(ws);
  1747. ws->preferred_fractional_scale = (double)scale / 120;
  1748. window_state_update_size(ws, ws->rect.size.width, ws->rect.size.height);
  1749. }
  1750. 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) {
  1751. SeatState *ss = (SeatState *)data;
  1752. ERR_FAIL_NULL(ss);
  1753. PointerData &pd = ss->pointer_data_buffer;
  1754. pd.relative_motion.x = wl_fixed_to_double(dx);
  1755. pd.relative_motion.y = wl_fixed_to_double(dy);
  1756. pd.relative_motion_time = uptime_lo;
  1757. }
  1758. 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) {
  1759. SeatState *ss = (SeatState *)data;
  1760. ERR_FAIL_NULL(ss);
  1761. if (fingers == 2) {
  1762. ss->old_pinch_scale = wl_fixed_from_int(1);
  1763. ss->active_gesture = Gesture::MAGNIFY;
  1764. }
  1765. }
  1766. 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) {
  1767. SeatState *ss = (SeatState *)data;
  1768. ERR_FAIL_NULL(ss);
  1769. // NOTE: From what I can tell, this and all other pointer gestures are separate
  1770. // from the "frame" mechanism of regular pointers. Thus, let's just assume we
  1771. // can read from the "committed" state.
  1772. const PointerData &pd = ss->pointer_data;
  1773. WaylandThread *wayland_thread = ss->wayland_thread;
  1774. ERR_FAIL_NULL(wayland_thread);
  1775. WindowState *ws = wayland_thread->window_get_state(pd.pointed_id);
  1776. ERR_FAIL_NULL(ws);
  1777. double win_scale = window_state_get_scale_factor(ws);
  1778. if (ss->active_gesture == Gesture::MAGNIFY) {
  1779. Ref<InputEventMagnifyGesture> mg;
  1780. mg.instantiate();
  1781. mg->set_window_id(pd.pointed_id);
  1782. if (ws) {
  1783. mg->set_window_id(ws->id);
  1784. }
  1785. // Set all pressed modifiers.
  1786. mg->set_shift_pressed(ss->shift_pressed);
  1787. mg->set_ctrl_pressed(ss->ctrl_pressed);
  1788. mg->set_alt_pressed(ss->alt_pressed);
  1789. mg->set_meta_pressed(ss->meta_pressed);
  1790. mg->set_position(pd.position * win_scale);
  1791. wl_fixed_t scale_delta = scale - ss->old_pinch_scale;
  1792. mg->set_factor(1 + wl_fixed_to_double(scale_delta));
  1793. Ref<InputEventMessage> magnify_msg;
  1794. magnify_msg.instantiate();
  1795. magnify_msg->event = mg;
  1796. // Since Wayland allows only one gesture at a time and godot instead expects
  1797. // both of them, we'll have to create two separate input events: one for
  1798. // magnification and one for panning.
  1799. Ref<InputEventPanGesture> pg;
  1800. pg.instantiate();
  1801. // Set all pressed modifiers.
  1802. pg->set_shift_pressed(ss->shift_pressed);
  1803. pg->set_ctrl_pressed(ss->ctrl_pressed);
  1804. pg->set_alt_pressed(ss->alt_pressed);
  1805. pg->set_meta_pressed(ss->meta_pressed);
  1806. pg->set_position(pd.position * win_scale);
  1807. pg->set_delta(Vector2(wl_fixed_to_double(dx), wl_fixed_to_double(dy)));
  1808. Ref<InputEventMessage> pan_msg;
  1809. pan_msg.instantiate();
  1810. pan_msg->event = pg;
  1811. wayland_thread->push_message(magnify_msg);
  1812. wayland_thread->push_message(pan_msg);
  1813. ss->old_pinch_scale = scale;
  1814. }
  1815. }
  1816. 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) {
  1817. SeatState *ss = (SeatState *)data;
  1818. ERR_FAIL_NULL(ss);
  1819. ss->active_gesture = Gesture::NONE;
  1820. }
  1821. // NOTE: Don't forget to `memfree` the offer's state.
  1822. 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) {
  1823. wl_proxy_tag_godot((struct wl_proxy *)offer);
  1824. zwp_primary_selection_offer_v1_add_listener(offer, &wp_primary_selection_offer_listener, memnew(OfferState));
  1825. }
  1826. 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) {
  1827. SeatState *ss = (SeatState *)data;
  1828. ERR_FAIL_NULL(ss);
  1829. if (ss->wp_primary_selection_offer) {
  1830. memfree(wp_primary_selection_offer_get_offer_state(ss->wp_primary_selection_offer));
  1831. zwp_primary_selection_offer_v1_destroy(ss->wp_primary_selection_offer);
  1832. }
  1833. ss->wp_primary_selection_offer = id;
  1834. }
  1835. 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) {
  1836. OfferState *os = (OfferState *)data;
  1837. ERR_FAIL_NULL(os);
  1838. if (os) {
  1839. os->mime_types.insert(String::utf8(mime_type));
  1840. }
  1841. }
  1842. 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) {
  1843. SeatState *ss = (SeatState *)data;
  1844. ERR_FAIL_NULL(ss);
  1845. Vector<uint8_t> *data_to_send = nullptr;
  1846. if (wp_primary_selection_source_v1 == ss->wp_primary_selection_source) {
  1847. data_to_send = &ss->primary_data;
  1848. DEBUG_LOG_WAYLAND_THREAD("Clipboard: requested primary selection.");
  1849. }
  1850. if (data_to_send) {
  1851. ssize_t written_bytes = 0;
  1852. if (strcmp(mime_type, "text/plain") == 0) {
  1853. written_bytes = write(fd, data_to_send->ptr(), data_to_send->size());
  1854. }
  1855. if (written_bytes > 0) {
  1856. DEBUG_LOG_WAYLAND_THREAD(vformat("Clipboard: sent %d bytes.", written_bytes));
  1857. } else if (written_bytes == 0) {
  1858. DEBUG_LOG_WAYLAND_THREAD("Clipboard: no bytes sent.");
  1859. } else {
  1860. ERR_PRINT(vformat("Clipboard: write error %d.", errno));
  1861. }
  1862. }
  1863. close(fd);
  1864. }
  1865. void WaylandThread::_wp_primary_selection_source_on_cancelled(void *data, struct zwp_primary_selection_source_v1 *wp_primary_selection_source_v1) {
  1866. SeatState *ss = (SeatState *)data;
  1867. ERR_FAIL_NULL(ss);
  1868. if (wp_primary_selection_source_v1 == ss->wp_primary_selection_source) {
  1869. zwp_primary_selection_source_v1_destroy(ss->wp_primary_selection_source);
  1870. ss->wp_primary_selection_source = nullptr;
  1871. ss->primary_data.clear();
  1872. DEBUG_LOG_WAYLAND_THREAD("Clipboard: primary selection set by another program.");
  1873. return;
  1874. }
  1875. }
  1876. void WaylandThread::_wp_tablet_seat_on_tablet_added(void *data, struct zwp_tablet_seat_v2 *wp_tablet_seat_v2, struct zwp_tablet_v2 *id) {
  1877. }
  1878. 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) {
  1879. SeatState *ss = (SeatState *)data;
  1880. ERR_FAIL_NULL(ss);
  1881. TabletToolState *state = memnew(TabletToolState);
  1882. state->wl_seat = ss->wl_seat;
  1883. wl_proxy_tag_godot((struct wl_proxy *)id);
  1884. zwp_tablet_tool_v2_add_listener(id, &wp_tablet_tool_listener, state);
  1885. ss->tablet_tools.push_back(id);
  1886. }
  1887. 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) {
  1888. }
  1889. void WaylandThread::_wp_tablet_tool_on_type(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t tool_type) {
  1890. TabletToolState *state = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  1891. if (state && tool_type == ZWP_TABLET_TOOL_V2_TYPE_ERASER) {
  1892. state->is_eraser = true;
  1893. }
  1894. }
  1895. 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) {
  1896. }
  1897. 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) {
  1898. }
  1899. void WaylandThread::_wp_tablet_tool_on_capability(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t capability) {
  1900. }
  1901. void WaylandThread::_wp_tablet_tool_on_done(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2) {
  1902. }
  1903. void WaylandThread::_wp_tablet_tool_on_removed(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2) {
  1904. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  1905. if (!ts) {
  1906. return;
  1907. }
  1908. SeatState *ss = wl_seat_get_seat_state(ts->wl_seat);
  1909. if (!ss) {
  1910. return;
  1911. }
  1912. List<struct zwp_tablet_tool_v2 *>::Element *E = ss->tablet_tools.find(wp_tablet_tool_v2);
  1913. if (E && E->get()) {
  1914. struct zwp_tablet_tool_v2 *tool = E->get();
  1915. TabletToolState *state = wp_tablet_tool_get_state(tool);
  1916. if (state) {
  1917. memdelete(state);
  1918. }
  1919. zwp_tablet_tool_v2_destroy(tool);
  1920. ss->tablet_tools.erase(E);
  1921. }
  1922. }
  1923. 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) {
  1924. // NOTE: Works pretty much like wl_pointer::enter.
  1925. WindowState *ws = wl_surface_get_window_state(surface);
  1926. if (!ws) {
  1927. return;
  1928. }
  1929. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  1930. ERR_FAIL_NULL(ts);
  1931. ts->data_pending.proximity_serial = serial;
  1932. ts->data_pending.proximal_id = ws->id;
  1933. ts->data_pending.last_proximal_id = ws->id;
  1934. DEBUG_LOG_WAYLAND_THREAD(vformat("Tablet tool entered window %d.", ts->data_pending.proximal_id));
  1935. }
  1936. void WaylandThread::_wp_tablet_tool_on_proximity_out(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2) {
  1937. // NOTE: Works pretty much like wl_pointer::leave.
  1938. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  1939. ERR_FAIL_NULL(ts);
  1940. if (ts->data_pending.proximal_id == DisplayServer::INVALID_WINDOW_ID) {
  1941. // We're probably on a decoration or some other third-party thing.
  1942. return;
  1943. }
  1944. DisplayServer::WindowID id = ts->data_pending.proximal_id;
  1945. ts->data_pending.proximal_id = DisplayServer::INVALID_WINDOW_ID;
  1946. ts->data_pending.pressed_button_mask.clear();
  1947. DEBUG_LOG_WAYLAND_THREAD(vformat("Tablet tool left window %d.", id));
  1948. }
  1949. void WaylandThread::_wp_tablet_tool_on_down(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t serial) {
  1950. // NOTE: Works pretty much like wl_pointer::button but only for a pressed left
  1951. // button.
  1952. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  1953. ERR_FAIL_NULL(ts);
  1954. TabletToolData &td = ts->data_pending;
  1955. td.pressed_button_mask.set_flag(mouse_button_to_mask(MouseButton::LEFT));
  1956. td.last_button_pressed = MouseButton::LEFT;
  1957. td.double_click_begun = true;
  1958. // The protocol doesn't cover this, but we can use this funky hack to make
  1959. // double clicking work.
  1960. td.button_time = OS::get_singleton()->get_ticks_msec();
  1961. }
  1962. void WaylandThread::_wp_tablet_tool_on_up(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2) {
  1963. // NOTE: Works pretty much like wl_pointer::button but only for a released left
  1964. // button.
  1965. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  1966. ERR_FAIL_NULL(ts);
  1967. TabletToolData &td = ts->data_pending;
  1968. td.pressed_button_mask.clear_flag(mouse_button_to_mask(MouseButton::LEFT));
  1969. // The protocol doesn't cover this, but we can use this funky hack to make
  1970. // double clicking work.
  1971. td.button_time = OS::get_singleton()->get_ticks_msec();
  1972. }
  1973. 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) {
  1974. // NOTE: Works pretty much like wl_pointer::motion.
  1975. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  1976. ERR_FAIL_NULL(ts);
  1977. TabletToolData &td = ts->data_pending;
  1978. td.position.x = wl_fixed_to_double(x);
  1979. td.position.y = wl_fixed_to_double(y);
  1980. }
  1981. void WaylandThread::_wp_tablet_tool_on_pressure(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t pressure) {
  1982. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  1983. ERR_FAIL_NULL(ts);
  1984. ts->data_pending.pressure = pressure;
  1985. }
  1986. void WaylandThread::_wp_tablet_tool_on_distance(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t distance) {
  1987. // Unsupported
  1988. }
  1989. 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) {
  1990. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  1991. ERR_FAIL_NULL(ts);
  1992. TabletToolData &td = ts->data_pending;
  1993. td.tilt.x = wl_fixed_to_double(tilt_x);
  1994. td.tilt.y = wl_fixed_to_double(tilt_y);
  1995. }
  1996. void WaylandThread::_wp_tablet_tool_on_rotation(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, wl_fixed_t degrees) {
  1997. // Unsupported.
  1998. }
  1999. void WaylandThread::_wp_tablet_tool_on_slider(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, int32_t position) {
  2000. // Unsupported.
  2001. }
  2002. 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) {
  2003. // TODO
  2004. }
  2005. 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) {
  2006. // NOTE: Works pretty much like wl_pointer::button.
  2007. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  2008. ERR_FAIL_NULL(ts);
  2009. TabletToolData &td = ts->data_pending;
  2010. MouseButton mouse_button = MouseButton::NONE;
  2011. if (button == BTN_STYLUS) {
  2012. mouse_button = MouseButton::LEFT;
  2013. }
  2014. if (button == BTN_STYLUS2) {
  2015. mouse_button = MouseButton::RIGHT;
  2016. }
  2017. if (mouse_button != MouseButton::NONE) {
  2018. MouseButtonMask mask = mouse_button_to_mask(mouse_button);
  2019. if (state == ZWP_TABLET_TOOL_V2_BUTTON_STATE_PRESSED) {
  2020. td.pressed_button_mask.set_flag(mask);
  2021. td.last_button_pressed = mouse_button;
  2022. td.double_click_begun = true;
  2023. } else {
  2024. td.pressed_button_mask.clear_flag(mask);
  2025. }
  2026. // The protocol doesn't cover this, but we can use this funky hack to make
  2027. // double clicking work.
  2028. td.button_time = OS::get_singleton()->get_ticks_msec();
  2029. }
  2030. }
  2031. void WaylandThread::_wp_tablet_tool_on_frame(void *data, struct zwp_tablet_tool_v2 *wp_tablet_tool_v2, uint32_t time) {
  2032. // NOTE: Works pretty much like wl_pointer::frame.
  2033. TabletToolState *ts = wp_tablet_tool_get_state(wp_tablet_tool_v2);
  2034. ERR_FAIL_NULL(ts);
  2035. SeatState *ss = wl_seat_get_seat_state(ts->wl_seat);
  2036. ERR_FAIL_NULL(ss);
  2037. WaylandThread *wayland_thread = ss->wayland_thread;
  2038. ERR_FAIL_NULL(wayland_thread);
  2039. TabletToolData &old_td = ts->data;
  2040. TabletToolData &td = ts->data_pending;
  2041. if (td.proximal_id != old_td.proximal_id) {
  2042. if (old_td.proximal_id != DisplayServer::INVALID_WINDOW_ID) {
  2043. Ref<WindowEventMessage> msg;
  2044. msg.instantiate();
  2045. msg->id = old_td.proximal_id;
  2046. msg->event = DisplayServer::WINDOW_EVENT_MOUSE_EXIT;
  2047. wayland_thread->push_message(msg);
  2048. }
  2049. if (td.proximal_id != DisplayServer::INVALID_WINDOW_ID) {
  2050. Ref<WindowEventMessage> msg;
  2051. msg.instantiate();
  2052. msg->id = td.proximal_id;
  2053. msg->event = DisplayServer::WINDOW_EVENT_MOUSE_ENTER;
  2054. wayland_thread->push_message(msg);
  2055. }
  2056. }
  2057. if (td.proximal_id == DisplayServer::INVALID_WINDOW_ID) {
  2058. // We're probably on a decoration or some other third-party thing. Let's
  2059. // "commit" the data and call it a day.
  2060. old_td = td;
  2061. return;
  2062. }
  2063. WindowState *ws = wayland_thread->window_get_state(td.proximal_id);
  2064. ERR_FAIL_NULL(ws);
  2065. double scale = window_state_get_scale_factor(ws);
  2066. if (old_td.position != td.position || old_td.tilt != td.tilt || old_td.pressure != td.pressure) {
  2067. td.motion_time = time;
  2068. Ref<InputEventMouseMotion> mm;
  2069. mm.instantiate();
  2070. mm->set_window_id(td.proximal_id);
  2071. // Set all pressed modifiers.
  2072. mm->set_shift_pressed(ss->shift_pressed);
  2073. mm->set_ctrl_pressed(ss->ctrl_pressed);
  2074. mm->set_alt_pressed(ss->alt_pressed);
  2075. mm->set_meta_pressed(ss->meta_pressed);
  2076. mm->set_button_mask(td.pressed_button_mask);
  2077. mm->set_global_position(td.position * scale);
  2078. mm->set_position(td.position * scale);
  2079. // NOTE: The Godot API expects normalized values and we store them raw,
  2080. // straight from the compositor, so we have to normalize them here.
  2081. // According to the tablet proto spec, tilt is expressed in degrees relative
  2082. // to the Z axis of the tablet, so it shouldn't go over 90 degrees either way,
  2083. // I think. We'll clamp it just in case.
  2084. td.tilt = td.tilt.clampf(-90, 90);
  2085. mm->set_tilt(td.tilt / 90);
  2086. // The tablet proto spec explicitly says that pressure is defined as a value
  2087. // between 0 to 65535.
  2088. mm->set_pressure(td.pressure / (float)65535);
  2089. mm->set_pen_inverted(ts->is_eraser);
  2090. Vector2 pos_delta = (td.position - old_td.position) * scale;
  2091. mm->set_relative(pos_delta);
  2092. mm->set_relative_screen_position(pos_delta);
  2093. uint32_t time_delta = td.motion_time - old_td.motion_time;
  2094. mm->set_velocity((Vector2)pos_delta / time_delta);
  2095. Ref<InputEventMessage> inputev_msg;
  2096. inputev_msg.instantiate();
  2097. inputev_msg->event = mm;
  2098. wayland_thread->push_message(inputev_msg);
  2099. }
  2100. if (old_td.pressed_button_mask != td.pressed_button_mask) {
  2101. td.button_time = time;
  2102. BitField<MouseButtonMask> pressed_mask_delta = old_td.pressed_button_mask.get_different(td.pressed_button_mask);
  2103. for (MouseButton test_button : { MouseButton::LEFT, MouseButton::RIGHT }) {
  2104. MouseButtonMask test_button_mask = mouse_button_to_mask(test_button);
  2105. if (pressed_mask_delta.has_flag(test_button_mask)) {
  2106. Ref<InputEventMouseButton> mb;
  2107. mb.instantiate();
  2108. // Set all pressed modifiers.
  2109. mb->set_shift_pressed(ss->shift_pressed);
  2110. mb->set_ctrl_pressed(ss->ctrl_pressed);
  2111. mb->set_alt_pressed(ss->alt_pressed);
  2112. mb->set_meta_pressed(ss->meta_pressed);
  2113. mb->set_window_id(td.proximal_id);
  2114. mb->set_position(td.position * scale);
  2115. mb->set_global_position(td.position * scale);
  2116. mb->set_button_mask(td.pressed_button_mask);
  2117. mb->set_button_index(test_button);
  2118. mb->set_pressed(td.pressed_button_mask.has_flag(test_button_mask));
  2119. // We have to set the last position pressed here as we can't take for
  2120. // granted what the individual events might have seen due to them not having
  2121. // a garaunteed order.
  2122. if (mb->is_pressed()) {
  2123. td.last_pressed_position = td.position;
  2124. }
  2125. 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 * scale).distance_to(Vector2(old_td.last_pressed_position * scale)) < 5) {
  2126. td.double_click_begun = false;
  2127. mb->set_double_click(true);
  2128. }
  2129. Ref<InputEventMessage> msg;
  2130. msg.instantiate();
  2131. msg->event = mb;
  2132. wayland_thread->push_message(msg);
  2133. }
  2134. }
  2135. }
  2136. old_td = td;
  2137. }
  2138. void WaylandThread::_wp_text_input_on_enter(void *data, struct zwp_text_input_v3 *wp_text_input_v3, struct wl_surface *surface) {
  2139. SeatState *ss = (SeatState *)data;
  2140. if (!ss) {
  2141. return;
  2142. }
  2143. WindowState *ws = wl_surface_get_window_state(surface);
  2144. if (!ws) {
  2145. return;
  2146. }
  2147. ss->ime_window_id = ws->id;
  2148. ss->ime_enabled = true;
  2149. }
  2150. void WaylandThread::_wp_text_input_on_leave(void *data, struct zwp_text_input_v3 *wp_text_input_v3, struct wl_surface *surface) {
  2151. SeatState *ss = (SeatState *)data;
  2152. if (!ss) {
  2153. return;
  2154. }
  2155. Ref<IMEUpdateEventMessage> msg;
  2156. msg.instantiate();
  2157. msg->id = ss->ime_window_id;
  2158. msg->text = String();
  2159. msg->selection = Vector2i();
  2160. ss->wayland_thread->push_message(msg);
  2161. ss->ime_window_id = DisplayServer::INVALID_WINDOW_ID;
  2162. ss->ime_enabled = false;
  2163. ss->ime_active = false;
  2164. ss->ime_text = String();
  2165. ss->ime_text_commit = String();
  2166. ss->ime_cursor = Vector2i();
  2167. }
  2168. 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) {
  2169. SeatState *ss = (SeatState *)data;
  2170. if (!ss) {
  2171. return;
  2172. }
  2173. ss->ime_text = String::utf8(text);
  2174. // Convert cursor positions from UTF-8 to UTF-32 offset.
  2175. int32_t cursor_begin_utf32 = 0;
  2176. int32_t cursor_end_utf32 = 0;
  2177. for (int i = 0; i < ss->ime_text.length(); i++) {
  2178. uint32_t c = ss->ime_text[i];
  2179. if (c <= 0x7f) { // 7 bits.
  2180. cursor_begin -= 1;
  2181. cursor_end -= 1;
  2182. } else if (c <= 0x7ff) { // 11 bits
  2183. cursor_begin -= 2;
  2184. cursor_end -= 2;
  2185. } else if (c <= 0xffff) { // 16 bits
  2186. cursor_begin -= 3;
  2187. cursor_end -= 3;
  2188. } else if (c <= 0x001fffff) { // 21 bits
  2189. cursor_begin -= 4;
  2190. cursor_end -= 4;
  2191. } else if (c <= 0x03ffffff) { // 26 bits
  2192. cursor_begin -= 5;
  2193. cursor_end -= 5;
  2194. } else if (c <= 0x7fffffff) { // 31 bits
  2195. cursor_begin -= 6;
  2196. cursor_end -= 6;
  2197. } else {
  2198. cursor_begin -= 1;
  2199. cursor_end -= 1;
  2200. }
  2201. if (cursor_begin == 0) {
  2202. cursor_begin_utf32 = i + 1;
  2203. }
  2204. if (cursor_end == 0) {
  2205. cursor_end_utf32 = i + 1;
  2206. }
  2207. if (cursor_begin <= 0 && cursor_end <= 0) {
  2208. break;
  2209. }
  2210. }
  2211. ss->ime_cursor = Vector2i(cursor_begin_utf32, cursor_end_utf32 - cursor_begin_utf32);
  2212. }
  2213. void WaylandThread::_wp_text_input_on_commit_string(void *data, struct zwp_text_input_v3 *wp_text_input_v3, const char *text) {
  2214. SeatState *ss = (SeatState *)data;
  2215. if (!ss) {
  2216. return;
  2217. }
  2218. ss->ime_text_commit = String::utf8(text);
  2219. }
  2220. 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) {
  2221. // Not implemented.
  2222. }
  2223. void WaylandThread::_wp_text_input_on_done(void *data, struct zwp_text_input_v3 *wp_text_input_v3, uint32_t serial) {
  2224. SeatState *ss = (SeatState *)data;
  2225. if (!ss) {
  2226. return;
  2227. }
  2228. if (!ss->ime_text_commit.is_empty()) {
  2229. Ref<IMECommitEventMessage> msg;
  2230. msg.instantiate();
  2231. msg->id = ss->ime_window_id;
  2232. msg->text = ss->ime_text_commit;
  2233. ss->wayland_thread->push_message(msg);
  2234. } else {
  2235. Ref<IMEUpdateEventMessage> msg;
  2236. msg.instantiate();
  2237. msg->id = ss->ime_window_id;
  2238. msg->text = ss->ime_text;
  2239. msg->selection = ss->ime_cursor;
  2240. ss->wayland_thread->push_message(msg);
  2241. }
  2242. ss->ime_text = String();
  2243. ss->ime_text_commit = String();
  2244. ss->ime_cursor = Vector2i();
  2245. }
  2246. void WaylandThread::_xdg_activation_token_on_done(void *data, struct xdg_activation_token_v1 *xdg_activation_token, const char *token) {
  2247. WindowState *ws = (WindowState *)data;
  2248. ERR_FAIL_NULL(ws);
  2249. ERR_FAIL_NULL(ws->wayland_thread);
  2250. ERR_FAIL_NULL(ws->wl_surface);
  2251. xdg_activation_v1_activate(ws->wayland_thread->registry.xdg_activation, token, ws->wl_surface);
  2252. xdg_activation_token_v1_destroy(xdg_activation_token);
  2253. DEBUG_LOG_WAYLAND_THREAD(vformat("Received activation token and requested window activation."));
  2254. }
  2255. // NOTE: This must be started after a valid wl_display is loaded.
  2256. void WaylandThread::_poll_events_thread(void *p_data) {
  2257. ThreadData *data = (ThreadData *)p_data;
  2258. ERR_FAIL_NULL(data);
  2259. ERR_FAIL_NULL(data->wl_display);
  2260. struct pollfd poll_fd;
  2261. poll_fd.fd = wl_display_get_fd(data->wl_display);
  2262. poll_fd.events = POLLIN | POLLHUP;
  2263. while (true) {
  2264. // Empty the event queue while it's full.
  2265. while (wl_display_prepare_read(data->wl_display) != 0) {
  2266. // We aren't using wl_display_dispatch(), instead "manually" handling events
  2267. // through wl_display_dispatch_pending so that we can use a global mutex and
  2268. // be sure that this and the main thread won't race over stuff, as long as
  2269. // the main thread locks it too.
  2270. //
  2271. // Note that the main thread can still call wl_display_roundtrip as that
  2272. // method directly handles all events, effectively bypassing this polling
  2273. // loop and thus the mutex locking, avoiding a deadlock.
  2274. MutexLock mutex_lock(data->mutex);
  2275. if (wl_display_dispatch_pending(data->wl_display) == -1) {
  2276. // Oh no. We'll check and handle any display error below.
  2277. break;
  2278. }
  2279. }
  2280. int werror = wl_display_get_error(data->wl_display);
  2281. if (werror) {
  2282. if (werror == EPROTO) {
  2283. struct wl_interface *wl_interface = nullptr;
  2284. uint32_t id = 0;
  2285. int error_code = wl_display_get_protocol_error(data->wl_display, (const struct wl_interface **)&wl_interface, &id);
  2286. CRASH_NOW_MSG(vformat("Wayland protocol error %d on interface %s@%d.", error_code, wl_interface ? wl_interface->name : "unknown", id));
  2287. } else {
  2288. CRASH_NOW_MSG(vformat("Wayland client error code %d.", werror));
  2289. }
  2290. }
  2291. wl_display_flush(data->wl_display);
  2292. // Wait for the event file descriptor to have new data.
  2293. poll(&poll_fd, 1, -1);
  2294. if (data->thread_done.is_set()) {
  2295. wl_display_cancel_read(data->wl_display);
  2296. break;
  2297. }
  2298. if (poll_fd.revents | POLLIN) {
  2299. // Load the queues with fresh new data.
  2300. wl_display_read_events(data->wl_display);
  2301. } else {
  2302. // Oh well... Stop signaling that we want to read.
  2303. wl_display_cancel_read(data->wl_display);
  2304. }
  2305. // The docs advise to redispatch unconditionally and it looks like that if we
  2306. // don't do this we can't catch protocol errors, which is bad.
  2307. MutexLock mutex_lock(data->mutex);
  2308. wl_display_dispatch_pending(data->wl_display);
  2309. }
  2310. }
  2311. struct wl_display *WaylandThread::get_wl_display() const {
  2312. return wl_display;
  2313. }
  2314. // NOTE: Stuff like libdecor can (and will) register foreign proxies which
  2315. // aren't formatted as we like. This method is needed to detect whether a proxy
  2316. // has our tag. Also, be careful! The proxy has to be manually tagged or it
  2317. // won't be recognized.
  2318. bool WaylandThread::wl_proxy_is_godot(struct wl_proxy *p_proxy) {
  2319. ERR_FAIL_NULL_V(p_proxy, false);
  2320. return wl_proxy_get_tag(p_proxy) == &proxy_tag;
  2321. }
  2322. void WaylandThread::wl_proxy_tag_godot(struct wl_proxy *p_proxy) {
  2323. ERR_FAIL_NULL(p_proxy);
  2324. wl_proxy_set_tag(p_proxy, &proxy_tag);
  2325. }
  2326. // Returns the wl_surface's `WindowState`, otherwise `nullptr`.
  2327. // NOTE: This will fail if the surface isn't tagged as ours.
  2328. WaylandThread::WindowState *WaylandThread::wl_surface_get_window_state(struct wl_surface *p_surface) {
  2329. if (p_surface && wl_proxy_is_godot((wl_proxy *)p_surface)) {
  2330. return (WindowState *)wl_surface_get_user_data(p_surface);
  2331. }
  2332. return nullptr;
  2333. }
  2334. // Returns the wl_outputs's `ScreenState`, otherwise `nullptr`.
  2335. // NOTE: This will fail if the output isn't tagged as ours.
  2336. WaylandThread::ScreenState *WaylandThread::wl_output_get_screen_state(struct wl_output *p_output) {
  2337. if (p_output && wl_proxy_is_godot((wl_proxy *)p_output)) {
  2338. return (ScreenState *)wl_output_get_user_data(p_output);
  2339. }
  2340. return nullptr;
  2341. }
  2342. // Returns the wl_seat's `SeatState`, otherwise `nullptr`.
  2343. // NOTE: This will fail if the output isn't tagged as ours.
  2344. WaylandThread::SeatState *WaylandThread::wl_seat_get_seat_state(struct wl_seat *p_seat) {
  2345. if (p_seat && wl_proxy_is_godot((wl_proxy *)p_seat)) {
  2346. return (SeatState *)wl_seat_get_user_data(p_seat);
  2347. }
  2348. return nullptr;
  2349. }
  2350. // Returns the wp_tablet_tool's `TabletToolState`, otherwise `nullptr`.
  2351. // NOTE: This will fail if the output isn't tagged as ours.
  2352. WaylandThread::TabletToolState *WaylandThread::wp_tablet_tool_get_state(struct zwp_tablet_tool_v2 *p_tool) {
  2353. if (p_tool && wl_proxy_is_godot((wl_proxy *)p_tool)) {
  2354. return (TabletToolState *)zwp_tablet_tool_v2_get_user_data(p_tool);
  2355. }
  2356. return nullptr;
  2357. }
  2358. // Returns the wl_data_offer's `OfferState`, otherwise `nullptr`.
  2359. // NOTE: This will fail if the output isn't tagged as ours.
  2360. WaylandThread::OfferState *WaylandThread::wl_data_offer_get_offer_state(struct wl_data_offer *p_offer) {
  2361. if (p_offer && wl_proxy_is_godot((wl_proxy *)p_offer)) {
  2362. return (OfferState *)wl_data_offer_get_user_data(p_offer);
  2363. }
  2364. return nullptr;
  2365. }
  2366. // Returns the wl_data_offer's `OfferState`, otherwise `nullptr`.
  2367. // NOTE: This will fail if the output isn't tagged as ours.
  2368. WaylandThread::OfferState *WaylandThread::wp_primary_selection_offer_get_offer_state(struct zwp_primary_selection_offer_v1 *p_offer) {
  2369. if (p_offer && wl_proxy_is_godot((wl_proxy *)p_offer)) {
  2370. return (OfferState *)zwp_primary_selection_offer_v1_get_user_data(p_offer);
  2371. }
  2372. return nullptr;
  2373. }
  2374. // This is implemented as a method because this is the simplest way of
  2375. // accounting for dynamic output scale changes.
  2376. int WaylandThread::window_state_get_preferred_buffer_scale(WindowState *p_ws) {
  2377. ERR_FAIL_NULL_V(p_ws, 1);
  2378. if (p_ws->preferred_fractional_scale > 0) {
  2379. // We're scaling fractionally. Per spec, the buffer scale is always 1.
  2380. return 1;
  2381. }
  2382. if (p_ws->wl_outputs.is_empty()) {
  2383. DEBUG_LOG_WAYLAND_THREAD("Window has no output associated, returning buffer scale of 1.");
  2384. return 1;
  2385. }
  2386. // TODO: Cache value?
  2387. int max_size = 1;
  2388. // ================================ IMPORTANT =================================
  2389. // NOTE: Due to a Godot limitation, we can't really rescale the whole UI yet.
  2390. // Because of this reason, all platforms have resorted to forcing the highest
  2391. // scale possible of a system on any window, despite of what screen it's onto.
  2392. // On this backend everything's already in place for dynamic window scale
  2393. // handling, but in the meantime we'll just select the biggest _global_ output.
  2394. // To restore dynamic scale selection, simply iterate over `p_ws->wl_outputs`
  2395. // instead.
  2396. for (struct wl_output *wl_output : p_ws->registry->wl_outputs) {
  2397. ScreenState *ss = wl_output_get_screen_state(wl_output);
  2398. if (ss && ss->pending_data.scale > max_size) {
  2399. // NOTE: For some mystical reason, wl_output.done is emitted _after_ windows
  2400. // get resized but the scale event gets sent _before_ that. I'm still leaning
  2401. // towards the idea that rescaling when a window gets a resolution change is a
  2402. // pretty good approach, but this means that we'll have to use the screen data
  2403. // before it's "committed".
  2404. // FIXME: Use the committed data. Somehow.
  2405. max_size = ss->pending_data.scale;
  2406. }
  2407. }
  2408. return max_size;
  2409. }
  2410. double WaylandThread::window_state_get_scale_factor(WindowState *p_ws) {
  2411. ERR_FAIL_NULL_V(p_ws, 1);
  2412. if (p_ws->fractional_scale > 0) {
  2413. // The fractional scale amount takes priority.
  2414. return p_ws->fractional_scale;
  2415. }
  2416. return p_ws->buffer_scale;
  2417. }
  2418. void WaylandThread::window_state_update_size(WindowState *p_ws, int p_width, int p_height) {
  2419. ERR_FAIL_NULL(p_ws);
  2420. int preferred_buffer_scale = window_state_get_preferred_buffer_scale(p_ws);
  2421. bool using_fractional = p_ws->preferred_fractional_scale > 0;
  2422. // If neither is true we no-op.
  2423. bool scale_changed = true;
  2424. bool size_changed = true;
  2425. if (p_ws->rect.size.width != p_width || p_ws->rect.size.height != p_height) {
  2426. p_ws->rect.size.width = p_width;
  2427. p_ws->rect.size.height = p_height;
  2428. size_changed = true;
  2429. }
  2430. if (using_fractional && p_ws->fractional_scale != p_ws->preferred_fractional_scale) {
  2431. p_ws->fractional_scale = p_ws->preferred_fractional_scale;
  2432. scale_changed = true;
  2433. }
  2434. if (p_ws->buffer_scale != preferred_buffer_scale) {
  2435. // The buffer scale is always important, even if we use frac scaling.
  2436. p_ws->buffer_scale = preferred_buffer_scale;
  2437. p_ws->buffer_scale_changed = true;
  2438. if (!using_fractional) {
  2439. // We don't bother updating everything else if it's turned on though.
  2440. scale_changed = true;
  2441. }
  2442. }
  2443. if (p_ws->wl_surface) {
  2444. if (p_ws->wp_viewport) {
  2445. wp_viewport_set_destination(p_ws->wp_viewport, p_width, p_height);
  2446. }
  2447. if (p_ws->xdg_surface) {
  2448. xdg_surface_set_window_geometry(p_ws->xdg_surface, 0, 0, p_width, p_height);
  2449. }
  2450. }
  2451. #ifdef LIBDECOR_ENABLED
  2452. if (p_ws->libdecor_frame) {
  2453. struct libdecor_state *state = libdecor_state_new(p_width, p_height);
  2454. libdecor_frame_commit(p_ws->libdecor_frame, state, p_ws->pending_libdecor_configuration);
  2455. libdecor_state_free(state);
  2456. p_ws->pending_libdecor_configuration = nullptr;
  2457. }
  2458. #endif
  2459. if (size_changed || scale_changed) {
  2460. double win_scale = window_state_get_scale_factor(p_ws);
  2461. Size2i scaled_size = scale_vector2i(p_ws->rect.size, win_scale);
  2462. if (using_fractional) {
  2463. 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));
  2464. } else {
  2465. 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));
  2466. }
  2467. // FIXME: Actually resize the hint instead of centering it.
  2468. p_ws->wayland_thread->pointer_set_hint(scaled_size / 2);
  2469. Ref<WindowRectMessage> rect_msg;
  2470. rect_msg.instantiate();
  2471. rect_msg->id = p_ws->id;
  2472. rect_msg->rect.position = scale_vector2i(p_ws->rect.position, win_scale);
  2473. rect_msg->rect.size = scaled_size;
  2474. p_ws->wayland_thread->push_message(rect_msg);
  2475. }
  2476. if (scale_changed) {
  2477. Ref<WindowEventMessage> dpi_msg;
  2478. dpi_msg.instantiate();
  2479. dpi_msg->id = p_ws->id;
  2480. dpi_msg->event = DisplayServer::WINDOW_EVENT_DPI_CHANGE;
  2481. p_ws->wayland_thread->push_message(dpi_msg);
  2482. }
  2483. }
  2484. // Scales a vector according to wp_fractional_scale's rules, where coordinates
  2485. // must be scaled with away from zero half-rounding.
  2486. Vector2i WaylandThread::scale_vector2i(const Vector2i &p_vector, double p_amount) {
  2487. // This snippet is tiny, I know, but this is done a lot.
  2488. int x = round(p_vector.x * p_amount);
  2489. int y = round(p_vector.y * p_amount);
  2490. return Vector2i(x, y);
  2491. }
  2492. void WaylandThread::seat_state_unlock_pointer(SeatState *p_ss) {
  2493. ERR_FAIL_NULL(p_ss);
  2494. if (p_ss->wl_pointer == nullptr) {
  2495. return;
  2496. }
  2497. if (p_ss->wp_locked_pointer) {
  2498. zwp_locked_pointer_v1_destroy(p_ss->wp_locked_pointer);
  2499. p_ss->wp_locked_pointer = nullptr;
  2500. }
  2501. if (p_ss->wp_confined_pointer) {
  2502. zwp_confined_pointer_v1_destroy(p_ss->wp_confined_pointer);
  2503. p_ss->wp_confined_pointer = nullptr;
  2504. }
  2505. }
  2506. void WaylandThread::seat_state_lock_pointer(SeatState *p_ss) {
  2507. ERR_FAIL_NULL(p_ss);
  2508. if (p_ss->wl_pointer == nullptr) {
  2509. return;
  2510. }
  2511. if (registry.wp_pointer_constraints == nullptr) {
  2512. return;
  2513. }
  2514. if (p_ss->wp_locked_pointer == nullptr) {
  2515. struct wl_surface *locked_surface = window_get_wl_surface(p_ss->pointer_data.last_pointed_id);
  2516. ERR_FAIL_NULL(locked_surface);
  2517. 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);
  2518. }
  2519. }
  2520. void WaylandThread::seat_state_set_hint(SeatState *p_ss, int p_x, int p_y) {
  2521. if (p_ss->wp_locked_pointer == nullptr) {
  2522. return;
  2523. }
  2524. 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));
  2525. }
  2526. void WaylandThread::seat_state_confine_pointer(SeatState *p_ss) {
  2527. ERR_FAIL_NULL(p_ss);
  2528. if (p_ss->wl_pointer == nullptr) {
  2529. return;
  2530. }
  2531. if (registry.wp_pointer_constraints == nullptr) {
  2532. return;
  2533. }
  2534. if (p_ss->wp_confined_pointer == nullptr) {
  2535. struct wl_surface *confined_surface = window_get_wl_surface(p_ss->pointer_data.last_pointed_id);
  2536. ERR_FAIL_NULL(confined_surface);
  2537. 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);
  2538. }
  2539. }
  2540. void WaylandThread::seat_state_update_cursor(SeatState *p_ss) {
  2541. ERR_FAIL_NULL(p_ss);
  2542. WaylandThread *thread = p_ss->wayland_thread;
  2543. ERR_FAIL_NULL(p_ss->wayland_thread);
  2544. if (!p_ss->wl_pointer || !p_ss->cursor_surface) {
  2545. return;
  2546. }
  2547. // NOTE: Those values are valid by default and will hide the cursor when
  2548. // unchanged.
  2549. struct wl_buffer *cursor_buffer = nullptr;
  2550. uint32_t hotspot_x = 0;
  2551. uint32_t hotspot_y = 0;
  2552. int scale = 1;
  2553. if (thread->cursor_visible) {
  2554. DisplayServer::CursorShape shape = thread->cursor_shape;
  2555. struct CustomCursor *custom_cursor = thread->custom_cursors.getptr(shape);
  2556. if (custom_cursor) {
  2557. cursor_buffer = custom_cursor->wl_buffer;
  2558. hotspot_x = custom_cursor->hotspot.x;
  2559. hotspot_y = custom_cursor->hotspot.y;
  2560. // We can't really reasonably scale custom cursors, so we'll let the
  2561. // compositor do it for us (badly).
  2562. scale = 1;
  2563. } else {
  2564. struct wl_cursor *wl_cursor = thread->wl_cursors[shape];
  2565. if (!wl_cursor) {
  2566. return;
  2567. }
  2568. int frame_idx = 0;
  2569. if (wl_cursor->image_count > 1) {
  2570. // The cursor is animated.
  2571. frame_idx = wl_cursor_frame(wl_cursor, p_ss->cursor_time_ms);
  2572. if (!p_ss->cursor_frame_callback) {
  2573. // Since it's animated, we'll re-update it the next frame.
  2574. p_ss->cursor_frame_callback = wl_surface_frame(p_ss->cursor_surface);
  2575. wl_callback_add_listener(p_ss->cursor_frame_callback, &cursor_frame_callback_listener, p_ss);
  2576. }
  2577. }
  2578. struct wl_cursor_image *wl_cursor_image = wl_cursor->images[frame_idx];
  2579. scale = thread->cursor_scale;
  2580. cursor_buffer = wl_cursor_image_get_buffer(wl_cursor_image);
  2581. // As the surface's buffer is scaled (thus the surface is smaller) and the
  2582. // hotspot must be expressed in surface-local coordinates, we need to scale
  2583. // it down accordingly.
  2584. hotspot_x = wl_cursor_image->hotspot_x / scale;
  2585. hotspot_y = wl_cursor_image->hotspot_y / scale;
  2586. }
  2587. }
  2588. wl_pointer_set_cursor(p_ss->wl_pointer, p_ss->pointer_enter_serial, p_ss->cursor_surface, hotspot_x, hotspot_y);
  2589. wl_surface_set_buffer_scale(p_ss->cursor_surface, scale);
  2590. wl_surface_attach(p_ss->cursor_surface, cursor_buffer, 0, 0);
  2591. wl_surface_damage_buffer(p_ss->cursor_surface, 0, 0, INT_MAX, INT_MAX);
  2592. wl_surface_commit(p_ss->cursor_surface);
  2593. }
  2594. void WaylandThread::seat_state_echo_keys(SeatState *p_ss) {
  2595. ERR_FAIL_NULL(p_ss);
  2596. if (p_ss->wl_keyboard == nullptr) {
  2597. return;
  2598. }
  2599. // TODO: Comment and document out properly this block of code.
  2600. // In short, this implements key repeating.
  2601. if (p_ss->repeat_key_delay_msec && p_ss->repeating_keycode != XKB_KEYCODE_INVALID) {
  2602. uint64_t current_ticks = OS::get_singleton()->get_ticks_msec();
  2603. uint64_t delayed_start_ticks = p_ss->last_repeat_start_msec + p_ss->repeat_start_delay_msec;
  2604. if (p_ss->last_repeat_msec < delayed_start_ticks) {
  2605. p_ss->last_repeat_msec = delayed_start_ticks;
  2606. }
  2607. if (current_ticks >= delayed_start_ticks) {
  2608. uint64_t ticks_delta = current_ticks - p_ss->last_repeat_msec;
  2609. int keys_amount = (ticks_delta / p_ss->repeat_key_delay_msec);
  2610. for (int i = 0; i < keys_amount; i++) {
  2611. Ref<InputEventKey> k = _seat_state_get_key_event(p_ss, p_ss->repeating_keycode, true);
  2612. if (k.is_null()) {
  2613. continue;
  2614. }
  2615. k->set_echo(true);
  2616. Ref<InputEventKey> uk = _seat_state_get_unstuck_key_event(p_ss, p_ss->repeating_keycode, true, k->get_keycode());
  2617. if (uk.is_valid()) {
  2618. Input::get_singleton()->parse_input_event(uk);
  2619. }
  2620. Input::get_singleton()->parse_input_event(k);
  2621. }
  2622. p_ss->last_repeat_msec += ticks_delta - (ticks_delta % p_ss->repeat_key_delay_msec);
  2623. }
  2624. }
  2625. }
  2626. void WaylandThread::push_message(Ref<Message> message) {
  2627. messages.push_back(message);
  2628. }
  2629. bool WaylandThread::has_message() {
  2630. return messages.front() != nullptr;
  2631. }
  2632. Ref<WaylandThread::Message> WaylandThread::pop_message() {
  2633. if (messages.front() != nullptr) {
  2634. Ref<Message> msg = messages.front()->get();
  2635. messages.pop_front();
  2636. return msg;
  2637. }
  2638. // This method should only be called if `has_messages` returns true but if
  2639. // that isn't the case we'll just return an invalid `Ref`. After all, due to
  2640. // its `InputEvent`-like interface, we still have to dynamically cast and check
  2641. // the `Ref`'s validity anyways.
  2642. return Ref<Message>();
  2643. }
  2644. void WaylandThread::window_create(DisplayServer::WindowID p_window_id, int p_width, int p_height) {
  2645. ERR_FAIL_COND(windows.has(p_window_id));
  2646. WindowState &ws = windows[p_window_id];
  2647. ws.id = p_window_id;
  2648. ws.registry = &registry;
  2649. ws.wayland_thread = this;
  2650. ws.rect.size.width = p_width;
  2651. ws.rect.size.height = p_height;
  2652. ws.wl_surface = wl_compositor_create_surface(registry.wl_compositor);
  2653. wl_proxy_tag_godot((struct wl_proxy *)ws.wl_surface);
  2654. wl_surface_add_listener(ws.wl_surface, &wl_surface_listener, &ws);
  2655. if (registry.wp_viewporter) {
  2656. ws.wp_viewport = wp_viewporter_get_viewport(registry.wp_viewporter, ws.wl_surface);
  2657. if (registry.wp_fractional_scale_manager) {
  2658. ws.wp_fractional_scale = wp_fractional_scale_manager_v1_get_fractional_scale(registry.wp_fractional_scale_manager, ws.wl_surface);
  2659. wp_fractional_scale_v1_add_listener(ws.wp_fractional_scale, &wp_fractional_scale_listener, &ws);
  2660. }
  2661. }
  2662. bool decorated = false;
  2663. #ifdef LIBDECOR_ENABLED
  2664. if (!decorated && libdecor_context) {
  2665. ws.libdecor_frame = libdecor_decorate(libdecor_context, ws.wl_surface, (struct libdecor_frame_interface *)&libdecor_frame_interface, &ws);
  2666. libdecor_frame_map(ws.libdecor_frame);
  2667. decorated = true;
  2668. }
  2669. #endif
  2670. if (!decorated) {
  2671. // libdecor has failed loading or is disabled, we shall handle xdg_toplevel
  2672. // creation and decoration ourselves (and by decorating for now I just mean
  2673. // asking for SSDs and hoping for the best).
  2674. ws.xdg_surface = xdg_wm_base_get_xdg_surface(registry.xdg_wm_base, ws.wl_surface);
  2675. xdg_surface_add_listener(ws.xdg_surface, &xdg_surface_listener, &ws);
  2676. ws.xdg_toplevel = xdg_surface_get_toplevel(ws.xdg_surface);
  2677. xdg_toplevel_add_listener(ws.xdg_toplevel, &xdg_toplevel_listener, &ws);
  2678. if (registry.xdg_decoration_manager) {
  2679. ws.xdg_toplevel_decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(registry.xdg_decoration_manager, ws.xdg_toplevel);
  2680. zxdg_toplevel_decoration_v1_add_listener(ws.xdg_toplevel_decoration, &xdg_toplevel_decoration_listener, &ws);
  2681. decorated = true;
  2682. }
  2683. }
  2684. ws.frame_callback = wl_surface_frame(ws.wl_surface);
  2685. wl_callback_add_listener(ws.frame_callback, &frame_wl_callback_listener, &ws);
  2686. if (registry.xdg_exporter_v2) {
  2687. ws.xdg_exported_v2 = zxdg_exporter_v2_export_toplevel(registry.xdg_exporter_v2, ws.wl_surface);
  2688. zxdg_exported_v2_add_listener(ws.xdg_exported_v2, &xdg_exported_v2_listener, &ws);
  2689. } else if (registry.xdg_exporter_v1) {
  2690. ws.xdg_exported_v1 = zxdg_exporter_v1_export(registry.xdg_exporter_v1, ws.wl_surface);
  2691. zxdg_exported_v1_add_listener(ws.xdg_exported_v1, &xdg_exported_v1_listener, &ws);
  2692. }
  2693. wl_surface_commit(ws.wl_surface);
  2694. // Wait for the surface to be configured before continuing.
  2695. wl_display_roundtrip(wl_display);
  2696. window_state_update_size(&ws, ws.rect.size.width, ws.rect.size.height);
  2697. }
  2698. void WaylandThread::window_create_popup(DisplayServer::WindowID p_window_id, DisplayServer::WindowID p_parent_id, Rect2i p_rect) {
  2699. ERR_FAIL_COND(windows.has(p_window_id));
  2700. ERR_FAIL_COND(!windows.has(p_parent_id));
  2701. WindowState &ws = windows[p_window_id];
  2702. WindowState &parent = windows[p_parent_id];
  2703. double parent_scale = window_state_get_scale_factor(&parent);
  2704. p_rect.position = scale_vector2i(p_rect.position, 1.0 / parent_scale);
  2705. p_rect.size = scale_vector2i(p_rect.size, 1.0 / parent_scale);
  2706. ws.id = p_window_id;
  2707. ws.parent_id = p_parent_id;
  2708. ws.registry = &registry;
  2709. ws.wayland_thread = this;
  2710. ws.rect = p_rect;
  2711. ws.wl_surface = wl_compositor_create_surface(registry.wl_compositor);
  2712. wl_proxy_tag_godot((struct wl_proxy *)ws.wl_surface);
  2713. wl_surface_add_listener(ws.wl_surface, &wl_surface_listener, &ws);
  2714. if (registry.wp_viewporter) {
  2715. ws.wp_viewport = wp_viewporter_get_viewport(registry.wp_viewporter, ws.wl_surface);
  2716. if (registry.wp_fractional_scale_manager) {
  2717. ws.wp_fractional_scale = wp_fractional_scale_manager_v1_get_fractional_scale(registry.wp_fractional_scale_manager, ws.wl_surface);
  2718. wp_fractional_scale_v1_add_listener(ws.wp_fractional_scale, &wp_fractional_scale_listener, &ws);
  2719. }
  2720. }
  2721. ws.xdg_surface = xdg_wm_base_get_xdg_surface(registry.xdg_wm_base, ws.wl_surface);
  2722. xdg_surface_add_listener(ws.xdg_surface, &xdg_surface_listener, &ws);
  2723. Rect2i positioner_rect;
  2724. positioner_rect.size = parent.rect.size;
  2725. struct xdg_surface *parent_xdg_surface = parent.xdg_surface;
  2726. Point2i offset = ws.rect.position - parent.rect.position;
  2727. #ifdef LIBDECOR_ENABLED
  2728. if (!parent_xdg_surface && parent.libdecor_frame) {
  2729. parent_xdg_surface = libdecor_frame_get_xdg_surface(parent.libdecor_frame);
  2730. int corner_x = 0;
  2731. int corner_y = 0;
  2732. libdecor_frame_translate_coordinate(parent.libdecor_frame, 0, 0, &corner_x, &corner_y);
  2733. positioner_rect.position.x = corner_x;
  2734. positioner_rect.position.y = corner_y;
  2735. positioner_rect.size.width -= corner_x;
  2736. positioner_rect.size.height -= corner_y;
  2737. }
  2738. #endif
  2739. ERR_FAIL_NULL(parent_xdg_surface);
  2740. struct xdg_positioner *xdg_positioner = xdg_wm_base_create_positioner(registry.xdg_wm_base);
  2741. xdg_positioner_set_size(xdg_positioner, ws.rect.size.width, ws.rect.size.height);
  2742. xdg_positioner_set_anchor(xdg_positioner, XDG_POSITIONER_ANCHOR_TOP_LEFT);
  2743. xdg_positioner_set_gravity(xdg_positioner, XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT);
  2744. xdg_positioner_set_constraint_adjustment(xdg_positioner, XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X | XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_Y | XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_X | XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_Y);
  2745. xdg_positioner_set_anchor_rect(xdg_positioner, positioner_rect.position.x, positioner_rect.position.y, positioner_rect.size.width, positioner_rect.size.height);
  2746. xdg_positioner_set_offset(xdg_positioner, offset.x, offset.y);
  2747. ws.xdg_popup = xdg_surface_get_popup(ws.xdg_surface, parent_xdg_surface, xdg_positioner);
  2748. xdg_popup_add_listener(ws.xdg_popup, &xdg_popup_listener, &ws);
  2749. xdg_positioner_destroy(xdg_positioner);
  2750. ws.frame_callback = wl_surface_frame(ws.wl_surface);
  2751. wl_callback_add_listener(ws.frame_callback, &frame_wl_callback_listener, &ws);
  2752. wl_surface_commit(ws.wl_surface);
  2753. // Wait for the surface to be configured before continuing.
  2754. wl_display_roundtrip(wl_display);
  2755. }
  2756. void WaylandThread::window_destroy(DisplayServer::WindowID p_window_id) {
  2757. ERR_FAIL_COND(!windows.has(p_window_id));
  2758. WindowState &ws = windows[p_window_id];
  2759. if (ws.xdg_popup) {
  2760. xdg_popup_destroy(ws.xdg_popup);
  2761. }
  2762. if (ws.xdg_toplevel_decoration) {
  2763. zxdg_toplevel_decoration_v1_destroy(ws.xdg_toplevel_decoration);
  2764. }
  2765. if (ws.xdg_toplevel) {
  2766. xdg_toplevel_destroy(ws.xdg_toplevel);
  2767. }
  2768. #ifdef LIBDECOR_ENABLED
  2769. if (ws.libdecor_frame) {
  2770. libdecor_frame_unref(ws.libdecor_frame);
  2771. }
  2772. #endif // LIBDECOR_ENABLED
  2773. if (ws.wp_fractional_scale) {
  2774. wp_fractional_scale_v1_destroy(ws.wp_fractional_scale);
  2775. }
  2776. if (ws.wp_viewport) {
  2777. wp_viewport_destroy(ws.wp_viewport);
  2778. }
  2779. if (ws.frame_callback) {
  2780. wl_callback_destroy(ws.frame_callback);
  2781. }
  2782. if (ws.xdg_surface) {
  2783. xdg_surface_destroy(ws.xdg_surface);
  2784. }
  2785. if (ws.wl_surface) {
  2786. wl_surface_destroy(ws.wl_surface);
  2787. }
  2788. // Before continuing, let's handle any leftover event that might still refer to
  2789. // this window.
  2790. wl_display_roundtrip(wl_display);
  2791. // We can already clean up here, we're done.
  2792. windows.erase(p_window_id);
  2793. }
  2794. struct wl_surface *WaylandThread::window_get_wl_surface(DisplayServer::WindowID p_window_id) const {
  2795. ERR_FAIL_COND_V(!windows.has(p_window_id), nullptr);
  2796. const WindowState &ws = windows[p_window_id];
  2797. return ws.wl_surface;
  2798. }
  2799. WaylandThread::WindowState *WaylandThread::window_get_state(DisplayServer::WindowID p_window_id) {
  2800. return windows.getptr(p_window_id);
  2801. }
  2802. void WaylandThread::beep() const {
  2803. if (registry.xdg_system_bell) {
  2804. xdg_system_bell_v1_ring(registry.xdg_system_bell, nullptr);
  2805. }
  2806. }
  2807. void WaylandThread::window_start_drag(DisplayServer::WindowID p_window_id) {
  2808. ERR_FAIL_COND(!windows.has(p_window_id));
  2809. WindowState &ws = windows[p_window_id];
  2810. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  2811. if (ss && ws.xdg_toplevel) {
  2812. xdg_toplevel_move(ws.xdg_toplevel, ss->wl_seat, ss->pointer_data.button_serial);
  2813. }
  2814. #ifdef LIBDECOR_ENABLED
  2815. if (ws.libdecor_frame) {
  2816. libdecor_frame_move(ws.libdecor_frame, ss->wl_seat, ss->pointer_data.button_serial);
  2817. }
  2818. #endif
  2819. }
  2820. void WaylandThread::window_start_resize(DisplayServer::WindowResizeEdge p_edge, DisplayServer::WindowID p_window) {
  2821. ERR_FAIL_COND(!windows.has(p_window));
  2822. WindowState &ws = windows[p_window];
  2823. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  2824. if (ss && ws.xdg_toplevel) {
  2825. xdg_toplevel_resize_edge edge = XDG_TOPLEVEL_RESIZE_EDGE_NONE;
  2826. switch (p_edge) {
  2827. case DisplayServer::WINDOW_EDGE_TOP_LEFT: {
  2828. edge = XDG_TOPLEVEL_RESIZE_EDGE_TOP_LEFT;
  2829. } break;
  2830. case DisplayServer::WINDOW_EDGE_TOP: {
  2831. edge = XDG_TOPLEVEL_RESIZE_EDGE_TOP;
  2832. } break;
  2833. case DisplayServer::WINDOW_EDGE_TOP_RIGHT: {
  2834. edge = XDG_TOPLEVEL_RESIZE_EDGE_TOP_RIGHT;
  2835. } break;
  2836. case DisplayServer::WINDOW_EDGE_LEFT: {
  2837. edge = XDG_TOPLEVEL_RESIZE_EDGE_LEFT;
  2838. } break;
  2839. case DisplayServer::WINDOW_EDGE_RIGHT: {
  2840. edge = XDG_TOPLEVEL_RESIZE_EDGE_RIGHT;
  2841. } break;
  2842. case DisplayServer::WINDOW_EDGE_BOTTOM_LEFT: {
  2843. edge = XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_LEFT;
  2844. } break;
  2845. case DisplayServer::WINDOW_EDGE_BOTTOM: {
  2846. edge = XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM;
  2847. } break;
  2848. case DisplayServer::WINDOW_EDGE_BOTTOM_RIGHT: {
  2849. edge = XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_RIGHT;
  2850. } break;
  2851. default:
  2852. break;
  2853. }
  2854. xdg_toplevel_resize(ws.xdg_toplevel, ss->wl_seat, ss->pointer_data.button_serial, edge);
  2855. }
  2856. #ifdef LIBDECOR_ENABLED
  2857. if (ws.libdecor_frame) {
  2858. libdecor_resize_edge edge = LIBDECOR_RESIZE_EDGE_NONE;
  2859. switch (p_edge) {
  2860. case DisplayServer::WINDOW_EDGE_TOP_LEFT: {
  2861. edge = LIBDECOR_RESIZE_EDGE_TOP_LEFT;
  2862. } break;
  2863. case DisplayServer::WINDOW_EDGE_TOP: {
  2864. edge = LIBDECOR_RESIZE_EDGE_TOP;
  2865. } break;
  2866. case DisplayServer::WINDOW_EDGE_TOP_RIGHT: {
  2867. edge = LIBDECOR_RESIZE_EDGE_TOP_RIGHT;
  2868. } break;
  2869. case DisplayServer::WINDOW_EDGE_LEFT: {
  2870. edge = LIBDECOR_RESIZE_EDGE_LEFT;
  2871. } break;
  2872. case DisplayServer::WINDOW_EDGE_RIGHT: {
  2873. edge = LIBDECOR_RESIZE_EDGE_RIGHT;
  2874. } break;
  2875. case DisplayServer::WINDOW_EDGE_BOTTOM_LEFT: {
  2876. edge = LIBDECOR_RESIZE_EDGE_BOTTOM_LEFT;
  2877. } break;
  2878. case DisplayServer::WINDOW_EDGE_BOTTOM: {
  2879. edge = LIBDECOR_RESIZE_EDGE_BOTTOM;
  2880. } break;
  2881. case DisplayServer::WINDOW_EDGE_BOTTOM_RIGHT: {
  2882. edge = LIBDECOR_RESIZE_EDGE_BOTTOM_RIGHT;
  2883. } break;
  2884. default:
  2885. break;
  2886. }
  2887. libdecor_frame_resize(ws.libdecor_frame, ss->wl_seat, ss->pointer_data.button_serial, edge);
  2888. }
  2889. #endif
  2890. }
  2891. void WaylandThread::window_set_parent(DisplayServer::WindowID p_window_id, DisplayServer::WindowID p_parent_id) {
  2892. ERR_FAIL_COND(!windows.has(p_window_id));
  2893. ERR_FAIL_COND(!windows.has(p_parent_id));
  2894. WindowState &child = windows[p_window_id];
  2895. child.parent_id = p_parent_id;
  2896. WindowState &parent = windows[p_parent_id];
  2897. // NOTE: We can't really unparent as, at the time of writing, libdecor
  2898. // segfaults when trying to set a null parent. Hopefully unparenting is not
  2899. // that common. Bummer.
  2900. #ifdef LIBDECOR_ENABLED
  2901. if (child.libdecor_frame && parent.libdecor_frame) {
  2902. libdecor_frame_set_parent(child.libdecor_frame, parent.libdecor_frame);
  2903. return;
  2904. }
  2905. #endif
  2906. if (child.xdg_toplevel && parent.xdg_toplevel) {
  2907. xdg_toplevel_set_parent(child.xdg_toplevel, parent.xdg_toplevel);
  2908. }
  2909. }
  2910. void WaylandThread::window_set_max_size(DisplayServer::WindowID p_window_id, const Size2i &p_size) {
  2911. ERR_FAIL_COND(!windows.has(p_window_id));
  2912. WindowState &ws = windows[p_window_id];
  2913. Vector2i logical_max_size = p_size / window_state_get_scale_factor(&ws);
  2914. if (ws.wl_surface && ws.xdg_toplevel) {
  2915. xdg_toplevel_set_max_size(ws.xdg_toplevel, logical_max_size.width, logical_max_size.height);
  2916. }
  2917. #ifdef LIBDECOR_ENABLED
  2918. if (ws.libdecor_frame) {
  2919. libdecor_frame_set_max_content_size(ws.libdecor_frame, logical_max_size.width, logical_max_size.height);
  2920. }
  2921. // FIXME: I'm not sure whether we have to commit the surface for this to apply.
  2922. #endif
  2923. }
  2924. void WaylandThread::window_set_min_size(DisplayServer::WindowID p_window_id, const Size2i &p_size) {
  2925. ERR_FAIL_COND(!windows.has(p_window_id));
  2926. WindowState &ws = windows[p_window_id];
  2927. Size2i logical_min_size = p_size / window_state_get_scale_factor(&ws);
  2928. if (ws.wl_surface && ws.xdg_toplevel) {
  2929. xdg_toplevel_set_min_size(ws.xdg_toplevel, logical_min_size.width, logical_min_size.height);
  2930. }
  2931. #ifdef LIBDECOR_ENABLED
  2932. if (ws.libdecor_frame) {
  2933. libdecor_frame_set_min_content_size(ws.libdecor_frame, logical_min_size.width, logical_min_size.height);
  2934. }
  2935. // FIXME: I'm not sure whether we have to commit the surface for this to apply.
  2936. #endif
  2937. }
  2938. bool WaylandThread::window_can_set_mode(DisplayServer::WindowID p_window_id, DisplayServer::WindowMode p_window_mode) const {
  2939. ERR_FAIL_COND_V(!windows.has(p_window_id), false);
  2940. const WindowState &ws = windows[p_window_id];
  2941. switch (p_window_mode) {
  2942. case DisplayServer::WINDOW_MODE_WINDOWED: {
  2943. // Looks like it's guaranteed.
  2944. return true;
  2945. };
  2946. case DisplayServer::WINDOW_MODE_MINIMIZED: {
  2947. #ifdef LIBDECOR_ENABLED
  2948. if (ws.libdecor_frame) {
  2949. return libdecor_frame_has_capability(ws.libdecor_frame, LIBDECOR_ACTION_MINIMIZE);
  2950. }
  2951. #endif // LIBDECOR_ENABLED
  2952. return ws.can_minimize;
  2953. };
  2954. case DisplayServer::WINDOW_MODE_MAXIMIZED: {
  2955. // NOTE: libdecor doesn't seem to have a maximize capability query?
  2956. // The fact that there's a fullscreen one makes me suspicious.
  2957. return ws.can_maximize;
  2958. };
  2959. case DisplayServer::WINDOW_MODE_FULLSCREEN:
  2960. case DisplayServer::WINDOW_MODE_EXCLUSIVE_FULLSCREEN: {
  2961. #ifdef LIBDECOR_ENABLED
  2962. if (ws.libdecor_frame) {
  2963. return libdecor_frame_has_capability(ws.libdecor_frame, LIBDECOR_ACTION_FULLSCREEN);
  2964. }
  2965. #endif // LIBDECOR_ENABLED
  2966. return ws.can_fullscreen;
  2967. };
  2968. }
  2969. return false;
  2970. }
  2971. void WaylandThread::window_try_set_mode(DisplayServer::WindowID p_window_id, DisplayServer::WindowMode p_window_mode) {
  2972. ERR_FAIL_COND(!windows.has(p_window_id));
  2973. WindowState &ws = windows[p_window_id];
  2974. if (ws.mode == p_window_mode) {
  2975. return;
  2976. }
  2977. // Don't waste time with hidden windows and whatnot. Behave like it worked.
  2978. #ifdef LIBDECOR_ENABLED
  2979. if ((!ws.wl_surface || !ws.xdg_toplevel) && !ws.libdecor_frame) {
  2980. #else
  2981. if (!ws.wl_surface || !ws.xdg_toplevel) {
  2982. #endif // LIBDECOR_ENABLED
  2983. ws.mode = p_window_mode;
  2984. return;
  2985. }
  2986. // Return back to a windowed state so that we can apply what the user asked.
  2987. switch (ws.mode) {
  2988. case DisplayServer::WINDOW_MODE_WINDOWED: {
  2989. // Do nothing.
  2990. } break;
  2991. case DisplayServer::WINDOW_MODE_MINIMIZED: {
  2992. // We can't do much according to the xdg_shell protocol. I have no idea
  2993. // whether this implies that we should return or who knows what. For now
  2994. // we'll do nothing.
  2995. // TODO: Test this properly.
  2996. } break;
  2997. case DisplayServer::WINDOW_MODE_MAXIMIZED: {
  2998. // Try to unmaximize. This isn't garaunteed to work actually, so we'll have
  2999. // to check whether something changed.
  3000. if (ws.xdg_toplevel) {
  3001. xdg_toplevel_unset_maximized(ws.xdg_toplevel);
  3002. }
  3003. #ifdef LIBDECOR_ENABLED
  3004. if (ws.libdecor_frame) {
  3005. libdecor_frame_unset_maximized(ws.libdecor_frame);
  3006. }
  3007. #endif // LIBDECOR_ENABLED
  3008. } break;
  3009. case DisplayServer::WINDOW_MODE_FULLSCREEN:
  3010. case DisplayServer::WINDOW_MODE_EXCLUSIVE_FULLSCREEN: {
  3011. // Same thing as above, unset fullscreen and check later if it worked.
  3012. if (ws.xdg_toplevel) {
  3013. xdg_toplevel_unset_fullscreen(ws.xdg_toplevel);
  3014. }
  3015. #ifdef LIBDECOR_ENABLED
  3016. if (ws.libdecor_frame) {
  3017. libdecor_frame_unset_fullscreen(ws.libdecor_frame);
  3018. }
  3019. #endif // LIBDECOR_ENABLED
  3020. } break;
  3021. }
  3022. // Wait for a configure event and hope that something changed.
  3023. wl_display_roundtrip(wl_display);
  3024. if (ws.mode != DisplayServer::WINDOW_MODE_WINDOWED) {
  3025. // The compositor refused our "normalization" request. It'd be useless or
  3026. // unpredictable to attempt setting a new state. We're done.
  3027. return;
  3028. }
  3029. // Ask the compositor to set the state indicated by the new mode.
  3030. switch (p_window_mode) {
  3031. case DisplayServer::WINDOW_MODE_WINDOWED: {
  3032. // Do nothing. We're already windowed.
  3033. } break;
  3034. case DisplayServer::WINDOW_MODE_MINIMIZED: {
  3035. if (!window_can_set_mode(p_window_id, p_window_mode)) {
  3036. // Minimization is special (read below). Better not mess with it if the
  3037. // compositor explicitly announces that it doesn't support it.
  3038. break;
  3039. }
  3040. if (ws.xdg_toplevel) {
  3041. xdg_toplevel_set_minimized(ws.xdg_toplevel);
  3042. }
  3043. #ifdef LIBDECOR_ENABLED
  3044. if (ws.libdecor_frame) {
  3045. libdecor_frame_set_minimized(ws.libdecor_frame);
  3046. }
  3047. #endif // LIBDECOR_ENABLED
  3048. // We have no way to actually detect this state, so we'll have to report it
  3049. // manually to the engine (hoping that it worked). In the worst case it'll
  3050. // get reset by the next configure event.
  3051. ws.mode = DisplayServer::WINDOW_MODE_MINIMIZED;
  3052. } break;
  3053. case DisplayServer::WINDOW_MODE_MAXIMIZED: {
  3054. if (ws.xdg_toplevel) {
  3055. xdg_toplevel_set_maximized(ws.xdg_toplevel);
  3056. }
  3057. #ifdef LIBDECOR_ENABLED
  3058. if (ws.libdecor_frame) {
  3059. libdecor_frame_set_maximized(ws.libdecor_frame);
  3060. }
  3061. #endif // LIBDECOR_ENABLED
  3062. } break;
  3063. case DisplayServer::WINDOW_MODE_FULLSCREEN:
  3064. case DisplayServer::WINDOW_MODE_EXCLUSIVE_FULLSCREEN: {
  3065. if (ws.xdg_toplevel) {
  3066. xdg_toplevel_set_fullscreen(ws.xdg_toplevel, nullptr);
  3067. }
  3068. #ifdef LIBDECOR_ENABLED
  3069. if (ws.libdecor_frame) {
  3070. libdecor_frame_set_fullscreen(ws.libdecor_frame, nullptr);
  3071. }
  3072. #endif // LIBDECOR_ENABLED
  3073. } break;
  3074. default: {
  3075. } break;
  3076. }
  3077. }
  3078. void WaylandThread::window_set_borderless(DisplayServer::WindowID p_window_id, bool p_borderless) {
  3079. ERR_FAIL_COND(!windows.has(p_window_id));
  3080. WindowState &ws = windows[p_window_id];
  3081. if (ws.xdg_toplevel_decoration) {
  3082. if (p_borderless) {
  3083. // We implement borderless windows by simply asking the compositor to let
  3084. // us handle decorations (we don't).
  3085. zxdg_toplevel_decoration_v1_set_mode(ws.xdg_toplevel_decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE);
  3086. } else {
  3087. zxdg_toplevel_decoration_v1_set_mode(ws.xdg_toplevel_decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
  3088. }
  3089. }
  3090. #ifdef LIBDECOR_ENABLED
  3091. if (ws.libdecor_frame) {
  3092. bool visible_current = libdecor_frame_is_visible(ws.libdecor_frame);
  3093. bool visible_target = !p_borderless;
  3094. // NOTE: We have to do this otherwise we trip on a libdecor bug where it's
  3095. // possible to destroy the frame more than once, by setting the visibility
  3096. // to false multiple times and thus crashing.
  3097. if (visible_current != visible_target) {
  3098. print_verbose(vformat("Setting libdecor frame visibility to %d", visible_target));
  3099. libdecor_frame_set_visibility(ws.libdecor_frame, visible_target);
  3100. }
  3101. }
  3102. #endif // LIBDECOR_ENABLED
  3103. }
  3104. void WaylandThread::window_set_title(DisplayServer::WindowID p_window_id, const String &p_title) {
  3105. ERR_FAIL_COND(!windows.has(p_window_id));
  3106. WindowState &ws = windows[p_window_id];
  3107. #ifdef LIBDECOR_ENABLED
  3108. if (ws.libdecor_frame) {
  3109. libdecor_frame_set_title(ws.libdecor_frame, p_title.utf8().get_data());
  3110. }
  3111. #endif // LIBDECOR_ENABLE
  3112. if (ws.xdg_toplevel) {
  3113. xdg_toplevel_set_title(ws.xdg_toplevel, p_title.utf8().get_data());
  3114. }
  3115. }
  3116. void WaylandThread::window_set_app_id(DisplayServer::WindowID p_window_id, const String &p_app_id) {
  3117. ERR_FAIL_COND(!windows.has(p_window_id));
  3118. WindowState &ws = windows[p_window_id];
  3119. #ifdef LIBDECOR_ENABLED
  3120. if (ws.libdecor_frame) {
  3121. libdecor_frame_set_app_id(ws.libdecor_frame, p_app_id.utf8().get_data());
  3122. return;
  3123. }
  3124. #endif // LIBDECOR_ENABLED
  3125. if (ws.xdg_toplevel) {
  3126. xdg_toplevel_set_app_id(ws.xdg_toplevel, p_app_id.utf8().get_data());
  3127. return;
  3128. }
  3129. }
  3130. DisplayServer::WindowMode WaylandThread::window_get_mode(DisplayServer::WindowID p_window_id) const {
  3131. ERR_FAIL_COND_V(!windows.has(p_window_id), DisplayServer::WINDOW_MODE_WINDOWED);
  3132. const WindowState &ws = windows[p_window_id];
  3133. return ws.mode;
  3134. }
  3135. void WaylandThread::window_request_attention(DisplayServer::WindowID p_window_id) {
  3136. ERR_FAIL_COND(!windows.has(p_window_id));
  3137. WindowState &ws = windows[p_window_id];
  3138. if (registry.xdg_activation) {
  3139. // Window attention requests are done through the XDG activation protocol.
  3140. xdg_activation_token_v1 *xdg_activation_token = xdg_activation_v1_get_activation_token(registry.xdg_activation);
  3141. xdg_activation_token_v1_add_listener(xdg_activation_token, &xdg_activation_token_listener, &ws);
  3142. xdg_activation_token_v1_commit(xdg_activation_token);
  3143. }
  3144. }
  3145. void WaylandThread::window_set_idle_inhibition(DisplayServer::WindowID p_window_id, bool p_enable) {
  3146. ERR_FAIL_COND(!windows.has(p_window_id));
  3147. WindowState &ws = windows[p_window_id];
  3148. if (p_enable) {
  3149. if (ws.registry->wp_idle_inhibit_manager && !ws.wp_idle_inhibitor) {
  3150. ERR_FAIL_NULL(ws.wl_surface);
  3151. ws.wp_idle_inhibitor = zwp_idle_inhibit_manager_v1_create_inhibitor(ws.registry->wp_idle_inhibit_manager, ws.wl_surface);
  3152. }
  3153. } else {
  3154. if (ws.wp_idle_inhibitor) {
  3155. zwp_idle_inhibitor_v1_destroy(ws.wp_idle_inhibitor);
  3156. ws.wp_idle_inhibitor = nullptr;
  3157. }
  3158. }
  3159. }
  3160. bool WaylandThread::window_get_idle_inhibition(DisplayServer::WindowID p_window_id) const {
  3161. ERR_FAIL_COND_V(!windows.has(p_window_id), false);
  3162. const WindowState &ws = windows[p_window_id];
  3163. return ws.wp_idle_inhibitor != nullptr;
  3164. }
  3165. WaylandThread::ScreenData WaylandThread::screen_get_data(int p_screen) const {
  3166. ERR_FAIL_INDEX_V(p_screen, registry.wl_outputs.size(), ScreenData());
  3167. return wl_output_get_screen_state(registry.wl_outputs.get(p_screen))->data;
  3168. }
  3169. int WaylandThread::get_screen_count() const {
  3170. return registry.wl_outputs.size();
  3171. }
  3172. DisplayServer::WindowID WaylandThread::pointer_get_pointed_window_id() const {
  3173. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3174. if (ss) {
  3175. // Let's determine the most recently used tablet tool.
  3176. TabletToolState *max_ts = nullptr;
  3177. for (struct zwp_tablet_tool_v2 *tool : ss->tablet_tools) {
  3178. TabletToolState *ts = wp_tablet_tool_get_state(tool);
  3179. ERR_CONTINUE(ts == nullptr);
  3180. TabletToolData &td = ts->data;
  3181. if (!max_ts) {
  3182. max_ts = ts;
  3183. continue;
  3184. }
  3185. if (MAX(td.button_time, td.motion_time) > MAX(max_ts->data.button_time, max_ts->data.motion_time)) {
  3186. max_ts = ts;
  3187. }
  3188. }
  3189. const PointerData &pd = ss->pointer_data;
  3190. if (max_ts) {
  3191. TabletToolData &td = max_ts->data;
  3192. if (MAX(td.button_time, td.motion_time) > MAX(pd.button_time, pd.motion_time)) {
  3193. return td.proximal_id;
  3194. }
  3195. }
  3196. return ss->pointer_data.pointed_id;
  3197. }
  3198. return DisplayServer::INVALID_WINDOW_ID;
  3199. }
  3200. DisplayServer::WindowID WaylandThread::pointer_get_last_pointed_window_id() const {
  3201. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3202. if (ss) {
  3203. // Let's determine the most recently used tablet tool.
  3204. TabletToolState *max_ts = nullptr;
  3205. for (struct zwp_tablet_tool_v2 *tool : ss->tablet_tools) {
  3206. TabletToolState *ts = wp_tablet_tool_get_state(tool);
  3207. ERR_CONTINUE(ts == nullptr);
  3208. TabletToolData &td = ts->data;
  3209. if (!max_ts) {
  3210. max_ts = ts;
  3211. continue;
  3212. }
  3213. if (MAX(td.button_time, td.motion_time) > MAX(max_ts->data.button_time, max_ts->data.motion_time)) {
  3214. max_ts = ts;
  3215. }
  3216. }
  3217. const PointerData &pd = ss->pointer_data;
  3218. if (max_ts) {
  3219. TabletToolData &td = max_ts->data;
  3220. if (MAX(td.button_time, td.motion_time) > MAX(pd.button_time, pd.motion_time)) {
  3221. return td.last_proximal_id;
  3222. }
  3223. }
  3224. return ss->pointer_data.last_pointed_id;
  3225. }
  3226. return DisplayServer::INVALID_WINDOW_ID;
  3227. }
  3228. void WaylandThread::pointer_set_constraint(PointerConstraint p_constraint) {
  3229. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3230. if (ss) {
  3231. seat_state_unlock_pointer(ss);
  3232. if (p_constraint == PointerConstraint::LOCKED) {
  3233. seat_state_lock_pointer(ss);
  3234. } else if (p_constraint == PointerConstraint::CONFINED) {
  3235. seat_state_confine_pointer(ss);
  3236. }
  3237. }
  3238. pointer_constraint = p_constraint;
  3239. }
  3240. void WaylandThread::pointer_set_hint(const Point2i &p_hint) {
  3241. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3242. if (!ss) {
  3243. return;
  3244. }
  3245. WindowState *ws = window_get_state(ss->pointer_data.pointed_id);
  3246. int hint_x = 0;
  3247. int hint_y = 0;
  3248. if (ws) {
  3249. // NOTE: It looks like it's not really recommended to convert from
  3250. // "godot-space" to "wayland-space" and in general I received mixed feelings
  3251. // discussing about this. I'm not really sure about the maths behind this but,
  3252. // oh well, we're setting a cursor hint. ¯\_(ツ)_/¯
  3253. // See: https://oftc.irclog.whitequark.org/wayland/2023-08-23#1692756914-1692816818
  3254. hint_x = round(p_hint.x / window_state_get_scale_factor(ws));
  3255. hint_y = round(p_hint.y / window_state_get_scale_factor(ws));
  3256. }
  3257. if (ss) {
  3258. seat_state_set_hint(ss, hint_x, hint_y);
  3259. }
  3260. }
  3261. WaylandThread::PointerConstraint WaylandThread::pointer_get_constraint() const {
  3262. return pointer_constraint;
  3263. }
  3264. BitField<MouseButtonMask> WaylandThread::pointer_get_button_mask() const {
  3265. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3266. if (ss) {
  3267. return ss->pointer_data.pressed_button_mask;
  3268. }
  3269. return BitField<MouseButtonMask>();
  3270. }
  3271. Error WaylandThread::init() {
  3272. #ifdef SOWRAP_ENABLED
  3273. #ifdef DEBUG_ENABLED
  3274. int dylibloader_verbose = 1;
  3275. #else
  3276. int dylibloader_verbose = 0;
  3277. #endif // DEBUG_ENABLED
  3278. if (initialize_wayland_client(dylibloader_verbose) != 0) {
  3279. WARN_PRINT("Can't load the Wayland client library.");
  3280. return ERR_CANT_CREATE;
  3281. }
  3282. if (initialize_wayland_cursor(dylibloader_verbose) != 0) {
  3283. WARN_PRINT("Can't load the Wayland cursor library.");
  3284. return ERR_CANT_CREATE;
  3285. }
  3286. if (initialize_xkbcommon(dylibloader_verbose) != 0) {
  3287. WARN_PRINT("Can't load the XKBcommon library.");
  3288. return ERR_CANT_CREATE;
  3289. }
  3290. #endif // SOWRAP_ENABLED
  3291. KeyMappingXKB::initialize();
  3292. wl_display = wl_display_connect(nullptr);
  3293. ERR_FAIL_NULL_V_MSG(wl_display, ERR_CANT_CREATE, "Can't connect to a Wayland display.");
  3294. thread_data.wl_display = wl_display;
  3295. events_thread.start(_poll_events_thread, &thread_data);
  3296. wl_registry = wl_display_get_registry(wl_display);
  3297. ERR_FAIL_NULL_V_MSG(wl_registry, ERR_UNAVAILABLE, "Can't obtain the Wayland registry global.");
  3298. registry.wayland_thread = this;
  3299. wl_registry_add_listener(wl_registry, &wl_registry_listener, &registry);
  3300. // Wait for registry to get notified from the compositor.
  3301. wl_display_roundtrip(wl_display);
  3302. ERR_FAIL_NULL_V_MSG(registry.wl_shm, ERR_UNAVAILABLE, "Can't obtain the Wayland shared memory global.");
  3303. ERR_FAIL_NULL_V_MSG(registry.wl_compositor, ERR_UNAVAILABLE, "Can't obtain the Wayland compositor global.");
  3304. ERR_FAIL_NULL_V_MSG(registry.xdg_wm_base, ERR_UNAVAILABLE, "Can't obtain the Wayland XDG shell global.");
  3305. if (!registry.xdg_decoration_manager) {
  3306. #ifdef LIBDECOR_ENABLED
  3307. WARN_PRINT("Can't obtain the XDG decoration manager. Libdecor will be used for drawing CSDs, if available.");
  3308. #else
  3309. WARN_PRINT("Can't obtain the XDG decoration manager. Decorations won't show up.");
  3310. #endif // LIBDECOR_ENABLED
  3311. }
  3312. if (!registry.xdg_activation) {
  3313. WARN_PRINT("Can't obtain the XDG activation global. Attention requesting won't work!");
  3314. }
  3315. #ifndef DBUS_ENABLED
  3316. if (!registry.wp_idle_inhibit_manager) {
  3317. WARN_PRINT("Can't obtain the idle inhibition manager. The screen might turn off even after calling screen_set_keep_on()!");
  3318. }
  3319. #endif // DBUS_ENABLED
  3320. // Wait for seat capabilities.
  3321. wl_display_roundtrip(wl_display);
  3322. #ifdef LIBDECOR_ENABLED
  3323. bool libdecor_found = true;
  3324. #ifdef SOWRAP_ENABLED
  3325. if (initialize_libdecor(dylibloader_verbose) != 0) {
  3326. libdecor_found = false;
  3327. }
  3328. #endif // SOWRAP_ENABLED
  3329. if (libdecor_found) {
  3330. libdecor_context = libdecor_new(wl_display, (struct libdecor_interface *)&libdecor_interface);
  3331. } else {
  3332. print_verbose("libdecor not found. Client-side decorations disabled.");
  3333. }
  3334. #endif // LIBDECOR_ENABLED
  3335. cursor_theme_name = OS::get_singleton()->get_environment("XCURSOR_THEME");
  3336. unscaled_cursor_size = OS::get_singleton()->get_environment("XCURSOR_SIZE").to_int();
  3337. if (unscaled_cursor_size <= 0) {
  3338. print_verbose("Detected invalid cursor size preference, defaulting to 24.");
  3339. unscaled_cursor_size = 24;
  3340. }
  3341. // NOTE: The scale is useful here as it might've been updated by _update_scale.
  3342. bool cursor_theme_loaded = _load_cursor_theme(unscaled_cursor_size * cursor_scale);
  3343. if (!cursor_theme_loaded) {
  3344. return ERR_CANT_CREATE;
  3345. }
  3346. // Update the cursor.
  3347. cursor_set_shape(DisplayServer::CURSOR_ARROW);
  3348. initialized = true;
  3349. return OK;
  3350. }
  3351. void WaylandThread::cursor_set_visible(bool p_visible) {
  3352. cursor_visible = p_visible;
  3353. for (struct wl_seat *wl_seat : registry.wl_seats) {
  3354. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  3355. ERR_FAIL_NULL(ss);
  3356. seat_state_update_cursor(ss);
  3357. }
  3358. }
  3359. void WaylandThread::cursor_set_shape(DisplayServer::CursorShape p_cursor_shape) {
  3360. cursor_shape = p_cursor_shape;
  3361. for (struct wl_seat *wl_seat : registry.wl_seats) {
  3362. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  3363. ERR_FAIL_NULL(ss);
  3364. seat_state_update_cursor(ss);
  3365. }
  3366. }
  3367. void WaylandThread::cursor_shape_set_custom_image(DisplayServer::CursorShape p_cursor_shape, Ref<Image> p_image, const Point2i &p_hotspot) {
  3368. ERR_FAIL_COND(p_image.is_null());
  3369. Size2i image_size = p_image->get_size();
  3370. // NOTE: The stride is the width of the image in bytes.
  3371. unsigned int image_stride = image_size.width * 4;
  3372. unsigned int data_size = image_stride * image_size.height;
  3373. // We need a shared memory object file descriptor in order to create a
  3374. // wl_buffer through wl_shm.
  3375. int fd = WaylandThread::_allocate_shm_file(data_size);
  3376. ERR_FAIL_COND(fd == -1);
  3377. CustomCursor &cursor = custom_cursors[p_cursor_shape];
  3378. cursor.hotspot = p_hotspot;
  3379. if (cursor.wl_buffer) {
  3380. // Clean up the old Wayland buffer.
  3381. wl_buffer_destroy(cursor.wl_buffer);
  3382. }
  3383. if (cursor.buffer_data) {
  3384. // Clean up the old buffer data.
  3385. munmap(cursor.buffer_data, cursor.buffer_data_size);
  3386. }
  3387. cursor.buffer_data = (uint32_t *)mmap(nullptr, data_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  3388. cursor.buffer_data_size = data_size;
  3389. // Create the Wayland buffer.
  3390. struct wl_shm_pool *wl_shm_pool = wl_shm_create_pool(registry.wl_shm, fd, data_size);
  3391. // TODO: Make sure that WL_SHM_FORMAT_ARGB8888 format is supported. It
  3392. // technically isn't garaunteed to be supported, but I think that'd be a
  3393. // pretty unlikely thing to stumble upon.
  3394. cursor.wl_buffer = wl_shm_pool_create_buffer(wl_shm_pool, 0, image_size.width, image_size.height, image_stride, WL_SHM_FORMAT_ARGB8888);
  3395. wl_shm_pool_destroy(wl_shm_pool);
  3396. // Fill the cursor buffer with the image data.
  3397. for (unsigned int index = 0; index < (unsigned int)(image_size.width * image_size.height); index++) {
  3398. int row_index = floor(index / image_size.width);
  3399. int column_index = (index % int(image_size.width));
  3400. cursor.buffer_data[index] = p_image->get_pixel(column_index, row_index).to_argb32();
  3401. // Wayland buffers, unless specified, require associated alpha, so we'll just
  3402. // associate the alpha in-place.
  3403. uint8_t *pixel_data = (uint8_t *)&cursor.buffer_data[index];
  3404. pixel_data[0] = pixel_data[0] * pixel_data[3] / 255;
  3405. pixel_data[1] = pixel_data[1] * pixel_data[3] / 255;
  3406. pixel_data[2] = pixel_data[2] * pixel_data[3] / 255;
  3407. }
  3408. }
  3409. void WaylandThread::cursor_shape_clear_custom_image(DisplayServer::CursorShape p_cursor_shape) {
  3410. if (custom_cursors.has(p_cursor_shape)) {
  3411. CustomCursor cursor = custom_cursors[p_cursor_shape];
  3412. custom_cursors.erase(p_cursor_shape);
  3413. if (cursor.wl_buffer) {
  3414. wl_buffer_destroy(cursor.wl_buffer);
  3415. }
  3416. if (cursor.buffer_data) {
  3417. munmap(cursor.buffer_data, cursor.buffer_data_size);
  3418. }
  3419. }
  3420. }
  3421. void WaylandThread::window_set_ime_active(const bool p_active, DisplayServer::WindowID p_window_id) {
  3422. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3423. if (ss && ss->wp_text_input && ss->ime_enabled) {
  3424. if (p_active) {
  3425. ss->ime_active = true;
  3426. zwp_text_input_v3_enable(ss->wp_text_input);
  3427. 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);
  3428. } else {
  3429. ss->ime_active = false;
  3430. ss->ime_text = String();
  3431. ss->ime_text_commit = String();
  3432. ss->ime_cursor = Vector2i();
  3433. zwp_text_input_v3_disable(ss->wp_text_input);
  3434. }
  3435. zwp_text_input_v3_commit(ss->wp_text_input);
  3436. }
  3437. }
  3438. void WaylandThread::window_set_ime_position(const Point2i &p_pos, DisplayServer::WindowID p_window_id) {
  3439. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3440. if (ss && ss->wp_text_input && ss->ime_enabled) {
  3441. ss->ime_rect = Rect2i(p_pos, Size2i(1, 10));
  3442. 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);
  3443. zwp_text_input_v3_commit(ss->wp_text_input);
  3444. }
  3445. }
  3446. int WaylandThread::keyboard_get_layout_count() const {
  3447. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3448. if (ss && ss->xkb_keymap) {
  3449. return xkb_keymap_num_layouts(ss->xkb_keymap);
  3450. }
  3451. return 0;
  3452. }
  3453. int WaylandThread::keyboard_get_current_layout_index() const {
  3454. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3455. if (ss) {
  3456. return ss->current_layout_index;
  3457. }
  3458. return 0;
  3459. }
  3460. void WaylandThread::keyboard_set_current_layout_index(int p_index) {
  3461. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3462. if (ss) {
  3463. ss->current_layout_index = p_index;
  3464. }
  3465. }
  3466. String WaylandThread::keyboard_get_layout_name(int p_index) const {
  3467. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3468. if (ss && ss->xkb_keymap) {
  3469. return String::utf8(xkb_keymap_layout_get_name(ss->xkb_keymap, p_index));
  3470. }
  3471. return "";
  3472. }
  3473. Key WaylandThread::keyboard_get_key_from_physical(Key p_key) const {
  3474. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3475. if (ss && ss->xkb_state) {
  3476. xkb_keycode_t xkb_keycode = KeyMappingXKB::get_xkb_keycode(p_key);
  3477. return KeyMappingXKB::get_keycode(xkb_state_key_get_one_sym(ss->xkb_state, xkb_keycode));
  3478. }
  3479. return Key::NONE;
  3480. }
  3481. void WaylandThread::keyboard_echo_keys() {
  3482. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3483. if (ss) {
  3484. seat_state_echo_keys(ss);
  3485. }
  3486. }
  3487. void WaylandThread::selection_set_text(const String &p_text) {
  3488. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3489. if (registry.wl_data_device_manager == nullptr) {
  3490. DEBUG_LOG_WAYLAND_THREAD("Couldn't set selection, wl_data_device_manager global not available.");
  3491. return;
  3492. }
  3493. if (ss == nullptr) {
  3494. DEBUG_LOG_WAYLAND_THREAD("Couldn't set selection, current seat not set.");
  3495. return;
  3496. }
  3497. if (ss->wl_data_device == nullptr) {
  3498. DEBUG_LOG_WAYLAND_THREAD("Couldn't set selection, seat doesn't have wl_data_device.");
  3499. }
  3500. ss->selection_data = p_text.to_utf8_buffer();
  3501. if (ss->wl_data_source_selection == nullptr) {
  3502. ss->wl_data_source_selection = wl_data_device_manager_create_data_source(registry.wl_data_device_manager);
  3503. wl_data_source_add_listener(ss->wl_data_source_selection, &wl_data_source_listener, ss);
  3504. wl_data_source_offer(ss->wl_data_source_selection, "text/plain;charset=utf-8");
  3505. wl_data_source_offer(ss->wl_data_source_selection, "text/plain");
  3506. // TODO: Implement a good way of getting the latest serial from the user.
  3507. 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));
  3508. }
  3509. // Wait for the message to get to the server before continuing, otherwise the
  3510. // clipboard update might come with a delay.
  3511. wl_display_roundtrip(wl_display);
  3512. }
  3513. bool WaylandThread::selection_has_mime(const String &p_mime) const {
  3514. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3515. if (ss == nullptr) {
  3516. DEBUG_LOG_WAYLAND_THREAD("Couldn't get selection, current seat not set.");
  3517. return false;
  3518. }
  3519. OfferState *os = wl_data_offer_get_offer_state(ss->wl_data_offer_selection);
  3520. if (!os) {
  3521. return false;
  3522. }
  3523. return os->mime_types.has(p_mime);
  3524. }
  3525. Vector<uint8_t> WaylandThread::selection_get_mime(const String &p_mime) const {
  3526. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3527. if (ss == nullptr) {
  3528. DEBUG_LOG_WAYLAND_THREAD("Couldn't get selection, current seat not set.");
  3529. return Vector<uint8_t>();
  3530. }
  3531. if (ss->wl_data_source_selection) {
  3532. // We have a source so the stuff we're pasting is ours. We'll have to pass the
  3533. // data directly or we'd stall waiting for Godot (ourselves) to send us the
  3534. // data :P
  3535. OfferState *os = wl_data_offer_get_offer_state(ss->wl_data_offer_selection);
  3536. ERR_FAIL_NULL_V(os, Vector<uint8_t>());
  3537. if (os->mime_types.has(p_mime)) {
  3538. // All righty, we're offering this type. Let's just return the data as is.
  3539. return ss->selection_data;
  3540. }
  3541. // ... we don't offer that type. Oh well.
  3542. return Vector<uint8_t>();
  3543. }
  3544. return _wl_data_offer_read(wl_display, p_mime.utf8().get_data(), ss->wl_data_offer_selection);
  3545. }
  3546. bool WaylandThread::primary_has_mime(const String &p_mime) const {
  3547. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3548. if (ss == nullptr) {
  3549. DEBUG_LOG_WAYLAND_THREAD("Couldn't get selection, current seat not set.");
  3550. return false;
  3551. }
  3552. OfferState *os = wp_primary_selection_offer_get_offer_state(ss->wp_primary_selection_offer);
  3553. if (!os) {
  3554. return false;
  3555. }
  3556. return os->mime_types.has(p_mime);
  3557. }
  3558. Vector<uint8_t> WaylandThread::primary_get_mime(const String &p_mime) const {
  3559. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3560. if (ss == nullptr) {
  3561. DEBUG_LOG_WAYLAND_THREAD("Couldn't get primary, current seat not set.");
  3562. return Vector<uint8_t>();
  3563. }
  3564. if (ss->wp_primary_selection_source) {
  3565. // We have a source so the stuff we're pasting is ours. We'll have to pass the
  3566. // data directly or we'd stall waiting for Godot (ourselves) to send us the
  3567. // data :P
  3568. OfferState *os = wp_primary_selection_offer_get_offer_state(ss->wp_primary_selection_offer);
  3569. ERR_FAIL_NULL_V(os, Vector<uint8_t>());
  3570. if (os->mime_types.has(p_mime)) {
  3571. // All righty, we're offering this type. Let's just return the data as is.
  3572. return ss->selection_data;
  3573. }
  3574. // ... we don't offer that type. Oh well.
  3575. return Vector<uint8_t>();
  3576. }
  3577. return _wp_primary_selection_offer_read(wl_display, p_mime.utf8().get_data(), ss->wp_primary_selection_offer);
  3578. }
  3579. void WaylandThread::primary_set_text(const String &p_text) {
  3580. SeatState *ss = wl_seat_get_seat_state(wl_seat_current);
  3581. if (registry.wp_primary_selection_device_manager == nullptr) {
  3582. DEBUG_LOG_WAYLAND_THREAD("Couldn't set primary, protocol not available.");
  3583. return;
  3584. }
  3585. if (ss == nullptr) {
  3586. DEBUG_LOG_WAYLAND_THREAD("Couldn't set primary, current seat not set.");
  3587. return;
  3588. }
  3589. if (ss->wp_primary_selection_device == nullptr) {
  3590. DEBUG_LOG_WAYLAND_THREAD("Couldn't set primary selection, seat doesn't have wp_primary_selection_device.");
  3591. return;
  3592. }
  3593. ss->primary_data = p_text.to_utf8_buffer();
  3594. if (ss->wp_primary_selection_source == nullptr) {
  3595. ss->wp_primary_selection_source = zwp_primary_selection_device_manager_v1_create_source(registry.wp_primary_selection_device_manager);
  3596. zwp_primary_selection_source_v1_add_listener(ss->wp_primary_selection_source, &wp_primary_selection_source_listener, ss);
  3597. zwp_primary_selection_source_v1_offer(ss->wp_primary_selection_source, "text/plain;charset=utf-8");
  3598. zwp_primary_selection_source_v1_offer(ss->wp_primary_selection_source, "text/plain");
  3599. // TODO: Implement a good way of getting the latest serial from the user.
  3600. 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));
  3601. }
  3602. // Wait for the message to get to the server before continuing, otherwise the
  3603. // clipboard update might come with a delay.
  3604. wl_display_roundtrip(wl_display);
  3605. }
  3606. void WaylandThread::commit_surfaces() {
  3607. for (KeyValue<DisplayServer::WindowID, WindowState> &pair : windows) {
  3608. wl_surface_commit(pair.value.wl_surface);
  3609. }
  3610. }
  3611. void WaylandThread::set_frame() {
  3612. frame = true;
  3613. }
  3614. bool WaylandThread::get_reset_frame() {
  3615. bool old_frame = frame;
  3616. frame = false;
  3617. return old_frame;
  3618. }
  3619. // Dispatches events until a frame event is received, a window is reported as
  3620. // suspended or the timeout expires.
  3621. bool WaylandThread::wait_frame_suspend_ms(int p_timeout) {
  3622. // This is a bit of a chicken and egg thing... Looks like the main event loop
  3623. // has to call its rightfully forever-blocking poll right in between
  3624. // `wl_display_prepare_read` and `wl_display_read`. This means, that it will
  3625. // basically be guaranteed to stay stuck in a "prepare read" state, where it
  3626. // will block any other attempt at reading the display fd, such as ours. The
  3627. // solution? Let's make sure the mutex is locked (it should) and unblock the
  3628. // main thread with a roundtrip!
  3629. MutexLock mutex_lock(mutex);
  3630. wl_display_roundtrip(wl_display);
  3631. if (is_suspended()) {
  3632. // All windows are suspended! The compositor is telling us _explicitly_ that
  3633. // we don't need to draw, without letting us guess through the frame event's
  3634. // timing and stuff like that. Our job here is done.
  3635. return false;
  3636. }
  3637. if (frame) {
  3638. // We already have a frame! Probably it got there while the caller locked :D
  3639. frame = false;
  3640. return true;
  3641. }
  3642. struct pollfd poll_fd;
  3643. poll_fd.fd = wl_display_get_fd(wl_display);
  3644. poll_fd.events = POLLIN | POLLHUP;
  3645. int begin_ms = OS::get_singleton()->get_ticks_msec();
  3646. int remaining_ms = p_timeout;
  3647. while (remaining_ms > 0) {
  3648. // Empty the event queue while it's full.
  3649. while (wl_display_prepare_read(wl_display) != 0) {
  3650. if (wl_display_dispatch_pending(wl_display) == -1) {
  3651. // Oh no. We'll check and handle any display error below.
  3652. break;
  3653. }
  3654. if (is_suspended()) {
  3655. return false;
  3656. }
  3657. if (frame) {
  3658. // We had a frame event in the queue :D
  3659. frame = false;
  3660. return true;
  3661. }
  3662. }
  3663. int werror = wl_display_get_error(wl_display);
  3664. if (werror) {
  3665. if (werror == EPROTO) {
  3666. struct wl_interface *wl_interface = nullptr;
  3667. uint32_t id = 0;
  3668. int error_code = wl_display_get_protocol_error(wl_display, (const struct wl_interface **)&wl_interface, &id);
  3669. CRASH_NOW_MSG(vformat("Wayland protocol error %d on interface %s@%d.", error_code, wl_interface ? wl_interface->name : "unknown", id));
  3670. } else {
  3671. CRASH_NOW_MSG(vformat("Wayland client error code %d.", werror));
  3672. }
  3673. }
  3674. wl_display_flush(wl_display);
  3675. // Wait for the event file descriptor to have new data.
  3676. poll(&poll_fd, 1, remaining_ms);
  3677. if (poll_fd.revents | POLLIN) {
  3678. // Load the queues with fresh new data.
  3679. wl_display_read_events(wl_display);
  3680. } else {
  3681. // Oh well... Stop signaling that we want to read.
  3682. wl_display_cancel_read(wl_display);
  3683. // We've got no new events :(
  3684. // We won't even bother with checking the frame flag.
  3685. return false;
  3686. }
  3687. // Let's try dispatching now...
  3688. wl_display_dispatch_pending(wl_display);
  3689. if (is_suspended()) {
  3690. return false;
  3691. }
  3692. if (frame) {
  3693. frame = false;
  3694. return true;
  3695. }
  3696. remaining_ms -= OS::get_singleton()->get_ticks_msec() - begin_ms;
  3697. }
  3698. DEBUG_LOG_WAYLAND_THREAD("Frame timeout.");
  3699. return false;
  3700. }
  3701. uint64_t WaylandThread::window_get_last_frame_time(DisplayServer::WindowID p_window_id) const {
  3702. ERR_FAIL_COND_V(!windows.has(p_window_id), false);
  3703. return windows[p_window_id].last_frame_time;
  3704. }
  3705. bool WaylandThread::window_is_suspended(DisplayServer::WindowID p_window_id) const {
  3706. ERR_FAIL_COND_V(!windows.has(p_window_id), false);
  3707. return windows[p_window_id].suspended;
  3708. }
  3709. bool WaylandThread::is_suspended() const {
  3710. for (const KeyValue<DisplayServer::WindowID, WindowState> &E : windows) {
  3711. if (!E.value.suspended) {
  3712. return false;
  3713. }
  3714. }
  3715. return true;
  3716. }
  3717. void WaylandThread::destroy() {
  3718. if (!initialized) {
  3719. return;
  3720. }
  3721. if (wl_display && events_thread.is_started()) {
  3722. thread_data.thread_done.set();
  3723. // By sending a roundtrip message we're unblocking the polling thread so that
  3724. // it can realize that it's done and also handle every event that's left.
  3725. wl_display_roundtrip(wl_display);
  3726. events_thread.wait_to_finish();
  3727. }
  3728. for (KeyValue<DisplayServer::WindowID, WindowState> &pair : windows) {
  3729. WindowState &ws = pair.value;
  3730. if (ws.wp_fractional_scale) {
  3731. wp_fractional_scale_v1_destroy(ws.wp_fractional_scale);
  3732. }
  3733. if (ws.wp_viewport) {
  3734. wp_viewport_destroy(ws.wp_viewport);
  3735. }
  3736. if (ws.frame_callback) {
  3737. wl_callback_destroy(ws.frame_callback);
  3738. }
  3739. #ifdef LIBDECOR_ENABLED
  3740. if (ws.libdecor_frame) {
  3741. libdecor_frame_close(ws.libdecor_frame);
  3742. }
  3743. #endif // LIBDECOR_ENABLED
  3744. if (ws.xdg_toplevel) {
  3745. xdg_toplevel_destroy(ws.xdg_toplevel);
  3746. }
  3747. if (ws.xdg_surface) {
  3748. xdg_surface_destroy(ws.xdg_surface);
  3749. }
  3750. if (ws.wl_surface) {
  3751. wl_surface_destroy(ws.wl_surface);
  3752. }
  3753. }
  3754. for (struct wl_seat *wl_seat : registry.wl_seats) {
  3755. SeatState *ss = wl_seat_get_seat_state(wl_seat);
  3756. ERR_FAIL_NULL(ss);
  3757. wl_seat_destroy(wl_seat);
  3758. xkb_context_unref(ss->xkb_context);
  3759. xkb_state_unref(ss->xkb_state);
  3760. xkb_keymap_unref(ss->xkb_keymap);
  3761. if (ss->wl_keyboard) {
  3762. wl_keyboard_destroy(ss->wl_keyboard);
  3763. }
  3764. if (ss->keymap_buffer) {
  3765. munmap((void *)ss->keymap_buffer, ss->keymap_buffer_size);
  3766. }
  3767. if (ss->wl_pointer) {
  3768. wl_pointer_destroy(ss->wl_pointer);
  3769. }
  3770. if (ss->cursor_frame_callback) {
  3771. // We don't need to set a null userdata for safety as the thread is done.
  3772. wl_callback_destroy(ss->cursor_frame_callback);
  3773. }
  3774. if (ss->cursor_surface) {
  3775. wl_surface_destroy(ss->cursor_surface);
  3776. }
  3777. if (ss->wl_data_device) {
  3778. wl_data_device_destroy(ss->wl_data_device);
  3779. }
  3780. if (ss->wp_relative_pointer) {
  3781. zwp_relative_pointer_v1_destroy(ss->wp_relative_pointer);
  3782. }
  3783. if (ss->wp_locked_pointer) {
  3784. zwp_locked_pointer_v1_destroy(ss->wp_locked_pointer);
  3785. }
  3786. if (ss->wp_confined_pointer) {
  3787. zwp_confined_pointer_v1_destroy(ss->wp_confined_pointer);
  3788. }
  3789. if (ss->wp_tablet_seat) {
  3790. zwp_tablet_seat_v2_destroy(ss->wp_tablet_seat);
  3791. }
  3792. for (struct zwp_tablet_tool_v2 *tool : ss->tablet_tools) {
  3793. TabletToolState *state = wp_tablet_tool_get_state(tool);
  3794. if (state) {
  3795. memdelete(state);
  3796. }
  3797. zwp_tablet_tool_v2_destroy(tool);
  3798. }
  3799. memdelete(ss);
  3800. }
  3801. for (struct wl_output *wl_output : registry.wl_outputs) {
  3802. ERR_FAIL_NULL(wl_output);
  3803. memdelete(wl_output_get_screen_state(wl_output));
  3804. wl_output_destroy(wl_output);
  3805. }
  3806. if (wl_cursor_theme) {
  3807. wl_cursor_theme_destroy(wl_cursor_theme);
  3808. }
  3809. if (registry.wp_idle_inhibit_manager) {
  3810. zwp_idle_inhibit_manager_v1_destroy(registry.wp_idle_inhibit_manager);
  3811. }
  3812. if (registry.wp_pointer_constraints) {
  3813. zwp_pointer_constraints_v1_destroy(registry.wp_pointer_constraints);
  3814. }
  3815. if (registry.wp_pointer_gestures) {
  3816. zwp_pointer_gestures_v1_destroy(registry.wp_pointer_gestures);
  3817. }
  3818. if (registry.wp_relative_pointer_manager) {
  3819. zwp_relative_pointer_manager_v1_destroy(registry.wp_relative_pointer_manager);
  3820. }
  3821. if (registry.xdg_activation) {
  3822. xdg_activation_v1_destroy(registry.xdg_activation);
  3823. }
  3824. if (registry.xdg_system_bell) {
  3825. xdg_system_bell_v1_destroy(registry.xdg_system_bell);
  3826. }
  3827. if (registry.xdg_decoration_manager) {
  3828. zxdg_decoration_manager_v1_destroy(registry.xdg_decoration_manager);
  3829. }
  3830. if (registry.wp_fractional_scale_manager) {
  3831. wp_fractional_scale_manager_v1_destroy(registry.wp_fractional_scale_manager);
  3832. }
  3833. if (registry.wp_viewporter) {
  3834. wp_viewporter_destroy(registry.wp_viewporter);
  3835. }
  3836. if (registry.xdg_wm_base) {
  3837. xdg_wm_base_destroy(registry.xdg_wm_base);
  3838. }
  3839. // NOTE: Deprecated.
  3840. if (registry.xdg_exporter_v1) {
  3841. zxdg_exporter_v1_destroy(registry.xdg_exporter_v1);
  3842. }
  3843. if (registry.xdg_exporter_v2) {
  3844. zxdg_exporter_v2_destroy(registry.xdg_exporter_v2);
  3845. }
  3846. if (registry.wl_shm) {
  3847. wl_shm_destroy(registry.wl_shm);
  3848. }
  3849. if (registry.wl_compositor) {
  3850. wl_compositor_destroy(registry.wl_compositor);
  3851. }
  3852. if (wl_registry) {
  3853. wl_registry_destroy(wl_registry);
  3854. }
  3855. if (wl_display) {
  3856. wl_display_disconnect(wl_display);
  3857. }
  3858. }
  3859. #endif // WAYLAND_ENABLED