From 49880b85de3d18f21792b9333571a396975697a0 Mon Sep 17 00:00:00 2001 From: Eddy Pedroni Date: Thu, 24 Jul 2025 07:27:51 +0200 Subject: Add udev notification support for connecting/disconnecting devices --- daemon/src/device_connection.h | 81 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 daemon/src/device_connection.h (limited to 'daemon/src/device_connection.h') diff --git a/daemon/src/device_connection.h b/daemon/src/device_connection.h new file mode 100644 index 0000000..17e354d --- /dev/null +++ b/daemon/src/device_connection.h @@ -0,0 +1,81 @@ +#pragma once + +#include "types.h" +#include "submitter.h" +#include "sender.h" + +#include + +#include + +namespace midi_router +{ + +struct Device_Connection : public Sender +{ + Device_Connection(Device_Id const & source_id, std::string const & device_name, Submitter & submitter, RtMidiIn::RtMidiCallback callback): + source_id(source_id), + device_name(device_name), + submitter(submitter) + { + midi_in.setCallback(callback, this); + midi_in.ignoreTypes(true, false, true); + } + + Device_Id const & source_id; + std::string const & device_name; + Submitter & submitter; + RtMidiIn midi_in {}; + RtMidiOut midi_out {}; + + void + send(std::vector const & payload) override + { + midi_out.sendMessage(&payload); + } + + std::string + get_id() const override + { + return source_id; + } + + 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() + { + 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; + } +}; + +} // namespace midi_router + -- cgit v1.2.3