11 #include <lib/base/eptrlist.h>
12 #include <libsig_comp.h>
16 extern eApplication* eApp;
18 static inline bool operator<( const timeval &t1, const timeval &t2 )
20 return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_usec < t2.tv_usec);
23 static inline timeval &operator+=( timeval &t1, const timeval &t2 )
25 t1.tv_sec += t2.tv_sec;
26 if ( (t1.tv_usec += t2.tv_usec) >= 1000000 )
29 t1.tv_usec -= 1000000;
34 static inline timeval operator+( const timeval &t1, const timeval &t2 )
37 tmp.tv_sec = t1.tv_sec + t2.tv_sec;
38 if ( (tmp.tv_usec = t1.tv_usec + t2.tv_usec) >= 1000000 )
41 tmp.tv_usec -= 1000000;
46 static inline timeval operator-( const timeval &t1, const timeval &t2 )
49 tmp.tv_sec = t1.tv_sec - t2.tv_sec;
50 if ( (tmp.tv_usec = t1.tv_usec - t2.tv_usec) < 0 )
53 tmp.tv_usec += 1000000;
58 static inline timeval operator-=( timeval &t1, const timeval &t2 )
60 t1.tv_sec -= t2.tv_sec;
61 if ( (t1.tv_usec -= t2.tv_usec) < 0 )
64 t1.tv_usec += 1000000;
69 static inline timeval &operator+=( timeval &t1, const long msek )
71 t1.tv_sec += msek / 1000;
72 if ( (t1.tv_usec += (msek % 1000) * 1000) >= 1000000 )
75 t1.tv_usec -= 1000000;
80 static inline timeval operator+( const timeval &t1, const long msek )
83 tmp.tv_sec = t1.tv_sec + msek / 1000;
84 if ( (tmp.tv_usec = t1.tv_usec + (msek % 1000) * 1000) >= 1000000 )
87 tmp.tv_usec -= 1000000;
92 static inline timeval operator-( const timeval &t1, const long msek )
95 tmp.tv_sec = t1.tv_sec - msek / 1000;
96 if ( (tmp.tv_usec = t1.tv_usec - (msek % 1000)*1000) < 0 )
99 tmp.tv_usec += 1000000;
104 static inline timeval operator-=( timeval &t1, const long msek )
106 t1.tv_sec -= msek / 1000;
107 if ( (t1.tv_usec -= (msek % 1000) * 1000) < 0 )
110 t1.tv_usec += 1000000;
115 static inline timeval timeout_timeval ( const timeval & orig )
118 gettimeofday(&now,0);
123 static inline long timeout_usec ( const timeval & orig )
126 gettimeofday(&now,0);
128 return (orig-now).tv_sec*1000000 + (orig-now).tv_usec;
133 // die beiden signalquellen: SocketNotifier...
136 * \brief Gives a callback when data on a file descriptor is ready.
138 * This class emits the signal \c eSocketNotifier::activate whenever the
139 * event specified by \c req is available.
141 class eSocketNotifier
144 enum { Read=POLLIN, Write=POLLOUT, Priority=POLLPRI, Error=POLLERR, Hungup=POLLHUP };
149 int requested; // requested events (POLLIN, ...)
152 * \brief Constructs a eSocketNotifier.
153 * \param context The thread where to bind the socketnotifier to. The signal is emitted from that thread.
154 * \param fd The filedescriptor to monitor. Can be a device or a socket.
155 * \param req The events to watch to, normally either \c Read or \c Write. You can specify any events that \c poll supports.
156 * \param startnow Specifies if the socketnotifier should start immediately.
158 eSocketNotifier(eMainloop *context, int fd, int req, bool startnow=true);
161 Signal1<void, int> activated;
162 void activate(int what) { /*emit*/ activated(what); }
166 bool isRunning() { return state; }
168 int getFD() { return fd; }
169 int getRequested() { return requested; }
170 void setRequested(int req) { requested=req; }
175 * \brief Gives a callback after a specified timeout.
177 * This class emits the signal \c eTimer::timeout after the specified timeout.
182 timeval nextActivation;
188 * \brief Constructs a timer.
190 * The timer is not yet active, it has to be started with \c start.
191 * \param context The thread from which the signal should be emitted.
193 eTimer(eMainloop *context): context(*context), bActive(false) { }
194 ~eTimer() { if (bActive) stop(); }
196 Signal0<void> timeout;
199 bool isActive() { return bActive; }
200 timeval &getNextActivation() { return nextActivation; }
202 void start(long msec, bool singleShot=false);
204 void changeInterval(long msek);
205 bool operator<(const eTimer& t) const { return nextActivation < t.nextActivation; }
208 // werden in einer mainloop verarbeitet
211 std::map<int, eSocketNotifier*> notifiers;
212 ePtrList<eTimer> TimerList;
216 void processOneEvent();
219 eMainloop():app_quit_now(0),loop_level(0),retval(0){ }
220 void addSocketNotifier(eSocketNotifier *sn);
221 void removeSocketNotifier(eSocketNotifier *sn);
222 void addTimer(eTimer* e) { TimerList.push_back(e); TimerList.sort(); }
223 void removeTimer(eTimer* e) { TimerList.remove(e); }
225 int looplevel() { return loop_level; }
227 int exec(); // recursive enter the loop
228 void quit(int ret=0); // leave all pending loops (recursive leave())
234 * \brief The application class.
236 * An application provides a mainloop, and runs in the primary thread.
237 * You can have other threads, too, but this is the primary one.
239 class eApplication: public eMainloop