4 * Copyright (C) 2005-2013 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 "XBPyThread.h"
25 #include "cores/IPlayerCallback.h"
26 #include "threads/CriticalSection.h"
27 #include "interfaces/IAnnouncer.h"
28 #include "addons/IAddon.h"
30 #include <boost/shared_ptr.hpp>
37 boost::shared_ptr<XBPyThread> pyThread;
50 template <class T> struct LockableType : public T, public CCriticalSection
51 { bool hadSomethingRemoved; };
53 typedef LockableType<std::vector<void*> > PlayerCallbackList;
54 typedef LockableType<std::vector<XBMCAddon::xbmc::Monitor*> > MonitorCallbackList;
55 typedef LockableType<std::vector<PyElem> > PyList;
56 typedef std::vector<LibraryLoader*> PythonExtensionLibraries;
59 public IPlayerCallback,
60 public ANNOUNCEMENT::IAnnouncer
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();
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="");
88 void FinalizeScript();
92 void PulseGlobalEvent();
93 bool WaitForEvent(CEvent& hEvent, unsigned int milliseconds);
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);
101 bool isRunning(int scriptId);
102 bool isStopping(int scriptId);
103 void setDone(int id);
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
109 bool StopScript(const CStdString &path);
111 // inject xbmc stuff into the interpreter.
112 // should be called for every new interpreter
113 void InitializeInterpreter(ADDON::AddonPtr addon);
115 // remove modules and references when interpreter done
116 void DeInitializeInterpreter();
118 void RegisterExtensionLib(LibraryLoader *pLib);
119 void UnregisterExtensionLib(LibraryLoader *pLib);
120 void UnloadExtensionLibs();
122 //only should be called from thread which is running the script
123 void stopScript(int scriptId);
125 // returns NULL if script doesn't exist or if script doesn't have a filename
126 const char* getFileName(int scriptId);
128 // returns -1 if no scripts exist with specified filename
129 int getScriptId(const CStdString &strFile);
131 void* getMainThreadState();
135 CCriticalSection m_critSection;
136 bool FileExist(const char* strFile);
139 void* m_mainThreadState;
140 ThreadIdentifier m_ThreadId;
142 int m_iDllScriptCounter; // to keep track of the total scripts running that need the dll
143 unsigned int m_endtime;
145 //Vector with list of threads used for running scripts
147 PlayerCallbackList m_vecPlayerCallbackList;
148 MonitorCallbackList m_vecMonitorCallbackList;
149 LibraryLoader* m_pDll;
151 // any global events that scripts should be using
152 CEvent m_globalEvent;
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;
159 extern XBPython g_pythonParser;