SDL_video.c 131 KB

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