Merge pull request #4436 from ace20022/ext_sub_details
[vuplus_xbmc] / xbmc / Application.h
1 #pragma once
2
3 /*
4  *      Copyright (C) 2005-2013 Team XBMC
5  *      http://xbmc.org
6  *
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)
10  *  any later version.
11  *
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.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with XBMC; see the file COPYING.  If not, see
19  *  <http://www.gnu.org/licenses/>.
20  *
21  */
22
23 #include "system.h" // for HAS_DVD_DRIVE et. al.
24 #include "XBApplicationEx.h"
25
26 #include "guilib/IMsgTargetCallback.h"
27 #include "threads/Condition.h"
28 #include "utils/GlobalsHandling.h"
29
30 #include <map>
31
32 class CAction;
33 class CFileItem;
34 class CFileItemList;
35 class CKey;
36 namespace ADDON
37 {
38   class CSkinInfo;
39   class IAddon;
40   typedef boost::shared_ptr<IAddon> AddonPtr;
41 }
42
43 namespace MEDIA_DETECT
44 {
45   class CAutorun;
46 }
47 class CPlayerController;
48
49 #include "cores/IPlayerCallback.h"
50 #include "cores/playercorefactory/PlayerCoreFactory.h"
51 #include "PlayListPlayer.h"
52 #include "settings/lib/ISettingsHandler.h"
53 #include "settings/lib/ISettingCallback.h"
54 #include "settings/lib/ISubSettings.h"
55 #if !defined(TARGET_WINDOWS) && defined(HAS_DVD_DRIVE)
56 #include "storage/DetectDVDType.h"
57 #endif
58 #ifdef TARGET_WINDOWS
59 #include "win32/WIN32Util.h"
60 #endif
61 #include "utils/Stopwatch.h"
62 #ifdef HAS_PERFORMANCE_SAMPLE
63 #include "utils/PerformanceStats.h"
64 #endif
65 #include "windowing/XBMC_events.h"
66 #include "threads/Thread.h"
67
68 #include "ApplicationPlayer.h"
69
70 class CSeekHandler;
71 class CKaraokeLyricsManager;
72 class CInertialScrollingHandler;
73 class DPMSSupport;
74 class CSplash;
75 class CBookmark;
76 class CNetwork;
77
78 namespace VIDEO
79 {
80   class CVideoInfoScanner;
81 }
82
83 namespace MUSIC_INFO
84 {
85   class CMusicInfoScanner;
86 }
87
88 #define VOLUME_MINIMUM 0.0f        // -60dB
89 #define VOLUME_MAXIMUM 1.0f        // 0dB
90 #define VOLUME_DYNAMIC_RANGE 90.0f // 60dB
91 #define VOLUME_CONTROL_STEPS 90    // 90 steps
92
93 // replay gain settings struct for quick access by the player multiple
94 // times per second (saves doing settings lookup)
95 struct ReplayGainSettings
96 {
97   int iPreAmp;
98   int iNoGainPreAmp;
99   int iType;
100   bool bAvoidClipping;
101 };
102
103 class CBackgroundPlayer : public CThread
104 {
105 public:
106   CBackgroundPlayer(const CFileItem &item, int iPlayList);
107   virtual ~CBackgroundPlayer();
108   virtual void Process();
109 protected:
110   CFileItem *m_item;
111   int       m_iPlayList;
112 };
113
114 class CApplication : public CXBApplicationEx, public IPlayerCallback, public IMsgTargetCallback,
115                      public ISettingCallback, public ISettingsHandler, public ISubSettings
116 {
117   friend class CApplicationPlayer;
118 public:
119
120   enum ESERVERS
121   {
122     ES_WEBSERVER = 1,
123     ES_AIRPLAYSERVER,
124     ES_JSONRPCSERVER,
125     ES_UPNPRENDERER,
126     ES_UPNPSERVER,
127     ES_EVENTSERVER,
128     ES_ZEROCONF
129   };
130
131   CApplication(void);
132   virtual ~CApplication(void);
133   virtual bool Initialize();
134   virtual void FrameMove(bool processEvents, bool processGUI = true);
135   virtual void Render();
136   virtual bool RenderNoPresent();
137   virtual void Preflight();
138   virtual bool Create();
139   virtual bool Cleanup();
140
141   bool CreateGUI();
142   bool InitWindow();
143   bool DestroyWindow();
144   void StartServices();
145   void StopServices();
146
147   bool StartServer(enum ESERVERS eServer, bool bStart, bool bWait = false);
148
149   void StartPVRManager(bool bOpenPVRWindow = false);
150   void StopPVRManager();
151   bool IsCurrentThread() const;
152   void Stop(int exitCode);
153   void RestartApp();
154   void UnloadSkin(bool forReload = false);
155   bool LoadUserWindows();
156   void ReloadSkin(bool confirm = false);
157   const CStdString& CurrentFile();
158   CFileItem& CurrentFileItem();
159   CFileItem& CurrentUnstackedItem();
160   virtual bool OnMessage(CGUIMessage& message);
161   PLAYERCOREID GetCurrentPlayer();
162   virtual void OnPlayBackEnded();
163   virtual void OnPlayBackStarted();
164   virtual void OnPlayBackPaused();
165   virtual void OnPlayBackResumed();
166   virtual void OnPlayBackStopped();
167   virtual void OnQueueNextItem();
168   virtual void OnPlayBackSeek(int iTime, int seekOffset);
169   virtual void OnPlayBackSeekChapter(int iChapter);
170   virtual void OnPlayBackSpeedChanged(int iSpeed);
171   bool PlayMedia(const CFileItem& item, int iPlaylist = PLAYLIST_MUSIC);
172   bool PlayMediaSync(const CFileItem& item, int iPlaylist = PLAYLIST_MUSIC);
173   bool ProcessAndStartPlaylist(const CStdString& strPlayList, PLAYLIST::CPlayList& playlist, int iPlaylist, int track=0);
174   PlayBackRet PlayFile(const CFileItem& item, bool bRestart = false);
175   void SaveFileState(bool bForeground = false);
176   void UpdateFileState();
177   void StopPlaying();
178   void Restart(bool bSamePosition = true);
179   void DelayedPlayerRestart();
180   void CheckDelayedPlayerRestart();
181   bool IsPlayingFullScreenVideo() const;
182   bool IsStartingPlayback() const { return m_bPlaybackStarting; }
183   bool IsFullScreen();
184   bool OnKey(const CKey& key);
185   bool OnAppCommand(const CAction &action);
186   bool OnAction(const CAction &action);
187   void CheckShutdown();
188   void InhibitIdleShutdown(bool inhibit);
189   bool IsIdleShutdownInhibited() const;
190   // Checks whether the screensaver and / or DPMS should become active.
191   void CheckScreenSaverAndDPMS();
192   void CheckPlayingProgress();
193   void ActivateScreenSaver(bool forceType = false);
194   void CloseNetworkShares();
195
196   virtual void Process();
197   void ProcessSlow();
198   void ResetScreenSaver();
199   float GetVolume(bool percentage = true) const;
200   void SetVolume(float iValue, bool isPercentage = true);
201   bool IsMuted() const;
202   bool IsMutedInternal() const { return m_muted; }
203   void ToggleMute(void);
204   void SetMute(bool mute);
205   void ShowVolumeBar(const CAction *action = NULL);
206   int GetSubtitleDelay() const;
207   int GetAudioDelay() const;
208   void ResetSystemIdleTimer();
209   void ResetScreenSaverTimer();
210   void StopScreenSaverTimer();
211   // Wakes up from the screensaver and / or DPMS. Returns true if woken up.
212   bool WakeUpScreenSaverAndDPMS(bool bPowerOffKeyPressed = false);
213   bool WakeUpScreenSaver(bool bPowerOffKeyPressed = false);
214   /*!
215    \brief Returns the total time in fractional seconds of the currently playing media
216
217    Beware that this method returns fractional seconds whereas IPlayer::GetTotalTime() returns milliseconds.
218    */
219   double GetTotalTime() const;
220   /*!
221    \brief Returns the current time in fractional seconds of the currently playing media
222
223    Beware that this method returns fractional seconds whereas IPlayer::GetTime() returns milliseconds.
224    */
225   double GetTime() const;
226   float GetPercentage() const;
227
228   // Get the percentage of data currently cached/buffered (aq/vq + FileCache) from the input stream if applicable.
229   float GetCachePercentage() const;
230
231   void SeekPercentage(float percent);
232   void SeekTime( double dTime = 0.0 );
233
234   void StopShutdownTimer();
235   void ResetShutdownTimers();
236
237   void StopVideoScan();
238   void StopMusicScan();
239   bool IsMusicScanning() const;
240   bool IsVideoScanning() const;
241
242   void StartVideoCleanup();
243
244   void StartVideoScan(const CStdString &path, bool scanAll = false);
245   void StartMusicScan(const CStdString &path, int flags = 0);
246   void StartMusicAlbumScan(const CStdString& strDirectory, bool refresh=false);
247   void StartMusicArtistScan(const CStdString& strDirectory, bool refresh=false);
248
249   void UpdateLibraries();
250   void CheckMusicPlaylist();
251
252   bool ExecuteXBMCAction(std::string action);
253
254   static bool OnEvent(XBMC_Event& newEvent);
255
256   CNetwork& getNetwork();
257 #ifdef HAS_PERFORMANCE_SAMPLE
258   CPerformanceStats &GetPerformanceStats();
259 #endif
260
261 #ifdef HAS_DVD_DRIVE
262   MEDIA_DETECT::CAutorun* m_Autorun;
263 #endif
264
265 #if !defined(TARGET_WINDOWS) && defined(HAS_DVD_DRIVE)
266   MEDIA_DETECT::CDetectDVDMedia m_DetectDVDType;
267 #endif
268
269   CApplicationPlayer* m_pPlayer;
270
271   inline bool IsInScreenSaver() { return m_bScreenSave; };
272   int m_iScreenSaveLock; // spiff: are we checking for a lock? if so, ignore the screensaver state, if -1 we have failed to input locks
273
274   bool m_bPlaybackStarting;
275   typedef enum
276   {
277     PLAY_STATE_NONE = 0,
278     PLAY_STATE_STARTING,
279     PLAY_STATE_PLAYING,
280     PLAY_STATE_STOPPED,
281     PLAY_STATE_ENDED,
282   } PlayState;
283   PlayState m_ePlayState;
284   CCriticalSection m_playStateMutex;
285
286   bool m_bInBackground;
287   inline bool IsInBackground() { return m_bInBackground; };
288   void SetInBackground(bool background);
289
290   CKaraokeLyricsManager* m_pKaraokeMgr;
291
292   PLAYERCOREID m_eForcedNextPlayer;
293   CStdString m_strPlayListFile;
294
295   int GlobalIdleTime();
296
297   void EnablePlatformDirectories(bool enable=true)
298   {
299     m_bPlatformDirectories = enable;
300   }
301
302   bool PlatformDirectoriesEnabled()
303   {
304     return m_bPlatformDirectories;
305   }
306
307   void SetStandAlone(bool value);
308
309   bool IsStandAlone()
310   {
311     return m_bStandalone;
312   }
313
314   void SetEnableLegacyRes(bool value)
315   {
316     m_bEnableLegacyRes = value;
317   }
318
319   bool IsEnableLegacyRes()
320   {
321     return m_bEnableLegacyRes;
322   }
323
324   void SetEnableTestMode(bool value)
325   {
326     m_bTestMode = value;
327   }
328
329   bool IsEnableTestMode()
330   {
331     return m_bTestMode;
332   }
333
334   void Minimize();
335   bool ToggleDPMS(bool manual);
336
337   float GetDimScreenSaverLevel() const;
338
339   /*! \brief Retrieve the applications seek handler.
340    \return a constant pointer to the seek handler.
341    \sa CSeekHandler
342    */
343   const CSeekHandler *GetSeekHandler() const { return m_seekHandler; };
344
345   bool SwitchToFullScreen();
346
347   CSplash* GetSplash() { return m_splash; }
348   void SetRenderGUI(bool renderGUI);
349   bool GetRenderGUI() const { return m_renderGUI; };
350
351   bool SetLanguage(const CStdString &strLanguage);
352
353   ReplayGainSettings& GetReplayGainSettings() { return m_replayGainSettings; }
354
355   void SetLoggingIn(bool loggingIn) { m_loggingIn = loggingIn; }
356
357 protected:
358   virtual bool OnSettingsSaving() const;
359
360   virtual bool Load(const TiXmlNode *settings);
361   virtual bool Save(TiXmlNode *settings) const;
362
363   virtual void OnSettingChanged(const CSetting *setting);
364   virtual void OnSettingAction(const CSetting *setting);
365   virtual bool OnSettingUpdate(CSetting* &setting, const char *oldSettingId, const TiXmlNode *oldSettingNode);
366
367   bool LoadSkin(const CStdString& skinID);
368   void LoadSkin(const boost::shared_ptr<ADDON::CSkinInfo>& skin);
369
370   bool m_skinReloading; // if true we disallow LoadSkin until ReloadSkin is called
371   bool m_skinReverting;
372
373   bool m_loggingIn;
374
375 #if defined(TARGET_DARWIN_IOS)
376   friend class CWinEventsIOS;
377 #endif
378 #if defined(TARGET_ANDROID)
379   friend class CWinEventsAndroid;
380 #endif
381   // screensaver
382   bool m_bScreenSave;
383   ADDON::AddonPtr m_screenSaver;
384
385   // timer information
386 #ifdef TARGET_WINDOWS
387   CWinIdleTimer m_idleTimer;
388   CWinIdleTimer m_screenSaverTimer;
389 #else
390   CStopWatch m_idleTimer;
391   CStopWatch m_screenSaverTimer;
392 #endif
393   CStopWatch m_restartPlayerTimer;
394   CStopWatch m_frameTime;
395   CStopWatch m_navigationTimer;
396   CStopWatch m_slowTimer;
397   CStopWatch m_shutdownTimer;
398
399   bool m_bInhibitIdleShutdown;
400
401   DPMSSupport* m_dpms;
402   bool m_dpmsIsActive;
403   bool m_dpmsIsManual;
404
405   CFileItemPtr m_itemCurrentFile;
406   CFileItemList* m_currentStack;
407   CFileItemPtr m_stackFileItemToUpdate;
408
409   CStdString m_prevMedia;
410   CSplash* m_splash;
411   ThreadIdentifier m_threadID;       // application thread ID.  Used in applicationMessanger to know where we are firing a thread with delay from.
412   bool m_bInitializing;
413   bool m_bPlatformDirectories;
414
415   CBookmark& m_progressTrackingVideoResumeBookmark;
416   CFileItemPtr m_progressTrackingItem;
417   bool m_progressTrackingPlayCountUpdate;
418
419   int m_currentStackPosition;
420   int m_nextPlaylistItem;
421
422   bool m_bPresentFrame;
423   unsigned int m_lastFrameTime;
424   unsigned int m_lastRenderTime;
425
426   bool m_bStandalone;
427   bool m_bEnableLegacyRes;
428   bool m_bTestMode;
429   bool m_bSystemScreenSaverEnable;
430
431   VIDEO::CVideoInfoScanner *m_videoInfoScanner;
432   MUSIC_INFO::CMusicInfoScanner *m_musicInfoScanner;
433
434   bool m_muted;
435   float m_volumeLevel;
436
437   void Mute();
438   void UnMute();
439
440   void SetHardwareVolume(float hardwareVolume);
441
442   void VolumeChanged() const;
443
444   PlayBackRet PlayStack(const CFileItem& item, bool bRestart);
445   bool ProcessMouse();
446   bool ProcessRemote(float frameTime);
447   bool ProcessGamepad(float frameTime);
448   bool ProcessEventServer(float frameTime);
449   bool ProcessPeripherals(float frameTime);
450   bool ProcessJoystickEvent(const std::string& joystickName, int button, short inputType, float fAmount, unsigned int holdTime = 0);
451   bool ExecuteInputAction(const CAction &action);
452   int  GetActiveWindowID(void);
453
454   float NavigationIdleTime();
455   static bool AlwaysProcess(const CAction& action);
456
457   void SaveCurrentFileSettings();
458
459   bool InitDirectoriesLinux();
460   bool InitDirectoriesOSX();
461   bool InitDirectoriesWin32();
462   void CreateUserDirs();
463
464   CSeekHandler *m_seekHandler;
465   CPlayerController *m_playerController;
466   CInertialScrollingHandler *m_pInertialScrollingHandler;
467   CNetwork    *m_network;
468 #ifdef HAS_PERFORMANCE_SAMPLE
469   CPerformanceStats m_perfStats;
470 #endif
471
472 #ifdef HAS_EVENT_SERVER
473   std::map<std::string, std::map<int, float> > m_lastAxisMap;
474 #endif
475
476   ReplayGainSettings m_replayGainSettings;
477 };
478
479 XBMC_GLOBAL_REF(CApplication,g_application);
480 #define g_application XBMC_GLOBAL_USE(CApplication)