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();
190 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
191 typedef int Py_ssize_t;
192 # define PY_SSIZE_T_MAX INT_MAX
193 # define PY_SSIZE_T_MIN INT_MIN
197 while (PyDict_Next(additional, &pos, &key, &val)) {
198 pfd[i].fd = PyObject_AsFileDescriptor(key);
199 pfd[i++].events = PyInt_AsLong(val);
207 Py_BEGIN_ALLOW_THREADS
208 ret = ::poll(pfd, fdcount, poll_timeout);
211 ret = ::poll(pfd, fdcount, poll_timeout);
215 /* ret > 0 means that there are some active poll entries. */
220 for (; i < nativecount; ++i)
224 it = notifiers.find(pfd[i].fd);
225 if (it != notifiers.end()
226 && it->second->state == 1) // added and in poll
228 int req = it->second->getRequested();
229 if (pfd[i].revents & req)
230 it->second->activate(pfd[i].revents & req);
231 pfd[i].revents &= ~req;
233 if (pfd[i].revents & (POLLERR|POLLHUP|POLLNVAL))
234 eDebug("poll: unhandled POLLERR/HUP/NVAL for fd %d(%d)", pfd[i].fd, pfd[i].revents);
237 for (; i < fdcount; ++i)
242 *res = PyList_New(0);
243 ePyObject it = PyTuple_New(2);
244 PyTuple_SET_ITEM(it, 0, PyInt_FromLong(pfd[i].fd));
245 PyTuple_SET_ITEM(it, 1, PyInt_FromLong(pfd[i].revents));
246 PyList_Append(*res, it);
253 /* when we got a signal, we get EINTR. */
255 eDebug("poll made error (%m)");
257 return_reason = 2; /* don't assume the timeout has passed when we got a signal */
260 return return_reason;
263 void eMainloop::addTimer(eTimer* e)
265 m_timer_list.insert_in_order(e);
268 void eMainloop::removeTimer(eTimer* e)
270 m_timer_list.remove(e);
273 int eMainloop::iterate(unsigned int twisted_timeout, PyObject **res, ePyObject dict)
279 clock_gettime(CLOCK_MONOTONIC, &m_twisted_timer);
280 m_twisted_timer += twisted_timeout;
283 /* TODO: this code just became ugly. fix that. */
286 if (m_interrupt_requested)
288 m_interrupt_requested = 0;
298 timespec now, timeout;
299 clock_gettime(CLOCK_MONOTONIC, &now);
300 if (m_twisted_timer<=now) // timeout
302 timeout = m_twisted_timer - now;
303 to = timeout.tv_sec * 1000 + timeout.tv_nsec / 1000000;
305 ret = processOneEvent(to, res, dict);
306 } while ( !ret && !(res && *res) );
311 int eMainloop::runLoop()
313 while (!app_quit_now)
318 void eMainloop::reset()
323 PyObject *eMainloop::poll(ePyObject timeout, ePyObject dict)
330 int twisted_timeout = (timeout == Py_None) ? 0 : PyInt_AsLong(timeout);
332 iterate(twisted_timeout, &res, dict);
336 return PyList_New(0); /* return empty list on timeout */
339 void eMainloop::interruptPoll()
341 m_interrupt_requested = 1;
344 void eMainloop::quit(int ret)
350 eApplication* eApp = 0;
352 #include "structmember.h"
356 // eTimer replacement
362 PyObject *in_weakreflist; /* List of weak references */
366 eTimerPy_traverse(eTimerPy *self, visitproc visit, void *arg)
368 PyObject *obj = self->tm->timeout.get();
374 eTimerPy_clear(eTimerPy *self)
376 PyObject *obj = self->tm->timeout.get();
382 eTimerPy_dealloc(eTimerPy* self)
384 if (self->in_weakreflist != NULL)
385 PyObject_ClearWeakRefs((PyObject *) self);
386 eTimerPy_clear(self);
388 self->ob_type->tp_free((PyObject*)self);
392 eTimerPy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
394 eTimerPy *self = (eTimerPy *)type->tp_alloc(type, 0);
395 self->tm = new eTimer(eApp);
396 self->in_weakreflist = NULL;
397 return (PyObject *)self;
401 eTimerPy_is_active(eTimerPy* self)
403 PyObject *ret = NULL;
404 ret = self->tm->isActive() ? Py_True : Py_False;
410 eTimerPy_start(eTimerPy* self, PyObject *args)
414 if (PyTuple_Size(args) > 1)
416 if (!PyArg_ParseTuple(args, "ll", &v, &singleShot)) // when 2nd arg is a value
419 if (!PyArg_ParseTuple(args, "lO", &v, &obj)) // get 2nd arg as python object
421 else if (obj == Py_True)
423 else if (obj != Py_False)
427 else if (!PyArg_ParseTuple(args, "l", &v))
429 self->tm->start(v, singleShot);
434 eTimerPy_start_long(eTimerPy* self, PyObject *args)
437 if (!PyArg_ParseTuple(args, "l", &v)) {
440 self->tm->startLongTimer(v);
445 eTimerPy_change_interval(eTimerPy* self, PyObject *args)
448 if (!PyArg_ParseTuple(args, "l", &v)) {
451 self->tm->changeInterval(v);
456 eTimerPy_stop(eTimerPy* self)
463 eTimerPy_get_callback_list(eTimerPy *self)
464 { //used for compatibilty with the old eTimer
465 return self->tm->timeout.get();
468 static PyMethodDef eTimerPy_methods[] = {
469 {"isActive", (PyCFunction)eTimerPy_is_active, METH_NOARGS,
470 "returns the timer state"
472 {"start", (PyCFunction)eTimerPy_start, METH_VARARGS,
473 "start timer with interval in msecs"
475 {"startLongTimer", (PyCFunction)eTimerPy_start_long, METH_VARARGS,
476 "start timer with interval in secs"
478 {"changeInterval", (PyCFunction)eTimerPy_change_interval, METH_VARARGS,
479 "change interval of a timer (in msecs)"
481 {"stop", (PyCFunction)eTimerPy_stop, METH_NOARGS,
484 //used for compatibilty with the old eTimer
485 {"get", (PyCFunction)eTimerPy_get_callback_list, METH_NOARGS,
486 "get timeout callback list"
488 {NULL} /* Sentinel */
492 eTimerPy_get_cb_list(eTimerPy *self, void *closure)
494 return self->tm->timeout.get();
498 eTimerPy_timeout(eTimerPy *self, void *closure)
499 { //used for compatibilty with the old eTimer
500 Org_Py_INCREF((PyObject*)self);
501 return (PyObject*)self;
504 static PyGetSetDef eTimerPy_getseters[] = {
506 (getter)eTimerPy_get_cb_list, (setter)0,
507 "returns the callback python list",
510 {"timeout", //used for compatibilty with the old eTimer
511 (getter)eTimerPy_timeout, (setter)0,
512 "synonym for our self",
515 {NULL} /* Sentinel */
518 static PyTypeObject eTimerPyType = {
519 PyObject_HEAD_INIT(NULL)
521 "eBaseImpl.eTimer", /*tp_name*/
522 sizeof(eTimerPy), /*tp_basicsize*/
524 (destructor)eTimerPy_dealloc, /*tp_dealloc*/
531 0, /*tp_as_sequence*/
539 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
540 "eTimer objects", /* tp_doc */
541 (traverseproc)eTimerPy_traverse, /* tp_traverse */
542 (inquiry)eTimerPy_clear, /* tp_clear */
543 0, /* tp_richcompare */
544 offsetof(eTimerPy, in_weakreflist), /* tp_weaklistoffset */
547 eTimerPy_methods, /* tp_methods */
549 eTimerPy_getseters, /* tp_getset */
552 0, /* tp_descr_get */
553 0, /* tp_descr_set */
554 0, /* tp_dictoffset */
557 eTimerPy_new, /* tp_new */
560 // eSocketNotifier replacement
562 struct eSocketNotifierPy
566 PyObject *in_weakreflist; /* List of weak references */
570 eSocketNotifierPy_traverse(eSocketNotifierPy *self, visitproc visit, void *arg)
572 PyObject *obj = self->sn->activated.get();
578 eSocketNotifierPy_clear(eSocketNotifierPy *self)
580 PyObject *obj = self->sn->activated.get();
586 eSocketNotifierPy_dealloc(eSocketNotifierPy* self)
588 if (self->in_weakreflist != NULL)
589 PyObject_ClearWeakRefs((PyObject *) self);
590 eSocketNotifierPy_clear(self);
592 self->ob_type->tp_free((PyObject*)self);
596 eSocketNotifierPy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
598 eSocketNotifierPy *self = (eSocketNotifierPy *)type->tp_alloc(type, 0);
599 int fd, req, immediate_start = 1, size = PyTuple_Size(args);
602 if (!PyArg_ParseTuple(args, "iii", &fd, &req, &immediate_start))
604 PyObject *obj = NULL;
605 if (!PyArg_ParseTuple(args, "iiO", &fd, &req, &immediate_start))
609 else if (obj != Py_True)
613 else if (size < 2 || !PyArg_ParseTuple(args, "ii", &fd, &req))
615 self->sn = new eSocketNotifier(eApp, fd, req, immediate_start);
616 self->in_weakreflist = NULL;
617 return (PyObject *)self;
621 eSocketNotifierPy_is_running(eSocketNotifierPy* self)
623 PyObject *ret = self->sn->isRunning() ? Py_True : Py_False;
629 eSocketNotifierPy_start(eSocketNotifierPy* self)
636 eSocketNotifierPy_stop(eSocketNotifierPy* self)
643 eSocketNotifierPy_get_fd(eSocketNotifierPy* self)
645 return PyInt_FromLong(self->sn->getFD());
649 eSocketNotifierPy_get_requested(eSocketNotifierPy* self)
651 return PyInt_FromLong(self->sn->getRequested());
655 eSocketNotifierPy_set_requested(eSocketNotifierPy* self, PyObject *args)
658 if (PyTuple_Size(args) != 1 || !PyArg_ParseTuple(args, "i", &req))
660 self->sn->setRequested(req);
664 static PyMethodDef eSocketNotifierPy_methods[] = {
665 {"isRunning", (PyCFunction)eSocketNotifierPy_is_running, METH_NOARGS,
666 "returns the running state"
668 {"start", (PyCFunction)eSocketNotifierPy_start, METH_NOARGS,
671 {"stop", (PyCFunction)eSocketNotifierPy_stop, METH_NOARGS,
674 {"getFD", (PyCFunction)eSocketNotifierPy_get_fd, METH_NOARGS,
675 "get file descriptor"
677 {"getRequested", (PyCFunction)eSocketNotifierPy_get_requested, METH_NOARGS,
680 {"setRequested", (PyCFunction)eSocketNotifierPy_set_requested, METH_VARARGS,
683 {NULL} /* Sentinel */
687 eSocketNotifierPy_get_cb_list(eSocketNotifierPy *self, void *closure)
689 return self->sn->activated.get();
692 static PyGetSetDef eSocketNotifierPy_getseters[] = {
694 (getter)eSocketNotifierPy_get_cb_list, (setter)0,
695 "returns the callback python list",
697 {NULL} /* Sentinel */
700 static PyTypeObject eSocketNotifierPyType = {
701 PyObject_HEAD_INIT(NULL)
703 "eBaseImpl.eSocketNotifier", /*tp_name*/
704 sizeof(eSocketNotifierPy), /*tp_basicsize*/
706 (destructor)eSocketNotifierPy_dealloc, /*tp_dealloc*/
713 0, /*tp_as_sequence*/
721 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
722 "eTimer objects", /* tp_doc */
723 (traverseproc)eSocketNotifierPy_traverse, /* tp_traverse */
724 (inquiry)eSocketNotifierPy_clear, /* tp_clear */
725 0, /* tp_richcompare */
726 offsetof(eSocketNotifierPy, in_weakreflist), /* tp_weaklistoffset */
729 eSocketNotifierPy_methods, /* tp_methods */
731 eSocketNotifierPy_getseters, /* tp_getset */
734 0, /* tp_descr_get */
735 0, /* tp_descr_set */
736 0, /* tp_dictoffset */
739 eSocketNotifierPy_new, /* tp_new */
742 static PyMethodDef module_methods[] = {
743 {NULL} /* Sentinel */
750 m = Py_InitModule3("eBaseImpl", module_methods,
751 "Module that implements some enigma classes with working cyclic garbage collection.");
756 if (!PyType_Ready(&eTimerPyType))
758 Org_Py_INCREF((PyObject*)&eTimerPyType);
759 PyModule_AddObject(m, "eTimer", (PyObject*)&eTimerPyType);
761 if (!PyType_Ready(&eSocketNotifierPyType))
763 Org_Py_INCREF((PyObject*)&eSocketNotifierPyType);
764 PyModule_AddObject(m, "eSocketNotifier", (PyObject*)&eSocketNotifierPyType);