some python import cleanups
[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 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 int timeval_to_usec(const timeval &t1)
127 {
128         return t1.tv_sec*1000000 + t1.tv_usec;
129 }
130 #endif
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         friend class eTimer;
180         friend class eSocketNotifier;
181         std::map<int, eSocketNotifier*> notifiers, new_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, ePyObject additional=ePyObject());
186         int retval;
187         pthread_mutex_t recalcLock;
188         
189         int m_now_is_invalid;
190         int m_interrupt_requested;
191         void addSocketNotifier(eSocketNotifier *sn);
192         void removeSocketNotifier(eSocketNotifier *sn);
193         void addTimer(eTimer* e);
194         void removeTimer(eTimer* e);
195 public:
196         static void addTimeOffset(int offset);
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), m_interrupt_requested(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, SWIG_PYOBJECT(ePyObject) additional=(PyObject*)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(SWIG_PYOBJECT(ePyObject) dict, SWIG_PYOBJECT(ePyObject) timeout);
233         void interruptPoll();
234         void reset();
235 };
236
237 /**
238  * \brief The application class.
239  *
240  * An application provides a mainloop, and runs in the primary thread.
241  * You can have other threads, too, but this is the primary one.
242  */
243 class eApplication: public eMainloop
244 {
245 public:
246         eApplication()
247         {
248                 if (!eApp)
249                         eApp = this;
250         }
251         ~eApplication()
252         {
253                 eApp = 0;
254         }
255 };
256
257                                 // ... und Timer
258 /**
259  * \brief Gives a callback after a specified timeout.
260  *
261  * This class emits the signal \c eTimer::timeout after the specified timeout.
262  */
263 class eTimer
264 {
265         friend class eMainloop;
266         eMainloop &context;
267         timeval nextActivation;
268         long interval;
269         bool bSingleShot;
270         bool bActive;
271         void addTimeOffset(int);
272 public:
273         /**
274          * \brief Constructs a timer.
275          *
276          * The timer is not yet active, it has to be started with \c start.
277          * \param context The thread from which the signal should be emitted.
278          */
279         eTimer(eMainloop *context=eApp): context(*context), bActive(false) { }
280         ~eTimer() { if (bActive) stop(); }
281
282         PSignal0<void> timeout;
283         void activate();
284
285         bool isActive() { return bActive; }
286         timeval &getNextActivation() { return nextActivation; }
287
288         void start(long msec, bool b=false);
289         void stop();
290         void changeInterval(long msek);
291 #ifndef SWIG
292         bool operator<(const eTimer& t) const { return nextActivation < t.nextActivation; }
293 #endif
294         void startLongTimer( int seconds );
295 };
296 #endif