/
GUIPythonWindow.cpp
273 lines (238 loc) · 7.03 KB
/
GUIPythonWindow.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright (C) 2005-2008 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "pyutil.h"
#include "GUIPythonWindow.h"
#include "window.h"
#include "control.h"
#include "action.h"
#include "guilib/GUIWindowManager.h"
#include "../XBPython.h"
#include "utils/log.h"
#include "threads/SingleLock.h"
using namespace PYXBMC;
PyXBMCAction::PyXBMCAction(void*& callback)
: param(0), pCallbackWindow(NULL), pObject(NULL), controlId(0), type(0)
{
// this is ugly, but we can't grab python lock
// while holding gfx, that will potentially deadlock
CSingleExit ex(g_graphicsContext);
PyEval_AcquireLock();
// callback is a reference to a pointer in the
// window, that is controlled by the python lock.
// callback can become null while we are trying
// to grab python lock above, so anything using
// this should allow that situation
void* tmp = callback; // copy the referenced value
pCallbackWindow = (PyObject*)tmp; // assign internally
Py_XINCREF((PyObject*)callback);
PyEval_ReleaseLock();
}
PyXBMCAction::~PyXBMCAction() {
Py_XDECREF((PyObject*)pObject);
Py_XDECREF((PyObject*)pCallbackWindow);
}
CGUIPythonWindow::CGUIPythonWindow(int id)
: CGUIWindow(id, "")
{
pCallbackWindow = NULL;
m_threadState = NULL;
m_actionEvent = CreateEvent(NULL, true, false, NULL);
m_loadOnDemand = false;
}
CGUIPythonWindow::~CGUIPythonWindow(void)
{
CloseHandle(m_actionEvent);
}
bool CGUIPythonWindow::OnAction(const CAction &action)
{
// do the base class window first, and the call to python after this
bool ret = CGUIWindow::OnAction(action);
// workaround - for scripts which try to access the active control (focused) when there is none.
// for example - the case when the mouse enters the screen.
CGUIControl *pControl = GetFocusedControl();
if (action.IsMouse() && !pControl)
return ret;
if(pCallbackWindow)
{
PyXBMCAction* inf = new PyXBMCAction(pCallbackWindow);
inf->pObject = Action_FromAction(action);
// aquire lock?
PyXBMC_AddPendingCall((PyThreadState*)m_threadState, Py_XBMC_Event_OnAction, inf);
PulseActionEvent();
}
return ret;
}
bool CGUIPythonWindow::OnMessage(CGUIMessage& message)
{
switch (message.GetMessage())
{
case GUI_MSG_WINDOW_DEINIT:
{
g_windowManager.ShowOverlay(OVERLAY_STATE_SHOWN);
}
break;
case GUI_MSG_WINDOW_INIT:
{
CGUIWindow::OnMessage(message);
g_windowManager.ShowOverlay(OVERLAY_STATE_HIDDEN);
return true;
}
break;
case GUI_MSG_CLICKED:
{
int iControl=message.GetSenderId();
if(pCallbackWindow)
{
PyXBMCAction* inf = new PyXBMCAction(pCallbackWindow);
// find python control object with same iControl
std::vector<Control*>::iterator it = ((PYXBMC::Window*)pCallbackWindow)->vecControls.begin();
while (it != ((PYXBMC::Window*)pCallbackWindow)->vecControls.end())
{
Control* pControl = *it;
if (pControl->iControlId == iControl)
{
inf->pObject = pControl;
Py_INCREF((PyObject*)inf->pObject);
break;
}
++it;
}
// did we find our control?
if (inf->pObject)
{
// currently we only accept messages from a button or controllist with a select action
if ((ControlList_CheckExact((PyObject*)inf->pObject) && (message.GetParam1() == ACTION_SELECT_ITEM || message.GetParam1() == ACTION_MOUSE_LEFT_CLICK)) ||
ControlButton_CheckExact((PyObject*)inf->pObject) || ControlRadioButton_CheckExact((PyObject*)inf->pObject) ||
ControlCheckMark_CheckExact((PyObject*)inf->pObject))
{
// aquire lock?
PyXBMC_AddPendingCall((PyThreadState*)m_threadState, Py_XBMC_Event_OnControl, inf);
PulseActionEvent();
// return true here as we are handling the event
return true;
}
}
// if we get here, we didn't add the action
delete inf;
}
}
break;
}
return CGUIWindow::OnMessage(message);
}
void CGUIPythonWindow::SetCallbackWindow(void *state, void *object)
{
pCallbackWindow = object;
m_threadState = state;
}
void CGUIPythonWindow::WaitForActionEvent(unsigned int timeout)
{
g_pythonParser.WaitForEvent(m_actionEvent, timeout);
ResetEvent(m_actionEvent);
}
void CGUIPythonWindow::PulseActionEvent()
{
SetEvent(m_actionEvent);
}
#ifdef _LINUX
/*
vector<PyXBMCAction*> g_actionQueue;
CRITICAL_SECTION g_critSection;
void Py_InitCriticalSection()
{
static bool first_call = true;
if (first_call)
{
InitializeCriticalSection(&g_critSection);
first_call = false;
}
}
void Py_AddPendingActionCall(PyXBMCAction* inf)
{
EnterCriticalSection(&g_critSection);
g_actionQueue.push_back(inf);
LeaveCriticalSection(&g_critSection);
}
void Py_MakePendingActionCalls()
{
vector<PyXBMCAction*>::iterator iter;
iter = g_actionQueue.begin();
while (iter!=g_actionQueue.end())
{
PyXBMCAction* arg = (*iter);
EnterCriticalSection(&g_critSection);
g_actionQueue.erase(iter);
LeaveCriticalSection(&g_critSection);
if (arg->type==0)
{
Py_XBMC_Event_OnAction(arg);
} else if (arg->type==1) {
Py_XBMC_Event_OnControl(arg);
}
EnterCriticalSection(&g_critSection);
iter=g_actionQueue.begin();
LeaveCriticalSection(&g_critSection);
}
}
*/
#endif
/*
* called from python library!
*/
int Py_XBMC_Event_OnControl(void* arg)
{
if(!arg)
return 0;
PyXBMCAction* action = (PyXBMCAction*)arg;
if (action->pCallbackWindow)
{
PyXBMCAction* action = (PyXBMCAction*)arg;
PyObject *ret = PyObject_CallMethod((PyObject*)action->pCallbackWindow, (char*)"onControl", (char*)"(O)", (PyObject*)action->pObject);
if (ret) {
Py_DECREF(ret);
}
}
delete action;
return 0;
}
/*
* called from python library!
*/
int Py_XBMC_Event_OnAction(void* arg)
{
if(!arg)
return 0;
PyXBMCAction* action = (PyXBMCAction*)arg;
if (action->pCallbackWindow)
{
Action *pAction= (Action *)action->pObject;
PyObject *ret = PyObject_CallMethod((PyObject*)action->pCallbackWindow, (char*)"onAction", (char*)"(O)", (PyObject*)pAction);
if (ret) {
Py_DECREF(ret);
}
else {
CLog::Log(LOGERROR,"Exception in python script's onAction");
PyErr_Print();
}
}
delete action;
return 0;
}