Update NEWS, README for 0.80.0
[dbus-python-phuang.git] / _dbus_glib_bindings / module.c
blob95a2c99a3c381a54a81e5fe1b4db7a6f3f054e5d
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
25 #include <Python.h>
26 #include "dbus-python.h"
27 #include <dbus/dbus-glib.h>
28 #include <dbus/dbus-glib-lowlevel.h>
30 #if defined(__GNUC__)
31 # if __GNUC__ >= 3
32 # define UNUSED __attribute__((__unused__))
33 # else
34 # define UNUSED /*nothing*/
35 # endif
36 #else
37 # define UNUSED /*nothing*/
38 #endif
40 static dbus_bool_t
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);
46 Py_END_ALLOW_THREADS
47 return 1;
50 static dbus_bool_t
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);
56 Py_END_ALLOW_THREADS
57 return 1;
60 static void
61 dbus_py_glib_unref_mainctx(void *data)
63 if (data)
64 g_main_context_unref((GMainContext *)data);
67 /* Generate a dbus-python NativeMainLoop wrapper from a GLib main loop */
68 static PyObject *
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)
75 : NULL);
76 if (!loop && ctx) {
77 g_main_context_unref(ctx);
79 return loop;
82 PyDoc_STRVAR(module_doc, "");
84 PyDoc_STRVAR(DBusGMainLoop__doc__,
85 "DBusGMainLoop([set_as_default=False]) -> NativeMainLoop\n"
86 "\n"
87 "Return a NativeMainLoop object which can be used to\n"
88 "represent the default GLib main context in dbus-python.\n"
89 "\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"
92 "\n"
93 "Non-default main contexts are not currently supported.\n");
94 static PyObject *
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");
104 return NULL;
106 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", argnames,
107 &set_as_default)) {
108 return NULL;
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");
115 Py_DECREF(mainloop);
116 return NULL;
118 function = PyObject_GetAttrString(_dbus_bindings_module,
119 "set_default_main_loop");
120 if (!function) {
121 Py_DECREF(mainloop);
122 return NULL;
124 result = PyObject_CallFunctionObjArgs(function, mainloop, NULL);
125 Py_DECREF(function);
126 if (!result) {
127 Py_DECREF(mainloop);
128 return NULL;
130 Py_DECREF(result);
132 return mainloop;
135 PyDoc_STRVAR(setup_with_g_main__doc__,
136 "setup_with_g_main(conn: dbus.Connection)\n"
137 "\n"
138 "Deprecated.\n");
139 static PyObject *
140 setup_with_g_main (PyObject *always_null UNUSED, PyObject *args)
142 DBusConnection *dbc;
143 PyObject *conn;
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);
149 Py_RETURN_NONE;
152 PyDoc_STRVAR(gthreads_init__doc__,
153 "gthreads_init()");
154 static PyObject *
155 gthreads_init (PyObject *always_null UNUSED, PyObject *no_args UNUSED)
157 dbus_g_thread_init();
158 Py_RETURN_NONE;
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}
170 PyMODINIT_FUNC
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,
177 module_doc);
178 if (!this_module) return;
181 /* vim:set ft=c cino< sw=4 sts=4 et: */