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.
--- a/src/video/quartz/SDL_QuartzEvents.m Mon Dec 02 03:11:36 2002 +0000
+++ b/src/video/quartz/SDL_QuartzEvents.m Sat Dec 07 06:48:49 2002 +0000
@@ -20,6 +20,8 @@
slouken@libsdl.org
*/
#include <stdlib.h> // For getenv()
+#include <IOKit/IOMessage.h> // For wake from sleep detection
+#include <IOKit/pwr_mgt/IOPMLib.h> // For wake from sleep detection
#include "SDL_QuartzKeys.h"
static void QZ_InitOSKeymap (_THIS) {
@@ -304,6 +306,44 @@
SDL_PrivateAppActive (0, SDL_APPINPUTFOCUS);
}
+void QZ_SleepNotificationHandler (void * refcon,
+ io_service_t service,
+ natural_t messageType,
+ void * messageArgument )
+{
+ SDL_VideoDevice *this = (SDL_VideoDevice*)refcon;
+
+ switch(messageType)
+ {
+ case kIOMessageSystemWillSleep:
+ IOAllowPowerChange(powerConnection, (long) messageArgument);
+ break;
+ case kIOMessageCanSystemSleep:
+ IOAllowPowerChange(powerConnection, (long) messageArgument);
+ break;
+ case kIOMessageSystemHasPoweredOn:
+ /* awake */
+ SDL_PrivateExpose();
+ break;
+ }
+}
+
+static void QZ_RegisterForSleepNotifications (_THIS)
+{
+ CFRunLoopSourceRef rls;
+ IONotificationPortRef thePortRef;
+ io_object_t notifier;
+
+ powerConnection = IORegisterForSystemPower (this, &thePortRef, QZ_SleepNotificationHandler, ¬ifier);
+
+ if (powerConnection == 0)
+ NSLog(@"SDL: QZ_SleepNotificationHandler() IORegisterForSystemPower failed.");
+
+ rls = IONotificationPortGetRunLoopSource (thePortRef);
+ CFRunLoopAddSource (CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
+ CFRelease (rls);
+}
+
static void QZ_PumpEvents (_THIS)
{
int firstMouseEvent;
--- a/src/video/quartz/SDL_QuartzVideo.h Mon Dec 02 03:11:36 2002 +0000
+++ b/src/video/quartz/SDL_QuartzVideo.h Sat Dec 07 06:48:49 2002 +0000
@@ -131,7 +131,8 @@
SDLKey keymap[256]; /* Mac OS X to SDL key mapping */
Uint32 current_mods; /* current keyboard modifiers, to track modifier state */
Uint32 last_virtual_button;/* last virtual mouse button pressed */
-
+ io_connect_t powerConnection; /* used with IOKit to detect wake from sleep */
+
ImageDescriptionHandle yuv_idh;
MatrixRecordPtr yuv_matrix;
DecompressorComponent yuv_codec;
@@ -165,6 +166,7 @@
#define keymap (this->hidden->keymap)
#define current_mods (this->hidden->current_mods)
#define last_virtual_button (this->hidden->last_virtual_button)
+#define powerConnection (this->hidden->powerConnection)
#define yuv_idh (this->hidden->yuv_idh)
#define yuv_matrix (this->hidden->yuv_matrix)
--- a/src/video/quartz/SDL_QuartzVideo.m Mon Dec 02 03:11:36 2002 +0000
+++ b/src/video/quartz/SDL_QuartzVideo.m Sat Dec 07 06:48:49 2002 +0000
@@ -131,6 +131,9 @@
current_grab_mode = SDL_GRAB_OFF;
in_foreground = YES;
+ /* register for sleep notifications so wake from sleep generates SDL_VIDEOEXPOSE */
+ QZ_RegisterForSleepNotifications (this);
+
return 0;
}