SDL_video.c 143 KB

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