From efd7b30cb45972e784cf49606798deea09f83fb1 Mon Sep 17 00:00:00 2001 From: lx-s Date: Thu, 13 Apr 2023 10:59:07 +0200 Subject: [PATCH] Added new settings functions for audio endpoints --- WinMute/WMSettings.cpp | 114 ++++++++++++++++++++++++++++++++++++++++- WinMute/WMSettings.h | 4 ++ 2 files changed, 117 insertions(+), 1 deletion(-) diff --git a/WinMute/WMSettings.cpp b/WinMute/WMSettings.cpp index 68dec2a..cc3d5b5 100644 --- a/WinMute/WMSettings.cpp +++ b/WinMute/WMSettings.cpp @@ -39,6 +39,8 @@ static const wchar_t* LX_SYSTEMS_WIFI_SUBKEY = L"SOFTWARE\\lx-systems\\WinMute\\WifiNetworks"; static const wchar_t* LX_SYSTEMS_BLUETOOTH_SUBKEY = L"SOFTWARE\\lx-systems\\WinMute\\BluetoothDevices"; +static const wchar_t *LX_SYSTEMS_AUDIO_ENDPOINTS_SUBKEY += L"SOFTWARE\\lx-systems\\WinMute\\ManagedAudioEndpoints"; static const wchar_t* LX_SYSTEMS_AUTOSTART_KEY = L"LX-Systems WinMute"; @@ -190,7 +192,8 @@ static bool ReadStringFromRegistry(HKEY hKey, const wchar_t *subKey, std::wstrin WMSettings::WMSettings() : hSettingsKey_(nullptr), hWifiKey_(nullptr), - hBluetoothKey_(nullptr) + hBluetoothKey_(nullptr), + hAudioEndpointsKey_(nullptr) { } @@ -255,6 +258,28 @@ bool WMSettings::Init() return false; } } + if (hAudioEndpointsKey_ == nullptr) { + DWORD regError = RegCreateKeyExW( + HKEY_CURRENT_USER, + LX_SYSTEMS_AUDIO_ENDPOINTS_SUBKEY, + 0, + nullptr, + 0, + KEY_READ | KEY_WRITE, + nullptr, + &hBluetoothKey_, + nullptr); + if (regError != ERROR_SUCCESS) { + PrintWindowsError(L"RegCreateKeyEx", regError); + RegCloseKey(hWifiKey_); + RegCloseKey(hSettingsKey_); + RegCloseKey(hBluetoothKey_); + hSettingsKey_ = nullptr; + hWifiKey_ = nullptr; + hBluetoothKey_ = nullptr; + return false; + } + } return true; } @@ -583,3 +608,90 @@ std::vector WMSettings::GetBluetoothDevicesW() const NormalizeStringList(devices); return devices; } + + +bool WMSettings::StoreManagedAudioEndpoints(std::vector &endpoints) +{ + // Clear all stored keys + for (;;) { + wchar_t valueName[260] = { 0 }; + DWORD valueSize = ARRAY_SIZE(valueName); + DWORD regError = RegEnumValueW( + hAudioEndpointsKey_, + 0, + valueName, + &valueSize, + nullptr, + nullptr, + nullptr, + nullptr); + if (regError == ERROR_NO_MORE_ITEMS) { + break; + } else if (regError != ERROR_SUCCESS) { + PrintWindowsError(L"RegEnumValue", regError); + return false; + } else { + regError = RegDeleteValue(hAudioEndpointsKey_, valueName); + if (regError != ERROR_SUCCESS) { + PrintWindowsError(L"RegDeleteValue", regError); + return false; + } + } + } + + NormalizeStringList(endpoints); + + for (size_t i = 0; i < endpoints.size(); ++i) { + wchar_t valueName[25]; + StringCchPrintfW( + valueName, + ARRAY_SIZE(valueName), + L"Endpoint %03lld", i + 1); + const std::wstring &v = endpoints[i]; + DWORD regError = RegSetValueEx( + hAudioEndpointsKey_, + valueName, + 0, + REG_SZ, + reinterpret_cast(v.c_str()), + static_cast(v.length() + 1) * sizeof(wchar_t)); + if (regError != ERROR_SUCCESS) { + PrintWindowsError(L"RegSetValueEx", regError); + return false; + } + } + + return true; +} + +std::vector WMSettings::GetManagedAudioEndpoints() const +{ + std::vector devices; + for (int valIdx = 0; ; ++valIdx) { + wchar_t valueName[260] = { 0 }; + wchar_t dataBuf[260] = { 0 }; + DWORD valueSize = ARRAY_SIZE(valueName); + DWORD valType = 0; + DWORD dataLen = ARRAY_SIZE(dataBuf); + + DWORD regError = RegEnumValueW( + hAudioEndpointsKey_, + valIdx, + valueName, + &valueSize, + nullptr, + &valType, + reinterpret_cast(dataBuf), + &dataLen); + if (regError == ERROR_NO_MORE_ITEMS) { + break; + } else if (regError != ERROR_SUCCESS) { + PrintWindowsError(L"RegEnumValue", regError); + return {}; + } else { + devices.push_back(dataBuf); + } + } + NormalizeStringList(devices); + return devices; +} diff --git a/WinMute/WMSettings.h b/WinMute/WMSettings.h index c99d310..081c659 100644 --- a/WinMute/WMSettings.h +++ b/WinMute/WMSettings.h @@ -79,6 +79,9 @@ public: std::vector GetBluetoothDevicesW() const; std::vector GetBluetoothDevicesA() const; + bool StoreManagedAudioEndpoints(std::vector &endpoints); + std::vector GetManagedAudioEndpoints() const; + DWORD QueryValue(SettingsKey key) const; bool SetValue(SettingsKey key, DWORD value); @@ -86,6 +89,7 @@ private: HKEY hSettingsKey_; HKEY hWifiKey_; HKEY hBluetoothKey_; + HKEY hAudioEndpointsKey_; HKEY OpenAutostartKey(REGSAM samDesired); }; -- 2.43.0