1 /*
2  *
3  *  D-Bus++ - C++ bindings for D-Bus
4  *
5  *  Copyright (C) 2005-2007  Paolo Durante <shackan@gmail.com>
6  *
7  *
8  *  This library is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU Lesser General Public
10  *  License as published by the Free Software Foundation; either
11  *  version 2.1 of the License, or (at your option) any later version.
12  *
13  *  This library is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public
19  *  License along with this library; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 
24 
25 #ifndef __DBUSXX_EVENTLOOP_H
26 #define __DBUSXX_EVENTLOOP_H
27 
28 #include <pthread.h>
29 #include <list>
30 
31 #include "api.h"
32 #include "util.h"
33 
34 namespace DBus
35 {
36 
37 /*
38  * these Default *classes implement a very simple event loop which
39  * is used here as the default main loop, if you want to hook
40  * a different one use the Bus *classes in eventloop-integration.h
41  * or the Glib::Bus *classes as a reference
42  */
43 
44 class DefaultMainLoop;
45 
46 class DXXAPI DefaultTimeout
47 {
48 public:
49 
50   DefaultTimeout(int interval, bool repeat, DefaultMainLoop *);
51 
52   virtual ~DefaultTimeout();
53 
enabled()54   bool enabled()
55   {
56     return _enabled;
57   }
enabled(bool e)58   void enabled(bool e)
59   {
60     _enabled = e;
61   }
62 
interval()63   int interval()
64   {
65     return _interval;
66   }
interval(int i)67   void interval(int i)
68   {
69     _interval = i;
70   }
71 
repeat()72   bool repeat()
73   {
74     return _repeat;
75   }
repeat(bool r)76   void repeat(bool r)
77   {
78     _repeat = r;
79   }
80 
data()81   void *data()
82   {
83     return _data;
84   }
data(void * d)85   void data(void *d)
86   {
87     _data = d;
88   }
89 
90   Slot<void, DefaultTimeout &> expired;
91 
92 private:
93 
94   bool _enabled;
95 
96   int _interval;
97   bool _repeat;
98 
99   double _expiration;
100 
101   void *_data;
102 
103   DefaultMainLoop *_disp;
104 
105   friend class DefaultMainLoop;
106 };
107 
108 typedef std::list< DefaultTimeout *> DefaultTimeouts;
109 
110 class DXXAPI DefaultWatch
111 {
112 public:
113 
114   DefaultWatch(int fd, int flags, DefaultMainLoop *);
115 
116   virtual ~DefaultWatch();
117 
enabled()118   bool enabled()
119   {
120     return _enabled;
121   }
enabled(bool e)122   void enabled(bool e)
123   {
124     _enabled = e;
125   }
126 
descriptor()127   int descriptor()
128   {
129     return _fd;
130   }
131 
flags()132   int flags()
133   {
134     return _flags;
135   }
flags(int f)136   void flags(int f)
137   {
138     _flags = f;
139   }
140 
state()141   int state()
142   {
143     return _state;
144   }
145 
data()146   void *data()
147   {
148     return _data;
149   }
data(void * d)150   void data(void *d)
151   {
152     _data = d;
153   }
154 
155   Slot<void, DefaultWatch &> ready;
156 
157 private:
158 
159   bool _enabled;
160 
161   int _fd;
162   int _flags;
163   int _state;
164 
165   void *_data;
166 
167   DefaultMainLoop *_disp;
168 
169   friend class DefaultMainLoop;
170 };
171 
172 typedef std::list< DefaultWatch *> DefaultWatches;
173 
174 class DXXAPI DefaultMutex
175 {
176 public:
177 
178   /*!
179    * Constructor for non recursive Mutex
180    */
181   DefaultMutex();
182 
183   /*!
184    * Constructor
185    * \param recursive Set if Mutex should be recursive or not.
186    */
187   DefaultMutex(bool recursive);
188 
189   ~DefaultMutex();
190 
191   void lock();
192 
193   void unlock();
194 
195 private:
196 
197   pthread_mutex_t _mutex;
198 };
199 
200 class DXXAPI DefaultMainLoop
201 {
202 public:
203 
204   DefaultMainLoop();
205 
206   virtual ~DefaultMainLoop();
207 
208   virtual void dispatch();
209 
210   int _fdunlock[2];
211 private:
212 
213   DefaultMutex _mutex_t;
214   DefaultTimeouts _timeouts;
215 
216   DefaultMutex _mutex_w;
217   DefaultWatches _watches;
218 
219   friend class DefaultTimeout;
220   friend class DefaultWatch;
221 };
222 
223 } /* namespace DBus */
224 
225 #endif//__DBUSXX_EVENTLOOP_H
226