better solution to add possibility to delete eSocketNotifiers,
[vuplus_dvbapp] / lib / base / console.cpp
index 9204bcc..a12cb5e 100644 (file)
@@ -55,8 +55,10 @@ int bidirpipe(int pfd[], const char *cmd , const char * const argv[], const char
        return(pid);
 }
 
+DEFINE_REF(eConsoleAppContainer);
+
 eConsoleAppContainer::eConsoleAppContainer()
-:pid(-1), killstate(0), in(0), out(0), err(0)
+:pid(-1), killstate(0)
 {
        for (int i=0; i < 3; ++i)
        {
@@ -109,12 +111,15 @@ int eConsoleAppContainer::execute(const char *cmdline, const char * const argv[]
 
        ::fcntl(fd[1], F_SETFL, O_NONBLOCK);
        ::fcntl(fd[2], F_SETFL, O_NONBLOCK);
-       in = new eSocketNotifier(eApp, fd[0], eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Hungup );
-       out = new eSocketNotifier(eApp, fd[1], eSocketNotifier::Write, false);  
-       err = new eSocketNotifier(eApp, fd[2], eSocketNotifier::Read|eSocketNotifier::Priority );
+       in = eSocketNotifier::create(eApp, fd[0], eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Hungup );
+       out = eSocketNotifier::create(eApp, fd[1], eSocketNotifier::Write, false);  
+       err = eSocketNotifier::create(eApp, fd[2], eSocketNotifier::Read|eSocketNotifier::Priority );
        CONNECT(in->activated, eConsoleAppContainer::readyRead);
        CONNECT(out->activated, eConsoleAppContainer::readyWrite);
        CONNECT(err->activated, eConsoleAppContainer::readyErrRead);
+       in->m_clients.push_back(this);
+       out->m_clients.push_back(this);
+       err->m_clients.push_back(this);
 
        return 0;
 }
@@ -143,10 +148,9 @@ void eConsoleAppContainer::kill()
                outbuf.pop();
                delete [] d.data;
        }
-       delete in;
-       delete out;
-       delete err;
-       in=out=err=0;
+       in = 0;
+       out = 0;
+       err = 0;
 
        for (int i=0; i < 3; ++i)
        {
@@ -209,6 +213,7 @@ void eConsoleAppContainer::closePipes()
                outbuf.pop();
                delete [] d.data;
        }
+       in = 0; out = 0; err = 0;
        pid = -1;
 }
 
@@ -333,152 +338,6 @@ struct eConsolePy
        PyObject *in_weakreflist; /* List of weak references */
 };
 
