3 * D-Bus++ - C++ bindings for D-Bus
5 * Copyright (C) 2005-2009 Paolo Durante <shackan@gmail.com>
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.
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.
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
28 #include <dbus-c++/eventloop-integration.h>
29 #include <dbus-c++/debug.h>
33 #include <dbus/dbus.h>
35 DBus::DefaultDispatcher
DBus::DefaultDispatcher::instance
;
39 DefaultTimeout::DefaultTimeout(Timeout::Internal
*ti
, DefaultDispatcher
*bd
)
40 : Timeout(ti
), SimpleTimeout(Timeout::interval(), true, bd
)
42 SimpleTimeout::enabled(Timeout::enabled());
45 void DefaultTimeout::toggle()
47 debug_log("timeout %p toggled (%s)", this, Timeout::enabled() ? "on":"off");
49 SimpleTimeout::enabled(Timeout::enabled());
52 DefaultWatch::DefaultWatch(Watch::Internal
*wi
, DefaultDispatcher
*bd
)
53 : Watch(wi
), SimpleWatch(Watch::descriptor(), 0, bd
)
55 int flags
= POLLHUP
| POLLERR
;
57 if (Watch::flags() & DBUS_WATCH_READABLE
)
59 if (Watch::flags() & DBUS_WATCH_WRITABLE
)
62 SimpleWatch::flags(flags
);
63 SimpleWatch::enabled(Watch::enabled());
66 void DefaultWatch::toggle()
68 debug_log("watch %p toggled (%s)", this, Watch::enabled() ? "on":"off");
70 SimpleWatch::enabled(Watch::enabled());
73 void DefaultDispatcher::enter()
75 debug_log("entering dispatcher %p", this);
84 debug_log("leaving dispatcher %p", this);
87 void DefaultDispatcher::leave()
92 void DefaultDispatcher::start_iteration()
97 void DefaultDispatcher::do_iteration()
103 Timeout
*DefaultDispatcher::add_timeout(Timeout::Internal
*ti
)
105 DefaultTimeout
*bt
= new DefaultTimeout(ti
, this);
107 bt
->expired
= new CallbackNoReturn
<DefaultDispatcher
, SimpleTimeout
&>(this, &DefaultDispatcher::timeout_expired
);
110 debug_log("added timeout %p (%s) interval=%d",
111 bt
, ((Timeout
*)bt
)->enabled() ? "on":"off", ((Timeout
*)bt
)->interval());
116 void DefaultDispatcher::rem_timeout(Timeout
*t
)
120 debug_log("removed timeout %p", t
);
123 Watch
*DefaultDispatcher::add_watch(Watch::Internal
*wi
)
125 DefaultWatch
*bw
= new DefaultWatch(wi
, this);
127 bw
->ready
= new CallbackNoReturn
<DefaultDispatcher
, SimpleWatch
&>(this, &DefaultDispatcher::watch_ready
);
130 debug_log("added watch %p (%s) fd=%d flags=%d",
131 bw
, ((Watch
*)bw
)->enabled() ? "on":"off", ((Watch
*)bw
)->descriptor(), ((Watch
*)bw
)->flags());
136 void DefaultDispatcher::rem_watch(Watch
*w
)
140 debug_log("removed watch %p", w
);
143 void DefaultDispatcher::timeout_expired(SimpleTimeout
&et
)
145 debug_log("timeout %p expired", &et
);
147 DefaultTimeout
*timeout
= reinterpret_cast<DefaultTimeout
*>(et
.data());
152 void DefaultDispatcher::watch_ready(SimpleWatch
&ew
)
154 DefaultWatch
*watch
= reinterpret_cast<DefaultWatch
*>(ew
.data());
156 debug_log("watch %p ready, flags=%d state=%d",
157 watch
, ((Watch
*)watch
)->flags(), watch
->state()
162 if (watch
->state() & POLLIN
)
163 flags
|= DBUS_WATCH_READABLE
;
164 if (watch
->state() & POLLOUT
)
165 flags
|= DBUS_WATCH_WRITABLE
;
166 if (watch
->state() & POLLHUP
)
167 flags
|= DBUS_WATCH_HANGUP
;
168 if (watch
->state() & POLLERR
)
169 flags
|= DBUS_WATCH_ERROR
;
171 watch
->handle(flags
);