SDL_video.c 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2015 Sam Lantinga <[email protected]>
  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. int
  595. SDL_GetDisplayDPI(int displayIndex, float * ddpi, float * hdpi, float * vdpi)
  596. {
  597. SDL_VideoDisplay *display;
  598. CHECK_DISPLAY_INDEX(displayIndex, -1);
  599. display = &_this->displays[displayIndex];
  600. if (_this->GetDisplayDPI) {
  601. if (_this->GetDisplayDPI(_this, display, ddpi, hdpi, vdpi) == 0) {
  602. return 0;
  603. }
  604. }
  605. return -1;
  606. }
  607. SDL_bool
  608. SDL_AddDisplayMode(SDL_VideoDisplay * display, const SDL_DisplayMode * mode)
  609. {
  610. SDL_DisplayMode *modes;
  611. int i, nmodes;
  612. /* Make sure we don't already have the mode in the list */
  613. modes = display->display_modes;
  614. nmodes = display->num_display_modes;
  615. for (i = 0; i < nmodes; ++i) {
  616. if (cmpmodes(mode, &modes[i]) == 0) {
  617. return SDL_FALSE;
  618. }
  619. }
  620. /* Go ahead and add the new mode */
  621. if (nmodes == display->max_display_modes) {
  622. modes =
  623. SDL_realloc(modes,
  624. (display->max_display_modes + 32) * sizeof(*modes));
  625. if (!modes) {
  626. return SDL_FALSE;
  627. }
  628. display->display_modes = modes;
  629. display->max_display_modes += 32;
  630. }
  631. modes[nmodes] = *mode;
  632. display->num_display_modes++;
  633. /* Re-sort video modes */
  634. SDL_qsort(display->display_modes, display->num_display_modes,
  635. sizeof(SDL_DisplayMode), cmpmodes);
  636. return SDL_TRUE;
  637. }
  638. static int
  639. SDL_GetNumDisplayModesForDisplay(SDL_VideoDisplay * display)
  640. {
  641. if (!display->num_display_modes && _this->GetDisplayModes) {
  642. _this->GetDisplayModes(_this, display);
  643. SDL_qsort(display->display_modes, display->num_display_modes,
  644. sizeof(SDL_DisplayMode), cmpmodes);
  645. }
  646. return display->num_display_modes;
  647. }
  648. int
  649. SDL_GetNumDisplayModes(int displayIndex)
  650. {
  651. CHECK_DISPLAY_INDEX(displayIndex, -1);
  652. return SDL_GetNumDisplayModesForDisplay(&_this->displays[displayIndex]);
  653. }
  654. int
  655. SDL_GetDisplayMode(int displayIndex, int index, SDL_DisplayMode * mode)
  656. {
  657. SDL_VideoDisplay *display;
  658. CHECK_DISPLAY_INDEX(displayIndex, -1);
  659. display = &_this->displays[displayIndex];
  660. if (index < 0 || index >= SDL_GetNumDisplayModesForDisplay(display)) {
  661. return SDL_SetError("index must be in the range of 0 - %d",
  662. SDL_GetNumDisplayModesForDisplay(display) - 1);
  663. }
  664. if (mode) {
  665. *mode = display->display_modes[index];
  666. }
  667. return 0;
  668. }
  669. int
  670. SDL_GetDesktopDisplayMode(int displayIndex, SDL_DisplayMode * mode)
  671. {
  672. SDL_VideoDisplay *display;
  673. CHECK_DISPLAY_INDEX(displayIndex, -1);
  674. display = &_this->displays[displayIndex];
  675. if (mode) {
  676. *mode = display->desktop_mode;
  677. }
  678. return 0;
  679. }
  680. int
  681. SDL_GetCurrentDisplayMode(int displayIndex, SDL_DisplayMode * mode)
  682. {
  683. SDL_VideoDisplay *display;
  684. CHECK_DISPLAY_INDEX(displayIndex, -1);
  685. display = &_this->displays[displayIndex];
  686. if (mode) {
  687. *mode = display->current_mode;
  688. }
  689. return 0;
  690. }
  691. static SDL_DisplayMode *
  692. SDL_GetClosestDisplayModeForDisplay(SDL_VideoDisplay * display,
  693. const SDL_DisplayMode * mode,
  694. SDL_DisplayMode * closest)
  695. {
  696. Uint32 target_format;
  697. int target_refresh_rate;
  698. int i;
  699. SDL_DisplayMode *current, *match;
  700. if (!mode || !closest) {
  701. SDL_SetError("Missing desired mode or closest mode parameter");
  702. return NULL;
  703. }
  704. /* Default to the desktop format */
  705. if (mode->format) {
  706. target_format = mode->format;
  707. } else {
  708. target_format = display->desktop_mode.format;
  709. }
  710. /* Default to the desktop refresh rate */
  711. if (mode->refresh_rate) {
  712. target_refresh_rate = mode->refresh_rate;
  713. } else {
  714. target_refresh_rate = display->desktop_mode.refresh_rate;
  715. }
  716. match = NULL;
  717. for (i = 0; i < SDL_GetNumDisplayModesForDisplay(display); ++i) {
  718. current = &display->display_modes[i];
  719. if (current->w && (current->w < mode->w)) {
  720. /* Out of sorted modes large enough here */
  721. break;
  722. }
  723. if (current->h && (current->h < mode->h)) {
  724. if (current->w && (current->w == mode->w)) {
  725. /* Out of sorted modes large enough here */
  726. break;
  727. }
  728. /* Wider, but not tall enough, due to a different
  729. aspect ratio. This mode must be skipped, but closer
  730. modes may still follow. */
  731. continue;
  732. }
  733. if (!match || current->w < match->w || current->h < match->h) {
  734. match = current;
  735. continue;
  736. }
  737. if (current->format != match->format) {
  738. /* Sorted highest depth to lowest */
  739. if (current->format == target_format ||
  740. (SDL_BITSPERPIXEL(current->format) >=
  741. SDL_BITSPERPIXEL(target_format)
  742. && SDL_PIXELTYPE(current->format) ==
  743. SDL_PIXELTYPE(target_format))) {
  744. match = current;
  745. }
  746. continue;
  747. }
  748. if (current->refresh_rate != match->refresh_rate) {
  749. /* Sorted highest refresh to lowest */
  750. if (current->refresh_rate >= target_refresh_rate) {
  751. match = current;
  752. }
  753. }
  754. }
  755. if (match) {
  756. if (match->format) {
  757. closest->format = match->format;
  758. } else {
  759. closest->format = mode->format;
  760. }
  761. if (match->w && match->h) {
  762. closest->w = match->w;
  763. closest->h = match->h;
  764. } else {
  765. closest->w = mode->w;
  766. closest->h = mode->h;
  767. }
  768. if (match->refresh_rate) {
  769. closest->refresh_rate = match->refresh_rate;
  770. } else {
  771. closest->refresh_rate = mode->refresh_rate;
  772. }
  773. closest->driverdata = match->driverdata;
  774. /*
  775. * Pick some reasonable defaults if the app and driver don't
  776. * care
  777. */
  778. if (!closest->format) {
  779. closest->format = SDL_PIXELFORMAT_RGB888;
  780. }
  781. if (!closest->w) {
  782. closest->w = 640;
  783. }
  784. if (!closest->h) {
  785. closest->h = 480;
  786. }
  787. return closest;
  788. }
  789. return NULL;
  790. }
  791. SDL_DisplayMode *
  792. SDL_GetClosestDisplayMode(int displayIndex,
  793. const SDL_DisplayMode * mode,
  794. SDL_DisplayMode * closest)
  795. {
  796. SDL_VideoDisplay *display;
  797. CHECK_DISPLAY_INDEX(displayIndex, NULL);
  798. display = &_this->displays[displayIndex];
  799. return SDL_GetClosestDisplayModeForDisplay(display, mode, closest);
  800. }
  801. static int
  802. SDL_SetDisplayModeForDisplay(SDL_VideoDisplay * display, const SDL_DisplayMode * mode)
  803. {
  804. SDL_DisplayMode display_mode;
  805. SDL_DisplayMode current_mode;
  806. if (mode) {
  807. display_mode = *mode;
  808. /* Default to the current mode */
  809. if (!display_mode.format) {
  810. display_mode.format = display->current_mode.format;
  811. }
  812. if (!display_mode.w) {
  813. display_mode.w = display->current_mode.w;
  814. }
  815. if (!display_mode.h) {
  816. display_mode.h = display->current_mode.h;
  817. }
  818. if (!display_mode.refresh_rate) {
  819. display_mode.refresh_rate = display->current_mode.refresh_rate;
  820. }
  821. /* Get a good video mode, the closest one possible */
  822. if (!SDL_GetClosestDisplayModeForDisplay(display, &display_mode, &display_mode)) {
  823. return SDL_SetError("No video mode large enough for %dx%d",
  824. display_mode.w, display_mode.h);
  825. }
  826. } else {
  827. display_mode = display->desktop_mode;
  828. }
  829. /* See if there's anything left to do */
  830. current_mode = display->current_mode;
  831. if (SDL_memcmp(&display_mode, &current_mode, sizeof(display_mode)) == 0) {
  832. return 0;
  833. }
  834. /* Actually change the display mode */
  835. if (!_this->SetDisplayMode) {
  836. return SDL_SetError("Video driver doesn't support changing display mode");
  837. }
  838. if (_this->SetDisplayMode(_this, display, &display_mode) < 0) {
  839. return -1;
  840. }
  841. display->current_mode = display_mode;
  842. return 0;
  843. }
  844. int
  845. SDL_GetWindowDisplayIndex(SDL_Window * window)
  846. {
  847. int displayIndex;
  848. int i, dist;
  849. int closest = -1;
  850. int closest_dist = 0x7FFFFFFF;
  851. SDL_Point center;
  852. SDL_Point delta;
  853. SDL_Rect rect;
  854. CHECK_WINDOW_MAGIC(window, -1);
  855. if (SDL_WINDOWPOS_ISUNDEFINED(window->x) ||
  856. SDL_WINDOWPOS_ISCENTERED(window->x)) {
  857. displayIndex = (window->x & 0xFFFF);
  858. if (displayIndex >= _this->num_displays) {
  859. displayIndex = 0;
  860. }
  861. return displayIndex;
  862. }
  863. if (SDL_WINDOWPOS_ISUNDEFINED(window->y) ||
  864. SDL_WINDOWPOS_ISCENTERED(window->y)) {
  865. displayIndex = (window->y & 0xFFFF);
  866. if (displayIndex >= _this->num_displays) {
  867. displayIndex = 0;
  868. }
  869. return displayIndex;
  870. }
  871. /* Find the display containing the window */
  872. for (i = 0; i < _this->num_displays; ++i) {
  873. SDL_VideoDisplay *display = &_this->displays[i];
  874. if (display->fullscreen_window == window) {
  875. return i;
  876. }
  877. }
  878. center.x = window->x + window->w / 2;
  879. center.y = window->y + window->h / 2;
  880. for (i = 0; i < _this->num_displays; ++i) {
  881. SDL_GetDisplayBounds(i, &rect);
  882. if (SDL_EnclosePoints(&center, 1, &rect, NULL)) {
  883. return i;
  884. }
  885. delta.x = center.x - (rect.x + rect.w / 2);
  886. delta.y = center.y - (rect.y + rect.h / 2);
  887. dist = (delta.x*delta.x + delta.y*delta.y);
  888. if (dist < closest_dist) {
  889. closest = i;
  890. closest_dist = dist;
  891. }
  892. }
  893. if (closest < 0) {
  894. SDL_SetError("Couldn't find any displays");
  895. }
  896. return closest;
  897. }
  898. SDL_VideoDisplay *
  899. SDL_GetDisplayForWindow(SDL_Window *window)
  900. {
  901. int displayIndex = SDL_GetWindowDisplayIndex(window);
  902. if (displayIndex >= 0) {
  903. return &_this->displays[displayIndex];
  904. } else {
  905. return NULL;
  906. }
  907. }
  908. int
  909. SDL_SetWindowDisplayMode(SDL_Window * window, const SDL_DisplayMode * mode)
  910. {
  911. CHECK_WINDOW_MAGIC(window, -1);
  912. if (mode) {
  913. window->fullscreen_mode = *mode;
  914. } else {
  915. SDL_zero(window->fullscreen_mode);
  916. }
  917. if (FULLSCREEN_VISIBLE(window) && (window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) != SDL_WINDOW_FULLSCREEN_DESKTOP) {
  918. SDL_DisplayMode fullscreen_mode;
  919. if (SDL_GetWindowDisplayMode(window, &fullscreen_mode) == 0) {
  920. SDL_SetDisplayModeForDisplay(SDL_GetDisplayForWindow(window), &fullscreen_mode);
  921. }
  922. }
  923. return 0;
  924. }
  925. int
  926. SDL_GetWindowDisplayMode(SDL_Window * window, SDL_DisplayMode * mode)
  927. {
  928. SDL_DisplayMode fullscreen_mode;
  929. SDL_VideoDisplay *display;
  930. CHECK_WINDOW_MAGIC(window, -1);
  931. if (!mode) {
  932. return SDL_InvalidParamError("mode");
  933. }
  934. fullscreen_mode = window->fullscreen_mode;
  935. if (!fullscreen_mode.w) {
  936. fullscreen_mode.w = window->windowed.w;
  937. }
  938. if (!fullscreen_mode.h) {
  939. fullscreen_mode.h = window->windowed.h;
  940. }
  941. display = SDL_GetDisplayForWindow(window);
  942. /* if in desktop size mode, just return the size of the desktop */
  943. if ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) {
  944. fullscreen_mode = display->desktop_mode;
  945. } else if (!SDL_GetClosestDisplayModeForDisplay(SDL_GetDisplayForWindow(window),
  946. &fullscreen_mode,
  947. &fullscreen_mode)) {
  948. return SDL_SetError("Couldn't find display mode match");
  949. }
  950. if (mode) {
  951. *mode = fullscreen_mode;
  952. }
  953. return 0;
  954. }
  955. Uint32
  956. SDL_GetWindowPixelFormat(SDL_Window * window)
  957. {
  958. SDL_VideoDisplay *display;
  959. CHECK_WINDOW_MAGIC(window, SDL_PIXELFORMAT_UNKNOWN);
  960. display = SDL_GetDisplayForWindow(window);
  961. return display->current_mode.format;
  962. }
  963. static void
  964. SDL_RestoreMousePosition(SDL_Window *window)
  965. {
  966. int x, y;
  967. if (window == SDL_GetMouseFocus()) {
  968. SDL_GetMouseState(&x, &y);
  969. SDL_WarpMouseInWindow(window, x, y);
  970. }
  971. }
  972. #if __WINRT__
  973. extern Uint32 WINRT_DetectWindowFlags(SDL_Window * window);
  974. #endif
  975. static int
  976. SDL_UpdateFullscreenMode(SDL_Window * window, SDL_bool fullscreen)
  977. {
  978. SDL_VideoDisplay *display;
  979. SDL_Window *other;
  980. CHECK_WINDOW_MAGIC(window,-1);
  981. /* if we are in the process of hiding don't go back to fullscreen */
  982. if ( window->is_hiding && fullscreen )
  983. return 0;
  984. #ifdef __MACOSX__
  985. /* if the window is going away and no resolution change is necessary,
  986. do nothing, or else we may trigger an ugly double-transition
  987. */
  988. if (window->is_destroying && (window->last_fullscreen_flags & FULLSCREEN_MASK) == SDL_WINDOW_FULLSCREEN_DESKTOP)
  989. return 0;
  990. /* If we're switching between a fullscreen Space and "normal" fullscreen, we need to get back to normal first. */
  991. if (fullscreen && ((window->last_fullscreen_flags & FULLSCREEN_MASK) == SDL_WINDOW_FULLSCREEN_DESKTOP) && ((window->flags & FULLSCREEN_MASK) == SDL_WINDOW_FULLSCREEN)) {
  992. if (!Cocoa_SetWindowFullscreenSpace(window, SDL_FALSE)) {
  993. return -1;
  994. }
  995. } else if (fullscreen && ((window->last_fullscreen_flags & FULLSCREEN_MASK) == SDL_WINDOW_FULLSCREEN) && ((window->flags & FULLSCREEN_MASK) == SDL_WINDOW_FULLSCREEN_DESKTOP)) {
  996. display = SDL_GetDisplayForWindow(window);
  997. SDL_SetDisplayModeForDisplay(display, NULL);
  998. if (_this->SetWindowFullscreen) {
  999. _this->SetWindowFullscreen(_this, window, display, SDL_FALSE);
  1000. }
  1001. }
  1002. if (Cocoa_SetWindowFullscreenSpace(window, fullscreen)) {
  1003. if (Cocoa_IsWindowInFullscreenSpace(window) != fullscreen) {
  1004. return -1;
  1005. }
  1006. window->last_fullscreen_flags = window->flags;
  1007. return 0;
  1008. }
  1009. #elif __WINRT__ && (NTDDI_VERSION < NTDDI_WIN10)
  1010. /* HACK: WinRT 8.x apps can't choose whether or not they are fullscreen
  1011. or not. The user can choose this, via OS-provided UI, but this can't
  1012. be set programmatically.
  1013. Just look at what SDL's WinRT video backend code detected with regards
  1014. to fullscreen (being active, or not), and figure out a return/error code
  1015. from that.
  1016. */
  1017. if (fullscreen == !(WINRT_DetectWindowFlags(window) & FULLSCREEN_MASK)) {
  1018. /* Uh oh, either:
  1019. 1. fullscreen was requested, and we're already windowed
  1020. 2. windowed-mode was requested, and we're already fullscreen
  1021. WinRT 8.x can't resolve either programmatically, so we're
  1022. giving up.
  1023. */
  1024. return -1;
  1025. } else {
  1026. /* Whatever was requested, fullscreen or windowed mode, is already
  1027. in-place.
  1028. */
  1029. return 0;
  1030. }
  1031. #endif
  1032. display = SDL_GetDisplayForWindow(window);
  1033. if (fullscreen) {
  1034. /* Hide any other fullscreen windows */
  1035. if (display->fullscreen_window &&
  1036. display->fullscreen_window != window) {
  1037. SDL_MinimizeWindow(display->fullscreen_window);
  1038. }
  1039. }
  1040. /* See if anything needs to be done now */
  1041. if ((display->fullscreen_window == window) == fullscreen) {
  1042. if ((window->last_fullscreen_flags & FULLSCREEN_MASK) == (window->flags & FULLSCREEN_MASK)) {
  1043. return 0;
  1044. }
  1045. }
  1046. /* See if there are any fullscreen windows */
  1047. for (other = _this->windows; other; other = other->next) {
  1048. SDL_bool setDisplayMode = SDL_FALSE;
  1049. if (other == window) {
  1050. setDisplayMode = fullscreen;
  1051. } else if (FULLSCREEN_VISIBLE(other) &&
  1052. SDL_GetDisplayForWindow(other) == display) {
  1053. setDisplayMode = SDL_TRUE;
  1054. }
  1055. if (setDisplayMode) {
  1056. SDL_DisplayMode fullscreen_mode;
  1057. SDL_zero(fullscreen_mode);
  1058. if (SDL_GetWindowDisplayMode(other, &fullscreen_mode) == 0) {
  1059. SDL_bool resized = SDL_TRUE;
  1060. if (other->w == fullscreen_mode.w && other->h == fullscreen_mode.h) {
  1061. resized = SDL_FALSE;
  1062. }
  1063. /* only do the mode change if we want exclusive fullscreen */
  1064. if ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) != SDL_WINDOW_FULLSCREEN_DESKTOP) {
  1065. if (SDL_SetDisplayModeForDisplay(display, &fullscreen_mode) < 0) {
  1066. return -1;
  1067. }
  1068. } else {
  1069. if (SDL_SetDisplayModeForDisplay(display, NULL) < 0) {
  1070. return -1;
  1071. }
  1072. }
  1073. if (_this->SetWindowFullscreen) {
  1074. _this->SetWindowFullscreen(_this, other, display, SDL_TRUE);
  1075. }
  1076. display->fullscreen_window = other;
  1077. /* Generate a mode change event here */
  1078. if (resized) {
  1079. SDL_SendWindowEvent(other, SDL_WINDOWEVENT_RESIZED,
  1080. fullscreen_mode.w, fullscreen_mode.h);
  1081. } else {
  1082. SDL_OnWindowResized(other);
  1083. }
  1084. SDL_RestoreMousePosition(other);
  1085. window->last_fullscreen_flags = window->flags;
  1086. return 0;
  1087. }
  1088. }
  1089. }
  1090. /* Nope, restore the desktop mode */
  1091. SDL_SetDisplayModeForDisplay(display, NULL);
  1092. if (_this->SetWindowFullscreen) {
  1093. _this->SetWindowFullscreen(_this, window, display, SDL_FALSE);
  1094. }
  1095. display->fullscreen_window = NULL;
  1096. /* Generate a mode change event here */
  1097. SDL_OnWindowResized(window);
  1098. /* Restore the cursor position */
  1099. SDL_RestoreMousePosition(window);
  1100. window->last_fullscreen_flags = window->flags;
  1101. return 0;
  1102. }
  1103. #define CREATE_FLAGS \
  1104. (SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI)
  1105. static void
  1106. SDL_FinishWindowCreation(SDL_Window *window, Uint32 flags)
  1107. {
  1108. window->windowed.x = window->x;
  1109. window->windowed.y = window->y;
  1110. window->windowed.w = window->w;
  1111. window->windowed.h = window->h;
  1112. if (flags & SDL_WINDOW_MAXIMIZED) {
  1113. SDL_MaximizeWindow(window);
  1114. }
  1115. if (flags & SDL_WINDOW_MINIMIZED) {
  1116. SDL_MinimizeWindow(window);
  1117. }
  1118. if (flags & SDL_WINDOW_FULLSCREEN) {
  1119. SDL_SetWindowFullscreen(window, flags);
  1120. }
  1121. if (flags & SDL_WINDOW_INPUT_GRABBED) {
  1122. SDL_SetWindowGrab(window, SDL_TRUE);
  1123. }
  1124. if (!(flags & SDL_WINDOW_HIDDEN)) {
  1125. SDL_ShowWindow(window);
  1126. }
  1127. }
  1128. SDL_Window *
  1129. SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
  1130. {
  1131. SDL_Window *window;
  1132. const char *hint;
  1133. if (!_this) {
  1134. /* Initialize the video system if needed */
  1135. if (SDL_VideoInit(NULL) < 0) {
  1136. return NULL;
  1137. }
  1138. }
  1139. /* Some platforms can't create zero-sized windows */
  1140. if (w < 1) {
  1141. w = 1;
  1142. }
  1143. if (h < 1) {
  1144. h = 1;
  1145. }
  1146. /* Some platforms blow up if the windows are too large. Raise it later? */
  1147. if ((w > 16384) || (h > 16384)) {
  1148. SDL_SetError("Window is too large.");
  1149. return NULL;
  1150. }
  1151. /* Some platforms have OpenGL enabled by default */
  1152. #if (SDL_VIDEO_OPENGL && __MACOSX__) || __IPHONEOS__ || __ANDROID__ || __NACL__
  1153. flags |= SDL_WINDOW_OPENGL;
  1154. #endif
  1155. if (flags & SDL_WINDOW_OPENGL) {
  1156. if (!_this->GL_CreateContext) {
  1157. SDL_SetError("No OpenGL support in video driver");
  1158. return NULL;
  1159. }
  1160. if (SDL_GL_LoadLibrary(NULL) < 0) {
  1161. return NULL;
  1162. }
  1163. }
  1164. /* Unless the user has specified the high-DPI disabling hint, respect the
  1165. * SDL_WINDOW_ALLOW_HIGHDPI flag.
  1166. */
  1167. if (flags & SDL_WINDOW_ALLOW_HIGHDPI) {
  1168. hint = SDL_GetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED);
  1169. if (hint && SDL_atoi(hint) > 0) {
  1170. flags &= ~SDL_WINDOW_ALLOW_HIGHDPI;
  1171. }
  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->x = x;
  1181. window->y = y;
  1182. window->w = w;
  1183. window->h = h;
  1184. if (SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISUNDEFINED(y) ||
  1185. SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
  1186. SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
  1187. int displayIndex;
  1188. SDL_Rect bounds;
  1189. displayIndex = SDL_GetIndexOfDisplay(display);
  1190. SDL_GetDisplayBounds(displayIndex, &bounds);
  1191. if (SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISCENTERED(x)) {
  1192. window->x = bounds.x + (bounds.w - w) / 2;
  1193. }
  1194. if (SDL_WINDOWPOS_ISUNDEFINED(y) || SDL_WINDOWPOS_ISCENTERED(y)) {
  1195. window->y = bounds.y + (bounds.h - h) / 2;
  1196. }
  1197. }
  1198. window->flags = ((flags & CREATE_FLAGS) | SDL_WINDOW_HIDDEN);
  1199. window->last_fullscreen_flags = window->flags;
  1200. window->brightness = 1.0f;
  1201. window->next = _this->windows;
  1202. window->is_destroying = SDL_FALSE;
  1203. if (_this->windows) {
  1204. _this->windows->prev = window;
  1205. }
  1206. _this->windows = window;
  1207. if (_this->CreateWindow && _this->CreateWindow(_this, window) < 0) {
  1208. SDL_DestroyWindow(window);
  1209. return NULL;
  1210. }
  1211. #if __WINRT__ && (NTDDI_VERSION < NTDDI_WIN10)
  1212. /* HACK: WinRT 8.x apps can't choose whether or not they are fullscreen
  1213. or not. The user can choose this, via OS-provided UI, but this can't
  1214. be set programmatically.
  1215. Just look at what SDL's WinRT video backend code detected with regards
  1216. to fullscreen (being active, or not), and figure out a return/error code
  1217. from that.
  1218. */
  1219. flags = window->flags;
  1220. #endif
  1221. if (title) {
  1222. SDL_SetWindowTitle(window, title);
  1223. }
  1224. SDL_FinishWindowCreation(window, flags);
  1225. /* If the window was created fullscreen, make sure the mode code matches */
  1226. SDL_UpdateFullscreenMode(window, FULLSCREEN_VISIBLE(window));
  1227. return window;
  1228. }
  1229. SDL_Window *
  1230. SDL_CreateWindowFrom(const void *data)
  1231. {
  1232. SDL_Window *window;
  1233. if (!_this) {
  1234. SDL_UninitializedVideo();
  1235. return NULL;
  1236. }
  1237. if (!_this->CreateWindowFrom) {
  1238. SDL_Unsupported();
  1239. return NULL;
  1240. }
  1241. window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
  1242. if (!window) {
  1243. SDL_OutOfMemory();
  1244. return NULL;
  1245. }
  1246. window->magic = &_this->window_magic;
  1247. window->id = _this->next_object_id++;
  1248. window->flags = SDL_WINDOW_FOREIGN;
  1249. window->last_fullscreen_flags = window->flags;
  1250. window->is_destroying = SDL_FALSE;
  1251. window->brightness = 1.0f;
  1252. window->next = _this->windows;
  1253. if (_this->windows) {
  1254. _this->windows->prev = window;
  1255. }
  1256. _this->windows = window;
  1257. if (_this->CreateWindowFrom(_this, window, data) < 0) {
  1258. SDL_DestroyWindow(window);
  1259. return NULL;
  1260. }
  1261. return window;
  1262. }
  1263. int
  1264. SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
  1265. {
  1266. SDL_bool loaded_opengl = SDL_FALSE;
  1267. if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
  1268. return SDL_SetError("No OpenGL support in video driver");
  1269. }
  1270. if (window->flags & SDL_WINDOW_FOREIGN) {
  1271. /* Can't destroy and re-create foreign windows, hrm */
  1272. flags |= SDL_WINDOW_FOREIGN;
  1273. } else {
  1274. flags &= ~SDL_WINDOW_FOREIGN;
  1275. }
  1276. /* Restore video mode, etc. */
  1277. SDL_HideWindow(window);
  1278. /* Tear down the old native window */
  1279. if (window->surface) {
  1280. window->surface->flags &= ~SDL_DONTFREE;
  1281. SDL_FreeSurface(window->surface);
  1282. window->surface = NULL;
  1283. }
  1284. if (_this->DestroyWindowFramebuffer) {
  1285. _this->DestroyWindowFramebuffer(_this, window);
  1286. }
  1287. if (_this->DestroyWindow && !(flags & SDL_WINDOW_FOREIGN)) {
  1288. _this->DestroyWindow(_this, window);
  1289. }
  1290. if ((window->flags & SDL_WINDOW_OPENGL) != (flags & SDL_WINDOW_OPENGL)) {
  1291. if (flags & SDL_WINDOW_OPENGL) {
  1292. if (SDL_GL_LoadLibrary(NULL) < 0) {
  1293. return -1;
  1294. }
  1295. loaded_opengl = SDL_TRUE;
  1296. } else {
  1297. SDL_GL_UnloadLibrary();
  1298. }
  1299. }
  1300. window->flags = ((flags & CREATE_FLAGS) | SDL_WINDOW_HIDDEN);
  1301. window->last_fullscreen_flags = window->flags;
  1302. window->is_destroying = SDL_FALSE;
  1303. if (_this->CreateWindow && !(flags & SDL_WINDOW_FOREIGN)) {
  1304. if (_this->CreateWindow(_this, window) < 0) {
  1305. if (loaded_opengl) {
  1306. SDL_GL_UnloadLibrary();
  1307. window->flags &= ~SDL_WINDOW_OPENGL;
  1308. }
  1309. return -1;
  1310. }
  1311. }
  1312. if (flags & SDL_WINDOW_FOREIGN) {
  1313. window->flags |= SDL_WINDOW_FOREIGN;
  1314. }
  1315. if (_this->SetWindowTitle && window->title) {
  1316. _this->SetWindowTitle(_this, window);
  1317. }
  1318. if (_this->SetWindowIcon && window->icon) {
  1319. _this->SetWindowIcon(_this, window, window->icon);
  1320. }
  1321. if (window->hit_test) {
  1322. _this->SetWindowHitTest(window, SDL_TRUE);
  1323. }
  1324. SDL_FinishWindowCreation(window, flags);
  1325. return 0;
  1326. }
  1327. Uint32
  1328. SDL_GetWindowID(SDL_Window * window)
  1329. {
  1330. CHECK_WINDOW_MAGIC(window, 0);
  1331. return window->id;
  1332. }
  1333. SDL_Window *
  1334. SDL_GetWindowFromID(Uint32 id)
  1335. {
  1336. SDL_Window *window;
  1337. if (!_this) {
  1338. return NULL;
  1339. }
  1340. for (window = _this->windows; window; window = window->next) {
  1341. if (window->id == id) {
  1342. return window;
  1343. }
  1344. }
  1345. return NULL;
  1346. }
  1347. Uint32
  1348. SDL_GetWindowFlags(SDL_Window * window)
  1349. {
  1350. CHECK_WINDOW_MAGIC(window, 0);
  1351. return window->flags;
  1352. }
  1353. void
  1354. SDL_SetWindowTitle(SDL_Window * window, const char *title)
  1355. {
  1356. CHECK_WINDOW_MAGIC(window,);
  1357. if (title == window->title) {
  1358. return;
  1359. }
  1360. SDL_free(window->title);
  1361. window->title = SDL_strdup(title ? title : "");
  1362. if (_this->SetWindowTitle) {
  1363. _this->SetWindowTitle(_this, window);
  1364. }
  1365. }
  1366. const char *
  1367. SDL_GetWindowTitle(SDL_Window * window)
  1368. {
  1369. CHECK_WINDOW_MAGIC(window, "");
  1370. return window->title ? window->title : "";
  1371. }
  1372. void
  1373. SDL_SetWindowIcon(SDL_Window * window, SDL_Surface * icon)
  1374. {
  1375. CHECK_WINDOW_MAGIC(window,);
  1376. if (!icon) {
  1377. return;
  1378. }
  1379. SDL_FreeSurface(window->icon);
  1380. /* Convert the icon into ARGB8888 */
  1381. window->icon = SDL_ConvertSurfaceFormat(icon, SDL_PIXELFORMAT_ARGB8888, 0);
  1382. if (!window->icon) {
  1383. return;
  1384. }
  1385. if (_this->SetWindowIcon) {
  1386. _this->SetWindowIcon(_this, window, window->icon);
  1387. }
  1388. }
  1389. void*
  1390. SDL_SetWindowData(SDL_Window * window, const char *name, void *userdata)
  1391. {
  1392. SDL_WindowUserData *prev, *data;
  1393. CHECK_WINDOW_MAGIC(window, NULL);
  1394. /* Input validation */
  1395. if (name == NULL || name[0] == '\0') {
  1396. SDL_InvalidParamError("name");
  1397. return NULL;
  1398. }
  1399. /* See if the named data already exists */
  1400. prev = NULL;
  1401. for (data = window->data; data; prev = data, data = data->next) {
  1402. if (data->name && SDL_strcmp(data->name, name) == 0) {
  1403. void *last_value = data->data;
  1404. if (userdata) {
  1405. /* Set the new value */
  1406. data->data = userdata;
  1407. } else {
  1408. /* Delete this value */
  1409. if (prev) {
  1410. prev->next = data->next;
  1411. } else {
  1412. window->data = data->next;
  1413. }
  1414. SDL_free(data->name);
  1415. SDL_free(data);
  1416. }
  1417. return last_value;
  1418. }
  1419. }
  1420. /* Add new data to the window */
  1421. if (userdata) {
  1422. data = (SDL_WindowUserData *)SDL_malloc(sizeof(*data));
  1423. data->name = SDL_strdup(name);
  1424. data->data = userdata;
  1425. data->next = window->data;
  1426. window->data = data;
  1427. }
  1428. return NULL;
  1429. }
  1430. void *
  1431. SDL_GetWindowData(SDL_Window * window, const char *name)
  1432. {
  1433. SDL_WindowUserData *data;
  1434. CHECK_WINDOW_MAGIC(window, NULL);
  1435. /* Input validation */
  1436. if (name == NULL || name[0] == '\0') {
  1437. SDL_InvalidParamError("name");
  1438. return NULL;
  1439. }
  1440. for (data = window->data; data; data = data->next) {
  1441. if (data->name && SDL_strcmp(data->name, name) == 0) {
  1442. return data->data;
  1443. }
  1444. }
  1445. return NULL;
  1446. }
  1447. void
  1448. SDL_SetWindowPosition(SDL_Window * window, int x, int y)
  1449. {
  1450. CHECK_WINDOW_MAGIC(window,);
  1451. if (SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
  1452. int displayIndex = (x & 0xFFFF);
  1453. SDL_Rect bounds;
  1454. if (displayIndex > _this->num_displays) {
  1455. displayIndex = 0;
  1456. }
  1457. SDL_zero(bounds);
  1458. SDL_GetDisplayBounds(displayIndex, &bounds);
  1459. if (SDL_WINDOWPOS_ISCENTERED(x)) {
  1460. x = bounds.x + (bounds.w - window->w) / 2;
  1461. }
  1462. if (SDL_WINDOWPOS_ISCENTERED(y)) {
  1463. y = bounds.y + (bounds.h - window->h) / 2;
  1464. }
  1465. }
  1466. if ((window->flags & SDL_WINDOW_FULLSCREEN)) {
  1467. if (!SDL_WINDOWPOS_ISUNDEFINED(x)) {
  1468. window->windowed.x = x;
  1469. }
  1470. if (!SDL_WINDOWPOS_ISUNDEFINED(y)) {
  1471. window->windowed.y = y;
  1472. }
  1473. } else {
  1474. if (!SDL_WINDOWPOS_ISUNDEFINED(x)) {
  1475. window->x = x;
  1476. }
  1477. if (!SDL_WINDOWPOS_ISUNDEFINED(y)) {
  1478. window->y = y;
  1479. }
  1480. if (_this->SetWindowPosition) {
  1481. _this->SetWindowPosition(_this, window);
  1482. }
  1483. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
  1484. }
  1485. }
  1486. void
  1487. SDL_GetWindowPosition(SDL_Window * window, int *x, int *y)
  1488. {
  1489. CHECK_WINDOW_MAGIC(window,);
  1490. /* Fullscreen windows are always at their display's origin */
  1491. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1492. int displayIndex;
  1493. if (x) {
  1494. *x = 0;
  1495. }
  1496. if (y) {
  1497. *y = 0;
  1498. }
  1499. /* Find the window's monitor and update to the
  1500. monitor offset. */
  1501. displayIndex = SDL_GetWindowDisplayIndex(window);
  1502. if (displayIndex >= 0) {
  1503. SDL_Rect bounds;
  1504. SDL_zero(bounds);
  1505. SDL_GetDisplayBounds(displayIndex, &bounds);
  1506. if (x) {
  1507. *x = bounds.x;
  1508. }
  1509. if (y) {
  1510. *y = bounds.y;
  1511. }
  1512. }
  1513. } else {
  1514. if (x) {
  1515. *x = window->x;
  1516. }
  1517. if (y) {
  1518. *y = window->y;
  1519. }
  1520. }
  1521. }
  1522. void
  1523. SDL_SetWindowBordered(SDL_Window * window, SDL_bool bordered)
  1524. {
  1525. CHECK_WINDOW_MAGIC(window,);
  1526. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  1527. const int want = (bordered != SDL_FALSE); /* normalize the flag. */
  1528. const int have = ((window->flags & SDL_WINDOW_BORDERLESS) == 0);
  1529. if ((want != have) && (_this->SetWindowBordered)) {
  1530. if (want) {
  1531. window->flags &= ~SDL_WINDOW_BORDERLESS;
  1532. } else {
  1533. window->flags |= SDL_WINDOW_BORDERLESS;
  1534. }
  1535. _this->SetWindowBordered(_this, window, (SDL_bool) want);
  1536. }
  1537. }
  1538. }
  1539. void
  1540. SDL_SetWindowSize(SDL_Window * window, int w, int h)
  1541. {
  1542. CHECK_WINDOW_MAGIC(window,);
  1543. if (w <= 0) {
  1544. SDL_InvalidParamError("w");
  1545. return;
  1546. }
  1547. if (h <= 0) {
  1548. SDL_InvalidParamError("h");
  1549. return;
  1550. }
  1551. /* Make sure we don't exceed any window size limits */
  1552. if (window->min_w && w < window->min_w)
  1553. {
  1554. w = window->min_w;
  1555. }
  1556. if (window->max_w && w > window->max_w)
  1557. {
  1558. w = window->max_w;
  1559. }
  1560. if (window->min_h && h < window->min_h)
  1561. {
  1562. h = window->min_h;
  1563. }
  1564. if (window->max_h && h > window->max_h)
  1565. {
  1566. h = window->max_h;
  1567. }
  1568. window->windowed.w = w;
  1569. window->windowed.h = h;
  1570. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1571. if (FULLSCREEN_VISIBLE(window) && (window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) != SDL_WINDOW_FULLSCREEN_DESKTOP) {
  1572. window->last_fullscreen_flags = 0;
  1573. SDL_UpdateFullscreenMode(window, SDL_TRUE);
  1574. }
  1575. } else {
  1576. window->w = w;
  1577. window->h = h;
  1578. if (_this->SetWindowSize) {
  1579. _this->SetWindowSize(_this, window);
  1580. }
  1581. if (window->w == w && window->h == h) {
  1582. /* We didn't get a SDL_WINDOWEVENT_RESIZED event (by design) */
  1583. SDL_OnWindowResized(window);
  1584. }
  1585. }
  1586. }
  1587. void
  1588. SDL_GetWindowSize(SDL_Window * window, int *w, int *h)
  1589. {
  1590. CHECK_WINDOW_MAGIC(window,);
  1591. if (w) {
  1592. *w = window->w;
  1593. }
  1594. if (h) {
  1595. *h = window->h;
  1596. }
  1597. }
  1598. void
  1599. SDL_SetWindowMinimumSize(SDL_Window * window, int min_w, int min_h)
  1600. {
  1601. CHECK_WINDOW_MAGIC(window,);
  1602. if (min_w <= 0) {
  1603. SDL_InvalidParamError("min_w");
  1604. return;
  1605. }
  1606. if (min_h <= 0) {
  1607. SDL_InvalidParamError("min_h");
  1608. return;
  1609. }
  1610. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  1611. window->min_w = min_w;
  1612. window->min_h = min_h;
  1613. if (_this->SetWindowMinimumSize) {
  1614. _this->SetWindowMinimumSize(_this, window);
  1615. }
  1616. /* Ensure that window is not smaller than minimal size */
  1617. SDL_SetWindowSize(window, SDL_max(window->w, window->min_w), SDL_max(window->h, window->min_h));
  1618. }
  1619. }
  1620. void
  1621. SDL_GetWindowMinimumSize(SDL_Window * window, int *min_w, int *min_h)
  1622. {
  1623. CHECK_WINDOW_MAGIC(window,);
  1624. if (min_w) {
  1625. *min_w = window->min_w;
  1626. }
  1627. if (min_h) {
  1628. *min_h = window->min_h;
  1629. }
  1630. }
  1631. void
  1632. SDL_SetWindowMaximumSize(SDL_Window * window, int max_w, int max_h)
  1633. {
  1634. CHECK_WINDOW_MAGIC(window,);
  1635. if (max_w <= 0) {
  1636. SDL_InvalidParamError("max_w");
  1637. return;
  1638. }
  1639. if (max_h <= 0) {
  1640. SDL_InvalidParamError("max_h");
  1641. return;
  1642. }
  1643. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  1644. window->max_w = max_w;
  1645. window->max_h = max_h;
  1646. if (_this->SetWindowMaximumSize) {
  1647. _this->SetWindowMaximumSize(_this, window);
  1648. }
  1649. /* Ensure that window is not larger than maximal size */
  1650. SDL_SetWindowSize(window, SDL_min(window->w, window->max_w), SDL_min(window->h, window->max_h));
  1651. }
  1652. }
  1653. void
  1654. SDL_GetWindowMaximumSize(SDL_Window * window, int *max_w, int *max_h)
  1655. {
  1656. CHECK_WINDOW_MAGIC(window,);
  1657. if (max_w) {
  1658. *max_w = window->max_w;
  1659. }
  1660. if (max_h) {
  1661. *max_h = window->max_h;
  1662. }
  1663. }
  1664. void
  1665. SDL_ShowWindow(SDL_Window * window)
  1666. {
  1667. CHECK_WINDOW_MAGIC(window,);
  1668. if (window->flags & SDL_WINDOW_SHOWN) {
  1669. return;
  1670. }
  1671. if (_this->ShowWindow) {
  1672. _this->ShowWindow(_this, window);
  1673. }
  1674. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SHOWN, 0, 0);
  1675. }
  1676. void
  1677. SDL_HideWindow(SDL_Window * window)
  1678. {
  1679. CHECK_WINDOW_MAGIC(window,);
  1680. if (!(window->flags & SDL_WINDOW_SHOWN)) {
  1681. return;
  1682. }
  1683. window->is_hiding = SDL_TRUE;
  1684. SDL_UpdateFullscreenMode(window, SDL_FALSE);
  1685. if (_this->HideWindow) {
  1686. _this->HideWindow(_this, window);
  1687. }
  1688. window->is_hiding = SDL_FALSE;
  1689. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
  1690. }
  1691. void
  1692. SDL_RaiseWindow(SDL_Window * window)
  1693. {
  1694. CHECK_WINDOW_MAGIC(window,);
  1695. if (!(window->flags & SDL_WINDOW_SHOWN)) {
  1696. return;
  1697. }
  1698. if (_this->RaiseWindow) {
  1699. _this->RaiseWindow(_this, window);
  1700. }
  1701. }
  1702. void
  1703. SDL_MaximizeWindow(SDL_Window * window)
  1704. {
  1705. CHECK_WINDOW_MAGIC(window,);
  1706. if (window->flags & SDL_WINDOW_MAXIMIZED) {
  1707. return;
  1708. }
  1709. /* !!! FIXME: should this check if the window is resizable? */
  1710. if (_this->MaximizeWindow) {
  1711. _this->MaximizeWindow(_this, window);
  1712. }
  1713. }
  1714. void
  1715. SDL_MinimizeWindow(SDL_Window * window)
  1716. {
  1717. CHECK_WINDOW_MAGIC(window,);
  1718. if (window->flags & SDL_WINDOW_MINIMIZED) {
  1719. return;
  1720. }
  1721. SDL_UpdateFullscreenMode(window, SDL_FALSE);
  1722. if (_this->MinimizeWindow) {
  1723. _this->MinimizeWindow(_this, window);
  1724. }
  1725. }
  1726. void
  1727. SDL_RestoreWindow(SDL_Window * window)
  1728. {
  1729. CHECK_WINDOW_MAGIC(window,);
  1730. if (!(window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
  1731. return;
  1732. }
  1733. if (_this->RestoreWindow) {
  1734. _this->RestoreWindow(_this, window);
  1735. }
  1736. }
  1737. int
  1738. SDL_SetWindowFullscreen(SDL_Window * window, Uint32 flags)
  1739. {
  1740. Uint32 oldflags;
  1741. CHECK_WINDOW_MAGIC(window, -1);
  1742. flags &= FULLSCREEN_MASK;
  1743. if (flags == (window->flags & FULLSCREEN_MASK)) {
  1744. return 0;
  1745. }
  1746. /* clear the previous flags and OR in the new ones */
  1747. oldflags = window->flags & FULLSCREEN_MASK;
  1748. window->flags &= ~FULLSCREEN_MASK;
  1749. window->flags |= flags;
  1750. if (SDL_UpdateFullscreenMode(window, FULLSCREEN_VISIBLE(window)) == 0) {
  1751. return 0;
  1752. }
  1753. window->flags &= ~FULLSCREEN_MASK;
  1754. window->flags |= oldflags;
  1755. return -1;
  1756. }
  1757. static SDL_Surface *
  1758. SDL_CreateWindowFramebuffer(SDL_Window * window)
  1759. {
  1760. Uint32 format;
  1761. void *pixels;
  1762. int pitch;
  1763. int bpp;
  1764. Uint32 Rmask, Gmask, Bmask, Amask;
  1765. if (!_this->CreateWindowFramebuffer || !_this->UpdateWindowFramebuffer) {
  1766. return NULL;
  1767. }
  1768. if (_this->CreateWindowFramebuffer(_this, window, &format, &pixels, &pitch) < 0) {
  1769. return NULL;
  1770. }
  1771. if (!SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1772. return NULL;
  1773. }
  1774. return SDL_CreateRGBSurfaceFrom(pixels, window->w, window->h, bpp, pitch, Rmask, Gmask, Bmask, Amask);
  1775. }
  1776. SDL_Surface *
  1777. SDL_GetWindowSurface(SDL_Window * window)
  1778. {
  1779. CHECK_WINDOW_MAGIC(window, NULL);
  1780. if (!window->surface_valid) {
  1781. if (window->surface) {
  1782. window->surface->flags &= ~SDL_DONTFREE;
  1783. SDL_FreeSurface(window->surface);
  1784. }
  1785. window->surface = SDL_CreateWindowFramebuffer(window);
  1786. if (window->surface) {
  1787. window->surface_valid = SDL_TRUE;
  1788. window->surface->flags |= SDL_DONTFREE;
  1789. }
  1790. }
  1791. return window->surface;
  1792. }
  1793. int
  1794. SDL_UpdateWindowSurface(SDL_Window * window)
  1795. {
  1796. SDL_Rect full_rect;
  1797. CHECK_WINDOW_MAGIC(window, -1);
  1798. full_rect.x = 0;
  1799. full_rect.y = 0;
  1800. full_rect.w = window->w;
  1801. full_rect.h = window->h;
  1802. return SDL_UpdateWindowSurfaceRects(window, &full_rect, 1);
  1803. }
  1804. int
  1805. SDL_UpdateWindowSurfaceRects(SDL_Window * window, const SDL_Rect * rects,
  1806. int numrects)
  1807. {
  1808. CHECK_WINDOW_MAGIC(window, -1);
  1809. if (!window->surface_valid) {
  1810. return SDL_SetError("Window surface is invalid, please call SDL_GetWindowSurface() to get a new surface");
  1811. }
  1812. return _this->UpdateWindowFramebuffer(_this, window, rects, numrects);
  1813. }
  1814. int
  1815. SDL_SetWindowBrightness(SDL_Window * window, float brightness)
  1816. {
  1817. Uint16 ramp[256];
  1818. int status;
  1819. CHECK_WINDOW_MAGIC(window, -1);
  1820. SDL_CalculateGammaRamp(brightness, ramp);
  1821. status = SDL_SetWindowGammaRamp(window, ramp, ramp, ramp);
  1822. if (status == 0) {
  1823. window->brightness = brightness;
  1824. }
  1825. return status;
  1826. }
  1827. float
  1828. SDL_GetWindowBrightness(SDL_Window * window)
  1829. {
  1830. CHECK_WINDOW_MAGIC(window, 1.0f);
  1831. return window->brightness;
  1832. }
  1833. int
  1834. SDL_SetWindowGammaRamp(SDL_Window * window, const Uint16 * red,
  1835. const Uint16 * green,
  1836. const Uint16 * blue)
  1837. {
  1838. CHECK_WINDOW_MAGIC(window, -1);
  1839. if (!_this->SetWindowGammaRamp) {
  1840. return SDL_Unsupported();
  1841. }
  1842. if (!window->gamma) {
  1843. if (SDL_GetWindowGammaRamp(window, NULL, NULL, NULL) < 0) {
  1844. return -1;
  1845. }
  1846. SDL_assert(window->gamma != NULL);
  1847. }
  1848. if (red) {
  1849. SDL_memcpy(&window->gamma[0*256], red, 256*sizeof(Uint16));
  1850. }
  1851. if (green) {
  1852. SDL_memcpy(&window->gamma[1*256], green, 256*sizeof(Uint16));
  1853. }
  1854. if (blue) {
  1855. SDL_memcpy(&window->gamma[2*256], blue, 256*sizeof(Uint16));
  1856. }
  1857. if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
  1858. return _this->SetWindowGammaRamp(_this, window, window->gamma);
  1859. } else {
  1860. return 0;
  1861. }
  1862. }
  1863. int
  1864. SDL_GetWindowGammaRamp(SDL_Window * window, Uint16 * red,
  1865. Uint16 * green,
  1866. Uint16 * blue)
  1867. {
  1868. CHECK_WINDOW_MAGIC(window, -1);
  1869. if (!window->gamma) {
  1870. int i;
  1871. window->gamma = (Uint16 *)SDL_malloc(256*6*sizeof(Uint16));
  1872. if (!window->gamma) {
  1873. return SDL_OutOfMemory();
  1874. }
  1875. window->saved_gamma = window->gamma + 3*256;
  1876. if (_this->GetWindowGammaRamp) {
  1877. if (_this->GetWindowGammaRamp(_this, window, window->gamma) < 0) {
  1878. return -1;
  1879. }
  1880. } else {
  1881. /* Create an identity gamma ramp */
  1882. for (i = 0; i < 256; ++i) {
  1883. Uint16 value = (Uint16)((i << 8) | i);
  1884. window->gamma[0*256+i] = value;
  1885. window->gamma[1*256+i] = value;
  1886. window->gamma[2*256+i] = value;
  1887. }
  1888. }
  1889. SDL_memcpy(window->saved_gamma, window->gamma, 3*256*sizeof(Uint16));
  1890. }
  1891. if (red) {
  1892. SDL_memcpy(red, &window->gamma[0*256], 256*sizeof(Uint16));
  1893. }
  1894. if (green) {
  1895. SDL_memcpy(green, &window->gamma[1*256], 256*sizeof(Uint16));
  1896. }
  1897. if (blue) {
  1898. SDL_memcpy(blue, &window->gamma[2*256], 256*sizeof(Uint16));
  1899. }
  1900. return 0;
  1901. }
  1902. void
  1903. SDL_UpdateWindowGrab(SDL_Window * window)
  1904. {
  1905. SDL_Window *grabbed_window;
  1906. SDL_bool grabbed;
  1907. if ((SDL_GetMouse()->relative_mode || (window->flags & SDL_WINDOW_INPUT_GRABBED)) &&
  1908. (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
  1909. grabbed = SDL_TRUE;
  1910. } else {
  1911. grabbed = SDL_FALSE;
  1912. }
  1913. grabbed_window = _this->grabbed_window;
  1914. if (grabbed) {
  1915. if (grabbed_window && (grabbed_window != window)) {
  1916. /* stealing a grab from another window! */
  1917. grabbed_window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
  1918. if (_this->SetWindowGrab) {
  1919. _this->SetWindowGrab(_this, grabbed_window, SDL_FALSE);
  1920. }
  1921. }
  1922. _this->grabbed_window = window;
  1923. } else if (grabbed_window == window) {
  1924. _this->grabbed_window = NULL; /* ungrabbing. */
  1925. }
  1926. if (_this->SetWindowGrab) {
  1927. _this->SetWindowGrab(_this, window, grabbed);
  1928. }
  1929. }
  1930. void
  1931. SDL_SetWindowGrab(SDL_Window * window, SDL_bool grabbed)
  1932. {
  1933. CHECK_WINDOW_MAGIC(window,);
  1934. if (!!grabbed == !!(window->flags & SDL_WINDOW_INPUT_GRABBED)) {
  1935. return;
  1936. }
  1937. if (grabbed) {
  1938. window->flags |= SDL_WINDOW_INPUT_GRABBED;
  1939. } else {
  1940. window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
  1941. }
  1942. SDL_UpdateWindowGrab(window);
  1943. }
  1944. SDL_bool
  1945. SDL_GetWindowGrab(SDL_Window * window)
  1946. {
  1947. CHECK_WINDOW_MAGIC(window, SDL_FALSE);
  1948. SDL_assert(!_this->grabbed_window || ((_this->grabbed_window->flags & SDL_WINDOW_INPUT_GRABBED) != 0));
  1949. return window == _this->grabbed_window;
  1950. }
  1951. SDL_Window *
  1952. SDL_GetGrabbedWindow(void)
  1953. {
  1954. SDL_assert(!_this->grabbed_window || ((_this->grabbed_window->flags & SDL_WINDOW_INPUT_GRABBED) != 0));
  1955. return _this->grabbed_window;
  1956. }
  1957. void
  1958. SDL_OnWindowShown(SDL_Window * window)
  1959. {
  1960. SDL_OnWindowRestored(window);
  1961. }
  1962. void
  1963. SDL_OnWindowHidden(SDL_Window * window)
  1964. {
  1965. SDL_UpdateFullscreenMode(window, SDL_FALSE);
  1966. }
  1967. void
  1968. SDL_OnWindowResized(SDL_Window * window)
  1969. {
  1970. window->surface_valid = SDL_FALSE;
  1971. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SIZE_CHANGED, window->w, window->h);
  1972. }
  1973. void
  1974. SDL_OnWindowMinimized(SDL_Window * window)
  1975. {
  1976. SDL_UpdateFullscreenMode(window, SDL_FALSE);
  1977. }
  1978. void
  1979. SDL_OnWindowRestored(SDL_Window * window)
  1980. {
  1981. /*
  1982. * FIXME: Is this fine to just remove this, or should it be preserved just
  1983. * for the fullscreen case? In principle it seems like just hiding/showing
  1984. * windows shouldn't affect the stacking order; maybe the right fix is to
  1985. * re-decouple OnWindowShown and OnWindowRestored.
  1986. */
  1987. /*SDL_RaiseWindow(window);*/
  1988. if (FULLSCREEN_VISIBLE(window)) {
  1989. SDL_UpdateFullscreenMode(window, SDL_TRUE);
  1990. }
  1991. }
  1992. void
  1993. SDL_OnWindowEnter(SDL_Window * window)
  1994. {
  1995. if (_this->OnWindowEnter) {
  1996. _this->OnWindowEnter(_this, window);
  1997. }
  1998. }
  1999. void
  2000. SDL_OnWindowLeave(SDL_Window * window)
  2001. {
  2002. }
  2003. void
  2004. SDL_OnWindowFocusGained(SDL_Window * window)
  2005. {
  2006. SDL_Mouse *mouse = SDL_GetMouse();
  2007. if (window->gamma && _this->SetWindowGammaRamp) {
  2008. _this->SetWindowGammaRamp(_this, window, window->gamma);
  2009. }
  2010. if (mouse && mouse->relative_mode) {
  2011. SDL_SetMouseFocus(window);
  2012. SDL_WarpMouseInWindow(window, window->w/2, window->h/2);
  2013. }
  2014. SDL_UpdateWindowGrab(window);
  2015. }
  2016. static SDL_bool
  2017. ShouldMinimizeOnFocusLoss(SDL_Window * window)
  2018. {
  2019. const char *hint;
  2020. if (!(window->flags & SDL_WINDOW_FULLSCREEN) || window->is_destroying) {
  2021. return SDL_FALSE;
  2022. }
  2023. #ifdef __MACOSX__
  2024. if (Cocoa_IsWindowInFullscreenSpace(window)) {
  2025. return SDL_FALSE;
  2026. }
  2027. #endif
  2028. hint = SDL_GetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS);
  2029. if (hint) {
  2030. if (*hint == '0') {
  2031. return SDL_FALSE;
  2032. } else {
  2033. return SDL_TRUE;
  2034. }
  2035. }
  2036. return SDL_TRUE;
  2037. }
  2038. void
  2039. SDL_OnWindowFocusLost(SDL_Window * window)
  2040. {
  2041. if (window->gamma && _this->SetWindowGammaRamp) {
  2042. _this->SetWindowGammaRamp(_this, window, window->saved_gamma);
  2043. }
  2044. SDL_UpdateWindowGrab(window);
  2045. if (ShouldMinimizeOnFocusLoss(window)) {
  2046. SDL_MinimizeWindow(window);
  2047. }
  2048. }
  2049. /* !!! FIXME: is this different than SDL_GetKeyboardFocus()?
  2050. !!! FIXME: Also, SDL_GetKeyboardFocus() is O(1), this isn't. */
  2051. SDL_Window *
  2052. SDL_GetFocusWindow(void)
  2053. {
  2054. SDL_Window *window;
  2055. if (!_this) {
  2056. return NULL;
  2057. }
  2058. for (window = _this->windows; window; window = window->next) {
  2059. if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
  2060. return window;
  2061. }
  2062. }
  2063. return NULL;
  2064. }
  2065. void
  2066. SDL_DestroyWindow(SDL_Window * window)
  2067. {
  2068. SDL_VideoDisplay *display;
  2069. CHECK_WINDOW_MAGIC(window,);
  2070. window->is_destroying = SDL_TRUE;
  2071. /* Restore video mode, etc. */
  2072. SDL_HideWindow(window);
  2073. /* Make sure this window no longer has focus */
  2074. if (SDL_GetKeyboardFocus() == window) {
  2075. SDL_SetKeyboardFocus(NULL);
  2076. }
  2077. if (SDL_GetMouseFocus() == window) {
  2078. SDL_SetMouseFocus(NULL);
  2079. }
  2080. /* make no context current if this is the current context window. */
  2081. if (window->flags & SDL_WINDOW_OPENGL) {
  2082. if (_this->current_glwin == window) {
  2083. SDL_GL_MakeCurrent(window, NULL);
  2084. }
  2085. }
  2086. if (window->surface) {
  2087. window->surface->flags &= ~SDL_DONTFREE;
  2088. SDL_FreeSurface(window->surface);
  2089. }
  2090. if (_this->DestroyWindowFramebuffer) {
  2091. _this->DestroyWindowFramebuffer(_this, window);
  2092. }
  2093. if (_this->DestroyWindow) {
  2094. _this->DestroyWindow(_this, window);
  2095. }
  2096. if (window->flags & SDL_WINDOW_OPENGL) {
  2097. SDL_GL_UnloadLibrary();
  2098. }
  2099. display = SDL_GetDisplayForWindow(window);
  2100. if (display->fullscreen_window == window) {
  2101. display->fullscreen_window = NULL;
  2102. }
  2103. /* Now invalidate magic */
  2104. window->magic = NULL;
  2105. /* Free memory associated with the window */
  2106. SDL_free(window->title);
  2107. SDL_FreeSurface(window->icon);
  2108. SDL_free(window->gamma);
  2109. while (window->data) {
  2110. SDL_WindowUserData *data = window->data;
  2111. window->data = data->next;
  2112. SDL_free(data->name);
  2113. SDL_free(data);
  2114. }
  2115. /* Unlink the window from the list */
  2116. if (window->next) {
  2117. window->next->prev = window->prev;
  2118. }
  2119. if (window->prev) {
  2120. window->prev->next = window->next;
  2121. } else {
  2122. _this->windows = window->next;
  2123. }
  2124. SDL_free(window);
  2125. }
  2126. SDL_bool
  2127. SDL_IsScreenSaverEnabled()
  2128. {
  2129. if (!_this) {
  2130. return SDL_TRUE;
  2131. }
  2132. return _this->suspend_screensaver ? SDL_FALSE : SDL_TRUE;
  2133. }
  2134. void
  2135. SDL_EnableScreenSaver()
  2136. {
  2137. if (!_this) {
  2138. return;
  2139. }
  2140. if (!_this->suspend_screensaver) {
  2141. return;
  2142. }
  2143. _this->suspend_screensaver = SDL_FALSE;
  2144. if (_this->SuspendScreenSaver) {
  2145. _this->SuspendScreenSaver(_this);
  2146. }
  2147. }
  2148. void
  2149. SDL_DisableScreenSaver()
  2150. {
  2151. if (!_this) {
  2152. return;
  2153. }
  2154. if (_this->suspend_screensaver) {
  2155. return;
  2156. }
  2157. _this->suspend_screensaver = SDL_TRUE;
  2158. if (_this->SuspendScreenSaver) {
  2159. _this->SuspendScreenSaver(_this);
  2160. }
  2161. }
  2162. void
  2163. SDL_VideoQuit(void)
  2164. {
  2165. int i, j;
  2166. if (!_this) {
  2167. return;
  2168. }
  2169. /* Halt event processing before doing anything else */
  2170. SDL_TouchQuit();
  2171. SDL_MouseQuit();
  2172. SDL_KeyboardQuit();
  2173. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  2174. SDL_EnableScreenSaver();
  2175. /* Clean up the system video */
  2176. while (_this->windows) {
  2177. SDL_DestroyWindow(_this->windows);
  2178. }
  2179. _this->VideoQuit(_this);
  2180. for (i = 0; i < _this->num_displays; ++i) {
  2181. SDL_VideoDisplay *display = &_this->displays[i];
  2182. for (j = display->num_display_modes; j--;) {
  2183. SDL_free(display->display_modes[j].driverdata);
  2184. display->display_modes[j].driverdata = NULL;
  2185. }
  2186. SDL_free(display->display_modes);
  2187. display->display_modes = NULL;
  2188. SDL_free(display->desktop_mode.driverdata);
  2189. display->desktop_mode.driverdata = NULL;
  2190. SDL_free(display->driverdata);
  2191. display->driverdata = NULL;
  2192. }
  2193. if (_this->displays) {
  2194. for (i = 0; i < _this->num_displays; ++i) {
  2195. SDL_free(_this->displays[i].name);
  2196. }
  2197. SDL_free(_this->displays);
  2198. _this->displays = NULL;
  2199. _this->num_displays = 0;
  2200. }
  2201. SDL_free(_this->clipboard_text);
  2202. _this->clipboard_text = NULL;
  2203. _this->free(_this);
  2204. _this = NULL;
  2205. }
  2206. int
  2207. SDL_GL_LoadLibrary(const char *path)
  2208. {
  2209. int retval;
  2210. if (!_this) {
  2211. return SDL_UninitializedVideo();
  2212. }
  2213. if (_this->gl_config.driver_loaded) {
  2214. if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
  2215. return SDL_SetError("OpenGL library already loaded");
  2216. }
  2217. retval = 0;
  2218. } else {
  2219. if (!_this->GL_LoadLibrary) {
  2220. return SDL_SetError("No dynamic GL support in video driver");
  2221. }
  2222. retval = _this->GL_LoadLibrary(_this, path);
  2223. }
  2224. if (retval == 0) {
  2225. ++_this->gl_config.driver_loaded;
  2226. } else {
  2227. if (_this->GL_UnloadLibrary) {
  2228. _this->GL_UnloadLibrary(_this);
  2229. }
  2230. }
  2231. return (retval);
  2232. }
  2233. void *
  2234. SDL_GL_GetProcAddress(const char *proc)
  2235. {
  2236. void *func;
  2237. if (!_this) {
  2238. SDL_UninitializedVideo();
  2239. return NULL;
  2240. }
  2241. func = NULL;
  2242. if (_this->GL_GetProcAddress) {
  2243. if (_this->gl_config.driver_loaded) {
  2244. func = _this->GL_GetProcAddress(_this, proc);
  2245. } else {
  2246. SDL_SetError("No GL driver has been loaded");
  2247. }
  2248. } else {
  2249. SDL_SetError("No dynamic GL support in video driver");
  2250. }
  2251. return func;
  2252. }
  2253. void
  2254. SDL_GL_UnloadLibrary(void)
  2255. {
  2256. if (!_this) {
  2257. SDL_UninitializedVideo();
  2258. return;
  2259. }
  2260. if (_this->gl_config.driver_loaded > 0) {
  2261. if (--_this->gl_config.driver_loaded > 0) {
  2262. return;
  2263. }
  2264. if (_this->GL_UnloadLibrary) {
  2265. _this->GL_UnloadLibrary(_this);
  2266. }
  2267. }
  2268. }
  2269. static SDL_INLINE SDL_bool
  2270. isAtLeastGL3(const char *verstr)
  2271. {
  2272. return (verstr && (SDL_atoi(verstr) >= 3));
  2273. }
  2274. SDL_bool
  2275. SDL_GL_ExtensionSupported(const char *extension)
  2276. {
  2277. #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
  2278. const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
  2279. const char *extensions;
  2280. const char *start;
  2281. const char *where, *terminator;
  2282. /* Extension names should not have spaces. */
  2283. where = SDL_strchr(extension, ' ');
  2284. if (where || *extension == '\0') {
  2285. return SDL_FALSE;
  2286. }
  2287. /* See if there's an environment variable override */
  2288. start = SDL_getenv(extension);
  2289. if (start && *start == '0') {
  2290. return SDL_FALSE;
  2291. }
  2292. /* Lookup the available extensions */
  2293. glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
  2294. if (!glGetStringFunc) {
  2295. return SDL_FALSE;
  2296. }
  2297. if (isAtLeastGL3((const char *) glGetStringFunc(GL_VERSION))) {
  2298. const GLubyte *(APIENTRY * glGetStringiFunc) (GLenum, GLuint);
  2299. void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
  2300. GLint num_exts = 0;
  2301. GLint i;
  2302. glGetStringiFunc = SDL_GL_GetProcAddress("glGetStringi");
  2303. glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
  2304. if ((!glGetStringiFunc) || (!glGetIntegervFunc)) {
  2305. return SDL_FALSE;
  2306. }
  2307. #ifndef GL_NUM_EXTENSIONS
  2308. #define GL_NUM_EXTENSIONS 0x821D
  2309. #endif
  2310. glGetIntegervFunc(GL_NUM_EXTENSIONS, &num_exts);
  2311. for (i = 0; i < num_exts; i++) {
  2312. const char *thisext = (const char *) glGetStringiFunc(GL_EXTENSIONS, i);
  2313. if (SDL_strcmp(thisext, extension) == 0) {
  2314. return SDL_TRUE;
  2315. }
  2316. }
  2317. return SDL_FALSE;
  2318. }
  2319. /* Try the old way with glGetString(GL_EXTENSIONS) ... */
  2320. extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
  2321. if (!extensions) {
  2322. return SDL_FALSE;
  2323. }
  2324. /*
  2325. * It takes a bit of care to be fool-proof about parsing the OpenGL
  2326. * extensions string. Don't be fooled by sub-strings, etc.
  2327. */
  2328. start = extensions;
  2329. for (;;) {
  2330. where = SDL_strstr(start, extension);
  2331. if (!where)
  2332. break;
  2333. terminator = where + SDL_strlen(extension);
  2334. if (where == start || *(where - 1) == ' ')
  2335. if (*terminator == ' ' || *terminator == '\0')
  2336. return SDL_TRUE;
  2337. start = terminator;
  2338. }
  2339. return SDL_FALSE;
  2340. #else
  2341. return SDL_FALSE;
  2342. #endif
  2343. }
  2344. void
  2345. SDL_GL_ResetAttributes()
  2346. {
  2347. if (!_this) {
  2348. return;
  2349. }
  2350. _this->gl_config.red_size = 3;
  2351. _this->gl_config.green_size = 3;
  2352. _this->gl_config.blue_size = 2;
  2353. _this->gl_config.alpha_size = 0;
  2354. _this->gl_config.buffer_size = 0;
  2355. _this->gl_config.depth_size = 16;
  2356. _this->gl_config.stencil_size = 0;
  2357. _this->gl_config.double_buffer = 1;
  2358. _this->gl_config.accum_red_size = 0;
  2359. _this->gl_config.accum_green_size = 0;
  2360. _this->gl_config.accum_blue_size = 0;
  2361. _this->gl_config.accum_alpha_size = 0;
  2362. _this->gl_config.stereo = 0;
  2363. _this->gl_config.multisamplebuffers = 0;
  2364. _this->gl_config.multisamplesamples = 0;
  2365. _this->gl_config.retained_backing = 1;
  2366. _this->gl_config.accelerated = -1; /* accelerated or not, both are fine */
  2367. _this->gl_config.profile_mask = 0;
  2368. #if SDL_VIDEO_OPENGL
  2369. _this->gl_config.major_version = 2;
  2370. _this->gl_config.minor_version = 1;
  2371. #elif SDL_VIDEO_OPENGL_ES2
  2372. _this->gl_config.major_version = 2;
  2373. _this->gl_config.minor_version = 0;
  2374. _this->gl_config.profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
  2375. #elif SDL_VIDEO_OPENGL_ES
  2376. _this->gl_config.major_version = 1;
  2377. _this->gl_config.minor_version = 1;
  2378. _this->gl_config.profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
  2379. #endif
  2380. _this->gl_config.flags = 0;
  2381. _this->gl_config.framebuffer_srgb_capable = 0;
  2382. _this->gl_config.release_behavior = SDL_GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH;
  2383. _this->gl_config.share_with_current_context = 0;
  2384. }
  2385. int
  2386. SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  2387. {
  2388. #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
  2389. int retval;
  2390. if (!_this) {
  2391. return SDL_UninitializedVideo();
  2392. }
  2393. retval = 0;
  2394. switch (attr) {
  2395. case SDL_GL_RED_SIZE:
  2396. _this->gl_config.red_size = value;
  2397. break;
  2398. case SDL_GL_GREEN_SIZE:
  2399. _this->gl_config.green_size = value;
  2400. break;
  2401. case SDL_GL_BLUE_SIZE:
  2402. _this->gl_config.blue_size = value;
  2403. break;
  2404. case SDL_GL_ALPHA_SIZE:
  2405. _this->gl_config.alpha_size = value;
  2406. break;
  2407. case SDL_GL_DOUBLEBUFFER:
  2408. _this->gl_config.double_buffer = value;
  2409. break;
  2410. case SDL_GL_BUFFER_SIZE:
  2411. _this->gl_config.buffer_size = value;
  2412. break;
  2413. case SDL_GL_DEPTH_SIZE:
  2414. _this->gl_config.depth_size = value;
  2415. break;
  2416. case SDL_GL_STENCIL_SIZE:
  2417. _this->gl_config.stencil_size = value;
  2418. break;
  2419. case SDL_GL_ACCUM_RED_SIZE:
  2420. _this->gl_config.accum_red_size = value;
  2421. break;
  2422. case SDL_GL_ACCUM_GREEN_SIZE:
  2423. _this->gl_config.accum_green_size = value;
  2424. break;
  2425. case SDL_GL_ACCUM_BLUE_SIZE:
  2426. _this->gl_config.accum_blue_size = value;
  2427. break;
  2428. case SDL_GL_ACCUM_ALPHA_SIZE:
  2429. _this->gl_config.accum_alpha_size = value;
  2430. break;
  2431. case SDL_GL_STEREO:
  2432. _this->gl_config.stereo = value;
  2433. break;
  2434. case SDL_GL_MULTISAMPLEBUFFERS:
  2435. _this->gl_config.multisamplebuffers = value;
  2436. break;
  2437. case SDL_GL_MULTISAMPLESAMPLES:
  2438. _this->gl_config.multisamplesamples = value;
  2439. break;
  2440. case SDL_GL_ACCELERATED_VISUAL:
  2441. _this->gl_config.accelerated = value;
  2442. break;
  2443. case SDL_GL_RETAINED_BACKING:
  2444. _this->gl_config.retained_backing = value;
  2445. break;
  2446. case SDL_GL_CONTEXT_MAJOR_VERSION:
  2447. _this->gl_config.major_version = value;
  2448. break;
  2449. case SDL_GL_CONTEXT_MINOR_VERSION:
  2450. _this->gl_config.minor_version = value;
  2451. break;
  2452. case SDL_GL_CONTEXT_EGL:
  2453. /* FIXME: SDL_GL_CONTEXT_EGL to be deprecated in SDL 2.1 */
  2454. if (value != 0) {
  2455. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
  2456. } else {
  2457. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0);
  2458. };
  2459. break;
  2460. case SDL_GL_CONTEXT_FLAGS:
  2461. if (value & ~(SDL_GL_CONTEXT_DEBUG_FLAG |
  2462. SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG |
  2463. SDL_GL_CONTEXT_ROBUST_ACCESS_FLAG |
  2464. SDL_GL_CONTEXT_RESET_ISOLATION_FLAG)) {
  2465. retval = SDL_SetError("Unknown OpenGL context flag %d", value);
  2466. break;
  2467. }
  2468. _this->gl_config.flags = value;
  2469. break;
  2470. case SDL_GL_CONTEXT_PROFILE_MASK:
  2471. if (value != 0 &&
  2472. value != SDL_GL_CONTEXT_PROFILE_CORE &&
  2473. value != SDL_GL_CONTEXT_PROFILE_COMPATIBILITY &&
  2474. value != SDL_GL_CONTEXT_PROFILE_ES) {
  2475. retval = SDL_SetError("Unknown OpenGL context profile %d", value);
  2476. break;
  2477. }
  2478. _this->gl_config.profile_mask = value;
  2479. break;
  2480. case SDL_GL_SHARE_WITH_CURRENT_CONTEXT:
  2481. _this->gl_config.share_with_current_context = value;
  2482. break;
  2483. case SDL_GL_FRAMEBUFFER_SRGB_CAPABLE:
  2484. _this->gl_config.framebuffer_srgb_capable = value;
  2485. break;
  2486. case SDL_GL_CONTEXT_RELEASE_BEHAVIOR:
  2487. _this->gl_config.release_behavior = value;
  2488. break;
  2489. default:
  2490. retval = SDL_SetError("Unknown OpenGL attribute");
  2491. break;
  2492. }
  2493. return retval;
  2494. #else
  2495. return SDL_Unsupported();
  2496. #endif /* SDL_VIDEO_OPENGL */
  2497. }
  2498. int
  2499. SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
  2500. {
  2501. #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
  2502. GLenum (APIENTRY *glGetErrorFunc) (void);
  2503. GLenum attrib = 0;
  2504. GLenum error = 0;
  2505. /*
  2506. * Some queries in Core Profile desktop OpenGL 3+ contexts require
  2507. * glGetFramebufferAttachmentParameteriv instead of glGetIntegerv. Note that
  2508. * the enums we use for the former function don't exist in OpenGL ES 2, and
  2509. * the function itself doesn't exist prior to OpenGL 3 and OpenGL ES 2.
  2510. */
  2511. #if SDL_VIDEO_OPENGL
  2512. const GLubyte *(APIENTRY *glGetStringFunc) (GLenum name);
  2513. void (APIENTRY *glGetFramebufferAttachmentParameterivFunc) (GLenum target, GLenum attachment, GLenum pname, GLint* params);
  2514. GLenum attachment = GL_BACK_LEFT;
  2515. GLenum attachmentattrib = 0;
  2516. #endif
  2517. /* Clear value in any case */
  2518. *value = 0;
  2519. switch (attr) {
  2520. case SDL_GL_RED_SIZE:
  2521. #if SDL_VIDEO_OPENGL
  2522. attachmentattrib = GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE;
  2523. #endif
  2524. attrib = GL_RED_BITS;
  2525. break;
  2526. case SDL_GL_BLUE_SIZE:
  2527. #if SDL_VIDEO_OPENGL
  2528. attachmentattrib = GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE;
  2529. #endif
  2530. attrib = GL_BLUE_BITS;
  2531. break;
  2532. case SDL_GL_GREEN_SIZE:
  2533. #if SDL_VIDEO_OPENGL
  2534. attachmentattrib = GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE;
  2535. #endif
  2536. attrib = GL_GREEN_BITS;
  2537. break;
  2538. case SDL_GL_ALPHA_SIZE:
  2539. #if SDL_VIDEO_OPENGL
  2540. attachmentattrib = GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE;
  2541. #endif
  2542. attrib = GL_ALPHA_BITS;
  2543. break;
  2544. case SDL_GL_DOUBLEBUFFER:
  2545. #if SDL_VIDEO_OPENGL
  2546. attrib = GL_DOUBLEBUFFER;
  2547. break;
  2548. #else
  2549. /* OpenGL ES 1.0 and above specifications have EGL_SINGLE_BUFFER */
  2550. /* parameter which switches double buffer to single buffer. OpenGL ES */
  2551. /* SDL driver must set proper value after initialization */
  2552. *value = _this->gl_config.double_buffer;
  2553. return 0;
  2554. #endif
  2555. case SDL_GL_DEPTH_SIZE:
  2556. #if SDL_VIDEO_OPENGL
  2557. attachment = GL_DEPTH;
  2558. attachmentattrib = GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE;
  2559. #endif
  2560. attrib = GL_DEPTH_BITS;
  2561. break;
  2562. case SDL_GL_STENCIL_SIZE:
  2563. #if SDL_VIDEO_OPENGL
  2564. attachment = GL_STENCIL;
  2565. attachmentattrib = GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE;
  2566. #endif
  2567. attrib = GL_STENCIL_BITS;
  2568. break;
  2569. #if SDL_VIDEO_OPENGL
  2570. case SDL_GL_ACCUM_RED_SIZE:
  2571. attrib = GL_ACCUM_RED_BITS;
  2572. break;
  2573. case SDL_GL_ACCUM_GREEN_SIZE:
  2574. attrib = GL_ACCUM_GREEN_BITS;
  2575. break;
  2576. case SDL_GL_ACCUM_BLUE_SIZE:
  2577. attrib = GL_ACCUM_BLUE_BITS;
  2578. break;
  2579. case SDL_GL_ACCUM_ALPHA_SIZE:
  2580. attrib = GL_ACCUM_ALPHA_BITS;
  2581. break;
  2582. case SDL_GL_STEREO:
  2583. attrib = GL_STEREO;
  2584. break;
  2585. #else
  2586. case SDL_GL_ACCUM_RED_SIZE:
  2587. case SDL_GL_ACCUM_GREEN_SIZE:
  2588. case SDL_GL_ACCUM_BLUE_SIZE:
  2589. case SDL_GL_ACCUM_ALPHA_SIZE:
  2590. case SDL_GL_STEREO:
  2591. /* none of these are supported in OpenGL ES */
  2592. *value = 0;
  2593. return 0;
  2594. #endif
  2595. case SDL_GL_MULTISAMPLEBUFFERS:
  2596. attrib = GL_SAMPLE_BUFFERS;
  2597. break;
  2598. case SDL_GL_MULTISAMPLESAMPLES:
  2599. attrib = GL_SAMPLES;
  2600. break;
  2601. case SDL_GL_CONTEXT_RELEASE_BEHAVIOR:
  2602. #if SDL_VIDEO_OPENGL
  2603. attrib = GL_CONTEXT_RELEASE_BEHAVIOR;
  2604. #else
  2605. attrib = GL_CONTEXT_RELEASE_BEHAVIOR_KHR;
  2606. #endif
  2607. break;
  2608. case SDL_GL_BUFFER_SIZE:
  2609. {
  2610. int rsize = 0, gsize = 0, bsize = 0, asize = 0;
  2611. /* There doesn't seem to be a single flag in OpenGL for this! */
  2612. if (SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &rsize) < 0) {
  2613. return -1;
  2614. }
  2615. if (SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &gsize) < 0) {
  2616. return -1;
  2617. }
  2618. if (SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &bsize) < 0) {
  2619. return -1;
  2620. }
  2621. if (SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &asize) < 0) {
  2622. return -1;
  2623. }
  2624. *value = rsize + gsize + bsize + asize;
  2625. return 0;
  2626. }
  2627. case SDL_GL_ACCELERATED_VISUAL:
  2628. {
  2629. /* FIXME: How do we get this information? */
  2630. *value = (_this->gl_config.accelerated != 0);
  2631. return 0;
  2632. }
  2633. case SDL_GL_RETAINED_BACKING:
  2634. {
  2635. *value = _this->gl_config.retained_backing;
  2636. return 0;
  2637. }
  2638. case SDL_GL_CONTEXT_MAJOR_VERSION:
  2639. {
  2640. *value = _this->gl_config.major_version;
  2641. return 0;
  2642. }
  2643. case SDL_GL_CONTEXT_MINOR_VERSION:
  2644. {
  2645. *value = _this->gl_config.minor_version;
  2646. return 0;
  2647. }
  2648. case SDL_GL_CONTEXT_EGL:
  2649. /* FIXME: SDL_GL_CONTEXT_EGL to be deprecated in SDL 2.1 */
  2650. {
  2651. if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
  2652. *value = 1;
  2653. }
  2654. else {
  2655. *value = 0;
  2656. }
  2657. return 0;
  2658. }
  2659. case SDL_GL_CONTEXT_FLAGS:
  2660. {
  2661. *value = _this->gl_config.flags;
  2662. return 0;
  2663. }
  2664. case SDL_GL_CONTEXT_PROFILE_MASK:
  2665. {
  2666. *value = _this->gl_config.profile_mask;
  2667. return 0;
  2668. }
  2669. case SDL_GL_SHARE_WITH_CURRENT_CONTEXT:
  2670. {
  2671. *value = _this->gl_config.share_with_current_context;
  2672. return 0;
  2673. }
  2674. case SDL_GL_FRAMEBUFFER_SRGB_CAPABLE:
  2675. {
  2676. *value = _this->gl_config.framebuffer_srgb_capable;
  2677. return 0;
  2678. }
  2679. default:
  2680. return SDL_SetError("Unknown OpenGL attribute");
  2681. }
  2682. #if SDL_VIDEO_OPENGL
  2683. glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
  2684. if (!glGetStringFunc) {
  2685. return SDL_SetError("Failed getting OpenGL glGetString entry point");
  2686. }
  2687. if (attachmentattrib && isAtLeastGL3((const char *) glGetStringFunc(GL_VERSION))) {
  2688. glGetFramebufferAttachmentParameterivFunc = SDL_GL_GetProcAddress("glGetFramebufferAttachmentParameteriv");
  2689. if (glGetFramebufferAttachmentParameterivFunc) {
  2690. glGetFramebufferAttachmentParameterivFunc(GL_FRAMEBUFFER, attachment, attachmentattrib, (GLint *) value);
  2691. } else {
  2692. return SDL_SetError("Failed getting OpenGL glGetFramebufferAttachmentParameteriv entry point");
  2693. }
  2694. } else
  2695. #endif
  2696. {
  2697. void (APIENTRY *glGetIntegervFunc) (GLenum pname, GLint * params);
  2698. glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
  2699. if (glGetIntegervFunc) {
  2700. glGetIntegervFunc(attrib, (GLint *) value);
  2701. } else {
  2702. return SDL_SetError("Failed getting OpenGL glGetIntegerv entry point");
  2703. }
  2704. }
  2705. glGetErrorFunc = SDL_GL_GetProcAddress("glGetError");
  2706. if (!glGetErrorFunc) {
  2707. return SDL_SetError("Failed getting OpenGL glGetError entry point");
  2708. }
  2709. error = glGetErrorFunc();
  2710. if (error != GL_NO_ERROR) {
  2711. if (error == GL_INVALID_ENUM) {
  2712. return SDL_SetError("OpenGL error: GL_INVALID_ENUM");
  2713. } else if (error == GL_INVALID_VALUE) {
  2714. return SDL_SetError("OpenGL error: GL_INVALID_VALUE");
  2715. }
  2716. return SDL_SetError("OpenGL error: %08X", error);
  2717. }
  2718. return 0;
  2719. #else
  2720. return SDL_Unsupported();
  2721. #endif /* SDL_VIDEO_OPENGL */
  2722. }
  2723. SDL_GLContext
  2724. SDL_GL_CreateContext(SDL_Window * window)
  2725. {
  2726. SDL_GLContext ctx = NULL;
  2727. CHECK_WINDOW_MAGIC(window, NULL);
  2728. if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2729. SDL_SetError("The specified window isn't an OpenGL window");
  2730. return NULL;
  2731. }
  2732. ctx = _this->GL_CreateContext(_this, window);
  2733. /* Creating a context is assumed to make it current in the SDL driver. */
  2734. if (ctx) {
  2735. _this->current_glwin = window;
  2736. _this->current_glctx = ctx;
  2737. SDL_TLSSet(_this->current_glwin_tls, window, NULL);
  2738. SDL_TLSSet(_this->current_glctx_tls, ctx, NULL);
  2739. }
  2740. return ctx;
  2741. }
  2742. int
  2743. SDL_GL_MakeCurrent(SDL_Window * window, SDL_GLContext ctx)
  2744. {
  2745. int retval;
  2746. if (window == SDL_GL_GetCurrentWindow() &&
  2747. ctx == SDL_GL_GetCurrentContext()) {
  2748. /* We're already current. */
  2749. return 0;
  2750. }
  2751. if (!ctx) {
  2752. window = NULL;
  2753. } else {
  2754. CHECK_WINDOW_MAGIC(window, -1);
  2755. if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2756. return SDL_SetError("The specified window isn't an OpenGL window");
  2757. }
  2758. }
  2759. retval = _this->GL_MakeCurrent(_this, window, ctx);
  2760. if (retval == 0) {
  2761. _this->current_glwin = window;
  2762. _this->current_glctx = ctx;
  2763. SDL_TLSSet(_this->current_glwin_tls, window, NULL);
  2764. SDL_TLSSet(_this->current_glctx_tls, ctx, NULL);
  2765. }
  2766. return retval;
  2767. }
  2768. SDL_Window *
  2769. SDL_GL_GetCurrentWindow(void)
  2770. {
  2771. if (!_this) {
  2772. SDL_UninitializedVideo();
  2773. return NULL;
  2774. }
  2775. return (SDL_Window *)SDL_TLSGet(_this->current_glwin_tls);
  2776. }
  2777. SDL_GLContext
  2778. SDL_GL_GetCurrentContext(void)
  2779. {
  2780. if (!_this) {
  2781. SDL_UninitializedVideo();
  2782. return NULL;
  2783. }
  2784. return (SDL_GLContext)SDL_TLSGet(_this->current_glctx_tls);
  2785. }
  2786. void SDL_GL_GetDrawableSize(SDL_Window * window, int *w, int *h)
  2787. {
  2788. CHECK_WINDOW_MAGIC(window,);
  2789. if (_this->GL_GetDrawableSize) {
  2790. _this->GL_GetDrawableSize(_this, window, w, h);
  2791. } else {
  2792. SDL_GetWindowSize(window, w, h);
  2793. }
  2794. }
  2795. int
  2796. SDL_GL_SetSwapInterval(int interval)
  2797. {
  2798. if (!_this) {
  2799. return SDL_UninitializedVideo();
  2800. } else if (SDL_GL_GetCurrentContext() == NULL) {
  2801. return SDL_SetError("No OpenGL context has been made current");
  2802. } else if (_this->GL_SetSwapInterval) {
  2803. return _this->GL_SetSwapInterval(_this, interval);
  2804. } else {
  2805. return SDL_SetError("Setting the swap interval is not supported");
  2806. }
  2807. }
  2808. int
  2809. SDL_GL_GetSwapInterval(void)
  2810. {
  2811. if (!_this) {
  2812. return 0;
  2813. } else if (SDL_GL_GetCurrentContext() == NULL) {
  2814. return 0;
  2815. } else if (_this->GL_GetSwapInterval) {
  2816. return _this->GL_GetSwapInterval(_this);
  2817. } else {
  2818. return 0;
  2819. }
  2820. }
  2821. void
  2822. SDL_GL_SwapWindow(SDL_Window * window)
  2823. {
  2824. CHECK_WINDOW_MAGIC(window,);
  2825. if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2826. SDL_SetError("The specified window isn't an OpenGL window");
  2827. return;
  2828. }
  2829. if (SDL_GL_GetCurrentWindow() != window) {
  2830. SDL_SetError("The specified window has not been made current");
  2831. return;
  2832. }
  2833. _this->GL_SwapWindow(_this, window);
  2834. }
  2835. void
  2836. SDL_GL_DeleteContext(SDL_GLContext context)
  2837. {
  2838. if (!_this || !context) {
  2839. return;
  2840. }
  2841. if (SDL_GL_GetCurrentContext() == context) {
  2842. SDL_GL_MakeCurrent(NULL, NULL);
  2843. }
  2844. _this->GL_DeleteContext(_this, context);
  2845. }
  2846. #if 0 /* FIXME */
  2847. /*
  2848. * Utility function used by SDL_WM_SetIcon(); flags & 1 for color key, flags
  2849. * & 2 for alpha channel.
  2850. */
  2851. static void
  2852. CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  2853. {
  2854. int x, y;
  2855. Uint32 colorkey;
  2856. #define SET_MASKBIT(icon, x, y, mask) \
  2857. mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  2858. colorkey = icon->format->colorkey;
  2859. switch (icon->format->BytesPerPixel) {
  2860. case 1:
  2861. {
  2862. Uint8 *pixels;
  2863. for (y = 0; y < icon->h; ++y) {
  2864. pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  2865. for (x = 0; x < icon->w; ++x) {
  2866. if (*pixels++ == colorkey) {
  2867. SET_MASKBIT(icon, x, y, mask);
  2868. }
  2869. }
  2870. }
  2871. }
  2872. break;
  2873. case 2:
  2874. {
  2875. Uint16 *pixels;
  2876. for (y = 0; y < icon->h; ++y) {
  2877. pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  2878. for (x = 0; x < icon->w; ++x) {
  2879. if ((flags & 1) && *pixels == colorkey) {
  2880. SET_MASKBIT(icon, x, y, mask);
  2881. } else if ((flags & 2)
  2882. && (*pixels & icon->format->Amask) == 0) {
  2883. SET_MASKBIT(icon, x, y, mask);
  2884. }
  2885. pixels++;
  2886. }
  2887. }
  2888. }
  2889. break;
  2890. case 4:
  2891. {
  2892. Uint32 *pixels;
  2893. for (y = 0; y < icon->h; ++y) {
  2894. pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  2895. for (x = 0; x < icon->w; ++x) {
  2896. if ((flags & 1) && *pixels == colorkey) {
  2897. SET_MASKBIT(icon, x, y, mask);
  2898. } else if ((flags & 2)
  2899. && (*pixels & icon->format->Amask) == 0) {
  2900. SET_MASKBIT(icon, x, y, mask);
  2901. }
  2902. pixels++;
  2903. }
  2904. }
  2905. }
  2906. break;
  2907. }
  2908. }
  2909. /*
  2910. * Sets the window manager icon for the display window.
  2911. */
  2912. void
  2913. SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  2914. {
  2915. if (icon && _this->SetIcon) {
  2916. /* Generate a mask if necessary, and create the icon! */
  2917. if (mask == NULL) {
  2918. int mask_len = icon->h * (icon->w + 7) / 8;
  2919. int flags = 0;
  2920. mask = (Uint8 *) SDL_malloc(mask_len);
  2921. if (mask == NULL) {
  2922. return;
  2923. }
  2924. SDL_memset(mask, ~0, mask_len);
  2925. if (icon->flags & SDL_SRCCOLORKEY)
  2926. flags |= 1;
  2927. if (icon->flags & SDL_SRCALPHA)
  2928. flags |= 2;
  2929. if (flags) {
  2930. CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  2931. }
  2932. _this->SetIcon(_this, icon, mask);
  2933. SDL_free(mask);
  2934. } else {
  2935. _this->SetIcon(_this, icon, mask);
  2936. }
  2937. }
  2938. }
  2939. #endif
  2940. SDL_bool
  2941. SDL_GetWindowWMInfo(SDL_Window * window, struct SDL_SysWMinfo *info)
  2942. {
  2943. CHECK_WINDOW_MAGIC(window, SDL_FALSE);
  2944. if (!info) {
  2945. SDL_InvalidParamError("info");
  2946. return SDL_FALSE;
  2947. }
  2948. info->subsystem = SDL_SYSWM_UNKNOWN;
  2949. if (!_this->GetWindowWMInfo) {
  2950. SDL_Unsupported();
  2951. return SDL_FALSE;
  2952. }
  2953. return (_this->GetWindowWMInfo(_this, window, info));
  2954. }
  2955. void
  2956. SDL_StartTextInput(void)
  2957. {
  2958. SDL_Window *window;
  2959. /* First, enable text events */
  2960. SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);
  2961. SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE);
  2962. /* Then show the on-screen keyboard, if any */
  2963. window = SDL_GetFocusWindow();
  2964. if (window && _this && _this->ShowScreenKeyboard) {
  2965. _this->ShowScreenKeyboard(_this, window);
  2966. }
  2967. /* Finally start the text input system */
  2968. if (_this && _this->StartTextInput) {
  2969. _this->StartTextInput(_this);
  2970. }
  2971. }
  2972. SDL_bool
  2973. SDL_IsTextInputActive(void)
  2974. {
  2975. return (SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE);
  2976. }
  2977. void
  2978. SDL_StopTextInput(void)
  2979. {
  2980. SDL_Window *window;
  2981. /* Stop the text input system */
  2982. if (_this && _this->StopTextInput) {
  2983. _this->StopTextInput(_this);
  2984. }
  2985. /* Hide the on-screen keyboard, if any */
  2986. window = SDL_GetFocusWindow();
  2987. if (window && _this && _this->HideScreenKeyboard) {
  2988. _this->HideScreenKeyboard(_this, window);
  2989. }
  2990. /* Finally disable text events */
  2991. SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
  2992. SDL_EventState(SDL_TEXTEDITING, SDL_DISABLE);
  2993. }
  2994. void
  2995. SDL_SetTextInputRect(SDL_Rect *rect)
  2996. {
  2997. if (_this && _this->SetTextInputRect) {
  2998. _this->SetTextInputRect(_this, rect);
  2999. }
  3000. }
  3001. SDL_bool
  3002. SDL_HasScreenKeyboardSupport(void)
  3003. {
  3004. if (_this && _this->HasScreenKeyboardSupport) {
  3005. return _this->HasScreenKeyboardSupport(_this);
  3006. }
  3007. return SDL_FALSE;
  3008. }
  3009. SDL_bool
  3010. SDL_IsScreenKeyboardShown(SDL_Window *window)
  3011. {
  3012. if (window && _this && _this->IsScreenKeyboardShown) {
  3013. return _this->IsScreenKeyboardShown(_this, window);
  3014. }
  3015. return SDL_FALSE;
  3016. }
  3017. #if SDL_VIDEO_DRIVER_ANDROID
  3018. #include "android/SDL_androidmessagebox.h"
  3019. #endif
  3020. #if SDL_VIDEO_DRIVER_WINDOWS
  3021. #include "windows/SDL_windowsmessagebox.h"
  3022. #endif
  3023. #if SDL_VIDEO_DRIVER_WINRT
  3024. #include "winrt/SDL_winrtmessagebox.h"
  3025. #endif
  3026. #if SDL_VIDEO_DRIVER_COCOA
  3027. #include "cocoa/SDL_cocoamessagebox.h"
  3028. #endif
  3029. #if SDL_VIDEO_DRIVER_UIKIT
  3030. #include "uikit/SDL_uikitmessagebox.h"
  3031. #endif
  3032. #if SDL_VIDEO_DRIVER_X11
  3033. #include "x11/SDL_x11messagebox.h"
  3034. #endif
  3035. // This function will be unused if none of the above video drivers are present.
  3036. SDL_UNUSED static SDL_bool SDL_MessageboxValidForDriver(const SDL_MessageBoxData *messageboxdata, SDL_SYSWM_TYPE drivertype)
  3037. {
  3038. SDL_SysWMinfo info;
  3039. SDL_Window *window = messageboxdata->window;
  3040. if (!window) {
  3041. return SDL_TRUE;
  3042. }
  3043. SDL_VERSION(&info.version);
  3044. if (!SDL_GetWindowWMInfo(window, &info)) {
  3045. return SDL_TRUE;
  3046. } else {
  3047. return (info.subsystem == drivertype);
  3048. }
  3049. }
  3050. int
  3051. SDL_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
  3052. {
  3053. int dummybutton;
  3054. int retval = -1;
  3055. SDL_bool relative_mode;
  3056. int show_cursor_prev;
  3057. SDL_bool mouse_captured;
  3058. SDL_Window *current_window;
  3059. if (!messageboxdata) {
  3060. return SDL_InvalidParamError("messageboxdata");
  3061. }
  3062. current_window = SDL_GetKeyboardFocus();
  3063. mouse_captured = current_window && ((SDL_GetWindowFlags(current_window) & SDL_WINDOW_MOUSE_CAPTURE) != 0);
  3064. relative_mode = SDL_GetRelativeMouseMode();
  3065. SDL_CaptureMouse(SDL_FALSE);
  3066. SDL_SetRelativeMouseMode(SDL_FALSE);
  3067. show_cursor_prev = SDL_ShowCursor(1);
  3068. SDL_ResetKeyboard();
  3069. if (!buttonid) {
  3070. buttonid = &dummybutton;
  3071. }
  3072. if (_this && _this->ShowMessageBox) {
  3073. retval = _this->ShowMessageBox(_this, messageboxdata, buttonid);
  3074. }
  3075. /* It's completely fine to call this function before video is initialized */
  3076. #if SDL_VIDEO_DRIVER_ANDROID
  3077. if (retval == -1 &&
  3078. Android_ShowMessageBox(messageboxdata, buttonid) == 0) {
  3079. retval = 0;
  3080. }
  3081. #endif
  3082. #if SDL_VIDEO_DRIVER_WINDOWS
  3083. if (retval == -1 &&
  3084. SDL_MessageboxValidForDriver(messageboxdata, SDL_SYSWM_WINDOWS) &&
  3085. WIN_ShowMessageBox(messageboxdata, buttonid) == 0) {
  3086. retval = 0;
  3087. }
  3088. #endif
  3089. #if SDL_VIDEO_DRIVER_WINRT
  3090. if (retval == -1 &&
  3091. SDL_MessageboxValidForDriver(messageboxdata, SDL_SYSWM_WINRT) &&
  3092. WINRT_ShowMessageBox(messageboxdata, buttonid) == 0) {
  3093. retval = 0;
  3094. }
  3095. #endif
  3096. #if SDL_VIDEO_DRIVER_COCOA
  3097. if (retval == -1 &&
  3098. SDL_MessageboxValidForDriver(messageboxdata, SDL_SYSWM_COCOA) &&
  3099. Cocoa_ShowMessageBox(messageboxdata, buttonid) == 0) {
  3100. retval = 0;
  3101. }
  3102. #endif
  3103. #if SDL_VIDEO_DRIVER_UIKIT
  3104. if (retval == -1 &&
  3105. SDL_MessageboxValidForDriver(messageboxdata, SDL_SYSWM_UIKIT) &&
  3106. UIKit_ShowMessageBox(messageboxdata, buttonid) == 0) {
  3107. retval = 0;
  3108. }
  3109. #endif
  3110. #if SDL_VIDEO_DRIVER_X11
  3111. if (retval == -1 &&
  3112. SDL_MessageboxValidForDriver(messageboxdata, SDL_SYSWM_X11) &&
  3113. X11_ShowMessageBox(messageboxdata, buttonid) == 0) {
  3114. retval = 0;
  3115. }
  3116. #endif
  3117. if (retval == -1) {
  3118. SDL_SetError("No message system available");
  3119. }
  3120. if (current_window) {
  3121. SDL_RaiseWindow(current_window);
  3122. if (mouse_captured) {
  3123. SDL_CaptureMouse(SDL_TRUE);
  3124. }
  3125. }
  3126. SDL_ShowCursor(show_cursor_prev);
  3127. SDL_SetRelativeMouseMode(relative_mode);
  3128. return retval;
  3129. }
  3130. int
  3131. SDL_ShowSimpleMessageBox(Uint32 flags, const char *title, const char *message, SDL_Window *window)
  3132. {
  3133. SDL_MessageBoxData data;
  3134. SDL_MessageBoxButtonData button;
  3135. SDL_zero(data);
  3136. data.flags = flags;
  3137. data.title = title;
  3138. data.message = message;
  3139. data.numbuttons = 1;
  3140. data.buttons = &button;
  3141. data.window = window;
  3142. SDL_zero(button);
  3143. button.flags |= SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT;
  3144. button.flags |= SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT;
  3145. button.text = "OK";
  3146. return SDL_ShowMessageBox(&data, NULL);
  3147. }
  3148. SDL_bool
  3149. SDL_ShouldAllowTopmost(void)
  3150. {
  3151. const char *hint = SDL_GetHint(SDL_HINT_ALLOW_TOPMOST);
  3152. if (hint) {
  3153. if (*hint == '0') {
  3154. return SDL_FALSE;
  3155. } else {
  3156. return SDL_TRUE;
  3157. }
  3158. }
  3159. return SDL_TRUE;
  3160. }
  3161. int
  3162. SDL_SetWindowHitTest(SDL_Window * window, SDL_HitTest callback, void *userdata)
  3163. {
  3164. CHECK_WINDOW_MAGIC(window, -1);
  3165. if (!_this->SetWindowHitTest) {
  3166. return SDL_Unsupported();
  3167. } else if (_this->SetWindowHitTest(window, callback != NULL) == -1) {
  3168. return -1;
  3169. }
  3170. window->hit_test = callback;
  3171. window->hit_test_data = userdata;
  3172. return 0;
  3173. }
  3174. float SDL_ComputeDiagonalDPI(int hpix, int vpix, float hinches, float vinches)
  3175. {
  3176. float den2 = hinches * hinches + vinches * vinches;
  3177. if ( den2 <= 0.0f ) {
  3178. return 0.0f;
  3179. }
  3180. return (float)(SDL_sqrt((double)hpix * (double)hpix + (double)vpix * (double)vpix) /
  3181. SDL_sqrt((double)den2));
  3182. }
  3183. /* vi: set ts=4 sw=4 expandtab: */