wayland_thread.cpp 127 KB

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