Unable to get event for shutdown/restart using QA1340

  • 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 %08lx, arg %08lx\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, &notifyPortRef,
    MySleepCallBack, &notifierObject );
        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'.
  • Any answers???

    Thanks,
    Palav

    On Jan 10, 2008 1:27 PM, parag vibhute <parag.vibhute...> wrote:

    >
    > 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 %08lx, arg %08lx\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, &notifyPortRef,
    > MySleepCallBack, &notifierObject );
    > 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'.

    --

    There are many things in your life that will catch your eye but only a few
    will catch your heart....pursue those'.
  • Palav,

    Your original post was only 5 hours ago!  And your question is entirely
    off-topic.  I suggest asking on the Darwin list, that's where IOKit
    questions belong.

    On 1/10/08 6:41 PM, parag vibhute said:

    > Any answers???
    >
    > Thanks,
    > Palav
    >
    > On Jan 10, 2008 1:27 PM, parag vibhute <parag.vibhute...>  wrote:
    >
    >>
    >> 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 %08lx, arg %08lx\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, &notifyPortRef,
    >> MySleepCallBack, &notifierObject );
    >> 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'.
    >
    >
    >
    >
    > --
    >
    > There are many things in your life that will catch your eye but only a few
    > will catch your heart....pursue those'.
  • My intention was to find answer if any Cocoa programmer has used the same
    approach to catch shutdown or restart. Thats why I posted this topic here.
    My application is cocoa application where I want to achieve this but before
    implementing code in my application, I want to try this as standalone first.

    Thanks,
    Palav

    On Jan 10, 2008 8:47 PM, Sean McBride <sean...> wrote:

    > Palav,
    >
    > Your original post was only 5 hours ago!  And your question is entirely
    > off-topic.  I suggest asking on the Darwin list, that's where IOKit
    > questions belong.
    >
    >
    > On 1/10/08 6:41 PM, parag vibhute said:
    >
    >> Any answers???
    >>
    >> Thanks,
    >> Palav
    >>
    >> On Jan 10, 2008 1:27 PM, parag vibhute <parag.vibhute...> wrote:
    >>
    >>>
    >>> 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 %08lx, arg %08lx\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, &notifyPortRef,
    >>> MySleepCallBack, &notifierObject );
    >>> 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'.
    >>
    >>
    >>
    >>
    >> --
    >>
    >> There are many things in your life that will catch your eye but only a
    > few
    >> will catch your heart....pursue those'.
    >
    >

    --

    There are many things in your life that will catch your eye but only a few
    will catch your heart....pursue those'.
  • On 1/10/08 9:03 PM, parag vibhute said:

    > My intention was to find answer if any Cocoa programmer has used the same
    > approach to catch shutdown or restart. Thats why I posted this topic here.
    > My application is cocoa application where I want to achieve this but before
    > implementing code in my application, I want to try this as standalone first.

    The Cocoa list means: 'about the Cocoa APIs', not 'for Cocoa
    applications'.  Your app may be Cocoa but your question is not about a
    Cocoa API.  You've done the right thing by narrowing your problem down
    to a test app, you've shown your problem has nothing to do with Cocoa
    (you don't even #include Cocoa.h), it doesn't belong on this list.  I'm
    not trying to be rude here, just trying to help you help yourself.

    --
    ____________________________________________________________
    Sean McBride, B. Eng                <sean...>
    Rogue Research                        www.rogue-research.com
    Mac Software Developer              Montréal, Québec, Canada
  • > The Cocoa list means: 'about the Cocoa APIs', not 'for Cocoa
    > applications'.  Your app may be Cocoa but your question is not about a
    > Cocoa API.  You've done the right thing by narrowing your problem down
    > to a test app, you've shown your problem has nothing to do with Cocoa
    > (you don't even #include Cocoa.h), it doesn't belong on this list.  I'm
    > not trying to be rude here, just trying to help you help yourself.

      It's worth noting, however, that there are many hits on this topic
    in this list's archives if you try a search ... Perhaps one of them
    will pay off?

    --
    I.S.
previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today