aaabd30d436d5bef8e21bc309a60dba0f381887a
[enigma2.git] / 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 static inline bool operator<( const timeval &t1, const timeval &t2 )
20 {
21         return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_usec < t2.tv_usec);
22 }
23
24 static inline timeval &operator+=( timeval &t1, const timeval &t2 )
25 {
26         t1.tv_sec += t2.tv_sec;
27         if ( (t1.tv_usec += t2.tv_usec) >= 1000000 )
28         {
29                 t1.tv_sec++;
30                 t1.tv_usec -= 1000000;
31         }
32         return t1;
33 }
34
35 static inline timeval operator+( const timeval &t1, const timeval &t2 )
36 {
37         timeval tmp;
38         tmp.tv_sec = t1.tv_sec + t2.tv_sec;
39         if ( (tmp.tv_usec = t1.tv_usec + t2.tv_usec) >= 1000000 )
40         {
41                 tmp.tv_sec++;
42                 tmp.tv_usec -= 1000000;
43         }
44         return tmp;
45 }
46
47 static inline timeval operator-( const timeval &t1, const timeval &t2 )
48 {
49         timeval tmp;
50         tmp.tv_sec = t1.tv_sec - t2.tv_sec;
51         if ( (tmp.tv_usec = t1.tv_usec - t2.tv_usec) < 0 )
52         {
53                 tmp.tv_sec--;
54                 tmp.tv_usec += 1000000;
55         }
56         return tmp;
57 }
58
59 static inline timeval operator-=( timeval &t1, const timeval &t2 )
60 {
61         t1.tv_sec -= t2.tv_sec;
62         if ( (t1.tv_usec -= t2.tv_usec) < 0 )
63         {
64                 t1.tv_sec--;
65                 t1.tv_usec += 1000000;
66         }
67         return t1;
68 }
69
70 static inline timeval &operator+=( timeval &t1, const long msek )
71 {
72         t1.tv_sec += msek / 1000;
73         if ( (t1.tv_usec += (msek % 1000) * 1000) >= 1000000 )
74         {
75                 t1.tv_sec++;
76                 t1.tv_usec -= 1000000;
77         }
78         return t1;
79 }
80
81 static inline timeval operator+( const timeval &t1, const long msek )
82 {
83         timeval tmp;
84         tmp.tv_sec = t1.tv_sec + msek / 1000;
85         if ( (tmp.tv_usec = t1.tv_usec + (msek % 1000) * 1000) >= 1000000 )
86         {
87                 tmp.tv_sec++;
88                 tmp.tv_usec -= 1000000;
89         }
90         return tmp;
91 }
92
93 static inline timeval operator-( const timeval &t1, const long msek )
94 {
95         timeval tmp;
96         tmp.tv_sec = t1.tv_sec - msek / 1000;
97         if ( (tmp.tv_usec = t1.tv_usec - (msek % 1000)*1000) < 0 )
98         {
99                 tmp.tv_sec--;
100                 tmp.tv_usec += 1000000;
101         }
102         return tmp;
103 }
104
105 static inline timeval operator-=( timeval &t1, const long msek )
106 {
107         t1.tv_sec -= msek / 1000;
108         if ( (t1.tv_usec -= (msek % 1000) * 1000) < 0 )
109         {
110                 t1.tv_sec--;
111                 t1.tv_usec += 1000000;
112         }
113         return t1;
114 }
115
116 static inline timeval timeout_timeval ( const timeval & orig )
117 {
118         timeval now;
119   gettimeofday(&now,0);
120
121         return orig-now;
122 }
123
124 static inline long timeout_usec ( const timeval & orig )
125 {
126         timeval now;
127   gettimeofday(&now,0);
128
129         return (orig-now).tv_sec*1000000 + (orig-now).tv_usec;
130 }
131
132 class eMainloop;
133
134                                         // die beiden signalquellen: SocketNotifier...
135
136 /**
137  * \brief Gives a callback when data on a file descriptor is ready.
138  *
139  * This class emits the signal \c eSocketNotifier::activate whenever the
140  * event specified by \c req is available.
141  */
142 class eSocketNotifier
143 {
144 public:
145         enum { Read=POLLIN, Write=POLLOUT, Priority=POLLPRI, Error=POLLERR, Hungup=POLLHUP };
146 private:
147         eMainloop &context;
148         int fd;
149         int state;
150         int requested;          // requested events (POLLIN, ...)
151 public:
152         /**
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.
158          */
159         eSocketNotifier(eMainloop *context, int fd, int req, bool startnow=true);
160         ~eSocketNotifier();
161
162         PSignal1<void, int> activated;
163         void activate(int what) { /*emit*/ activated(what); }
164
165         void start();
166         void stop();
167         bool isRunning() { return state; }
168
169         int getFD() { return fd; }
170         int getRequested() { return requested; }
171         void setRequested(int req) { requested=req; }
172 };
173
174 class eTimer;
175
176                         // werden in einer mainloop verarbeitet
177 class eMainloop
178 {
179         friend class eTimer;
180         friend class eSocketNotifier;
181         std::map<int, eSocketNotifier*> notifiers;
182         ePtrList<eTimer> TimerList;
183         bool app_exit_loop;
184         bool app_quit_now;
185         int loop_level;
186         void processOneEvent();
187         int retval;
188         int timer_offset;
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);    }
195 public:
196         static ePtrList<eMainloop> existing_loops;
197         eMainloop()
198                 :app_quit_now(0),loop_level(0),retval(0),timer_offset(0)
199         {
200                 existing_loops.push_back(this);
201                 pthread_mutex_init(&recalcLock, 0);
202         }
203         ~eMainloop()
204         {
205                 existing_loops.remove(this);
206                 pthread_mutex_destroy(&recalcLock);
207         }
208         int looplevel() { return loop_level; }
209
210         int exec();  // recursive enter the loop
211         void quit(int ret=0); // leave all pending loops (recursive leave())
212         void enter_loop();
213         void exit_loop();
214         void setTimerOffset( int );
215         int getTimerOffset() { return timer_offset; }
216         bool isAppQuitNowSet() { return app_quit_now; }
217 };
218
219 /**
220  * \brief The application class.
221  *
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.
224  */
225 class eApplication: public eMainloop
226 {
227 public:
228         eApplication()
229         {
230                 if (!eApp)
231                         eApp = this;
232         }
233         ~eApplication()
234         {
235                 eApp = 0;
236         }
237 };
238
239                                 // ... und Timer
240 /**
241  * \brief Gives a callback after a specified timeout.
242  *
243  * This class emits the signal \c eTimer::timeout after the specified timeout.
244  */
245 class eTimer
246 {
247         friend class eMainloop;
248         eMainloop &context;
249         timeval nextActivation;
250         long interval;
251         bool bSingleShot;
252         bool bActive;
253         inline void recalc(int);
254 public:
255         /**
256          * \brief Constructs a timer.
257          *
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.
260          */
261         eTimer(eMainloop *context=eApp): context(*context), bActive(false) { }
262         ~eTimer() { if (bActive) stop(); }
263
264         PSignal0<void> timeout;
265         void activate();
266
267         bool isActive() { return bActive; }
268         timeval &getNextActivation() { return nextActivation; }
269
270         void start(long msec, bool b=false);
271         void stop();
272         void changeInterval(long msek);
273         bool operator<(const eTimer& t) const { return nextActivation < t.nextActivation; }
274         void startLongTimer( int seconds );
275 };
276 #endif