1 #include <lib/base/ebase.h>
7 #include <lib/base/eerror.h>
8 #include <lib/base/elock.h>
9 #include <lib/gdi/grc.h>
11 DEFINE_REF(eSocketNotifier);
13 eSocketNotifier::eSocketNotifier(eMainloop *context, int fd, int requested, bool startnow): context(*context), fd(fd), state(0), requested(requested)
19 eSocketNotifier::~eSocketNotifier()
24 void eSocketNotifier::start()
29 context.addSocketNotifier(this);
30 state=2; // running but not in poll yet
33 void eSocketNotifier::stop()
38 context.removeSocketNotifier(this);
44 void eTimer::start(long msek, bool singleShot)
50 bSingleShot = singleShot;
52 clock_gettime(CLOCK_MONOTONIC, &nextActivation);
53 // eDebug("this = %p\nnow sec = %d, nsec = %d\nadd %d msec", this, nextActivation.tv_sec, nextActivation.tv_nsec, msek);
54 nextActivation += (msek<0 ? 0 : msek);
55 // eDebug("next Activation sec = %d, nsec = %d", nextActivation.tv_sec, nextActivation.tv_nsec );
56 context.addTimer(this);
59 void eTimer::startLongTimer( int seconds )
64 bActive = bSingleShot = true;
66 clock_gettime(CLOCK_MONOTONIC, &nextActivation);
67 // eDebug("this = %p\nnow sec = %d, nsec = %d\nadd %d sec", this, nextActivation.tv_sec, nextActivation.tv_nsec, seconds);
69 nextActivation.tv_sec += seconds;
70 // eDebug("next Activation sec = %d, nsec = %d", nextActivation.tv_sec, nextActivation.tv_nsec );
71 context.addTimer(this);
79 context.removeTimer(this);
83 void eTimer::changeInterval(long msek)
85 if (bActive) // Timer is running?
87 context.removeTimer(this); // then stop
88 nextActivation -= interval; // sub old interval
91 bActive=true; // then activate Timer
93 interval = msek; // set new Interval
94 nextActivation += interval; // calc nextActivation
96 context.addTimer(this); // add Timer to context TimerList
99 void eTimer::activate() // Internal Funktion... called from eApplication
101 context.removeTimer(this);
105 nextActivation += interval;
106 context.addTimer(this);
115 ePtrList<eMainloop> eMainloop::existing_loops;
117 eMainloop::~eMainloop()
119 existing_loops.remove(this);
120 for (std::map<int, eSocketNotifier*>::iterator it(notifiers.begin());it != notifiers.end();++it)
122 while(m_timer_list.begin() != m_timer_list.end())
123 m_timer_list.begin()->stop();
126 void eMainloop::addSocketNotifier(eSocketNotifier *sn)
128 int fd = sn->getFD();
129 ASSERT(notifiers.find(fd) == notifiers.end());
133 void eMainloop::removeSocketNotifier(eSocketNotifier *sn)
135 int fd = sn->getFD();
136 std::map<int,eSocketNotifier*>::iterator i(notifiers.find(fd));
137 if (i != notifiers.end())
142 for (i = notifiers.begin(); i != notifiers.end(); ++i)
143 eDebug("fd=%d, sn=%d", i->second->getFD(), (void*)i->second);
144 eFatal("removed socket notifier which is not present, fd=%d", fd);
147 int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePyObject additional)
149 int return_reason = 0;
150 /* get current time */
152 if (additional && !PyDict_Check(additional))
153 eFatal("additional, but it's not dict");
155 if (additional && !res)
156 eFatal("additional, but no res");
158 long poll_timeout = -1; /* infinite in case of empty timer list */
160 if (!m_timer_list.empty())
162 /* process all timers which are ready. first remove them out of the list. */
163 while (!m_timer_list.empty() && (poll_timeout = timeout_usec( m_timer_list.begin()->getNextActivation() ) ) <= 0 )
165 eTimer *tmr = m_timer_list.begin();
170 if (poll_timeout < 0)
172 else /* convert us to ms */
173 poll_timeout /= 1000;
176 if ((twisted_timeout > 0) && (poll_timeout > 0) && ((unsigned int)poll_timeout > twisted_timeout))
178 poll_timeout = twisted_timeout;
182 int nativecount=notifiers.size(),
187 fdcount += PyDict_Size(additional);
189 // build the poll aray
190 pollfd pfd[fdcount]; // make new pollfd array
191 std::map<int,eSocketNotifier*>::iterator it = notifiers.begin();
194 for (; i < nativecount; ++i, ++it)
196 it->second->state = 1; // running and in poll
197 pfd[i].fd = it->first;
198 pfd[i].events = it->second->getRequested();
203 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
204 typedef int Py_ssize_t;
205 # define PY_SSIZE_T_MAX INT_MAX
206 # define PY_SSIZE_T_MIN INT_MIN
210 while (PyDict_Next(additional, &pos, &key, &val)) {
211 pfd[i].fd = PyObject_AsFileDescriptor(key);
212 pfd[i++].events = PyInt_AsLong(val);
222 op.opcode = gOpcode::flush;
223 gRC::getInstance()->submit(op);
224 Py_BEGIN_ALLOW_THREADS
225 ret = ::poll(pfd, fdcount, poll_timeout);
229 ret = ::poll(pfd, fdcount, poll_timeout);
233 /* ret > 0 means that there are some active poll entries. */
238 for (; i < nativecount; ++i)
242 it = notifiers.find(pfd[i].fd);
243 if (it != notifiers.end()
244 && it->second->state == 1) // added and in poll
246 eSocketNotifier *sn = it->second;
247 int req = sn->getRequested();
248 if (pfd[i].revents & req) {
250 sn->activate(pfd[i].revents & req);
253 pfd[i].revents &= ~req;
255 if (pfd[i].revents & (POLLERR|POLLHUP|POLLNVAL))
256 eDebug("poll: unhandled POLLERR/HUP/NVAL for fd %d(%d)", pfd[i].fd, pfd[i].revents);
259 for (; i < fdcount; ++i)
264 *res = PyList_New(0);
265 ePyObject it = PyTuple_New(2);
266 PyTuple_SET_ITEM(it, 0, PyInt_FromLong(pfd[i].fd));
267 PyTuple_SET_ITEM(it, 1, PyInt_FromLong(pfd[i].revents));
268 PyList_Append(*res, it);
275 /* when we got a signal, we get EINTR. */
277 eDebug("poll made error (%m)");
279 return_reason = 2; /* don't assume the timeout has passed when we got a signal */
282 return return_reason;
285 void eMainloop::addTimer(eTimer* e)
287 m_timer_list.insert_in_order(e);
290 void eMainloop::removeTimer(eTimer* e)
292 m_timer_list.remove(e);
295 int eMainloop::iterate(unsigned int twisted_timeout, PyObject **res, ePyObject dict)
301 clock_gettime(CLOCK_MONOTONIC, &m_twisted_timer);
302 m_twisted_timer += twisted_timeout;
305 /* TODO: this code just became ugly. fix that. */
308 if (m_interrupt_requested)
310 m_interrupt_requested = 0;
320 timespec now, timeout;
321 clock_gettime(CLOCK_MONOTONIC, &now);
322 if (m_twisted_timer<=now) // timeout
324 timeout = m_twisted_timer - now;
325 to = timeout.tv_sec * 1000 + timeout.tv_nsec / 1000000;
327 ret = processOneEvent(to, res, dict);
328 } while ( !ret && !(res && *res) );
333 int eMainloop::runLoop()
335 while (!app_quit_now)
340 void eMainloop::reset()
345 PyObject *eMainloop::poll(ePyObject timeout, ePyObject dict)
352 int twisted_timeout = (timeout == Py_None) ? 0 : PyInt_AsLong(timeout);
354 iterate(twisted_timeout, &res, dict);
358 return PyList_New(0); /* return empty list on timeout */
361 void eMainloop::interruptPoll()
363 m_interrupt_requested = 1;
366 void eMainloop::quit(int ret)
372 eApplication* eApp = 0;
374 #include "structmember.h"
378 // eTimer replacement
384 PyObject *in_weakreflist; /* List of weak references */
388 eTimerPy_traverse(eTimerPy *self, visitproc visit, void *arg)
390 PyObject *obj = self->tm->timeout.getSteal();
398 eTimerPy_clear(eTimerPy *self)
400 PyObject *obj = self->tm->timeout.getSteal(true);
407 eTimerPy_dealloc(eTimerPy* self)
409 if (self->in_weakreflist != NULL)
410 PyObject_ClearWeakRefs((PyObject *) self);
411 eTimerPy_clear(self);
413 self->ob_type->tp_free((PyObject*)self);
417 eTimerPy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
419 eTimerPy *self = (eTimerPy *)type->tp_alloc(type, 0);
420 self->tm = eTimer::create(eApp);
422 self->in_weakreflist = NULL;
423 return (PyObject *)self;
427 eTimerPy_is_active(eTimerPy* self)
429 PyObject *ret = NULL;
430 ret = self->tm->isActive() ? Py_True : Py_False;
436 eTimerPy_start(eTimerPy* self, PyObject *args)
440 if (PyTuple_Size(args) > 1)
442 if (!PyArg_ParseTuple(args, "ll", &v, &singleShot)) // when 2nd arg is a value
445 if (!PyArg_ParseTuple(args, "lO", &v, &obj)) // get 2nd arg as python object
447 else if (obj == Py_True)
449 else if (obj != Py_False)
453 else if (!PyArg_ParseTuple(args, "l", &v))
455 self->tm->start(v, singleShot);
460 eTimerPy_start_long(eTimerPy* self, PyObject *args)
463 if (!PyArg_ParseTuple(args, "i", &v)) {
466 self->tm->startLongTimer(v);
471 eTimerPy_change_interval(eTimerPy* self, PyObject *args)
474 if (!PyArg_ParseTuple(args, "l", &v)) {
477 self->tm->changeInterval(v);
482 eTimerPy_stop(eTimerPy* self)
489 eTimerPy_get_callback_list(eTimerPy *self)
490 { //used for compatibilty with the old eTimer
491 return self->tm->timeout.get();
494 static PyMethodDef eTimerPy_methods[] = {
495 {"isActive", (PyCFunction)eTimerPy_is_active, METH_NOARGS,
496 "returns the timer state"
498 {"start", (PyCFunction)eTimerPy_start, METH_VARARGS,
499 "start timer with interval in msecs"
501 {"startLongTimer", (PyCFunction)eTimerPy_start_long, METH_VARARGS,
502 "start timer with interval in secs"
504 {"changeInterval", (PyCFunction)eTimerPy_change_interval, METH_VARARGS,
505 "change interval of a timer (in msecs)"
507 {"stop", (PyCFunction)eTimerPy_stop, METH_NOARGS,
510 //used for compatibilty with the old eTimer
511 {"get", (PyCFunction)eTimerPy_get_callback_list, METH_NOARGS,
512 "get timeout callback list"
514 {NULL} /* Sentinel */
518 eTimerPy_get_cb_list(eTimerPy *self, void *closure)
520 return self->tm->timeout.get();
524 eTimerPy_timeout(eTimerPy *self, void *closure)
525 { //used for compatibilty with the old eTimer
526 Org_Py_INCREF((PyObject*)self);
527 return (PyObject*)self;
530 static PyGetSetDef eTimerPy_getseters[] = {
532 (getter)eTimerPy_get_cb_list, (setter)0,
533 "returns the callback python list",
536 {"timeout", //used for compatibilty with the old eTimer
537 (getter)eTimerPy_timeout, (setter)0,
538 "synonym for our self",
541 {NULL} /* Sentinel */
544 static PyTypeObject eTimerPyType = {
545 PyObject_HEAD_INIT(NULL)
547 "eBaseImpl.eTimer", /*tp_name*/
548 sizeof(eTimerPy), /*tp_basicsize*/
550 (destructor)eTimerPy_dealloc, /*tp_dealloc*/
557 0, /*tp_as_sequence*/
565 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
566 "eTimer objects", /* tp_doc */
567 (traverseproc)eTimerPy_traverse, /* tp_traverse */
568 (inquiry)eTimerPy_clear, /* tp_clear */
569 0, /* tp_richcompare */
570 offsetof(eTimerPy, in_weakreflist), /* tp_weaklistoffset */
573 eTimerPy_methods, /* tp_methods */
575 eTimerPy_getseters, /* tp_getset */
578 0, /* tp_descr_get */
579 0, /* tp_descr_set */
580 0, /* tp_dictoffset */
583 eTimerPy_new, /* tp_new */
586 // eSocketNotifier replacement
588 struct eSocketNotifierPy
592 PyObject *in_weakreflist; /* List of weak references */
596 eSocketNotifierPy_traverse(eSocketNotifierPy *self, visitproc visit, void *arg)
598 PyObject *obj = self->sn->activated.getSteal();
605 eSocketNotifierPy_clear(eSocketNotifierPy *self)
607 PyObject *obj = self->sn->activated.getSteal(true);
614 eSocketNotifierPy_dealloc(eSocketNotifierPy* self)
616 if (self->in_weakreflist != NULL)
617 PyObject_ClearWeakRefs((PyObject *) self);
618 eSocketNotifierPy_clear(self);
620 self->ob_type->tp_free((PyObject*)self);
624 eSocketNotifierPy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
626 eSocketNotifierPy *self = (eSocketNotifierPy *)type->tp_alloc(type, 0);
627 int fd, req, immediate_start = 1, size = PyTuple_Size(args);
630 if (!PyArg_ParseTuple(args, "iii", &fd, &req, &immediate_start))
632 PyObject *obj = NULL;
633 if (!PyArg_ParseTuple(args, "iiO", &fd, &req, &immediate_start))
637 else if (obj != Py_True)
641 else if (size < 2 || !PyArg_ParseTuple(args, "ii", &fd, &req))
643 self->sn = eSocketNotifier::create(eApp, fd, req, immediate_start);
645 self->in_weakreflist = NULL;
646 return (PyObject *)self;
650 eSocketNotifierPy_is_running(eSocketNotifierPy* self)
652 PyObject *ret = self->sn->isRunning() ? Py_True : Py_False;
658 eSocketNotifierPy_start(eSocketNotifierPy* self)
665 eSocketNotifierPy_stop(eSocketNotifierPy* self)
672 eSocketNotifierPy_get_fd(eSocketNotifierPy* self)
674 return PyInt_FromLong(self->sn->getFD());
678 eSocketNotifierPy_get_requested(eSocketNotifierPy* self)
680 return PyInt_FromLong(self->sn->getRequested());
684 eSocketNotifierPy_set_requested(eSocketNotifierPy* self, PyObject *args)
687 if (PyTuple_Size(args) != 1 || !PyArg_ParseTuple(args, "i", &req))
689 self->sn->setRequested(req);
693 static PyMethodDef eSocketNotifierPy_methods[] = {
694 {"isRunning", (PyCFunction)eSocketNotifierPy_is_running, METH_NOARGS,
695 "returns the running state"
697 {"start", (PyCFunction)eSocketNotifierPy_start, METH_NOARGS,
700 {"stop", (PyCFunction)eSocketNotifierPy_stop, METH_NOARGS,
703 {"getFD", (PyCFunction)eSocketNotifierPy_get_fd, METH_NOARGS,
704 "get file descriptor"
706 {"getRequested", (PyCFunction)eSocketNotifierPy_get_requested, METH_NOARGS,
709 {"setRequested", (PyCFunction)eSocketNotifierPy_set_requested, METH_VARARGS,
712 {NULL} /* Sentinel */
716 eSocketNotifierPy_get_cb_list(eSocketNotifierPy *self, void *closure)
718 return self->sn->activated.get();
721 static PyGetSetDef eSocketNotifierPy_getseters[] = {
723 (getter)eSocketNotifierPy_get_cb_list, (setter)0,
724 "returns the callback python list",
726 {NULL} /* Sentinel */
729 static PyTypeObject eSocketNotifierPyType = {
730 PyObject_HEAD_INIT(NULL)
732 "eBaseImpl.eSocketNotifier", /*tp_name*/
733 sizeof(eSocketNotifierPy), /*tp_basicsize*/
735 (destructor)eSocketNotifierPy_dealloc, /*tp_dealloc*/
742 0, /*tp_as_sequence*/
750 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
751 "eTimer objects", /* tp_doc */
752 (traverseproc)eSocketNotifierPy_traverse, /* tp_traverse */
753 (inquiry)eSocketNotifierPy_clear, /* tp_clear */
754 0, /* tp_richcompare */
755 offsetof(eSocketNotifierPy, in_weakreflist), /* tp_weaklistoffset */
758 eSocketNotifierPy_methods, /* tp_methods */
760 eSocketNotifierPy_getseters, /* tp_getset */
763 0, /* tp_descr_get */
764 0, /* tp_descr_set */
765 0, /* tp_dictoffset */
768 eSocketNotifierPy_new, /* tp_new */
771 static PyMethodDef module_methods[] = {
772 {NULL} /* Sentinel */
777 PyObject* m = Py_InitModule3("eBaseImpl", module_methods,
778 "Module that implements some enigma classes with working cyclic garbage collection.");
783 if (!PyType_Ready(&eTimerPyType))
785 Org_Py_INCREF((PyObject*)&eTimerPyType);
786 PyModule_AddObject(m, "eTimer", (PyObject*)&eTimerPyType);
788 if (!PyType_Ready(&eSocketNotifierPyType))
790 Org_Py_INCREF((PyObject*)&eSocketNotifierPyType);
791 PyModule_AddObject(m, "eSocketNotifier", (PyObject*)&eSocketNotifierPyType);