1 #include <lib/base/ebase.h>
7 #include <lib/base/eerror.h>
8 #include <lib/base/elock.h>
10 eSocketNotifier::eSocketNotifier(eMainloop *context, int fd, int requested, bool startnow): context(*context), fd(fd), state(0), requested(requested)
16 eSocketNotifier::~eSocketNotifier()
21 void eSocketNotifier::start()
26 context.addSocketNotifier(this);
27 state=2; // running but not in poll yet
30 void eSocketNotifier::stop()
33 context.removeSocketNotifier(this);
39 void eTimer::start(long msek, bool singleShot)
45 bSingleShot = singleShot;
47 gettimeofday(&nextActivation, 0);
48 // eDebug("this = %p\nnow sec = %d, usec = %d\nadd %d msec", this, nextActivation.tv_sec, nextActivation.tv_usec, msek);
49 nextActivation += (msek<0 ? 0 : msek);
50 // eDebug("next Activation sec = %d, usec = %d", nextActivation.tv_sec, nextActivation.tv_usec );
51 context.addTimer(this);
54 void eTimer::startLongTimer( int seconds )
59 bActive = bSingleShot = true;
61 gettimeofday(&nextActivation, 0);
62 // eDebug("this = %p\nnow sec = %d, usec = %d\nadd %d sec", this, nextActivation.tv_sec, nextActivation.tv_usec, seconds);
64 nextActivation.tv_sec += seconds;
65 // eDebug("next Activation sec = %d, usec = %d", nextActivation.tv_sec, nextActivation.tv_usec );
66 context.addTimer(this);
74 context.removeTimer(this);
78 void eTimer::changeInterval(long msek)
80 if (bActive) // Timer is running?
82 context.removeTimer(this); // then stop
83 nextActivation -= interval; // sub old interval
86 bActive=true; // then activate Timer
88 interval = msek; // set new Interval
89 nextActivation += interval; // calc nextActivation
91 context.addTimer(this); // add Timer to context TimerList
94 void eTimer::activate() // Internal Funktion... called from eApplication
96 context.removeTimer(this);
100 nextActivation += interval;
101 context.addTimer(this);
109 void eTimer::addTimeOffset( int offset )
111 nextActivation.tv_sec += offset;
115 ePtrList<eMainloop> eMainloop::existing_loops;
117 void eMainloop::addSocketNotifier(eSocketNotifier *sn)
119 int fd = sn->getFD();
120 ASSERT(notifiers.find(fd) == notifiers.end());
124 void eMainloop::removeSocketNotifier(eSocketNotifier *sn)
126 int fd = sn->getFD();
127 std::map<int,eSocketNotifier*>::iterator i(notifiers.find(fd));
128 if (i != notifiers.end())
129 return notifiers.erase(i);
130 eFatal("removed socket notifier which is not present");
133 int eMainloop::processOneEvent(unsigned int user_timeout, PyObject **res, ePyObject additional)
135 int return_reason = 0;
136 /* get current time */
138 gettimeofday(&now, 0);
139 m_now_is_invalid = 0;
141 if (additional && !PyDict_Check(additional))
142 eFatal("additional, but it's not dict");
144 if (additional && !res)
145 eFatal("additional, but no res");
147 int poll_timeout = -1; /* infinite in case of empty timer list */
151 singleLock s(recalcLock);
152 poll_timeout = timeval_to_usec(m_timer_list.begin()->getNextActivation() - now);
153 /* if current timer already passed, don't delay infinite. */
154 if (poll_timeout < 0)
157 /* convert us to ms */
158 poll_timeout /= 1000;
161 if ((user_timeout > 0) && (poll_timeout > 0) && ((unsigned int)poll_timeout > user_timeout))
163 poll_timeout = user_timeout;
167 int nativecount=notifiers.size(),
172 fdcount += PyDict_Size(additional);
174 // build the poll aray
175 pollfd pfd[fdcount]; // make new pollfd array
176 std::map<int,eSocketNotifier*>::iterator it = notifiers.begin();
178 for (; i < nativecount; ++i, ++it)
180 it->second->state = 1; // running and in poll
181 pfd[i].fd = it->first;
182 pfd[i].events = it->second->getRequested();
189 while (PyDict_Next(additional, &pos, &key, &val)) {
190 pfd[i].fd = PyObject_AsFileDescriptor(key);
191 pfd[i++].events = PyInt_AsLong(val);
196 Py_BEGIN_ALLOW_THREADS
197 ret = ::poll(pfd, fdcount, poll_timeout);
200 ret = ::poll(pfd, fdcount, poll_timeout);
202 /* ret > 0 means that there are some active poll entries. */
207 for (; i < nativecount; ++i)
211 it = notifiers.find(pfd[i].fd);
212 if (it != notifiers.end()
213 && it->second->state == 1) // added and in poll
215 int req = it->second->getRequested();
216 if (pfd[i].revents & req)
217 it->second->activate(pfd[i].revents & req);
218 pfd[i].revents &= ~req;
220 if (pfd[i].revents & (POLLERR|POLLHUP|POLLNVAL))
221 eDebug("poll: unhandled POLLERR/HUP/NVAL for fd %d(%d)", pfd[i].fd, pfd[i].revents);
224 for (; i < fdcount; ++i)
229 *res = PyList_New(0);
230 ePyObject it = PyTuple_New(2);
231 PyTuple_SET_ITEM(it, 0, PyInt_FromLong(pfd[i].fd));
232 PyTuple_SET_ITEM(it, 1, PyInt_FromLong(pfd[i].revents));
233 PyList_Append(*res, it);
238 ret = 1; /* poll did not timeout. */
241 /* when we got a signal, we get EINTR. */
243 eDebug("poll made error (%m)");
247 ret = -1; /* don't assume the timeout has passed when we got a signal */
251 /* when we not processed anything, check timers. */
252 if (!m_timer_list.empty())
254 /* we know that this time has passed. */
255 singleLock s(recalcLock);
257 if (ret || m_now_is_invalid)
258 gettimeofday(&now, 0);
262 /* process all timers which are ready. first remove them out of the list. */
263 while ((!m_timer_list.empty()) && (m_timer_list.begin()->getNextActivation() <= now))
264 m_timer_list.begin()->activate();
267 return return_reason;
270 void eMainloop::addTimer(eTimer* e)
272 m_timer_list.insert_in_order(e);
275 void eMainloop::removeTimer(eTimer* e)
277 m_timer_list.remove(e);
280 int eMainloop::iterate(unsigned int user_timeout, PyObject **res, ePyObject dict)
285 gettimeofday(&user_timer, 0);
286 user_timer += user_timeout;
288 /* TODO: this code just became ugly. fix that. */
291 if (m_interrupt_requested)
293 m_interrupt_requested = 0;
296 if (app_quit_now) return -1;
297 timeval now, timeout;
298 gettimeofday(&now, 0);
299 timeout = user_timer - now;
301 if (user_timeout && (user_timer <= now))
306 to = timeout.tv_sec * 1000 + timeout.tv_usec / 1000;
308 ret = processOneEvent(to, res, dict);
316 int eMainloop::runLoop()
318 while (!app_quit_now)
323 void eMainloop::reset()
328 PyObject *eMainloop::poll(ePyObject timeout, ePyObject dict)
335 int user_timeout = (timeout == Py_None) ? 0 : PyInt_AsLong(timeout);
337 iterate(user_timeout, &res, dict);
340 if (!ret) /* return empty list on timeout */
341 return PyList_New(0);
346 void eMainloop::interruptPoll()
348 m_interrupt_requested = 1;
351 void eMainloop::quit(int ret)
357 void eMainloop::addTimeOffset(int offset)
359 for (ePtrList<eMainloop>::iterator it(eMainloop::existing_loops)
360 ;it != eMainloop::existing_loops.end(); ++it)
362 singleLock s(it->recalcLock);
363 it->m_now_is_invalid = 1;
364 for (ePtrList<eTimer>::iterator tit = it->m_timer_list.begin(); tit != it->m_timer_list.end(); ++tit )
365 tit->addTimeOffset(offset);
369 eApplication* eApp = 0;