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:$
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 if ( nn::applet::IsExpectedToReplySleepQuery() )
52 return true;
53
54 return false;
55 }
56
57 /*------------------------------------------------------------------------*
58 Handles processing to cause a transition to Sleep Mode.
59 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.
60
61 *------------------------------------------------------------------------*/
SleepSystem(void)62 void SleepHandler::SleepSystem( void )
63 {
64 // If no instructions are received, returns without doing anything.
65 if ( !IsSleepRequested() )
66 {
67 return;
68 }
69
70 nn::applet::ReplySleepQuery(nn::applet::REPLY_ACCEPT);
71
72 s_AwakeEvent.Wait();
73 }
74
75 /*------------------------------------------------------------------------*
76 Callback called when there is a sleep query
77 *------------------------------------------------------------------------*/
SleepQueryCallback(uptr arg)78 AppletQueryReply SleepHandler::SleepQueryCallback( uptr arg )
79 {
80 NN_UNUSED_VAR(arg);
81
82 s_AwakeEvent.ClearSignal();
83
84 if ( !nn::applet::IsActive() )
85 {
86 return nn::applet::REPLY_ACCEPT;
87 }
88
89 if ( s_IsInitialized )
90 {
91 return nn::applet::REPLY_LATER;
92 }
93 else
94 {
95 return nn::applet::REPLY_REJECT;
96 }
97 }
98
99 /*------------------------------------------------------------------------*
100 Callback called when recovering from sleep
101 *------------------------------------------------------------------------*/
AwakeCallback(uptr arg)102 void SleepHandler::AwakeCallback( uptr arg )
103 {
104 NN_UNUSED_VAR(arg);
105 s_AwakeEvent.Signal();
106 }
107
108