PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/xbmc/interfaces/python/xbmcmodule/PythonMonitor.cpp

https://github.com/xbmc/xbmc-rbp
C++ | 137 lines | 92 code | 21 blank | 24 comment | 5 complexity | 2bd25e17b9964498bcf271b1ba5ba8a4 MD5 | raw file
  1. /*
  2. * Copyright (C) 2005-2012 Team XBMC
  3. * http://www.xbmc.org
  4. *
  5. * This Program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  9. *
  10. * This Program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with XBMC; see the file COPYING. If not, write to
  17. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. */
  21. #include "PythonMonitor.h"
  22. #include "pyutil.h"
  23. #include "pythreadstate.h"
  24. #include "../XBPython.h"
  25. #include "threads/Atomics.h"
  26. using namespace PYXBMC;
  27. struct SPyMonitor
  28. {
  29. SPyMonitor(CPythonMonitor* monitor
  30. , const std::string &function, const std::string &arg = "")
  31. {
  32. m_monitor = monitor;
  33. m_monitor->Acquire();
  34. m_function = function;
  35. m_arg = arg;
  36. }
  37. ~SPyMonitor()
  38. {
  39. m_monitor->Release();
  40. }
  41. std::string m_function;
  42. std::string m_arg;
  43. CPythonMonitor* m_monitor;
  44. };
  45. /*
  46. * called from python library!
  47. */
  48. static int SPyMonitor_Function(void* e)
  49. {
  50. SPyMonitor* object = (SPyMonitor*)e;
  51. PyObject* ret = NULL;
  52. if(object->m_monitor->m_callback)
  53. {
  54. if (!object->m_arg.empty())
  55. ret = PyObject_CallMethod(object->m_monitor->m_callback, (char*)object->m_function.c_str(), (char*)"(s)", object->m_arg.c_str());
  56. else
  57. ret = PyObject_CallMethod(object->m_monitor->m_callback, (char*)object->m_function.c_str(), NULL);
  58. }
  59. if(ret)
  60. {
  61. Py_DECREF(ret);
  62. }
  63. CPyThreadState pyState;
  64. delete object;
  65. return 0;
  66. }
  67. CPythonMonitor::CPythonMonitor()
  68. {
  69. m_callback = NULL;
  70. m_refs = 1;
  71. g_pythonParser.RegisterPythonMonitorCallBack(this);
  72. }
  73. void CPythonMonitor::Release()
  74. {
  75. if(AtomicDecrement(&m_refs) == 0)
  76. delete this;
  77. }
  78. void CPythonMonitor::Acquire()
  79. {
  80. AtomicIncrement(&m_refs);
  81. }
  82. CPythonMonitor::~CPythonMonitor(void)
  83. {
  84. g_pythonParser.UnregisterPythonMonitorCallBack(this);
  85. }
  86. void CPythonMonitor::OnSettingsChanged()
  87. {
  88. PyXBMC_AddPendingCall(m_state, SPyMonitor_Function, new SPyMonitor(this, "onSettingsChanged"));
  89. g_pythonParser.PulseGlobalEvent();
  90. }
  91. void CPythonMonitor::OnScreensaverActivated()
  92. {
  93. PyXBMC_AddPendingCall(m_state, SPyMonitor_Function, new SPyMonitor(this, "onScreensaverActivated"));
  94. g_pythonParser.PulseGlobalEvent();
  95. }
  96. void CPythonMonitor::OnScreensaverDeactivated()
  97. {
  98. PyXBMC_AddPendingCall(m_state, SPyMonitor_Function, new SPyMonitor(this, "onScreensaverDeactivated"));
  99. g_pythonParser.PulseGlobalEvent();
  100. }
  101. void CPythonMonitor::OnDatabaseUpdated(const std::string &database)
  102. {
  103. PyXBMC_AddPendingCall(m_state, SPyMonitor_Function, new SPyMonitor(this, "onDatabaseUpdated", database));
  104. g_pythonParser.PulseGlobalEvent();
  105. }
  106. void CPythonMonitor::OnAbortRequested()
  107. {
  108. PyXBMC_AddPendingCall(m_state, SPyMonitor_Function, new SPyMonitor(this, "onAbortRequested"));
  109. g_pythonParser.PulseGlobalEvent();
  110. }
  111. void CPythonMonitor::SetCallback(PyThreadState *state, PyObject *object)
  112. {
  113. /* python lock should be held */
  114. m_callback = object;
  115. m_state = state;
  116. }