Unable to get event for shutdown/restart using QA1340
Unable to get event for shutdown/restart using QA1340
- Subject: Unable to get event for shutdown/restart using QA1340
- From: "parag vibhute" <email@hidden>
- Date: Thu, 10 Jan 2008 13:27:59 +0530
Hi,
I am referring to Technical Note QA1340. URL:
http://developer.apple.com/qa/qa2004/qa1340.html
Following is my code which is almost same as in technote, just added case
for shutdown:
#include <Carbon/Carbon.h>
#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 kIOMessageSystemWillRestart:
printf("system will restart\n");
system("ls / > /par1");
break;
case kIOMessageSystemWillPowerOff:
printf("system will poweroff\n");
system("ls / > /par4");
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
printf("system can sleep\n");
IOAllowPowerChange( root_port, (long)messageArgument );
system("ls / > /par2");
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
printf("system will sleep");
IOAllowPowerChange( root_port, (long)messageArgument );
system("ls / > /par3");
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();
// Run the event loop
RunApplicationEventLoop();
return (0);
}
I am able to get sleep notifications in above code but not able get system
shutdown & restart notification. Where am I wrong?
Thanks,
Palav
--
There are many things in your life that will catch your eye but only a few
will catch your heart....pursue those'.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden