Blame _dbus_glib_bindings/module.c

Packit 130fc8
/* Glue code to attach the GObject main loop to D-Bus from within Python.
Packit 130fc8
 *
Packit 130fc8
 * Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
Packit 130fc8
 *
Packit 130fc8
 * Permission is hereby granted, free of charge, to any person
Packit 130fc8
 * obtaining a copy of this software and associated documentation
Packit 130fc8
 * files (the "Software"), to deal in the Software without
Packit 130fc8
 * restriction, including without limitation the rights to use, copy,
Packit 130fc8
 * modify, merge, publish, distribute, sublicense, and/or sell copies
Packit 130fc8
 * of the Software, and to permit persons to whom the Software is
Packit 130fc8
 * furnished to do so, subject to the following conditions:
Packit 130fc8
 *
Packit 130fc8
 * The above copyright notice and this permission notice shall be
Packit 130fc8
 * included in all copies or substantial portions of the Software.
Packit 130fc8
 *
Packit 130fc8
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit 130fc8
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit 130fc8
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit 130fc8
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
Packit 130fc8
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
Packit 130fc8
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Packit 130fc8
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Packit 130fc8
 * DEALINGS IN THE SOFTWARE.
Packit 130fc8
 */
Packit 130fc8
Packit 130fc8
#include <Python.h>
Packit 130fc8
#include <dbus/dbus-python.h>
Packit 130fc8
#include <dbus/dbus-glib.h>
Packit 130fc8
#include <dbus/dbus-glib-lowlevel.h>
Packit 130fc8
Packit 130fc8
#ifdef PY3
Packit 130fc8
PyMODINIT_FUNC PyInit__dbus_glib_bindings(void);
Packit 130fc8
#else
Packit 130fc8
PyMODINIT_FUNC init_dbus_glib_bindings(void);
Packit 130fc8
#endif
Packit 130fc8
Packit 130fc8
#if defined(__GNUC__)
Packit 130fc8
#   if __GNUC__ >= 3
Packit 130fc8
#       define UNUSED __attribute__((__unused__))
Packit 130fc8
#   else
Packit 130fc8
#       define UNUSED /*nothing*/
Packit 130fc8
#   endif
Packit 130fc8
#else
Packit 130fc8
#   define UNUSED /*nothing*/
Packit 130fc8
#endif
Packit 130fc8
Packit 130fc8
static dbus_bool_t
Packit 130fc8
dbus_py_glib_set_up_conn(DBusConnection *conn, void *data)
Packit 130fc8
{
Packit 130fc8
    GMainContext *ctx = (GMainContext *)data;
Packit 130fc8
    Py_BEGIN_ALLOW_THREADS
Packit 130fc8
    dbus_connection_setup_with_g_main(conn, ctx);
Packit 130fc8
    Py_END_ALLOW_THREADS
Packit 130fc8
    return 1;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static dbus_bool_t
Packit 130fc8
dbus_py_glib_set_up_srv(DBusServer *srv, void *data)
Packit 130fc8
{
Packit 130fc8
    GMainContext *ctx = (GMainContext *)data;
Packit 130fc8
    Py_BEGIN_ALLOW_THREADS
Packit 130fc8
    dbus_server_setup_with_g_main(srv, ctx);
Packit 130fc8
    Py_END_ALLOW_THREADS
Packit 130fc8
    return 1;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static void
Packit 130fc8
dbus_py_glib_unref_mainctx(void *data)
Packit 130fc8
{
Packit 130fc8
    if (data)
Packit 130fc8
        g_main_context_unref((GMainContext *)data);
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
/* Generate a dbus-python NativeMainLoop wrapper from a GLib main loop */
Packit 130fc8
static PyObject *
Packit 130fc8
dbus_glib_native_mainloop(GMainContext *ctx)
Packit 130fc8
{
Packit 130fc8
    PyObject *loop = DBusPyNativeMainLoop_New4(dbus_py_glib_set_up_conn,
Packit 130fc8
                                               dbus_py_glib_set_up_srv,
Packit 130fc8
                                               dbus_py_glib_unref_mainctx,
Packit 130fc8
                                               ctx ? g_main_context_ref(ctx)
Packit 130fc8
                                                   : NULL);
Packit 130fc8
    if (!loop && ctx) {
Packit 130fc8
        g_main_context_unref(ctx);
Packit 130fc8
    }
Packit 130fc8
    return loop;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
PyDoc_STRVAR(module_doc, "");
Packit 130fc8
Packit 130fc8
PyDoc_STRVAR(DBusGMainLoop__doc__,
Packit 130fc8
"DBusGMainLoop([set_as_default=False]) -> NativeMainLoop\n"
Packit 130fc8
"\n"
Packit 130fc8
"Return a NativeMainLoop object which can be used to\n"
Packit 130fc8
"represent the default GLib main context in dbus-python.\n"
Packit 130fc8
"\n"
Packit 130fc8
"If the keyword argument set_as_default is given and is true, set the new\n"
Packit 130fc8
"main loop as the default for all new Connection or Bus instances.\n"
Packit 130fc8
"\n"
Packit 130fc8
"Non-default main contexts are not currently supported.\n");
Packit 130fc8
static PyObject *
Packit 130fc8
DBusGMainLoop (PyObject *always_null UNUSED, PyObject *args, PyObject *kwargs)
Packit 130fc8
{
Packit 130fc8
    PyObject *mainloop, *function, *result;
Packit 130fc8
    int set_as_default = 0;
Packit 130fc8
    static char *argnames[] = {"set_as_default", NULL};
Packit 130fc8
Packit 130fc8
    if (PyTuple_Size(args) != 0) {
Packit 130fc8
        PyErr_SetString(PyExc_TypeError, "DBusGMainLoop() takes no "
Packit 130fc8
                                         "positional arguments");
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", argnames,
Packit 130fc8
                                     &set_as_default)) {
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    mainloop = dbus_glib_native_mainloop(NULL);
Packit 130fc8
    if (mainloop && set_as_default) {
Packit 130fc8
        if (!_dbus_bindings_module) {
Packit 130fc8
            PyErr_SetString(PyExc_ImportError, "_dbus_bindings not imported");
Packit 130fc8
            Py_CLEAR(mainloop);
Packit 130fc8
            return NULL;
Packit 130fc8
        }
Packit 130fc8
        function = PyObject_GetAttrString(_dbus_bindings_module,
Packit 130fc8
                                          "set_default_main_loop");
Packit 130fc8
        if (!function) {
Packit 130fc8
            Py_CLEAR(mainloop);
Packit 130fc8
            return NULL;
Packit 130fc8
        }
Packit 130fc8
        result = PyObject_CallFunctionObjArgs(function, mainloop, NULL);
Packit 130fc8
        Py_CLEAR(function);
Packit 130fc8
        if (!result) {
Packit 130fc8
            Py_CLEAR(mainloop);
Packit 130fc8
            return NULL;
Packit 130fc8
        }
Packit 130fc8
        Py_CLEAR(result);
Packit 130fc8
    }
Packit 130fc8
    return mainloop;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
PyDoc_STRVAR(setup_with_g_main__doc__,
Packit 130fc8
"setup_with_g_main(conn: dbus.Connection)\n"
Packit 130fc8
"\n"
Packit 130fc8
"Deprecated.\n");
Packit 130fc8
static PyObject *
Packit 130fc8
setup_with_g_main (PyObject *always_null UNUSED, PyObject *args)
Packit 130fc8
{
Packit 130fc8
    DBusConnection *dbc;
Packit 130fc8
    PyObject *conn;
Packit 130fc8
    if (!PyArg_ParseTuple(args, "O:setup_with_g_main", &conn)) return NULL;
Packit 130fc8
Packit 130fc8
    dbc = DBusPyConnection_BorrowDBusConnection (conn);
Packit 130fc8
    if (!dbc) return NULL;
Packit 130fc8
    dbus_py_glib_set_up_conn(dbc, NULL);
Packit 130fc8
    Py_RETURN_NONE;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
PyDoc_STRVAR(gthreads_init__doc__,
Packit 130fc8
"gthreads_init()");
Packit 130fc8
static PyObject *
Packit 130fc8
gthreads_init (PyObject *always_null UNUSED, PyObject *no_args UNUSED)
Packit 130fc8
{
Packit 130fc8
    dbus_g_thread_init();
Packit 130fc8
    Py_RETURN_NONE;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static PyMethodDef module_functions[] = {
Packit 130fc8
    {"setup_with_g_main", setup_with_g_main, METH_VARARGS,
Packit 130fc8
     setup_with_g_main__doc__},
Packit 130fc8
    {"gthreads_init", gthreads_init, METH_NOARGS, gthreads_init__doc__},
Packit 130fc8
    {"DBusGMainLoop", (PyCFunction)DBusGMainLoop,
Packit 130fc8
     METH_VARARGS|METH_KEYWORDS, DBusGMainLoop__doc__},
Packit 130fc8
    {NULL, NULL, 0, NULL}
Packit 130fc8
};
Packit 130fc8
Packit 130fc8
#ifdef PY3
Packit 130fc8
PyMODINIT_FUNC
Packit 130fc8
PyInit__dbus_glib_bindings(void)
Packit 130fc8
{
Packit 130fc8
    PyObject *this_module;
Packit 130fc8
Packit 130fc8
    static struct PyModuleDef moduledef = {
Packit 130fc8
        PyModuleDef_HEAD_INIT,
Packit 130fc8
        "_dbus_glib_bindings",  /* m_name */
Packit 130fc8
        module_doc,             /* m_doc */
Packit 130fc8
        -1,                     /* m_size */
Packit 130fc8
        module_functions,       /* m_methods */
Packit 130fc8
        NULL,                   /* m_reload */
Packit 130fc8
        NULL,                   /* m_traverse */
Packit 130fc8
        NULL,                   /* m_clear */
Packit 130fc8
        NULL                    /* m_free */
Packit 130fc8
    };
Packit 130fc8
Packit 130fc8
    if (import_dbus_bindings("_dbus_glib_bindings") < 0)
Packit 130fc8
        return NULL;
Packit 130fc8
Packit 130fc8
    if (!(this_module = PyModule_Create(&moduledef))) {
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
    return this_module;
Packit 130fc8
}
Packit 130fc8
#else
Packit 130fc8
PyMODINIT_FUNC
Packit 130fc8
init_dbus_glib_bindings(void)
Packit 130fc8
{
Packit 130fc8
    PyObject *this_module;
Packit 130fc8
Packit 130fc8
    if (import_dbus_bindings("_dbus_glib_bindings") < 0) return;
Packit 130fc8
    this_module = Py_InitModule3 ("_dbus_glib_bindings", module_functions,
Packit 130fc8
                                  module_doc);
Packit 130fc8
    if (!this_module) return;
Packit 130fc8
}
Packit 130fc8
#endif
Packit 130fc8
Packit 130fc8
/* vim:set ft=c cino< sw=4 sts=4 et: */