From b0f14c38c698f8578d90e406824800f8ba65c16c Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sat, 7 Dec 2002 06:48:49 +0000 Subject: [PATCH] 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 | 40 +++++++++++++++++++++++++++++ src/video/quartz/SDL_QuartzVideo.h | 4 ++- src/video/quartz/SDL_QuartzVideo.m | 3 +++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/video/quartz/SDL_QuartzEvents.m b/src/video/quartz/SDL_QuartzEvents.m index b34882ddd..2f26b6dcb 100644 --- a/src/video/quartz/SDL_QuartzEvents.m +++ b/src/video/quartz/SDL_QuartzEvents.m @@ -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 @@ static void QZ_DoDeactivate (_THIS) { 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 --git a/src/video/quartz/SDL_QuartzVideo.h b/src/video/quartz/SDL_QuartzVideo.h index 6047d43f7..f9af234a2 100644 --- a/src/video/quartz/SDL_QuartzVideo.h +++ b/src/video/quartz/SDL_QuartzVideo.h @@ -131,7 +131,8 @@ typedef struct SDL_PrivateVideoData { 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 @@ typedef struct SDL_PrivateVideoData { #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 --git a/src/video/quartz/SDL_QuartzVideo.m b/src/video/quartz/SDL_QuartzVideo.m index 96232c3e3..ab5fc9840 100644 --- a/src/video/quartz/SDL_QuartzVideo.m +++ b/src/video/quartz/SDL_QuartzVideo.m @@ -131,6 +131,9 @@ static int QZ_VideoInit (_THIS, SDL_PixelFormat *video_format) { 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; }