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