11 #include <lib/base/eptrlist.h>
12 #include <lib/python/connections.h>
13 #include <libsig_comp.h>
17 extern eApplication* eApp;
19 static inline bool operator<( const timeval &t1, const timeval &t2 )
21 return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_usec < t2.tv_usec);
24 static inline timeval &operator+=( timeval &t1, const timeval &t2 )
26 t1.tv_sec += t2.tv_sec;
27 if ( (t1.tv_usec += t2.tv_usec) >= 1000000 )
30 t1.tv_usec -= 1000000;
35 static inline timeval operator+( const timeval &t1, const timeval &t2 )
38 tmp.tv_sec = t1.tv_sec + t2.tv_sec;
39 if ( (tmp.tv_usec = t1.tv_usec + t2.tv_usec) >= 1000000 )
42 tmp.tv_usec -= 1000000;
47 static inline timeval operator-( const timeval &t1, const timeval &t2 )
50 tmp.tv_sec = t1.tv_sec - t2.tv_sec;
51 if ( (tmp.tv_usec = t1.tv_usec - t2.tv_usec) < 0 )
54 tmp.tv_usec += 1000000;
59 static inline timeval operator-=( timeval &t1, const timeval &t2 )
61 t1.tv_sec -= t2.tv_sec;
62 if ( (t1.tv_usec -= t2.tv_usec) < 0 )
65 t1.tv_usec += 1000000;
70 static inline timeval &operator+=( timeval &t1, const long msek )
72 t1.tv_sec += msek / 1000;
73 if ( (t1.tv_usec += (msek % 1000) * 1000) >= 1000000 )
76 t1.tv_usec -= 1000000;
81 static inline timeval operator+( const timeval &t1, const long msek )
84 tmp.tv_sec = t1.tv_sec + msek / 1000;
85 if ( (tmp.tv_usec = t1.tv_usec + (msek % 1000) * 1000) >= 1000000 )
88 tmp.tv_usec -= 1000000;
93 static inline timeval operator-( const timeval &t1, const long msek )
96 tmp.tv_sec = t1.tv_sec - msek / 1000;
97 if ( (tmp.tv_usec = t1.tv_usec - (msek % 1000)*1000) < 0 )
100 tmp.tv_usec += 1000000;
105 static inline timeval operator-=( timeval &t1, const long msek )
107 t1.tv_sec -= msek / 1000;
108 if ( (t1.tv_usec -= (msek % 1000) * 1000) < 0 )
111 t1.tv_usec += 1000000;
116 static inline timeval timeout_timeval ( const timeval & orig )
119 gettimeofday(&now,0);
124 static inline long timeout_usec ( const timeval & orig )
127 gettimeofday(&now,0);
129 return (orig-now).tv_sec*1000000 + (orig-now).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
145 enum { Read=POLLIN, Write=POLLOUT, Priority=POLLPRI, Error=POLLERR, Hungup=POLLHUP };
150 int requested; // requested events (POLLIN, ...)
153 * \brief Constructs a eSocketNotifier.
154 * \param context The thread where to bind the socketnotifier to. The signal is emitted from that thread.
155 * \param fd The filedescriptor to monitor. Can be a device or a socket.
156 * \param req The events to watch to, normally either \c Read or \c Write. You can specify any events that \c poll supports.
157 * \param startnow Specifies if the socketnotifier should start immediately.
159 eSocketNotifier(eMainloop *context, int fd, int req, bool startnow=true);
162 PSignal1<void, int> activated;
163 void activate(int what) { /*emit*/ activated(what); }
167 bool isRunning() { return state; }
169 int getFD() { return fd; }
170 int getRequested() { return requested; }
171 void setRequested(int req) { requested=req; }
176 // werden in einer mainloop verarbeitet
180 friend class eSocketNotifier;
181 std::map<int, eSocketNotifier*> notifiers;
182 ePtrList<eTimer> TimerList;
186 void processOneEvent();
189 pthread_mutex_t recalcLock;
190 inline void doRecalcTimers();
191 inline void addSocketNotifier(eSocketNotifier *sn);
192 inline void removeSocketNotifier(eSocketNotifier *sn);
193 inline void addTimer(eTimer* e) { TimerList.insert_in_order(e); }
194 inline void removeTimer(eTimer* e) { TimerList.remove(e); }
196 static ePtrList<eMainloop> existing_loops;
198 :app_quit_now(0),loop_level(0),retval(0),timer_offset(0)
200 existing_loops.push_back(this);
201 pthread_mutex_init(&recalcLock, 0);
205 existing_loops.remove(this);
206 pthread_mutex_destroy(&recalcLock);
208 int looplevel() { return loop_level; }
210 int exec(); // recursive enter the loop
211 void quit(int ret=0); // leave all pending loops (recursive leave())
214 void setTimerOffset( int );
215 int getTimerOffset() { return timer_offset; }
216 bool isAppQuitNowSet() { return app_quit_now; }
220 * \brief The application class.
222 * An application provides a mainloop, and runs in the primary thread.
223 * You can have other threads, too, but this is the primary one.
225 class eApplication: public eMainloop
241 * \brief Gives a callback after a specified timeout.
243 * This class emits the signal \c eTimer::timeout after the specified timeout.
247 friend class eMainloop;
249 timeval nextActivation;
253 inline void recalc(int);
256 * \brief Constructs a timer.
258 * The timer is not yet active, it has to be started with \c start.
259 * \param context The thread from which the signal should be emitted.
261 eTimer(eMainloop *context=eApp): context(*context), bActive(false) { }
262 ~eTimer() { if (bActive) stop(); }
264 PSignal0<void> timeout;
267 bool isActive() { return bActive; }
268 timeval &getNextActivation() { return nextActivation; }
270 void start(long msec, bool b=false);
272 void changeInterval(long msek);
273 bool operator<(const eTimer& t) const { return nextActivation < t.nextActivation; }
274 void startLongTimer( int seconds );