wayland_thread.cpp 156 KB

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