swift
Loading...
Searching...
No Matches
dbusdispatcher.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
4#include "dbusdispatcher.h"
5
6#include <algorithm>
7
8#include "dbusconnection.h"
9
10namespace XSwiftBus
11{
12
15 {
17 void operator()(event *obj) const
18 {
19 event_del(obj);
20 event_free(obj);
21 }
22 };
23
26 {
27 public:
29 WatchHandler(event_base *base, DBusWatch *watch) : m_base(base), m_watch(watch)
30 {
31 const unsigned int flags = dbus_watch_get_flags(watch);
32 short monitoredEvents = EV_PERSIST;
33
34 if (flags & DBUS_WATCH_READABLE)
35 {
36 monitoredEvents |= EV_READ;
37 }
38 if (flags & DBUS_WATCH_WRITABLE)
39 {
40 monitoredEvents |= EV_WRITE;
41 }
42
43 const int fd = dbus_watch_get_unix_fd(watch);
44 m_event.reset(event_new(m_base, fd, monitoredEvents, callback, this));
45 event_add(m_event.get(), nullptr);
46 }
47
49 DBusWatch *getWatch() { return m_watch; }
50
52 const DBusWatch *getWatch() const { return m_watch; }
53
54 private:
56 static void callback(evutil_socket_t fd, short event, void *data)
57 {
58 (void)fd; // Not really unused, but GCC/Clang still complain about it.
59 auto *watchHandler = static_cast<WatchHandler *>(data);
60
61 unsigned int flags = 0;
62 if (event & EV_READ)
63 {
64 flags |= DBUS_WATCH_READABLE;
65 }
66 if (event & EV_WRITE)
67 {
68 flags |= DBUS_WATCH_WRITABLE;
69 }
70 dbus_watch_handle(watchHandler->m_watch, flags);
71 }
72
73 event_base *m_base = nullptr;
74 std::unique_ptr<event, EventDeleter> m_event;
75 DBusWatch *m_watch = nullptr;
76 };
77
80 {
81 public:
83 TimeoutHandler(event_base *base, DBusTimeout *timeout) : m_base(base), m_timeout(timeout)
84 {
85 timeval timer;
86 const int interval = dbus_timeout_get_interval(timeout);
87 timer.tv_sec = interval / 1000;
88 timer.tv_usec = (interval % 1000) * 1000;
89
90 m_event.reset(evtimer_new(m_base, callback, this));
91 evtimer_add(m_event.get(), &timer);
92 }
93
95 const DBusTimeout *getTimeout() const { return m_timeout; }
96
97 private:
99 static void callback(evutil_socket_t fd, short event, void *data)
100 {
101 (void)fd; // unused
102 (void)event; // unused
103 auto *timeoutHandler = static_cast<TimeoutHandler *>(data);
104 dbus_timeout_handle(timeoutHandler->m_timeout);
105 }
106
107 event_base *m_base = nullptr;
108 std::unique_ptr<event, EventDeleter> m_event;
109 DBusTimeout *m_timeout = nullptr;
110 };
111
113 class Timer
114 {
115 public:
116 Timer() = default;
118 Timer(event_base *base, const timeval &timeout, const std::function<void()> &func) : m_base(base), m_func(func)
119 {
120 m_event.reset(evtimer_new(m_base, callback, this));
121 evtimer_add(m_event.get(), &timeout);
122 }
123
124 private:
126 static void callback(evutil_socket_t fd, short event, void *data)
127 {
128 (void)fd; // unused
129 (void)event; // unused
130 auto *timer = static_cast<Timer *>(data);
131 timer->m_func();
132 delete timer;
133 }
134
135 event_base *m_base = nullptr;
136 std::unique_ptr<event, EventDeleter> m_event;
137 std::function<void()> m_func;
138 };
139
140 CDBusDispatcher::CDBusDispatcher() : m_eventBase(event_base_new())
141 {
142 using namespace std::placeholders;
143 m_watchCallbacks = WatchCallbacks(std::bind(&CDBusDispatcher::dbusAddWatch, this, _1),
144 std::bind(&CDBusDispatcher::dbusRemoveWatch, this, _1),
145 std::bind(&CDBusDispatcher::dbusWatchToggled, this, _1));
146
147 m_timeoutCallbacks = TimeoutCallbacks(std::bind(&CDBusDispatcher::dbusAddTimeout, this, _1),
148 std::bind(&CDBusDispatcher::dbusRemoveTimeout, this, _1),
149 std::bind(&CDBusDispatcher::dbusTimeoutToggled, this, _1));
150 }
151
153
154 void CDBusDispatcher::add(IDispatchable *dispatchable) { m_dispatchList.push_back(dispatchable); }
155
157 {
158 auto it = std::find(m_dispatchList.begin(), m_dispatchList.end(), dispatchable);
159 if (it != m_dispatchList.end())
160 {
161 m_dispatchList.erase(it);
162 }
163 }
164
166 {
167 if (!m_eventBase)
168 {
169 return;
170 }
171 event_base_dispatch(m_eventBase.get());
172 }
173
175 {
176 if (!m_eventBase)
177 {
178 return;
179 }
180 event_base_loop(m_eventBase.get(), EVLOOP_NONBLOCK);
181 dispatch();
182 }
183
184 void CDBusDispatcher::dispatch()
185 {
186 if (m_dispatchList.empty())
187 {
188 return;
189 }
190
191 for (IDispatchable *dispatchable : m_dispatchList)
192 {
193 dispatchable->dispatch();
194 }
195 }
196
197 dbus_bool_t CDBusDispatcher::dbusAddWatch(DBusWatch *watch)
198 {
199 if (dbus_watch_get_enabled(watch) == FALSE)
200 {
201 return true;
202 }
203
204 int fd = dbus_watch_get_unix_fd(watch);
205 m_watchers.emplace(fd, std::make_unique<WatchHandler>(m_eventBase.get(), watch));
206 return true;
207 }
208
209 void CDBusDispatcher::dbusRemoveWatch(const DBusWatch *watch)
210 {
211 for (auto it = m_watchers.begin(); it != m_watchers.end();)
212 {
213 if (it->second->getWatch() == watch)
214 {
215 it = m_watchers.erase(it);
216 }
217 else
218 {
219 ++it;
220 }
221 }
222 }
223
224 void CDBusDispatcher::dbusWatchToggled(DBusWatch *watch)
225 {
226 if (dbus_watch_get_enabled(watch) == TRUE)
227 {
228 dbusAddWatch(watch);
229 }
230 else
231 {
232 dbusRemoveWatch(watch);
233 }
234 }
235
236 dbus_bool_t CDBusDispatcher::dbusAddTimeout(DBusTimeout *timeout)
237 {
238 if (dbus_timeout_get_enabled(timeout) == FALSE)
239 {
240 return TRUE;
241 }
242 m_timeouts.emplace(m_timeouts.end(), std::make_unique<TimeoutHandler>(m_eventBase.get(), timeout));
243 return true;
244 }
245
246 void CDBusDispatcher::dbusRemoveTimeout(DBusTimeout *timeout)
247 {
248 auto predicate = [timeout](const std::unique_ptr<TimeoutHandler> &ptr) { return ptr->getTimeout() == timeout; };
249
250 m_timeouts.erase(std::remove_if(m_timeouts.begin(), m_timeouts.end(), predicate), m_timeouts.end());
251 }
252
253 void CDBusDispatcher::dbusTimeoutToggled(DBusTimeout *timeout)
254 {
255 if (dbus_timeout_get_enabled(timeout) == TRUE)
256 dbusAddTimeout(timeout);
257 else
258 dbusRemoveTimeout(timeout);
259 }
260
261} // namespace XSwiftBus
void add(IDispatchable *dispatchable)
Add dispatchable object.
virtual ~CDBusDispatcher()
Destructor.
void remove(IDispatchable *dispatchable)
Remove dispatchable object.
void runOnce()
Dispatches ready handlers and returns without waiting.
void waitAndRun()
Waits for events to be dispatched and handles them.
Dispatchable Interface.
DBus timeout handler.
const DBusTimeout * getTimeout() const
Get DBus timeout.
TimeoutHandler(event_base *base, DBusTimeout *timeout)
Constructor.
Timer(event_base *base, const timeval &timeout, const std::function< void()> &func)
Constructor.
DBus watch handler.
WatchHandler(event_base *base, DBusWatch *watch)
Constructor.
DBusWatch * getWatch()
Get DBus watch.
const DBusWatch * getWatch() const
Get DBus watch.
Plugin loaded by X-Plane which publishes a DBus service.
Definition command.h:14
Functor struct deleteing an event.
void operator()(event *obj) const
Delete functor.