From 6b99840a3d7af07d51d84b1658ef2b1a7cdb7bcb Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 11 Sep 2016 17:26:13 +0200 Subject: [PATCH] Unplug ALSA cards as soon as we get the inotify message. If not, devices not in use would not be properly removed before someone tried to remove them. --- alsa_input.cpp | 15 ++++++++++++++- alsa_input.h | 2 ++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/alsa_input.cpp b/alsa_input.cpp index 78faaf3..0ba07b1 100644 --- a/alsa_input.cpp +++ b/alsa_input.cpp @@ -471,6 +471,19 @@ ALSAPool::ProbeResult ALSAPool::probe_device_once(unsigned card_index, unsigned return ALSAPool::ProbeResult::SUCCESS; } +void ALSAPool::unplug_device(unsigned card_index, unsigned dev_index) +{ + char address[256]; + snprintf(address, sizeof(address), "hw:%d,%d", card_index, dev_index); + for (unsigned i = 0; i < devices.size(); ++i) { + if (devices[i].state != Device::State::EMPTY && + devices[i].state != Device::State::DEAD && + devices[i].address == address) { + free_card(i); + } + } +} + void ALSAPool::init() { thread(&ALSAPool::inotify_thread_func, this).detach(); @@ -519,7 +532,7 @@ void ALSAPool::inotify_thread_func() if (sscanf(event->name, "pcmC%uD%u%c", &card, &device, &type) == 3 && type == 'c') { if (event->mask & (IN_MOVED_FROM | IN_DELETE)) { printf("Deleted capture device: Card %u, device %u\n", card, device); - // TODO: Unplug. + unplug_device(card, device); } if (event->mask & (IN_MOVED_TO | IN_CREATE)) { printf("Adding capture device: Card %u, device %u\n", card, device); diff --git a/alsa_input.h b/alsa_input.h index 4638e4b..c0dbafc 100644 --- a/alsa_input.h +++ b/alsa_input.h @@ -182,6 +182,8 @@ private: }; ProbeResult probe_device_once(unsigned card_index, unsigned dev_index); + void unplug_device(unsigned card_index, unsigned dev_index); + // Must be called with held. Will allocate a new entry if needed. // The returned entry will be set to READY state. unsigned find_free_device_index(const std::string &name, -- 2.39.2