wayland_thread.cpp 131 KB

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