]> repos.lx-s.de Git - winmute.git/commitdiff
WiFi muting with allow/blocklist.
authorAlexander Steinhöfer <kontakt@lx-s.de>
Wed, 13 Apr 2022 18:38:17 +0000 (20:38 +0200)
committerAlexander Steinhöfer <kontakt@lx-s.de>
Wed, 13 Apr 2022 18:38:17 +0000 (20:38 +0200)
WinMute/Settings_WifiDlg.cpp
WinMute/WMSettings.cpp
WinMute/WMSettings.h
WinMute/WiFiDetector.h
WinMute/WifiDetector.cpp
WinMute/WinMute.cpp
WinMute/WinMute.h
WinMute/WinMute.rc
WinMute/WinMute.vcxproj
WinMute/common.h
WinMute/resource.h

index a71f16d2f00dd48a3adb157a39b8a08493755bcb..73b31f89eb5c4d6ba822e374d9b17fc7891ddcf0 100644 (file)
@@ -134,6 +134,14 @@ INT_PTR CALLBACK Settings_GeneralWifiDlgProc(HWND hDlg, UINT msg, WPARAM wParam,
       assert(settings != nullptr);
       SetWindowLongPtr(hDlg, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(settings));
 
+      DWORD enabled = settings->QueryValue(SettingsKey::MUTE_ON_WLAN);
+      Button_SetCheck(GetDlgItem(hDlg, IDC_ENABLE_WIFI_MUTE),
+                      enabled ? BST_CHECKED : BST_UNCHECKED);
+
+      enabled = settings->QueryValue(SettingsKey::MUTE_ON_WLAN_ALLOWLIST);
+      Button_SetCheck(GetDlgItem(hDlg, IDC_IS_PERMITLIST),
+                      enabled ? BST_CHECKED : BST_UNCHECKED);
+
       Button_Enable(GetDlgItem(hDlg, IDC_WIFI_EDIT), FALSE);
       Button_Enable(GetDlgItem(hDlg, IDC_WIFI_REMOVE), FALSE);
 
@@ -232,6 +240,13 @@ INT_PTR CALLBACK Settings_GeneralWifiDlgProc(HWND hDlg, UINT msg, WPARAM wParam,
       WMSettings* settings = reinterpret_cast<WMSettings*>(GetWindowLongPtr(hDlg, GWLP_USERDATA));
       std::vector<tstring> networks = ExportSsidListItems(GetDlgItem(hDlg, IDC_WIFI_LIST));
       settings->StoreWifiNetworks(networks);
+
+      DWORD checked = Button_GetCheck(GetDlgItem(hDlg, IDC_ENABLE_WIFI_MUTE));
+      settings->SetValue(SettingsKey::MUTE_ON_WLAN, checked == BST_CHECKED);
+
+      checked = Button_GetCheck(GetDlgItem(hDlg, IDC_IS_PERMITLIST));
+      settings->SetValue(SettingsKey::MUTE_ON_WLAN_ALLOWLIST, checked == BST_CHECKED);
+      
       return 0;
    }
    default:
index c502222dccf5e438ef8d3ba3038cef6e3c2e382f..9b3349d779305cf271d395b4ff55229e1f91ace6 100644 (file)
@@ -69,6 +69,12 @@ static LPCWSTR KeyToStr(SettingsKey key)
    case SettingsKey::MUTE_ON_LOGOUT:
       keyStr = _T("MuteOnLogout");
       break;
+   case SettingsKey::MUTE_ON_WLAN:
+      keyStr = _T("MuteOnWlan");
+      break;
+   case SettingsKey::MUTE_ON_WLAN_ALLOWLIST:
+      keyStr = _T("MuteOnWlanAllowList");
+      break;
    case SettingsKey::QUIETHOURS_ENABLE:
       keyStr = _T("QuietHoursEnabled");
       break;
@@ -113,6 +119,10 @@ static DWORD GetDefaultSetting(SettingsKey key)
       return 0;
    case SettingsKey::MUTE_ON_LOGOUT:
       return 0;
+   case SettingsKey::MUTE_ON_WLAN:
+      return 0;
+   case SettingsKey::MUTE_ON_WLAN_ALLOWLIST:
+      return 0;
    case SettingsKey::QUIETHOURS_ENABLE:
       return 0;
    case SettingsKey::QUIETHOURS_FORCEUNMUTE:
index a1b2d5a836a9e4556015cf8657536345fc06f4b6..c0b3722ee56c8272f0617719bd445e0d0be4ec0b 100644 (file)
@@ -44,6 +44,8 @@ enum class SettingsKey {
    , MUTE_ON_SUSPEND
    , MUTE_ON_SHUTDOWN
    , MUTE_ON_LOGOUT
+   , MUTE_ON_WLAN
+   , MUTE_ON_WLAN_ALLOWLIST
    , QUIETHOURS_ENABLE
    , QUIETHOURS_FORCEUNMUTE
    , QUIETHOURS_NOTIFICATIONS
index c91fede3a28ed3f9db3b5195999d47dcd1a53de6..82e752411cdcbb34363e25bb27715df35a921091 100644 (file)
@@ -35,6 +35,9 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #include "common.h"
 
+/* wParam = Connected = 1 | Disconnected = 0. lParam = Pointer to Wifi Name */
+constexpr int WM_WIFISTATUSCHANGED = WM_USER + 400;
+
 class WifiDetector {
 public:
    WifiDetector();
@@ -43,11 +46,15 @@ public:
    WifiDetector& operator=(const WifiDetector&) = delete;
 
    bool Init(HWND hNotifyWnd);
-   void SetNetworkList(const std::vector<tstring> networks);
+   void Unload();
+   void SetNetworkList(const std::vector<tstring> networks, bool isMuteList);
+
+   void WlanNotificationCallback(PWLAN_NOTIFICATION_DATA notifyData);
 
 private:
    HWND hNotifyWnd_;
    HANDLE wlanHandle_;
+   bool isMuteList_;
    bool initialized_;
    std::vector<tstring> networks_;
 };
index fb020a9f87cb6d9c25bef19114c7c036097dc414..2652ab3dd136c386bf604fac3e53866701ea0aec 100644 (file)
@@ -35,8 +35,19 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #include "common.h"
 
+static void WlanNotificationCallback(
+   PWLAN_NOTIFICATION_DATA unnamedParam1,
+   PVOID unnamedParam2)
+{
+   WifiDetector* wifiDetector = reinterpret_cast<WifiDetector*>(unnamedParam2);
+   if (wifiDetector != nullptr) {
+      wifiDetector->WlanNotificationCallback(unnamedParam1);
+   }
+}
+
+
 WifiDetector::WifiDetector()
-   : hNotifyWnd_(NULL), wlanHandle_(NULL), initialized_(false)
+   : hNotifyWnd_(NULL), wlanHandle_(NULL), isMuteList_(true), initialized_(false)
 {
 }
 
@@ -44,13 +55,63 @@ WifiDetector::~WifiDetector()
 {
 }
 
+void WifiDetector::Unload()
+{
+   if (initialized_) {
+      WlanRegisterNotification(wlanHandle_, WLAN_NOTIFICATION_SOURCE_NONE,
+         TRUE, NULL, NULL, NULL, NULL);
+      WlanCloseHandle(wlanHandle_, NULL);
+      initialized_ = false;
+      hNotifyWnd_ = NULL;
+   }
+}
+
 bool WifiDetector::Init(HWND hNotifyWnd)
 {
-   hNotifyWnd_ = hNotifyWnd;
-   return true;
+   if (!initialized_) {
+      DWORD vers = 2;
+      hNotifyWnd_ = hNotifyWnd;
+      DWORD wlErr = WlanOpenHandle(vers, NULL, &vers, &wlanHandle_);
+      if (wlErr != ERROR_SUCCESS) {
+         PrintWindowsError(_T("WlanOpenHandle"), wlErr);
+      } else {
+         wlErr = WlanRegisterNotification(
+            wlanHandle_, WLAN_NOTIFICATION_SOURCE_ACM, TRUE,
+            ::WlanNotificationCallback, this, NULL, NULL);
+         if (wlErr != ERROR_SUCCESS) {
+            PrintWindowsError(_T("WlanOpenHandle"), wlErr);
+            WlanCloseHandle(wlanHandle_, NULL);
+         } else {
+            initialized_ = true;
+         }
+      }
+   }
+   return initialized_;
 }
 
-void WifiDetector::SetNetworkList(const std::vector<tstring> networks)
+void WifiDetector::SetNetworkList(const std::vector<tstring> networks, bool isMuteList)
 {
    networks_ = networks;
+   isMuteList_ = isMuteList;
+}
+
+void WifiDetector::WlanNotificationCallback(PWLAN_NOTIFICATION_DATA notifyData)
+{
+   if (notifyData->NotificationSource != WLAN_NOTIFICATION_SOURCE_ACM) {
+      return;
+   }
+   if (notifyData->NotificationCode == wlan_notification_acm_connection_complete
+       || notifyData->NotificationCode == wlan_notification_acm_disconnected) {
+      bool connected = notifyData->NotificationCode == wlan_notification_acm_connection_complete;
+      WLAN_CONNECTION_NOTIFICATION_DATA *wcnd =
+         reinterpret_cast<WLAN_CONNECTION_NOTIFICATION_DATA*>(notifyData->pData);
+      const auto it = std::find(std::begin(networks_), std::end(networks_),
+                                wcnd->strProfileName);
+      if (isMuteList_ && it != std::end(networks_)) {
+         SendMessage(hNotifyWnd_, WM_WIFISTATUSCHANGED, connected,
+                      reinterpret_cast<LPARAM>((*it).c_str()));
+      } else if (!isMuteList_ && it == std::end(networks_)) {
+         SendMessage(hNotifyWnd_, WM_WIFISTATUSCHANGED, connected, NULL);
+      }
+   }
 }
index 797f4685ab595d1c3a746ca53f72df99d7a1eb66..5fe5c3de2261480da3e775ceb760adee2a0ef405 100644 (file)
@@ -247,6 +247,10 @@ bool WinMute::Init()
 #endif
    log.Write(_T("Starting new session..."));
 
+   if (!RegisterWindowClass() || !InitWindow()) {
+      return false;
+   }
+
    if (!InitAudio()) {
       return false;
    }
@@ -255,9 +259,7 @@ bool WinMute::Init()
       return false;
    }
 
-   if (!RegisterWindowClass() ||
-       !InitWindow() ||
-       !InitTrayMenu()) {
+   if (!InitTrayMenu()) {
       return false;
    }
 
@@ -327,6 +329,15 @@ bool WinMute::LoadSettings()
    muteConfig_.quietHours.start = settings_.QueryValue(SettingsKey::QUIETHOURS_START);
    muteConfig_.quietHours.end = settings_.QueryValue(SettingsKey::QUIETHOURS_END);
 
+   muteConfig_.muteOnWlan = settings_.QueryValue(SettingsKey::MUTE_ON_WLAN);
+   if (muteConfig_.muteOnWlan) {
+      wifiDetector_.Init(hWnd_);
+      bool isMuteList = !settings_.QueryValue(SettingsKey::MUTE_ON_WLAN_ALLOWLIST);
+      wifiDetector_.SetNetworkList(settings_.GetWifiNetworks(), isMuteList);
+   } else {
+      wifiDetector_.Unload();
+   }
+
    return true;
 }
 
@@ -557,6 +568,38 @@ LRESULT CALLBACK WinMute::WindowProc(
       }
       return 0;
    }
