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 notifiers.insert(std::pair<int,eSocketNotifier*> (sn->getFD(), sn));
122 void eMainloop::removeSocketNotifier(eSocketNotifier *sn)
124 for (std::multimap<int,eSocketNotifier*>::iterator i = notifiers.find(sn->getFD());
125 i != notifiers.end();
128 return notifiers.erase(i);
129 eFatal("removed socket notifier which is not present");
132 int eMainloop::processOneEvent(unsigned int user_timeout, PyObject **res, PyObject *additional)
134 int return_reason = 0;
135 /* get current time */
137 gettimeofday(&now, 0);
138 m_now_is_invalid = 0;
140 if (additional && !PyDict_Check(additional))
141 eFatal("additional, but it's not dict");
143 if (additional && !res)
144 eFatal("additional, but no res");
146 int poll_timeout = -1; /* infinite in case of empty timer list */
150 singleLock s(recalcLock);
151 poll_timeout = timeval_to_usec(m_timer_list.begin()->getNextActivation() - now);
152 /* if current timer already passed, don't delay infinite. */
153 if (poll_timeout < 0)
156 /* convert us to ms */
157 poll_timeout /= 1000;
160 if ((user_timeout > 0) && (poll_timeout > 0) && ((unsigned int)poll_timeout > user_timeout))
162 poll_timeout = user_timeout;
170 std::multimap<int,eSocketNotifier*>::iterator it;
171 std::map<int,int> fd_merged;
172 std::map<int,int>::const_iterator fd_merged_it;
174 for (it = notifiers.begin(); it != notifiers.end(); ++it)
175 fd_merged[it->first] |= it->second->getRequested();
177 fd_merged_it = fd_merged.begin();
179 int nativecount, fdcount;
181 nativecount = fdcount = fd_merged.size();
185 additional = PyDict_Items(additional);
186 fdcount += PyList_Size(additional);
189 // build the poll aray
190 pollfd* pfd = new pollfd[fdcount]; // make new pollfd array
192 for (int i=0; i < nativecount; i++, fd_merged_it++)
194 pfd[i].fd = fd_merged_it->first;
195 pfd[i].events = fd_merged_it->second;
200 for (int i=0; i < PyList_Size(additional); ++i)
202 PyObject *it = PyList_GET_ITEM(additional, i);
203 if (!PyTuple_Check(it))
204 eFatal("poll item is not a tuple");
205 if (PyTuple_Size(it) != 2)
206 eFatal("poll tuple size is not 2");
207 int fd = PyObject_AsFileDescriptor(PyTuple_GET_ITEM(it, 0));
209 eFatal("poll tuple not a filedescriptor");
210 pfd[nativecount + i].fd = fd;
211 pfd[nativecount + i].events = PyInt_AsLong(PyTuple_GET_ITEM(it, 1));
215 ret = ::poll(pfd, fdcount, poll_timeout);
217 /* ret > 0 means that there are some active poll entries. */
221 for (int i=0; i < nativecount ; i++)
223 it = notifiers.begin();
227 std::multimap<int,eSocketNotifier*>::iterator
228 l = notifiers.lower_bound(pfd[i].fd),
229 u = notifiers.upper_bound(pfd[i].fd);
231 ePtrList<eSocketNotifier> n;
234 n.push_back(l->second);
236 for (ePtrList<eSocketNotifier>::iterator li(n.begin()); li != n.end(); ++li)
238 int req = li->getRequested();
242 if (pfd[i].revents & req)
243 (*li)->activate(pfd[i].revents);
245 if ((pfd[i].revents&~handled) & (POLLERR|POLLHUP|POLLNVAL))
246 eDebug("poll: unhandled POLLERR/HUP/NVAL for fd %d(%d)", pfd[i].fd, pfd[i].revents);
249 for (int i = nativecount; i < fdcount; ++i)
254 *res = PyList_New(0);
255 PyObject *it = PyTuple_New(2);
256 PyTuple_SET_ITEM(it, 0, PyInt_FromLong(pfd[i].fd));
257 PyTuple_SET_ITEM(it, 1, PyInt_FromLong(pfd[i].revents));
258 PyList_Append(*res, it);
263 ret = 1; /* poll did not timeout. */
266 /* when we got a signal, we get EINTR. */
268 eDebug("poll made error (%m)");
272 ret = -1; /* don't assume the timeout has passed when we got a signal */
276 Py_XDECREF(additional);
279 /* when we not processed anything, check timers. */
282 /* we know that this time has passed. */
285 singleLock s(recalcLock);
287 /* this will never change while we have the recalcLock */
288 /* we can savely return here, the timer will be re-checked soon. */
289 if (m_now_is_invalid)
292 /* process all timers which are ready. first remove them out of the list. */
293 while ((!m_timer_list.empty()) && (m_timer_list.begin()->getNextActivation() <= now))
294 m_timer_list.begin()->activate();
297 return return_reason;
300 void eMainloop::addTimer(eTimer* e)
302 m_timer_list.insert_in_order(e);
305 void eMainloop::removeTimer(eTimer* e)
307 m_timer_list.remove(e);
310 int eMainloop::iterate(unsigned int user_timeout, PyObject **res, PyObject *dict)
315 gettimeofday(&user_timer, 0);
316 user_timer += user_timeout;
318 /* TODO: this code just became ugly. fix that. */
321 if (m_interrupt_requested)
323 m_interrupt_requested = 0;
326 if (app_quit_now) return -1;
327 timeval now, timeout;
328 gettimeofday(&now, 0);
329 timeout = user_timer - now;
331 if (user_timeout && (user_timer <= now))
336 to = timeout.tv_sec * 1000 + timeout.tv_usec / 1000;
338 ret = processOneEvent(to, res, dict);
346 int eMainloop::runLoop()
348 while (!app_quit_now)
353 PyObject *eMainloop::poll(PyObject *timeout, PyObject *dict)
363 int user_timeout = (timeout == Py_None) ? 0 : PyInt_AsLong(timeout);
365 iterate(user_timeout, &res, dict);
367 if (!res) /* return empty list on timeout */
373 void eMainloop::interruptPoll()
375 m_interrupt_requested = 1;
378 void eMainloop::quit(int ret)
384 void eMainloop::addTimeOffset(int offset)
386 for (ePtrList<eMainloop>::iterator it(eMainloop::existing_loops)
387 ;it != eMainloop::existing_loops.end(); ++it)
389 singleLock s(it->recalcLock);
390 it->m_now_is_invalid = 1;
391 for (ePtrList<eTimer>::iterator tit = it->m_timer_list.begin(); tit != it->m_timer_list.end(); ++tit )
392 tit->addTimeOffset(offset);
396 eApplication* eApp = 0;