/xbmc/Application.h

http://github.com/xbmc/xbmc · C Header · 490 lines · 330 code · 89 blank · 71 comment · 2 complexity · 92ddb7b49a6c587b2011d6d62ae84d09 MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2018 Team Kodi
  3. * This file is part of Kodi - https://kodi.tv
  4. *
  5. * SPDX-License-Identifier: GPL-2.0-or-later
  6. * See LICENSES/README.md for more information.
  7. */
  8. #pragma once
  9. #include "XBApplicationEx.h"
  10. #include "guilib/IMsgTargetCallback.h"
  11. #include "windowing/Resolution.h"
  12. #include "utils/GlobalsHandling.h"
  13. #include "messaging/IMessageTarget.h"
  14. #include "ServiceManager.h"
  15. #include "ApplicationStackHelper.h"
  16. #include <atomic>
  17. #include <deque>
  18. #include <map>
  19. #include <memory>
  20. #include <string>
  21. #include <vector>
  22. #include "cores/IPlayerCallback.h"
  23. #include "settings/lib/ISettingsHandler.h"
  24. #include "settings/lib/ISettingCallback.h"
  25. #include "settings/ISubSettings.h"
  26. #if !defined(TARGET_WINDOWS) && defined(HAS_DVD_DRIVE)
  27. #include "storage/DetectDVDType.h"
  28. #endif
  29. #ifdef TARGET_WINDOWS
  30. #include "powermanagement/WinIdleTimer.h"
  31. #endif
  32. #include "utils/Stopwatch.h"
  33. #include "windowing/OSScreenSaver.h"
  34. #include "windowing/XBMC_events.h"
  35. #include "threads/SystemClock.h"
  36. #include "threads/Thread.h"
  37. #include "ApplicationPlayer.h"
  38. class CAction;
  39. class CFileItem;
  40. class CFileItemList;
  41. class CKey;
  42. class CSeekHandler;
  43. class CInertialScrollingHandler;
  44. class CSplash;
  45. class CBookmark;
  46. class IActionListener;
  47. class CGUIComponent;
  48. class CAppInboundProtocol;
  49. class CSettingsComponent;
  50. namespace ADDON
  51. {
  52. class CSkinInfo;
  53. class IAddon;
  54. typedef std::shared_ptr<IAddon> AddonPtr;
  55. }
  56. namespace ANNOUNCEMENT
  57. {
  58. class CAnnouncementManager;
  59. }
  60. namespace MEDIA_DETECT
  61. {
  62. class CAutorun;
  63. }
  64. namespace PLAYLIST
  65. {
  66. class CPlayList;
  67. }
  68. namespace ActiveAE
  69. {
  70. class CActiveAE;
  71. }
  72. namespace VIDEO
  73. {
  74. class CVideoInfoScanner;
  75. }
  76. namespace MUSIC_INFO
  77. {
  78. class CMusicInfoScanner;
  79. }
  80. #define VOLUME_MINIMUM 0.0f // -60dB
  81. #define VOLUME_MAXIMUM 1.0f // 0dB
  82. #define VOLUME_DYNAMIC_RANGE 90.0f // 60dB
  83. // replay gain settings struct for quick access by the player multiple
  84. // times per second (saves doing settings lookup)
  85. struct ReplayGainSettings
  86. {
  87. int iPreAmp;
  88. int iNoGainPreAmp;
  89. int iType;
  90. bool bAvoidClipping;
  91. };
  92. enum StartupAction
  93. {
  94. STARTUP_ACTION_NONE = 0,
  95. STARTUP_ACTION_PLAY_TV,
  96. STARTUP_ACTION_PLAY_RADIO
  97. };
  98. class CApplication : public CXBApplicationEx, public IPlayerCallback, public IMsgTargetCallback,
  99. public ISettingCallback, public ISettingsHandler, public ISubSettings,
  100. public KODI::MESSAGING::IMessageTarget
  101. {
  102. friend class CAppInboundProtocol;
  103. public:
  104. // If playback time of current item is greater than this value, ACTION_PREV_ITEM will seek to start
  105. // of currently playing item, otherwise it will seek to start of the previous item in playlist
  106. static const unsigned int ACTION_PREV_ITEM_THRESHOLD = 3; // seconds;
  107. enum ESERVERS
  108. {
  109. ES_WEBSERVER = 1,
  110. ES_AIRPLAYSERVER,
  111. ES_JSONRPCSERVER,
  112. ES_UPNPRENDERER,
  113. ES_UPNPSERVER,
  114. ES_EVENTSERVER,
  115. ES_ZEROCONF
  116. };
  117. CApplication(void);
  118. ~CApplication(void) override;
  119. bool Initialize() override;
  120. void FrameMove(bool processEvents, bool processGUI = true) override;
  121. void Render() override;
  122. virtual void Preflight();
  123. bool Create(const CAppParamParser &params);
  124. bool Cleanup() override;
  125. bool IsInitialized() { return !m_bInitializing; }
  126. bool CreateGUI();
  127. bool InitWindow(RESOLUTION res = RES_INVALID);
  128. void StartServices();
  129. void StopServices();
  130. bool StartServer(enum ESERVERS eServer, bool bStart, bool bWait = false);
  131. bool IsCurrentThread() const;
  132. void Stop(int exitCode);
  133. void UnloadSkin(bool forReload = false);
  134. bool LoadCustomWindows();
  135. void ReloadSkin(bool confirm = false);
  136. const std::string& CurrentFile();
  137. CFileItem& CurrentFileItem();
  138. std::shared_ptr<CFileItem> CurrentFileItemPtr();
  139. CFileItem& CurrentUnstackedItem();
  140. bool OnMessage(CGUIMessage& message) override;
  141. CApplicationPlayer& GetAppPlayer();
  142. std::string GetCurrentPlayer();
  143. CApplicationStackHelper& GetAppStackHelper();
  144. void OnPlayBackEnded() override;
  145. void OnPlayBackStarted(const CFileItem &file) override;
  146. void OnPlayerCloseFile(const CFileItem &file, const CBookmark &bookmark) override;
  147. void OnPlayBackPaused() override;
  148. void OnPlayBackResumed() override;
  149. void OnPlayBackStopped() override;
  150. void OnPlayBackError() override;
  151. void OnQueueNextItem() override;
  152. void OnPlayBackSeek(int64_t iTime, int64_t seekOffset) override;
  153. void OnPlayBackSeekChapter(int iChapter) override;
  154. void OnPlayBackSpeedChanged(int iSpeed) override;
  155. void OnAVChange() override;
  156. void OnAVStarted(const CFileItem &file) override;
  157. void RequestVideoSettings(const CFileItem &fileItem) override;
  158. void StoreVideoSettings(const CFileItem &fileItem, CVideoSettings vs) override;
  159. int GetMessageMask() override;
  160. void OnApplicationMessage(KODI::MESSAGING::ThreadMessage* pMsg) override;
  161. bool PlayMedia(CFileItem& item, const std::string &player, int iPlaylist);
  162. bool ProcessAndStartPlaylist(const std::string& strPlayList, PLAYLIST::CPlayList& playlist, int iPlaylist, int track=0);
  163. bool PlayFile(CFileItem item, const std::string& player, bool bRestart = false);
  164. void StopPlaying();
  165. void Restart(bool bSamePosition = true);
  166. void DelayedPlayerRestart();
  167. void CheckDelayedPlayerRestart();
  168. bool IsPlayingFullScreenVideo() const;
  169. bool IsFullScreen();
  170. bool OnAction(const CAction &action);
  171. void CheckShutdown();
  172. void InhibitIdleShutdown(bool inhibit);
  173. bool IsIdleShutdownInhibited() const;
  174. void InhibitScreenSaver(bool inhibit);
  175. bool IsScreenSaverInhibited() const;
  176. // Checks whether the screensaver and / or DPMS should become active.
  177. void CheckScreenSaverAndDPMS();
  178. void ActivateScreenSaver(bool forceType = false);
  179. void CloseNetworkShares();
  180. void ShowAppMigrationMessage();
  181. void Process() override;
  182. void ProcessSlow();
  183. void ResetScreenSaver();
  184. float GetVolumePercent() const;
  185. float GetVolumeRatio() const;
  186. void SetVolume(float iValue, bool isPercentage = true);
  187. bool IsMuted() const;
  188. bool IsMutedInternal() const { return m_muted; }
  189. void ToggleMute(void);
  190. void SetMute(bool mute);
  191. void ShowVolumeBar(const CAction *action = NULL);
  192. int GetSubtitleDelay();
  193. int GetAudioDelay();
  194. void ResetSystemIdleTimer();
  195. void ResetScreenSaverTimer();
  196. void StopScreenSaverTimer();
  197. // Wakes up from the screensaver and / or DPMS. Returns true if woken up.
  198. bool WakeUpScreenSaverAndDPMS(bool bPowerOffKeyPressed = false);
  199. bool WakeUpScreenSaver(bool bPowerOffKeyPressed = false);
  200. /*!
  201. \brief Returns the total time in fractional seconds of the currently playing media
  202. Beware that this method returns fractional seconds whereas IPlayer::GetTotalTime() returns milliseconds.
  203. */
  204. double GetTotalTime() const;
  205. /*!
  206. \brief Returns the current time in fractional seconds of the currently playing media
  207. Beware that this method returns fractional seconds whereas IPlayer::GetTime() returns milliseconds.
  208. */
  209. double GetTime() const;
  210. float GetPercentage() const;
  211. // Get the percentage of data currently cached/buffered (aq/vq + FileCache) from the input stream if applicable.
  212. float GetCachePercentage() const;
  213. void SeekPercentage(float percent);
  214. void SeekTime( double dTime = 0.0 );
  215. void StopShutdownTimer();
  216. void ResetShutdownTimers();
  217. void StopVideoScan();
  218. void StopMusicScan();
  219. bool IsMusicScanning() const;
  220. bool IsVideoScanning() const;
  221. /*!
  222. \brief Starts a video library cleanup.
  223. \param userInitiated Whether the action was initiated by the user (either via GUI or any other method) or not. It is meant to hide or show dialogs.
  224. \param content Content type to clean, blank for everything
  225. */
  226. void StartVideoCleanup(bool userInitiated = true, const std::string& content = "");
  227. /*!
  228. \brief Starts a video library update.
  229. \param path The path to scan or "" (empty string) for a global scan.
  230. \param userInitiated Whether the action was initiated by the user (either via GUI or any other method) or not. It is meant to hide or show dialogs.
  231. \param scanAll Whether to scan everything not already scanned (regardless of whether the user normally doesn't want a folder scanned).
  232. */
  233. void StartVideoScan(const std::string &path, bool userInitiated = true, bool scanAll = false);
  234. /*!
  235. \brief Starts a music library cleanup.
  236. \param userInitiated Whether the action was initiated by the user (either via GUI or any other method) or not. It is meant to hide or show dialogs.
  237. */
  238. void StartMusicCleanup(bool userInitiated = true);
  239. /*!
  240. \brief Starts a music library update.
  241. \param path The path to scan or "" (empty string) for a global scan.
  242. \param userInitiated Whether the action was initiated by the user (either via GUI or any other method) or not. It is meant to hide or show dialogs.
  243. \param flags Flags for controlling the scanning process. See xbmc/music/infoscanner/MusicInfoScanner.h for possible values.
  244. */
  245. void StartMusicScan(const std::string &path, bool userInitiated = true, int flags = 0);
  246. void StartMusicAlbumScan(const std::string& strDirectory, bool refresh = false);
  247. void StartMusicArtistScan(const std::string& strDirectory, bool refresh = false);
  248. void UpdateLibraries();
  249. void UpdateCurrentPlayArt();
  250. bool ExecuteXBMCAction(std::string action, const CGUIListItemPtr &item = NULL);
  251. #ifdef HAS_DVD_DRIVE
  252. std::unique_ptr<MEDIA_DETECT::CAutorun> m_Autorun;
  253. #endif
  254. #if !defined(TARGET_WINDOWS) && defined(HAS_DVD_DRIVE)
  255. MEDIA_DETECT::CDetectDVDMedia m_DetectDVDType;
  256. #endif
  257. inline bool IsInScreenSaver() { return m_screensaverActive; };
  258. inline std::string ScreensaverIdInUse() { return m_screensaverIdInUse; }
  259. inline bool IsDPMSActive() { return m_dpmsIsActive; };
  260. int m_iScreenSaveLock = 0; // spiff: are we checking for a lock? if so, ignore the screensaver state, if -1 we have failed to input locks
  261. std::string m_strPlayListFile;
  262. int GlobalIdleTime();
  263. bool PlatformDirectoriesEnabled() { return m_bPlatformDirectories; }
  264. bool IsStandAlone() { return m_bStandalone; }
  265. bool IsEnableTestMode() { return m_bTestMode; }
  266. bool IsAppFocused() const { return m_AppFocused; }
  267. void Minimize();
  268. bool ToggleDPMS(bool manual);
  269. bool SwitchToFullScreen(bool force = false);
  270. bool GetRenderGUI() const override { return m_renderGUI; };
  271. bool SetLanguage(const std::string &strLanguage);
  272. bool LoadLanguage(bool reload);
  273. ReplayGainSettings& GetReplayGainSettings() { return m_replayGainSettings; }
  274. void SetLoggingIn(bool switchingProfiles);
  275. /*!
  276. \brief Register an action listener.
  277. \param listener The listener to register
  278. */
  279. void RegisterActionListener(IActionListener *listener);
  280. /*!
  281. \brief Unregister an action listener.
  282. \param listener The listener to unregister
  283. */
  284. void UnregisterActionListener(IActionListener *listener);
  285. std::unique_ptr<CServiceManager> m_ServiceManager;
  286. /*!
  287. \brief Locks calls from outside kodi (e.g. python) until framemove is processed.
  288. */
  289. void LockFrameMoveGuard();
  290. /*!
  291. \brief Unlocks calls from outside kodi (e.g. python).
  292. */
  293. void UnlockFrameMoveGuard();
  294. void SetRenderGUI(bool renderGUI);
  295. protected:
  296. bool OnSettingsSaving() const override;
  297. bool Load(const TiXmlNode *settings) override;
  298. bool Save(TiXmlNode *settings) const override;
  299. void OnSettingChanged(std::shared_ptr<const CSetting> setting) override;
  300. void OnSettingAction(std::shared_ptr<const CSetting> setting) override;
  301. bool OnSettingUpdate(std::shared_ptr<CSetting> setting, const char *oldSettingId, const TiXmlNode *oldSettingNode) override;
  302. bool LoadSkin(const std::string& skinID);
  303. void CheckOSScreenSaverInhibitionSetting();
  304. void PlaybackCleanup();
  305. // inbound protocol
  306. bool OnEvent(XBMC_Event& newEvent);
  307. /*!
  308. \brief Delegates the action to all registered action handlers.
  309. \param action The action
  310. \return true, if the action was taken by one of the action listener.
  311. */
  312. bool NotifyActionListeners(const CAction &action) const;
  313. std::shared_ptr<ANNOUNCEMENT::CAnnouncementManager> m_pAnnouncementManager;
  314. std::unique_ptr<CSettingsComponent> m_pSettingsComponent;
  315. std::unique_ptr<CGUIComponent> m_pGUI;
  316. std::unique_ptr<CWinSystemBase> m_pWinSystem;
  317. std::unique_ptr<ActiveAE::CActiveAE> m_pActiveAE;
  318. std::shared_ptr<CAppInboundProtocol> m_pAppPort;
  319. std::deque<XBMC_Event> m_portEvents;
  320. CCriticalSection m_portSection;
  321. bool m_confirmSkinChange = true;
  322. bool m_ignoreSkinSettingChanges = false;
  323. bool m_saveSkinOnUnloading = true;
  324. bool m_autoExecScriptExecuted = false;
  325. #if defined(TARGET_DARWIN_IOS)
  326. friend class CWinEventsIOS;
  327. #endif
  328. #if defined(TARGET_DARWIN_TVOS)
  329. friend class CWinEventsTVOS;
  330. #endif
  331. #if defined(TARGET_ANDROID)
  332. friend class CWinEventsAndroid;
  333. #endif
  334. // screensaver
  335. bool m_screensaverActive = false;
  336. std::string m_screensaverIdInUse;
  337. ADDON::AddonPtr m_pythonScreenSaver; // @warning: Fallback for Python interface, for binaries not needed!
  338. // OS screen saver inhibitor that is always active if user selected a Kodi screen saver
  339. KODI::WINDOWING::COSScreenSaverInhibitor m_globalScreensaverInhibitor;
  340. // Inhibitor that is active e.g. during video playback
  341. KODI::WINDOWING::COSScreenSaverInhibitor m_screensaverInhibitor;
  342. // timer information
  343. #ifdef TARGET_WINDOWS
  344. CWinIdleTimer m_idleTimer;
  345. CWinIdleTimer m_screenSaverTimer;
  346. #else
  347. CStopWatch m_idleTimer;
  348. CStopWatch m_screenSaverTimer;
  349. #endif
  350. CStopWatch m_restartPlayerTimer;
  351. CStopWatch m_frameTime;
  352. CStopWatch m_navigationTimer;
  353. CStopWatch m_slowTimer;
  354. CStopWatch m_shutdownTimer;
  355. XbmcThreads::EndTime m_guiRefreshTimer;
  356. bool m_bInhibitIdleShutdown = false;
  357. bool m_bInhibitScreenSaver = false;
  358. bool m_dpmsIsActive = false;
  359. bool m_dpmsIsManual = false;
  360. CFileItemPtr m_itemCurrentFile;
  361. std::string m_prevMedia;
  362. std::thread::id m_threadID; // application thread ID. Used in applicationMessenger to know where we are firing a thread with delay from.
  363. bool m_bInitializing = true;
  364. bool m_bPlatformDirectories = true;
  365. int m_nextPlaylistItem = -1;
  366. unsigned int m_lastRenderTime = 0;
  367. bool m_skipGuiRender = false;
  368. bool m_bStandalone = false;
  369. bool m_bTestMode = false;
  370. bool m_bSystemScreenSaverEnable = false;
  371. std::unique_ptr<MUSIC_INFO::CMusicInfoScanner> m_musicInfoScanner;
  372. bool m_muted = false;
  373. float m_volumeLevel = VOLUME_MAXIMUM;
  374. void Mute();
  375. void UnMute();
  376. void SetHardwareVolume(float hardwareVolume);
  377. void VolumeChanged();
  378. bool PlayStack(CFileItem& item, bool bRestart);
  379. float NavigationIdleTime();
  380. void HandlePortEvents();
  381. /*! \brief Helper method to determine how to handle TMSG_SHUTDOWN
  382. */
  383. void HandleShutdownMessage();
  384. CInertialScrollingHandler *m_pInertialScrollingHandler;
  385. ReplayGainSettings m_replayGainSettings;
  386. std::vector<IActionListener *> m_actionListeners;
  387. std::vector<std::string> m_incompatibleAddons; /*!< Result of addon migration */
  388. private:
  389. mutable CCriticalSection m_critSection; /*!< critical section for all changes to this class, except for changes to triggers */
  390. CCriticalSection m_frameMoveGuard; /*!< critical section for synchronizing GUI actions from inside and outside (python) */
  391. std::atomic_uint m_WaitingExternalCalls; /*!< counts threads wich are waiting to be processed in FrameMove */
  392. unsigned int m_ProcessedExternalCalls = 0; /*!< counts calls wich are processed during one "door open" cycle in FrameMove */
  393. unsigned int m_ProcessedExternalDecay = 0; /*!< counts to close door after a few frames of no python activity */
  394. CApplicationPlayer m_appPlayer;
  395. CEvent m_playerEvent;
  396. CApplicationStackHelper m_stackHelper;
  397. };
  398. XBMC_GLOBAL_REF(CApplication,g_application);
  399. #define g_application XBMC_GLOBAL_USE(CApplication)