keep track of python function so we can decref it
This commit is contained in:
parent
c9588af3c3
commit
d808478ab1
1 changed files with 100 additions and 8 deletions
|
@ -16,6 +16,80 @@
|
||||||
* but if it does, estrdup will return NULL.)
|
* but if it does, estrdup will return NULL.)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
int callbackLen = 0;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
PyObject *func;
|
||||||
|
int callbackId;
|
||||||
|
} callbackInfo;
|
||||||
|
|
||||||
|
static callbackInfo callbackList[20];
|
||||||
|
|
||||||
|
void
|
||||||
|
addCallback(PyObject *func, int callbackId)
|
||||||
|
{
|
||||||
|
if (callbackLen < 20) {
|
||||||
|
callbackList[callbackLen].func = func;
|
||||||
|
callbackList[callbackLen].callbackId = callbackId;
|
||||||
|
callbackLen++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
removeCallback(int callbackId)
|
||||||
|
{
|
||||||
|
int index = -1;
|
||||||
|
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
for (i = 0; i < callbackLen; i++)
|
||||||
|
{
|
||||||
|
if (callbackList[i].callbackId == callbackId)
|
||||||
|
{
|
||||||
|
index = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!(index == -1)) {
|
||||||
|
for (j = index; j < callbackLen; j++)
|
||||||
|
{
|
||||||
|
callbackList[j] = callbackList[j+1];
|
||||||
|
}
|
||||||
|
callbackLen--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
hasCallback(int callbackId)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < callbackLen; i++)
|
||||||
|
{
|
||||||
|
if (callbackList[i].callbackId == callbackId)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
PyObject *
|
||||||
|
getCallback(int callbackId)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < callbackLen; i++)
|
||||||
|
{
|
||||||
|
if (callbackList[i].callbackId == callbackId)
|
||||||
|
{
|
||||||
|
return callbackList[i].func;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
char *
|
char *
|
||||||
estrdup(char *s)
|
estrdup(char *s)
|
||||||
{
|
{
|
||||||
|
@ -31,14 +105,17 @@ estrdup(char *s)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
telldus_tdInit(PyObject *self)
|
telldus_tdInit(PyObject *self)
|
||||||
{
|
{
|
||||||
return PyLong_FromLong(tdInit());
|
tdInit();
|
||||||
|
Py_INCREF(Py_None);
|
||||||
|
return Py_None;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
telldus_tdClose(PyObject *self)
|
telldus_tdClose(PyObject *self)
|
||||||
{
|
{
|
||||||
return PyLong_FromLong(tdClose();)
|
tdClose();
|
||||||
|
Py_INCREF(Py_None);
|
||||||
|
return Py_None;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
|
@ -382,6 +459,8 @@ telldus_tdRegisterDeviceEvent(PyObject *self, PyObject *args)
|
||||||
|
|
||||||
result = tdRegisterDeviceEvent((TDDeviceEvent) &telldus_deviceEventCallback, 0);
|
result = tdRegisterDeviceEvent((TDDeviceEvent) &telldus_deviceEventCallback, 0);
|
||||||
|
|
||||||
|
addCallback(func, result);
|
||||||
|
|
||||||
return PyLong_FromLong((long) result);
|
return PyLong_FromLong((long) result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -434,6 +513,8 @@ telldus_tdRegisterDeviceChangeEvent(PyObject *self, PyObject *args)
|
||||||
|
|
||||||
result = tdRegisterDeviceChangeEvent((TDDeviceChangeEvent) &telldus_deviceChangeEventCallback, 0);
|
result = tdRegisterDeviceChangeEvent((TDDeviceChangeEvent) &telldus_deviceChangeEventCallback, 0);
|
||||||
|
|
||||||
|
addCallback(func, result);
|
||||||
|
|
||||||
return PyLong_FromLong((long) result);
|
return PyLong_FromLong((long) result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -486,6 +567,8 @@ telldus_tdRegisterRawDeviceEvent(PyObject *self, PyObject *args)
|
||||||
|
|
||||||
result = tdRegisterRawDeviceEvent((TDRawDeviceEvent) &telldus_rawDeviceEventCallback, 0);
|
result = tdRegisterRawDeviceEvent((TDRawDeviceEvent) &telldus_rawDeviceEventCallback, 0);
|
||||||
|
|
||||||
|
addCallback(func, result);
|
||||||
|
|
||||||
return PyLong_FromLong((long) result);
|
return PyLong_FromLong((long) result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -538,6 +621,8 @@ telldus_tdRegisterSensorEvent(PyObject *self, PyObject *args)
|
||||||
|
|
||||||
result = tdRegisterSensorEvent((TDSensorEvent) &telldus_sensorEventCallback, 0);
|
result = tdRegisterSensorEvent((TDSensorEvent) &telldus_sensorEventCallback, 0);
|
||||||
|
|
||||||
|
addCallback(func, result);
|
||||||
|
|
||||||
return PyLong_FromLong((long) result);
|
return PyLong_FromLong((long) result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -545,10 +630,17 @@ static PyObject *
|
||||||
telldus_tdUnregisterCallback(PyObject *self, PyObject *args)
|
telldus_tdUnregisterCallback(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
long id;
|
long id;
|
||||||
|
PyObject *callback;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "l", &id))
|
if (!PyArg_ParseTuple(args, "l", &id))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
if (hasCallback(id) == 1) {
|
||||||
|
callback = getCallback(id);
|
||||||
|
Py_DECREF(callback);
|
||||||
|
removeCallback(id);
|
||||||
|
}
|
||||||
|
|
||||||
return PyLong_FromLong((long) tdUnregisterCallback(id));
|
return PyLong_FromLong((long) tdUnregisterCallback(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue