diff options
author | Eddy Pedroni <epedroni@pm.me> | 2025-07-24 10:34:13 +0200 |
---|---|---|
committer | Eddy Pedroni <epedroni@pm.me> | 2025-07-24 10:34:13 +0200 |
commit | 5a40f51085e2132bf91eccdd37e1a8dabe588efd (patch) | |
tree | ebd455ce36539cc1ad00e977b0b7cbc095780939 | |
parent | 49880b85de3d18f21792b9333571a396975697a0 (diff) |
Rough implementation of autoconnect
-rw-r--r-- | daemon/src/connection_manager.cpp | 52 | ||||
-rw-r--r-- | daemon/src/connection_manager.h | 3 | ||||
-rw-r--r-- | daemon/src/device_connection.h | 32 | ||||
-rw-r--r-- | daemon/src/main.cpp | 2 |
4 files changed, 52 insertions, 37 deletions
diff --git a/daemon/src/connection_manager.cpp b/daemon/src/connection_manager.cpp index ac4c6cb..faad061 100644 --- a/daemon/src/connection_manager.cpp +++ b/daemon/src/connection_manager.cpp @@ -3,9 +3,10 @@ #include "device_connection.h" #include "udevw/include/udevw.hpp" +#include <rtmidi/RtMidi.h> #include <iostream> -#include <chrono> +#include <map> namespace midi_router { @@ -36,8 +37,8 @@ Connection_Manager::Connection_Manager(Device_Map const & device_map, Submitter for (auto const & [name, id] : device_map) { m_connections[id] = std::make_unique<Device_Connection>(id, name, submitter, callback); - m_connections[id]->open(); } + refresh_devices(true, false); } Connection_Manager::~Connection_Manager() = default; @@ -51,13 +52,50 @@ Connection_Manager::get_sender(Device_Id const & device) const void Connection_Manager::detect_devices() { - while (true) + auto udev = udevw::Udev::create(); + auto monitor = udevw::Monitor::create_from_netlink(udev, "udev"); + monitor.filter_add_match_subsystem("sound"); // devtype = nullptr implied + monitor.enable_receiving(); + + int fd = monitor.get_fd(); + for (;;) { + fd_set fds; + FD_ZERO(&fds); + FD_SET(fd, &fds); + + if (select(fd +1, &fds, nullptr, nullptr, nullptr) > 0 && FD_ISSET(fd, &fds)) { + auto device = monitor.receive_device(); + + auto action = device.get_action(); + if (!action) continue; + + bool add = *action == "add"; + bool remove = *action == "remove"; + refresh_devices(add, remove); + } + } +} + +void +Connection_Manager::refresh_devices([[maybe_unused]] bool add, [[maybe_unused]] bool remove) +{ + // collect currently connected devices + RtMidiIn enumerator {}; + std::map<std::size_t, std::string> port_map {}; + for (std::size_t i = 0; i < enumerator.getPortCount(); ++i) + { + std::string name = enumerator.getPortName(i); + port_map[i] = name; + } + + for (auto & [id, device] : m_connections) { - std::cout << "Polling for devices\n"; - std::this_thread::sleep_for(std::chrono::seconds(1)); - for (auto && [id, connection] : m_connections) + for (auto const & [port, name] : port_map) { - connection->open(); + if (name.contains(device->device_name)) + { + device->reconnect(port); + } } } } diff --git a/daemon/src/connection_manager.h b/daemon/src/connection_manager.h index 21977a4..d8b0e90 100644 --- a/daemon/src/connection_manager.h +++ b/daemon/src/connection_manager.h @@ -28,6 +28,9 @@ private: void detect_devices(); + void + refresh_devices(bool add, bool remove); + Device_Map const & m_device_map; Submitter & m_submitter; std::map<Device_Id, std::unique_ptr<Device_Connection>> m_connections; diff --git a/daemon/src/device_connection.h b/daemon/src/device_connection.h index 17e354d..a2af8cc 100644 --- a/daemon/src/device_connection.h +++ b/daemon/src/device_connection.h @@ -41,39 +41,13 @@ struct Device_Connection : public Sender } void - open() - { - if (!open_port(midi_in, device_name)) - { - std::cerr << "Input port not found for device " << device_name << "\n"; - } - - if (!open_port(midi_out, device_name)) - { - std::cerr << "Output port not found for device " << device_name << "\n"; - } - } - - void - close() + reconnect(std::size_t port) { midi_in.closePort(); midi_out.closePort(); - } -private: - bool - open_port(RtMidi & midi, std::string const & name) - { - for (std::size_t i = 0; i < midi.getPortCount(); ++i) - { - if (midi.getPortName(i).contains(name)) - { - midi.openPort(i); - return true; - } - } - return false; + midi_in.openPort(port); + midi_out.openPort(port); } }; diff --git a/daemon/src/main.cpp b/daemon/src/main.cpp index f131f92..8ca0dd6 100644 --- a/daemon/src/main.cpp +++ b/daemon/src/main.cpp @@ -9,7 +9,7 @@ using namespace midi_router; -bool verbose = true; +bool verbose = false; int main() { |