Skip to content

Commit

Permalink
From: Darrell Walisser
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
slouken committed Dec 7, 2002
1 parent 10e38ab commit b0f14c3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/video/quartz/SDL_QuartzEvents.m
Expand Up @@ -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) {
Expand Down Expand Up @@ -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, &notifier);

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;
Expand Down
4 changes: 3 additions & 1 deletion src/video/quartz/SDL_QuartzVideo.h
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/video/quartz/SDL_QuartzVideo.m
Expand Up @@ -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;
}

Expand Down

0 comments on commit b0f14c3

Please sign in to comment.