🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.4 KiB

  1. #ifndef CORE_DBUS_WRAP_H
  2. #define CORE_DBUS_WRAP_H
  3. #include <memory>
  4. #include <dbus/dbus.h>
  5. #include "dynload.h"
  6. #ifdef HAVE_DYNLOAD
  7. #include <mutex>
  8. #define DBUS_FUNCTIONS(MAGIC) \
  9. MAGIC(dbus_error_init) \
  10. MAGIC(dbus_error_free) \
  11. MAGIC(dbus_bus_get) \
  12. MAGIC(dbus_connection_set_exit_on_disconnect) \
  13. MAGIC(dbus_connection_unref) \
  14. MAGIC(dbus_connection_send_with_reply_and_block) \
  15. MAGIC(dbus_message_unref) \
  16. MAGIC(dbus_message_new_method_call) \
  17. MAGIC(dbus_message_append_args) \
  18. MAGIC(dbus_message_iter_init) \
  19. MAGIC(dbus_message_iter_next) \
  20. MAGIC(dbus_message_iter_recurse) \
  21. MAGIC(dbus_message_iter_get_arg_type) \
  22. MAGIC(dbus_message_iter_get_basic) \
  23. MAGIC(dbus_set_error_from_message)
  24. extern void *dbus_handle;
  25. #define DECL_FUNC(x) extern decltype(x) *p##x;
  26. DBUS_FUNCTIONS(DECL_FUNC)
  27. #undef DECL_FUNC
  28. #ifndef IN_IDE_PARSER
  29. #define dbus_error_init (*pdbus_error_init)
  30. #define dbus_error_free (*pdbus_error_free)
  31. #define dbus_bus_get (*pdbus_bus_get)
  32. #define dbus_connection_set_exit_on_disconnect (*pdbus_connection_set_exit_on_disconnect)
  33. #define dbus_connection_unref (*pdbus_connection_unref)
  34. #define dbus_connection_send_with_reply_and_block (*pdbus_connection_send_with_reply_and_block)
  35. #define dbus_message_unref (*pdbus_message_unref)
  36. #define dbus_message_new_method_call (*pdbus_message_new_method_call)
  37. #define dbus_message_append_args (*pdbus_message_append_args)
  38. #define dbus_message_iter_init (*pdbus_message_iter_init)
  39. #define dbus_message_iter_next (*pdbus_message_iter_next)
  40. #define dbus_message_iter_recurse (*pdbus_message_iter_recurse)
  41. #define dbus_message_iter_get_arg_type (*pdbus_message_iter_get_arg_type)
  42. #define dbus_message_iter_get_basic (*pdbus_message_iter_get_basic)
  43. #define dbus_set_error_from_message (*pdbus_set_error_from_message)
  44. #endif
  45. void PrepareDBus();
  46. inline auto HasDBus()
  47. {
  48. static std::once_flag init_dbus{};
  49. std::call_once(init_dbus, []{ PrepareDBus(); });
  50. return dbus_handle;
  51. }
  52. #else
  53. constexpr bool HasDBus() noexcept { return true; }
  54. #endif /* HAVE_DYNLOAD */
  55. namespace dbus {
  56. struct Error {
  57. Error() { dbus_error_init(&mError); }
  58. ~Error() { dbus_error_free(&mError); }
  59. DBusError* operator->() { return &mError; }
  60. DBusError &get() { return mError; }
  61. private:
  62. DBusError mError{};
  63. };
  64. struct ConnectionDeleter {
  65. void operator()(DBusConnection *c) { dbus_connection_unref(c); }
  66. };
  67. using ConnectionPtr = std::unique_ptr<DBusConnection,ConnectionDeleter>;
  68. } // namespace dbus
  69. #endif /* CORE_DBUS_WRAP_H */