From 958281875c1cdcf5dc8047159ae4f0ee84b08691 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexander=20Steinh=C3=B6fer?= Date: Fri, 29 Jul 2022 15:38:55 +0200 Subject: [PATCH] Integrated mute logic for bluetooth detector --- WinMute/BluetoothDetector.cpp | 7 ++++++ WinMute/WMSettings.cpp | 3 +++ WinMute/WMSettings.h | 1 + WinMute/WinMute.cpp | 42 ++++++++++++++++++++++++----------- WinMute/WinMute.h | 1 + WinMute/WinMute.rc | 4 ++-- WinMute/resource.h | 4 +++- 7 files changed, 46 insertions(+), 16 deletions(-) diff --git a/WinMute/BluetoothDetector.cpp b/WinMute/BluetoothDetector.cpp index b18f7fb..bfa8b95 100644 --- a/WinMute/BluetoothDetector.cpp +++ b/WinMute/BluetoothDetector.cpp @@ -109,6 +109,13 @@ bool BluetoothDetector::Init(HWND hNotifyWnd) return initialized_; } +void BluetoothDetector::Unload() +{ + UnloadRadioNotifications(); + hNotifyWnd_ = NULL; + initialized_ = false; +} + BluetoothDetector::BluetoothStatus BluetoothDetector::GetBluetoothStatus( const UINT message, const WPARAM wParam, const LPARAM lParam) { diff --git a/WinMute/WMSettings.cpp b/WinMute/WMSettings.cpp index ca7d655..e662674 100644 --- a/WinMute/WMSettings.cpp +++ b/WinMute/WMSettings.cpp @@ -69,6 +69,9 @@ static LPCWSTR KeyToStr(SettingsKey key) case SettingsKey::MUTE_ON_LOGOUT: keyStr = _T("MuteOnLogout"); break; + case SettingsKey::MUTE_ON_BLUETOOTH: + keyStr = _T("MuteOnBluetooth"); + break; case SettingsKey::MUTE_ON_WLAN: keyStr = _T("MuteOnWlan"); break; diff --git a/WinMute/WMSettings.h b/WinMute/WMSettings.h index c0b3722..169d2db 100644 --- a/WinMute/WMSettings.h +++ b/WinMute/WMSettings.h @@ -44,6 +44,7 @@ enum class SettingsKey { , MUTE_ON_SUSPEND , MUTE_ON_SHUTDOWN , MUTE_ON_LOGOUT + , MUTE_ON_BLUETOOTH , MUTE_ON_WLAN , MUTE_ON_WLAN_ALLOWLIST , QUIETHOURS_ENABLE diff --git a/WinMute/WinMute.cpp b/WinMute/WinMute.cpp index dfa8597..90a6c2a 100644 --- a/WinMute/WinMute.cpp +++ b/WinMute/WinMute.cpp @@ -135,7 +135,7 @@ static bool IsCurrentSessionRemoteable() } WinMute::MuteConfig::MuteConfig() - : muteOnWlan(false), showNotifications(false) + : muteOnWlan(false), muteOnBluetooth(false), showNotifications(false) { } @@ -330,8 +330,23 @@ bool WinMute::LoadSettings() muteConfig_.showNotifications = settings_.QueryValue(SettingsKey::NOTIFICATIONS_ENABLED); + muteConfig_.muteOnBluetooth = settings_.QueryValue(SettingsKey::MUTE_ON_BLUETOOTH); + if (!muteConfig_.muteOnBluetooth) { + btDetector_.Unload(); + } else if (!btDetector_.Init(hWnd_)) { + TaskDialog(nullptr, nullptr, PROGRAM_NAME, + _T("Bluetooth not available"), + _T("Either the Bluetooth service has not been started, ") + _T("or your device is not capable of using bluetooth connections. ") + _T("This feature will be disabled for this computer."), + TDCBF_OK_BUTTON, TD_INFORMATION_ICON, nullptr); + settings_.SetValue(SettingsKey::MUTE_ON_BLUETOOTH, FALSE); + } + muteConfig_.muteOnWlan = settings_.QueryValue(SettingsKey::MUTE_ON_WLAN); - if (muteConfig_.muteOnWlan) { + if (!muteConfig_.muteOnWlan) { + wifiDetector_.Unload(); + } else { if (!wifiDetector_.Init(hWnd_)) { TaskDialog(nullptr, nullptr, PROGRAM_NAME, _T("WLAN not available"), @@ -345,8 +360,6 @@ bool WinMute::LoadSettings() wifiDetector_.SetNetworkList(settings_.GetWifiNetworks(), isMuteList); wifiDetector_.CheckNetwork(); } - } else { - wifiDetector_.Unload(); } return true; @@ -444,7 +457,7 @@ LRESULT CALLBACK WinMute::WindowProc( } break; } - case ID_TRAYMENU_SCREENSUSPEND: { + case ID_TRAYMENU_MUTEONSCREENSUSPEND: { bool checked = false; ToggleMenuCheck(ID_TRAYMENU_SCREENSUSPEND, &checked); muteCtrl_.SetMuteOnDisplayStandby(checked); @@ -467,15 +480,16 @@ LRESULT CALLBACK WinMute::WindowProc( } case ID_TRAYMENU_MUTEONLOGOUT: { bool checked = false; - ToggleMenuCheck(ID_TRAYMENU_MUTEONLOGOUT, &checked); + ToggleMenuCheck(ID_TRAYMENU_MUTEONBLUETOOTH, &checked); muteCtrl_.SetMuteOnLogout(checked); settings_.SetValue(SettingsKey::MUTE_ON_LOGOUT, checked); break; } case ID_TRAYMENU_BLUETOOTH: { bool checked = false; - ToggleMenuCheck(ID_TRAYMENU_MUTEONLOGOUT, &checked); - + ToggleMenuCheck(ID_TRAYMENU_MUTEON, &checked); + settings_.SetValue(SettingsKey::MUTE_ON_BLUETOOTH, checked); + break; } default: @@ -566,11 +580,13 @@ LRESULT CALLBACK WinMute::WindowProc( return 0; } case WM_DEVICECHANGE: { - const auto btStatus = btDetector_.GetBluetoothStatus(msg, wParam, lParam); - if (btStatus == BluetoothDetector::BluetoothStatus::Connected) { - MessageBox(hWnd, L"CONNECTED!", NULL, 0); - } else if (btStatus == BluetoothDetector::BluetoothStatus::Disconnected) { - MessageBox(hWnd, L"DIS-CONNECTED!", NULL, 0); + if (muteConfig_.muteOnBluetooth) { + const auto btStatus = btDetector_.GetBluetoothStatus(msg, wParam, lParam); + if (btStatus == BluetoothDetector::BluetoothStatus::Connected) { + muteCtrl_.SetMute(false); + } else if (btStatus == BluetoothDetector::BluetoothStatus::Disconnected) { + muteCtrl_.SetMute(true); + } } return TRUE; } diff --git a/WinMute/WinMute.h b/WinMute/WinMute.h index a6501ef..47c14b2 100644 --- a/WinMute/WinMute.h +++ b/WinMute/WinMute.h @@ -57,6 +57,7 @@ private: MuteConfig(); bool showNotifications; bool muteOnWlan; + bool muteOnBluetooth; } muteConfig_; TrayIcon trayIcon_; diff --git a/WinMute/WinMute.rc b/WinMute/WinMute.rc index 8b8dbe0..589e635 100644 --- a/WinMute/WinMute.rc +++ b/WinMute/WinMute.rc @@ -338,8 +338,8 @@ BEGIN MENUITEM "Mute when...", 40003, INACTIVE MENUITEM "... Workstation is locked", ID_TRAYMENU_MUTEONLOCK, CHECKED MENUITEM "... Screensaver starts", ID_TRAYMENU_MUTEONSCREENSAVER, CHECKED - MENUITEM "... Screen turns off", ID_TRAYMENU_SCREENSUSPEND, CHECKED - MENUITEM "...Bluetooth Device disconnects", ID_TRAYMENU_BLUETOOTH + MENUITEM "... Screen turns off", ID_TRAYMENU_MUTEONSCREENSUSPEND, CHECKED + MENUITEM "...Bluetooth Device disconnects", ID_TRAYMENU_MUTEONBLUETOOTH MENUITEM SEPARATOR MENUITEM "Restore volume afterwards", ID_TRAYMENU_RESTOREAUDIO, CHECKED MENUITEM SEPARATOR diff --git a/WinMute/resource.h b/WinMute/resource.h index 62084ac..620db5c 100644 --- a/WinMute/resource.h +++ b/WinMute/resource.h @@ -88,13 +88,15 @@ #define ID_TRAYMENU_40028 40028 #define ID_TRAYMENU_40029 40029 #define ID_TRAYMENU_BLUETOOTH 40030 +#define ID_TRAYMENU_MUTEONBLUETOOTH 40031 +#define ID_TRAYMENU_MUTEONSCREENSUSPEND 40032 // Next default values for new objects // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 131 -#define _APS_NEXT_COMMAND_VALUE 40031 +#define _APS_NEXT_COMMAND_VALUE 40033 #define _APS_NEXT_CONTROL_VALUE 1030 #define _APS_NEXT_SYMED_VALUE 101 #endif -- 2.43.0