SDL_cocoawindow.m 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_VIDEO_DRIVER_COCOA
  20. #include <float.h> // For FLT_MAX
  21. #include "../../events/SDL_dropevents_c.h"
  22. #include "../../events/SDL_keyboard_c.h"
  23. #include "../../events/SDL_mouse_c.h"
  24. #include "../../events/SDL_touch_c.h"
  25. #include "../../events/SDL_windowevents_c.h"
  26. #include "../SDL_sysvideo.h"
  27. #include "SDL_cocoamouse.h"
  28. #include "SDL_cocoaopengl.h"
  29. #include "SDL_cocoaopengles.h"
  30. #include "SDL_cocoavideo.h"
  31. #if 0
  32. #define DEBUG_COCOAWINDOW
  33. #endif
  34. #ifdef DEBUG_COCOAWINDOW
  35. #define DLog(fmt, ...) printf("%s: " fmt "\n", __func__, ##__VA_ARGS__)
  36. #else
  37. #define DLog(...) \
  38. do { \
  39. } while (0)
  40. #endif
  41. #ifndef MAC_OS_X_VERSION_10_12
  42. #define NSEventModifierFlagCapsLock NSAlphaShiftKeyMask
  43. #endif
  44. #ifndef NSAppKitVersionNumber10_13_2
  45. #define NSAppKitVersionNumber10_13_2 1561.2
  46. #endif
  47. #ifndef NSAppKitVersionNumber10_14
  48. #define NSAppKitVersionNumber10_14 1671
  49. #endif
  50. @implementation SDL_CocoaWindowData
  51. @end
  52. @interface NSScreen (SDL)
  53. #if MAC_OS_X_VERSION_MAX_ALLOWED < 120000 // Added in the 12.0 SDK
  54. @property(readonly) NSEdgeInsets safeAreaInsets;
  55. #endif
  56. @end
  57. @interface NSWindow (SDL)
  58. // This is available as of 10.13.2, but isn't in public headers
  59. @property(nonatomic) NSRect mouseConfinementRect;
  60. @end
  61. @interface SDL3Window : NSWindow <NSDraggingDestination>
  62. // These are needed for borderless/fullscreen windows
  63. - (BOOL)canBecomeKeyWindow;
  64. - (BOOL)canBecomeMainWindow;
  65. - (void)sendEvent:(NSEvent *)event;
  66. - (void)doCommandBySelector:(SEL)aSelector;
  67. // Handle drag-and-drop of files onto the SDL window.
  68. - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender;
  69. - (void)draggingExited:(id<NSDraggingInfo>)sender;
  70. - (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender;
  71. - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender;
  72. - (BOOL)wantsPeriodicDraggingUpdates;
  73. - (BOOL)validateMenuItem:(NSMenuItem *)menuItem;
  74. - (SDL_Window *)findSDLWindow;
  75. @end
  76. @implementation SDL3Window
  77. - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
  78. {
  79. /* Only allow using the macOS native fullscreen toggle menubar item if the
  80. * window is resizable and not in a SDL fullscreen mode.
  81. */
  82. if ([menuItem action] == @selector(toggleFullScreen:)) {
  83. SDL_Window *window = [self findSDLWindow];
  84. if (!window) {
  85. return NO;
  86. }
  87. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  88. if ((window->flags & SDL_WINDOW_FULLSCREEN) && ![data.listener isInFullscreenSpace]) {
  89. return NO;
  90. } else if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
  91. return NO;
  92. }
  93. }
  94. return [super validateMenuItem:menuItem];
  95. }
  96. - (BOOL)canBecomeKeyWindow
  97. {
  98. SDL_Window *window = [self findSDLWindow];
  99. if (window && !(window->flags & (SDL_WINDOW_TOOLTIP | SDL_WINDOW_NOT_FOCUSABLE))) {
  100. return YES;
  101. } else {
  102. return NO;
  103. }
  104. }
  105. - (BOOL)canBecomeMainWindow
  106. {
  107. SDL_Window *window = [self findSDLWindow];
  108. if (window && !(window->flags & (SDL_WINDOW_TOOLTIP | SDL_WINDOW_NOT_FOCUSABLE)) && !SDL_WINDOW_IS_POPUP(window)) {
  109. return YES;
  110. } else {
  111. return NO;
  112. }
  113. }
  114. - (void)sendEvent:(NSEvent *)event
  115. {
  116. id delegate;
  117. [super sendEvent:event];
  118. if ([event type] != NSEventTypeLeftMouseUp) {
  119. return;
  120. }
  121. delegate = [self delegate];
  122. if (![delegate isKindOfClass:[SDL3Cocoa_WindowListener class]]) {
  123. return;
  124. }
  125. if ([delegate isMoving]) {
  126. [delegate windowDidFinishMoving];
  127. }
  128. }
  129. /* We'll respond to selectors by doing nothing so we don't beep.
  130. * The escape key gets converted to a "cancel" selector, etc.
  131. */
  132. - (void)doCommandBySelector:(SEL)aSelector
  133. {
  134. // NSLog(@"doCommandBySelector: %@\n", NSStringFromSelector(aSelector));
  135. }
  136. - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender
  137. {
  138. if (([sender draggingSourceOperationMask] & NSDragOperationGeneric) == NSDragOperationGeneric) {
  139. return NSDragOperationGeneric;
  140. } else if (([sender draggingSourceOperationMask] & NSDragOperationCopy) == NSDragOperationCopy) {
  141. return NSDragOperationCopy;
  142. }
  143. return NSDragOperationNone; // no idea what to do with this, reject it.
  144. }
  145. - (void)draggingExited:(id<NSDraggingInfo>)sender
  146. {
  147. SDL_Window *sdlwindow = [self findSDLWindow];
  148. SDL_SendDropComplete(sdlwindow);
  149. }
  150. - (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender
  151. {
  152. if (([sender draggingSourceOperationMask] & NSDragOperationGeneric) == NSDragOperationGeneric) {
  153. SDL_Window *sdlwindow = [self findSDLWindow];
  154. NSPoint point = [sender draggingLocation];
  155. float x, y;
  156. x = point.x;
  157. y = (sdlwindow->h - point.y);
  158. SDL_SendDropPosition(sdlwindow, x, y);
  159. return NSDragOperationGeneric;
  160. } else if (([sender draggingSourceOperationMask] & NSDragOperationCopy) == NSDragOperationCopy) {
  161. SDL_Window *sdlwindow = [self findSDLWindow];
  162. NSPoint point = [sender draggingLocation];
  163. float x, y;
  164. x = point.x;
  165. y = (sdlwindow->h - point.y);
  166. SDL_SendDropPosition(sdlwindow, x, y);
  167. return NSDragOperationCopy;
  168. }
  169. return NSDragOperationNone; // no idea what to do with this, reject it.
  170. }
  171. - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender
  172. {
  173. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  174. ". [SDL] In performDragOperation, draggingSourceOperationMask %lx, "
  175. "expected Generic %lx, others Copy %lx, Link %lx, Private %lx, Move %lx, Delete %lx\n",
  176. (unsigned long)[sender draggingSourceOperationMask],
  177. (unsigned long)NSDragOperationGeneric,
  178. (unsigned long)NSDragOperationCopy,
  179. (unsigned long)NSDragOperationLink,
  180. (unsigned long)NSDragOperationPrivate,
  181. (unsigned long)NSDragOperationMove,
  182. (unsigned long)NSDragOperationDelete);
  183. if ([sender draggingPasteboard]) {
  184. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  185. ". [SDL] In performDragOperation, valid draggingPasteboard, "
  186. "name [%s] '%s', changeCount %ld\n",
  187. [[[[sender draggingPasteboard] name] className] UTF8String],
  188. [[[[sender draggingPasteboard] name] description] UTF8String],
  189. (long)[[sender draggingPasteboard] changeCount]);
  190. }
  191. @autoreleasepool {
  192. NSPasteboard *pasteboard = [sender draggingPasteboard];
  193. NSString *desiredType = [pasteboard availableTypeFromArray:@[ NSFilenamesPboardType, NSPasteboardTypeString ]];
  194. SDL_Window *sdlwindow = [self findSDLWindow];
  195. NSData *pboardData;
  196. id pboardPlist;
  197. NSString *pboardString;
  198. NSPoint point;
  199. float x, y;
  200. for (NSString *supportedType in [pasteboard types]) {
  201. NSString *typeString = [pasteboard stringForType:supportedType];
  202. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  203. ". [SDL] In performDragOperation, Pasteboard type '%s', stringForType (%lu) '%s'\n",
  204. [[supportedType description] UTF8String],
  205. (unsigned long)[[typeString description] length],
  206. [[typeString description] UTF8String]);
  207. }
  208. if (desiredType == nil) {
  209. return NO; // can't accept anything that's being dropped here.
  210. }
  211. pboardData = [pasteboard dataForType:desiredType];
  212. if (pboardData == nil) {
  213. return NO;
  214. }
  215. SDL_assert([desiredType isEqualToString:NSFilenamesPboardType] ||
  216. [desiredType isEqualToString:NSPasteboardTypeString]);
  217. pboardString = [pasteboard stringForType:desiredType];
  218. pboardPlist = [pasteboard propertyListForType:desiredType];
  219. // Use SendDropPosition to update the mouse location
  220. point = [sender draggingLocation];
  221. x = point.x;
  222. y = (sdlwindow->h - point.y);
  223. if (x >= 0.0f && x < (float)sdlwindow->w && y >= 0.0f && y < (float)sdlwindow->h) {
  224. SDL_SendDropPosition(sdlwindow, x, y);
  225. }
  226. // Use SendDropPosition to update the mouse location
  227. if ([desiredType isEqualToString:NSFilenamesPboardType]) {
  228. for (NSString *path in (NSArray *)pboardPlist) {
  229. NSURL *fileURL = [NSURL fileURLWithPath:path];
  230. NSNumber *isAlias = nil;
  231. [fileURL getResourceValue:&isAlias forKey:NSURLIsAliasFileKey error:nil];
  232. // If the URL is an alias, resolve it.
  233. if ([isAlias boolValue]) {
  234. NSURLBookmarkResolutionOptions opts = NSURLBookmarkResolutionWithoutMounting |
  235. NSURLBookmarkResolutionWithoutUI;
  236. NSData *bookmark = [NSURL bookmarkDataWithContentsOfURL:fileURL error:nil];
  237. if (bookmark != nil) {
  238. NSURL *resolvedURL = [NSURL URLByResolvingBookmarkData:bookmark
  239. options:opts
  240. relativeToURL:nil
  241. bookmarkDataIsStale:nil
  242. error:nil];
  243. if (resolvedURL != nil) {
  244. fileURL = resolvedURL;
  245. }
  246. }
  247. }
  248. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  249. ". [SDL] In performDragOperation, desiredType '%s', "
  250. "Submitting DropFile as (%lu) '%s'\n",
  251. [[desiredType description] UTF8String],
  252. (unsigned long)[[[fileURL path] description] length],
  253. [[[fileURL path] description] UTF8String]);
  254. if (!SDL_SendDropFile(sdlwindow, NULL, [[[fileURL path] description] UTF8String])) {
  255. return NO;
  256. }
  257. }
  258. } else if ([desiredType isEqualToString:NSPasteboardTypeString]) {
  259. char *buffer = SDL_strdup([[pboardString description] UTF8String]);
  260. char *saveptr = NULL;
  261. char *token = SDL_strtok_r(buffer, "\r\n", &saveptr);
  262. while (token) {
  263. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  264. ". [SDL] In performDragOperation, desiredType '%s', "
  265. "Submitting DropText as (%lu) '%s'\n",
  266. [[desiredType description] UTF8String],
  267. SDL_strlen(token), token);
  268. if (!SDL_SendDropText(sdlwindow, token)) {
  269. SDL_free(buffer);
  270. return NO;
  271. }
  272. token = SDL_strtok_r(NULL, "\r\n", &saveptr);
  273. }
  274. SDL_free(buffer);
  275. }
  276. SDL_SendDropComplete(sdlwindow);
  277. return YES;
  278. }
  279. }
  280. - (BOOL)wantsPeriodicDraggingUpdates
  281. {
  282. return NO;
  283. }
  284. - (SDL_Window *)findSDLWindow
  285. {
  286. SDL_Window *sdlwindow = NULL;
  287. SDL_VideoDevice *_this = SDL_GetVideoDevice();
  288. // !!! FIXME: is there a better way to do this?
  289. if (_this) {
  290. for (sdlwindow = _this->windows; sdlwindow; sdlwindow = sdlwindow->next) {
  291. NSWindow *nswindow = ((__bridge SDL_CocoaWindowData *)sdlwindow->internal).nswindow;
  292. if (nswindow == self) {
  293. break;
  294. }
  295. }
  296. }
  297. return sdlwindow;
  298. }
  299. @end
  300. bool b_inModeTransition;
  301. static CGFloat SqDistanceToRect(const NSPoint *point, const NSRect *rect)
  302. {
  303. NSPoint edge = *point;
  304. CGFloat left = NSMinX(*rect), right = NSMaxX(*rect);
  305. CGFloat bottom = NSMinX(*rect), top = NSMaxY(*rect);
  306. NSPoint delta;
  307. if (point->x < left) {
  308. edge.x = left;
  309. } else if (point->x > right) {
  310. edge.x = right;
  311. }
  312. if (point->y < bottom) {
  313. edge.y = bottom;
  314. } else if (point->y > top) {
  315. edge.y = top;
  316. }
  317. delta = NSMakePoint(edge.x - point->x, edge.y - point->y);
  318. return delta.x * delta.x + delta.y * delta.y;
  319. }
  320. static NSScreen *ScreenForPoint(const NSPoint *point)
  321. {
  322. NSScreen *screen;
  323. // Do a quick check first to see if the point lies on a specific screen
  324. for (NSScreen *candidate in [NSScreen screens]) {
  325. if (NSPointInRect(*point, [candidate frame])) {
  326. screen = candidate;
  327. break;
  328. }
  329. }
  330. // Find the screen the point is closest to
  331. if (!screen) {
  332. CGFloat closest = MAXFLOAT;
  333. for (NSScreen *candidate in [NSScreen screens]) {
  334. NSRect screenRect = [candidate frame];
  335. CGFloat sqdist = SqDistanceToRect(point, &screenRect);
  336. if (sqdist < closest) {
  337. screen = candidate;
  338. closest = sqdist;
  339. }
  340. }
  341. }
  342. return screen;
  343. }
  344. bool Cocoa_IsWindowInFullscreenSpace(SDL_Window *window)
  345. {
  346. @autoreleasepool {
  347. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  348. if ([data.listener isInFullscreenSpace]) {
  349. return true;
  350. } else {
  351. return false;
  352. }
  353. }
  354. }
  355. bool Cocoa_IsWindowInFullscreenSpaceTransition(SDL_Window *window)
  356. {
  357. @autoreleasepool {
  358. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  359. if ([data.listener isInFullscreenSpaceTransition]) {
  360. return true;
  361. } else {
  362. return false;
  363. }
  364. }
  365. }
  366. bool Cocoa_IsWindowZoomed(SDL_Window *window)
  367. {
  368. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  369. NSWindow *nswindow = data.nswindow;
  370. bool zoomed = false;
  371. // isZoomed always returns true if the window is not resizable or the window is fullscreen
  372. if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed] &&
  373. !(window->flags & SDL_WINDOW_FULLSCREEN) && !Cocoa_IsWindowInFullscreenSpace(window)) {
  374. // If we are at our desired floating area, then we're not zoomed
  375. bool floating = (window->x == window->floating.x &&
  376. window->y == window->floating.y &&
  377. window->w == window->floating.w &&
  378. window->h == window->floating.h);
  379. if (!floating) {
  380. zoomed = true;
  381. }
  382. }
  383. return zoomed;
  384. }
  385. bool Cocoa_IsShowingModalDialog(SDL_Window *window)
  386. {
  387. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  388. return data.has_modal_dialog;
  389. }
  390. void Cocoa_SetWindowHasModalDialog(SDL_Window *window, bool has_modal)
  391. {
  392. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  393. data.has_modal_dialog = has_modal;
  394. }
  395. typedef enum CocoaMenuVisibility
  396. {
  397. COCOA_MENU_VISIBILITY_AUTO = 0,
  398. COCOA_MENU_VISIBILITY_NEVER,
  399. COCOA_MENU_VISIBILITY_ALWAYS
  400. } CocoaMenuVisibility;
  401. static CocoaMenuVisibility menu_visibility_hint = COCOA_MENU_VISIBILITY_AUTO;
  402. static void Cocoa_ToggleFullscreenSpaceMenuVisibility(SDL_Window *window)
  403. {
  404. if (window && Cocoa_IsWindowInFullscreenSpace(window)) {
  405. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  406. // 'Auto' sets the menu to visible if fullscreen wasn't explicitly entered via SDL_SetWindowFullscreen().
  407. if ((menu_visibility_hint == COCOA_MENU_VISIBILITY_AUTO && !data.fullscreen_space_requested) ||
  408. menu_visibility_hint == COCOA_MENU_VISIBILITY_ALWAYS) {
  409. [NSMenu setMenuBarVisible:YES];
  410. } else {
  411. [NSMenu setMenuBarVisible:NO];
  412. }
  413. }
  414. }
  415. void Cocoa_MenuVisibilityCallback(void *userdata, const char *name, const char *oldValue, const char *newValue)
  416. {
  417. if (newValue) {
  418. if (*newValue == '0' || SDL_strcasecmp(newValue, "false") == 0) {
  419. menu_visibility_hint = COCOA_MENU_VISIBILITY_NEVER;
  420. } else if (*newValue == '1' || SDL_strcasecmp(newValue, "true") == 0) {
  421. menu_visibility_hint = COCOA_MENU_VISIBILITY_ALWAYS;
  422. } else {
  423. menu_visibility_hint = COCOA_MENU_VISIBILITY_AUTO;
  424. }
  425. } else {
  426. menu_visibility_hint = COCOA_MENU_VISIBILITY_AUTO;
  427. }
  428. // Update the current menu visibility.
  429. Cocoa_ToggleFullscreenSpaceMenuVisibility(SDL_GetKeyboardFocus());
  430. }
  431. static NSScreen *ScreenForRect(const NSRect *rect)
  432. {
  433. NSPoint center = NSMakePoint(NSMidX(*rect), NSMidY(*rect));
  434. return ScreenForPoint(&center);
  435. }
  436. static void ConvertNSRect(NSRect *r)
  437. {
  438. SDL_CocoaVideoData *videodata = (__bridge SDL_CocoaVideoData *)SDL_GetVideoDevice()->internal;
  439. r->origin.y = videodata.mainDisplayHeight - r->origin.y - r->size.height;
  440. }
  441. static void ScheduleContextUpdates(SDL_CocoaWindowData *data)
  442. {
  443. // We still support OpenGL as long as Apple offers it, deprecated or not, so disable deprecation warnings about it.
  444. #ifdef SDL_VIDEO_OPENGL
  445. #ifdef __clang__
  446. #pragma clang diagnostic push
  447. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  448. #endif
  449. NSOpenGLContext *currentContext;
  450. NSMutableArray *contexts;
  451. if (!data || !data.nscontexts) {
  452. return;
  453. }
  454. currentContext = [NSOpenGLContext currentContext];
  455. contexts = data.nscontexts;
  456. @synchronized(contexts) {
  457. for (SDL3OpenGLContext *context in contexts) {
  458. if (context == currentContext) {
  459. [context update];
  460. } else {
  461. [context scheduleUpdate];
  462. }
  463. }
  464. }
  465. #ifdef __clang__
  466. #pragma clang diagnostic pop
  467. #endif
  468. #endif // SDL_VIDEO_OPENGL
  469. }
  470. // !!! FIXME: this should use a hint callback.
  471. static bool GetHintCtrlClickEmulateRightClick(void)
  472. {
  473. return SDL_GetHintBoolean(SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK, false);
  474. }
  475. static NSUInteger GetWindowWindowedStyle(SDL_Window *window)
  476. {
  477. /* IF YOU CHANGE ANY FLAGS IN HERE, PLEASE READ
  478. the NSWindowStyleMaskBorderless comments in SetupWindowData()! */
  479. /* always allow miniaturization, otherwise you can't programmatically
  480. minimize the window, whether there's a title bar or not */
  481. NSUInteger style = NSWindowStyleMaskMiniaturizable;
  482. if (!SDL_WINDOW_IS_POPUP(window)) {
  483. if (window->flags & SDL_WINDOW_BORDERLESS) {
  484. style |= NSWindowStyleMaskBorderless;
  485. } else {
  486. style |= (NSWindowStyleMaskTitled | NSWindowStyleMaskClosable);
  487. }
  488. if (window->flags & SDL_WINDOW_RESIZABLE) {
  489. style |= NSWindowStyleMaskResizable;
  490. }
  491. } else {
  492. style |= NSWindowStyleMaskBorderless;
  493. }
  494. return style;
  495. }
  496. static NSUInteger GetWindowStyle(SDL_Window *window)
  497. {
  498. NSUInteger style = 0;
  499. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  500. style = NSWindowStyleMaskBorderless;
  501. } else {
  502. style = GetWindowWindowedStyle(window);
  503. }
  504. return style;
  505. }
  506. static bool SetWindowStyle(SDL_Window *window, NSUInteger style)
  507. {
  508. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  509. NSWindow *nswindow = data.nswindow;
  510. // The view responder chain gets messed with during setStyleMask
  511. if ([data.sdlContentView nextResponder] == data.listener) {
  512. [data.sdlContentView setNextResponder:nil];
  513. }
  514. [nswindow setStyleMask:style];
  515. // The view responder chain gets messed with during setStyleMask
  516. if ([data.sdlContentView nextResponder] != data.listener) {
  517. [data.sdlContentView setNextResponder:data.listener];
  518. }
  519. return true;
  520. }
  521. static bool ShouldAdjustCoordinatesForGrab(SDL_Window *window)
  522. {
  523. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  524. if (!data || [data.listener isMovingOrFocusClickPending]) {
  525. return false;
  526. }
  527. if (!(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
  528. return false;
  529. }
  530. if ((window->flags & SDL_WINDOW_MOUSE_GRABBED) || (window->mouse_rect.w > 0 && window->mouse_rect.h > 0)) {
  531. return true;
  532. }
  533. return false;
  534. }
  535. static bool AdjustCoordinatesForGrab(SDL_Window *window, float x, float y, CGPoint *adjusted)
  536. {
  537. if (window->mouse_rect.w > 0 && window->mouse_rect.h > 0) {
  538. SDL_Rect window_rect;
  539. SDL_Rect mouse_rect;
  540. window_rect.x = 0;
  541. window_rect.y = 0;
  542. window_rect.w = window->w;
  543. window_rect.h = window->h;
  544. if (SDL_GetRectIntersection(&window->mouse_rect, &window_rect, &mouse_rect)) {
  545. float left = (float)window->x + mouse_rect.x;
  546. float right = left + mouse_rect.w - 1;
  547. float top = (float)window->y + mouse_rect.y;
  548. float bottom = top + mouse_rect.h - 1;
  549. if (x < left || x > right || y < top || y > bottom) {
  550. adjusted->x = SDL_clamp(x, left, right);
  551. adjusted->y = SDL_clamp(y, top, bottom);
  552. return true;
  553. }
  554. return false;
  555. }
  556. }
  557. if (window->flags & SDL_WINDOW_MOUSE_GRABBED) {
  558. float left = (float)window->x;
  559. float right = left + window->w - 1;
  560. float top = (float)window->y;
  561. float bottom = top + window->h - 1;
  562. if (x < left || x > right || y < top || y > bottom) {
  563. adjusted->x = SDL_clamp(x, left, right);
  564. adjusted->y = SDL_clamp(y, top, bottom);
  565. return true;
  566. }
  567. }
  568. return false;
  569. }
  570. static void Cocoa_UpdateClipCursor(SDL_Window *window)
  571. {
  572. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  573. if (NSAppKitVersionNumber >= NSAppKitVersionNumber10_13_2) {
  574. NSWindow *nswindow = data.nswindow;
  575. SDL_Rect mouse_rect;
  576. SDL_zero(mouse_rect);
  577. if (ShouldAdjustCoordinatesForGrab(window)) {
  578. SDL_Rect window_rect;
  579. window_rect.x = 0;
  580. window_rect.y = 0;
  581. window_rect.w = window->w;
  582. window_rect.h = window->h;
  583. if (window->mouse_rect.w > 0 && window->mouse_rect.h > 0) {
  584. SDL_GetRectIntersection(&window->mouse_rect, &window_rect, &mouse_rect);
  585. }
  586. if ((window->flags & SDL_WINDOW_MOUSE_GRABBED) != 0 &&
  587. SDL_RectEmpty(&mouse_rect)) {
  588. SDL_memcpy(&mouse_rect, &window_rect, sizeof(mouse_rect));
  589. }
  590. }
  591. if (SDL_RectEmpty(&mouse_rect)) {
  592. nswindow.mouseConfinementRect = NSZeroRect;
  593. } else {
  594. NSRect rect;
  595. rect.origin.x = mouse_rect.x;
  596. rect.origin.y = [nswindow contentLayoutRect].size.height - mouse_rect.y - mouse_rect.h;
  597. rect.size.width = mouse_rect.w;
  598. rect.size.height = mouse_rect.h;
  599. nswindow.mouseConfinementRect = rect;
  600. }
  601. } else {
  602. // Move the cursor to the nearest point in the window
  603. if (ShouldAdjustCoordinatesForGrab(window)) {
  604. float x, y;
  605. CGPoint cgpoint;
  606. SDL_GetGlobalMouseState(&x, &y);
  607. if (AdjustCoordinatesForGrab(window, x, y, &cgpoint)) {
  608. Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
  609. CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
  610. }
  611. }
  612. }
  613. }
  614. static SDL_Window *GetParentToplevelWindow(SDL_Window *window)
  615. {
  616. SDL_Window *toplevel = window;
  617. // Find the topmost parent
  618. while (SDL_WINDOW_IS_POPUP(toplevel)) {
  619. toplevel = toplevel->parent;
  620. }
  621. return toplevel;
  622. }
  623. static void Cocoa_SetKeyboardFocus(SDL_Window *window, bool set_active_focus)
  624. {
  625. SDL_Window *toplevel = GetParentToplevelWindow(window);
  626. toplevel->keyboard_focus = window;
  627. if (set_active_focus && !window->is_hiding && !window->is_destroying) {
  628. SDL_SetKeyboardFocus(window);
  629. }
  630. }
  631. static void Cocoa_SendExposedEventIfVisible(SDL_Window *window)
  632. {
  633. NSWindow *nswindow = ((__bridge SDL_CocoaWindowData *)window->internal).nswindow;
  634. if ([nswindow occlusionState] & NSWindowOcclusionStateVisible) {
  635. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_EXPOSED, 0, 0);
  636. }
  637. }
  638. static void Cocoa_WaitForMiniaturizable(SDL_Window *window)
  639. {
  640. NSWindow *nswindow = ((__bridge SDL_CocoaWindowData *)window->internal).nswindow;
  641. NSButton *button = [nswindow standardWindowButton:NSWindowMiniaturizeButton];
  642. if (button) {
  643. int iterations = 0;
  644. while (![button isEnabled] && (iterations < 100)) {
  645. SDL_Delay(10);
  646. SDL_PumpEvents();
  647. iterations++;
  648. }
  649. }
  650. }
  651. static void Cocoa_IncrementCursorFrame(void)
  652. {
  653. SDL_Mouse *mouse = SDL_GetMouse();
  654. if (mouse->cur_cursor) {
  655. SDL_CursorData *cdata = mouse->cur_cursor->internal;
  656. cdata->current_frame = (cdata->current_frame + 1) % cdata->num_cursors;
  657. SDL_Window *focus = SDL_GetMouseFocus();
  658. if (focus) {
  659. SDL_CocoaWindowData *_data = (__bridge SDL_CocoaWindowData *)focus->internal;
  660. [_data.nswindow invalidateCursorRectsForView:_data.sdlContentView];
  661. }
  662. }
  663. }
  664. static NSCursor *Cocoa_GetDesiredCursor(void)
  665. {
  666. SDL_Mouse *mouse = SDL_GetMouse();
  667. if (mouse->cursor_visible && mouse->cur_cursor && !mouse->relative_mode) {
  668. SDL_CursorData *cdata = mouse->cur_cursor->internal;
  669. if (cdata) {
  670. if (cdata->num_cursors > 1 && cdata->frames[cdata->current_frame].duration && !cdata->frameTimer) {
  671. const NSTimeInterval interval = cdata->frames[cdata->current_frame].duration * 0.001;
  672. cdata->frameTimer = [NSTimer timerWithTimeInterval:interval
  673. repeats:NO
  674. block:^(NSTimer *timer) {
  675. cdata->frameTimer = nil;
  676. Cocoa_IncrementCursorFrame();
  677. }];
  678. [[NSRunLoop currentRunLoop] addTimer:cdata->frameTimer forMode:NSRunLoopCommonModes];
  679. }
  680. return (__bridge NSCursor *)cdata->frames[cdata->current_frame].cursor;
  681. }
  682. }
  683. return [NSCursor invisibleCursor];
  684. }
  685. @implementation SDL3Cocoa_WindowListener
  686. - (void)listen:(SDL_CocoaWindowData *)data
  687. {
  688. NSNotificationCenter *center;
  689. NSWindow *window = data.nswindow;
  690. NSView *view = data.sdlContentView;
  691. _data = data;
  692. observingVisible = YES;
  693. wasCtrlLeft = NO;
  694. wasVisible = [window isVisible];
  695. isFullscreenSpace = NO;
  696. inFullscreenTransition = NO;
  697. pendingWindowOperation = PENDING_OPERATION_NONE;
  698. isMoving = NO;
  699. isMiniaturizing = NO;
  700. isDragAreaRunning = NO;
  701. pendingWindowWarpX = pendingWindowWarpY = FLT_MAX;
  702. liveResizeTimer = nil;
  703. center = [NSNotificationCenter defaultCenter];
  704. if ([window delegate] != nil) {
  705. [center addObserver:self selector:@selector(windowDidExpose:) name:NSWindowDidExposeNotification object:window];
  706. [center addObserver:self selector:@selector(windowDidChangeOcclusionState:) name:NSWindowDidChangeOcclusionStateNotification object:window];
  707. [center addObserver:self selector:@selector(windowWillStartLiveResize:) name:NSWindowWillStartLiveResizeNotification object:window];
  708. [center addObserver:self selector:@selector(windowDidEndLiveResize:) name:NSWindowDidEndLiveResizeNotification object:window];
  709. [center addObserver:self selector:@selector(windowWillMove:) name:NSWindowWillMoveNotification object:window];
  710. [center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:window];
  711. [center addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:window];
  712. [center addObserver:self selector:@selector(windowWillMiniaturize:) name:NSWindowWillMiniaturizeNotification object:window];
  713. [center addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:window];
  714. [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:window];
  715. [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:window];
  716. [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:window];
  717. [center addObserver:self selector:@selector(windowDidChangeBackingProperties:) name:NSWindowDidChangeBackingPropertiesNotification object:window];
  718. [center addObserver:self selector:@selector(windowDidChangeScreenProfile:) name:NSWindowDidChangeScreenProfileNotification object:window];
  719. [center addObserver:self selector:@selector(windowDidChangeScreen:) name:NSWindowDidChangeScreenNotification object:window];
  720. [center addObserver:self selector:@selector(windowWillEnterFullScreen:) name:NSWindowWillEnterFullScreenNotification object:window];
  721. [center addObserver:self selector:@selector(windowDidEnterFullScreen:) name:NSWindowDidEnterFullScreenNotification object:window];
  722. [center addObserver:self selector:@selector(windowWillExitFullScreen:) name:NSWindowWillExitFullScreenNotification object:window];
  723. [center addObserver:self selector:@selector(windowDidExitFullScreen:) name:NSWindowDidExitFullScreenNotification object:window];
  724. [center addObserver:self selector:@selector(windowDidFailToEnterFullScreen:) name:@"NSWindowDidFailToEnterFullScreenNotification" object:window];
  725. [center addObserver:self selector:@selector(windowDidFailToExitFullScreen:) name:@"NSWindowDidFailToExitFullScreenNotification" object:window];
  726. } else {
  727. [window setDelegate:self];
  728. }
  729. /* Haven't found a delegate / notification that triggers when the window is
  730. * ordered out (is not visible any more). You can be ordered out without
  731. * minimizing, so DidMiniaturize doesn't work. (e.g. -[NSWindow orderOut:])
  732. */
  733. [window addObserver:self
  734. forKeyPath:@"visible"
  735. options:NSKeyValueObservingOptionNew
  736. context:NULL];
  737. [window setNextResponder:self];
  738. [window setAcceptsMouseMovedEvents:YES];
  739. [view setNextResponder:self];
  740. [view setAcceptsTouchEvents:YES];
  741. }
  742. - (void)observeValueForKeyPath:(NSString *)keyPath
  743. ofObject:(id)object
  744. change:(NSDictionary *)change
  745. context:(void *)context
  746. {
  747. if (!observingVisible) {
  748. return;
  749. }
  750. if (object == _data.nswindow && [keyPath isEqualToString:@"visible"]) {
  751. int newVisibility = [[change objectForKey:@"new"] intValue];
  752. if (newVisibility) {
  753. SDL_SendWindowEvent(_data.window, SDL_EVENT_WINDOW_SHOWN, 0, 0);
  754. } else if (![_data.nswindow isMiniaturized]) {
  755. SDL_SendWindowEvent(_data.window, SDL_EVENT_WINDOW_HIDDEN, 0, 0);
  756. }
  757. }
  758. }
  759. - (void)pauseVisibleObservation
  760. {
  761. observingVisible = NO;
  762. wasVisible = [_data.nswindow isVisible];
  763. }
  764. - (void)resumeVisibleObservation
  765. {
  766. BOOL isVisible = [_data.nswindow isVisible];
  767. observingVisible = YES;
  768. if (wasVisible != isVisible) {
  769. if (isVisible) {
  770. SDL_SendWindowEvent(_data.window, SDL_EVENT_WINDOW_SHOWN, 0, 0);
  771. } else if (![_data.nswindow isMiniaturized]) {
  772. SDL_SendWindowEvent(_data.window, SDL_EVENT_WINDOW_HIDDEN, 0, 0);
  773. }
  774. wasVisible = isVisible;
  775. }
  776. }
  777. - (BOOL)setFullscreenSpace:(BOOL)state
  778. {
  779. SDL_Window *window = _data.window;
  780. NSWindow *nswindow = _data.nswindow;
  781. SDL_CocoaVideoData *videodata = ((__bridge SDL_CocoaWindowData *)window->internal).videodata;
  782. if (!videodata.allow_spaces) {
  783. return NO; // Spaces are forcibly disabled.
  784. } else if (state && window->fullscreen_exclusive) {
  785. return NO; // we only allow you to make a Space on fullscreen desktop windows.
  786. } else if (!state && window->last_fullscreen_exclusive_display) {
  787. return NO; // we only handle leaving the Space on windows that were previously fullscreen desktop.
  788. } else if (state == isFullscreenSpace && !inFullscreenTransition) {
  789. return YES; // already there.
  790. }
  791. if (inFullscreenTransition) {
  792. if (state) {
  793. [self clearPendingWindowOperation:PENDING_OPERATION_LEAVE_FULLSCREEN];
  794. [self addPendingWindowOperation:PENDING_OPERATION_ENTER_FULLSCREEN];
  795. } else {
  796. [self clearPendingWindowOperation:PENDING_OPERATION_ENTER_FULLSCREEN];
  797. [self addPendingWindowOperation:PENDING_OPERATION_LEAVE_FULLSCREEN];
  798. }
  799. return YES;
  800. }
  801. inFullscreenTransition = YES;
  802. // you need to be FullScreenPrimary, or toggleFullScreen doesn't work. Unset it again in windowDidExitFullScreen.
  803. [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
  804. [nswindow performSelectorOnMainThread:@selector(toggleFullScreen:) withObject:nswindow waitUntilDone:NO];
  805. return YES;
  806. }
  807. - (BOOL)isInFullscreenSpace
  808. {
  809. return isFullscreenSpace;
  810. }
  811. - (BOOL)isInFullscreenSpaceTransition
  812. {
  813. return inFullscreenTransition;
  814. }
  815. - (void)clearPendingWindowOperation:(PendingWindowOperation)operation
  816. {
  817. pendingWindowOperation &= ~operation;
  818. }
  819. - (void)clearAllPendingWindowOperations
  820. {
  821. pendingWindowOperation = PENDING_OPERATION_NONE;
  822. }
  823. - (void)addPendingWindowOperation:(PendingWindowOperation)operation
  824. {
  825. pendingWindowOperation |= operation;
  826. }
  827. - (BOOL)windowOperationIsPending:(PendingWindowOperation)operation
  828. {
  829. return !!(pendingWindowOperation & operation);
  830. }
  831. - (BOOL)hasPendingWindowOperation
  832. {
  833. // A pending zoom may be deferred until leaving fullscreen, so don't block on it.
  834. return (pendingWindowOperation & ~PENDING_OPERATION_ZOOM) != PENDING_OPERATION_NONE ||
  835. isMiniaturizing || inFullscreenTransition;
  836. }
  837. - (void)close
  838. {
  839. NSNotificationCenter *center;
  840. NSWindow *window = _data.nswindow;
  841. NSView *view = [window contentView];
  842. center = [NSNotificationCenter defaultCenter];
  843. if ([window delegate] != self) {
  844. [center removeObserver:self name:NSWindowDidExposeNotification object:window];
  845. [center removeObserver:self name:NSWindowDidChangeOcclusionStateNotification object:window];
  846. [center removeObserver:self name:NSWindowWillStartLiveResizeNotification object:window];
  847. [center removeObserver:self name:NSWindowDidEndLiveResizeNotification object:window];
  848. [center removeObserver:self name:NSWindowWillMoveNotification object:window];
  849. [center removeObserver:self name:NSWindowDidMoveNotification object:window];
  850. [center removeObserver:self name:NSWindowDidResizeNotification object:window];
  851. [center removeObserver:self name:NSWindowWillMiniaturizeNotification object:window];
  852. [center removeObserver:self name:NSWindowDidMiniaturizeNotification object:window];
  853. [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:window];
  854. [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:window];
  855. [center removeObserver:self name:NSWindowDidResignKeyNotification object:window];
  856. [center removeObserver:self name:NSWindowDidChangeBackingPropertiesNotification object:window];
  857. [center removeObserver:self name:NSWindowDidChangeScreenProfileNotification object:window];
  858. [center removeObserver:self name:NSWindowDidChangeScreenNotification object:window];
  859. [center removeObserver:self name:NSWindowWillEnterFullScreenNotification object:window];
  860. [center removeObserver:self name:NSWindowDidEnterFullScreenNotification object:window];
  861. [center removeObserver:self name:NSWindowWillExitFullScreenNotification object:window];
  862. [center removeObserver:self name:NSWindowDidExitFullScreenNotification object:window];
  863. [center removeObserver:self name:@"NSWindowDidFailToEnterFullScreenNotification" object:window];
  864. [center removeObserver:self name:@"NSWindowDidFailToExitFullScreenNotification" object:window];
  865. } else {
  866. [window setDelegate:nil];
  867. }
  868. [window removeObserver:self forKeyPath:@"visible"];
  869. if ([window nextResponder] == self) {
  870. [window setNextResponder:nil];
  871. }
  872. if ([view nextResponder] == self) {
  873. [view setNextResponder:nil];
  874. }
  875. }
  876. - (BOOL)isMoving
  877. {
  878. return isMoving;
  879. }
  880. - (BOOL)isMovingOrFocusClickPending
  881. {
  882. return isMoving || (focusClickPending != 0);
  883. }
  884. - (void)setFocusClickPending:(NSInteger)button
  885. {
  886. focusClickPending |= (1 << button);
  887. }
  888. - (void)clearFocusClickPending:(NSInteger)button
  889. {
  890. if (focusClickPending & (1 << button)) {
  891. focusClickPending &= ~(1 << button);
  892. if (focusClickPending == 0) {
  893. [self onMovingOrFocusClickPendingStateCleared];
  894. }
  895. }
  896. }
  897. - (void)updateIgnoreMouseState:(NSEvent *)theEvent
  898. {
  899. SDL_Window *window = _data.window;
  900. SDL_Surface *shape = (SDL_Surface *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_SHAPE_POINTER, NULL);
  901. BOOL ignoresMouseEvents = NO;
  902. if (shape) {
  903. NSPoint point = [theEvent locationInWindow];
  904. NSRect windowRect = [[_data.nswindow contentView] frame];
  905. if (NSMouseInRect(point, windowRect, NO)) {
  906. int x = (int)SDL_roundf((point.x / (window->w - 1)) * (shape->w - 1));
  907. int y = (int)SDL_roundf(((window->h - point.y) / (window->h - 1)) * (shape->h - 1));
  908. Uint8 a;
  909. if (!SDL_ReadSurfacePixel(shape, x, y, NULL, NULL, NULL, &a) || a == SDL_ALPHA_TRANSPARENT) {
  910. ignoresMouseEvents = YES;
  911. }
  912. }
  913. }
  914. _data.nswindow.ignoresMouseEvents = ignoresMouseEvents;
  915. }
  916. - (void)setPendingMoveX:(float)x Y:(float)y
  917. {
  918. pendingWindowWarpX = x;
  919. pendingWindowWarpY = y;
  920. }
  921. - (void)windowDidFinishMoving
  922. {
  923. if (isMoving) {
  924. isMoving = NO;
  925. [self onMovingOrFocusClickPendingStateCleared];
  926. }
  927. }
  928. - (void)onMovingOrFocusClickPendingStateCleared
  929. {
  930. if (![self isMovingOrFocusClickPending]) {
  931. SDL_Mouse *mouse = SDL_GetMouse();
  932. if (pendingWindowWarpX != FLT_MAX && pendingWindowWarpY != FLT_MAX) {
  933. mouse->WarpMouseGlobal(pendingWindowWarpX, pendingWindowWarpY);
  934. pendingWindowWarpX = pendingWindowWarpY = FLT_MAX;
  935. }
  936. if (mouse->relative_mode && mouse->focus == _data.window) {
  937. // Move the cursor to the nearest point in the window
  938. {
  939. float x, y;
  940. CGPoint cgpoint;
  941. SDL_GetMouseState(&x, &y);
  942. cgpoint.x = _data.window->x + x;
  943. cgpoint.y = _data.window->y + y;
  944. Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
  945. DLog("Returning cursor to (%g, %g)", cgpoint.x, cgpoint.y);
  946. CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
  947. }
  948. mouse->SetRelativeMouseMode(true);
  949. } else {
  950. Cocoa_UpdateClipCursor(_data.window);
  951. }
  952. }
  953. }
  954. - (BOOL)windowShouldClose:(id)sender
  955. {
  956. SDL_SendWindowEvent(_data.window, SDL_EVENT_WINDOW_CLOSE_REQUESTED, 0, 0);
  957. return NO;
  958. }
  959. - (void)windowDidExpose:(NSNotification *)aNotification
  960. {
  961. Cocoa_SendExposedEventIfVisible(_data.window);
  962. }
  963. - (void)windowDidChangeOcclusionState:(NSNotification *)aNotification
  964. {
  965. if ([_data.nswindow occlusionState] & NSWindowOcclusionStateVisible) {
  966. SDL_SendWindowEvent(_data.window, SDL_EVENT_WINDOW_EXPOSED, 0, 0);
  967. } else {
  968. SDL_SendWindowEvent(_data.window, SDL_EVENT_WINDOW_OCCLUDED, 0, 0);
  969. }
  970. }
  971. - (void)windowWillStartLiveResize:(NSNotification *)aNotification
  972. {
  973. // We'll try to maintain 60 FPS during live resizing
  974. const NSTimeInterval interval = 1.0 / 60.0;
  975. liveResizeTimer = [NSTimer scheduledTimerWithTimeInterval:interval
  976. repeats:TRUE
  977. block:^(NSTimer *unusedTimer)
  978. {
  979. SDL_OnWindowLiveResizeUpdate(_data.window);
  980. }];
  981. [[NSRunLoop currentRunLoop] addTimer:liveResizeTimer forMode:NSRunLoopCommonModes];
  982. }
  983. - (void)windowDidEndLiveResize:(NSNotification *)aNotification
  984. {
  985. [liveResizeTimer invalidate];
  986. liveResizeTimer = nil;
  987. }
  988. - (void)windowWillMove:(NSNotification *)aNotification
  989. {
  990. if ([_data.nswindow isKindOfClass:[SDL3Window class]]) {
  991. pendingWindowWarpX = pendingWindowWarpY = FLT_MAX;
  992. isMoving = YES;
  993. }
  994. }
  995. - (void)windowDidMove:(NSNotification *)aNotification
  996. {
  997. int x, y;
  998. SDL_Window *window = _data.window;
  999. NSWindow *nswindow = _data.nswindow;
  1000. NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
  1001. ConvertNSRect(&rect);
  1002. if (inFullscreenTransition || b_inModeTransition) {
  1003. // We'll take care of this at the end of the transition
  1004. return;
  1005. }
  1006. x = (int)rect.origin.x;
  1007. y = (int)rect.origin.y;
  1008. ScheduleContextUpdates(_data);
  1009. // Get the parent-relative coordinates for child windows.
  1010. SDL_GlobalToRelativeForWindow(window, x, y, &x, &y);
  1011. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_MOVED, x, y);
  1012. }
  1013. - (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
  1014. {
  1015. SDL_Window *window = _data.window;
  1016. if (window->min_aspect != window->max_aspect) {
  1017. NSWindow *nswindow = _data.nswindow;
  1018. NSRect newContentRect = [nswindow contentRectForFrameRect:NSMakeRect(0, 0, frameSize.width, frameSize.height)];
  1019. NSSize newSize = newContentRect.size;
  1020. CGFloat minAspectRatio = window->min_aspect;
  1021. CGFloat maxAspectRatio = window->max_aspect;
  1022. CGFloat aspectRatio;
  1023. if (newSize.height > 0) {
  1024. aspectRatio = newSize.width / newSize.height;
  1025. if (maxAspectRatio > 0.0f && aspectRatio > maxAspectRatio) {
  1026. newSize.width = SDL_roundf(newSize.height * maxAspectRatio);
  1027. } else if (minAspectRatio > 0.0f && aspectRatio < minAspectRatio) {
  1028. newSize.height = SDL_roundf(newSize.width / minAspectRatio);
  1029. }
  1030. NSRect newFrameRect = [nswindow frameRectForContentRect:NSMakeRect(0, 0, newSize.width, newSize.height)];
  1031. frameSize = newFrameRect.size;
  1032. }
  1033. }
  1034. return frameSize;
  1035. }
  1036. - (void)windowDidResize:(NSNotification *)aNotification
  1037. {
  1038. SDL_Window *window;
  1039. NSWindow *nswindow;
  1040. NSRect rect;
  1041. int x, y, w, h;
  1042. BOOL zoomed;
  1043. if (inFullscreenTransition || b_inModeTransition) {
  1044. // We'll take care of this at the end of the transition
  1045. return;
  1046. }
  1047. if (focusClickPending) {
  1048. focusClickPending = 0;
  1049. [self onMovingOrFocusClickPendingStateCleared];
  1050. }
  1051. window = _data.window;
  1052. nswindow = _data.nswindow;
  1053. rect = [nswindow contentRectForFrameRect:[nswindow frame]];
  1054. ConvertNSRect(&rect);
  1055. x = (int)rect.origin.x;
  1056. y = (int)rect.origin.y;
  1057. w = (int)rect.size.width;
  1058. h = (int)rect.size.height;
  1059. _data.viewport = [_data.sdlContentView bounds];
  1060. if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) {
  1061. // This gives us the correct viewport for a Retina-enabled view.
  1062. _data.viewport = [_data.sdlContentView convertRectToBacking:_data.viewport];
  1063. }
  1064. ScheduleContextUpdates(_data);
  1065. /* The OS can resize the window automatically if the display density
  1066. * changes while the window is miniaturized or hidden.
  1067. */
  1068. if ([nswindow isVisible])
  1069. {
  1070. /* isZoomed always returns true if the window is not resizable
  1071. * and fullscreen windows are considered zoomed.
  1072. */
  1073. if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed] &&
  1074. !(window->flags & SDL_WINDOW_FULLSCREEN) && ![self isInFullscreenSpace]) {
  1075. zoomed = YES;
  1076. } else {
  1077. zoomed = NO;
  1078. }
  1079. if (!zoomed) {
  1080. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESTORED, 0, 0);
  1081. } else {
  1082. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_MAXIMIZED, 0, 0);
  1083. if ([self windowOperationIsPending:PENDING_OPERATION_MINIMIZE]) {
  1084. [nswindow miniaturize:nil];
  1085. }
  1086. }
  1087. }
  1088. /* The window can move during a resize event, such as when maximizing
  1089. or resizing from a corner */
  1090. SDL_GlobalToRelativeForWindow(window, x, y, &x, &y);
  1091. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_MOVED, x, y);
  1092. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, w, h);
  1093. }
  1094. - (void)windowWillMiniaturize:(NSNotification *)aNotification
  1095. {
  1096. isMiniaturizing = YES;
  1097. Cocoa_WaitForMiniaturizable(_data.window);
  1098. }
  1099. - (void)windowDidMiniaturize:(NSNotification *)aNotification
  1100. {
  1101. if (focusClickPending) {
  1102. focusClickPending = 0;
  1103. [self onMovingOrFocusClickPendingStateCleared];
  1104. }
  1105. isMiniaturizing = NO;
  1106. [self clearPendingWindowOperation:PENDING_OPERATION_MINIMIZE];
  1107. SDL_SendWindowEvent(_data.window, SDL_EVENT_WINDOW_MINIMIZED, 0, 0);
  1108. }
  1109. - (void)windowDidDeminiaturize:(NSNotification *)aNotification
  1110. {
  1111. // Always send restored before maximized.
  1112. SDL_SendWindowEvent(_data.window, SDL_EVENT_WINDOW_RESTORED, 0, 0);
  1113. if (Cocoa_IsWindowZoomed(_data.window)) {
  1114. SDL_SendWindowEvent(_data.window, SDL_EVENT_WINDOW_MAXIMIZED, 0, 0);
  1115. }
  1116. if ([self windowOperationIsPending:PENDING_OPERATION_ENTER_FULLSCREEN]) {
  1117. SDL_UpdateFullscreenMode(_data.window, true, true);
  1118. }
  1119. }
  1120. - (void)windowDidBecomeKey:(NSNotification *)aNotification
  1121. {
  1122. SDL_Window *window = _data.window;
  1123. // We're going to get keyboard events, since we're key.
  1124. // This needs to be done before restoring the relative mouse mode.
  1125. Cocoa_SetKeyboardFocus(window->keyboard_focus ? window->keyboard_focus : window, true);
  1126. // If we just gained focus we need the updated mouse position
  1127. if (!(window->flags & SDL_WINDOW_MOUSE_RELATIVE_MODE)) {
  1128. NSPoint point;
  1129. float x, y;
  1130. point = [_data.nswindow mouseLocationOutsideOfEventStream];
  1131. x = point.x;
  1132. y = (window->h - point.y);
  1133. if (x >= 0.0f && x < (float)window->w && y >= 0.0f && y < (float)window->h) {
  1134. SDL_SendMouseMotion(0, window, SDL_GLOBAL_MOUSE_ID, false, x, y);
  1135. }
  1136. }
  1137. // Check to see if someone updated the clipboard
  1138. Cocoa_CheckClipboardUpdate(_data.videodata);
  1139. if (isFullscreenSpace && !window->fullscreen_exclusive) {
  1140. Cocoa_ToggleFullscreenSpaceMenuVisibility(window);
  1141. }
  1142. {
  1143. const unsigned int newflags = [NSEvent modifierFlags] & NSEventModifierFlagCapsLock;
  1144. _data.videodata.modifierFlags = (_data.videodata.modifierFlags & ~NSEventModifierFlagCapsLock) | newflags;
  1145. SDL_ToggleModState(SDL_KMOD_CAPS, newflags ? true : false);
  1146. }
  1147. /* Restore fullscreen mode unless the window is deminiaturizing.
  1148. * If it is, fullscreen will be restored when deminiaturization is complete.
  1149. */
  1150. if (!(window->flags & SDL_WINDOW_MINIMIZED) &&
  1151. [self windowOperationIsPending:PENDING_OPERATION_ENTER_FULLSCREEN]) {
  1152. SDL_UpdateFullscreenMode(window, true, true);
  1153. }
  1154. }
  1155. - (void)windowDidResignKey:(NSNotification *)aNotification
  1156. {
  1157. // Some other window will get mouse events, since we're not key.
  1158. if (SDL_GetMouseFocus() == _data.window) {
  1159. SDL_SetMouseFocus(NULL);
  1160. }
  1161. // Some other window will get keyboard events, since we're not key.
  1162. if (SDL_GetKeyboardFocus() == _data.window) {
  1163. SDL_SetKeyboardFocus(NULL);
  1164. }
  1165. if (isFullscreenSpace) {
  1166. [NSMenu setMenuBarVisible:YES];
  1167. }
  1168. }
  1169. - (void)windowDidChangeBackingProperties:(NSNotification *)aNotification
  1170. {
  1171. SDL_CocoaWindowData *windata = (__bridge SDL_CocoaWindowData *)_data.window->internal;
  1172. NSView *contentView = windata.sdlContentView;
  1173. NSNumber *oldscale = [[aNotification userInfo] objectForKey:NSBackingPropertyOldScaleFactorKey];
  1174. if (inFullscreenTransition) {
  1175. return;
  1176. }
  1177. if ([oldscale doubleValue] != [_data.nswindow backingScaleFactor]) {
  1178. // Update the content scale on the window layer
  1179. // This is required to keep content scale in sync with ANGLE
  1180. contentView.layer.contentsScale = [_data.nswindow backingScaleFactor];
  1181. // Send a resize event when the backing scale factor changes.
  1182. [self windowDidResize:aNotification];
  1183. }
  1184. }
  1185. - (void)windowDidChangeScreenProfile:(NSNotification *)aNotification
  1186. {
  1187. SDL_SendWindowEvent(_data.window, SDL_EVENT_WINDOW_ICCPROF_CHANGED, 0, 0);
  1188. }
  1189. - (void)windowDidChangeScreen:(NSNotification *)aNotification
  1190. {
  1191. // printf("WINDOWDIDCHANGESCREEN\n");
  1192. #ifdef SDL_VIDEO_OPENGL
  1193. if (_data && _data.nscontexts) {
  1194. for (SDL3OpenGLContext *context in _data.nscontexts) {
  1195. [context movedToNewScreen];
  1196. }
  1197. }
  1198. #endif // SDL_VIDEO_OPENGL
  1199. }
  1200. - (void)windowWillEnterFullScreen:(NSNotification *)aNotification
  1201. {
  1202. SDL_Window *window = _data.window;
  1203. const NSUInteger flags = NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable | NSWindowStyleMaskTitled;
  1204. /* For some reason, the fullscreen window won't get any mouse button events
  1205. * without the NSWindowStyleMaskTitled flag being set when entering fullscreen,
  1206. * so it's needed even if the window is borderless.
  1207. */
  1208. SetWindowStyle(window, flags);
  1209. _data.was_zoomed = !!(window->flags & SDL_WINDOW_MAXIMIZED);
  1210. isFullscreenSpace = YES;
  1211. inFullscreenTransition = YES;
  1212. }
  1213. /* This is usually sent after an unexpected windowDidExitFullscreen if the window
  1214. * failed to become fullscreen.
  1215. *
  1216. * Since something went wrong and the current state is unknown, dump any pending events.
  1217. */
  1218. - (void)windowDidFailToEnterFullScreen:(NSNotification *)aNotification
  1219. {
  1220. [self clearAllPendingWindowOperations];
  1221. }
  1222. - (void)windowDidEnterFullScreen:(NSNotification *)aNotification
  1223. {
  1224. SDL_Window *window = _data.window;
  1225. inFullscreenTransition = NO;
  1226. isFullscreenSpace = YES;
  1227. [self clearPendingWindowOperation:PENDING_OPERATION_ENTER_FULLSCREEN];
  1228. if ([self windowOperationIsPending:PENDING_OPERATION_LEAVE_FULLSCREEN]) {
  1229. [self setFullscreenSpace:NO];
  1230. } else {
  1231. Cocoa_ToggleFullscreenSpaceMenuVisibility(window);
  1232. /* Don't recurse back into UpdateFullscreenMode() if this was hit in
  1233. * a blocking transition, as the caller is already waiting in
  1234. * UpdateFullscreenMode().
  1235. */
  1236. if (!_data.in_blocking_transition) {
  1237. SDL_UpdateFullscreenMode(window, true, false);
  1238. }
  1239. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_ENTER_FULLSCREEN, 0, 0);
  1240. _data.pending_position = NO;
  1241. _data.pending_size = NO;
  1242. /* Force the size change event in case it was delivered earlier
  1243. while the window was still animating into place.
  1244. */
  1245. window->w = 0;
  1246. window->h = 0;
  1247. [self windowDidMove:aNotification];
  1248. [self windowDidResize:aNotification];
  1249. Cocoa_UpdateClipCursor(window);
  1250. }
  1251. }
  1252. - (void)windowWillExitFullScreen:(NSNotification *)aNotification
  1253. {
  1254. SDL_Window *window = _data.window;
  1255. /* If the windowed mode borders were toggled on while in a fullscreen space,
  1256. * NSWindowStyleMaskTitled has to be cleared here, or the window can end up
  1257. * in a weird, semi-decorated state upon returning to windowed mode.
  1258. */
  1259. if (_data.border_toggled && !(window->flags & SDL_WINDOW_BORDERLESS)) {
  1260. const NSUInteger flags = NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
  1261. SetWindowStyle(window, flags);
  1262. _data.border_toggled = false;
  1263. }
  1264. isFullscreenSpace = NO;
  1265. inFullscreenTransition = YES;
  1266. }
  1267. /* This may be sent before windowDidExitFullscreen to signal that the window was
  1268. * dumped out of fullscreen with no animation.
  1269. *
  1270. * Since something went wrong and the state is unknown, dump any pending events.
  1271. */
  1272. - (void)windowDidFailToExitFullScreen:(NSNotification *)aNotification
  1273. {
  1274. [self clearAllPendingWindowOperations];
  1275. }
  1276. - (void)windowDidExitFullScreen:(NSNotification *)aNotification
  1277. {
  1278. SDL_Window *window = _data.window;
  1279. NSWindow *nswindow = _data.nswindow;
  1280. inFullscreenTransition = NO;
  1281. isFullscreenSpace = NO;
  1282. _data.fullscreen_space_requested = NO;
  1283. /* As of macOS 10.15, the window decorations can go missing sometimes after
  1284. certain fullscreen-desktop->exclusive-fullscreen->windowed mode flows
  1285. sometimes. Making sure the style mask always uses the windowed mode style
  1286. when returning to windowed mode from a space (instead of using a pending
  1287. fullscreen mode style mask) seems to work around that issue.
  1288. */
  1289. SetWindowStyle(window, GetWindowWindowedStyle(window));
  1290. /* This can happen if the window failed to enter fullscreen, as this
  1291. * may be called *before* windowDidFailToEnterFullScreen in that case.
  1292. */
  1293. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  1294. [self clearAllPendingWindowOperations];
  1295. }
  1296. /* Don't recurse back into UpdateFullscreenMode() if this was hit in
  1297. * a blocking transition, as the caller is already waiting in
  1298. * UpdateFullscreenMode().
  1299. */
  1300. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_LEAVE_FULLSCREEN, 0, 0);
  1301. if (!_data.in_blocking_transition) {
  1302. SDL_UpdateFullscreenMode(window, false, false);
  1303. }
  1304. if (window->flags & SDL_WINDOW_ALWAYS_ON_TOP) {
  1305. [nswindow setLevel:NSFloatingWindowLevel];
  1306. } else {
  1307. [nswindow setLevel:kCGNormalWindowLevel];
  1308. }
  1309. [self clearPendingWindowOperation:PENDING_OPERATION_LEAVE_FULLSCREEN];
  1310. if ([self windowOperationIsPending:PENDING_OPERATION_ENTER_FULLSCREEN]) {
  1311. [self setFullscreenSpace:YES];
  1312. } else if ([self windowOperationIsPending:PENDING_OPERATION_MINIMIZE]) {
  1313. /* There's some state that isn't quite back to normal when
  1314. * windowDidExitFullScreen triggers. For example, the minimize button on
  1315. * the title bar doesn't actually enable for another 200 milliseconds or
  1316. * so on this MacBook. Camp here and wait for that to happen before
  1317. * going on, in case we're exiting fullscreen to minimize, which need
  1318. * that window state to be normal before it will work.
  1319. */
  1320. Cocoa_WaitForMiniaturizable(_data.window);
  1321. [self addPendingWindowOperation:PENDING_OPERATION_ENTER_FULLSCREEN];
  1322. [nswindow miniaturize:nil];
  1323. } else {
  1324. // Adjust the fullscreen toggle button and re-add menu now that we're here.
  1325. if (window->flags & SDL_WINDOW_RESIZABLE) {
  1326. // resizable windows are Spaces-friendly: they get the "go fullscreen" toggle button on their titlebar.
  1327. [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
  1328. } else {
  1329. [nswindow setCollectionBehavior:NSWindowCollectionBehaviorManaged];
  1330. }
  1331. [NSMenu setMenuBarVisible:YES];
  1332. // Toggle zoom, if changed while fullscreen.
  1333. if ([self windowOperationIsPending:PENDING_OPERATION_ZOOM]) {
  1334. [self clearPendingWindowOperation:PENDING_OPERATION_ZOOM];
  1335. [nswindow zoom:nil];
  1336. _data.was_zoomed = !_data.was_zoomed;
  1337. }
  1338. if (!_data.was_zoomed) {
  1339. // Apply a pending window size, if not zoomed.
  1340. NSRect rect;
  1341. rect.origin.x = _data.pending_position ? window->pending.x : window->floating.x;
  1342. rect.origin.y = _data.pending_position ? window->pending.y : window->floating.y;
  1343. rect.size.width = _data.pending_size ? window->pending.w : window->floating.w;
  1344. rect.size.height = _data.pending_size ? window->pending.h : window->floating.h;
  1345. ConvertNSRect(&rect);
  1346. if (_data.pending_size) {
  1347. [nswindow setContentSize:rect.size];
  1348. }
  1349. if (_data.pending_position) {
  1350. [nswindow setFrameOrigin:rect.origin];
  1351. }
  1352. }
  1353. _data.pending_size = NO;
  1354. _data.pending_position = NO;
  1355. _data.was_zoomed = NO;
  1356. /* Force the size change event in case it was delivered earlier
  1357. * while the window was still animating into place.
  1358. */
  1359. window->w = 0;
  1360. window->h = 0;
  1361. [self windowDidMove:aNotification];
  1362. [self windowDidResize:aNotification];
  1363. // FIXME: Why does the window get hidden?
  1364. if (!(window->flags & SDL_WINDOW_HIDDEN)) {
  1365. Cocoa_ShowWindow(SDL_GetVideoDevice(), window);
  1366. }
  1367. Cocoa_UpdateClipCursor(window);
  1368. }
  1369. }
  1370. - (NSApplicationPresentationOptions)window:(NSWindow *)window willUseFullScreenPresentationOptions:(NSApplicationPresentationOptions)proposedOptions
  1371. {
  1372. if (_data.window->fullscreen_exclusive) {
  1373. return NSApplicationPresentationFullScreen | NSApplicationPresentationHideDock | NSApplicationPresentationHideMenuBar;
  1374. } else {
  1375. return proposedOptions;
  1376. }
  1377. }
  1378. /* We'll respond to key events by mostly doing nothing so we don't beep.
  1379. * We could handle key messages here, but we lose some in the NSApp dispatch,
  1380. * where they get converted to action messages, etc.
  1381. */
  1382. - (void)flagsChanged:(NSEvent *)theEvent
  1383. {
  1384. // Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);
  1385. /* Catch capslock in here as a special case:
  1386. https://developer.apple.com/library/archive/qa/qa1519/_index.html
  1387. Note that technote's check of keyCode doesn't work. At least on the
  1388. 10.15 beta, capslock comes through here as keycode 255, but it's safe
  1389. to send duplicate key events; SDL filters them out quickly in
  1390. SDL_SendKeyboardKey(). */
  1391. /* Also note that SDL_SendKeyboardKey expects all capslock events to be
  1392. keypresses; it won't toggle the mod state if you send a keyrelease. */
  1393. const bool osenabled = ([theEvent modifierFlags] & NSEventModifierFlagCapsLock) ? true : false;
  1394. const bool sdlenabled = (SDL_GetModState() & SDL_KMOD_CAPS) ? true : false;
  1395. if (osenabled ^ sdlenabled) {
  1396. SDL_SendKeyboardKey(0, SDL_DEFAULT_KEYBOARD_ID, 0, SDL_SCANCODE_CAPSLOCK, true);
  1397. SDL_SendKeyboardKey(0, SDL_DEFAULT_KEYBOARD_ID, 0, SDL_SCANCODE_CAPSLOCK, false);
  1398. }
  1399. }
  1400. - (void)keyDown:(NSEvent *)theEvent
  1401. {
  1402. // Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);
  1403. }
  1404. - (void)keyUp:(NSEvent *)theEvent
  1405. {
  1406. // Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);
  1407. }
  1408. /* We'll respond to selectors by doing nothing so we don't beep.
  1409. * The escape key gets converted to a "cancel" selector, etc.
  1410. */
  1411. - (void)doCommandBySelector:(SEL)aSelector
  1412. {
  1413. // NSLog(@"doCommandBySelector: %@\n", NSStringFromSelector(aSelector));
  1414. }
  1415. - (void)updateHitTest
  1416. {
  1417. SDL_Window *window = _data.window;
  1418. BOOL draggable = NO;
  1419. if (window->hit_test) {
  1420. float x, y;
  1421. SDL_Point point;
  1422. SDL_GetGlobalMouseState(&x, &y);
  1423. point.x = (int)SDL_roundf(x - window->x);
  1424. point.y = (int)SDL_roundf(y - window->y);
  1425. if (point.x >= 0 && point.x < window->w && point.y >= 0 && point.y < window->h) {
  1426. if (window->hit_test(window, &point, window->hit_test_data) == SDL_HITTEST_DRAGGABLE) {
  1427. draggable = YES;
  1428. }
  1429. }
  1430. }
  1431. if (isDragAreaRunning != draggable) {
  1432. isDragAreaRunning = draggable;
  1433. [_data.nswindow setMovableByWindowBackground:draggable];
  1434. }
  1435. }
  1436. - (BOOL)processHitTest:(NSEvent *)theEvent
  1437. {
  1438. SDL_Window *window = _data.window;
  1439. if (window->hit_test) { // if no hit-test, skip this.
  1440. const NSPoint location = [theEvent locationInWindow];
  1441. const SDL_Point point = { (int)location.x, window->h - (((int)location.y) - 1) };
  1442. const SDL_HitTestResult rc = window->hit_test(window, &point, window->hit_test_data);
  1443. if (rc == SDL_HITTEST_DRAGGABLE) {
  1444. if (!isDragAreaRunning) {
  1445. isDragAreaRunning = YES;
  1446. [_data.nswindow setMovableByWindowBackground:YES];
  1447. }
  1448. return YES; // dragging!
  1449. } else {
  1450. if (isDragAreaRunning) {
  1451. isDragAreaRunning = NO;
  1452. [_data.nswindow setMovableByWindowBackground:NO];
  1453. return YES; // was dragging, drop event.
  1454. }
  1455. }
  1456. }
  1457. return NO; // not a special area, carry on.
  1458. }
  1459. static void Cocoa_SendMouseButtonClicks(SDL_Mouse *mouse, NSEvent *theEvent, SDL_Window *window, Uint8 button, bool down)
  1460. {
  1461. SDL_MouseID mouseID = SDL_DEFAULT_MOUSE_ID;
  1462. //const int clicks = (int)[theEvent clickCount];
  1463. SDL_Window *focus = SDL_GetKeyboardFocus();
  1464. // macOS will send non-left clicks to background windows without raising them, so we need to
  1465. // temporarily adjust the mouse position when this happens, as `mouse` will be tracking
  1466. // the position in the currently-focused window. We don't (currently) send a mousemove
  1467. // event for the background window, this just makes sure the button is reported at the
  1468. // correct position in its own event.
  1469. if (focus && ([theEvent window] == ((__bridge SDL_CocoaWindowData *)focus->internal).nswindow)) {
  1470. //SDL_SendMouseButtonClicks(Cocoa_GetEventTimestamp([theEvent timestamp]), window, mouseID, button, down, clicks);
  1471. SDL_SendMouseButton(Cocoa_GetEventTimestamp([theEvent timestamp]), window, mouseID, button, down);
  1472. } else {
  1473. const float orig_x = mouse->x;
  1474. const float orig_y = mouse->y;
  1475. const NSPoint point = [theEvent locationInWindow];
  1476. mouse->x = (int)point.x;
  1477. mouse->y = (int)(window->h - point.y);
  1478. //SDL_SendMouseButtonClicks(Cocoa_GetEventTimestamp([theEvent timestamp]), window, mouseID, button, down, clicks);
  1479. SDL_SendMouseButton(Cocoa_GetEventTimestamp([theEvent timestamp]), window, mouseID, button, down);
  1480. mouse->x = orig_x;
  1481. mouse->y = orig_y;
  1482. }
  1483. }
  1484. - (void)mouseDown:(NSEvent *)theEvent
  1485. {
  1486. if (Cocoa_HandlePenEvent(_data, theEvent)) {
  1487. return; // pen code handled it.
  1488. }
  1489. SDL_Mouse *mouse = SDL_GetMouse();
  1490. int button;
  1491. if (!mouse) {
  1492. return;
  1493. }
  1494. // Ignore events that aren't inside the client area (i.e. title bar.)
  1495. if ([theEvent window]) {
  1496. NSRect windowRect = [[[theEvent window] contentView] frame];
  1497. if (!NSMouseInRect([theEvent locationInWindow], windowRect, NO)) {
  1498. return;
  1499. }
  1500. }
  1501. switch ([theEvent buttonNumber]) {
  1502. case 0:
  1503. if (([theEvent modifierFlags] & NSEventModifierFlagControl) &&
  1504. GetHintCtrlClickEmulateRightClick()) {
  1505. wasCtrlLeft = YES;
  1506. button = SDL_BUTTON_RIGHT;
  1507. } else {
  1508. wasCtrlLeft = NO;
  1509. button = SDL_BUTTON_LEFT;
  1510. }
  1511. break;
  1512. case 1:
  1513. button = SDL_BUTTON_RIGHT;
  1514. break;
  1515. case 2:
  1516. button = SDL_BUTTON_MIDDLE;
  1517. break;
  1518. default:
  1519. button = (int)[theEvent buttonNumber] + 1;
  1520. break;
  1521. }
  1522. if (button == SDL_BUTTON_LEFT && [self processHitTest:theEvent]) {
  1523. SDL_SendWindowEvent(_data.window, SDL_EVENT_WINDOW_HIT_TEST, 0, 0);
  1524. return; // dragging, drop event.
  1525. }
  1526. Cocoa_SendMouseButtonClicks(mouse, theEvent, _data.window, button, true);
  1527. }
  1528. - (void)rightMouseDown:(NSEvent *)theEvent
  1529. {
  1530. [self mouseDown:theEvent];
  1531. }
  1532. - (void)otherMouseDown:(NSEvent *)theEvent
  1533. {
  1534. [self mouseDown:theEvent];
  1535. }
  1536. - (void)mouseUp:(NSEvent *)theEvent
  1537. {
  1538. if (Cocoa_HandlePenEvent(_data, theEvent)) {
  1539. return; // pen code handled it.
  1540. }
  1541. SDL_Mouse *mouse = SDL_GetMouse();
  1542. int button;
  1543. if (!mouse) {
  1544. return;
  1545. }
  1546. switch ([theEvent buttonNumber]) {
  1547. case 0:
  1548. if (wasCtrlLeft) {
  1549. button = SDL_BUTTON_RIGHT;
  1550. wasCtrlLeft = NO;
  1551. } else {
  1552. button = SDL_BUTTON_LEFT;
  1553. }
  1554. break;
  1555. case 1:
  1556. button = SDL_BUTTON_RIGHT;
  1557. break;
  1558. case 2:
  1559. button = SDL_BUTTON_MIDDLE;
  1560. break;
  1561. default:
  1562. button = (int)[theEvent buttonNumber] + 1;
  1563. break;
  1564. }
  1565. if (button == SDL_BUTTON_LEFT && [self processHitTest:theEvent]) {
  1566. SDL_SendWindowEvent(_data.window, SDL_EVENT_WINDOW_HIT_TEST, 0, 0);
  1567. return; // stopped dragging, drop event.
  1568. }
  1569. Cocoa_SendMouseButtonClicks(mouse, theEvent, _data.window, button, false);
  1570. }
  1571. - (void)rightMouseUp:(NSEvent *)theEvent
  1572. {
  1573. [self mouseUp:theEvent];
  1574. }
  1575. - (void)otherMouseUp:(NSEvent *)theEvent
  1576. {
  1577. [self mouseUp:theEvent];
  1578. }
  1579. - (void)mouseMoved:(NSEvent *)theEvent
  1580. {
  1581. if (Cocoa_HandlePenEvent(_data, theEvent)) {
  1582. return; // pen code handled it.
  1583. }
  1584. SDL_MouseID mouseID = SDL_DEFAULT_MOUSE_ID;
  1585. SDL_Mouse *mouse = SDL_GetMouse();
  1586. NSPoint point;
  1587. float x, y;
  1588. SDL_Window *window;
  1589. NSView *contentView;
  1590. if (!mouse) {
  1591. return;
  1592. }
  1593. if (!Cocoa_GetMouseFocus()) {
  1594. // The mouse is no longer over any window in the application
  1595. SDL_SetMouseFocus(NULL);
  1596. return;
  1597. }
  1598. window = _data.window;
  1599. contentView = _data.sdlContentView;
  1600. point = [theEvent locationInWindow];
  1601. if ([contentView mouse:[contentView convertPoint:point fromView:nil] inRect:[contentView bounds]] &&
  1602. [NSCursor currentCursor] != Cocoa_GetDesiredCursor()) {
  1603. // The wrong cursor is on screen, fix it. This fixes an macOS bug that is only known to
  1604. // occur in fullscreen windows on the built-in displays of newer MacBooks with camera
  1605. // notches. When the mouse is moved near the top of such a window (within about 44 units)
  1606. // and then moved back down, the cursor rects aren't respected.
  1607. [_data.nswindow invalidateCursorRectsForView:contentView];
  1608. }
  1609. if (window->flags & SDL_WINDOW_TRANSPARENT) {
  1610. [self updateIgnoreMouseState:theEvent];
  1611. }
  1612. if ([self processHitTest:theEvent]) {
  1613. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_HIT_TEST, 0, 0);
  1614. return; // dragging, drop event.
  1615. }
  1616. if (mouse->relative_mode) {
  1617. return;
  1618. }
  1619. x = point.x;
  1620. y = (window->h - point.y);
  1621. // On macOS 26 if you move away from a space and then back, mouse motion events will have incorrect
  1622. // values at the top of the screen. The global mouse position query is still correct, so we'll fall
  1623. // back to that until this is fixed by Apple. Mouse button events are interestingly not affected.
  1624. if (@available(macOS 26.0, *)) {
  1625. if ([_data.listener isInFullscreenSpace]) {
  1626. int posx = 0, posy = 0;
  1627. SDL_GetWindowPosition(window, &posx, &posy);
  1628. SDL_GetGlobalMouseState(&x, &y);
  1629. x -= posx;
  1630. y -= posy;
  1631. }
  1632. }
  1633. if (NSAppKitVersionNumber >= NSAppKitVersionNumber10_13_2) {
  1634. // Mouse grab is taken care of by the confinement rect
  1635. } else {
  1636. CGPoint cgpoint;
  1637. if (ShouldAdjustCoordinatesForGrab(window) &&
  1638. AdjustCoordinatesForGrab(window, window->x + x, window->y + y, &cgpoint)) {
  1639. Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
  1640. CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
  1641. CGAssociateMouseAndMouseCursorPosition(YES);
  1642. }
  1643. }
  1644. SDL_SendMouseMotion(Cocoa_GetEventTimestamp([theEvent timestamp]), window, mouseID, false, x, y);
  1645. }
  1646. - (void)mouseDragged:(NSEvent *)theEvent
  1647. {
  1648. [self mouseMoved:theEvent];
  1649. }
  1650. - (void)rightMouseDragged:(NSEvent *)theEvent
  1651. {
  1652. [self mouseMoved:theEvent];
  1653. }
  1654. - (void)otherMouseDragged:(NSEvent *)theEvent
  1655. {
  1656. [self mouseMoved:theEvent];
  1657. }
  1658. - (void)scrollWheel:(NSEvent *)theEvent
  1659. {
  1660. Cocoa_HandleMouseWheel(_data.window, theEvent);
  1661. }
  1662. - (BOOL)isTouchFromTrackpad:(NSEvent *)theEvent
  1663. {
  1664. SDL_Window *window = _data.window;
  1665. SDL_CocoaVideoData *videodata = ((__bridge SDL_CocoaWindowData *)window->internal).videodata;
  1666. /* if this a MacBook trackpad, we'll make input look like a synthesized
  1667. event. This is backwards from reality, but better matches user
  1668. expectations. You can make it look like a generic touch device instead
  1669. with the SDL_HINT_TRACKPAD_IS_TOUCH_ONLY hint. */
  1670. BOOL istrackpad = NO;
  1671. if (!videodata.trackpad_is_touch_only) {
  1672. @try {
  1673. istrackpad = ([theEvent subtype] == NSEventSubtypeMouseEvent);
  1674. }
  1675. @catch (NSException *e) {
  1676. /* if NSEvent type doesn't have subtype, such as NSEventTypeBeginGesture on
  1677. * macOS 10.5 to 10.10, then NSInternalInconsistencyException is thrown.
  1678. * This still prints a message to terminal so catching it's not an ideal solution.
  1679. *
  1680. * *** Assertion failure in -[NSEvent subtype]
  1681. */
  1682. }
  1683. }
  1684. return istrackpad;
  1685. }
  1686. - (void)touchesBeganWithEvent:(NSEvent *)theEvent
  1687. {
  1688. NSSet *touches;
  1689. SDL_TouchID touchID;
  1690. int existingTouchCount;
  1691. const BOOL istrackpad = [self isTouchFromTrackpad:theEvent];
  1692. touches = [theEvent touchesMatchingPhase:NSTouchPhaseAny inView:nil];
  1693. touchID = istrackpad ? SDL_MOUSE_TOUCHID : (SDL_TouchID)(intptr_t)[[touches anyObject] device];
  1694. existingTouchCount = 0;
  1695. for (NSTouch *touch in touches) {
  1696. if ([touch phase] != NSTouchPhaseBegan) {
  1697. existingTouchCount++;
  1698. }
  1699. }
  1700. if (existingTouchCount == 0) {
  1701. int numFingers;
  1702. SDL_Finger **fingers = SDL_GetTouchFingers(touchID, &numFingers);
  1703. if (fingers) {
  1704. DLog("Reset Lost Fingers: %d", numFingers);
  1705. for (--numFingers; numFingers >= 0; --numFingers) {
  1706. const SDL_Finger *finger = fingers[numFingers];
  1707. /* trackpad touches have no window. If we really wanted one we could
  1708. * use the window that has mouse or keyboard focus.
  1709. * Sending a null window currently also prevents synthetic mouse
  1710. * events from being generated from touch events.
  1711. */
  1712. SDL_Window *window = NULL;
  1713. SDL_SendTouch(Cocoa_GetEventTimestamp([theEvent timestamp]), touchID, finger->id, window, SDL_EVENT_FINGER_CANCELED, 0, 0, 0);
  1714. }
  1715. SDL_free(fingers);
  1716. }
  1717. }
  1718. DLog("Began Fingers: %lu .. existing: %d", (unsigned long)[touches count], existingTouchCount);
  1719. [self handleTouches:NSTouchPhaseBegan withEvent:theEvent];
  1720. }
  1721. - (void)touchesMovedWithEvent:(NSEvent *)theEvent
  1722. {
  1723. [self handleTouches:NSTouchPhaseMoved withEvent:theEvent];
  1724. }
  1725. - (void)touchesEndedWithEvent:(NSEvent *)theEvent
  1726. {
  1727. [self handleTouches:NSTouchPhaseEnded withEvent:theEvent];
  1728. }
  1729. - (void)touchesCancelledWithEvent:(NSEvent *)theEvent
  1730. {
  1731. [self handleTouches:NSTouchPhaseCancelled withEvent:theEvent];
  1732. }
  1733. - (void)magnifyWithEvent:(NSEvent *)theEvent
  1734. {
  1735. switch ([theEvent phase]) {
  1736. case NSEventPhaseBegan:
  1737. SDL_SendPinch(SDL_EVENT_PINCH_BEGIN, Cocoa_GetEventTimestamp([theEvent timestamp]), NULL, 0);
  1738. break;
  1739. case NSEventPhaseChanged:
  1740. {
  1741. CGFloat scale = 1.0f + [theEvent magnification];
  1742. SDL_SendPinch(SDL_EVENT_PINCH_UPDATE, Cocoa_GetEventTimestamp([theEvent timestamp]), NULL, scale);
  1743. }
  1744. break;
  1745. case NSEventPhaseEnded:
  1746. case NSEventPhaseCancelled:
  1747. SDL_SendPinch(SDL_EVENT_PINCH_END, Cocoa_GetEventTimestamp([theEvent timestamp]), NULL, 0);
  1748. break;
  1749. default:
  1750. break;
  1751. }
  1752. }
  1753. - (void)handleTouches:(NSTouchPhase)phase withEvent:(NSEvent *)theEvent
  1754. {
  1755. NSSet *touches = [theEvent touchesMatchingPhase:phase inView:nil];
  1756. const BOOL istrackpad = [self isTouchFromTrackpad:theEvent];
  1757. SDL_FingerID fingerId;
  1758. float x, y;
  1759. for (NSTouch *touch in touches) {
  1760. const SDL_TouchID touchId = istrackpad ? SDL_MOUSE_TOUCHID : (SDL_TouchID)(uintptr_t)[touch device];
  1761. SDL_TouchDeviceType devtype = SDL_TOUCH_DEVICE_INDIRECT_ABSOLUTE;
  1762. /* trackpad touches have no window. If we really wanted one we could
  1763. * use the window that has mouse or keyboard focus.
  1764. * Sending a null window currently also prevents synthetic mouse events
  1765. * from being generated from touch events.
  1766. */
  1767. SDL_Window *window = NULL;
  1768. /* TODO: Before implementing direct touch support here, we need to
  1769. * figure out whether the OS generates mouse events from them on its
  1770. * own. If it does, we should prevent SendTouch from generating
  1771. * synthetic mouse events for these touches itself (while also
  1772. * sending a window.) It will also need to use normalized window-
  1773. * relative coordinates via [touch locationInView:].
  1774. */
  1775. if ([touch type] == NSTouchTypeDirect) {
  1776. continue;
  1777. }
  1778. if (SDL_AddTouch(touchId, devtype, "") < 0) {
  1779. return;
  1780. }
  1781. fingerId = (SDL_FingerID)(uintptr_t)[touch identity];
  1782. x = [touch normalizedPosition].x;
  1783. y = [touch normalizedPosition].y;
  1784. // Make the origin the upper left instead of the lower left
  1785. y = 1.0f - y;
  1786. switch (phase) {
  1787. case NSTouchPhaseBegan:
  1788. SDL_SendTouch(Cocoa_GetEventTimestamp([theEvent timestamp]), touchId, fingerId, window, SDL_EVENT_FINGER_DOWN, x, y, 1.0f);
  1789. break;
  1790. case NSTouchPhaseEnded:
  1791. SDL_SendTouch(Cocoa_GetEventTimestamp([theEvent timestamp]), touchId, fingerId, window, SDL_EVENT_FINGER_UP, x, y, 1.0f);
  1792. break;
  1793. case NSTouchPhaseCancelled:
  1794. SDL_SendTouch(Cocoa_GetEventTimestamp([theEvent timestamp]), touchId, fingerId, window, SDL_EVENT_FINGER_CANCELED, x, y, 1.0f);
  1795. break;
  1796. case NSTouchPhaseMoved:
  1797. SDL_SendTouchMotion(Cocoa_GetEventTimestamp([theEvent timestamp]), touchId, fingerId, window, x, y, 1.0f);
  1798. break;
  1799. default:
  1800. break;
  1801. }
  1802. }
  1803. }
  1804. - (void)tabletProximity:(NSEvent *)theEvent
  1805. {
  1806. Cocoa_HandlePenEvent(_data, theEvent);
  1807. }
  1808. - (void)tabletPoint:(NSEvent *)theEvent
  1809. {
  1810. Cocoa_HandlePenEvent(_data, theEvent);
  1811. }
  1812. @end
  1813. @interface SDL3View : NSView
  1814. {
  1815. SDL_Window *_sdlWindow;
  1816. NSTrackingArea *_trackingArea; // only used on macOS <= 11.0
  1817. }
  1818. - (void)setSDLWindow:(SDL_Window *)window;
  1819. // The default implementation doesn't pass rightMouseDown to responder chain
  1820. - (void)rightMouseDown:(NSEvent *)theEvent;
  1821. - (BOOL)mouseDownCanMoveWindow;
  1822. - (void)drawRect:(NSRect)dirtyRect;
  1823. - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent;
  1824. - (BOOL)wantsUpdateLayer;
  1825. - (void)updateLayer;
  1826. - (void)updateTrackingAreas;
  1827. @end
  1828. @implementation SDL3View
  1829. - (void)setSDLWindow:(SDL_Window *)window
  1830. {
  1831. _sdlWindow = window;
  1832. }
  1833. /* this is used on older macOS revisions, and newer ones which emulate old
  1834. NSOpenGLContext behaviour while still using a layer under the hood. 10.8 and
  1835. later use updateLayer, up until 10.14.2 or so, which uses drawRect without
  1836. a GraphicsContext and with a layer active instead (for OpenGL contexts). */
  1837. - (void)drawRect:(NSRect)dirtyRect
  1838. {
  1839. /* Force the graphics context to clear to black so we don't get a flash of
  1840. white until the app is ready to draw. In practice on modern macOS, this
  1841. only gets called for window creation and other extraordinary events. */
  1842. BOOL transparent = (_sdlWindow->flags & SDL_WINDOW_TRANSPARENT) != 0;
  1843. if ([NSGraphicsContext currentContext]) {
  1844. NSColor *fillColor = transparent ? [NSColor clearColor] : [NSColor blackColor];
  1845. [fillColor setFill];
  1846. NSRectFill(dirtyRect);
  1847. } else if (self.layer) {
  1848. CFStringRef color = transparent ? kCGColorClear : kCGColorBlack;
  1849. self.layer.backgroundColor = CGColorGetConstantColor(color);
  1850. }
  1851. Cocoa_SendExposedEventIfVisible(_sdlWindow);
  1852. }
  1853. - (BOOL)wantsUpdateLayer
  1854. {
  1855. return YES;
  1856. }
  1857. // This is also called when a Metal layer is active.
  1858. - (void)updateLayer
  1859. {
  1860. /* Force the graphics context to clear to black so we don't get a flash of
  1861. white until the app is ready to draw. In practice on modern macOS, this
  1862. only gets called for window creation and other extraordinary events. */
  1863. BOOL transparent = (_sdlWindow->flags & SDL_WINDOW_TRANSPARENT) != 0;
  1864. CFStringRef color = transparent ? kCGColorClear : kCGColorBlack;
  1865. self.layer.backgroundColor = CGColorGetConstantColor(color);
  1866. ScheduleContextUpdates((__bridge SDL_CocoaWindowData *)_sdlWindow->internal);
  1867. Cocoa_SendExposedEventIfVisible(_sdlWindow);
  1868. }
  1869. - (void)rightMouseDown:(NSEvent *)theEvent
  1870. {
  1871. [[self nextResponder] rightMouseDown:theEvent];
  1872. }
  1873. - (BOOL)mouseDownCanMoveWindow
  1874. {
  1875. /* Always say YES, but this doesn't do anything until we call
  1876. -[NSWindow setMovableByWindowBackground:YES], which we ninja-toggle
  1877. during mouse events when we're using a drag area. */
  1878. return YES;
  1879. }
  1880. - (void)resetCursorRects
  1881. {
  1882. [super resetCursorRects];
  1883. [self addCursorRect:[self bounds]
  1884. cursor:Cocoa_GetDesiredCursor()];
  1885. }
  1886. - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
  1887. {
  1888. if (_sdlWindow->flags & SDL_WINDOW_POPUP_MENU) {
  1889. return YES;
  1890. } else if (SDL_GetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH)) {
  1891. return SDL_GetHintBoolean(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, false);
  1892. } else {
  1893. return SDL_GetHintBoolean("SDL_MAC_MOUSE_FOCUS_CLICKTHROUGH", false);
  1894. }
  1895. }
  1896. // NSTrackingArea is how Cocoa tells you when the mouse cursor has entered or
  1897. // left certain regions. We put one over our entire window so we know when
  1898. // it has "mouse focus."
  1899. - (void)updateTrackingAreas
  1900. {
  1901. [super updateTrackingAreas];
  1902. SDL_CocoaWindowData *windata = (__bridge SDL_CocoaWindowData *)_sdlWindow->internal;
  1903. if (_trackingArea) {
  1904. [self removeTrackingArea:_trackingArea];
  1905. }
  1906. _trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] options:NSTrackingMouseEnteredAndExited|NSTrackingActiveAlways owner:windata.listener userInfo:nil];
  1907. [self addTrackingArea:_trackingArea];
  1908. }
  1909. @end
  1910. static void Cocoa_UpdateMouseFocus()
  1911. {
  1912. const NSPoint mouseLocation = [NSEvent mouseLocation];
  1913. // Find the topmost window under the pointer and send a motion event if it is an SDL window.
  1914. [NSApp enumerateWindowsWithOptions:NSWindowListOrderedFrontToBack
  1915. usingBlock:^(NSWindow *nswin, BOOL *stop) {
  1916. NSRect r = [nswin contentRectForFrameRect:[nswin frame]];
  1917. if (NSPointInRect(mouseLocation, r)) {
  1918. SDL_VideoDevice *vid = SDL_GetVideoDevice();
  1919. SDL_Window *sdlwindow;
  1920. for (sdlwindow = vid->windows; sdlwindow; sdlwindow = sdlwindow->next) {
  1921. if (nswin == ((__bridge SDL_CocoaWindowData *)sdlwindow->internal).nswindow) {
  1922. break;
  1923. }
  1924. }
  1925. *stop = YES;
  1926. if (sdlwindow) {
  1927. SDL_CocoaVideoData *videodata = (__bridge SDL_CocoaVideoData *)vid->internal;
  1928. int wx, wy;
  1929. SDL_RelativeToGlobalForWindow(sdlwindow, sdlwindow->x, sdlwindow->y, &wx, &wy);
  1930. // Calculate the cursor coordinates relative to the window.
  1931. const float dx = mouseLocation.x - wx;
  1932. const float dy = (videodata.mainDisplayHeight - mouseLocation.y) - wy;
  1933. SDL_SendMouseMotion(0, sdlwindow, SDL_GLOBAL_MOUSE_ID, false, dx, dy);
  1934. }
  1935. }
  1936. }];
  1937. }
  1938. static bool SetupWindowData(SDL_VideoDevice *_this, SDL_Window *window, NSWindow *nswindow, NSView *nsview)
  1939. {
  1940. @autoreleasepool {
  1941. SDL_CocoaVideoData *videodata = (__bridge SDL_CocoaVideoData *)_this->internal;
  1942. SDL_CocoaWindowData *data;
  1943. // Allocate the window data
  1944. data = [[SDL_CocoaWindowData alloc] init];
  1945. if (!data) {
  1946. return SDL_OutOfMemory();
  1947. }
  1948. window->internal = (SDL_WindowData *)CFBridgingRetain(data);
  1949. data.window = window;
  1950. data.nswindow = nswindow;
  1951. data.videodata = videodata;
  1952. data.window_number = nswindow.windowNumber;
  1953. data.nscontexts = [[NSMutableArray alloc] init];
  1954. data.sdlContentView = nsview;
  1955. data.viewport = [data.sdlContentView bounds];
  1956. if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) {
  1957. // This gives us the correct viewport for a Retina-enabled view.
  1958. data.viewport = [data.sdlContentView convertRectToBacking:data.viewport];
  1959. }
  1960. // Create an event listener for the window
  1961. data.listener = [[SDL3Cocoa_WindowListener alloc] init];
  1962. // Fill in the SDL window with the window data
  1963. {
  1964. int x, y;
  1965. NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
  1966. ConvertNSRect(&rect);
  1967. SDL_GlobalToRelativeForWindow(window, (int)rect.origin.x, (int)rect.origin.y, &x, &y);
  1968. window->x = x;
  1969. window->y = y;
  1970. window->w = (int)rect.size.width;
  1971. window->h = (int)rect.size.height;
  1972. }
  1973. // Set up the listener after we create the view
  1974. [data.listener listen:data];
  1975. if ([nswindow isVisible]) {
  1976. window->flags &= ~SDL_WINDOW_HIDDEN;
  1977. } else {
  1978. window->flags |= SDL_WINDOW_HIDDEN;
  1979. }
  1980. {
  1981. unsigned long style = [nswindow styleMask];
  1982. /* NSWindowStyleMaskBorderless is zero, and it's possible to be
  1983. Resizeable _and_ borderless, so we can't do a simple bitwise AND
  1984. of NSWindowStyleMaskBorderless here. */
  1985. if ((style & ~(NSWindowStyleMaskResizable | NSWindowStyleMaskMiniaturizable)) == NSWindowStyleMaskBorderless) {
  1986. window->flags |= SDL_WINDOW_BORDERLESS;
  1987. } else {
  1988. window->flags &= ~SDL_WINDOW_BORDERLESS;
  1989. }
  1990. if (style & NSWindowStyleMaskResizable) {
  1991. window->flags |= SDL_WINDOW_RESIZABLE;
  1992. } else {
  1993. window->flags &= ~SDL_WINDOW_RESIZABLE;
  1994. }
  1995. }
  1996. // isZoomed always returns true if the window is not resizable
  1997. if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
  1998. window->flags |= SDL_WINDOW_MAXIMIZED;
  1999. } else {
  2000. window->flags &= ~SDL_WINDOW_MAXIMIZED;
  2001. }
  2002. if ([nswindow isMiniaturized]) {
  2003. window->flags |= SDL_WINDOW_MINIMIZED;
  2004. } else {
  2005. window->flags &= ~SDL_WINDOW_MINIMIZED;
  2006. }
  2007. if (window->parent) {
  2008. NSWindow *nsparent = ((__bridge SDL_CocoaWindowData *)window->parent->internal).nswindow;
  2009. [nsparent addChildWindow:nswindow ordered:NSWindowAbove];
  2010. /* FIXME: Should not need to call addChildWindow then orderOut.
  2011. Attaching a hidden child window to a hidden parent window will cause the child window
  2012. to show when the parent does. We therefore shouldn't attach the child window here as we're
  2013. going to do so when the child window is explicitly shown later but skipping the addChildWindow
  2014. entirely causes the child window to not get key focus correctly the first time it's shown. Adding
  2015. then immediately ordering out (removing) the window does work. */
  2016. if (window->flags & SDL_WINDOW_HIDDEN) {
  2017. [nswindow orderOut:nil];
  2018. }
  2019. }
  2020. if (!SDL_WINDOW_IS_POPUP(window)) {
  2021. if ([nswindow isKeyWindow]) {
  2022. window->flags |= SDL_WINDOW_INPUT_FOCUS;
  2023. Cocoa_SetKeyboardFocus(data.window, true);
  2024. }
  2025. } else {
  2026. if (window->flags & SDL_WINDOW_TOOLTIP) {
  2027. [nswindow setIgnoresMouseEvents:YES];
  2028. [nswindow setAcceptsMouseMovedEvents:NO];
  2029. } else if ((window->flags & SDL_WINDOW_POPUP_MENU) && !(window->flags & SDL_WINDOW_HIDDEN)) {
  2030. if (!(window->flags & SDL_WINDOW_NOT_FOCUSABLE)) {
  2031. Cocoa_SetKeyboardFocus(window, true);
  2032. }
  2033. Cocoa_UpdateMouseFocus();
  2034. }
  2035. }
  2036. if (nswindow.isOpaque) {
  2037. window->flags &= ~SDL_WINDOW_TRANSPARENT;
  2038. } else {
  2039. window->flags |= SDL_WINDOW_TRANSPARENT;
  2040. }
  2041. /* SDL_CocoaWindowData will be holding a strong reference to the NSWindow, and
  2042. * it will also call [NSWindow close] in DestroyWindow before releasing the
  2043. * NSWindow, so the extra release provided by releasedWhenClosed isn't
  2044. * necessary. */
  2045. nswindow.releasedWhenClosed = NO;
  2046. /* Prevents the window's "window device" from being destroyed when it is
  2047. * hidden. See http://www.mikeash.com/pyblog/nsopenglcontext-and-one-shot.html
  2048. */
  2049. [nswindow setOneShot:NO];
  2050. if (window->flags & SDL_WINDOW_EXTERNAL) {
  2051. // Query the title from the existing window
  2052. NSString *title = [nswindow title];
  2053. if (title) {
  2054. window->title = SDL_strdup([title UTF8String]);
  2055. }
  2056. }
  2057. SDL_PropertiesID props = SDL_GetWindowProperties(window);
  2058. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, (__bridge void *)data.nswindow);
  2059. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_COCOA_METAL_VIEW_TAG_NUMBER, SDL_METALVIEW_TAG);
  2060. // All done!
  2061. return true;
  2062. }
  2063. }
  2064. bool Cocoa_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID create_props)
  2065. {
  2066. @autoreleasepool {
  2067. SDL_CocoaVideoData *videodata = (__bridge SDL_CocoaVideoData *)_this->internal;
  2068. const void *data = SDL_GetPointerProperty(create_props, "sdl2-compat.external_window", NULL);
  2069. NSWindow *nswindow = nil;
  2070. NSView *nsview = nil;
  2071. if (data) {
  2072. if ([(__bridge id)data isKindOfClass:[NSWindow class]]) {
  2073. nswindow = (__bridge NSWindow *)data;
  2074. } else if ([(__bridge id)data isKindOfClass:[NSView class]]) {
  2075. nsview = (__bridge NSView *)data;
  2076. } else {
  2077. SDL_assert(false);
  2078. }
  2079. } else {
  2080. nswindow = (__bridge NSWindow *)SDL_GetPointerProperty(create_props, SDL_PROP_WINDOW_CREATE_COCOA_WINDOW_POINTER, NULL);
  2081. nsview = (__bridge NSView *)SDL_GetPointerProperty(create_props, SDL_PROP_WINDOW_CREATE_COCOA_VIEW_POINTER, NULL);
  2082. }
  2083. if (nswindow && !nsview) {
  2084. nsview = [nswindow contentView];
  2085. }
  2086. if (nsview && !nswindow) {
  2087. nswindow = [nsview window];
  2088. }
  2089. if (nswindow) {
  2090. window->flags |= SDL_WINDOW_EXTERNAL;
  2091. } else {
  2092. int x, y;
  2093. NSScreen *screen;
  2094. NSRect rect, screenRect;
  2095. NSUInteger style;
  2096. SDL3View *contentView;
  2097. SDL_RelativeToGlobalForWindow(window, window->x, window->y, &x, &y);
  2098. rect.origin.x = x;
  2099. rect.origin.y = y;
  2100. rect.size.width = window->w;
  2101. rect.size.height = window->h;
  2102. ConvertNSRect(&rect);
  2103. style = GetWindowStyle(window);
  2104. // Figure out which screen to place this window
  2105. screen = ScreenForRect(&rect);
  2106. screenRect = [screen frame];
  2107. rect.origin.x -= screenRect.origin.x;
  2108. rect.origin.y -= screenRect.origin.y;
  2109. // Constrain the popup
  2110. if (SDL_WINDOW_IS_POPUP(window) && window->constrain_popup) {
  2111. if (rect.origin.x + rect.size.width > screenRect.origin.x + screenRect.size.width) {
  2112. rect.origin.x -= (rect.origin.x + rect.size.width) - (screenRect.origin.x + screenRect.size.width);
  2113. }
  2114. if (rect.origin.y + rect.size.height > screenRect.origin.y + screenRect.size.height) {
  2115. rect.origin.y -= (rect.origin.y + rect.size.height) - (screenRect.origin.y + screenRect.size.height);
  2116. }
  2117. rect.origin.x = SDL_max(rect.origin.x, screenRect.origin.x);
  2118. rect.origin.y = SDL_max(rect.origin.y, screenRect.origin.y);
  2119. }
  2120. @try {
  2121. nswindow = [[SDL3Window alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:NO screen:screen];
  2122. }
  2123. @catch (NSException *e) {
  2124. return SDL_SetError("%s", [[e reason] UTF8String]);
  2125. }
  2126. [nswindow setColorSpace:[NSColorSpace sRGBColorSpace]];
  2127. [nswindow setTabbingMode:NSWindowTabbingModeDisallowed];
  2128. // we put fullscreen desktop windows in their own Space, without a toggle button or menubar, later
  2129. if ((window->flags & SDL_WINDOW_RESIZABLE) && videodata.allow_spaces) {
  2130. // resizable windows are Spaces-friendly: they get the "go fullscreen" toggle button on their titlebar.
  2131. [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
  2132. } else {
  2133. [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenNone];
  2134. }
  2135. // Create a default view for this window
  2136. rect = [nswindow contentRectForFrameRect:[nswindow frame]];
  2137. contentView = [[SDL3View alloc] initWithFrame:rect];
  2138. [contentView setSDLWindow:window];
  2139. nsview = contentView;
  2140. }
  2141. if (window->flags & SDL_WINDOW_ALWAYS_ON_TOP) {
  2142. [nswindow setLevel:NSFloatingWindowLevel];
  2143. }
  2144. if (window->flags & SDL_WINDOW_TRANSPARENT) {
  2145. nswindow.opaque = NO;
  2146. nswindow.hasShadow = NO;
  2147. nswindow.backgroundColor = [NSColor clearColor];
  2148. }
  2149. // We still support OpenGL as long as Apple offers it, deprecated or not, so disable deprecation warnings about it.
  2150. #ifdef __clang__
  2151. #pragma clang diagnostic push
  2152. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  2153. #endif
  2154. /* Note: as of the macOS 10.15 SDK, this defaults to YES instead of NO when
  2155. * the NSHighResolutionCapable boolean is set in Info.plist. */
  2156. BOOL highdpi = (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) ? YES : NO;
  2157. [nsview setWantsBestResolutionOpenGLSurface:highdpi];
  2158. #ifdef __clang__
  2159. #pragma clang diagnostic pop
  2160. #endif
  2161. #ifdef SDL_VIDEO_OPENGL_ES2
  2162. #ifdef SDL_VIDEO_OPENGL_EGL
  2163. if ((window->flags & SDL_WINDOW_OPENGL) &&
  2164. _this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
  2165. [nsview setWantsLayer:TRUE];
  2166. if ((window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY)) {
  2167. nsview.layer.contentsScale = nswindow.screen.backingScaleFactor;
  2168. } else {
  2169. nsview.layer.contentsScale = 1;
  2170. }
  2171. }
  2172. #endif // SDL_VIDEO_OPENGL_EGL
  2173. #endif // SDL_VIDEO_OPENGL_ES2
  2174. [nswindow setContentView:nsview];
  2175. if (!SetupWindowData(_this, window, nswindow, nsview)) {
  2176. return false;
  2177. }
  2178. if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2179. return true;
  2180. }
  2181. // The rest of this macro mess is for OpenGL or OpenGL ES windows
  2182. #ifdef SDL_VIDEO_OPENGL_ES2
  2183. if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
  2184. #ifdef SDL_VIDEO_OPENGL_EGL
  2185. if (!Cocoa_GLES_SetupWindow(_this, window)) {
  2186. Cocoa_DestroyWindow(_this, window);
  2187. return false;
  2188. }
  2189. return true;
  2190. #else
  2191. return SDL_SetError("Could not create GLES window surface (EGL support not configured)");
  2192. #endif // SDL_VIDEO_OPENGL_EGL
  2193. }
  2194. #endif // SDL_VIDEO_OPENGL_ES2
  2195. return true;
  2196. }
  2197. }
  2198. void Cocoa_SetWindowTitle(SDL_VideoDevice *_this, SDL_Window *window)
  2199. {
  2200. @autoreleasepool {
  2201. const char *title = window->title ? window->title : "";
  2202. NSWindow *nswindow = ((__bridge SDL_CocoaWindowData *)window->internal).nswindow;
  2203. NSString *string = [[NSString alloc] initWithUTF8String:title];
  2204. [nswindow setTitle:string];
  2205. }
  2206. }
  2207. bool Cocoa_SetWindowIcon(SDL_VideoDevice *_this, SDL_Window *window, SDL_Surface *icon)
  2208. {
  2209. @autoreleasepool {
  2210. NSImage *nsimage = Cocoa_CreateImage(icon);
  2211. if (nsimage) {
  2212. [NSApp setApplicationIconImage:nsimage];
  2213. return true;
  2214. }
  2215. return SDL_SetError("Unable to set the window's icon");
  2216. }
  2217. }
  2218. bool Cocoa_SetWindowPosition(SDL_VideoDevice *_this, SDL_Window *window)
  2219. {
  2220. @autoreleasepool {
  2221. SDL_CocoaWindowData *windata = (__bridge SDL_CocoaWindowData *)window->internal;
  2222. NSWindow *nswindow = windata.nswindow;
  2223. NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
  2224. BOOL fullscreen = (window->flags & SDL_WINDOW_FULLSCREEN) ? YES : NO;
  2225. int x, y;
  2226. if ([windata.listener isInFullscreenSpaceTransition]) {
  2227. windata.pending_position = YES;
  2228. return true;
  2229. }
  2230. if (!(window->flags & SDL_WINDOW_MAXIMIZED)) {
  2231. if (fullscreen) {
  2232. SDL_VideoDisplay *display = SDL_GetVideoDisplayForFullscreenWindow(window);
  2233. SDL_Rect r;
  2234. SDL_GetDisplayBounds(display->id, &r);
  2235. rect.origin.x = r.x;
  2236. rect.origin.y = r.y;
  2237. } else {
  2238. SDL_RelativeToGlobalForWindow(window, window->pending.x, window->pending.y, &x, &y);
  2239. rect.origin.x = x;
  2240. rect.origin.y = y;
  2241. }
  2242. ConvertNSRect(&rect);
  2243. // Position and constrain the popup
  2244. if (SDL_WINDOW_IS_POPUP(window) && window->constrain_popup) {
  2245. NSRect screenRect = [ScreenForRect(&rect) frame];
  2246. if (rect.origin.x + rect.size.width > screenRect.origin.x + screenRect.size.width) {
  2247. rect.origin.x -= (rect.origin.x + rect.size.width) - (screenRect.origin.x + screenRect.size.width);
  2248. }
  2249. if (rect.origin.y + rect.size.height > screenRect.origin.y + screenRect.size.height) {
  2250. rect.origin.y -= (rect.origin.y + rect.size.height) - (screenRect.origin.y + screenRect.size.height);
  2251. }
  2252. rect.origin.x = SDL_max(rect.origin.x, screenRect.origin.x);
  2253. rect.origin.y = SDL_max(rect.origin.y, screenRect.origin.y);
  2254. }
  2255. [nswindow setFrameOrigin:rect.origin];
  2256. ScheduleContextUpdates(windata);
  2257. }
  2258. }
  2259. return true;
  2260. }
  2261. void Cocoa_SetWindowSize(SDL_VideoDevice *_this, SDL_Window *window)
  2262. {
  2263. @autoreleasepool {
  2264. SDL_CocoaWindowData *windata = (__bridge SDL_CocoaWindowData *)window->internal;
  2265. NSWindow *nswindow = windata.nswindow;
  2266. if ([windata.listener isInFullscreenSpace] ||
  2267. [windata.listener isInFullscreenSpaceTransition]) {
  2268. windata.pending_size = YES;
  2269. return;
  2270. }
  2271. if (!Cocoa_IsWindowZoomed(window)) {
  2272. int x, y;
  2273. NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
  2274. /* Cocoa will resize the window from the bottom-left rather than the
  2275. * top-left when -[nswindow setContentSize:] is used, so we must set the
  2276. * entire frame based on the new size, in order to preserve the position.
  2277. */
  2278. SDL_RelativeToGlobalForWindow(window, window->floating.x, window->floating.y, &x, &y);
  2279. rect.origin.x = x;
  2280. rect.origin.y = y;
  2281. rect.size.width = window->pending.w;
  2282. rect.size.height = window->pending.h;
  2283. ConvertNSRect(&rect);
  2284. [nswindow setFrame:[nswindow frameRectForContentRect:rect] display:YES];
  2285. ScheduleContextUpdates(windata);
  2286. } else {
  2287. // Can't set the window size.
  2288. window->last_size_pending = false;
  2289. }
  2290. }
  2291. }
  2292. void Cocoa_SetWindowMinimumSize(SDL_VideoDevice *_this, SDL_Window *window)
  2293. {
  2294. @autoreleasepool {
  2295. SDL_CocoaWindowData *windata = (__bridge SDL_CocoaWindowData *)window->internal;
  2296. NSSize minSize;
  2297. minSize.width = window->min_w;
  2298. minSize.height = window->min_h;
  2299. [windata.nswindow setContentMinSize:minSize];
  2300. }
  2301. }
  2302. void Cocoa_SetWindowMaximumSize(SDL_VideoDevice *_this, SDL_Window *window)
  2303. {
  2304. @autoreleasepool {
  2305. SDL_CocoaWindowData *windata = (__bridge SDL_CocoaWindowData *)window->internal;
  2306. NSSize maxSize;
  2307. maxSize.width = window->max_w ? window->max_w : CGFLOAT_MAX;
  2308. maxSize.height = window->max_h ? window->max_h : CGFLOAT_MAX;
  2309. [windata.nswindow setContentMaxSize:maxSize];
  2310. }
  2311. }
  2312. void Cocoa_SetWindowAspectRatio(SDL_VideoDevice *_this, SDL_Window *window)
  2313. {
  2314. @autoreleasepool {
  2315. SDL_CocoaWindowData *windata = (__bridge SDL_CocoaWindowData *)window->internal;
  2316. if (window->min_aspect > 0.0f && window->min_aspect == window->max_aspect) {
  2317. int numerator = 0, denominator = 1;
  2318. SDL_CalculateFraction(window->max_aspect, &numerator, &denominator);
  2319. [windata.nswindow setContentAspectRatio:NSMakeSize(numerator, denominator)];
  2320. } else {
  2321. [windata.nswindow setContentAspectRatio:NSMakeSize(0, 0)];
  2322. }
  2323. }
  2324. }
  2325. void Cocoa_GetWindowSizeInPixels(SDL_VideoDevice *_this, SDL_Window *window, int *w, int *h)
  2326. {
  2327. @autoreleasepool {
  2328. SDL_CocoaWindowData *windata = (__bridge SDL_CocoaWindowData *)window->internal;
  2329. *w = (int)windata.viewport.size.width;
  2330. *h = (int)windata.viewport.size.height;
  2331. }
  2332. }
  2333. void Cocoa_ShowWindow(SDL_VideoDevice *_this, SDL_Window *window)
  2334. {
  2335. @autoreleasepool {
  2336. SDL_CocoaWindowData *windowData = ((__bridge SDL_CocoaWindowData *)window->internal);
  2337. NSWindow *nswindow = windowData.nswindow;
  2338. bool bActivate = SDL_GetHintBoolean(SDL_HINT_WINDOW_ACTIVATE_WHEN_SHOWN, true);
  2339. if (![nswindow isMiniaturized]) {
  2340. [windowData.listener pauseVisibleObservation];
  2341. if (window->parent) {
  2342. NSWindow *nsparent = ((__bridge SDL_CocoaWindowData *)window->parent->internal).nswindow;
  2343. [nsparent addChildWindow:nswindow ordered:NSWindowAbove];
  2344. if (window->flags & SDL_WINDOW_MODAL) {
  2345. Cocoa_SetWindowModal(_this, window, true);
  2346. }
  2347. }
  2348. if (!SDL_WINDOW_IS_POPUP(window)) {
  2349. if (bActivate) {
  2350. [nswindow makeKeyAndOrderFront:nil];
  2351. } else {
  2352. // Order this window below the key window if we're not activating it
  2353. if ([NSApp keyWindow]) {
  2354. [nswindow orderWindow:NSWindowBelow relativeTo:[[NSApp keyWindow] windowNumber]];
  2355. }
  2356. }
  2357. } else if (window->flags & SDL_WINDOW_POPUP_MENU) {
  2358. if (!(window->flags & SDL_WINDOW_NOT_FOCUSABLE)) {
  2359. Cocoa_SetKeyboardFocus(window, true);
  2360. }
  2361. Cocoa_UpdateMouseFocus();
  2362. }
  2363. }
  2364. [nswindow setIsVisible:YES];
  2365. [windowData.listener resumeVisibleObservation];
  2366. }
  2367. }
  2368. void Cocoa_HideWindow(SDL_VideoDevice *_this, SDL_Window *window)
  2369. {
  2370. @autoreleasepool {
  2371. NSWindow *nswindow = ((__bridge SDL_CocoaWindowData *)window->internal).nswindow;
  2372. const BOOL waskey = [nswindow isKeyWindow];
  2373. /* orderOut has no effect on miniaturized windows, so close must be used to remove
  2374. * the window from the desktop and window list in this case.
  2375. *
  2376. * SDL holds a strong reference to the window (oneShot/releasedWhenClosed are 'NO'),
  2377. * and calling 'close' doesn't send a 'windowShouldClose' message, so it's safe to
  2378. * use for this purpose as nothing is implicitly released.
  2379. */
  2380. if (![nswindow isMiniaturized]) {
  2381. [nswindow orderOut:nil];
  2382. } else {
  2383. [nswindow close];
  2384. }
  2385. /* If this window is the source of a modal session, end it when
  2386. * hidden, or other windows will be prevented from closing.
  2387. */
  2388. Cocoa_SetWindowModal(_this, window, false);
  2389. // Transfer keyboard focus back to the parent when closing a popup menu
  2390. if ((window->flags & SDL_WINDOW_POPUP_MENU) && !(window->flags & SDL_WINDOW_NOT_FOCUSABLE)) {
  2391. SDL_Window *new_focus;
  2392. const bool set_focus = SDL_ShouldRelinquishPopupFocus(window, &new_focus);
  2393. Cocoa_SetKeyboardFocus(new_focus, set_focus);
  2394. Cocoa_UpdateMouseFocus();
  2395. } else if (window->parent && waskey) {
  2396. /* Key status is not automatically set on the parent when a child is hidden. Check if the
  2397. * child window was key, and set the first visible parent to be key if so.
  2398. */
  2399. SDL_Window *new_focus = window->parent;
  2400. while (new_focus->parent != NULL && (new_focus->is_hiding || new_focus->is_destroying)) {
  2401. new_focus = new_focus->parent;
  2402. }
  2403. if (new_focus) {
  2404. NSWindow *newkey = ((__bridge SDL_CocoaWindowData *)window->internal).nswindow;
  2405. [newkey makeKeyAndOrderFront:nil];
  2406. }
  2407. }
  2408. }
  2409. }
  2410. void Cocoa_RaiseWindow(SDL_VideoDevice *_this, SDL_Window *window)
  2411. {
  2412. @autoreleasepool {
  2413. SDL_CocoaWindowData *windowData = ((__bridge SDL_CocoaWindowData *)window->internal);
  2414. NSWindow *nswindow = windowData.nswindow;
  2415. bool bActivate = SDL_GetHintBoolean(SDL_HINT_WINDOW_ACTIVATE_WHEN_RAISED, true);
  2416. /* makeKeyAndOrderFront: has the side-effect of deminiaturizing and showing
  2417. a minimized or hidden window, so check for that before showing it.
  2418. */
  2419. [windowData.listener pauseVisibleObservation];
  2420. if (![nswindow isMiniaturized] && [nswindow isVisible]) {
  2421. if (window->parent) {
  2422. NSWindow *nsparent = ((__bridge SDL_CocoaWindowData *)window->parent->internal).nswindow;
  2423. [nsparent addChildWindow:nswindow ordered:NSWindowAbove];
  2424. }
  2425. if (!SDL_WINDOW_IS_POPUP(window)) {
  2426. if (bActivate) {
  2427. [NSApp activateIgnoringOtherApps:YES];
  2428. [nswindow makeKeyAndOrderFront:nil];
  2429. } else {
  2430. [nswindow orderFront:nil];
  2431. }
  2432. } else {
  2433. if (bActivate) {
  2434. [nswindow makeKeyWindow];
  2435. }
  2436. }
  2437. }
  2438. [windowData.listener resumeVisibleObservation];
  2439. }
  2440. }
  2441. void Cocoa_MaximizeWindow(SDL_VideoDevice *_this, SDL_Window *window)
  2442. {
  2443. @autoreleasepool {
  2444. SDL_CocoaWindowData *windata = (__bridge SDL_CocoaWindowData *)window->internal;
  2445. NSWindow *nswindow = windata.nswindow;
  2446. if ([windata.listener windowOperationIsPending:(PENDING_OPERATION_ENTER_FULLSCREEN | PENDING_OPERATION_LEAVE_FULLSCREEN)] ||
  2447. [windata.listener isInFullscreenSpaceTransition]) {
  2448. Cocoa_SyncWindow(_this, window);
  2449. }
  2450. if (!(window->flags & SDL_WINDOW_FULLSCREEN) &&
  2451. ![windata.listener isInFullscreenSpaceTransition] &&
  2452. ![windata.listener isInFullscreenSpace]) {
  2453. [nswindow zoom:nil];
  2454. ScheduleContextUpdates(windata);
  2455. } else if (!windata.was_zoomed) {
  2456. [windata.listener addPendingWindowOperation:PENDING_OPERATION_ZOOM];
  2457. } else {
  2458. [windata.listener clearPendingWindowOperation:PENDING_OPERATION_ZOOM];
  2459. }
  2460. }
  2461. }
  2462. void Cocoa_MinimizeWindow(SDL_VideoDevice *_this, SDL_Window *window)
  2463. {
  2464. @autoreleasepool {
  2465. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  2466. NSWindow *nswindow = data.nswindow;
  2467. [data.listener addPendingWindowOperation:PENDING_OPERATION_MINIMIZE];
  2468. if ([data.listener isInFullscreenSpace] || (window->flags & SDL_WINDOW_FULLSCREEN)) {
  2469. [data.listener addPendingWindowOperation:PENDING_OPERATION_LEAVE_FULLSCREEN];
  2470. SDL_UpdateFullscreenMode(window, false, true);
  2471. } else if ([data.listener isInFullscreenSpaceTransition]) {
  2472. [data.listener addPendingWindowOperation:PENDING_OPERATION_LEAVE_FULLSCREEN];
  2473. } else {
  2474. [nswindow miniaturize:nil];
  2475. }
  2476. }
  2477. }
  2478. void Cocoa_RestoreWindow(SDL_VideoDevice *_this, SDL_Window *window)
  2479. {
  2480. @autoreleasepool {
  2481. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  2482. NSWindow *nswindow = data.nswindow;
  2483. if (([data.listener windowOperationIsPending:(PENDING_OPERATION_ENTER_FULLSCREEN | PENDING_OPERATION_LEAVE_FULLSCREEN)] &&
  2484. ![data.nswindow isMiniaturized]) || [data.listener isInFullscreenSpaceTransition]) {
  2485. Cocoa_SyncWindow(_this, window);
  2486. }
  2487. [data.listener clearPendingWindowOperation:(PENDING_OPERATION_MINIMIZE)];
  2488. if (!(window->flags & SDL_WINDOW_FULLSCREEN) &&
  2489. ![data.listener isInFullscreenSpaceTransition] &&
  2490. ![data.listener isInFullscreenSpace]) {
  2491. if ([nswindow isMiniaturized]) {
  2492. [nswindow deminiaturize:nil];
  2493. } else if (Cocoa_IsWindowZoomed(window)) {
  2494. [nswindow zoom:nil];
  2495. }
  2496. } else if (data.was_zoomed) {
  2497. [data.listener addPendingWindowOperation:PENDING_OPERATION_ZOOM];
  2498. } else {
  2499. [data.listener clearPendingWindowOperation:PENDING_OPERATION_ZOOM];
  2500. }
  2501. }
  2502. }
  2503. void Cocoa_SetWindowBordered(SDL_VideoDevice *_this, SDL_Window *window, bool bordered)
  2504. {
  2505. @autoreleasepool {
  2506. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  2507. // If the window is in or transitioning to/from fullscreen, this will be set on leave.
  2508. if (!(window->flags & SDL_WINDOW_FULLSCREEN) && ![data.listener isInFullscreenSpaceTransition]) {
  2509. if (SetWindowStyle(window, GetWindowStyle(window))) {
  2510. if (bordered) {
  2511. Cocoa_SetWindowTitle(_this, window); // this got blanked out.
  2512. }
  2513. }
  2514. } else {
  2515. data.border_toggled = true;
  2516. }
  2517. Cocoa_UpdateClipCursor(window);
  2518. }
  2519. }
  2520. void Cocoa_SetWindowResizable(SDL_VideoDevice *_this, SDL_Window *window, bool resizable)
  2521. {
  2522. @autoreleasepool {
  2523. /* Don't set this if we're in or transitioning to/from a space!
  2524. * The window will get permanently stuck if resizable is false.
  2525. * -flibit
  2526. */
  2527. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  2528. SDL3Cocoa_WindowListener *listener = data.listener;
  2529. NSWindow *nswindow = data.nswindow;
  2530. SDL_CocoaVideoData *videodata = data.videodata;
  2531. if (![listener isInFullscreenSpace] && ![listener isInFullscreenSpaceTransition]) {
  2532. SetWindowStyle(window, GetWindowStyle(window));
  2533. }
  2534. if (resizable && videodata.allow_spaces) {
  2535. // resizable windows are Spaces-friendly: they get the "go fullscreen" toggle button on their titlebar.
  2536. [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
  2537. } else {
  2538. [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenNone];
  2539. }
  2540. }
  2541. }
  2542. void Cocoa_SetWindowAlwaysOnTop(SDL_VideoDevice *_this, SDL_Window *window, bool on_top)
  2543. {
  2544. @autoreleasepool {
  2545. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  2546. NSWindow *nswindow = data.nswindow;
  2547. // If the window is in or transitioning to/from fullscreen, this will be set on leave.
  2548. if (!(window->flags & SDL_WINDOW_FULLSCREEN) && ![data.listener isInFullscreenSpaceTransition]) {
  2549. if (on_top) {
  2550. [nswindow setLevel:NSFloatingWindowLevel];
  2551. } else {
  2552. [nswindow setLevel:kCGNormalWindowLevel];
  2553. }
  2554. }
  2555. }
  2556. }
  2557. SDL_FullscreenResult Cocoa_SetWindowFullscreen(SDL_VideoDevice *_this, SDL_Window *window, SDL_VideoDisplay *display, SDL_FullscreenOp fullscreen)
  2558. {
  2559. @autoreleasepool {
  2560. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  2561. NSWindow *nswindow = data.nswindow;
  2562. NSRect rect;
  2563. // This is a synchronous operation, so always clear the pending flags.
  2564. [data.listener clearPendingWindowOperation:PENDING_OPERATION_ENTER_FULLSCREEN | PENDING_OPERATION_LEAVE_FULLSCREEN];
  2565. // The view responder chain gets messed with during setStyleMask
  2566. if ([data.sdlContentView nextResponder] == data.listener) {
  2567. [data.sdlContentView setNextResponder:nil];
  2568. }
  2569. if (fullscreen) {
  2570. SDL_Rect bounds;
  2571. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  2572. data.was_zoomed = !!(window->flags & SDL_WINDOW_MAXIMIZED);
  2573. }
  2574. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_ENTER_FULLSCREEN, 0, 0);
  2575. Cocoa_GetDisplayBounds(_this, display, &bounds);
  2576. rect.origin.x = bounds.x;
  2577. rect.origin.y = bounds.y;
  2578. rect.size.width = bounds.w;
  2579. rect.size.height = bounds.h;
  2580. ConvertNSRect(&rect);
  2581. /* Hack to fix origin on macOS 10.4
  2582. This is no longer needed as of macOS 10.15, according to bug 4822.
  2583. */
  2584. if (SDL_floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_14) {
  2585. NSRect screenRect = [[nswindow screen] frame];
  2586. if (screenRect.size.height >= 1.0f) {
  2587. rect.origin.y += (screenRect.size.height - rect.size.height);
  2588. }
  2589. }
  2590. [nswindow setStyleMask:NSWindowStyleMaskBorderless];
  2591. } else {
  2592. NSRect frameRect;
  2593. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_LEAVE_FULLSCREEN, 0, 0);
  2594. rect.origin.x = data.was_zoomed ? window->windowed.x : window->floating.x;
  2595. rect.origin.y = data.was_zoomed ? window->windowed.y : window->floating.y;
  2596. rect.size.width = data.was_zoomed ? window->windowed.w : window->floating.w;
  2597. rect.size.height = data.was_zoomed ? window->windowed.h : window->floating.h;
  2598. ConvertNSRect(&rect);
  2599. /* The window is not meant to be fullscreen, but its flags might have a
  2600. * fullscreen bit set if it's scheduled to go fullscreen immediately
  2601. * after. Always using the windowed mode style here works around bugs in
  2602. * macOS 10.15 where the window doesn't properly restore the windowed
  2603. * mode decorations after exiting fullscreen-desktop, when the window
  2604. * was created as fullscreen-desktop. */
  2605. [nswindow setStyleMask:GetWindowWindowedStyle(window)];
  2606. // Hack to restore window decorations on macOS 10.10
  2607. frameRect = [nswindow frame];
  2608. [nswindow setFrame:NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width + 1, frameRect.size.height) display:NO];
  2609. [nswindow setFrame:frameRect display:NO];
  2610. }
  2611. // The view responder chain gets messed with during setStyleMask
  2612. if ([data.sdlContentView nextResponder] != data.listener) {
  2613. [data.sdlContentView setNextResponder:data.listener];
  2614. }
  2615. [nswindow setContentSize:rect.size];
  2616. [nswindow setFrameOrigin:rect.origin];
  2617. // When the window style changes the title is cleared
  2618. if (!fullscreen) {
  2619. Cocoa_SetWindowTitle(_this, window);
  2620. data.was_zoomed = NO;
  2621. if ([data.listener windowOperationIsPending:PENDING_OPERATION_ZOOM]) {
  2622. [data.listener clearPendingWindowOperation:PENDING_OPERATION_ZOOM];
  2623. [nswindow zoom:nil];
  2624. }
  2625. }
  2626. if (SDL_ShouldAllowTopmost() && fullscreen) {
  2627. // OpenGL is rendering to the window, so make it visible!
  2628. [nswindow setLevel:kCGMainMenuWindowLevel + 1];
  2629. } else if (window->flags & SDL_WINDOW_ALWAYS_ON_TOP) {
  2630. [nswindow setLevel:NSFloatingWindowLevel];
  2631. } else {
  2632. [nswindow setLevel:kCGNormalWindowLevel];
  2633. }
  2634. if ([nswindow isVisible] || fullscreen) {
  2635. [data.listener pauseVisibleObservation];
  2636. [nswindow makeKeyAndOrderFront:nil];
  2637. [data.listener resumeVisibleObservation];
  2638. }
  2639. // Update the safe area insets
  2640. // The view never seems to reflect the safe area, so we'll use the screen instead
  2641. if (@available(macOS 12.0, *)) {
  2642. if (fullscreen) {
  2643. NSScreen *screen = [nswindow screen];
  2644. SDL_SetWindowSafeAreaInsets(data.window,
  2645. (int)SDL_ceilf(screen.safeAreaInsets.left),
  2646. (int)SDL_ceilf(screen.safeAreaInsets.right),
  2647. (int)SDL_ceilf(screen.safeAreaInsets.top),
  2648. (int)SDL_ceilf(screen.safeAreaInsets.bottom));
  2649. } else {
  2650. SDL_SetWindowSafeAreaInsets(data.window, 0, 0, 0, 0);
  2651. }
  2652. }
  2653. /* When coming out of fullscreen to minimize, this needs to happen after the window
  2654. * is made key again, or it won't minimize on 15.0 (Sequoia).
  2655. */
  2656. if (!fullscreen && [data.listener windowOperationIsPending:PENDING_OPERATION_MINIMIZE]) {
  2657. Cocoa_WaitForMiniaturizable(window);
  2658. [data.listener addPendingWindowOperation:PENDING_OPERATION_ENTER_FULLSCREEN];
  2659. [data.listener clearPendingWindowOperation:PENDING_OPERATION_MINIMIZE];
  2660. [nswindow miniaturize:nil];
  2661. }
  2662. ScheduleContextUpdates(data);
  2663. Cocoa_SyncWindow(_this, window);
  2664. Cocoa_UpdateClipCursor(window);
  2665. }
  2666. return SDL_FULLSCREEN_SUCCEEDED;
  2667. }
  2668. void *Cocoa_GetWindowICCProfile(SDL_VideoDevice *_this, SDL_Window *window, size_t *size)
  2669. {
  2670. @autoreleasepool {
  2671. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  2672. NSWindow *nswindow = data.nswindow;
  2673. NSScreen *screen = [nswindow screen];
  2674. NSData *iccProfileData = nil;
  2675. void *retIccProfileData = NULL;
  2676. if (screen == nil) {
  2677. SDL_SetError("Could not get screen of window.");
  2678. return NULL;
  2679. }
  2680. if ([screen colorSpace] == nil) {
  2681. SDL_SetError("Could not get colorspace information of screen.");
  2682. return NULL;
  2683. }
  2684. iccProfileData = [[screen colorSpace] ICCProfileData];
  2685. if (iccProfileData == nil) {
  2686. SDL_SetError("Could not get ICC profile data.");
  2687. return NULL;
  2688. }
  2689. retIccProfileData = SDL_malloc([iccProfileData length]);
  2690. if (!retIccProfileData) {
  2691. return NULL;
  2692. }
  2693. [iccProfileData getBytes:retIccProfileData length:[iccProfileData length]];
  2694. *size = [iccProfileData length];
  2695. return retIccProfileData;
  2696. }
  2697. }
  2698. SDL_DisplayID Cocoa_GetDisplayForWindow(SDL_VideoDevice *_this, SDL_Window *window)
  2699. {
  2700. @autoreleasepool {
  2701. NSScreen *screen;
  2702. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  2703. // Not recognized via CHECK_WINDOW_MAGIC
  2704. if (data == nil) {
  2705. // Don't set the error here, it hides other errors and is ignored anyway
  2706. // return SDL_SetError("Window data not set");
  2707. return 0;
  2708. }
  2709. // NSWindow.screen may be nil when the window is off-screen.
  2710. screen = data.nswindow.screen;
  2711. if (screen != nil) {
  2712. // https://developer.apple.com/documentation/appkit/nsscreen/1388360-devicedescription?language=objc
  2713. CGDirectDisplayID displayid = [[screen.deviceDescription objectForKey:@"NSScreenNumber"] unsignedIntValue];
  2714. SDL_VideoDisplay *display = Cocoa_FindSDLDisplayByCGDirectDisplayID(_this, displayid);
  2715. if (display) {
  2716. return display->id;
  2717. }
  2718. }
  2719. // The higher level code will use other logic to find the display
  2720. return 0;
  2721. }
  2722. }
  2723. bool Cocoa_SetWindowMouseRect(SDL_VideoDevice *_this, SDL_Window *window)
  2724. {
  2725. Cocoa_UpdateClipCursor(window);
  2726. return true;
  2727. }
  2728. bool Cocoa_SetWindowMouseGrab(SDL_VideoDevice *_this, SDL_Window *window, bool grabbed)
  2729. {
  2730. @autoreleasepool {
  2731. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  2732. Cocoa_UpdateClipCursor(window);
  2733. if (data && (window->flags & SDL_WINDOW_FULLSCREEN) != 0) {
  2734. if (SDL_ShouldAllowTopmost() && (window->flags & SDL_WINDOW_INPUT_FOCUS) && ![data.listener isInFullscreenSpace]) {
  2735. // OpenGL is rendering to the window, so make it visible!
  2736. // Doing this in 10.11 while in a Space breaks things (bug #3152)
  2737. [data.nswindow setLevel:kCGMainMenuWindowLevel + 1];
  2738. } else if (window->flags & SDL_WINDOW_ALWAYS_ON_TOP) {
  2739. [data.nswindow setLevel:NSFloatingWindowLevel];
  2740. } else {
  2741. [data.nswindow setLevel:kCGNormalWindowLevel];
  2742. }
  2743. }
  2744. }
  2745. return true;
  2746. }
  2747. void Cocoa_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window)
  2748. {
  2749. @autoreleasepool {
  2750. SDL_CocoaWindowData *data = (SDL_CocoaWindowData *)CFBridgingRelease(window->internal);
  2751. if (data) {
  2752. #ifdef SDL_VIDEO_OPENGL
  2753. NSArray *contexts;
  2754. #endif // SDL_VIDEO_OPENGL
  2755. SDL_Window *topmost = GetParentToplevelWindow(window);
  2756. /* Reset the input focus of the root window if this window is still set as keyboard focus.
  2757. * SDL_DestroyWindow will have already taken care of reassigning focus if this is the SDL
  2758. * keyboard focus, this ensures that an inactive window with this window set as input focus
  2759. * does not try to reference it the next time it gains focus.
  2760. */
  2761. if (topmost->keyboard_focus == window) {
  2762. SDL_Window *new_focus = window;
  2763. while (SDL_WINDOW_IS_POPUP(new_focus) && (new_focus->is_hiding || new_focus->is_destroying)) {
  2764. new_focus = new_focus->parent;
  2765. }
  2766. topmost->keyboard_focus = new_focus;
  2767. }
  2768. if ([data.listener isInFullscreenSpace]) {
  2769. [NSMenu setMenuBarVisible:YES];
  2770. }
  2771. [data.listener close];
  2772. data.listener = nil;
  2773. if (!(window->flags & SDL_WINDOW_EXTERNAL)) {
  2774. // Release the content view to avoid further updateLayer callbacks
  2775. [data.nswindow setContentView:nil];
  2776. [data.nswindow close];
  2777. }
  2778. #ifdef SDL_VIDEO_OPENGL
  2779. contexts = [data.nscontexts copy];
  2780. for (SDL3OpenGLContext *context in contexts) {
  2781. // Calling setWindow:NULL causes the context to remove itself from the context list.
  2782. [context setWindow:NULL];
  2783. }
  2784. #endif // SDL_VIDEO_OPENGL
  2785. }
  2786. window->internal = NULL;
  2787. }
  2788. }
  2789. bool Cocoa_SetWindowFullscreenSpace(SDL_Window *window, bool state, bool blocking)
  2790. {
  2791. @autoreleasepool {
  2792. bool succeeded = false;
  2793. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  2794. if (state) {
  2795. data.fullscreen_space_requested = YES;
  2796. }
  2797. data.in_blocking_transition = blocking;
  2798. if ([data.listener setFullscreenSpace:(state ? YES : NO)]) {
  2799. if (blocking) {
  2800. const int maxattempts = 3;
  2801. int attempt = 0;
  2802. while (++attempt <= maxattempts) {
  2803. /* Wait for the transition to complete, so application changes
  2804. take effect properly (e.g. setting the window size, etc.)
  2805. */
  2806. const int limit = 10000;
  2807. int count = 0;
  2808. while ([data.listener isInFullscreenSpaceTransition]) {
  2809. if (++count == limit) {
  2810. // Uh oh, transition isn't completing. Should we assert?
  2811. break;
  2812. }
  2813. SDL_Delay(1);
  2814. SDL_PumpEvents();
  2815. }
  2816. if ([data.listener isInFullscreenSpace] == (state ? YES : NO)) {
  2817. break;
  2818. }
  2819. // Try again, the last attempt was interrupted by user gestures
  2820. if (![data.listener setFullscreenSpace:(state ? YES : NO)]) {
  2821. break; // ???
  2822. }
  2823. }
  2824. }
  2825. // Return TRUE to prevent non-space fullscreen logic from running
  2826. succeeded = true;
  2827. }
  2828. data.in_blocking_transition = NO;
  2829. return succeeded;
  2830. }
  2831. }
  2832. bool Cocoa_SetWindowHitTest(SDL_Window *window, bool enabled)
  2833. {
  2834. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  2835. [data.listener updateHitTest];
  2836. return true;
  2837. }
  2838. void Cocoa_AcceptDragAndDrop(SDL_Window *window, bool accept)
  2839. {
  2840. @autoreleasepool {
  2841. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  2842. if (accept) {
  2843. [data.nswindow registerForDraggedTypes:@[ (NSString *)kUTTypeFileURL,
  2844. (NSString *)kUTTypeUTF8PlainText ]];
  2845. } else {
  2846. [data.nswindow unregisterDraggedTypes];
  2847. }
  2848. }
  2849. }
  2850. bool Cocoa_SetWindowParent(SDL_VideoDevice *_this, SDL_Window *window, SDL_Window *parent)
  2851. {
  2852. @autoreleasepool {
  2853. SDL_CocoaWindowData *child_data = (__bridge SDL_CocoaWindowData *)window->internal;
  2854. // Remove an existing parent.
  2855. if (child_data.nswindow.parentWindow) {
  2856. NSWindow *nsparent = ((__bridge SDL_CocoaWindowData *)window->parent->internal).nswindow;
  2857. [nsparent removeChildWindow:child_data.nswindow];
  2858. }
  2859. if (parent) {
  2860. SDL_CocoaWindowData *parent_data = (__bridge SDL_CocoaWindowData *)parent->internal;
  2861. [parent_data.nswindow addChildWindow:child_data.nswindow ordered:NSWindowAbove];
  2862. }
  2863. }
  2864. return true;
  2865. }
  2866. bool Cocoa_SetWindowModal(SDL_VideoDevice *_this, SDL_Window *window, bool modal)
  2867. {
  2868. @autoreleasepool {
  2869. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  2870. if (data.modal_session) {
  2871. [NSApp endModalSession:data.modal_session];
  2872. data.modal_session = nil;
  2873. }
  2874. if (modal) {
  2875. data.modal_session = [NSApp beginModalSessionForWindow:data.nswindow];
  2876. }
  2877. }
  2878. return true;
  2879. }
  2880. bool Cocoa_FlashWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_FlashOperation operation)
  2881. {
  2882. @autoreleasepool {
  2883. // Note that this is app-wide and not window-specific!
  2884. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  2885. if (data.flash_request) {
  2886. [NSApp cancelUserAttentionRequest:data.flash_request];
  2887. data.flash_request = 0;
  2888. }
  2889. switch (operation) {
  2890. case SDL_FLASH_CANCEL:
  2891. // Canceled above
  2892. break;
  2893. case SDL_FLASH_BRIEFLY:
  2894. data.flash_request = [NSApp requestUserAttention:NSInformationalRequest];
  2895. break;
  2896. case SDL_FLASH_UNTIL_FOCUSED:
  2897. data.flash_request = [NSApp requestUserAttention:NSCriticalRequest];
  2898. break;
  2899. default:
  2900. return SDL_Unsupported();
  2901. }
  2902. return true;
  2903. }
  2904. }
  2905. bool Cocoa_SetWindowFocusable(SDL_VideoDevice *_this, SDL_Window *window, bool focusable)
  2906. {
  2907. if (window->flags & SDL_WINDOW_POPUP_MENU) {
  2908. if (!(window->flags & SDL_WINDOW_HIDDEN)) {
  2909. if (!focusable && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
  2910. SDL_Window *new_focus;
  2911. const bool set_focus = SDL_ShouldRelinquishPopupFocus(window, &new_focus);
  2912. Cocoa_SetKeyboardFocus(new_focus, set_focus);
  2913. } else if (focusable) {
  2914. if (SDL_ShouldFocusPopup(window)) {
  2915. Cocoa_SetKeyboardFocus(window, true);
  2916. }
  2917. }
  2918. }
  2919. }
  2920. return true; // just succeed, the real work is done elsewhere.
  2921. }
  2922. bool Cocoa_SetWindowOpacity(SDL_VideoDevice *_this, SDL_Window *window, float opacity)
  2923. {
  2924. @autoreleasepool {
  2925. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  2926. [data.nswindow setAlphaValue:opacity];
  2927. return true;
  2928. }
  2929. }
  2930. bool Cocoa_SyncWindow(SDL_VideoDevice *_this, SDL_Window *window)
  2931. {
  2932. bool result = false;
  2933. @autoreleasepool {
  2934. const Uint64 timeout = SDL_GetTicksNS() + SDL_MS_TO_NS(2500);
  2935. SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
  2936. for (;;) {
  2937. SDL_PumpEvents();
  2938. result = ![data.listener hasPendingWindowOperation];
  2939. if (result || SDL_GetTicksNS() >= timeout) {
  2940. break;
  2941. }
  2942. // Small delay before going again.
  2943. SDL_Delay(10);
  2944. }
  2945. }
  2946. return result;
  2947. }
  2948. #endif // SDL_VIDEO_DRIVER_COCOA