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