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 library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 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 Lesser 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
24 #include "dbus-python.h"
25 #include <dbus/dbus-glib.h>
26 #include <dbus/dbus-glib-lowlevel.h>
30 # define UNUSED __attribute__((__unused__))
32 # define UNUSED /*nothing*/
35 # define UNUSED /*nothing*/
39 dbus_py_glib_set_up_conn(DBusConnection
*conn
, void *data
)
41 GMainContext
*ctx
= (GMainContext
*)data
;
42 Py_BEGIN_ALLOW_THREADS
43 dbus_connection_setup_with_g_main(conn
, ctx
);
49 dbus_py_glib_set_up_srv(DBusServer
*srv
, void *data
)
51 GMainContext
*ctx
= (GMainContext
*)data
;
52 Py_BEGIN_ALLOW_THREADS
53 dbus_server_setup_with_g_main(srv
, ctx
);
59 dbus_py_glib_unref_mainctx(void *data
)
62 g_main_context_unref((GMainContext
*)data
);
65 /* Generate a dbus-python NativeMainLoop wrapper from a GLib main loop */
67 dbus_glib_native_mainloop(GMainContext
*ctx
)
69 PyObject
*loop
= DBusPyNativeMainLoop_New4(dbus_py_glib_set_up_conn
,
70 dbus_py_glib_set_up_srv
,
71 dbus_py_glib_unref_mainctx
,
72 ctx
? g_main_context_ref(ctx
)
75 g_main_context_unref(ctx
);
80 PyDoc_STRVAR(module_doc
, "");
82 PyDoc_STRVAR(DBusGMainLoop__doc__
,
83 "DBusGMainLoop([set_as_default=False]) -> NativeMainLoop\n"
85 "Return a NativeMainLoop object which can be used to\n"
86 "represent the default GLib main context in dbus-python.\n"
88 "If the keyword argument set_as_default is given and is true, set the new\n"
89 "main loop as the default for all new Connection or Bus instances.\n"
91 "Non-default main contexts are not currently supported.\n");
93 DBusGMainLoop (PyObject
*always_null UNUSED
, PyObject
*args
, PyObject
*kwargs
)
95 PyObject
*mainloop
, *function
, *result
;
96 int set_as_default
= 0;
97 static char *argnames
[] = {"set_as_default", NULL
};
99 if (PyTuple_Size(args
) != 0) {
100 PyErr_SetString(PyExc_TypeError
, "DBusGMainLoop() takes no "
101 "positional arguments");
104 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|i", argnames
,
109 mainloop
= dbus_glib_native_mainloop(NULL
);
110 if (mainloop
&& set_as_default
) {
111 if (!_dbus_bindings_module
) {
112 PyErr_SetString(PyExc_ImportError
, "_dbus_bindings not imported");
116 function
= PyObject_GetAttrString(_dbus_bindings_module
,
117 "set_default_main_loop");
122 result
= PyObject_CallFunctionObjArgs(function
, mainloop
, NULL
);
133 PyDoc_STRVAR(setup_with_g_main__doc__
,
134 "setup_with_g_main(conn: dbus.Connection)\n"
138 setup_with_g_main (PyObject
*always_null UNUSED
, PyObject
*args
)
142 if (!PyArg_ParseTuple(args
, "O:setup_with_g_main", &conn
)) return NULL
;
144 dbc
= DBusPyConnection_BorrowDBusConnection (conn
);
145 if (!dbc
) return NULL
;
146 dbus_py_glib_set_up_conn(dbc
, NULL
);
150 PyDoc_STRVAR(gthreads_init__doc__
,
153 gthreads_init (PyObject
*always_null UNUSED
, PyObject
*no_args UNUSED
)
155 dbus_g_thread_init();
159 static PyMethodDef module_functions
[] = {
160 {"setup_with_g_main", setup_with_g_main
, METH_VARARGS
,
161 setup_with_g_main__doc__
},
162 {"gthreads_init", gthreads_init
, METH_NOARGS
, gthreads_init__doc__
},
163 {"DBusGMainLoop", (PyCFunction
)DBusGMainLoop
,
164 METH_VARARGS
|METH_KEYWORDS
, DBusGMainLoop__doc__
},
165 {NULL
, NULL
, 0, NULL
}
169 init_dbus_glib_bindings(void)
171 PyObject
*this_module
;
173 if (import_dbus_bindings("_dbus_glib_bindings") < 0) return;
174 this_module
= Py_InitModule3 ("_dbus_glib_bindings", module_functions
,
176 if (!this_module
) return;
179 /* vim:set ft=c cino< sw=4 sts=4 et: */