Merge branch 'bug_411_timeshift_disable_without_live'
[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 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 public:
159         /**
160          * \brief Constructs a eSocketNotifier.
161          * \param context The thread where to bind the socketnotifier to. The signal is emitted from that thread.
162          * \param fd The filedescriptor to monitor. Can be a device or a socket.
163          * \param req The events to watch to, normally either \c Read or \c Write. You can specify any events that \c poll supports.
164          * \param startnow Specifies if the socketnotifier should start immediately.
165          */
166         static eSocketNotifier* create(eMainloop *context, int fd, int req, bool startnow=true) { return new eSocketNotifier(context, fd, req, startnow); }
167         ~eSocketNotifier();
168
169         PSignal1<void, int> activated;
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         eSmartPtrList<iObject> m_clients;
180 };
181
182 #endif
183
184 class eTimer;
185
186                         // werden in einer mainloop verarbeitet
187 class eMainloop
188 {
189         friend class eTimer;
190         friend class eSocketNotifier;
191         std::map<int, eSocketNotifier*> notifiers;
192         ePtrList<eTimer> m_timer_list;
193         bool app_quit_now;
194         int loop_level;
195         int processOneEvent(unsigned int user_timeout, PyObject **res=0, ePyObject additional=ePyObject());
196         int retval;
197         int m_is_idle;
198         int m_idle_count;
199         eSocketNotifier *m_inActivate;
200
201         int m_interrupt_requested;
202         timespec m_twisted_timer; // twisted timer
203
204         void addSocketNotifier(eSocketNotifier *sn);
205         void removeSocketNotifier(eSocketNotifier *sn);
206         void addTimer(eTimer* e);
207         void removeTimer(eTimer* e);
208 public:
209 #ifndef SWIG
210         static ePtrList<eMainloop> existing_loops;
211 #endif
212
213         eMainloop()
214                 :app_quit_now(0),loop_level(0),retval(0), m_is_idle(0), m_idle_count(0), m_inActivate(0), m_interrupt_requested(0)
215         {
216                 existing_loops.push_back(this);
217         }
218         virtual ~eMainloop();
219
220         int looplevel() { return loop_level; }
221
222 #ifndef SWIG
223         void quit(int ret=0); // leave all pending loops (recursive leave())
224 #endif
225
226                 /* a user supplied timeout. enter_loop will return with:
227                   0 - no timeout, no signal
228                   1 - timeout
229                   2 - signal
230                 */
231         int iterate(unsigned int timeout=0, PyObject **res=0, SWIG_PYOBJECT(ePyObject) additional=(PyObject*)0);
232
233                 /* run will iterate endlessly until the app is quit, and return
234                    the exit code */
235         int runLoop();
236
237                 /* our new shared polling interface. */
238         PyObject *poll(SWIG_PYOBJECT(ePyObject) dict, SWIG_PYOBJECT(ePyObject) timeout);
239         void interruptPoll();
240         void reset();
241
242                 /* m_is_idle needs to be atomic, but it doesn't really matter much, as it's read-only from outside */
243         int isIdle() { return m_is_idle; }
244         int idleCount() { return m_idle_count; }
245 };
246
247 /**
248  * \brief The application class.
249  *
250  * An application provides a mainloop, and runs in the primary thread.
251  * You can have other threads, too, but this is the primary one.
252  */
253 class eApplication: public eMainloop
254 {
255 public:
256         eApplication()
257         {
258                 if (!eApp)
259                         eApp = this;
260         }
261         ~eApplication()
262         {
263                 eApp = 0;
264         }
265 };
266
267 #ifndef SWIG
268                                 // ... und Timer
269 /**
270  * \brief Gives a callback after a specified timeout.
271  *
272  * This class emits the signal \c eTimer::timeout after the specified timeout.
273  */
274 class eTimer: iObject
275 {
276         DECLARE_REF(eTimer);
277         friend class eMainloop;
278         eMainloop &context;
279         timespec nextActivation;
280         long interval;
281         bool bSingleShot;
282         bool bActive;
283         void activate();
284
285         eTimer(eMainloop *context): context(*context), bActive(false) { }
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         static eTimer *create(eMainloop *context=eApp) { return new eTimer(context); }
294         ~eTimer() { if (bActive) stop(); }
295
296         PSignal0<void> timeout;
297
298         bool isActive() { return bActive; }
299
300         timespec &getNextActivation() { return nextActivation; }
301
302         void start(long msec, bool b=false);
303         void stop();
304         void changeInterval(long msek);
305         void startLongTimer( int seconds );
306         bool operator<(const eTimer& t) const { return nextActivation < t.nextActivation; }
307         eSmartPtrList<iObject> m_clients;
308 };
309 #endif  // SWIG
310
311 #endif