]> repos.lx-s.de Git - winmute.git/commitdiff
#30 Reinit devices before unmuting
authorlx-s <lx-s@users.noreply.github.com>
Thu, 2 Nov 2023 08:50:12 +0000 (09:50 +0100)
committerlx-s <lx-s@users.noreply.github.com>
Thu, 2 Nov 2023 08:50:12 +0000 (09:50 +0100)
WinMute/MMNotificationClient.cpp
WinMute/MMNotificationClient.h
WinMute/VistaAudio.cpp

index 6348cbd10cf4b538940004a93d3e8acdaf26dc74..31c874027a0243b70ccd72ecc5aa928237ede25d 100644 (file)
@@ -35,6 +35,8 @@ POSSIBILITY OF SUCH DAMAGE.
 #include "WinAudio.h"
 #include "MMNotificationClient.h"
 
+#include <Functiondiscoverykeys_devpkey.h>
+
 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<void **>(&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;
+}
index a4eb9c1cb11f07479be9e5c56b13d82f5d0d9aa9..68cf5dd1ef8bc2337f3ad4e591fafa9936d5ca75 100644 (file)
@@ -59,5 +59,7 @@ private:
    LONG ref_;
    IMMDeviceEnumerator* pEnumerator_;
    WinAudio* notifyParent_;
+
+   std::wstring GetFriendlyDeviceName(LPCWSTR pwstrDeviceId);
 };
 
index 7aeb94366fb22cd6aa6f44d5620e6c735f828fb2..203d4ff20b16a17548073ee659c8b52d6a664be0 100644 (file)
@@ -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   |