SDL_video.c 173 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. // The high-level video driver subsystem
  20. #include "SDL_sysvideo.h"
  21. #include "SDL_egl_c.h"
  22. #include "SDL_surface_c.h"
  23. #include "SDL_pixels_c.h"
  24. #include "SDL_rect_c.h"
  25. #include "SDL_video_c.h"
  26. #include "../events/SDL_events_c.h"
  27. #include "../SDL_hints_c.h"
  28. #include "../SDL_properties_c.h"
  29. #include "../timer/SDL_timer_c.h"
  30. #include "../camera/SDL_camera_c.h"
  31. #include "../render/SDL_sysrender.h"
  32. #ifdef SDL_VIDEO_OPENGL
  33. #include <SDL3/SDL_opengl.h>
  34. #endif // SDL_VIDEO_OPENGL
  35. #if defined(SDL_VIDEO_OPENGL_ES) && !defined(SDL_VIDEO_OPENGL)
  36. #include <SDL3/SDL_opengles.h>
  37. #endif // SDL_VIDEO_OPENGL_ES && !SDL_VIDEO_OPENGL
  38. // GL and GLES2 headers conflict on Linux 32 bits
  39. #if defined(SDL_VIDEO_OPENGL_ES2) && !defined(SDL_VIDEO_OPENGL)
  40. #include <SDL3/SDL_opengles2.h>
  41. #endif // SDL_VIDEO_OPENGL_ES2 && !SDL_VIDEO_OPENGL
  42. #ifndef SDL_VIDEO_OPENGL
  43. #ifndef GL_CONTEXT_RELEASE_BEHAVIOR_KHR
  44. #define GL_CONTEXT_RELEASE_BEHAVIOR_KHR 0x82FB
  45. #endif
  46. #endif
  47. #ifdef SDL_PLATFORM_EMSCRIPTEN
  48. #include <emscripten.h>
  49. #endif
  50. #ifdef SDL_PLATFORM_3DS
  51. #include <3ds.h>
  52. #endif
  53. #ifdef SDL_PLATFORM_LINUX
  54. #include <sys/types.h>
  55. #include <sys/stat.h>
  56. #include <unistd.h>
  57. #endif
  58. // Available video drivers
  59. static VideoBootStrap *bootstrap[] = {
  60. #ifdef SDL_VIDEO_DRIVER_PRIVATE
  61. &PRIVATE_bootstrap,
  62. #endif
  63. #ifdef SDL_VIDEO_DRIVER_COCOA
  64. &COCOA_bootstrap,
  65. #endif
  66. #ifdef SDL_VIDEO_DRIVER_X11
  67. #ifdef SDL_VIDEO_DRIVER_WAYLAND
  68. &Wayland_preferred_bootstrap,
  69. #endif
  70. &X11_bootstrap,
  71. #endif
  72. #ifdef SDL_VIDEO_DRIVER_WAYLAND
  73. &Wayland_bootstrap,
  74. #endif
  75. #ifdef SDL_VIDEO_DRIVER_VIVANTE
  76. &VIVANTE_bootstrap,
  77. #endif
  78. #ifdef SDL_VIDEO_DRIVER_WINDOWS
  79. &WINDOWS_bootstrap,
  80. #endif
  81. #ifdef SDL_VIDEO_DRIVER_HAIKU
  82. &HAIKU_bootstrap,
  83. #endif
  84. #ifdef SDL_VIDEO_DRIVER_UIKIT
  85. &UIKIT_bootstrap,
  86. #endif
  87. #ifdef SDL_VIDEO_DRIVER_ANDROID
  88. &Android_bootstrap,
  89. #endif
  90. #ifdef SDL_VIDEO_DRIVER_PS2
  91. &PS2_bootstrap,
  92. #endif
  93. #ifdef SDL_VIDEO_DRIVER_PSP
  94. &PSP_bootstrap,
  95. #endif
  96. #ifdef SDL_VIDEO_DRIVER_VITA
  97. &VITA_bootstrap,
  98. #endif
  99. #ifdef SDL_VIDEO_DRIVER_N3DS
  100. &N3DS_bootstrap,
  101. #endif
  102. #ifdef SDL_VIDEO_DRIVER_KMSDRM
  103. &KMSDRM_bootstrap,
  104. #endif
  105. #ifdef SDL_VIDEO_DRIVER_RISCOS
  106. &RISCOS_bootstrap,
  107. #endif
  108. #ifdef SDL_VIDEO_DRIVER_RPI
  109. &RPI_bootstrap,
  110. #endif
  111. #ifdef SDL_VIDEO_DRIVER_EMSCRIPTEN
  112. &Emscripten_bootstrap,
  113. #endif
  114. #ifdef SDL_VIDEO_DRIVER_QNX
  115. &QNX_bootstrap,
  116. #endif
  117. #ifdef SDL_VIDEO_DRIVER_NGAGE
  118. &NGAGE_bootstrap,
  119. #endif
  120. #ifdef SDL_VIDEO_DRIVER_OFFSCREEN
  121. &OFFSCREEN_bootstrap,
  122. #endif
  123. #ifdef SDL_VIDEO_DRIVER_DUMMY
  124. &DUMMY_bootstrap,
  125. #ifdef SDL_INPUT_LINUXEV
  126. &DUMMY_evdev_bootstrap,
  127. #endif
  128. #endif
  129. #ifdef SDL_VIDEO_DRIVER_OPENVR
  130. &OPENVR_bootstrap,
  131. #endif
  132. NULL
  133. };
  134. #define CHECK_WINDOW_MAGIC(window, result) \
  135. if (!_this) { \
  136. SDL_UninitializedVideo(); \
  137. return result; \
  138. } \
  139. if (!SDL_ObjectValid(window, SDL_OBJECT_TYPE_WINDOW)) { \
  140. SDL_SetError("Invalid window"); \
  141. return result; \
  142. }
  143. #define CHECK_DISPLAY_MAGIC(display, result) \
  144. if (!display) { \
  145. return result; \
  146. } \
  147. #define CHECK_WINDOW_NOT_POPUP(window, result) \
  148. if (SDL_WINDOW_IS_POPUP(window)) { \
  149. SDL_SetError("Operation invalid on popup windows"); \
  150. return result; \
  151. }
  152. #if defined(SDL_PLATFORM_MACOS) && defined(SDL_VIDEO_DRIVER_COCOA)
  153. // Support for macOS fullscreen spaces
  154. extern bool Cocoa_IsWindowInFullscreenSpace(SDL_Window *window);
  155. extern bool Cocoa_SetWindowFullscreenSpace(SDL_Window *window, bool state, bool blocking);
  156. #endif
  157. static void SDL_CheckWindowDisplayChanged(SDL_Window *window);
  158. static void SDL_CheckWindowDisplayScaleChanged(SDL_Window *window);
  159. static void SDL_CheckWindowSafeAreaChanged(SDL_Window *window);
  160. // Convenience functions for reading driver flags
  161. static bool SDL_ModeSwitchingEmulated(SDL_VideoDevice *_this)
  162. {
  163. if (_this->device_caps & VIDEO_DEVICE_CAPS_MODE_SWITCHING_EMULATED) {
  164. return true;
  165. }
  166. return false;
  167. }
  168. static bool SDL_SendsFullscreenDimensions(SDL_VideoDevice *_this)
  169. {
  170. return !!(_this->device_caps & VIDEO_DEVICE_CAPS_SENDS_FULLSCREEN_DIMENSIONS);
  171. }
  172. static bool IsFullscreenOnly(SDL_VideoDevice *_this)
  173. {
  174. return !!(_this->device_caps & VIDEO_DEVICE_CAPS_FULLSCREEN_ONLY);
  175. }
  176. static bool SDL_SendsDisplayChanges(SDL_VideoDevice *_this)
  177. {
  178. return !!(_this->device_caps & VIDEO_DEVICE_CAPS_SENDS_DISPLAY_CHANGES);
  179. }
  180. static bool SDL_DisableMouseWarpOnFullscreenTransitions(SDL_VideoDevice *_this)
  181. {
  182. return !!(_this->device_caps & VIDEO_DEVICE_CAPS_DISABLE_MOUSE_WARP_ON_FULLSCREEN_TRANSITIONS);
  183. }
  184. static bool SDL_DriverSendsHDRChanges(SDL_VideoDevice *_this)
  185. {
  186. return !!(_this->device_caps & VIDEO_DEVICE_CAPS_SENDS_HDR_CHANGES);
  187. }
  188. // Hint to treat all window ops as synchronous
  189. static bool syncHint;
  190. static void SDL_SyncHintWatcher(void *userdata, const char *name, const char *oldValue, const char *newValue)
  191. {
  192. syncHint = SDL_GetStringBoolean(newValue, false);
  193. }
  194. static void SDL_SyncIfRequired(SDL_Window *window)
  195. {
  196. if (syncHint) {
  197. SDL_SyncWindow(window);
  198. }
  199. }
  200. static void SDL_UpdateWindowHierarchy(SDL_Window *window, SDL_Window *parent)
  201. {
  202. // Unlink the window from the existing parent.
  203. if (window->parent) {
  204. if (window->next_sibling) {
  205. window->next_sibling->prev_sibling = window->prev_sibling;
  206. }
  207. if (window->prev_sibling) {
  208. window->prev_sibling->next_sibling = window->next_sibling;
  209. } else {
  210. window->parent->first_child = window->next_sibling;
  211. }
  212. window->parent = NULL;
  213. }
  214. if (parent) {
  215. window->parent = parent;
  216. window->next_sibling = parent->first_child;
  217. if (parent->first_child) {
  218. parent->first_child->prev_sibling = window;
  219. }
  220. parent->first_child = window;
  221. }
  222. }
  223. // Support for framebuffer emulation using an accelerated renderer
  224. #define SDL_PROP_WINDOW_TEXTUREDATA_POINTER "SDL.internal.window.texturedata"
  225. typedef struct
  226. {
  227. SDL_Renderer *renderer;
  228. SDL_Texture *texture;
  229. void *pixels;
  230. int pitch;
  231. int bytes_per_pixel;
  232. } SDL_WindowTextureData;
  233. static Uint32 SDL_DefaultGraphicsBackends(SDL_VideoDevice *_this)
  234. {
  235. #if (defined(SDL_VIDEO_OPENGL) && defined(SDL_PLATFORM_MACOS)) || (defined(SDL_PLATFORM_IOS) && !TARGET_OS_MACCATALYST)
  236. if (_this->GL_CreateContext) {
  237. return SDL_WINDOW_OPENGL;
  238. }
  239. #endif
  240. #if defined(SDL_VIDEO_METAL) && (TARGET_OS_MACCATALYST || defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS))
  241. if (_this->Metal_CreateView) {
  242. return SDL_WINDOW_METAL;
  243. }
  244. #endif
  245. #if defined(SDL_VIDEO_OPENGL) && defined(SDL_VIDEO_DRIVER_OPENVR)
  246. if (SDL_strcmp(_this->name, "openvr") == 0) {
  247. return SDL_WINDOW_OPENGL;
  248. }
  249. #endif
  250. return 0;
  251. }
  252. static void SDLCALL SDL_CleanupWindowTextureData(void *userdata, void *value)
  253. {
  254. SDL_WindowTextureData *data = (SDL_WindowTextureData *)value;
  255. if (data->texture) {
  256. SDL_DestroyTexture(data->texture);
  257. }
  258. if (data->renderer) {
  259. SDL_DestroyRenderer(data->renderer);
  260. }
  261. SDL_free(data->pixels);
  262. SDL_free(data);
  263. }
  264. static bool SDL_CreateWindowTexture(SDL_VideoDevice *_this, SDL_Window *window, SDL_PixelFormat *format, void **pixels, int *pitch)
  265. {
  266. SDL_PropertiesID props = SDL_GetWindowProperties(window);
  267. SDL_WindowTextureData *data = (SDL_WindowTextureData *)SDL_GetPointerProperty(props, SDL_PROP_WINDOW_TEXTUREDATA_POINTER, NULL);
  268. const bool transparent = (window->flags & SDL_WINDOW_TRANSPARENT) ? true : false;
  269. int i;
  270. int w, h;
  271. const SDL_PixelFormat *texture_formats;
  272. SDL_GetWindowSizeInPixels(window, &w, &h);
  273. if (!data) {
  274. SDL_Renderer *renderer = NULL;
  275. const char *render_driver = NULL;
  276. const char *hint;
  277. // See if there's a render driver being requested
  278. hint = SDL_GetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION);
  279. if (hint && *hint != '0' && *hint != '1' &&
  280. SDL_strcasecmp(hint, "true") != 0 &&
  281. SDL_strcasecmp(hint, "false") != 0 &&
  282. SDL_strcasecmp(hint, SDL_SOFTWARE_RENDERER) != 0) {
  283. render_driver = hint;
  284. }
  285. if (!render_driver) {
  286. hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
  287. if (hint && *hint && SDL_strcasecmp(hint, SDL_SOFTWARE_RENDERER) != 0) {
  288. render_driver = hint;
  289. }
  290. }
  291. // Check to see if there's a specific driver requested
  292. if (render_driver) {
  293. renderer = SDL_CreateRenderer(window, render_driver);
  294. if (!renderer) {
  295. // The error for this specific renderer has already been set
  296. return false;
  297. }
  298. } else {
  299. const int total = SDL_GetNumRenderDrivers();
  300. for (i = 0; i < total; ++i) {
  301. const char *name = SDL_GetRenderDriver(i);
  302. if (name && SDL_strcmp(name, SDL_SOFTWARE_RENDERER) != 0) {
  303. renderer = SDL_CreateRenderer(window, name);
  304. if (renderer) {
  305. break; // this will work.
  306. }
  307. }
  308. }
  309. if (!renderer) {
  310. return SDL_SetError("No hardware accelerated renderers available");
  311. }
  312. }
  313. SDL_assert(renderer != NULL); // should have explicitly checked this above.
  314. // Create the data after we successfully create the renderer (bug #1116)
  315. data = (SDL_WindowTextureData *)SDL_calloc(1, sizeof(*data));
  316. if (!data) {
  317. SDL_DestroyRenderer(renderer);
  318. return false;
  319. }
  320. if (!SDL_SetPointerPropertyWithCleanup(props, SDL_PROP_WINDOW_TEXTUREDATA_POINTER, data, SDL_CleanupWindowTextureData, NULL)) {
  321. SDL_DestroyRenderer(renderer);
  322. return false;
  323. }
  324. data->renderer = renderer;
  325. }
  326. texture_formats = (const SDL_PixelFormat *)SDL_GetPointerProperty(SDL_GetRendererProperties(data->renderer), SDL_PROP_RENDERER_TEXTURE_FORMATS_POINTER, NULL);
  327. if (!texture_formats) {
  328. return false;
  329. }
  330. // Free any old texture and pixel data
  331. if (data->texture) {
  332. SDL_DestroyTexture(data->texture);
  333. data->texture = NULL;
  334. }
  335. SDL_free(data->pixels);
  336. data->pixels = NULL;
  337. // Find the first format with or without an alpha channel
  338. *format = texture_formats[0];
  339. for (i = 0; texture_formats[i] != SDL_PIXELFORMAT_UNKNOWN; ++i) {
  340. SDL_PixelFormat texture_format = texture_formats[i];
  341. if (!SDL_ISPIXELFORMAT_FOURCC(texture_format) &&
  342. !SDL_ISPIXELFORMAT_10BIT(texture_format) &&
  343. !SDL_ISPIXELFORMAT_FLOAT(texture_format) &&
  344. transparent == SDL_ISPIXELFORMAT_ALPHA(texture_format)) {
  345. *format = texture_format;
  346. break;
  347. }
  348. }
  349. data->texture = SDL_CreateTexture(data->renderer, *format,
  350. SDL_TEXTUREACCESS_STREAMING,
  351. w, h);
  352. if (!data->texture) {
  353. // codechecker_false_positive [Malloc] Static analyzer doesn't realize allocated `data` is saved to SDL_PROP_WINDOW_TEXTUREDATA_POINTER and not leaked here.
  354. return false; // NOLINT(clang-analyzer-unix.Malloc)
  355. }
  356. // Create framebuffer data
  357. data->bytes_per_pixel = SDL_BYTESPERPIXEL(*format);
  358. data->pitch = (((w * data->bytes_per_pixel) + 3) & ~3);
  359. {
  360. // Make static analysis happy about potential SDL_malloc(0) calls.
  361. const size_t allocsize = (size_t)h * data->pitch;
  362. data->pixels = SDL_malloc((allocsize > 0) ? allocsize : 1);
  363. if (!data->pixels) {
  364. return false;
  365. }
  366. }
  367. *pixels = data->pixels;
  368. *pitch = data->pitch;
  369. // Make sure we're not double-scaling the viewport
  370. SDL_SetRenderViewport(data->renderer, NULL);
  371. return true;
  372. }
  373. bool SDL_SetWindowTextureVSync(SDL_VideoDevice *_this, SDL_Window *window, int vsync)
  374. {
  375. SDL_WindowTextureData *data;
  376. data = (SDL_WindowTextureData *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_TEXTUREDATA_POINTER, NULL);
  377. if (!data) {
  378. return false;
  379. }
  380. if (!data->renderer) {
  381. return false;
  382. }
  383. return SDL_SetRenderVSync(data->renderer, vsync);
  384. }
  385. static bool SDL_GetWindowTextureVSync(SDL_VideoDevice *_this, SDL_Window *window, int *vsync)
  386. {
  387. SDL_WindowTextureData *data;
  388. data = (SDL_WindowTextureData *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_TEXTUREDATA_POINTER, NULL);
  389. if (!data) {
  390. return false;
  391. }
  392. if (!data->renderer) {
  393. return false;
  394. }
  395. return SDL_GetRenderVSync(data->renderer, vsync);
  396. }
  397. static bool SDL_UpdateWindowTexture(SDL_VideoDevice *_this, SDL_Window *window, const SDL_Rect *rects, int numrects)
  398. {
  399. SDL_WindowTextureData *data;
  400. SDL_Rect rect;
  401. void *src;
  402. int w, h;
  403. SDL_GetWindowSizeInPixels(window, &w, &h);
  404. data = (SDL_WindowTextureData *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_TEXTUREDATA_POINTER, NULL);
  405. if (!data || !data->texture) {
  406. return SDL_SetError("No window texture data");
  407. }
  408. // Update a single rect that contains subrects for best DMA performance
  409. if (SDL_GetSpanEnclosingRect(w, h, numrects, rects, &rect)) {
  410. src = (void *)((Uint8 *)data->pixels +
  411. rect.y * data->pitch +
  412. rect.x * data->bytes_per_pixel);
  413. if (!SDL_UpdateTexture(data->texture, &rect, src, data->pitch)) {
  414. return false;
  415. }
  416. if (!SDL_RenderTexture(data->renderer, data->texture, NULL, NULL)) {
  417. return false;
  418. }
  419. SDL_RenderPresent(data->renderer);
  420. }
  421. return true;
  422. }
  423. static void SDL_DestroyWindowTexture(SDL_VideoDevice *_this, SDL_Window *window)
  424. {
  425. SDL_ClearProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_TEXTUREDATA_POINTER);
  426. }
  427. static SDL_VideoDevice *_this = NULL;
  428. static SDL_AtomicInt SDL_messagebox_count;
  429. static int SDLCALL cmpmodes(const void *A, const void *B)
  430. {
  431. const SDL_DisplayMode *a = (const SDL_DisplayMode *)A;
  432. const SDL_DisplayMode *b = (const SDL_DisplayMode *)B;
  433. int a_refresh_rate = (int)(a->refresh_rate * 100);
  434. int b_refresh_rate = (int)(b->refresh_rate * 100);
  435. int a_pixel_density = (int)(a->pixel_density * 100);
  436. int b_pixel_density = (int)(b->pixel_density * 100);
  437. if (a->w != b->w) {
  438. return b->w - a->w;
  439. } else if (a->h != b->h) {
  440. return b->h - a->h;
  441. } else if (SDL_BITSPERPIXEL(a->format) != SDL_BITSPERPIXEL(b->format)) {
  442. return SDL_BITSPERPIXEL(b->format) - SDL_BITSPERPIXEL(a->format);
  443. } else if (SDL_PIXELLAYOUT(a->format) != SDL_PIXELLAYOUT(b->format)) {
  444. return SDL_PIXELLAYOUT(b->format) - SDL_PIXELLAYOUT(a->format);
  445. } else if (a_refresh_rate != b_refresh_rate) {
  446. return b_refresh_rate - a_refresh_rate;
  447. } else if (a_pixel_density != b_pixel_density) {
  448. return a_pixel_density - b_pixel_density;
  449. }
  450. return 0;
  451. }
  452. static bool SDL_UninitializedVideo(void)
  453. {
  454. return SDL_SetError("Video subsystem has not been initialized");
  455. }
  456. // Deduplicated list of video bootstrap drivers.
  457. static const VideoBootStrap *deduped_bootstrap[SDL_arraysize(bootstrap) - 1];
  458. int SDL_GetNumVideoDrivers(void)
  459. {
  460. static int num_drivers = -1;
  461. if (num_drivers >= 0) {
  462. return num_drivers;
  463. }
  464. num_drivers = 0;
  465. // Build a list of unique video drivers.
  466. for (int i = 0; bootstrap[i] != NULL; ++i) {
  467. bool duplicate = false;
  468. for (int j = 0; j < i; ++j) {
  469. if (SDL_strcmp(bootstrap[i]->name, bootstrap[j]->name) == 0) {
  470. duplicate = true;
  471. break;
  472. }
  473. }
  474. if (!duplicate) {
  475. deduped_bootstrap[num_drivers++] = bootstrap[i];
  476. }
  477. }
  478. return num_drivers;
  479. }
  480. const char *SDL_GetVideoDriver(int index)
  481. {
  482. if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
  483. return deduped_bootstrap[index]->name;
  484. }
  485. return NULL;
  486. }
  487. /*
  488. * Initialize the video and event subsystems -- determine native pixel format
  489. */
  490. bool SDL_VideoInit(const char *driver_name)
  491. {
  492. SDL_VideoDevice *video;
  493. bool init_events = false;
  494. bool init_keyboard = false;
  495. bool init_mouse = false;
  496. bool init_touch = false;
  497. bool init_pen = false;
  498. int i = 0;
  499. // Check to make sure we don't overwrite '_this'
  500. if (_this) {
  501. SDL_VideoQuit();
  502. }
  503. SDL_InitTicks();
  504. // Start the event loop
  505. if (!SDL_InitSubSystem(SDL_INIT_EVENTS)) {
  506. goto pre_driver_error;
  507. }
  508. init_events = true;
  509. if (!SDL_InitKeyboard()) {
  510. goto pre_driver_error;
  511. }
  512. init_keyboard = true;
  513. if (!SDL_PreInitMouse()) {
  514. goto pre_driver_error;
  515. }
  516. init_mouse = true;
  517. if (!SDL_InitTouch()) {
  518. goto pre_driver_error;
  519. }
  520. init_touch = true;
  521. if (!SDL_InitPen()) {
  522. goto pre_driver_error;
  523. }
  524. init_pen = true;
  525. // Select the proper video driver
  526. video = NULL;
  527. if (!driver_name) {
  528. driver_name = SDL_GetHint(SDL_HINT_VIDEO_DRIVER);
  529. }
  530. if (driver_name && *driver_name != 0) {
  531. const char *driver_attempt = driver_name;
  532. while (driver_attempt && *driver_attempt != 0 && !video) {
  533. const char *driver_attempt_end = SDL_strchr(driver_attempt, ',');
  534. size_t driver_attempt_len = (driver_attempt_end) ? (driver_attempt_end - driver_attempt)
  535. : SDL_strlen(driver_attempt);
  536. for (i = 0; bootstrap[i]; ++i) {
  537. if ((driver_attempt_len == SDL_strlen(bootstrap[i]->name)) &&
  538. (SDL_strncasecmp(bootstrap[i]->name, driver_attempt, driver_attempt_len) == 0)) {
  539. video = bootstrap[i]->create();
  540. if (video) {
  541. break;
  542. }
  543. }
  544. }
  545. driver_attempt = (driver_attempt_end) ? (driver_attempt_end + 1) : NULL;
  546. }
  547. } else {
  548. for (i = 0; bootstrap[i]; ++i) {
  549. video = bootstrap[i]->create();
  550. if (video) {
  551. break;
  552. }
  553. }
  554. }
  555. if (!video) {
  556. if (driver_name) {
  557. SDL_SetError("%s not available", driver_name);
  558. goto pre_driver_error;
  559. }
  560. SDL_SetError("No available video device");
  561. goto pre_driver_error;
  562. }
  563. /* From this point on, use SDL_VideoQuit to cleanup on error, rather than
  564. pre_driver_error. */
  565. _this = video;
  566. _this->name = bootstrap[i]->name;
  567. _this->thread = SDL_GetCurrentThreadID();
  568. // Set some very sane GL defaults
  569. _this->gl_config.driver_loaded = 0;
  570. _this->gl_config.dll_handle = NULL;
  571. SDL_GL_ResetAttributes();
  572. // Initialize the video subsystem
  573. if (!_this->VideoInit(_this)) {
  574. SDL_VideoQuit();
  575. return false;
  576. }
  577. // Make sure some displays were added
  578. if (_this->num_displays == 0) {
  579. SDL_VideoQuit();
  580. return SDL_SetError("The video driver did not add any displays");
  581. }
  582. SDL_AddHintCallback(SDL_HINT_VIDEO_SYNC_WINDOW_OPERATIONS, SDL_SyncHintWatcher, NULL);
  583. /* Disable the screen saver by default. This is a change from <= 2.0.1,
  584. but most things using SDL are games or media players; you wouldn't
  585. want a screensaver to trigger if you're playing exclusively with a
  586. joystick, or passively watching a movie. Things that use SDL but
  587. function more like a normal desktop app should explicitly reenable the
  588. screensaver. */
  589. if (!SDL_GetHintBoolean(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, false)) {
  590. SDL_DisableScreenSaver();
  591. }
  592. SDL_PostInitMouse();
  593. // We're ready to go!
  594. return true;
  595. pre_driver_error:
  596. SDL_assert(_this == NULL);
  597. if (init_pen) {
  598. SDL_QuitPen();
  599. }
  600. if (init_touch) {
  601. SDL_QuitTouch();
  602. }
  603. if (init_mouse) {
  604. SDL_QuitMouse();
  605. }
  606. if (init_keyboard) {
  607. SDL_QuitKeyboard();
  608. }
  609. if (init_events) {
  610. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  611. }
  612. return false;
  613. }
  614. const char *SDL_GetCurrentVideoDriver(void)
  615. {
  616. if (!_this) {
  617. SDL_UninitializedVideo();
  618. return NULL;
  619. }
  620. return _this->name;
  621. }
  622. SDL_VideoDevice *SDL_GetVideoDevice(void)
  623. {
  624. return _this;
  625. }
  626. bool SDL_OnVideoThread(void)
  627. {
  628. return (_this && SDL_GetCurrentThreadID() == _this->thread);
  629. }
  630. void SDL_SetSystemTheme(SDL_SystemTheme theme)
  631. {
  632. if (_this && theme != _this->system_theme) {
  633. _this->system_theme = theme;
  634. SDL_SendSystemThemeChangedEvent();
  635. }
  636. }
  637. SDL_SystemTheme SDL_GetSystemTheme(void)
  638. {
  639. if (_this) {
  640. return _this->system_theme;
  641. } else {
  642. return SDL_SYSTEM_THEME_UNKNOWN;
  643. }
  644. }
  645. static void SDL_UpdateDesktopBounds(void)
  646. {
  647. SDL_Rect rect;
  648. SDL_zero(rect);
  649. SDL_DisplayID *displays = SDL_GetDisplays(NULL);
  650. if (displays) {
  651. for (int i = 0; displays[i]; ++i) {
  652. SDL_Rect bounds;
  653. if (SDL_GetDisplayBounds(displays[i], &bounds)) {
  654. if (i == 0) {
  655. SDL_copyp(&rect, &bounds);
  656. } else {
  657. SDL_GetRectUnion(&rect, &bounds, &rect);
  658. }
  659. }
  660. }
  661. SDL_free(displays);
  662. }
  663. SDL_copyp(&_this->desktop_bounds, &rect);
  664. }
  665. static void SDL_FinalizeDisplayMode(SDL_DisplayMode *mode)
  666. {
  667. // Make sure all the fields are set up correctly
  668. if (mode->pixel_density <= 0.0f) {
  669. mode->pixel_density = 1.0f;
  670. }
  671. if (mode->refresh_rate_numerator > 0) {
  672. if (mode->refresh_rate_denominator <= 0) {
  673. mode->refresh_rate_denominator = 1;
  674. }
  675. mode->refresh_rate = ((100 * (Sint64)mode->refresh_rate_numerator) / mode->refresh_rate_denominator) / 100.0f;
  676. } else {
  677. SDL_CalculateFraction(mode->refresh_rate, &mode->refresh_rate_numerator, &mode->refresh_rate_denominator);
  678. mode->refresh_rate = (int)(mode->refresh_rate * 100) / 100.0f;
  679. }
  680. }
  681. SDL_DisplayID SDL_AddBasicVideoDisplay(const SDL_DisplayMode *desktop_mode)
  682. {
  683. SDL_VideoDisplay display;
  684. SDL_zero(display);
  685. if (desktop_mode) {
  686. SDL_memcpy(&display.desktop_mode, desktop_mode, sizeof(display.desktop_mode));
  687. }
  688. return SDL_AddVideoDisplay(&display, false);
  689. }
  690. SDL_DisplayID SDL_AddVideoDisplay(const SDL_VideoDisplay *display, bool send_event)
  691. {
  692. SDL_VideoDisplay **displays, *new_display;
  693. SDL_DisplayID id;
  694. SDL_PropertiesID props;
  695. int i;
  696. new_display = (SDL_VideoDisplay *)SDL_malloc(sizeof(*new_display));
  697. if (!new_display) {
  698. return true;
  699. }
  700. displays = (SDL_VideoDisplay **)SDL_realloc(_this->displays, (_this->num_displays + 1) * sizeof(*displays));
  701. if (!displays) {
  702. SDL_free(new_display);
  703. return true;
  704. }
  705. _this->displays = displays;
  706. _this->displays[_this->num_displays++] = new_display;
  707. id = SDL_GetNextObjectID();
  708. SDL_copyp(new_display, display);
  709. new_display->id = id;
  710. new_display->device = _this;
  711. if (display->name) {
  712. new_display->name = SDL_strdup(display->name);
  713. } else {
  714. char name[32];
  715. SDL_itoa(id, name, 10);
  716. new_display->name = SDL_strdup(name);
  717. }
  718. if (new_display->content_scale == 0.0f) {
  719. new_display->content_scale = 1.0f;
  720. }
  721. new_display->desktop_mode.displayID = id;
  722. new_display->current_mode = &new_display->desktop_mode;
  723. SDL_FinalizeDisplayMode(&new_display->desktop_mode);
  724. for (i = 0; i < new_display->num_fullscreen_modes; ++i) {
  725. new_display->fullscreen_modes[i].displayID = id;
  726. }
  727. new_display->HDR.HDR_headroom = SDL_max(display->HDR.HDR_headroom, 1.0f);
  728. new_display->HDR.SDR_white_level = SDL_max(display->HDR.SDR_white_level, 1.0f);
  729. props = SDL_GetDisplayProperties(id);
  730. SDL_SetBooleanProperty(props, SDL_PROP_DISPLAY_HDR_ENABLED_BOOLEAN, new_display->HDR.HDR_headroom > 1.0f);
  731. SDL_UpdateDesktopBounds();
  732. if (send_event) {
  733. SDL_SendDisplayEvent(new_display, SDL_EVENT_DISPLAY_ADDED, 0, 0);
  734. }
  735. return id;
  736. }
  737. void SDL_OnDisplayAdded(SDL_VideoDisplay *display)
  738. {
  739. SDL_Window *window;
  740. // See if any windows have changed to the new display
  741. for (window = _this->windows; window; window = window->next) {
  742. SDL_CheckWindowDisplayChanged(window);
  743. }
  744. }
  745. void SDL_OnDisplayMoved(SDL_VideoDisplay *display)
  746. {
  747. SDL_UpdateDesktopBounds();
  748. }
  749. void SDL_DelVideoDisplay(SDL_DisplayID displayID, bool send_event)
  750. {
  751. SDL_VideoDisplay *display;
  752. int display_index = SDL_GetDisplayIndex(displayID);
  753. if (display_index < 0) {
  754. return;
  755. }
  756. display = _this->displays[display_index];
  757. if (send_event) {
  758. SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_REMOVED, 0, 0);
  759. }
  760. SDL_DestroyProperties(display->props);
  761. SDL_free(display->name);
  762. SDL_ResetFullscreenDisplayModes(display);
  763. SDL_free(display->desktop_mode.internal);
  764. display->desktop_mode.internal = NULL;
  765. SDL_free(display->internal);
  766. display->internal = NULL;
  767. SDL_free(display);
  768. if (display_index < (_this->num_displays - 1)) {
  769. SDL_memmove(&_this->displays[display_index], &_this->displays[display_index + 1], (_this->num_displays - display_index - 1) * sizeof(_this->displays[display_index]));
  770. }
  771. --_this->num_displays;
  772. SDL_UpdateDesktopBounds();
  773. }
  774. SDL_DisplayID *SDL_GetDisplays(int *count)
  775. {
  776. int i;
  777. SDL_DisplayID *displays;
  778. if (!_this) {
  779. if (count) {
  780. *count = 0;
  781. }
  782. SDL_UninitializedVideo();
  783. return NULL;
  784. }
  785. displays = (SDL_DisplayID *)SDL_malloc((_this->num_displays + 1) * sizeof(*displays));
  786. if (displays) {
  787. if (count) {
  788. *count = _this->num_displays;
  789. }
  790. for (i = 0; i < _this->num_displays; ++i) {
  791. displays[i] = _this->displays[i]->id;
  792. }
  793. displays[i] = 0;
  794. } else {
  795. if (count) {
  796. *count = 0;
  797. }
  798. }
  799. return displays;
  800. }
  801. SDL_VideoDisplay *SDL_GetVideoDisplay(SDL_DisplayID displayID)
  802. {
  803. int display_index;
  804. display_index = SDL_GetDisplayIndex(displayID);
  805. if (display_index < 0) {
  806. return NULL;
  807. }
  808. return _this->displays[display_index];
  809. }
  810. SDL_VideoDisplay *SDL_GetVideoDisplayForWindow(SDL_Window *window)
  811. {
  812. return SDL_GetVideoDisplay(SDL_GetDisplayForWindow(window));
  813. }
  814. SDL_DisplayID SDL_GetPrimaryDisplay(void)
  815. {
  816. if (!_this || _this->num_displays == 0) {
  817. SDL_UninitializedVideo();
  818. return 0;
  819. }
  820. return _this->displays[0]->id;
  821. }
  822. int SDL_GetDisplayIndex(SDL_DisplayID displayID)
  823. {
  824. int display_index;
  825. if (!_this) {
  826. SDL_UninitializedVideo();
  827. return -1;
  828. }
  829. for (display_index = 0; display_index < _this->num_displays; ++display_index) {
  830. if (displayID == _this->displays[display_index]->id) {
  831. return display_index;
  832. }
  833. }
  834. SDL_SetError("Invalid display");
  835. return -1;
  836. }
  837. SDL_DisplayData *SDL_GetDisplayDriverData(SDL_DisplayID displayID)
  838. {
  839. SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
  840. CHECK_DISPLAY_MAGIC(display, NULL);
  841. return display->internal;
  842. }
  843. SDL_DisplayData *SDL_GetDisplayDriverDataForWindow(SDL_Window *window)
  844. {
  845. return SDL_GetDisplayDriverData(SDL_GetDisplayForWindow(window));
  846. }
  847. SDL_PropertiesID SDL_GetDisplayProperties(SDL_DisplayID displayID)
  848. {
  849. SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
  850. CHECK_DISPLAY_MAGIC(display, 0);
  851. if (display->props == 0) {
  852. display->props = SDL_CreateProperties();
  853. }
  854. return display->props;
  855. }
  856. const char *SDL_GetDisplayName(SDL_DisplayID displayID)
  857. {
  858. SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
  859. CHECK_DISPLAY_MAGIC(display, NULL);
  860. return display->name;
  861. }
  862. bool SDL_GetDisplayBounds(SDL_DisplayID displayID, SDL_Rect *rect)
  863. {
  864. SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
  865. CHECK_DISPLAY_MAGIC(display, false);
  866. if (!rect) {
  867. return SDL_InvalidParamError("rect");
  868. }
  869. if (_this->GetDisplayBounds) {
  870. if (_this->GetDisplayBounds(_this, display, rect)) {
  871. return true;
  872. }
  873. }
  874. // Assume that the displays are left to right
  875. if (displayID == SDL_GetPrimaryDisplay()) {
  876. rect->x = 0;
  877. rect->y = 0;
  878. } else {
  879. SDL_GetDisplayBounds(_this->displays[SDL_GetDisplayIndex(displayID) - 1]->id, rect);
  880. rect->x += rect->w;
  881. }
  882. rect->w = display->current_mode->w;
  883. rect->h = display->current_mode->h;
  884. return true;
  885. }
  886. static int ParseDisplayUsableBoundsHint(SDL_Rect *rect)
  887. {
  888. const char *hint = SDL_GetHint(SDL_HINT_DISPLAY_USABLE_BOUNDS);
  889. return hint && (SDL_sscanf(hint, "%d,%d,%d,%d", &rect->x, &rect->y, &rect->w, &rect->h) == 4);
  890. }
  891. bool SDL_GetDisplayUsableBounds(SDL_DisplayID displayID, SDL_Rect *rect)
  892. {
  893. SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
  894. CHECK_DISPLAY_MAGIC(display, false);
  895. if (!rect) {
  896. return SDL_InvalidParamError("rect");
  897. }
  898. if (displayID == SDL_GetPrimaryDisplay() && ParseDisplayUsableBoundsHint(rect)) {
  899. return true;
  900. }
  901. if (_this->GetDisplayUsableBounds) {
  902. if (_this->GetDisplayUsableBounds(_this, display, rect)) {
  903. return true;
  904. }
  905. }
  906. // Oh well, just give the entire display bounds.
  907. return SDL_GetDisplayBounds(displayID, rect);
  908. }
  909. SDL_DisplayOrientation SDL_GetNaturalDisplayOrientation(SDL_DisplayID displayID)
  910. {
  911. SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
  912. CHECK_DISPLAY_MAGIC(display, SDL_ORIENTATION_UNKNOWN);
  913. if (display->natural_orientation != SDL_ORIENTATION_UNKNOWN) {
  914. return display->natural_orientation;
  915. } else {
  916. // Default to landscape if the driver hasn't set it
  917. return SDL_ORIENTATION_LANDSCAPE;
  918. }
  919. }
  920. SDL_DisplayOrientation SDL_GetCurrentDisplayOrientation(SDL_DisplayID displayID)
  921. {
  922. SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
  923. CHECK_DISPLAY_MAGIC(display, SDL_ORIENTATION_UNKNOWN);
  924. if (display->current_orientation != SDL_ORIENTATION_UNKNOWN) {
  925. return display->current_orientation;
  926. } else {
  927. // Default to landscape if the driver hasn't set it
  928. return SDL_ORIENTATION_LANDSCAPE;
  929. }
  930. }
  931. void SDL_SetDisplayContentScale(SDL_VideoDisplay *display, float scale)
  932. {
  933. if (scale != display->content_scale) {
  934. SDL_Window *window;
  935. display->content_scale = scale;
  936. SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED, 0, 0);
  937. // Check the windows on this display
  938. for (window = _this->windows; window; window = window->next) {
  939. if (display->id == window->last_displayID) {
  940. SDL_CheckWindowDisplayScaleChanged(window);
  941. }
  942. }
  943. }
  944. }
  945. float SDL_GetDisplayContentScale(SDL_DisplayID displayID)
  946. {
  947. SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
  948. CHECK_DISPLAY_MAGIC(display, 0.0f);
  949. return display->content_scale;
  950. }
  951. void SDL_SetWindowHDRProperties(SDL_Window *window, const SDL_HDROutputProperties *HDR, bool send_event)
  952. {
  953. if (window->HDR.HDR_headroom != HDR->HDR_headroom || window->HDR.SDR_white_level != window->HDR.SDR_white_level) {
  954. SDL_PropertiesID window_props = SDL_GetWindowProperties(window);
  955. SDL_SetFloatProperty(window_props, SDL_PROP_WINDOW_HDR_HEADROOM_FLOAT, SDL_max(HDR->HDR_headroom, 1.0f));
  956. SDL_SetFloatProperty(window_props, SDL_PROP_WINDOW_SDR_WHITE_LEVEL_FLOAT, SDL_max(HDR->SDR_white_level, 1.0f));
  957. SDL_SetBooleanProperty(window_props, SDL_PROP_WINDOW_HDR_ENABLED_BOOLEAN, HDR->HDR_headroom > 1.0f);
  958. SDL_copyp(&window->HDR, HDR);
  959. if (send_event) {
  960. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_HDR_STATE_CHANGED, HDR->HDR_headroom > 1.0f, 0);
  961. }
  962. }
  963. }
  964. void SDL_SetDisplayHDRProperties(SDL_VideoDisplay *display, const SDL_HDROutputProperties *HDR)
  965. {
  966. bool changed = false;
  967. if (HDR->SDR_white_level != display->HDR.SDR_white_level) {
  968. display->HDR.SDR_white_level = SDL_max(HDR->SDR_white_level, 1.0f);
  969. changed = true;
  970. }
  971. if (HDR->HDR_headroom != display->HDR.HDR_headroom) {
  972. display->HDR.HDR_headroom = SDL_max(HDR->HDR_headroom, 1.0f);
  973. changed = true;
  974. }
  975. SDL_copyp(&display->HDR, HDR);
  976. if (changed && !SDL_DriverSendsHDRChanges(_this)) {
  977. for (SDL_Window *w = display->device->windows; w; w = w->next) {
  978. if (SDL_GetDisplayForWindow(w) == display->id) {
  979. SDL_SetWindowHDRProperties(w, &display->HDR, true);
  980. }
  981. }
  982. }
  983. }
  984. static void SDL_UpdateFullscreenDisplayModes(SDL_VideoDisplay *display)
  985. {
  986. if (display->num_fullscreen_modes == 0 && _this->GetDisplayModes) {
  987. _this->GetDisplayModes(_this, display);
  988. }
  989. }
  990. // Return the matching mode as a pointer into our current mode list
  991. static const SDL_DisplayMode *SDL_GetFullscreenModeMatch(const SDL_DisplayMode *mode)
  992. {
  993. SDL_VideoDisplay *display;
  994. SDL_DisplayMode fullscreen_mode;
  995. if (mode->w <= 0 || mode->h <= 0) {
  996. // Use the desktop mode
  997. return NULL;
  998. }
  999. SDL_memcpy(&fullscreen_mode, mode, sizeof(fullscreen_mode));
  1000. if (fullscreen_mode.displayID == 0) {
  1001. fullscreen_mode.displayID = SDL_GetPrimaryDisplay();
  1002. }
  1003. SDL_FinalizeDisplayMode(&fullscreen_mode);
  1004. mode = NULL;
  1005. display = SDL_GetVideoDisplay(fullscreen_mode.displayID);
  1006. if (display) {
  1007. SDL_UpdateFullscreenDisplayModes(display);
  1008. // Search for an exact match
  1009. if (!mode) {
  1010. for (int i = 0; i < display->num_fullscreen_modes; ++i) {
  1011. if (SDL_memcmp(&fullscreen_mode, &display->fullscreen_modes[i], sizeof(fullscreen_mode)) == 0) {
  1012. mode = &display->fullscreen_modes[i];
  1013. break;
  1014. }
  1015. }
  1016. }
  1017. // Search for a mode with the same characteristics
  1018. if (!mode) {
  1019. for (int i = 0; i < display->num_fullscreen_modes; ++i) {
  1020. if (cmpmodes(&fullscreen_mode, &display->fullscreen_modes[i]) == 0) {
  1021. mode = &display->fullscreen_modes[i];
  1022. break;
  1023. }
  1024. }
  1025. }
  1026. }
  1027. return mode;
  1028. }
  1029. bool SDL_AddFullscreenDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode *mode)
  1030. {
  1031. SDL_DisplayMode *modes;
  1032. SDL_DisplayMode new_mode;
  1033. int i, nmodes;
  1034. // Finalize the mode for the display
  1035. SDL_memcpy(&new_mode, mode, sizeof(new_mode));
  1036. new_mode.displayID = display->id;
  1037. SDL_FinalizeDisplayMode(&new_mode);
  1038. // Make sure we don't already have the mode in the list
  1039. modes = display->fullscreen_modes;
  1040. nmodes = display->num_fullscreen_modes;
  1041. for (i = 0; i < nmodes; ++i) {
  1042. if (cmpmodes(&new_mode, &modes[i]) == 0) {
  1043. return false;
  1044. }
  1045. }
  1046. // Go ahead and add the new mode
  1047. if (nmodes == display->max_fullscreen_modes) {
  1048. modes = (SDL_DisplayMode *)SDL_malloc((display->max_fullscreen_modes + 32) * sizeof(*modes));
  1049. if (!modes) {
  1050. return false;
  1051. }
  1052. if (display->fullscreen_modes) {
  1053. // Copy the list and update the current mode pointer, if necessary.
  1054. SDL_memcpy(modes, display->fullscreen_modes, nmodes * sizeof(*modes));
  1055. for (i = 0; i < nmodes; ++i) {
  1056. if (display->current_mode == &display->fullscreen_modes[i]) {
  1057. display->current_mode = &modes[i];
  1058. }
  1059. }
  1060. SDL_free(display->fullscreen_modes);
  1061. }
  1062. display->fullscreen_modes = modes;
  1063. display->max_fullscreen_modes += 32;
  1064. }
  1065. SDL_memcpy(&modes[display->num_fullscreen_modes++], &new_mode, sizeof(new_mode));
  1066. // Re-sort video modes
  1067. SDL_qsort(display->fullscreen_modes, display->num_fullscreen_modes,
  1068. sizeof(SDL_DisplayMode), cmpmodes);
  1069. return true;
  1070. }
  1071. void SDL_ResetFullscreenDisplayModes(SDL_VideoDisplay *display)
  1072. {
  1073. int i;
  1074. for (i = display->num_fullscreen_modes; i--;) {
  1075. SDL_free(display->fullscreen_modes[i].internal);
  1076. display->fullscreen_modes[i].internal = NULL;
  1077. }
  1078. SDL_free(display->fullscreen_modes);
  1079. display->fullscreen_modes = NULL;
  1080. display->num_fullscreen_modes = 0;
  1081. display->max_fullscreen_modes = 0;
  1082. display->current_mode = &display->desktop_mode;
  1083. }
  1084. SDL_DisplayMode **SDL_GetFullscreenDisplayModes(SDL_DisplayID displayID, int *count)
  1085. {
  1086. int i;
  1087. int num_modes;
  1088. SDL_DisplayMode **result;
  1089. SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
  1090. if (count) {
  1091. *count = 0;
  1092. }
  1093. CHECK_DISPLAY_MAGIC(display, NULL);
  1094. SDL_UpdateFullscreenDisplayModes(display);
  1095. num_modes = display->num_fullscreen_modes;
  1096. result = (SDL_DisplayMode **)SDL_malloc((num_modes + 1) * sizeof(*result) + num_modes * sizeof(**result));
  1097. if (result) {
  1098. SDL_DisplayMode *modes = (SDL_DisplayMode *)((Uint8 *)result + ((num_modes + 1) * sizeof(*result)));
  1099. SDL_memcpy(modes, display->fullscreen_modes, num_modes * sizeof(*modes));
  1100. for (i = 0; i < num_modes; ++i) {
  1101. result[i] = modes++;
  1102. }
  1103. result[i] = NULL;
  1104. if (count) {
  1105. *count = num_modes;
  1106. }
  1107. } else {
  1108. if (count) {
  1109. *count = 0;
  1110. }
  1111. }
  1112. return result;
  1113. }
  1114. bool SDL_GetClosestFullscreenDisplayMode(SDL_DisplayID displayID, int w, int h, float refresh_rate, bool include_high_density_modes, SDL_DisplayMode *result)
  1115. {
  1116. if (!result) {
  1117. return SDL_InvalidParamError("closest"); // Parameter `result` is called `closest` in the header.
  1118. }
  1119. const SDL_DisplayMode *mode, *closest = NULL;
  1120. float aspect_ratio;
  1121. int i;
  1122. SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
  1123. SDL_zerop(result);
  1124. CHECK_DISPLAY_MAGIC(display, false);
  1125. if (h > 0) {
  1126. aspect_ratio = (float)w / h;
  1127. } else {
  1128. aspect_ratio = 1.0f;
  1129. }
  1130. if (refresh_rate == 0.0f) {
  1131. refresh_rate = display->desktop_mode.refresh_rate;
  1132. }
  1133. SDL_UpdateFullscreenDisplayModes(display);
  1134. for (i = 0; i < display->num_fullscreen_modes; ++i) {
  1135. mode = &display->fullscreen_modes[i];
  1136. if (w > mode->w) {
  1137. // Out of sorted modes large enough here
  1138. break;
  1139. }
  1140. if (h > mode->h) {
  1141. /* Wider, but not tall enough, due to a different aspect ratio.
  1142. * This mode must be skipped, but closer modes may still follow */
  1143. continue;
  1144. }
  1145. if (mode->pixel_density > 1.0f && !include_high_density_modes) {
  1146. continue;
  1147. }
  1148. if (closest) {
  1149. float current_aspect_ratio = (float)mode->w / mode->h;
  1150. float closest_aspect_ratio = (float)closest->w / closest->h;
  1151. if (SDL_fabsf(aspect_ratio - closest_aspect_ratio) < SDL_fabsf(aspect_ratio - current_aspect_ratio)) {
  1152. // The mode we already found has a better aspect ratio match
  1153. continue;
  1154. }
  1155. if (mode->w == closest->w && mode->h == closest->h &&
  1156. SDL_fabsf(closest->refresh_rate - refresh_rate) < SDL_fabsf(mode->refresh_rate - refresh_rate)) {
  1157. /* We already found a mode and the new mode is further from our
  1158. * refresh rate target */
  1159. continue;
  1160. }
  1161. }
  1162. closest = mode;
  1163. }
  1164. if (!closest) {
  1165. return SDL_SetError("Couldn't find any matching video modes");
  1166. }
  1167. SDL_copyp(result, closest);
  1168. return true;
  1169. }
  1170. static bool DisplayModeChanged(const SDL_DisplayMode *old_mode, const SDL_DisplayMode *new_mode)
  1171. {
  1172. return ((old_mode->displayID && old_mode->displayID != new_mode->displayID) ||
  1173. (old_mode->format && old_mode->format != new_mode->format) ||
  1174. (old_mode->w && old_mode->h && (old_mode->w != new_mode->w ||old_mode->h != new_mode->h)) ||
  1175. (old_mode->pixel_density != 0.0f && old_mode->pixel_density != new_mode->pixel_density) ||
  1176. (old_mode->refresh_rate != 0.0f && old_mode->refresh_rate != new_mode->refresh_rate));
  1177. }
  1178. void SDL_SetDesktopDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode *mode)
  1179. {
  1180. SDL_DisplayMode last_mode;
  1181. SDL_copyp(&last_mode, &display->desktop_mode);
  1182. if (display->desktop_mode.internal) {
  1183. SDL_free(display->desktop_mode.internal);
  1184. }
  1185. SDL_copyp(&display->desktop_mode, mode);
  1186. display->desktop_mode.displayID = display->id;
  1187. SDL_FinalizeDisplayMode(&display->desktop_mode);
  1188. if (DisplayModeChanged(&last_mode, &display->desktop_mode)) {
  1189. SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_DESKTOP_MODE_CHANGED, mode->w, mode->h);
  1190. if (display->current_mode == &display->desktop_mode) {
  1191. SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_CURRENT_MODE_CHANGED, mode->w, mode->h);
  1192. }
  1193. }
  1194. }
  1195. const SDL_DisplayMode *SDL_GetDesktopDisplayMode(SDL_DisplayID displayID)
  1196. {
  1197. SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
  1198. CHECK_DISPLAY_MAGIC(display, NULL);
  1199. return &display->desktop_mode;
  1200. }
  1201. void SDL_SetCurrentDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode *mode)
  1202. {
  1203. SDL_DisplayMode last_mode;
  1204. if (display->current_mode) {
  1205. SDL_copyp(&last_mode, display->current_mode);
  1206. } else {
  1207. SDL_zero(last_mode);
  1208. }
  1209. display->current_mode = mode;
  1210. if (DisplayModeChanged(&last_mode, mode)) {
  1211. SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_CURRENT_MODE_CHANGED, mode->w, mode->h);
  1212. }
  1213. }
  1214. const SDL_DisplayMode *SDL_GetCurrentDisplayMode(SDL_DisplayID displayID)
  1215. {
  1216. SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
  1217. CHECK_DISPLAY_MAGIC(display, NULL);
  1218. // Make sure our mode list is updated
  1219. SDL_UpdateFullscreenDisplayModes(display);
  1220. return display->current_mode;
  1221. }
  1222. bool SDL_SetDisplayModeForDisplay(SDL_VideoDisplay *display, SDL_DisplayMode *mode)
  1223. {
  1224. /* Mode switching is being emulated per-window; nothing to do and cannot fail,
  1225. * except for XWayland, which still needs the actual mode setting call since
  1226. * it's emulated via the XRandR interface.
  1227. */
  1228. if (SDL_ModeSwitchingEmulated(_this) && SDL_strcmp(_this->name, "x11") != 0) {
  1229. return true;
  1230. }
  1231. if (!mode) {
  1232. mode = &display->desktop_mode;
  1233. }
  1234. if (mode == display->current_mode) {
  1235. return true;
  1236. }
  1237. // Actually change the display mode
  1238. if (_this->SetDisplayMode) {
  1239. bool result;
  1240. _this->setting_display_mode = true;
  1241. result = _this->SetDisplayMode(_this, display, mode);
  1242. _this->setting_display_mode = false;
  1243. if (!result) {
  1244. return false;
  1245. }
  1246. }
  1247. SDL_SetCurrentDisplayMode(display, mode);
  1248. return true;
  1249. }
  1250. /**
  1251. * If x, y are outside of rect, snaps them to the closest point inside rect
  1252. * (between rect->x, rect->y, inclusive, and rect->x + w, rect->y + h, exclusive)
  1253. */
  1254. static void SDL_GetClosestPointOnRect(const SDL_Rect *rect, SDL_Point *point)
  1255. {
  1256. const int right = rect->x + rect->w - 1;
  1257. const int bottom = rect->y + rect->h - 1;
  1258. if (point->x < rect->x) {
  1259. point->x = rect->x;
  1260. } else if (point->x > right) {
  1261. point->x = right;
  1262. }
  1263. if (point->y < rect->y) {
  1264. point->y = rect->y;
  1265. } else if (point->y > bottom) {
  1266. point->y = bottom;
  1267. }
  1268. }
  1269. static SDL_DisplayID GetDisplayForRect(int x, int y, int w, int h)
  1270. {
  1271. int i, dist;
  1272. SDL_DisplayID closest = 0;
  1273. int closest_dist = 0x7FFFFFFF;
  1274. SDL_Point closest_point_on_display;
  1275. SDL_Point delta;
  1276. SDL_Point center;
  1277. center.x = x + w / 2;
  1278. center.y = y + h / 2;
  1279. if (_this) {
  1280. for (i = 0; i < _this->num_displays; ++i) {
  1281. SDL_VideoDisplay *display = _this->displays[i];
  1282. SDL_Rect display_rect;
  1283. SDL_GetDisplayBounds(display->id, &display_rect);
  1284. // Check if the window is fully enclosed
  1285. if (SDL_GetRectEnclosingPoints(&center, 1, &display_rect, NULL)) {
  1286. return display->id;
  1287. }
  1288. // Snap window center to the display rect
  1289. closest_point_on_display = center;
  1290. SDL_GetClosestPointOnRect(&display_rect, &closest_point_on_display);
  1291. delta.x = center.x - closest_point_on_display.x;
  1292. delta.y = center.y - closest_point_on_display.y;
  1293. dist = (delta.x * delta.x + delta.y * delta.y);
  1294. if (dist < closest_dist) {
  1295. closest = display->id;
  1296. closest_dist = dist;
  1297. }
  1298. }
  1299. }
  1300. if (closest == 0) {
  1301. SDL_SetError("Couldn't find any displays");
  1302. }
  1303. return closest;
  1304. }
  1305. void SDL_RelativeToGlobalForWindow(SDL_Window *window, int rel_x, int rel_y, int *abs_x, int *abs_y)
  1306. {
  1307. SDL_Window *w;
  1308. if (SDL_WINDOW_IS_POPUP(window)) {
  1309. // Calculate the total offset of the popup from the parents
  1310. for (w = window->parent; w; w = w->parent) {
  1311. rel_x += w->x;
  1312. rel_y += w->y;
  1313. }
  1314. }
  1315. if (abs_x) {
  1316. *abs_x = rel_x;
  1317. }
  1318. if (abs_y) {
  1319. *abs_y = rel_y;
  1320. }
  1321. }
  1322. void SDL_GlobalToRelativeForWindow(SDL_Window *window, int abs_x, int abs_y, int *rel_x, int *rel_y)
  1323. {
  1324. SDL_Window *w;
  1325. if (SDL_WINDOW_IS_POPUP(window)) {
  1326. // Convert absolute window coordinates to relative for a popup
  1327. for (w = window->parent; w; w = w->parent) {
  1328. abs_x -= w->x;
  1329. abs_y -= w->y;
  1330. }
  1331. }
  1332. if (rel_x) {
  1333. *rel_x = abs_x;
  1334. }
  1335. if (rel_y) {
  1336. *rel_y = abs_y;
  1337. }
  1338. }
  1339. SDL_DisplayID SDL_GetDisplayForPoint(const SDL_Point *point)
  1340. {
  1341. if (!point) {
  1342. SDL_InvalidParamError("point");
  1343. return 0;
  1344. }
  1345. return GetDisplayForRect(point->x, point->y, 1, 1);
  1346. }
  1347. SDL_DisplayID SDL_GetDisplayForRect(const SDL_Rect *rect)
  1348. {
  1349. if (!rect) {
  1350. SDL_InvalidParamError("rect");
  1351. return 0;
  1352. }
  1353. return GetDisplayForRect(rect->x, rect->y, rect->w, rect->h);
  1354. }
  1355. SDL_DisplayID SDL_GetDisplayForWindowPosition(SDL_Window *window)
  1356. {
  1357. int x, y;
  1358. SDL_DisplayID displayID = 0;
  1359. CHECK_WINDOW_MAGIC(window, 0);
  1360. if (_this->GetDisplayForWindow) {
  1361. displayID = _this->GetDisplayForWindow(_this, window);
  1362. }
  1363. /* A backend implementation may fail to get a display for the window
  1364. * (for example if the window is off-screen), but other code may expect it
  1365. * to succeed in that situation, so we fall back to a generic position-
  1366. * based implementation in that case. */
  1367. SDL_RelativeToGlobalForWindow(window, window->x, window->y, &x, &y);
  1368. if (!displayID) {
  1369. /* Fullscreen windows may be larger than the display if they were moved between differently sized
  1370. * displays and the new position was received before the new size or vice versa. Using the center
  1371. * of the window rect in this case can report the wrong display, so use the origin.
  1372. */
  1373. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1374. displayID = GetDisplayForRect(x, y, 1, 1);
  1375. } else {
  1376. displayID = GetDisplayForRect(x, y, window->w, window->h);
  1377. }
  1378. }
  1379. if (!displayID) {
  1380. // Use the primary display for a window if we can't find it anywhere else
  1381. displayID = SDL_GetPrimaryDisplay();
  1382. }
  1383. return displayID;
  1384. }
  1385. SDL_VideoDisplay *SDL_GetVideoDisplayForFullscreenWindow(SDL_Window *window)
  1386. {
  1387. SDL_DisplayID displayID = 0;
  1388. CHECK_WINDOW_MAGIC(window, 0);
  1389. // An explicit fullscreen display overrides all
  1390. if (window->current_fullscreen_mode.displayID) {
  1391. displayID = window->current_fullscreen_mode.displayID;
  1392. }
  1393. /* The floating position is used here as a very common pattern is
  1394. * SDL_SetWindowPosition() followed by SDL_SetWindowFullscreen() to make the
  1395. * window fullscreen desktop on a specific display. If the backend doesn't
  1396. * support changing the window position, or the compositor hasn't yet actually
  1397. * moved the window, the current position won't be updated at the time of the
  1398. * fullscreen call.
  1399. */
  1400. if (!displayID) {
  1401. if (window->flags & SDL_WINDOW_FULLSCREEN && !window->is_repositioning) {
  1402. // This was a window manager initiated move, use the current position.
  1403. displayID = GetDisplayForRect(window->x, window->y, 1, 1);
  1404. } else {
  1405. displayID = GetDisplayForRect(window->floating.x, window->floating.y, window->floating.w, window->floating.h);
  1406. }
  1407. }
  1408. if (!displayID) {
  1409. // Use the primary display for a window if we can't find it anywhere else
  1410. displayID = SDL_GetPrimaryDisplay();
  1411. }
  1412. return SDL_GetVideoDisplay(displayID);
  1413. }
  1414. SDL_DisplayID SDL_GetDisplayForWindow(SDL_Window *window)
  1415. {
  1416. SDL_DisplayID displayID = 0;
  1417. CHECK_WINDOW_MAGIC(window, 0);
  1418. // An explicit fullscreen display overrides all
  1419. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1420. displayID = window->current_fullscreen_mode.displayID;
  1421. }
  1422. if (!displayID) {
  1423. displayID = SDL_GetDisplayForWindowPosition(window);
  1424. }
  1425. return displayID;
  1426. }
  1427. static void SDL_CheckWindowDisplayChanged(SDL_Window *window)
  1428. {
  1429. if (SDL_SendsDisplayChanges(_this)) {
  1430. return;
  1431. }
  1432. SDL_DisplayID displayID = SDL_GetDisplayForWindowPosition(window);
  1433. if (displayID != window->last_displayID) {
  1434. int i, display_index;
  1435. // Sanity check our fullscreen windows
  1436. display_index = SDL_GetDisplayIndex(displayID);
  1437. for (i = 0; i < _this->num_displays; ++i) {
  1438. SDL_VideoDisplay *display = _this->displays[i];
  1439. if (display->fullscreen_window == window) {
  1440. if (display_index != i) {
  1441. if (display_index < 0) {
  1442. display_index = i;
  1443. } else {
  1444. SDL_VideoDisplay *new_display = _this->displays[display_index];
  1445. // The window was moved to a different display
  1446. if (new_display->fullscreen_window &&
  1447. new_display->fullscreen_window != window) {
  1448. // Uh oh, there's already a fullscreen window here; minimize it
  1449. SDL_MinimizeWindow(new_display->fullscreen_window);
  1450. }
  1451. new_display->fullscreen_window = window;
  1452. display->fullscreen_window = NULL;
  1453. }
  1454. }
  1455. break;
  1456. }
  1457. }
  1458. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_DISPLAY_CHANGED, (int)displayID, 0);
  1459. }
  1460. }
  1461. float SDL_GetWindowPixelDensity(SDL_Window *window)
  1462. {
  1463. int window_w, window_h, pixel_w, pixel_h;
  1464. float pixel_density = 1.0f;
  1465. CHECK_WINDOW_MAGIC(window, 0.0f);
  1466. if (SDL_GetWindowSize(window, &window_w, &window_h) &&
  1467. SDL_GetWindowSizeInPixels(window, &pixel_w, &pixel_h)) {
  1468. pixel_density = (float)pixel_w / window_w;
  1469. }
  1470. return pixel_density;
  1471. }
  1472. float SDL_GetWindowDisplayScale(SDL_Window *window)
  1473. {
  1474. CHECK_WINDOW_MAGIC(window, 0.0f);
  1475. return window->display_scale;
  1476. }
  1477. static void SDL_CheckWindowDisplayScaleChanged(SDL_Window *window)
  1478. {
  1479. float display_scale;
  1480. if (_this->GetWindowContentScale) {
  1481. display_scale = _this->GetWindowContentScale(_this, window);
  1482. } else {
  1483. const float pixel_density = SDL_GetWindowPixelDensity(window);
  1484. const float content_scale = SDL_GetDisplayContentScale(SDL_GetDisplayForWindowPosition(window));
  1485. display_scale = pixel_density * content_scale;
  1486. }
  1487. if (display_scale != window->display_scale) {
  1488. window->display_scale = display_scale;
  1489. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED, 0, 0);
  1490. }
  1491. }
  1492. static void SDL_RestoreMousePosition(SDL_Window *window)
  1493. {
  1494. float x, y;
  1495. SDL_Mouse *mouse = SDL_GetMouse();
  1496. if (window == SDL_GetMouseFocus()) {
  1497. const bool prev_warp_val = mouse->warp_emulation_prohibited;
  1498. SDL_GetMouseState(&x, &y);
  1499. // Disable the warp emulation so it isn't accidentally activated on a fullscreen transitions.
  1500. mouse->warp_emulation_prohibited = true;
  1501. SDL_WarpMouseInWindow(window, x, y);
  1502. mouse->warp_emulation_prohibited = prev_warp_val;
  1503. }
  1504. }
  1505. bool SDL_UpdateFullscreenMode(SDL_Window *window, SDL_FullscreenOp fullscreen, bool commit)
  1506. {
  1507. SDL_VideoDisplay *display = NULL;
  1508. SDL_DisplayMode *mode = NULL;
  1509. int i;
  1510. CHECK_WINDOW_MAGIC(window, false);
  1511. window->fullscreen_exclusive = false;
  1512. // If we are in the process of hiding don't go back to fullscreen
  1513. if (window->is_destroying || window->is_hiding) {
  1514. fullscreen = SDL_FULLSCREEN_OP_LEAVE;
  1515. }
  1516. // Get the correct display for this operation
  1517. if (fullscreen) {
  1518. display = SDL_GetVideoDisplayForFullscreenWindow(window);
  1519. if (!display) {
  1520. // This should never happen, but it did...
  1521. goto done;
  1522. }
  1523. } else {
  1524. for (i = 0; i < _this->num_displays; ++i) {
  1525. display = _this->displays[i];
  1526. if (display->fullscreen_window == window) {
  1527. break;
  1528. }
  1529. }
  1530. if (!display || i == _this->num_displays) {
  1531. // Already not fullscreen on any display
  1532. display = NULL;
  1533. }
  1534. }
  1535. if (fullscreen) {
  1536. mode = (SDL_DisplayMode *)SDL_GetWindowFullscreenMode(window);
  1537. if (mode) {
  1538. window->fullscreen_exclusive = true;
  1539. } else {
  1540. // Make sure the current mode is zeroed for fullscreen desktop.
  1541. SDL_zero(window->current_fullscreen_mode);
  1542. }
  1543. }
  1544. #if defined(SDL_PLATFORM_MACOS) && defined(SDL_VIDEO_DRIVER_COCOA)
  1545. /* if the window is going away and no resolution change is necessary,
  1546. do nothing, or else we may trigger an ugly double-transition
  1547. */
  1548. if (SDL_strcmp(_this->name, "cocoa") == 0) { // don't do this for X11, etc
  1549. if (window->is_destroying && !window->last_fullscreen_exclusive_display) {
  1550. window->fullscreen_exclusive = false;
  1551. if (display) {
  1552. display->fullscreen_window = NULL;
  1553. }
  1554. goto done;
  1555. }
  1556. if (commit) {
  1557. // If we're switching between a fullscreen Space and exclusive fullscreen, we need to get back to normal first.
  1558. if (fullscreen && Cocoa_IsWindowInFullscreenSpace(window) && !window->last_fullscreen_exclusive_display && window->fullscreen_exclusive) {
  1559. if (!Cocoa_SetWindowFullscreenSpace(window, false, true)) {
  1560. goto error;
  1561. }
  1562. } else if (fullscreen && window->last_fullscreen_exclusive_display && !window->fullscreen_exclusive) {
  1563. for (i = 0; i < _this->num_displays; ++i) {
  1564. SDL_VideoDisplay *last_display = _this->displays[i];
  1565. if (last_display->fullscreen_window == window) {
  1566. SDL_SetDisplayModeForDisplay(last_display, NULL);
  1567. if (_this->SetWindowFullscreen) {
  1568. _this->SetWindowFullscreen(_this, window, last_display, false);
  1569. }
  1570. last_display->fullscreen_window = NULL;
  1571. }
  1572. }
  1573. }
  1574. if (Cocoa_SetWindowFullscreenSpace(window, !!fullscreen, syncHint)) {
  1575. goto done;
  1576. }
  1577. }
  1578. }
  1579. #endif
  1580. if (display) {
  1581. // Restore the video mode on other displays if needed
  1582. for (i = 0; i < _this->num_displays; ++i) {
  1583. SDL_VideoDisplay *other = _this->displays[i];
  1584. if (other != display && other->fullscreen_window == window) {
  1585. SDL_SetDisplayModeForDisplay(other, NULL);
  1586. other->fullscreen_window = NULL;
  1587. }
  1588. }
  1589. }
  1590. if (fullscreen) {
  1591. int mode_w = 0, mode_h = 0;
  1592. bool resized = false;
  1593. // Hide any other fullscreen window on this display
  1594. if (display->fullscreen_window &&
  1595. display->fullscreen_window != window) {
  1596. SDL_MinimizeWindow(display->fullscreen_window);
  1597. }
  1598. if (!SDL_SetDisplayModeForDisplay(display, mode)) {
  1599. goto error;
  1600. }
  1601. if (commit) {
  1602. SDL_FullscreenResult ret = SDL_FULLSCREEN_SUCCEEDED;
  1603. if (_this->SetWindowFullscreen) {
  1604. ret = _this->SetWindowFullscreen(_this, window, display, fullscreen);
  1605. } else {
  1606. resized = true;
  1607. }
  1608. if (ret == SDL_FULLSCREEN_SUCCEEDED) {
  1609. // Window is fullscreen immediately upon return. If the driver hasn't already sent the event, do so now.
  1610. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  1611. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_ENTER_FULLSCREEN, 0, 0);
  1612. }
  1613. } else if (ret == SDL_FULLSCREEN_FAILED) {
  1614. goto error;
  1615. }
  1616. }
  1617. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1618. display->fullscreen_window = window;
  1619. /* Android may not resize the window to exactly what our fullscreen mode is,
  1620. * especially on windowed Android environments like the Chromebook or Samsung DeX.
  1621. * Given this, we shouldn't use the mode size. Android's SetWindowFullscreen
  1622. * will generate the window event for us with the proper final size.
  1623. *
  1624. * This is also unnecessary on Cocoa, Wayland, Win32, and X11 (will send SDL_EVENT_WINDOW_RESIZED).
  1625. */
  1626. if (!SDL_SendsFullscreenDimensions(_this)) {
  1627. if (mode) {
  1628. mode_w = mode->w;
  1629. mode_h = mode->h;
  1630. } else {
  1631. mode_w = display->desktop_mode.w;
  1632. mode_h = display->desktop_mode.h;
  1633. }
  1634. if (window->w != mode_w || window->h != mode_h) {
  1635. resized = true;
  1636. }
  1637. if (resized) {
  1638. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, mode_w, mode_h);
  1639. } else {
  1640. SDL_OnWindowResized(window);
  1641. }
  1642. }
  1643. // Restore the cursor position
  1644. if (!SDL_DisableMouseWarpOnFullscreenTransitions(_this)) {
  1645. SDL_RestoreMousePosition(window);
  1646. }
  1647. }
  1648. } else {
  1649. bool resized = false;
  1650. // Restore the desktop mode
  1651. if (display) {
  1652. SDL_SetDisplayModeForDisplay(display, NULL);
  1653. }
  1654. if (commit) {
  1655. SDL_FullscreenResult ret = SDL_FULLSCREEN_SUCCEEDED;
  1656. if (_this->SetWindowFullscreen) {
  1657. SDL_VideoDisplay *full_screen_display = display ? display : SDL_GetVideoDisplayForFullscreenWindow(window);
  1658. if (full_screen_display) {
  1659. ret = _this->SetWindowFullscreen(_this, window, full_screen_display, SDL_FULLSCREEN_OP_LEAVE);
  1660. }
  1661. } else {
  1662. resized = true;
  1663. }
  1664. if (ret == SDL_FULLSCREEN_SUCCEEDED) {
  1665. // Window left fullscreen immediately upon return. If the driver hasn't already sent the event, do so now.
  1666. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1667. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_LEAVE_FULLSCREEN, 0, 0);
  1668. }
  1669. } else if (ret == SDL_FULLSCREEN_FAILED) {
  1670. goto error;
  1671. }
  1672. }
  1673. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  1674. if (display) {
  1675. display->fullscreen_window = NULL;
  1676. }
  1677. if (!SDL_SendsFullscreenDimensions(_this)) {
  1678. if (resized) {
  1679. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, window->windowed.w, window->windowed.h);
  1680. } else {
  1681. SDL_OnWindowResized(window);
  1682. }
  1683. }
  1684. // Restore the cursor position if we've exited fullscreen on a display
  1685. if (display && !SDL_DisableMouseWarpOnFullscreenTransitions(_this)) {
  1686. SDL_RestoreMousePosition(window);
  1687. }
  1688. }
  1689. }
  1690. done:
  1691. window->last_fullscreen_exclusive_display = display && (window->flags & SDL_WINDOW_FULLSCREEN) && window->fullscreen_exclusive ? display->id : 0;
  1692. return true;
  1693. error:
  1694. if (fullscreen) {
  1695. // Something went wrong and the window is no longer fullscreen.
  1696. SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_LEAVE, commit);
  1697. }
  1698. return false;
  1699. }
  1700. bool SDL_SetWindowFullscreenMode(SDL_Window *window, const SDL_DisplayMode *mode)
  1701. {
  1702. CHECK_WINDOW_MAGIC(window, false);
  1703. CHECK_WINDOW_NOT_POPUP(window, false);
  1704. if (mode) {
  1705. if (!SDL_GetFullscreenModeMatch(mode)) {
  1706. return SDL_SetError("Invalid fullscreen display mode");
  1707. }
  1708. // Save the mode so we can look up the closest match later
  1709. SDL_copyp(&window->requested_fullscreen_mode, mode);
  1710. } else {
  1711. SDL_zero(window->requested_fullscreen_mode);
  1712. }
  1713. /* Copy to the current mode now, in case an asynchronous fullscreen window request
  1714. * is in progress. It will be overwritten if a new request is made.
  1715. */
  1716. SDL_copyp(&window->current_fullscreen_mode, &window->requested_fullscreen_mode);
  1717. if (SDL_WINDOW_FULLSCREEN_VISIBLE(window)) {
  1718. SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_UPDATE, true);
  1719. SDL_SyncIfRequired(window);
  1720. }
  1721. return true;
  1722. }
  1723. const SDL_DisplayMode *SDL_GetWindowFullscreenMode(SDL_Window *window)
  1724. {
  1725. CHECK_WINDOW_MAGIC(window, NULL);
  1726. CHECK_WINDOW_NOT_POPUP(window, NULL);
  1727. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1728. return SDL_GetFullscreenModeMatch(&window->current_fullscreen_mode);
  1729. } else {
  1730. return SDL_GetFullscreenModeMatch(&window->requested_fullscreen_mode);
  1731. }
  1732. }
  1733. void *SDL_GetWindowICCProfile(SDL_Window *window, size_t *size)
  1734. {
  1735. if (!_this->GetWindowICCProfile) {
  1736. SDL_Unsupported();
  1737. return NULL;
  1738. }
  1739. return _this->GetWindowICCProfile(_this, window, size);
  1740. }
  1741. SDL_PixelFormat SDL_GetWindowPixelFormat(SDL_Window *window)
  1742. {
  1743. SDL_DisplayID displayID;
  1744. const SDL_DisplayMode *mode;
  1745. CHECK_WINDOW_MAGIC(window, SDL_PIXELFORMAT_UNKNOWN);
  1746. displayID = SDL_GetDisplayForWindow(window);
  1747. mode = SDL_GetCurrentDisplayMode(displayID);
  1748. if (mode) {
  1749. return mode->format;
  1750. } else {
  1751. return SDL_PIXELFORMAT_UNKNOWN;
  1752. }
  1753. }
  1754. #define CREATE_FLAGS \
  1755. (SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_ALWAYS_ON_TOP | SDL_WINDOW_POPUP_MENU | SDL_WINDOW_UTILITY | SDL_WINDOW_TOOLTIP | SDL_WINDOW_VULKAN | SDL_WINDOW_MINIMIZED | SDL_WINDOW_METAL | SDL_WINDOW_TRANSPARENT | SDL_WINDOW_NOT_FOCUSABLE)
  1756. static SDL_INLINE bool IsAcceptingDragAndDrop(void)
  1757. {
  1758. if (SDL_EventEnabled(SDL_EVENT_DROP_FILE) || SDL_EventEnabled(SDL_EVENT_DROP_TEXT)) {
  1759. return true;
  1760. }
  1761. return false;
  1762. }
  1763. // prepare a newly-created window
  1764. static SDL_INLINE void PrepareDragAndDropSupport(SDL_Window *window)
  1765. {
  1766. if (_this->AcceptDragAndDrop) {
  1767. _this->AcceptDragAndDrop(window, IsAcceptingDragAndDrop());
  1768. }
  1769. }
  1770. // toggle d'n'd for all existing windows.
  1771. void SDL_ToggleDragAndDropSupport(void)
  1772. {
  1773. if (_this && _this->AcceptDragAndDrop) {
  1774. const bool enable = IsAcceptingDragAndDrop();
  1775. SDL_Window *window;
  1776. for (window = _this->windows; window; window = window->next) {
  1777. _this->AcceptDragAndDrop(window, enable);
  1778. }
  1779. }
  1780. }
  1781. SDL_Window **SDLCALL SDL_GetWindows(int *count)
  1782. {
  1783. if (count) {
  1784. *count = 0;
  1785. }
  1786. if (!_this) {
  1787. SDL_UninitializedVideo();
  1788. return NULL;
  1789. }
  1790. SDL_Window *window;
  1791. int num_added = 0;
  1792. int num_windows = 0;
  1793. for (window = _this->windows; window; window = window->next) {
  1794. ++num_windows;
  1795. }
  1796. SDL_Window **windows = (SDL_Window **)SDL_malloc((num_windows + 1) * sizeof(*windows));
  1797. if (!windows) {
  1798. return NULL;
  1799. }
  1800. for (window = _this->windows; window; window = window->next) {
  1801. windows[num_added++] = window;
  1802. if (num_added == num_windows) {
  1803. // Race condition? Multi-threading not supported, ignore it
  1804. break;
  1805. }
  1806. }
  1807. windows[num_added] = NULL;
  1808. if (count) {
  1809. *count = num_added;
  1810. }
  1811. return windows;
  1812. }
  1813. static void ApplyWindowFlags(SDL_Window *window, SDL_WindowFlags flags)
  1814. {
  1815. if (!SDL_WINDOW_IS_POPUP(window)) {
  1816. if (!(flags & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED))) {
  1817. SDL_RestoreWindow(window);
  1818. }
  1819. if (flags & SDL_WINDOW_MAXIMIZED) {
  1820. SDL_MaximizeWindow(window);
  1821. }
  1822. SDL_SetWindowFullscreen(window, (flags & SDL_WINDOW_FULLSCREEN) != 0);
  1823. if (flags & SDL_WINDOW_MINIMIZED) {
  1824. SDL_MinimizeWindow(window);
  1825. }
  1826. if (flags & SDL_WINDOW_MODAL) {
  1827. SDL_SetWindowModal(window, true);
  1828. }
  1829. if (flags & SDL_WINDOW_MOUSE_GRABBED) {
  1830. SDL_SetWindowMouseGrab(window, true);
  1831. }
  1832. if (flags & SDL_WINDOW_KEYBOARD_GRABBED) {
  1833. SDL_SetWindowKeyboardGrab(window, true);
  1834. }
  1835. }
  1836. }
  1837. static void SDL_FinishWindowCreation(SDL_Window *window, SDL_WindowFlags flags)
  1838. {
  1839. PrepareDragAndDropSupport(window);
  1840. if (window->flags & SDL_WINDOW_EXTERNAL) {
  1841. // Whoever has created the window has already applied whatever flags are needed
  1842. } else {
  1843. ApplyWindowFlags(window, flags);
  1844. if (!(flags & SDL_WINDOW_HIDDEN)) {
  1845. SDL_ShowWindow(window);
  1846. }
  1847. }
  1848. }
  1849. static bool SDL_ContextNotSupported(const char *name)
  1850. {
  1851. return SDL_SetError("%s support is either not configured in SDL "
  1852. "or not available in current SDL video driver "
  1853. "(%s) or platform",
  1854. name,
  1855. _this->name);
  1856. }
  1857. static bool SDL_DllNotSupported(const char *name)
  1858. {
  1859. return SDL_SetError("No dynamic %s support in current SDL video driver (%s)", name, _this->name);
  1860. }
  1861. static struct {
  1862. const char *property_name;
  1863. SDL_WindowFlags flag;
  1864. bool invert_value;
  1865. } SDL_WindowFlagProperties[] = {
  1866. { SDL_PROP_WINDOW_CREATE_ALWAYS_ON_TOP_BOOLEAN, SDL_WINDOW_ALWAYS_ON_TOP, false },
  1867. { SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN, SDL_WINDOW_BORDERLESS, false },
  1868. { SDL_PROP_WINDOW_CREATE_FOCUSABLE_BOOLEAN, SDL_WINDOW_NOT_FOCUSABLE, true },
  1869. { SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN, SDL_WINDOW_FULLSCREEN, false },
  1870. { SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN, SDL_WINDOW_HIDDEN, false },
  1871. { SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, SDL_WINDOW_HIGH_PIXEL_DENSITY, false },
  1872. { SDL_PROP_WINDOW_CREATE_MAXIMIZED_BOOLEAN, SDL_WINDOW_MAXIMIZED, false },
  1873. { SDL_PROP_WINDOW_CREATE_MENU_BOOLEAN, SDL_WINDOW_POPUP_MENU, false },
  1874. { SDL_PROP_WINDOW_CREATE_METAL_BOOLEAN, SDL_WINDOW_METAL, false },
  1875. { SDL_PROP_WINDOW_CREATE_MINIMIZED_BOOLEAN, SDL_WINDOW_MINIMIZED, false },
  1876. { SDL_PROP_WINDOW_CREATE_MODAL_BOOLEAN, SDL_WINDOW_MODAL, false },
  1877. { SDL_PROP_WINDOW_CREATE_MOUSE_GRABBED_BOOLEAN, SDL_WINDOW_MOUSE_GRABBED, false },
  1878. { SDL_PROP_WINDOW_CREATE_OPENGL_BOOLEAN, SDL_WINDOW_OPENGL, false },
  1879. { SDL_PROP_WINDOW_CREATE_RESIZABLE_BOOLEAN, SDL_WINDOW_RESIZABLE, false },
  1880. { SDL_PROP_WINDOW_CREATE_TRANSPARENT_BOOLEAN, SDL_WINDOW_TRANSPARENT, false },
  1881. { SDL_PROP_WINDOW_CREATE_TOOLTIP_BOOLEAN, SDL_WINDOW_TOOLTIP, false },
  1882. { SDL_PROP_WINDOW_CREATE_UTILITY_BOOLEAN, SDL_WINDOW_UTILITY, false },
  1883. { SDL_PROP_WINDOW_CREATE_VULKAN_BOOLEAN, SDL_WINDOW_VULKAN, false }
  1884. };
  1885. static SDL_WindowFlags SDL_GetWindowFlagProperties(SDL_PropertiesID props)
  1886. {
  1887. unsigned i;
  1888. SDL_WindowFlags flags = (SDL_WindowFlags)SDL_GetNumberProperty(props, SDL_PROP_WINDOW_CREATE_FLAGS_NUMBER, 0);
  1889. for (i = 0; i < SDL_arraysize(SDL_WindowFlagProperties); ++i) {
  1890. if (SDL_WindowFlagProperties[i].invert_value) {
  1891. if (!SDL_GetBooleanProperty(props, SDL_WindowFlagProperties[i].property_name, true)) {
  1892. flags |= SDL_WindowFlagProperties[i].flag;
  1893. }
  1894. } else {
  1895. if (SDL_GetBooleanProperty(props, SDL_WindowFlagProperties[i].property_name, false)) {
  1896. flags |= SDL_WindowFlagProperties[i].flag;
  1897. }
  1898. }
  1899. }
  1900. return flags;
  1901. }
  1902. SDL_Window *SDL_CreateWindowWithProperties(SDL_PropertiesID props)
  1903. {
  1904. SDL_Window *window;
  1905. const char *title = SDL_GetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, NULL);
  1906. int x = (int)SDL_GetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, SDL_WINDOWPOS_UNDEFINED);
  1907. int y = (int)SDL_GetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, SDL_WINDOWPOS_UNDEFINED);
  1908. int w = (int)SDL_GetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, 0);
  1909. int h = (int)SDL_GetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, 0);
  1910. SDL_Window *parent = (SDL_Window *)SDL_GetPointerProperty(props, SDL_PROP_WINDOW_CREATE_PARENT_POINTER, NULL);
  1911. SDL_WindowFlags flags = SDL_GetWindowFlagProperties(props);
  1912. SDL_WindowFlags type_flags, graphics_flags;
  1913. bool undefined_x = false;
  1914. bool undefined_y = false;
  1915. bool external_graphics_context = SDL_GetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_EXTERNAL_GRAPHICS_CONTEXT_BOOLEAN, false);
  1916. if (!_this) {
  1917. // Initialize the video system if needed
  1918. if (!SDL_Init(SDL_INIT_VIDEO)) {
  1919. return NULL;
  1920. }
  1921. // Make clang-tidy happy
  1922. if (!_this) {
  1923. return NULL;
  1924. }
  1925. }
  1926. if ((flags & SDL_WINDOW_MODAL) && !SDL_ObjectValid(parent, SDL_OBJECT_TYPE_WINDOW)) {
  1927. SDL_SetError("Modal windows must specify a parent window");
  1928. return NULL;
  1929. }
  1930. if ((flags & (SDL_WINDOW_TOOLTIP | SDL_WINDOW_POPUP_MENU)) != 0) {
  1931. if (!(_this->device_caps & VIDEO_DEVICE_CAPS_HAS_POPUP_WINDOW_SUPPORT)) {
  1932. SDL_Unsupported();
  1933. return NULL;
  1934. }
  1935. // Tooltip and popup menu window must specify a parent window
  1936. if (!SDL_ObjectValid(parent, SDL_OBJECT_TYPE_WINDOW)) {
  1937. SDL_SetError("Tooltip and popup menu windows must specify a parent window");
  1938. return NULL;
  1939. }
  1940. // Remove invalid flags
  1941. flags &= ~(SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS);
  1942. }
  1943. // Ensure no more than one of these flags is set
  1944. type_flags = flags & (SDL_WINDOW_UTILITY | SDL_WINDOW_TOOLTIP | SDL_WINDOW_POPUP_MENU | SDL_WINDOW_MODAL);
  1945. if (type_flags & (type_flags - 1)) {
  1946. SDL_SetError("Conflicting window type flags specified: 0x%.8x", (unsigned int)type_flags);
  1947. return NULL;
  1948. }
  1949. // Make sure the display list is up to date for window placement
  1950. if (_this->RefreshDisplays) {
  1951. _this->RefreshDisplays(_this);
  1952. }
  1953. // Some platforms can't create zero-sized windows
  1954. if (w < 1) {
  1955. w = 1;
  1956. }
  1957. if (h < 1) {
  1958. h = 1;
  1959. }
  1960. if (SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISUNDEFINED(y) ||
  1961. SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
  1962. SDL_DisplayID displayID = 0;
  1963. SDL_Rect bounds;
  1964. if ((SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISCENTERED(x)) && (x & 0xFFFF)) {
  1965. displayID = (x & 0xFFFF);
  1966. } else if ((SDL_WINDOWPOS_ISUNDEFINED(y) || SDL_WINDOWPOS_ISCENTERED(y)) && (y & 0xFFFF)) {
  1967. displayID = (y & 0xFFFF);
  1968. }
  1969. if (displayID == 0 || SDL_GetDisplayIndex(displayID) < 0) {
  1970. displayID = SDL_GetPrimaryDisplay();
  1971. }
  1972. SDL_zero(bounds);
  1973. SDL_GetDisplayUsableBounds(displayID, &bounds);
  1974. if (w > bounds.w || h > bounds.h) {
  1975. // This window is larger than the usable bounds, just center on the display
  1976. SDL_GetDisplayBounds(displayID, &bounds);
  1977. }
  1978. if (SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISUNDEFINED(x)) {
  1979. if (SDL_WINDOWPOS_ISUNDEFINED(x)) {
  1980. undefined_x = true;
  1981. }
  1982. x = bounds.x + (bounds.w - w) / 2;
  1983. }
  1984. if (SDL_WINDOWPOS_ISCENTERED(y) || SDL_WINDOWPOS_ISUNDEFINED(y)) {
  1985. if (SDL_WINDOWPOS_ISUNDEFINED(y)) {
  1986. undefined_y = true;
  1987. }
  1988. y = bounds.y + (bounds.h - h) / 2;
  1989. }
  1990. }
  1991. // ensure no more than one of these flags is set
  1992. graphics_flags = flags & (SDL_WINDOW_OPENGL | SDL_WINDOW_METAL | SDL_WINDOW_VULKAN);
  1993. if (graphics_flags & (graphics_flags - 1)) {
  1994. SDL_SetError("Conflicting window graphics flags specified: 0x%.8x", (unsigned int)graphics_flags);
  1995. return NULL;
  1996. }
  1997. // Some platforms have certain graphics backends enabled by default
  1998. if (!graphics_flags && !external_graphics_context) {
  1999. flags |= SDL_DefaultGraphicsBackends(_this);
  2000. }
  2001. if (flags & SDL_WINDOW_OPENGL) {
  2002. if (!_this->GL_CreateContext) {
  2003. SDL_ContextNotSupported("OpenGL");
  2004. return NULL;
  2005. }
  2006. if (!SDL_GL_LoadLibrary(NULL)) {
  2007. return NULL;
  2008. }
  2009. }
  2010. if (flags & SDL_WINDOW_VULKAN) {
  2011. if (!_this->Vulkan_CreateSurface) {
  2012. SDL_ContextNotSupported("Vulkan");
  2013. return NULL;
  2014. }
  2015. if (!SDL_Vulkan_LoadLibrary(NULL)) {
  2016. return NULL;
  2017. }
  2018. }
  2019. if (flags & SDL_WINDOW_METAL) {
  2020. if (!_this->Metal_CreateView) {
  2021. SDL_ContextNotSupported("Metal");
  2022. return NULL;
  2023. }
  2024. }
  2025. window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
  2026. if (!window) {
  2027. return NULL;
  2028. }
  2029. SDL_SetObjectValid(window, SDL_OBJECT_TYPE_WINDOW, true);
  2030. window->id = SDL_GetNextObjectID();
  2031. window->floating.x = window->windowed.x = window->x = x;
  2032. window->floating.y = window->windowed.y = window->y = y;
  2033. window->floating.w = window->windowed.w = window->w = w;
  2034. window->floating.h = window->windowed.h = window->h = h;
  2035. window->undefined_x = undefined_x;
  2036. window->undefined_y = undefined_y;
  2037. SDL_VideoDisplay *display = SDL_GetVideoDisplayForWindow(window);
  2038. if (display) {
  2039. SDL_SetWindowHDRProperties(window, &display->HDR, false);
  2040. }
  2041. if (flags & SDL_WINDOW_FULLSCREEN || IsFullscreenOnly(_this)) {
  2042. SDL_Rect bounds;
  2043. SDL_GetDisplayBounds(display ? display->id : SDL_GetPrimaryDisplay(), &bounds);
  2044. window->x = bounds.x;
  2045. window->y = bounds.y;
  2046. window->w = bounds.w;
  2047. window->h = bounds.h;
  2048. window->pending_flags |= SDL_WINDOW_FULLSCREEN;
  2049. flags |= SDL_WINDOW_FULLSCREEN;
  2050. }
  2051. window->flags = ((flags & CREATE_FLAGS) | SDL_WINDOW_HIDDEN);
  2052. window->display_scale = 1.0f;
  2053. window->opacity = 1.0f;
  2054. window->next = _this->windows;
  2055. window->is_destroying = false;
  2056. window->last_displayID = SDL_GetDisplayForWindow(window);
  2057. window->external_graphics_context = external_graphics_context;
  2058. if (_this->windows) {
  2059. _this->windows->prev = window;
  2060. }
  2061. _this->windows = window;
  2062. // Set the parent before creation.
  2063. SDL_UpdateWindowHierarchy(window, parent);
  2064. if (_this->CreateSDLWindow && !_this->CreateSDLWindow(_this, window, props)) {
  2065. SDL_DestroyWindow(window);
  2066. return NULL;
  2067. }
  2068. /* Clear minimized if not on windows, only windows handles it at create rather than FinishWindowCreation,
  2069. * but it's important or window focus will get broken on windows!
  2070. */
  2071. #if !defined(SDL_PLATFORM_WINDOWS)
  2072. if (window->flags & SDL_WINDOW_MINIMIZED) {
  2073. window->flags &= ~SDL_WINDOW_MINIMIZED;
  2074. }
  2075. #endif
  2076. if (title) {
  2077. SDL_SetWindowTitle(window, title);
  2078. }
  2079. SDL_FinishWindowCreation(window, flags);
  2080. // Make sure window pixel size is up to date
  2081. SDL_CheckWindowPixelSizeChanged(window);
  2082. SDL_ClearError();
  2083. return window;
  2084. }
  2085. SDL_Window *SDL_CreateWindow(const char *title, int w, int h, SDL_WindowFlags flags)
  2086. {
  2087. SDL_Window *window;
  2088. SDL_PropertiesID props = SDL_CreateProperties();
  2089. if (title && *title) {
  2090. SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, title);
  2091. }
  2092. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, w);
  2093. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, h);
  2094. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_FLAGS_NUMBER, flags);
  2095. window = SDL_CreateWindowWithProperties(props);
  2096. SDL_DestroyProperties(props);
  2097. return window;
  2098. }
  2099. SDL_Window *SDL_CreatePopupWindow(SDL_Window *parent, int offset_x, int offset_y, int w, int h, SDL_WindowFlags flags)
  2100. {
  2101. SDL_Window *window;
  2102. SDL_PropertiesID props = SDL_CreateProperties();
  2103. // Popups must specify either the tooltip or popup menu window flags
  2104. if (!(flags & (SDL_WINDOW_TOOLTIP | SDL_WINDOW_POPUP_MENU))) {
  2105. SDL_SetError("Popup windows must specify either the 'SDL_WINDOW_TOOLTIP' or the 'SDL_WINDOW_POPUP_MENU' flag");
  2106. return NULL;
  2107. }
  2108. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_CREATE_PARENT_POINTER, parent);
  2109. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, offset_x);
  2110. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, offset_y);
  2111. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, w);
  2112. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, h);
  2113. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_FLAGS_NUMBER, flags);
  2114. window = SDL_CreateWindowWithProperties(props);
  2115. SDL_DestroyProperties(props);
  2116. return window;
  2117. }
  2118. bool SDL_RecreateWindow(SDL_Window *window, SDL_WindowFlags flags)
  2119. {
  2120. bool loaded_opengl = false;
  2121. bool need_gl_unload = false;
  2122. bool need_gl_load = false;
  2123. bool loaded_vulkan = false;
  2124. bool need_vulkan_unload = false;
  2125. bool need_vulkan_load = false;
  2126. SDL_WindowFlags graphics_flags;
  2127. // ensure no more than one of these flags is set
  2128. graphics_flags = flags & (SDL_WINDOW_OPENGL | SDL_WINDOW_METAL | SDL_WINDOW_VULKAN);
  2129. if (graphics_flags & (graphics_flags - 1)) {
  2130. return SDL_SetError("Conflicting window flags specified");
  2131. }
  2132. if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
  2133. return SDL_ContextNotSupported("OpenGL");
  2134. }
  2135. if ((flags & SDL_WINDOW_VULKAN) && !_this->Vulkan_CreateSurface) {
  2136. return SDL_ContextNotSupported("Vulkan");
  2137. }
  2138. if ((flags & SDL_WINDOW_METAL) && !_this->Metal_CreateView) {
  2139. return SDL_ContextNotSupported("Metal");
  2140. }
  2141. if (window->flags & SDL_WINDOW_EXTERNAL) {
  2142. // Can't destroy and re-create external windows, hrm
  2143. flags |= SDL_WINDOW_EXTERNAL;
  2144. } else {
  2145. flags &= ~SDL_WINDOW_EXTERNAL;
  2146. }
  2147. // If this is a modal dialog, clear the modal status.
  2148. if (window->flags & SDL_WINDOW_MODAL) {
  2149. SDL_SetWindowModal(window, false);
  2150. }
  2151. // Restore video mode, etc.
  2152. if (!(window->flags & SDL_WINDOW_EXTERNAL)) {
  2153. const bool restore_on_show = window->restore_on_show;
  2154. SDL_HideWindow(window);
  2155. window->restore_on_show = restore_on_show;
  2156. }
  2157. // Tear down the old native window
  2158. SDL_DestroyWindowSurface(window);
  2159. if ((window->flags & SDL_WINDOW_OPENGL) != (flags & SDL_WINDOW_OPENGL)) {
  2160. if (flags & SDL_WINDOW_OPENGL) {
  2161. need_gl_load = true;
  2162. } else {
  2163. need_gl_unload = true;
  2164. }
  2165. } else if (window->flags & SDL_WINDOW_OPENGL) {
  2166. need_gl_unload = true;
  2167. need_gl_load = true;
  2168. }
  2169. if ((window->flags & SDL_WINDOW_VULKAN) != (flags & SDL_WINDOW_VULKAN)) {
  2170. if (flags & SDL_WINDOW_VULKAN) {
  2171. need_vulkan_load = true;
  2172. } else {
  2173. need_vulkan_unload = true;
  2174. }
  2175. } else if (window->flags & SDL_WINDOW_VULKAN) {
  2176. need_vulkan_unload = true;
  2177. need_vulkan_load = true;
  2178. }
  2179. if (need_gl_unload) {
  2180. SDL_GL_UnloadLibrary();
  2181. }
  2182. if (need_vulkan_unload) {
  2183. SDL_Vulkan_UnloadLibrary();
  2184. }
  2185. if (_this->DestroyWindow && !(flags & SDL_WINDOW_EXTERNAL)) {
  2186. _this->DestroyWindow(_this, window);
  2187. }
  2188. if (need_gl_load) {
  2189. if (!SDL_GL_LoadLibrary(NULL)) {
  2190. return false;
  2191. }
  2192. loaded_opengl = true;
  2193. }
  2194. if (need_vulkan_load) {
  2195. if (!SDL_Vulkan_LoadLibrary(NULL)) {
  2196. return false;
  2197. }
  2198. loaded_vulkan = true;
  2199. }
  2200. window->flags = ((flags & CREATE_FLAGS) | SDL_WINDOW_HIDDEN);
  2201. window->is_destroying = false;
  2202. if (_this->CreateSDLWindow && !(flags & SDL_WINDOW_EXTERNAL)) {
  2203. /* Reset the window size to the original floating value, so the
  2204. * recreated window has the proper base size.
  2205. */
  2206. window->x = window->windowed.x = window->floating.x;
  2207. window->y = window->windowed.y = window->floating.y;
  2208. window->w = window->windowed.w = window->floating.w;
  2209. window->h = window->windowed.h = window->floating.h;
  2210. if (!_this->CreateSDLWindow(_this, window, 0)) {
  2211. if (loaded_opengl) {
  2212. SDL_GL_UnloadLibrary();
  2213. window->flags &= ~SDL_WINDOW_OPENGL;
  2214. }
  2215. if (loaded_vulkan) {
  2216. SDL_Vulkan_UnloadLibrary();
  2217. window->flags &= ~SDL_WINDOW_VULKAN;
  2218. }
  2219. return false;
  2220. }
  2221. }
  2222. if (flags & SDL_WINDOW_EXTERNAL) {
  2223. window->flags |= SDL_WINDOW_EXTERNAL;
  2224. }
  2225. if (_this->SetWindowTitle && window->title) {
  2226. _this->SetWindowTitle(_this, window);
  2227. }
  2228. if (_this->SetWindowIcon && window->icon) {
  2229. _this->SetWindowIcon(_this, window, window->icon);
  2230. }
  2231. if (_this->SetWindowMinimumSize && (window->min_w || window->min_h)) {
  2232. _this->SetWindowMinimumSize(_this, window);
  2233. }
  2234. if (_this->SetWindowMaximumSize && (window->max_w || window->max_h)) {
  2235. _this->SetWindowMaximumSize(_this, window);
  2236. }
  2237. if (_this->SetWindowAspectRatio && (window->min_aspect > 0.0f || window->max_aspect > 0.0f)) {
  2238. _this->SetWindowAspectRatio(_this, window);
  2239. }
  2240. if (window->hit_test) {
  2241. _this->SetWindowHitTest(window, true);
  2242. }
  2243. SDL_FinishWindowCreation(window, flags);
  2244. return true;
  2245. }
  2246. bool SDL_HasWindows(void)
  2247. {
  2248. return _this && _this->windows;
  2249. }
  2250. SDL_WindowID SDL_GetWindowID(SDL_Window *window)
  2251. {
  2252. CHECK_WINDOW_MAGIC(window, 0);
  2253. return window->id;
  2254. }
  2255. SDL_Window *SDL_GetWindowFromID(SDL_WindowID id)
  2256. {
  2257. SDL_Window *window;
  2258. if (!_this) {
  2259. SDL_UninitializedVideo();
  2260. return NULL;
  2261. }
  2262. if (id) {
  2263. for (window = _this->windows; window; window = window->next) {
  2264. if (window->id == id) {
  2265. return window;
  2266. }
  2267. }
  2268. }
  2269. SDL_SetError("Invalid window ID"); \
  2270. return NULL;
  2271. }
  2272. SDL_Window *SDL_GetWindowParent(SDL_Window *window)
  2273. {
  2274. CHECK_WINDOW_MAGIC(window, NULL);
  2275. return window->parent;
  2276. }
  2277. SDL_PropertiesID SDL_GetWindowProperties(SDL_Window *window)
  2278. {
  2279. CHECK_WINDOW_MAGIC(window, 0);
  2280. if (window->props == 0) {
  2281. window->props = SDL_CreateProperties();
  2282. }
  2283. return window->props;
  2284. }
  2285. SDL_WindowFlags SDL_GetWindowFlags(SDL_Window *window)
  2286. {
  2287. CHECK_WINDOW_MAGIC(window, 0);
  2288. return window->flags | window->pending_flags;
  2289. }
  2290. bool SDL_SetWindowTitle(SDL_Window *window, const char *title)
  2291. {
  2292. CHECK_WINDOW_MAGIC(window, false);
  2293. CHECK_WINDOW_NOT_POPUP(window, false);
  2294. if (title == window->title) {
  2295. return true;
  2296. }
  2297. SDL_free(window->title);
  2298. window->title = SDL_strdup(title ? title : "");
  2299. if (_this->SetWindowTitle) {
  2300. _this->SetWindowTitle(_this, window);
  2301. }
  2302. return true;
  2303. }
  2304. const char *SDL_GetWindowTitle(SDL_Window *window)
  2305. {
  2306. CHECK_WINDOW_MAGIC(window, "");
  2307. return window->title ? window->title : "";
  2308. }
  2309. bool SDL_SetWindowIcon(SDL_Window *window, SDL_Surface *icon)
  2310. {
  2311. CHECK_WINDOW_MAGIC(window, false);
  2312. if (!icon) {
  2313. return SDL_InvalidParamError("icon");
  2314. }
  2315. SDL_DestroySurface(window->icon);
  2316. // Convert the icon into ARGB8888
  2317. window->icon = SDL_ConvertSurface(icon, SDL_PIXELFORMAT_ARGB8888);
  2318. if (!window->icon) {
  2319. return false;
  2320. }
  2321. if (!_this->SetWindowIcon) {
  2322. return SDL_Unsupported();
  2323. }
  2324. return _this->SetWindowIcon(_this, window, window->icon);
  2325. }
  2326. bool SDL_SetWindowPosition(SDL_Window *window, int x, int y)
  2327. {
  2328. SDL_DisplayID original_displayID;
  2329. CHECK_WINDOW_MAGIC(window, false);
  2330. original_displayID = SDL_GetDisplayForWindow(window);
  2331. if (SDL_WINDOWPOS_ISUNDEFINED(x)) {
  2332. x = window->windowed.x;
  2333. }
  2334. if (SDL_WINDOWPOS_ISUNDEFINED(y)) {
  2335. y = window->windowed.y;
  2336. }
  2337. if (SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
  2338. SDL_DisplayID displayID = original_displayID;
  2339. SDL_Rect bounds;
  2340. if (SDL_WINDOWPOS_ISCENTERED(x) && (x & 0xFFFF)) {
  2341. displayID = (x & 0xFFFF);
  2342. } else if (SDL_WINDOWPOS_ISCENTERED(y) && (y & 0xFFFF)) {
  2343. displayID = (y & 0xFFFF);
  2344. }
  2345. if (displayID == 0 || SDL_GetDisplayIndex(displayID) < 0) {
  2346. displayID = SDL_GetPrimaryDisplay();
  2347. }
  2348. SDL_zero(bounds);
  2349. if (!SDL_GetDisplayUsableBounds(displayID, &bounds) ||
  2350. window->windowed.w > bounds.w ||
  2351. window->windowed.h > bounds.h) {
  2352. if (!SDL_GetDisplayBounds(displayID, &bounds)) {
  2353. return false;
  2354. }
  2355. }
  2356. if (SDL_WINDOWPOS_ISCENTERED(x)) {
  2357. x = bounds.x + (bounds.w - window->windowed.w) / 2;
  2358. }
  2359. if (SDL_WINDOWPOS_ISCENTERED(y)) {
  2360. y = bounds.y + (bounds.h - window->windowed.h) / 2;
  2361. }
  2362. }
  2363. window->floating.x = x;
  2364. window->floating.y = y;
  2365. window->undefined_x = false;
  2366. window->undefined_y = false;
  2367. if (_this->SetWindowPosition) {
  2368. window->is_repositioning = true;
  2369. const bool result = _this->SetWindowPosition(_this, window);
  2370. window->is_repositioning = false;
  2371. if (result) {
  2372. SDL_SyncIfRequired(window);
  2373. }
  2374. return result;
  2375. }
  2376. return SDL_Unsupported();
  2377. }
  2378. bool SDL_GetWindowPosition(SDL_Window *window, int *x, int *y)
  2379. {
  2380. CHECK_WINDOW_MAGIC(window, false);
  2381. // Fullscreen windows are always at their display's origin
  2382. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  2383. SDL_DisplayID displayID;
  2384. if (x) {
  2385. *x = 0;
  2386. }
  2387. if (y) {
  2388. *y = 0;
  2389. }
  2390. /* Find the window's monitor and update to the
  2391. monitor offset. */
  2392. displayID = SDL_GetDisplayForWindow(window);
  2393. if (displayID != 0) {
  2394. SDL_Rect bounds;
  2395. SDL_zero(bounds);
  2396. SDL_GetDisplayBounds(displayID, &bounds);
  2397. if (x) {
  2398. *x = bounds.x;
  2399. }
  2400. if (y) {
  2401. *y = bounds.y;
  2402. }
  2403. }
  2404. } else {
  2405. if (x) {
  2406. *x = window->x;
  2407. }
  2408. if (y) {
  2409. *y = window->y;
  2410. }
  2411. }
  2412. return true;
  2413. }
  2414. bool SDL_SetWindowBordered(SDL_Window *window, bool bordered)
  2415. {
  2416. CHECK_WINDOW_MAGIC(window, false);
  2417. CHECK_WINDOW_NOT_POPUP(window, false);
  2418. const bool want = (bordered != false); // normalize the flag.
  2419. const bool have = !(window->flags & SDL_WINDOW_BORDERLESS);
  2420. if ((want != have) && (_this->SetWindowBordered)) {
  2421. if (want) {
  2422. window->flags &= ~SDL_WINDOW_BORDERLESS;
  2423. } else {
  2424. window->flags |= SDL_WINDOW_BORDERLESS;
  2425. }
  2426. _this->SetWindowBordered(_this, window, want);
  2427. }
  2428. return true;
  2429. }
  2430. bool SDL_SetWindowResizable(SDL_Window *window, bool resizable)
  2431. {
  2432. CHECK_WINDOW_MAGIC(window, false);
  2433. CHECK_WINDOW_NOT_POPUP(window, false);
  2434. const bool want = (resizable != false); // normalize the flag.
  2435. const bool have = ((window->flags & SDL_WINDOW_RESIZABLE) != 0);
  2436. if ((want != have) && (_this->SetWindowResizable)) {
  2437. if (want) {
  2438. window->flags |= SDL_WINDOW_RESIZABLE;
  2439. } else {
  2440. window->flags &= ~SDL_WINDOW_RESIZABLE;
  2441. SDL_copyp(&window->windowed, &window->floating);
  2442. }
  2443. _this->SetWindowResizable(_this, window, want);
  2444. }
  2445. return true;
  2446. }
  2447. bool SDL_SetWindowAlwaysOnTop(SDL_Window *window, bool on_top)
  2448. {
  2449. CHECK_WINDOW_MAGIC(window, false);
  2450. CHECK_WINDOW_NOT_POPUP(window, false);
  2451. const bool want = (on_top != false); // normalize the flag.
  2452. const bool have = ((window->flags & SDL_WINDOW_ALWAYS_ON_TOP) != 0);
  2453. if ((want != have) && (_this->SetWindowAlwaysOnTop)) {
  2454. if (want) {
  2455. window->flags |= SDL_WINDOW_ALWAYS_ON_TOP;
  2456. } else {
  2457. window->flags &= ~SDL_WINDOW_ALWAYS_ON_TOP;
  2458. }
  2459. _this->SetWindowAlwaysOnTop(_this, window, want);
  2460. }
  2461. return true;
  2462. }
  2463. bool SDL_SetWindowSize(SDL_Window *window, int w, int h)
  2464. {
  2465. CHECK_WINDOW_MAGIC(window, false);
  2466. if (w <= 0) {
  2467. return SDL_InvalidParamError("w");
  2468. }
  2469. if (h <= 0) {
  2470. return SDL_InvalidParamError("h");
  2471. }
  2472. // It is possible for the aspect ratio constraints to not satisfy the size constraints.
  2473. // The size constraints will override the aspect ratio constraints so we will apply the
  2474. // the aspect ratio constraints first
  2475. float new_aspect = w / (float)h;
  2476. if (window->max_aspect > 0.0f && new_aspect > window->max_aspect) {
  2477. w = (int)SDL_roundf(h * window->max_aspect);
  2478. } else if (window->min_aspect > 0.0f && new_aspect < window->min_aspect) {
  2479. h = (int)SDL_roundf(w / window->min_aspect);
  2480. }
  2481. // Make sure we don't exceed any window size limits
  2482. if (window->min_w && w < window->min_w) {
  2483. w = window->min_w;
  2484. }
  2485. if (window->max_w && w > window->max_w) {
  2486. w = window->max_w;
  2487. }
  2488. if (window->min_h && h < window->min_h) {
  2489. h = window->min_h;
  2490. }
  2491. if (window->max_h && h > window->max_h) {
  2492. h = window->max_h;
  2493. }
  2494. window->floating.w = w;
  2495. window->floating.h = h;
  2496. if (_this->SetWindowSize) {
  2497. _this->SetWindowSize(_this, window);
  2498. SDL_SyncIfRequired(window);
  2499. } else {
  2500. return SDL_Unsupported();
  2501. }
  2502. return true;
  2503. }
  2504. bool SDL_GetWindowSize(SDL_Window *window, int *w, int *h)
  2505. {
  2506. CHECK_WINDOW_MAGIC(window, false);
  2507. if (w) {
  2508. *w = window->w;
  2509. }
  2510. if (h) {
  2511. *h = window->h;
  2512. }
  2513. return true;
  2514. }
  2515. bool SDL_SetWindowAspectRatio(SDL_Window *window, float min_aspect, float max_aspect)
  2516. {
  2517. CHECK_WINDOW_MAGIC(window, false);
  2518. window->min_aspect = min_aspect;
  2519. window->max_aspect = max_aspect;
  2520. if (_this->SetWindowAspectRatio) {
  2521. _this->SetWindowAspectRatio(_this, window);
  2522. }
  2523. return SDL_SetWindowSize(window, window->floating.w, window->floating.h);
  2524. }
  2525. bool SDL_GetWindowAspectRatio(SDL_Window *window, float *min_aspect, float *max_aspect)
  2526. {
  2527. CHECK_WINDOW_MAGIC(window, false);
  2528. if (min_aspect) {
  2529. *min_aspect = window->min_aspect;
  2530. }
  2531. if (max_aspect) {
  2532. *max_aspect = window->max_aspect;
  2533. }
  2534. return true;
  2535. }
  2536. bool SDL_GetWindowBordersSize(SDL_Window *window, int *top, int *left, int *bottom, int *right)
  2537. {
  2538. int dummy = 0;
  2539. if (!top) {
  2540. top = &dummy;
  2541. }
  2542. if (!left) {
  2543. left = &dummy;
  2544. }
  2545. if (!right) {
  2546. right = &dummy;
  2547. }
  2548. if (!bottom) {
  2549. bottom = &dummy;
  2550. }
  2551. // Always initialize, so applications don't have to care
  2552. *top = *left = *bottom = *right = 0;
  2553. CHECK_WINDOW_MAGIC(window, false);
  2554. if (!_this->GetWindowBordersSize) {
  2555. return SDL_Unsupported();
  2556. }
  2557. return _this->GetWindowBordersSize(_this, window, top, left, bottom, right);
  2558. }
  2559. bool SDL_GetWindowSizeInPixels(SDL_Window *window, int *w, int *h)
  2560. {
  2561. int filter;
  2562. CHECK_WINDOW_MAGIC(window, false);
  2563. if (!w) {
  2564. w = &filter;
  2565. }
  2566. if (!h) {
  2567. h = &filter;
  2568. }
  2569. if (_this->GetWindowSizeInPixels) {
  2570. _this->GetWindowSizeInPixels(_this, window, w, h);
  2571. } else {
  2572. SDL_DisplayID displayID = SDL_GetDisplayForWindow(window);
  2573. const SDL_DisplayMode *mode;
  2574. SDL_GetWindowSize(window, w, h);
  2575. if ((window->flags & SDL_WINDOW_FULLSCREEN) && SDL_GetWindowFullscreenMode(window)) {
  2576. mode = SDL_GetCurrentDisplayMode(displayID);
  2577. } else {
  2578. mode = SDL_GetDesktopDisplayMode(displayID);
  2579. }
  2580. if (mode) {
  2581. *w = (int)SDL_ceilf(*w * mode->pixel_density);
  2582. *h = (int)SDL_ceilf(*h * mode->pixel_density);
  2583. }
  2584. }
  2585. return true;
  2586. }
  2587. bool SDL_SetWindowMinimumSize(SDL_Window *window, int min_w, int min_h)
  2588. {
  2589. int w, h;
  2590. CHECK_WINDOW_MAGIC(window, false);
  2591. if (min_w < 0) {
  2592. return SDL_InvalidParamError("min_w");
  2593. }
  2594. if (min_h < 0) {
  2595. return SDL_InvalidParamError("min_h");
  2596. }
  2597. if ((window->max_w && min_w > window->max_w) ||
  2598. (window->max_h && min_h > window->max_h)) {
  2599. return SDL_SetError("SDL_SetWindowMinimumSize(): Tried to set minimum size larger than maximum size");
  2600. }
  2601. window->min_w = min_w;
  2602. window->min_h = min_h;
  2603. if (_this->SetWindowMinimumSize) {
  2604. _this->SetWindowMinimumSize(_this, window);
  2605. }
  2606. // Ensure that window is not smaller than minimal size
  2607. w = window->min_w ? SDL_max(window->floating.w, window->min_w) : window->floating.w;
  2608. h = window->min_h ? SDL_max(window->floating.h, window->min_h) : window->floating.h;
  2609. return SDL_SetWindowSize(window, w, h);
  2610. }
  2611. bool SDL_GetWindowMinimumSize(SDL_Window *window, int *min_w, int *min_h)
  2612. {
  2613. CHECK_WINDOW_MAGIC(window, false);
  2614. if (min_w) {
  2615. *min_w = window->min_w;
  2616. }
  2617. if (min_h) {
  2618. *min_h = window->min_h;
  2619. }
  2620. return true;
  2621. }
  2622. bool SDL_SetWindowMaximumSize(SDL_Window *window, int max_w, int max_h)
  2623. {
  2624. int w, h;
  2625. CHECK_WINDOW_MAGIC(window, false);
  2626. if (max_w < 0) {
  2627. return SDL_InvalidParamError("max_w");
  2628. }
  2629. if (max_h < 0) {
  2630. return SDL_InvalidParamError("max_h");
  2631. }
  2632. if ((max_w && max_w < window->min_w) ||
  2633. (max_h && max_h < window->min_h)) {
  2634. return SDL_SetError("SDL_SetWindowMaximumSize(): Tried to set maximum size smaller than minimum size");
  2635. }
  2636. window->max_w = max_w;
  2637. window->max_h = max_h;
  2638. if (_this->SetWindowMaximumSize) {
  2639. _this->SetWindowMaximumSize(_this, window);
  2640. }
  2641. // Ensure that window is not larger than maximal size
  2642. w = window->max_w ? SDL_min(window->floating.w, window->max_w) : window->floating.w;
  2643. h = window->max_h ? SDL_min(window->floating.h, window->max_h) : window->floating.h;
  2644. return SDL_SetWindowSize(window, w, h);
  2645. }
  2646. bool SDL_GetWindowMaximumSize(SDL_Window *window, int *max_w, int *max_h)
  2647. {
  2648. CHECK_WINDOW_MAGIC(window, false);
  2649. if (max_w) {
  2650. *max_w = window->max_w;
  2651. }
  2652. if (max_h) {
  2653. *max_h = window->max_h;
  2654. }
  2655. return true;
  2656. }
  2657. bool SDL_ShowWindow(SDL_Window *window)
  2658. {
  2659. SDL_Window *child;
  2660. CHECK_WINDOW_MAGIC(window, false);
  2661. if (!(window->flags & SDL_WINDOW_HIDDEN)) {
  2662. return true;
  2663. }
  2664. // If the parent is hidden, set the flag to restore this when the parent is shown
  2665. if (window->parent && (window->parent->flags & SDL_WINDOW_HIDDEN)) {
  2666. window->restore_on_show = true;
  2667. return true;
  2668. }
  2669. if (_this->ShowWindow) {
  2670. _this->ShowWindow(_this, window);
  2671. } else {
  2672. SDL_SetMouseFocus(window);
  2673. SDL_SetKeyboardFocus(window);
  2674. }
  2675. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_SHOWN, 0, 0);
  2676. // Restore child windows
  2677. for (child = window->first_child; child; child = child->next_sibling) {
  2678. if (!child->restore_on_show && (child->flags & SDL_WINDOW_HIDDEN)) {
  2679. break;
  2680. }
  2681. SDL_ShowWindow(child);
  2682. child->restore_on_show = false;
  2683. }
  2684. return true;
  2685. }
  2686. bool SDL_HideWindow(SDL_Window *window)
  2687. {
  2688. SDL_Window *child;
  2689. CHECK_WINDOW_MAGIC(window, false);
  2690. if (window->flags & SDL_WINDOW_HIDDEN) {
  2691. window->restore_on_show = false;
  2692. return true;
  2693. }
  2694. // Hide all child windows
  2695. for (child = window->first_child; child; child = child->next_sibling) {
  2696. if (child->flags & SDL_WINDOW_HIDDEN) {
  2697. break;
  2698. }
  2699. SDL_HideWindow(child);
  2700. child->restore_on_show = true;
  2701. }
  2702. // Store the flags for restoration later.
  2703. window->pending_flags = window->flags;
  2704. window->is_hiding = true;
  2705. if (_this->HideWindow) {
  2706. _this->HideWindow(_this, window);
  2707. } else {
  2708. SDL_SetMouseFocus(NULL);
  2709. SDL_SetKeyboardFocus(NULL);
  2710. }
  2711. window->is_hiding = false;
  2712. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_HIDDEN, 0, 0);
  2713. return true;
  2714. }
  2715. bool SDL_RaiseWindow(SDL_Window *window)
  2716. {
  2717. CHECK_WINDOW_MAGIC(window, false);
  2718. if (window->flags & SDL_WINDOW_HIDDEN) {
  2719. return true;
  2720. }
  2721. if (_this->RaiseWindow) {
  2722. _this->RaiseWindow(_this, window);
  2723. }
  2724. return true;
  2725. }
  2726. bool SDL_MaximizeWindow(SDL_Window *window)
  2727. {
  2728. CHECK_WINDOW_MAGIC(window, false);
  2729. CHECK_WINDOW_NOT_POPUP(window, false);
  2730. if (!_this->MaximizeWindow) {
  2731. return SDL_Unsupported();
  2732. }
  2733. if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
  2734. return SDL_SetError("A window without the 'SDL_WINDOW_RESIZABLE' flag can't be maximized");
  2735. }
  2736. if (window->flags & SDL_WINDOW_HIDDEN) {
  2737. window->pending_flags |= SDL_WINDOW_MAXIMIZED;
  2738. return true;
  2739. }
  2740. _this->MaximizeWindow(_this, window);
  2741. SDL_SyncIfRequired(window);
  2742. return true;
  2743. }
  2744. bool SDL_MinimizeWindow(SDL_Window *window)
  2745. {
  2746. CHECK_WINDOW_MAGIC(window, false);
  2747. CHECK_WINDOW_NOT_POPUP(window, false);
  2748. if (!_this->MinimizeWindow) {
  2749. return SDL_Unsupported();
  2750. }
  2751. if (window->flags & SDL_WINDOW_HIDDEN) {
  2752. window->pending_flags |= SDL_WINDOW_MINIMIZED;
  2753. return true;
  2754. }
  2755. _this->MinimizeWindow(_this, window);
  2756. SDL_SyncIfRequired(window);
  2757. return true;
  2758. }
  2759. bool SDL_RestoreWindow(SDL_Window *window)
  2760. {
  2761. CHECK_WINDOW_MAGIC(window, false);
  2762. CHECK_WINDOW_NOT_POPUP(window, false);
  2763. if (!_this->RestoreWindow) {
  2764. return SDL_Unsupported();
  2765. }
  2766. if (window->flags & SDL_WINDOW_HIDDEN) {
  2767. window->pending_flags &= ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED);
  2768. return true;
  2769. }
  2770. _this->RestoreWindow(_this, window);
  2771. SDL_SyncIfRequired(window);
  2772. return true;
  2773. }
  2774. bool SDL_SetWindowFullscreen(SDL_Window *window, bool fullscreen)
  2775. {
  2776. bool result;
  2777. CHECK_WINDOW_MAGIC(window, false);
  2778. CHECK_WINDOW_NOT_POPUP(window, false);
  2779. if (window->flags & SDL_WINDOW_HIDDEN) {
  2780. if (fullscreen) {
  2781. window->pending_flags |= SDL_WINDOW_FULLSCREEN;
  2782. } else {
  2783. window->pending_flags &= ~SDL_WINDOW_FULLSCREEN;
  2784. }
  2785. return true;
  2786. }
  2787. if (fullscreen) {
  2788. // Set the current fullscreen mode to the desired mode
  2789. SDL_copyp(&window->current_fullscreen_mode, &window->requested_fullscreen_mode);
  2790. }
  2791. result = SDL_UpdateFullscreenMode(window, fullscreen ? SDL_FULLSCREEN_OP_ENTER : SDL_FULLSCREEN_OP_LEAVE, true);
  2792. if (!fullscreen || !result) {
  2793. // Clear the current fullscreen mode.
  2794. SDL_zero(window->current_fullscreen_mode);
  2795. }
  2796. if (result) {
  2797. SDL_SyncIfRequired(window);
  2798. }
  2799. return result;
  2800. }
  2801. bool SDL_SyncWindow(SDL_Window *window)
  2802. {
  2803. CHECK_WINDOW_MAGIC(window, false)
  2804. if (_this->SyncWindow) {
  2805. return _this->SyncWindow(_this, window);
  2806. } else {
  2807. return true;
  2808. }
  2809. }
  2810. static bool ShouldAttemptTextureFramebuffer(void)
  2811. {
  2812. const char *hint;
  2813. bool attempt_texture_framebuffer = true;
  2814. // The dummy driver never has GPU support, of course.
  2815. if (_this->is_dummy) {
  2816. return false;
  2817. }
  2818. // See if there's a hint override
  2819. hint = SDL_GetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION);
  2820. if (hint && *hint) {
  2821. if (*hint == '0' || SDL_strcasecmp(hint, "false") == 0 || SDL_strcasecmp(hint, SDL_SOFTWARE_RENDERER) == 0) {
  2822. attempt_texture_framebuffer = false;
  2823. } else {
  2824. attempt_texture_framebuffer = true;
  2825. }
  2826. } else {
  2827. // Check for platform specific defaults
  2828. #ifdef SDL_PLATFORM_LINUX
  2829. // On WSL, direct X11 is faster than using OpenGL for window framebuffers, so try to detect WSL and avoid texture framebuffer.
  2830. if ((_this->CreateWindowFramebuffer) && (SDL_strcmp(_this->name, "x11") == 0)) {
  2831. struct stat sb;
  2832. if ((stat("/proc/sys/fs/binfmt_misc/WSLInterop", &sb) == 0) || (stat("/run/WSL", &sb) == 0)) { // if either of these exist, we're on WSL.
  2833. attempt_texture_framebuffer = false;
  2834. }
  2835. }
  2836. #endif
  2837. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK) // GDI BitBlt() is way faster than Direct3D dynamic textures right now. (!!! FIXME: is this still true?)
  2838. if (_this->CreateWindowFramebuffer && (SDL_strcmp(_this->name, "windows") == 0)) {
  2839. attempt_texture_framebuffer = false;
  2840. }
  2841. #endif
  2842. #ifdef SDL_PLATFORM_EMSCRIPTEN
  2843. attempt_texture_framebuffer = false;
  2844. #endif
  2845. }
  2846. return attempt_texture_framebuffer;
  2847. }
  2848. static SDL_Surface *SDL_CreateWindowFramebuffer(SDL_Window *window)
  2849. {
  2850. SDL_PixelFormat format = SDL_PIXELFORMAT_UNKNOWN;
  2851. void *pixels = NULL;
  2852. int pitch = 0;
  2853. bool created_framebuffer = false;
  2854. int w, h;
  2855. SDL_GetWindowSizeInPixels(window, &w, &h);
  2856. /* This will switch the video backend from using a software surface to
  2857. using a GPU texture through the 2D render API, if we think this would
  2858. be more efficient. This only checks once, on demand. */
  2859. if (!_this->checked_texture_framebuffer) {
  2860. if (ShouldAttemptTextureFramebuffer()) {
  2861. if (!SDL_CreateWindowTexture(_this, window, &format, &pixels, &pitch)) {
  2862. /* !!! FIXME: if this failed halfway (made renderer, failed to make texture, etc),
  2863. !!! FIXME: we probably need to clean this up so it doesn't interfere with
  2864. !!! FIXME: a software fallback at the system level (can we blit to an
  2865. !!! FIXME: OpenGL window? etc). */
  2866. } else {
  2867. // future attempts will just try to use a texture framebuffer.
  2868. /* !!! FIXME: maybe we shouldn't override these but check if we used a texture
  2869. !!! FIXME: framebuffer at the right places; is it feasible we could have an
  2870. !!! FIXME: accelerated OpenGL window and a second ends up in software? */
  2871. _this->CreateWindowFramebuffer = SDL_CreateWindowTexture;
  2872. _this->SetWindowFramebufferVSync = SDL_SetWindowTextureVSync;
  2873. _this->GetWindowFramebufferVSync = SDL_GetWindowTextureVSync;
  2874. _this->UpdateWindowFramebuffer = SDL_UpdateWindowTexture;
  2875. _this->DestroyWindowFramebuffer = SDL_DestroyWindowTexture;
  2876. created_framebuffer = true;
  2877. }
  2878. }
  2879. _this->checked_texture_framebuffer = true; // don't check this again.
  2880. }
  2881. if (!created_framebuffer) {
  2882. if (!_this->CreateWindowFramebuffer || !_this->UpdateWindowFramebuffer) {
  2883. SDL_SetError("Window framebuffer support not available");
  2884. return NULL;
  2885. }
  2886. if (!_this->CreateWindowFramebuffer(_this, window, &format, &pixels, &pitch)) {
  2887. return NULL;
  2888. }
  2889. }
  2890. if (window->surface) {
  2891. // We may have gone recursive and already created the surface
  2892. return window->surface;
  2893. }
  2894. return SDL_CreateSurfaceFrom(w, h, format, pixels, pitch);
  2895. }
  2896. bool SDL_WindowHasSurface(SDL_Window *window)
  2897. {
  2898. CHECK_WINDOW_MAGIC(window, false);
  2899. return window->surface ? true : false;
  2900. }
  2901. SDL_Surface *SDL_GetWindowSurface(SDL_Window *window)
  2902. {
  2903. CHECK_WINDOW_MAGIC(window, NULL);
  2904. if (!window->surface_valid) {
  2905. if (window->surface) {
  2906. window->surface->internal_flags &= ~SDL_INTERNAL_SURFACE_DONTFREE;
  2907. SDL_DestroySurface(window->surface);
  2908. window->surface = NULL;
  2909. }
  2910. window->surface = SDL_CreateWindowFramebuffer(window);
  2911. if (window->surface) {
  2912. window->surface_valid = true;
  2913. window->surface->internal_flags |= SDL_INTERNAL_SURFACE_DONTFREE;
  2914. }
  2915. }
  2916. return window->surface;
  2917. }
  2918. bool SDL_SetWindowSurfaceVSync(SDL_Window *window, int vsync)
  2919. {
  2920. CHECK_WINDOW_MAGIC(window, false);
  2921. if (!_this->SetWindowFramebufferVSync) {
  2922. return SDL_Unsupported();
  2923. }
  2924. return _this->SetWindowFramebufferVSync(_this, window, vsync);
  2925. }
  2926. bool SDL_GetWindowSurfaceVSync(SDL_Window *window, int *vsync)
  2927. {
  2928. CHECK_WINDOW_MAGIC(window, false);
  2929. if (!_this->GetWindowFramebufferVSync) {
  2930. return SDL_Unsupported();
  2931. }
  2932. return _this->GetWindowFramebufferVSync(_this, window, vsync);
  2933. }
  2934. bool SDL_UpdateWindowSurface(SDL_Window *window)
  2935. {
  2936. SDL_Rect full_rect;
  2937. CHECK_WINDOW_MAGIC(window, false);
  2938. full_rect.x = 0;
  2939. full_rect.y = 0;
  2940. SDL_GetWindowSizeInPixels(window, &full_rect.w, &full_rect.h);
  2941. return SDL_UpdateWindowSurfaceRects(window, &full_rect, 1);
  2942. }
  2943. bool SDL_UpdateWindowSurfaceRects(SDL_Window *window, const SDL_Rect *rects,
  2944. int numrects)
  2945. {
  2946. CHECK_WINDOW_MAGIC(window, false);
  2947. if (!window->surface_valid) {
  2948. return SDL_SetError("Window surface is invalid, please call SDL_GetWindowSurface() to get a new surface");
  2949. }
  2950. SDL_assert(_this->checked_texture_framebuffer); // we should have done this before we had a valid surface.
  2951. return _this->UpdateWindowFramebuffer(_this, window, rects, numrects);
  2952. }
  2953. bool SDL_DestroyWindowSurface(SDL_Window *window)
  2954. {
  2955. CHECK_WINDOW_MAGIC(window, false);
  2956. if (window->surface) {
  2957. window->surface->internal_flags &= ~SDL_INTERNAL_SURFACE_DONTFREE;
  2958. SDL_DestroySurface(window->surface);
  2959. window->surface = NULL;
  2960. window->surface_valid = false;
  2961. }
  2962. if (_this->checked_texture_framebuffer) { // never checked? No framebuffer to destroy. Don't risk calling the wrong implementation.
  2963. if (_this->DestroyWindowFramebuffer) {
  2964. _this->DestroyWindowFramebuffer(_this, window);
  2965. }
  2966. }
  2967. return true;
  2968. }
  2969. bool SDL_SetWindowOpacity(SDL_Window *window, float opacity)
  2970. {
  2971. bool result;
  2972. CHECK_WINDOW_MAGIC(window, false);
  2973. if (!_this->SetWindowOpacity) {
  2974. return SDL_Unsupported();
  2975. }
  2976. if (opacity < 0.0f) {
  2977. opacity = 0.0f;
  2978. } else if (opacity > 1.0f) {
  2979. opacity = 1.0f;
  2980. }
  2981. result = _this->SetWindowOpacity(_this, window, opacity);
  2982. if (result) {
  2983. window->opacity = opacity;
  2984. }
  2985. return result;
  2986. }
  2987. float SDL_GetWindowOpacity(SDL_Window *window)
  2988. {
  2989. CHECK_WINDOW_MAGIC(window, -1.0f);
  2990. return window->opacity;
  2991. }
  2992. bool SDL_SetWindowParent(SDL_Window *window, SDL_Window *parent)
  2993. {
  2994. CHECK_WINDOW_MAGIC(window, false);
  2995. CHECK_WINDOW_NOT_POPUP(window, false);
  2996. if (parent) {
  2997. CHECK_WINDOW_MAGIC(parent, false);
  2998. CHECK_WINDOW_NOT_POPUP(parent, false);
  2999. }
  3000. if (!_this->SetWindowParent) {
  3001. return SDL_Unsupported();
  3002. }
  3003. if (window->flags & SDL_WINDOW_MODAL) {
  3004. return SDL_SetError("Modal windows cannot change parents; call SDL_SetWindowModal() to clear modal status first.");
  3005. }
  3006. if (window->parent == parent) {
  3007. return true;
  3008. }
  3009. const bool ret = _this->SetWindowParent(_this, window, parent);
  3010. SDL_UpdateWindowHierarchy(window, ret ? parent : NULL);
  3011. return ret;
  3012. }
  3013. bool SDL_SetWindowModal(SDL_Window *window, bool modal)
  3014. {
  3015. CHECK_WINDOW_MAGIC(window, false);
  3016. CHECK_WINDOW_NOT_POPUP(window, false);
  3017. if (!_this->SetWindowModal) {
  3018. return SDL_Unsupported();
  3019. }
  3020. if (modal) {
  3021. if (!window->parent) {
  3022. return SDL_SetError("Window must have a parent to enable the modal state; use SDL_SetWindowParent() to set the parent first.");
  3023. }
  3024. window->flags |= SDL_WINDOW_MODAL;
  3025. } else if (window->flags & SDL_WINDOW_MODAL) {
  3026. window->flags &= ~SDL_WINDOW_MODAL;
  3027. } else {
  3028. return true; // Already not modal, so nothing to do.
  3029. }
  3030. if (window->flags & SDL_WINDOW_HIDDEN) {
  3031. return true;
  3032. }
  3033. return _this->SetWindowModal(_this, window, modal);
  3034. }
  3035. bool SDL_SetWindowFocusable(SDL_Window *window, bool focusable)
  3036. {
  3037. CHECK_WINDOW_MAGIC(window, false);
  3038. const bool want = (focusable != false); // normalize the flag.
  3039. const bool have = !(window->flags & SDL_WINDOW_NOT_FOCUSABLE);
  3040. if ((want != have) && (_this->SetWindowFocusable)) {
  3041. if (want) {
  3042. window->flags &= ~SDL_WINDOW_NOT_FOCUSABLE;
  3043. } else {
  3044. window->flags |= SDL_WINDOW_NOT_FOCUSABLE;
  3045. }
  3046. if (!_this->SetWindowFocusable(_this, window, want)) {
  3047. return false;
  3048. }
  3049. }
  3050. return true;
  3051. }
  3052. void SDL_UpdateWindowGrab(SDL_Window *window)
  3053. {
  3054. bool keyboard_grabbed, mouse_grabbed;
  3055. if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
  3056. if (SDL_GetMouse()->relative_mode || (window->flags & SDL_WINDOW_MOUSE_GRABBED)) {
  3057. mouse_grabbed = true;
  3058. } else {
  3059. mouse_grabbed = false;
  3060. }
  3061. if (window->flags & SDL_WINDOW_KEYBOARD_GRABBED) {
  3062. keyboard_grabbed = true;
  3063. } else {
  3064. keyboard_grabbed = false;
  3065. }
  3066. } else {
  3067. mouse_grabbed = false;
  3068. keyboard_grabbed = false;
  3069. }
  3070. if (mouse_grabbed || keyboard_grabbed) {
  3071. if (_this->grabbed_window && (_this->grabbed_window != window)) {
  3072. // stealing a grab from another window!
  3073. _this->grabbed_window->flags &= ~(SDL_WINDOW_MOUSE_GRABBED | SDL_WINDOW_KEYBOARD_GRABBED);
  3074. if (_this->SetWindowMouseGrab) {
  3075. _this->SetWindowMouseGrab(_this, _this->grabbed_window, false);
  3076. }
  3077. if (_this->SetWindowKeyboardGrab) {
  3078. _this->SetWindowKeyboardGrab(_this, _this->grabbed_window, false);
  3079. }
  3080. }
  3081. _this->grabbed_window = window;
  3082. } else if (_this->grabbed_window == window) {
  3083. _this->grabbed_window = NULL; // ungrabbing input.
  3084. }
  3085. if (_this->SetWindowMouseGrab) {
  3086. if (!_this->SetWindowMouseGrab(_this, window, mouse_grabbed)) {
  3087. window->flags &= ~SDL_WINDOW_MOUSE_GRABBED;
  3088. }
  3089. }
  3090. if (_this->SetWindowKeyboardGrab) {
  3091. if (!_this->SetWindowKeyboardGrab(_this, window, keyboard_grabbed)) {
  3092. window->flags &= ~SDL_WINDOW_KEYBOARD_GRABBED;
  3093. }
  3094. }
  3095. if (_this->grabbed_window && !(_this->grabbed_window->flags & (SDL_WINDOW_MOUSE_GRABBED | SDL_WINDOW_KEYBOARD_GRABBED))) {
  3096. _this->grabbed_window = NULL;
  3097. }
  3098. }
  3099. bool SDL_SetWindowKeyboardGrab(SDL_Window *window, bool grabbed)
  3100. {
  3101. CHECK_WINDOW_MAGIC(window, false);
  3102. CHECK_WINDOW_NOT_POPUP(window, false);
  3103. if (window->flags & SDL_WINDOW_HIDDEN) {
  3104. if (grabbed) {
  3105. window->pending_flags |= SDL_WINDOW_KEYBOARD_GRABBED;
  3106. } else {
  3107. window->pending_flags &= ~SDL_WINDOW_KEYBOARD_GRABBED;
  3108. }
  3109. return true;
  3110. }
  3111. if (!!grabbed == !!(window->flags & SDL_WINDOW_KEYBOARD_GRABBED)) {
  3112. return true;
  3113. }
  3114. if (grabbed) {
  3115. window->flags |= SDL_WINDOW_KEYBOARD_GRABBED;
  3116. } else {
  3117. window->flags &= ~SDL_WINDOW_KEYBOARD_GRABBED;
  3118. }
  3119. SDL_UpdateWindowGrab(window);
  3120. if (grabbed && !(window->flags & SDL_WINDOW_KEYBOARD_GRABBED)) {
  3121. return false;
  3122. }
  3123. return true;
  3124. }
  3125. bool SDL_SetWindowMouseGrab(SDL_Window *window, bool grabbed)
  3126. {
  3127. CHECK_WINDOW_MAGIC(window, false);
  3128. CHECK_WINDOW_NOT_POPUP(window, false);
  3129. if (window->flags & SDL_WINDOW_HIDDEN) {
  3130. if (grabbed) {
  3131. window->pending_flags |= SDL_WINDOW_MOUSE_GRABBED;
  3132. } else {
  3133. window->pending_flags &= ~SDL_WINDOW_MOUSE_GRABBED;
  3134. }
  3135. return true;
  3136. }
  3137. if (!!grabbed == !!(window->flags & SDL_WINDOW_MOUSE_GRABBED)) {
  3138. return true;
  3139. }
  3140. if (grabbed) {
  3141. window->flags |= SDL_WINDOW_MOUSE_GRABBED;
  3142. } else {
  3143. window->flags &= ~SDL_WINDOW_MOUSE_GRABBED;
  3144. }
  3145. SDL_UpdateWindowGrab(window);
  3146. if (grabbed && !(window->flags & SDL_WINDOW_MOUSE_GRABBED)) {
  3147. return false;
  3148. }
  3149. return true;
  3150. }
  3151. bool SDL_GetWindowKeyboardGrab(SDL_Window *window)
  3152. {
  3153. CHECK_WINDOW_MAGIC(window, false);
  3154. return window == _this->grabbed_window && (_this->grabbed_window->flags & SDL_WINDOW_KEYBOARD_GRABBED);
  3155. }
  3156. bool SDL_GetWindowMouseGrab(SDL_Window *window)
  3157. {
  3158. CHECK_WINDOW_MAGIC(window, false);
  3159. return window == _this->grabbed_window && (_this->grabbed_window->flags & SDL_WINDOW_MOUSE_GRABBED);
  3160. }
  3161. SDL_Window *SDL_GetGrabbedWindow(void)
  3162. {
  3163. if (_this->grabbed_window &&
  3164. (_this->grabbed_window->flags & (SDL_WINDOW_MOUSE_GRABBED | SDL_WINDOW_KEYBOARD_GRABBED)) != 0) {
  3165. return _this->grabbed_window;
  3166. } else {
  3167. return NULL;
  3168. }
  3169. }
  3170. bool SDL_SetWindowMouseRect(SDL_Window *window, const SDL_Rect *rect)
  3171. {
  3172. CHECK_WINDOW_MAGIC(window, false);
  3173. if (rect) {
  3174. SDL_memcpy(&window->mouse_rect, rect, sizeof(*rect));
  3175. } else {
  3176. SDL_zero(window->mouse_rect);
  3177. }
  3178. if (_this->SetWindowMouseRect) {
  3179. return _this->SetWindowMouseRect(_this, window);
  3180. }
  3181. return true;
  3182. }
  3183. const SDL_Rect *SDL_GetWindowMouseRect(SDL_Window *window)
  3184. {
  3185. CHECK_WINDOW_MAGIC(window, NULL);
  3186. if (SDL_RectEmpty(&window->mouse_rect)) {
  3187. return NULL;
  3188. } else {
  3189. return &window->mouse_rect;
  3190. }
  3191. }
  3192. bool SDL_SetWindowRelativeMouseMode(SDL_Window *window, bool enabled)
  3193. {
  3194. CHECK_WINDOW_MAGIC(window, false);
  3195. /* If the app toggles relative mode directly, it probably shouldn't
  3196. * also be emulating it using repeated mouse warps, so disable
  3197. * mouse warp emulation by default.
  3198. */
  3199. SDL_DisableMouseWarpEmulation();
  3200. if (enabled == SDL_GetWindowRelativeMouseMode(window)) {
  3201. return true;
  3202. }
  3203. if (enabled) {
  3204. window->flags |= SDL_WINDOW_MOUSE_RELATIVE_MODE;
  3205. } else {
  3206. window->flags &= ~SDL_WINDOW_MOUSE_RELATIVE_MODE;
  3207. }
  3208. SDL_UpdateRelativeMouseMode();
  3209. return true;
  3210. }
  3211. bool SDL_GetWindowRelativeMouseMode(SDL_Window *window)
  3212. {
  3213. CHECK_WINDOW_MAGIC(window, false);
  3214. if (window->flags & SDL_WINDOW_MOUSE_RELATIVE_MODE) {
  3215. return true;
  3216. } else {
  3217. return false;
  3218. }
  3219. }
  3220. bool SDL_FlashWindow(SDL_Window *window, SDL_FlashOperation operation)
  3221. {
  3222. CHECK_WINDOW_MAGIC(window, false);
  3223. CHECK_WINDOW_NOT_POPUP(window, false);
  3224. if (_this->FlashWindow) {
  3225. return _this->FlashWindow(_this, window, operation);
  3226. }
  3227. return SDL_Unsupported();
  3228. }
  3229. void SDL_OnWindowShown(SDL_Window *window)
  3230. {
  3231. // Set window state if we have pending window flags cached
  3232. ApplyWindowFlags(window, window->pending_flags);
  3233. window->pending_flags = 0;
  3234. }
  3235. void SDL_OnWindowHidden(SDL_Window *window)
  3236. {
  3237. /* Store the maximized and fullscreen flags for restoration later, in case
  3238. * this was initiated by the window manager due to the window being unmapped
  3239. * when minimized.
  3240. */
  3241. window->pending_flags |= (window->flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_MAXIMIZED));
  3242. // The window is already hidden at this point, so just change the mode back if necessary.
  3243. SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_LEAVE, false);
  3244. }
  3245. void SDL_OnWindowDisplayChanged(SDL_Window *window)
  3246. {
  3247. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  3248. SDL_DisplayID displayID = SDL_GetDisplayForWindowPosition(window);
  3249. if (window->requested_fullscreen_mode.w != 0 || window->requested_fullscreen_mode.h != 0) {
  3250. bool include_high_density_modes = false;
  3251. if (window->requested_fullscreen_mode.pixel_density > 1.0f) {
  3252. include_high_density_modes = true;
  3253. }
  3254. SDL_GetClosestFullscreenDisplayMode(displayID, window->requested_fullscreen_mode.w, window->requested_fullscreen_mode.h, window->requested_fullscreen_mode.refresh_rate, include_high_density_modes, &window->current_fullscreen_mode);
  3255. } else {
  3256. SDL_zero(window->current_fullscreen_mode);
  3257. }
  3258. if (SDL_WINDOW_FULLSCREEN_VISIBLE(window)) {
  3259. SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_UPDATE, true);
  3260. }
  3261. }
  3262. SDL_CheckWindowPixelSizeChanged(window);
  3263. }
  3264. void SDL_OnWindowMoved(SDL_Window *window)
  3265. {
  3266. SDL_CheckWindowDisplayChanged(window);
  3267. }
  3268. void SDL_OnWindowResized(SDL_Window *window)
  3269. {
  3270. SDL_CheckWindowDisplayChanged(window);
  3271. SDL_CheckWindowPixelSizeChanged(window);
  3272. SDL_CheckWindowSafeAreaChanged(window);
  3273. if ((window->flags & SDL_WINDOW_TRANSPARENT) && _this->UpdateWindowShape) {
  3274. SDL_Surface *surface = (SDL_Surface *)SDL_GetPointerProperty(window->props, SDL_PROP_WINDOW_SHAPE_POINTER, NULL);
  3275. if (surface) {
  3276. _this->UpdateWindowShape(_this, window, surface);
  3277. }
  3278. }
  3279. }
  3280. void SDL_CheckWindowPixelSizeChanged(SDL_Window *window)
  3281. {
  3282. int pixel_w = 0, pixel_h = 0;
  3283. SDL_GetWindowSizeInPixels(window, &pixel_w, &pixel_h);
  3284. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED, pixel_w, pixel_h);
  3285. SDL_CheckWindowDisplayScaleChanged(window);
  3286. }
  3287. void SDL_OnWindowPixelSizeChanged(SDL_Window *window)
  3288. {
  3289. window->surface_valid = false;
  3290. }
  3291. static void SDL_CheckWindowSafeAreaChanged(SDL_Window *window)
  3292. {
  3293. SDL_Rect rect;
  3294. rect.x = window->safe_inset_left;
  3295. rect.y = window->safe_inset_top;
  3296. rect.w = window->w - (window->safe_inset_right + window->safe_inset_left);
  3297. rect.h = window->h - (window->safe_inset_top + window->safe_inset_bottom);
  3298. if (SDL_memcmp(&rect, &window->safe_rect, sizeof(rect)) != 0) {
  3299. SDL_copyp(&window->safe_rect, &rect);
  3300. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_SAFE_AREA_CHANGED, 0, 0);
  3301. }
  3302. }
  3303. void SDL_SetWindowSafeAreaInsets(SDL_Window *window, int left, int right, int top, int bottom)
  3304. {
  3305. window->safe_inset_left = left;
  3306. window->safe_inset_right = right;
  3307. window->safe_inset_top = top;
  3308. window->safe_inset_bottom = bottom;
  3309. SDL_CheckWindowSafeAreaChanged(window);
  3310. }
  3311. bool SDL_GetWindowSafeArea(SDL_Window *window, SDL_Rect *rect)
  3312. {
  3313. if (rect) {
  3314. SDL_zerop(rect);
  3315. }
  3316. CHECK_WINDOW_MAGIC(window, false);
  3317. if (rect) {
  3318. if (SDL_RectEmpty(&window->safe_rect)) {
  3319. rect->w = window->w;
  3320. rect->h = window->h;
  3321. } else {
  3322. SDL_copyp(rect, &window->safe_rect);
  3323. }
  3324. }
  3325. return true;
  3326. }
  3327. void SDL_OnWindowMinimized(SDL_Window *window)
  3328. {
  3329. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  3330. SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_LEAVE, false);
  3331. }
  3332. }
  3333. void SDL_OnWindowMaximized(SDL_Window *window)
  3334. {
  3335. }
  3336. void SDL_OnWindowRestored(SDL_Window *window)
  3337. {
  3338. /*
  3339. * FIXME: Is this fine to just remove this, or should it be preserved just
  3340. * for the fullscreen case? In principle it seems like just hiding/showing
  3341. * windows shouldn't affect the stacking order; maybe the right fix is to
  3342. * re-decouple OnWindowShown and OnWindowRestored.
  3343. */
  3344. // SDL_RaiseWindow(window);
  3345. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  3346. SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_ENTER, false);
  3347. }
  3348. }
  3349. void SDL_OnWindowEnter(SDL_Window *window)
  3350. {
  3351. if (_this->OnWindowEnter) {
  3352. _this->OnWindowEnter(_this, window);
  3353. }
  3354. }
  3355. void SDL_OnWindowLeave(SDL_Window *window)
  3356. {
  3357. }
  3358. void SDL_OnWindowFocusGained(SDL_Window *window)
  3359. {
  3360. SDL_Mouse *mouse = SDL_GetMouse();
  3361. if (mouse && mouse->relative_mode) {
  3362. SDL_SetMouseFocus(window);
  3363. if (mouse->relative_mode_warp) {
  3364. SDL_PerformWarpMouseInWindow(window, (float)window->w / 2.0f, (float)window->h / 2.0f, true);
  3365. }
  3366. }
  3367. SDL_UpdateWindowGrab(window);
  3368. }
  3369. static bool SDL_ShouldMinimizeOnFocusLoss(SDL_Window *window)
  3370. {
  3371. const char *hint;
  3372. if (!(window->flags & SDL_WINDOW_FULLSCREEN) || window->is_destroying) {
  3373. return false;
  3374. }
  3375. #if defined(SDL_PLATFORM_MACOS) && defined(SDL_VIDEO_DRIVER_COCOA)
  3376. if (SDL_strcmp(_this->name, "cocoa") == 0) { // don't do this for X11, etc
  3377. if (Cocoa_IsWindowInFullscreenSpace(window)) {
  3378. return false;
  3379. }
  3380. }
  3381. #endif
  3382. #ifdef SDL_PLATFORM_ANDROID
  3383. {
  3384. extern bool Android_JNI_ShouldMinimizeOnFocusLoss(void);
  3385. if (!Android_JNI_ShouldMinimizeOnFocusLoss()) {
  3386. return false;
  3387. }
  3388. }
  3389. #endif
  3390. // Real fullscreen windows should minimize on focus loss so the desktop video mode is restored
  3391. hint = SDL_GetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS);
  3392. if (!hint || !*hint || SDL_strcasecmp(hint, "auto") == 0) {
  3393. if (window->fullscreen_exclusive && !SDL_ModeSwitchingEmulated(_this)) {
  3394. return true;
  3395. } else {
  3396. return false;
  3397. }
  3398. }
  3399. return SDL_GetHintBoolean(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, false);
  3400. }
  3401. void SDL_OnWindowFocusLost(SDL_Window *window)
  3402. {
  3403. SDL_UpdateWindowGrab(window);
  3404. if (SDL_ShouldMinimizeOnFocusLoss(window)) {
  3405. SDL_MinimizeWindow(window);
  3406. }
  3407. }
  3408. SDL_Window *SDL_GetToplevelForKeyboardFocus(void)
  3409. {
  3410. SDL_Window *focus = SDL_GetKeyboardFocus();
  3411. if (focus) {
  3412. // Get the toplevel parent window.
  3413. while (focus->parent) {
  3414. focus = focus->parent;
  3415. }
  3416. }
  3417. return focus;
  3418. }
  3419. void SDL_DestroyWindow(SDL_Window *window)
  3420. {
  3421. CHECK_WINDOW_MAGIC(window,);
  3422. window->is_destroying = true;
  3423. // Destroy any child windows of this window
  3424. while (window->first_child) {
  3425. SDL_DestroyWindow(window->first_child);
  3426. }
  3427. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_DESTROYED, 0, 0);
  3428. SDL_Renderer *renderer = SDL_GetRenderer(window);
  3429. if (renderer) {
  3430. SDL_DestroyRendererWithoutFreeing(renderer);
  3431. }
  3432. // Restore video mode, etc.
  3433. SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_LEAVE, true);
  3434. if (!(window->flags & SDL_WINDOW_EXTERNAL)) {
  3435. SDL_HideWindow(window);
  3436. }
  3437. SDL_DestroyProperties(window->text_input_props);
  3438. SDL_DestroyProperties(window->props);
  3439. /* Clear the modal status, but don't unset the parent just yet, as it
  3440. * may be needed later in the destruction process if a backend needs
  3441. * to update the input focus.
  3442. */
  3443. if (_this->SetWindowModal && (window->flags & SDL_WINDOW_MODAL)) {
  3444. _this->SetWindowModal(_this, window, false);
  3445. }
  3446. // Make sure the destroyed window isn't referenced by any display as a fullscreen window.
  3447. for (int i = 0; i < _this->num_displays; ++i) {
  3448. if (_this->displays[i]->fullscreen_window == window) {
  3449. _this->displays[i]->fullscreen_window = NULL;
  3450. }
  3451. }
  3452. // Make sure this window no longer has focus
  3453. if (SDL_GetKeyboardFocus() == window) {
  3454. SDL_SetKeyboardFocus(NULL);
  3455. }
  3456. if ((window->flags & SDL_WINDOW_MOUSE_CAPTURE)) {
  3457. SDL_UpdateMouseCapture(true);
  3458. }
  3459. if (SDL_GetMouseFocus() == window) {
  3460. SDL_SetMouseFocus(NULL);
  3461. }
  3462. SDL_DestroyWindowSurface(window);
  3463. // Make no context current if this is the current context window
  3464. if (window->flags & SDL_WINDOW_OPENGL) {
  3465. if (_this->current_glwin == window) {
  3466. SDL_GL_MakeCurrent(window, NULL);
  3467. }
  3468. }
  3469. if (_this->DestroyWindow) {
  3470. _this->DestroyWindow(_this, window);
  3471. }
  3472. // Unload the graphics libraries after the window is destroyed, which may clean up EGL surfaces
  3473. if (window->flags & SDL_WINDOW_OPENGL) {
  3474. SDL_GL_UnloadLibrary();
  3475. }
  3476. if (window->flags & SDL_WINDOW_VULKAN) {
  3477. SDL_Vulkan_UnloadLibrary();
  3478. }
  3479. if (_this->grabbed_window == window) {
  3480. _this->grabbed_window = NULL; // ungrabbing input.
  3481. }
  3482. if (_this->current_glwin == window) {
  3483. _this->current_glwin = NULL;
  3484. }
  3485. if (_this->wakeup_window == window) {
  3486. _this->wakeup_window = NULL;
  3487. }
  3488. // Now invalidate magic
  3489. SDL_SetObjectValid(window, SDL_OBJECT_TYPE_WINDOW, false);
  3490. // Free memory associated with the window
  3491. SDL_free(window->title);
  3492. SDL_DestroySurface(window->icon);
  3493. // Unlink the window from its siblings.
  3494. SDL_UpdateWindowHierarchy(window, NULL);
  3495. // Unlink the window from the global window list
  3496. if (window->next) {
  3497. window->next->prev = window->prev;
  3498. }
  3499. if (window->prev) {
  3500. window->prev->next = window->next;
  3501. } else {
  3502. _this->windows = window->next;
  3503. }
  3504. SDL_free(window);
  3505. }
  3506. bool SDL_ScreenSaverEnabled(void)
  3507. {
  3508. if (!_this) {
  3509. return true;
  3510. }
  3511. return !_this->suspend_screensaver;
  3512. }
  3513. bool SDL_EnableScreenSaver(void)
  3514. {
  3515. if (!_this) {
  3516. return SDL_UninitializedVideo();
  3517. }
  3518. if (!_this->suspend_screensaver) {
  3519. return true;
  3520. }
  3521. _this->suspend_screensaver = false;
  3522. if (_this->SuspendScreenSaver) {
  3523. return _this->SuspendScreenSaver(_this);
  3524. }
  3525. return SDL_Unsupported();
  3526. }
  3527. bool SDL_DisableScreenSaver(void)
  3528. {
  3529. if (!_this) {
  3530. return SDL_UninitializedVideo();
  3531. }
  3532. if (_this->suspend_screensaver) {
  3533. return true;
  3534. }
  3535. _this->suspend_screensaver = true;
  3536. if (_this->SuspendScreenSaver) {
  3537. return _this->SuspendScreenSaver(_this);
  3538. }
  3539. return SDL_Unsupported();
  3540. }
  3541. void SDL_VideoQuit(void)
  3542. {
  3543. int i;
  3544. if (!_this) {
  3545. return;
  3546. }
  3547. // Halt event processing before doing anything else
  3548. SDL_QuitPen();
  3549. SDL_QuitTouch();
  3550. SDL_QuitMouse();
  3551. SDL_QuitKeyboard();
  3552. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  3553. SDL_EnableScreenSaver();
  3554. // Clean up the system video
  3555. while (_this->windows) {
  3556. SDL_DestroyWindow(_this->windows);
  3557. }
  3558. _this->VideoQuit(_this);
  3559. for (i = _this->num_displays; i--; ) {
  3560. SDL_VideoDisplay *display = _this->displays[i];
  3561. SDL_DelVideoDisplay(display->id, false);
  3562. }
  3563. SDL_assert(_this->num_displays == 0);
  3564. SDL_free(_this->displays);
  3565. _this->displays = NULL;
  3566. if (_this->primary_selection_text) {
  3567. SDL_free(_this->primary_selection_text);
  3568. _this->primary_selection_text = NULL;
  3569. }
  3570. _this->free(_this);
  3571. _this = NULL;
  3572. }
  3573. bool SDL_GL_LoadLibrary(const char *path)
  3574. {
  3575. bool result;
  3576. if (!_this) {
  3577. return SDL_UninitializedVideo();
  3578. }
  3579. if (_this->gl_config.driver_loaded) {
  3580. if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
  3581. return SDL_SetError("OpenGL library already loaded");
  3582. }
  3583. result = true;
  3584. } else {
  3585. if (!_this->GL_LoadLibrary) {
  3586. return SDL_DllNotSupported("OpenGL");
  3587. }
  3588. result = _this->GL_LoadLibrary(_this, path);
  3589. }
  3590. if (result) {
  3591. ++_this->gl_config.driver_loaded;
  3592. } else {
  3593. if (_this->GL_UnloadLibrary) {
  3594. _this->GL_UnloadLibrary(_this);
  3595. }
  3596. }
  3597. return result;
  3598. }
  3599. SDL_FunctionPointer SDL_GL_GetProcAddress(const char *proc)
  3600. {
  3601. SDL_FunctionPointer func;
  3602. if (!_this) {
  3603. SDL_UninitializedVideo();
  3604. return NULL;
  3605. }
  3606. func = NULL;
  3607. if (_this->GL_GetProcAddress) {
  3608. if (_this->gl_config.driver_loaded) {
  3609. func = _this->GL_GetProcAddress(_this, proc);
  3610. } else {
  3611. SDL_SetError("No GL driver has been loaded");
  3612. }
  3613. } else {
  3614. SDL_SetError("No dynamic GL support in current SDL video driver (%s)", _this->name);
  3615. }
  3616. return func;
  3617. }
  3618. SDL_FunctionPointer SDL_EGL_GetProcAddress(const char *proc)
  3619. {
  3620. #ifdef SDL_VIDEO_OPENGL_EGL
  3621. SDL_FunctionPointer func;
  3622. if (!_this) {
  3623. SDL_UninitializedVideo();
  3624. return NULL;
  3625. }
  3626. func = NULL;
  3627. if (_this->egl_data) {
  3628. func = SDL_EGL_GetProcAddressInternal(_this, proc);
  3629. } else {
  3630. SDL_SetError("No EGL library has been loaded");
  3631. }
  3632. return func;
  3633. #else
  3634. SDL_SetError("SDL was not built with EGL support");
  3635. return NULL;
  3636. #endif
  3637. }
  3638. void SDL_GL_UnloadLibrary(void)
  3639. {
  3640. if (!_this) {
  3641. SDL_UninitializedVideo();
  3642. return;
  3643. }
  3644. if (_this->gl_config.driver_loaded > 0) {
  3645. if (--_this->gl_config.driver_loaded > 0) {
  3646. return;
  3647. }
  3648. if (_this->GL_UnloadLibrary) {
  3649. _this->GL_UnloadLibrary(_this);
  3650. }
  3651. }
  3652. }
  3653. #if defined(SDL_VIDEO_OPENGL) || defined(SDL_VIDEO_OPENGL_ES) || defined(SDL_VIDEO_OPENGL_ES2)
  3654. typedef GLenum (APIENTRY* PFNGLGETERRORPROC) (void);
  3655. typedef void (APIENTRY* PFNGLGETINTEGERVPROC) (GLenum pname, GLint *params);
  3656. typedef const GLubyte *(APIENTRY* PFNGLGETSTRINGPROC) (GLenum name);
  3657. #ifndef SDL_VIDEO_OPENGL
  3658. typedef const GLubyte *(APIENTRY* PFNGLGETSTRINGIPROC) (GLenum name, GLuint index);
  3659. #endif
  3660. static SDL_INLINE bool isAtLeastGL3(const char *verstr)
  3661. {
  3662. return verstr && (SDL_atoi(verstr) >= 3);
  3663. }
  3664. #endif // SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
  3665. bool SDL_GL_ExtensionSupported(const char *extension)
  3666. {
  3667. #if defined(SDL_VIDEO_OPENGL) || defined(SDL_VIDEO_OPENGL_ES) || defined(SDL_VIDEO_OPENGL_ES2)
  3668. PFNGLGETSTRINGPROC glGetStringFunc;
  3669. const char *extensions;
  3670. const char *start;
  3671. const char *where, *terminator;
  3672. // Extension names should not have spaces.
  3673. where = SDL_strchr(extension, ' ');
  3674. if (where || *extension == '\0') {
  3675. return false;
  3676. }
  3677. // See if there's a hint or environment variable override
  3678. start = SDL_GetHint(extension);
  3679. if (start && *start == '0') {
  3680. return false;
  3681. }
  3682. // Lookup the available extensions
  3683. glGetStringFunc = (PFNGLGETSTRINGPROC)SDL_GL_GetProcAddress("glGetString");
  3684. if (!glGetStringFunc) {
  3685. return false;
  3686. }
  3687. if (isAtLeastGL3((const char *)glGetStringFunc(GL_VERSION))) {
  3688. PFNGLGETSTRINGIPROC glGetStringiFunc;
  3689. PFNGLGETINTEGERVPROC glGetIntegervFunc;
  3690. GLint num_exts = 0;
  3691. GLint i;
  3692. glGetStringiFunc = (PFNGLGETSTRINGIPROC)SDL_GL_GetProcAddress("glGetStringi");
  3693. glGetIntegervFunc = (PFNGLGETINTEGERVPROC)SDL_GL_GetProcAddress("glGetIntegerv");
  3694. if ((!glGetStringiFunc) || (!glGetIntegervFunc)) {
  3695. return false;
  3696. }
  3697. #ifndef GL_NUM_EXTENSIONS
  3698. #define GL_NUM_EXTENSIONS 0x821D
  3699. #endif
  3700. glGetIntegervFunc(GL_NUM_EXTENSIONS, &num_exts);
  3701. for (i = 0; i < num_exts; i++) {
  3702. const char *thisext = (const char *)glGetStringiFunc(GL_EXTENSIONS, i);
  3703. if (SDL_strcmp(thisext, extension) == 0) {
  3704. return true;
  3705. }
  3706. }
  3707. return false;
  3708. }
  3709. // Try the old way with glGetString(GL_EXTENSIONS) ...
  3710. extensions = (const char *)glGetStringFunc(GL_EXTENSIONS);
  3711. if (!extensions) {
  3712. return false;
  3713. }
  3714. /*
  3715. * It takes a bit of care to be fool-proof about parsing the OpenGL
  3716. * extensions string. Don't be fooled by sub-strings, etc.
  3717. */
  3718. start = extensions;
  3719. for (;;) {
  3720. where = SDL_strstr(start, extension);
  3721. if (!where) {
  3722. break;
  3723. }
  3724. terminator = where + SDL_strlen(extension);
  3725. if (where == extensions || *(where - 1) == ' ') {
  3726. if (*terminator == ' ' || *terminator == '\0') {
  3727. return true;
  3728. }
  3729. }
  3730. start = terminator;
  3731. }
  3732. return false;
  3733. #else
  3734. return false;
  3735. #endif
  3736. }
  3737. /* Deduce supported ES profile versions from the supported
  3738. ARB_ES*_compatibility extensions. There is no direct query.
  3739. This is normally only called when the OpenGL driver supports
  3740. {GLX,WGL}_EXT_create_context_es2_profile.
  3741. */
  3742. void SDL_GL_DeduceMaxSupportedESProfile(int *major, int *minor)
  3743. {
  3744. // THIS REQUIRES AN EXISTING GL CONTEXT THAT HAS BEEN MADE CURRENT.
  3745. // Please refer to https://bugzilla.libsdl.org/show_bug.cgi?id=3725 for discussion.
  3746. #if defined(SDL_VIDEO_OPENGL) || defined(SDL_VIDEO_OPENGL_ES) || defined(SDL_VIDEO_OPENGL_ES2)
  3747. /* XXX This is fragile; it will break in the event of release of
  3748. * new versions of OpenGL ES.
  3749. */
  3750. if (SDL_GL_ExtensionSupported("GL_ARB_ES3_2_compatibility")) {
  3751. *major = 3;
  3752. *minor = 2;
  3753. } else if (SDL_GL_ExtensionSupported("GL_ARB_ES3_1_compatibility")) {
  3754. *major = 3;
  3755. *minor = 1;
  3756. } else if (SDL_GL_ExtensionSupported("GL_ARB_ES3_compatibility")) {
  3757. *major = 3;
  3758. *minor = 0;
  3759. } else {
  3760. *major = 2;
  3761. *minor = 0;
  3762. }
  3763. #endif
  3764. }
  3765. void SDL_EGL_SetAttributeCallbacks(SDL_EGLAttribArrayCallback platformAttribCallback,
  3766. SDL_EGLIntArrayCallback surfaceAttribCallback,
  3767. SDL_EGLIntArrayCallback contextAttribCallback,
  3768. void *userdata)
  3769. {
  3770. if (!_this) {
  3771. return;
  3772. }
  3773. _this->egl_platformattrib_callback = platformAttribCallback;
  3774. _this->egl_surfaceattrib_callback = surfaceAttribCallback;
  3775. _this->egl_contextattrib_callback = contextAttribCallback;
  3776. _this->egl_attrib_callback_userdata = userdata;
  3777. }
  3778. void SDL_GL_ResetAttributes(void)
  3779. {
  3780. if (!_this) {
  3781. return;
  3782. }
  3783. _this->egl_platformattrib_callback = NULL;
  3784. _this->egl_surfaceattrib_callback = NULL;
  3785. _this->egl_contextattrib_callback = NULL;
  3786. _this->egl_attrib_callback_userdata = NULL;
  3787. _this->gl_config.red_size = 8;
  3788. _this->gl_config.green_size = 8;
  3789. _this->gl_config.blue_size = 8;
  3790. _this->gl_config.alpha_size = 8;
  3791. _this->gl_config.buffer_size = 0;
  3792. _this->gl_config.depth_size = 16;
  3793. _this->gl_config.stencil_size = 0;
  3794. _this->gl_config.double_buffer = 1;
  3795. _this->gl_config.accum_red_size = 0;
  3796. _this->gl_config.accum_green_size = 0;
  3797. _this->gl_config.accum_blue_size = 0;
  3798. _this->gl_config.accum_alpha_size = 0;
  3799. _this->gl_config.stereo = 0;
  3800. _this->gl_config.multisamplebuffers = 0;
  3801. _this->gl_config.multisamplesamples = 0;
  3802. _this->gl_config.floatbuffers = 0;
  3803. _this->gl_config.retained_backing = 1;
  3804. _this->gl_config.accelerated = -1; // accelerated or not, both are fine
  3805. #ifdef SDL_VIDEO_OPENGL
  3806. _this->gl_config.major_version = 2;
  3807. _this->gl_config.minor_version = 1;
  3808. _this->gl_config.profile_mask = 0;
  3809. #elif defined(SDL_VIDEO_OPENGL_ES2)
  3810. _this->gl_config.major_version = 2;
  3811. _this->gl_config.minor_version = 0;
  3812. _this->gl_config.profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
  3813. #elif defined(SDL_VIDEO_OPENGL_ES)
  3814. _this->gl_config.major_version = 1;
  3815. _this->gl_config.minor_version = 1;
  3816. _this->gl_config.profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
  3817. #endif
  3818. if (_this->GL_DefaultProfileConfig) {
  3819. _this->GL_DefaultProfileConfig(_this, &_this->gl_config.profile_mask,
  3820. &_this->gl_config.major_version,
  3821. &_this->gl_config.minor_version);
  3822. }
  3823. _this->gl_config.flags = 0;
  3824. _this->gl_config.framebuffer_srgb_capable = 0;
  3825. _this->gl_config.no_error = 0;
  3826. _this->gl_config.release_behavior = SDL_GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH;
  3827. _this->gl_config.reset_notification = SDL_GL_CONTEXT_RESET_NO_NOTIFICATION;
  3828. _this->gl_config.share_with_current_context = 0;
  3829. _this->gl_config.egl_platform = 0;
  3830. }
  3831. bool SDL_GL_SetAttribute(SDL_GLAttr attr, int value)
  3832. {
  3833. #if defined(SDL_VIDEO_OPENGL) || defined(SDL_VIDEO_OPENGL_ES) || defined(SDL_VIDEO_OPENGL_ES2)
  3834. bool result;
  3835. if (!_this) {
  3836. return SDL_UninitializedVideo();
  3837. }
  3838. result = true;
  3839. switch (attr) {
  3840. case SDL_GL_RED_SIZE:
  3841. _this->gl_config.red_size = value;
  3842. break;
  3843. case SDL_GL_GREEN_SIZE:
  3844. _this->gl_config.green_size = value;
  3845. break;
  3846. case SDL_GL_BLUE_SIZE:
  3847. _this->gl_config.blue_size = value;
  3848. break;
  3849. case SDL_GL_ALPHA_SIZE:
  3850. _this->gl_config.alpha_size = value;
  3851. break;
  3852. case SDL_GL_DOUBLEBUFFER:
  3853. _this->gl_config.double_buffer = value;
  3854. break;
  3855. case SDL_GL_BUFFER_SIZE:
  3856. _this->gl_config.buffer_size = value;
  3857. break;
  3858. case SDL_GL_DEPTH_SIZE:
  3859. _this->gl_config.depth_size = value;
  3860. break;
  3861. case SDL_GL_STENCIL_SIZE:
  3862. _this->gl_config.stencil_size = value;
  3863. break;
  3864. case SDL_GL_ACCUM_RED_SIZE:
  3865. _this->gl_config.accum_red_size = value;
  3866. break;
  3867. case SDL_GL_ACCUM_GREEN_SIZE:
  3868. _this->gl_config.accum_green_size = value;
  3869. break;
  3870. case SDL_GL_ACCUM_BLUE_SIZE:
  3871. _this->gl_config.accum_blue_size = value;
  3872. break;
  3873. case SDL_GL_ACCUM_ALPHA_SIZE:
  3874. _this->gl_config.accum_alpha_size = value;
  3875. break;
  3876. case SDL_GL_STEREO:
  3877. _this->gl_config.stereo = value;
  3878. break;
  3879. case SDL_GL_MULTISAMPLEBUFFERS:
  3880. _this->gl_config.multisamplebuffers = value;
  3881. break;
  3882. case SDL_GL_MULTISAMPLESAMPLES:
  3883. _this->gl_config.multisamplesamples = value;
  3884. break;
  3885. case SDL_GL_FLOATBUFFERS:
  3886. _this->gl_config.floatbuffers = value;
  3887. break;
  3888. case SDL_GL_ACCELERATED_VISUAL:
  3889. _this->gl_config.accelerated = value;
  3890. break;
  3891. case SDL_GL_RETAINED_BACKING:
  3892. _this->gl_config.retained_backing = value;
  3893. break;
  3894. case SDL_GL_CONTEXT_MAJOR_VERSION:
  3895. _this->gl_config.major_version = value;
  3896. break;
  3897. case SDL_GL_CONTEXT_MINOR_VERSION:
  3898. _this->gl_config.minor_version = value;
  3899. break;
  3900. case SDL_GL_CONTEXT_FLAGS:
  3901. if (value & ~(SDL_GL_CONTEXT_DEBUG_FLAG |
  3902. SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG |
  3903. SDL_GL_CONTEXT_ROBUST_ACCESS_FLAG |
  3904. SDL_GL_CONTEXT_RESET_ISOLATION_FLAG)) {
  3905. result = SDL_SetError("Unknown OpenGL context flag %d", value);
  3906. break;
  3907. }
  3908. _this->gl_config.flags = value;
  3909. break;
  3910. case SDL_GL_CONTEXT_PROFILE_MASK:
  3911. if (value != 0 &&
  3912. value != SDL_GL_CONTEXT_PROFILE_CORE &&
  3913. value != SDL_GL_CONTEXT_PROFILE_COMPATIBILITY &&
  3914. value != SDL_GL_CONTEXT_PROFILE_ES) {
  3915. result = SDL_SetError("Unknown OpenGL context profile %d", value);
  3916. break;
  3917. }
  3918. _this->gl_config.profile_mask = value;
  3919. break;
  3920. case SDL_GL_SHARE_WITH_CURRENT_CONTEXT:
  3921. _this->gl_config.share_with_current_context = value;
  3922. break;
  3923. case SDL_GL_FRAMEBUFFER_SRGB_CAPABLE:
  3924. _this->gl_config.framebuffer_srgb_capable = value;
  3925. break;
  3926. case SDL_GL_CONTEXT_RELEASE_BEHAVIOR:
  3927. _this->gl_config.release_behavior = value;
  3928. break;
  3929. case SDL_GL_CONTEXT_RESET_NOTIFICATION:
  3930. _this->gl_config.reset_notification = value;
  3931. break;
  3932. case SDL_GL_CONTEXT_NO_ERROR:
  3933. _this->gl_config.no_error = value;
  3934. break;
  3935. case SDL_GL_EGL_PLATFORM:
  3936. _this->gl_config.egl_platform = value;
  3937. break;
  3938. default:
  3939. result = SDL_SetError("Unknown OpenGL attribute");
  3940. break;
  3941. }
  3942. return result;
  3943. #else
  3944. return SDL_Unsupported();
  3945. #endif // SDL_VIDEO_OPENGL
  3946. }
  3947. bool SDL_GL_GetAttribute(SDL_GLAttr attr, int *value)
  3948. {
  3949. #if defined(SDL_VIDEO_OPENGL) || defined(SDL_VIDEO_OPENGL_ES) || defined(SDL_VIDEO_OPENGL_ES2)
  3950. PFNGLGETERRORPROC glGetErrorFunc;
  3951. GLenum attrib = 0;
  3952. GLenum error = 0;
  3953. /*
  3954. * Some queries in Core Profile desktop OpenGL 3+ contexts require
  3955. * glGetFramebufferAttachmentParameteriv instead of glGetIntegerv. Note that
  3956. * the enums we use for the former function don't exist in OpenGL ES 2, and
  3957. * the function itself doesn't exist prior to OpenGL 3 and OpenGL ES 2.
  3958. */
  3959. #ifdef SDL_VIDEO_OPENGL
  3960. PFNGLGETSTRINGPROC glGetStringFunc;
  3961. PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC glGetFramebufferAttachmentParameterivFunc;
  3962. GLenum attachment = GL_BACK_LEFT;
  3963. GLenum attachmentattrib = 0;
  3964. #endif
  3965. if (!value) {
  3966. return SDL_InvalidParamError("value");
  3967. }
  3968. // Clear value in any case
  3969. *value = 0;
  3970. if (!_this) {
  3971. return SDL_UninitializedVideo();
  3972. }
  3973. switch (attr) {
  3974. case SDL_GL_RED_SIZE:
  3975. #ifdef SDL_VIDEO_OPENGL
  3976. attachmentattrib = GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE;
  3977. #endif
  3978. attrib = GL_RED_BITS;
  3979. break;
  3980. case SDL_GL_BLUE_SIZE:
  3981. #ifdef SDL_VIDEO_OPENGL
  3982. attachmentattrib = GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE;
  3983. #endif
  3984. attrib = GL_BLUE_BITS;
  3985. break;
  3986. case SDL_GL_GREEN_SIZE:
  3987. #ifdef SDL_VIDEO_OPENGL
  3988. attachmentattrib = GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE;
  3989. #endif
  3990. attrib = GL_GREEN_BITS;
  3991. break;
  3992. case SDL_GL_ALPHA_SIZE:
  3993. #ifdef SDL_VIDEO_OPENGL
  3994. attachmentattrib = GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE;
  3995. #endif
  3996. attrib = GL_ALPHA_BITS;
  3997. break;
  3998. case SDL_GL_DOUBLEBUFFER:
  3999. #ifdef SDL_VIDEO_OPENGL
  4000. attrib = GL_DOUBLEBUFFER;
  4001. break;
  4002. #else
  4003. // OpenGL ES 1.0 and above specifications have EGL_SINGLE_BUFFER
  4004. // parameter which switches double buffer to single buffer. OpenGL ES
  4005. // SDL driver must set proper value after initialization
  4006. *value = _this->gl_config.double_buffer;
  4007. return true;
  4008. #endif
  4009. case SDL_GL_DEPTH_SIZE:
  4010. #ifdef SDL_VIDEO_OPENGL
  4011. attachment = GL_DEPTH;
  4012. attachmentattrib = GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE;
  4013. #endif
  4014. attrib = GL_DEPTH_BITS;
  4015. break;
  4016. case SDL_GL_STENCIL_SIZE:
  4017. #ifdef SDL_VIDEO_OPENGL
  4018. attachment = GL_STENCIL;
  4019. attachmentattrib = GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE;
  4020. #endif
  4021. attrib = GL_STENCIL_BITS;
  4022. break;
  4023. #ifdef SDL_VIDEO_OPENGL
  4024. case SDL_GL_ACCUM_RED_SIZE:
  4025. attrib = GL_ACCUM_RED_BITS;
  4026. break;
  4027. case SDL_GL_ACCUM_GREEN_SIZE:
  4028. attrib = GL_ACCUM_GREEN_BITS;
  4029. break;
  4030. case SDL_GL_ACCUM_BLUE_SIZE:
  4031. attrib = GL_ACCUM_BLUE_BITS;
  4032. break;
  4033. case SDL_GL_ACCUM_ALPHA_SIZE:
  4034. attrib = GL_ACCUM_ALPHA_BITS;
  4035. break;
  4036. case SDL_GL_STEREO:
  4037. attrib = GL_STEREO;
  4038. break;
  4039. #else
  4040. case SDL_GL_ACCUM_RED_SIZE:
  4041. case SDL_GL_ACCUM_GREEN_SIZE:
  4042. case SDL_GL_ACCUM_BLUE_SIZE:
  4043. case SDL_GL_ACCUM_ALPHA_SIZE:
  4044. case SDL_GL_STEREO:
  4045. // none of these are supported in OpenGL ES
  4046. *value = 0;
  4047. return true;
  4048. #endif
  4049. case SDL_GL_MULTISAMPLEBUFFERS:
  4050. attrib = GL_SAMPLE_BUFFERS;
  4051. break;
  4052. case SDL_GL_MULTISAMPLESAMPLES:
  4053. attrib = GL_SAMPLES;
  4054. break;
  4055. case SDL_GL_CONTEXT_RELEASE_BEHAVIOR:
  4056. #ifdef SDL_VIDEO_OPENGL
  4057. attrib = GL_CONTEXT_RELEASE_BEHAVIOR;
  4058. #else
  4059. attrib = GL_CONTEXT_RELEASE_BEHAVIOR_KHR;
  4060. #endif
  4061. break;
  4062. case SDL_GL_BUFFER_SIZE:
  4063. {
  4064. int rsize = 0, gsize = 0, bsize = 0, asize = 0;
  4065. // There doesn't seem to be a single flag in OpenGL for this!
  4066. if (!SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &rsize)) {
  4067. return false;
  4068. }
  4069. if (!SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &gsize)) {
  4070. return false;
  4071. }
  4072. if (!SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &bsize)) {
  4073. return false;
  4074. }
  4075. if (!SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &asize)) {
  4076. return false;
  4077. }
  4078. *value = rsize + gsize + bsize + asize;
  4079. return true;
  4080. }
  4081. case SDL_GL_ACCELERATED_VISUAL:
  4082. {
  4083. // FIXME: How do we get this information?
  4084. *value = (_this->gl_config.accelerated != 0);
  4085. return true;
  4086. }
  4087. case SDL_GL_RETAINED_BACKING:
  4088. {
  4089. *value = _this->gl_config.retained_backing;
  4090. return true;
  4091. }
  4092. case SDL_GL_CONTEXT_MAJOR_VERSION:
  4093. {
  4094. *value = _this->gl_config.major_version;
  4095. return true;
  4096. }
  4097. case SDL_GL_CONTEXT_MINOR_VERSION:
  4098. {
  4099. *value = _this->gl_config.minor_version;
  4100. return true;
  4101. }
  4102. case SDL_GL_CONTEXT_FLAGS:
  4103. {
  4104. *value = _this->gl_config.flags;
  4105. return true;
  4106. }
  4107. case SDL_GL_CONTEXT_PROFILE_MASK:
  4108. {
  4109. *value = _this->gl_config.profile_mask;
  4110. return true;
  4111. }
  4112. case SDL_GL_SHARE_WITH_CURRENT_CONTEXT:
  4113. {
  4114. *value = _this->gl_config.share_with_current_context;
  4115. return true;
  4116. }
  4117. case SDL_GL_FRAMEBUFFER_SRGB_CAPABLE:
  4118. {
  4119. *value = _this->gl_config.framebuffer_srgb_capable;
  4120. return true;
  4121. }
  4122. case SDL_GL_CONTEXT_NO_ERROR:
  4123. {
  4124. *value = _this->gl_config.no_error;
  4125. return true;
  4126. }
  4127. case SDL_GL_EGL_PLATFORM:
  4128. {
  4129. *value = _this->gl_config.egl_platform;
  4130. return true;
  4131. }
  4132. default:
  4133. return SDL_SetError("Unknown OpenGL attribute");
  4134. }
  4135. #ifdef SDL_VIDEO_OPENGL
  4136. glGetStringFunc = (PFNGLGETSTRINGPROC)SDL_GL_GetProcAddress("glGetString");
  4137. if (!glGetStringFunc) {
  4138. return false;
  4139. }
  4140. if (attachmentattrib && isAtLeastGL3((const char *)glGetStringFunc(GL_VERSION))) {
  4141. // glGetFramebufferAttachmentParameteriv needs to operate on the window framebuffer for this, so bind FBO 0 if necessary.
  4142. GLint current_fbo = 0;
  4143. PFNGLGETINTEGERVPROC glGetIntegervFunc = (PFNGLGETINTEGERVPROC) SDL_GL_GetProcAddress("glGetIntegerv");
  4144. PFNGLBINDFRAMEBUFFERPROC glBindFramebufferFunc = (PFNGLBINDFRAMEBUFFERPROC)SDL_GL_GetProcAddress("glBindFramebuffer");
  4145. if (glGetIntegervFunc && glBindFramebufferFunc) {
  4146. glGetIntegervFunc(GL_DRAW_FRAMEBUFFER_BINDING, &current_fbo);
  4147. }
  4148. glGetFramebufferAttachmentParameterivFunc = (PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC)SDL_GL_GetProcAddress("glGetFramebufferAttachmentParameteriv");
  4149. if (glGetFramebufferAttachmentParameterivFunc) {
  4150. if (glBindFramebufferFunc && (current_fbo != 0)) {
  4151. glBindFramebufferFunc(GL_DRAW_FRAMEBUFFER, 0);
  4152. }
  4153. glGetFramebufferAttachmentParameterivFunc(GL_FRAMEBUFFER, attachment, attachmentattrib, (GLint *)value);
  4154. if (glBindFramebufferFunc && (current_fbo != 0)) {
  4155. glBindFramebufferFunc(GL_DRAW_FRAMEBUFFER, current_fbo);
  4156. }
  4157. } else {
  4158. return false;
  4159. }
  4160. } else
  4161. #endif
  4162. {
  4163. PFNGLGETINTEGERVPROC glGetIntegervFunc = (PFNGLGETINTEGERVPROC)SDL_GL_GetProcAddress("glGetIntegerv");
  4164. if (glGetIntegervFunc) {
  4165. glGetIntegervFunc(attrib, (GLint *)value);
  4166. } else {
  4167. return false;
  4168. }
  4169. }
  4170. glGetErrorFunc = (PFNGLGETERRORPROC)SDL_GL_GetProcAddress("glGetError");
  4171. if (!glGetErrorFunc) {
  4172. return false;
  4173. }
  4174. error = glGetErrorFunc();
  4175. if (error != GL_NO_ERROR) {
  4176. if (error == GL_INVALID_ENUM) {
  4177. return SDL_SetError("OpenGL error: GL_INVALID_ENUM");
  4178. } else if (error == GL_INVALID_VALUE) {
  4179. return SDL_SetError("OpenGL error: GL_INVALID_VALUE");
  4180. }
  4181. return SDL_SetError("OpenGL error: %08X", error);
  4182. }
  4183. return true;
  4184. #else
  4185. return SDL_Unsupported();
  4186. #endif // SDL_VIDEO_OPENGL
  4187. }
  4188. #define NOT_AN_OPENGL_WINDOW "The specified window isn't an OpenGL window"
  4189. SDL_GLContext SDL_GL_CreateContext(SDL_Window *window)
  4190. {
  4191. SDL_GLContext ctx = NULL;
  4192. CHECK_WINDOW_MAGIC(window, NULL);
  4193. if (!(window->flags & SDL_WINDOW_OPENGL)) {
  4194. SDL_SetError(NOT_AN_OPENGL_WINDOW);
  4195. return NULL;
  4196. }
  4197. ctx = _this->GL_CreateContext(_this, window);
  4198. // Creating a context is assumed to make it current in the SDL driver.
  4199. if (ctx) {
  4200. _this->current_glwin = window;
  4201. _this->current_glctx = ctx;
  4202. SDL_SetTLS(&_this->current_glwin_tls, window, NULL);
  4203. SDL_SetTLS(&_this->current_glctx_tls, ctx, NULL);
  4204. }
  4205. return ctx;
  4206. }
  4207. bool SDL_GL_MakeCurrent(SDL_Window *window, SDL_GLContext context)
  4208. {
  4209. bool result;
  4210. if (!_this) {
  4211. return SDL_UninitializedVideo();
  4212. }
  4213. if (window == SDL_GL_GetCurrentWindow() &&
  4214. context == SDL_GL_GetCurrentContext()) {
  4215. // We're already current.
  4216. return true;
  4217. }
  4218. if (!context) {
  4219. window = NULL;
  4220. } else if (window) {
  4221. CHECK_WINDOW_MAGIC(window, false);
  4222. if (!(window->flags & SDL_WINDOW_OPENGL)) {
  4223. return SDL_SetError(NOT_AN_OPENGL_WINDOW);
  4224. }
  4225. } else if (!_this->gl_allow_no_surface) {
  4226. return SDL_SetError("Use of OpenGL without a window is not supported on this platform");
  4227. }
  4228. result = _this->GL_MakeCurrent(_this, window, context);
  4229. if (result) {
  4230. _this->current_glwin = window;
  4231. _this->current_glctx = context;
  4232. SDL_SetTLS(&_this->current_glwin_tls, window, NULL);
  4233. SDL_SetTLS(&_this->current_glctx_tls, context, NULL);
  4234. }
  4235. return result;
  4236. }
  4237. SDL_Window *SDL_GL_GetCurrentWindow(void)
  4238. {
  4239. if (!_this) {
  4240. SDL_UninitializedVideo();
  4241. return NULL;
  4242. }
  4243. return (SDL_Window *)SDL_GetTLS(&_this->current_glwin_tls);
  4244. }
  4245. SDL_GLContext SDL_GL_GetCurrentContext(void)
  4246. {
  4247. if (!_this) {
  4248. SDL_UninitializedVideo();
  4249. return NULL;
  4250. }
  4251. return (SDL_GLContext)SDL_GetTLS(&_this->current_glctx_tls);
  4252. }
  4253. SDL_EGLDisplay SDL_EGL_GetCurrentDisplay(void)
  4254. {
  4255. #ifdef SDL_VIDEO_OPENGL_EGL
  4256. if (!_this) {
  4257. SDL_UninitializedVideo();
  4258. return EGL_NO_DISPLAY;
  4259. }
  4260. if (!_this->egl_data) {
  4261. SDL_SetError("There is no current EGL display");
  4262. return EGL_NO_DISPLAY;
  4263. }
  4264. return _this->egl_data->egl_display;
  4265. #else
  4266. SDL_SetError("SDL was not built with EGL support");
  4267. return NULL;
  4268. #endif
  4269. }
  4270. SDL_EGLConfig SDL_EGL_GetCurrentConfig(void)
  4271. {
  4272. #ifdef SDL_VIDEO_OPENGL_EGL
  4273. if (!_this) {
  4274. SDL_UninitializedVideo();
  4275. return NULL;
  4276. }
  4277. if (!_this->egl_data) {
  4278. SDL_SetError("There is no current EGL display");
  4279. return NULL;
  4280. }
  4281. return _this->egl_data->egl_config;
  4282. #else
  4283. SDL_SetError("SDL was not built with EGL support");
  4284. return NULL;
  4285. #endif
  4286. }
  4287. SDL_EGLConfig SDL_EGL_GetWindowSurface(SDL_Window *window)
  4288. {
  4289. #ifdef SDL_VIDEO_OPENGL_EGL
  4290. if (!_this) {
  4291. SDL_UninitializedVideo();
  4292. return NULL;
  4293. }
  4294. if (!_this->egl_data) {
  4295. SDL_SetError("There is no current EGL display");
  4296. return NULL;
  4297. }
  4298. if (_this->GL_GetEGLSurface) {
  4299. return _this->GL_GetEGLSurface(_this, window);
  4300. }
  4301. return NULL;
  4302. #else
  4303. SDL_SetError("SDL was not built with EGL support");
  4304. return NULL;
  4305. #endif
  4306. }
  4307. bool SDL_GL_SetSwapInterval(int interval)
  4308. {
  4309. if (!_this) {
  4310. return SDL_UninitializedVideo();
  4311. } else if (SDL_GL_GetCurrentContext() == NULL) {
  4312. return SDL_SetError("No OpenGL context has been made current");
  4313. } else if (_this->GL_SetSwapInterval) {
  4314. return _this->GL_SetSwapInterval(_this, interval);
  4315. } else {
  4316. return SDL_SetError("Setting the swap interval is not supported");
  4317. }
  4318. }
  4319. bool SDL_GL_GetSwapInterval(int *interval)
  4320. {
  4321. if (!interval) {
  4322. return SDL_InvalidParamError("interval");
  4323. }
  4324. *interval = 0;
  4325. if (!_this) {
  4326. return SDL_SetError("no video driver");
  4327. } else if (SDL_GL_GetCurrentContext() == NULL) {
  4328. return SDL_SetError("no current context");
  4329. } else if (_this->GL_GetSwapInterval) {
  4330. return _this->GL_GetSwapInterval(_this, interval);
  4331. } else {
  4332. return SDL_SetError("not implemented");
  4333. }
  4334. }
  4335. bool SDL_GL_SwapWindow(SDL_Window *window)
  4336. {
  4337. CHECK_WINDOW_MAGIC(window, false);
  4338. if (!(window->flags & SDL_WINDOW_OPENGL)) {
  4339. return SDL_SetError(NOT_AN_OPENGL_WINDOW);
  4340. }
  4341. if (SDL_GL_GetCurrentWindow() != window) {
  4342. return SDL_SetError("The specified window has not been made current");
  4343. }
  4344. return _this->GL_SwapWindow(_this, window);
  4345. }
  4346. bool SDL_GL_DestroyContext(SDL_GLContext context)
  4347. {
  4348. if (!_this) {
  4349. return SDL_UninitializedVideo(); \
  4350. }
  4351. if (!context) {
  4352. return SDL_InvalidParamError("context");
  4353. }
  4354. if (SDL_GL_GetCurrentContext() == context) {
  4355. SDL_GL_MakeCurrent(NULL, NULL);
  4356. }
  4357. return _this->GL_DestroyContext(_this, context);
  4358. }
  4359. #if 0 // FIXME
  4360. /*
  4361. * Utility function used by SDL_WM_SetIcon(); flags & 1 for color key, flags
  4362. * & 2 for alpha channel.
  4363. */
  4364. static void CreateMaskFromColorKeyOrAlpha(SDL_Surface *icon, Uint8 *mask, int flags)
  4365. {
  4366. int x, y;
  4367. Uint32 colorkey;
  4368. #define SET_MASKBIT(icon, x, y, mask) \
  4369. mask[(y * ((icon->w + 7) / 8)) + (x / 8)] &= ~(0x01 << (7 - (x % 8)))
  4370. colorkey = icon->format->colorkey;
  4371. switch (SDL_BYTESPERPIXEL(icon->format)) {
  4372. case 1:
  4373. {
  4374. Uint8 *pixels;
  4375. for (y = 0; y < icon->h; ++y) {
  4376. pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  4377. for (x = 0; x < icon->w; ++x) {
  4378. if (*pixels++ == colorkey) {
  4379. SET_MASKBIT(icon, x, y, mask);
  4380. }
  4381. }
  4382. }
  4383. }
  4384. break;
  4385. case 2:
  4386. {
  4387. Uint16 *pixels;
  4388. for (y = 0; y < icon->h; ++y) {
  4389. pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  4390. for (x = 0; x < icon->w; ++x) {
  4391. if ((flags & 1) && *pixels == colorkey) {
  4392. SET_MASKBIT(icon, x, y, mask);
  4393. } else if ((flags & 2)
  4394. && (*pixels & icon->format->Amask) == 0) {
  4395. SET_MASKBIT(icon, x, y, mask);
  4396. }
  4397. pixels++;
  4398. }
  4399. }
  4400. }
  4401. break;
  4402. case 4:
  4403. {
  4404. Uint32 *pixels;
  4405. for (y = 0; y < icon->h; ++y) {
  4406. pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  4407. for (x = 0; x < icon->w; ++x) {
  4408. if ((flags & 1) && *pixels == colorkey) {
  4409. SET_MASKBIT(icon, x, y, mask);
  4410. } else if ((flags & 2)
  4411. && (*pixels & icon->format->Amask) == 0) {
  4412. SET_MASKBIT(icon, x, y, mask);
  4413. }
  4414. pixels++;
  4415. }
  4416. }
  4417. }
  4418. break;
  4419. }
  4420. }
  4421. /*
  4422. * Sets the window manager icon for the display window.
  4423. */
  4424. void SDL_WM_SetIcon(SDL_Surface *icon, Uint8 *mask)
  4425. {
  4426. if (icon && _this->SetIcon) {
  4427. // Generate a mask if necessary, and create the icon!
  4428. if (mask == NULL) {
  4429. int mask_len = icon->h * (icon->w + 7) / 8;
  4430. int flags = 0;
  4431. mask = (Uint8 *) SDL_malloc(mask_len);
  4432. if (mask == NULL) {
  4433. return;
  4434. }
  4435. SDL_memset(mask, ~0, mask_len);
  4436. if (icon->flags & SDL_SRCCOLORKEY)
  4437. flags |= 1;
  4438. if (icon->flags & SDL_SRCALPHA)
  4439. flags |= 2;
  4440. if (flags) {
  4441. CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  4442. }
  4443. _this->SetIcon(_this, icon, mask);
  4444. SDL_free(mask);
  4445. } else {
  4446. _this->SetIcon(_this, icon, mask);
  4447. }
  4448. }
  4449. }
  4450. #endif
  4451. SDL_TextInputType SDL_GetTextInputType(SDL_PropertiesID props)
  4452. {
  4453. return (SDL_TextInputType)SDL_GetNumberProperty(props, SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT);
  4454. }
  4455. SDL_Capitalization SDL_GetTextInputCapitalization(SDL_PropertiesID props)
  4456. {
  4457. if (SDL_HasProperty(props, SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER)) {
  4458. return (SDL_Capitalization)SDL_GetNumberProperty(props, SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_NONE);
  4459. }
  4460. switch (SDL_GetTextInputType(props)) {
  4461. case SDL_TEXTINPUT_TYPE_TEXT:
  4462. return SDL_CAPITALIZE_SENTENCES;
  4463. case SDL_TEXTINPUT_TYPE_TEXT_NAME:
  4464. return SDL_CAPITALIZE_WORDS;
  4465. default:
  4466. return SDL_CAPITALIZE_NONE;
  4467. }
  4468. }
  4469. bool SDL_GetTextInputAutocorrect(SDL_PropertiesID props)
  4470. {
  4471. return SDL_GetBooleanProperty(props, SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN, true);
  4472. }
  4473. bool SDL_GetTextInputMultiline(SDL_PropertiesID props)
  4474. {
  4475. if (SDL_HasProperty(props, SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN)) {
  4476. return SDL_GetBooleanProperty(props, SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN, false);
  4477. }
  4478. if (SDL_GetHintBoolean(SDL_HINT_RETURN_KEY_HIDES_IME, false)) {
  4479. return false;
  4480. } else {
  4481. return true;
  4482. }
  4483. }
  4484. static bool AutoShowingScreenKeyboard(void)
  4485. {
  4486. const char *hint = SDL_GetHint(SDL_HINT_ENABLE_SCREEN_KEYBOARD);
  4487. if (((!hint || SDL_strcasecmp(hint, "auto") == 0) && !SDL_HasKeyboard()) ||
  4488. SDL_GetStringBoolean(hint, false)) {
  4489. return true;
  4490. } else {
  4491. return false;
  4492. }
  4493. }
  4494. bool SDL_StartTextInput(SDL_Window *window)
  4495. {
  4496. return SDL_StartTextInputWithProperties(window, 0);
  4497. }
  4498. bool SDL_StartTextInputWithProperties(SDL_Window *window, SDL_PropertiesID props)
  4499. {
  4500. CHECK_WINDOW_MAGIC(window, false);
  4501. if (window->text_input_props) {
  4502. SDL_DestroyProperties(window->text_input_props);
  4503. window->text_input_props = 0;
  4504. }
  4505. if (props) {
  4506. window->text_input_props = SDL_CreateProperties();
  4507. if (!window->text_input_props) {
  4508. return false;
  4509. }
  4510. if (!SDL_CopyProperties(props, window->text_input_props)) {
  4511. return false;
  4512. }
  4513. }
  4514. if (_this->SetTextInputProperties) {
  4515. _this->SetTextInputProperties(_this, window, props);
  4516. }
  4517. // Show the on-screen keyboard, if desired
  4518. if (AutoShowingScreenKeyboard() && !SDL_ScreenKeyboardShown(window)) {
  4519. if (_this->ShowScreenKeyboard) {
  4520. _this->ShowScreenKeyboard(_this, window, props);
  4521. }
  4522. }
  4523. if (!window->text_input_active) {
  4524. // Finally start the text input system
  4525. if (_this->StartTextInput) {
  4526. if (!_this->StartTextInput(_this, window, props)) {
  4527. return false;
  4528. }
  4529. }
  4530. window->text_input_active = true;
  4531. }
  4532. return true;
  4533. }
  4534. bool SDL_TextInputActive(SDL_Window *window)
  4535. {
  4536. CHECK_WINDOW_MAGIC(window, false);
  4537. return window->text_input_active;
  4538. }
  4539. bool SDL_StopTextInput(SDL_Window *window)
  4540. {
  4541. CHECK_WINDOW_MAGIC(window, false);
  4542. if (window->text_input_active) {
  4543. // Stop the text input system
  4544. if (_this->StopTextInput) {
  4545. _this->StopTextInput(_this, window);
  4546. }
  4547. window->text_input_active = false;
  4548. }
  4549. // Hide the on-screen keyboard, if desired
  4550. if (AutoShowingScreenKeyboard() && SDL_ScreenKeyboardShown(window)) {
  4551. if (_this->HideScreenKeyboard) {
  4552. _this->HideScreenKeyboard(_this, window);
  4553. }
  4554. }
  4555. return true;
  4556. }
  4557. bool SDL_SetTextInputArea(SDL_Window *window, const SDL_Rect *rect, int cursor)
  4558. {
  4559. CHECK_WINDOW_MAGIC(window, false);
  4560. if (rect) {
  4561. SDL_copyp(&window->text_input_rect, rect);
  4562. window->text_input_cursor = cursor;
  4563. } else {
  4564. SDL_zero(window->text_input_rect);
  4565. window->text_input_cursor = 0;
  4566. }
  4567. if (_this && _this->UpdateTextInputArea) {
  4568. if (!_this->UpdateTextInputArea(_this, window)) {
  4569. return false;
  4570. }
  4571. }
  4572. return true;
  4573. }
  4574. bool SDL_GetTextInputArea(SDL_Window *window, SDL_Rect *rect, int *cursor)
  4575. {
  4576. CHECK_WINDOW_MAGIC(window, false);
  4577. if (rect) {
  4578. SDL_copyp(rect, &window->text_input_rect);
  4579. }
  4580. if (cursor) {
  4581. *cursor = window->text_input_cursor;
  4582. }
  4583. return true;
  4584. }
  4585. bool SDL_ClearComposition(SDL_Window *window)
  4586. {
  4587. CHECK_WINDOW_MAGIC(window, false);
  4588. if (_this->ClearComposition) {
  4589. return _this->ClearComposition(_this, window);
  4590. }
  4591. return true;
  4592. }
  4593. bool SDL_HasScreenKeyboardSupport(void)
  4594. {
  4595. if (_this && _this->HasScreenKeyboardSupport) {
  4596. return _this->HasScreenKeyboardSupport(_this);
  4597. }
  4598. return false;
  4599. }
  4600. bool SDL_ScreenKeyboardShown(SDL_Window *window)
  4601. {
  4602. CHECK_WINDOW_MAGIC(window, false);
  4603. if (_this->IsScreenKeyboardShown) {
  4604. return _this->IsScreenKeyboardShown(_this, window);
  4605. }
  4606. return false;
  4607. }
  4608. int SDL_GetMessageBoxCount(void)
  4609. {
  4610. return SDL_GetAtomicInt(&SDL_messagebox_count);
  4611. }
  4612. #ifdef SDL_VIDEO_DRIVER_ANDROID
  4613. #include "android/SDL_androidmessagebox.h"
  4614. #endif
  4615. #ifdef SDL_VIDEO_DRIVER_WINDOWS
  4616. #include "windows/SDL_windowsmessagebox.h"
  4617. #endif
  4618. #ifdef SDL_VIDEO_DRIVER_COCOA
  4619. #include "cocoa/SDL_cocoamessagebox.h"
  4620. #endif
  4621. #ifdef SDL_VIDEO_DRIVER_UIKIT
  4622. #include "uikit/SDL_uikitmessagebox.h"
  4623. #endif
  4624. #ifdef SDL_VIDEO_DRIVER_WAYLAND
  4625. #include "wayland/SDL_waylandmessagebox.h"
  4626. #endif
  4627. #ifdef SDL_VIDEO_DRIVER_X11
  4628. #include "x11/SDL_x11messagebox.h"
  4629. #endif
  4630. #ifdef SDL_VIDEO_DRIVER_HAIKU
  4631. #include "haiku/SDL_bmessagebox.h"
  4632. #endif
  4633. #ifdef SDL_VIDEO_DRIVER_RISCOS
  4634. #include "riscos/SDL_riscosmessagebox.h"
  4635. #endif
  4636. #ifdef SDL_VIDEO_DRIVER_VITA
  4637. #include "vita/SDL_vitamessagebox.h"
  4638. #endif
  4639. bool SDL_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonID)
  4640. {
  4641. int dummybutton;
  4642. bool result = false;
  4643. bool show_cursor_prev;
  4644. SDL_Window *current_window;
  4645. SDL_MessageBoxData mbdata;
  4646. if (!messageboxdata) {
  4647. return SDL_InvalidParamError("messageboxdata");
  4648. } else if (messageboxdata->numbuttons < 0) {
  4649. return SDL_SetError("Invalid number of buttons");
  4650. }
  4651. (void)SDL_AtomicIncRef(&SDL_messagebox_count);
  4652. current_window = SDL_GetKeyboardFocus();
  4653. SDL_UpdateMouseCapture(false);
  4654. SDL_SetRelativeMouseMode(false);
  4655. show_cursor_prev = SDL_CursorVisible();
  4656. SDL_ShowCursor();
  4657. SDL_ResetKeyboard();
  4658. if (!buttonID) {
  4659. buttonID = &dummybutton;
  4660. }
  4661. SDL_memcpy(&mbdata, messageboxdata, sizeof(*messageboxdata));
  4662. if (!mbdata.title) {
  4663. mbdata.title = "";
  4664. }
  4665. if (!mbdata.message) {
  4666. mbdata.message = "";
  4667. }
  4668. messageboxdata = &mbdata;
  4669. SDL_ClearError();
  4670. if (_this && _this->ShowMessageBox) {
  4671. result = _this->ShowMessageBox(_this, messageboxdata, buttonID);
  4672. } else {
  4673. // It's completely fine to call this function before video is initialized
  4674. const char *driver_name = SDL_GetHint(SDL_HINT_VIDEO_DRIVER);
  4675. if (driver_name && *driver_name != 0) {
  4676. const char *driver_attempt = driver_name;
  4677. while (driver_attempt && (*driver_attempt != 0) && !result) {
  4678. const char *driver_attempt_end = SDL_strchr(driver_attempt, ',');
  4679. size_t driver_attempt_len = (driver_attempt_end) ? (driver_attempt_end - driver_attempt)
  4680. : SDL_strlen(driver_attempt);
  4681. for (int i = 0; bootstrap[i]; ++i) {
  4682. if (bootstrap[i]->ShowMessageBox && (driver_attempt_len == SDL_strlen(bootstrap[i]->name)) &&
  4683. (SDL_strncasecmp(bootstrap[i]->name, driver_attempt, driver_attempt_len) == 0)) {
  4684. if (bootstrap[i]->ShowMessageBox(messageboxdata, buttonID)) {
  4685. result = true;
  4686. }
  4687. break;
  4688. }
  4689. }
  4690. driver_attempt = (driver_attempt_end) ? (driver_attempt_end + 1) : NULL;
  4691. }
  4692. } else {
  4693. for (int i = 0; bootstrap[i]; ++i) {
  4694. if (bootstrap[i]->ShowMessageBox && bootstrap[i]->ShowMessageBox(messageboxdata, buttonID)) {
  4695. result = true;
  4696. break;
  4697. }
  4698. }
  4699. }
  4700. }
  4701. if (!result) {
  4702. const char *error = SDL_GetError();
  4703. if (!*error) {
  4704. SDL_SetError("No message system available");
  4705. }
  4706. } else {
  4707. SDL_ClearError();
  4708. }
  4709. (void)SDL_AtomicDecRef(&SDL_messagebox_count);
  4710. if (current_window) {
  4711. SDL_RaiseWindow(current_window);
  4712. }
  4713. if (!show_cursor_prev) {
  4714. SDL_HideCursor();
  4715. }
  4716. SDL_UpdateRelativeMouseMode();
  4717. SDL_UpdateMouseCapture(false);
  4718. return result;
  4719. }
  4720. bool SDL_ShowSimpleMessageBox(SDL_MessageBoxFlags flags, const char *title, const char *message, SDL_Window *window)
  4721. {
  4722. #ifdef SDL_PLATFORM_EMSCRIPTEN
  4723. // !!! FIXME: propose a browser API for this, get this #ifdef out of here?
  4724. /* Web browsers don't (currently) have an API for a custom message box
  4725. that can block, but for the most common case (SDL_ShowSimpleMessageBox),
  4726. we can use the standard Javascript alert() function. */
  4727. if (!title) {
  4728. title = "";
  4729. }
  4730. if (!message) {
  4731. message = "";
  4732. }
  4733. EM_ASM({
  4734. alert(UTF8ToString($0) + "\n\n" + UTF8ToString($1));
  4735. },
  4736. title, message);
  4737. return true;
  4738. #elif defined(SDL_PLATFORM_3DS)
  4739. errorConf errCnf;
  4740. bool hasGpuRight;
  4741. // If the video subsystem has not been initialised, set up graphics temporarily
  4742. hasGpuRight = gspHasGpuRight();
  4743. if (!hasGpuRight)
  4744. gfxInitDefault();
  4745. errorInit(&errCnf, ERROR_TEXT_WORD_WRAP, CFG_LANGUAGE_EN);
  4746. errorText(&errCnf, message);
  4747. errorDisp(&errCnf);
  4748. if (!hasGpuRight)
  4749. gfxExit();
  4750. return true;
  4751. #else
  4752. SDL_MessageBoxData data;
  4753. SDL_MessageBoxButtonData button;
  4754. SDL_zero(data);
  4755. data.flags = flags;
  4756. data.title = title;
  4757. data.message = message;
  4758. data.numbuttons = 1;
  4759. data.buttons = &button;
  4760. data.window = window;
  4761. SDL_zero(button);
  4762. button.flags |= SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT;
  4763. button.flags |= SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT;
  4764. button.text = "OK";
  4765. return SDL_ShowMessageBox(&data, NULL);
  4766. #endif
  4767. }
  4768. bool SDL_ShouldAllowTopmost(void)
  4769. {
  4770. return SDL_GetHintBoolean(SDL_HINT_WINDOW_ALLOW_TOPMOST, true);
  4771. }
  4772. bool SDL_ShowWindowSystemMenu(SDL_Window *window, int x, int y)
  4773. {
  4774. CHECK_WINDOW_MAGIC(window, false)
  4775. CHECK_WINDOW_NOT_POPUP(window, false)
  4776. if (_this->ShowWindowSystemMenu) {
  4777. _this->ShowWindowSystemMenu(window, x, y);
  4778. return true;
  4779. }
  4780. return SDL_Unsupported();
  4781. }
  4782. bool SDL_SetWindowHitTest(SDL_Window *window, SDL_HitTest callback, void *callback_data)
  4783. {
  4784. CHECK_WINDOW_MAGIC(window, false);
  4785. if (!_this->SetWindowHitTest) {
  4786. return SDL_Unsupported();
  4787. }
  4788. window->hit_test = callback;
  4789. window->hit_test_data = callback_data;
  4790. return _this->SetWindowHitTest(window, callback != NULL);
  4791. }
  4792. bool SDL_SetWindowShape(SDL_Window *window, SDL_Surface *shape)
  4793. {
  4794. SDL_PropertiesID props;
  4795. SDL_Surface *surface;
  4796. CHECK_WINDOW_MAGIC(window, false);
  4797. if (!(window->flags & SDL_WINDOW_TRANSPARENT)) {
  4798. return SDL_SetError("Window must be created with SDL_WINDOW_TRANSPARENT");
  4799. }
  4800. props = SDL_GetWindowProperties(window);
  4801. if (!props) {
  4802. return false;
  4803. }
  4804. surface = SDL_ConvertSurface(shape, SDL_PIXELFORMAT_ARGB32);
  4805. if (!surface) {
  4806. return false;
  4807. }
  4808. if (!SDL_SetSurfaceProperty(props, SDL_PROP_WINDOW_SHAPE_POINTER, surface)) {
  4809. return false;
  4810. }
  4811. if (_this->UpdateWindowShape) {
  4812. if (!_this->UpdateWindowShape(_this, window, surface)) {
  4813. return false;
  4814. }
  4815. }
  4816. return true;
  4817. }
  4818. /*
  4819. * Functions used by iOS application delegates
  4820. */
  4821. void SDL_OnApplicationWillTerminate(void)
  4822. {
  4823. SDL_SendAppEvent(SDL_EVENT_TERMINATING);
  4824. }
  4825. void SDL_OnApplicationDidReceiveMemoryWarning(void)
  4826. {
  4827. SDL_SendAppEvent(SDL_EVENT_LOW_MEMORY);
  4828. }
  4829. void SDL_OnApplicationWillEnterBackground(void)
  4830. {
  4831. if (_this) {
  4832. SDL_Window *window;
  4833. for (window = _this->windows; window; window = window->next) {
  4834. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_MINIMIZED, 0, 0);
  4835. }
  4836. SDL_SetKeyboardFocus(NULL);
  4837. }
  4838. SDL_SendAppEvent(SDL_EVENT_WILL_ENTER_BACKGROUND);
  4839. }
  4840. void SDL_OnApplicationDidEnterBackground(void)
  4841. {
  4842. SDL_SendAppEvent(SDL_EVENT_DID_ENTER_BACKGROUND);
  4843. }
  4844. void SDL_OnApplicationWillEnterForeground(void)
  4845. {
  4846. SDL_SendAppEvent(SDL_EVENT_WILL_ENTER_FOREGROUND);
  4847. }
  4848. void SDL_OnApplicationDidEnterForeground(void)
  4849. {
  4850. SDL_SendAppEvent(SDL_EVENT_DID_ENTER_FOREGROUND);
  4851. if (_this) {
  4852. SDL_Window *window;
  4853. for (window = _this->windows; window; window = window->next) {
  4854. SDL_SetKeyboardFocus(window);
  4855. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESTORED, 0, 0);
  4856. }
  4857. }
  4858. }
  4859. #define NOT_A_VULKAN_WINDOW "The specified window isn't a Vulkan window"
  4860. bool SDL_Vulkan_LoadLibrary(const char *path)
  4861. {
  4862. bool result;
  4863. if (!_this) {
  4864. return SDL_UninitializedVideo();
  4865. }
  4866. if (_this->vulkan_config.loader_loaded) {
  4867. if (path && SDL_strcmp(path, _this->vulkan_config.loader_path) != 0) {
  4868. return SDL_SetError("Vulkan loader library already loaded");
  4869. }
  4870. result = true;
  4871. } else {
  4872. if (!_this->Vulkan_LoadLibrary) {
  4873. return SDL_DllNotSupported("Vulkan");
  4874. }
  4875. result = _this->Vulkan_LoadLibrary(_this, path);
  4876. }
  4877. if (result) {
  4878. _this->vulkan_config.loader_loaded++;
  4879. }
  4880. return result;
  4881. }
  4882. SDL_FunctionPointer SDL_Vulkan_GetVkGetInstanceProcAddr(void)
  4883. {
  4884. if (!_this) {
  4885. SDL_UninitializedVideo();
  4886. return NULL;
  4887. }
  4888. if (!_this->vulkan_config.loader_loaded) {
  4889. SDL_SetError("No Vulkan loader has been loaded");
  4890. return NULL;
  4891. }
  4892. return (SDL_FunctionPointer)_this->vulkan_config.vkGetInstanceProcAddr;
  4893. }
  4894. void SDL_Vulkan_UnloadLibrary(void)
  4895. {
  4896. if (!_this) {
  4897. SDL_UninitializedVideo();
  4898. return;
  4899. }
  4900. if (_this->vulkan_config.loader_loaded > 0) {
  4901. if (--_this->vulkan_config.loader_loaded > 0) {
  4902. return;
  4903. }
  4904. if (_this->Vulkan_UnloadLibrary) {
  4905. _this->Vulkan_UnloadLibrary(_this);
  4906. }
  4907. }
  4908. }
  4909. char const* const* SDL_Vulkan_GetInstanceExtensions(Uint32 *count)
  4910. {
  4911. return _this->Vulkan_GetInstanceExtensions(_this, count);
  4912. }
  4913. bool SDL_Vulkan_CreateSurface(SDL_Window *window,
  4914. VkInstance instance,
  4915. const struct VkAllocationCallbacks *allocator,
  4916. VkSurfaceKHR *surface)
  4917. {
  4918. CHECK_WINDOW_MAGIC(window, false);
  4919. if (!(window->flags & SDL_WINDOW_VULKAN)) {
  4920. return SDL_SetError(NOT_A_VULKAN_WINDOW);
  4921. }
  4922. if (!instance) {
  4923. return SDL_InvalidParamError("instance");
  4924. }
  4925. if (!surface) {
  4926. return SDL_InvalidParamError("surface");
  4927. }
  4928. return _this->Vulkan_CreateSurface(_this, window, instance, allocator, surface);
  4929. }
  4930. void SDL_Vulkan_DestroySurface(VkInstance instance,
  4931. VkSurfaceKHR surface,
  4932. const struct VkAllocationCallbacks *allocator)
  4933. {
  4934. if (_this && instance && surface && _this->Vulkan_DestroySurface) {
  4935. _this->Vulkan_DestroySurface(_this, instance, surface, allocator);
  4936. }
  4937. }
  4938. bool SDL_Vulkan_GetPresentationSupport(VkInstance instance,
  4939. VkPhysicalDevice physicalDevice,
  4940. Uint32 queueFamilyIndex)
  4941. {
  4942. if (!_this) {
  4943. SDL_UninitializedVideo();
  4944. return false;
  4945. }
  4946. if (!instance) {
  4947. SDL_InvalidParamError("instance");
  4948. return false;
  4949. }
  4950. if (!physicalDevice) {
  4951. SDL_InvalidParamError("physicalDevice");
  4952. return false;
  4953. }
  4954. if (_this->Vulkan_GetPresentationSupport) {
  4955. return _this->Vulkan_GetPresentationSupport(_this, instance, physicalDevice, queueFamilyIndex);
  4956. }
  4957. /* If the backend does not have this function then it does not have a
  4958. * WSI function to query it; in other words it's not necessary to check
  4959. * as it is always supported.
  4960. */
  4961. return true;
  4962. }
  4963. SDL_MetalView SDL_Metal_CreateView(SDL_Window *window)
  4964. {
  4965. CHECK_WINDOW_MAGIC(window, NULL);
  4966. if (!_this->Metal_CreateView) {
  4967. SDL_Unsupported();
  4968. return NULL;
  4969. }
  4970. if (!(window->flags & SDL_WINDOW_METAL)) {
  4971. // No problem, we can convert to Metal
  4972. if (window->flags & SDL_WINDOW_OPENGL) {
  4973. window->flags &= ~SDL_WINDOW_OPENGL;
  4974. SDL_GL_UnloadLibrary();
  4975. }
  4976. if (window->flags & SDL_WINDOW_VULKAN) {
  4977. window->flags &= ~SDL_WINDOW_VULKAN;
  4978. SDL_Vulkan_UnloadLibrary();
  4979. }
  4980. window->flags |= SDL_WINDOW_METAL;
  4981. }
  4982. return _this->Metal_CreateView(_this, window);
  4983. }
  4984. void SDL_Metal_DestroyView(SDL_MetalView view)
  4985. {
  4986. if (_this && view && _this->Metal_DestroyView) {
  4987. _this->Metal_DestroyView(_this, view);
  4988. }
  4989. }
  4990. void *SDL_Metal_GetLayer(SDL_MetalView view)
  4991. {
  4992. if (_this && _this->Metal_GetLayer) {
  4993. if (view) {
  4994. return _this->Metal_GetLayer(_this, view);
  4995. } else {
  4996. SDL_InvalidParamError("view");
  4997. return NULL;
  4998. }
  4999. } else {
  5000. SDL_SetError("Metal is not supported.");
  5001. return NULL;
  5002. }
  5003. }
  5004. #if defined(SDL_VIDEO_DRIVER_X11) || defined(SDL_VIDEO_DRIVER_WAYLAND) || defined(SDL_VIDEO_DRIVER_EMSCRIPTEN)
  5005. const char *SDL_GetCSSCursorName(SDL_SystemCursor id, const char **fallback_name)
  5006. {
  5007. // Reference: https://www.w3.org/TR/css-ui-4/#cursor
  5008. // Also in: https://www.freedesktop.org/wiki/Specifications/cursor-spec/
  5009. switch (id) {
  5010. case SDL_SYSTEM_CURSOR_DEFAULT:
  5011. return "default";
  5012. case SDL_SYSTEM_CURSOR_TEXT:
  5013. return "text";
  5014. case SDL_SYSTEM_CURSOR_WAIT:
  5015. return "wait";
  5016. case SDL_SYSTEM_CURSOR_CROSSHAIR:
  5017. return "crosshair";
  5018. case SDL_SYSTEM_CURSOR_PROGRESS:
  5019. return "progress";
  5020. case SDL_SYSTEM_CURSOR_NWSE_RESIZE:
  5021. if (fallback_name) {
  5022. // only a single arrow
  5023. *fallback_name = "nw-resize";
  5024. }
  5025. return "nwse-resize";
  5026. case SDL_SYSTEM_CURSOR_NESW_RESIZE:
  5027. if (fallback_name) {
  5028. // only a single arrow
  5029. *fallback_name = "ne-resize";
  5030. }
  5031. return "nesw-resize";
  5032. case SDL_SYSTEM_CURSOR_EW_RESIZE:
  5033. if (fallback_name) {
  5034. *fallback_name = "col-resize";
  5035. }
  5036. return "ew-resize";
  5037. case SDL_SYSTEM_CURSOR_NS_RESIZE:
  5038. if (fallback_name) {
  5039. *fallback_name = "row-resize";
  5040. }
  5041. return "ns-resize";
  5042. case SDL_SYSTEM_CURSOR_MOVE:
  5043. return "all-scroll";
  5044. case SDL_SYSTEM_CURSOR_NOT_ALLOWED:
  5045. return "not-allowed";
  5046. case SDL_SYSTEM_CURSOR_POINTER:
  5047. return "pointer";
  5048. case SDL_SYSTEM_CURSOR_NW_RESIZE:
  5049. return "nw-resize";
  5050. case SDL_SYSTEM_CURSOR_N_RESIZE:
  5051. return "n-resize";
  5052. case SDL_SYSTEM_CURSOR_NE_RESIZE:
  5053. return "ne-resize";
  5054. case SDL_SYSTEM_CURSOR_E_RESIZE:
  5055. return "e-resize";
  5056. case SDL_SYSTEM_CURSOR_SE_RESIZE:
  5057. return "se-resize";
  5058. case SDL_SYSTEM_CURSOR_S_RESIZE:
  5059. return "s-resize";
  5060. case SDL_SYSTEM_CURSOR_SW_RESIZE:
  5061. return "sw-resize";
  5062. case SDL_SYSTEM_CURSOR_W_RESIZE:
  5063. return "w-resize";
  5064. default:
  5065. return "default";
  5066. }
  5067. }
  5068. #endif