11 #include <lib/base/eptrlist.h>
12 #include <lib/python/connections.h>
13 #include <libsig_comp.h>
17 extern eApplication* eApp;
20 /* TODO: remove these inlines. */
21 static inline bool operator<( const timeval &t1, const timeval &t2 )
23 return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_usec < t2.tv_usec);
26 static inline bool operator<=( const timeval &t1, const timeval &t2 )
28 return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_usec <= t2.tv_usec);
31 static inline timeval &operator+=( timeval &t1, const timeval &t2 )
33 t1.tv_sec += t2.tv_sec;
34 if ( (t1.tv_usec += t2.tv_usec) >= 1000000 )
37 t1.tv_usec -= 1000000;
42 static inline timeval operator+( const timeval &t1, const timeval &t2 )
45 tmp.tv_sec = t1.tv_sec + t2.tv_sec;
46 if ( (tmp.tv_usec = t1.tv_usec + t2.tv_usec) >= 1000000 )
49 tmp.tv_usec -= 1000000;
54 static inline timeval operator-( const timeval &t1, const timeval &t2 )
57 tmp.tv_sec = t1.tv_sec - t2.tv_sec;
58 if ( (tmp.tv_usec = t1.tv_usec - t2.tv_usec) < 0 )
61 tmp.tv_usec += 1000000;
66 static inline timeval operator-=( timeval &t1, const timeval &t2 )
68 t1.tv_sec -= t2.tv_sec;
69 if ( (t1.tv_usec -= t2.tv_usec) < 0 )
72 t1.tv_usec += 1000000;
77 static inline timeval &operator+=( timeval &t1, const long msek )
79 t1.tv_sec += msek / 1000;
80 if ( (t1.tv_usec += (msek % 1000) * 1000) >= 1000000 )
83 t1.tv_usec -= 1000000;
88 static inline timeval operator+( const timeval &t1, const long msek )
91 tmp.tv_sec = t1.tv_sec + msek / 1000;
92 if ( (tmp.tv_usec = t1.tv_usec + (msek % 1000) * 1000) >= 1000000 )
95 tmp.tv_usec -= 1000000;
100 static inline timeval operator-( const timeval &t1, const long msek )
103 tmp.tv_sec = t1.tv_sec - msek / 1000;
104 if ( (tmp.tv_usec = t1.tv_usec - (msek % 1000)*1000) < 0 )
107 tmp.tv_usec += 1000000;
112 static inline timeval operator-=( timeval &t1, const long msek )
114 t1.tv_sec -= msek / 1000;
115 if ( (t1.tv_usec -= (msek % 1000) * 1000) < 0 )
118 t1.tv_usec += 1000000;
123 static inline int timeval_to_usec(const timeval &t1)
125 return t1.tv_sec*1000000 + t1.tv_usec;
131 // die beiden signalquellen: SocketNotifier...
134 * \brief Gives a callback when data on a file descriptor is ready.
136 * This class emits the signal \c eSocketNotifier::activate whenever the
137 * event specified by \c req is available.
139 class eSocketNotifier
142 enum { Read=POLLIN, Write=POLLOUT, Priority=POLLPRI, Error=POLLERR, Hungup=POLLHUP };
147 int requested; // requested events (POLLIN, ...)
150 * \brief Constructs a eSocketNotifier.
151 * \param context The thread where to bind the socketnotifier to. The signal is emitted from that thread.
152 * \param fd The filedescriptor to monitor. Can be a device or a socket.
153 * \param req The events to watch to, normally either \c Read or \c Write. You can specify any events that \c poll supports.
154 * \param startnow Specifies if the socketnotifier should start immediately.
156 eSocketNotifier(eMainloop *context, int fd, int req, bool startnow=true);
159 PSignal1<void, int> activated;
160 void activate(int what) { /*emit*/ activated(what); }
164 bool isRunning() { return state; }
166 int getFD() { return fd; }
167 int getRequested() { return requested; }
168 void setRequested(int req) { requested=req; }
173 // werden in einer mainloop verarbeitet
177 friend class eSocketNotifier;
178 std::map<int, eSocketNotifier*> notifiers, new_notifiers;
179 ePtrList<eTimer> m_timer_list;
182 int processOneEvent(unsigned int user_timeout, PyObject **res=0, ePyObject additional=ePyObject());
184 pthread_mutex_t recalcLock;
186 int m_now_is_invalid;
187 int m_interrupt_requested;
188 void addSocketNotifier(eSocketNotifier *sn);
189 void removeSocketNotifier(eSocketNotifier *sn);
190 void addTimer(eTimer* e);
191 void removeTimer(eTimer* e);
193 static void addTimeOffset(int offset);
196 static ePtrList<eMainloop> existing_loops;
200 :app_quit_now(0),loop_level(0),retval(0), m_interrupt_requested(0)
202 m_now_is_invalid = 0;
203 existing_loops.push_back(this);
204 pthread_mutex_init(&recalcLock, 0);
208 existing_loops.remove(this);
209 pthread_mutex_destroy(&recalcLock);
211 int looplevel() { return loop_level; }
214 void quit(int ret=0); // leave all pending loops (recursive leave())
217 /* a user supplied timeout. enter_loop will return with:
218 0 - no timeout, no signal
222 int iterate(unsigned int timeout=0, PyObject **res=0, SWIG_PYOBJECT(ePyObject) additional=(PyObject*)0);
224 /* run will iterate endlessly until the app is quit, and return
228 /* our new shared polling interface. */
229 PyObject *poll(SWIG_PYOBJECT(ePyObject) dict, SWIG_PYOBJECT(ePyObject) timeout);
230 void interruptPoll();
235 * \brief The application class.
237 * An application provides a mainloop, and runs in the primary thread.
238 * You can have other threads, too, but this is the primary one.
240 class eApplication: public eMainloop
256 * \brief Gives a callback after a specified timeout.
258 * This class emits the signal \c eTimer::timeout after the specified timeout.
262 friend class eMainloop;
264 timeval nextActivation;
268 void addTimeOffset(int);
271 * \brief Constructs a timer.
273 * The timer is not yet active, it has to be started with \c start.
274 * \param context The thread from which the signal should be emitted.
276 eTimer(eMainloop *context=eApp): context(*context), bActive(false) { }
277 ~eTimer() { if (bActive) stop(); }
279 PSignal0<void> timeout;
282 bool isActive() { return bActive; }
283 timeval &getNextActivation() { return nextActivation; }
285 void start(long msec, bool b=false);
287 void changeInterval(long msek);
289 bool operator<(const eTimer& t) const { return nextActivation < t.nextActivation; }
291 void startLongTimer( int seconds );