SDL_video.c 145 KB

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