Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
udev_monitor.hpp
Go to the documentation of this file.
1#pragma once
2
3// Cross-platform device-hotplug watcher. On Linux it monitors libudev
4// subsystems (loaded at runtime — no build-time libudev-dev dependency) and
5// invokes a callback on device add/remove. On non-Linux platforms it compiles
6// to an honest no-op: library_available() and start() return false, so callers
7// can use it unconditionally and simply get no hotplug events off Linux.
8//
9// Consumers: AlsaSystem (audio) and AlsaMidiSystem (MIDI) both watch the
10// "sound" subsystem — which covers ALSA PCM and raw-midi cards — and forward
11// the callback to their device-change notification. Lives in runtime so both
12// the audio and midi subsystems can share it without a cross-subsystem
13// dependency (both already link pulp::runtime).
14
15#include <atomic>
16#include <functional>
17#include <string>
18#include <thread>
19#include <vector>
20
22
23namespace pulp::runtime {
24
26enum class UdevChange { added, removed, other };
27
30UdevChange classify_udev_action(const char* action) noexcept;
31
33public:
34 using ChangeCallback = std::function<void(UdevChange)>;
35
36 UdevMonitor() = default;
38
41 static bool library_available();
42
47 bool start(const std::vector<std::string>& subsystems, ChangeCallback on_change);
48
51 void stop();
52
53 bool running() const { return running_.load(std::memory_order_acquire); }
54
55 UdevMonitor(const UdevMonitor&) = delete;
57
58private:
59 void run_loop(); // monitor thread body (Linux only)
60
61 DynamicLibrary lib_;
62 void* udev_ = nullptr; // struct udev*
63 void* monitor_ = nullptr; // struct udev_monitor*
64 int fd_ = -1; // monitor netlink fd
65 int wake_pipe_[2] = {-1, -1}; // self-pipe to break poll() on stop()
66 std::atomic<bool> running_{false};
67 std::thread thread_;
68 ChangeCallback on_change_;
69
70 struct Api; // resolved libudev entry points
71 Api* api_ = nullptr;
72};
73
74} // namespace pulp::runtime
Definition dynamic_library.hpp:10
bool running() const
Definition udev_monitor.hpp:53
std::function< void(UdevChange)> ChangeCallback
Definition udev_monitor.hpp:34
bool start(const std::vector< std::string > &subsystems, ChangeCallback on_change)
Begin watching subsystems (e.g.
static bool library_available()
True iff libudev can be loaded right now (Linux dlopen probe).
void stop()
Stop the monitor thread and release handles.
UdevMonitor(const UdevMonitor &)=delete
UdevMonitor & operator=(const UdevMonitor &)=delete
Definition generated_event_source.hpp:33
UdevChange
Classification of a udev action string. Pure + testable on every platform.
Definition udev_monitor.hpp:26
@ other
Definition udev_monitor.hpp:26
@ removed
Definition udev_monitor.hpp:26
@ added
Definition udev_monitor.hpp:26
UdevChange classify_udev_action(const char *action) noexcept
Map a udev ACTION string to a change kind.