-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathPyRainmeter.cpp
202 lines (185 loc) · 6.59 KB
/
PyRainmeter.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
/*
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 "structmember.h"
#include "rainmeter-plugin-sdk/API/RainmeterAPI.h"
typedef struct RainmeterObject {
PyObject_HEAD
void *rm;
int logError;
int logWarning;
int logNotice;
int logDebug;
RainmeterObject()
{
logError = LOG_ERROR;
logWarning = LOG_WARNING;
logNotice = LOG_NOTICE;
logDebug = LOG_DEBUG;
}
} RainmeterObject;
static PyObject *Rainmeter_RmReadString(RainmeterObject *self, PyObject *args)
{
PyObject *option, *defValue;
int replaceMeasures;
PyArg_ParseTuple(args, "UUp", &option, &defValue, &replaceMeasures);
wchar_t *optionStr = PyUnicode_AsWideCharString(option, NULL);
wchar_t *defValueStr = PyUnicode_AsWideCharString(defValue, NULL);
LPCWSTR result = RmReadString(self->rm, optionStr, defValueStr, replaceMeasures);
PyMem_Free(defValueStr);
PyMem_Free(optionStr);
if (result == NULL)
{
Py_INCREF(Py_None);
return Py_None;
}
return PyUnicode_FromWideChar(result, -1);
}
static PyObject *Rainmeter_RmReadPath(RainmeterObject *self, PyObject *args)
{
PyObject *option, *defValue;
PyArg_ParseTuple(args, "UU", &option, &defValue);
wchar_t *optionStr = PyUnicode_AsWideCharString(option, NULL);
wchar_t *defValueStr = PyUnicode_AsWideCharString(defValue, NULL);
LPCWSTR result = RmReadPath(self->rm, optionStr, defValueStr);
PyMem_Free(defValueStr);
PyMem_Free(optionStr);
if (result == NULL)
{
Py_INCREF(Py_None);
return Py_None;
}
return PyUnicode_FromWideChar(result, -1);
}
static PyObject *Rainmeter_RmReadDouble(RainmeterObject *self, PyObject *args)
{
PyObject *option;
double defValue;
PyArg_ParseTuple(args, "Ud", &option, &defValue);
wchar_t *optionStr = PyUnicode_AsWideCharString(option, NULL);
double result = RmReadDouble(self->rm, optionStr, defValue);
PyMem_Free(optionStr);
return PyFloat_FromDouble(result);
}
static PyObject *Rainmeter_RmReadInt(RainmeterObject *self, PyObject *args)
{
PyObject *option;
int defValue;
PyArg_ParseTuple(args, "Ui", &option, &defValue);
wchar_t *optionStr = PyUnicode_AsWideCharString(option, NULL);
int result = RmReadInt(self->rm, optionStr, defValue);
PyMem_Free(optionStr);
return PyLong_FromLong(result);
}
static PyObject *Rainmeter_RmGetMeasureName(RainmeterObject *self)
{
LPCWSTR result = RmGetMeasureName(self->rm);
if (result == NULL)
{
Py_INCREF(Py_None);
return Py_None;
}
return PyUnicode_FromWideChar(result, -1);
}
static PyObject *Rainmeter_RmExecute(RainmeterObject *self, PyObject *args)
{
PyObject *command;
PyArg_ParseTuple(args, "U", &command);
wchar_t *commandStr = PyUnicode_AsWideCharString(command, NULL);
RmExecute(RmGetSkin(self->rm), commandStr);
PyMem_Free(commandStr);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *Rainmeter_RmLog(RainmeterObject *self, PyObject *args)
{
int level;
PyObject *message;
PyArg_ParseTuple(args, "iU", &level, &message);
wchar_t *messageStr = PyUnicode_AsWideCharString(message, NULL);
RmLog(level, messageStr);
PyMem_Free(messageStr);
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef Rainmeter_methods[] = {
{"RmReadString", (PyCFunction)Rainmeter_RmReadString, METH_VARARGS, ""},
{"RmReadPath", (PyCFunction)Rainmeter_RmReadPath, METH_VARARGS, ""},
{"RmReadDouble", (PyCFunction)Rainmeter_RmReadDouble, METH_VARARGS, ""},
{"RmReadInt", (PyCFunction)Rainmeter_RmReadInt, METH_VARARGS, ""},
{"RmGetMeasureName", (PyCFunction)Rainmeter_RmGetMeasureName, METH_NOARGS, ""},
{"RmExecute", (PyCFunction)Rainmeter_RmExecute, METH_VARARGS, ""},
{"RmLog", (PyCFunction)Rainmeter_RmLog, METH_VARARGS, ""},
{NULL}
};
static PyMemberDef Rainmeter_members[] = {
{"LOG_ERROR", T_INT, offsetof(RainmeterObject, logError), READONLY, ""},
{"LOG_WARNING", T_INT, offsetof(RainmeterObject, logWarning), READONLY, ""},
{"LOG_NOTICE", T_INT, offsetof(RainmeterObject, logNotice), READONLY, ""},
{"LOG_DEBUG", T_INT, offsetof(RainmeterObject, logDebug), READONLY, ""},
{NULL}
};
static PyTypeObject rainmeterType = {
PyVarObject_HEAD_INIT(NULL, 0)
"Rainmeter", /* tp_name */
sizeof(RainmeterObject), /* tp_basicsize */
0, /* tp_itemsize */
0, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
"Rainmeter objects", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
Rainmeter_methods, /* tp_methods */
Rainmeter_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
PyObject* CreateRainmeterObject(void *rm)
{
if (!(rainmeterType.tp_flags & Py_TPFLAGS_READY))
{
rainmeterType.tp_new = PyType_GenericNew;
PyType_Ready(&rainmeterType);
}
PyObject *obj = PyObject_CallObject((PyObject*) &rainmeterType, NULL);
((RainmeterObject*)obj)->rm = rm;
return (PyObject*) obj;
}