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 timeval &operator+=( timeval &t1, const timeval &t2 )
27 t1.tv_sec += t2.tv_sec;
28 if ( (t1.tv_usec += t2.tv_usec) >= 1000000 )
31 t1.tv_usec -= 1000000;
36 static inline timeval operator+( const timeval &t1, const timeval &t2 )
39 tmp.tv_sec = t1.tv_sec + t2.tv_sec;
40 if ( (tmp.tv_usec = t1.tv_usec + t2.tv_usec) >= 1000000 )
43 tmp.tv_usec -= 1000000;
48 static inline timeval operator-( const timeval &t1, const timeval &t2 )
51 tmp.tv_sec = t1.tv_sec - t2.tv_sec;
52 if ( (tmp.tv_usec = t1.tv_usec - t2.tv_usec) < 0 )
55 tmp.tv_usec += 1000000;
60 static inline timeval operator-=( timeval &t1, const timeval &t2 )
62 t1.tv_sec -= t2.tv_sec;
63 if ( (t1.tv_usec -= t2.tv_usec) < 0 )
66 t1.tv_usec += 1000000;
71 static inline timeval &operator+=( timeval &t1, const long msek )
73 t1.tv_sec += msek / 1000;
74 if ( (t1.tv_usec += (msek % 1000) * 1000) >= 1000000 )
77 t1.tv_usec -= 1000000;
82 static inline timeval operator+( const timeval &t1, const long msek )
85 tmp.tv_sec = t1.tv_sec + msek / 1000;
86 if ( (tmp.tv_usec = t1.tv_usec + (msek % 1000) * 1000) >= 1000000 )
89 tmp.tv_usec -= 1000000;
94 static inline timeval operator-( const timeval &t1, const long msek )
97 tmp.tv_sec = t1.tv_sec - msek / 1000;
98 if ( (tmp.tv_usec = t1.tv_usec - (msek % 1000)*1000) < 0 )
101 tmp.tv_usec += 1000000;
106 static inline timeval operator-=( timeval &t1, const long msek )
108 t1.tv_sec -= msek / 1000;
109 if ( (t1.tv_usec -= (msek % 1000) * 1000) < 0 )
112 t1.tv_usec += 1000000;
117 static inline int timeval_to_usec(const timeval &t1)
119 return t1.tv_sec*1000000 + t1.tv_usec;
124 // die beiden signalquellen: SocketNotifier...
127 * \brief Gives a callback when data on a file descriptor is ready.
129 * This class emits the signal \c eSocketNotifier::activate whenever the
130 * event specified by \c req is available.
132 class eSocketNotifier
135 enum { Read=POLLIN, Write=POLLOUT, Priority=POLLPRI, Error=POLLERR, Hungup=POLLHUP };
140 int requested; // requested events (POLLIN, ...)
143 * \brief Constructs a eSocketNotifier.
144 * \param context The thread where to bind the socketnotifier to. The signal is emitted from that thread.
145 * \param fd The filedescriptor to monitor. Can be a device or a socket.
146 * \param req The events to watch to, normally either \c Read or \c Write. You can specify any events that \c poll supports.
147 * \param startnow Specifies if the socketnotifier should start immediately.
149 eSocketNotifier(eMainloop *context, int fd, int req, bool startnow=true);
152 PSignal1<void, int> activated;
153 void activate(int what) { /*emit*/ activated(what); }
157 bool isRunning() { return state; }
159 int getFD() { return fd; }
160 int getRequested() { return requested; }
161 void setRequested(int req) { requested=req; }
166 // werden in einer mainloop verarbeitet
170 friend class eSocketNotifier;
171 std::map<int, eSocketNotifier*> notifiers;
172 ePtrList<eTimer> m_timer_list;
176 void processOneEvent();
178 pthread_mutex_t recalcLock;
180 int m_now_is_invalid;
182 static void addTimeOffset(int offset);
183 void addSocketNotifier(eSocketNotifier *sn);
184 void removeSocketNotifier(eSocketNotifier *sn);
185 void addTimer(eTimer* e);
186 void removeTimer(eTimer* e);
188 static ePtrList<eMainloop> existing_loops;
190 :app_quit_now(0),loop_level(0),retval(0)
192 m_now_is_invalid = 0;
193 existing_loops.push_back(this);
194 pthread_mutex_init(&recalcLock, 0);
198 existing_loops.remove(this);
199 pthread_mutex_destroy(&recalcLock);
201 int looplevel() { return loop_level; }
203 /* OBSOLETE. DONT USE. */
204 int exec(); // recursive enter the loop
205 void quit(int ret=0); // leave all pending loops (recursive leave())
211 * \brief The application class.
213 * An application provides a mainloop, and runs in the primary thread.
214 * You can have other threads, too, but this is the primary one.
216 class eApplication: public eMainloop
232 * \brief Gives a callback after a specified timeout.
234 * This class emits the signal \c eTimer::timeout after the specified timeout.
238 friend class eMainloop;
240 timeval nextActivation;
246 * \brief Constructs a timer.
248 * The timer is not yet active, it has to be started with \c start.
249 * \param context The thread from which the signal should be emitted.
251 eTimer(eMainloop *context=eApp): context(*context), bActive(false) { }
252 ~eTimer() { if (bActive) stop(); }
254 PSignal0<void> timeout;
257 bool isActive() { return bActive; }
258 timeval &getNextActivation() { return nextActivation; }
260 void start(long msec, bool b=false);
262 void changeInterval(long msek);
263 bool operator<(const eTimer& t) const { return nextActivation < t.nextActivation; }
264 void startLongTimer( int seconds );
265 void addTimeOffset(int);