1 /* Glue code to attach the GObject main loop to D-Bus from within 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.
27 #include "dbus-python.h"
28 #include <dbus/dbus-glib.h>
29 #include <dbus/dbus-glib-lowlevel.h>
33 # define UNUSED __attribute__((__unused__))
35 # define UNUSED /*nothing*/
38 # define UNUSED /*nothing*/
42 dbus_py_glib_set_up_conn(DBusConnection
*conn
, void *data
)
44 GMainContext
*ctx
= (GMainContext
*)data
;
45 Py_BEGIN_ALLOW_THREADS
46 dbus_connection_setup_with_g_main(conn
, ctx
);
52 dbus_py_glib_set_up_srv(DBusServer
*srv
, void *data
)
54 GMainContext
*ctx
= (GMainContext
*)data
;
55 Py_BEGIN_ALLOW_THREADS
56 dbus_server_setup_with_g_main(srv
, ctx
);
62 dbus_py_glib_unref_mainctx(void *data
)
65 g_main_context_unref((GMainContext
*)data
);
68 /* Generate a dbus-python NativeMainLoop wrapper from a GLib main loop */
70 dbus_glib_native_mainloop(GMainContext
*ctx
)
72 PyObject
*loop
= DBusPyNativeMainLoop_New4(dbus_py_glib_set_up_conn
,
73 dbus_py_glib_set_up_srv
,
74 dbus_py_glib_unref_mainctx
,
75 ctx
? g_main_context_ref(ctx
)
78 g_main_context_unref(ctx
);
83 PyDoc_STRVAR(module_doc
, "");
85 PyDoc_STRVAR(DBusGMainLoop__doc__
,
86 "DBusGMainLoop([set_as_default=False]) -> NativeMainLoop\n"
88 "Return a NativeMainLoop object which can be used to\n"
89 "represent the default GLib main context in dbus-python.\n"
91 "If the keyword argument set_as_default is given and is true, set the new\n"
92 "main loop as the default for all new Connection or Bus instances.\n"
94 "Non-default main contexts are not currently supported.\n");
96 DBusGMainLoop (PyObject
*always_null UNUSED
, PyObject
*args
, PyObject
*kwargs
)
98 PyObject
*mainloop
, *function
, *result
;
99 int set_as_default
= 0;
100 static char *argnames
[] = {"set_as_default", NULL
};
102 if (PyTuple_Size(args
) != 0) {
103 PyErr_SetString(PyExc_TypeError
, "DBusGMainLoop() takes no "
104 "positional arguments");
107 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|i", argnames
,
112 mainloop
= dbus_glib_native_mainloop(NULL
);
113 if (mainloop
&& set_as_default
) {
114 if (!_dbus_bindings_module
) {
115 PyErr_SetString(PyExc_ImportError
, "_dbus_bindings not imported");
119 function
= PyObject_GetAttrString(_dbus_bindings_module
,
120 "set_default_main_loop");
125 result
= PyObject_CallFunctionObjArgs(function
, mainloop
, NULL
);
136 PyDoc_STRVAR(setup_with_g_main__doc__
,
137 "setup_with_g_main(conn: dbus.Connection)\n"
141 setup_with_g_main (PyObject
*always_null UNUSED
, PyObject
*args
)
145 if (!PyArg_ParseTuple(args
, "O:setup_with_g_main", &conn
)) return NULL
;
147 dbc
= DBusPyConnection_BorrowDBusConnection (conn
);
148 if (!dbc
) return NULL
;
149 dbus_py_glib_set_up_conn(dbc
, NULL
);
153 PyDoc_STRVAR(gthreads_init__doc__
,
156 gthreads_init (PyObject
*always_null UNUSED
, PyObject
*no_args UNUSED
)
158 dbus_g_thread_init();
162 static PyMethodDef module_functions
[] = {
163 {"setup_with_g_main", setup_with_g_main
, METH_VARARGS
,
164 setup_with_g_main__doc__
},
165 {"gthreads_init", gthreads_init
, METH_NOARGS
, gthreads_init__doc__
},
166 {"DBusGMainLoop", (PyCFunction
)DBusGMainLoop
,
167 METH_VARARGS
|METH_KEYWORDS
, DBusGMainLoop__doc__
},
168 {NULL
, NULL
, 0, NULL
}
172 init_dbus_glib_bindings(void)
174 PyObject
*this_module
;
176 if (import_dbus_bindings("_dbus_glib_bindings") < 0) return;
177 this_module
= Py_InitModule3 ("_dbus_glib_bindings", module_functions
,
179 if (!this_module
) return;
182 /* vim:set ft=c cino< sw=4 sts=4 et: */