Fix keymap.
[vuplus_xbmc] / xbmc / XBApplicationEx.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://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, or (at your option)
8  *  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
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
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_AppFocused = true;
42   m_ExitCode = EXITCODE_QUIT;
43   m_renderGUI = false;
44 }
45
46 CXBApplicationEx::~CXBApplicationEx()
47 {
48 }
49
50 /* Create the app */
51 bool CXBApplicationEx::Create()
52 {
53   // Variables to perform app timing
54   m_bStop = false;
55   m_AppFocused = true;
56   m_ExitCode = EXITCODE_QUIT;
57
58   // Initialize the app's device-dependent objects
59   if (!Initialize())
60   {
61     CLog::Log(LOGERROR, "XBAppEx: Call to Initialize() failed!" );
62     return false;
63   }
64
65   return true;
66 }
67
68 /* Destroy the app */
69 VOID CXBApplicationEx::Destroy()
70 {
71   CLog::Log(LOGNOTICE, "destroy");
72   // Perform app-specific cleanup
73   Cleanup();
74 }
75
76 /* Function that runs the application */
77 INT CXBApplicationEx::Run()
78 {
79   CLog::Log(LOGNOTICE, "Running the application..." );
80
81   unsigned int lastFrameTime = 0;
82   unsigned int frameTime = 0;
83   const unsigned int noRenderFrameTime = 15;  // Simulates ~66fps
84
85 #ifdef XBMC_TRACK_EXCEPTIONS
86   BYTE processExceptionCount = 0;
87   BYTE frameMoveExceptionCount = 0;
88   BYTE renderExceptionCount = 0;
89   const BYTE MAX_EXCEPTION_COUNT = 10;
90 #endif
91
92   // Run xbmc
93   while (!m_bStop)
94   {
95 #ifdef HAS_PERFORMANCE_SAMPLE
96     CPerformanceSample sampleLoop("XBApplicationEx-loop");
97 #endif
98     //-----------------------------------------
99     // Animate and render a frame
100     //-----------------------------------------
101 #ifdef XBMC_TRACK_EXCEPTIONS
102     try
103     {
104 #endif
105       lastFrameTime = XbmcThreads::SystemClockMillis();
106       Process();
107       //reset exception count
108 #ifdef XBMC_TRACK_EXCEPTIONS
109       processExceptionCount = 0;
110
111     }
112     catch (const XbmcCommons::UncheckedException &e)
113     {
114       e.LogThrowMessage("CApplication::Process()");
115       processExceptionCount++;
116       //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
117       if (processExceptionCount > MAX_EXCEPTION_COUNT)
118       {
119         CLog::Log(LOGERROR, "CApplication::Process(), too many exceptions");
120         throw;
121       }
122     }
123     catch (...)
124     {
125       CLog::Log(LOGERROR, "exception in CApplication::Process()");
126       processExceptionCount++;
127       //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
128       if (processExceptionCount > MAX_EXCEPTION_COUNT)
129       {
130         CLog::Log(LOGERROR, "CApplication::Process(), too many exceptions");
131         throw;
132       }
133     }
134 #endif
135     // Frame move the scene
136 #ifdef XBMC_TRACK_EXCEPTIONS
137     try
138     {
139 #endif
140       if (!m_bStop) FrameMove(true, m_renderGUI);
141       //reset exception count
142 #ifdef XBMC_TRACK_EXCEPTIONS
143       frameMoveExceptionCount = 0;
144
145     }
146     catch (const XbmcCommons::UncheckedException &e)
147     {
148       e.LogThrowMessage("CApplication::FrameMove()");
149       frameMoveExceptionCount++;
150       //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
151       if (frameMoveExceptionCount > MAX_EXCEPTION_COUNT)
152       {
153         CLog::Log(LOGERROR, "CApplication::FrameMove(), too many exceptions");
154         throw;
155       }
156     }
157     catch (...)
158     {
159       CLog::Log(LOGERROR, "exception in CApplication::FrameMove()");
160       frameMoveExceptionCount++;
161       //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
162       if (frameMoveExceptionCount > MAX_EXCEPTION_COUNT)
163       {
164         CLog::Log(LOGERROR, "CApplication::FrameMove(), too many exceptions");
165         throw;
166       }
167     }
168 #endif
169
170     // Render the scene
171 #ifdef XBMC_TRACK_EXCEPTIONS
172     try
173     {
174 #endif
175       if (m_renderGUI && !m_bStop) Render();
176       else if (!m_renderGUI)
177       {
178         frameTime = XbmcThreads::SystemClockMillis() - lastFrameTime;
179         if(frameTime < noRenderFrameTime)
180           Sleep(noRenderFrameTime - frameTime);
181       }
182 #ifdef XBMC_TRACK_EXCEPTIONS
183       //reset exception count
184       renderExceptionCount = 0;
185
186     }
187     catch (const XbmcCommons::UncheckedException &e)
188     {
189       e.LogThrowMessage("CApplication::Render()");
190       renderExceptionCount++;
191       //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
192       if (renderExceptionCount > MAX_EXCEPTION_COUNT)
193       {
194         CLog::Log(LOGERROR, "CApplication::Render(), too many exceptions");
195         throw;
196       }
197     }
198     catch (...)
199     {
200       CLog::Log(LOGERROR, "exception in CApplication::Render()");
201       renderExceptionCount++;
202       //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
203       if (renderExceptionCount > MAX_EXCEPTION_COUNT)
204       {
205         CLog::Log(LOGERROR, "CApplication::Render(), too many exceptions");
206         throw;
207       }
208     }
209 #endif
210   } // while (!m_bStop)
211   Destroy();
212
213 #ifdef TARGET_DVBBOX // oskwon
214   system("xbmc.helper --stop");
215 #endif /*TARGET_DVBBOX*/
216
217   CLog::Log(LOGNOTICE, "application stopped..." );
218   return m_ExitCode;
219 }