1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     sleep.cpp
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 46365 $
14  *---------------------------------------------------------------------------*/
15 
16 #include <nn/applet.h>
17 
18 #include "sleep.h"
19 
20 /*------------------------------------------------------------------------*
21         Processing concerning console sleep
22  *------------------------------------------------------------------------*/
23 nn::os::LightEvent  SleepHandler::s_AwakeEvent;
24 bool                SleepHandler::s_IsInitialized = false;
25 
26 /*------------------------------------------------------------------------*/
Initialize(void)27 void SleepHandler::Initialize( void )
28 {
29     s_AwakeEvent.Initialize(false);
30     nn::applet::SetSleepQueryCallback(SleepQueryCallback, 0);
31     nn::applet::SetAwakeCallback(AwakeCallback, 0);
32     nn::applet::Enable();
33     s_IsInitialized = true;
34 }
35 
36 /*------------------------------------------------------------------------*/
Finalize(void)37 void SleepHandler::Finalize( void )
38 {
39     nn::applet::DisableSleep();
40     s_AwakeEvent.Finalize();
41     nn::applet::SetSleepQueryCallback(NULL, 0);
42     nn::applet::SetAwakeCallback(NULL, 0);
43     s_IsInitialized = false;
44 }
45 
46 /*------------------------------------------------------------------------*
47     Returns a value indicating whether there was an instruction to enter Sleep Mode.
48  *------------------------------------------------------------------------*/
IsSleepRequested(void)49 bool SleepHandler::IsSleepRequested( void )
50 {
51     NN_TASSERT_(s_IsInitialized);
52 
53     if ( nn::applet::IsExpectedToReplySleepQuery() )
54         return true;
55 
56     return false;
57 }
58 
59 /*------------------------------------------------------------------------*
60     Handles processing to cause a transition to Sleep Mode.
61     This must be called at an appropriate time so that there are no problems with graphics or other processing even if the system enters Sleep Mode.
62 
63  *------------------------------------------------------------------------*/
SleepSystem(void)64 void SleepHandler::SleepSystem( void )
65 {
66     // Do nothing and return if an instruction has not been received.
67     if ( !IsSleepRequested() )
68     {
69         return;
70     }
71 
72     nn::applet::ReplySleepQuery(nn::applet::REPLY_ACCEPT);
73 
74     s_AwakeEvent.Wait();
75 }
76 
77 /*------------------------------------------------------------------------*
78     Callback called when there is a sleep query
79  *------------------------------------------------------------------------*/
SleepQueryCallback(uptr arg)80 AppletQueryReply SleepHandler::SleepQueryCallback( uptr arg )
81 {
82     NN_UNUSED_VAR(arg);
83 
84     s_AwakeEvent.ClearSignal();
85 
86     if ( !nn::applet::IsActive() )
87     {
88         return nn::applet::REPLY_ACCEPT;
89     }
90 
91     if ( s_IsInitialized )
92     {
93         return nn::applet::REPLY_LATER;
94     }
95     else
96     {
97         return nn::applet::REPLY_REJECT;
98     }
99 }
100 
101 /*------------------------------------------------------------------------*
102     Callback called when recovering from sleep
103  *------------------------------------------------------------------------*/
AwakeCallback(uptr arg)104 void SleepHandler::AwakeCallback( uptr arg )
105 {
106     NN_UNUSED_VAR(arg);
107     s_AwakeEvent.Signal();
108 }
109 
110