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())
152 /* process all timers which are ready. first remove them out of the list. */
153 while (!m_timer_list.empty() && (poll_timeout = timeout_usec( m_timer_list.begin()->getNextActivation() ) ) <= 0 )
154 m_timer_list.begin()->activate();
155 if (poll_timeout < 0)
157 else /* convert us to ms */
158 poll_timeout /= 1000;
161 if ((twisted_timeout > 0) && (poll_timeout > 0) && ((unsigned int)poll_timeout > twisted_timeout))
163 poll_timeout = twisted_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();
187 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
188 typedef int Py_ssize_t;
189 # define PY_SSIZE_T_MAX INT_MAX
190 # define PY_SSIZE_T_MIN INT_MIN
194 while (PyDict_Next(additional, &pos, &key, &val)) {
195 pfd[i].fd = PyObject_AsFileDescriptor(key);
196 pfd[i++].events = PyInt_AsLong(val);
204 Py_BEGIN_ALLOW_THREADS
205 ret = ::poll(pfd, fdcount, poll_timeout);
208 ret = ::poll(pfd, fdcount, poll_timeout);
212 /* ret > 0 means that there are some active poll entries. */
217 for (; i < nativecount; ++i)
221 it = notifiers.find(pfd[i].fd);
222 if (it != notifiers.end()
223 && it->second->state == 1) // added and in poll
225 int req = it->second->getRequested();
226 if (pfd[i].revents & req)
227 it->second->activate(pfd[i].revents & req);
228 pfd[i].revents &= ~req;
230 if (pfd[i].revents & (POLLERR|POLLHUP|POLLNVAL))
231 eDebug("poll: unhandled POLLERR/HUP/NVAL for fd %d(%d)", pfd[i].fd, pfd[i].revents);
234 for (; i < fdcount; ++i)
239 *res = PyList_New(0);
240 ePyObject it = PyTuple_New(2);
241 PyTuple_SET_ITEM(it, 0, PyInt_FromLong(pfd[i].fd));
242 PyTuple_SET_ITEM(it, 1, PyInt_FromLong(pfd[i].revents));
243 PyList_Append(*res, it);
250 /* when we got a signal, we get EINTR. */
252 eDebug("poll made error (%m)");
254 return_reason = 2; /* don't assume the timeout has passed when we got a signal */
257 return return_reason;
260 void eMainloop::addTimer(eTimer* e)
262 m_timer_list.insert_in_order(e);
265 void eMainloop::removeTimer(eTimer* e)
267 m_timer_list.remove(e);
270 int eMainloop::iterate(unsigned int twisted_timeout, PyObject **res, ePyObject dict)
276 clock_gettime(CLOCK_MONOTONIC, &m_twisted_timer);
277 m_twisted_timer += twisted_timeout;
280 /* TODO: this code just became ugly. fix that. */
283 if (m_interrupt_requested)
285 m_interrupt_requested = 0;
295 timespec now, timeout;
296 clock_gettime(CLOCK_MONOTONIC, &now);
297 if (m_twisted_timer<=now) // timeout
299 timeout = m_twisted_timer - now;
300 to = timeout.tv_sec * 1000 + timeout.tv_nsec / 1000000;
302 ret = processOneEvent(to, res, dict);
303 } while ( !ret && !(res && *res) );
308 int eMainloop::runLoop()
310 while (!app_quit_now)
315 void eMainloop::reset()
320 PyObject *eMainloop::poll(ePyObject timeout, ePyObject dict)
327 int twisted_timeout = (timeout == Py_None) ? 0 : PyInt_AsLong(timeout);
329 iterate(twisted_timeout, &res, dict);
333 return PyList_New(0); /* return empty list on timeout */
336 void eMainloop::interruptPoll()
338 m_interrupt_requested = 1;
341 void eMainloop::quit(int ret)
347 eApplication* eApp = 0;
349 #include "structmember.h"
353 // eTimer replacement
359 PyObject *in_weakreflist; /* List of weak references */
363 eTimerPy_traverse(eTimerPy *self, visitproc visit, void *arg)
365 PyObject *obj = self->tm->timeout.get();
371 eTimerPy_clear(eTimerPy *self)
373 PyObject *obj = self->tm->timeout.get();
379 eTimerPy_dealloc(eTimerPy* self)
381 if (self->in_weakreflist != NULL)
382 PyObject_ClearWeakRefs((PyObject *) self);
383 eTimerPy_clear(self);
385 self->ob_type->tp_free((PyObject*)self);
389 eTimerPy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
391 eTimerPy *self = (eTimerPy *)type->tp_alloc(type, 0);
392 self->tm = new eTimer(eApp);
393 self->in_weakreflist = NULL;
394 return (PyObject *)self;
398 eTimerPy_is_active(eTimerPy* self)
400 PyObject *ret = NULL;
401 ret = self->tm->isActive() ? Py_True : Py_False;
407 eTimerPy_start(eTimerPy* self, PyObject *args)
411 if (PyTuple_Size(args) > 1)
413 if (!PyArg_ParseTuple(args, "ll", &v, &singleShot)) // when 2nd arg is a value
416 if (!PyArg_ParseTuple(args, "lO", &v, &obj)) // get 2nd arg as python object
418 else if (obj == Py_True)
420 else if (obj != Py_False)
424 else if (!PyArg_ParseTuple(args, "l", &v))
426 self->tm->start(v, singleShot);
431 eTimerPy_start_long(eTimerPy* self, PyObject *args)
434 if (!PyArg_ParseTuple(args, "l", &v)) {
437 self->tm->startLongTimer(v);
442 eTimerPy_change_interval(eTimerPy* self, PyObject *args)
445 if (!PyArg_ParseTuple(args, "l", &v)) {
448 self->tm->changeInterval(v);
453 eTimerPy_stop(eTimerPy* self)
460 eTimerPy_get_callback_list(eTimerPy *self)
461 { //used for compatibilty with the old eTimer
462 return self->tm->timeout.get();
465 static PyMethodDef eTimerPy_methods[] = {
466 {"isActive", (PyCFunction)eTimerPy_is_active, METH_NOARGS,
467 "returns the timer state"
469 {"start", (PyCFunction)eTimerPy_start, METH_VARARGS,
470 "start timer with interval in msecs"
472 {"startLongTimer", (PyCFunction)eTimerPy_start_long, METH_VARARGS,
473 "start timer with interval in secs"
475 {"changeInterval", (PyCFunction)eTimerPy_change_interval, METH_VARARGS,
476 "change interval of a timer (in msecs)"
478 {"stop", (PyCFunction)eTimerPy_stop, METH_NOARGS,
481 //used for compatibilty with the old eTimer
482 {"get", (PyCFunction)eTimerPy_get_callback_list, METH_NOARGS,
483 "get timeout callback list"
485 {NULL} /* Sentinel */
489 eTimerPy_get_cb_list(eTimerPy *self, void *closure)
491 return self->tm->timeout.get();
495 eTimerPy_timeout(eTimerPy *self, void *closure)
496 { //used for compatibilty with the old eTimer
497 Org_Py_INCREF((PyObject*)self);
498 return (PyObject*)self;
501 static PyGetSetDef eTimerPy_getseters[] = {
503 (getter)eTimerPy_get_cb_list, (setter)0,
504 "returns the callback python list",
507 {"timeout", //used for compatibilty with the old eTimer
508 (getter)eTimerPy_timeout, (setter)0,
509 "synonym for our self",
512 {NULL} /* Sentinel */
515 static PyTypeObject eTimerPyType = {
516 PyObject_HEAD_INIT(NULL)
518 "eBaseImpl.eTimer", /*tp_name*/
519 sizeof(eTimerPy), /*tp_basicsize*/
521 (destructor)eTimerPy_dealloc, /*tp_dealloc*/
528 0, /*tp_as_sequence*/
536 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
537 "eTimer objects", /* tp_doc */
538 (traverseproc)eTimerPy_traverse, /* tp_traverse */
539 (inquiry)eTimerPy_clear, /* tp_clear */
540 0, /* tp_richcompare */
541 offsetof(eTimerPy, in_weakreflist), /* tp_weaklistoffset */
544 eTimerPy_methods, /* tp_methods */
546 eTimerPy_getseters, /* tp_getset */
549 0, /* tp_descr_get */
550 0, /* tp_descr_set */
551 0, /* tp_dictoffset */
554 eTimerPy_new, /* tp_new */
557 // eSocketNotifier replacement
559 struct eSocketNotifierPy
563 PyObject *in_weakreflist; /* List of weak references */
567 eSocketNotifierPy_traverse(eSocketNotifierPy *self, visitproc visit, void *arg)
569 PyObject *obj = self->sn->activated.get();
575 eSocketNotifierPy_clear(eSocketNotifierPy *self)
577 PyObject *obj = self->sn->activated.get();
583 eSocketNotifierPy_dealloc(eSocketNotifierPy* self)
585 if (self->in_weakreflist != NULL)
586 PyObject_ClearWeakRefs((PyObject *) self);
587 eSocketNotifierPy_clear(self);
589 self->ob_type->tp_free((PyObject*)self);
593 eSocketNotifierPy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
595 eSocketNotifierPy *self = (eSocketNotifierPy *)type->tp_alloc(type, 0);
596 int fd, req, immediate_start = 1, size = PyTuple_Size(args);
599 if (!PyArg_ParseTuple(args, "iii", &fd, &req, &immediate_start))
601 PyObject *obj = NULL;
602 if (!PyArg_ParseTuple(args, "iiO", &fd, &req, &immediate_start))
606 else if (obj != Py_True)
610 else if (size < 2 || !PyArg_ParseTuple(args, "ii", &fd, &req))
612 self->sn = new eSocketNotifier(eApp, fd, req, immediate_start);
613 self->in_weakreflist = NULL;
614 return (PyObject *)self;
618 eSocketNotifierPy_is_running(eSocketNotifierPy* self)
620 PyObject *ret = self->sn->isRunning() ? Py_True : Py_False;
626 eSocketNotifierPy_start(eSocketNotifierPy* self)
633 eSocketNotifierPy_stop(eSocketNotifierPy* self)
640 eSocketNotifierPy_get_fd(eSocketNotifierPy* self)
642 return PyInt_FromLong(self->sn->getFD());
646 eSocketNotifierPy_get_requested(eSocketNotifierPy* self)
648 return PyInt_FromLong(self->sn->getRequested());
652 eSocketNotifierPy_set_requested(eSocketNotifierPy* self, PyObject *args)
655 if (PyTuple_Size(args) != 1 || !PyArg_ParseTuple(args, "i", &req))
657 self->sn->setRequested(req);
661 static PyMethodDef eSocketNotifierPy_methods[] = {
662 {"isRunning", (PyCFunction)eSocketNotifierPy_is_running, METH_NOARGS,
663 "returns the running state"
665 {"start", (PyCFunction)eSocketNotifierPy_start, METH_NOARGS,
668 {"stop", (PyCFunction)eSocketNotifierPy_stop, METH_NOARGS,
671 {"getFD", (PyCFunction)eSocketNotifierPy_get_fd, METH_NOARGS,
672 "get file descriptor"
674 {"getRequested", (PyCFunction)eSocketNotifierPy_get_requested, METH_NOARGS,
677 {"setRequested", (PyCFunction)eSocketNotifierPy_set_requested, METH_VARARGS,
680 {NULL} /* Sentinel */
684 eSocketNotifierPy_get_cb_list(eSocketNotifierPy *self, void *closure)
686 return self->sn->activated.get();
689 static PyGetSetDef eSocketNotifierPy_getseters[] = {
691 (getter)eSocketNotifierPy_get_cb_list, (setter)0,
692 "returns the callback python list",
694 {NULL} /* Sentinel */
697 static PyTypeObject eSocketNotifierPyType = {
698 PyObject_HEAD_INIT(NULL)
700 "eBaseImpl.eSocketNotifier", /*tp_name*/
701 sizeof(eSocketNotifierPy), /*tp_basicsize*/
703 (destructor)eSocketNotifierPy_dealloc, /*tp_dealloc*/
710 0, /*tp_as_sequence*/
718 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
719 "eTimer objects", /* tp_doc */
720 (traverseproc)eSocketNotifierPy_traverse, /* tp_traverse */
721 (inquiry)eSocketNotifierPy_clear, /* tp_clear */
722 0, /* tp_richcompare */
723 offsetof(eSocketNotifierPy, in_weakreflist), /* tp_weaklistoffset */
726 eSocketNotifierPy_methods, /* tp_methods */
728 eSocketNotifierPy_getseters, /* tp_getset */
731 0, /* tp_descr_get */
732 0, /* tp_descr_set */
733 0, /* tp_dictoffset */
736 eSocketNotifierPy_new, /* tp_new */
739 static PyMethodDef module_methods[] = {
740 {NULL} /* Sentinel */
747 m = Py_InitModule3("eBaseImpl", module_methods,
748 "Module that implements some enigma classes with working cyclic garbage collection.");
753 if (!PyType_Ready(&eTimerPyType))
755 Org_Py_INCREF((PyObject*)&eTimerPyType);
756 PyModule_AddObject(m, "eTimer", (PyObject*)&eTimerPyType);
758 if (!PyType_Ready(&eSocketNotifierPyType))
760 Org_Py_INCREF((PyObject*)&eSocketNotifierPyType);
761 PyModule_AddObject(m, "eSocketNotifier", (PyObject*)&eSocketNotifierPyType);