dbus-python.h
001:
002:
003:
004:
005:
006: <http://www.collabora.co.uk/>
007:
008:
009:
010:
011:
012:
013:
014:
015:
016:
017:
018:
019:
020:
021:
022:
023:
024:
025:
026:
027:
028:
029: #ifndef DBUS_PYTHON_H
030: #define DBUS_PYTHON_H
031:
032: #include <Python.h>
033: #include <dbus/dbus.h>
034:
035: #if PY_MAJOR_VERSION >= 3
036: #define PY3
037: #define PYDBUS_CAPSULE_NAME "_dbus_bindings._C_API"
038: #endif
039:
040: DBUS_BEGIN_DECLS
041:
042: typedef void (*_dbus_py_func_ptr)(void);
043:
044: typedef dbus_bool_t (*_dbus_py_conn_setup_func)(DBusConnection *, void *);
045: typedef dbus_bool_t (*_dbus_py_srv_setup_func)(DBusServer *, void *);
046: typedef void (*_dbus_py_free_func)(void *);
047:
048: #define DBUS_BINDINGS_API_COUNT 3
049:
050: #ifdef INSIDE_DBUS_PYTHON_BINDINGS
051:
052: extern DBusConnection *DBusPyConnection_BorrowDBusConnection(PyObject *);
053: extern PyObject *DBusPyNativeMainLoop_New4(_dbus_py_conn_setup_func,
054: _dbus_py_srv_setup_func,
055: _dbus_py_free_func,
056: void *);
057:
058: #else
059:
060: static PyObject *_dbus_bindings_module = NULL;
061: static _dbus_py_func_ptr *dbus_bindings_API;
062:
063: #define DBusPyConnection_BorrowDBusConnection \
064: (*(DBusConnection *(*)(PyObject *))dbus_bindings_API[1])
065: #define DBusPyNativeMainLoop_New4 \
066: ((PyObject *(*)(_dbus_py_conn_setup_func, _dbus_py_srv_setup_func, \
067: _dbus_py_free_func, void *))dbus_bindings_API[2])
068:
069: static int
070: import_dbus_bindings(const char *this_module_name)
071: {
072: PyObject *c_api;
073: int count;
074:
075: _dbus_bindings_module = PyImport_ImportModule("_dbus_bindings");
076: if (!_dbus_bindings_module) {
077: return -1;
078: }
079: c_api = PyObject_GetAttrString(_dbus_bindings_module, "_C_API");
080: if (c_api == NULL) return -1;
081: #ifdef PY3
082: dbus_bindings_API = NULL;
083: if (PyCapsule_IsValid(c_api, PYDBUS_CAPSULE_NAME)) {
084: dbus_bindings_API = (_dbus_py_func_ptr *)PyCapsule_GetPointer(
085: c_api, PYDBUS_CAPSULE_NAME);
086: }
087: Py_CLEAR(c_api);
088: if (!dbus_bindings_API) {
089: PyErr_SetString(PyExc_RuntimeError, "C API is not a PyCapsule");
090: return -1;
091: }
092: #else
093: if (PyCObject_Check(c_api)) {
094: dbus_bindings_API = (_dbus_py_func_ptr *)PyCObject_AsVoidPtr(c_api);
095: }
096: else {
097: Py_DECREF(c_api);
098: PyErr_SetString(PyExc_RuntimeError, "C API is not a PyCObject");
099: return -1;
100: }
101: Py_DECREF (c_api);
102: #endif
103: count = *(int *)dbus_bindings_API[0];
104: if (count < DBUS_BINDINGS_API_COUNT) {
105: PyErr_Format(PyExc_RuntimeError,
106: "_dbus_bindings has API version %d but %s needs "
107: "_dbus_bindings API version at least %d",
108: count, this_module_name,
109: DBUS_BINDINGS_API_COUNT);
110: return -1;
111: }
112: return 0;
113: }
114:
115: #endif
116:
117: DBUS_END_DECLS
118:
119: #endif
120:
© Andrew Scott 2006 -
2025,
All Rights Reserved