d9a17b79881cf7714775f883cbaafe96f2a6dc6a
[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 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         std::map<int, eSocketNotifier*> notifiers;
180         ePtrList<eTimer> TimerList;
181         bool app_exit_loop;
182         bool app_quit_now;
183         int loop_level;
184         void processOneEvent();
185         int retval;
186 public:
187         eMainloop():app_quit_now(0),loop_level(0),retval(0){    }
188         void addSocketNotifier(eSocketNotifier *sn);
189         void removeSocketNotifier(eSocketNotifier *sn);
190         void addTimer(eTimer* e)        {               TimerList.push_back(e);         TimerList.sort();       }
191         void removeTimer(eTimer* e)     {               TimerList.remove(e);    }
192
193         int looplevel() { return loop_level; }
194         
195         int exec();  // recursive enter the loop
196         void quit(int ret=0); // leave all pending loops (recursive leave())
197         void enter_loop();
198         void exit_loop();
199 };
200
201
202 /**
203  * \brief The application class.
204  *
205  * An application provides a mainloop, and runs in the primary thread.
206  * You can have other threads, too, but this is the primary one.
207  */
208 class eApplication: public eMainloop
209 {
210 public:
211         eApplication()
212         {
213                 if (!eApp)
214                         eApp = this;
215         }
216         ~eApplication()
217         {
218                 eApp = 0;
219         }
220 };
221
222                                 // ... und Timer
223 /**
224  * \brief Gives a callback after a specified timeout.
225  *
226  * This class emits the signal \c eTimer::timeout after the specified timeout.
227  */
228 class eTimer
229 {
230         eMainloop &context;
231         timeval nextActivation;
232         long interval;
233         bool bSingleShot;
234         bool bActive;
235 public:
236         /**
237          * \brief Constructs a timer.
238          *
239          * The timer is not yet active, it has to be started with \c start.
240          * \param context The thread from which the signal should be emitted.
241          */
242         eTimer(eMainloop *context = eApp): context(*context), bActive(false) { }
243         ~eTimer()       { if (bActive) stop(); }
244
245         PSignal0<void> timeout;
246         void activate();
247
248         bool isActive() { return bActive; }
249         timeval &getNextActivation() { return nextActivation; }
250
251         void start(long msec, bool singleShot=false);
252         void stop();
253         void changeInterval(long msek);
254         bool operator<(const eTimer& t) const   {               return nextActivation < t.nextActivation;               }
255 };
256
257 #endif