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