1 /* Implementation of main-loop integration for dbus-python.
3 * Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use, copy,
9 * modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
28 #include "dbus_bindings-internal.h"
30 /* Native mainloop wrapper ========================================= */
32 PyDoc_STRVAR(NativeMainLoop_tp_doc
,
33 "Object representing D-Bus main loop integration done in native code.\n"
34 "Cannot be instantiated directly.\n"
37 static PyTypeObject NativeMainLoop_Type
;
39 DEFINE_CHECK(NativeMainLoop
)
43 /* Called with the GIL held, should set a Python exception on error */
44 dbus_bool_t (*set_up_connection_cb
)(DBusConnection
*, void *);
45 dbus_bool_t (*set_up_server_cb
)(DBusServer
*, void *);
46 /* Called in a destructor. Must not touch the exception state (use
47 * PyErr_Fetch and PyErr_Restore if necessary). */
48 void (*free_cb
)(void *);
52 static void NativeMainLoop_tp_dealloc(NativeMainLoop
*self
)
54 if (self
->data
&& self
->free_cb
) {
55 (self
->free_cb
)(self
->data
);
57 PyObject_Del((PyObject
*)self
);
60 static PyTypeObject NativeMainLoop_Type
= {
61 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
63 "dbus.mainloop.NativeMainLoop",
64 sizeof(NativeMainLoop
),
66 (destructor
)NativeMainLoop_tp_dealloc
, /* tp_dealloc */
73 0, /* tp_as_sequence */
74 0, /* tp_as_mapping */
81 Py_TPFLAGS_DEFAULT
, /* tp_flags */
82 NativeMainLoop_tp_doc
, /* tp_doc */
85 0, /* tp_richcompare */
86 0, /* tp_weaklistoffset */
96 0, /* tp_dictoffset */
99 /* deliberately not callable! */
103 /* Internal C API for Connection, Bus, Server ======================= */
106 dbus_py_check_mainloop_sanity(PyObject
*mainloop
)
108 if (NativeMainLoop_Check(mainloop
)) {
111 PyErr_SetString(PyExc_TypeError
,
112 "A dbus.mainloop.NativeMainLoop instance is required");
117 dbus_py_set_up_connection(PyObject
*conn
, PyObject
*mainloop
)
119 if (NativeMainLoop_Check(mainloop
)) {
120 /* Native mainloops are allowed to do arbitrary strange things */
121 NativeMainLoop
*nml
= (NativeMainLoop
*)mainloop
;
122 DBusConnection
*dbc
= DBusPyConnection_BorrowDBusConnection(conn
);
127 return (nml
->set_up_connection_cb
)(dbc
, nml
->data
);
129 PyErr_SetString(PyExc_TypeError
,
130 "A dbus.mainloop.NativeMainLoop instance is required");
134 /* C API ============================================================ */
137 DBusPyNativeMainLoop_New4(dbus_bool_t (*conn_cb
)(DBusConnection
*, void *),
138 dbus_bool_t (*server_cb
)(DBusServer
*, void *),
139 void (*free_cb
)(void *),
142 NativeMainLoop
*self
= PyObject_New(NativeMainLoop
, &NativeMainLoop_Type
);
145 self
->free_cb
= free_cb
;
146 self
->set_up_connection_cb
= conn_cb
;
147 self
->set_up_server_cb
= server_cb
;
149 return (PyObject
*)self
;
152 /* Null mainloop implementation ===================================== */
155 noop_main_loop_cb(void *conn_or_server UNUSED
, void *data UNUSED
)
160 #define noop_conn_cb ((dbus_bool_t (*)(DBusConnection *, void *))(noop_main_loop_cb))
161 #define noop_server_cb ((dbus_bool_t (*)(DBusServer *, void *))(noop_main_loop_cb))
163 /* Initialization =================================================== */
166 dbus_py_init_mainloop(void)
168 if (PyType_Ready (&NativeMainLoop_Type
) < 0) return 0;
174 dbus_py_insert_mainloop_types(PyObject
*this_module
)
176 PyObject
*null_main_loop
= DBusPyNativeMainLoop_New4(noop_conn_cb
,
180 if (!null_main_loop
) return 0;
182 if (PyModule_AddObject (this_module
, "NativeMainLoop",
183 (PyObject
*)&NativeMainLoop_Type
) < 0) return 0;
184 if (PyModule_AddObject (this_module
, "NULL_MAIN_LOOP",
185 null_main_loop
) < 0) return 0;
189 /* vim:set ft=c cino< sw=4 sts=4 et: */