aboutsummaryrefslogtreecommitdiffstats
path: root/core/dbus_wrap.h
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2021-04-18 00:34:36 -0700
committerChris Robinson <[email protected]>2021-04-18 00:43:01 -0700
commit5165b29b1945e1cff5e8c042bd371a5b2da9b492 (patch)
tree686d5edf04f9b33ca65ec3cf16c2bf536c73400a /core/dbus_wrap.h
parent784dbd7d21f36b0dbf034e7ac5d46cdc5533b91b (diff)
Optionally use RTKit/D-Bus to set elevated priority
If pthread_setschedparam fails or is unavailable.
Diffstat (limited to 'core/dbus_wrap.h')
-rw-r--r--core/dbus_wrap.h75
1 files changed, 75 insertions, 0 deletions
diff --git a/core/dbus_wrap.h b/core/dbus_wrap.h
new file mode 100644
index 00000000..61dbb971
--- /dev/null
+++ b/core/dbus_wrap.h
@@ -0,0 +1,75 @@
+#ifndef CORE_DBUS_WRAP_H
+#define CORE_DBUS_WRAP_H
+
+#include <memory>
+
+#include <dbus/dbus.h>
+
+#include "dynload.h"
+
+
+#define DBUS_FUNCTIONS(MAGIC) \
+MAGIC(dbus_error_init) \
+MAGIC(dbus_error_free) \
+MAGIC(dbus_bus_get) \
+MAGIC(dbus_connection_set_exit_on_disconnect) \
+MAGIC(dbus_connection_unref) \
+MAGIC(dbus_connection_send_with_reply_and_block) \
+MAGIC(dbus_message_unref) \
+MAGIC(dbus_message_new_method_call) \
+MAGIC(dbus_message_append_args) \
+MAGIC(dbus_message_iter_init) \
+MAGIC(dbus_message_iter_next) \
+MAGIC(dbus_message_iter_recurse) \
+MAGIC(dbus_message_iter_get_arg_type) \
+MAGIC(dbus_message_iter_get_basic) \
+MAGIC(dbus_set_error_from_message)
+
+#ifdef HAVE_DYNLOAD
+
+#include <mutex>
+
+extern void *dbus_handle;
+#define DECL_FUNC(x) extern decltype(x) *p##x;
+DBUS_FUNCTIONS(DECL_FUNC)
+#undef DECL_FUNC
+
+void PrepareDBus();
+
+inline auto HasDBus()
+{
+ static std::once_flag init_dbus{};
+ std::call_once(init_dbus, PrepareDBus);
+ return dbus_handle;
+}
+
+#else
+
+#define DECL_FUNC(x) constexpr auto p##x = &x;
+DBUS_FUNCTIONS(DECL_FUNC)
+#undef DECL_FUNC
+
+constexpr bool HasDBus() noexcept { return true; }
+#endif /* HAVE_DYNLOAD */
+
+
+namespace dbus {
+
+struct Error {
+ Error() { (*pdbus_error_init)(&mError); }
+ ~Error() { (*pdbus_error_free)(&mError); }
+ DBusError* operator->() { return &mError; }
+ DBusError &get() { return mError; }
+private:
+ DBusError mError{};
+};
+
+struct ConnectionDeleter {
+ void operator()(DBusConnection *c) { (*pdbus_connection_unref)(c); }
+};
+using ConnectionPtr = std::unique_ptr<DBusConnection,ConnectionDeleter>;
+
+} // namespace dbus
+
+
+#endif /* CORE_DBUS_WRAP_H */