+   case WM_WIFISTATUSCHANGED:
+      if (muteConfig_.muteOnWlan) {
+         if (wParam == 1) { // Connected
+            if (settings_.QueryValue(SettingsKey::NOTIFICATIONS_ENABLED)) {
+               TCHAR msgBuf[260] = { 0 };
+               const TCHAR* wifiName = reinterpret_cast<TCHAR*>(lParam);
+               
+               if (settings_.QueryValue(SettingsKey::MUTE_ON_WLAN_ALLOWLIST)) {
+                  StringCchCopy(
+                     msgBuf,
+                     ARRAY_SIZE(msgBuf),
+                     _T("Current WLAN network is not on allowed list for AutoMute"));
+               }  else {
+#ifdef _UNICODE
+                  swprintf(
+                     msgBuf, ARRAY_SIZE(msgBuf),
+                     _T("WLAN network \"%s\" is configured for AutoMute."),
+                     wifiName);
+#else
+                  sprintf_s(
+                     msgBuf,
+                     "WLAN network \"%s\" is configured for AutoMute.",
+                     wifiName);
+#endif
+               }
+               trayIcon_.ShowPopup(_T("Workstation muted"), msgBuf);
+               
+            }
+            muteCtrl_.SetMute(true);
+         }
+      }
+      return 0;
    case WM_SETTINGCHANGE: {
       if (lstrcmp(LPCTSTR(lParam), _T("ImmersiveColorSet")) == 0) {
          bool isDarkMode = true;
index 88b2a72209d15c904e84a70cf9799f7f9fcd7e4a..f84f8391cbfec9e924261d4307dbe611cc3df78b 100644 (file)
@@ -69,10 +69,12 @@ private:
          time_t start;
          time_t end;
       } quietHours;
+      bool muteOnWlan;
    } muteConfig_;
 
    TrayIcon trayIcon_;
    ScreensaverNotifier scrnSaverNoti_;
