11 #include <lib/base/eptrlist.h>
12 #include <lib/python/connections.h>
13 #include <libsig_comp.h>
17 extern eApplication* eApp;
19 /* TODO: remove these inlines. */
20 static inline bool operator<( const timeval &t1, const timeval &t2 )
22 return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_usec < t2.tv_usec);
25 static inline bool operator<=( const timeval &t1, const timeval &t2 )
27 return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_usec <= t2.tv_usec);
30 static inline timeval &operator+=( timeval &t1, const timeval &t2 )
32 t1.tv_sec += t2.tv_sec;
33 if ( (t1.tv_usec += t2.tv_usec) >= 1000000 )
36 t1.tv_usec -= 1000000;
41 static inline timeval operator+( const timeval &t1, const timeval &t2 )
44 tmp.tv_sec = t1.tv_sec + t2.tv_sec;
45 if ( (tmp.tv_usec = t1.tv_usec + t2.tv_usec) >= 1000000 )
48 tmp.tv_usec -= 1000000;
53 static inline timeval operator-( const timeval &t1, const timeval &t2 )
56 tmp.tv_sec = t1.tv_sec - t2.tv_sec;
57 if ( (tmp.tv_usec = t1.tv_usec - t2.tv_usec) < 0 )
60 tmp.tv_usec += 1000000;
65 static inline timeval operator-=( timeval &t1, const timeval &t2 )
67 t1.tv_sec -= t2.tv_sec;
68 if ( (t1.tv_usec -= t2.tv_usec) < 0 )
71 t1.tv_usec += 1000000;
76 static inline timeval &operator+=( timeval &t1, const long msek )
78 t1.tv_sec += msek / 1000;
79 if ( (t1.tv_usec += (msek % 1000) * 1000) >= 1000000 )
82 t1.tv_usec -= 1000000;
87 static inline timeval operator+( const timeval &t1, const long msek )
90 tmp.tv_sec = t1.tv_sec + msek / 1000;
91 if ( (tmp.tv_usec = t1.tv_usec + (msek % 1000) * 1000) >= 1000000 )
94 tmp.tv_usec -= 1000000;
99 static inline timeval operator-( const timeval &t1, const long msek )
102 tmp.tv_sec = t1.tv_sec - msek / 1000;
103 if ( (tmp.tv_usec = t1.tv_usec - (msek % 1000)*1000) < 0 )
106 tmp.tv_usec += 1000000;
111 static inline timeval operator-=( timeval &t1, const long msek )
113 t1.tv_sec -= msek / 1000;
114 if ( (t1.tv_usec -= (msek % 1000) * 1000) < 0 )
117 t1.tv_usec += 1000000;
122 static inline int timeval_to_usec(const timeval &t1)
124 return t1.tv_sec*1000000 + t1.tv_usec;
129 // die beiden signalquellen: SocketNotifier...
132 * \brief Gives a callback when data on a file descriptor is ready.
134 * This class emits the signal \c eSocketNotifier::activate whenever the
135 * event specified by \c req is available.
137 class eSocketNotifier
140 enum { Read=POLLIN, Write=POLLOUT, Priority=POLLPRI, Error=POLLERR, Hungup=POLLHUP };
145 int requested; // requested events (POLLIN, ...)
148 * \brief Constructs a eSocketNotifier.
149 * \param context The thread where to bind the socketnotifier to. The signal is emitted from that thread.
150 * \param fd The filedescriptor to monitor. Can be a device or a socket.
151 * \param req The events to watch to, normally either \c Read or \c Write. You can specify any events that \c poll supports.
152 * \param startnow Specifies if the socketnotifier should start immediately.
154 eSocketNotifier(eMainloop *context, int fd, int req, bool startnow=true);
157 PSignal1<void, int> activated;
158 void activate(int what) { /*emit*/ activated(what); }
162 bool isRunning() { return state; }
164 int getFD() { return fd; }
165 int getRequested() { return requested; }
166 void setRequested(int req) { requested=req; }
171 // werden in einer mainloop verarbeitet
175 friend class eSocketNotifier;
176 std::map<int, eSocketNotifier*> notifiers;
177 ePtrList<eTimer> m_timer_list;
181 void processOneEvent();
183 pthread_mutex_t recalcLock;
185 int m_now_is_invalid;
187 static void addTimeOffset(int offset);
188 void addSocketNotifier(eSocketNotifier *sn);
189 void removeSocketNotifier(eSocketNotifier *sn);
190 void addTimer(eTimer* e);
191 void removeTimer(eTimer* e);
193 static ePtrList<eMainloop> existing_loops;
195 :app_quit_now(0),loop_level(0),retval(0)
197 m_now_is_invalid = 0;
198 existing_loops.push_back(this);
199 pthread_mutex_init(&recalcLock, 0);
203 existing_loops.remove(this);
204 pthread_mutex_destroy(&recalcLock);
206 int looplevel() { return loop_level; }
208 /* OBSOLETE. DONT USE. */
209 int exec(); // recursive enter the loop
210 void quit(int ret=0); // leave all pending loops (recursive leave())
216 * \brief The application class.
218 * An application provides a mainloop, and runs in the primary thread.
219 * You can have other threads, too, but this is the primary one.
221 class eApplication: public eMainloop
237 * \brief Gives a callback after a specified timeout.
239 * This class emits the signal \c eTimer::timeout after the specified timeout.
243 friend class eMainloop;
245 timeval nextActivation;
251 * \brief Constructs a timer.
253 * The timer is not yet active, it has to be started with \c start.
254 * \param context The thread from which the signal should be emitted.
256 eTimer(eMainloop *context=eApp): context(*context), bActive(false) { }
257 ~eTimer() { if (bActive) stop(); }
259 PSignal0<void> timeout;
262 bool isActive() { return bActive; }
263 timeval &getNextActivation() { return nextActivation; }
265 void start(long msec, bool b=false);
267 void changeInterval(long msek);
268 bool operator<(const eTimer& t) const { return nextActivation < t.nextActivation; }
269 void startLongTimer( int seconds );
270 void addTimeOffset(int);