nn::applet::CTR::SetSleepCanceledCallback Function

Syntax

void SetSleepCanceledCallback(
     AppletSleepCanceledCallback callback,
     uptr arg = 0
);

Arguments

Name Description
in callback Callback.
in arg Callback arguments.

Return Values

None.

Description

Sets the sleep cancel callback.

This callback is called when the system is opened while the current applet manager is sending a sleep query to the applet or application.

The name of this callback implies that this callback is called after it is certain that sleep is canceled. This is not the case. This callback is intended simply to inform ongoing processes that the system has been opened.

If this callback is called while the local process is deferring reply to the sleep request (see Note 1) and you then want to reject the sleep request, use the nn::applet::CTR::ReplySleepQuery function to return nn::applet::CTR::REPLY_REJECT. If you simply wish to be informed that the system was opened, you do not need to do anything. The applet manager will continue to wait for replies to the sleep query.

This callback is intended for uses such as quickly detecting that the system has been opened and canceling the transition to sleep mode when it might otherwise take a while to fully transition, with the screens remaining dark for several seconds if the user opens the system in the middle of the transition.

Example
bool isShellOpened;
nn::os::LightEvent awakeEvent;

// Sleep cancel callback
void mySleepCanceledCallback(uptr arg)
{
  NN_UNUSED_VAR(arg);
  if ( applet::IsActive() )
  {
    isShellOpened = true; // System opened
    applet::ReplySleepQuery( applet::REPLY_REJECT );
  }
}

// Sleep query callback
void mySleepQueryCallback(uptr arg)
{
  NN_UNUSED_VAR(arg);
  if ( applet::IsActive() )
  {
    isShellOpened = false;
    awakeEvent.ClearSignal();
    return applet::REPLY_LATER;
  }
  else
  {
    return applet::REPLY_ACCEPT;
  }
}

// Wakeup callback
void myAwakeCallback(uptr arg)
{
  NN_UNUSED_VAR(arg);
  awakeEvent.Signal();   // Wake up
}

// Main
void nnMain()
{
   awakeEvent.Initialize(false);
   applet::SetSleepQueryCallback( mySleepQueryCallback, 0 );
   applet::SetSleepCanceledCallback( mySleepCanceledCallback, 0 );
   applet::SetAwakeCallback( myAwakeCallback, 0 );
   applet::Enable();


   while(1)
   {
      _exec();
      _draw();

      if ( applet::IsExpectedToReplySleepQuery() )
      {
          for(int i=0; i<N; n++) // Heavy processing before going to sleep. For instance, when saving N items of data.
          {
            _save( data[i] );
            if ( isShellOpened ) // Interrupt since system is opened in the middle of all this
            {
              break;
            }
          }

          // If system not opened, accept sleep query and wait to wake up
          if ( ! isShellOpened )
          {
            applet::ReplySleepQuery( applet::REPLY_ACCEPT );
            awakeEvent.Wait();
          }
      }
   }
}

Note 1:

This callback is called whenever the system is opened while the applet manager is still waiting to receive all the replies from all the applications and applets. That means that coincidences of system status and timing can cause it to be called before applications have returned any replies at all, and likewise it might be called after applications have replied as long as some other applet has not yet returned a reply.

Revision History

2010/11/29
Initial version.

CONFIDENTIAL