dbus-python.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* C API for _dbus_bindings, used by _dbus_glib_bindings and any third-party
  2. * main loop integration which might happen in future.
  3. *
  4. * This file is currently Python-version-independent - please keep it that way.
  5. *
  6. * Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
  7. *
  8. * Permission is hereby granted, free of charge, to any person
  9. * obtaining a copy of this software and associated documentation
  10. * files (the "Software"), to deal in the Software without
  11. * restriction, including without limitation the rights to use, copy,
  12. * modify, merge, publish, distribute, sublicense, and/or sell copies
  13. * of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  26. * DEALINGS IN THE SOFTWARE.
  27. */
  28. #ifndef DBUS_PYTHON_H
  29. #define DBUS_PYTHON_H
  30. #include <Python.h>
  31. #include <dbus/dbus.h>
  32. #if PY_MAJOR_VERSION >= 3
  33. #define PY3
  34. #define PYDBUS_CAPSULE_NAME "_dbus_bindings._C_API"
  35. #endif
  36. DBUS_BEGIN_DECLS
  37. typedef void (*_dbus_py_func_ptr)(void);
  38. typedef dbus_bool_t (*_dbus_py_conn_setup_func)(DBusConnection *, void *);
  39. typedef dbus_bool_t (*_dbus_py_srv_setup_func)(DBusServer *, void *);
  40. typedef void (*_dbus_py_free_func)(void *);
  41. #define DBUS_BINDINGS_API_COUNT 3
  42. #ifdef INSIDE_DBUS_PYTHON_BINDINGS
  43. extern DBusConnection *DBusPyConnection_BorrowDBusConnection(PyObject *);
  44. extern PyObject *DBusPyNativeMainLoop_New4(_dbus_py_conn_setup_func,
  45. _dbus_py_srv_setup_func,
  46. _dbus_py_free_func,
  47. void *);
  48. #else
  49. static PyObject *_dbus_bindings_module = NULL;
  50. static _dbus_py_func_ptr *dbus_bindings_API;
  51. #define DBusPyConnection_BorrowDBusConnection \
  52. (*(DBusConnection *(*)(PyObject *))dbus_bindings_API[1])
  53. #define DBusPyNativeMainLoop_New4 \
  54. ((PyObject *(*)(_dbus_py_conn_setup_func, _dbus_py_srv_setup_func, \
  55. _dbus_py_free_func, void *))dbus_bindings_API[2])
  56. static int
  57. import_dbus_bindings(const char *this_module_name)
  58. {
  59. PyObject *c_api;
  60. int count;
  61. _dbus_bindings_module = PyImport_ImportModule("_dbus_bindings");
  62. if (!_dbus_bindings_module) {
  63. return -1;
  64. }
  65. c_api = PyObject_GetAttrString(_dbus_bindings_module, "_C_API");
  66. if (c_api == NULL) return -1;
  67. #ifdef PY3
  68. dbus_bindings_API = NULL;
  69. if (PyCapsule_IsValid(c_api, PYDBUS_CAPSULE_NAME)) {
  70. dbus_bindings_API = (_dbus_py_func_ptr *)PyCapsule_GetPointer(
  71. c_api, PYDBUS_CAPSULE_NAME);
  72. }
  73. Py_CLEAR(c_api);
  74. if (!dbus_bindings_API) {
  75. PyErr_SetString(PyExc_RuntimeError, "C API is not a PyCapsule");
  76. return -1;
  77. }
  78. #else
  79. if (PyCObject_Check(c_api)) {
  80. dbus_bindings_API = (_dbus_py_func_ptr *)PyCObject_AsVoidPtr(c_api);
  81. }
  82. else {
  83. Py_DECREF(c_api);
  84. PyErr_SetString(PyExc_RuntimeError, "C API is not a PyCObject");
  85. return -1;
  86. }
  87. Py_DECREF (c_api);
  88. #endif
  89. count = *(int *)dbus_bindings_API[0];
  90. if (count < DBUS_BINDINGS_API_COUNT) {
  91. PyErr_Format(PyExc_RuntimeError,
  92. "_dbus_bindings has API version %d but %s needs "
  93. "_dbus_bindings API version at least %d",
  94. count, this_module_name,
  95. DBUS_BINDINGS_API_COUNT);
  96. return -1;
  97. }
  98. return 0;
  99. }
  100. #endif
  101. DBUS_END_DECLS
  102. #endif