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 clock_gettime(CLOCK_MONOTONIC, &nextActivation);
48 // eDebug("this = %p\nnow sec = %d, nsec = %d\nadd %d msec", this, nextActivation.tv_sec, nextActivation.tv_nsec, msek);
49 nextActivation += (msek<0 ? 0 : msek);
50 // eDebug("next Activation sec = %d, nsec = %d", nextActivation.tv_sec, nextActivation.tv_nsec );
51 context.addTimer(this);
54 void eTimer::startLongTimer( int seconds )
59 bActive = bSingleShot = true;
61 clock_gettime(CLOCK_MONOTONIC, &nextActivation);
62 // eDebug("this = %p\nnow sec = %d, nsec = %d\nadd %d sec", this, nextActivation.tv_sec, nextActivation.tv_nsec, seconds);
64 nextActivation.tv_sec += seconds;
65 // eDebug("next Activation sec = %d, nsec = %d", nextActivation.tv_sec, nextActivation.tv_nsec );
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);
110 ePtrList<eMainloop> eMainloop::existing_loops;
112 eMainloop::~eMainloop()
114 existing_loops.remove(this);
115 for (std::map<int, eSocketNotifier*>::iterator it(notifiers.begin());it != notifiers.end();++it)
117 while(m_timer_list.begin() != m_timer_list.end())
118 m_timer_list.begin()->stop();
121 void eMainloop::addSocketNotifier(eSocketNotifier *sn)
123 int fd = sn->getFD();
124 ASSERT(notifiers.find(fd) == notifiers.end());
128 void eMainloop::removeSocketNotifier(eSocketNotifier *sn)
130 int fd = sn->getFD();
131 std::map<int,eSocketNotifier*>::iterator i(notifiers.find(fd));
132 if (i != notifiers.end())
133 return notifiers.erase(i);
134 eFatal("removed socket notifier which is not present");
137 int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePyObject additional)
139 int return_reason = 0;
140 /* get current time */
142 if (additional && !PyDict_Check(additional))
143 eFatal("additional, but it's not dict");
145 if (additional && !res)
146 eFatal("additional, but no res");
148 long poll_timeout = -1; /* infinite in case of empty timer list */
150 if (!m_timer_list.empty() || twisted_timeout > 0)
152 if (!m_timer_list.empty())
154 /* process all timers which are ready. first remove them out of the list. */
155 while (!m_timer_list.empty() && (poll_timeout = timeout_usec( m_timer_list.begin()->getNextActivation() ) ) <= 0 )
156 m_timer_list.begin()->activate();
157 if (poll_timeout < 0)
159 else /* convert us to ms */
160 poll_timeout /= 1000;
164 if ((twisted_timeout > 0) && (poll_timeout > 0) && ((unsigned int)poll_timeout > twisted_timeout))
166 poll_timeout = twisted_timeout;
170 int nativecount=notifiers.size(),
175 fdcount += PyDict_Size(additional);
177 // build the poll aray
178 pollfd pfd[fdcount]; // make new pollfd array
179 std::map<int,eSocketNotifier*>::iterator it = notifiers.begin();
181 for (; i < nativecount; ++i, ++it)
183 it->second->state = 1; // running and in poll
184 pfd[i].fd = it->first;
185 pfd[i].events = it->second->getRequested();
192 while (PyDict_Next(additional, &pos, &key, &val)) {
193 pfd[i].fd = PyObject_AsFileDescriptor(key);
194 pfd[i++].events = PyInt_AsLong(val);
202 Py_BEGIN_ALLOW_THREADS
203 ret = ::poll(pfd, fdcount, poll_timeout);
206 ret = ::poll(pfd, fdcount, poll_timeout);
210 /* ret > 0 means that there are some active poll entries. */
215 for (; i < nativecount; ++i)
219 it = notifiers.find(pfd[i].fd);
220 if (it != notifiers.end()
221 && it->second->state == 1) // added and in poll
223 int req = it->second->getRequested();
224 if (pfd[i].revents & req)
225 it->second->activate(pfd[i].revents & req);
226 pfd[i].revents &= ~req;
228 if (pfd[i].revents & (POLLERR|POLLHUP|POLLNVAL))
229 eDebug("poll: unhandled POLLERR/HUP/NVAL for fd %d(%d)", pfd[i].fd, pfd[i].revents);
232 for (; i < fdcount; ++i)
237 *res = PyList_New(0);
238 ePyObject it = PyTuple_New(2);
239 PyTuple_SET_ITEM(it, 0, PyInt_FromLong(pfd[i].fd));
240 PyTuple_SET_ITEM(it, 1, PyInt_FromLong(pfd[i].revents));
241 PyList_Append(*res, it);
248 /* when we got a signal, we get EINTR. */
250 eDebug("poll made error (%m)");
252 return_reason = 2; /* don't assume the timeout has passed when we got a signal */
255 return return_reason;
258 void eMainloop::addTimer(eTimer* e)
260 m_timer_list.insert_in_order(e);
263 void eMainloop::removeTimer(eTimer* e)
265 m_timer_list.remove(e);
268 int eMainloop::iterate(unsigned int twisted_timeout, PyObject **res, ePyObject dict)
274 clock_gettime(CLOCK_MONOTONIC, &m_twisted_timer);
275 m_twisted_timer += twisted_timeout;
278 /* TODO: this code just became ugly. fix that. */
281 if (m_interrupt_requested)
283 m_interrupt_requested = 0;
293 timespec now, timeout;
294 clock_gettime(CLOCK_MONOTONIC, &now);
295 if (m_twisted_timer<=now) // timeout
297 timeout = m_twisted_timer - now;
298 to = timeout.tv_sec * 1000 + timeout.tv_nsec / 1000000;
300 ret = processOneEvent(to, res, dict);
301 } while ( !ret && !(res && *res) );
306 int eMainloop::runLoop()
308 while (!app_quit_now)
313 void eMainloop::reset()
318 PyObject *eMainloop::poll(ePyObject timeout, ePyObject dict)
325 int twisted_timeout = (timeout == Py_None) ? 0 : PyInt_AsLong(timeout);
327 iterate(twisted_timeout, &res, dict);
331 return PyList_New(0); /* return empty list on timeout */
334 void eMainloop::interruptPoll()
336 m_interrupt_requested = 1;
339 void eMainloop::quit(int ret)
345 eApplication* eApp = 0;
347 #include "structmember.h"
351 // eTimer replacement
357 PyObject *in_weakreflist; /* List of weak references */
361 eTimerPy_traverse(eTimerPy *self, visitproc visit, void *arg)
363 PyObject *obj = self->tm->timeout.get();
369 eTimerPy_clear(eTimerPy *self)
371 PyObject *obj = self->tm->timeout.get();
377 eTimerPy_dealloc(eTimerPy* self)
379 if (self->in_weakreflist != NULL)
380 PyObject_ClearWeakRefs((PyObject *) self);
381 eTimerPy_clear(self);
383 self->ob_type->tp_free((PyObject*)self);
387 eTimerPy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
389 eTimerPy *self = (eTimerPy *)type->tp_alloc(type, 0);
390 self->tm = new eTimer(eApp);
391 self->in_weakreflist = NULL;
392 return (PyObject *)self;
396 eTimerPy_is_active(eTimerPy* self)
398 PyObject *ret = NULL;
399 ret = self->tm->isActive() ? Py_True : Py_False;
405 eTimerPy_start(eTimerPy* self, PyObject *args)
409 if (PyTuple_Size(args) > 1)
411 if (!PyArg_ParseTuple(args, "ll", &v, &singleShot)) // when 2nd arg is a value
414 if (!PyArg_ParseTuple(args, "lO", &v, &obj)) // get 2nd arg as python object
416 else if (obj == Py_True)
418 else if (obj != Py_False)
422 else if (!PyArg_ParseTuple(args, "l", &v))
424 self->tm->start(v, singleShot);
429 eTimerPy_start_long(eTimerPy* self, PyObject *args)
432 if (!PyArg_ParseTuple(args, "l", &v)) {
435 self->tm->startLongTimer(v);
440 eTimerPy_change_interval(eTimerPy* self, PyObject *args)
443 if (!PyArg_ParseTuple(args, "l", &v)) {
446 self->tm->changeInterval(v);
451 eTimerPy_stop(eTimerPy* self)
458 eTimerPy_get_callback_list(eTimerPy *self)
459 { //used for compatibilty with the old eTimer
460 return self->tm->timeout.get();
463 static PyMethodDef eTimerPy_methods[] = {
464 {"isActive", (PyCFunction)eTimerPy_is_active, METH_NOARGS,
465 "returns the timer state"
467 {"start", (PyCFunction)eTimerPy_start, METH_VARARGS,
468 "start timer with interval in msecs"
470 {"startLongTimer", (PyCFunction)eTimerPy_start_long, METH_VARARGS,
471 "start timer with interval in secs"
473 {"changeInterval", (PyCFunction)eTimerPy_change_interval, METH_VARARGS,
474 "change interval of a timer (in msecs)"
476 {"stop", (PyCFunction)eTimerPy_stop, METH_NOARGS,
479 //used for compatibilty with the old eTimer
480 {"get", (PyCFunction)eTimerPy_get_callback_list, METH_NOARGS,
481 "get timeout callback list"
483 {NULL} /* Sentinel */
487 eTimerPy_get_cb_list(eTimerPy *self, void *closure)
489 return self->tm->timeout.get();
493 eTimerPy_timeout(eTimerPy *self, void *closure)
494 { //used for compatibilty with the old eTimer
495 Org_Py_INCREF((PyObject*)self);
496 return (PyObject*)self;
499 static PyGetSetDef eTimerPy_getseters[] = {
501 (getter)eTimerPy_get_cb_list, (setter)0,
502 "returns the callback python list",
505 {"timeout", //used for compatibilty with the old eTimer
506 (getter)eTimerPy_timeout, (setter)0,
507 "synonym for our self",
510 {NULL} /* Sentinel */
513 static PyTypeObject eTimerPyType = {
514 PyObject_HEAD_INIT(NULL)
516 "eBaseImpl.eTimer", /*tp_name*/
517 sizeof(eTimerPy), /*tp_basicsize*/
519 (destructor)eTimerPy_dealloc, /*tp_dealloc*/
526 0, /*tp_as_sequence*/
534 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
535 "eTimer objects", /* tp_doc */
536 (traverseproc)eTimerPy_traverse, /* tp_traverse */
537 (inquiry)eTimerPy_clear, /* tp_clear */
538 0, /* tp_richcompare */
539 offsetof(eTimerPy, in_weakreflist), /* tp_weaklistoffset */
542 eTimerPy_methods, /* tp_methods */
544 eTimerPy_getseters, /* tp_getset */
547 0, /* tp_descr_get */
548 0, /* tp_descr_set */
549 0, /* tp_dictoffset */
552 eTimerPy_new, /* tp_new */
555 // eSocketNotifier replacement
557 struct eSocketNotifierPy
561 PyObject *in_weakreflist; /* List of weak references */
565 eSocketNotifierPy_traverse(eSocketNotifierPy *self, visitproc visit, void *arg)
567 PyObject *obj = self->sn->activated.get();
573 eSocketNotifierPy_clear(eSocketNotifierPy *self)
575 PyObject *obj = self->sn->activated.get();
581 eSocketNotifierPy_dealloc(eSocketNotifierPy* self)
583 if (self->in_weakreflist != NULL)
584 PyObject_ClearWeakRefs((PyObject *) self);
585 eSocketNotifierPy_clear(self);
587 self->ob_type->tp_free((PyObject*)self);
591 eSocketNotifierPy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
593 eSocketNotifierPy *self = (eSocketNotifierPy *)type->tp_alloc(type, 0);
594 int fd, req, immediate_start = 1, size = PyTuple_Size(args);
597 if (!PyArg_ParseTuple(args, "iii", &fd, &req, &immediate_start))
599 PyObject *obj = NULL;
600 if (!PyArg_ParseTuple(args, "iiO", &fd, &req, &immediate_start))
604 else if (obj != Py_True)
608 else if (size < 2 || !PyArg_ParseTuple(args, "ii", &fd, &req))
610 self->sn = new eSocketNotifier(eApp, fd, req, immediate_start);
611 self->in_weakreflist = NULL;
612 return (PyObject *)self;
616 eSocketNotifierPy_is_running(eSocketNotifierPy* self)
618 PyObject *ret = self->sn->isRunning() ? Py_True : Py_False;
624 eSocketNotifierPy_start(eSocketNotifierPy* self)
631 eSocketNotifierPy_stop(eSocketNotifierPy* self)
638 eSocketNotifierPy_get_fd(eSocketNotifierPy* self)
640 return PyInt_FromLong(self->sn->getFD());
644 eSocketNotifierPy_get_requested(eSocketNotifierPy* self)
646 return PyInt_FromLong(self->sn->getRequested());
650 eSocketNotifierPy_set_requested(eSocketNotifierPy* self, PyObject *args)
653 if (PyTuple_Size(args) != 1 || !PyArg_ParseTuple(args, "i", &req))
655 self->sn->setRequested(req);
659 static PyMethodDef eSocketNotifierPy_methods[] = {
660 {"isRunning", (PyCFunction)eSocketNotifierPy_is_running, METH_NOARGS,
661 "returns the running state"
663 {"start", (PyCFunction)eSocketNotifierPy_start, METH_NOARGS,
666 {"stop", (PyCFunction)eSocketNotifierPy_stop, METH_NOARGS,
669 {"getFD", (PyCFunction)eSocketNotifierPy_get_fd, METH_NOARGS,
670 "get file descriptor"
672 {"getRequested", (PyCFunction)eSocketNotifierPy_get_requested, METH_NOARGS,
675 {"setRequested", (PyCFunction)eSocketNotifierPy_set_requested, METH_VARARGS,
678 {NULL} /* Sentinel */
682 eSocketNotifierPy_get_cb_list(eSocketNotifierPy *self, void *closure)
684 return self->sn->activated.get();
687 static PyGetSetDef eSocketNotifierPy_getseters[] = {
689 (getter)eSocketNotifierPy_get_cb_list, (setter)0,
690 "returns the callback python list",
692 {NULL} /* Sentinel */
695 static PyTypeObject eSocketNotifierPyType = {
696 PyObject_HEAD_INIT(NULL)
698 "eBaseImpl.eSocketNotifier", /*tp_name*/
699 sizeof(eSocketNotifierPy), /*tp_basicsize*/
701 (destructor)eSocketNotifierPy_dealloc, /*tp_dealloc*/
708 0, /*tp_as_sequence*/
716 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
717 "eTimer objects", /* tp_doc */
718 (traverseproc)eSocketNotifierPy_traverse, /* tp_traverse */
719 (inquiry)eSocketNotifierPy_clear, /* tp_clear */
720 0, /* tp_richcompare */
721 offsetof(eSocketNotifierPy, in_weakreflist), /* tp_weaklistoffset */
724 eSocketNotifierPy_methods, /* tp_methods */
726 eSocketNotifierPy_getseters, /* tp_getset */
729 0, /* tp_descr_get */
730 0, /* tp_descr_set */
731 0, /* tp_dictoffset */
734 eSocketNotifierPy_new, /* tp_new */
737 static PyMethodDef module_methods[] = {
738 {NULL} /* Sentinel */
745 m = Py_InitModule3("eBaseImpl", module_methods,
746 "Module that implements some enigma classes with working cyclic garbage collection.");
751 if (!PyType_Ready(&eTimerPyType))
753 Org_Py_INCREF((PyObject*)&eTimerPyType);
754 PyModule_AddObject(m, "eTimer", (PyObject*)&eTimerPyType);
756 if (!PyType_Ready(&eSocketNotifierPyType))
758 Org_Py_INCREF((PyObject*)&eSocketNotifierPyType);
759 PyModule_AddObject(m, "eSocketNotifier", (PyObject*)&eSocketNotifierPyType);