+   WifiDetector wifiDetector_;
    WMSettings settings_;
    MuteControl muteCtrl_;
 
index e53c9f8e4dabfadfc4f35ea539a2408ef1937fc5..4a112c43abf71c4bde41fe2135a6f8b8a5e65904 100644 (file)
@@ -146,13 +146,16 @@ IDD_SETTINGS_WIFI DIALOGEX 0, 0, 242, 204
 STYLE DS_SETFONT | DS_FIXEDSYS | WS_CHILD
 FONT 8, "MS Shell Dlg", 400, 0, 0x1
 BEGIN
-    LTEXT           "WinMute can detect when you are connecting to a particular WiFi Network and mute your PC when it finishes connecting. \nHere you can manage SSIDs of networks that should trigger muting:",IDC_STATIC,7,7,228,35
-    LISTBOX         IDC_WIFI_LIST,7,40,170,134,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
-    PUSHBUTTON      "Add",IDC_WIFI_ADD,179,40,56,16
-    PUSHBUTTON      "Edit",IDC_WIFI_EDIT,179,59,56,16
-    PUSHBUTTON      "Remove",IDC_WIFI_REMOVE,179,78,56,16
-    PUSHBUTTON      "Remove all",IDC_WIFI_REMOVEALL,179,98,56,16
-    LTEXT           "Note: This feature is currently experimental",IDC_STATIC,7,182,171,15
+    LISTBOX         IDC_WIFI_LIST,7,66,170,115,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
+    PUSHBUTTON      "Add",IDC_WIFI_ADD,179,65,56,16
+    PUSHBUTTON      "Edit",IDC_WIFI_EDIT,179,84,56,16
+    PUSHBUTTON      "Remove",IDC_WIFI_REMOVE,179,103,56,16
+    PUSHBUTTON      "Remove all",IDC_WIFI_REMOVEALL,179,123,56,16
+    LTEXT           "Note: This feature is currently experimental",IDC_STATIC,7,186,171,11
+    CONTROL         "Enable ""WLAN Mute"".",IDC_ENABLE_WIFI_MUTE,"Button",BS_AUTOCHECKBOX | BS_MULTILINE | WS_TABSTOP,7,26,228,14
+    CONTROL         "Mute workstation when WLAN network name is NOT in list\n(default: mute if in list).",IDC_IS_PERMITLIST,
+                    "Button",BS_AUTOCHECKBOX | BS_MULTILINE | WS_TABSTOP,7,41,228,18
+    LTEXT           """WLAN Mute"" allows you to mute your workstation based on the name of the connected wireless network.",IDC_STATIC,7,7,228,19
 END
 
 IDD_SETTINGS_WIFI_ADD DIALOGEX 0, 0, 201, 62
index 473bcd5db497d8342759e780b7047f913741c475..3a76e087ad40b0d93b761dee52548a4b372628da 100644 (file)
     <Link>
       <SubSystem>Windows</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
-      <AdditionalDependencies>strsafe.lib;comctl32.lib;WtsApi32.lib;Winmm.lib;UxTheme.lib;comsuppwd.lib;Version.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>strsafe.lib;comctl32.lib;WtsApi32.lib;Winmm.lib;UxTheme.lib;comsuppwd.lib;Wlanapi.lib;Version.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
     <Manifest>
       <EnableDpiAwareness>PerMonitorHighDPIAware</EnableDpiAwareness>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
       <GenerateDebugInformation>true</GenerateDebugInformation>
-      <AdditionalDependencies>strsafe.lib;comctl32.lib;WtsApi32.lib;Winmm.lib;UxTheme.lib;comsuppwd.lib;Version.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>strsafe.lib;comctl32.lib;WtsApi32.lib;Winmm.lib;UxTheme.lib;comsuppwd.lib;Wlanapi.lib;Version.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
     <Manifest>
       <EnableDpiAwareness>PerMonitorHighDPIAware</EnableDpiAwareness>
     <Link>
       <SubSystem>Windows</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
-      <AdditionalDependencies>strsafe.lib;comctl32.lib;WtsApi32.lib;Winmm.lib;UxTheme.lib;comsuppwd.lib;Version.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>strsafe.lib;comctl32.lib;WtsApi32.lib;Winmm.lib;UxTheme.lib;comsuppwd.lib;Wlanapi.lib;Version.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
     <Manifest>
       <EnableDpiAwareness>PerMonitorHighDPIAware</EnableDpiAwareness>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
       <GenerateDebugInformation>true</GenerateDebugInformation>
-      <AdditionalDependencies>strsafe.lib;comctl32.lib;WtsApi32.lib;Winmm.lib;UxTheme.lib;comsuppwd.lib;Version.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>strsafe.lib;comctl32.lib;WtsApi32.lib;Winmm.lib;UxTheme.lib;comsuppwd.lib;Wlanapi.lib;Version.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
     <Manifest>
       <EnableDpiAwareness>PerMonitorHighDPIAware</EnableDpiAwareness>
index 761381087f1b71ed010ee09bcaa282ae52803f6c..a0c354ccb6ded9ffd9a901ecc828a48f76fdb5e9 100644 (file)
@@ -73,6 +73,7 @@ POSSIBILITY OF SUCH DAMAGE.
 #include <uxtheme.h>
 #include <comip.h>
 #include <comdef.h>
+#include <wlanapi.h>
 #include <Mmdeviceapi.h>
 #pragma warning(disable : 4201)
 #  include <endpointvolume.h>
@@ -87,9 +88,10 @@ POSSIBILITY OF SUCH DAMAGE.
 #include "TrayIcon.h"
 #include "WinAudio.h"
 #include "MuteControl.h"
+#include "WiFiDetector.h"
 #include "WinMute.h"
 #include "VersionHelper.h"
-#include "WiFiDetector.h"
+
 
 // Utility
 void PrintWindowsError(LPCWSTR lpszFunction, DWORD lastError = -1);
index 8155bcaa34c5a901950f61028326bf915034608a..0ad7729bd1d673b6394dcab16f6e5ddf51366e81 100644 (file)
@@ -32,6 +32,7 @@
 #define IDC_CHECK1                      1011
 #define IDC_RUNONSTARTUP                1011
 #define IDC_MUTE_WHEN_WS_LOCKED         1011
+#define IDC_ENABLE_WIFI_MUTE            1011
 #define IDC_ENABLELOGGING               1012
 #define IDC_CHECK3                      1013
 #define IDC_OPENLOG                     1014
@@ -54,6 +55,8 @@
 #define IDC_MUTE_WHEN_SLEEP             1027
 #define IDC_WIFI_REMOVE2                1027
 #define IDC_WIFI_REMOVEALL              1027
+#define IDC_CHECK2                      1028
+#define IDC_IS_PERMITLIST               1028
 #define ID_TRAYMENU_INFO                40001
 #define ID_TRAYMENU_                    40002
 #define ID_TRAYMENU_MUTEON              40003
@@ -89,7 +92,7 @@
 #ifndef APSTUDIO_READONLY_SYMBOLS
 #define _APS_NEXT_RESOURCE_VALUE        131
 #define _APS_NEXT_COMMAND_VALUE         40029
-#define _APS_NEXT_CONTROL_VALUE         1026
+#define _APS_NEXT_CONTROL_VALUE         1029
 #define _APS_NEXT_SYMED_VALUE           101
 #endif
 #endif