diff options
author | Eddy Pedroni <epedroni@pm.me> | 2025-07-24 07:27:51 +0200 |
---|---|---|
committer | Eddy Pedroni <epedroni@pm.me> | 2025-07-24 07:27:51 +0200 |
commit | 49880b85de3d18f21792b9333571a396975697a0 (patch) | |
tree | 0e287353e6705b72f98e73d967bf3b91d9bc15b8 /daemon/src/device_connection.h | |
parent | c5c195ff5318f00d544c0fbceb133abcc4ba7a5a (diff) |
Add udev notification support for connecting/disconnecting devices
Diffstat (limited to 'daemon/src/device_connection.h')
-rw-r--r-- | daemon/src/device_connection.h | 81 |
1 files changed, 81 insertions, 0 deletions
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 <rtmidi/RtMidi.h> + +#include <string> + +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<std::uint8_t> 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 + |