wayland_thread.cpp 142 KB

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