SDL_video.c 102 KB

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