SDL_video.c 116 KB

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