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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
// This is the SIP interface definition for the Qt support for the standard
// Python DBus bindings.
//
// Copyright (c) 2012 Riverbank Computing Limited
//
// Licensed under the Academic Free License version 2.1
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include <dbus/dbus-python.h>
#include <QCoreApplication>
#include <QSocketNotifier>
#include <QTimer>
#include <QTimerEvent>
#include "helper.h"
// The callback to add a watch.
extern "C" {static dbus_bool_t add_watch(DBusWatch *watch, void *data);}
static dbus_bool_t add_watch(DBusWatch *watch, void *data)
{
pyqtDBusHelper *hlp = reinterpret_cast<pyqtDBusHelper *>(data);
int fd = dbus_watch_get_fd(watch);
unsigned int flags = dbus_watch_get_flags(watch);
dbus_bool_t enabled = dbus_watch_get_enabled(watch);
pyqtDBusHelper::Watcher watcher;
watcher.watch = watch;
if (flags & DBUS_WATCH_READABLE)
{
watcher.read = new QSocketNotifier(fd, QSocketNotifier::Read, hlp);
watcher.read->setEnabled(enabled);
hlp->connect(watcher.read, SIGNAL(activated(int)), SLOT(readSocket(int)));
}
if (flags & DBUS_WATCH_WRITABLE)
{
watcher.write = new QSocketNotifier(fd, QSocketNotifier::Write, hlp);
watcher.write->setEnabled(enabled);
hlp->connect(watcher.write, SIGNAL(activated(int)), SLOT(writeSocket(int)));
}
hlp->watchers.insertMulti(fd, watcher);
return true;
}
// The callback to remove a watch.
extern "C" {static void remove_watch(DBusWatch *watch, void *data);}
static void remove_watch(DBusWatch *watch, void *data)
{
pyqtDBusHelper *hlp = reinterpret_cast<pyqtDBusHelper *>(data);
int fd = dbus_watch_get_fd(watch);
pyqtDBusHelper::Watchers::iterator it = hlp->watchers.find(fd);
while (it != hlp->watchers.end() && it.key() == fd)
{
pyqtDBusHelper::Watcher &watcher = it.value();
if (watcher.watch == watch)
{
if (watcher.read)
delete watcher.read;
if (watcher.write)
delete watcher.write;
hlp->watchers.erase(it);
return;
}
++it;
}
}
// The callback to toggle a watch.
extern "C" {static void toggle_watch(DBusWatch *watch, void *data);}
static void toggle_watch(DBusWatch *watch, void *data)
{
pyqtDBusHelper *hlp = reinterpret_cast<pyqtDBusHelper *>(data);
int fd = dbus_watch_get_fd(watch);
unsigned int flags = dbus_watch_get_flags(watch);
dbus_bool_t enabled = dbus_watch_get_enabled(watch);
pyqtDBusHelper::Watchers::const_iterator it = hlp->watchers.find(fd);
while (it != hlp->watchers.end() && it.key() == fd)
{
const pyqtDBusHelper::Watcher &watcher = it.value();
if (watcher.watch == watch)
{
if (flags & DBUS_WATCH_READABLE && watcher.read)
watcher.read->setEnabled(enabled);
if (flags & DBUS_WATCH_WRITABLE && watcher.write)
watcher.write->setEnabled(enabled);
return;
}
++it;
}
}
// The callback to add a timeout.
extern "C" {static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data);}
static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
{
// Nothing to do if the timeout is disabled.
if (!dbus_timeout_get_enabled(timeout))
return true;
// Pretend it is successful if there is no application instance. This can
// happen if the global application gets garbage collected before the
// dbus-python main loop does.
if (!QCoreApplication::instance())
return true;
pyqtDBusHelper *hlp = reinterpret_cast<pyqtDBusHelper *>(data);
int id = hlp->startTimer(dbus_timeout_get_interval(timeout));
if (!id)
return false;
hlp->timeouts[id] = timeout;
return true;
}
// The callback to remove a timeout.
extern "C" {static void remove_timeout(DBusTimeout *timeout, void *data);}
static void remove_timeout(DBusTimeout *timeout, void *data)
{
pyqtDBusHelper *hlp = reinterpret_cast<pyqtDBusHelper *>(data);
pyqtDBusHelper::Timeouts::iterator it = hlp->timeouts.begin();
while (it != hlp->timeouts.end())
if (it.value() == timeout)
{
hlp->killTimer(it.key());
it = hlp->timeouts.erase(it);
}
else
++it;
}
// The callback to toggle a timeout.
extern "C" {static void toggle_timeout(DBusTimeout *timeout, void *data);}
static void toggle_timeout(DBusTimeout *timeout, void *data)
{
remove_timeout(timeout, data);
add_timeout(timeout, data);
}
// The callback to delete a helper instance.
extern "C" {static void dbus_qt_delete_helper(void *data);}
static void dbus_qt_delete_helper(void *data)
{
delete reinterpret_cast<pyqtDBusHelper *>(data);
}
// The callback to wakeup the event loop.
extern "C" {static void wakeup_main(void *data);}
static void wakeup_main(void *data)
{
pyqtDBusHelper *hlp = reinterpret_cast<pyqtDBusHelper *>(data);
QTimer::singleShot(0, hlp, SLOT(dispatch()));
}
// The callback to set up a DBus connection.
extern "C" {static dbus_bool_t dbus_qt_conn(DBusConnection *conn, void *data);}
static dbus_bool_t dbus_qt_conn(DBusConnection *conn, void *data)
{
bool rc;
Py_BEGIN_ALLOW_THREADS
pyqtDBusHelper *hlp = reinterpret_cast<pyqtDBusHelper *>(data);
hlp->connections.append(conn);
if (!dbus_connection_set_watch_functions(conn, add_watch, remove_watch,
toggle_watch, data, 0))
rc = false;
else if (!dbus_connection_set_timeout_functions(conn, add_timeout,
remove_timeout, toggle_timeout, data, 0))
rc = false;
else
rc = true;
dbus_connection_set_wakeup_main_function(conn, wakeup_main, hlp, 0);
Py_END_ALLOW_THREADS
return rc;
}
// The callback to set up a DBus server.
extern "C" {static dbus_bool_t dbus_qt_srv(DBusServer *srv, void *data);}
static dbus_bool_t dbus_qt_srv(DBusServer *srv, void *data)
{
bool rc;
Py_BEGIN_ALLOW_THREADS
if (!dbus_server_set_watch_functions(srv, add_watch, remove_watch,
toggle_watch, data, 0))
rc = false;
else if (!dbus_server_set_timeout_functions(srv, add_timeout,
remove_timeout, toggle_timeout, data, 0))
rc = false;
else
rc = true;
Py_END_ALLOW_THREADS
return rc;
}
// Create a new helper instance.
pyqtDBusHelper::pyqtDBusHelper() : QObject()
{
}
// Handle a socket being ready to read.
void pyqtDBusHelper::readSocket(int fd)
{
Watchers::const_iterator it = watchers.find(fd);
while (it != watchers.end() && it.key() == fd)
{
const Watcher &watcher = it.value();
if (watcher.read && watcher.read->isEnabled())
{
watcher.read->setEnabled(false);
dbus_watch_handle(watcher.watch, DBUS_WATCH_READABLE);
// The notifier could have just been destroyed.
if (watcher.read)
watcher.read->setEnabled(true);
break;
}
++it;
}
dispatch();
}
// Handle a socket being ready to write.
void pyqtDBusHelper::writeSocket(int fd)
{
Watchers::const_iterator it = watchers.find(fd);
while (it != watchers.end() && it.key() == fd)
{
const Watcher &watcher = it.value();
if (watcher.write && watcher.write->isEnabled())
{
watcher.write->setEnabled(false);
dbus_watch_handle(watcher.watch, DBUS_WATCH_WRITABLE);
// The notifier could have just been destroyed.
if (watcher.write)
watcher.write->setEnabled(true);
break;
}
++it;
}
}
// Handoff to the connection dispatcher while there is data.
void pyqtDBusHelper::dispatch()
{
for (Connections::const_iterator it = connections.constBegin(); it != connections.constEnd(); ++it)
while (dbus_connection_dispatch(*it) == DBUS_DISPATCH_DATA_REMAINS)
;
}
// Reimplemented to handle timer events.
void pyqtDBusHelper::timerEvent(QTimerEvent *e)
{
DBusTimeout *timeout = timeouts.value(e->timerId());
if (timeout)
dbus_timeout_handle(timeout);
}
PyDoc_STRVAR(DBusQtMainLoop__doc__,
"DBusQtMainLoop([set_as_default=False]) -> NativeMainLoop\n"
"\n"
"Return a NativeMainLoop object.\n"
"\n"
"If the keyword argument set_as_default is given and is True, set the new\n"
"main loop as the default for all new Connection or Bus instances.\n");
extern "C" {static PyObject *DBusQtMainLoop(PyObject *, PyObject *args, PyObject *kwargs);}
static PyObject *DBusQtMainLoop(PyObject *, PyObject *args, PyObject *kwargs)
{
if (PyTuple_Size(args) != 0)
{
PyErr_SetString(PyExc_TypeError, "DBusQtMainLoop() takes no positional arguments");
return 0;
}
int set_as_default = 0;
static char *argnames[] = {"set_as_default", 0};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", argnames, &set_as_default))
return 0;
pyqtDBusHelper *hlp = new pyqtDBusHelper;
PyObject *mainloop = DBusPyNativeMainLoop_New4(dbus_qt_conn, dbus_qt_srv,
dbus_qt_delete_helper, hlp);
if (!mainloop)
{
delete hlp;
return 0;
}
if (set_as_default)
{
PyObject *func = PyObject_GetAttrString(_dbus_bindings_module, "set_default_main_loop");
if (!func)
{
Py_DECREF(mainloop);
return 0;
}
PyObject *res = PyObject_CallFunctionObjArgs(func, mainloop, 0);
Py_DECREF(func);
if (!res)
{
Py_DECREF(mainloop);
return 0;
}
Py_DECREF(res);
}
return mainloop;
}
// The table of module functions.
static PyMethodDef module_functions[] = {
{"DBusQtMainLoop", (PyCFunction)DBusQtMainLoop, METH_VARARGS|METH_KEYWORDS,
DBusQtMainLoop__doc__},
{0, 0, 0, 0}
};
// The module entry point.
#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC PyInit_qt()
{
static PyModuleDef module_def = {
PyModuleDef_HEAD_INIT,
"qt",
NULL,
-1,
module_functions,
};
// Import the generic part of the Python DBus bindings.
if (import_dbus_bindings("dbus.mainloop.qt") < 0)
return 0;
return PyModule_Create(&module_def);
}
#else
PyMODINIT_FUNC initqt()
{
// Import the generic part of the Python DBus bindings.
if (import_dbus_bindings("dbus.mainloop.qt") < 0)
return;
Py_InitModule("qt", module_functions);
}
#endif
|