Free memory with dbus_free instead of free. Account for NULL signatures.
[dbus-cxx-async.git] / src / eventloop-integration.cpp
blob7f66cc407b4b0920d7ab1b28970d6b48e616951e
1 /*
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
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #include <dbus-c++/eventloop-integration.h>
29 #include <dbus-c++/debug.h>
31 #include <sys/poll.h>
33 #include <dbus/dbus.h>
35 DBus::DefaultDispatcher DBus::DefaultDispatcher::instance;
37 using namespace DBus;
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)
58 flags |= POLLIN;
59 if (Watch::flags() & DBUS_WATCH_WRITABLE)
60 flags |= POLLOUT;
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);
77 start_iteration();
79 while (_running)
81 do_iteration();
84 debug_log("leaving dispatcher %p", this);
87 void DefaultDispatcher::leave()
89 _running = false;
92 void DefaultDispatcher::start_iteration()
94 _running = true;
97 void DefaultDispatcher::do_iteration()
99 dispatch_pending();
100 dispatch();
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);
108 bt->data(bt);
110 debug_log("added timeout %p (%s) interval=%d",
111 bt, ((Timeout *)bt)->enabled() ? "on":"off", ((Timeout *)bt)->interval());
113 return bt;
116 void DefaultDispatcher::rem_timeout(Timeout *t)
118 delete 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);
128 bw->data(bw);
130 debug_log("added watch %p (%s) fd=%d flags=%d",
131 bw, ((Watch *)bw)->enabled() ? "on":"off", ((Watch *)bw)->descriptor(), ((Watch *)bw)->flags());
133 return bw;
136 void DefaultDispatcher::rem_watch(Watch *w)
138 delete 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());
149 timeout->handle();
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()
160 int flags = 0;
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);