wayland_thread.cpp 128 KB

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