[droid] splash: adjust progressbar color
[vuplus_xbmc] / xbmc / XBApplicationEx.cpp
1 /*
2  *      Copyright (C) 2005-2012 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License along
16  *  with this program; if not, write to the Free Software Foundation, Inc.,
17  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  */
20
21 #include "system.h"
22 #include "XBApplicationEx.h"
23 #include "utils/log.h"
24 #include "threads/SystemClock.h"
25 #ifdef HAS_PERFORMANCE_SAMPLE
26 #include "utils/PerformanceSample.h"
27 #else
28 #define MEASURE_FUNCTION
29 #endif
30 #include "commons/Exception.h"
31
32 // Put this here for easy enable and disable
33 #ifndef _DEBUG
34 #define XBMC_TRACK_EXCEPTIONS
35 #endif
36
37 CXBApplicationEx::CXBApplicationEx()
38 {
39   // Variables to perform app timing
40   m_bStop = false;
41   m_AppActive = true;
42   m_AppFocused = true;
43   m_ExitCode = EXITCODE_QUIT;
44   m_renderGUI = false;
45 }
46
47 CXBApplicationEx::~CXBApplicationEx()
48 {
49 }
50
51 /* Create the app */
52 bool CXBApplicationEx::Create()
53 {
54   // Variables to perform app timing
55   m_bStop = false;
56   m_AppActive = true;
57   m_AppFocused = true;
58   m_ExitCode = EXITCODE_QUIT;
59
60   // Initialize the app's device-dependent objects
61   if (!Initialize())
62   {
63     CLog::Log(LOGERROR, "XBAppEx: Call to Initialize() failed!" );
64     return false;
65   }
66
67   return true;
68 }
69
70 /* Destroy the app */
71 VOID CXBApplicationEx::Destroy()
72 {
73   CLog::Log(LOGNOTICE, "destroy");
74   // Perform app-specific cleanup
75   Cleanup();
76 }
77
78 /* Function that runs the application */
79 INT CXBApplicationEx::Run()
80 {
81   CLog::Log(LOGNOTICE, "Running the application..." );
82
83   unsigned int lastFrameTime = 0;
84   unsigned int frameTime = 0;
85   const unsigned int noRenderFrameTime = 15;  // Simulates ~66fps
86
87 #ifdef XBMC_TRACK_EXCEPTIONS
88   BYTE processExceptionCount = 0;
89   BYTE frameMoveExceptionCount = 0;
90   BYTE renderExceptionCount = 0;
91   const BYTE MAX_EXCEPTION_COUNT = 10;
92 #endif
93
94   // Run xbmc
95   while (!m_bStop)
96   {
97 #ifdef HAS_PERFORMANCE_SAMPLE
98     CPerformanceSample sampleLoop("XBApplicationEx-loop");
99 #endif
100     //-----------------------------------------
101     // Animate and render a frame
102     //-----------------------------------------
103 #ifdef XBMC_TRACK_EXCEPTIONS
104     try
105     {
106 #endif
107       lastFrameTime = XbmcThreads::SystemClockMillis();
108       Process();
109       //reset exception count
110 #ifdef XBMC_TRACK_EXCEPTIONS
111       processExceptionCount = 0;
112
113     }
114     catch (const XbmcCommons::UncheckedException &e)
115     {
116       e.LogThrowMessage("CApplication::Process()");
117       processExceptionCount++;
118       //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
119       if (processExceptionCount > MAX_EXCEPTION_COUNT)
120       {
121         CLog::Log(LOGERROR, "CApplication::Process(), too many exceptions");
122         throw;
123       }
124     }
125     catch (...)
126     {
127       CLog::Log(LOGERROR, "exception in CApplication::Process()");
128       processExceptionCount++;
129       //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
130       if (processExceptionCount > MAX_EXCEPTION_COUNT)
131       {
132         CLog::Log(LOGERROR, "CApplication::Process(), too many exceptions");
133         throw;
134       }
135     }
136 #endif
137     // Frame move the scene
138 #ifdef XBMC_TRACK_EXCEPTIONS
139     try
140     {
141 #endif
142       if (!m_bStop) FrameMove(true, m_renderGUI);
143       //reset exception count
144 #ifdef XBMC_TRACK_EXCEPTIONS
145       frameMoveExceptionCount = 0;
146
147     }
148     catch (const XbmcCommons::UncheckedException &e)
149     {
150       e.LogThrowMessage("CApplication::FrameMove()");
151       frameMoveExceptionCount++;
152       //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
153       if (frameMoveExceptionCount > MAX_EXCEPTION_COUNT)
154       {
155         CLog::Log(LOGERROR, "CApplication::FrameMove(), too many exceptions");
156         throw;
157       }
158     }
159     catch (...)
160     {
161       CLog::Log(LOGERROR, "exception in CApplication::FrameMove()");
162       frameMoveExceptionCount++;
163       //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
164       if (frameMoveExceptionCount > MAX_EXCEPTION_COUNT)
165       {
166         CLog::Log(LOGERROR, "CApplication::FrameMove(), too many exceptions");
167         throw;
168       }
169     }
170 #endif
171
172     // Render the scene
173 #ifdef XBMC_TRACK_EXCEPTIONS
174     try
175     {
176 #endif
177       if (m_renderGUI && !m_bStop) Render();
178       else if (!m_renderGUI)
179       {
180         frameTime = XbmcThreads::SystemClockMillis() - lastFrameTime;
181         if(frameTime < noRenderFrameTime)
182           Sleep(noRenderFrameTime - frameTime);
183       }
184 #ifdef XBMC_TRACK_EXCEPTIONS
185       //reset exception count
186       renderExceptionCount = 0;
187
188     }
189     catch (const XbmcCommons::UncheckedException &e)
190     {
191       e.LogThrowMessage("CApplication::Render()");
192       renderExceptionCount++;
193       //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
194       if (renderExceptionCount > MAX_EXCEPTION_COUNT)
195       {
196         CLog::Log(LOGERROR, "CApplication::Render(), too many exceptions");
197         throw;
198       }
199     }
200     catch (...)
201     {
202       CLog::Log(LOGERROR, "exception in CApplication::Render()");
203       renderExceptionCount++;
204       //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
205       if (renderExceptionCount > MAX_EXCEPTION_COUNT)
206       {
207         CLog::Log(LOGERROR, "CApplication::Render(), too many exceptions");
208         throw;
209       }
210     }
211 #endif
212   } // while (!m_bStop)
213   Destroy();
214
215   CLog::Log(LOGNOTICE, "application stopped..." );
216   return m_ExitCode;
217 }