# HG changeset patch # User Sam Lantinga # Date 1039243729 0 # Node ID 2536446a92deb44cdde7b87ef6e0433d69a4e2e0 # Parent 38b1a98aeb11c916eda066749f7f8fd68cf16f86 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. diff -r 38b1a98aeb11 -r 2536446a92de src/video/quartz/SDL_QuartzEvents.m --- 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 // For getenv() +#include // For wake from sleep detection +#include // 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; diff -r 38b1a98aeb11 -r 2536446a92de src/video/quartz/SDL_QuartzVideo.h --- 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) diff -r 38b1a98aeb11 -r 2536446a92de src/video/quartz/SDL_QuartzVideo.m --- 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; }