SDL_video.c 134 KB

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