dbus/bus.py: get BUS_DAEMON_NAME etc. from _dbus_bindings
[dbus-python-phuang.git] / _dbus_glib_bindings / module.c
blob0a47d04cf59e0215535c7a431dc719407855cea1
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
23 #include <Python.h>
24 #include "dbus-python.h"
25 #include <dbus/dbus-glib.h>
26 #include <dbus/dbus-glib-lowlevel.h>
28 #if defined(__GNUC__)
29 # if __GNUC__ >= 3
30 # define UNUSED __attribute__((__unused__))
31 # else
32 # define UNUSED /*nothing*/
33 # endif
34 #else
35 # define UNUSED /*nothing*/
36 #endif
38 static dbus_bool_t
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);
44 Py_END_ALLOW_THREADS
45 return 1;
48 static dbus_bool_t
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);
54 Py_END_ALLOW_THREADS
55 return 1;
58 static void
59 dbus_py_glib_unref_mainctx(void *data)
61 if (data)
62 g_main_context_unref((GMainContext *)data);
65 /* Generate a dbus-python NativeMainLoop wrapper from a GLib main loop */
66 static PyObject *
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)
73 : NULL);
74 if (!loop && ctx) {
75 g_main_context_unref(ctx);
77 return loop;
80 PyDoc_STRVAR(module_doc, "");
82 PyDoc_STRVAR(DBusGMainLoop__doc__,
83 "DBusGMainLoop([set_as_default=False]) -> NativeMainLoop\n"
84 "\n"
85 "Return a NativeMainLoop object which can be used to\n"
86 "represent the default GLib main context in dbus-python.\n"
87 "\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"
90 "\n"
91 "Non-default main contexts are not currently supported.\n");
92 static PyObject *
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");
102 return NULL;
104 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", argnames,
105 &set_as_default)) {
106 return NULL;
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");
113 Py_DECREF(mainloop);
114 return NULL;
116 function = PyObject_GetAttrString(_dbus_bindings_module,
117 "set_default_main_loop");
118 if (!function) {
119 Py_DECREF(mainloop);
120 return NULL;
122 result = PyObject_CallFunctionObjArgs(function, mainloop, NULL);
123 Py_DECREF(function);
124 if (!result) {
125 Py_DECREF(mainloop);
126 return NULL;
128 Py_DECREF(result);
130 return mainloop;
133 PyDoc_STRVAR(setup_with_g_main__doc__,
134 "setup_with_g_main(conn: dbus.Connection)\n"
135 "\n"
136 "Deprecated.\n");
137 static PyObject *
138 setup_with_g_main (PyObject *always_null UNUSED, PyObject *args)
140 DBusConnection *dbc;
141 PyObject *conn;
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);
147 Py_RETURN_NONE;
150 PyDoc_STRVAR(gthreads_init__doc__,
151 "gthreads_init()");
152 static PyObject *
153 gthreads_init (PyObject *always_null UNUSED, PyObject *no_args UNUSED)
155 dbus_g_thread_init();
156 Py_RETURN_NONE;
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}
168 PyMODINIT_FUNC
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,
175 module_doc);
176 if (!this_module) return;
179 /* vim:set ft=c cino< sw=4 sts=4 et: */