openATV enigma2
openATV is an open source SetTopBox Graphical user interface.
ebase.h
Go to the documentation of this file.
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 class eMainloop;
20 class eTimer;
21 
22 extern eApplication* eApp;
23 
24 #ifndef SWIG
25 /* TODO: remove these inlines. */
26 static inline bool operator<( const timespec &t1, const timespec &t2 )
27 {
28  return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec);
29 }
30 
31 static inline bool operator<=( const timespec &t1, const timespec &t2 )
32 {
33  return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_nsec <= t2.tv_nsec);
34 }
35 
36 static inline timespec &operator+=( timespec &t1, const timespec &t2 )
37 {
38  t1.tv_sec += t2.tv_sec;
39  if ( (t1.tv_nsec += t2.tv_nsec) >= 1000000000 )
40  {
41  t1.tv_sec++;
42  t1.tv_nsec -= 1000000000;
43  }
44  return t1;
45 }
46 
47 static inline timespec operator+( const timespec &t1, const timespec &t2 )
48 {
49  timespec tmp;
50  tmp.tv_sec = t1.tv_sec + t2.tv_sec;
51  if ( (tmp.tv_nsec = t1.tv_nsec + t2.tv_nsec) >= 1000000000 )
52  {
53  tmp.tv_sec++;
54  tmp.tv_nsec -= 1000000000;
55  }
56  return tmp;
57 }
58 
59 static inline timespec operator-( const timespec &t1, const timespec &t2 )
60 {
61  timespec tmp;
62  tmp.tv_sec = t1.tv_sec - t2.tv_sec;
63  if ( (tmp.tv_nsec = t1.tv_nsec - t2.tv_nsec) < 0 )
64  {
65  tmp.tv_sec--;
66  tmp.tv_nsec += 1000000000;
67  }
68  return tmp;
69 }
70 
71 static inline timespec operator-=( timespec &t1, const timespec &t2 )
72 {
73  t1.tv_sec -= t2.tv_sec;
74  if ( (t1.tv_nsec -= t2.tv_nsec) < 0 )
75  {
76  t1.tv_sec--;
77  t1.tv_nsec += 1000000000;
78  }
79  return t1;
80 }
81 
82 static inline timespec &operator+=( timespec &t1, const long msek )
83 {
84  t1.tv_sec += msek / 1000;
85  if ( (t1.tv_nsec += (msek % 1000) * 1000000) >= 1000000000 )
86  {
87  t1.tv_sec++;
88  t1.tv_nsec -= 1000000000;
89  }
90  return t1;
91 }
92 
93 static inline timespec operator+( const timespec &t1, const long msek )
94 {
95  timespec tmp;
96  tmp.tv_sec = t1.tv_sec + msek / 1000;
97  if ( (tmp.tv_nsec = t1.tv_nsec + (msek % 1000) * 1000000) >= 1000000000 )
98  {
99  tmp.tv_sec++;
100  tmp.tv_nsec -= 1000000;
101  }
102  return tmp;
103 }
104 
105 static inline timespec operator-( const timespec &t1, const long msek )
106 {
107  timespec tmp;
108  tmp.tv_sec = t1.tv_sec - msek / 1000;
109  if ( (tmp.tv_nsec = t1.tv_nsec - (msek % 1000)*1000000) < 0 )
110  {
111  tmp.tv_sec--;
112  tmp.tv_nsec += 1000000000;
113  }
114  return tmp;
115 }
116 
117 static inline timespec operator-=( timespec &t1, const long msek )
118 {
119  t1.tv_sec -= msek / 1000;
120  if ( (t1.tv_nsec -= (msek % 1000) * 1000000) < 0 )
121  {
122  t1.tv_sec--;
123  t1.tv_nsec += 1000000000;
124  }
125  return t1;
126 }
127 
128 static inline long timeout_usec ( const timespec & orig )
129 {
130  timespec now, diff;
131  clock_gettime(CLOCK_MONOTONIC, &now);
132  diff = orig - now;
133  if (diff.tv_sec > 2000)
134  return 2000 * 1000 * 1000;
135  return diff.tv_sec * 1000000 + diff.tv_nsec / 1000;
136 }
137 
138 
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;
156 
157  void activate(int what) { /*emit*/ activated(what); }
158  eSocketNotifier(eMainloop *context, int fd, int req, bool startnow);
159 public:
167  static eSocketNotifier* create(eMainloop *context, int fd, int req, bool startnow=true) { return new eSocketNotifier(context, fd, req, startnow); }
169 
171 
172  void start();
173  void stop();
174  bool isRunning() const { return state != 0; }
175 
176  int getFD() const { return fd; }
177  int getRequested() const { return requested; }
178  void setRequested(int req) { requested=req; }
179 
180  /* Only eConsoleAppContainer uses this, purpose unkown */
182 };
183 
184 #endif
185 
187 {
188  friend class eTimer;
189  friend class eSocketNotifier;
190 
191  std::map<int, eSocketNotifier*> notifiers;
192  ePtrList<eTimer> m_timer_list;
193  bool app_quit_now;
194  int retval;
195  eSocketNotifier *m_inActivate;
196  int m_interrupt_requested;
197  timespec m_twisted_timer;
198 
199  /* user_timeout < 0 - forever
200  * user_timeout = 0 - immediately
201  * user_timeout > 0 - wait
202  */
203  int processOneEvent(long user_timeout, PyObject **res=0, ePyObject additional=ePyObject());
204  void addSocketNotifier(eSocketNotifier *sn);
205  void removeSocketNotifier(eSocketNotifier *sn);
206  void addTimer(eTimer* e);
207  void removeTimer(eTimer* e);
208  static ePtrList<eMainloop> existing_loops;
209  static bool isValid(eMainloop *);
210 public:
212  :app_quit_now(0), retval(0), m_inActivate(0), m_interrupt_requested(0)
213  {
214  existing_loops.push_back(this);
215  }
216  virtual ~eMainloop();
217 
218 #ifndef SWIG
219  void quit(int ret=0); // leave all pending loops (recursive leave())
220 #endif
221 
222  /* a user supplied timeout. enter_loop will return with:
223  0 - no timeout, no signal
224  1 - timeout
225  2 - signal
226  */
227  int iterate(unsigned int timeout=0, PyObject **res=0, SWIG_PYOBJECT(ePyObject) additional=(PyObject*)0);
228 
229  /* run will iterate endlessly until the app is quit, and return
230  the exit code */
231  int runLoop();
232 
233  /* our new shared polling interface. */
234  PyObject *poll(SWIG_PYOBJECT(ePyObject) dict, SWIG_PYOBJECT(ePyObject) timeout);
235  void interruptPoll();
236  void reset();
237 
238 protected:
239  virtual int _poll(struct pollfd *fds, nfds_t nfds, int timeout);
240 };
241 
248 class eApplication: public eMainloop
249 {
250  int m_is_idle;
251  int m_idle_count;
252 public:
254  m_is_idle(0), m_idle_count(0)
255  {
256  if (!eApp)
257  eApp = this;
258  }
260  {
261  eApp = 0;
262  }
263  int isIdle() const { return m_is_idle; }
264  int idleCount() const { return m_idle_count; }
265 protected:
266  /* override */ int _poll(struct pollfd *fds, nfds_t nfds, int timeout);
267 };
268 
269 #ifndef SWIG
270 
277 {
278  DECLARE_REF(eTimer);
279  friend class eMainloop;
280 
281  eMainloop &context;
282  timespec nextActivation;
283  long interval;
284  bool bSingleShot;
285  bool bActive;
286 
287  void activate();
288  eTimer(eMainloop *context): context(*context), bActive(false) { }
289 public:
296  static eTimer *create(eMainloop *context=eApp) { return new eTimer(context); }
297  ~eTimer() { if (bActive) stop(); }
298 
300 
301  bool isActive() { return bActive; }
302 
303  timespec &getNextActivation() { return nextActivation; }
304  bool needsActivation(const timespec &now) { return nextActivation <= now; }
305  bool needsActivation() { timespec now; clock_gettime(CLOCK_MONOTONIC, &now); return nextActivation <= now; }
306 
307  void start(long msec, bool b=false);
308  void stop();
309  void changeInterval(long msek);
310  void startLongTimer(int seconds);
311  bool operator<(const eTimer& t) const { return nextActivation < t.nextActivation; }
312 };
313 #endif // SWIG
314 
315 #endif
The application class.
Definition: ebase.h:249
eApplication()
Definition: ebase.h:253
int _poll(struct pollfd *fds, nfds_t nfds, int timeout)
Definition: ebase.cpp:400
~eApplication()
Definition: ebase.h:259
int idleCount() const
Definition: ebase.h:264
int isIdle() const
Definition: ebase.h:263
Definition: ebase.h:187
int runLoop()
Definition: ebase.cpp:356
friend class eTimer
Definition: ebase.h:188
void interruptPoll()
Definition: ebase.cpp:384
int iterate(unsigned int timeout=0, PyObject **res=0, SWIG_PYOBJECT(ePyObject) additional=(PyObject *) 0)
Definition: ebase.cpp:318
eMainloop()
Definition: ebase.h:211
friend class eSocketNotifier
Definition: ebase.h:189
void reset()
Definition: ebase.cpp:363
virtual ~eMainloop()
Definition: ebase.cpp:132
PyObject * poll(SWIG_PYOBJECT(ePyObject) dict, SWIG_PYOBJECT(ePyObject) timeout)
Definition: ebase.cpp:368
void quit(int ret=0)
Definition: ebase.cpp:389
virtual int _poll(struct pollfd *fds, nfds_t nfds, int timeout)
Definition: ebase.cpp:395
void push_back(T *)
Definition: eptrlist.h:458
Definition: python.h:31
Gives a callback when data on a file descriptor is ready.
Definition: ebase.h:146
@ Priority
Definition: ebase.h:150
@ Read
Definition: ebase.h:150
@ Write
Definition: ebase.h:150
@ Hungup
Definition: ebase.h:150
@ Error
Definition: ebase.h:150
void start()
Definition: ebase.cpp:25
int getRequested() const
Definition: ebase.h:177
int getFD() const
Definition: ebase.h:176
static eSocketNotifier * create(eMainloop *context, int fd, int req, bool startnow=true)
Constructs a eSocketNotifier.
Definition: ebase.h:167
void setRequested(int req)
Definition: ebase.h:178
~eSocketNotifier()
Definition: ebase.cpp:20
void stop()
Definition: ebase.cpp:37
bool isRunning() const
Definition: ebase.h:174
PSignal1< void, int > activated
Definition: ebase.h:170
eSmartPtrList< iObject > m_clients
Definition: ebase.h:181
Gives a callback after a specified timeout.
Definition: ebase.h:277
void start(long msec, bool b=false)
Definition: ebase.cpp:48
void changeInterval(long msek)
Definition: ebase.cpp:93
bool needsActivation()
Definition: ebase.h:305
bool isActive()
Definition: ebase.h:301
timespec & getNextActivation()
Definition: ebase.h:303
~eTimer()
Definition: ebase.h:297
void stop()
Definition: ebase.cpp:84
PSignal0< void > timeout
Definition: ebase.h:299
bool needsActivation(const timespec &now)
Definition: ebase.h:304
bool operator<(const eTimer &t) const
Definition: ebase.h:311
void startLongTimer(int seconds)
Definition: ebase.cpp:66
static eTimer * create(eMainloop *context=eApp)
Constructs a timer.
Definition: ebase.h:296
Definition: object.h:15
static long timeout_usec(const timespec &orig)
Definition: ebase.h:128
static timespec & operator+=(timespec &t1, const timespec &t2)
Definition: ebase.h:36
static bool operator<(const timespec &t1, const timespec &t2)
Definition: ebase.h:26
static bool operator<=(const timespec &t1, const timespec &t2)
Definition: ebase.h:31
static timespec operator-=(timespec &t1, const timespec &t2)
Definition: ebase.h:71
static timespec operator-(const timespec &t1, const timespec &t2)
Definition: ebase.h:59
eApplication * eApp
Definition: ebase.cpp:416
static timespec operator+(const timespec &t1, const timespec &t2)
Definition: ebase.h:47
activate
Definition: SystemPlugins/FastChannelChange/plugin.py:14
#define SWIG_PYOBJECT(x)
Definition: swig.h:23