1
/* Implementation of main-loop integration for dbus-python.
3
* Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
4
* Copyright (C) 2008 Huang Peng <phuang@redhat.com>
6
* Permission is hereby granted, free of charge, to any person
7
* obtaining a copy of this software and associated documentation
8
* files (the "Software"), to deal in the Software without
9
* restriction, including without limitation the rights to use, copy,
10
* modify, merge, publish, distribute, sublicense, and/or sell copies
11
* of the Software, and to permit persons to whom the Software is
12
* furnished to do so, subject to the following conditions:
14
* The above copyright notice and this permission notice shall be
15
* included in all copies or substantial portions of the Software.
17
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24
* DEALINGS IN THE SOFTWARE.
29
#include "dbus_bindings-internal.h"
31
/* Native mainloop wrapper ========================================= */
33
PyDoc_STRVAR(NativeMainLoop_tp_doc,
34
"Object representing D-Bus main loop integration done in native code.\n"
35
"Cannot be instantiated directly.\n"
38
static PyTypeObject NativeMainLoop_Type;
40
DEFINE_CHECK(NativeMainLoop)
44
/* Called with the GIL held, should set a Python exception on error */
45
dbus_bool_t (*set_up_connection_cb)(DBusConnection *, void *);
46
dbus_bool_t (*set_up_server_cb)(DBusServer *, void *);
47
/* Called in a destructor. Must not touch the exception state (use
48
* PyErr_Fetch and PyErr_Restore if necessary). */
49
void (*free_cb)(void *);
53
static void NativeMainLoop_tp_dealloc(NativeMainLoop *self)
55
if (self->data && self->free_cb) {
56
(self->free_cb)(self->data);
58
PyObject_Del((PyObject *)self);
61
static PyTypeObject NativeMainLoop_Type = {
62
PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
64
"dbus.mainloop.NativeMainLoop",
65
sizeof(NativeMainLoop),
67
(destructor)NativeMainLoop_tp_dealloc, /* tp_dealloc */
74
0, /* tp_as_sequence */
75
0, /* tp_as_mapping */
82
Py_TPFLAGS_DEFAULT, /* tp_flags */
83
NativeMainLoop_tp_doc, /* tp_doc */
86
0, /* tp_richcompare */
87
0, /* tp_weaklistoffset */
97
0, /* tp_dictoffset */
100
/* deliberately not callable! */
104
/* Internal C API for Connection, Bus, Server ======================= */
107
dbus_py_check_mainloop_sanity(PyObject *mainloop)
109
if (NativeMainLoop_Check(mainloop)) {
112
PyErr_SetString(PyExc_TypeError,
113
"A dbus.mainloop.NativeMainLoop instance is required");
118
dbus_py_set_up_connection(PyObject *conn, PyObject *mainloop)
120
if (NativeMainLoop_Check(mainloop)) {
121
/* Native mainloops are allowed to do arbitrary strange things */
122
NativeMainLoop *nml = (NativeMainLoop *)mainloop;
123
DBusConnection *dbc = DBusPyConnection_BorrowDBusConnection(conn);
128
return (nml->set_up_connection_cb)(dbc, nml->data);
130
PyErr_SetString(PyExc_TypeError,
131
"A dbus.mainloop.NativeMainLoop instance is required");
136
dbus_py_set_up_server(PyObject *server, PyObject *mainloop)
138
if (NativeMainLoop_Check(mainloop)) {
139
/* Native mainloops are allowed to do arbitrary strange things */
140
NativeMainLoop *nml = (NativeMainLoop *)mainloop;
141
DBusServer *dbs = DBusPyServer_BorrowDBusServer(server);
146
return (nml->set_up_server_cb)(dbs, nml->data);
148
PyErr_SetString(PyExc_TypeError,
149
"A dbus.mainloop.NativeMainLoop instance is required");
153
/* C API ============================================================ */
156
DBusPyNativeMainLoop_New4(dbus_bool_t (*conn_cb)(DBusConnection *, void *),
157
dbus_bool_t (*server_cb)(DBusServer *, void *),
158
void (*free_cb)(void *),
161
NativeMainLoop *self = PyObject_New(NativeMainLoop, &NativeMainLoop_Type);
164
self->free_cb = free_cb;
165
self->set_up_connection_cb = conn_cb;
166
self->set_up_server_cb = server_cb;
168
return (PyObject *)self;
171
/* Null mainloop implementation ===================================== */
174
noop_main_loop_cb(void *conn_or_server UNUSED, void *data UNUSED)
179
#define noop_conn_cb ((dbus_bool_t (*)(DBusConnection *, void *))(noop_main_loop_cb))
180
#define noop_server_cb ((dbus_bool_t (*)(DBusServer *, void *))(noop_main_loop_cb))
182
/* Initialization =================================================== */
185
dbus_py_init_mainloop(void)
187
if (PyType_Ready (&NativeMainLoop_Type) < 0) return 0;
193
dbus_py_insert_mainloop_types(PyObject *this_module)
195
PyObject *null_main_loop = DBusPyNativeMainLoop_New4(noop_conn_cb,
199
if (!null_main_loop) return 0;
201
/* PyModule_AddObject steals a ref */
202
Py_INCREF (&NativeMainLoop_Type);
203
if (PyModule_AddObject (this_module, "NativeMainLoop",
204
(PyObject *)&NativeMainLoop_Type) < 0) return 0;
205
if (PyModule_AddObject (this_module, "NULL_MAIN_LOOP",
206
null_main_loop) < 0) return 0;
210
/* vim:set ft=c cino< sw=4 sts=4 et: */