#if defined( __APPLE__ )
#include "Sleep.h"
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <IOKit/IOMessage.h>
#include "basic_test_conversions.h"
#define UNUSED __attribute__((unused))
struct
{
io_connect_t connection;
IONotificationPortRef port;
io_object_t iterator;
}sleepInfo;
void sleepCallback( void * refcon,
io_service_t service,
natural_t messageType,
void * messageArgument );
void sleepCallback( void * refcon UNUSED,
io_service_t service UNUSED,
natural_t messageType,
void * messageArgument )
{
IOReturn result;
service -- The IOService whose state has changed.
messageType -- A messageType enum, defined by IOKit/IOMessage.h or by the IOService's family.
messageArgument -- An argument for the message, dependent on the messageType.
*/
switch ( messageType )
{
case kIOMessageSystemWillSleep:
IOAllowPowerChange(sleepInfo.connection,(long)messageArgument);
vlog( "Hard sleep occurred.\n" );
break;
case kIOMessageCanSystemSleep:
result = IOCancelPowerChange(sleepInfo.connection,(long)messageArgument);
if( kIOReturnSuccess != result )
vlog( "sleep prevention failed. (%d)\n", result);
break;
case kIOMessageSystemHasPoweredOn:
break;
}
}
void PreventSleep( void )
{
vlog( "Disabling sleep... " );
sleepInfo.iterator = (io_object_t) 0;
sleepInfo.port = NULL;
sleepInfo.connection = IORegisterForSystemPower
(
&sleepInfo,
&sleepInfo.port,
sleepCallback,
&sleepInfo.iterator
);
if( (io_connect_t) 0 == sleepInfo.connection )
vlog( "failed.\n" );
else
vlog( "done.\n" );
CFRunLoopAddSource(CFRunLoopGetCurrent(),
IONotificationPortGetRunLoopSource(sleepInfo.port),
kCFRunLoopDefaultMode);
}
void ResumeSleep( void )
{
IOReturn result = IODeregisterForSystemPower ( &sleepInfo.iterator );
if( 0 != result )
vlog( "Got error %d restoring sleep \n", result );
else
vlog( "Sleep restored.\n" );
}
#else
void PreventSleep( void ) {}
void ResumeSleep( void ) {}
#endif