SDL_video.c 166 KB

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