src/video/cocoa/SDL_cocoaevents.m
changeset 9364 ad9d35983de5
parent 9087 eef2e43f60d1
child 9419 9763f689bced
     1.1 --- a/src/video/cocoa/SDL_cocoaevents.m	Thu Feb 19 19:40:58 2015 -0500
     1.2 +++ b/src/video/cocoa/SDL_cocoaevents.m	Thu Feb 19 19:55:30 2015 -0500
     1.3 @@ -27,6 +27,11 @@
     1.4  #include "../../events/SDL_events_c.h"
     1.5  #include "SDL_assert.h"
     1.6  
     1.7 +/* This define was added in the 10.9 SDK. */
     1.8 +#ifndef kIOPMAssertPreventUserIdleDisplaySleep
     1.9 +#define kIOPMAssertPreventUserIdleDisplaySleep kIOPMAssertionTypePreventUserIdleDisplaySleep
    1.10 +#endif
    1.11 +
    1.12  @interface SDLApplication : NSApplication
    1.13  
    1.14  - (void)terminate:(id)sender;
    1.15 @@ -251,17 +256,24 @@
    1.16  { @autoreleasepool
    1.17  {
    1.18      /* This can get called more than once! Be careful what you initialize! */
    1.19 -    ProcessSerialNumber psn;
    1.20 -
    1.21 -    if (!GetCurrentProcess(&psn)) {
    1.22 -        TransformProcessType(&psn, kProcessTransformToForegroundApplication);
    1.23 -        SetFrontProcess(&psn);
    1.24 -    }
    1.25  
    1.26      if (NSApp == nil) {
    1.27          [SDLApplication sharedApplication];
    1.28          SDL_assert(NSApp != nil);
    1.29  
    1.30 +#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
    1.31 +        if ([NSApp respondsToSelector:@selector(setActivationPolicy:)]) {
    1.32 +#endif
    1.33 +            [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
    1.34 +#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
    1.35 +        } else {
    1.36 +            ProcessSerialNumber psn = {0, kCurrentProcess};
    1.37 +            TransformProcessType(&psn, kProcessTransformToForegroundApplication);
    1.38 +        }
    1.39 +#endif
    1.40 +
    1.41 +        [NSApp activateIgnoringOtherApps:YES];
    1.42 +
    1.43          if ([NSApp mainMenu] == nil) {
    1.44              CreateApplicationMenus();
    1.45          }
    1.46 @@ -293,8 +305,8 @@
    1.47  { @autoreleasepool
    1.48  {
    1.49      /* Update activity every 30 seconds to prevent screensaver */
    1.50 -    if (_this->suspend_screensaver) {
    1.51 -        SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
    1.52 +    SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
    1.53 +    if (_this->suspend_screensaver && !data->screensaver_use_iopm) {
    1.54          Uint32 now = SDL_GetTicks();
    1.55          if (!data->screensaver_activity ||
    1.56              SDL_TICKS_PASSED(now, data->screensaver_activity + 30000)) {
    1.57 @@ -336,6 +348,35 @@
    1.58      }
    1.59  }}
    1.60  
    1.61 +void
    1.62 +Cocoa_SuspendScreenSaver(_THIS)
    1.63 +{ @autoreleasepool
    1.64 +{
    1.65 +    SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
    1.66 +
    1.67 +    if (!data->screensaver_use_iopm) {
    1.68 +        return;
    1.69 +    }
    1.70 +
    1.71 +    if (data->screensaver_assertion) {
    1.72 +        IOPMAssertionRelease(data->screensaver_assertion);
    1.73 +        data->screensaver_assertion = 0;
    1.74 +    }
    1.75 +
    1.76 +    if (_this->suspend_screensaver) {
    1.77 +        /* FIXME: this should ideally describe the real reason why the game
    1.78 +         * called SDL_DisableScreenSaver. Note that the name is only meant to be
    1.79 +         * seen by OS X power users. there's an additional optional human-readable
    1.80 +         * (localized) reason parameter which we don't set.
    1.81 +         */
    1.82 +        NSString *name = [GetApplicationName() stringByAppendingString:@" using SDL_DisableScreenSaver"];
    1.83 +        IOPMAssertionCreateWithDescription(kIOPMAssertPreventUserIdleDisplaySleep,
    1.84 +                                           (CFStringRef) name,
    1.85 +                                           NULL, NULL, NULL, 0, NULL,
    1.86 +                                           &data->screensaver_assertion);
    1.87 +    }
    1.88 +}}
    1.89 +
    1.90  #endif /* SDL_VIDEO_DRIVER_COCOA */
    1.91  
    1.92  /* vi: set ts=4 sw=4 expandtab: */