-----------------------------------------------------------------------------
*/
+#include "MuteControl.h"
+
#include "common.h"
extern HINSTANCE hglobInstance;
-static constexpr int BLUETOOTH_RECONNECT_UNMUTE_DELAY = 5000; // Milliseconds
+static constexpr int BLUETOOTH_RECONNECT_UNMUTE_DELAY = 5000; // Milliseconds
static constexpr int MUTE_DELAY_MAGIC_VALUE = 0x198604;
// Fixed timer IDs: SetTimer with an ID of 0 returns a value that is not
static constexpr UINT_PTR DELAYED_MUTE_TIMER_ID = 190501;
static constexpr UINT_PTR BLUETOOTH_UNMUTE_TIMER_ID = 190502;
-static const wchar_t *MUTECONTROL_CLASS_NAME = L"WinMuteMuteControl";
+static const wchar_t* MUTECONTROL_CLASS_NAME = L"WinMuteMuteControl";
void CALLBACK DelayedMuteTimerProc(HWND hWnd, UINT, UINT_PTR, DWORD)
{
- MuteControl *muteCtrl = reinterpret_cast<MuteControl *>(GetWindowLongPtrW(hWnd, GWLP_USERDATA));
- if (muteCtrl != nullptr)
- {
- muteCtrl->MuteDelayed(MUTE_DELAY_MAGIC_VALUE);
- }
-}
-
-static void CALLBACK BluetoothUnmuteTimerProc(HWND hWnd, UINT, UINT_PTR id, DWORD)
-{
- KillTimer(hWnd, id);
- MuteControl *muteCtrl = reinterpret_cast<MuteControl *>(GetWindowLongPtrW(hWnd, GWLP_USERDATA));
- if (muteCtrl != nullptr)
- {
- muteCtrl->CompleteVolumeRestore();
- }
-}
-
-static LRESULT CALLBACK MuteControlWndProc(
- HWND hWnd,
- UINT msg,
- WPARAM wParam,
- LPARAM lParam)
-{
- auto wm = reinterpret_cast<MuteControl *>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
- switch (msg)
- {
- case WM_NCCREATE:
- {
- LPCREATESTRUCTW cs = reinterpret_cast<LPCREATESTRUCTW>(lParam);
- SetWindowLongPtrW(hWnd, GWLP_USERDATA,
- reinterpret_cast<LONG_PTR>(cs->lpCreateParams));
- return TRUE;
- }
- default:
- break;
- }
- return (wm)
- ? wm->WindowProc(hWnd, msg, wParam, lParam)
- : DefWindowProcW(hWnd, msg, wParam, lParam);
+ MuteControl* muteCtrl =
+ reinterpret_cast<MuteControl*>(GetWindowLongPtrW(hWnd, GWLP_USERDATA));
+ if (muteCtrl != nullptr) {
+ muteCtrl->MuteDelayed(MUTE_DELAY_MAGIC_VALUE);
+ }
+}
+
+static void CALLBACK BluetoothUnmuteTimerProc(HWND hWnd, UINT, UINT_PTR id,
+ DWORD)
+{
+ KillTimer(hWnd, id);
+ MuteControl* muteCtrl =
+ reinterpret_cast<MuteControl*>(GetWindowLongPtrW(hWnd, GWLP_USERDATA));
+ if (muteCtrl != nullptr) {
+ muteCtrl->CompleteVolumeRestore();
+ }
+}
+
+static LRESULT CALLBACK MuteControlWndProc(HWND hWnd, UINT msg, WPARAM wParam,
+ LPARAM lParam)
+{
+ auto wm =
+ reinterpret_cast<MuteControl*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
+ switch (msg) {
+ case WM_NCCREATE: {
+ LPCREATESTRUCTW cs = reinterpret_cast<LPCREATESTRUCTW>(lParam);
+ SetWindowLongPtrW(hWnd, GWLP_USERDATA,
+ reinterpret_cast<LONG_PTR>(cs->lpCreateParams));
+ return TRUE;
+ }
+ default:
+ break;
+ }
+ return (wm) ? wm->WindowProc(hWnd, msg, wParam, lParam)
+ : DefWindowProcW(hWnd, msg, wParam, lParam);
}
MuteControl::MuteControl()
{
- MuteConfig initMuteConf;
- initMuteConf.active = false;
- initMuteConf.shouldMute = false;
- for (int i = 0; i < MuteTypeCount; ++i)
- {
- muteConfig_.push_back(initMuteConf);
- }
+ MuteConfig initMuteConf;
+ initMuteConf.active = false;
+ initMuteConf.shouldMute = false;
+ for (int i = 0; i < MuteTypeCount; ++i) {
+ muteConfig_.push_back(initMuteConf);
+ }
}
MuteControl::~MuteControl()
{
- if (hMuteCtrlWnd_ != nullptr)
- {
- DestroyWindow(hMuteCtrlWnd_);
- hMuteCtrlWnd_ = nullptr;
- }
- UnregisterClassW(MUTECONTROL_CLASS_NAME, hglobInstance);
-}
-
-bool MuteControl::Init(HWND hParent, const TrayIcon *trayIcon)
-{
- WNDCLASSEXW wndClass{0};
- wndClass.cbSize = sizeof(wndClass);
- wndClass.lpfnWndProc = MuteControlWndProc;
- wndClass.hInstance = hglobInstance;
- wndClass.lpszClassName = MUTECONTROL_CLASS_NAME;
- if (!RegisterClassExW(&wndClass))
- {
- WMLog::GetInstance().LogWinError(L"RegisterClassEx");
- return false;
- }
- hMuteCtrlWnd_ = CreateWindowEx(
- WS_EX_TOOLWINDOW,
- MUTECONTROL_CLASS_NAME,
- L"",
- 0,
- 0, 0, 0, 0,
- nullptr,
- nullptr,
- hglobInstance,
- this);
- if (hMuteCtrlWnd_ == nullptr)
- {
- WMLog::GetInstance().LogWinError(L"CreateWindowEx");
- UnregisterClassW(MUTECONTROL_CLASS_NAME, hglobInstance);
- return false;
- }
- winAudio_ = std::make_unique<VistaAudio>();
- if (!winAudio_->Init(hParent))
- {
- DestroyWindow(hMuteCtrlWnd_);
- hMuteCtrlWnd_ = nullptr;
- UnregisterClassW(MUTECONTROL_CLASS_NAME, hglobInstance);
- return false;
- }
-
- trayIcon_ = trayIcon;
- return true;
-}
-
-LRESULT CALLBACK MuteControl::WindowProc(
- HWND hWnd,
- UINT msg,
- WPARAM wParam,
- LPARAM lParam)
-{
- return DefWindowProc(hWnd, msg, wParam, lParam);
+ if (hMuteCtrlWnd_ != nullptr) {
+ DestroyWindow(hMuteCtrlWnd_);
+ hMuteCtrlWnd_ = nullptr;
+ }
+ UnregisterClassW(MUTECONTROL_CLASS_NAME, hglobInstance);
+}
+
+bool MuteControl::Init(HWND hParent, const TrayIcon* trayIcon)
+{
+ WNDCLASSEXW wndClass{0};
+ wndClass.cbSize = sizeof(wndClass);
+ wndClass.lpfnWndProc = MuteControlWndProc;
+ wndClass.hInstance = hglobInstance;
+ wndClass.lpszClassName = MUTECONTROL_CLASS_NAME;
+ if (!RegisterClassExW(&wndClass)) {
+ WMLog::GetInstance().LogWinError(L"RegisterClassEx");
+ return false;
+ }
+ hMuteCtrlWnd_ =
+ CreateWindowEx(WS_EX_TOOLWINDOW, MUTECONTROL_CLASS_NAME, L"", 0, 0, 0,
+ 0, 0, nullptr, nullptr, hglobInstance, this);
+ if (hMuteCtrlWnd_ == nullptr) {
+ WMLog::GetInstance().LogWinError(L"CreateWindowEx");
+ UnregisterClassW(MUTECONTROL_CLASS_NAME, hglobInstance);
+ return false;
+ }
+ winAudio_ = std::make_unique<VistaAudio>();
+ if (!winAudio_->Init(hParent)) {
+ DestroyWindow(hMuteCtrlWnd_);
+ hMuteCtrlWnd_ = nullptr;
+ UnregisterClassW(MUTECONTROL_CLASS_NAME, hglobInstance);
+ return false;
+ }
+
+ trayIcon_ = trayIcon;
+ return true;
+}
+
+LRESULT CALLBACK MuteControl::WindowProc(HWND hWnd, UINT msg, WPARAM wParam,
+ LPARAM lParam)
+{
+ return DefWindowProc(hWnd, msg, wParam, lParam);
}
void MuteControl::SetMute(bool mute)
{
- WMLog::GetInstance().LogInfo(L"Manual muting: {}", mute ? L"on" : L"off");
- winAudio_->SetMute(mute);
+ WMLog::GetInstance().LogInfo(L"Manual muting: {}", mute ? L"on" : L"off");
+ winAudio_->SetMute(mute);
}
void MuteControl::SaveMuteStatus()
{
- const bool alreadySaved = std::any_of(
- muteConfig_.begin(),
- muteConfig_.end(),
- [](const MuteConfig &conf)
- {
- return conf.shouldMute && conf.active;
- });
+ const bool alreadySaved = std::any_of(
+ muteConfig_.begin(), muteConfig_.end(),
+ [](const MuteConfig& conf) { return conf.shouldMute && conf.active; });
- if (alreadySaved)
- {
- WMLog::GetInstance().LogInfo(L"Muting event already active. Skipping status save");
- }
- else
- {
- WMLog::GetInstance().LogInfo(L"Saving mute status");
- winAudio_->SaveMuteStatus();
- }
+ if (alreadySaved) {
+ WMLog::GetInstance().LogInfo(
+ L"Muting event already active. Skipping status save");
+ } else {
+ WMLog::GetInstance().LogInfo(L"Saving mute status");
+ winAudio_->SaveMuteStatus();
+ }
}
-void MuteControl::ShowNotification(const std::wstring &title, const std::wstring &text)
+void MuteControl::ShowNotification(const std::wstring& title,
+ const std::wstring& text)
{
- if (notificationsEnabled_ && trayIcon_ != nullptr)
- {
- trayIcon_->ShowPopup(title, text);
- }
+ if (notificationsEnabled_ && trayIcon_ != nullptr) {
+ trayIcon_->ShowPopup(title, text);
+ }
}
void MuteControl::SetNotifications(bool enable)
{
- notificationsEnabled_ = enable;
+ notificationsEnabled_ = enable;
}
void MuteControl::MuteDelayed(int magic)
{
- if (magic != MUTE_DELAY_MAGIC_VALUE || delayedMuteTimerId_ == 0)
- {
- return;
- }
- WMLog::GetInstance().LogInfo(L"Muting workstation after delay");
- MuteNow();
- KillTimer(hMuteCtrlWnd_, delayedMuteTimerId_);
- delayedMuteTimerId_ = 0;
+ if (magic != MUTE_DELAY_MAGIC_VALUE || delayedMuteTimerId_ == 0) {
+ return;
+ }
+ WMLog::GetInstance().LogInfo(L"Muting workstation after delay");
+ MuteNow();
+ KillTimer(hMuteCtrlWnd_, delayedMuteTimerId_);
+ delayedMuteTimerId_ = 0;
}
bool MuteControl::StartDelayedMute()
{
- if (SetTimer(
- hMuteCtrlWnd_,
- DELAYED_MUTE_TIMER_ID,
- muteDelaySeconds_ * 1000,
- DelayedMuteTimerProc) == 0)
- {
- WMLog::GetInstance().LogWinError(L"SetTimer", GetLastError());
- delayedMuteTimerId_ = 0;
- }
- else
- {
- delayedMuteTimerId_ = DELAYED_MUTE_TIMER_ID;
- }
- return delayedMuteTimerId_ != 0;
+ if (SetTimer(hMuteCtrlWnd_, DELAYED_MUTE_TIMER_ID, muteDelaySeconds_ * 1000,
+ DelayedMuteTimerProc) == 0)
+ {
+ WMLog::GetInstance().LogWinError(L"SetTimer", GetLastError());
+ delayedMuteTimerId_ = 0;
+ } else {
+ delayedMuteTimerId_ = DELAYED_MUTE_TIMER_ID;
+ }
+ return delayedMuteTimerId_ != 0;
}
void MuteControl::RestoreVolume(bool withDelay)
{
- WMLog &log = WMLog::GetInstance();
- if (!restoreVolume_)
- {
- log.LogInfo(L"Volume Restore has been disabled");
- return;
- }
- const bool restore = !std::any_of(
- muteConfig_.begin(),
- muteConfig_.end(),
- [](const MuteConfig &conf)
- {
- return conf.shouldMute && conf.active;
- });
- if (!restore)
- {
- log.LogInfo(L"Skipping restore since other mute event is currently active");
- }
- else if (delayedMuteTimerId_ != 0)
- {
- KillTimer(hMuteCtrlWnd_, delayedMuteTimerId_);
- delayedMuteTimerId_ = 0;
- log.LogInfo(L"Skipping restore, since delayed mute was not triggered yet");
- }
- else if (withDelay)
- {
- log.LogInfo(L"Restoring previous mute state after Bluetooth delay");
- ShowNotification(
- WMi18n::GetInstance().GetTranslationW("popup.volume-restored.title"),
- WMi18n::GetInstance().GetTranslationW("popup.volume-restored.text"));
- if (SetTimer(
- hMuteCtrlWnd_,
- BLUETOOTH_UNMUTE_TIMER_ID,
- BLUETOOTH_RECONNECT_UNMUTE_DELAY,
- BluetoothUnmuteTimerProc) == 0)
- {
- log.LogWinError(L"SetTimer (Bluetooth unmute delay)");
- bluetoothUnmuteTimerId_ = 0;
- CompleteVolumeRestore(); // fall back to immediate restore
- }
- else
- {
- bluetoothUnmuteTimerId_ = BLUETOOTH_UNMUTE_TIMER_ID;
- }
- }
- else
- {
- log.LogInfo(L"Restoring previous mute state");
- ShowNotification(
- WMi18n::GetInstance().GetTranslationW("popup.volume-restored.title"),
- WMi18n::GetInstance().GetTranslationW("popup.volume-restored.text"));
- CompleteVolumeRestore();
- }
+ WMLog& log = WMLog::GetInstance();
+ if (!restoreVolume_) {
+ log.LogInfo(L"Volume Restore has been disabled");
+ return;
+ }
+ const bool restore = !std::any_of(
+ muteConfig_.begin(), muteConfig_.end(),
+ [](const MuteConfig& conf) { return conf.shouldMute && conf.active; });
+ if (!restore) {
+ log.LogInfo(
+ L"Skipping restore since other mute event is currently active");
+ } else if (delayedMuteTimerId_ != 0) {
+ KillTimer(hMuteCtrlWnd_, delayedMuteTimerId_);
+ delayedMuteTimerId_ = 0;
+ log.LogInfo(
+ L"Skipping restore, since delayed mute was not triggered yet");
+ } else if (withDelay) {
+ log.LogInfo(L"Restoring previous mute state after Bluetooth delay");
+ ShowNotification(WMi18n::GetInstance().GetTranslationW(
+ "popup.volume-restored.title"),
+ WMi18n::GetInstance().GetTranslationW(
+ "popup.volume-restored.text"));
+ if (SetTimer(hMuteCtrlWnd_, BLUETOOTH_UNMUTE_TIMER_ID,
+ BLUETOOTH_RECONNECT_UNMUTE_DELAY,
+ BluetoothUnmuteTimerProc) == 0)
+ {
+ log.LogWinError(L"SetTimer (Bluetooth unmute delay)");
+ bluetoothUnmuteTimerId_ = 0;
+ CompleteVolumeRestore(); // fall back to immediate restore
+ } else {
+ bluetoothUnmuteTimerId_ = BLUETOOTH_UNMUTE_TIMER_ID;
+ }
+ } else {
+ log.LogInfo(L"Restoring previous mute state");
+ ShowNotification(WMi18n::GetInstance().GetTranslationW(
+ "popup.volume-restored.title"),
+ WMi18n::GetInstance().GetTranslationW(
+ "popup.volume-restored.text"));
+ CompleteVolumeRestore();
+ }
}
void MuteControl::CompleteVolumeRestore()
{
- bluetoothUnmuteTimerId_ = 0;
- winAudio_->RestoreMuteStatus();
- if (mediaConfig_.tryResume)
- {
- // Only resumes if a previous RequestPause actually paused the session.
- mediaController_.RequestResume();
- }
+ bluetoothUnmuteTimerId_ = 0;
+ winAudio_->RestoreMuteStatus();
+ if (mediaConfig_.tryResume) {
+ // Only resumes if a previous RequestPause actually paused the session.
+ mediaController_.RequestResume();
+ }
}
void MuteControl::SetRestoreVolume(bool enable)
{
- restoreVolume_ = enable;
+ restoreVolume_ = enable;
}
void MuteControl::SetMuteDelay(int delaySeconds)
{
- muteDelaySeconds_ = delaySeconds;
+ muteDelaySeconds_ = delaySeconds;
}
void MuteControl::SetMuteOnWorkstationLock(bool enable)
{
- muteConfig_[MuteTypeWorkstationLock].shouldMute = enable;
+ muteConfig_[MuteTypeWorkstationLock].shouldMute = enable;
}
void MuteControl::SetMuteOnLogout(bool enable)
{
- muteConfig_[MuteTypeLogout].shouldMute = enable;
+ muteConfig_[MuteTypeLogout].shouldMute = enable;
}
void MuteControl::SetMuteOnSuspend(bool enable)
{
- muteConfig_[MuteTypeSuspend].shouldMute = enable;
+ muteConfig_[MuteTypeSuspend].shouldMute = enable;
}
void MuteControl::SetMuteOnShutdown(bool enable)
{
- muteConfig_[MuteTypeShutdown].shouldMute = enable;
+ muteConfig_[MuteTypeShutdown].shouldMute = enable;
}
void MuteControl::SetMuteOnRemoteSession(bool enable)
{
- muteConfig_[MuteTypeRemoteSession].shouldMute = enable;
+ muteConfig_[MuteTypeRemoteSession].shouldMute = enable;
}
void MuteControl::SetMuteOnDisplayStandby(bool enable)
{
- muteConfig_[MuteTypeDisplayStandby].shouldMute = enable;
+ muteConfig_[MuteTypeDisplayStandby].shouldMute = enable;
}
+void MuteControl::SetMuteOnLidClose(bool enable)
+{
+ muteConfig_[MuteTypeLidClose].shouldMute = enable;
+}
void MuteControl::SetMuteOnBluetoothDisconnect(bool enable)
{
- muteConfig_[MuteTypeBluetoothDisconnect].shouldMute = enable;
+ muteConfig_[MuteTypeBluetoothDisconnect].shouldMute = enable;
}
void MuteControl::SetMuteTryPauseMedia(bool enable)
{
- mediaConfig_.tryPause = enable;
+ mediaConfig_.tryPause = enable;
}
void MuteControl::SetMuteTryResumeMedia(bool enable)
{
- mediaConfig_.tryResume = enable;
+ mediaConfig_.tryResume = enable;
}
bool MuteControl::GetRestoreVolume()
{
- return restoreVolume_;
+ return restoreVolume_;
}
bool MuteControl::GetMuteOnWorkstationLock() const
{
- return muteConfig_[MuteTypeWorkstationLock].shouldMute;
+ return muteConfig_[MuteTypeWorkstationLock].shouldMute;
}
bool MuteControl::GetMuteOnRemoteSession() const
{
- return muteConfig_[MuteTypeRemoteSession].shouldMute;
+ return muteConfig_[MuteTypeRemoteSession].shouldMute;
}
bool MuteControl::GetMuteOnDisplayStandby() const
{
- return muteConfig_[MuteTypeDisplayStandby].shouldMute;
+ return muteConfig_[MuteTypeDisplayStandby].shouldMute;
+}
+
+bool MuteControl::GetMuteOnLidClose() const
+{
+ return muteConfig_[MuteTypeLidClose].shouldMute;
}
bool MuteControl::GetMuteOnBluetoothDisconnect() const
{
- return muteConfig_[MuteTypeBluetoothDisconnect].shouldMute;
+ return muteConfig_[MuteTypeBluetoothDisconnect].shouldMute;
}
bool MuteControl::GetMuteOnLogout() const
{
- return muteConfig_[MuteTypeLogout].shouldMute;
+ return muteConfig_[MuteTypeLogout].shouldMute;
}
bool MuteControl::GetMuteOnSuspend() const
{
- return muteConfig_[MuteTypeSuspend].shouldMute;
+ return muteConfig_[MuteTypeSuspend].shouldMute;
}
bool MuteControl::GetMuteOnShutdown() const
{
- return muteConfig_[MuteTypeShutdown].shouldMute;
+ return muteConfig_[MuteTypeShutdown].shouldMute;
}
void MuteControl::MuteNow()
{
- WMLog::GetInstance().LogInfo(L"Muting workstation");
- ShowNotification(
- WMi18n::GetInstance().GetTranslationW("popup.muting-workstation.title"),
- WMi18n::GetInstance().GetTranslationW("popup.muting-workstation.text"));
- winAudio_->SetMute(true);
- if (mediaConfig_.tryPause)
- {
- mediaController_.RequestPause();
- }
-}
-
-void MuteControl::NotifyRestoreCondition(MuteType type, bool active, bool withDelay)
-{
- if (active)
- {
- SaveMuteStatus();
- muteConfig_[type].active = active;
- if (muteConfig_[type].shouldMute)
- {
- if (muteDelaySeconds_ == 0)
- {
- MuteNow();
- }
- else
- {
- WMLog::GetInstance().LogInfo(L"Starting delayed mute timer...");
- StartDelayedMute();
- }
- }
- }
- else
- {
- if (muteConfig_[type].active)
- {
- muteConfig_[type].active = false;
- if (muteConfig_[type].shouldMute)
- {
- RestoreVolume(withDelay);
- }
- }
- }
+ WMLog::GetInstance().LogInfo(L"Muting workstation");
+ ShowNotification(
+ WMi18n::GetInstance().GetTranslationW("popup.muting-workstation.title"),
+ WMi18n::GetInstance().GetTranslationW("popup.muting-workstation.text"));
+ winAudio_->SetMute(true);
+ if (mediaConfig_.tryPause) {
+ mediaController_.RequestPause();
+ }
+}
+
+void MuteControl::NotifyRestoreCondition(MuteType type, bool active,
+ bool withDelay)
+{
+ if (active) {
+ SaveMuteStatus();
+ muteConfig_[type].active = active;
+ if (muteConfig_[type].shouldMute) {
+ if (muteDelaySeconds_ == 0) {
+ MuteNow();
+ } else {
+ WMLog::GetInstance().LogInfo(L"Starting delayed mute timer...");
+ StartDelayedMute();
+ }
+ }
+ } else {
+ if (muteConfig_[type].active) {
+ muteConfig_[type].active = false;
+ if (muteConfig_[type].shouldMute) {
+ RestoreVolume(withDelay);
+ }
+ }
+ }
}
void MuteControl::NotifyWorkstationLock(bool active)
{
- WMLog::GetInstance().LogInfo(L"Mute Event: Workstation Lock {}",
- active ? L"start" : L"stop");
- NotifyRestoreCondition(MuteTypeWorkstationLock, active);
+ WMLog::GetInstance().LogInfo(L"Mute Event: Workstation Lock {}",
+ active ? L"start" : L"stop");
+ NotifyRestoreCondition(MuteTypeWorkstationLock, active);
}
void MuteControl::NotifyRemoteSession(bool active)
{
- WMLog::GetInstance().LogInfo(L"Mute Event: Remote Session {}",
- active ? L"start" : L"stop");
- NotifyRestoreCondition(MuteTypeRemoteSession, active);
+ WMLog::GetInstance().LogInfo(L"Mute Event: Remote Session {}",
+ active ? L"start" : L"stop");
+ NotifyRestoreCondition(MuteTypeRemoteSession, active);
}
void MuteControl::NotifyDisplayStandby(bool active)
{
- if (displayWasOffOnce_ || active)
- {
- WMLog::GetInstance().LogInfo(L"Mute Event: Display Standby {}",
- active ? L"start" : L"stop");
- NotifyRestoreCondition(MuteTypeDisplayStandby, active);
- displayWasOffOnce_ = true;
- }
+ if (displayWasOffOnce_ || active) {
+ WMLog::GetInstance().LogInfo(L"Mute Event: Display Standby {}",
+ active ? L"start" : L"stop");
+ NotifyRestoreCondition(MuteTypeDisplayStandby, active);
+ displayWasOffOnce_ = true;
+ }
+}
+
+void MuteControl::NotifyLidClosed(bool active)
+{
+ WMLog::GetInstance().LogInfo(L"Mute Event: Lid Close {}",
+ active ? L"start" : L"stop");
+ NotifyRestoreCondition(MuteTypeLidClose, active);
}
void MuteControl::NotifyBluetoothConnected(bool connected)
{
- WMLog::GetInstance().LogInfo(
- L"Mute Event: Bluetooth audio device {}",
- connected ? L"connected" : L"disconnected");
- NotifyRestoreCondition(MuteTypeBluetoothDisconnect, !connected, true);
+ WMLog::GetInstance().LogInfo(L"Mute Event: Bluetooth audio device {}",
+ connected ? L"connected" : L"disconnected");
+ NotifyRestoreCondition(MuteTypeBluetoothDisconnect, !connected, true);
}
void MuteControl::NotifyLogout()
{
- WMLog::GetInstance().LogInfo(L"Mute Event: Logout start");
- if (muteConfig_[MuteTypeLogout].shouldMute)
- {
- winAudio_->SetMute(true);
- }
+ WMLog::GetInstance().LogInfo(L"Mute Event: Logout start");
+ if (muteConfig_[MuteTypeLogout].shouldMute) {
+ winAudio_->SetMute(true);
+ }
}
-void MuteControl::NotifySuspend(bool /*active*/)
+void MuteControl::NotifySuspend(bool [[maybe_unused]] active)
{
- WMLog::GetInstance().LogInfo(L"Mute Event: Suspend start");
- if (muteConfig_[MuteTypeSuspend].shouldMute)
- {
- winAudio_->SetMute(true);
- }
+ WMLog::GetInstance().LogInfo(L"Mute Event: Suspend start");
+ if (muteConfig_[MuteTypeSuspend].shouldMute) {
+ winAudio_->SetMute(true);
+ }
}
void MuteControl::NotifyShutdown()
{
- WMLog::GetInstance().LogInfo(L"Mute Event: Shutdown start");
- if (muteConfig_[MuteTypeShutdown].shouldMute)
- {
- winAudio_->SetMute(true);
- }
+ WMLog::GetInstance().LogInfo(L"Mute Event: Shutdown start");
+ if (muteConfig_[MuteTypeShutdown].shouldMute) {
+ winAudio_->SetMute(true);
+ }
}
void MuteControl::NotifyQuietHours(bool active)
{
- if (active)
- {
- // SaveMuteStatus() must stay the only save here: calling
- // winAudio_->SaveMuteStatus() directly would overwrite the state
- // remembered by an already-active mute event (e.g. workstation lock)
- // with the currently muted state.
- SaveMuteStatus();
- WMLog::GetInstance().LogInfo(L"Mute Event: Quiet Hours started");
- winAudio_->SetMute(true);
- }
- else
- {
- WMLog::GetInstance().LogInfo(L"Mute Event: Quiet Hours ended");
- RestoreVolume();
- }
+ if (active) {
+ // SaveMuteStatus() must stay the only save here: calling
+ // winAudio_->SaveMuteStatus() directly would overwrite the state
+ // remembered by an already-active mute event (e.g. workstation lock)
+ // with the currently muted state.
+ SaveMuteStatus();
+ WMLog::GetInstance().LogInfo(L"Mute Event: Quiet Hours started");
+ winAudio_->SetMute(true);
+ } else {
+ WMLog::GetInstance().LogInfo(L"Mute Event: Quiet Hours ended");
+ RestoreVolume();
+ }
}
void MuteControl::SetManagedEndpoints(
- const std::vector<std::wstring> &endpoints,
- bool isAllowList)
+ const std::vector<std::wstring>& endpoints, bool isAllowList)
{
- winAudio_->MuteSpecificEndpoints(true);
- winAudio_->SetManagedEndpoints(endpoints, isAllowList);
+ winAudio_->MuteSpecificEndpoints(true);
+ winAudio_->SetManagedEndpoints(endpoints, isAllowList);
}
void MuteControl::ClearManagedEndpoints()
{
- winAudio_->MuteSpecificEndpoints(false);
+ winAudio_->MuteSpecificEndpoints(false);
}
-----------------------------------------------------------------------------
*/
-#include "common.h"
#include "WinMute.h"
+
#include "WinAudio.h"
+#include "common.h"
extern HINSTANCE hglobInstance;
extern INT_PTR CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM);
extern INT_PTR CALLBACK SettingsDlgProc(HWND, UINT, WPARAM, LPARAM);
-static const wchar_t *WINMUTE_CLASS_NAME = L"WinMute";
+static const wchar_t* WINMUTE_CLASS_NAME = L"WinMute";
-static const wchar_t *TERMINAL_SERVER_KEY
- = L"SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\";
-static const wchar_t* GLASS_SESSION_ID
- = L"GlassSessionId";
+static const wchar_t* TERMINAL_SERVER_KEY =
+ L"SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\";
+static const wchar_t* GLASS_SESSION_ID = L"GlassSessionId";
-static LRESULT CALLBACK WinMuteWndProc(
- HWND hWnd,
- UINT msg,
- WPARAM wParam,
- LPARAM lParam)
+static LRESULT CALLBACK WinMuteWndProc(HWND hWnd, UINT msg, WPARAM wParam,
+ LPARAM lParam)
{
- auto wm = reinterpret_cast<WinMute*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
- switch (msg) {
- case WM_NCCREATE: {
- LPCREATESTRUCTW cs = reinterpret_cast<LPCREATESTRUCTW>(lParam);
- SetWindowLongPtrW(hWnd, GWLP_USERDATA,
- reinterpret_cast<LONG_PTR>(cs->lpCreateParams));
- return TRUE;
- }
- default:
- break;
- }
- return (wm)
- ? wm->WindowProc(hWnd, msg, wParam, lParam)
- : DefWindowProcW(hWnd, msg, wParam, lParam);
+ auto wm = reinterpret_cast<WinMute*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
+ switch (msg) {
+ case WM_NCCREATE: {
+ LPCREATESTRUCTW cs = reinterpret_cast<LPCREATESTRUCTW>(lParam);
+ SetWindowLongPtrW(hWnd, GWLP_USERDATA,
+ reinterpret_cast<LONG_PTR>(cs->lpCreateParams));
+ return TRUE;
+ }
+ default:
+ break;
+ }
+ return (wm) ? wm->WindowProc(hWnd, msg, wParam, lParam)
+ : DefWindowProcW(hWnd, msg, wParam, lParam);
}
static bool IsCurrentSessionRemoteable() noexcept
{
- bool isRemoteable = false;
-
- if (GetSystemMetrics(SM_REMOTESESSION)) {
- isRemoteable = true;
- } else {
- HKEY hRegKey = nullptr;
- LONG lResult;
-
- lResult = RegOpenKeyExW(HKEY_LOCAL_MACHINE, TERMINAL_SERVER_KEY, 0, KEY_READ, &hRegKey);
- if (lResult == ERROR_SUCCESS) {
- DWORD dwGlassSessionId = 0;
- DWORD cbGlassSessionId = sizeof(dwGlassSessionId);
- DWORD dwType;
-
- lResult = RegQueryValueExW(hRegKey, GLASS_SESSION_ID, nullptr, &dwType,
- reinterpret_cast<BYTE*>(&dwGlassSessionId),
- &cbGlassSessionId);
-
- if (lResult == ERROR_SUCCESS
- && dwType == REG_DWORD
- && cbGlassSessionId == sizeof(dwGlassSessionId)) {
- DWORD dwCurrentSessionId;
- if (ProcessIdToSessionId(GetCurrentProcessId(), &dwCurrentSessionId)) {
- isRemoteable = (dwCurrentSessionId != dwGlassSessionId);
+ bool isRemoteable = false;
+
+ if (GetSystemMetrics(SM_REMOTESESSION)) {
+ isRemoteable = true;
+ } else {
+ HKEY hRegKey = nullptr;
+ LONG lResult;
+
+ lResult = RegOpenKeyExW(HKEY_LOCAL_MACHINE, TERMINAL_SERVER_KEY, 0,
+ KEY_READ, &hRegKey);
+ if (lResult == ERROR_SUCCESS) {
+ DWORD dwGlassSessionId = 0;
+ DWORD cbGlassSessionId = sizeof(dwGlassSessionId);
+ DWORD dwType;
+
+ lResult = RegQueryValueExW(
+ hRegKey, GLASS_SESSION_ID, nullptr, &dwType,
+ reinterpret_cast<BYTE*>(&dwGlassSessionId), &cbGlassSessionId);
+
+ if (lResult == ERROR_SUCCESS && dwType == REG_DWORD &&
+ cbGlassSessionId == sizeof(dwGlassSessionId))
+ {
+ DWORD dwCurrentSessionId;
+ if (ProcessIdToSessionId(GetCurrentProcessId(),
+ &dwCurrentSessionId))
+ {
+ isRemoteable = (dwCurrentSessionId != dwGlassSessionId);
+ }
}
- }
- RegCloseKey(hRegKey);
- }
- }
+ RegCloseKey(hRegKey);
+ }
+ }
- return isRemoteable;
+ return isRemoteable;
}
-
WinMute::MuteConfig::MuteConfig()
- : muteOnWlan(false), muteOnBluetooth(false), showNotifications(false)
+ : muteOnWlan(false), muteOnBluetooth(false), showNotifications(false)
{
}
-WinMute::WinMute(WMSettings& settings) :
- hWnd_(nullptr),
- hTrayMenu_(nullptr),
- hAppIcon_(nullptr),
- hTrayIcon_(nullptr),
- hUpdateIcon_(nullptr),
- settings_(settings),
- i18n_(WMi18n::GetInstance())
+WinMute::WinMute(WMSettings& settings)
+ : hWnd_(nullptr),
+ hTrayMenu_(nullptr),
+ hAppIcon_(nullptr),
+ hTrayIcon_(nullptr),
+ hUpdateIcon_(nullptr),
+ settings_(settings),
+ i18n_(WMi18n::GetInstance())
{
}
WinMute::~WinMute() noexcept
{
- Unload();
+ Unload();
}
bool WinMute::RegisterWindowClass()
{
- WNDCLASSEX wc = { 0 };
- wc.cbSize = sizeof(wc);
- wc.hIcon = LoadIcon(hglobInstance, MAKEINTRESOURCE(IDI_APP));
- // System color index brush: no GDI object to leak, tracks theme changes
- wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BTNFACE + 1);
- wc.hInstance = hglobInstance;
- wc.lpfnWndProc = WinMuteWndProc;
- wc.lpszClassName = WINMUTE_CLASS_NAME;
-
- if (!RegisterClassExW(&wc)) {
- ShowWindowsError(L"RegisterClass");
- return false;
- }
- return true;
+ WNDCLASSEX wc = {0};
+ wc.cbSize = sizeof(wc);
+ wc.hIcon = LoadIcon(hglobInstance, MAKEINTRESOURCE(IDI_APP));
+ // System color index brush: no GDI object to leak, tracks theme changes
+ wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BTNFACE + 1);
+ wc.hInstance = hglobInstance;
+ wc.lpfnWndProc = WinMuteWndProc;
+ wc.lpszClassName = WINMUTE_CLASS_NAME;
+
+ if (!RegisterClassExW(&wc)) {
+ ShowWindowsError(L"RegisterClass");
+ return false;
+ }
+ return true;
}
bool WinMute::InitWindow()
{
- hWnd_ = CreateWindowExW(WS_EX_TOOLWINDOW, WINMUTE_CLASS_NAME, PROGRAM_NAME,
- WS_POPUP, 0, 0, 0, 0, nullptr, 0, hglobInstance, this);
- if (hWnd_ == nullptr) {
- ShowWindowsError(L"CreateWindowEx");
- return false;
- }
- return true;
+ hWnd_ =
+ CreateWindowExW(WS_EX_TOOLWINDOW, WINMUTE_CLASS_NAME, PROGRAM_NAME,
+ WS_POPUP, 0, 0, 0, 0, nullptr, 0, hglobInstance, this);
+ if (hWnd_ == nullptr) {
+ ShowWindowsError(L"CreateWindowEx");
+ return false;
+ }
+ return true;
}
bool WinMute::InitAudio()
{
- if (IsWindowsVistaOrGreater()) {
- // Nothing to do
- } else if (IsWindowsXPOrGreater()) {
- TaskDialog(
- nullptr, nullptr,
- PROGRAM_NAME,
- i18n_.GetTranslationW("init.error.winmute.platform-support.title").c_str(),
- i18n_.GetTranslationW("init.error.winmute.platform-support.text").c_str(),
- TDCBF_OK_BUTTON, TD_ERROR_ICON, nullptr);
- return false;
- }
-
- if (!muteCtrl_.Init(hWnd_, &wmTray_)) {
- return false;
- }
-
- return true;
+ if (IsWindowsVistaOrGreater()) {
+ // Nothing to do
+ } else if (IsWindowsXPOrGreater()) {
+ TaskDialog(
+ nullptr, nullptr, PROGRAM_NAME,
+ i18n_.GetTranslationW("init.error.winmute.platform-support.title")
+ .c_str(),
+ i18n_.GetTranslationW("init.error.winmute.platform-support.text")
+ .c_str(),
+ TDCBF_OK_BUTTON, TD_ERROR_ICON, nullptr);
+ return false;
+ }
+
+ if (!muteCtrl_.Init(hWnd_, &wmTray_)) {
+ return false;
+ }
+
+ return true;
}
-#define CHECK_MENU_ITEM(id, cond) \
- (CheckMenuItem(hTrayMenu_, ID_TRAYMENU_ ## id, \
- (cond) ? MF_CHECKED : MF_UNCHECKED) != -1)
+#define CHECK_MENU_ITEM(id, cond) \
+ (CheckMenuItem(hTrayMenu_, ID_TRAYMENU_##id, \
+ (cond) ? MF_CHECKED : MF_UNCHECKED) != -1)
bool WinMute::InitTrayMenu()
{
- if (hTrayMenu_ == nullptr) {
- hTrayMenu_ = LoadMenuW(hglobInstance, MAKEINTRESOURCE(IDR_TRAYMENU));
- if (hTrayMenu_ == nullptr) {
- ShowWindowsError(L"LoadMenu");
- return false;
- }
- }
-
- if (!CHECK_MENU_ITEM(MUTEONLOCK, muteCtrl_.GetMuteOnWorkstationLock()) ||
- !CHECK_MENU_ITEM(RESTOREAUDIO, muteCtrl_.GetRestoreVolume()) ||
- !CHECK_MENU_ITEM(MUTEONSUSPEND, muteCtrl_.GetMuteOnSuspend()) ||
- !CHECK_MENU_ITEM(MUTEONSHUTDOWN, muteCtrl_.GetMuteOnShutdown()) ||
- !CHECK_MENU_ITEM(MUTEONLOGOUT, muteCtrl_.GetMuteOnLogout())) {
- return false;
- }
-
- LoadMainMenuText();
- return true;
+ if (hTrayMenu_ == nullptr) {
+ hTrayMenu_ = LoadMenuW(hglobInstance, MAKEINTRESOURCE(IDR_TRAYMENU));
+ if (hTrayMenu_ == nullptr) {
+ ShowWindowsError(L"LoadMenu");
+ return false;
+ }
+ }
+
+ if (!CHECK_MENU_ITEM(MUTEONLOCK, muteCtrl_.GetMuteOnWorkstationLock()) ||
+ !CHECK_MENU_ITEM(RESTOREAUDIO, muteCtrl_.GetRestoreVolume()) ||
+ !CHECK_MENU_ITEM(MUTEONSUSPEND, muteCtrl_.GetMuteOnSuspend()) ||
+ !CHECK_MENU_ITEM(MUTEONSHUTDOWN, muteCtrl_.GetMuteOnShutdown()) ||
+ !CHECK_MENU_ITEM(MUTEONLOGOUT, muteCtrl_.GetMuteOnLogout()))
+ {
+ return false;
+ }
+
+ LoadMainMenuText();
+ return true;
}
#undef CHECK_MENU_ITEM
bool WinMute::Init()
{
- WMLog& log = WMLog::GetInstance();
+ WMLog& log = WMLog::GetInstance();
- hAppIcon_ = LoadIconW(hglobInstance, MAKEINTRESOURCE(IDI_APP));
+ hAppIcon_ = LoadIconW(hglobInstance, MAKEINTRESOURCE(IDI_APP));
#ifdef _DEBUG
- WMLog::GetInstance().EnableLogFile(true);
+ WMLog::GetInstance().EnableLogFile(true);
#else
- WMLog::GetInstance().EnableLogFile(settings_.QueryValue(SettingsKey::LOGGING_ENABLED));
+ WMLog::GetInstance().EnableLogFile(
+ settings_.QueryValue(SettingsKey::LOGGING_ENABLED));
#endif
- log.LogInfo(L"Starting new session...");
-
- if (!RegisterWindowClass() || !InitWindow()) {
- return false;
- }
-
- if (!InitAudio()) {
- return false;
- }
-
- if (!LoadSettings()) {
- return false;
- }
-
- if (!InitTrayMenu()) {
- return false;
- }
-
- if (!WTSRegisterSessionNotification(hWnd_, NOTIFY_FOR_THIS_SESSION)) {
- const DWORD lastError = GetLastError();
- ShowWindowsError(L"WTSRegisterSessionNotification");
- log.LogWinError(L"WTSRegisterSessionNotification", lastError);
- return false;
- }
- wtsSessionNotificationRegistered_ = true;
-
- hPowerNotify_ = RegisterPowerSettingNotification(
- hWnd_, &GUID_CONSOLE_DISPLAY_STATE, DEVICE_NOTIFY_WINDOW_HANDLE);
- if (hPowerNotify_ == nullptr) {
- const DWORD lastError = GetLastError();
- ShowWindowsError(L"RegisterPowerSettingNotification", lastError);
- log.LogWinError(L"RegisterPowerSettingNotification", lastError);
- return false;
- }
-
- hTrayIcon_ = LoadIconW(hglobInstance, MAKEINTRESOURCE(IDI_APP));
- if (hTrayIcon_ == nullptr) {
- ShowWindowsError(L"LoadIcon");
- return false;
- }
- hUpdateIcon_ = LoadIconW(hglobInstance, MAKEINTRESOURCE(IDI_APPUPDATE));
- if (hUpdateIcon_ == nullptr) {
- ShowWindowsError(L"LoadIcon");
- return false;
- }
- wmTray_.Init(hWnd_, 0, hTrayIcon_, L"WinMute", true);
- updateTray_.Init(hWnd_, 1, hUpdateIcon_, L"WinMute Update", false, WM_WINMUTE_UPDATE_POPUP);
-
- quietHours_.Init(hWnd_, settings_);
-
- log.LogInfo(L"WinMute initialized");
-
- if (settings_.QueryValue(SettingsKey::MUTE_ON_RDP)
- && IsCurrentSessionRemoteable()) {
- wmTray_.ShowPopup(
- i18n_.GetTranslationW("popup.remote-session-detected.title"),
- i18n_.GetTranslationW("popup.remote-session-detected.text"));
- muteCtrl_.SetMute(true);
- }
-
- CheckForUpdates();
-
- return true;
+ log.LogInfo(L"Starting new session...");
+
+ if (!RegisterWindowClass() || !InitWindow()) {
+ return false;
+ }
+
+ if (!InitAudio()) {
+ return false;
+ }
+
+ if (!LoadSettings()) {
+ return false;
+ }
+
+ if (!InitTrayMenu()) {
+ return false;
+ }
+
+ if (!WTSRegisterSessionNotification(hWnd_, NOTIFY_FOR_THIS_SESSION)) {
+ const DWORD lastError = GetLastError();
+ ShowWindowsError(L"WTSRegisterSessionNotification");
+ log.LogWinError(L"WTSRegisterSessionNotification", lastError);
+ return false;
+ }
+ wtsSessionNotificationRegistered_ = true;
+
+ hPowerNotify_ = RegisterPowerSettingNotification(
+ hWnd_, &GUID_CONSOLE_DISPLAY_STATE, DEVICE_NOTIFY_WINDOW_HANDLE);
+ if (hPowerNotify_ == nullptr) {
+ const DWORD lastError = GetLastError();
+ ShowWindowsError(L"RegisterPowerSettingNotification", lastError);
+ log.LogWinError(L"RegisterPowerSettingNotification", lastError);
+ return false;
+ }
+
+ hLidCloseNotify_ = RegisterPowerSettingNotification(
+ hWnd_, &GUID_LIDSWITCH_STATE_CHANGE, DEVICE_NOTIFY_WINDOW_HANDLE);
+ if (hLidCloseNotify_ == nullptr) {
+ const DWORD lastError = GetLastError();
+ ShowWindowsError(L"RegisterPowerSettingNotification", lastError);
+ log.LogWinError(L"RegisterPowerSettingNotification", lastError);
+ UnregisterPowerSettingNotification(hPowerNotify_);
+ return false;
+ }
+
+ hTrayIcon_ = LoadIconW(hglobInstance, MAKEINTRESOURCE(IDI_APP));
+ if (hTrayIcon_ == nullptr) {
+ ShowWindowsError(L"LoadIcon");
+ return false;
+ }
+ hUpdateIcon_ = LoadIconW(hglobInstance, MAKEINTRESOURCE(IDI_APPUPDATE));
+ if (hUpdateIcon_ == nullptr) {
+ ShowWindowsError(L"LoadIcon");
+ return false;
+ }
+ wmTray_.Init(hWnd_, 0, hTrayIcon_, L"WinMute", true);
+ updateTray_.Init(hWnd_, 1, hUpdateIcon_, L"WinMute Update", false,
+ WM_WINMUTE_UPDATE_POPUP);
+
+ quietHours_.Init(hWnd_, settings_);
+
+ log.LogInfo(L"WinMute initialized");
+
+ if (settings_.QueryValue(SettingsKey::MUTE_ON_RDP) &&
+ IsCurrentSessionRemoteable())
+ {
+ wmTray_.ShowPopup(
+ i18n_.GetTranslationW("popup.remote-session-detected.title"),
+ i18n_.GetTranslationW("popup.remote-session-detected.text"));
+ muteCtrl_.SetMute(true);
+ }
+
+ CheckForUpdates();
+
+ return true;
}
bool WinMute::LoadSettings()
{
- WMLog& log = WMLog::GetInstance();
-
- if (log.IsLogFileEnabled()) {
- std::wstring versionNumber;
- GetWinMuteVersion(versionNumber);
- log.LogInfo(L"Starting WinMute {}", versionNumber);
- log.LogInfo(L"Loading settings:");
- log.LogInfo(L"Check for updates: {}", settings_.QueryValue(SettingsKey::CHECK_FOR_UPDATE) ? L"Yes" : L"No");
- log.LogInfo(L"\tCheck for beta updates: {}", settings_.QueryValue(SettingsKey::CHECK_FOR_BETA_UPDATE) ? L"Yes" : L"No");
- log.LogInfo(L"\tRestore volume: {}", settings_.QueryValue(SettingsKey::RESTORE_AUDIO) ? L"Yes" : L"No");
- log.LogInfo(L"\tMute delay: {}", settings_.QueryValue(SettingsKey::MUTE_DELAY));
- log.LogInfo(L"\tMute on lock: {}", settings_.QueryValue(SettingsKey::MUTE_ON_LOCK) ? L"Yes" : L"No");
- log.LogInfo(L"\tMute on display standby: {}", settings_.QueryValue(SettingsKey::MUTE_ON_DISPLAYSTANDBY) ? L"Yes" : L"No");
- log.LogInfo(L"\tMute on logout: {}", settings_.QueryValue(SettingsKey::MUTE_ON_LOGOUT) ? L"Yes" : L"No");
- log.LogInfo(L"\tMute on suspend: {}", settings_.QueryValue(SettingsKey::MUTE_ON_SUSPEND) ? L"Yes" : L"No");
- log.LogInfo(L"\tMute on shutdown: {}", settings_.QueryValue(SettingsKey::MUTE_ON_SHUTDOWN) ? L"Yes" : L"No");
- log.LogInfo(L"\tShow notifications: {}", settings_.QueryValue(SettingsKey::NOTIFICATIONS_ENABLED) ? L"Yes" : L"No");
- log.LogInfo(L"\tMute on bluetooth: {}", settings_.QueryValue(SettingsKey::MUTE_ON_BLUETOOTH) ? L"Yes" : L"No");
- log.LogInfo(L"\tTry pausing media when muting: {}", settings_.QueryValue(SettingsKey::MUTE_TRY_PAUSE_MEDIA) ? L"Yes" : L"No");
- log.LogInfo(L"\tTry resuming media when unmuting: {}", settings_.QueryValue(SettingsKey::MUTE_TRY_RESUME_MEDIA) ? L"Yes" : L"No");
- log.LogInfo(L"\t\tUse devicelist: {}", settings_.QueryValue(SettingsKey::MUTE_ON_BLUETOOTH_DEVICELIST) ? L"Yes" : L"No");
- log.LogInfo(L"\tMute on WLAN: {}", settings_.QueryValue(SettingsKey::MUTE_ON_WLAN) ? L"Yes" : L"No");
- log.LogInfo(L"\t\tUse allowlist: {}", settings_.QueryValue(SettingsKey::MUTE_ON_WLAN_ALLOWLIST) ? L"Yes" : L"No");
- log.LogInfo(L"\tMute specific endpoints only: {}", settings_.QueryValue(SettingsKey::MUTE_INDIVIDUAL_ENDPOINTS) ? L"Yes" : L"No");
- }
-
- if (!settings_.QueryValue(SettingsKey::MUTE_INDIVIDUAL_ENDPOINTS)) {
- muteCtrl_.ClearManagedEndpoints();
- } else {
- const auto endpoints = settings_.GetManagedAudioEndpoints();
- const bool isAllowList = settings_.QueryValue(SettingsKey::MUTE_INDIVIDUAL_ENDPOINTS_MODE) == MUTE_ENDPOINT_MODE_INDIVIDUAL_ALLOW_LIST;
- muteCtrl_.SetManagedEndpoints(endpoints, isAllowList);
- }
- muteCtrl_.SetMuteDelay(settings_.QueryValue(SettingsKey::MUTE_DELAY));
- muteCtrl_.SetRestoreVolume(settings_.QueryValue(SettingsKey::RESTORE_AUDIO));
- muteCtrl_.SetMuteOnWorkstationLock(settings_.QueryValue(SettingsKey::MUTE_ON_LOCK));
- muteCtrl_.SetMuteOnDisplayStandby(settings_.QueryValue(SettingsKey::MUTE_ON_DISPLAYSTANDBY));
- muteCtrl_.SetMuteOnLogout(settings_.QueryValue(SettingsKey::MUTE_ON_LOGOUT));
- muteCtrl_.SetMuteOnSuspend(settings_.QueryValue(SettingsKey::MUTE_ON_SUSPEND));
- muteCtrl_.SetMuteOnShutdown(settings_.QueryValue(SettingsKey::MUTE_ON_SHUTDOWN));
- muteCtrl_.SetMuteTryPauseMedia(settings_.QueryValue(SettingsKey::MUTE_TRY_PAUSE_MEDIA));
- muteCtrl_.SetMuteTryResumeMedia(settings_.QueryValue(SettingsKey::MUTE_TRY_RESUME_MEDIA));
-
- muteCtrl_.SetNotifications(settings_.QueryValue(SettingsKey::NOTIFICATIONS_ENABLED));
-
- muteConfig_.showNotifications = settings_.QueryValue(SettingsKey::NOTIFICATIONS_ENABLED);
-
- muteConfig_.muteOnBluetooth = settings_.QueryValue(SettingsKey::MUTE_ON_BLUETOOTH);
- if (!muteConfig_.muteOnBluetooth) {
- muteCtrl_.SetMuteOnBluetoothDisconnect(false);
- btDetector_.Unload();
- } else {
- if (!btDetector_.Init(hWnd_)) {
- wmTray_.ShowPopup(
- i18n_.GetTranslationW("popup.bluetooth-muting-disabled.title"),
- i18n_.GetTranslationW("popup.bluetooth-muting-disabled.text"));
- settings_.SetValue(SettingsKey::MUTE_ON_BLUETOOTH, FALSE);
- } else {
- muteCtrl_.SetMuteOnBluetoothDisconnect(true);
- const bool muteOnWithDeviceList = settings_.QueryValue(SettingsKey::MUTE_ON_BLUETOOTH_DEVICELIST);
- btDetector_.SetDeviceList(settings_.GetBluetoothDevices(), muteOnWithDeviceList);
- }
- }
-
- muteConfig_.muteOnWlan = settings_.QueryValue(SettingsKey::MUTE_ON_WLAN);
- if (!muteConfig_.muteOnWlan) {
- wifiDetector_.Unload();
- } else {
- if (!wifiDetector_.Init(hWnd_)) {
- wmTray_.ShowPopup(
- i18n_.GetTranslationW("popup.wlan-muting-disabled.title"),
- i18n_.GetTranslationW("popup.wlan-muting-disabled.text"));
- settings_.SetValue(SettingsKey::MUTE_ON_WLAN, FALSE);
- } else {
- const bool isMuteList = !settings_.QueryValue(SettingsKey::MUTE_ON_WLAN_ALLOWLIST);
- wifiDetector_.SetNetworkList(settings_.GetWifiNetworks(), isMuteList);
- wifiDetector_.CheckNetwork();
- }
- }
-
- //GUID_MONITOR_POWER_ON
- return true;
+ WMLog& log = WMLog::GetInstance();
+
+ if (log.IsLogFileEnabled()) {
+ std::wstring versionNumber;
+ GetWinMuteVersion(versionNumber);
+ log.LogInfo(L"Starting WinMute {}", versionNumber);
+ log.LogInfo(L"Loading settings:");
+ log.LogInfo(L"Check for updates: {}",
+ settings_.QueryValue(SettingsKey::CHECK_FOR_UPDATE)
+ ? L"Yes"
+ : L"No");
+ log.LogInfo(L"\tCheck for beta updates: {}",
+ settings_.QueryValue(SettingsKey::CHECK_FOR_BETA_UPDATE)
+ ? L"Yes"
+ : L"No");
+ log.LogInfo(
+ L"\tRestore volume: {}",
+ settings_.QueryValue(SettingsKey::RESTORE_AUDIO) ? L"Yes" : L"No");
+ log.LogInfo(L"\tMute delay: {}",
+ settings_.QueryValue(SettingsKey::MUTE_DELAY));
+ log.LogInfo(
+ L"\tMute on lock: {}",
+ settings_.QueryValue(SettingsKey::MUTE_ON_LOCK) ? L"Yes" : L"No");
+ log.LogInfo(L"\tMute on display standby: {}",
+ settings_.QueryValue(SettingsKey::MUTE_ON_DISPLAYSTANDBY)
+ ? L"Yes"
+ : L"No");
+ log.LogInfo(
+ L"\tMute on logout: {}",
+ settings_.QueryValue(SettingsKey::MUTE_ON_LOGOUT) ? L"Yes" : L"No");
+ log.LogInfo(L"\tMute on suspend: {}",
+ settings_.QueryValue(SettingsKey::MUTE_ON_SUSPEND) ? L"Yes"
+ : L"No");
+ log.LogInfo(L"\tMute on shutdown: {}",
+ settings_.QueryValue(SettingsKey::MUTE_ON_SHUTDOWN)
+ ? L"Yes"
+ : L"No");
+ log.LogInfo(L"\tShow notifications: {}",
+ settings_.QueryValue(SettingsKey::NOTIFICATIONS_ENABLED)
+ ? L"Yes"
+ : L"No");
+ log.LogInfo(L"\tMute on bluetooth: {}",
+ settings_.QueryValue(SettingsKey::MUTE_ON_BLUETOOTH)
+ ? L"Yes"
+ : L"No");
+ log.LogInfo(L"\tTry pausing media when muting: {}",
+ settings_.QueryValue(SettingsKey::MUTE_TRY_PAUSE_MEDIA)
+ ? L"Yes"
+ : L"No");
+ log.LogInfo(L"\tTry resuming media when unmuting: {}",
+ settings_.QueryValue(SettingsKey::MUTE_TRY_RESUME_MEDIA)
+ ? L"Yes"
+ : L"No");
+ log.LogInfo(
+ L"\t\tUse devicelist: {}",
+ settings_.QueryValue(SettingsKey::MUTE_ON_BLUETOOTH_DEVICELIST)
+ ? L"Yes"
+ : L"No");
+ log.LogInfo(
+ L"\tMute on WLAN: {}",
+ settings_.QueryValue(SettingsKey::MUTE_ON_WLAN) ? L"Yes" : L"No");
+ log.LogInfo(L"\t\tUse allowlist: {}",
+ settings_.QueryValue(SettingsKey::MUTE_ON_WLAN_ALLOWLIST)
+ ? L"Yes"
+ : L"No");
+ log.LogInfo(L"\tMute specific endpoints only: {}",
+ settings_.QueryValue(SettingsKey::MUTE_INDIVIDUAL_ENDPOINTS)
+ ? L"Yes"
+ : L"No");
+ }
+
+ if (!settings_.QueryValue(SettingsKey::MUTE_INDIVIDUAL_ENDPOINTS)) {
+ muteCtrl_.ClearManagedEndpoints();
+ } else {
+ const auto endpoints = settings_.GetManagedAudioEndpoints();
+ const bool isAllowList =
+ settings_.QueryValue(SettingsKey::MUTE_INDIVIDUAL_ENDPOINTS_MODE) ==
+ MUTE_ENDPOINT_MODE_INDIVIDUAL_ALLOW_LIST;
+ muteCtrl_.SetManagedEndpoints(endpoints, isAllowList);
+ }
+ muteCtrl_.SetMuteDelay(settings_.QueryValue(SettingsKey::MUTE_DELAY));
+ muteCtrl_.SetRestoreVolume(
+ settings_.QueryValue(SettingsKey::RESTORE_AUDIO));
+ muteCtrl_.SetMuteOnWorkstationLock(
+ settings_.QueryValue(SettingsKey::MUTE_ON_LOCK));
+ muteCtrl_.SetMuteOnDisplayStandby(
+ settings_.QueryValue(SettingsKey::MUTE_ON_DISPLAYSTANDBY));
+ muteCtrl_.SetMuteOnLogout(
+ settings_.QueryValue(SettingsKey::MUTE_ON_LOGOUT));
+ muteCtrl_.SetMuteOnSuspend(
+ settings_.QueryValue(SettingsKey::MUTE_ON_SUSPEND));
+ muteCtrl_.SetMuteOnShutdown(
+ settings_.QueryValue(SettingsKey::MUTE_ON_SHUTDOWN));
+ muteCtrl_.SetMuteTryPauseMedia(
+ settings_.QueryValue(SettingsKey::MUTE_TRY_PAUSE_MEDIA));
+ muteCtrl_.SetMuteTryResumeMedia(
+ settings_.QueryValue(SettingsKey::MUTE_TRY_RESUME_MEDIA));
+
+ muteCtrl_.SetNotifications(
+ settings_.QueryValue(SettingsKey::NOTIFICATIONS_ENABLED));
+
+ muteConfig_.showNotifications =
+ settings_.QueryValue(SettingsKey::NOTIFICATIONS_ENABLED);
+
+ muteConfig_.muteOnBluetooth =
+ settings_.QueryValue(SettingsKey::MUTE_ON_BLUETOOTH);
+ if (!muteConfig_.muteOnBluetooth) {
+ muteCtrl_.SetMuteOnBluetoothDisconnect(false);
+ btDetector_.Unload();
+ } else {
+ if (!btDetector_.Init(hWnd_)) {
+ wmTray_.ShowPopup(
+ i18n_.GetTranslationW("popup.bluetooth-muting-disabled.title"),
+ i18n_.GetTranslationW("popup.bluetooth-muting-disabled.text"));
+ settings_.SetValue(SettingsKey::MUTE_ON_BLUETOOTH, FALSE);
+ } else {
+ muteCtrl_.SetMuteOnBluetoothDisconnect(true);
+ const bool muteOnWithDeviceList =
+ settings_.QueryValue(SettingsKey::MUTE_ON_BLUETOOTH_DEVICELIST);
+ btDetector_.SetDeviceList(settings_.GetBluetoothDevices(),
+ muteOnWithDeviceList);
+ }
+ }
+
+ muteConfig_.muteOnWlan = settings_.QueryValue(SettingsKey::MUTE_ON_WLAN);
+ if (!muteConfig_.muteOnWlan) {
+ wifiDetector_.Unload();
+ } else {
+ if (!wifiDetector_.Init(hWnd_)) {
+ wmTray_.ShowPopup(
+ i18n_.GetTranslationW("popup.wlan-muting-disabled.title"),
+ i18n_.GetTranslationW("popup.wlan-muting-disabled.text"));
+ settings_.SetValue(SettingsKey::MUTE_ON_WLAN, FALSE);
+ } else {
+ const bool isMuteList =
+ !settings_.QueryValue(SettingsKey::MUTE_ON_WLAN_ALLOWLIST);
+ wifiDetector_.SetNetworkList(settings_.GetWifiNetworks(),
+ isMuteList);
+ wifiDetector_.CheckNetwork();
+ }
+ }
+
+ // GUID_MONITOR_POWER_ON
+ return true;
}
void WinMute::ToggleMenuCheck(UINT item, bool* setting) noexcept
{
- UINT state = GetMenuState(hTrayMenu_, item, MF_BYCOMMAND);
- if (state & MF_CHECKED) {
- *setting = false;
- CheckMenuItem(hTrayMenu_, item, MF_UNCHECKED);
- } else {
- *setting = true;
- CheckMenuItem(hTrayMenu_, item, MF_CHECKED);
- }
+ UINT state = GetMenuState(hTrayMenu_, item, MF_BYCOMMAND);
+ if (state & MF_CHECKED) {
+ *setting = false;
+ CheckMenuItem(hTrayMenu_, item, MF_UNCHECKED);
+ } else {
+ *setting = true;
+ CheckMenuItem(hTrayMenu_, item, MF_CHECKED);
+ }
}
void WinMute::LoadMainMenuText()
{
- std::map<UINT, std::wstring> menuText;
- menuText[ID_TRAYMENU_INFO] = i18n_.GetTranslationW("traymenu.info");
- menuText[ID_TRAYMENU_LABEL_MUTEWHEN] = i18n_.GetTranslationW("traymenu.mute-when");
- menuText[ID_TRAYMENU_MUTEONLOCK] = i18n_.GetTranslationW("traymenu.mute-on-lock");
- menuText[ID_TRAYMENU_MUTEONSCREENSUSPEND] = i18n_.GetTranslationW("traymenu.mute-on-screen-suspend");
- menuText[ID_TRAYMENU_RESTOREAUDIO] = i18n_.GetTranslationW("traymenu.restore-volume");
- menuText[ID_TRAYMENU_LABEL_MUTEON_NO_RESTORE] = i18n_.GetTranslationW("traymenu.mute-no-restore");
- menuText[ID_TRAYMENU_MUTEONSHUTDOWN] = i18n_.GetTranslationW("traymenu.mute-on-shutdown");
- menuText[ID_TRAYMENU_MUTEONSUSPEND] = i18n_.GetTranslationW("traymenu.mute-on-sleep");
- menuText[ID_TRAYMENU_MUTEONLOGOUT] = i18n_.GetTranslationW("traymenu.mute-on-logout");
- menuText[ID_TRAYMENU_MUTE] = i18n_.GetTranslationW("traymenu.mute-all-devices");
- menuText[ID_TRAYMENU_SETTINGS] = i18n_.GetTranslationW("traymenu.settings");
- menuText[ID_TRAYMENU_EXIT] = i18n_.GetTranslationW("traymenu.exit");
-
- for (const auto& mt : menuText) {
- MENUITEMINFO mii{ sizeof(MENUITEMINFO) };
- if (!GetMenuItemInfo(hTrayMenu_, mt.first, false, &mii)) {
- continue;
- }
- mii.fMask = MIIM_TYPE;
- mii.fType = MFT_STRING;
- mii.dwTypeData = const_cast<LPWSTR>(mt.second.c_str());
- mii.cch = static_cast<UINT>(mt.second.length());
- if (!SetMenuItemInfo(hTrayMenu_, mt.first, false, &mii)) {
- continue;
- }
- }
+ std::map<UINT, std::wstring> menuText;
+ menuText[ID_TRAYMENU_INFO] = i18n_.GetTranslationW("traymenu.info");
+ menuText[ID_TRAYMENU_LABEL_MUTEWHEN] =
+ i18n_.GetTranslationW("traymenu.mute-when");
+ menuText[ID_TRAYMENU_MUTEONLOCK] =
+ i18n_.GetTranslationW("traymenu.mute-on-lock");
+ menuText[ID_TRAYMENU_MUTEONSCREENSUSPEND] =
+ i18n_.GetTranslationW("traymenu.mute-on-screen-suspend");
+ menuText[ID_TRAYMENU_RESTOREAUDIO] =
+ i18n_.GetTranslationW("traymenu.restore-volume");
+ menuText[ID_TRAYMENU_LABEL_MUTEON_NO_RESTORE] =
+ i18n_.GetTranslationW("traymenu.mute-no-restore");
+ menuText[ID_TRAYMENU_MUTEONSHUTDOWN] =
+ i18n_.GetTranslationW("traymenu.mute-on-shutdown");
+ menuText[ID_TRAYMENU_MUTEONSUSPEND] =
+ i18n_.GetTranslationW("traymenu.mute-on-sleep");
+ menuText[ID_TRAYMENU_MUTEONLOGOUT] =
+ i18n_.GetTranslationW("traymenu.mute-on-logout");
+ menuText[ID_TRAYMENU_MUTE] =
+ i18n_.GetTranslationW("traymenu.mute-all-devices");
+ menuText[ID_TRAYMENU_SETTINGS] = i18n_.GetTranslationW("traymenu.settings");
+ menuText[ID_TRAYMENU_EXIT] = i18n_.GetTranslationW("traymenu.exit");
+
+ for (const auto& mt : menuText) {
+ MENUITEMINFO mii{sizeof(MENUITEMINFO)};
+ if (!GetMenuItemInfo(hTrayMenu_, mt.first, false, &mii)) {
+ continue;
+ }
+ mii.fMask = MIIM_TYPE;
+ mii.fType = MFT_STRING;
+ mii.dwTypeData = const_cast<LPWSTR>(mt.second.c_str());
+ mii.cch = static_cast<UINT>(mt.second.length());
+ if (!SetMenuItemInfo(hTrayMenu_, mt.first, false, &mii)) {
+ continue;
+ }
+ }
}
void WinMute::CheckForUpdates()
{
- const UpdateChecker updateChecker;
- if (!updateChecker.IsUpdateCheckEnabled(settings_)) {
- return;
- }
- if (updateThread_.joinable()) { // Check still running
- return;
- }
- // The worker only uses its own locals and the raw window handle; the
- // result is marshalled back to the UI thread via
- // WM_WINMUTE_UPDATE_CHECK_DONE and handled in OnUpdateCheckDone. As a
- // std::jthread member, the thread is joined before this object dies.
- updateThread_ = std::jthread(&WinMute::CheckForUpdatesAsync, hWnd_);
+ const UpdateChecker updateChecker;
+ if (!updateChecker.IsUpdateCheckEnabled(settings_)) {
+ return;
+ }
+ if (updateThread_.joinable()) { // Check still running
+ return;
+ }
+ // The worker only uses its own locals and the raw window handle; the
+ // result is marshalled back to the UI thread via
+ // WM_WINMUTE_UPDATE_CHECK_DONE and handled in OnUpdateCheckDone. As a
+ // std::jthread member, the thread is joined before this object dies.
+ updateThread_ = std::jthread(&WinMute::CheckForUpdatesAsync, hWnd_);
}
void WinMute::CheckForUpdatesAsync(HWND hWnd)
{
- const UpdateChecker updateChecker;
- auto updateInfo = std::make_unique<UpdateInfo>();
- const bool success = updateChecker.GetUpdateInfo(*updateInfo);
- if (PostMessageW(hWnd, WM_WINMUTE_UPDATE_CHECK_DONE,
- static_cast<WPARAM>(success),
- reinterpret_cast<LPARAM>(updateInfo.get()))) {
- // Ownership has passed to OnUpdateCheckDone
- updateInfo.release();
- }
+ const UpdateChecker updateChecker;
+ auto updateInfo = std::make_unique<UpdateInfo>();
+ const bool success = updateChecker.GetUpdateInfo(*updateInfo);
+ if (PostMessageW(hWnd, WM_WINMUTE_UPDATE_CHECK_DONE,
+ static_cast<WPARAM>(success),
+ reinterpret_cast<LPARAM>(updateInfo.get())))
+ {
+ // Ownership has passed to OnUpdateCheckDone
+ updateInfo.release();
+ }
}
LRESULT WinMute::OnUpdateCheckDone(HWND, WPARAM wParam, LPARAM lParam)
{
- const std::unique_ptr<UpdateInfo> updateInfo{
- reinterpret_cast<UpdateInfo *>(lParam) };
- if (updateThread_.joinable()) {
- updateThread_.join();
- }
- if (wParam == 0 || !updateInfo) {
- wmTray_.ShowPopup(
- i18n_.GetTranslationW("popup.error.update-check-failed.title"),
- i18n_.GetTranslationW("popup.error.update-check-failed.text"));
- return 0;
- }
- updateInfo_ = *updateInfo;
- const bool betaUpdates = settings_.QueryValue(SettingsKey::CHECK_FOR_BETA_UPDATE) != 0;
- if (updateInfo_.beta.shouldUpdate && betaUpdates) {
- const std::wstring popupTitle = SafeVFormat(
- i18n_.GetTranslationW("popup.update-available-beta.title"),
- updateInfo_.beta.version);
- const std::wstring popupText = SafeVFormat(
- i18n_.GetTranslationW("popup.update-available-beta.text"),
- updateInfo_.currentVersion);
- updateTray_.Show();
- updateTray_.ShowPopup(popupTitle, popupText);
- } else if (updateInfo_.stable.shouldUpdate) {
- const std::wstring popupTitle = SafeVFormat(
- i18n_.GetTranslationW("popup.update-available.title"),
- updateInfo_.stable.version);
- const std::wstring popupText = SafeVFormat(
- i18n_.GetTranslationW("popup.update-available.text"),
- updateInfo_.currentVersion);
- updateTray_.Show();
- updateTray_.ShowPopup(popupTitle, popupText);
- }
- return 0;
+ const std::unique_ptr<UpdateInfo> updateInfo{
+ reinterpret_cast<UpdateInfo*>(lParam)};
+ if (updateThread_.joinable()) {
+ updateThread_.join();
+ }
+ if (wParam == 0 || !updateInfo) {
+ wmTray_.ShowPopup(
+ i18n_.GetTranslationW("popup.error.update-check-failed.title"),
+ i18n_.GetTranslationW("popup.error.update-check-failed.text"));
+ return 0;
+ }
+ updateInfo_ = *updateInfo;
+ const bool betaUpdates =
+ settings_.QueryValue(SettingsKey::CHECK_FOR_BETA_UPDATE) != 0;
+ if (updateInfo_.beta.shouldUpdate && betaUpdates) {
+ const std::wstring popupTitle = SafeVFormat(
+ i18n_.GetTranslationW("popup.update-available-beta.title"),
+ updateInfo_.beta.version);
+ const std::wstring popupText = SafeVFormat(
+ i18n_.GetTranslationW("popup.update-available-beta.text"),
+ updateInfo_.currentVersion);
+ updateTray_.Show();
+ updateTray_.ShowPopup(popupTitle, popupText);
+ } else if (updateInfo_.stable.shouldUpdate) {
+ const std::wstring popupTitle =
+ SafeVFormat(i18n_.GetTranslationW("popup.update-available.title"),
+ updateInfo_.stable.version);
+ const std::wstring popupText =
+ SafeVFormat(i18n_.GetTranslationW("popup.update-available.text"),
+ updateInfo_.currentVersion);
+ updateTray_.Show();
+ updateTray_.ShowPopup(popupTitle, popupText);
+ }
+ return 0;
}
LRESULT WinMute::OnCommand(HWND hWnd, WPARAM wParam, LPARAM)
{
- switch (LOWORD(wParam)) {
- case ID_TRAYMENU_INFO:
- {
- static bool dialogOpen = false;
- if (!dialogOpen) {
- dialogOpen = true;
- DialogBox(
- hglobInstance,
- MAKEINTRESOURCE(IDD_ABOUT),
- hWnd_,
- AboutDlgProc);
- dialogOpen = false;
- }
- break;
- }
- case ID_TRAYMENU_EXIT:
- SendMessage(hWnd, WM_CLOSE, 0, 0);
- break;
- case ID_TRAYMENU_SETTINGS: {
- static bool dialogOpen = false;
- if (!dialogOpen) {
- dialogOpen = true;
- if (DialogBoxParamW(
- hglobInstance,
- MAKEINTRESOURCE(IDD_SETTINGS),
- hWnd_,
- SettingsDlgProc,
- reinterpret_cast<LPARAM>(&settings_)) == 0) {
- LoadSettings();
- InitTrayMenu();
- quietHours_.Reset(settings_);
- }
- dialogOpen = false;
- }
- break;
- }
- case ID_TRAYMENU_MUTE: {
- bool state = false;
- ToggleMenuCheck(ID_TRAYMENU_MUTE, &state);
- muteCtrl_.SetMute(state);
- break;
- }
- case ID_TRAYMENU_MUTEONLOCK: {
- bool checked = false;
- ToggleMenuCheck(ID_TRAYMENU_MUTEONLOCK, &checked);
- muteCtrl_.SetMuteOnWorkstationLock(checked);
- settings_.SetValue(SettingsKey::MUTE_ON_LOCK, checked);
- break;
- }
- case ID_TRAYMENU_RESTOREAUDIO: {
- bool checked = false;
- ToggleMenuCheck(ID_TRAYMENU_RESTOREAUDIO, &checked);
- muteCtrl_.SetRestoreVolume(checked);
- settings_.SetValue(SettingsKey::RESTORE_AUDIO, checked);
- break;
- }
- case ID_TRAYMENU_MUTEONSCREENSUSPEND: {
- bool checked = false;
- ToggleMenuCheck(ID_TRAYMENU_SCREENSUSPEND, &checked);
- muteCtrl_.SetMuteOnDisplayStandby(checked);
- settings_.SetValue(SettingsKey::MUTE_ON_DISPLAYSTANDBY, checked);
- break;
- }
- case ID_TRAYMENU_MUTEONSHUTDOWN: {
- bool checked = false;
- ToggleMenuCheck(ID_TRAYMENU_MUTEONSHUTDOWN, &checked);
- muteCtrl_.SetMuteOnShutdown(checked);
- settings_.SetValue(SettingsKey::MUTE_ON_SHUTDOWN, checked);
- break;
- }
- case ID_TRAYMENU_MUTEONSUSPEND: {
- bool checked = false;
- ToggleMenuCheck(ID_TRAYMENU_MUTEONSUSPEND, &checked);
- muteCtrl_.SetMuteOnSuspend(checked);
- settings_.SetValue(SettingsKey::MUTE_ON_SUSPEND, checked);
- break;
- }
- case ID_TRAYMENU_MUTEONLOGOUT: {
- bool checked = false;
- ToggleMenuCheck(ID_TRAYMENU_MUTEONLOGOUT, &checked);
- muteCtrl_.SetMuteOnLogout(checked);
- settings_.SetValue(SettingsKey::MUTE_ON_LOGOUT, checked);
- break;
- }
- default:
- break;
- }
- return 0;
+ switch (LOWORD(wParam)) {
+ case ID_TRAYMENU_INFO: {
+ static bool dialogOpen = false;
+ if (!dialogOpen) {
+ dialogOpen = true;
+ DialogBox(hglobInstance, MAKEINTRESOURCE(IDD_ABOUT), hWnd_,
+ AboutDlgProc);
+ dialogOpen = false;
+ }
+ break;
+ }
+ case ID_TRAYMENU_EXIT:
+ SendMessage(hWnd, WM_CLOSE, 0, 0);
+ break;
+ case ID_TRAYMENU_SETTINGS: {
+ static bool dialogOpen = false;
+ if (!dialogOpen) {
+ dialogOpen = true;
+ if (DialogBoxParamW(hglobInstance,
+ MAKEINTRESOURCE(IDD_SETTINGS), hWnd_,
+ SettingsDlgProc,
+ reinterpret_cast<LPARAM>(&settings_)) == 0)
+ {
+ LoadSettings();
+ InitTrayMenu();
+ quietHours_.Reset(settings_);
+ }
+ dialogOpen = false;
+ }
+ break;
+ }
+ case ID_TRAYMENU_MUTE: {
+ bool state = false;
+ ToggleMenuCheck(ID_TRAYMENU_MUTE, &state);
+ muteCtrl_.SetMute(state);
+ break;
+ }
+ case ID_TRAYMENU_MUTEONLOCK: {
+ bool checked = false;
+ ToggleMenuCheck(ID_TRAYMENU_MUTEONLOCK, &checked);
+ muteCtrl_.SetMuteOnWorkstationLock(checked);
+ settings_.SetValue(SettingsKey::MUTE_ON_LOCK, checked);
+ break;
+ }
+ case ID_TRAYMENU_RESTOREAUDIO: {
+ bool checked = false;
+ ToggleMenuCheck(ID_TRAYMENU_RESTOREAUDIO, &checked);
+ muteCtrl_.SetRestoreVolume(checked);
+ settings_.SetValue(SettingsKey::RESTORE_AUDIO, checked);
+ break;
+ }
+ case ID_TRAYMENU_MUTEONSCREENSUSPEND: {
+ bool checked = false;
+ ToggleMenuCheck(ID_TRAYMENU_SCREENSUSPEND, &checked);
+ muteCtrl_.SetMuteOnDisplayStandby(checked);
+ settings_.SetValue(SettingsKey::MUTE_ON_DISPLAYSTANDBY, checked);
+ break;
+ }
+ case ID_TRAYMENU_MUTEONSHUTDOWN: {
+ bool checked = false;
+ ToggleMenuCheck(ID_TRAYMENU_MUTEONSHUTDOWN, &checked);
+ muteCtrl_.SetMuteOnShutdown(checked);
+ settings_.SetValue(SettingsKey::MUTE_ON_SHUTDOWN, checked);
+ break;
+ }
+ case ID_TRAYMENU_MUTEONSUSPEND: {
+ bool checked = false;
+ ToggleMenuCheck(ID_TRAYMENU_MUTEONSUSPEND, &checked);
+ muteCtrl_.SetMuteOnSuspend(checked);
+ settings_.SetValue(SettingsKey::MUTE_ON_SUSPEND, checked);
+ break;
+ }
+ case ID_TRAYMENU_MUTEONLOGOUT: {
+ bool checked = false;
+ ToggleMenuCheck(ID_TRAYMENU_MUTEONLOGOUT, &checked);
+ muteCtrl_.SetMuteOnLogout(checked);
+ settings_.SetValue(SettingsKey::MUTE_ON_LOGOUT, checked);
+ break;
+ }
+ default:
+ break;
+ }
+ return 0;
}
LRESULT WinMute::OnTrayIcon(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
- // NOTIFYICON_VERSION_4 semantics: LOWORD(lParam) carries the event,
- // HIWORD(lParam) the icon ID and wParam the anchor coordinates.
- const UINT event = LOWORD(lParam);
- if (event == WM_CONTEXTMENU ||
- event == NIN_SELECT ||
- event == NIN_KEYSELECT) {
- const POINT p{ GET_X_LPARAM(wParam), GET_Y_LPARAM(wParam) };
- SetForegroundWindow(hWnd);
- TrackPopupMenuEx(
- GetSubMenu(hTrayMenu_, 0),
- TPM_NONOTIFY | TPM_TOPALIGN | TPM_LEFTALIGN,
- p.x, p.y, hWnd_, nullptr);
- }
- return TRUE;
+ // NOTIFYICON_VERSION_4 semantics: LOWORD(lParam) carries the event,
+ // HIWORD(lParam) the icon ID and wParam the anchor coordinates.
+ const UINT event = LOWORD(lParam);
+ if (event == WM_CONTEXTMENU || event == NIN_SELECT ||
+ event == NIN_KEYSELECT)
+ {
+ const POINT p{GET_X_LPARAM(wParam), GET_Y_LPARAM(wParam)};
+ SetForegroundWindow(hWnd);
+ TrackPopupMenuEx(GetSubMenu(hTrayMenu_, 0),
+ TPM_NONOTIFY | TPM_TOPALIGN | TPM_LEFTALIGN, p.x, p.y,
+ hWnd_, nullptr);
+ }
+ return TRUE;
}
LRESULT WinMute::OnUpdatePopup(HWND hWnd, WPARAM, LPARAM lParam)
{
- // NOTIFYICON_VERSION_4 semantics: LOWORD(lParam) carries the event.
- const UINT event = LOWORD(lParam);
- if (event == NIN_BALLOONUSERCLICK) {
- const bool betaUpdates = settings_.QueryValue(SettingsKey::CHECK_FOR_BETA_UPDATE) != 0;
- if (betaUpdates && updateInfo_.beta.shouldUpdate) {
- LaunchBrowser(hWnd, updateInfo_.beta.downloadUrl);
- } else if (updateInfo_.stable.shouldUpdate) {
- LaunchBrowser(hWnd, updateInfo_.stable.downloadUrl);
- }
- }
- if (event == NIN_BALLOONHIDE || event == NIN_BALLOONTIMEOUT || event == NIN_BALLOONUSERCLICK) {
- updateTray_.Hide();
- }
- return 0;
+ // NOTIFYICON_VERSION_4 semantics: LOWORD(lParam) carries the event.
+ const UINT event = LOWORD(lParam);
+ if (event == NIN_BALLOONUSERCLICK) {
+ const bool betaUpdates =
+ settings_.QueryValue(SettingsKey::CHECK_FOR_BETA_UPDATE) != 0;
+ if (betaUpdates && updateInfo_.beta.shouldUpdate) {
+ LaunchBrowser(hWnd, updateInfo_.beta.downloadUrl);
+ } else if (updateInfo_.stable.shouldUpdate) {
+ LaunchBrowser(hWnd, updateInfo_.stable.downloadUrl);
+ }
+ }
+ if (event == NIN_BALLOONHIDE || event == NIN_BALLOONTIMEOUT ||
+ event == NIN_BALLOONUSERCLICK)
+ {
+ updateTray_.Hide();
+ }
+ return 0;
}
-
LRESULT WinMute::OnSettingChange(HWND, WPARAM, LPARAM)
{
- return 0;
+ return 0;
}
LRESULT WinMute::OnPowerBroadcast(HWND, WPARAM wParam, LPARAM lParam)
{
- if (wParam == PBT_APMSUSPEND) {
- muteCtrl_.NotifySuspend(true);
- } else if (wParam == PBT_POWERSETTINGCHANGE) {
- const PPOWERBROADCAST_SETTING bs =
- reinterpret_cast<PPOWERBROADCAST_SETTING>(lParam);
- if (IsEqualGUID(bs->PowerSetting, GUID_CONSOLE_DISPLAY_STATE)) {
- const DWORD state = bs->Data[0];
- if (state == 0x0) { // Display standby
- muteCtrl_.NotifyDisplayStandby(true);
- } else if (state == 0x1) { // Display on
- muteCtrl_.NotifyDisplayStandby(false);
- } else if (state == 0x2) { // Display dimmed
- }
- }
- }
- return TRUE;
+ if (wParam == PBT_APMSUSPEND) {
+ muteCtrl_.NotifySuspend(true);
+ } else if (wParam == PBT_POWERSETTINGCHANGE) {
+ const PPOWERBROADCAST_SETTING bs =
+ reinterpret_cast<PPOWERBROADCAST_SETTING>(lParam);
+ if (IsEqualGUID(bs->PowerSetting, GUID_CONSOLE_DISPLAY_STATE)) {
+ const DWORD state = bs->Data[0];
+ if (state == 0x0) { // Display standby
+ muteCtrl_.NotifyDisplayStandby(true);
+ } else if (state == 0x1) { // Display on
+ muteCtrl_.NotifyDisplayStandby(false);
+ } else if (state == 0x2) { // Display dimmed
+ }
+ } else if (IsEqualGUID(bs->PowerSetting, GUID_LIDSWITCH_STATE_CHANGE)) {
+ const DWORD state = bs->Data[0];
+ if (state == 0x0) { // Lid closed
+ muteCtrl_.NotifyLidClosed(true);
+ } else if (state == 0x1) { // Lid open
+ muteCtrl_.NotifyLidClosed(true);
+ }
+ }
+ }
+ return TRUE;
}
LRESULT WinMute::OnQuietHours(HWND, UINT msg, WPARAM, LPARAM)
{
- if (msg == WM_WINMUTE_QUIETHOURS_START) {
- muteCtrl_.NotifyQuietHours(true);
- if (settings_.QueryValue(SettingsKey::QUIETHOURS_NOTIFICATIONS)) {
- wmTray_.ShowPopup(
- i18n_.GetTranslationW("popup.quiet-hours-started.title"),
- i18n_.GetTranslationW("popup.quiet-hours-started.text"));
- }
- quietHours_.SetEnd();
- return 0;
- } else if (msg == WM_WINMUTE_QUIETHOURS_END) {
- muteCtrl_.NotifyQuietHours(false);
- if (settings_.QueryValue(SettingsKey::QUIETHOURS_NOTIFICATIONS)) {
- wmTray_.ShowPopup(
- i18n_.GetTranslationW("popup.quiet-hours-ended.title"),
- i18n_.GetTranslationW("popup.quiet-hours-ended.text"));
- }
- if (settings_.QueryValue(SettingsKey::QUIETHOURS_FORCEUNMUTE)) {
- muteCtrl_.SetMute(false);
- }
- quietHours_.SetStart();
- }
- return 0;
+ if (msg == WM_WINMUTE_QUIETHOURS_START) {
+ muteCtrl_.NotifyQuietHours(true);
+ if (settings_.QueryValue(SettingsKey::QUIETHOURS_NOTIFICATIONS)) {
+ wmTray_.ShowPopup(
+ i18n_.GetTranslationW("popup.quiet-hours-started.title"),
+ i18n_.GetTranslationW("popup.quiet-hours-started.text"));
+ }
+ quietHours_.SetEnd();
+ return 0;
+ } else if (msg == WM_WINMUTE_QUIETHOURS_END) {
+ muteCtrl_.NotifyQuietHours(false);
+ if (settings_.QueryValue(SettingsKey::QUIETHOURS_NOTIFICATIONS)) {
+ wmTray_.ShowPopup(
+ i18n_.GetTranslationW("popup.quiet-hours-ended.title"),
+ i18n_.GetTranslationW("popup.quiet-hours-ended.text"));
+ }
+ if (settings_.QueryValue(SettingsKey::QUIETHOURS_FORCEUNMUTE)) {
+ muteCtrl_.SetMute(false);
+ }
+ quietHours_.SetStart();
+ }
+ return 0;
}
LRESULT WinMute::OnAudioServiceShutdown(HWND hWnd, WPARAM, LPARAM)
{
- TaskDialog(
- hWnd, hglobInstance, PROGRAM_NAME,
- i18n_.GetTranslationW("general.error.audio-service-shutdown.title").c_str(),
- i18n_.GetTranslationW("general.error.audio-service-shutdown.text").c_str(),
- TDCBF_OK_BUTTON, TD_WARNING_ICON, nullptr);
- return 0;
+ TaskDialog(
+ hWnd, hglobInstance, PROGRAM_NAME,
+ i18n_.GetTranslationW("general.error.audio-service-shutdown.title")
+ .c_str(),
+ i18n_.GetTranslationW("general.error.audio-service-shutdown.text")
+ .c_str(),
+ TDCBF_OK_BUTTON, TD_WARNING_ICON, nullptr);
+ return 0;
}
LRESULT WinMute::OnDeviceChange(HWND, UINT msg, WPARAM wParam, LPARAM lParam)
{
- if (muteConfig_.muteOnBluetooth) {
- const auto btStatus = btDetector_.GetBluetoothStatus(msg, wParam, lParam);
- if (btStatus == BluetoothDetector::BluetoothStatus::Connected) {
- muteCtrl_.NotifyBluetoothConnected(true);
- } else if (btStatus == BluetoothDetector::BluetoothStatus::Disconnected) {
- muteCtrl_.NotifyBluetoothConnected(false);
- }
- }
- return TRUE;
+ if (muteConfig_.muteOnBluetooth) {
+ const auto btStatus =
+ btDetector_.GetBluetoothStatus(msg, wParam, lParam);
+ if (btStatus == BluetoothDetector::BluetoothStatus::Connected) {
+ muteCtrl_.NotifyBluetoothConnected(true);
+ } else if (btStatus == BluetoothDetector::BluetoothStatus::Disconnected)
+ {
+ muteCtrl_.NotifyBluetoothConnected(false);
+ }
+ }
+ return TRUE;
}
LRESULT WinMute::OnWifiStatusChange(HWND, WPARAM wParam, LPARAM lParam)
{
- if (!muteConfig_.muteOnWlan) {
- return 0;
- }
- if (wParam != 1) { // Not Connected
- return 0;
- }
-
- if (settings_.QueryValue(SettingsKey::NOTIFICATIONS_ENABLED)) {
- std::wstring popupMsg;
- // Non-owning: the name is only valid for the duration of this call.
- const wchar_t *wifiName = reinterpret_cast<const wchar_t *>(lParam);
- if (settings_.QueryValue(SettingsKey::MUTE_ON_WLAN_ALLOWLIST)) {
- popupMsg = SafeVFormat(
- i18n_.GetTranslationW("popup.wlan-not-on-mute-list.text"),
- wifiName);
- } else {
- popupMsg = SafeVFormat(
- i18n_.GetTranslationW("popup.wlan-is-on-mute-list.text"),
- wifiName);
- }
- wmTray_.ShowPopup(
- i18n_.GetTranslationW("popup.workstation-muted.title"),
- popupMsg);
- }
- muteCtrl_.SetMute(true);
-
- return 0;
+ if (!muteConfig_.muteOnWlan) {
+ return 0;
+ }
+ if (wParam != 1) { // Not Connected
+ return 0;
+ }
+
+ if (settings_.QueryValue(SettingsKey::NOTIFICATIONS_ENABLED)) {
+ std::wstring popupMsg;
+ // Non-owning: the name is only valid for the duration of this call.
+ const wchar_t* wifiName = reinterpret_cast<const wchar_t*>(lParam);
+ if (settings_.QueryValue(SettingsKey::MUTE_ON_WLAN_ALLOWLIST)) {
+ popupMsg = SafeVFormat(
+ i18n_.GetTranslationW("popup.wlan-not-on-mute-list.text"),
+ wifiName);
+ } else {
+ popupMsg = SafeVFormat(
+ i18n_.GetTranslationW("popup.wlan-is-on-mute-list.text"),
+ wifiName);
+ }
+ wmTray_.ShowPopup(
+ i18n_.GetTranslationW("popup.workstation-muted.title"), popupMsg);
+ }
+ muteCtrl_.SetMute(true);
+
+ return 0;
}
-LRESULT CALLBACK WinMute::WindowProc(
- HWND hWnd,
- UINT msg,
- WPARAM wParam,
- LPARAM lParam)
+LRESULT CALLBACK WinMute::WindowProc(HWND hWnd, UINT msg, WPARAM wParam,
+ LPARAM lParam)
{
- static UINT uTaskbarRestart = 0;
- switch (msg) {
- case WM_CREATE:
- uTaskbarRestart = RegisterWindowMessageW(L"TaskbarCreated");
- return TRUE;
- case WM_COMMAND:
- return OnCommand(hWnd, wParam, lParam);
- case WM_WINMUTE_UPDATE_POPUP:
- return OnUpdatePopup(hWnd, wParam, lParam);
- case WM_TRAYICON:
- return OnTrayIcon(hWnd, wParam, lParam);
- case WM_CLOSE:
- Close();
- return 0;
- case WM_WTSSESSION_CHANGE: {
- if (wParam == WTS_SESSION_LOCK) {
- muteCtrl_.NotifyWorkstationLock(true);
- } else if (wParam == WTS_SESSION_UNLOCK) {
- muteCtrl_.NotifyWorkstationLock(false);
- }
- return 0;
- }
- case WM_POWERBROADCAST:
- return OnPowerBroadcast(hWnd, wParam, lParam);
- case WM_QUERYENDSESSION:
- return TRUE;
- case WM_ENDSESSION:
- if (wParam == TRUE) {
- if (lParam == 0) { // Shutdown
- muteCtrl_.NotifyShutdown();
- } else if ((lParam & ENDSESSION_LOGOFF)) {
- muteCtrl_.NotifyLogout();
- }
- }
- break;
- case WM_WINMUTE_QUIETHOURS_START: // fall through
- case WM_WINMUTE_QUIETHOURS_END:
- return OnQuietHours(hWnd, msg, wParam, lParam);
- case WM_WINMUTE_AUDIO_SERVICE_SHUTDOWN:
- return OnAudioServiceShutdown(hWnd, wParam, lParam);
- case WM_DEVICECHANGE:
- return OnDeviceChange(hWnd, msg, wParam, lParam);
- case WM_WIFISTATUSCHANGED:
- return OnWifiStatusChange(hWnd, wParam, lParam);
- case WM_WINMUTE_UPDATE_CHECK_DONE:
- return OnUpdateCheckDone(hWnd, wParam, lParam);
- case WM_SETTINGCHANGE:
- return OnSettingChange(hWnd, wParam, lParam);
- default:
- if (msg == uTaskbarRestart) { // Restore trayicon if explorer.exe crashes
- wmTray_.Hide();
- wmTray_.Show();
- }
- break;
- }
-
- return DefWindowProc(hWnd, msg, wParam, lParam);
+ static UINT uTaskbarRestart = 0;
+ switch (msg) {
+ case WM_CREATE:
+ uTaskbarRestart = RegisterWindowMessageW(L"TaskbarCreated");
+ return TRUE;
+ case WM_COMMAND:
+ return OnCommand(hWnd, wParam, lParam);
+ case WM_WINMUTE_UPDATE_POPUP:
+ return OnUpdatePopup(hWnd, wParam, lParam);
+ case WM_TRAYICON:
+ return OnTrayIcon(hWnd, wParam, lParam);
+ case WM_CLOSE:
+ Close();
+ return 0;
+ case WM_WTSSESSION_CHANGE: {
+ if (wParam == WTS_SESSION_LOCK) {
+ muteCtrl_.NotifyWorkstationLock(true);
+ } else if (wParam == WTS_SESSION_UNLOCK) {
+ muteCtrl_.NotifyWorkstationLock(false);
+ }
+ return 0;
+ }
+ case WM_POWERBROADCAST:
+ return OnPowerBroadcast(hWnd, wParam, lParam);
+ case WM_QUERYENDSESSION:
+ return TRUE;
+ case WM_ENDSESSION:
+ if (wParam == TRUE) {
+ if (lParam == 0) { // Shutdown
+ muteCtrl_.NotifyShutdown();
+ } else if ((lParam & ENDSESSION_LOGOFF)) {
+ muteCtrl_.NotifyLogout();
+ }
+ }
+ break;
+ case WM_WINMUTE_QUIETHOURS_START: // fall through
+ case WM_WINMUTE_QUIETHOURS_END:
+ return OnQuietHours(hWnd, msg, wParam, lParam);
+ case WM_WINMUTE_AUDIO_SERVICE_SHUTDOWN:
+ return OnAudioServiceShutdown(hWnd, wParam, lParam);
+ case WM_DEVICECHANGE:
+ return OnDeviceChange(hWnd, msg, wParam, lParam);
+ case WM_WIFISTATUSCHANGED:
+ return OnWifiStatusChange(hWnd, wParam, lParam);
+ case WM_WINMUTE_UPDATE_CHECK_DONE:
+ return OnUpdateCheckDone(hWnd, wParam, lParam);
+ case WM_SETTINGCHANGE:
+ return OnSettingChange(hWnd, wParam, lParam);
+ default:
+ if (msg ==
+ uTaskbarRestart) { // Restore trayicon if explorer.exe crashes
+ wmTray_.Hide();
+ wmTray_.Show();
+ }
+ break;
+ }
+
+ return DefWindowProc(hWnd, msg, wParam, lParam);
}
void WinMute::Unload() noexcept
{
- if (hPowerNotify_ != nullptr) {
- UnregisterPowerSettingNotification(hPowerNotify_);
- hPowerNotify_ = nullptr;
- }
- if (wtsSessionNotificationRegistered_) {
- WTSUnRegisterSessionNotification(hWnd_);
- wtsSessionNotificationRegistered_ = false;
- }
- settings_.Unload();
+ if (hPowerNotify_ != nullptr) {
+ UnregisterPowerSettingNotification(hPowerNotify_);
+ hPowerNotify_ = nullptr;
+ }
+ if (hLidCloseNotify_ != nullptr) {
+ UnregisterPowerSettingNotification(hLidCloseNotify_);
+ hLidCloseNotify_ = nullptr;
+ }
+ if (wtsSessionNotificationRegistered_) {
+ WTSUnRegisterSessionNotification(hWnd_);
+ wtsSessionNotificationRegistered_ = false;
+ }
+ settings_.Unload();
}
void WinMute::Close()
{
- Unload();
- PostQuitMessage(0);
+ Unload();
+ PostQuitMessage(0);
}