summaryrefslogtreecommitdiffstats
path: root/xbmc/interfaces/python/XBPython.h
blob: e54b4a2193433f81110d2f3e988eba0f0d4d65b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

#include "cores/IPlayerCallback.h"
#include "interfaces/IAnnouncer.h"
#include "interfaces/generic/ILanguageInvocationHandler.h"
#include "threads/CriticalSection.h"
#include "threads/Event.h"
#include "threads/Thread.h"

#include <memory>
#include <vector>

class CPythonInvoker;
class CVariant;

typedef struct
{
  int id;
  bool bDone;
  CPythonInvoker* pyThread;
} PyElem;

class LibraryLoader;

namespace XBMCAddon
{
namespace xbmc
{
class Monitor;
}
} // namespace XBMCAddon

template<class T>
struct LockableType : public T, public CCriticalSection
{
  bool hadSomethingRemoved;
};

typedef LockableType<std::vector<void*>> PlayerCallbackList;
typedef LockableType<std::vector<XBMCAddon::xbmc::Monitor*>> MonitorCallbackList;
typedef LockableType<std::vector<PyElem>> PyList;
typedef std::vector<LibraryLoader*> PythonExtensionLibraries;

class XBPython : public IPlayerCallback,
                 public ANNOUNCEMENT::IAnnouncer,
                 public ILanguageInvocationHandler
{
public:
  XBPython();
  ~XBPython() override;
  void OnPlayBackEnded() override;
  void OnPlayBackStarted(const CFileItem& file) override;
  void OnAVStarted(const CFileItem& file) override;
  void OnAVChange() override;
  void OnPlayBackPaused() override;
  void OnPlayBackResumed() override;
  void OnPlayBackStopped() override;
  void OnPlayBackError() override;
  void OnPlayBackSpeedChanged(int iSpeed) override;
  void OnPlayBackSeek(int64_t iTime, int64_t seekOffset) override;
  void OnPlayBackSeekChapter(int iChapter) override;
  void OnQueueNextItem() override;

  void Announce(ANNOUNCEMENT::AnnouncementFlag flag,
                const std::string& sender,
                const std::string& message,
                const CVariant& data) override;
  void RegisterPythonPlayerCallBack(IPlayerCallback* pCallback);
  void UnregisterPythonPlayerCallBack(IPlayerCallback* pCallback);
  void RegisterPythonMonitorCallBack(XBMCAddon::xbmc::Monitor* pCallback);
  void UnregisterPythonMonitorCallBack(XBMCAddon::xbmc::Monitor* pCallback);
  void OnSettingsChanged(const std::string& strings);
  void OnScreensaverActivated();
  void OnScreensaverDeactivated();
  void OnDPMSActivated();
  void OnDPMSDeactivated();
  void OnScanStarted(const std::string& library);
  void OnScanFinished(const std::string& library);
  void OnCleanStarted(const std::string& library);
  void OnCleanFinished(const std::string& library);
  void OnNotification(const std::string& sender,
                      const std::string& method,
                      const std::string& data);

  void Process() override;
  void PulseGlobalEvent() override;
  void Uninitialize() override;
  bool OnScriptInitialized(ILanguageInvoker* invoker) override;
  void OnScriptStarted(ILanguageInvoker* invoker) override;
  void NotifyScriptAborting(ILanguageInvoker* invoker) override;
  void OnExecutionEnded(ILanguageInvoker* invoker) override;
  void OnScriptFinalized(ILanguageInvoker* invoker) override;
  ILanguageInvoker* CreateInvoker() override;

  bool WaitForEvent(CEvent& hEvent, unsigned int milliseconds);

private:
  static bool m_bInitialized; // whether global python runtime was already initialized

  CCriticalSection m_critSection;
  void* m_mainThreadState{nullptr};
  int m_iDllScriptCounter{0}; // to keep track of the total scripts running that need the dll

  //Vector with list of threads used for running scripts
  PyList m_vecPyList;
  PlayerCallbackList m_vecPlayerCallbackList;
  MonitorCallbackList m_vecMonitorCallbackList;

  // any global events that scripts should be using
  CEvent m_globalEvent;

  // in order to finalize and unload the python library, need to save all the extension libraries that are
  // loaded by it and unload them first (not done by finalize)
  PythonExtensionLibraries m_extensions;
};