From: lx-s Date: Thu, 2 Nov 2023 08:50:12 +0000 (+0100) Subject: #30 Reinit devices before unmuting X-Git-Tag: 2.4.9.0~80 X-Git-Url: https://repos.lx-s.de/gitweb/?a=commitdiff_plain;h=8b18e019a71a9f3e539d910ef5daba52ae6369c0;p=winmute.git #30 Reinit devices before unmuting --- diff --git a/WinMute/MMNotificationClient.cpp b/WinMute/MMNotificationClient.cpp index 6348cbd..31c8740 100644 --- a/WinMute/MMNotificationClient.cpp +++ b/WinMute/MMNotificationClient.cpp @@ -35,6 +35,8 @@ POSSIBILITY OF SUCH DAMAGE. #include "WinAudio.h" #include "MMNotificationClient.h" +#include + class WinAudio; MMNotificationClient::MMNotificationClient(WinAudio* notifyParent) : @@ -84,17 +86,21 @@ HRESULT STDMETHODCALLTYPE MMNotificationClient::OnDefaultDeviceChanged( return S_OK; } -HRESULT STDMETHODCALLTYPE MMNotificationClient::OnDeviceAdded(LPCWSTR) +HRESULT STDMETHODCALLTYPE MMNotificationClient::OnDeviceAdded(LPCWSTR pwstrDeviceId) { - if (notifyParent_) { + if (notifyParent_ && pwstrDeviceId != nullptr) { + const auto deviceName = GetFriendlyDeviceName(pwstrDeviceId); + WMLog::GetInstance().GetInstance().Write(L"Device \"%S\" added", deviceName.c_str()); notifyParent_->ShouldReInit(); } return S_OK; } -HRESULT STDMETHODCALLTYPE MMNotificationClient::OnDeviceRemoved(LPCWSTR) +HRESULT STDMETHODCALLTYPE MMNotificationClient::OnDeviceRemoved(LPCWSTR pwstrDeviceId) { - if (notifyParent_) { + if (notifyParent_ && pwstrDeviceId != nullptr) { + const auto deviceName = GetFriendlyDeviceName(pwstrDeviceId); + WMLog::GetInstance().GetInstance().Write(L"Device \"%S\" removed", deviceName.c_str()); notifyParent_->ShouldReInit(); } return S_OK; @@ -103,12 +109,29 @@ HRESULT STDMETHODCALLTYPE MMNotificationClient::OnDeviceRemoved(LPCWSTR) HRESULT STDMETHODCALLTYPE MMNotificationClient::OnDeviceStateChanged( LPCWSTR pwstrDeviceId, DWORD dwNewState) { - UNREFERENCED_PARAMETER(pwstrDeviceId); - if (dwNewState == DEVICE_STATE_UNPLUGGED || - dwNewState == DEVICE_STATE_ACTIVE) { - if (notifyParent_) { - notifyParent_->ShouldReInit(); - } + bool notify = true; + std::wstring what; + if (pwstrDeviceId == nullptr) { + return S_OK; + } + if (dwNewState == DEVICE_STATE_NOTPRESENT) { + what = L"Not present"; + } else if (dwNewState == DEVICE_STATE_UNPLUGGED) { + what = L"Unplugged"; + } else if (dwNewState == DEVICE_STATE_ACTIVE) { + what = L"Active"; + } else { + notify = false; + } + + if (notify && notifyParent_) { + // TODO: Check if output device + const auto deviceName = GetFriendlyDeviceName(pwstrDeviceId); + WMLog::GetInstance().GetInstance().Write( + L"Device \"%s\" status changed to %s", + deviceName.c_str(), + what.c_str()); + notifyParent_->ShouldReInit(); } return S_OK; } @@ -119,3 +142,41 @@ HRESULT STDMETHODCALLTYPE MMNotificationClient::OnPropertyValueChanged( return S_OK; } +std::wstring MMNotificationClient:: GetFriendlyDeviceName(LPCWSTR pwstrDeviceId) +{ + HRESULT hr = S_OK; + IMMDevice *pDevice = nullptr; + IPropertyStore *pProps = nullptr; + PROPVARIANT varString; + + PropVariantInit(&varString); + + if (pEnumerator_ == nullptr) { + // Get enumerator for audio endpoint devices. + hr = CoCreateInstance( + __uuidof(MMDeviceEnumerator), + nullptr, CLSCTX_INPROC_SERVER, + __uuidof(IMMDeviceEnumerator), + reinterpret_cast(&pEnumerator_)); + } + if (hr == S_OK) { + hr = pEnumerator_->GetDevice(pwstrDeviceId, &pDevice); + } + if (hr == S_OK) { + hr = pDevice->OpenPropertyStore(STGM_READ, &pProps); + } + if (hr == S_OK){ + // Get the endpoint device's friendly-name property. + hr = pProps->GetValue(PKEY_Device_FriendlyName, &varString); + } + std::wstring deviceName = L"Unknown device"; + if (hr == S_OK) { + deviceName = varString.pwszVal; + } + + PropVariantClear(&varString); + + SafeRelease(&pProps); + SafeRelease(&pDevice); + return deviceName; +} diff --git a/WinMute/MMNotificationClient.h b/WinMute/MMNotificationClient.h index a4eb9c1..68cf5dd 100644 --- a/WinMute/MMNotificationClient.h +++ b/WinMute/MMNotificationClient.h @@ -59,5 +59,7 @@ private: LONG ref_; IMMDeviceEnumerator* pEnumerator_; WinAudio* notifyParent_; + + std::wstring GetFriendlyDeviceName(LPCWSTR pwstrDeviceId); }; diff --git a/WinMute/VistaAudio.cpp b/WinMute/VistaAudio.cpp index 7aeb943..203d4ff 100644 --- a/WinMute/VistaAudio.cpp +++ b/WinMute/VistaAudio.cpp @@ -261,6 +261,9 @@ bool VistaAudio::RestoreMuteStatus() bool success = true; WMLog& log = WMLog::GetInstance(); + if (!CheckForReInit()) { + return false; + } for (auto& e : endpoints_) { if (!IsEndpointManaged(e->deviceName)) { log.Write(L"Skipping Endpoint %s", e->deviceName); @@ -318,7 +321,7 @@ bool VistaAudio::IsEndpointManaged(const std::wstring &endpointName) const std::end(managedEndpointNames_), endpointName) != std::end(managedEndpointNames_); - // ---------------------------- + // ------------+----------+-------------+ // | In List | Not in List | // ------------+----------+-------------+ // Allow List | Mute | Not mute |