12 #include <lib/base/eptrlist.h>
13 #include <libsig_comp.h>
16 #include <lib/python/connections.h>
20 extern eApplication* eApp;
23 /* TODO: remove these inlines. */
24 static inline bool operator<( const timespec &t1, const timespec &t2 )
26 return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec);
29 static inline bool operator<=( const timespec &t1, const timespec &t2 )
31 return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_nsec <= t2.tv_nsec);
34 static inline timespec &operator+=( timespec &t1, const timespec &t2 )
36 t1.tv_sec += t2.tv_sec;
37 if ( (t1.tv_nsec += t2.tv_nsec) >= 1000000000 )
40 t1.tv_nsec -= 1000000000;
45 static inline timespec operator+( const timespec &t1, const timespec &t2 )
48 tmp.tv_sec = t1.tv_sec + t2.tv_sec;
49 if ( (tmp.tv_nsec = t1.tv_nsec + t2.tv_nsec) >= 1000000000 )
52 tmp.tv_nsec -= 1000000000;
57 static inline timespec operator-( const timespec &t1, const timespec &t2 )
60 tmp.tv_sec = t1.tv_sec - t2.tv_sec;
61 if ( (tmp.tv_nsec = t1.tv_nsec - t2.tv_nsec) < 0 )
64 tmp.tv_nsec += 1000000000;
69 static inline timespec operator-=( timespec &t1, const timespec &t2 )
71 t1.tv_sec -= t2.tv_sec;
72 if ( (t1.tv_nsec -= t2.tv_nsec) < 0 )
75 t1.tv_nsec += 1000000000;
80 static inline timespec &operator+=( timespec &t1, const long msek )
82 t1.tv_sec += msek / 1000;
83 if ( (t1.tv_nsec += (msek % 1000) * 1000000) >= 1000000000 )
86 t1.tv_nsec -= 1000000000;
91 static inline timespec operator+( const timespec &t1, const long msek )
94 tmp.tv_sec = t1.tv_sec + msek / 1000;
95 if ( (tmp.tv_nsec = t1.tv_nsec + (msek % 1000) * 1000000) >= 1000000000 )
98 tmp.tv_nsec -= 1000000;
103 static inline timespec operator-( const timespec &t1, const long msek )
106 tmp.tv_sec = t1.tv_sec - msek / 1000;
107 if ( (tmp.tv_nsec = t1.tv_nsec - (msek % 1000)*1000000) < 0 )
110 tmp.tv_nsec += 1000000000;
115 static inline timespec operator-=( timespec &t1, const long msek )
117 t1.tv_sec -= msek / 1000;
118 if ( (t1.tv_nsec -= (msek % 1000) * 1000000) < 0 )
121 t1.tv_nsec += 1000000000;
126 static inline long timeout_usec ( const timespec & orig )
129 clock_gettime(CLOCK_MONOTONIC, &now);
130 if ( (orig-now).tv_sec > 2000 )
131 return 2000*1000*1000;
132 return (orig-now).tv_sec*1000000 + (orig-now).tv_nsec/1000;
137 // die beiden signalquellen: SocketNotifier...
140 * \brief Gives a callback when data on a file descriptor is ready.
142 * This class emits the signal \c eSocketNotifier::activate whenever the
143 * event specified by \c req is available.
145 class eSocketNotifier
147 friend class eMainloop;
149 enum { Read=POLLIN, Write=POLLOUT, Priority=POLLPRI, Error=POLLERR, Hungup=POLLHUP };
154 int requested; // requested events (POLLIN, ...)
155 void activate(int what) { /*emit*/ activated(what); }
158 * \brief Constructs a eSocketNotifier.
159 * \param context The thread where to bind the socketnotifier to. The signal is emitted from that thread.
160 * \param fd The filedescriptor to monitor. Can be a device or a socket.
161 * \param req The events to watch to, normally either \c Read or \c Write. You can specify any events that \c poll supports.
162 * \param startnow Specifies if the socketnotifier should start immediately.
164 eSocketNotifier(eMainloop *context, int fd, int req, bool startnow=true);
167 PSignal1<void, int> activated;
171 bool isRunning() { return state; }
173 int getFD() { return fd; }
174 int getRequested() { return requested; }
175 void setRequested(int req) { requested=req; }
182 // werden in einer mainloop verarbeitet
186 friend class eSocketNotifier;
187 std::map<int, eSocketNotifier*> notifiers;
188 ePtrList<eTimer> m_timer_list;
191 int processOneEvent(unsigned int user_timeout, PyObject **res=0, ePyObject additional=ePyObject());
195 int m_interrupt_requested;
196 timespec m_twisted_timer; // twisted timer
198 void addSocketNotifier(eSocketNotifier *sn);
199 void removeSocketNotifier(eSocketNotifier *sn);
200 void addTimer(eTimer* e);
201 void removeTimer(eTimer* e);
204 static ePtrList<eMainloop> existing_loops;
208 :app_quit_now(0),loop_level(0),retval(0), m_is_idle(0), m_interrupt_requested(0)
210 existing_loops.push_back(this);
212 virtual ~eMainloop();
214 int looplevel() { return loop_level; }
217 void quit(int ret=0); // leave all pending loops (recursive leave())
220 /* a user supplied timeout. enter_loop will return with:
221 0 - no timeout, no signal
225 int iterate(unsigned int timeout=0, PyObject **res=0, SWIG_PYOBJECT(ePyObject) additional=(PyObject*)0);
227 /* run will iterate endlessly until the app is quit, and return
231 /* our new shared polling interface. */
232 PyObject *poll(SWIG_PYOBJECT(ePyObject) dict, SWIG_PYOBJECT(ePyObject) timeout);
233 void interruptPoll();
236 /* m_is_idle needs to be atomic, but it doesn't really matter much, as it's read-only from outside */
237 int isIdle() { return m_is_idle; }
241 * \brief The application class.
243 * An application provides a mainloop, and runs in the primary thread.
244 * You can have other threads, too, but this is the primary one.
246 class eApplication: public eMainloop
263 * \brief Gives a callback after a specified timeout.
265 * This class emits the signal \c eTimer::timeout after the specified timeout.
269 friend class eMainloop;
271 timespec nextActivation;
278 * \brief Constructs a timer.
280 * The timer is not yet active, it has to be started with \c start.
281 * \param context The thread from which the signal should be emitted.
283 eTimer(eMainloop *context=eApp): context(*context), bActive(false) { }
284 ~eTimer() { if (bActive) stop(); }
286 PSignal0<void> timeout;
288 bool isActive() { return bActive; }
290 timespec &getNextActivation() { return nextActivation; }
292 void start(long msec, bool b=false);
294 void changeInterval(long msek);
295 void startLongTimer( int seconds );
296 bool operator<(const eTimer& t) const { return nextActivation < t.nextActivation; }