cleaned up poll core
[vuplus_dvbapp] / lib / base / ebase.h
1 #ifndef __ebase_h
2 #define __ebase_h
3
4 #include <vector>
5 #include <map>
6 #include <sys/poll.h>
7 #include <sys/time.h>
8 #include <asm/types.h>
9 #include <time.h>
10
11 #include <lib/base/eptrlist.h>
12 #include <lib/python/connections.h>
13 #include <libsig_comp.h>
14
15 class eApplication;
16
17 extern eApplication* eApp;
18
19         /* TODO: remove these inlines. */
20 static inline bool operator<( const timeval &t1, const timeval &t2 )
21 {
22         return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_usec < t2.tv_usec);
23 }
24
25 static inline timeval &operator+=( timeval &t1, const timeval &t2 )
26 {
27         t1.tv_sec += t2.tv_sec;
28         if ( (t1.tv_usec += t2.tv_usec) >= 1000000 )
29         {
30                 t1.tv_sec++;
31                 t1.tv_usec -= 1000000;
32         }
33         return t1;
34 }
35
36 static inline timeval operator+( const timeval &t1, const timeval &t2 )
37 {
38         timeval tmp;
39         tmp.tv_sec = t1.tv_sec + t2.tv_sec;
40         if ( (tmp.tv_usec = t1.tv_usec + t2.tv_usec) >= 1000000 )
41         {
42                 tmp.tv_sec++;
43                 tmp.tv_usec -= 1000000;
44         }
45         return tmp;
46 }
47
48 static inline timeval operator-( const timeval &t1, const timeval &t2 )
49 {
50         timeval tmp;
51         tmp.tv_sec = t1.tv_sec - t2.tv_sec;
52         if ( (tmp.tv_usec = t1.tv_usec - t2.tv_usec) < 0 )
53         {
54                 tmp.tv_sec--;
55                 tmp.tv_usec += 1000000;
56         }
57         return tmp;
58 }
59
60 static inline timeval operator-=( timeval &t1, const timeval &t2 )
61 {
62         t1.tv_sec -= t2.tv_sec;
63         if ( (t1.tv_usec -= t2.tv_usec) < 0 )
64         {
65                 t1.tv_sec--;
66                 t1.tv_usec += 1000000;
67         }
68         return t1;
69 }
70
71 static inline timeval &operator+=( timeval &t1, const long msek )
72 {
73         t1.tv_sec += msek / 1000;
74         if ( (t1.tv_usec += (msek % 1000) * 1000) >= 1000000 )
75         {
76                 t1.tv_sec++;
77                 t1.tv_usec -= 1000000;
78         }
79         return t1;
80 }
81
82 static inline timeval operator+( const timeval &t1, const long msek )
83 {
84         timeval tmp;
85         tmp.tv_sec = t1.tv_sec + msek / 1000;
86         if ( (tmp.tv_usec = t1.tv_usec + (msek % 1000) * 1000) >= 1000000 )
87         {
88                 tmp.tv_sec++;
89                 tmp.tv_usec -= 1000000;
90         }
91         return tmp;
92 }
93
94 static inline timeval operator-( const timeval &t1, const long msek )
95 {
96         timeval tmp;
97         tmp.tv_sec = t1.tv_sec - msek / 1000;
98         if ( (tmp.tv_usec = t1.tv_usec - (msek % 1000)*1000) < 0 )
99         {
100                 tmp.tv_sec--;
101                 tmp.tv_usec += 1000000;
102         }
103         return tmp;
104 }
105
106 static inline timeval operator-=( timeval &t1, const long msek )
107 {
108         t1.tv_sec -= msek / 1000;
109         if ( (t1.tv_usec -= (msek % 1000) * 1000) < 0 )
110         {
111                 t1.tv_sec--;
112                 t1.tv_usec += 1000000;
113         }
114         return t1;
115 }
116
117 static inline int timeval_to_usec(const timeval &t1)
118 {
119         return t1.tv_sec*1000000 + t1.tv_usec;
120 }
121
122 class eMainloop;
123
124                                         // die beiden signalquellen: SocketNotifier...
125
126 /**
127  * \brief Gives a callback when data on a file descriptor is ready.
128  *
129  * This class emits the signal \c eSocketNotifier::activate whenever the
130  * event specified by \c req is available.
131  */
132 class eSocketNotifier
133 {
134 public:
135         enum { Read=POLLIN, Write=POLLOUT, Priority=POLLPRI, Error=POLLERR, Hungup=POLLHUP };
136 private:
137         eMainloop &context;
138         int fd;
139         int state;
140         int requested;          // requested events (POLLIN, ...)
141 public:
142         /**
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.
148          */
149         eSocketNotifier(eMainloop *context, int fd, int req, bool startnow=true);
150         ~eSocketNotifier();
151
152         PSignal1<void, int> activated;
153         void activate(int what) { /*emit*/ activated(what); }
154
155         void start();
156         void stop();
157         bool isRunning() { return state; }
158
159         int getFD() { return fd; }
160         int getRequested() { return requested; }
161         void setRequested(int req) { requested=req; }
162 };
163
164 class eTimer;
165
166                         // werden in einer mainloop verarbeitet
167 class eMainloop
168 {
169         friend class eTimer;
170         friend class eSocketNotifier;
171         std::map<int, eSocketNotifier*> notifiers;
172         ePtrList<eTimer> m_timer_list;
173         bool app_exit_loop;
174         bool app_quit_now;
175         int loop_level;
176         void processOneEvent();
177         int retval;
178         int timer_offset;
179         pthread_mutex_t recalcLock;
180 public:
181         void addTimeOffset(int offset);
182         void addSocketNotifier(eSocketNotifier *sn);
183         void removeSocketNotifier(eSocketNotifier *sn);
184         void addTimer(eTimer* e);
185         void removeTimer(eTimer* e);
186
187         static ePtrList<eMainloop> existing_loops;
188         eMainloop()
189                 :app_quit_now(0),loop_level(0),retval(0),timer_offset(0)
190         {
191                 existing_loops.push_back(this);
192                 pthread_mutex_init(&recalcLock, 0);
193         }
194         ~eMainloop()
195         {
196                 existing_loops.remove(this);
197                 pthread_mutex_destroy(&recalcLock);
198         }
199         int looplevel() { return loop_level; }
200
201                 /* OBSOLETE. DONT USE. */
202         int exec();  // recursive enter the loop
203         void quit(int ret=0); // leave all pending loops (recursive leave())
204         void enter_loop();
205         void exit_loop();
206 };
207
208 /**
209  * \brief The application class.
210  *
211  * An application provides a mainloop, and runs in the primary thread.
212  * You can have other threads, too, but this is the primary one.
213  */
214 class eApplication: public eMainloop
215 {
216 public:
217         eApplication()
218         {
219                 if (!eApp)
220                         eApp = this;
221         }
222         ~eApplication()
223         {
224                 eApp = 0;
225         }
226 };
227
228                                 // ... und Timer
229 /**
230  * \brief Gives a callback after a specified timeout.
231  *
232  * This class emits the signal \c eTimer::timeout after the specified timeout.
233  */
234 class eTimer
235 {
236         friend class eMainloop;
237         eMainloop &context;
238         timeval nextActivation;
239         long interval;
240         bool bSingleShot;
241         bool bActive;
242 public:
243         /**
244          * \brief Constructs a timer.
245          *
246          * The timer is not yet active, it has to be started with \c start.
247          * \param context The thread from which the signal should be emitted.
248          */
249         eTimer(eMainloop *context=eApp): context(*context), bActive(false) { }
250         ~eTimer() { if (bActive) stop(); }
251
252         PSignal0<void> timeout;
253         void activate();
254
255         bool isActive() { return bActive; }
256         timeval &getNextActivation() { return nextActivation; }
257
258         void start(long msec, bool b=false);
259         void stop();
260         void changeInterval(long msek);
261         bool operator<(const eTimer& t) const { return nextActivation < t.nextActivation; }
262         void startLongTimer( int seconds );
263         void addTimeOffset(int);
264 };
265 #endif