-
Notifications
You must be signed in to change notification settings - Fork 18
/
PythonPlugin.cpp
251 lines (224 loc) · 6.02 KB
/
PythonPlugin.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
/*
Copyright (C) 2013 Johannes Blume
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
of the License, 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <Windows.h>
#include <Python.h>
#include "./rainmeter-plugin-sdk/API/RainmeterAPI.h"
PyObject* CreateRainmeterObject(void *rm);
PyThreadState *mainThreadState = NULL;
struct Measure
{
Measure()
{
measureObject = NULL;
getStringResult = NULL;
}
PyObject *measureObject;
wchar_t *getStringResult;
};
PLUGIN_EXPORT void Initialize(void** data, void* rm)
{
LPCWSTR pythonHome = RmReadString(rm, L"PythonHome", NULL, FALSE);
if (pythonHome != NULL)
{
Py_SetPythonHome((wchar_t*) pythonHome);
}
Py_Initialize();
PyEval_InitThreads();
mainThreadState = PyThreadState_Get();
Measure *measure = new Measure;
*data = measure;
PyEval_SaveThread();
}
void AddDirToPath(LPCWSTR dir)
{
PyObject *pathObj = PySys_GetObject("path");
PyObject *scriptDirObj = PyUnicode_FromWideChar(dir, -1);
if (!PySequence_Contains(pathObj, scriptDirObj))
{
PyList_Append(pathObj, scriptDirObj);
}
Py_DECREF(scriptDirObj);
}
void LoadScript(LPCWSTR scriptPath, char* fileName, LPCWSTR className, Measure* measure)
{
try
{
FILE* f = _Py_wfopen(scriptPath, L"r");
if (f == NULL)
{
throw L"Error opening Python script";
}
PyObject *globals = PyModule_GetDict(PyImport_AddModule("__main__"));
PyObject *result = PyRun_FileEx(f, fileName, Py_file_input, globals, globals, 1);
if (result == NULL)
{
throw L"Error loading Python script";
}
Py_DECREF(result);
PyObject *classNameObj = PyUnicode_FromWideChar(className, -1);
PyObject *classObj = PyDict_GetItem(globals, classNameObj);
Py_DECREF(classNameObj);
if (classObj == NULL)
{
throw L"Python class not found";
}
PyObject *measureObj = PyObject_CallObject(classObj, NULL);
if (measureObj == NULL)
{
throw L"Error instantiating Python class";
}
measure->measureObject = measureObj;
measure->getStringResult = NULL;
}
catch (wchar_t *error)
{
measure->measureObject = NULL;
measure->getStringResult = error;
}
}
PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
{
Measure *measure = (Measure*) data;
PyEval_RestoreThread(mainThreadState);
if (measure->measureObject == NULL)
{
LPCWSTR scriptPath = RmReadPath(rm, L"ScriptPath", L"default.py");
wchar_t scriptBaseName[_MAX_FNAME];
wchar_t scriptExt[_MAX_EXT];
wchar_t scriptDir[_MAX_DIR];
_wsplitpath_s(scriptPath, NULL, 0, scriptDir, _MAX_DIR, scriptBaseName, _MAX_FNAME, scriptExt, _MAX_EXT);
AddDirToPath(scriptDir);
wchar_t fileName[_MAX_FNAME + 1 + _MAX_EXT];
lstrcpyW(fileName, scriptBaseName);
lstrcatW(fileName, L".");
lstrcatW(fileName, scriptExt);
char fileNameMb[_MAX_FNAME + 1 + _MAX_EXT];
wcstombs_s(NULL, fileNameMb, sizeof(fileNameMb), fileName, sizeof(fileName));
LPCWSTR className = RmReadString(rm, L"ClassName", L"Measure", FALSE);
LoadScript(scriptPath, fileNameMb, className, measure);
}
if (measure->measureObject != NULL)
{
PyObject *rainmeterObject = CreateRainmeterObject(rm);
PyObject *resultObj = PyObject_CallMethod(measure->measureObject, "Reload", "Od", rainmeterObject, maxValue);
if (resultObj != NULL)
{
Py_DECREF(resultObj);
}
else
{
PyErr_Clear();
}
Py_DECREF(rainmeterObject);
}
PyEval_SaveThread();
}
PLUGIN_EXPORT double Update(void* data)
{
Measure *measure = (Measure*) data;
if (measure->measureObject == NULL)
{
return 0.0;
}
PyEval_RestoreThread(mainThreadState);
PyObject *resultObj = PyObject_CallMethod(measure->measureObject, "Update", NULL);
double result = 0.0;
if (resultObj != NULL)
{
result = PyFloat_Check(resultObj) ? PyFloat_AsDouble(resultObj) : 0.0;
Py_DECREF(resultObj);
}
else
{
PyErr_Clear();
}
PyEval_SaveThread();
return result;
}
PLUGIN_EXPORT LPCWSTR GetString(void* data)
{
Measure *measure = (Measure*) data;
if (measure->measureObject == NULL)
{
return measure->getStringResult;
}
PyEval_RestoreThread(mainThreadState);
PyObject *resultObj = PyObject_CallMethod(measure->measureObject, "GetString", NULL);
if (measure->getStringResult != NULL)
{
PyMem_Free(measure->getStringResult);
measure->getStringResult = NULL;
}
if (resultObj != NULL)
{
if (resultObj != Py_None)
{
PyObject *strObj = PyObject_Str(resultObj);
measure->getStringResult = PyUnicode_AsWideCharString(strObj, NULL);
Py_DECREF(strObj);
}
Py_DECREF(resultObj);
}
else
{
PyErr_Clear();
}
PyEval_SaveThread();
return measure->getStringResult;
}
PLUGIN_EXPORT void ExecuteBang(void* data, LPCWSTR args)
{
Measure *measure = (Measure*) data;
if (measure->measureObject == NULL)
{
return;
}
PyEval_RestoreThread(mainThreadState);
PyObject *argsObj = PyUnicode_FromWideChar(args, -1);
PyObject *resultObj = PyObject_CallMethod(measure->measureObject, "ExecuteBang", "O", argsObj);
if (resultObj != NULL)
{
Py_DECREF(resultObj);
}
else
{
PyErr_Clear();
}
Py_DECREF(argsObj);
PyEval_SaveThread();
}
PLUGIN_EXPORT void Finalize(void* data)
{
Measure *measure = (Measure*) data;
PyEval_RestoreThread(mainThreadState);
if (measure->measureObject != NULL)
{
PyObject *resultObj = PyObject_CallMethod(measure->measureObject, "Finalize", NULL);
if (resultObj != NULL)
{
Py_DECREF(resultObj);
}
else
{
PyErr_Clear();
}
if (measure->getStringResult != NULL)
{
PyMem_Free(measure->getStringResult);
}
}
delete measure;
//Py_Finalize(); // Testing this without killing the interpreter to reset its status
}