4 * Copyright (C) 2005-2008 Team XBMC
7 * This Program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This Program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with XBMC; see the file COPYING. If not, write to
19 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20 * http://www.gnu.org/copyleft/gpl.html
24 #include "system.h" // for HAS_DVD_DRIVE et. al.
25 #include "XBApplicationEx.h"
27 #include "guilib/IMsgTargetCallback.h"
28 #include "threads/Condition.h"
38 typedef boost::shared_ptr<IAddon> AddonPtr;
41 namespace MEDIA_DETECT
46 #include "cores/IPlayer.h"
47 #include "cores/playercorefactory/PlayerCoreFactory.h"
48 #include "PlayListPlayer.h"
49 #if !defined(_WIN32) && defined(HAS_DVD_DRIVE)
50 #include "storage/DetectDVDType.h"
53 #include "win32/WIN32Util.h"
55 #include "utils/Stopwatch.h"
56 #include "ApplicationMessenger.h"
57 #include "network/Network.h"
58 #include "utils/CharsetConverter.h"
59 #ifdef HAS_PERFORMANCE_SAMPLE
60 #include "utils/PerformanceStats.h"
62 #include "windowing/XBMC_events.h"
63 #include "threads/Thread.h"
65 class CKaraokeLyricsManager;
66 class CInertialScrollingHandler;
67 class CApplicationMessenger;
74 class CHTTPImageHandler;
75 class CHTTPVfsHandler;
77 class CHTTPJsonRpcHandler;
80 class CHTTPApiHandler;
82 #ifdef HAS_WEB_INTERFACE
83 class CHTTPWebinterfaceHandler;
84 class CHTTPWebinterfaceAddonsHandler;
89 class CVideoInfoScanner;
94 class CMusicInfoScanner;
97 class CBackgroundPlayer : public CThread
100 CBackgroundPlayer(const CFileItem &item, int iPlayList);
101 virtual ~CBackgroundPlayer();
102 virtual void Process();
108 class CApplication : public CXBApplicationEx, public IPlayerCallback, public IMsgTargetCallback
112 virtual ~CApplication(void);
113 virtual bool Initialize();
114 virtual void FrameMove(bool processEvents, bool processGUI = true);
115 virtual void Render();
116 virtual bool RenderNoPresent();
117 virtual void Preflight();
118 virtual bool Create();
119 virtual bool Cleanup();
123 bool DestroyWindow();
124 void StartServices();
126 bool StartWebServer();
127 void StopWebServer();
128 void StartAirplayServer();
129 void StopAirplayServer(bool bWait);
130 bool StartJSONRPCServer();
131 void StopJSONRPCServer(bool bWait);
133 void StopUPnP(bool bWait);
134 void StartUPnPRenderer();
135 void StopUPnPRenderer();
136 void StartUPnPServer();
137 void StopUPnPServer();
138 bool StartEventServer();
139 bool StopEventServer(bool bWait, bool promptuser);
140 void RefreshEventServer();
141 void StartZeroconf();
143 void DimLCDOnPlayback(bool dim);
144 bool IsCurrentThread() const;
145 void Stop(int exitCode);
147 void UnloadSkin(bool forReload = false);
148 bool LoadUserWindows();
150 const CStdString& CurrentFile();
151 CFileItem& CurrentFileItem();
152 virtual bool OnMessage(CGUIMessage& message);
153 PLAYERCOREID GetCurrentPlayer();
154 virtual void OnPlayBackEnded();
155 virtual void OnPlayBackStarted();
156 virtual void OnPlayBackPaused();
157 virtual void OnPlayBackResumed();
158 virtual void OnPlayBackStopped();
159 virtual void OnQueueNextItem();
160 virtual void OnPlayBackSeek(int iTime, int seekOffset);
161 virtual void OnPlayBackSeekChapter(int iChapter);
162 virtual void OnPlayBackSpeedChanged(int iSpeed);
163 bool PlayMedia(const CFileItem& item, int iPlaylist = PLAYLIST_MUSIC);
164 bool PlayMediaSync(const CFileItem& item, int iPlaylist = PLAYLIST_MUSIC);
165 bool ProcessAndStartPlaylist(const CStdString& strPlayList, PLAYLIST::CPlayList& playlist, int iPlaylist, int track=0);
166 bool PlayFile(const CFileItem& item, bool bRestart = false);
167 void SaveFileState(bool bForeground = false);
168 void UpdateFileState();
170 void Restart(bool bSamePosition = true);
171 void DelayedPlayerRestart();
172 void CheckDelayedPlayerRestart();
173 bool IsPlaying() const;
174 bool IsPaused() const;
175 bool IsPlayingAudio() const;
176 bool IsPlayingVideo() const;
177 bool IsPlayingFullScreenVideo() const;
178 bool IsStartingPlayback() const { return m_bPlaybackStarting; }
180 bool OnKey(const CKey& key);
181 bool OnAppCommand(const CAction &action);
182 bool OnAction(const CAction &action);
183 void CheckShutdown();
184 // Checks whether the screensaver and / or DPMS should become active.
185 void CheckScreenSaverAndDPMS();
186 void CheckPlayingProgress();
187 void CheckAudioScrobblerStatus();
188 void CheckForTitleChange();
189 void ActivateScreenSaver(bool forceType = false);
191 virtual void Process();
193 void ResetScreenSaver();
194 int GetVolume() const;
195 void SetVolume(float iValue, bool isPercentage = true);
196 bool IsMuted() const;
197 void ToggleMute(void);
198 void ShowVolumeBar(const CAction *action = NULL);
199 int GetPlaySpeed() const;
200 int GetSubtitleDelay() const;
201 int GetAudioDelay() const;
202 void SetPlaySpeed(int iSpeed);
203 void ResetSystemIdleTimer();
204 void ResetScreenSaverTimer();
205 void StopScreenSaverTimer();
206 // Wakes up from the screensaver and / or DPMS. Returns true if woken up.
207 bool WakeUpScreenSaverAndDPMS(bool bPowerOffKeyPressed = false);
208 bool WakeUpScreenSaver(bool bPowerOffKeyPressed = false);
209 double GetTotalTime() const;
210 double GetTime() const;
211 float GetPercentage() const;
213 // Get the percentage of data currently cached/buffered (aq/vq + FileCache) from the input stream if applicable.
214 float GetCachePercentage() const;
216 void SeekPercentage(float percent);
217 void SeekTime( double dTime = 0.0 );
219 void StopShutdownTimer();
220 void ResetShutdownTimers();
222 void SaveMusicScanSettings();
223 void RestoreMusicScanSettings();
225 void StopVideoScan();
226 void StopMusicScan();
227 bool IsMusicScanning() const;
228 bool IsVideoScanning() const;
230 void StartVideoCleanup();
232 void StartVideoScan(const CStdString &path, bool scanAll = false);
233 void StartMusicScan(const CStdString &path, int flags = 0);
234 void StartMusicAlbumScan(const CStdString& strDirectory, bool refresh=false);
235 void StartMusicArtistScan(const CStdString& strDirectory, bool refresh=false);
237 void UpdateLibraries();
238 void CheckMusicPlaylist();
240 bool ExecuteXBMCAction(std::string action);
242 static bool OnEvent(XBMC_Event& newEvent);
244 CApplicationMessenger& getApplicationMessenger();
245 #if defined(HAS_LINUX_NETWORK)
246 CNetworkLinux& getNetwork();
247 #elif defined(HAS_WIN32_NETWORK)
248 CNetworkWin32& getNetwork();
250 CNetwork& getNetwork();
252 #ifdef HAS_PERFORMANCE_SAMPLE
253 CPerformanceStats &GetPerformanceStats();
257 MEDIA_DETECT::CAutorun* m_Autorun;
260 #if !defined(_WIN32) && defined(HAS_DVD_DRIVE)
261 MEDIA_DETECT::CDetectDVDMedia m_DetectDVDType;
266 #ifdef HAS_WEB_SERVER
267 CWebServer& m_WebServer;
268 CHTTPImageHandler& m_httpImageHandler;
269 CHTTPVfsHandler& m_httpVfsHandler;
271 CHTTPJsonRpcHandler& m_httpJsonRpcHandler;
274 CHTTPApiHandler& m_httpApiHandler;
276 #ifdef HAS_WEB_INTERFACE
277 CHTTPWebinterfaceHandler& m_httpWebinterfaceHandler;
278 CHTTPWebinterfaceAddonsHandler& m_httpWebinterfaceAddonsHandler;
282 inline bool IsInScreenSaver() { return m_bScreenSave; };
283 int m_iScreenSaveLock; // spiff: are we checking for a lock? if so, ignore the screensaver state, if -1 we have failed to input locks
286 bool m_bPlaybackStarting;
288 CKaraokeLyricsManager* m_pKaraokeMgr;
290 PLAYERCOREID m_eForcedNextPlayer;
291 CStdString m_strPlayListFile;
293 int GlobalIdleTime();
295 bool WaitFrame(unsigned int timeout);
297 void EnablePlatformDirectories(bool enable=true)
299 m_bPlatformDirectories = enable;
302 bool PlatformDirectoriesEnabled()
304 return m_bPlatformDirectories;
307 void SetStandAlone(bool value);
311 return m_bStandalone;
314 void SetEnableLegacyRes(bool value)
316 m_bEnableLegacyRes = value;
319 bool IsEnableLegacyRes()
321 return m_bEnableLegacyRes;
324 void SetEnableTestMode(bool value)
329 bool IsEnableTestMode()
334 bool IsPresentFrame();
337 bool ToggleDPMS(bool manual);
339 float GetDimScreenSaverLevel() const;
341 bool LoadSkin(const CStdString& skinID);
342 void LoadSkin(const boost::shared_ptr<ADDON::CSkinInfo>& skin);
344 bool m_skinReloading; // if true we disallow LoadSkin until ReloadSkin is called
346 friend class CApplicationMessenger;
347 #if defined(TARGET_DARWIN_IOS)
348 friend class CWinEventsIOS;
352 ADDON::AddonPtr m_screenSaver;
356 CWinIdleTimer m_idleTimer;
358 CStopWatch m_idleTimer;
360 CStopWatch m_restartPlayerTimer;
361 CStopWatch m_frameTime;
362 CStopWatch m_navigationTimer;
363 CStopWatch m_slowTimer;
364 CStopWatch m_screenSaverTimer;
365 CStopWatch m_shutdownTimer;
371 CFileItemPtr m_itemCurrentFile;
372 CFileItemList* m_currentStack;
373 CFileItemPtr m_stackFileItemToUpdate;
375 CStdString m_prevMedia;
377 ThreadIdentifier m_threadID; // application thread ID. Used in applicationMessanger to know where we are firing a thread with delay from.
378 PLAYERCOREID m_eCurrentPlayer;
379 bool m_bSettingsLoaded;
380 bool m_bAllSettingsLoaded;
381 bool m_bInitializing;
382 bool m_bPlatformDirectories;
384 CBookmark& m_progressTrackingVideoResumeBookmark;
385 CFileItemPtr m_progressTrackingItem;
386 bool m_progressTrackingPlayCountUpdate;
389 int m_currentStackPosition;
390 int m_nextPlaylistItem;
392 bool m_bPresentFrame;
393 unsigned int m_lastFrameTime;
394 unsigned int m_lastRenderTime;
397 bool m_bEnableLegacyRes;
399 bool m_bSystemScreenSaverEnable;
402 CCriticalSection m_frameMutex;
403 XbmcThreads::ConditionVariable m_frameCond;
405 VIDEO::CVideoInfoScanner *m_videoInfoScanner;
406 MUSIC_INFO::CMusicInfoScanner *m_musicInfoScanner;
411 void SetHardwareVolume(float hardwareVolume);
413 void FatalErrorHandler(bool WindowSystemInitialized, bool MapDrives, bool InitNetwork);
415 void VolumeChanged() const;
417 bool PlayStack(const CFileItem& item, bool bRestart);
418 bool SwitchToFullScreen();
420 bool ProcessRemote(float frameTime);
421 bool ProcessGamepad(float frameTime);
422 bool ProcessEventServer(float frameTime);
423 bool ProcessPeripherals(float frameTime);
424 bool ProcessHTTPApiButtons();
425 bool ProcessJsonRpcButtons();
426 bool ProcessJoystickEvent(const std::string& joystickName, int button, bool isAxis, float fAmount, unsigned int holdTime = 0);
427 int GetActiveWindowID(void);
429 float NavigationIdleTime();
430 static bool AlwaysProcess(const CAction& action);
432 void SaveCurrentFileSettings();
434 bool InitDirectoriesLinux();
435 bool InitDirectoriesOSX();
436 bool InitDirectoriesWin32();
437 void CreateUserDirs();
439 CInertialScrollingHandler *m_pInertialScrollingHandler;
440 CApplicationMessenger m_applicationMessenger;
441 #if defined(HAS_LINUX_NETWORK)
442 CNetworkLinux m_network;
443 #elif defined(HAS_WIN32_NETWORK)
444 CNetworkWin32 m_network;
448 #ifdef HAS_PERFORMANCE_SAMPLE
449 CPerformanceStats m_perfStats;
452 #ifdef HAS_EVENT_SERVER
453 std::map<std::string, std::map<int, float> > m_lastAxisMap;
458 extern CApplication g_application;