wayland_thread.cpp 159 KB

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