[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / interfaces / python / XBPython.h
1 #pragma once
2
3 /*
4  *      Copyright (C) 2005-2013 Team XBMC
5  *      http://www.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, write to
19  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20  *  http://www.gnu.org/copyleft/gpl.html
21  *
22  */
23
24 #include "XBPyThread.h"
25 #include "cores/IPlayer.h"
26 #include "threads/CriticalSection.h"
27 #include "interfaces/IAnnouncer.h"
28 #include "addons/IAddon.h"
29
30 #include <boost/shared_ptr.hpp>
31 #include <vector>
32
33 typedef struct {
34   int id;
35   bool bDone;
36   std::string strFile;
37   boost::shared_ptr<XBPyThread> pyThread;
38 }PyElem;
39
40 class LibraryLoader;
41
42 namespace XBMCAddon
43 {
44   namespace xbmc
45   {
46     class Monitor;
47   }
48 }
49
50 template <class T> struct LockableType : public T, public CCriticalSection 
51 { bool hadSomethingRemoved; };
52
53 typedef LockableType<std::vector<PVOID> > PlayerCallbackList;
54 typedef LockableType<std::vector<XBMCAddon::xbmc::Monitor*> > MonitorCallbackList;
55 typedef LockableType<std::vector<PyElem> > PyList;
56 typedef std::vector<LibraryLoader*> PythonExtensionLibraries;
57
58 class XBPython : 
59   public IPlayerCallback,
60   public ANNOUNCEMENT::IAnnouncer
61 {
62   void Finalize();
63 public:
64   XBPython();
65   virtual ~XBPython();
66   virtual void OnPlayBackEnded();
67   virtual void OnPlayBackStarted();
68   virtual void OnPlayBackPaused();
69   virtual void OnPlayBackResumed();
70   virtual void OnPlayBackStopped();
71   virtual void OnPlayBackSpeedChanged(int iSpeed);
72   virtual void OnPlayBackSeek(int iTime, int seekOffset);
73   virtual void OnPlayBackSeekChapter(int iChapter);
74   virtual void OnQueueNextItem();
75
76   virtual void Announce(ANNOUNCEMENT::AnnouncementFlag flag, const char *sender, const char *message, const CVariant &data);
77   void RegisterPythonPlayerCallBack(IPlayerCallback* pCallback);
78   void UnregisterPythonPlayerCallBack(IPlayerCallback* pCallback);
79   void RegisterPythonMonitorCallBack(XBMCAddon::xbmc::Monitor* pCallback);
80   void UnregisterPythonMonitorCallBack(XBMCAddon::xbmc::Monitor* pCallback);
81   void OnSettingsChanged(const CStdString &strings);
82   void OnScreensaverActivated();
83   void OnScreensaverDeactivated();
84   void OnDatabaseUpdated(const std::string &database);
85   void OnDatabaseScanStarted(const std::string &database);
86   void OnAbortRequested(const CStdString &ID="");
87   void Initialize();
88   void FinalizeScript();
89   void FreeResources();
90   void Process();
91
92   void PulseGlobalEvent();
93   bool WaitForEvent(CEvent& hEvent, unsigned int milliseconds);
94
95   int ScriptsSize();
96   int GetPythonScriptId(int scriptPosition);
97   int evalFile(const CStdString &src, ADDON::AddonPtr addon);
98   int evalFile(const CStdString &src, const std::vector<CStdString> &argv, ADDON::AddonPtr addon);
99   int evalString(const CStdString &src, const std::vector<CStdString> &argv);
100
101   bool isRunning(int scriptId);
102   bool isStopping(int scriptId);
103   void setDone(int id);
104   
105   /*! \brief Stop a script if it's running
106    \param path path to the script
107    \return true if the script was running and is now stopped, false otherwise
108    */
109   bool StopScript(const CStdString &path);
110
111   // inject xbmc stuff into the interpreter.
112   // should be called for every new interpreter
113   void InitializeInterpreter(ADDON::AddonPtr addon);
114
115   // remove modules and references when interpreter done
116   void DeInitializeInterpreter();
117
118   void RegisterExtensionLib(LibraryLoader *pLib);
119   void UnregisterExtensionLib(LibraryLoader *pLib);
120   void UnloadExtensionLibs();
121
122   //only should be called from thread which is running the script
123   void  stopScript(int scriptId);
124
125   // returns NULL if script doesn't exist or if script doesn't have a filename
126   const char* getFileName(int scriptId);
127
128   // returns -1 if no scripts exist with specified filename
129   int getScriptId(const CStdString &strFile);
130
131   void* getMainThreadState();
132
133   bool m_bLogin;
134 private:
135   CCriticalSection    m_critSection;
136   bool              FileExist(const char* strFile);
137
138   int               m_nextid;
139   void*             m_mainThreadState;
140   ThreadIdentifier  m_ThreadId;
141   bool              m_bInitialized;
142   int               m_iDllScriptCounter; // to keep track of the total scripts running that need the dll
143   unsigned int      m_endtime;
144
145   //Vector with list of threads used for running scripts
146   PyList              m_vecPyList;
147   PlayerCallbackList  m_vecPlayerCallbackList;
148   MonitorCallbackList m_vecMonitorCallbackList;
149   LibraryLoader*      m_pDll;
150
151   // any global events that scripts should be using
152   CEvent m_globalEvent;
153
154   // in order to finalize and unload the python library, need to save all the extension libraries that are
155   // loaded by it and unload them first (not done by finalize)
156   PythonExtensionLibraries m_extensions;
157 };
158
159 extern XBPython g_pythonParser;