wayland_thread.cpp 162 KB

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