Mac: Better handling when someone else is the app delegate.
authorJørgen P. Tjernø <jorgen@valvesoftware.com>
Mon, 07 Oct 2013 16:01:40 -0700
changeset 7801f00cc0a8cd5d
parent 7800 76e4b6825efc
child 7802 d239ba573818
Mac: Better handling when someone else is the app delegate.
README-macosx.txt
src/video/cocoa/SDL_cocoaevents.m
     1.1 --- a/README-macosx.txt	Thu Oct 10 02:37:36 2013 -0400
     1.2 +++ b/README-macosx.txt	Mon Oct 07 16:01:40 2013 -0700
     1.3 @@ -30,6 +30,42 @@
     1.4  use the traditional autoconf/automake/make method, or use Xcode.
     1.5  
     1.6  ==============================================================================
     1.7 +Caveats for using SDL with Mac OS X
     1.8 +==============================================================================
     1.9 +
    1.10 +Some things you have to be aware of when using SDL on Mac OS X:
    1.11 +
    1.12 +- If you register your own NSApplicationDelegate (using [NSApp setDelegate:]),
    1.13 +  SDL will not register its own. This means that SDL will not terminate using
    1.14 +  SDL_Quit if it receives a termination request, it will terminate like a 
    1.15 +  normal app, and it will not send a SDL_DROPFILE when you request to open a
    1.16 +  file with the app. To solve these issues, put the following code in your 
    1.17 +  NSApplicationDelegate implementation:
    1.18 +
    1.19 +  - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
    1.20 +  {
    1.21 +      if (SDL_GetEventState(SDL_QUIT) == SDL_ENABLE) {
    1.22 +          SDL_Event event;
    1.23 +          event.type = SDL_QUIT;
    1.24 +          SDL_PushEvent(&event);
    1.25 +      }
    1.26 +
    1.27 +      return NSTerminateCancel;
    1.28 +  }
    1.29 +
    1.30 +  - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
    1.31 +  {
    1.32 +      if (SDL_GetEventState(SDL_DROPFILE) == SDL_ENABLE) {
    1.33 +          SDL_Event event;
    1.34 +          event.type = SDL_DROPFILE;
    1.35 +          event.drop.file = SDL_strdup([filename UTF8String]);
    1.36 +          return (SDL_PushEvent(&event) > 0);
    1.37 +      }
    1.38 +
    1.39 +      return NO;
    1.40 +  }
    1.41 +
    1.42 +==============================================================================
    1.43  Using the Simple DirectMedia Layer with a traditional Makefile
    1.44  ==============================================================================
    1.45  
     2.1 --- a/src/video/cocoa/SDL_cocoaevents.m	Thu Oct 10 02:37:36 2013 -0400
     2.2 +++ b/src/video/cocoa/SDL_cocoaevents.m	Mon Oct 07 16:01:40 2013 -0700
     2.3 @@ -42,12 +42,11 @@
     2.4  @end
     2.5  
     2.6  @interface SDLAppDelegate : NSObject {
     2.7 +@public
     2.8      BOOL seenFirstActivate;
     2.9  }
    2.10  
    2.11  - (id)init;
    2.12 -- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
    2.13 -- (void)applicationDidBecomeActive:(NSNotification *)aNotification;
    2.14  @end
    2.15  
    2.16  @implementation SDLAppDelegate : NSObject
    2.17 @@ -57,18 +56,28 @@
    2.18  
    2.19      if (self) {
    2.20          seenFirstActivate = NO;
    2.21 +        [[NSNotificationCenter defaultCenter] addObserver:self
    2.22 +                                                 selector:@selector(focusSomeWindow:)
    2.23 +                                                     name:NSApplicationDidBecomeActiveNotification
    2.24 +                                                   object:nil];
    2.25      }
    2.26  
    2.27      return self;
    2.28  }
    2.29  
    2.30 +- (void)dealloc
    2.31 +{
    2.32 +    [[NSNotificationCenter defaultCenter] removeObserver:self];
    2.33 +    [super dealloc];
    2.34 +}
    2.35 +
    2.36  - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
    2.37  {
    2.38      SDL_SendQuit();
    2.39      return NSTerminateCancel;
    2.40  }
    2.41  
    2.42 -- (void)applicationDidBecomeActive:(NSNotification *)aNotification
    2.43 +- (void)focusSomeWindow:(NSNotification *)aNotification
    2.44  {
    2.45      /* HACK: Ignore the first call. The application gets a
    2.46       * applicationDidBecomeActive: a little bit after the first window is
    2.47 @@ -111,6 +120,8 @@
    2.48  }
    2.49  @end
    2.50  
    2.51 +static SDLAppDelegate *appDelegate = nil;
    2.52 +
    2.53  static NSString *
    2.54  GetApplicationName(void)
    2.55  {
    2.56 @@ -235,8 +246,17 @@
    2.57          [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
    2.58  
    2.59      }
    2.60 -    if (NSApp && ![NSApp delegate]) {
    2.61 -        [NSApp setDelegate:[[SDLAppDelegate alloc] init]];
    2.62 +    if (NSApp && !appDelegate) {
    2.63 +        appDelegate = [[SDLAppDelegate alloc] init];
    2.64 +
    2.65 +        /* If someone else has an app delegate, it means we can't turn a
    2.66 +         * termination into SDL_Quit, and we can't handle application:openFile:
    2.67 +         */
    2.68 +        if (![NSApp delegate]) {
    2.69 +            [NSApp setDelegate:appDelegate];
    2.70 +        } else {
    2.71 +            appDelegate->seenFirstActivate = YES;
    2.72 +        }
    2.73      }
    2.74      [pool release];
    2.75  }