registerPrioritySleepWakeInterest problems with shutdown event
registerPrioritySleepWakeInterest problems with shutdown event
- Subject: registerPrioritySleepWakeInterest problems with shutdown event
- From: Herbert <email@hidden>
- Date: Fri, 25 Nov 2005 21:12:23 -0800
I'm trying to use this sample code from Apple, but I want the
shutdown event:
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <mach/mach_port.h>
#include <mach/mach_interface.h>
#include <mach/mach_init.h>
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <IOKit/IOMessage.h>
io_connect_t root_port; // a reference to the Root Power Domain
IOService
void
MySleepCallBack( void * refCon, io_service_t service, natural_t
messageType, void * messageArgument )
{
printf( "messageType lx, arg lx\n",
(long unsigned int)messageType,
(long unsigned int)messageArgument );
switch ( messageType )
{
case kIOMessageDeviceWillPowerOff:
// we cannot deny powerdown
SysBeep ();
IOAllowPowerChange( root_port, (long)messageArgument );
break;
case kIOMessageCanSystemSleep:
/*
Idle sleep is about to kick in.
Applications have a chance to prevent sleep by
calling IOCancelPowerChange.
Most applications should not prevent idle sleep.
Power Management waits up to 30 seconds for you to
either allow or deny idle sleep.
If you don't acknowledge this power change by calling
either IOAllowPowerChange
or IOCancelPowerChange, the system will wait 30
seconds then go to sleep.
*/
// we will allow idle sleep
IOAllowPowerChange( root_port, (long)messageArgument );
break;
case kIOMessageSystemWillSleep:
/* The system WILL go to sleep. If you do not call
IOAllowPowerChange or
IOCancelPowerChange to acknowledge this message,
sleep will be
delayed by 30 seconds.
NOTE: If you call IOCancelPowerChange to deny sleep
it returns kIOReturnSuccess,
however the system WILL still go to sleep.
*/
// we cannot deny forced sleep
IOAllowPowerChange( root_port, (long)messageArgument );
break;
default:
break;
}
}
int main( int argc, char **argv )
{
IONotificationPortRef notifyPortRef; // notification port
allocated by IORegisterForSystemPower
io_object_t notifierObject; // notifier object, used
to deregister later
void* refCon; // this parameter is
passed to the callback
// register to receive system sleep notifications
root_port = IORegisterForSystemPower( refCon, ¬ifyPortRef,
MySleepCallBack, ¬ifierObject );
if ( root_port == NULL )
{
printf("IORegisterForSystemPower failed\n");
return 1;
}
// add the notification port to the application runloop
CFRunLoopAddSource( CFRunLoopGetCurrent(),
IONotificationPortGetRunLoopSource
(notifyPortRef),
kCFRunLoopCommonModes );
printf( "waiting...\n\n" );
/*
Start the run loop to receive sleep notifications. You don't
need to
call this if you already have a Carbon or Cocoa EventLoop
running.
*/
CFRunLoopRun();
return (0);
}
however, I can't seem to get that power off message. The machine
shuts down so fast it's impossible to see anything like a log
message. I tried the beep just to see if I could hear anything, but
nope.
So I don't know really if the event is being caught, or the app is
killed before it gets a chance to do anything.
I need this to do some housekeeping before shutdown.
Anyone have experience with this?
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden