1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
//
// C++ Implementation: k9dbusdispatch
//
// Description:
//
//
// Author: Jean-Michel PETIT <[email protected]>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "k9dbusdispatch.h"
#ifdef HAVE_HAL
#include <dbus/dbus.h>
//==============================callbacks ========================================
static dbus_bool_t qDBusAddWatch(DBusWatch *watch, void *data)
{
int flags = dbus_watch_get_flags(watch);
int fd = dbus_watch_get_fd(watch);
K9DBusDispatch *d=(K9DBusDispatch*)data;
k9Watcher watcher;
if (flags & DBUS_WATCH_READABLE) {
bool enabled = dbus_watch_get_enabled(watch);
//qDebug("addReadWatch %d %s", fd, (enabled ? "enabled" : "disabled"));
watcher.watch = watch;
watcher.read = new TQSocketNotifier(fd, TQSocketNotifier::Read, d);
if (!enabled) watcher.read->setEnabled(false);
d->connect(watcher.read, TQT_SIGNAL(activated(int)), TQT_SLOT(socketRead(int)));
}
if (flags & DBUS_WATCH_WRITABLE) {
bool enabled = dbus_watch_get_enabled(watch);
//qDebug("addWriteWatch %d %s", fd, (enabled ? "enabled" : "disabled"));
watcher.watch = watch;
watcher.write = new TQSocketNotifier(fd, TQSocketNotifier::Write, d);
if (!enabled) watcher.write->setEnabled(false);
d->connect(watcher.write, TQT_SIGNAL(activated(int)), TQT_SLOT(socketWrite(int)));
}
// FIXME-QT4 d->watchers.insertMulti(fd, watcher);
K9DBusDispatch::WatcherHash::iterator it = d->watchers.tqfind(fd);
if (it == d->watchers.end())
{
it = d->watchers.insert(fd, K9DBusDispatch::WatcherList());
}
it.data().append(watcher);
return true;
}
static void qDBusRemoveWatch(DBusWatch *watch, void *data)
{
K9DBusDispatch *d = (K9DBusDispatch*)data;
int fd = dbus_watch_get_fd(watch);
K9DBusDispatch::WatcherHash::iterator it = d->watchers.tqfind(fd);
if (it != d->watchers.end())
{
K9DBusDispatch::WatcherList& list = *it;
for (K9DBusDispatch::WatcherList::iterator wit = list.begin();
wit != list.end(); ++wit)
{
if ((*wit).watch == watch)
{
// migth be called from a function triggered by a socket listener
// so just disconnect them and schedule their delayed deletion.
d->removedWatches.append(*wit);
if ((*wit).read)
{
(*wit).read->disconnect(d);
(*wit).read = 0;
}
if ((*wit).write)
{
(*wit).write->disconnect(d);
(*wit).write = 0;
}
(*wit).watch = 0;
}
}
}
if (d->removedWatches.count() > 0)
TQTimer::singleShot(0, d, TQT_SLOT(purgeRemovedWatches()));
}
static void qDBusToggleWatch(DBusWatch *watch, void *data)
{
K9DBusDispatch *d=(K9DBusDispatch*)data;
int fd = dbus_watch_get_fd(watch);
K9DBusDispatch::WatcherHash::iterator it = d->watchers.tqfind(fd);
if (it != d->watchers.end()) {
K9DBusDispatch::WatcherList& list = *it;
for (K9DBusDispatch::WatcherList::iterator wit = list.begin(); wit != list.end();
++wit)
{
if ((*wit).watch == watch) {
bool enabled = dbus_watch_get_enabled(watch);
int flags = dbus_watch_get_flags(watch);
// qDebug("toggle watch %d to %d (write: %d, read: %d)",
// dbus_watch_get_fd(watch), enabled,
// flags & DBUS_WATCH_WRITABLE, flags & DBUS_WATCH_READABLE);
if (flags & DBUS_WATCH_READABLE && (*wit).read)
(*wit).read->setEnabled(enabled);
if (flags & DBUS_WATCH_WRITABLE && (*wit).write)
(*wit).write->setEnabled(enabled);
return;
}
}
}
}
void K9DBusDispatch::purgeRemovedWatches()
{
if (removedWatches.isEmpty()) return;
WatcherList::iterator listIt = removedWatches.begin();
for (; listIt != removedWatches.end(); ++listIt)
{
delete (*listIt).read;
delete (*listIt).write;
}
removedWatches.clear();
uint count = 0;
WatcherHash::iterator it = watchers.begin();
while (it != watchers.end())
{
WatcherList& list = *it;
listIt = list.begin();
while (listIt != list.end())
{
if (!((*listIt).read) && !((*listIt).write))
{
listIt = list.erase(listIt);
++count;
}
}
if (list.isEmpty())
{
WatcherHash::iterator copyIt = it;
++it;
watchers.erase(copyIt);
}
else
++it;
}
}
//==============================================================================
void K9DBusDispatch::socketRead(int fd)
{
// FIXME-QT4 TQHashIterator<int, TQDBusConnectionPrivate::Watcher> it(watchers);
WatcherHash::const_iterator it = watchers.tqfind(fd);
if (it != watchers.end()) {
const WatcherList& list = *it;
for (WatcherList::const_iterator wit = list.begin(); wit != list.end(); ++wit) {
if ((*wit).read && (*wit).read->isEnabled()) {
if (!dbus_watch_handle((*wit).watch, DBUS_WATCH_READABLE))
qDebug("OUT OF MEM");
}
}
}
scheduleDispatch();
}
void K9DBusDispatch::socketWrite(int fd)
{
// FIXME-QT4 TQHashIterator<int, TQDBusConnectionPrivate::Watcher> it(watchers);
WatcherHash::const_iterator it = watchers.tqfind(fd);
if (it != watchers.end()) {
const WatcherList& list = *it;
for (WatcherList::const_iterator wit = list.begin(); wit != list.end(); ++wit) {
if ((*wit).write && (*wit).write->isEnabled()) {
if (!dbus_watch_handle((*wit).watch, DBUS_WATCH_WRITABLE))
qDebug("OUT OF MEM");
}
}
}
}
void K9DBusDispatch::scheduleDispatch()
{
m_dispatcher->start(0);
}
void K9DBusDispatch::dispatch()
{
if (dbus_connection_dispatch(m_connection) != DBUS_DISPATCH_DATA_REMAINS)
{
// stop dispatch timer
m_dispatcher->stop();
}
}
K9DBusDispatch::K9DBusDispatch(TQObject *tqparent, const char *name)
: TQObject(tqparent, name)
{
m_dispatcher = new TQTimer(this);
TQObject::connect(m_dispatcher, TQT_SIGNAL(timeout()), this, TQT_SLOT(dispatch()));
}
K9DBusDispatch::~K9DBusDispatch()
{
}
void K9DBusDispatch::setConnection(DBusConnection* _value) {
m_connection = _value;
dbus_connection_set_exit_on_disconnect(m_connection, false);
dbus_connection_set_watch_functions(m_connection, qDBusAddWatch, qDBusRemoveWatch,
qDBusToggleWatch, this, 0);
}
#include "k9dbusdispatch.moc"
#endif
|