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 eSocketNotifier::eSocketNotifier(eMainloop *context, int fd, int requested, bool startnow): context(*context), fd(fd), state(0), requested(requested)
17 eSocketNotifier::~eSocketNotifier()
22 void eSocketNotifier::start()
27 context.addSocketNotifier(this);
28 state=2; // running but not in poll yet
31 void eSocketNotifier::stop()
34 context.removeSocketNotifier(this);
40 void eTimer::start(long msek, bool singleShot)
46 bSingleShot = singleShot;
48 clock_gettime(CLOCK_MONOTONIC, &nextActivation);
49 // eDebug("this = %p\nnow sec = %d, nsec = %d\nadd %d msec", this, nextActivation.tv_sec, nextActivation.tv_nsec, msek);
50 nextActivation += (msek<0 ? 0 : msek);
51 // eDebug("next Activation sec = %d, nsec = %d", nextActivation.tv_sec, nextActivation.tv_nsec );
52 context.addTimer(this);
55 void eTimer::startLongTimer( int seconds )
60 bActive = bSingleShot = true;
62 clock_gettime(CLOCK_MONOTONIC, &nextActivation);
63 // eDebug("this = %p\nnow sec = %d, nsec = %d\nadd %d sec", this, nextActivation.tv_sec, nextActivation.tv_nsec, seconds);
65 nextActivation.tv_sec += seconds;
66 // eDebug("next Activation sec = %d, nsec = %d", nextActivation.tv_sec, nextActivation.tv_nsec );
67 context.addTimer(this);
75 context.removeTimer(this);
79 void eTimer::changeInterval(long msek)
81 if (bActive) // Timer is running?
83 context.removeTimer(this); // then stop
84 nextActivation -= interval; // sub old interval
87 bActive=true; // then activate Timer
89 interval = msek; // set new Interval
90 nextActivation += interval; // calc nextActivation
92 context.addTimer(this); // add Timer to context TimerList
95 void eTimer::activate() // Internal Funktion... called from eApplication
97 context.removeTimer(this);
101 nextActivation += interval;
102 context.addTimer(this);
111 ePtrList<eMainloop> eMainloop::existing_loops;
113 eMainloop::~eMainloop()
115 existing_loops.remove(this);
116 for (std::map<int, eSocketNotifier*>::iterator it(notifiers.begin());it != notifiers.end();++it)
118 while(m_timer_list.begin() != m_timer_list.end())
119 m_timer_list.begin()->stop();
122 void eMainloop::addSocketNotifier(eSocketNotifier *sn)
124 int fd = sn->getFD();
125 ASSERT(notifiers.find(fd) == notifiers.end());
129 void eMainloop::removeSocketNotifier(eSocketNotifier *sn)
131 int fd = sn->getFD();
132 std::map<int,eSocketNotifier*>::iterator i(notifiers.find(fd));
133 if (i != notifiers.end())
134 return notifiers.erase(i);
135 eFatal("removed socket notifier which is not present");
138 int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePyObject additional)
140 int return_reason = 0;
141 /* get current time */
143 if (additional && !PyDict_Check(additional))
144 eFatal("additional, but it's not dict");
146 if (additional && !res)
147 eFatal("additional, but no res");
149 long poll_timeout = -1; /* infinite in case of empty timer list */
151 if (!m_timer_list.empty())
153 /* process all timers which are ready. first remove them out of the list. */
154 while (!m_timer_list.empty() && (poll_timeout = timeout_usec( m_timer_list.begin()->getNextActivation() ) ) <= 0 )
155 m_timer_list.begin()->activate();
156 if (poll_timeout < 0)
158 else /* convert us to ms */
159 poll_timeout /= 1000;
162 if ((twisted_timeout > 0) && (poll_timeout > 0) && ((unsigned int)poll_timeout > twisted_timeout))
164 poll_timeout = twisted_timeout;
168 int nativecount=notifiers.size(),
173 fdcount += PyDict_Size(additional);
175 // build the poll aray
176 pollfd pfd[fdcount]; // make new pollfd array
177 std::map<int,eSocketNotifier*>::iterator it = notifiers.begin();
179 for (; i < nativecount; ++i, ++it)
181 it->second->state = 1; // running and in poll
182 pfd[i].fd = it->first;
183 pfd[i].events = it->second->getRequested();
188 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
189 typedef int Py_ssize_t;
190 # define PY_SSIZE_T_MAX INT_MAX
191 # define PY_SSIZE_T_MIN INT_MIN
195 while (PyDict_Next(additional, &pos, &key, &val)) {
196 pfd[i].fd = PyObject_AsFileDescriptor(key);
197 pfd[i++].events = PyInt_AsLong(val);
207 op.opcode = gOpcode::flush;
208 gRC::getInstance()->submit(op);
209 Py_BEGIN_ALLOW_THREADS
210 ret = ::poll(pfd, fdcount, poll_timeout);
214 ret = ::poll(pfd, fdcount, poll_timeout);
218 /* ret > 0 means that there are some active poll entries. */
223 for (; i < nativecount; ++i)
227 it = notifiers.find(pfd[i].fd);
228 if (it != notifiers.end()
229 && it->second->state == 1) // added and in poll
231 int req = it->second->getRequested();
232 if (pfd[i].revents & req)
233 it->second->activate(pfd[i].revents & req);
234 pfd[i].revents &= ~req;
236 if (pfd[i].revents & (POLLERR|POLLHUP|POLLNVAL))
237 eDebug("poll: unhandled POLLERR/HUP/NVAL for fd %d(%d)", pfd[i].fd, pfd[i].revents);
240 for (; i < fdcount; ++i)
245 *res = PyList_New(0);
246 ePyObject it = PyTuple_New(2);
247 PyTuple_SET_ITEM(it, 0, PyInt_FromLong(pfd[i].fd));
248 PyTuple_SET_ITEM(it, 1, PyInt_FromLong(pfd[i].revents));
249 PyList_Append(*res, it);
256 /* when we got a signal, we get EINTR. */
258 eDebug("poll made error (%m)");
260 return_reason = 2; /* don't assume the timeout has passed when we got a signal */
263 return return_reason;
266 void eMainloop::addTimer(eTimer* e)
268 m_timer_list.insert_in_order(e);
271 void eMainloop::removeTimer(eTimer* e)
273 m_timer_list.remove(e);
276 int eMainloop::iterate(unsigned int twisted_timeout, PyObject **res, ePyObject dict)
282 clock_gettime(CLOCK_MONOTONIC, &m_twisted_timer);
283 m_twisted_timer += twisted_timeout;
286 /* TODO: this code just became ugly. fix that. */
289 if (m_interrupt_requested)
291 m_interrupt_requested = 0;
301 timespec now, timeout;
302 clock_gettime(CLOCK_MONOTONIC, &now);
303 if (m_twisted_timer<=now) // timeout
305 timeout = m_twisted_timer - now;
306 to = timeout.tv_sec * 1000 + timeout.tv_nsec / 1000000;
308 ret = processOneEvent(to, res, dict);
309 } while ( !ret && !(res && *res) );
314 int eMainloop::runLoop()
316 while (!app_quit_now)
321 void eMainloop::reset()
326 PyObject *eMainloop::poll(ePyObject timeout, ePyObject dict)
333 int twisted_timeout = (timeout == Py_None) ? 0 : PyInt_AsLong(timeout);
335 iterate(twisted_timeout, &res, dict);
339 return PyList_New(0); /* return empty list on timeout */
342 void eMainloop::interruptPoll()
344 m_interrupt_requested = 1;
347 void eMainloop::quit(int ret)
353 eApplication* eApp = 0;
355 #include "structmember.h"
359 // eTimer replacement
365 PyObject *in_weakreflist; /* List of weak references */
369 eTimerPy_traverse(eTimerPy *self, visitproc visit, void *arg)
371 PyObject *obj = self->tm->timeout.get();
377 eTimerPy_clear(eTimerPy *self)
379 PyObject *obj = self->tm->timeout.get();
385 eTimerPy_dealloc(eTimerPy* self)
387 if (self->in_weakreflist != NULL)
388 PyObject_ClearWeakRefs((PyObject *) self);
389 eTimerPy_clear(self);
391 self->ob_type->tp_free((PyObject*)self);
395 eTimerPy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
397 eTimerPy *self = (eTimerPy *)type->tp_alloc(type, 0);
398 self->tm = new eTimer(eApp);
399 self->in_weakreflist = NULL;
400 return (PyObject *)self;
404 eTimerPy_is_active(eTimerPy* self)
406 PyObject *ret = NULL;
407 ret = self->tm->isActive() ? Py_True : Py_False;
413 eTimerPy_start(eTimerPy* self, PyObject *args)
417 if (PyTuple_Size(args) > 1)
419 if (!PyArg_ParseTuple(args, "ll", &v, &singleShot)) // when 2nd arg is a value
422 if (!PyArg_ParseTuple(args, "lO", &v, &obj)) // get 2nd arg as python object
424 else if (obj == Py_True)
426 else if (obj != Py_False)
430 else if (!PyArg_ParseTuple(args, "l", &v))
432 self->tm->start(v, singleShot);
437 eTimerPy_start_long(eTimerPy* self, PyObject *args)
440 if (!PyArg_ParseTuple(args, "l", &v)) {
443 self->tm->startLongTimer(v);
448 eTimerPy_change_interval(eTimerPy* self, PyObject *args)
451 if (!PyArg_ParseTuple(args, "l", &v)) {
454 self->tm->changeInterval(v);
459 eTimerPy_stop(eTimerPy* self)
466 eTimerPy_get_callback_list(eTimerPy *self)
467 { //used for compatibilty with the old eTimer
468 return self->tm->timeout.get();
471 static PyMethodDef eTimerPy_methods[] = {
472 {"isActive", (PyCFunction)eTimerPy_is_active, METH_NOARGS,
473 "returns the timer state"
475 {"start", (PyCFunction)eTimerPy_start, METH_VARARGS,
476 "start timer with interval in msecs"
478 {"startLongTimer", (PyCFunction)eTimerPy_start_long, METH_VARARGS,
479 "start timer with interval in secs"
481 {"changeInterval", (PyCFunction)eTimerPy_change_interval, METH_VARARGS,
482 "change interval of a timer (in msecs)"
484 {"stop", (PyCFunction)eTimerPy_stop, METH_NOARGS,
487 //used for compatibilty with the old eTimer
488 {"get", (PyCFunction)eTimerPy_get_callback_list, METH_NOARGS,
489 "get timeout callback list"
491 {NULL} /* Sentinel */
495 eTimerPy_get_cb_list(eTimerPy *self, void *closure)
497 return self->tm->timeout.get();
501 eTimerPy_timeout(eTimerPy *self, void *closure)
502 { //used for compatibilty with the old eTimer
503 Org_Py_INCREF((PyObject*)self);
504 return (PyObject*)self;
507 static PyGetSetDef eTimerPy_getseters[] = {
509 (getter)eTimerPy_get_cb_list, (setter)0,
510 "returns the callback python list",
513 {"timeout", //used for compatibilty with the old eTimer
514 (getter)eTimerPy_timeout, (setter)0,
515 "synonym for our self",
518 {NULL} /* Sentinel */
521 static PyTypeObject eTimerPyType = {
522 PyObject_HEAD_INIT(NULL)
524 "eBaseImpl.eTimer", /*tp_name*/
525 sizeof(eTimerPy), /*tp_basicsize*/
527 (destructor)eTimerPy_dealloc, /*tp_dealloc*/
534 0, /*tp_as_sequence*/
542 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
543 "eTimer objects", /* tp_doc */
544 (traverseproc)eTimerPy_traverse, /* tp_traverse */
545 (inquiry)eTimerPy_clear, /* tp_clear */
546 0, /* tp_richcompare */
547 offsetof(eTimerPy, in_weakreflist), /* tp_weaklistoffset */
550 eTimerPy_methods, /* tp_methods */
552 eTimerPy_getseters, /* tp_getset */
555 0, /* tp_descr_get */
556 0, /* tp_descr_set */
557 0, /* tp_dictoffset */
560 eTimerPy_new, /* tp_new */
563 // eSocketNotifier replacement
565 struct eSocketNotifierPy
569 PyObject *in_weakreflist; /* List of weak references */
573 eSocketNotifierPy_traverse(eSocketNotifierPy *self, visitproc visit, void *arg)
575 PyObject *obj = self->sn->activated.get();
581 eSocketNotifierPy_clear(eSocketNotifierPy *self)
583 PyObject *obj = self->sn->activated.get();
589 eSocketNotifierPy_dealloc(eSocketNotifierPy* self)
591 if (self->in_weakreflist != NULL)
592 PyObject_ClearWeakRefs((PyObject *) self);
593 eSocketNotifierPy_clear(self);
595 self->ob_type->tp_free((PyObject*)self);
599 eSocketNotifierPy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
601 eSocketNotifierPy *self = (eSocketNotifierPy *)type->tp_alloc(type, 0);
602 int fd, req, immediate_start = 1, size = PyTuple_Size(args);
605 if (!PyArg_ParseTuple(args, "iii", &fd, &req, &immediate_start))
607 PyObject *obj = NULL;
608 if (!PyArg_ParseTuple(args, "iiO", &fd, &req, &immediate_start))
612 else if (obj != Py_True)
616 else if (size < 2 || !PyArg_ParseTuple(args, "ii", &fd, &req))
618 self->sn = new eSocketNotifier(eApp, fd, req, immediate_start);
619 self->in_weakreflist = NULL;
620 return (PyObject *)self;
624 eSocketNotifierPy_is_running(eSocketNotifierPy* self)
626 PyObject *ret = self->sn->isRunning() ? Py_True : Py_False;
632 eSocketNotifierPy_start(eSocketNotifierPy* self)
639 eSocketNotifierPy_stop(eSocketNotifierPy* self)
646 eSocketNotifierPy_get_fd(eSocketNotifierPy* self)
648 return PyInt_FromLong(self->sn->getFD());
652 eSocketNotifierPy_get_requested(eSocketNotifierPy* self)
654 return PyInt_FromLong(self->sn->getRequested());
658 eSocketNotifierPy_set_requested(eSocketNotifierPy* self, PyObject *args)
661 if (PyTuple_Size(args) != 1 || !PyArg_ParseTuple(args, "i", &req))
663 self->sn->setRequested(req);
667 static PyMethodDef eSocketNotifierPy_methods[] = {
668 {"isRunning", (PyCFunction)eSocketNotifierPy_is_running, METH_NOARGS,
669 "returns the running state"
671 {"start", (PyCFunction)eSocketNotifierPy_start, METH_NOARGS,
674 {"stop", (PyCFunction)eSocketNotifierPy_stop, METH_NOARGS,
677 {"getFD", (PyCFunction)eSocketNotifierPy_get_fd, METH_NOARGS,
678 "get file descriptor"
680 {"getRequested", (PyCFunction)eSocketNotifierPy_get_requested, METH_NOARGS,
683 {"setRequested", (PyCFunction)eSocketNotifierPy_set_requested, METH_VARARGS,
686 {NULL} /* Sentinel */
690 eSocketNotifierPy_get_cb_list(eSocketNotifierPy *self, void *closure)
692 return self->sn->activated.get();
695 static PyGetSetDef eSocketNotifierPy_getseters[] = {
697 (getter)eSocketNotifierPy_get_cb_list, (setter)0,
698 "returns the callback python list",
700 {NULL} /* Sentinel */
703 static PyTypeObject eSocketNotifierPyType = {
704 PyObject_HEAD_INIT(NULL)
706 "eBaseImpl.eSocketNotifier", /*tp_name*/
707 sizeof(eSocketNotifierPy), /*tp_basicsize*/
709 (destructor)eSocketNotifierPy_dealloc, /*tp_dealloc*/
716 0, /*tp_as_sequence*/
724 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
725 "eTimer objects", /* tp_doc */
726 (traverseproc)eSocketNotifierPy_traverse, /* tp_traverse */
727 (inquiry)eSocketNotifierPy_clear, /* tp_clear */
728 0, /* tp_richcompare */
729 offsetof(eSocketNotifierPy, in_weakreflist), /* tp_weaklistoffset */
732 eSocketNotifierPy_methods, /* tp_methods */
734 eSocketNotifierPy_getseters, /* tp_getset */
737 0, /* tp_descr_get */
738 0, /* tp_descr_set */
739 0, /* tp_dictoffset */
742 eSocketNotifierPy_new, /* tp_new */
745 static PyMethodDef module_methods[] = {
746 {NULL} /* Sentinel */
753 m = Py_InitModule3("eBaseImpl", module_methods,
754 "Module that implements some enigma classes with working cyclic garbage collection.");
759 if (!PyType_Ready(&eTimerPyType))
761 Org_Py_INCREF((PyObject*)&eTimerPyType);
762 PyModule_AddObject(m, "eTimer", (PyObject*)&eTimerPyType);
764 if (!PyType_Ready(&eSocketNotifierPyType))
766 Org_Py_INCREF((PyObject*)&eSocketNotifierPyType);
767 PyModule_AddObject(m, "eSocketNotifier", (PyObject*)&eSocketNotifierPyType);