SDL_video.c 121 KB

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