comment out debug message.. increase waiting time for thread start
[enigma2.git] / lib / base / ebase.cpp
index 1b92995b2e3cb5c1d90d872b74c17f33655de98a..addd1b88de2c080cc39f03ea58e3a8378816a550 100644 (file)
@@ -157,7 +157,7 @@ int eMainloop::processOneEvent(unsigned int user_timeout, PyObject **res, PyObje
                poll_timeout /= 1000;
        }
        
-       if ((user_timeout > 0) && (poll_timeout > user_timeout))
+       if ((user_timeout > 0) && (poll_timeout > 0) && ((unsigned int)poll_timeout > user_timeout))
        {
                poll_timeout = user_timeout;
                return_reason = 1;
@@ -165,7 +165,6 @@ int eMainloop::processOneEvent(unsigned int user_timeout, PyObject **res, PyObje
        
        int ret = 0;
        
-       
        if (poll_timeout)
        {
                std::multimap<int,eSocketNotifier*>::iterator it;
@@ -200,16 +199,16 @@ int eMainloop::processOneEvent(unsigned int user_timeout, PyObject **res, PyObje
                {
                        for (int i=0; i < PyList_Size(additional); ++i)
                        {
-                               PyObject *it = PyList_GetItem(additional, i);
+                               PyObject *it = PyList_GET_ITEM(additional, i);
                                if (!PyTuple_Check(it))
                                        eFatal("poll item is not a tuple");
                                if (PyTuple_Size(it) != 2)
                                        eFatal("poll tuple size is not 2");
-                               int fd = PyObject_AsFileDescriptor(PyTuple_GetItem(it, 0));
+                               int fd = PyObject_AsFileDescriptor(PyTuple_GET_ITEM(it, 0));
                                if (fd == -1)
                                        eFatal("poll tuple not a filedescriptor");
                                pfd[nativecount + i].fd = fd;
-                               pfd[nativecount + i].events = PyInt_AsLong(PyTuple_GetItem(it, 1));
+                               pfd[nativecount + i].events = PyInt_AsLong(PyTuple_GET_ITEM(it, 1));
                        }
                }
 
@@ -254,9 +253,10 @@ int eMainloop::processOneEvent(unsigned int user_timeout, PyObject **res, PyObje
                                        if (!*res)
                                                *res = PyList_New(0);
                                        PyObject *it = PyTuple_New(2);
-                                       PyTuple_SetItem(it, 0, PyInt_FromLong(pfd[i].fd));
-                                       PyTuple_SetItem(it, 1, PyInt_FromLong(pfd[i].revents));
+                                       PyTuple_SET_ITEM(it, 0, PyInt_FromLong(pfd[i].fd));
+                                       PyTuple_SET_ITEM(it, 1, PyInt_FromLong(pfd[i].revents));
                                        PyList_Append(*res, it);
+                                       Py_DECREF(it);
                                }
                        }
                        
@@ -311,10 +311,31 @@ int eMainloop::iterate(unsigned int user_timeout, PyObject **res, PyObject *dict
 {
        int ret = 0;
        
+       timeval user_timer;
+       gettimeofday(&user_timer, 0);
+       user_timer += user_timeout;
+
+               /* TODO: this code just became ugly. fix that. */
        do
-       { 
+       {
+               if (m_interrupt_requested)
+               {
+                       m_interrupt_requested = 0;
+                       return 0;
+               }
                if (app_quit_now) return -1;
-               ret = processOneEvent(user_timeout, res, dict);
+               timeval now, timeout;
+               gettimeofday(&now, 0);
+               timeout = user_timer - now;
+               
+               if (user_timeout && (user_timer <= now))
+                       return 0;
+               
+               int to = 0;
+               if (user_timeout)
+                       to = timeout.tv_sec * 1000 + timeout.tv_usec / 1000;
+               
+               ret = processOneEvent(to, res, dict);
                if (res && *res)
                        return ret;
        } while (ret == 0);
@@ -332,8 +353,15 @@ int eMainloop::runLoop()
 PyObject *eMainloop::poll(PyObject *timeout, PyObject *dict)
 {
        PyObject *res = 0;
-       int user_timeout = (timeout == Py_None) ? 0 : PyInt_AsLong(timeout);
        
+       if (app_quit_now)
+       {
+               Py_INCREF(Py_None);
+               return Py_None;
+       }
+       
+       int user_timeout = (timeout == Py_None) ? 0 : PyInt_AsLong(timeout);
+
        iterate(user_timeout, &res, dict);
        
        if (!res) /* return empty list on timeout */
@@ -342,6 +370,11 @@ PyObject *eMainloop::poll(PyObject *timeout, PyObject *dict)
        return res;
 }
 
+void eMainloop::interruptPoll()
+{
+       m_interrupt_requested = 1;
+}
+
 void eMainloop::quit(int ret)
 {
        retval = ret;