SDL_video.c 136 KB

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