SDL_video.c 89 KB

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