OpenCPN Partial API docs
Loading...
Searching...
No Matches
linux_usb_watch.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2024 Alec Leamas *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
26
27// Definitions for "New device" and "device gone" events. Could be any device,
28// but in real life this is only USB.
29static const char* const kDevSender = "org.freedesktop.systemd1";
30static const char* const kDevInterface = "org.freedesktop.systemd1.Manager";
31static const char* const kDevAddMember = "UnitNew";
32static const char* const kDevRemoveMember = "UnitRemoved";
33
34// Definitions for suspend/resume event
35// Link:
36// https://www.freedesktop.org/software/systemd/man/latest/org.freedesktop.login1.html#Signals
37static const char* const kResSender = "org.freedesktop.login1";
38static const char* const kResInterface = "org.freedesktop.login1.Manager";
39static const char* const kResMember = "PrepareForSleep";
40
41static void dev_signal_cb(GDBusConnection* connection, const gchar* sender,
42 const gchar* object_path, const gchar* interface,
43 const gchar* signal, GVariant* parameters,
44 gpointer user_data) {
45 auto watch_daemon = static_cast<LinuxUsbWatchDaemon*>(user_data);
46 watch_daemon->m_sys_events.evt_dev_change.Notify();
47}
48
49static void prepare_for_sleep_cb(GDBusConnection* connection,
50 const gchar* sender, const gchar* object_path,
51 const gchar* interface, const gchar* signal,
52 GVariant* parameters, gpointer user_data) {
53 gboolean suspending;
54 g_variant_get(parameters, "(b)", &suspending);
55 auto watch_daemon = static_cast<LinuxUsbWatchDaemon*>(user_data);
56 if (!suspending) watch_daemon->m_sys_events.evt_resume.Notify();
57
58 // printf("Resume callback, arg: %s", suspending ? "true" : "false");
59}
60
61LinuxUsbWatchDaemon::~LinuxUsbWatchDaemon() { Stop(); }
62
63void LinuxUsbWatchDaemon::DoStart() {
64 g_main_context_push_thread_default(m_worker_context);
65 g_main_loop_run(m_main_loop);
66 g_main_context_pop_thread_default(m_worker_context);
67}
68
69void LinuxUsbWatchDaemon::Start() {
70 int filter_id_dev;
71 int filter_id_res;
72 GError* err = 0;
73
74 if (m_thread.joinable()) return; // already running
75 m_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, 0, &err);
76 g_dbus_connection_signal_subscribe(
77 m_conn, kDevSender, kDevInterface, kDevAddMember, 0, 0,
78 G_DBUS_SIGNAL_FLAGS_NONE, dev_signal_cb, this, 0);
79 g_dbus_connection_signal_subscribe(
80 m_conn, kDevSender, kDevInterface, kDevRemoveMember, 0, 0,
81 G_DBUS_SIGNAL_FLAGS_NONE, dev_signal_cb, this, 0);
82 g_dbus_connection_signal_subscribe(m_conn, kResSender, kResInterface,
83 kResMember, 0, 0, G_DBUS_SIGNAL_FLAGS_NONE,
84 prepare_for_sleep_cb, this, 0);
85 m_worker_context = g_main_context_new();
86 m_main_loop = g_main_loop_new(m_worker_context, false);
87 m_thread = std::thread([&] { DoStart(); });
88}
89
90void LinuxUsbWatchDaemon::Stop() {
91 if (!m_thread.joinable()) return; // Already stopped
92 g_main_loop_unref(m_main_loop);
93 g_main_loop_quit(m_main_loop);
94 m_thread.join();
95}
void Notify() override
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.
Definition sys_events.h:39
EventVar evt_dev_change
Notified when a new or removed device is detected, usually an USB hotplug event:
Definition sys_events.h:45
Linux specific hardware events DBus interface.