Add wrapper for DBusServer.
[dbus-python-phuang.git] / _dbus_bindings / mainloop.c
blobc4659fc0946aeda7f368099afe24792f9a6b32c1
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.
26 #include "config.h"
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)
41 typedef struct {
42 PyObject_HEAD
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 *);
49 void *data;
50 } NativeMainLoop;
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 */
67 0, /* tp_print */
68 0, /* tp_getattr */
69 0, /* tp_setattr */
70 0, /* tp_compare */
71 0, /* tp_repr */
72 0, /* tp_as_number */
73 0, /* tp_as_sequence */
74 0, /* tp_as_mapping */
75 0, /* tp_hash */
76 0, /* tp_call */
77 0, /* tp_str */
78 0, /* tp_getattro */
79 0, /* tp_setattro */
80 0, /* tp_as_buffer */
81 Py_TPFLAGS_DEFAULT, /* tp_flags */
82 NativeMainLoop_tp_doc, /* tp_doc */
83 0, /* tp_traverse */
84 0, /* tp_clear */
85 0, /* tp_richcompare */
86 0, /* tp_weaklistoffset */
87 0, /* tp_iter */
88 0, /* tp_iternext */
89 0, /* tp_methods */
90 0, /* tp_members */
91 0, /* tp_getset */
92 0, /* tp_base */
93 0, /* tp_dict */
94 0, /* tp_descr_get */
95 0, /* tp_descr_set */
96 0, /* tp_dictoffset */
97 0, /* tp_init */
98 0, /* tp_alloc */
99 /* deliberately not callable! */
100 0, /* tp_new */
103 /* Internal C API for Connection, Bus, Server ======================= */
105 dbus_bool_t
106 dbus_py_check_mainloop_sanity(PyObject *mainloop)
108 if (NativeMainLoop_Check(mainloop)) {
109 return TRUE;
111 PyErr_SetString(PyExc_TypeError,
112 "A dbus.mainloop.NativeMainLoop instance is required");
113 return FALSE;
116 dbus_bool_t
117 dbus_py_set_up_server(PyObject *server, PyObject *mainloop)
119 if (NativeMainLoop_Check(mainloop)) {
120 /* Native mainloops are allowed to do arbitrary strange things */
121 NativeMainLoop *nml = (NativeMainLoop *)mainloop;
122 DBusServer *dbs = DBusPyServer_BorrowDBusServer(server);
124 if (!dbs) {
125 return FALSE;
127 return (nml->set_up_server_cb)(dbs, nml->data);
129 PyErr_SetString(PyExc_TypeError,
130 "A dbus.mainloop.NativeMainLoop instance is required");
131 return FALSE;
134 dbus_bool_t
135 dbus_py_set_up_connection(PyObject *conn, PyObject *mainloop)
137 if (NativeMainLoop_Check(mainloop)) {
138 /* Native mainloops are allowed to do arbitrary strange things */
139 NativeMainLoop *nml = (NativeMainLoop *)mainloop;
140 DBusConnection *dbc = DBusPyConnection_BorrowDBusConnection(conn);
142 if (!dbc) {
143 return FALSE;
145 return (nml->set_up_connection_cb)(dbc, nml->data);
147 PyErr_SetString(PyExc_TypeError,
148 "A dbus.mainloop.NativeMainLoop instance is required");
149 return FALSE;
152 /* C API ============================================================ */
154 PyObject *
155 DBusPyNativeMainLoop_New4(dbus_bool_t (*conn_cb)(DBusConnection *, void *),
156 dbus_bool_t (*server_cb)(DBusServer *, void *),
157 void (*free_cb)(void *),
158 void *data)
160 NativeMainLoop *self = PyObject_New(NativeMainLoop, &NativeMainLoop_Type);
161 if (self) {
162 self->data = data;
163 self->free_cb = free_cb;
164 self->set_up_connection_cb = conn_cb;
165 self->set_up_server_cb = server_cb;
167 return (PyObject *)self;
170 /* Null mainloop implementation ===================================== */
172 static dbus_bool_t
173 noop_main_loop_cb(void *conn_or_server UNUSED, void *data UNUSED)
175 return TRUE;
178 #define noop_conn_cb ((dbus_bool_t (*)(DBusConnection *, void *))(noop_main_loop_cb))
179 #define noop_server_cb ((dbus_bool_t (*)(DBusServer *, void *))(noop_main_loop_cb))
181 /* Initialization =================================================== */
183 dbus_bool_t
184 dbus_py_init_mainloop(void)
186 if (PyType_Ready (&NativeMainLoop_Type) < 0) return 0;
188 return 1;
191 dbus_bool_t
192 dbus_py_insert_mainloop_types(PyObject *this_module)
194 PyObject *null_main_loop = DBusPyNativeMainLoop_New4(noop_conn_cb,
195 noop_server_cb,
196 NULL,
197 NULL);
198 if (!null_main_loop) return 0;
200 if (PyModule_AddObject (this_module, "NativeMainLoop",
201 (PyObject *)&NativeMainLoop_Type) < 0) return 0;
202 if (PyModule_AddObject (this_module, "NULL_MAIN_LOOP",
203 null_main_loop) < 0) return 0;
204 return 1;
207 /* vim:set ft=c cino< sw=4 sts=4 et: */