SDL_video.c 136 KB

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