1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     voicesel_demo.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.h>
17 
18 #include "demo.h"
19 
20 #include <nn/voicesel.h>
21 
22 #include <string.h>
23 #include <wchar.h>
24 
25 
26 //----------------------------------------------------------------
27 
28 namespace
29 {
30     demo::RenderSystemDrawing df;
31 
32     s32 loopCnt = 0;
33     s32 selectParam = 0;
34     nn::voicesel::Parameter vsel_param;
35 
36     const char* filterTypeStr[] =
37     {
38         "ALL",
39         "100_LT",
40         "75_LT",
41         "50_LT",
42         "25_LT"
43     };
44 
45     const char* softResetStr[] =
46     {
47         "FALSE",
48         "TRUE",
49     };
50 
51     const char* homeButtonStr[] =
52     {
53         "FALSE",
54         "TRUE",
55     };
56 
SetupRenderSystem()57     void SetupRenderSystem()
58     {
59         demo::RenderBufferDescription colorBufferDesc;
60         {
61             colorBufferDesc.m_Format     = GL_RGBA8_OES;
62             colorBufferDesc.m_Width      = nn::gx::DISPLAY0_WIDTH;
63             colorBufferDesc.m_Height     = nn::gx::DISPLAY0_HEIGHT;
64             colorBufferDesc.m_Area       = NN_GX_MEM_VRAMA;
65             colorBufferDesc.m_Attachment = GL_COLOR_ATTACHMENT0;
66         }
67         demo::RenderBufferDescription depthStencilBufferDesc;
68         {
69             depthStencilBufferDesc.m_Format     = GL_DEPTH24_STENCIL8_EXT;
70             depthStencilBufferDesc.m_Width      = nn::gx::DISPLAY0_WIDTH;
71             depthStencilBufferDesc.m_Height     = nn::gx::DISPLAY0_HEIGHT;
72             depthStencilBufferDesc.m_Area       = NN_GX_MEM_VRAMB;
73             depthStencilBufferDesc.m_Attachment = GL_DEPTH_STENCIL_ATTACHMENT;
74         }
75 
76         demo::FrameBufferDescription frameBufferDesc;
77         {
78             frameBufferDesc.m_RenderBufferDescriptionArray[demo::DEMO_COLOR_BUFFER_INDEX] = colorBufferDesc;
79             frameBufferDesc.m_RenderBufferDescriptionArray[demo::DEMO_DEPTH_STENCIL_BUFFER_INDEX] = depthStencilBufferDesc;
80         }
81 
82         demo::DisplayBuffersDescription display0BuffersDesc;
83         {
84             display0BuffersDesc.m_TargetDisplay    = NN_GX_DISPLAY0;
85             display0BuffersDesc.m_DisplayBufferNum = demo::DEMO_DISPLAY_BUFFER_NUM;
86             display0BuffersDesc.m_Format           = GL_RGB8_OES;
87             display0BuffersDesc.m_Width            = nn::gx::DISPLAY0_WIDTH;
88             display0BuffersDesc.m_Height           = nn::gx::DISPLAY0_HEIGHT;
89             display0BuffersDesc.m_Area             = NN_GX_MEM_FCRAM;
90 //            display0BuffersDesc.m_Area             = NN_GX_MEM_VRAMA;
91         }
92 
93         demo::DisplayBuffersDescription display1BuffersDesc;
94         {
95             display1BuffersDesc.m_TargetDisplay    = NN_GX_DISPLAY1;
96             display1BuffersDesc.m_DisplayBufferNum = demo::DEMO_DISPLAY_BUFFER_NUM;
97             display1BuffersDesc.m_Format           = GL_RGB8_OES;
98             display1BuffersDesc.m_Width            = nn::gx::DISPLAY1_WIDTH;
99             display1BuffersDesc.m_Height           = nn::gx::DISPLAY1_HEIGHT;
100             display1BuffersDesc.m_Area             = NN_GX_MEM_FCRAM;
101 //            display1BuffersDesc.m_Area             = NN_GX_MEM_VRAMB;
102         }
103 
104         df.Initialize(nn::os::GetDeviceMemoryAddress(), nn::os::GetDeviceMemorySize(),
105                                   0x40000, 128, true,
106                                   display0BuffersDesc, display1BuffersDesc,
107                                   frameBufferDesc
108                                   );
109     }
110 
ReleaseRenderSystem()111     void ReleaseRenderSystem()
112     {
113         df.Finalize();
114     }
115 
SelectMark(s32 select)116     char SelectMark( s32 select )
117     {
118         return (select == selectParam) ? '>' : ' ';
119     }
120 
121     template <typename T>
ChangeValue(T & src,s32 add,const T min,const T max)122     void ChangeValue( T& src, s32 add, const T min, const T max )
123     {
124         s32 value = static_cast<s32>(src) + add;
125         s32 vmin = static_cast<s32>(min);
126         s32 vmax = static_cast<s32>(max);
127 
128         if (value < vmin) { value += (vmax+1); }
129         if (vmax < value) { value -= (vmax+1); value += vmin; }
130 
131         src = static_cast<T>( value );
132     }
133 
DrawText()134     void DrawText()
135     {
136         // display 0
137         df.SetRenderTarget(NN_GX_DISPLAY0);
138         df.Clear();
139         {
140             df.SetColor(0.0f, 0.0f, 0.5f);
141             df.FillRectangle( 0, 0, 399, 255 );
142             df.SetColor(0.5f, 0.4f, 0.0f);
143             df.FillRectangle( 0, 0, 399, 40 );
144             df.SetColor(0.5f, 0.0f, 0.4f);
145             df.FillRectangle( 0, 180, 399, 40 );
146             df.SetColor(1.0f, 1.0f, 1.0f);
147             df.DrawText( 100, 50, "** DEMO APPLICATION **" );
148 
149             df.SetColor(1.0f, 1.0f, 1.0f);
150             df.DrawText( 30,  80, "[A]             Launch library applet." );
151             df.DrawText( 30, 100, "[Up or Down]    Select parameter." );
152             df.DrawText( 30, 110, "[Left or Right] Change parameter." );
153 
154             // Count
155             df.DrawText( 340, 50, "%d", loopCnt );
156         }
157         df.SwapBuffers();
158 
159         // display 1
160         df.SetRenderTarget(NN_GX_DISPLAY1);
161         df.Clear();
162         {
163             df.SetColor(0.0f, 0.5f, 0.0f);
164             df.FillRectangle( 0, 0, 399, 255 );
165             df.SetColor(1.0f, 1.0f, 1.0f);
166             df.DrawText( 80, 50, "** DEMO APPLICATION **" );
167 
168             df.DrawText( 30, 140, "%c FILTER [%s]",             SelectMark(0), filterTypeStr[vsel_param.input.filterFillType] );
169             df.DrawText( 30, 150, "%c ENABLE SOFT RESET [%s]",  SelectMark(1), softResetStr[vsel_param.config.softwareReset ? 1 : 0] );
170             df.DrawText( 30, 160, "%c ENABLE HOME BUTTON [%s]", SelectMark(2), homeButtonStr[vsel_param.config.homeButton ? 1 : 0] );
171             df.DrawText( 30, 170, "%c MESSAGE [%ls]",           SelectMark(3), vsel_param.input.titleText );
172         }
173         df.SwapBuffers();
174     }
DrawDisplay()175     void DrawDisplay()
176     {
177         DrawText();
178     }
RestoreGraphics()179     void RestoreGraphics()
180     {
181         // Recover the GPU register settings
182         nngxUpdateState(NN_GX_STATE_ALL);
183         nngxValidateState(NN_GX_STATE_ALL,GL_TRUE);
184     }
185 }
186 
187 //----------------------------------------------------------------
myReceiveSleepQueryNotification(uptr arg)188 AppletQueryReply myReceiveSleepQueryNotification( uptr arg )
189 {
190     NN_UNUSED_VAR( arg );
191     NN_LOG("*** Application-1 sleepQuery \n");
192     if ( nn::applet::IsActive() )
193     {
194         return nn::applet::REPLY_LATER;
195     }
196     else
197     {
198         return nn::applet::REPLY_ACCEPT;
199     }
200 }
201 
202 //----------------------------------------------------------------
myReceiveAwakeNotification(uptr arg)203 void myReceiveAwakeNotification( uptr arg )
204 {
205     NN_UNUSED_VAR( arg );
206     NN_LOG("*** Application-1 awake \n");
207     if ( nn::applet::IsActive() )
208     {
209         nngxStartLcdDisplay();
210     }
211 }
212 
213 //----------------------------------------------------------------
nnMain()214 extern "C" void nnMain()
215 {
216     NN_TLOG_("----Application start\n");
217     nn::Result result;
218 
219     // applet declarations
220     nn::applet::SetSleepQueryCallback( myReceiveSleepQueryNotification, 0 );
221     nn::applet::SetAwakeCallback( myReceiveAwakeNotification, 0 );
222     nn::applet::Enable();
223     if(nn::applet::IsExpectedToCloseApplication())
224     {
225         // Quickly end the application
226         nn::applet::PrepareToCloseApplication();
227         nn::applet::CloseApplication();
228 
229         // Never reached
230         return;
231     }
232     // hid
233     result = nn::hid::Initialize();
234     NN_UTIL_PANIC_IF_FAILED(result);
235 
236     // Render class initialization
237     SetupRenderSystem();
238 
239     nn::hid::PadReader padReader;
240     nn::hid::PadStatus ps;
241 
242     // Call parameter
243     nn::voicesel::InitializeParameter( &vsel_param );
244 
245 
246     while(1)
247     {
248         loopCnt++;
249 
250         padReader.ReadLatest(&ps);
251 
252         // Render
253         DrawDisplay();
254 
255         // Start library applet with A Button
256         if ( ps.trigger & nn::hid::BUTTON_A )
257         {
258             nn::voicesel::StartVoiceSel( &vsel_param );
259             if(nn::applet::IsExpectedToCloseApplication())
260             {
261                 break;
262             }
263 
264             // Recover the GPU register settings
265             RestoreGraphics();
266 
267             // Show information for selected file path
268             NN_LOG("file path = [%ls]\n",vsel_param.output.filePath);
269         }
270 
271         // Item selection
272         if ( ps.trigger & nn::hid::BUTTON_UP )
273         {
274             ChangeValue( selectParam, -1, 0, 3 );
275         }
276         if ( ps.trigger & nn::hid::BUTTON_DOWN )
277         {
278             ChangeValue( selectParam, +1, 0, 3 );
279         }
280 
281         // Item setting
282         if ( ps.trigger & (nn::hid::BUTTON_LEFT | nn::hid::BUTTON_RIGHT) )
283         {
284             s32 value = (ps.trigger & nn::hid::BUTTON_RIGHT) ? +1 : -1;
285 
286             switch (selectParam)
287             {
288             case 0:
289                 ChangeValue(
290                     vsel_param.input.filterFillType,
291                     value,
292                     nn::voicesel::FILTER_FILL_TYPE_ALL,
293                     nn::voicesel::FILTER_FILL_TYPE_25_LT );
294                 break;
295             case 1:
296                 ChangeValue( vsel_param.config.softwareReset, value, false, true );
297                 break;
298             case 2:
299                 ChangeValue( vsel_param.config.homeButton, value, false, true );
300                 break;
301             case 3:
302                 ::wcscpy( vsel_param.input.titleText, (value < 0) ? L"" : L"TEST MESSAGE" );
303                 break;
304             }
305         }
306 
307         // Process HOME Button
308         if ( nn::applet::IsExpectedToProcessHomeButton() )
309         {
310             NN_LOG("HomeBtn.\n");
311             nn::applet::ProcessHomeButtonAndWait();
312 
313             if(nn::applet::IsExpectedToCloseApplication())
314             {
315                 break;
316             }
317 
318             RestoreGraphics();
319 
320         }
321 
322         // POWER button process
323         if ( nn::applet::IsExpectedToProcessPowerButton() )
324         {
325             NN_LOG("PowerBtn.\n");
326             nn::applet::ProcessPowerButtonAndWait();
327 
328             if(nn::applet::IsExpectedToCloseApplication())
329             {
330                 break;
331             }
332 
333             RestoreGraphics();
334         }
335 
336     }
337 
338 
339     NN_TLOG_("----Application is to be end\n");
340 
341     // Free the render class
342     ReleaseRenderSystem();
343 
344     // Finalize hid
345     nn::hid::Finalize();
346 
347     // Exit application
348     nn::applet::PrepareToCloseApplication();
349     nn::applet::CloseApplication();
350 
351     // No need to call Finalize on applet
352     // Execution will probably not reach here.
353     NN_TLOG_("----Application end\n");
354 }
355