wayland_thread.cpp 137 KB

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