c86c177ddd08e79e592143bbe7956620b041c556
[vuplus_dvbapp] / lib / base / ebase.h
1 #ifndef __ebase_h
2 #define __ebase_h
3
4 #ifndef SWIG
5 #include <vector>
6 #include <map>
7 #include <sys/poll.h>
8 #include <sys/time.h>
9 #include <asm/types.h>
10 #include <time.h>
11
12 #include <lib/base/eptrlist.h>
13 #include <libsig_comp.h>
14 #endif
15
16 #include <lib/python/connections.h>
17
18 class eApplication;
19
20 extern eApplication* eApp;
21
22 #ifndef SWIG
23         /* TODO: remove these inlines. */
24 static inline bool operator<( const timespec &t1, const timespec &t2 )
25 {
26         return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec);
27 }
28
29 static inline bool operator<=( const timespec &t1, const timespec &t2 )
30 {
31         return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_nsec <= t2.tv_nsec);
32 }
33
34 static inline timespec &operator+=( timespec &t1, const timespec &t2 )
35 {
36         t1.tv_sec += t2.tv_sec;
37         if ( (t1.tv_nsec += t2.tv_nsec) >= 1000000000 )
38         {
39                 t1.tv_sec++;
40                 t1.tv_nsec -= 1000000000;
41         }
42         return t1;
43 }
44
45 static inline timespec operator+( const timespec &t1, const timespec &t2 )
46 {
47         timespec tmp;
48         tmp.tv_sec = t1.tv_sec + t2.tv_sec;
49         if ( (tmp.tv_nsec = t1.tv_nsec + t2.tv_nsec) >= 1000000000 )
50         {
51                 tmp.tv_sec++;
52                 tmp.tv_nsec -= 1000000000;
53         }
54         return tmp;
55 }
56
57 static inline timespec operator-( const timespec &t1, const timespec &t2 )
58 {
59         timespec tmp;
60         tmp.tv_sec = t1.tv_sec - t2.tv_sec;
61         if ( (tmp.tv_nsec = t1.tv_nsec - t2.tv_nsec) < 0 )
62         {
63                 tmp.tv_sec--;
64                 tmp.tv_nsec += 1000000000;
65         }
66         return tmp;
67 }
68
69 static inline timespec operator-=( timespec &t1, const timespec &t2 )
70 {
71         t1.tv_sec -= t2.tv_sec;
72         if ( (t1.tv_nsec -= t2.tv_nsec) < 0 )
73         {
74                 t1.tv_sec--;
75                 t1.tv_nsec += 1000000000;
76         }
77         return t1;
78 }
79
80 static inline timespec &operator+=( timespec &t1, const long msek )
81 {
82         t1.tv_sec += msek / 1000;
83         if ( (t1.tv_nsec += (msek % 1000) * 1000000) >= 1000000000 )
84         {
85                 t1.tv_sec++;
86                 t1.tv_nsec -= 1000000000;
87         }
88         return t1;
89 }
90
91 static inline timespec operator+( const timespec &t1, const long msek )
92 {
93         timespec tmp;
94         tmp.tv_sec = t1.tv_sec + msek / 1000;
95         if ( (tmp.tv_nsec = t1.tv_nsec + (msek % 1000) * 1000000) >= 1000000000 )
96         {
97                 tmp.tv_sec++;
98                 tmp.tv_nsec -= 1000000;
99         }
100         return tmp;
101 }
102
103 static inline timespec operator-( const timespec &t1, const long msek )
104 {
105         timespec tmp;
106         tmp.tv_sec = t1.tv_sec - msek / 1000;
107         if ( (tmp.tv_nsec = t1.tv_nsec - (msek % 1000)*1000000) < 0 )
108         {
109                 tmp.tv_sec--;
110                 tmp.tv_nsec += 1000000000;
111         }
112         return tmp;
113 }
114
115 static inline timespec operator-=( timespec &t1, const long msek )
116 {
117         t1.tv_sec -= msek / 1000;
118         if ( (t1.tv_nsec -= (msek % 1000) * 1000000) < 0 )
119         {
120                 t1.tv_sec--;
121                 t1.tv_nsec += 1000000000;
122         }
123         return t1;
124 }
125
126 static inline long timeout_usec ( const timespec & orig )
127 {
128         timespec now;
129         clock_gettime(CLOCK_MONOTONIC, &now);
130         if ( (orig-now).tv_sec > 2000 )
131                 return 2000*1000*1000;
132         return (orig-now).tv_sec*1000000 + (orig-now).tv_nsec/1000;
133 }
134
135 class eMainloop;
136
137                                         // die beiden signalquellen: SocketNotifier...
138
139 /**
140  * \brief Gives a callback when data on a file descriptor is ready.
141  *
142  * This class emits the signal \c eSocketNotifier::activate whenever the
143  * event specified by \c req is available.
144  */
145 class eSocketNotifier: iObject
146 {
147         DECLARE_REF(eSocketNotifier);
148         friend class eMainloop;
149 public:
150         enum { Read=POLLIN, Write=POLLOUT, Priority=POLLPRI, Error=POLLERR, Hungup=POLLHUP };
151 private:
152         eMainloop &context;
153         int fd;
154         int state;
155         int requested;          // requested events (POLLIN, ...)
156         void activate(int what) { /*emit*/ activated(what); }
157         eSocketNotifier(eMainloop *context, int fd, int req, bool startnow);
158         void operator delete(void *pmem) { ::operator delete(pmem); }
159 public:
160         /**
161          * \brief Constructs a eSocketNotifier.
162          * \param context The thread where to bind the socketnotifier to. The signal is emitted from that thread.
163          * \param fd The filedescriptor to monitor. Can be a device or a socket.
164          * \param req The events to watch to, normally either \c Read or \c Write. You can specify any events that \c poll supports.
165          * \param startnow Specifies if the socketnotifier should start immediately.
166          */
167         static eSocketNotifier* create(eMainloop *context, int fd, int req, bool startnow=true) { return new eSocketNotifier(context, fd, req, startnow); }
168         ~eSocketNotifier();
169
170         PSignal1<void, int> activated;
171
172         void start();
173         void stop();
174         bool isRunning() { return state; }
175
176         int getFD() { return fd; }
177         int getRequested() { return requested; }
178         void setRequested(int req) { requested=req; }
179
180         eSmartPtrList<iObject> m_clients;
181 };
182
183 #endif
184
185 class eTimer;
186
187                         // werden in einer mainloop verarbeitet
188 class eMainloop
189 {
190         friend class eTimer;
191         friend class eSocketNotifier;
192         std::map<int, eSocketNotifier*> notifiers;
193         ePtrList<eTimer> m_timer_list;
194         bool app_quit_now;
195         int loop_level;
196         int processOneEvent(unsigned int user_timeout, PyObject **res=0, ePyObject additional=ePyObject());
197         int retval;
198         int m_is_idle;
199
200         int m_interrupt_requested;
201         timespec m_twisted_timer; // twisted timer
202
203         void addSocketNotifier(eSocketNotifier *sn);
204         void removeSocketNotifier(eSocketNotifier *sn);
205         void addTimer(eTimer* e);
206         void removeTimer(eTimer* e);
207 public:
208 #ifndef SWIG
209         static ePtrList<eMainloop> existing_loops;
210 #endif
211
212         eMainloop()
213                 :app_quit_now(0),loop_level(0),retval(0), m_is_idle(0), m_interrupt_requested(0)
214         {
215                 existing_loops.push_back(this);
216         }
217         virtual ~eMainloop();
218
219         int looplevel() { return loop_level; }
220
221 #ifndef SWIG
222         void quit(int ret=0); // leave all pending loops (recursive leave())
223 #endif
224
225                 /* a user supplied timeout. enter_loop will return with:
226                   0 - no timeout, no signal
227                   1 - timeout
228                   2 - signal
229                 */
230         int iterate(unsigned int timeout=0, PyObject **res=0, SWIG_PYOBJECT(ePyObject) additional=(PyObject*)0);
231
232                 /* run will iterate endlessly until the app is quit, and return
233                    the exit code */
234         int runLoop();
235
236                 /* our new shared polling interface. */
237         PyObject *poll(SWIG_PYOBJECT(ePyObject) dict, SWIG_PYOBJECT(ePyObject) timeout);
238         void interruptPoll();
239         void reset();
240
241                 /* m_is_idle needs to be atomic, but it doesn't really matter much, as it's read-only from outside */
242         int isIdle() { return m_is_idle; }
243 };
244
245 /**
246  * \brief The application class.
247  *
248  * An application provides a mainloop, and runs in the primary thread.
249  * You can have other threads, too, but this is the primary one.
250  */
251 class eApplication: public eMainloop
252 {
253 public:
254         eApplication()
255         {
256                 if (!eApp)
257                         eApp = this;
258         }
259         ~eApplication()
260         {
261                 eApp = 0;
262         }
263 };
264
265 #ifndef SWIG
266                                 // ... und Timer
267 /**
268  * \brief Gives a callback after a specified timeout.
269  *
270  * This class emits the signal \c eTimer::timeout after the specified timeout.
271  */
272 class eTimer: iObject
273 {
274         DECLARE_REF(eTimer);
275         friend class eMainloop;
276         eMainloop &context;
277         timespec nextActivation;
278         long interval;
279         bool bSingleShot;
280         bool bActive;
281         void activate();
282
283         eTimer(eMainloop *context): context(*context), bActive(false) { }
284         void operator delete(void *pmem) { ::operator delete(pmem); }
285 public:
286         /**
287          * \brief Constructs a timer.
288          *
289          * The timer is not yet active, it has to be started with \c start.
290          * \param context The thread from which the signal should be emitted.
291          */
292         static eTimer *create(eMainloop *context=eApp) { return new eTimer(context); }
293         ~eTimer() { if (bActive) stop(); }
294
295         PSignal0<void> timeout;
296
297         bool isActive() { return bActive; }
298
299         timespec &getNextActivation() { return nextActivation; }
300
301         void start(long msec, bool b=false);
302         void stop();
303         void changeInterval(long msek);
304         void startLongTimer( int seconds );
305         bool operator<(const eTimer& t) const { return nextActivation < t.nextActivation; }
306         eSmartPtrList<iObject> m_clients;
307 };
308 #endif  // SWIG
309
310 #endif