SDL_video.c 128 KB

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