SDL_video.c 135 KB

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