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);
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());
121 ASSERT(new_notifiers.find(fd) == new_notifiers.end());
122 new_notifiers[fd]=sn;
125 void eMainloop::removeSocketNotifier(eSocketNotifier *sn)
127 int fd = sn->getFD();
128 std::map<int,eSocketNotifier*>::iterator i(notifiers.find(fd));
129 if (i != notifiers.end())
130 return notifiers.erase(i);
131 i = new_notifiers.find(fd);
132 if (i != new_notifiers.end())
133 return new_notifiers.erase(i);
134 eFatal("removed socket notifier which is not present");
137 int eMainloop::processOneEvent(unsigned int user_timeout, PyObject **res, ePyObject additional)
139 int return_reason = 0;
140 /* get current time */
142 gettimeofday(&now, 0);
143 m_now_is_invalid = 0;
145 if (additional && !PyDict_Check(additional))
146 eFatal("additional, but it's not dict");
148 if (additional && !res)
149 eFatal("additional, but no res");
151 int poll_timeout = -1; /* infinite in case of empty timer list */
155 singleLock s(recalcLock);
156 poll_timeout = timeval_to_usec(m_timer_list.begin()->getNextActivation() - now);
157 /* if current timer already passed, don't delay infinite. */
158 if (poll_timeout < 0)
161 /* convert us to ms */
162 poll_timeout /= 1000;
165 if ((user_timeout > 0) && (poll_timeout > 0) && ((unsigned int)poll_timeout > user_timeout))
167 poll_timeout = user_timeout;
171 for (std::map<int, eSocketNotifier*>::iterator it(new_notifiers.begin()); it != new_notifiers.end();)
173 notifiers[it->first]=it->second;
174 new_notifiers.erase(it++);
177 int nativecount=notifiers.size(),
182 fdcount += PyDict_Size(additional);
184 // build the poll aray
185 pollfd pfd[fdcount]; // make new pollfd array
186 std::map<int,eSocketNotifier*>::iterator it = notifiers.begin();
188 for (; i < nativecount; ++i, ++it)
190 pfd[i].fd = it->first;
191 pfd[i].events = it->second->getRequested();
198 while (PyDict_Next(additional, &pos, &key, &val)) {
199 pfd[i].fd = PyObject_AsFileDescriptor(key);
200 pfd[i++].events = PyInt_AsLong(val);
204 ret = ::poll(pfd, fdcount, poll_timeout);
205 /* ret > 0 means that there are some active poll entries. */
210 for (; i < nativecount ; ++i)
215 it = notifiers.find(pfd[i].fd);
216 if (it != notifiers.end())
218 int req = it->second->getRequested();
220 if (pfd[i].revents & req)
221 it->second->activate(pfd[i].revents);
223 pfd[i].revents &= ~handled;
224 if (pfd[i].revents & (POLLERR|POLLHUP|POLLNVAL))
225 eDebug("poll: unhandled POLLERR/HUP/NVAL for fd %d(%d)", pfd[i].fd, pfd[i].revents);
228 for (; i < fdcount; ++i)
233 *res = PyList_New(0);
234 ePyObject it = PyTuple_New(2);
235 PyTuple_SET_ITEM(it, 0, PyInt_FromLong(pfd[i].fd));
236 PyTuple_SET_ITEM(it, 1, PyInt_FromLong(pfd[i].revents));
237 PyList_Append(*res, it);
242 ret = 1; /* poll did not timeout. */
245 /* when we got a signal, we get EINTR. */
247 eDebug("poll made error (%m)");
251 ret = -1; /* don't assume the timeout has passed when we got a signal */
255 /* when we not processed anything, check timers. */
256 if (!m_timer_list.empty())
258 /* we know that this time has passed. */
259 singleLock s(recalcLock);
261 if (ret || m_now_is_invalid)
262 gettimeofday(&now, 0);
266 /* process all timers which are ready. first remove them out of the list. */
267 while ((!m_timer_list.empty()) && (m_timer_list.begin()->getNextActivation() <= now))
268 m_timer_list.begin()->activate();
271 return return_reason;
274 void eMainloop::addTimer(eTimer* e)
276 m_timer_list.insert_in_order(e);
279 void eMainloop::removeTimer(eTimer* e)
281 m_timer_list.remove(e);
284 int eMainloop::iterate(unsigned int user_timeout, PyObject **res, ePyObject dict)
289 gettimeofday(&user_timer, 0);
290 user_timer += user_timeout;
292 /* TODO: this code just became ugly. fix that. */
295 if (m_interrupt_requested)
297 m_interrupt_requested = 0;
300 if (app_quit_now) return -1;
301 timeval now, timeout;
302 gettimeofday(&now, 0);
303 timeout = user_timer - now;
305 if (user_timeout && (user_timer <= now))
310 to = timeout.tv_sec * 1000 + timeout.tv_usec / 1000;
312 ret = processOneEvent(to, res, dict);
320 int eMainloop::runLoop()
322 while (!app_quit_now)
327 void eMainloop::reset()
332 PyObject *eMainloop::poll(ePyObject timeout, ePyObject dict)
342 int user_timeout = (timeout == Py_None) ? 0 : PyInt_AsLong(timeout);
344 iterate(user_timeout, &res, dict);
347 if (!ret) /* return empty list on timeout */
348 return PyList_New(0);
353 void eMainloop::interruptPoll()
355 m_interrupt_requested = 1;
358 void eMainloop::quit(int ret)
364 void eMainloop::addTimeOffset(int offset)
366 for (ePtrList<eMainloop>::iterator it(eMainloop::existing_loops)
367 ;it != eMainloop::existing_loops.end(); ++it)
369 singleLock s(it->recalcLock);
370 it->m_now_is_invalid = 1;
371 for (ePtrList<eTimer>::iterator tit = it->m_timer_list.begin(); tit != it->m_timer_list.end(); ++tit )
372 tit->addTimeOffset(offset);
376 eApplication* eApp = 0;