SDL_video.c 183 KB

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