1 /* Implementation of main-loop integration for dbus-python.
3 * Copyright (C) 2006 Collabora Ltd.
5 * Licensed under the Academic Free License version 2.1
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus_bindings-internal.h"
27 /* Native mainloop wrapper ========================================= */
29 PyDoc_STRVAR(NativeMainLoop_tp_doc
,
30 "Object representing D-Bus main loop integration done in native code.\n"
31 "Cannot be instantiated directly.\n"
34 static PyTypeObject NativeMainLoop_Type
;
36 DEFINE_CHECK(NativeMainLoop
)
40 /* Called with the GIL held, should set a Python exception on error */
41 dbus_bool_t (*set_up_connection_cb
)(DBusConnection
*, void *);
42 dbus_bool_t (*set_up_server_cb
)(DBusServer
*, void *);
43 /* Called in a destructor. Must not touch the exception state (use
44 * PyErr_Fetch and PyErr_Restore if necessary). */
45 void (*free_cb
)(void *);
49 static void NativeMainLoop_tp_dealloc(NativeMainLoop
*self
)
51 if (self
->data
&& self
->free_cb
) {
52 (self
->free_cb
)(self
->data
);
54 PyObject_Del((PyObject
*)self
);
57 static PyTypeObject NativeMainLoop_Type
= {
58 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
60 "dbus.mainloop.NativeMainLoop",
61 sizeof(NativeMainLoop
),
63 (destructor
)NativeMainLoop_tp_dealloc
, /* tp_dealloc */
70 0, /* tp_as_sequence */
71 0, /* tp_as_mapping */
78 Py_TPFLAGS_DEFAULT
, /* tp_flags */
79 NativeMainLoop_tp_doc
, /* tp_doc */
82 0, /* tp_richcompare */
83 0, /* tp_weaklistoffset */
93 0, /* tp_dictoffset */
96 /* deliberately not callable! */
100 /* Internal C API for Connection, Bus, Server ======================= */
103 dbus_py_check_mainloop_sanity(PyObject
*mainloop
)
105 if (NativeMainLoop_Check(mainloop
)) {
108 PyErr_SetString(PyExc_TypeError
,
109 "A dbus.mainloop.NativeMainLoop instance is required");
114 dbus_py_set_up_connection(PyObject
*conn
, PyObject
*mainloop
)
116 if (NativeMainLoop_Check(mainloop
)) {
117 /* Native mainloops are allowed to do arbitrary strange things */
118 NativeMainLoop
*nml
= (NativeMainLoop
*)mainloop
;
119 DBusConnection
*dbc
= DBusPyConnection_BorrowDBusConnection(conn
);
124 return (nml
->set_up_connection_cb
)(dbc
, nml
->data
);
126 PyErr_SetString(PyExc_TypeError
,
127 "A dbus.mainloop.NativeMainLoop instance is required");
131 /* C API ============================================================ */
134 DBusPyNativeMainLoop_New4(dbus_bool_t (*conn_cb
)(DBusConnection
*, void *),
135 dbus_bool_t (*server_cb
)(DBusServer
*, void *),
136 void (*free_cb
)(void *),
139 NativeMainLoop
*self
= PyObject_New(NativeMainLoop
, &NativeMainLoop_Type
);
142 self
->free_cb
= free_cb
;
143 self
->set_up_connection_cb
= conn_cb
;
144 self
->set_up_server_cb
= server_cb
;
146 return (PyObject
*)self
;
149 /* Null mainloop implementation ===================================== */
152 noop_main_loop_cb(void *conn_or_server UNUSED
, void *data UNUSED
)
157 #define noop_conn_cb ((dbus_bool_t (*)(DBusConnection *, void *))(noop_main_loop_cb))
158 #define noop_server_cb ((dbus_bool_t (*)(DBusServer *, void *))(noop_main_loop_cb))
160 /* Initialization =================================================== */
163 dbus_py_init_mainloop(void)
165 if (PyType_Ready (&NativeMainLoop_Type
) < 0) return 0;
171 dbus_py_insert_mainloop_types(PyObject
*this_module
)
173 PyObject
*null_main_loop
= DBusPyNativeMainLoop_New4(noop_conn_cb
,
177 if (!null_main_loop
) return 0;
179 if (PyModule_AddObject (this_module
, "NativeMainLoop",
180 (PyObject
*)&NativeMainLoop_Type
) < 0) return 0;
181 if (PyModule_AddObject (this_module
, "NULL_MAIN_LOOP",
182 null_main_loop
) < 0) return 0;
186 /* vim:set ft=c cino< sw=4 sts=4 et: */