Tomáš Pecka | 33563f5 | 2021-01-25 14:31:41 +0100 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <csignal> |
| 4 | #include <sdbus-c++/sdbus-c++.h> |
| 5 | #include <thread> |
| 6 | #include "utils/log.h" |
| 7 | |
| 8 | #define DBUS_EVENTLOOP_INIT \ |
| 9 | std::unique_ptr<sdbus::IConnection> g_dbusConnection; \ |
| 10 | std::thread g_eventLoop; |
| 11 | |
| 12 | #define DBUS_EVENTLOOP_START \ |
| 13 | spdlog::get("main")->debug("Opening DBus connection"); \ |
| 14 | g_dbusConnection = sdbus::createSystemBusConnection(); \ |
| 15 | /* Gracefully leave dbus event loop on SIGTERM */ \ |
| 16 | struct sigaction sigact; \ |
| 17 | memset(&sigact, 0, sizeof(sigact)); \ |
| 18 | /* sdbus-c++'s implementation doesn't mind if called before entering the event loop. It simply leaves the loop on entry */ \ |
| 19 | sigact.sa_flags = SA_SIGINFO; \ |
| 20 | sigact.sa_handler = [](int) { g_dbusConnection->leaveEventLoop(); }; \ |
| 21 | sigaction(SIGTERM, &sigact, nullptr); \ |
| 22 | spdlog::get("main")->debug("Starting DBus event loop"); \ |
| 23 | g_eventLoop = std::thread([] { g_dbusConnection->enterEventLoop(); }); \ |
| 24 | |
| 25 | #define DBUS_EVENTLOOP_END \ |
| 26 | g_eventLoop.join(); \ |
| 27 | spdlog::get("main")->debug("Shutting down"); |