SDL_video.c 91 KB

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