-#define COMPATIBILITY_MODE
-// with COMPATIBILITY_MODE enabled the callback list is accessed via console.appClosed.get()
-// we remove this code after next enigma2 release... then the list should be accessed via console.appClosed ( without .get() )
-
-#ifdef COMPATIBILITY_MODE
-struct eListCompatibilityWrapper
-{
-       PyObject_HEAD
-       PyObject *list;
-       PyObject *in_weakreflist; /* List of weak references */
-};
-
-static int
-eListCompatibilityWrapper_traverse(eListCompatibilityWrapper *self, visitproc visit, void *arg)
-{
-       Py_VISIT(self->list);
-       return 0;
-}
-
-static int
-eListCompatibilityWrapper_clear(eListCompatibilityWrapper *self)
-{
-       Py_CLEAR(self->list);
-       return 0;
-}
-
-static void
-eListCompatibilityWrapper_dealloc(eListCompatibilityWrapper* self)
-{
-       if (self->in_weakreflist != NULL)
-               PyObject_ClearWeakRefs((PyObject *) self);
-       eListCompatibilityWrapper_clear(self);
-       Org_Py_DECREF(self->list);
-       self->ob_type->tp_free((PyObject*)self);
-}
-
-static PyObject *
-eListCompatibilityWrapper_get(eListCompatibilityWrapper *self, void *closure)
-{
-       Org_Py_INCREF(self->list);
-       return self->list;
-}
-
-static PyMethodDef eListCompatibilityWrapper_methods[] = {
-       {"get", (PyCFunction)eListCompatibilityWrapper_get, METH_NOARGS,
-        "returns the list"
-       },
-       {NULL}  /* Sentinel */
-};
-
-static PyGetSetDef eListCompatibilityWrapper_getseters[] = {
-       {NULL} /* Sentinel */
-};
-
-static PyTypeObject eListCompatibilityWrapperType = {
-       PyObject_HEAD_INIT(NULL)
-       0, /*ob_size*/
-       "eConsoleImpl.eListCompatibilityWrapper", /*tp_name*/
-       sizeof(eListCompatibilityWrapper), /*tp_basicsize*/
-       0, /*tp_itemsize*/
-       (destructor)eListCompatibilityWrapper_dealloc, /*tp_dealloc*/
-       0, /*tp_print*/
-       0, /*tp_getattr*/
-       0, /*tp_setattr*/
-       0, /*tp_compare*/
-       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 | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
-       "eListCompatibilityWrapper objects", /* tp_doc */
-       (traverseproc)eListCompatibilityWrapper_traverse, /* tp_traverse */
-       (inquiry)eListCompatibilityWrapper_clear, /* tp_clear */
-       0, /* tp_richcompare */
-       offsetof(eListCompatibilityWrapper, in_weakreflist), /* tp_weaklistoffset */
-       0, /* tp_iter */
-       0, /* tp_iternext */
-       eListCompatibilityWrapper_methods, /* tp_methods */
-       0, /* tp_members */
-       eListCompatibilityWrapper_getseters, /* 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 */
-};
-
-static PyObject *
-eConsolePy_dataAvail(eConsolePy *self, void *closure)
-{
-       eListCompatibilityWrapper *wrapper = (eListCompatibilityWrapper *)eListCompatibilityWrapperType.tp_alloc(&eListCompatibilityWrapperType, 0);
-       Org_Py_INCREF((PyObject*)wrapper);
-       wrapper->list = self->cont->dataAvail.get();
-       wrapper->in_weakreflist = NULL;
-       return (PyObject*)wrapper;
-}
-
-static PyObject *
-eConsolePy_stdoutAvail(eConsolePy *self, void *closure)
-{
-       eListCompatibilityWrapper *wrapper = (eListCompatibilityWrapper *)eListCompatibilityWrapperType.tp_alloc(&eListCompatibilityWrapperType, 0);
-       Org_Py_INCREF((PyObject*)wrapper);
-       wrapper->list = self->cont->stdoutAvail.get();
-       wrapper->in_weakreflist = NULL;
-       return (PyObject*)wrapper;
-}
-
-static PyObject *
-eConsolePy_stderrAvail(eConsolePy *self, void *closure)
-{
-       eListCompatibilityWrapper *wrapper = (eListCompatibilityWrapper *)eListCompatibilityWrapperType.tp_alloc(&eListCompatibilityWrapperType, 0);
-       Org_Py_INCREF((PyObject*)wrapper);
-       wrapper->list = self->cont->stderrAvail.get();
-       wrapper->in_weakreflist = NULL;
-       return (PyObject*)wrapper;
-}
-
-static PyObject *
-eConsolePy_dataSent(eConsolePy *self, void *closure)
-{
-       eListCompatibilityWrapper *wrapper = (eListCompatibilityWrapper *)eListCompatibilityWrapperType.tp_alloc(&eListCompatibilityWrapperType, 0);
-       Org_Py_INCREF((PyObject*)wrapper);
-       wrapper->list = self->cont->dataSent.get();
-       wrapper->in_weakreflist = NULL;
-       return (PyObject*)wrapper;
-}
-
-static PyObject *
-eConsolePy_appClosed(eConsolePy *self, void *closure)
-{
-       eListCompatibilityWrapper *wrapper = (eListCompatibilityWrapper *)eListCompatibilityWrapperType.tp_alloc(&eListCompatibilityWrapperType, 0);
-       Org_Py_INCREF((PyObject*)wrapper);
-       wrapper->list = self->cont->appClosed.get();
-       wrapper->in_weakreflist = NULL;
-       return (PyObject*)wrapper;
-}
-#else
 static PyObject *
 eConsolePy_dataAvail(eConsolePy *self, void *closure)
 {
@@ -508,7 +367,6 @@ eConsolePy_appClosed(eConsolePy *self, void *closure)
 {
        return self->cont->appClosed.get();
 }
-#endif
 
 static PyGetSetDef eConsolePy_getseters[] = {
        {"dataAvail",
@@ -537,23 +395,23 @@ static PyGetSetDef eConsolePy_getseters[] = {
 static int
 eConsolePy_traverse(eConsolePy *self, visitproc visit, void *arg)
 {
-       PyObject *obj = self->cont->dataAvail.get(true);
+       PyObject *obj = self->cont->dataAvail.getSteal();
        if (obj) {
                Py_VISIT(obj);
        }
-       obj = self->cont->stdoutAvail.get(true);
+       obj = self->cont->stdoutAvail.getSteal();
        if (obj) {
                Py_VISIT(obj);
        }
-       obj = self->cont->stderrAvail.get(true);
+       obj = self->cont->stderrAvail.getSteal();
        if (obj) {
                Py_VISIT(obj);
        }
-       obj = self->cont->dataSent.get(true);
+       obj = self->cont->dataSent.getSteal();
        if (obj) {
                Py_VISIT(obj);
        }
-       obj = self->cont->appClosed.get(true);
+       obj = self->cont->appClosed.getSteal();
        if (obj) {
                Py_VISIT(obj);
        }
@@ -563,23 +421,23 @@ eConsolePy_traverse(eConsolePy *self, visitproc visit, void *arg)
 static int
 eConsolePy_clear(eConsolePy *self)
 {
-       PyObject *obj = self->cont->dataAvail.get(true);
+       PyObject *obj = self->cont->dataAvail.getSteal(true);
        if (obj) {
                Py_CLEAR(obj);
        }
-       obj = self->cont->stdoutAvail.get(true);
+       obj = self->cont->stdoutAvail.getSteal(true);
        if (obj) {
                Py_CLEAR(obj);
        }
-       obj = self->cont->stderrAvail.get(true);
+       obj = self->cont->stderrAvail.getSteal(true);
        if (obj) {
                Py_CLEAR(obj);
        }
-       obj = self->cont->dataSent.get(true);
+       obj = self->cont->dataSent.getSteal(true);
        if (obj) {
                Py_CLEAR(obj);
        }
-       obj = self->cont->appClosed.get(true);
+       obj = self->cont->appClosed.getSteal(true);
        if (obj) {
                Py_CLEAR(obj);
        }
@@ -592,7 +450,7 @@ eConsolePy_dealloc(eConsolePy* self)
        if (self->in_weakreflist != NULL)
                PyObject_ClearWeakRefs((PyObject *) self);
        eConsolePy_clear(self);
-       delete self->cont;
+       self->cont->Release();
        self->ob_type->tp_free((PyObject*)self);
 }
 
@@ -601,6 +459,7 @@ eConsolePy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
        eConsolePy *self = (eConsolePy *)type->tp_alloc(type, 0);
        self->cont = new eConsoleAppContainer();
+       self->cont->AddRef();
        self->in_weakreflist = NULL;
        return (PyObject *)self;
 }
@@ -828,13 +687,6 @@ void eConsoleInit(void)
        if (m == NULL)
                return;
 
-#ifdef COMPATIBILITY_MODE
-       if (!PyType_Ready(&eListCompatibilityWrapperType))
-       {
-               Org_Py_INCREF((PyObject*)&eListCompatibilityWrapperType);
-               PyModule_AddObject(m, "eListCompatibilityWrapper", (PyObject*)&eListCompatibilityWrapperType);
-       }
-#endif
        if (!PyType_Ready(&eConsolePyType))
        {
                Org_Py_INCREF((PyObject*)&eConsolePyType);