Add wrapper for DBusServer.
[dbus-python-phuang.git] / _dbus_glib_bindings / module.c
blob53e39f36622555e2be1c32c1dd0b30b67911bf1e
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.
26 #include <Python.h>
27 #include "dbus-python.h"
28 #include <dbus/dbus-glib.h>
29 #include <dbus/dbus-glib-lowlevel.h>
31 #if defined(__GNUC__)
32 # if __GNUC__ >= 3
33 # define UNUSED __attribute__((__unused__))
34 # else
35 # define UNUSED /*nothing*/
36 # endif
37 #else
38 # define UNUSED /*nothing*/
39 #endif
41 static dbus_bool_t
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);
47 Py_END_ALLOW_THREADS
48 return 1;
51 static dbus_bool_t
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);
57 Py_END_ALLOW_THREADS
58 return 1;
61 static void
62 dbus_py_glib_unref_mainctx(void *data)
64 if (data)
65 g_main_context_unref((GMainContext *)data);
68 /* Generate a dbus-python NativeMainLoop wrapper from a GLib main loop */
69 static PyObject *
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)
76 : NULL);
77 if (!loop && ctx) {
78 g_main_context_unref(ctx);
80 return loop;
83 PyDoc_STRVAR(module_doc, "");
85 PyDoc_STRVAR(DBusGMainLoop__doc__,
86 "DBusGMainLoop([set_as_default=False]) -> NativeMainLoop\n"
87 "\n"
88 "Return a NativeMainLoop object which can be used to\n"
89 "represent the default GLib main context in dbus-python.\n"
90 "\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"
93 "\n"
94 "Non-default main contexts are not currently supported.\n");
95 static PyObject *
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");
105 return NULL;
107 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", argnames,
108 &set_as_default)) {
109 return NULL;
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");
116 Py_DECREF(mainloop);
117 return NULL;
119 function = PyObject_GetAttrString(_dbus_bindings_module,
120 "set_default_main_loop");
121 if (!function) {
122 Py_DECREF(mainloop);
123 return NULL;
125 result = PyObject_CallFunctionObjArgs(function, mainloop, NULL);
126 Py_DECREF(function);
127 if (!result) {
128 Py_DECREF(mainloop);
129 return NULL;
131 Py_DECREF(result);
133 return mainloop;
136 PyDoc_STRVAR(setup_with_g_main__doc__,
137 "setup_with_g_main(conn: dbus.Connection)\n"
138 "\n"
139 "Deprecated.\n");
140 static PyObject *
141 setup_with_g_main (PyObject *always_null UNUSED, PyObject *args)
143 DBusConnection *dbc;
144 PyObject *conn;
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);
150 Py_RETURN_NONE;
153 PyDoc_STRVAR(gthreads_init__doc__,
154 "gthreads_init()");
155 static PyObject *
156 gthreads_init (PyObject *always_null UNUSED, PyObject *no_args UNUSED)
158 dbus_g_thread_init();
159 Py_RETURN_NONE;
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}
171 PyMODINIT_FUNC
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,
178 module_doc);
179 if (!this_module) return;
182 /* vim:set ft=c cino< sw=4 sts=4 et: */