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 timeval &t1, const timeval &t2 )
26 return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_usec < t2.tv_usec);
29 static inline bool operator<=( const timeval &t1, const timeval &t2 )
31 return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_usec <= t2.tv_usec);
34 static inline timeval &operator+=( timeval &t1, const timeval &t2 )
36 t1.tv_sec += t2.tv_sec;
37 if ( (t1.tv_usec += t2.tv_usec) >= 1000000 )
40 t1.tv_usec -= 1000000;
45 static inline timeval operator+( const timeval &t1, const timeval &t2 )
48 tmp.tv_sec = t1.tv_sec + t2.tv_sec;
49 if ( (tmp.tv_usec = t1.tv_usec + t2.tv_usec) >= 1000000 )
52 tmp.tv_usec -= 1000000;
57 static inline timeval operator-( const timeval &t1, const timeval &t2 )
60 tmp.tv_sec = t1.tv_sec - t2.tv_sec;
61 if ( (tmp.tv_usec = t1.tv_usec - t2.tv_usec) < 0 )
64 tmp.tv_usec += 1000000;
69 static inline timeval operator-=( timeval &t1, const timeval &t2 )
71 t1.tv_sec -= t2.tv_sec;
72 if ( (t1.tv_usec -= t2.tv_usec) < 0 )
75 t1.tv_usec += 1000000;
80 static inline timeval &operator+=( timeval &t1, const long msek )
82 t1.tv_sec += msek / 1000;
83 if ( (t1.tv_usec += (msek % 1000) * 1000) >= 1000000 )
86 t1.tv_usec -= 1000000;
91 static inline timeval operator+( const timeval &t1, const long msek )
94 tmp.tv_sec = t1.tv_sec + msek / 1000;
95 if ( (tmp.tv_usec = t1.tv_usec + (msek % 1000) * 1000) >= 1000000 )
98 tmp.tv_usec -= 1000000;
103 static inline timeval operator-( const timeval &t1, const long msek )
106 tmp.tv_sec = t1.tv_sec - msek / 1000;
107 if ( (tmp.tv_usec = t1.tv_usec - (msek % 1000)*1000) < 0 )
110 tmp.tv_usec += 1000000;
115 static inline timeval operator-=( timeval &t1, const long msek )
117 t1.tv_sec -= msek / 1000;
118 if ( (t1.tv_usec -= (msek % 1000) * 1000) < 0 )
121 t1.tv_usec += 1000000;
126 static inline int timeval_to_usec(const timeval &t1)
128 return t1.tv_sec*1000000 + t1.tv_usec;
134 // die beiden signalquellen: SocketNotifier...
137 * \brief Gives a callback when data on a file descriptor is ready.
139 * This class emits the signal \c eSocketNotifier::activate whenever the
140 * event specified by \c req is available.
142 class eSocketNotifier
144 friend class eMainloop;
146 enum { Read=POLLIN, Write=POLLOUT, Priority=POLLPRI, Error=POLLERR, Hungup=POLLHUP };
151 int requested; // requested events (POLLIN, ...)
154 * \brief Constructs a eSocketNotifier.
155 * \param context The thread where to bind the socketnotifier to. The signal is emitted from that thread.
156 * \param fd The filedescriptor to monitor. Can be a device or a socket.
157 * \param req The events to watch to, normally either \c Read or \c Write. You can specify any events that \c poll supports.
158 * \param startnow Specifies if the socketnotifier should start immediately.
160 eSocketNotifier(eMainloop *context, int fd, int req, bool startnow=true);
163 PSignal1<void, int> activated;
164 void activate(int what) { /*emit*/ activated(what); }
168 bool isRunning() { return state; }
170 int getFD() { return fd; }
171 int getRequested() { return requested; }
172 void setRequested(int req) { requested=req; }
177 // werden in einer mainloop verarbeitet
181 friend class eSocketNotifier;
182 std::map<int, eSocketNotifier*> notifiers;
183 ePtrList<eTimer> m_timer_list;
186 int processOneEvent(unsigned int user_timeout, PyObject **res=0, ePyObject additional=ePyObject());
188 pthread_mutex_t recalcLock;
190 int m_now_is_invalid;
191 int m_interrupt_requested;
192 void addSocketNotifier(eSocketNotifier *sn);
193 void removeSocketNotifier(eSocketNotifier *sn);
194 void addTimer(eTimer* e);
195 void removeTimer(eTimer* e);
197 static void addTimeOffset(int offset);
200 static ePtrList<eMainloop> existing_loops;
204 :app_quit_now(0),loop_level(0),retval(0), m_interrupt_requested(0)
206 m_now_is_invalid = 0;
207 existing_loops.push_back(this);
208 pthread_mutex_init(&recalcLock, 0);
212 existing_loops.remove(this);
213 pthread_mutex_destroy(&recalcLock);
215 int looplevel() { return loop_level; }
218 void quit(int ret=0); // leave all pending loops (recursive leave())
221 /* a user supplied timeout. enter_loop will return with:
222 0 - no timeout, no signal
226 int iterate(unsigned int timeout=0, PyObject **res=0, SWIG_PYOBJECT(ePyObject) additional=(PyObject*)0);
228 /* run will iterate endlessly until the app is quit, and return
232 /* our new shared polling interface. */
233 PyObject *poll(SWIG_PYOBJECT(ePyObject) dict, SWIG_PYOBJECT(ePyObject) timeout);
234 void interruptPoll();
239 * \brief The application class.
241 * An application provides a mainloop, and runs in the primary thread.
242 * You can have other threads, too, but this is the primary one.
244 class eApplication: public eMainloop
260 * \brief Gives a callback after a specified timeout.
262 * This class emits the signal \c eTimer::timeout after the specified timeout.
266 friend class eMainloop;
268 timeval nextActivation;
272 void addTimeOffset(int);
275 * \brief Constructs a timer.
277 * The timer is not yet active, it has to be started with \c start.
278 * \param context The thread from which the signal should be emitted.
280 eTimer(eMainloop *context=eApp): context(*context), bActive(false) { }
281 ~eTimer() { if (bActive) stop(); }
283 PSignal0<void> timeout;
286 bool isActive() { return bActive; }
287 timeval &getNextActivation() { return nextActivation; }
289 void start(long msec, bool b=false);
291 void changeInterval(long msek);
293 bool operator<(const eTimer& t) const { return nextActivation < t.nextActivation; }
295 void startLongTimer( int seconds );