SDL_video.c 178 KB

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