From: Darrell Walisser
authorSam Lantinga
Sat, 07 Dec 2002 06:48:49 +0000
changeset 5552536446a92de
parent 554 38b1a98aeb11
child 556 08588ee79a67
From: Darrell Walisser
Subject: Re: [SDL] OS X and power save

Here ya go. This works just fine. One might complain that it doesn't
generate the event until after wake as completed (there is about 5
seconds between the screen coming up and the expose event), but I think
that's OK.
src/video/quartz/SDL_QuartzEvents.m
src/video/quartz/SDL_QuartzVideo.h
src/video/quartz/SDL_QuartzVideo.m
     1.1 --- a/src/video/quartz/SDL_QuartzEvents.m	Mon Dec 02 03:11:36 2002 +0000
     1.2 +++ b/src/video/quartz/SDL_QuartzEvents.m	Sat Dec 07 06:48:49 2002 +0000
     1.3 @@ -20,6 +20,8 @@
     1.4      slouken@libsdl.org
     1.5  */
     1.6  #include <stdlib.h>	// For getenv()
     1.7 +#include <IOKit/IOMessage.h> // For wake from sleep detection
     1.8 +#include <IOKit/pwr_mgt/IOPMLib.h> // For wake from sleep detection
     1.9  #include "SDL_QuartzKeys.h"
    1.10  
    1.11  static void     QZ_InitOSKeymap (_THIS) {
    1.12 @@ -304,6 +306,44 @@
    1.13      SDL_PrivateAppActive (0, SDL_APPINPUTFOCUS);
    1.14  }
    1.15  
    1.16 +void QZ_SleepNotificationHandler (void * refcon,
    1.17 +                                  io_service_t service,
    1.18 +                                  natural_t messageType,
    1.19 +                                  void * messageArgument )
    1.20 +{
    1.21 +     SDL_VideoDevice *this = (SDL_VideoDevice*)refcon;
    1.22 +     
    1.23 +     switch(messageType)
    1.24 +     {
    1.25 +         case kIOMessageSystemWillSleep:
    1.26 +             IOAllowPowerChange(powerConnection, (long) messageArgument);
    1.27 +             break;
    1.28 +         case kIOMessageCanSystemSleep:
    1.29 +             IOAllowPowerChange(powerConnection, (long) messageArgument);
    1.30 +             break;
    1.31 +         case kIOMessageSystemHasPoweredOn:
    1.32 +			/* awake */
    1.33 +            SDL_PrivateExpose();
    1.34 +            break;
    1.35 +     }
    1.36 +}
    1.37 +
    1.38 +static void QZ_RegisterForSleepNotifications (_THIS)
    1.39 +{
    1.40 +     CFRunLoopSourceRef rls;
    1.41 +     IONotificationPortRef thePortRef;
    1.42 +     io_object_t notifier;
    1.43 +
    1.44 +     powerConnection = IORegisterForSystemPower (this, &thePortRef, QZ_SleepNotificationHandler, &notifier);
    1.45 +
    1.46 +     if (powerConnection == 0)
    1.47 +         NSLog(@"SDL: QZ_SleepNotificationHandler() IORegisterForSystemPower failed.");
    1.48 +
    1.49 +     rls = IONotificationPortGetRunLoopSource (thePortRef);
    1.50 +     CFRunLoopAddSource (CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
    1.51 +     CFRelease (rls);
    1.52 +}
    1.53 +
    1.54  static void QZ_PumpEvents (_THIS)
    1.55  {
    1.56      int firstMouseEvent;
     2.1 --- a/src/video/quartz/SDL_QuartzVideo.h	Mon Dec 02 03:11:36 2002 +0000
     2.2 +++ b/src/video/quartz/SDL_QuartzVideo.h	Sat Dec 07 06:48:49 2002 +0000
     2.3 @@ -131,7 +131,8 @@
     2.4      SDLKey             keymap[256];        /* Mac OS X to SDL key mapping */
     2.5      Uint32             current_mods;       /* current keyboard modifiers, to track modifier state */
     2.6      Uint32             last_virtual_button;/* last virtual mouse button pressed */
     2.7 -    
     2.8 +    io_connect_t       powerConnection;    /* used with IOKit to detect wake from sleep */
     2.9 +
    2.10      ImageDescriptionHandle yuv_idh;
    2.11      MatrixRecordPtr        yuv_matrix;
    2.12      DecompressorComponent  yuv_codec;
    2.13 @@ -165,6 +166,7 @@
    2.14  #define keymap (this->hidden->keymap)
    2.15  #define current_mods (this->hidden->current_mods)
    2.16  #define last_virtual_button (this->hidden->last_virtual_button)
    2.17 +#define powerConnection (this->hidden->powerConnection)
    2.18  
    2.19  #define yuv_idh (this->hidden->yuv_idh)
    2.20  #define yuv_matrix (this->hidden->yuv_matrix)
     3.1 --- a/src/video/quartz/SDL_QuartzVideo.m	Mon Dec 02 03:11:36 2002 +0000
     3.2 +++ b/src/video/quartz/SDL_QuartzVideo.m	Sat Dec 07 06:48:49 2002 +0000
     3.3 @@ -131,6 +131,9 @@
     3.4      current_grab_mode = SDL_GRAB_OFF;
     3.5      in_foreground     = YES;
     3.6      
     3.7 +    /* register for sleep notifications so wake from sleep generates SDL_VIDEOEXPOSE */
     3.8 +    QZ_RegisterForSleepNotifications (this);
     3.9 +    
    3.10      return 0;
    3.11  }
    3.12