1 /* Glue code to attach the GObject main loop to D-Bus from within 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 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "dbus-python.h"
27 #include <dbus/dbus-glib.h>
28 #include <dbus/dbus-glib-lowlevel.h>
32 # define UNUSED __attribute__((__unused__))
34 # define UNUSED /*nothing*/
37 # define UNUSED /*nothing*/
41 dbus_py_glib_set_up_conn(DBusConnection
*conn
, void *data
)
43 GMainContext
*ctx
= (GMainContext
*)data
;
44 Py_BEGIN_ALLOW_THREADS
45 dbus_connection_setup_with_g_main(conn
, ctx
);
51 dbus_py_glib_set_up_srv(DBusServer
*srv
, void *data
)
53 GMainContext
*ctx
= (GMainContext
*)data
;
54 Py_BEGIN_ALLOW_THREADS
55 dbus_server_setup_with_g_main(srv
, ctx
);
61 dbus_py_glib_unref_mainctx(void *data
)
64 g_main_context_unref((GMainContext
*)data
);
67 /* Generate a dbus-python NativeMainLoop wrapper from a GLib main loop */
69 dbus_glib_native_mainloop(GMainContext
*ctx
)
71 PyObject
*loop
= DBusPyNativeMainLoop_New4(dbus_py_glib_set_up_conn
,
72 dbus_py_glib_set_up_srv
,
73 dbus_py_glib_unref_mainctx
,
74 ctx
? g_main_context_ref(ctx
)
77 g_main_context_unref(ctx
);
82 PyDoc_STRVAR(module_doc
, "");
84 PyDoc_STRVAR(DBusGMainLoop__doc__
,
85 "DBusGMainLoop([set_as_default=False]) -> NativeMainLoop\n"
87 "Return a NativeMainLoop object which can be used to\n"
88 "represent the default GLib main context in dbus-python.\n"
90 "If the keyword argument set_as_default is given and is true, set the new\n"
91 "main loop as the default for all new Connection or Bus instances.\n"
93 "Non-default main contexts are not currently supported.\n");
95 DBusGMainLoop (PyObject
*always_null UNUSED
, PyObject
*args
, PyObject
*kwargs
)
97 PyObject
*mainloop
, *function
, *result
;
98 int set_as_default
= 0;
99 static char *argnames
[] = {"set_as_default", NULL
};
101 if (PyTuple_Size(args
) != 0) {
102 PyErr_SetString(PyExc_TypeError
, "DBusGMainLoop() takes no "
103 "positional arguments");
106 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|i", argnames
,
111 mainloop
= dbus_glib_native_mainloop(NULL
);
112 if (mainloop
&& set_as_default
) {
113 if (!_dbus_bindings_module
) {
114 PyErr_SetString(PyExc_ImportError
, "_dbus_bindings not imported");
118 function
= PyObject_GetAttrString(_dbus_bindings_module
,
119 "set_default_main_loop");
124 result
= PyObject_CallFunctionObjArgs(function
, mainloop
, NULL
);
135 PyDoc_STRVAR(setup_with_g_main__doc__
,
136 "setup_with_g_main(conn: dbus.Connection)\n"
140 setup_with_g_main (PyObject
*always_null UNUSED
, PyObject
*args
)
144 if (!PyArg_ParseTuple(args
, "O:setup_with_g_main", &conn
)) return NULL
;
146 dbc
= DBusPyConnection_BorrowDBusConnection (conn
);
147 if (!dbc
) return NULL
;
148 dbus_py_glib_set_up_conn(dbc
, NULL
);
152 PyDoc_STRVAR(gthreads_init__doc__
,
155 gthreads_init (PyObject
*always_null UNUSED
, PyObject
*no_args UNUSED
)
157 dbus_g_thread_init();
161 static PyMethodDef module_functions
[] = {
162 {"setup_with_g_main", setup_with_g_main
, METH_VARARGS
,
163 setup_with_g_main__doc__
},
164 {"gthreads_init", gthreads_init
, METH_NOARGS
, gthreads_init__doc__
},
165 {"DBusGMainLoop", (PyCFunction
)DBusGMainLoop
,
166 METH_VARARGS
|METH_KEYWORDS
, DBusGMainLoop__doc__
},
167 {NULL
, NULL
, 0, NULL
}
171 init_dbus_glib_bindings(void)
173 PyObject
*this_module
;
175 if (import_dbus_bindings("_dbus_glib_bindings") < 0) return;
176 this_module
= Py_InitModule3 ("_dbus_glib_bindings", module_functions
,
178 if (!this_module
) return;
181 /* vim:set ft=c cino< sw=4 sts=4 et: */