30 static const char*
const kDevSender =
"org.freedesktop.systemd1";
31 static const char*
const kDevInterface =
"org.freedesktop.systemd1.Manager";
32 static const char*
const kDevAddMember =
"UnitNew";
33 static const char*
const kDevRemoveMember =
"UnitRemoved";
38 static const char*
const kResSender =
"org.freedesktop.login1";
39 static const char*
const kResInterface =
"org.freedesktop.login1.Manager";
40 static const char*
const kResMember =
"PrepareForSleep";
42 static void dev_signal_cb(GDBusConnection* connection,
const gchar* sender,
43 const gchar* object_path,
const gchar* interface,
44 const gchar* signal, GVariant* parameters,
50 static void prepare_for_sleep_cb(GDBusConnection* connection,
51 const gchar* sender,
const gchar* object_path,
52 const gchar* interface,
const gchar* signal,
53 GVariant* parameters, gpointer user_data) {
55 g_variant_get(parameters,
"(b)", &suspending);
62 LinuxUsbWatchDaemon::~LinuxUsbWatchDaemon() { Stop(); }
64 void LinuxUsbWatchDaemon::DoStart() {
65 g_main_context_push_thread_default(m_worker_context);
66 g_main_loop_run(m_main_loop);
67 g_main_context_pop_thread_default(m_worker_context);
70 void LinuxUsbWatchDaemon::Start() {
75 if (m_thread.joinable())
return;
76 m_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, 0, &err);
77 g_dbus_connection_signal_subscribe(
78 m_conn, kDevSender, kDevInterface, kDevAddMember, 0, 0,
79 G_DBUS_SIGNAL_FLAGS_NONE, dev_signal_cb,
this, 0);
80 g_dbus_connection_signal_subscribe(
81 m_conn, kDevSender, kDevInterface, kDevRemoveMember, 0, 0,
82 G_DBUS_SIGNAL_FLAGS_NONE, dev_signal_cb,
this, 0);
83 g_dbus_connection_signal_subscribe(
84 m_conn, kResSender, kResInterface, kResMember, 0, 0,
85 G_DBUS_SIGNAL_FLAGS_NONE, prepare_for_sleep_cb,
this, 0);
86 m_worker_context = g_main_context_new();
87 m_main_loop = g_main_loop_new(m_worker_context,
false);
88 m_thread = std::thread([&]{ DoStart(); });
91 void LinuxUsbWatchDaemon::Stop() {
92 if (!m_thread.joinable())
return;
93 g_main_loop_unref(m_main_loop);
94 g_main_loop_quit(m_main_loop);
const void Notify()
Notify all listeners, no data supplied.
Listen to DBus system bus signals reflecting for example suspend/resume, new USB devicesbeing plugged...
EventVar evt_resume
Notified when resuming from hibernate.
EventVar evt_dev_change
Notified when a new or removed device is detected, usually an USB hotplug event:
Linux specific hardware events DBus interface.