File: module.c

package info (click to toggle)
python-edbus 0.5.0+r49577-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 144 kB
  • ctags: 75
  • sloc: python: 337; ansic: 100; makefile: 3
file content (146 lines) | stat: -rw-r--r-- 3,809 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (C) 2007-2008 Gustavo Sverzut Barbieri, Ulisses Furquim
 *
 * This file is part of Python-e_dbus.
 *
 * Python-e_dbus is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Python-e_dbus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this Python-e_dbus.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Glue code to attach the Ecore main loop to D-Bus from within Python.
 * @author Ulisses Furquim <ulissesf@gmail.com>
 */
#include <Python.h>

#include <dbus/dbus.h>
#include <dbus/dbus-python.h>

#include <Ecore.h>
#include <E_DBus.h>

#if defined(__GNUC__)
#if __GNUC__ >= 3
#define UNUSED __attribute__((__unused__))
#else
#define UNUSED /* nothing */
#endif
#else
#define UNUSED /* nothing */
#endif

static dbus_bool_t
dbus_py_ecore_set_up_conn(DBusConnection *conn, void *data UNUSED)
{
    Py_BEGIN_ALLOW_THREADS
    e_dbus_connection_setup(conn);
    Py_END_ALLOW_THREADS

    return TRUE;
}

static PyObject *
dbus_ecore_native_mainloop(void *data UNUSED)
{
    return DBusPyNativeMainLoop_New4(dbus_py_ecore_set_up_conn,
                                     NULL, NULL, NULL);
}

PyDoc_STRVAR(DBusEcoreMainLoop__doc__,
"Returns a NativeMainLoop to attach the Ecore main loop to D-Bus\n"
"from within Python.\n");
static PyObject *
dbus_ecore_main_loop(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "set_as_default", NULL };
    int set_as_default = 0;
    PyObject *ml;

    if (PyTuple_Size(args) != 0) {
        PyErr_SetString(PyExc_TypeError,
                        "DBusEcoreMainLoop() takes no positional arguments");
        return NULL;
    }

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i",
                                     kwlist, &set_as_default))
        return NULL;

    ml = dbus_ecore_native_mainloop(NULL);

    if (ml && set_as_default) {
        PyObject *func, *res;

        if (!_dbus_bindings_module) {
            PyErr_SetString(PyExc_ImportError, "_dbus_bindings not imported");
            Py_DECREF(ml);
            return NULL;
        }

        func = PyObject_GetAttrString(_dbus_bindings_module,
                                      "set_default_main_loop");
        if (!func) {
            Py_DECREF(ml);
            return NULL;
        }

        res = PyObject_CallFunctionObjArgs(func, ml, NULL);
        Py_DECREF(func);
        if (!res) {
            Py_DECREF(ml);
            return NULL;
        }

        Py_DECREF(res);
    }

    return ml;
}

static PyMethodDef module_functions[] = {
    { "DBusEcoreMainLoop", (PyCFunction)dbus_ecore_main_loop,
      METH_VARARGS | METH_KEYWORDS, DBusEcoreMainLoop__doc__ },
    { NULL, NULL, 0, NULL }
};

static void
module_cleanup(void)
{
    e_dbus_shutdown();
    ecore_shutdown();
}

PyDoc_STRVAR(module_doc,
"D-Bus python integration for Ecore main loop.\n");
PyMODINIT_FUNC
inite_dbus(void)
{
    PyObject *mod;

    if (import_dbus_bindings("e_dbus") < 0) {
        PyErr_SetString(PyExc_ImportError, "failed to import D-Bus bindings");
        return;
    }

    mod = Py_InitModule3("e_dbus", module_functions, module_doc);
    if (!mod) {
        PyErr_SetString(PyExc_ImportError,
                        "Py_InitModule3(\"_e_dbus\") failed");
        return;
    }

    ecore_init();
    e_dbus_init();

    Py_AtExit(module_cleanup);
}