wayland_thread.cpp 174 KB

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