From 4185ad80a6f884990f79b60d26efe5bfe4f645c8 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Wed, 8 May 2019 19:58:05 +0200 Subject: Prepare for Ardour/ALSA multiple identical MIDI devices The engine setup identifies devices by name (device list is a map<> with the device-name as key). To support multiple devices with the same name, the name needs to be unique. So far this is achieved by simply adding a number suffix starting with the 2nd device (this allows to re-use configurations). Ideally we'd use UUIDs or unique device IDs to handle this, and also somehow clarify which device is which... --- libs/ardouralsautil/devicelist.cc | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'libs/ardouralsautil') diff --git a/libs/ardouralsautil/devicelist.cc b/libs/ardouralsautil/devicelist.cc index c2d83aab85..29aeb996a1 100644 --- a/libs/ardouralsautil/devicelist.cc +++ b/libs/ardouralsautil/devicelist.cc @@ -94,6 +94,29 @@ ARDOUR::get_alsa_audio_device_names (std::map& devices } } +static void +insert_unique_device_name (std::map& devices, std::string const& card_name, std::string const& devname) +{ + std::pair::iterator, bool> rv; + char cnt = '1'; + std::string cn = card_name; + /* Add numbers first this is be independent of physical ID (sequencer vs rawmidi). + * If this fails (>= 10 devices) add the device-name for uniqness + * + * XXX: Perhaps this is a bad idea, and `devname` should always be added if + * there is more than one device with the same name. + */ + do { + rv = devices.insert (std::make_pair (cn, devname)); + cn = card_name + " (" + cnt + ")"; + } while (!rv.second && ++cnt <= '9'); + + if (!rv.second) { + rv = devices.insert (std::make_pair (card_name + " (" + devname + ")", devname)); + assert (rv.second == true); + } +} + void ARDOUR::get_alsa_rawmidi_device_names (std::map& devices) { @@ -156,7 +179,7 @@ ARDOUR::get_alsa_rawmidi_device_names (std::map& devic if (sub < subs_out) card_name += "O"; card_name += ")"; - devices.insert (std::make_pair (card_name, devname)); + insert_unique_device_name (devices, card_name, devname); break; } else { devname = "hw:"; @@ -171,7 +194,7 @@ ARDOUR::get_alsa_rawmidi_device_names (std::map& devic if (sub < subs_in) card_name += "I"; if (sub < subs_out) card_name += "O"; card_name += ")"; - devices.insert (std::make_pair (card_name, devname)); + insert_unique_device_name (devices, card_name, devname); } } } @@ -226,7 +249,7 @@ ARDOUR::get_alsa_sequencer_names (std::map& devices) devname = PBD::to_string(snd_seq_port_info_get_client (pinfo)); devname += ":"; devname += PBD::to_string(snd_seq_port_info_get_port (pinfo)); - devices.insert (std::make_pair (card_name, devname)); + insert_unique_device_name (devices, card_name, devname); } } snd_seq_close (seq); -- cgit v1.2.3