]> repos.lx-s.de Git - winmute.git/commitdiff
Mute when BT Audio Device is disconnected/connected
authorlx-s <lx-s@users.noreply.github.com>
Mon, 1 Aug 2022 10:03:02 +0000 (12:03 +0200)
committerlx-s <lx-s@users.noreply.github.com>
Mon, 1 Aug 2022 10:03:02 +0000 (12:03 +0200)
Setup/wmsetup.iss [new file with mode: 0644]
WinMute/BluetoothDetector.cpp
WinMute/WMLog.cpp
WinMute/WMLog.h

diff --git a/Setup/wmsetup.iss b/Setup/wmsetup.iss
new file mode 100644 (file)
index 0000000..4967476
--- /dev/null
@@ -0,0 +1,58 @@
+; Script generated by the Inno Setup Script Wizard.
+; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
+
+#define MyAppName "WinMute"
+#define MyAppVersion "2.0.0.0"
+#define MyAppPublisher "LX-Systems"
+#define MyAppURL "https://www.lx-s.de/winmute"
+#define MyAppExeName "WinMute.exe"
+
+[Setup]
+; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
+; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
+AppId={{D2E8F9EF-11E7-418B-B9B7-A35A69A30490}
+AppName={#MyAppName}
+AppVersion={#MyAppVersion}
+;AppVerName={#MyAppName} {#MyAppVersion}
+AppPublisher={#MyAppPublisher}
+AppPublisherURL={#MyAppURL}
+AppSupportURL={#MyAppURL}
+AppUpdatesURL={#MyAppURL}
+DefaultDirName={autopf}\{#MyAppName}
+DisableProgramGroupPage=yes
+LicenseFile=E:\Development\Code\WinMute\Dist\bin\license.txt
+InfoAfterFile=E:\Development\Code\WinMute\Dist\bin\readme.html
+; Uncomment the following line to run in non administrative install mode (install for current user only.)
+;PrivilegesRequired=lowest
+PrivilegesRequiredOverridesAllowed=dialog
+OutputDir=E:\Development\Code\WinMute\Build\Setup
+OutputBaseFilename=wm-setup
+SetupIconFile=E:\Development\Code\WinMute\WinMute\icons\app.ico
+Compression=lzma
+SolidCompression=yes
+WizardStyle=modern
+
+[Languages]
+Name: "english"; MessagesFile: "compiler:Default.isl"
+
+[Tasks]
+Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
+
+[Files]
+Source: "E:\Development\Code\WinMute\Dist\bin\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion
+Source: "E:\Development\Code\WinMute\Dist\bin\changelog.txt"; DestDir: "{app}"; Flags: ignoreversion
+Source: "E:\Development\Code\WinMute\Dist\bin\license.txt"; DestDir: "{app}"; Flags: ignoreversion
+Source: "E:\Development\Code\WinMute\Dist\bin\liesmich.html"; DestDir: "{app}"; Flags: ignoreversion
+Source: "E:\Development\Code\WinMute\Dist\bin\readme.html"; DestDir: "{app}"; Flags: ignoreversion
+Source: "E:\Development\Code\WinMute\Dist\bin\ScreensaverNotify.dll"; DestDir: "{app}"; Flags: ignoreversion
+Source: "E:\Development\Code\WinMute\Dist\bin\ScreensaverNotify32.dll"; DestDir: "{app}"; Flags: ignoreversion
+Source: "E:\Development\Code\WinMute\Dist\bin\ScreensaverProxy32.exe"; DestDir: "{app}"; Flags: ignoreversion
+; NOTE: Don't use "Flags: ignoreversion" on any shared system files
+
+[Icons]
+Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
+Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
+
+[Run]
+Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
+
index bfa8b95847a1dae8b051220fed385ead8e052339..0896b5b2d279d8b944a7a2aa50d4b87fb323c2a6 100644 (file)
@@ -57,13 +57,14 @@ void BluetoothDetector::UnloadRadioNotifications()
 bool BluetoothDetector::LoadRadioNotifications()
 {
    bool success = false;
+   auto& log = WMLog::GetInstance();
    notificationHandles_.clear();
 
    BLUETOOTH_FIND_RADIO_PARAMS bfrp = { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) };
    HANDLE btHandle;
    HBLUETOOTH_RADIO_FIND hRadiosFind = BluetoothFindFirstRadio(&bfrp, &btHandle);
    if (hRadiosFind == nullptr) {
-      PrintWindowsError(L"BluetoothFindFirstRadio");
+      log.WriteWindowsError(L"BluetoothFindFirstRadio", GetLastError());
       return false;
    }
 
@@ -81,14 +82,14 @@ bool BluetoothDetector::LoadRadioNotifications()
       CloseHandle(btHandle);
 
       if (devNotify == NULL) {
-         PrintWindowsError(L"RegisterDeviceNotification", GetLastError());
+         log.WriteWindowsError(L"RegisterDeviceNotification", GetLastError());
       } else {
          notificationHandles_.push_back(devNotify);
       }
    } while (BluetoothFindNextRadio(hRadiosFind, &btHandle));
    DWORD dwLastError = GetLastError();
    if (dwLastError != ERROR_NO_MORE_ITEMS) {
-      PrintWindowsError(L"BluetoothFindNextRadio");
+      log.WriteWindowsError(L"BluetoothFindNextRadio", GetLastError());
       UnloadRadioNotifications();
    } else {
       success = true;
@@ -138,9 +139,18 @@ BluetoothDetector::BluetoothStatus BluetoothDetector::GetBluetoothStatus(
       return BluetoothStatus::Unknown;
    }
 
+   // only react to audio devices and not (e. g.) game controllers
+   if ((inRangeInfo->deviceInfo.flags & BDIF_COD) &&
+       GET_COD_MAJOR(inRangeInfo->deviceInfo.classOfDevice) != COD_MAJOR_AUDIO &&
+       GET_COD_MAJOR(inRangeInfo->deviceInfo.classOfDevice) != COD_MAJOR_PHONE) {
+      return BluetoothStatus::Unknown;
+   }
+
+   auto& log = WMLog::GetInstance();
    if (inRangeInfo->deviceInfo.flags & BDIF_CONNECTED) {
+      log.Write(L"Bluetooth Audio device \"%S\" connected.", inRangeInfo->deviceInfo.name);
       return BluetoothStatus::Connected;
    }
-
+   log.Write(L"Bluetooth Audio device \"%S\" disconnected.", inRangeInfo->deviceInfo.name);
    return BluetoothStatus::Disconnected;
 }
index f2842e5e09f5b71ee7cb23222002e1b6248212e9..fddb479f1bebf74c5f573ba8b9994bdf779e61c0 100644 (file)
@@ -134,3 +134,38 @@ void WMLog::Write(const TCHAR *fmt, ...)
 
    va_end(ap);
 }
+
+void WMLog::WriteWindowsError(LPCWSTR lpszFunction, DWORD lastError)
+{
+   // Retrieve the system error message for the last-error code
+   if (lastError == -1) {
+      lastError = GetLastError();
+   }
+
+   LPVOID lpMsgBuf;
+   if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
+      FORMAT_MESSAGE_FROM_SYSTEM |
+      FORMAT_MESSAGE_IGNORE_INSERTS,
+      nullptr, lastError,
+      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+      reinterpret_cast<LPTSTR>(&lpMsgBuf), 0, nullptr) != 0) {
+      size_t displayBufSize =
+         ((size_t)lstrlen(static_cast<LPCTSTR>(lpMsgBuf)) +
+            (size_t)lstrlen(static_cast<LPCTSTR>(lpszFunction))
+            + 40) * sizeof(TCHAR);
+      // Display the error message and exit the process
+      LPVOID lpDisplayBuf = reinterpret_cast<LPVOID>(
+         LocalAlloc(LMEM_ZEROINIT, displayBufSize));
+      if (lpDisplayBuf) {
+         StringCchPrintf((LPTSTR)lpDisplayBuf,
+            LocalSize(lpDisplayBuf),
+            _T("%s failed with error %u: %s"),
+            lpszFunction,
+            lastError,
+            reinterpret_cast<TCHAR*>(lpMsgBuf));
+         WriteMessage(static_cast<LPCTSTR>(lpMsgBuf));
+         LocalFree(lpDisplayBuf);
+      }
+      LocalFree(lpMsgBuf);
+   }
+}
index 91de656f363015f11e1412f18a849929ddcf85c9..16c0d137b7615d2b92494b5ac861061248e8c1c9 100644 (file)
@@ -40,6 +40,7 @@ public:
    static WMLog& GetInstance();
 
    void Write(const TCHAR* fmt, ...);
+   void WriteWindowsError(LPCWSTR lpszFunction, DWORD errorCode = -1);
 
    void SetEnabled(bool enable);
    std::string GetLogFilePath();