12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690 |
- /*
- Simple DirectMedia Layer
- Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
- This software is provided 'as-is', without any express or implied
- warranty. In no event will the authors be held liable for any damages
- arising from the use of this software.
- Permission is granted to anyone to use this software for any purpose,
- including commercial applications, and to alter it and redistribute it
- freely, subject to the following restrictions:
- 1. The origin of this software must not be misrepresented; you must not
- claim that you wrote the original software. If you use this software
- in a product, an acknowledgment in the product documentation would be
- appreciated but is not required.
- 2. Altered source versions must be plainly marked as such, and must not be
- misrepresented as being the original software.
- 3. This notice may not be removed or altered from any source distribution.
- */
- #include "SDL_internal.h"
- /* The high-level video driver subsystem */
- #include "SDL_sysvideo.h"
- #include "SDL_egl_c.h"
- #include "SDL_blit.h"
- #include "SDL_pixels_c.h"
- #include "SDL_rect_c.h"
- #include "SDL_video_c.h"
- #include "../events/SDL_events_c.h"
- #include "../SDL_hints_c.h"
- #include "../SDL_properties_c.h"
- #include "../timer/SDL_timer_c.h"
- #include "../camera/SDL_camera_c.h"
- #include "../render/SDL_sysrender.h"
- #ifdef SDL_VIDEO_OPENGL
- #include <SDL3/SDL_opengl.h>
- #endif /* SDL_VIDEO_OPENGL */
- #if defined(SDL_VIDEO_OPENGL_ES) && !defined(SDL_VIDEO_OPENGL)
- #include <SDL3/SDL_opengles.h>
- #endif /* SDL_VIDEO_OPENGL_ES && !SDL_VIDEO_OPENGL */
- /* GL and GLES2 headers conflict on Linux 32 bits */
- #if defined(SDL_VIDEO_OPENGL_ES2) && !defined(SDL_VIDEO_OPENGL)
- #include <SDL3/SDL_opengles2.h>
- #endif /* SDL_VIDEO_OPENGL_ES2 && !SDL_VIDEO_OPENGL */
- #ifndef SDL_VIDEO_OPENGL
- #ifndef GL_CONTEXT_RELEASE_BEHAVIOR_KHR
- #define GL_CONTEXT_RELEASE_BEHAVIOR_KHR 0x82FB
- #endif
- #endif
- #ifdef SDL_PLATFORM_EMSCRIPTEN
- #include <emscripten.h>
- #endif
- #ifdef SDL_PLATFORM_LINUX
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #endif
- /* Available video drivers */
- static VideoBootStrap *bootstrap[] = {
- #ifdef SDL_VIDEO_DRIVER_COCOA
- &COCOA_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_WAYLAND
- &Wayland_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_X11
- &X11_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_VIVANTE
- &VIVANTE_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_WINDOWS
- &WINDOWS_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_WINRT
- &WINRT_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_HAIKU
- &HAIKU_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_UIKIT
- &UIKIT_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_ANDROID
- &Android_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_PS2
- &PS2_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_PSP
- &PSP_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_VITA
- &VITA_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_N3DS
- &N3DS_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_KMSDRM
- &KMSDRM_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_RISCOS
- &RISCOS_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_RPI
- &RPI_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_EMSCRIPTEN
- &Emscripten_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_QNX
- &QNX_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_NGAGE
- &NGAGE_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_OFFSCREEN
- &OFFSCREEN_bootstrap,
- #endif
- #ifdef SDL_VIDEO_DRIVER_DUMMY
- &DUMMY_bootstrap,
- #ifdef SDL_INPUT_LINUXEV
- &DUMMY_evdev_bootstrap,
- #endif
- #endif
- NULL
- };
- #define CHECK_WINDOW_MAGIC(window, retval) \
- if (!_this) { \
- SDL_UninitializedVideo(); \
- return retval; \
- } \
- if (!SDL_ObjectValid(window, SDL_OBJECT_TYPE_WINDOW)) { \
- SDL_SetError("Invalid window"); \
- return retval; \
- }
- #define CHECK_DISPLAY_MAGIC(display, retval) \
- if (!display) { \
- return retval; \
- } \
- #define CHECK_WINDOW_NOT_POPUP(window, retval) \
- if (SDL_WINDOW_IS_POPUP(window)) { \
- SDL_SetError("Operation invalid on popup windows"); \
- return retval; \
- }
- #if defined(SDL_PLATFORM_MACOS) && defined(SDL_VIDEO_DRIVER_COCOA)
- /* Support for macOS fullscreen spaces */
- extern SDL_bool Cocoa_IsWindowInFullscreenSpace(SDL_Window *window);
- extern SDL_bool Cocoa_SetWindowFullscreenSpace(SDL_Window *window, SDL_bool state, SDL_bool blocking);
- #endif
- static void SDL_CheckWindowDisplayChanged(SDL_Window *window);
- static void SDL_CheckWindowDisplayScaleChanged(SDL_Window *window);
- /* Convenience functions for reading driver flags */
- static SDL_bool SDL_ModeSwitchingEmulated(SDL_VideoDevice *_this)
- {
- if (_this->device_caps & VIDEO_DEVICE_CAPS_MODE_SWITCHING_EMULATED) {
- return SDL_TRUE;
- }
- return SDL_FALSE;
- }
- static SDL_bool SDL_SendsFullscreenDimensions(SDL_VideoDevice *_this)
- {
- return !!(_this->device_caps & VIDEO_DEVICE_CAPS_SENDS_FULLSCREEN_DIMENSIONS);
- }
- static SDL_bool IsFullscreenOnly(SDL_VideoDevice *_this)
- {
- return !!(_this->device_caps & VIDEO_DEVICE_CAPS_FULLSCREEN_ONLY);
- }
- static SDL_bool SDL_SendsDisplayChanges(SDL_VideoDevice *_this)
- {
- return !!(_this->device_caps & VIDEO_DEVICE_CAPS_SENDS_DISPLAY_CHANGES);
- }
- static SDL_bool SDL_DisableMouseWarpOnFullscreenTransitions(SDL_VideoDevice *_this)
- {
- return !!(_this->device_caps & VIDEO_DEVICE_CAPS_DISABLE_MOUSE_WARP_ON_FULLSCREEN_TRANSITIONS);
- }
- static SDL_bool SDL_DriverSendsHDRChanges(SDL_VideoDevice *_this)
- {
- return !!(_this->device_caps & VIDEO_DEVICE_CAPS_SENDS_HDR_CHANGES);
- }
- /* Hint to treat all window ops as synchronous */
- static SDL_bool syncHint;
- static void SDL_SyncHintWatcher(void *userdata, const char *name, const char *oldValue, const char *newValue)
- {
- syncHint = SDL_GetStringBoolean(newValue, SDL_FALSE);
- }
- static void SDL_SyncIfRequired(SDL_Window *window)
- {
- if (syncHint) {
- SDL_SyncWindow(window);
- }
- }
- static void SDL_SetWindowParent(SDL_Window *window, SDL_Window *parent)
- {
- /* Unlink the window from the existing parent. */
- if (window->parent) {
- if (window->next_sibling) {
- window->next_sibling->prev_sibling = window->prev_sibling;
- }
- if (window->prev_sibling) {
- window->prev_sibling->next_sibling = window->next_sibling;
- } else {
- window->parent->first_child = window->next_sibling;
- }
- window->parent = NULL;
- }
- if (parent) {
- window->parent = parent;
- window->next_sibling = parent->first_child;
- if (parent->first_child) {
- parent->first_child->prev_sibling = window;
- }
- parent->first_child = window;
- }
- }
- /* Support for framebuffer emulation using an accelerated renderer */
- #define SDL_PROP_WINDOW_TEXTUREDATA_POINTER "SDL.internal.window.texturedata"
- typedef struct
- {
- SDL_Renderer *renderer;
- SDL_Texture *texture;
- void *pixels;
- int pitch;
- int bytes_per_pixel;
- } SDL_WindowTextureData;
- static Uint32 SDL_DefaultGraphicsBackends(SDL_VideoDevice *_this)
- {
- #if (defined(SDL_VIDEO_OPENGL) && defined(SDL_PLATFORM_MACOS)) || (defined(SDL_PLATFORM_IOS) && !TARGET_OS_MACCATALYST) || defined(SDL_PLATFORM_ANDROID)
- if (_this->GL_CreateContext) {
- return SDL_WINDOW_OPENGL;
- }
- #endif
- #if defined(SDL_VIDEO_METAL) && (TARGET_OS_MACCATALYST || defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS))
- if (_this->Metal_CreateView) {
- return SDL_WINDOW_METAL;
- }
- #endif
- return 0;
- }
- static void SDLCALL SDL_CleanupWindowTextureData(void *userdata, void *value)
- {
- SDL_WindowTextureData *data = (SDL_WindowTextureData *)value;
- if (data->texture) {
- SDL_DestroyTexture(data->texture);
- }
- if (data->renderer) {
- SDL_DestroyRenderer(data->renderer);
- }
- SDL_free(data->pixels);
- SDL_free(data);
- }
- static int SDL_CreateWindowTexture(SDL_VideoDevice *_this, SDL_Window *window, SDL_PixelFormat *format, void **pixels, int *pitch)
- {
- SDL_PropertiesID props = SDL_GetWindowProperties(window);
- SDL_WindowTextureData *data = (SDL_WindowTextureData *)SDL_GetPointerProperty(props, SDL_PROP_WINDOW_TEXTUREDATA_POINTER, NULL);
- const SDL_bool transparent = (window->flags & SDL_WINDOW_TRANSPARENT) ? SDL_TRUE : SDL_FALSE;
- int i;
- int w, h;
- const SDL_PixelFormat *texture_formats;
- SDL_GetWindowSizeInPixels(window, &w, &h);
- if (!data) {
- SDL_Renderer *renderer = NULL;
- const char *render_driver = NULL;
- const char *hint;
- /* See if there's a render driver being requested */
- hint = SDL_GetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION);
- if (hint && *hint != '0' && *hint != '1' &&
- SDL_strcasecmp(hint, "true") != 0 &&
- SDL_strcasecmp(hint, "false") != 0 &&
- SDL_strcasecmp(hint, SDL_SOFTWARE_RENDERER) != 0) {
- render_driver = hint;
- }
- if (!render_driver) {
- hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
- if (hint && *hint && SDL_strcasecmp(hint, SDL_SOFTWARE_RENDERER) != 0) {
- render_driver = hint;
- }
- }
- /* Check to see if there's a specific driver requested */
- if (render_driver) {
- renderer = SDL_CreateRenderer(window, render_driver);
- if (!renderer) {
- /* The error for this specific renderer has already been set */
- return -1;
- }
- } else {
- const int total = SDL_GetNumRenderDrivers();
- for (i = 0; i < total; ++i) {
- const char *name = SDL_GetRenderDriver(i);
- if (name && SDL_strcmp(name, SDL_SOFTWARE_RENDERER) != 0) {
- renderer = SDL_CreateRenderer(window, name);
- if (renderer) {
- break; /* this will work. */
- }
- }
- }
- if (!renderer) {
- return SDL_SetError("No hardware accelerated renderers available");
- }
- }
- SDL_assert(renderer != NULL); /* should have explicitly checked this above. */
- /* Create the data after we successfully create the renderer (bug #1116) */
- data = (SDL_WindowTextureData *)SDL_calloc(1, sizeof(*data));
- if (!data) {
- SDL_DestroyRenderer(renderer);
- return -1;
- }
- if (SDL_SetPointerPropertyWithCleanup(props, SDL_PROP_WINDOW_TEXTUREDATA_POINTER, data, SDL_CleanupWindowTextureData, NULL) < 0) {
- SDL_DestroyRenderer(renderer);
- return -1;
- }
- data->renderer = renderer;
- }
- texture_formats = (const SDL_PixelFormat *)SDL_GetPointerProperty(SDL_GetRendererProperties(data->renderer), SDL_PROP_RENDERER_TEXTURE_FORMATS_POINTER, NULL);
- if (!texture_formats) {
- return -1;
- }
- /* Free any old texture and pixel data */
- if (data->texture) {
- SDL_DestroyTexture(data->texture);
- data->texture = NULL;
- }
- SDL_free(data->pixels);
- data->pixels = NULL;
- /* Find the first format with or without an alpha channel */
- *format = texture_formats[0];
- for (i = 0; texture_formats[i] != SDL_PIXELFORMAT_UNKNOWN; ++i) {
- SDL_PixelFormat texture_format = texture_formats[i];
- if (!SDL_ISPIXELFORMAT_FOURCC(texture_format) &&
- !SDL_ISPIXELFORMAT_10BIT(texture_format) &&
- !SDL_ISPIXELFORMAT_FLOAT(texture_format) &&
- transparent == SDL_ISPIXELFORMAT_ALPHA(texture_format)) {
- *format = texture_format;
- break;
- }
- }
- data->texture = SDL_CreateTexture(data->renderer, *format,
- SDL_TEXTUREACCESS_STREAMING,
- w, h);
- if (!data->texture) {
- /* codechecker_false_positive [Malloc] Static analyzer doesn't realize allocated `data` is saved to SDL_PROP_WINDOW_TEXTUREDATA_POINTER and not leaked here. */
- return -1; /* NOLINT(clang-analyzer-unix.Malloc) */
- }
- /* Create framebuffer data */
- data->bytes_per_pixel = SDL_BYTESPERPIXEL(*format);
- data->pitch = (((w * data->bytes_per_pixel) + 3) & ~3);
- {
- /* Make static analysis happy about potential SDL_malloc(0) calls. */
- const size_t allocsize = (size_t)h * data->pitch;
- data->pixels = SDL_malloc((allocsize > 0) ? allocsize : 1);
- if (!data->pixels) {
- return -1;
- }
- }
- *pixels = data->pixels;
- *pitch = data->pitch;
- /* Make sure we're not double-scaling the viewport */
- SDL_SetRenderViewport(data->renderer, NULL);
- return 0;
- }
- int SDL_SetWindowTextureVSync(SDL_VideoDevice *_this, SDL_Window *window, int vsync)
- {
- SDL_WindowTextureData *data;
- data = (SDL_WindowTextureData *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_TEXTUREDATA_POINTER, NULL);
- if (!data) {
- return -1;
- }
- if (!data->renderer) {
- return -1;
- }
- return SDL_SetRenderVSync(data->renderer, vsync);
- }
- static int SDL_GetWindowTextureVSync(SDL_VideoDevice *_this, SDL_Window *window, int *vsync)
- {
- SDL_WindowTextureData *data;
- data = (SDL_WindowTextureData *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_TEXTUREDATA_POINTER, NULL);
- if (!data) {
- return -1;
- }
- if (!data->renderer) {
- return -1;
- }
- return SDL_GetRenderVSync(data->renderer, vsync);
- }
- static int SDL_UpdateWindowTexture(SDL_VideoDevice *_this, SDL_Window *window, const SDL_Rect *rects, int numrects)
- {
- SDL_WindowTextureData *data;
- SDL_Rect rect;
- void *src;
- int w, h;
- SDL_GetWindowSizeInPixels(window, &w, &h);
- data = (SDL_WindowTextureData *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_TEXTUREDATA_POINTER, NULL);
- if (!data || !data->texture) {
- return SDL_SetError("No window texture data");
- }
- /* Update a single rect that contains subrects for best DMA performance */
- if (SDL_GetSpanEnclosingRect(w, h, numrects, rects, &rect)) {
- src = (void *)((Uint8 *)data->pixels +
- rect.y * data->pitch +
- rect.x * data->bytes_per_pixel);
- if (SDL_UpdateTexture(data->texture, &rect, src, data->pitch) < 0) {
- return -1;
- }
- if (SDL_RenderTexture(data->renderer, data->texture, NULL, NULL) < 0) {
- return -1;
- }
- SDL_RenderPresent(data->renderer);
- }
- return 0;
- }
- static void SDL_DestroyWindowTexture(SDL_VideoDevice *_this, SDL_Window *window)
- {
- SDL_ClearProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_TEXTUREDATA_POINTER);
- }
- static SDL_VideoDevice *_this = NULL;
- static SDL_AtomicInt SDL_messagebox_count;
- static int SDLCALL cmpmodes(const void *A, const void *B)
- {
- const SDL_DisplayMode *a = (const SDL_DisplayMode *)A;
- const SDL_DisplayMode *b = (const SDL_DisplayMode *)B;
- int a_refresh_rate = (int)(a->refresh_rate * 100);
- int b_refresh_rate = (int)(b->refresh_rate * 100);
- int a_pixel_density = (int)(a->pixel_density * 100);
- int b_pixel_density = (int)(b->pixel_density * 100);
- if (a->w != b->w) {
- return b->w - a->w;
- } else if (a->h != b->h) {
- return b->h - a->h;
- } else if (SDL_BITSPERPIXEL(a->format) != SDL_BITSPERPIXEL(b->format)) {
- return SDL_BITSPERPIXEL(b->format) - SDL_BITSPERPIXEL(a->format);
- } else if (SDL_PIXELLAYOUT(a->format) != SDL_PIXELLAYOUT(b->format)) {
- return SDL_PIXELLAYOUT(b->format) - SDL_PIXELLAYOUT(a->format);
- } else if (a_refresh_rate != b_refresh_rate) {
- return b_refresh_rate - a_refresh_rate;
- } else if (a_pixel_density != b_pixel_density) {
- return a_pixel_density - b_pixel_density;
- }
- return 0;
- }
- static int SDL_UninitializedVideo(void)
- {
- return SDL_SetError("Video subsystem has not been initialized");
- }
- int SDL_GetNumVideoDrivers(void)
- {
- return SDL_arraysize(bootstrap) - 1;
- }
- // this returns string literals, so there's no need to use SDL_FreeLater.
- const char *SDL_GetVideoDriver(int index)
- {
- if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
- return bootstrap[index]->name;
- }
- return NULL;
- }
- /*
- * Initialize the video and event subsystems -- determine native pixel format
- */
- int SDL_VideoInit(const char *driver_name)
- {
- SDL_VideoDevice *video;
- SDL_bool init_events = SDL_FALSE;
- SDL_bool init_keyboard = SDL_FALSE;
- SDL_bool init_mouse = SDL_FALSE;
- SDL_bool init_touch = SDL_FALSE;
- int i = 0;
- /* Check to make sure we don't overwrite '_this' */
- if (_this) {
- SDL_VideoQuit();
- }
- SDL_InitTicks();
- /* Start the event loop */
- if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) {
- goto pre_driver_error;
- }
- init_events = SDL_TRUE;
- if (SDL_InitKeyboard() < 0) {
- goto pre_driver_error;
- }
- init_keyboard = SDL_TRUE;
- if (SDL_PreInitMouse() < 0) {
- goto pre_driver_error;
- }
- init_mouse = SDL_TRUE;
- if (SDL_InitTouch() < 0) {
- goto pre_driver_error;
- }
- init_touch = SDL_TRUE;
- /* Select the proper video driver */
- video = NULL;
- if (!driver_name) {
- driver_name = SDL_GetHint(SDL_HINT_VIDEO_DRIVER);
- }
- if (driver_name && *driver_name != 0) {
- const char *driver_attempt = driver_name;
- while (driver_attempt && *driver_attempt != 0 && !video) {
- const char *driver_attempt_end = SDL_strchr(driver_attempt, ',');
- size_t driver_attempt_len = (driver_attempt_end) ? (driver_attempt_end - driver_attempt)
- : SDL_strlen(driver_attempt);
- for (i = 0; bootstrap[i]; ++i) {
- if ((driver_attempt_len == SDL_strlen(bootstrap[i]->name)) &&
- (SDL_strncasecmp(bootstrap[i]->name, driver_attempt, driver_attempt_len) == 0)) {
- video = bootstrap[i]->create();
- break;
- }
- }
- driver_attempt = (driver_attempt_end) ? (driver_attempt_end + 1) : NULL;
- }
- } else {
- for (i = 0; bootstrap[i]; ++i) {
- video = bootstrap[i]->create();
- if (video) {
- break;
- }
- }
- }
- if (!video) {
- if (driver_name) {
- SDL_SetError("%s not available", driver_name);
- goto pre_driver_error;
- }
- SDL_SetError("No available video device");
- goto pre_driver_error;
- }
- /* From this point on, use SDL_VideoQuit to cleanup on error, rather than
- pre_driver_error. */
- _this = video;
- _this->name = bootstrap[i]->name;
- _this->thread = SDL_GetCurrentThreadID();
- /* Set some very sane GL defaults */
- _this->gl_config.driver_loaded = 0;
- _this->gl_config.dll_handle = NULL;
- SDL_GL_ResetAttributes();
- _this->current_glwin_tls = SDL_CreateTLS();
- _this->current_glctx_tls = SDL_CreateTLS();
- /* Initialize the video subsystem */
- if (_this->VideoInit(_this) < 0) {
- SDL_VideoQuit();
- return -1;
- }
- /* Make sure some displays were added */
- if (_this->num_displays == 0) {
- SDL_VideoQuit();
- return SDL_SetError("The video driver did not add any displays");
- }
- SDL_AddHintCallback(SDL_HINT_VIDEO_SYNC_WINDOW_OPERATIONS, SDL_SyncHintWatcher, NULL);
- /* Disable the screen saver by default. This is a change from <= 2.0.1,
- but most things using SDL are games or media players; you wouldn't
- want a screensaver to trigger if you're playing exclusively with a
- joystick, or passively watching a movie. Things that use SDL but
- function more like a normal desktop app should explicitly reenable the
- screensaver. */
- if (!SDL_GetHintBoolean(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, SDL_FALSE)) {
- SDL_DisableScreenSaver();
- }
- SDL_PostInitMouse();
- /* We're ready to go! */
- return 0;
- pre_driver_error:
- SDL_assert(_this == NULL);
- if (init_touch) {
- SDL_QuitTouch();
- }
- if (init_mouse) {
- SDL_QuitMouse();
- }
- if (init_keyboard) {
- SDL_QuitKeyboard();
- }
- if (init_events) {
- SDL_QuitSubSystem(SDL_INIT_EVENTS);
- }
- return -1;
- }
- // this returns string literals, so there's no need to use SDL_FreeLater.
- const char *SDL_GetCurrentVideoDriver(void)
- {
- if (!_this) {
- SDL_UninitializedVideo();
- return NULL;
- }
- return _this->name;
- }
- SDL_VideoDevice *SDL_GetVideoDevice(void)
- {
- return _this;
- }
- SDL_bool SDL_OnVideoThread(void)
- {
- return (_this && SDL_GetCurrentThreadID() == _this->thread);
- }
- void SDL_SetSystemTheme(SDL_SystemTheme theme)
- {
- if (_this && theme != _this->system_theme) {
- _this->system_theme = theme;
- SDL_SendSystemThemeChangedEvent();
- }
- }
- SDL_SystemTheme SDL_GetSystemTheme(void)
- {
- if (_this) {
- return _this->system_theme;
- } else {
- return SDL_SYSTEM_THEME_UNKNOWN;
- }
- }
- static void SDL_UpdateDesktopBounds()
- {
- SDL_Rect rect;
- SDL_zero(rect);
- SDL_DisplayID *displays = SDL_GetDisplays(NULL);
- if (displays) {
- for (int i = 0; displays[i]; ++i) {
- SDL_Rect bounds;
- if (SDL_GetDisplayBounds(displays[i], &bounds) == 0) {
- if (i == 0) {
- SDL_copyp(&rect, &bounds);
- } else {
- SDL_GetRectUnion(&rect, &bounds, &rect);
- }
- }
- }
- SDL_free(displays);
- }
- SDL_copyp(&_this->desktop_bounds, &rect);
- }
- static void SDL_FinalizeDisplayMode(SDL_DisplayMode *mode)
- {
- /* Make sure all the fields are set up correctly */
- if (mode->pixel_density <= 0.0f) {
- mode->pixel_density = 1.0f;
- }
- }
- SDL_DisplayID SDL_AddBasicVideoDisplay(const SDL_DisplayMode *desktop_mode)
- {
- SDL_VideoDisplay display;
- SDL_zero(display);
- if (desktop_mode) {
- SDL_memcpy(&display.desktop_mode, desktop_mode, sizeof(display.desktop_mode));
- }
- return SDL_AddVideoDisplay(&display, SDL_FALSE);
- }
- SDL_DisplayID SDL_AddVideoDisplay(const SDL_VideoDisplay *display, SDL_bool send_event)
- {
- SDL_VideoDisplay **displays, *new_display;
- SDL_DisplayID id;
- SDL_PropertiesID props;
- int i;
- new_display = (SDL_VideoDisplay *)SDL_malloc(sizeof(*new_display));
- if (!new_display) {
- return 0;
- }
- displays = (SDL_VideoDisplay **)SDL_realloc(_this->displays, (_this->num_displays + 1) * sizeof(*displays));
- if (!displays) {
- SDL_free(new_display);
- return 0;
- }
- _this->displays = displays;
- _this->displays[_this->num_displays++] = new_display;
- id = SDL_GetNextObjectID();
- SDL_copyp(new_display, display);
- new_display->id = id;
- new_display->device = _this;
- if (display->name) {
- new_display->name = SDL_strdup(display->name);
- } else {
- char name[32];
- SDL_itoa(id, name, 10);
- new_display->name = SDL_strdup(name);
- }
- if (new_display->content_scale == 0.0f) {
- new_display->content_scale = 1.0f;
- }
- new_display->desktop_mode.displayID = id;
- new_display->current_mode = &new_display->desktop_mode;
- SDL_FinalizeDisplayMode(&new_display->desktop_mode);
- for (i = 0; i < new_display->num_fullscreen_modes; ++i) {
- new_display->fullscreen_modes[i].displayID = id;
- }
- new_display->HDR.HDR_headroom = SDL_max(display->HDR.HDR_headroom, 1.0f);
- new_display->HDR.SDR_white_level = SDL_max(display->HDR.SDR_white_level, 1.0f);
- props = SDL_GetDisplayProperties(id);
- SDL_SetBooleanProperty(props, SDL_PROP_DISPLAY_HDR_ENABLED_BOOLEAN, new_display->HDR.HDR_headroom > 1.0f);
- SDL_UpdateDesktopBounds();
- if (send_event) {
- SDL_SendDisplayEvent(new_display, SDL_EVENT_DISPLAY_ADDED, 0);
- }
- return id;
- }
- void SDL_OnDisplayAdded(SDL_VideoDisplay *display)
- {
- SDL_Window *window;
- /* See if any windows have changed to the new display */
- for (window = _this->windows; window; window = window->next) {
- SDL_CheckWindowDisplayChanged(window);
- }
- }
- void SDL_OnDisplayMoved(SDL_VideoDisplay *display)
- {
- SDL_UpdateDesktopBounds();
- }
- void SDL_DelVideoDisplay(SDL_DisplayID displayID, SDL_bool send_event)
- {
- SDL_VideoDisplay *display;
- int display_index = SDL_GetDisplayIndex(displayID);
- if (display_index < 0) {
- return;
- }
- display = _this->displays[display_index];
- if (send_event) {
- SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_REMOVED, 0);
- }
- SDL_DestroyProperties(display->props);
- SDL_free(display->name);
- SDL_ResetFullscreenDisplayModes(display);
- SDL_free(display->desktop_mode.driverdata);
- display->desktop_mode.driverdata = NULL;
- SDL_free(display->driverdata);
- display->driverdata = NULL;
- SDL_free(display);
- if (display_index < (_this->num_displays - 1)) {
- SDL_memmove(&_this->displays[display_index], &_this->displays[display_index + 1], (_this->num_displays - display_index - 1) * sizeof(_this->displays[display_index]));
- }
- --_this->num_displays;
- SDL_UpdateDesktopBounds();
- }
- SDL_DisplayID *SDL_GetDisplays(int *count)
- {
- int i;
- SDL_DisplayID *displays;
- if (!_this) {
- if (count) {
- *count = 0;
- }
- SDL_UninitializedVideo();
- return NULL;
- }
- displays = (SDL_DisplayID *)SDL_malloc((_this->num_displays + 1) * sizeof(*displays));
- if (displays) {
- if (count) {
- *count = _this->num_displays;
- }
- for (i = 0; i < _this->num_displays; ++i) {
- displays[i] = _this->displays[i]->id;
- }
- displays[i] = 0;
- } else {
- if (count) {
- *count = 0;
- }
- }
- return displays;
- }
- SDL_VideoDisplay *SDL_GetVideoDisplay(SDL_DisplayID displayID)
- {
- int display_index;
- display_index = SDL_GetDisplayIndex(displayID);
- if (display_index < 0) {
- return NULL;
- }
- return _this->displays[display_index];
- }
- SDL_VideoDisplay *SDL_GetVideoDisplayForWindow(SDL_Window *window)
- {
- return SDL_GetVideoDisplay(SDL_GetDisplayForWindow(window));
- }
- SDL_DisplayID SDL_GetPrimaryDisplay(void)
- {
- if (!_this || _this->num_displays == 0) {
- SDL_UninitializedVideo();
- return 0;
- }
- return _this->displays[0]->id;
- }
- int SDL_GetDisplayIndex(SDL_DisplayID displayID)
- {
- int display_index;
- if (!_this) {
- return SDL_UninitializedVideo();
- }
- for (display_index = 0; display_index < _this->num_displays; ++display_index) {
- if (displayID == _this->displays[display_index]->id) {
- return display_index;
- }
- }
- return SDL_SetError("Invalid display");
- }
- SDL_DisplayData *SDL_GetDisplayDriverData(SDL_DisplayID displayID)
- {
- SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
- CHECK_DISPLAY_MAGIC(display, NULL);
- return display->driverdata;
- }
- SDL_DisplayData *SDL_GetDisplayDriverDataForWindow(SDL_Window *window)
- {
- return SDL_GetDisplayDriverData(SDL_GetDisplayForWindow(window));
- }
- SDL_PropertiesID SDL_GetDisplayProperties(SDL_DisplayID displayID)
- {
- SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
- CHECK_DISPLAY_MAGIC(display, 0);
- if (display->props == 0) {
- display->props = SDL_CreateProperties();
- }
- return display->props;
- }
- const char *SDL_GetDisplayName(SDL_DisplayID displayID)
- {
- SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
- CHECK_DISPLAY_MAGIC(display, NULL);
- return display->name;
- }
- int SDL_GetDisplayBounds(SDL_DisplayID displayID, SDL_Rect *rect)
- {
- SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
- CHECK_DISPLAY_MAGIC(display, -1);
- if (!rect) {
- return SDL_InvalidParamError("rect");
- }
- if (_this->GetDisplayBounds) {
- if (_this->GetDisplayBounds(_this, display, rect) == 0) {
- return 0;
- }
- }
- /* Assume that the displays are left to right */
- if (displayID == SDL_GetPrimaryDisplay()) {
- rect->x = 0;
- rect->y = 0;
- } else {
- SDL_GetDisplayBounds(_this->displays[SDL_GetDisplayIndex(displayID) - 1]->id, rect);
- rect->x += rect->w;
- }
- rect->w = display->current_mode->w;
- rect->h = display->current_mode->h;
- return 0;
- }
- static int ParseDisplayUsableBoundsHint(SDL_Rect *rect)
- {
- const char *hint = SDL_GetHint(SDL_HINT_DISPLAY_USABLE_BOUNDS);
- return hint && (SDL_sscanf(hint, "%d,%d,%d,%d", &rect->x, &rect->y, &rect->w, &rect->h) == 4);
- }
- int SDL_GetDisplayUsableBounds(SDL_DisplayID displayID, SDL_Rect *rect)
- {
- SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
- CHECK_DISPLAY_MAGIC(display, -1);
- if (!rect) {
- return SDL_InvalidParamError("rect");
- }
- if (displayID == SDL_GetPrimaryDisplay() && ParseDisplayUsableBoundsHint(rect)) {
- return 0;
- }
- if (_this->GetDisplayUsableBounds) {
- if (_this->GetDisplayUsableBounds(_this, display, rect) == 0) {
- return 0;
- }
- }
- /* Oh well, just give the entire display bounds. */
- return SDL_GetDisplayBounds(displayID, rect);
- }
- SDL_DisplayOrientation SDL_GetNaturalDisplayOrientation(SDL_DisplayID displayID)
- {
- SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
- CHECK_DISPLAY_MAGIC(display, SDL_ORIENTATION_UNKNOWN);
- if (display->natural_orientation != SDL_ORIENTATION_UNKNOWN) {
- return display->natural_orientation;
- } else {
- /* Default to landscape if the driver hasn't set it */
- return SDL_ORIENTATION_LANDSCAPE;
- }
- }
- SDL_DisplayOrientation SDL_GetCurrentDisplayOrientation(SDL_DisplayID displayID)
- {
- SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
- CHECK_DISPLAY_MAGIC(display, SDL_ORIENTATION_UNKNOWN);
- if (display->current_orientation != SDL_ORIENTATION_UNKNOWN) {
- return display->current_orientation;
- } else {
- /* Default to landscape if the driver hasn't set it */
- return SDL_ORIENTATION_LANDSCAPE;
- }
- }
- void SDL_SetDisplayContentScale(SDL_VideoDisplay *display, float scale)
- {
- if (scale != display->content_scale) {
- SDL_Window *window;
- display->content_scale = scale;
- SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED, 0);
- /* Check the windows on this display */
- for (window = _this->windows; window; window = window->next) {
- if (display->id == window->last_displayID) {
- SDL_CheckWindowDisplayScaleChanged(window);
- }
- }
- }
- }
- float SDL_GetDisplayContentScale(SDL_DisplayID displayID)
- {
- SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
- CHECK_DISPLAY_MAGIC(display, 0.0f);
- return display->content_scale;
- }
- void SDL_SetWindowHDRProperties(SDL_Window *window, const SDL_HDROutputProperties *HDR, SDL_bool send_event)
- {
- if (window->HDR.HDR_headroom != HDR->HDR_headroom || window->HDR.SDR_white_level != window->HDR.SDR_white_level) {
- SDL_PropertiesID window_props = SDL_GetWindowProperties(window);
- SDL_SetFloatProperty(window_props, SDL_PROP_WINDOW_HDR_HEADROOM_FLOAT, SDL_max(HDR->HDR_headroom, 1.0f));
- SDL_SetFloatProperty(window_props, SDL_PROP_WINDOW_SDR_WHITE_LEVEL_FLOAT, SDL_max(HDR->SDR_white_level, 1.0f));
- SDL_SetBooleanProperty(window_props, SDL_PROP_WINDOW_HDR_ENABLED_BOOLEAN, HDR->HDR_headroom > 1.0f);
- SDL_copyp(&window->HDR, HDR);
- if (send_event) {
- SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_HDR_STATE_CHANGED, HDR->HDR_headroom > 1.0f, 0);
- }
- }
- }
- void SDL_SetDisplayHDRProperties(SDL_VideoDisplay *display, const SDL_HDROutputProperties *HDR)
- {
- SDL_bool changed = SDL_FALSE;
- if (HDR->SDR_white_level != display->HDR.SDR_white_level) {
- display->HDR.SDR_white_level = SDL_max(HDR->SDR_white_level, 1.0f);
- changed = SDL_TRUE;
- }
- if (HDR->HDR_headroom != display->HDR.HDR_headroom) {
- display->HDR.HDR_headroom = SDL_max(HDR->HDR_headroom, 1.0f);
- changed = SDL_TRUE;
- }
- SDL_copyp(&display->HDR, HDR);
- if (changed && !SDL_DriverSendsHDRChanges(_this)) {
- for (SDL_Window *w = display->device->windows; w; w = w->next) {
- if (SDL_GetDisplayForWindow(w) == display->id) {
- SDL_SetWindowHDRProperties(w, &display->HDR, SDL_TRUE);
- }
- }
- }
- }
- static const SDL_DisplayMode *SDL_GetFullscreenModeMatch(const SDL_DisplayMode *mode)
- {
- const SDL_DisplayMode **modes;
- SDL_DisplayMode fullscreen_mode;
- if (mode->w <= 0 || mode->h <= 0) {
- /* Use the desktop mode */
- return NULL;
- }
- SDL_memcpy(&fullscreen_mode, mode, sizeof(fullscreen_mode));
- if (fullscreen_mode.displayID == 0) {
- fullscreen_mode.displayID = SDL_GetPrimaryDisplay();
- }
- SDL_FinalizeDisplayMode(&fullscreen_mode);
- mode = NULL;
- modes = SDL_GetFullscreenDisplayModes(fullscreen_mode.displayID, NULL);
- if (modes) {
- int i;
- /* Search for an exact match */
- if (!mode) {
- for (i = 0; modes[i]; ++i) {
- if (SDL_memcmp(&fullscreen_mode, modes[i], sizeof(fullscreen_mode)) == 0) {
- mode = modes[i];
- break;
- }
- }
- }
- /* Search for a mode with the same characteristics */
- if (!mode) {
- for (i = 0; modes[i]; ++i) {
- if (cmpmodes(&fullscreen_mode, modes[i]) == 0) {
- mode = modes[i];
- break;
- }
- }
- }
- SDL_free((void *)modes);
- }
- return mode;
- }
- SDL_bool SDL_AddFullscreenDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode *mode)
- {
- SDL_DisplayMode *modes;
- SDL_DisplayMode new_mode;
- int i, nmodes;
- /* Finalize the mode for the display */
- SDL_memcpy(&new_mode, mode, sizeof(new_mode));
- new_mode.displayID = display->id;
- SDL_FinalizeDisplayMode(&new_mode);
- /* Make sure we don't already have the mode in the list */
- modes = display->fullscreen_modes;
- nmodes = display->num_fullscreen_modes;
- for (i = 0; i < nmodes; ++i) {
- if (cmpmodes(&new_mode, &modes[i]) == 0) {
- return SDL_FALSE;
- }
- }
- /* Go ahead and add the new mode */
- if (nmodes == display->max_fullscreen_modes) {
- modes = (SDL_DisplayMode *)SDL_malloc((display->max_fullscreen_modes + 32) * sizeof(*modes));
- if (!modes) {
- return SDL_FALSE;
- }
- if (display->fullscreen_modes) {
- /* Copy the list and update the current mode pointer, if necessary. */
- SDL_memcpy(modes, display->fullscreen_modes, nmodes * sizeof(*modes));
- for (i = 0; i < nmodes; ++i) {
- if (display->current_mode == &display->fullscreen_modes[i]) {
- display->current_mode = &modes[i];
- }
- }
- SDL_free(display->fullscreen_modes);
- }
- display->fullscreen_modes = modes;
- display->max_fullscreen_modes += 32;
- }
- SDL_memcpy(&modes[display->num_fullscreen_modes++], &new_mode, sizeof(new_mode));
- /* Re-sort video modes */
- SDL_qsort(display->fullscreen_modes, display->num_fullscreen_modes,
- sizeof(SDL_DisplayMode), cmpmodes);
- return SDL_TRUE;
- }
- void SDL_ResetFullscreenDisplayModes(SDL_VideoDisplay *display)
- {
- int i;
- for (i = display->num_fullscreen_modes; i--;) {
- SDL_free(display->fullscreen_modes[i].driverdata);
- display->fullscreen_modes[i].driverdata = NULL;
- }
- SDL_free(display->fullscreen_modes);
- display->fullscreen_modes = NULL;
- display->num_fullscreen_modes = 0;
- display->max_fullscreen_modes = 0;
- display->current_mode = &display->desktop_mode;
- }
- const SDL_DisplayMode **SDL_GetFullscreenDisplayModes(SDL_DisplayID displayID, int *count)
- {
- int i;
- const SDL_DisplayMode **modes;
- SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
- if (count) {
- *count = 0;
- }
- CHECK_DISPLAY_MAGIC(display, NULL);
- if (display->num_fullscreen_modes == 0 && _this->GetDisplayModes) {
- _this->GetDisplayModes(_this, display);
- }
- modes = (const SDL_DisplayMode **)SDL_malloc((display->num_fullscreen_modes + 1) * sizeof(*modes));
- if (modes) {
- if (count) {
- *count = display->num_fullscreen_modes;
- }
- for (i = 0; i < display->num_fullscreen_modes; ++i) {
- modes[i] = &display->fullscreen_modes[i];
- }
- modes[i] = NULL;
- } else {
- if (count) {
- *count = 0;
- }
- }
- return modes;
- }
- const SDL_DisplayMode *SDL_GetClosestFullscreenDisplayMode(SDL_DisplayID displayID, int w, int h, float refresh_rate, SDL_bool include_high_density_modes)
- {
- const SDL_DisplayMode **modes;
- const SDL_DisplayMode *mode, *closest = NULL;
- float aspect_ratio;
- int i;
- if (h > 0) {
- aspect_ratio = (float)w / h;
- } else {
- aspect_ratio = 1.0f;
- }
- if (refresh_rate == 0.0f) {
- mode = SDL_GetDesktopDisplayMode(displayID);
- if (mode) {
- refresh_rate = mode->refresh_rate;
- }
- }
- modes = SDL_GetFullscreenDisplayModes(displayID, NULL);
- if (modes) {
- for (i = 0; modes[i]; ++i) {
- mode = modes[i];
- if (w > mode->w) {
- /* Out of sorted modes large enough here */
- break;
- }
- if (h > mode->h) {
- /* Wider, but not tall enough, due to a different aspect ratio.
- * This mode must be skipped, but closer modes may still follow */
- continue;
- }
- if (mode->pixel_density > 1.0f && !include_high_density_modes) {
- continue;
- }
- if (closest) {
- float current_aspect_ratio = (float)mode->w / mode->h;
- float closest_aspect_ratio = (float)closest->w / closest->h;
- if (SDL_fabsf(aspect_ratio - closest_aspect_ratio) < SDL_fabsf(aspect_ratio - current_aspect_ratio)) {
- /* The mode we already found has a better aspect ratio match */
- continue;
- }
- if (mode->w == closest->w && mode->h == closest->h &&
- SDL_fabsf(closest->refresh_rate - refresh_rate) < SDL_fabsf(mode->refresh_rate - refresh_rate)) {
- /* We already found a mode and the new mode is further from our
- * refresh rate target */
- continue;
- }
- }
- closest = mode;
- }
- SDL_free((void *)modes);
- }
- return closest;
- }
- void SDL_SetDesktopDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode *mode)
- {
- if (display->desktop_mode.driverdata) {
- SDL_free(display->desktop_mode.driverdata);
- }
- SDL_memcpy(&display->desktop_mode, mode, sizeof(*mode));
- display->desktop_mode.displayID = display->id;
- SDL_FinalizeDisplayMode(&display->desktop_mode);
- }
- const SDL_DisplayMode *SDL_GetDesktopDisplayMode(SDL_DisplayID displayID)
- {
- SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
- CHECK_DISPLAY_MAGIC(display, NULL);
- return &display->desktop_mode;
- }
- void SDL_SetCurrentDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode *mode)
- {
- display->current_mode = mode;
- }
- const SDL_DisplayMode *SDL_GetCurrentDisplayMode(SDL_DisplayID displayID)
- {
- SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
- CHECK_DISPLAY_MAGIC(display, NULL);
- /* Make sure our mode list is updated */
- if (display->num_fullscreen_modes == 0 && _this->GetDisplayModes) {
- _this->GetDisplayModes(_this, display);
- }
- return display->current_mode;
- }
- int SDL_SetDisplayModeForDisplay(SDL_VideoDisplay *display, SDL_DisplayMode *mode)
- {
- /* Mode switching is being emulated per-window; nothing to do and cannot fail,
- * except for XWayland, which still needs the actual mode setting call since
- * it's emulated via the XRandR interface.
- */
- if (SDL_ModeSwitchingEmulated(_this) && SDL_strcmp(_this->name, "x11") != 0) {
- return 0;
- }
- if (!mode) {
- mode = &display->desktop_mode;
- }
- if (mode == display->current_mode) {
- return 0;
- }
- /* Actually change the display mode */
- if (_this->SetDisplayMode) {
- int result;
- _this->setting_display_mode = SDL_TRUE;
- result = _this->SetDisplayMode(_this, display, mode);
- _this->setting_display_mode = SDL_FALSE;
- if (result < 0) {
- return -1;
- }
- }
- SDL_SetCurrentDisplayMode(display, mode);
- return 0;
- }
- /**
- * If x, y are outside of rect, snaps them to the closest point inside rect
- * (between rect->x, rect->y, inclusive, and rect->x + w, rect->y + h, exclusive)
- */
- static void SDL_GetClosestPointOnRect(const SDL_Rect *rect, SDL_Point *point)
- {
- const int right = rect->x + rect->w - 1;
- const int bottom = rect->y + rect->h - 1;
- if (point->x < rect->x) {
- point->x = rect->x;
- } else if (point->x > right) {
- point->x = right;
- }
- if (point->y < rect->y) {
- point->y = rect->y;
- } else if (point->y > bottom) {
- point->y = bottom;
- }
- }
- static SDL_DisplayID GetDisplayForRect(int x, int y, int w, int h)
- {
- int i, dist;
- SDL_DisplayID closest = 0;
- int closest_dist = 0x7FFFFFFF;
- SDL_Point closest_point_on_display;
- SDL_Point delta;
- SDL_Point center;
- center.x = x + w / 2;
- center.y = y + h / 2;
- if (_this) {
- for (i = 0; i < _this->num_displays; ++i) {
- SDL_VideoDisplay *display = _this->displays[i];
- SDL_Rect display_rect;
- SDL_GetDisplayBounds(display->id, &display_rect);
- /* Check if the window is fully enclosed */
- if (SDL_GetRectEnclosingPoints(¢er, 1, &display_rect, NULL)) {
- return display->id;
- }
- /* Snap window center to the display rect */
- closest_point_on_display = center;
- SDL_GetClosestPointOnRect(&display_rect, &closest_point_on_display);
- delta.x = center.x - closest_point_on_display.x;
- delta.y = center.y - closest_point_on_display.y;
- dist = (delta.x * delta.x + delta.y * delta.y);
- if (dist < closest_dist) {
- closest = display->id;
- closest_dist = dist;
- }
- }
- }
- if (closest == 0) {
- SDL_SetError("Couldn't find any displays");
- }
- return closest;
- }
- void SDL_RelativeToGlobalForWindow(SDL_Window *window, int rel_x, int rel_y, int *abs_x, int *abs_y)
- {
- SDL_Window *w;
- if (SDL_WINDOW_IS_POPUP(window)) {
- /* Calculate the total offset of the popup from the parents */
- for (w = window->parent; w; w = w->parent) {
- rel_x += w->x;
- rel_y += w->y;
- }
- }
- if (abs_x) {
- *abs_x = rel_x;
- }
- if (abs_y) {
- *abs_y = rel_y;
- }
- }
- void SDL_GlobalToRelativeForWindow(SDL_Window *window, int abs_x, int abs_y, int *rel_x, int *rel_y)
- {
- SDL_Window *w;
- if (SDL_WINDOW_IS_POPUP(window)) {
- /* Convert absolute window coordinates to relative for a popup */
- for (w = window->parent; w; w = w->parent) {
- abs_x -= w->x;
- abs_y -= w->y;
- }
- }
- if (rel_x) {
- *rel_x = abs_x;
- }
- if (rel_y) {
- *rel_y = abs_y;
- }
- }
- SDL_DisplayID SDL_GetDisplayForPoint(const SDL_Point *point)
- {
- return GetDisplayForRect(point->x, point->y, 1, 1);
- }
- SDL_DisplayID SDL_GetDisplayForRect(const SDL_Rect *rect)
- {
- return GetDisplayForRect(rect->x, rect->y, rect->w, rect->h);
- }
- SDL_DisplayID SDL_GetDisplayForWindowPosition(SDL_Window *window)
- {
- int x, y;
- SDL_DisplayID displayID = 0;
- CHECK_WINDOW_MAGIC(window, 0);
- if (_this->GetDisplayForWindow) {
- displayID = _this->GetDisplayForWindow(_this, window);
- }
- /* A backend implementation may fail to get a display for the window
- * (for example if the window is off-screen), but other code may expect it
- * to succeed in that situation, so we fall back to a generic position-
- * based implementation in that case. */
- SDL_RelativeToGlobalForWindow(window, window->x, window->y, &x, &y);
- if (!displayID) {
- /* Fullscreen windows may be larger than the display if they were moved between differently sized
- * displays and the new position was received before the new size or vice versa. Using the center
- * of the window rect in this case can report the wrong display, so use the origin.
- */
- if (window->flags & SDL_WINDOW_FULLSCREEN) {
- displayID = GetDisplayForRect(x, y, 1, 1);
- } else {
- displayID = GetDisplayForRect(x, y, window->w, window->h);
- }
- }
- if (!displayID) {
- /* Use the primary display for a window if we can't find it anywhere else */
- displayID = SDL_GetPrimaryDisplay();
- }
- return displayID;
- }
- SDL_VideoDisplay *SDL_GetVideoDisplayForFullscreenWindow(SDL_Window *window)
- {
- SDL_DisplayID displayID = 0;
- CHECK_WINDOW_MAGIC(window, 0);
- /* An explicit fullscreen display overrides all */
- if (window->current_fullscreen_mode.displayID) {
- displayID = window->current_fullscreen_mode.displayID;
- }
- /* The floating position is used here as a very common pattern is
- * SDL_SetWindowPosition() followed by SDL_SetWindowFullscreen() to make the
- * window fullscreen desktop on a specific display. If the backend doesn't
- * support changing the window position, or the compositor hasn't yet actually
- * moved the window, the current position won't be updated at the time of the
- * fullscreen call.
- */
- if (!displayID) {
- if (window->flags & SDL_WINDOW_FULLSCREEN && !window->is_repositioning) {
- /* This was a window manager initiated move, use the current position. */
- displayID = GetDisplayForRect(window->x, window->y, 1, 1);
- } else {
- displayID = GetDisplayForRect(window->floating.x, window->floating.y, window->floating.w, window->floating.h);
- }
- }
- if (!displayID) {
- /* Use the primary display for a window if we can't find it anywhere else */
- displayID = SDL_GetPrimaryDisplay();
- }
- return SDL_GetVideoDisplay(displayID);
- }
- SDL_DisplayID SDL_GetDisplayForWindow(SDL_Window *window)
- {
- SDL_DisplayID displayID = 0;
- CHECK_WINDOW_MAGIC(window, 0);
- /* An explicit fullscreen display overrides all */
- if (window->flags & SDL_WINDOW_FULLSCREEN) {
- displayID = window->current_fullscreen_mode.displayID;
- }
- if (!displayID) {
- displayID = SDL_GetDisplayForWindowPosition(window);
- }
- return displayID;
- }
- static void SDL_CheckWindowDisplayChanged(SDL_Window *window)
- {
- if (SDL_SendsDisplayChanges(_this)) {
- return;
- }
- SDL_DisplayID displayID = SDL_GetDisplayForWindowPosition(window);
- if (displayID != window->last_displayID) {
- int i, display_index;
- /* Sanity check our fullscreen windows */
- display_index = SDL_GetDisplayIndex(displayID);
- for (i = 0; i < _this->num_displays; ++i) {
- SDL_VideoDisplay *display = _this->displays[i];
- if (display->fullscreen_window == window) {
- if (display_index != i) {
- if (display_index < 0) {
- display_index = i;
- } else {
- SDL_VideoDisplay *new_display = _this->displays[display_index];
- /* The window was moved to a different display */
- if (new_display->fullscreen_window &&
- new_display->fullscreen_window != window) {
- /* Uh oh, there's already a fullscreen window here; minimize it */
- SDL_MinimizeWindow(new_display->fullscreen_window);
- }
- new_display->fullscreen_window = window;
- display->fullscreen_window = NULL;
- }
- }
- break;
- }
- }
- SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_DISPLAY_CHANGED, (int)displayID, 0);
- }
- }
- float SDL_GetWindowPixelDensity(SDL_Window *window)
- {
- int window_w, window_h, pixel_w, pixel_h;
- float pixel_density = 1.0f;
- CHECK_WINDOW_MAGIC(window, 0.0f);
- if (SDL_GetWindowSize(window, &window_w, &window_h) == 0 &&
- SDL_GetWindowSizeInPixels(window, &pixel_w, &pixel_h) == 0) {
- pixel_density = (float)pixel_w / window_w;
- }
- return pixel_density;
- }
- float SDL_GetWindowDisplayScale(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, 0.0f);
- return window->display_scale;
- }
- static void SDL_CheckWindowDisplayScaleChanged(SDL_Window *window)
- {
- float pixel_density = SDL_GetWindowPixelDensity(window);
- float content_scale = SDL_GetDisplayContentScale(SDL_GetDisplayForWindowPosition(window));
- float display_scale;
- display_scale = (pixel_density * content_scale);
- if (display_scale != window->display_scale) {
- window->display_scale = display_scale;
- SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED, 0, 0);
- }
- }
- #ifdef SDL_PLATFORM_WINRT
- extern SDL_WindowFlags WINRT_DetectWindowFlags(SDL_Window *window);
- #endif
- static void SDL_RestoreMousePosition(SDL_Window *window)
- {
- float x, y;
- if (window == SDL_GetMouseFocus()) {
- SDL_GetMouseState(&x, &y);
- SDL_WarpMouseInWindow(window, x, y);
- }
- }
- int SDL_UpdateFullscreenMode(SDL_Window *window, SDL_FullscreenOp fullscreen, SDL_bool commit)
- {
- SDL_VideoDisplay *display = NULL;
- SDL_DisplayMode *mode = NULL;
- int i;
- CHECK_WINDOW_MAGIC(window, -1);
- window->fullscreen_exclusive = SDL_FALSE;
- /* If we are in the process of hiding don't go back to fullscreen */
- if (window->is_destroying || window->is_hiding) {
- fullscreen = SDL_FALSE;
- }
- /* Get the correct display for this operation */
- if (fullscreen) {
- display = SDL_GetVideoDisplayForFullscreenWindow(window);
- if (!display) {
- /* This should never happen, but it did... */
- goto done;
- }
- } else {
- for (i = 0; i < _this->num_displays; ++i) {
- display = _this->displays[i];
- if (display->fullscreen_window == window) {
- break;
- }
- }
- if (!display || i == _this->num_displays) {
- /* Already not fullscreen on any display */
- display = NULL;
- }
- }
- if (fullscreen) {
- mode = (SDL_DisplayMode *)SDL_GetWindowFullscreenMode(window);
- if (mode) {
- window->fullscreen_exclusive = SDL_TRUE;
- } else {
- /* Make sure the current mode is zeroed for fullscreen desktop. */
- SDL_zero(window->current_fullscreen_mode);
- }
- }
- #if defined(SDL_PLATFORM_MACOS) && defined(SDL_VIDEO_DRIVER_COCOA)
- /* if the window is going away and no resolution change is necessary,
- do nothing, or else we may trigger an ugly double-transition
- */
- if (SDL_strcmp(_this->name, "cocoa") == 0) { /* don't do this for X11, etc */
- if (window->is_destroying && !window->last_fullscreen_exclusive_display) {
- window->fullscreen_exclusive = SDL_FALSE;
- if (display) {
- display->fullscreen_window = NULL;
- }
- goto done;
- }
- if (commit) {
- /* If we're switching between a fullscreen Space and exclusive fullscreen, we need to get back to normal first. */
- if (fullscreen && Cocoa_IsWindowInFullscreenSpace(window) && !window->last_fullscreen_exclusive_display && window->fullscreen_exclusive) {
- if (!Cocoa_SetWindowFullscreenSpace(window, SDL_FALSE, SDL_TRUE)) {
- goto error;
- }
- } else if (fullscreen && window->last_fullscreen_exclusive_display && !window->fullscreen_exclusive) {
- for (i = 0; i < _this->num_displays; ++i) {
- SDL_VideoDisplay *last_display = _this->displays[i];
- if (last_display->fullscreen_window == window) {
- SDL_SetDisplayModeForDisplay(last_display, NULL);
- if (_this->SetWindowFullscreen) {
- _this->SetWindowFullscreen(_this, window, last_display, SDL_FALSE);
- }
- last_display->fullscreen_window = NULL;
- }
- }
- }
- if (Cocoa_SetWindowFullscreenSpace(window, !!fullscreen, syncHint)) {
- goto done;
- }
- }
- }
- #elif defined(SDL_PLATFORM_WINRT) && (NTDDI_VERSION < NTDDI_WIN10)
- /* HACK: WinRT 8.x apps can't choose whether or not they are fullscreen
- or not. The user can choose this, via OS-provided UI, but this can't
- be set programmatically.
- Just look at what SDL's WinRT video backend code detected with regards
- to fullscreen (being active, or not), and figure out a return/error code
- from that.
- */
- if (!!fullscreen == !(WINRT_DetectWindowFlags(window) & SDL_WINDOW_FULLSCREEN)) {
- /* Uh oh, either:
- 1. fullscreen was requested, and we're already windowed
- 2. windowed-mode was requested, and we're already fullscreen
- WinRT 8.x can't resolve either programmatically, so we're
- giving up.
- */
- goto error;
- } else {
- /* Whatever was requested, fullscreen or windowed mode, is already
- in-place.
- */
- if (fullscreen) {
- display->fullscreen_window = window;
- } else {
- display->fullscreen_window = NULL;
- }
- goto done;
- }
- #endif
- if (display) {
- /* Restore the video mode on other displays if needed */
- for (i = 0; i < _this->num_displays; ++i) {
- SDL_VideoDisplay *other = _this->displays[i];
- if (other != display && other->fullscreen_window == window) {
- SDL_SetDisplayModeForDisplay(other, NULL);
- other->fullscreen_window = NULL;
- }
- }
- }
- if (fullscreen) {
- int mode_w = 0, mode_h = 0;
- SDL_bool resized = SDL_FALSE;
- /* Hide any other fullscreen window on this display */
- if (display->fullscreen_window &&
- display->fullscreen_window != window) {
- SDL_MinimizeWindow(display->fullscreen_window);
- }
- if (SDL_SetDisplayModeForDisplay(display, mode) < 0) {
- goto error;
- }
- if (commit) {
- int ret = 0;
- if (_this->SetWindowFullscreen) {
- ret = _this->SetWindowFullscreen(_this, window, display, fullscreen);
- } else {
- resized = SDL_TRUE;
- }
- if (ret == 0) {
- /* Window is fullscreen immediately upon return. If the driver hasn't already sent the event, do so now. */
- if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
- SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_ENTER_FULLSCREEN, 0, 0);
- }
- } else if (ret < 0) {
- goto error;
- }
- }
- if (window->flags & SDL_WINDOW_FULLSCREEN) {
- display->fullscreen_window = window;
- /* Android may not resize the window to exactly what our fullscreen mode is,
- * especially on windowed Android environments like the Chromebook or Samsung DeX.
- * Given this, we shouldn't use the mode size. Android's SetWindowFullscreen
- * will generate the window event for us with the proper final size.
- *
- * This is also unnecessary on Cocoa, Wayland, Win32, and X11 (will send SDL_EVENT_WINDOW_RESIZED).
- */
- if (!SDL_SendsFullscreenDimensions(_this)) {
- if (mode) {
- mode_w = mode->w;
- mode_h = mode->h;
- } else {
- mode_w = display->desktop_mode.w;
- mode_h = display->desktop_mode.h;
- }
- if (window->w != mode_w || window->h != mode_h) {
- resized = SDL_TRUE;
- }
- if (resized) {
- SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, mode_w, mode_h);
- } else {
- SDL_OnWindowResized(window);
- }
- }
- /* Restore the cursor position */
- if (!SDL_DisableMouseWarpOnFullscreenTransitions(_this)) {
- SDL_RestoreMousePosition(window);
- }
- }
- } else {
- SDL_bool resized = SDL_FALSE;
- /* Restore the desktop mode */
- if (display) {
- SDL_SetDisplayModeForDisplay(display, NULL);
- }
- if (commit) {
- int ret = 0;
- if (_this->SetWindowFullscreen) {
- SDL_VideoDisplay *full_screen_display = display ? display : SDL_GetVideoDisplayForFullscreenWindow(window);
- if (full_screen_display) {
- ret = _this->SetWindowFullscreen(_this, window, full_screen_display, SDL_FULLSCREEN_OP_LEAVE);
- }
- } else {
- resized = SDL_TRUE;
- }
- if (ret == 0) {
- /* Window left fullscreen immediately upon return. If the driver hasn't already sent the event, do so now. */
- if (window->flags & SDL_WINDOW_FULLSCREEN) {
- SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_LEAVE_FULLSCREEN, 0, 0);
- }
- } else if (ret < 0) {
- goto error;
- }
- }
- if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
- if (display) {
- display->fullscreen_window = NULL;
- }
- if (!SDL_SendsFullscreenDimensions(_this)) {
- if (resized) {
- SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, window->windowed.w, window->windowed.h);
- } else {
- SDL_OnWindowResized(window);
- }
- }
- /* Restore the cursor position if we've exited fullscreen on a display */
- if (display && !SDL_DisableMouseWarpOnFullscreenTransitions(_this)) {
- SDL_RestoreMousePosition(window);
- }
- }
- }
- done:
- window->last_fullscreen_exclusive_display = display && (window->flags & SDL_WINDOW_FULLSCREEN) && window->fullscreen_exclusive ? display->id : 0;
- return 0;
- error:
- if (fullscreen) {
- /* Something went wrong and the window is no longer fullscreen. */
- SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_LEAVE, commit);
- }
- return -1;
- }
- int SDL_SetWindowFullscreenMode(SDL_Window *window, const SDL_DisplayMode *mode)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- CHECK_WINDOW_NOT_POPUP(window, -1);
- if (mode) {
- if (!SDL_GetFullscreenModeMatch(mode)) {
- return SDL_SetError("Invalid fullscreen display mode");
- }
- /* Save the mode so we can look up the closest match later */
- SDL_copyp(&window->requested_fullscreen_mode, mode);
- } else {
- SDL_zero(window->requested_fullscreen_mode);
- }
- /* Copy to the current mode now, in case an asynchronous fullscreen window request
- * is in progress. It will be overwritten if a new request is made.
- */
- SDL_copyp(&window->current_fullscreen_mode, &window->requested_fullscreen_mode);
- if (SDL_WINDOW_FULLSCREEN_VISIBLE(window)) {
- SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_UPDATE, SDL_TRUE);
- SDL_SyncIfRequired(window);
- }
- return 0;
- }
- const SDL_DisplayMode *SDL_GetWindowFullscreenMode(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, NULL);
- CHECK_WINDOW_NOT_POPUP(window, NULL);
- if (window->flags & SDL_WINDOW_FULLSCREEN) {
- return SDL_GetFullscreenModeMatch(&window->current_fullscreen_mode);
- } else {
- return SDL_GetFullscreenModeMatch(&window->requested_fullscreen_mode);
- }
- }
- void *SDL_GetWindowICCProfile(SDL_Window *window, size_t *size)
- {
- if (!_this->GetWindowICCProfile) {
- SDL_Unsupported();
- return NULL;
- }
- return _this->GetWindowICCProfile(_this, window, size);
- }
- Uint32 SDL_GetWindowPixelFormat(SDL_Window *window)
- {
- SDL_DisplayID displayID;
- const SDL_DisplayMode *mode;
- CHECK_WINDOW_MAGIC(window, SDL_PIXELFORMAT_UNKNOWN);
- displayID = SDL_GetDisplayForWindow(window);
- mode = SDL_GetCurrentDisplayMode(displayID);
- if (mode) {
- return mode->format;
- } else {
- return SDL_PIXELFORMAT_UNKNOWN;
- }
- }
- #define CREATE_FLAGS \
- (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)
- static SDL_INLINE SDL_bool IsAcceptingDragAndDrop(void)
- {
- if (SDL_EventEnabled(SDL_EVENT_DROP_FILE) || SDL_EventEnabled(SDL_EVENT_DROP_TEXT)) {
- return SDL_TRUE;
- }
- return SDL_FALSE;
- }
- /* prepare a newly-created window */
- static SDL_INLINE void PrepareDragAndDropSupport(SDL_Window *window)
- {
- if (_this->AcceptDragAndDrop) {
- _this->AcceptDragAndDrop(window, IsAcceptingDragAndDrop());
- }
- }
- /* toggle d'n'd for all existing windows. */
- void SDL_ToggleDragAndDropSupport(void)
- {
- if (_this && _this->AcceptDragAndDrop) {
- const SDL_bool enable = IsAcceptingDragAndDrop();
- SDL_Window *window;
- for (window = _this->windows; window; window = window->next) {
- _this->AcceptDragAndDrop(window, enable);
- }
- }
- }
- SDL_Window **SDLCALL SDL_GetWindows(int *count)
- {
- if (count) {
- *count = 0;
- }
- if (!_this) {
- SDL_UninitializedVideo();
- return NULL;
- }
- SDL_Window *window;
- int num_added = 0;
- int num_windows = 0;
- for (window = _this->windows; window; window = window->next) {
- ++num_windows;
- }
- SDL_Window **windows = SDL_malloc((num_windows + 1) * sizeof(*windows));
- if (!windows) {
- return NULL;
- }
- for (window = _this->windows; window; window = window->next) {
- windows[num_added++] = window;
- if (num_added == num_windows) {
- // Race condition? Multi-threading not supported, ignore it
- break;
- }
- }
- windows[num_added] = NULL;
- if (count) {
- *count = num_added;
- }
- return windows;
- }
- static void ApplyWindowFlags(SDL_Window *window, SDL_WindowFlags flags)
- {
- if (!SDL_WINDOW_IS_POPUP(window)) {
- if (!(flags & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED))) {
- SDL_RestoreWindow(window);
- }
- if (flags & SDL_WINDOW_MAXIMIZED) {
- SDL_MaximizeWindow(window);
- }
- SDL_SetWindowFullscreen(window, (flags & SDL_WINDOW_FULLSCREEN) != 0);
- if (flags & SDL_WINDOW_MINIMIZED) {
- SDL_MinimizeWindow(window);
- }
- if (flags & SDL_WINDOW_MOUSE_GRABBED) {
- SDL_SetWindowMouseGrab(window, SDL_TRUE);
- }
- if (flags & SDL_WINDOW_KEYBOARD_GRABBED) {
- SDL_SetWindowKeyboardGrab(window, SDL_TRUE);
- }
- }
- }
- static void SDL_FinishWindowCreation(SDL_Window *window, SDL_WindowFlags flags)
- {
- PrepareDragAndDropSupport(window);
- ApplyWindowFlags(window, flags);
- if (!(flags & SDL_WINDOW_HIDDEN)) {
- SDL_ShowWindow(window);
- }
- }
- static int SDL_ContextNotSupported(const char *name)
- {
- return SDL_SetError("%s support is either not configured in SDL "
- "or not available in current SDL video driver "
- "(%s) or platform",
- name,
- _this->name);
- }
- static int SDL_DllNotSupported(const char *name)
- {
- return SDL_SetError("No dynamic %s support in current SDL video driver (%s)", name, _this->name);
- }
- static struct {
- const char *property_name;
- SDL_WindowFlags flag;
- SDL_bool invert_value;
- } SDL_WindowFlagProperties[] = {
- { SDL_PROP_WINDOW_CREATE_ALWAYS_ON_TOP_BOOLEAN, SDL_WINDOW_ALWAYS_ON_TOP, SDL_FALSE },
- { SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN, SDL_WINDOW_BORDERLESS, SDL_FALSE },
- { SDL_PROP_WINDOW_CREATE_FOCUSABLE_BOOLEAN, SDL_WINDOW_NOT_FOCUSABLE, SDL_TRUE },
- { SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN, SDL_WINDOW_FULLSCREEN, SDL_FALSE },
- { SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN, SDL_WINDOW_HIDDEN, SDL_FALSE },
- { SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, SDL_WINDOW_HIGH_PIXEL_DENSITY, SDL_FALSE },
- { SDL_PROP_WINDOW_CREATE_MAXIMIZED_BOOLEAN, SDL_WINDOW_MAXIMIZED, SDL_FALSE },
- { SDL_PROP_WINDOW_CREATE_MENU_BOOLEAN, SDL_WINDOW_POPUP_MENU, SDL_FALSE },
- { SDL_PROP_WINDOW_CREATE_METAL_BOOLEAN, SDL_WINDOW_METAL, SDL_FALSE },
- { SDL_PROP_WINDOW_CREATE_MINIMIZED_BOOLEAN, SDL_WINDOW_MINIMIZED, SDL_FALSE },
- { SDL_PROP_WINDOW_CREATE_MODAL_BOOLEAN, SDL_WINDOW_MODAL, SDL_FALSE },
- { SDL_PROP_WINDOW_CREATE_MOUSE_GRABBED_BOOLEAN, SDL_WINDOW_MOUSE_GRABBED, SDL_FALSE },
- { SDL_PROP_WINDOW_CREATE_OPENGL_BOOLEAN, SDL_WINDOW_OPENGL, SDL_FALSE },
- { SDL_PROP_WINDOW_CREATE_RESIZABLE_BOOLEAN, SDL_WINDOW_RESIZABLE, SDL_FALSE },
- { SDL_PROP_WINDOW_CREATE_TRANSPARENT_BOOLEAN, SDL_WINDOW_TRANSPARENT, SDL_FALSE },
- { SDL_PROP_WINDOW_CREATE_TOOLTIP_BOOLEAN, SDL_WINDOW_TOOLTIP, SDL_FALSE },
- { SDL_PROP_WINDOW_CREATE_UTILITY_BOOLEAN, SDL_WINDOW_UTILITY, SDL_FALSE },
- { SDL_PROP_WINDOW_CREATE_VULKAN_BOOLEAN, SDL_WINDOW_VULKAN, SDL_FALSE }
- };
- static SDL_WindowFlags SDL_GetWindowFlagProperties(SDL_PropertiesID props)
- {
- unsigned i;
- SDL_WindowFlags flags = (SDL_WindowFlags)SDL_GetNumberProperty(props, "flags", 0);
- for (i = 0; i < SDL_arraysize(SDL_WindowFlagProperties); ++i) {
- if (SDL_WindowFlagProperties[i].invert_value) {
- if (!SDL_GetBooleanProperty(props, SDL_WindowFlagProperties[i].property_name, SDL_TRUE)) {
- flags |= SDL_WindowFlagProperties[i].flag;
- }
- } else {
- if (SDL_GetBooleanProperty(props, SDL_WindowFlagProperties[i].property_name, SDL_FALSE)) {
- flags |= SDL_WindowFlagProperties[i].flag;
- }
- }
- }
- return flags;
- }
- SDL_Window *SDL_CreateWindowWithProperties(SDL_PropertiesID props)
- {
- SDL_Window *window;
- const char *title = SDL_GetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, NULL);
- int x = (int)SDL_GetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, SDL_WINDOWPOS_UNDEFINED);
- int y = (int)SDL_GetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, SDL_WINDOWPOS_UNDEFINED);
- int w = (int)SDL_GetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, 0);
- int h = (int)SDL_GetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, 0);
- SDL_Window *parent = (SDL_Window *)SDL_GetPointerProperty(props, SDL_PROP_WINDOW_CREATE_PARENT_POINTER, NULL);
- SDL_WindowFlags flags = SDL_GetWindowFlagProperties(props);
- SDL_WindowFlags type_flags, graphics_flags;
- SDL_bool undefined_x = SDL_FALSE;
- SDL_bool undefined_y = SDL_FALSE;
- SDL_bool external_graphics_context = SDL_GetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_EXTERNAL_GRAPHICS_CONTEXT_BOOLEAN, SDL_FALSE);
- if (!_this) {
- /* Initialize the video system if needed */
- if (SDL_Init(SDL_INIT_VIDEO) < 0) {
- return NULL;
- }
- /* Make clang-tidy happy */
- if (!_this) {
- return NULL;
- }
- }
- if ((flags & SDL_WINDOW_MODAL) && !SDL_ObjectValid(parent, SDL_OBJECT_TYPE_WINDOW)) {
- SDL_SetError("Modal windows must specify a parent window");
- return NULL;
- }
- if ((flags & (SDL_WINDOW_TOOLTIP | SDL_WINDOW_POPUP_MENU)) != 0) {
- if (!(_this->device_caps & VIDEO_DEVICE_CAPS_HAS_POPUP_WINDOW_SUPPORT)) {
- SDL_Unsupported();
- return NULL;
- }
- /* Tooltip and popup menu window must specify a parent window */
- if (!SDL_ObjectValid(parent, SDL_OBJECT_TYPE_WINDOW)) {
- SDL_SetError("Tooltip and popup menu windows must specify a parent window");
- return NULL;
- }
- /* Remove invalid flags */
- flags &= ~(SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS);
- }
- /* Ensure no more than one of these flags is set */
- type_flags = flags & (SDL_WINDOW_UTILITY | SDL_WINDOW_TOOLTIP | SDL_WINDOW_POPUP_MENU | SDL_WINDOW_MODAL);
- if (type_flags & (type_flags - 1)) {
- SDL_SetError("Conflicting window type flags specified: 0x%.8x", (unsigned int)type_flags);
- return NULL;
- }
- /* Make sure the display list is up to date for window placement */
- if (_this->RefreshDisplays) {
- _this->RefreshDisplays(_this);
- }
- /* Some platforms can't create zero-sized windows */
- if (w < 1) {
- w = 1;
- }
- if (h < 1) {
- h = 1;
- }
- if (SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISUNDEFINED(y) ||
- SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
- SDL_DisplayID displayID = 0;
- SDL_Rect bounds;
- if ((SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISCENTERED(x)) && (x & 0xFFFF)) {
- displayID = (x & 0xFFFF);
- } else if ((SDL_WINDOWPOS_ISUNDEFINED(y) || SDL_WINDOWPOS_ISCENTERED(y)) && (y & 0xFFFF)) {
- displayID = (y & 0xFFFF);
- }
- if (displayID == 0 || SDL_GetDisplayIndex(displayID) < 0) {
- displayID = SDL_GetPrimaryDisplay();
- }
- SDL_zero(bounds);
- SDL_GetDisplayUsableBounds(displayID, &bounds);
- if (w > bounds.w || h > bounds.h) {
- /* This window is larger than the usable bounds, just center on the display */
- SDL_GetDisplayUsableBounds(displayID, &bounds);
- }
- if (SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISUNDEFINED(x)) {
- if (SDL_WINDOWPOS_ISUNDEFINED(x)) {
- undefined_x = SDL_TRUE;
- }
- x = bounds.x + (bounds.w - w) / 2;
- }
- if (SDL_WINDOWPOS_ISCENTERED(y) || SDL_WINDOWPOS_ISUNDEFINED(y)) {
- if (SDL_WINDOWPOS_ISUNDEFINED(y)) {
- undefined_y = SDL_TRUE;
- }
- y = bounds.y + (bounds.h - h) / 2;
- }
- }
- /* ensure no more than one of these flags is set */
- graphics_flags = flags & (SDL_WINDOW_OPENGL | SDL_WINDOW_METAL | SDL_WINDOW_VULKAN);
- if (graphics_flags & (graphics_flags - 1)) {
- SDL_SetError("Conflicting window graphics flags specified: 0x%.8x", (unsigned int)graphics_flags);
- return NULL;
- }
- /* Some platforms have certain graphics backends enabled by default */
- if (!graphics_flags && !external_graphics_context) {
- flags |= SDL_DefaultGraphicsBackends(_this);
- }
- if (flags & SDL_WINDOW_OPENGL) {
- if (!_this->GL_CreateContext) {
- SDL_ContextNotSupported("OpenGL");
- return NULL;
- }
- if (SDL_GL_LoadLibrary(NULL) < 0) {
- return NULL;
- }
- }
- if (flags & SDL_WINDOW_VULKAN) {
- if (!_this->Vulkan_CreateSurface) {
- SDL_ContextNotSupported("Vulkan");
- return NULL;
- }
- if (SDL_Vulkan_LoadLibrary(NULL) < 0) {
- return NULL;
- }
- }
- if (flags & SDL_WINDOW_METAL) {
- if (!_this->Metal_CreateView) {
- SDL_ContextNotSupported("Metal");
- return NULL;
- }
- }
- window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
- if (!window) {
- return NULL;
- }
- SDL_SetObjectValid(window, SDL_OBJECT_TYPE_WINDOW, SDL_TRUE);
- window->id = SDL_GetNextObjectID();
- window->floating.x = window->windowed.x = window->x = x;
- window->floating.y = window->windowed.y = window->y = y;
- window->floating.w = window->windowed.w = window->w = w;
- window->floating.h = window->windowed.h = window->h = h;
- window->undefined_x = undefined_x;
- window->undefined_y = undefined_y;
- SDL_VideoDisplay *display = SDL_GetVideoDisplayForWindow(window);
- SDL_SetWindowHDRProperties(window, &display->HDR, SDL_FALSE);
- if (flags & SDL_WINDOW_FULLSCREEN || IsFullscreenOnly(_this)) {
- SDL_Rect bounds;
- SDL_GetDisplayBounds(display->id, &bounds);
- window->x = bounds.x;
- window->y = bounds.y;
- window->w = bounds.w;
- window->h = bounds.h;
- window->pending_flags |= SDL_WINDOW_FULLSCREEN;
- flags |= SDL_WINDOW_FULLSCREEN;
- }
- window->flags = ((flags & CREATE_FLAGS) | SDL_WINDOW_HIDDEN);
- window->display_scale = 1.0f;
- window->opacity = 1.0f;
- window->next = _this->windows;
- window->is_destroying = SDL_FALSE;
- window->last_displayID = SDL_GetDisplayForWindow(window);
- window->external_graphics_context = external_graphics_context;
- if (_this->windows) {
- _this->windows->prev = window;
- }
- _this->windows = window;
- /* Set the parent before creation if this is non-modal, otherwise it will be set later. */
- if (!(flags & SDL_WINDOW_MODAL)) {
- SDL_SetWindowParent(window, parent);
- }
- if (_this->CreateSDLWindow && _this->CreateSDLWindow(_this, window, props) < 0) {
- SDL_DestroyWindow(window);
- return NULL;
- }
- /* Clear minimized if not on windows, only windows handles it at create rather than FinishWindowCreation,
- * but it's important or window focus will get broken on windows!
- */
- #if !defined(SDL_PLATFORM_WIN32) && !defined(SDL_PLATFORM_GDK)
- if (window->flags & SDL_WINDOW_MINIMIZED) {
- window->flags &= ~SDL_WINDOW_MINIMIZED;
- }
- #endif
- #if defined(SDL_PLATFORM_WINRT) && (NTDDI_VERSION < NTDDI_WIN10)
- /* HACK: WinRT 8.x apps can't choose whether or not they are fullscreen
- or not. The user can choose this, via OS-provided UI, but this can't
- be set programmatically.
- Just look at what SDL's WinRT video backend code detected with regards
- to fullscreen (being active, or not), and figure out a return/error code
- from that.
- */
- flags = window->flags;
- #endif
- if (flags & SDL_WINDOW_MODAL) {
- SDL_SetWindowModalFor(window, parent);
- }
- if (title) {
- SDL_SetWindowTitle(window, title);
- }
- SDL_FinishWindowCreation(window, flags);
- /* Make sure window pixel size is up to date */
- SDL_CheckWindowPixelSizeChanged(window);
- return window;
- }
- SDL_Window *SDL_CreateWindow(const char *title, int w, int h, SDL_WindowFlags flags)
- {
- SDL_Window *window;
- SDL_PropertiesID props = SDL_CreateProperties();
- if (title && *title) {
- SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, title);
- }
- SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, w);
- SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, h);
- SDL_SetNumberProperty(props, "flags", flags);
- window = SDL_CreateWindowWithProperties(props);
- SDL_DestroyProperties(props);
- return window;
- }
- SDL_Window *SDL_CreatePopupWindow(SDL_Window *parent, int offset_x, int offset_y, int w, int h, SDL_WindowFlags flags)
- {
- SDL_Window *window;
- SDL_PropertiesID props = SDL_CreateProperties();
- /* Popups must specify either the tooltip or popup menu window flags */
- if (!(flags & (SDL_WINDOW_TOOLTIP | SDL_WINDOW_POPUP_MENU))) {
- SDL_SetError("Popup windows must specify either the 'SDL_WINDOW_TOOLTIP' or the 'SDL_WINDOW_POPUP_MENU' flag");
- return NULL;
- }
- SDL_SetPointerProperty(props, SDL_PROP_WINDOW_CREATE_PARENT_POINTER, parent);
- SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, offset_x);
- SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, offset_y);
- SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, w);
- SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, h);
- SDL_SetNumberProperty(props, "flags", flags);
- window = SDL_CreateWindowWithProperties(props);
- SDL_DestroyProperties(props);
- return window;
- }
- int SDL_RecreateWindow(SDL_Window *window, SDL_WindowFlags flags)
- {
- SDL_bool loaded_opengl = SDL_FALSE;
- SDL_bool need_gl_unload = SDL_FALSE;
- SDL_bool need_gl_load = SDL_FALSE;
- SDL_bool loaded_vulkan = SDL_FALSE;
- SDL_bool need_vulkan_unload = SDL_FALSE;
- SDL_bool need_vulkan_load = SDL_FALSE;
- SDL_WindowFlags graphics_flags;
- SDL_Window *parent = window->parent;
- /* ensure no more than one of these flags is set */
- graphics_flags = flags & (SDL_WINDOW_OPENGL | SDL_WINDOW_METAL | SDL_WINDOW_VULKAN);
- if (graphics_flags & (graphics_flags - 1)) {
- return SDL_SetError("Conflicting window flags specified");
- }
- if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
- return SDL_ContextNotSupported("OpenGL");
- }
- if ((flags & SDL_WINDOW_VULKAN) && !_this->Vulkan_CreateSurface) {
- return SDL_ContextNotSupported("Vulkan");
- }
- if ((flags & SDL_WINDOW_METAL) && !_this->Metal_CreateView) {
- return SDL_ContextNotSupported("Metal");
- }
- if (window->flags & SDL_WINDOW_EXTERNAL) {
- /* Can't destroy and re-create external windows, hrm */
- flags |= SDL_WINDOW_EXTERNAL;
- } else {
- flags &= ~SDL_WINDOW_EXTERNAL;
- }
- /* If this is a modal dialog, clear the modal status. */
- if (window->flags & SDL_WINDOW_MODAL) {
- SDL_SetWindowModalFor(window, NULL);
- }
- /* Restore video mode, etc. */
- if (!(window->flags & SDL_WINDOW_EXTERNAL)) {
- const SDL_bool restore_on_show = window->restore_on_show;
- SDL_HideWindow(window);
- window->restore_on_show = restore_on_show;
- }
- /* Tear down the old native window */
- SDL_DestroyWindowSurface(window);
- if ((window->flags & SDL_WINDOW_OPENGL) != (flags & SDL_WINDOW_OPENGL)) {
- if (flags & SDL_WINDOW_OPENGL) {
- need_gl_load = SDL_TRUE;
- } else {
- need_gl_unload = SDL_TRUE;
- }
- } else if (window->flags & SDL_WINDOW_OPENGL) {
- need_gl_unload = SDL_TRUE;
- need_gl_load = SDL_TRUE;
- }
- if ((window->flags & SDL_WINDOW_VULKAN) != (flags & SDL_WINDOW_VULKAN)) {
- if (flags & SDL_WINDOW_VULKAN) {
- need_vulkan_load = SDL_TRUE;
- } else {
- need_vulkan_unload = SDL_TRUE;
- }
- } else if (window->flags & SDL_WINDOW_VULKAN) {
- need_vulkan_unload = SDL_TRUE;
- need_vulkan_load = SDL_TRUE;
- }
- if (need_gl_unload) {
- SDL_GL_UnloadLibrary();
- }
- if (need_vulkan_unload) {
- SDL_Vulkan_UnloadLibrary();
- }
- if (_this->DestroyWindow && !(flags & SDL_WINDOW_EXTERNAL)) {
- _this->DestroyWindow(_this, window);
- }
- if (need_gl_load) {
- if (SDL_GL_LoadLibrary(NULL) < 0) {
- return -1;
- }
- loaded_opengl = SDL_TRUE;
- }
- if (need_vulkan_load) {
- if (SDL_Vulkan_LoadLibrary(NULL) < 0) {
- return -1;
- }
- loaded_vulkan = SDL_TRUE;
- }
- window->flags = ((flags & CREATE_FLAGS) | SDL_WINDOW_HIDDEN);
- window->is_destroying = SDL_FALSE;
- if (_this->CreateSDLWindow && !(flags & SDL_WINDOW_EXTERNAL)) {
- /* Reset the window size to the original floating value, so the
- * recreated window has the proper base size.
- */
- window->x = window->windowed.x = window->floating.x;
- window->y = window->windowed.y = window->floating.y;
- window->w = window->windowed.w = window->floating.w;
- window->h = window->windowed.h = window->floating.h;
- if (_this->CreateSDLWindow(_this, window, 0) < 0) {
- if (loaded_opengl) {
- SDL_GL_UnloadLibrary();
- window->flags &= ~SDL_WINDOW_OPENGL;
- }
- if (loaded_vulkan) {
- SDL_Vulkan_UnloadLibrary();
- window->flags &= ~SDL_WINDOW_VULKAN;
- }
- return -1;
- }
- }
- if (flags & SDL_WINDOW_EXTERNAL) {
- window->flags |= SDL_WINDOW_EXTERNAL;
- }
- if (flags & SDL_WINDOW_MODAL) {
- SDL_SetWindowModalFor(window, parent);
- }
- if (_this->SetWindowTitle && window->title) {
- _this->SetWindowTitle(_this, window);
- }
- if (_this->SetWindowIcon && window->icon) {
- _this->SetWindowIcon(_this, window, window->icon);
- }
- if (_this->SetWindowMinimumSize && (window->min_w || window->min_h)) {
- _this->SetWindowMinimumSize(_this, window);
- }
- if (_this->SetWindowMaximumSize && (window->max_w || window->max_h)) {
- _this->SetWindowMaximumSize(_this, window);
- }
- if (_this->SetWindowAspectRatio && (window->min_aspect > 0.0f || window->max_aspect > 0.0f)) {
- _this->SetWindowAspectRatio(_this, window);
- }
- if (window->hit_test) {
- _this->SetWindowHitTest(window, SDL_TRUE);
- }
- SDL_FinishWindowCreation(window, flags);
- return 0;
- }
- SDL_bool SDL_HasWindows(void)
- {
- return _this && _this->windows;
- }
- SDL_WindowID SDL_GetWindowID(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, 0);
- return window->id;
- }
- SDL_Window *SDL_GetWindowFromID(SDL_WindowID id)
- {
- SDL_Window *window;
- if (!_this) {
- return NULL;
- }
- for (window = _this->windows; window; window = window->next) {
- if (window->id == id) {
- return window;
- }
- }
- return NULL;
- }
- SDL_Window *SDL_GetWindowParent(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, NULL);
- return window->parent;
- }
- SDL_PropertiesID SDL_GetWindowProperties(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, 0);
- if (window->props == 0) {
- window->props = SDL_CreateProperties();
- }
- return window->props;
- }
- SDL_WindowFlags SDL_GetWindowFlags(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, 0);
- return window->flags | window->pending_flags;
- }
- int SDL_SetWindowTitle(SDL_Window *window, const char *title)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- CHECK_WINDOW_NOT_POPUP(window, -1);
- if (title == window->title) {
- return 0;
- }
- SDL_free(window->title);
- window->title = SDL_strdup(title ? title : "");
- if (_this->SetWindowTitle) {
- _this->SetWindowTitle(_this, window);
- }
- return 0;
- }
- const char *SDL_GetWindowTitle(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, "");
- return window->title ? window->title : "";
- }
- int SDL_SetWindowIcon(SDL_Window *window, SDL_Surface *icon)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (!icon) {
- return SDL_InvalidParamError("icon");
- }
- SDL_DestroySurface(window->icon);
- /* Convert the icon into ARGB8888 */
- window->icon = SDL_ConvertSurface(icon, SDL_PIXELFORMAT_ARGB8888);
- if (!window->icon) {
- return -1;
- }
- if (!_this->SetWindowIcon) {
- return SDL_Unsupported();
- }
- return _this->SetWindowIcon(_this, window, window->icon);
- }
- int SDL_SetWindowPosition(SDL_Window *window, int x, int y)
- {
- SDL_DisplayID original_displayID;
- CHECK_WINDOW_MAGIC(window, -1);
- original_displayID = SDL_GetDisplayForWindow(window);
- if (SDL_WINDOWPOS_ISUNDEFINED(x)) {
- x = window->windowed.x;
- }
- if (SDL_WINDOWPOS_ISUNDEFINED(y)) {
- y = window->windowed.y;
- }
- if (SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
- SDL_DisplayID displayID = original_displayID;
- SDL_Rect bounds;
- if (SDL_WINDOWPOS_ISCENTERED(x) && (x & 0xFFFF)) {
- displayID = (x & 0xFFFF);
- } else if (SDL_WINDOWPOS_ISCENTERED(y) && (y & 0xFFFF)) {
- displayID = (y & 0xFFFF);
- }
- if (displayID == 0 || SDL_GetDisplayIndex(displayID) < 0) {
- displayID = SDL_GetPrimaryDisplay();
- }
- SDL_zero(bounds);
- if (SDL_GetDisplayUsableBounds(displayID, &bounds) < 0 ||
- window->windowed.w > bounds.w ||
- window->windowed.h > bounds.h) {
- if (SDL_GetDisplayBounds(displayID, &bounds) < 0) {
- return -1;
- }
- }
- if (SDL_WINDOWPOS_ISCENTERED(x)) {
- x = bounds.x + (bounds.w - window->windowed.w) / 2;
- }
- if (SDL_WINDOWPOS_ISCENTERED(y)) {
- y = bounds.y + (bounds.h - window->windowed.h) / 2;
- }
- }
- window->floating.x = x;
- window->floating.y = y;
- window->undefined_x = SDL_FALSE;
- window->undefined_y = SDL_FALSE;
- if (_this->SetWindowPosition) {
- window->is_repositioning = SDL_TRUE;
- const int ret = _this->SetWindowPosition(_this, window);
- window->is_repositioning = SDL_FALSE;
- if (!ret) {
- SDL_SyncIfRequired(window);
- }
- return ret;
- }
- return SDL_Unsupported();
- }
- int SDL_GetWindowPosition(SDL_Window *window, int *x, int *y)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- /* Fullscreen windows are always at their display's origin */
- if (window->flags & SDL_WINDOW_FULLSCREEN) {
- SDL_DisplayID displayID;
- if (x) {
- *x = 0;
- }
- if (y) {
- *y = 0;
- }
- /* Find the window's monitor and update to the
- monitor offset. */
- displayID = SDL_GetDisplayForWindow(window);
- if (displayID != 0) {
- SDL_Rect bounds;
- SDL_zero(bounds);
- SDL_GetDisplayBounds(displayID, &bounds);
- if (x) {
- *x = bounds.x;
- }
- if (y) {
- *y = bounds.y;
- }
- }
- } else {
- if (x) {
- *x = window->x;
- }
- if (y) {
- *y = window->y;
- }
- }
- return 0;
- }
- int SDL_SetWindowBordered(SDL_Window *window, SDL_bool bordered)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- CHECK_WINDOW_NOT_POPUP(window, -1);
- const SDL_bool want = (bordered != SDL_FALSE); /* normalize the flag. */
- const SDL_bool have = !(window->flags & SDL_WINDOW_BORDERLESS);
- if ((want != have) && (_this->SetWindowBordered)) {
- if (want) {
- window->flags &= ~SDL_WINDOW_BORDERLESS;
- } else {
- window->flags |= SDL_WINDOW_BORDERLESS;
- }
- _this->SetWindowBordered(_this, window, want);
- }
- return 0;
- }
- int SDL_SetWindowResizable(SDL_Window *window, SDL_bool resizable)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- CHECK_WINDOW_NOT_POPUP(window, -1);
- const SDL_bool want = (resizable != SDL_FALSE); /* normalize the flag. */
- const SDL_bool have = ((window->flags & SDL_WINDOW_RESIZABLE) != 0);
- if ((want != have) && (_this->SetWindowResizable)) {
- if (want) {
- window->flags |= SDL_WINDOW_RESIZABLE;
- } else {
- window->flags &= ~SDL_WINDOW_RESIZABLE;
- SDL_copyp(&window->windowed, &window->floating);
- }
- _this->SetWindowResizable(_this, window, want);
- }
- return 0;
- }
- int SDL_SetWindowAlwaysOnTop(SDL_Window *window, SDL_bool on_top)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- CHECK_WINDOW_NOT_POPUP(window, -1);
- const SDL_bool want = (on_top != SDL_FALSE); /* normalize the flag. */
- const SDL_bool have = ((window->flags & SDL_WINDOW_ALWAYS_ON_TOP) != 0);
- if ((want != have) && (_this->SetWindowAlwaysOnTop)) {
- if (want) {
- window->flags |= SDL_WINDOW_ALWAYS_ON_TOP;
- } else {
- window->flags &= ~SDL_WINDOW_ALWAYS_ON_TOP;
- }
- _this->SetWindowAlwaysOnTop(_this, window, want);
- }
- return 0;
- }
- int SDL_SetWindowSize(SDL_Window *window, int w, int h)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (w <= 0) {
- return SDL_InvalidParamError("w");
- }
- if (h <= 0) {
- return SDL_InvalidParamError("h");
- }
- /* It is possible for the aspect ratio contraints to not satisfy the size constraints. */
- /* The size constraints will override the aspect ratio contraints so we will apply the */
- /* the aspect ratio constraints first */
- float new_aspect = w / (float)h;
- if (window->max_aspect > 0.0f && new_aspect > window->max_aspect) {
- w = (int)SDL_roundf(h * window->max_aspect);
- } else if (window->min_aspect > 0.0f && new_aspect < window->min_aspect) {
- h = (int)SDL_roundf(w / window->min_aspect);
- }
- /* Make sure we don't exceed any window size limits */
- if (window->min_w && w < window->min_w) {
- w = window->min_w;
- }
- if (window->max_w && w > window->max_w) {
- w = window->max_w;
- }
- if (window->min_h && h < window->min_h) {
- h = window->min_h;
- }
- if (window->max_h && h > window->max_h) {
- h = window->max_h;
- }
- window->floating.w = w;
- window->floating.h = h;
- if (_this->SetWindowSize) {
- _this->SetWindowSize(_this, window);
- SDL_SyncIfRequired(window);
- } else {
- return SDL_Unsupported();
- }
- return 0;
- }
- int SDL_GetWindowSize(SDL_Window *window, int *w, int *h)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (w) {
- *w = window->w;
- }
- if (h) {
- *h = window->h;
- }
- return 0;
- }
- int SDL_SetWindowAspectRatio(SDL_Window *window, float min_aspect, float max_aspect)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- window->min_aspect = min_aspect;
- window->max_aspect = max_aspect;
- if (_this->SetWindowAspectRatio) {
- _this->SetWindowAspectRatio(_this, window);
- }
- return SDL_SetWindowSize(window, window->floating.w, window->floating.h);
- }
- int SDL_GetWindowAspectRatio(SDL_Window *window, float *min_aspect, float *max_aspect)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (min_aspect) {
- *min_aspect = window->min_aspect;
- }
- if (max_aspect) {
- *max_aspect = window->max_aspect;
- }
- return 0;
- }
- int SDL_GetWindowBordersSize(SDL_Window *window, int *top, int *left, int *bottom, int *right)
- {
- int dummy = 0;
- if (!top) {
- top = &dummy;
- }
- if (!left) {
- left = &dummy;
- }
- if (!right) {
- right = &dummy;
- }
- if (!bottom) {
- bottom = &dummy;
- }
- /* Always initialize, so applications don't have to care */
- *top = *left = *bottom = *right = 0;
- CHECK_WINDOW_MAGIC(window, -1);
- if (!_this->GetWindowBordersSize) {
- return SDL_Unsupported();
- }
- return _this->GetWindowBordersSize(_this, window, top, left, bottom, right);
- }
- int SDL_GetWindowSizeInPixels(SDL_Window *window, int *w, int *h)
- {
- int filter;
- CHECK_WINDOW_MAGIC(window, -1);
- if (!w) {
- w = &filter;
- }
- if (!h) {
- h = &filter;
- }
- if (_this->GetWindowSizeInPixels) {
- _this->GetWindowSizeInPixels(_this, window, w, h);
- } else {
- SDL_DisplayID displayID = SDL_GetDisplayForWindow(window);
- const SDL_DisplayMode *mode;
- SDL_GetWindowSize(window, w, h);
- if (SDL_GetWindowFullscreenMode(window)) {
- mode = SDL_GetCurrentDisplayMode(displayID);
- } else {
- mode = SDL_GetDesktopDisplayMode(displayID);
- }
- if (mode) {
- *w = (int)SDL_ceilf(*w * mode->pixel_density);
- *h = (int)SDL_ceilf(*h * mode->pixel_density);
- }
- }
- return 0;
- }
- int SDL_SetWindowMinimumSize(SDL_Window *window, int min_w, int min_h)
- {
- int w, h;
- CHECK_WINDOW_MAGIC(window, -1);
- if (min_w < 0) {
- return SDL_InvalidParamError("min_w");
- }
- if (min_h < 0) {
- return SDL_InvalidParamError("min_h");
- }
- if ((window->max_w && min_w > window->max_w) ||
- (window->max_h && min_h > window->max_h)) {
- return SDL_SetError("SDL_SetWindowMinimumSize(): Tried to set minimum size larger than maximum size");
- }
- window->min_w = min_w;
- window->min_h = min_h;
- if (_this->SetWindowMinimumSize) {
- _this->SetWindowMinimumSize(_this, window);
- }
- /* Ensure that window is not smaller than minimal size */
- w = window->min_w ? SDL_max(window->floating.w, window->min_w) : window->floating.w;
- h = window->min_h ? SDL_max(window->floating.h, window->min_h) : window->floating.h;
- return SDL_SetWindowSize(window, w, h);
- }
- int SDL_GetWindowMinimumSize(SDL_Window *window, int *min_w, int *min_h)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (min_w) {
- *min_w = window->min_w;
- }
- if (min_h) {
- *min_h = window->min_h;
- }
- return 0;
- }
- int SDL_SetWindowMaximumSize(SDL_Window *window, int max_w, int max_h)
- {
- int w, h;
- CHECK_WINDOW_MAGIC(window, -1);
- if (max_w < 0) {
- return SDL_InvalidParamError("max_w");
- }
- if (max_h < 0) {
- return SDL_InvalidParamError("max_h");
- }
- if (max_w < window->min_w || max_h < window->min_h) {
- return SDL_SetError("SDL_SetWindowMaximumSize(): Tried to set maximum size smaller than minimum size");
- }
- window->max_w = max_w;
- window->max_h = max_h;
- if (_this->SetWindowMaximumSize) {
- _this->SetWindowMaximumSize(_this, window);
- }
- /* Ensure that window is not larger than maximal size */
- w = window->max_w ? SDL_min(window->floating.w, window->max_w) : window->floating.w;
- h = window->max_h ? SDL_min(window->floating.h, window->max_h) : window->floating.h;
- return SDL_SetWindowSize(window, w, h);
- }
- int SDL_GetWindowMaximumSize(SDL_Window *window, int *max_w, int *max_h)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (max_w) {
- *max_w = window->max_w;
- }
- if (max_h) {
- *max_h = window->max_h;
- }
- return 0;
- }
- int SDL_ShowWindow(SDL_Window *window)
- {
- SDL_Window *child;
- CHECK_WINDOW_MAGIC(window, -1);
- if (!(window->flags & SDL_WINDOW_HIDDEN)) {
- return 0;
- }
- /* If the parent is hidden, set the flag to restore this when the parent is shown */
- if (window->parent && (window->parent->flags & SDL_WINDOW_HIDDEN)) {
- window->restore_on_show = SDL_TRUE;
- return 0;
- }
- if (_this->ShowWindow) {
- _this->ShowWindow(_this, window);
- } else {
- SDL_SetMouseFocus(window);
- SDL_SetKeyboardFocus(window);
- }
- SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_SHOWN, 0, 0);
- /* Restore child windows */
- for (child = window->first_child; child; child = child->next_sibling) {
- if (!child->restore_on_show && (child->flags & SDL_WINDOW_HIDDEN)) {
- break;
- }
- SDL_ShowWindow(child);
- child->restore_on_show = SDL_FALSE;
- }
- return 0;
- }
- int SDL_HideWindow(SDL_Window *window)
- {
- SDL_Window *child;
- CHECK_WINDOW_MAGIC(window, -1);
- if (window->flags & SDL_WINDOW_HIDDEN) {
- window->restore_on_show = SDL_FALSE;
- return 0;
- }
- /* Hide all child windows */
- for (child = window->first_child; child; child = child->next_sibling) {
- if (child->flags & SDL_WINDOW_HIDDEN) {
- break;
- }
- SDL_HideWindow(child);
- child->restore_on_show = SDL_TRUE;
- }
- /* Store the flags for restoration later. */
- window->pending_flags = window->flags;
- window->is_hiding = SDL_TRUE;
- if (_this->HideWindow) {
- _this->HideWindow(_this, window);
- } else {
- SDL_SetMouseFocus(NULL);
- SDL_SetKeyboardFocus(NULL);
- }
- window->is_hiding = SDL_FALSE;
- SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_HIDDEN, 0, 0);
- return 0;
- }
- int SDL_RaiseWindow(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (window->flags & SDL_WINDOW_HIDDEN) {
- return 0;
- }
- if (_this->RaiseWindow) {
- _this->RaiseWindow(_this, window);
- }
- return 0;
- }
- int SDL_MaximizeWindow(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- CHECK_WINDOW_NOT_POPUP(window, -1);
- if (!_this->MaximizeWindow) {
- return SDL_Unsupported();
- }
- if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
- return SDL_SetError("A window without the 'SDL_WINDOW_RESIZABLE' flag can't be maximized");
- }
- if (window->flags & SDL_WINDOW_HIDDEN) {
- window->pending_flags |= SDL_WINDOW_MAXIMIZED;
- return 0;
- }
- _this->MaximizeWindow(_this, window);
- SDL_SyncIfRequired(window);
- return 0;
- }
- int SDL_MinimizeWindow(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- CHECK_WINDOW_NOT_POPUP(window, -1);
- if (!_this->MinimizeWindow) {
- return SDL_Unsupported();
- }
- if (window->flags & SDL_WINDOW_HIDDEN) {
- window->pending_flags |= SDL_WINDOW_MINIMIZED;
- return 0;
- }
- _this->MinimizeWindow(_this, window);
- SDL_SyncIfRequired(window);
- return 0;
- }
- int SDL_RestoreWindow(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- CHECK_WINDOW_NOT_POPUP(window, -1);
- if (!_this->RestoreWindow) {
- return SDL_Unsupported();
- }
- if (window->flags & SDL_WINDOW_HIDDEN) {
- window->pending_flags &= ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED);
- return 0;
- }
- _this->RestoreWindow(_this, window);
- SDL_SyncIfRequired(window);
- return 0;
- }
- int SDL_SetWindowFullscreen(SDL_Window *window, SDL_bool fullscreen)
- {
- int ret = 0;
- CHECK_WINDOW_MAGIC(window, -1);
- CHECK_WINDOW_NOT_POPUP(window, -1);
- if (window->flags & SDL_WINDOW_HIDDEN) {
- if (fullscreen) {
- window->pending_flags |= SDL_WINDOW_FULLSCREEN;
- } else {
- window->pending_flags &= ~SDL_WINDOW_FULLSCREEN;
- }
- return 0;
- }
- if (fullscreen) {
- /* Set the current fullscreen mode to the desired mode */
- SDL_copyp(&window->current_fullscreen_mode, &window->requested_fullscreen_mode);
- }
- ret = SDL_UpdateFullscreenMode(window, fullscreen ? SDL_FULLSCREEN_OP_ENTER : SDL_FULLSCREEN_OP_LEAVE, SDL_TRUE);
- if (!fullscreen || ret != 0) {
- /* Clear the current fullscreen mode. */
- SDL_zero(window->current_fullscreen_mode);
- }
- if (ret == 0) {
- SDL_SyncIfRequired(window);
- }
- return ret;
- }
- int SDL_SyncWindow(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, -1)
- if (_this->SyncWindow) {
- return _this->SyncWindow(_this, window);
- }
- return 0;
- }
- static SDL_bool ShouldAttemptTextureFramebuffer(void)
- {
- const char *hint;
- SDL_bool attempt_texture_framebuffer = SDL_TRUE;
- /* The dummy driver never has GPU support, of course. */
- if (_this->is_dummy) {
- return SDL_FALSE;
- }
- /* See if there's a hint override */
- hint = SDL_GetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION);
- if (hint && *hint) {
- if (*hint == '0' || SDL_strcasecmp(hint, "false") == 0 || SDL_strcasecmp(hint, SDL_SOFTWARE_RENDERER) == 0) {
- attempt_texture_framebuffer = SDL_FALSE;
- } else {
- attempt_texture_framebuffer = SDL_TRUE;
- }
- } else {
- /* Check for platform specific defaults */
- #ifdef SDL_PLATFORM_LINUX
- /* On WSL, direct X11 is faster than using OpenGL for window framebuffers, so try to detect WSL and avoid texture framebuffer. */
- if ((_this->CreateWindowFramebuffer) && (SDL_strcmp(_this->name, "x11") == 0)) {
- struct stat sb;
- if ((stat("/proc/sys/fs/binfmt_misc/WSLInterop", &sb) == 0) || (stat("/run/WSL", &sb) == 0)) { /* if either of these exist, we're on WSL. */
- attempt_texture_framebuffer = SDL_FALSE;
- }
- }
- #endif
- #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?) */
- if (_this->CreateWindowFramebuffer && (SDL_strcmp(_this->name, "windows") == 0)) {
- attempt_texture_framebuffer = SDL_FALSE;
- }
- #endif
- #ifdef SDL_PLATFORM_EMSCRIPTEN
- attempt_texture_framebuffer = SDL_FALSE;
- #endif
- }
- return attempt_texture_framebuffer;
- }
- static SDL_Surface *SDL_CreateWindowFramebuffer(SDL_Window *window)
- {
- SDL_PixelFormat format = SDL_PIXELFORMAT_UNKNOWN;
- void *pixels = NULL;
- int pitch = 0;
- SDL_bool created_framebuffer = SDL_FALSE;
- int w, h;
- SDL_GetWindowSizeInPixels(window, &w, &h);
- /* This will switch the video backend from using a software surface to
- using a GPU texture through the 2D render API, if we think this would
- be more efficient. This only checks once, on demand. */
- if (!_this->checked_texture_framebuffer) {
- if (ShouldAttemptTextureFramebuffer()) {
- if (SDL_CreateWindowTexture(_this, window, &format, &pixels, &pitch) < 0) {
- /* !!! FIXME: if this failed halfway (made renderer, failed to make texture, etc),
- !!! FIXME: we probably need to clean this up so it doesn't interfere with
- !!! FIXME: a software fallback at the system level (can we blit to an
- !!! FIXME: OpenGL window? etc). */
- } else {
- /* future attempts will just try to use a texture framebuffer. */
- /* !!! FIXME: maybe we shouldn't override these but check if we used a texture
- !!! FIXME: framebuffer at the right places; is it feasible we could have an
- !!! FIXME: accelerated OpenGL window and a second ends up in software? */
- _this->CreateWindowFramebuffer = SDL_CreateWindowTexture;
- _this->SetWindowFramebufferVSync = SDL_SetWindowTextureVSync;
- _this->GetWindowFramebufferVSync = SDL_GetWindowTextureVSync;
- _this->UpdateWindowFramebuffer = SDL_UpdateWindowTexture;
- _this->DestroyWindowFramebuffer = SDL_DestroyWindowTexture;
- created_framebuffer = SDL_TRUE;
- }
- }
- _this->checked_texture_framebuffer = SDL_TRUE; /* don't check this again. */
- }
- if (!created_framebuffer) {
- if (!_this->CreateWindowFramebuffer || !_this->UpdateWindowFramebuffer) {
- SDL_SetError("Window framebuffer support not available");
- return NULL;
- }
- if (_this->CreateWindowFramebuffer(_this, window, &format, &pixels, &pitch) < 0) {
- return NULL;
- }
- }
- if (window->surface) {
- /* We may have gone recursive and already created the surface */
- return window->surface;
- }
- return SDL_CreateSurfaceFrom(w, h, format, pixels, pitch);
- }
- SDL_bool SDL_WindowHasSurface(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, SDL_FALSE);
- return window->surface ? SDL_TRUE : SDL_FALSE;
- }
- SDL_Surface *SDL_GetWindowSurface(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, NULL);
- if (!window->surface_valid) {
- if (window->surface) {
- window->surface->internal->flags &= ~SDL_INTERNAL_SURFACE_DONTFREE;
- SDL_DestroySurface(window->surface);
- window->surface = NULL;
- }
- window->surface = SDL_CreateWindowFramebuffer(window);
- if (window->surface) {
- window->surface_valid = SDL_TRUE;
- window->surface->internal->flags |= SDL_INTERNAL_SURFACE_DONTFREE;
- }
- }
- return window->surface;
- }
- int SDL_SetWindowSurfaceVSync(SDL_Window *window, int vsync)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (!_this->SetWindowFramebufferVSync) {
- return SDL_Unsupported();
- }
- return _this->SetWindowFramebufferVSync(_this, window, vsync);
- }
- int SDL_GetWindowSurfaceVSync(SDL_Window *window, int *vsync)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (!_this->GetWindowFramebufferVSync) {
- return SDL_Unsupported();
- }
- return _this->GetWindowFramebufferVSync(_this, window, vsync);
- }
- int SDL_UpdateWindowSurface(SDL_Window *window)
- {
- SDL_Rect full_rect;
- CHECK_WINDOW_MAGIC(window, -1);
- full_rect.x = 0;
- full_rect.y = 0;
- SDL_GetWindowSizeInPixels(window, &full_rect.w, &full_rect.h);
- return SDL_UpdateWindowSurfaceRects(window, &full_rect, 1);
- }
- int SDL_UpdateWindowSurfaceRects(SDL_Window *window, const SDL_Rect *rects,
- int numrects)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (!window->surface_valid) {
- return SDL_SetError("Window surface is invalid, please call SDL_GetWindowSurface() to get a new surface");
- }
- SDL_assert(_this->checked_texture_framebuffer); /* we should have done this before we had a valid surface. */
- return _this->UpdateWindowFramebuffer(_this, window, rects, numrects);
- }
- int SDL_DestroyWindowSurface(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (window->surface) {
- window->surface->internal->flags &= ~SDL_INTERNAL_SURFACE_DONTFREE;
- SDL_DestroySurface(window->surface);
- window->surface = NULL;
- window->surface_valid = SDL_FALSE;
- }
- if (_this->checked_texture_framebuffer) { /* never checked? No framebuffer to destroy. Don't risk calling the wrong implementation. */
- if (_this->DestroyWindowFramebuffer) {
- _this->DestroyWindowFramebuffer(_this, window);
- }
- }
- return 0;
- }
- int SDL_SetWindowOpacity(SDL_Window *window, float opacity)
- {
- int retval;
- CHECK_WINDOW_MAGIC(window, -1);
- if (!_this->SetWindowOpacity) {
- return SDL_Unsupported();
- }
- if (opacity < 0.0f) {
- opacity = 0.0f;
- } else if (opacity > 1.0f) {
- opacity = 1.0f;
- }
- retval = _this->SetWindowOpacity(_this, window, opacity);
- if (retval == 0) {
- window->opacity = opacity;
- }
- return retval;
- }
- int SDL_GetWindowOpacity(SDL_Window *window, float *out_opacity)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (out_opacity) {
- *out_opacity = window->opacity;
- }
- return 0;
- }
- int SDL_SetWindowModalFor(SDL_Window *modal_window, SDL_Window *parent_window)
- {
- CHECK_WINDOW_MAGIC(modal_window, -1);
- CHECK_WINDOW_NOT_POPUP(modal_window, -1);
- if (parent_window) {
- CHECK_WINDOW_MAGIC(parent_window, -1);
- CHECK_WINDOW_NOT_POPUP(parent_window, -1);
- }
- if (!_this->SetWindowModalFor) {
- return SDL_Unsupported();
- }
- if (parent_window) {
- modal_window->flags |= SDL_WINDOW_MODAL;
- } else if (modal_window->flags & SDL_WINDOW_MODAL) {
- modal_window->flags &= ~SDL_WINDOW_MODAL;
- } else {
- return 0; /* Not modal; nothing to do. */
- }
- const int ret = _this->SetWindowModalFor(_this, modal_window, parent_window);
- /* The existing parent might be needed when changing the modal status,
- * so don't change the hierarchy until after setting the new modal state.
- */
- if (!ret) {
- SDL_SetWindowParent(modal_window, !ret ? parent_window : NULL);
- }
- return ret;
- }
- int SDL_SetWindowFocusable(SDL_Window *window, SDL_bool focusable)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- const SDL_bool want = (focusable != SDL_FALSE); /* normalize the flag. */
- const SDL_bool have = !(window->flags & SDL_WINDOW_NOT_FOCUSABLE);
- if ((want != have) && (_this->SetWindowFocusable)) {
- if (want) {
- window->flags &= ~SDL_WINDOW_NOT_FOCUSABLE;
- } else {
- window->flags |= SDL_WINDOW_NOT_FOCUSABLE;
- }
- _this->SetWindowFocusable(_this, window, want);
- }
- return 0;
- }
- void SDL_UpdateWindowGrab(SDL_Window *window)
- {
- SDL_bool keyboard_grabbed, mouse_grabbed;
- if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
- if (SDL_GetMouse()->relative_mode || (window->flags & SDL_WINDOW_MOUSE_GRABBED)) {
- mouse_grabbed = SDL_TRUE;
- } else {
- mouse_grabbed = SDL_FALSE;
- }
- if (window->flags & SDL_WINDOW_KEYBOARD_GRABBED) {
- keyboard_grabbed = SDL_TRUE;
- } else {
- keyboard_grabbed = SDL_FALSE;
- }
- } else {
- mouse_grabbed = SDL_FALSE;
- keyboard_grabbed = SDL_FALSE;
- }
- if (mouse_grabbed || keyboard_grabbed) {
- if (_this->grabbed_window && (_this->grabbed_window != window)) {
- /* stealing a grab from another window! */
- _this->grabbed_window->flags &= ~(SDL_WINDOW_MOUSE_GRABBED | SDL_WINDOW_KEYBOARD_GRABBED);
- if (_this->SetWindowMouseGrab) {
- _this->SetWindowMouseGrab(_this, _this->grabbed_window, SDL_FALSE);
- }
- if (_this->SetWindowKeyboardGrab) {
- _this->SetWindowKeyboardGrab(_this, _this->grabbed_window, SDL_FALSE);
- }
- }
- _this->grabbed_window = window;
- } else if (_this->grabbed_window == window) {
- _this->grabbed_window = NULL; /* ungrabbing input. */
- }
- if (_this->SetWindowMouseGrab) {
- if (_this->SetWindowMouseGrab(_this, window, mouse_grabbed) < 0) {
- window->flags &= ~SDL_WINDOW_MOUSE_GRABBED;
- }
- }
- if (_this->SetWindowKeyboardGrab) {
- if (_this->SetWindowKeyboardGrab(_this, window, keyboard_grabbed) < 0) {
- window->flags &= ~SDL_WINDOW_KEYBOARD_GRABBED;
- }
- }
- if (_this->grabbed_window && !(_this->grabbed_window->flags & (SDL_WINDOW_MOUSE_GRABBED | SDL_WINDOW_KEYBOARD_GRABBED))) {
- _this->grabbed_window = NULL;
- }
- }
- int SDL_SetWindowKeyboardGrab(SDL_Window *window, SDL_bool grabbed)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- CHECK_WINDOW_NOT_POPUP(window, -1);
- if (window->flags & SDL_WINDOW_HIDDEN) {
- if (grabbed) {
- window->pending_flags |= SDL_WINDOW_KEYBOARD_GRABBED;
- } else {
- window->pending_flags &= ~SDL_WINDOW_KEYBOARD_GRABBED;
- }
- return 0;
- }
- if (!!grabbed == !!(window->flags & SDL_WINDOW_KEYBOARD_GRABBED)) {
- return 0;
- }
- if (grabbed) {
- window->flags |= SDL_WINDOW_KEYBOARD_GRABBED;
- } else {
- window->flags &= ~SDL_WINDOW_KEYBOARD_GRABBED;
- }
- SDL_UpdateWindowGrab(window);
- if (grabbed && !(window->flags & SDL_WINDOW_KEYBOARD_GRABBED)) {
- return -1;
- }
- return 0;
- }
- int SDL_SetWindowMouseGrab(SDL_Window *window, SDL_bool grabbed)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- CHECK_WINDOW_NOT_POPUP(window, -1);
- if (window->flags & SDL_WINDOW_HIDDEN) {
- if (grabbed) {
- window->pending_flags |= SDL_WINDOW_MOUSE_GRABBED;
- } else {
- window->pending_flags &= ~SDL_WINDOW_MOUSE_GRABBED;
- }
- return 0;
- }
- if (!!grabbed == !!(window->flags & SDL_WINDOW_MOUSE_GRABBED)) {
- return 0;
- }
- if (grabbed) {
- window->flags |= SDL_WINDOW_MOUSE_GRABBED;
- } else {
- window->flags &= ~SDL_WINDOW_MOUSE_GRABBED;
- }
- SDL_UpdateWindowGrab(window);
- if (grabbed && !(window->flags & SDL_WINDOW_MOUSE_GRABBED)) {
- return -1;
- }
- return 0;
- }
- SDL_bool SDL_GetWindowKeyboardGrab(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, SDL_FALSE);
- return window == _this->grabbed_window && (_this->grabbed_window->flags & SDL_WINDOW_KEYBOARD_GRABBED);
- }
- SDL_bool SDL_GetWindowMouseGrab(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, SDL_FALSE);
- return window == _this->grabbed_window && (_this->grabbed_window->flags & SDL_WINDOW_MOUSE_GRABBED);
- }
- SDL_Window *SDL_GetGrabbedWindow(void)
- {
- if (_this->grabbed_window &&
- (_this->grabbed_window->flags & (SDL_WINDOW_MOUSE_GRABBED | SDL_WINDOW_KEYBOARD_GRABBED)) != 0) {
- return _this->grabbed_window;
- } else {
- return NULL;
- }
- }
- int SDL_SetWindowMouseRect(SDL_Window *window, const SDL_Rect *rect)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (rect) {
- SDL_memcpy(&window->mouse_rect, rect, sizeof(*rect));
- } else {
- SDL_zero(window->mouse_rect);
- }
- if (_this->SetWindowMouseRect) {
- return _this->SetWindowMouseRect(_this, window);
- }
- return 0;
- }
- const SDL_Rect *SDL_GetWindowMouseRect(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, NULL);
- if (SDL_RectEmpty(&window->mouse_rect)) {
- return NULL;
- } else {
- return &window->mouse_rect;
- }
- }
- int SDL_FlashWindow(SDL_Window *window, SDL_FlashOperation operation)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- CHECK_WINDOW_NOT_POPUP(window, -1);
- if (_this->FlashWindow) {
- return _this->FlashWindow(_this, window, operation);
- }
- return SDL_Unsupported();
- }
- void SDL_OnWindowShown(SDL_Window *window)
- {
- /* Set window state if we have pending window flags cached */
- ApplyWindowFlags(window, window->pending_flags);
- window->pending_flags = 0;
- }
- void SDL_OnWindowHidden(SDL_Window *window)
- {
- /* Store the maximized and fullscreen flags for restoration later, in case
- * this was initiated by the window manager due to the window being unmapped
- * when minimized.
- */
- window->pending_flags |= (window->flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_MAXIMIZED));
- /* The window is already hidden at this point, so just change the mode back if necessary. */
- SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_LEAVE, SDL_FALSE);
- }
- void SDL_OnWindowDisplayChanged(SDL_Window *window)
- {
- if (window->flags & SDL_WINDOW_FULLSCREEN) {
- SDL_DisplayID displayID = SDL_GetDisplayForWindowPosition(window);
- const SDL_DisplayMode *new_mode = NULL;
- if (window->requested_fullscreen_mode.w != 0 || window->requested_fullscreen_mode.h != 0) {
- SDL_bool include_high_density_modes = SDL_FALSE;
- if (window->requested_fullscreen_mode.pixel_density > 1.0f) {
- include_high_density_modes = SDL_TRUE;
- }
- new_mode = SDL_GetClosestFullscreenDisplayMode(displayID, window->requested_fullscreen_mode.w, window->requested_fullscreen_mode.h, window->requested_fullscreen_mode.refresh_rate, include_high_density_modes);
- }
- if (new_mode) {
- SDL_copyp(&window->current_fullscreen_mode, new_mode);
- } else {
- SDL_zero(window->current_fullscreen_mode);
- }
- if (SDL_WINDOW_FULLSCREEN_VISIBLE(window)) {
- SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_UPDATE, SDL_TRUE);
- }
- }
- SDL_CheckWindowPixelSizeChanged(window);
- }
- void SDL_OnWindowMoved(SDL_Window *window)
- {
- SDL_CheckWindowDisplayChanged(window);
- }
- void SDL_OnWindowResized(SDL_Window *window)
- {
- SDL_CheckWindowDisplayChanged(window);
- SDL_CheckWindowPixelSizeChanged(window);
- if ((window->flags & SDL_WINDOW_TRANSPARENT) && _this->UpdateWindowShape) {
- SDL_Surface *surface = (SDL_Surface *)SDL_GetPointerProperty(window->props, SDL_PROP_WINDOW_SHAPE_POINTER, NULL);
- if (surface) {
- _this->UpdateWindowShape(_this, window, surface);
- }
- }
- }
- void SDL_CheckWindowPixelSizeChanged(SDL_Window *window)
- {
- int pixel_w = 0, pixel_h = 0;
- SDL_GetWindowSizeInPixels(window, &pixel_w, &pixel_h);
- SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED, pixel_w, pixel_h);
- SDL_CheckWindowDisplayScaleChanged(window);
- }
- void SDL_OnWindowPixelSizeChanged(SDL_Window *window)
- {
- window->surface_valid = SDL_FALSE;
- }
- void SDL_OnWindowMinimized(SDL_Window *window)
- {
- if (window->flags & SDL_WINDOW_FULLSCREEN) {
- SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_LEAVE, SDL_FALSE);
- }
- }
- void SDL_OnWindowMaximized(SDL_Window *window)
- {
- }
- void SDL_OnWindowRestored(SDL_Window *window)
- {
- /*
- * FIXME: Is this fine to just remove this, or should it be preserved just
- * for the fullscreen case? In principle it seems like just hiding/showing
- * windows shouldn't affect the stacking order; maybe the right fix is to
- * re-decouple OnWindowShown and OnWindowRestored.
- */
- /*SDL_RaiseWindow(window);*/
- if (window->flags & SDL_WINDOW_FULLSCREEN) {
- SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_ENTER, SDL_FALSE);
- }
- }
- void SDL_OnWindowEnter(SDL_Window *window)
- {
- if (_this->OnWindowEnter) {
- _this->OnWindowEnter(_this, window);
- }
- }
- void SDL_OnWindowLeave(SDL_Window *window)
- {
- }
- void SDL_OnWindowFocusGained(SDL_Window *window)
- {
- SDL_Mouse *mouse = SDL_GetMouse();
- if (mouse && mouse->relative_mode) {
- SDL_SetMouseFocus(window);
- if (mouse->relative_mode_warp) {
- SDL_PerformWarpMouseInWindow(window, (float)window->w / 2.0f, (float)window->h / 2.0f, SDL_TRUE);
- }
- }
- SDL_UpdateWindowGrab(window);
- }
- static SDL_bool SDL_ShouldMinimizeOnFocusLoss(SDL_Window *window)
- {
- const char *hint;
- if (!(window->flags & SDL_WINDOW_FULLSCREEN) || window->is_destroying) {
- return SDL_FALSE;
- }
- #if defined(SDL_PLATFORM_MACOS) && defined(SDL_VIDEO_DRIVER_COCOA)
- if (SDL_strcmp(_this->name, "cocoa") == 0) { /* don't do this for X11, etc */
- if (Cocoa_IsWindowInFullscreenSpace(window)) {
- return SDL_FALSE;
- }
- }
- #endif
- #ifdef SDL_PLATFORM_ANDROID
- {
- extern SDL_bool Android_JNI_ShouldMinimizeOnFocusLoss(void);
- if (!Android_JNI_ShouldMinimizeOnFocusLoss()) {
- return SDL_FALSE;
- }
- }
- #endif
- /* Real fullscreen windows should minimize on focus loss so the desktop video mode is restored */
- hint = SDL_GetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS);
- if (!hint || !*hint || SDL_strcasecmp(hint, "auto") == 0) {
- if (window->fullscreen_exclusive && !SDL_ModeSwitchingEmulated(_this)) {
- return SDL_TRUE;
- } else {
- return SDL_FALSE;
- }
- }
- return SDL_GetHintBoolean(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, SDL_FALSE);
- }
- void SDL_OnWindowFocusLost(SDL_Window *window)
- {
- SDL_UpdateWindowGrab(window);
- if (SDL_ShouldMinimizeOnFocusLoss(window)) {
- SDL_MinimizeWindow(window);
- }
- }
- SDL_Window *SDL_GetToplevelForKeyboardFocus(void)
- {
- SDL_Window *focus = SDL_GetKeyboardFocus();
- if (focus) {
- /* Get the toplevel parent window. */
- while (focus->parent) {
- focus = focus->parent;
- }
- }
- return focus;
- }
- void SDL_DestroyWindow(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window,);
- window->is_destroying = SDL_TRUE;
- /* Destroy any child windows of this window */
- while (window->first_child) {
- SDL_DestroyWindow(window->first_child);
- }
- SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_DESTROYED, 0, 0);
- SDL_Renderer *renderer = SDL_GetRenderer(window);
- if (renderer) {
- SDL_DestroyRendererWithoutFreeing(renderer);
- }
- /* Restore video mode, etc. */
- SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_LEAVE, SDL_TRUE);
- if (!(window->flags & SDL_WINDOW_EXTERNAL)) {
- SDL_HideWindow(window);
- }
- SDL_DestroyProperties(window->props);
- /* Clear the modal status, but don't unset the parent, as it may be
- * needed later in the destruction process if a backend needs to
- * update the input focus.
- */
- if (_this->SetWindowModalFor && (window->flags & SDL_WINDOW_MODAL)) {
- _this->SetWindowModalFor(_this, window, NULL);
- }
- /* Make sure the destroyed window isn't referenced by any display as a fullscreen window. */
- for (int i = 0; i < _this->num_displays; ++i) {
- if (_this->displays[i]->fullscreen_window == window) {
- _this->displays[i]->fullscreen_window = NULL;
- }
- }
- /* Make sure this window no longer has focus */
- if (SDL_GetKeyboardFocus() == window) {
- SDL_SetKeyboardFocus(NULL);
- }
- if (SDL_GetMouseFocus() == window) {
- SDL_SetMouseFocus(NULL);
- }
- SDL_DestroyWindowSurface(window);
- /* Make no context current if this is the current context window */
- if (window->flags & SDL_WINDOW_OPENGL) {
- if (_this->current_glwin == window) {
- SDL_GL_MakeCurrent(window, NULL);
- }
- }
- if (_this->DestroyWindow) {
- _this->DestroyWindow(_this, window);
- }
- /* Unload the graphics libraries after the window is destroyed, which may clean up EGL surfaces */
- if (window->flags & SDL_WINDOW_OPENGL) {
- SDL_GL_UnloadLibrary();
- }
- if (window->flags & SDL_WINDOW_VULKAN) {
- SDL_Vulkan_UnloadLibrary();
- }
- if (_this->grabbed_window == window) {
- _this->grabbed_window = NULL; /* ungrabbing input. */
- }
- if (_this->current_glwin == window) {
- _this->current_glwin = NULL;
- }
- if (_this->wakeup_window == window) {
- _this->wakeup_window = NULL;
- }
- /* Now invalidate magic */
- SDL_SetObjectValid(window, SDL_OBJECT_TYPE_WINDOW, SDL_FALSE);
- /* Free memory associated with the window */
- SDL_free(window->title);
- SDL_DestroySurface(window->icon);
- /* Unlink the window from its siblings. */
- SDL_SetWindowParent(window, NULL);
- /* Unlink the window from the list */
- if (window->next) {
- window->next->prev = window->prev;
- }
- if (window->prev) {
- window->prev->next = window->next;
- } else {
- _this->windows = window->next;
- }
- SDL_free(window);
- }
- SDL_bool SDL_ScreenSaverEnabled(void)
- {
- if (!_this) {
- return SDL_TRUE;
- }
- return !_this->suspend_screensaver;
- }
- int SDL_EnableScreenSaver(void)
- {
- if (!_this) {
- return SDL_UninitializedVideo();
- }
- if (!_this->suspend_screensaver) {
- return 0;
- }
- _this->suspend_screensaver = SDL_FALSE;
- if (_this->SuspendScreenSaver) {
- return _this->SuspendScreenSaver(_this);
- }
- return SDL_Unsupported();
- }
- int SDL_DisableScreenSaver(void)
- {
- if (!_this) {
- return SDL_UninitializedVideo();
- }
- if (_this->suspend_screensaver) {
- return 0;
- }
- _this->suspend_screensaver = SDL_TRUE;
- if (_this->SuspendScreenSaver) {
- return _this->SuspendScreenSaver(_this);
- }
- return SDL_Unsupported();
- }
- void SDL_VideoQuit(void)
- {
- int i;
- if (!_this) {
- return;
- }
- /* Halt event processing before doing anything else */
- SDL_QuitTouch();
- SDL_QuitMouse();
- SDL_QuitKeyboard();
- SDL_QuitSubSystem(SDL_INIT_EVENTS);
- SDL_EnableScreenSaver();
- /* Clean up the system video */
- while (_this->windows) {
- SDL_DestroyWindow(_this->windows);
- }
- _this->VideoQuit(_this);
- for (i = _this->num_displays; i--; ) {
- SDL_VideoDisplay *display = _this->displays[i];
- SDL_DelVideoDisplay(display->id, SDL_FALSE);
- }
- SDL_assert(_this->num_displays == 0);
- SDL_free(_this->displays);
- _this->displays = NULL;
- if (_this->primary_selection_text) {
- SDL_free(_this->primary_selection_text);
- _this->primary_selection_text = NULL;
- }
- _this->free(_this);
- _this = NULL;
- }
- int SDL_GL_LoadLibrary(const char *path)
- {
- int retval;
- if (!_this) {
- return SDL_UninitializedVideo();
- }
- if (_this->gl_config.driver_loaded) {
- if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
- return SDL_SetError("OpenGL library already loaded");
- }
- retval = 0;
- } else {
- if (!_this->GL_LoadLibrary) {
- return SDL_DllNotSupported("OpenGL");
- }
- retval = _this->GL_LoadLibrary(_this, path);
- }
- if (retval == 0) {
- ++_this->gl_config.driver_loaded;
- } else {
- if (_this->GL_UnloadLibrary) {
- _this->GL_UnloadLibrary(_this);
- }
- }
- return retval;
- }
- SDL_FunctionPointer SDL_GL_GetProcAddress(const char *proc)
- {
- SDL_FunctionPointer func;
- if (!_this) {
- SDL_UninitializedVideo();
- return NULL;
- }
- func = NULL;
- if (_this->GL_GetProcAddress) {
- if (_this->gl_config.driver_loaded) {
- func = _this->GL_GetProcAddress(_this, proc);
- } else {
- SDL_SetError("No GL driver has been loaded");
- }
- } else {
- SDL_SetError("No dynamic GL support in current SDL video driver (%s)", _this->name);
- }
- return func;
- }
- SDL_FunctionPointer SDL_EGL_GetProcAddress(const char *proc)
- {
- #ifdef SDL_VIDEO_OPENGL_EGL
- SDL_FunctionPointer func;
- if (!_this) {
- SDL_UninitializedVideo();
- return NULL;
- }
- func = NULL;
- if (_this->egl_data) {
- func = SDL_EGL_GetProcAddressInternal(_this, proc);
- } else {
- SDL_SetError("No EGL library has been loaded");
- }
- return func;
- #else
- SDL_SetError("SDL was not built with EGL support");
- return NULL;
- #endif
- }
- void SDL_GL_UnloadLibrary(void)
- {
- if (!_this) {
- SDL_UninitializedVideo();
- return;
- }
- if (_this->gl_config.driver_loaded > 0) {
- if (--_this->gl_config.driver_loaded > 0) {
- return;
- }
- if (_this->GL_UnloadLibrary) {
- _this->GL_UnloadLibrary(_this);
- }
- }
- }
- #if defined(SDL_VIDEO_OPENGL) || defined(SDL_VIDEO_OPENGL_ES) || defined(SDL_VIDEO_OPENGL_ES2)
- typedef GLenum (APIENTRY* PFNGLGETERRORPROC) (void);
- typedef void (APIENTRY* PFNGLGETINTEGERVPROC) (GLenum pname, GLint *params);
- typedef const GLubyte *(APIENTRY* PFNGLGETSTRINGPROC) (GLenum name);
- #ifndef SDL_VIDEO_OPENGL
- typedef const GLubyte *(APIENTRY* PFNGLGETSTRINGIPROC) (GLenum name, GLuint index);
- #endif
- static SDL_INLINE SDL_bool isAtLeastGL3(const char *verstr)
- {
- return verstr && (SDL_atoi(verstr) >= 3);
- }
- #endif /* SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2 */
- SDL_bool SDL_GL_ExtensionSupported(const char *extension)
- {
- #if defined(SDL_VIDEO_OPENGL) || defined(SDL_VIDEO_OPENGL_ES) || defined(SDL_VIDEO_OPENGL_ES2)
- PFNGLGETSTRINGPROC glGetStringFunc;
- const char *extensions;
- const char *start;
- const char *where, *terminator;
- /* Extension names should not have spaces. */
- where = SDL_strchr(extension, ' ');
- if (where || *extension == '\0') {
- return SDL_FALSE;
- }
- /* See if there's an environment variable override */
- start = SDL_getenv(extension);
- if (start && *start == '0') {
- return SDL_FALSE;
- }
- /* Lookup the available extensions */
- glGetStringFunc = (PFNGLGETSTRINGPROC)SDL_GL_GetProcAddress("glGetString");
- if (!glGetStringFunc) {
- return SDL_FALSE;
- }
- if (isAtLeastGL3((const char *)glGetStringFunc(GL_VERSION))) {
- PFNGLGETSTRINGIPROC glGetStringiFunc;
- PFNGLGETINTEGERVPROC glGetIntegervFunc;
- GLint num_exts = 0;
- GLint i;
- glGetStringiFunc = (PFNGLGETSTRINGIPROC)SDL_GL_GetProcAddress("glGetStringi");
- glGetIntegervFunc = (PFNGLGETINTEGERVPROC)SDL_GL_GetProcAddress("glGetIntegerv");
- if ((!glGetStringiFunc) || (!glGetIntegervFunc)) {
- return SDL_FALSE;
- }
- #ifndef GL_NUM_EXTENSIONS
- #define GL_NUM_EXTENSIONS 0x821D
- #endif
- glGetIntegervFunc(GL_NUM_EXTENSIONS, &num_exts);
- for (i = 0; i < num_exts; i++) {
- const char *thisext = (const char *)glGetStringiFunc(GL_EXTENSIONS, i);
- if (SDL_strcmp(thisext, extension) == 0) {
- return SDL_TRUE;
- }
- }
- return SDL_FALSE;
- }
- /* Try the old way with glGetString(GL_EXTENSIONS) ... */
- extensions = (const char *)glGetStringFunc(GL_EXTENSIONS);
- if (!extensions) {
- return SDL_FALSE;
- }
- /*
- * It takes a bit of care to be fool-proof about parsing the OpenGL
- * extensions string. Don't be fooled by sub-strings, etc.
- */
- start = extensions;
- for (;;) {
- where = SDL_strstr(start, extension);
- if (!where) {
- break;
- }
- terminator = where + SDL_strlen(extension);
- if (where == extensions || *(where - 1) == ' ') {
- if (*terminator == ' ' || *terminator == '\0') {
- return SDL_TRUE;
- }
- }
- start = terminator;
- }
- return SDL_FALSE;
- #else
- return SDL_FALSE;
- #endif
- }
- /* Deduce supported ES profile versions from the supported
- ARB_ES*_compatibility extensions. There is no direct query.
- This is normally only called when the OpenGL driver supports
- {GLX,WGL}_EXT_create_context_es2_profile.
- */
- void SDL_GL_DeduceMaxSupportedESProfile(int *major, int *minor)
- {
- /* THIS REQUIRES AN EXISTING GL CONTEXT THAT HAS BEEN MADE CURRENT. */
- /* Please refer to https://bugzilla.libsdl.org/show_bug.cgi?id=3725 for discussion. */
- #if defined(SDL_VIDEO_OPENGL) || defined(SDL_VIDEO_OPENGL_ES) || defined(SDL_VIDEO_OPENGL_ES2)
- /* XXX This is fragile; it will break in the event of release of
- * new versions of OpenGL ES.
- */
- if (SDL_GL_ExtensionSupported("GL_ARB_ES3_2_compatibility")) {
- *major = 3;
- *minor = 2;
- } else if (SDL_GL_ExtensionSupported("GL_ARB_ES3_1_compatibility")) {
- *major = 3;
- *minor = 1;
- } else if (SDL_GL_ExtensionSupported("GL_ARB_ES3_compatibility")) {
- *major = 3;
- *minor = 0;
- } else {
- *major = 2;
- *minor = 0;
- }
- #endif
- }
- void SDL_EGL_SetEGLAttributeCallbacks(SDL_EGLAttribArrayCallback platformAttribCallback,
- SDL_EGLIntArrayCallback surfaceAttribCallback,
- SDL_EGLIntArrayCallback contextAttribCallback)
- {
- if (!_this) {
- return;
- }
- _this->egl_platformattrib_callback = platformAttribCallback;
- _this->egl_surfaceattrib_callback = surfaceAttribCallback;
- _this->egl_contextattrib_callback = contextAttribCallback;
- }
- void SDL_GL_ResetAttributes(void)
- {
- if (!_this) {
- return;
- }
- _this->egl_platformattrib_callback = NULL;
- _this->egl_surfaceattrib_callback = NULL;
- _this->egl_contextattrib_callback = NULL;
- _this->gl_config.red_size = 8;
- _this->gl_config.green_size = 8;
- _this->gl_config.blue_size = 8;
- _this->gl_config.alpha_size = 8;
- _this->gl_config.buffer_size = 0;
- _this->gl_config.depth_size = 16;
- _this->gl_config.stencil_size = 0;
- _this->gl_config.double_buffer = 1;
- _this->gl_config.accum_red_size = 0;
- _this->gl_config.accum_green_size = 0;
- _this->gl_config.accum_blue_size = 0;
- _this->gl_config.accum_alpha_size = 0;
- _this->gl_config.stereo = 0;
- _this->gl_config.multisamplebuffers = 0;
- _this->gl_config.multisamplesamples = 0;
- _this->gl_config.floatbuffers = 0;
- _this->gl_config.retained_backing = 1;
- _this->gl_config.accelerated = -1; /* accelerated or not, both are fine */
- #ifdef SDL_VIDEO_OPENGL
- _this->gl_config.major_version = 2;
- _this->gl_config.minor_version = 1;
- _this->gl_config.profile_mask = 0;
- #elif defined(SDL_VIDEO_OPENGL_ES2)
- _this->gl_config.major_version = 2;
- _this->gl_config.minor_version = 0;
- _this->gl_config.profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
- #elif defined(SDL_VIDEO_OPENGL_ES)
- _this->gl_config.major_version = 1;
- _this->gl_config.minor_version = 1;
- _this->gl_config.profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
- #endif
- if (_this->GL_DefaultProfileConfig) {
- _this->GL_DefaultProfileConfig(_this, &_this->gl_config.profile_mask,
- &_this->gl_config.major_version,
- &_this->gl_config.minor_version);
- }
- _this->gl_config.flags = 0;
- _this->gl_config.framebuffer_srgb_capable = 0;
- _this->gl_config.no_error = 0;
- _this->gl_config.release_behavior = SDL_GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH;
- _this->gl_config.reset_notification = SDL_GL_CONTEXT_RESET_NO_NOTIFICATION;
- _this->gl_config.share_with_current_context = 0;
- _this->gl_config.egl_platform = 0;
- }
- int SDL_GL_SetAttribute(SDL_GLattr attr, int value)
- {
- #if defined(SDL_VIDEO_OPENGL) || defined(SDL_VIDEO_OPENGL_ES) || defined(SDL_VIDEO_OPENGL_ES2)
- int retval;
- if (!_this) {
- return SDL_UninitializedVideo();
- }
- retval = 0;
- switch (attr) {
- case SDL_GL_RED_SIZE:
- _this->gl_config.red_size = value;
- break;
- case SDL_GL_GREEN_SIZE:
- _this->gl_config.green_size = value;
- break;
- case SDL_GL_BLUE_SIZE:
- _this->gl_config.blue_size = value;
- break;
- case SDL_GL_ALPHA_SIZE:
- _this->gl_config.alpha_size = value;
- break;
- case SDL_GL_DOUBLEBUFFER:
- _this->gl_config.double_buffer = value;
- break;
- case SDL_GL_BUFFER_SIZE:
- _this->gl_config.buffer_size = value;
- break;
- case SDL_GL_DEPTH_SIZE:
- _this->gl_config.depth_size = value;
- break;
- case SDL_GL_STENCIL_SIZE:
- _this->gl_config.stencil_size = value;
- break;
- case SDL_GL_ACCUM_RED_SIZE:
- _this->gl_config.accum_red_size = value;
- break;
- case SDL_GL_ACCUM_GREEN_SIZE:
- _this->gl_config.accum_green_size = value;
- break;
- case SDL_GL_ACCUM_BLUE_SIZE:
- _this->gl_config.accum_blue_size = value;
- break;
- case SDL_GL_ACCUM_ALPHA_SIZE:
- _this->gl_config.accum_alpha_size = value;
- break;
- case SDL_GL_STEREO:
- _this->gl_config.stereo = value;
- break;
- case SDL_GL_MULTISAMPLEBUFFERS:
- _this->gl_config.multisamplebuffers = value;
- break;
- case SDL_GL_MULTISAMPLESAMPLES:
- _this->gl_config.multisamplesamples = value;
- break;
- case SDL_GL_FLOATBUFFERS:
- _this->gl_config.floatbuffers = value;
- break;
- case SDL_GL_ACCELERATED_VISUAL:
- _this->gl_config.accelerated = value;
- break;
- case SDL_GL_RETAINED_BACKING:
- _this->gl_config.retained_backing = value;
- break;
- case SDL_GL_CONTEXT_MAJOR_VERSION:
- _this->gl_config.major_version = value;
- break;
- case SDL_GL_CONTEXT_MINOR_VERSION:
- _this->gl_config.minor_version = value;
- break;
- case SDL_GL_CONTEXT_FLAGS:
- if (value & ~(SDL_GL_CONTEXT_DEBUG_FLAG |
- SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG |
- SDL_GL_CONTEXT_ROBUST_ACCESS_FLAG |
- SDL_GL_CONTEXT_RESET_ISOLATION_FLAG)) {
- retval = SDL_SetError("Unknown OpenGL context flag %d", value);
- break;
- }
- _this->gl_config.flags = value;
- break;
- case SDL_GL_CONTEXT_PROFILE_MASK:
- if (value != 0 &&
- value != SDL_GL_CONTEXT_PROFILE_CORE &&
- value != SDL_GL_CONTEXT_PROFILE_COMPATIBILITY &&
- value != SDL_GL_CONTEXT_PROFILE_ES) {
- retval = SDL_SetError("Unknown OpenGL context profile %d", value);
- break;
- }
- _this->gl_config.profile_mask = value;
- break;
- case SDL_GL_SHARE_WITH_CURRENT_CONTEXT:
- _this->gl_config.share_with_current_context = value;
- break;
- case SDL_GL_FRAMEBUFFER_SRGB_CAPABLE:
- _this->gl_config.framebuffer_srgb_capable = value;
- break;
- case SDL_GL_CONTEXT_RELEASE_BEHAVIOR:
- _this->gl_config.release_behavior = value;
- break;
- case SDL_GL_CONTEXT_RESET_NOTIFICATION:
- _this->gl_config.reset_notification = value;
- break;
- case SDL_GL_CONTEXT_NO_ERROR:
- _this->gl_config.no_error = value;
- break;
- case SDL_GL_EGL_PLATFORM:
- _this->gl_config.egl_platform = value;
- break;
- default:
- retval = SDL_SetError("Unknown OpenGL attribute");
- break;
- }
- return retval;
- #else
- return SDL_Unsupported();
- #endif /* SDL_VIDEO_OPENGL */
- }
- int SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
- {
- #if defined(SDL_VIDEO_OPENGL) || defined(SDL_VIDEO_OPENGL_ES) || defined(SDL_VIDEO_OPENGL_ES2)
- PFNGLGETERRORPROC glGetErrorFunc;
- GLenum attrib = 0;
- GLenum error = 0;
- /*
- * Some queries in Core Profile desktop OpenGL 3+ contexts require
- * glGetFramebufferAttachmentParameteriv instead of glGetIntegerv. Note that
- * the enums we use for the former function don't exist in OpenGL ES 2, and
- * the function itself doesn't exist prior to OpenGL 3 and OpenGL ES 2.
- */
- #ifdef SDL_VIDEO_OPENGL
- PFNGLGETSTRINGPROC glGetStringFunc;
- PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC glGetFramebufferAttachmentParameterivFunc;
- GLenum attachment = GL_BACK_LEFT;
- GLenum attachmentattrib = 0;
- #endif
- if (!value) {
- return SDL_InvalidParamError("value");
- }
- /* Clear value in any case */
- *value = 0;
- if (!_this) {
- return SDL_UninitializedVideo();
- }
- switch (attr) {
- case SDL_GL_RED_SIZE:
- #ifdef SDL_VIDEO_OPENGL
- attachmentattrib = GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE;
- #endif
- attrib = GL_RED_BITS;
- break;
- case SDL_GL_BLUE_SIZE:
- #ifdef SDL_VIDEO_OPENGL
- attachmentattrib = GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE;
- #endif
- attrib = GL_BLUE_BITS;
- break;
- case SDL_GL_GREEN_SIZE:
- #ifdef SDL_VIDEO_OPENGL
- attachmentattrib = GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE;
- #endif
- attrib = GL_GREEN_BITS;
- break;
- case SDL_GL_ALPHA_SIZE:
- #ifdef SDL_VIDEO_OPENGL
- attachmentattrib = GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE;
- #endif
- attrib = GL_ALPHA_BITS;
- break;
- case SDL_GL_DOUBLEBUFFER:
- #ifdef SDL_VIDEO_OPENGL
- attrib = GL_DOUBLEBUFFER;
- break;
- #else
- /* OpenGL ES 1.0 and above specifications have EGL_SINGLE_BUFFER */
- /* parameter which switches double buffer to single buffer. OpenGL ES */
- /* SDL driver must set proper value after initialization */
- *value = _this->gl_config.double_buffer;
- return 0;
- #endif
- case SDL_GL_DEPTH_SIZE:
- #ifdef SDL_VIDEO_OPENGL
- attachment = GL_DEPTH;
- attachmentattrib = GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE;
- #endif
- attrib = GL_DEPTH_BITS;
- break;
- case SDL_GL_STENCIL_SIZE:
- #ifdef SDL_VIDEO_OPENGL
- attachment = GL_STENCIL;
- attachmentattrib = GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE;
- #endif
- attrib = GL_STENCIL_BITS;
- break;
- #ifdef SDL_VIDEO_OPENGL
- case SDL_GL_ACCUM_RED_SIZE:
- attrib = GL_ACCUM_RED_BITS;
- break;
- case SDL_GL_ACCUM_GREEN_SIZE:
- attrib = GL_ACCUM_GREEN_BITS;
- break;
- case SDL_GL_ACCUM_BLUE_SIZE:
- attrib = GL_ACCUM_BLUE_BITS;
- break;
- case SDL_GL_ACCUM_ALPHA_SIZE:
- attrib = GL_ACCUM_ALPHA_BITS;
- break;
- case SDL_GL_STEREO:
- attrib = GL_STEREO;
- break;
- #else
- case SDL_GL_ACCUM_RED_SIZE:
- case SDL_GL_ACCUM_GREEN_SIZE:
- case SDL_GL_ACCUM_BLUE_SIZE:
- case SDL_GL_ACCUM_ALPHA_SIZE:
- case SDL_GL_STEREO:
- /* none of these are supported in OpenGL ES */
- *value = 0;
- return 0;
- #endif
- case SDL_GL_MULTISAMPLEBUFFERS:
- attrib = GL_SAMPLE_BUFFERS;
- break;
- case SDL_GL_MULTISAMPLESAMPLES:
- attrib = GL_SAMPLES;
- break;
- case SDL_GL_CONTEXT_RELEASE_BEHAVIOR:
- #ifdef SDL_VIDEO_OPENGL
- attrib = GL_CONTEXT_RELEASE_BEHAVIOR;
- #else
- attrib = GL_CONTEXT_RELEASE_BEHAVIOR_KHR;
- #endif
- break;
- case SDL_GL_BUFFER_SIZE:
- {
- int rsize = 0, gsize = 0, bsize = 0, asize = 0;
- /* There doesn't seem to be a single flag in OpenGL for this! */
- if (SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &rsize) < 0) {
- return -1;
- }
- if (SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &gsize) < 0) {
- return -1;
- }
- if (SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &bsize) < 0) {
- return -1;
- }
- if (SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &asize) < 0) {
- return -1;
- }
- *value = rsize + gsize + bsize + asize;
- return 0;
- }
- case SDL_GL_ACCELERATED_VISUAL:
- {
- /* FIXME: How do we get this information? */
- *value = (_this->gl_config.accelerated != 0);
- return 0;
- }
- case SDL_GL_RETAINED_BACKING:
- {
- *value = _this->gl_config.retained_backing;
- return 0;
- }
- case SDL_GL_CONTEXT_MAJOR_VERSION:
- {
- *value = _this->gl_config.major_version;
- return 0;
- }
- case SDL_GL_CONTEXT_MINOR_VERSION:
- {
- *value = _this->gl_config.minor_version;
- return 0;
- }
- case SDL_GL_CONTEXT_FLAGS:
- {
- *value = _this->gl_config.flags;
- return 0;
- }
- case SDL_GL_CONTEXT_PROFILE_MASK:
- {
- *value = _this->gl_config.profile_mask;
- return 0;
- }
- case SDL_GL_SHARE_WITH_CURRENT_CONTEXT:
- {
- *value = _this->gl_config.share_with_current_context;
- return 0;
- }
- case SDL_GL_FRAMEBUFFER_SRGB_CAPABLE:
- {
- *value = _this->gl_config.framebuffer_srgb_capable;
- return 0;
- }
- case SDL_GL_CONTEXT_NO_ERROR:
- {
- *value = _this->gl_config.no_error;
- return 0;
- }
- case SDL_GL_EGL_PLATFORM:
- {
- *value = _this->gl_config.egl_platform;
- return 0;
- }
- default:
- return SDL_SetError("Unknown OpenGL attribute");
- }
- #ifdef SDL_VIDEO_OPENGL
- glGetStringFunc = (PFNGLGETSTRINGPROC)SDL_GL_GetProcAddress("glGetString");
- if (!glGetStringFunc) {
- return -1;
- }
- if (attachmentattrib && isAtLeastGL3((const char *)glGetStringFunc(GL_VERSION))) {
- /* glGetFramebufferAttachmentParameteriv needs to operate on the window framebuffer for this, so bind FBO 0 if necessary. */
- GLint current_fbo = 0;
- PFNGLGETINTEGERVPROC glGetIntegervFunc = (PFNGLGETINTEGERVPROC) SDL_GL_GetProcAddress("glGetIntegerv");
- PFNGLBINDFRAMEBUFFERPROC glBindFramebufferFunc = (PFNGLBINDFRAMEBUFFERPROC)SDL_GL_GetProcAddress("glBindFramebuffer");
- if (glGetIntegervFunc && glBindFramebufferFunc) {
- glGetIntegervFunc(GL_DRAW_FRAMEBUFFER_BINDING, ¤t_fbo);
- }
- glGetFramebufferAttachmentParameterivFunc = (PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC)SDL_GL_GetProcAddress("glGetFramebufferAttachmentParameteriv");
- if (glGetFramebufferAttachmentParameterivFunc) {
- if (glBindFramebufferFunc && (current_fbo != 0)) {
- glBindFramebufferFunc(GL_DRAW_FRAMEBUFFER, 0);
- }
- glGetFramebufferAttachmentParameterivFunc(GL_FRAMEBUFFER, attachment, attachmentattrib, (GLint *)value);
- if (glBindFramebufferFunc && (current_fbo != 0)) {
- glBindFramebufferFunc(GL_DRAW_FRAMEBUFFER, current_fbo);
- }
- } else {
- return -1;
- }
- } else
- #endif
- {
- PFNGLGETINTEGERVPROC glGetIntegervFunc = (PFNGLGETINTEGERVPROC)SDL_GL_GetProcAddress("glGetIntegerv");
- if (glGetIntegervFunc) {
- glGetIntegervFunc(attrib, (GLint *)value);
- } else {
- return -1;
- }
- }
- glGetErrorFunc = (PFNGLGETERRORPROC)SDL_GL_GetProcAddress("glGetError");
- if (!glGetErrorFunc) {
- return -1;
- }
- error = glGetErrorFunc();
- if (error != GL_NO_ERROR) {
- if (error == GL_INVALID_ENUM) {
- return SDL_SetError("OpenGL error: GL_INVALID_ENUM");
- } else if (error == GL_INVALID_VALUE) {
- return SDL_SetError("OpenGL error: GL_INVALID_VALUE");
- }
- return SDL_SetError("OpenGL error: %08X", error);
- }
- return 0;
- #else
- return SDL_Unsupported();
- #endif /* SDL_VIDEO_OPENGL */
- }
- #define NOT_AN_OPENGL_WINDOW "The specified window isn't an OpenGL window"
- SDL_GLContext SDL_GL_CreateContext(SDL_Window *window)
- {
- SDL_GLContext ctx = NULL;
- CHECK_WINDOW_MAGIC(window, NULL);
- if (!(window->flags & SDL_WINDOW_OPENGL)) {
- SDL_SetError(NOT_AN_OPENGL_WINDOW);
- return NULL;
- }
- ctx = _this->GL_CreateContext(_this, window);
- /* Creating a context is assumed to make it current in the SDL driver. */
- if (ctx) {
- _this->current_glwin = window;
- _this->current_glctx = ctx;
- SDL_SetTLS(_this->current_glwin_tls, window, NULL);
- SDL_SetTLS(_this->current_glctx_tls, ctx, NULL);
- }
- return ctx;
- }
- int SDL_GL_MakeCurrent(SDL_Window *window, SDL_GLContext context)
- {
- int retval;
- if (!_this) {
- return SDL_UninitializedVideo();
- }
- if (window == SDL_GL_GetCurrentWindow() &&
- context == SDL_GL_GetCurrentContext()) {
- /* We're already current. */
- return 0;
- }
- if (!context) {
- window = NULL;
- } else if (window) {
- CHECK_WINDOW_MAGIC(window, -1);
- if (!(window->flags & SDL_WINDOW_OPENGL)) {
- return SDL_SetError(NOT_AN_OPENGL_WINDOW);
- }
- } else if (!_this->gl_allow_no_surface) {
- return SDL_SetError("Use of OpenGL without a window is not supported on this platform");
- }
- retval = _this->GL_MakeCurrent(_this, window, context);
- if (retval == 0) {
- _this->current_glwin = window;
- _this->current_glctx = context;
- SDL_SetTLS(_this->current_glwin_tls, window, NULL);
- SDL_SetTLS(_this->current_glctx_tls, context, NULL);
- }
- return retval;
- }
- SDL_Window *SDL_GL_GetCurrentWindow(void)
- {
- if (!_this) {
- SDL_UninitializedVideo();
- return NULL;
- }
- return (SDL_Window *)SDL_GetTLS(_this->current_glwin_tls);
- }
- SDL_GLContext SDL_GL_GetCurrentContext(void)
- {
- if (!_this) {
- SDL_UninitializedVideo();
- return NULL;
- }
- return (SDL_GLContext)SDL_GetTLS(_this->current_glctx_tls);
- }
- SDL_EGLDisplay SDL_EGL_GetCurrentEGLDisplay(void)
- {
- #ifdef SDL_VIDEO_OPENGL_EGL
- if (!_this) {
- SDL_UninitializedVideo();
- return EGL_NO_DISPLAY;
- }
- if (!_this->egl_data) {
- SDL_SetError("There is no current EGL display");
- return EGL_NO_DISPLAY;
- }
- return _this->egl_data->egl_display;
- #else
- SDL_SetError("SDL was not built with EGL support");
- return NULL;
- #endif
- }
- SDL_EGLConfig SDL_EGL_GetCurrentEGLConfig(void)
- {
- #ifdef SDL_VIDEO_OPENGL_EGL
- if (!_this) {
- SDL_UninitializedVideo();
- return NULL;
- }
- if (!_this->egl_data) {
- SDL_SetError("There is no current EGL display");
- return NULL;
- }
- return _this->egl_data->egl_config;
- #else
- SDL_SetError("SDL was not built with EGL support");
- return NULL;
- #endif
- }
- SDL_EGLConfig SDL_EGL_GetWindowEGLSurface(SDL_Window *window)
- {
- #ifdef SDL_VIDEO_OPENGL_EGL
- if (!_this) {
- SDL_UninitializedVideo();
- return NULL;
- }
- if (!_this->egl_data) {
- SDL_SetError("There is no current EGL display");
- return NULL;
- }
- if (_this->GL_GetEGLSurface) {
- return _this->GL_GetEGLSurface(_this, window);
- }
- return NULL;
- #else
- SDL_SetError("SDL was not built with EGL support");
- return NULL;
- #endif
- }
- int SDL_GL_SetSwapInterval(int interval)
- {
- if (!_this) {
- return SDL_UninitializedVideo();
- } else if (SDL_GL_GetCurrentContext() == NULL) {
- return SDL_SetError("No OpenGL context has been made current");
- } else if (_this->GL_SetSwapInterval) {
- return _this->GL_SetSwapInterval(_this, interval);
- } else {
- return SDL_SetError("Setting the swap interval is not supported");
- }
- }
- int SDL_GL_GetSwapInterval(int *interval)
- {
- if (!interval) {
- return SDL_InvalidParamError("interval");
- }
- *interval = 0;
- if (!_this) {
- return SDL_SetError("no video driver");;
- } else if (SDL_GL_GetCurrentContext() == NULL) {
- return SDL_SetError("no current context");;
- } else if (_this->GL_GetSwapInterval) {
- return _this->GL_GetSwapInterval(_this, interval);
- } else {
- return SDL_SetError("not implemented");;
- }
- }
- int SDL_GL_SwapWindow(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (!(window->flags & SDL_WINDOW_OPENGL)) {
- return SDL_SetError(NOT_AN_OPENGL_WINDOW);
- }
- if (SDL_GL_GetCurrentWindow() != window) {
- return SDL_SetError("The specified window has not been made current");
- }
- return _this->GL_SwapWindow(_this, window);
- }
- int SDL_GL_DestroyContext(SDL_GLContext context)
- {
- if (!_this) {
- return SDL_UninitializedVideo(); \
- }
- if (!context) {
- return SDL_InvalidParamError("context");
- }
- if (SDL_GL_GetCurrentContext() == context) {
- SDL_GL_MakeCurrent(NULL, NULL);
- }
- return _this->GL_DeleteContext(_this, context);
- }
- #if 0 /* FIXME */
- /*
- * Utility function used by SDL_WM_SetIcon(); flags & 1 for color key, flags
- * & 2 for alpha channel.
- */
- static void CreateMaskFromColorKeyOrAlpha(SDL_Surface *icon, Uint8 *mask, int flags)
- {
- int x, y;
- Uint32 colorkey;
- #define SET_MASKBIT(icon, x, y, mask) \
- mask[(y * ((icon->w + 7) / 8)) + (x / 8)] &= ~(0x01 << (7 - (x % 8)))
- colorkey = icon->format->colorkey;
- switch (SDL_BYTESPERPIXEL(icon->format)) {
- case 1:
- {
- Uint8 *pixels;
- for (y = 0; y < icon->h; ++y) {
- pixels = (Uint8 *) icon->pixels + y * icon->pitch;
- for (x = 0; x < icon->w; ++x) {
- if (*pixels++ == colorkey) {
- SET_MASKBIT(icon, x, y, mask);
- }
- }
- }
- }
- break;
- case 2:
- {
- Uint16 *pixels;
- for (y = 0; y < icon->h; ++y) {
- pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
- for (x = 0; x < icon->w; ++x) {
- if ((flags & 1) && *pixels == colorkey) {
- SET_MASKBIT(icon, x, y, mask);
- } else if ((flags & 2)
- && (*pixels & icon->format->Amask) == 0) {
- SET_MASKBIT(icon, x, y, mask);
- }
- pixels++;
- }
- }
- }
- break;
- case 4:
- {
- Uint32 *pixels;
- for (y = 0; y < icon->h; ++y) {
- pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
- for (x = 0; x < icon->w; ++x) {
- if ((flags & 1) && *pixels == colorkey) {
- SET_MASKBIT(icon, x, y, mask);
- } else if ((flags & 2)
- && (*pixels & icon->format->Amask) == 0) {
- SET_MASKBIT(icon, x, y, mask);
- }
- pixels++;
- }
- }
- }
- break;
- }
- }
- /*
- * Sets the window manager icon for the display window.
- */
- void SDL_WM_SetIcon(SDL_Surface *icon, Uint8 *mask)
- {
- if (icon && _this->SetIcon) {
- /* Generate a mask if necessary, and create the icon! */
- if (mask == NULL) {
- int mask_len = icon->h * (icon->w + 7) / 8;
- int flags = 0;
- mask = (Uint8 *) SDL_malloc(mask_len);
- if (mask == NULL) {
- return;
- }
- SDL_memset(mask, ~0, mask_len);
- if (icon->flags & SDL_SRCCOLORKEY)
- flags |= 1;
- if (icon->flags & SDL_SRCALPHA)
- flags |= 2;
- if (flags) {
- CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
- }
- _this->SetIcon(_this, icon, mask);
- SDL_free(mask);
- } else {
- _this->SetIcon(_this, icon, mask);
- }
- }
- }
- #endif
- int SDL_StartTextInput(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- /* Show the on-screen keyboard, if desired */
- const char *hint = SDL_GetHint(SDL_HINT_ENABLE_SCREEN_KEYBOARD);
- if (((!hint || SDL_strcasecmp(hint, "auto") == 0) && !SDL_HasKeyboard()) ||
- SDL_GetStringBoolean(hint, SDL_FALSE)) {
- if (_this->ShowScreenKeyboard) {
- _this->ShowScreenKeyboard(_this, window);
- }
- }
- if (!window->text_input_active) {
- /* Finally start the text input system */
- if (_this->StartTextInput) {
- if (_this->StartTextInput(_this, window) < 0) {
- return -1;
- }
- }
- window->text_input_active = SDL_TRUE;
- }
- return 0;
- }
- SDL_bool SDL_TextInputActive(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, SDL_FALSE);
- return window->text_input_active;
- }
- int SDL_StopTextInput(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (window->text_input_active) {
- /* Stop the text input system */
- if (_this->StopTextInput) {
- _this->StopTextInput(_this, window);
- }
- window->text_input_active = SDL_FALSE;
- }
- /* Hide the on-screen keyboard, if desired */
- const char *hint = SDL_GetHint(SDL_HINT_ENABLE_SCREEN_KEYBOARD);
- if (((!hint || SDL_strcasecmp(hint, "auto") == 0) && !SDL_HasKeyboard()) ||
- SDL_GetStringBoolean(hint, SDL_FALSE)) {
- if (_this->HideScreenKeyboard) {
- _this->HideScreenKeyboard(_this, window);
- }
- }
- return 0;
- }
- int SDL_SetTextInputArea(SDL_Window *window, const SDL_Rect *rect, int cursor)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (rect) {
- SDL_copyp(&window->text_input_rect, rect);
- window->text_input_cursor = cursor;
- } else {
- SDL_zero(window->text_input_rect);
- window->text_input_cursor = 0;
- }
- if (_this && _this->UpdateTextInputArea) {
- if (_this->UpdateTextInputArea(_this, window) < 0) {
- return -1;
- }
- }
- return 0;
- }
- int SDL_GetTextInputArea(SDL_Window *window, SDL_Rect *rect, int *cursor)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (rect) {
- SDL_copyp(rect, &window->text_input_rect);
- }
- if (cursor) {
- *cursor = window->text_input_cursor;
- }
- return 0;
- }
- int SDL_ClearComposition(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (_this->ClearComposition) {
- if (_this->ClearComposition(_this, window) < 0) {
- return -1;
- }
- }
- return 0;
- }
- SDL_bool SDL_HasScreenKeyboardSupport(void)
- {
- if (_this && _this->HasScreenKeyboardSupport) {
- return _this->HasScreenKeyboardSupport(_this);
- }
- return SDL_FALSE;
- }
- SDL_bool SDL_ScreenKeyboardShown(SDL_Window *window)
- {
- if (window && _this && _this->IsScreenKeyboardShown) {
- return _this->IsScreenKeyboardShown(_this, window);
- }
- return SDL_FALSE;
- }
- int SDL_GetMessageBoxCount(void)
- {
- return SDL_AtomicGet(&SDL_messagebox_count);
- }
- #ifdef SDL_VIDEO_DRIVER_ANDROID
- #include "android/SDL_androidmessagebox.h"
- #endif
- #ifdef SDL_VIDEO_DRIVER_WINDOWS
- #include "windows/SDL_windowsmessagebox.h"
- #endif
- #ifdef SDL_VIDEO_DRIVER_WINRT
- #include "winrt/SDL_winrtmessagebox.h"
- #endif
- #ifdef SDL_VIDEO_DRIVER_COCOA
- #include "cocoa/SDL_cocoamessagebox.h"
- #endif
- #ifdef SDL_VIDEO_DRIVER_UIKIT
- #include "uikit/SDL_uikitmessagebox.h"
- #endif
- #ifdef SDL_VIDEO_DRIVER_WAYLAND
- #include "wayland/SDL_waylandmessagebox.h"
- #endif
- #ifdef SDL_VIDEO_DRIVER_X11
- #include "x11/SDL_x11messagebox.h"
- #endif
- #ifdef SDL_VIDEO_DRIVER_HAIKU
- #include "haiku/SDL_bmessagebox.h"
- #endif
- #ifdef SDL_VIDEO_DRIVER_RISCOS
- #include "riscos/SDL_riscosmessagebox.h"
- #endif
- #ifdef SDL_VIDEO_DRIVER_VITA
- #include "vita/SDL_vitamessagebox.h"
- #endif
- int SDL_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonID)
- {
- int dummybutton;
- int retval = -1;
- SDL_bool relative_mode;
- SDL_bool show_cursor_prev;
- SDL_Window *current_window;
- SDL_MessageBoxData mbdata;
- if (!messageboxdata) {
- return SDL_InvalidParamError("messageboxdata");
- } else if (messageboxdata->numbuttons < 0) {
- return SDL_SetError("Invalid number of buttons");
- }
- (void)SDL_AtomicIncRef(&SDL_messagebox_count);
- current_window = SDL_GetKeyboardFocus();
- relative_mode = SDL_GetRelativeMouseMode();
- SDL_UpdateMouseCapture(SDL_FALSE);
- SDL_SetRelativeMouseMode(SDL_FALSE);
- show_cursor_prev = SDL_CursorVisible();
- SDL_ShowCursor();
- SDL_ResetKeyboard();
- if (!buttonID) {
- buttonID = &dummybutton;
- }
- SDL_memcpy(&mbdata, messageboxdata, sizeof(*messageboxdata));
- if (!mbdata.title) {
- mbdata.title = "";
- }
- if (!mbdata.message) {
- mbdata.message = "";
- }
- messageboxdata = &mbdata;
- SDL_ClearError();
- if (_this && _this->ShowMessageBox) {
- retval = _this->ShowMessageBox(_this, messageboxdata, buttonID);
- } else {
- /* It's completely fine to call this function before video is initialized */
- const char *driver_name = SDL_GetHint(SDL_HINT_VIDEO_DRIVER);
- if (driver_name && *driver_name != 0) {
- const char *driver_attempt = driver_name;
- while (driver_attempt && (*driver_attempt != 0) && (retval == -1)) {
- const char *driver_attempt_end = SDL_strchr(driver_attempt, ',');
- size_t driver_attempt_len = (driver_attempt_end) ? (driver_attempt_end - driver_attempt)
- : SDL_strlen(driver_attempt);
- for (int i = 0; bootstrap[i]; ++i) {
- if (bootstrap[i]->ShowMessageBox && (driver_attempt_len == SDL_strlen(bootstrap[i]->name)) &&
- (SDL_strncasecmp(bootstrap[i]->name, driver_attempt, driver_attempt_len) == 0)) {
- if (bootstrap[i]->ShowMessageBox(messageboxdata, buttonID) == 0) {
- retval = 0;
- }
- break;
- }
- }
- driver_attempt = (driver_attempt_end) ? (driver_attempt_end + 1) : NULL;
- }
- } else {
- for (int i = 0; bootstrap[i]; ++i) {
- if (bootstrap[i]->ShowMessageBox && bootstrap[i]->ShowMessageBox(messageboxdata, buttonID) == 0) {
- retval = 0;
- break;
- }
- }
- }
- }
- if (retval == -1) {
- const char *error = SDL_GetError();
- if (!*error) {
- SDL_SetError("No message system available");
- }
- } else {
- SDL_ClearError();
- }
- (void)SDL_AtomicDecRef(&SDL_messagebox_count);
- if (current_window) {
- SDL_RaiseWindow(current_window);
- }
- if (!show_cursor_prev) {
- SDL_HideCursor();
- }
- SDL_SetRelativeMouseMode(relative_mode);
- SDL_UpdateMouseCapture(SDL_FALSE);
- return retval;
- }
- int SDL_ShowSimpleMessageBox(SDL_MessageBoxFlags flags, const char *title, const char *message, SDL_Window *window)
- {
- #ifdef SDL_PLATFORM_EMSCRIPTEN
- /* !!! FIXME: propose a browser API for this, get this #ifdef out of here? */
- /* Web browsers don't (currently) have an API for a custom message box
- that can block, but for the most common case (SDL_ShowSimpleMessageBox),
- we can use the standard Javascript alert() function. */
- if (!title) {
- title = "";
- }
- if (!message) {
- message = "";
- }
- EM_ASM({
- alert(UTF8ToString($0) + "\n\n" + UTF8ToString($1));
- },
- title, message);
- return 0;
- #else
- SDL_MessageBoxData data;
- SDL_MessageBoxButtonData button;
- SDL_zero(data);
- data.flags = flags;
- data.title = title;
- data.message = message;
- data.numbuttons = 1;
- data.buttons = &button;
- data.window = window;
- SDL_zero(button);
- button.flags |= SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT;
- button.flags |= SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT;
- button.text = "OK";
- return SDL_ShowMessageBox(&data, NULL);
- #endif
- }
- SDL_bool SDL_ShouldAllowTopmost(void)
- {
- return SDL_GetHintBoolean(SDL_HINT_WINDOW_ALLOW_TOPMOST, SDL_TRUE);
- }
- int SDL_ShowWindowSystemMenu(SDL_Window *window, int x, int y)
- {
- CHECK_WINDOW_MAGIC(window, -1)
- CHECK_WINDOW_NOT_POPUP(window, -1)
- if (_this->ShowWindowSystemMenu) {
- _this->ShowWindowSystemMenu(window, x, y);
- return 0;
- }
- return SDL_Unsupported();
- }
- int SDL_SetWindowHitTest(SDL_Window *window, SDL_HitTest callback, void *callback_data)
- {
- CHECK_WINDOW_MAGIC(window, -1);
- if (!_this->SetWindowHitTest) {
- return SDL_Unsupported();
- }
- window->hit_test = callback;
- window->hit_test_data = callback_data;
- return _this->SetWindowHitTest(window, callback != NULL);
- }
- int SDL_SetWindowShape(SDL_Window *window, SDL_Surface *shape)
- {
- SDL_PropertiesID props;
- SDL_Surface *surface;
- CHECK_WINDOW_MAGIC(window, -1);
- if (!(window->flags & SDL_WINDOW_TRANSPARENT)) {
- return SDL_SetError("Window must be created with SDL_WINDOW_TRANSPARENT");
- }
- props = SDL_GetWindowProperties(window);
- if (!props) {
- return -1;
- }
- surface = SDL_ConvertSurface(shape, SDL_PIXELFORMAT_ARGB32);
- if (!surface) {
- return -1;
- }
- if (SDL_SetSurfaceProperty(props, SDL_PROP_WINDOW_SHAPE_POINTER, surface) < 0) {
- return -1;
- }
- if (_this->UpdateWindowShape) {
- if (_this->UpdateWindowShape(_this, window, surface) < 0) {
- return -1;
- }
- }
- return 0;
- }
- /*
- * Functions used by iOS application delegates
- */
- void SDL_OnApplicationWillTerminate(void)
- {
- SDL_SendAppEvent(SDL_EVENT_TERMINATING);
- }
- void SDL_OnApplicationDidReceiveMemoryWarning(void)
- {
- SDL_SendAppEvent(SDL_EVENT_LOW_MEMORY);
- }
- void SDL_OnApplicationWillResignActive(void)
- {
- if (_this) {
- SDL_Window *window;
- for (window = _this->windows; window; window = window->next) {
- SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_MINIMIZED, 0, 0);
- }
- SDL_SetKeyboardFocus(NULL);
- }
- SDL_SendAppEvent(SDL_EVENT_WILL_ENTER_BACKGROUND);
- }
- void SDL_OnApplicationDidEnterBackground(void)
- {
- SDL_SendAppEvent(SDL_EVENT_DID_ENTER_BACKGROUND);
- }
- void SDL_OnApplicationWillEnterForeground(void)
- {
- SDL_SendAppEvent(SDL_EVENT_WILL_ENTER_FOREGROUND);
- }
- void SDL_OnApplicationDidBecomeActive(void)
- {
- SDL_SendAppEvent(SDL_EVENT_DID_ENTER_FOREGROUND);
- if (_this) {
- SDL_Window *window;
- for (window = _this->windows; window; window = window->next) {
- SDL_SetKeyboardFocus(window);
- SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESTORED, 0, 0);
- }
- }
- }
- #define NOT_A_VULKAN_WINDOW "The specified window isn't a Vulkan window"
- int SDL_Vulkan_LoadLibrary(const char *path)
- {
- int retval;
- if (!_this) {
- SDL_UninitializedVideo();
- return -1;
- }
- if (_this->vulkan_config.loader_loaded) {
- if (path && SDL_strcmp(path, _this->vulkan_config.loader_path) != 0) {
- return SDL_SetError("Vulkan loader library already loaded");
- }
- retval = 0;
- } else {
- if (!_this->Vulkan_LoadLibrary) {
- return SDL_DllNotSupported("Vulkan");
- }
- retval = _this->Vulkan_LoadLibrary(_this, path);
- }
- if (retval == 0) {
- _this->vulkan_config.loader_loaded++;
- }
- return retval;
- }
- SDL_FunctionPointer SDL_Vulkan_GetVkGetInstanceProcAddr(void)
- {
- if (!_this) {
- SDL_UninitializedVideo();
- return NULL;
- }
- if (!_this->vulkan_config.loader_loaded) {
- SDL_SetError("No Vulkan loader has been loaded");
- return NULL;
- }
- return (SDL_FunctionPointer)_this->vulkan_config.vkGetInstanceProcAddr;
- }
- void SDL_Vulkan_UnloadLibrary(void)
- {
- if (!_this) {
- SDL_UninitializedVideo();
- return;
- }
- if (_this->vulkan_config.loader_loaded > 0) {
- if (--_this->vulkan_config.loader_loaded > 0) {
- return;
- }
- if (_this->Vulkan_UnloadLibrary) {
- _this->Vulkan_UnloadLibrary(_this);
- }
- }
- }
- char const* const* SDL_Vulkan_GetInstanceExtensions(Uint32 *count)
- {
- return _this->Vulkan_GetInstanceExtensions(_this, count);
- }
- int SDL_Vulkan_CreateSurface(SDL_Window *window,
- VkInstance instance,
- const struct VkAllocationCallbacks *allocator,
- VkSurfaceKHR *surface)
- {
- CHECK_WINDOW_MAGIC(window, SDL_FALSE);
- if (!(window->flags & SDL_WINDOW_VULKAN)) {
- return SDL_SetError(NOT_A_VULKAN_WINDOW);
- }
- if (!instance) {
- return SDL_InvalidParamError("instance");
- }
- if (!surface) {
- return SDL_InvalidParamError("surface");
- }
- return _this->Vulkan_CreateSurface(_this, window, instance, allocator, surface);
- }
- void SDL_Vulkan_DestroySurface(VkInstance instance,
- VkSurfaceKHR surface,
- const struct VkAllocationCallbacks *allocator)
- {
- if (_this && instance && surface && _this->Vulkan_DestroySurface) {
- _this->Vulkan_DestroySurface(_this, instance, surface, allocator);
- }
- }
- SDL_bool SDL_Vulkan_GetPresentationSupport(VkInstance instance,
- VkPhysicalDevice physicalDevice,
- Uint32 queueFamilyIndex)
- {
- if (!_this) {
- SDL_UninitializedVideo();
- return SDL_FALSE;
- }
- if (!instance) {
- SDL_InvalidParamError("instance");
- return SDL_FALSE;
- }
- if (!physicalDevice) {
- SDL_InvalidParamError("physicalDevice");
- return SDL_FALSE;
- }
- if (_this->Vulkan_GetPresentationSupport) {
- return _this->Vulkan_GetPresentationSupport(_this, instance, physicalDevice, queueFamilyIndex);
- }
- /* If the backend does not have this function then it does not have a
- * WSI function to query it; in other words it's not necessary to check
- * as it is always supported.
- */
- return SDL_TRUE;
- }
- SDL_MetalView SDL_Metal_CreateView(SDL_Window *window)
- {
- CHECK_WINDOW_MAGIC(window, NULL);
- if (!_this->Metal_CreateView) {
- SDL_Unsupported();
- return NULL;
- }
- if (!(window->flags & SDL_WINDOW_METAL)) {
- /* No problem, we can convert to Metal */
- if (window->flags & SDL_WINDOW_OPENGL) {
- window->flags &= ~SDL_WINDOW_OPENGL;
- SDL_GL_UnloadLibrary();
- }
- if (window->flags & SDL_WINDOW_VULKAN) {
- window->flags &= ~SDL_WINDOW_VULKAN;
- SDL_Vulkan_UnloadLibrary();
- }
- window->flags |= SDL_WINDOW_METAL;
- }
- return _this->Metal_CreateView(_this, window);
- }
- void SDL_Metal_DestroyView(SDL_MetalView view)
- {
- if (_this && view && _this->Metal_DestroyView) {
- _this->Metal_DestroyView(_this, view);
- }
- }
- void *SDL_Metal_GetLayer(SDL_MetalView view)
- {
- if (_this && _this->Metal_GetLayer) {
- if (view) {
- return _this->Metal_GetLayer(_this, view);
- } else {
- SDL_InvalidParamError("view");
- return NULL;
- }
- } else {
- SDL_SetError("Metal is not supported.");
- return NULL;
- }
- }
- #if defined(SDL_VIDEO_DRIVER_X11) || defined(SDL_VIDEO_DRIVER_WAYLAND) || defined(SDL_VIDEO_DRIVER_EMSCRIPTEN)
- const char *SDL_GetCSSCursorName(SDL_SystemCursor id, const char **fallback_name)
- {
- /* Reference: https://www.w3.org/TR/css-ui-4/#cursor */
- /* Also in: https://www.freedesktop.org/wiki/Specifications/cursor-spec/ */
- switch (id) {
- case SDL_SYSTEM_CURSOR_DEFAULT:
- return "default";
- case SDL_SYSTEM_CURSOR_TEXT:
- return "text";
- case SDL_SYSTEM_CURSOR_WAIT:
- return "wait";
- case SDL_SYSTEM_CURSOR_CROSSHAIR:
- return "crosshair";
- case SDL_SYSTEM_CURSOR_PROGRESS:
- return "progress";
- case SDL_SYSTEM_CURSOR_NWSE_RESIZE:
- if (fallback_name) {
- /* only a single arrow */
- *fallback_name = "nw-resize";
- }
- return "nwse-resize";
- case SDL_SYSTEM_CURSOR_NESW_RESIZE:
- if (fallback_name) {
- /* only a single arrow */
- *fallback_name = "ne-resize";
- }
- return "nesw-resize";
- case SDL_SYSTEM_CURSOR_EW_RESIZE:
- if (fallback_name) {
- *fallback_name = "col-resize";
- }
- return "ew-resize";
- case SDL_SYSTEM_CURSOR_NS_RESIZE:
- if (fallback_name) {
- *fallback_name = "row-resize";
- }
- return "ns-resize";
- case SDL_SYSTEM_CURSOR_MOVE:
- return "all-scroll";
- case SDL_SYSTEM_CURSOR_NOT_ALLOWED:
- return "not-allowed";
- case SDL_SYSTEM_CURSOR_POINTER:
- return "pointer";
- case SDL_SYSTEM_CURSOR_NW_RESIZE:
- return "nw-resize";
- case SDL_SYSTEM_CURSOR_N_RESIZE:
- return "n-resize";
- case SDL_SYSTEM_CURSOR_NE_RESIZE:
- return "ne-resize";
- case SDL_SYSTEM_CURSOR_E_RESIZE:
- return "e-resize";
- case SDL_SYSTEM_CURSOR_SE_RESIZE:
- return "se-resize";
- case SDL_SYSTEM_CURSOR_S_RESIZE:
- return "s-resize";
- case SDL_SYSTEM_CURSOR_SW_RESIZE:
- return "sw-resize";
- case SDL_SYSTEM_CURSOR_W_RESIZE:
- return "w-resize";
- default:
- return "default";
- }
- }
- #endif
|