X-Git-Url: https://git.cweiske.de/enigma2.git/blobdiff_plain/5b82f868abfc84512c11b7c2cfa6e89af30ab203..9b02ceae89a3b478549f6dbd89ba7aa174cb7b12:/lib/base/ebase.cpp diff --git a/lib/base/ebase.cpp b/lib/base/ebase.cpp index 04b15ee8..addd1b88 100644 --- a/lib/base/ebase.cpp +++ b/lib/base/ebase.cpp @@ -129,7 +129,7 @@ void eMainloop::removeSocketNotifier(eSocketNotifier *sn) eFatal("removed socket notifier which is not present"); } -int eMainloop::processOneEvent(unsigned int user_timeout) +int eMainloop::processOneEvent(unsigned int user_timeout, PyObject **res, PyObject *additional) { int return_reason = 0; /* get current time */ @@ -137,6 +137,12 @@ int eMainloop::processOneEvent(unsigned int user_timeout) gettimeofday(&now, 0); m_now_is_invalid = 0; + if (additional && !PyDict_Check(additional)) + eFatal("additional, but it's not dict"); + + if (additional && !res) + eFatal("additional, but no res"); + int poll_timeout = -1; /* infinite in case of empty timer list */ if (m_timer_list) @@ -151,7 +157,7 @@ int eMainloop::processOneEvent(unsigned int user_timeout) 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; @@ -161,7 +167,6 @@ int eMainloop::processOneEvent(unsigned int user_timeout) if (poll_timeout) { - std::multimap::iterator it; std::map fd_merged; std::map::const_iterator fd_merged_it; @@ -171,24 +176,49 @@ int eMainloop::processOneEvent(unsigned int user_timeout) fd_merged_it = fd_merged.begin(); - int fdcount = fd_merged.size(); + int nativecount, fdcount; + + nativecount = fdcount = fd_merged.size(); + + if (additional) + { + additional = PyDict_Items(additional); + fdcount += PyList_Size(additional); + } // build the poll aray pollfd* pfd = new pollfd[fdcount]; // make new pollfd array - for (int i=0; i < fdcount; i++, fd_merged_it++) + for (int i=0; i < nativecount; i++, fd_merged_it++) { pfd[i].fd = fd_merged_it->first; pfd[i].events = fd_merged_it->second; } + + if (additional) + { + for (int i=0; i < PyList_Size(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_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_GET_ITEM(it, 1)); + } + } - ret = poll(pfd, fdcount, poll_timeout); + ret = ::poll(pfd, fdcount, poll_timeout); /* ret > 0 means that there are some active poll entries. */ if (ret > 0) { return_reason = 0; - for (int i=0; i < fdcount ; i++) + for (int i=0; i < nativecount ; i++) { it = notifiers.begin(); @@ -216,6 +246,20 @@ int eMainloop::processOneEvent(unsigned int user_timeout) eDebug("poll: unhandled POLLERR/HUP/NVAL for fd %d(%d)", pfd[i].fd, pfd[i].revents); } + for (int i = nativecount; i < fdcount; ++i) + { + if (pfd[i].revents) + { + if (!*res) + *res = PyList_New(0); + PyObject *it = PyTuple_New(2); + 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); + } + } + ret = 1; /* poll did not timeout. */ } else if (ret < 0) { @@ -229,6 +273,7 @@ int eMainloop::processOneEvent(unsigned int user_timeout) } } delete [] pfd; + Py_XDECREF(additional); } /* when we not processed anything, check timers. */ @@ -262,14 +307,37 @@ void eMainloop::removeTimer(eTimer* e) m_timer_list.remove(e); } -int eMainloop::iterate(unsigned int user_timeout) +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 (app_quit_now) break; - ret = processOneEvent(user_timeout); + { + if (m_interrupt_requested) + { + m_interrupt_requested = 0; + return 0; + } + if (app_quit_now) return -1; + 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); return ret; @@ -282,6 +350,31 @@ int eMainloop::runLoop() return retval; } +PyObject *eMainloop::poll(PyObject *timeout, PyObject *dict) +{ + PyObject *res = 0; + + 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 */ + res = PyList_New(0); + + return res; +} + +void eMainloop::interruptPoll() +{ + m_interrupt_requested = 1; +} + void eMainloop::quit(int ret) { retval = ret;