]> repos.lx-s.de Git - winmute.git/commitdiff
Quiet hours enhancement. Added notifications.
authorlx-s <hello@lx-s.de>
Wed, 28 Oct 2020 10:34:02 +0000 (11:34 +0100)
committerlx-s <hello@lx-s.de>
Wed, 28 Oct 2020 10:37:54 +0000 (11:37 +0100)
Quiet Hours now respect if the workstation was already muted.
An option has been added to force an unmute regardless whether the
workstation was muted before.
Also added system notifications when quiet hours start or end.

WinMute/QuietHoursDlg.cpp
WinMute/Settings.cpp
WinMute/Settings.h
WinMute/StdAfx.h
WinMute/TrayIcon.cpp
WinMute/TrayIcon.h
WinMute/WinMute.cpp
WinMute/WinMute.h
WinMute/res.rc
WinMute/resource.h

index 8d51e19ca3f3c8bcc1185a0320d3f67f7c90599f..7b3bad9de8220f199fe489ab3abe5bce1714cae2 100644 (file)
@@ -55,6 +55,7 @@ static bool IsValidTimeRange(
 static bool SaveQuietHours(
    Settings *settings,
    const int enabled,
+   const int forceUnmute,
    const LPSYSTEMTIME start,
    const LPSYSTEMTIME end)
 {
@@ -78,6 +79,7 @@ static bool SaveQuietHours(
    int setEnd   = end->wHour * 3600 + end->wMinute * 60 + end->wSecond;
 
    if (!settings->SetValue(SettingsKey::QUIETHOURS_ENABLE, enabled) ||
+       !settings->SetValue(SettingsKey::QUIETHOURS_FORCEUNMUTE, forceUnmute) ||
        !settings->SetValue(SettingsKey::QUIETHOURS_START, setStart) ||
        !settings->SetValue(SettingsKey::QUIETHOURS_END, setEnd)) {
       TaskDialog(
@@ -91,7 +93,7 @@ static bool SaveQuietHours(
          nullptr);
       return false;
    }
-   
+
    return true;
 }
 
@@ -101,6 +103,7 @@ INT_PTR CALLBACK QuietHoursDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lP
    switch (msg) {
    case WM_INITDIALOG: {
       HWND hEnable = GetDlgItem(hDlg, IDC_ENABLEQUIETHOURS);
+      HWND hForce = GetDlgItem(hDlg, IDC_FORCEUNMUTE);
       HWND hStart = GetDlgItem(hDlg, IDC_QUIETHOURS_START);
       HWND hEnd = GetDlgItem(hDlg, IDC_QUIETHOURS_END);
       Settings *settings = reinterpret_cast<Settings*>(lParam);
@@ -109,6 +112,11 @@ INT_PTR CALLBACK QuietHoursDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lP
 
       DWORD qhEnabled = !!settings->QueryValue(SettingsKey::QUIETHOURS_ENABLE, FALSE);
       Button_SetCheck(hEnable, qhEnabled ? BST_CHECKED : BST_UNCHECKED);
+
+      DWORD qhForceUnmute = !!settings->QueryValue(SettingsKey::QUIETHOURS_FORCEUNMUTE, FALSE);
+      Button_SetCheck(hForce, qhForceUnmute ? BST_CHECKED : BST_UNCHECKED);
+
+      EnableWindow(hForce, qhEnabled);
       EnableWindow(hStart, qhEnabled);
       EnableWindow(hEnd, qhEnabled);
 
@@ -137,6 +145,7 @@ INT_PTR CALLBACK QuietHoursDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lP
    }
    case WM_COMMAND: {
       HWND hEnable = GetDlgItem(hDlg, IDC_ENABLEQUIETHOURS);
+      HWND hForce = GetDlgItem(hDlg, IDC_FORCEUNMUTE);
       HWND hStart = GetDlgItem(hDlg, IDC_QUIETHOURS_START);
       HWND hEnd = GetDlgItem(hDlg, IDC_QUIETHOURS_END);
       if (LOWORD(wParam) == IDOK) {
@@ -144,10 +153,11 @@ INT_PTR CALLBACK QuietHoursDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lP
          SYSTEMTIME end;
          Settings *settings = reinterpret_cast<Settings*>(GetWindowLongPtr(hDlg, GWLP_USERDATA));
          int qhEnabled = Button_GetCheck(hEnable) == BST_CHECKED;
+         int qhForceUnmute = Button_GetCheck(hForce) == BST_CHECKED;
          DateTime_GetSystemtime(hStart, &start);
          DateTime_GetSystemtime(hEnd, &end);
          settings->SetValue(SettingsKey::QUIETHOURS_ENABLE, qhEnabled);
-         if (SaveQuietHours(settings, qhEnabled, &start, &end)) {
+         if (SaveQuietHours(settings, qhEnabled, qhForceUnmute, &start, &end)) {
             SendMessage(GetParent(hDlg), WM_WINMUTE_QUIETHOURS_CHANGE, 0, 0);
             EndDialog(hDlg, 0);
          }
index f0d18fbb6038d3f04344d49bfd0eb5948769cb5a..aaf8a1f38104bd5cdb4bde493eeeef0e919e451b 100644 (file)
@@ -62,6 +62,9 @@ static LPCWSTR KeyToStr(SettingsKey key)
    case SettingsKey::QUIETHOURS_ENABLE:
       keyStr = _T("QuietHoursEnabled");
       break;
+   case SettingsKey::QUIETHOURS_FORCEUNMUTE:
+      keyStr = _T("QuietHoursForceUnmute");
+      break;
    case SettingsKey::QUIETHOURS_START:
       keyStr = _T("QuietHoursStart");
       break;
index 2476a6db4abf3d093e147a27da59779a7f0258f8..56c213e81b645f9b65015690e3bf78e51c5b6ec3 100644 (file)
@@ -43,6 +43,7 @@ enum class SettingsKey {
    , MUTE_ON_SHUTDOWN
    , MUTE_ON_LOGOUT
    , QUIETHOURS_ENABLE
+   , QUIETHOURS_FORCEUNMUTE
    , QUIETHOURS_START
    , QUIETHOURS_END
 };
index b62e5f9b4a6876f4d0ea277dc53ae7cfcd1566c9..f25beca9f39888db9e27358905bc211e24663e90 100644 (file)
@@ -46,7 +46,7 @@ POSSIBILITY OF SUCH DAMAGE.
 #endif
 
 #define STRICT
-#define _WIN32_WINNT 0x0600
+#define _WIN32_WINNT 0x0601
 #include <sdkddkver.h>
 
 #include <cassert>
index 699034b90e724d6002bbd9c7e4c3249b4454383a..d3ef06fa137fafba0fdf5b18433e5b129f47ddc4 100644 (file)
@@ -124,6 +124,24 @@ void TrayIcon::ChangeText(const std::wstring& tooltip)
       ChangeText();
 }
 
+void TrayIcon::ShowPopup(
+   const std::wstring &title,
+   const std::wstring &text)
+{
+   NOTIFYICONDATA tnid;
+   tnid.cbSize = sizeof(NOTIFYICONDATA);
+   tnid.hWnd = hWnd_;
+   tnid.uID = trayID_;
+   tnid.uFlags = NIF_INFO;
+   tnid.dwInfoFlags = NIIF_INFO | NIIF_NOSOUND | NIIF_LARGE_ICON
+                     | NIIF_RESPECT_QUIET_TIME;
+   tnid.uTimeout = 10 * 1000;
+   wcscpy_s(tnid.szInfoTitle, sizeof(tnid.szInfoTitle)/sizeof(TCHAR), title.c_str());
+   wcscpy_s(tnid.szInfo, sizeof(tnid.szInfo)/sizeof(TCHAR), text.c_str());
+
+   Shell_NotifyIcon(NIM_MODIFY, &tnid);
+}
+
 void TrayIcon::DestroyTrayIcon()
 {
    if (hIcon_) {
@@ -178,3 +196,5 @@ bool TrayIcon::ChangeText()
    }
    return false;
 }
+
+
index 78417dc577ec38e73e62be19686fcf13bf82dda4..b56359893f82302692da52b89f95bb2c2a0bbe26 100644 (file)
@@ -49,6 +49,7 @@ public:
    bool IsShown() const { return iconVisible_; }
    void ChangeIcon(HICON hNewIcon);
    void ChangeText(const std::wstring& tooltip);
+   void ShowPopup(const std::wstring& title, const std::wstring& text);
 
 private:
    void DestroyTrayIcon();
index 65ec3c5867d19565d6cf5c63b00db46e16fda328..f0c159d05da9b8b1e18293c4263ac2a508c76681 100644 (file)
@@ -70,6 +70,7 @@ WinMute::MuteConfig::MuteConfig()
    this->noRestore.onShutdown = false;
 
    this->quietHours.enabled = false;
+   this->quietHours.forceUnmute = false;
    this->quietHours.start = 0;
    this->quietHours.end = 0;
 }
@@ -78,7 +79,8 @@ WinMute::WinMute() :
    hWnd_(nullptr),
    hTrayMenu_(nullptr),
    hAppIcon_(nullptr),
-   hTrayIcon_(nullptr)
+   hTrayIcon_(nullptr),
+   wsAlreadyMuted_(false)
 { }
 
 WinMute::~WinMute()
@@ -219,6 +221,8 @@ bool WinMute::LoadDefaults()
 
    muteConfig_.quietHours.enabled =
       settings_.QueryValue(SettingsKey::QUIETHOURS_ENABLE, 0) != 0;
+   muteConfig_.quietHours.forceUnmute =
+      settings_.QueryValue(SettingsKey::QUIETHOURS_FORCEUNMUTE, 0) != 0;
    muteConfig_.quietHours.start =
       settings_.QueryValue(SettingsKey::QUIETHOURS_START, 0);
    muteConfig_.quietHours.end =
@@ -346,14 +350,13 @@ LRESULT CALLBACK WinMute::WindowProc(
       return 0;
    }
    case WM_WTSSESSION_CHANGE: {
-      static bool wasAlreadyMuted = false;
       if (wParam == WTS_SESSION_LOCK) {
-         wasAlreadyMuted = audio_->IsMuted();
-         if (!wasAlreadyMuted && muteConfig_.withRestore.onLock) {
+         wsAlreadyMuted_ = audio_->IsMuted();
+         if (!wsAlreadyMuted_ && muteConfig_.withRestore.onLock) {
             audio_->Mute();
          }
       } else if (wParam == WTS_SESSION_UNLOCK) {
-         if (!wasAlreadyMuted &&
+         if (!wsAlreadyMuted_ &&
              muteConfig_.restoreAudio &&
              muteConfig_.withRestore.onLock) {
             audio_->UnMute();
@@ -379,6 +382,26 @@ LRESULT CALLBACK WinMute::WindowProc(
          }
       }
       break;
+   case WM_WINMUTE_QUIETHOURS_START:
+      wsAlreadyMuted_ = audio_->IsMuted();
+      audio_->Mute();
+      trayIcon_.ShowPopup(
+         _T("WinMute: Quiet hours started"),
+         _T("Your workstation audio is now muted"));
+      return 0;
+   case WM_WINMUTE_QUIETHOURS_END:
+      if (wsAlreadyMuted_ && muteConfig_.quietHours.forceUnmute ||
+          !wsAlreadyMuted_) {
+         audio_->UnMute();
+         trayIcon_.ShowPopup(
+            _T("WinMute: Quiet Hours ended"),
+            _T("Your workstation audio has been restored."));
+      } else {
+         trayIcon_.ShowPopup(
+            _T("WinMute: Quiet Hours ended"),
+            _T("Quiet hours ended, your workstation remains muted."));
+      }
+      return 0;
    case WM_WINMUTE_MUTE: {
       bool mute = !!static_cast<int>(wParam);
       if (mute) {
@@ -410,14 +433,13 @@ LRESULT CALLBACK WinMute::WindowProc(
       return 0;
    }
    case WM_SCRNSAVE_CHANGE: {
-      static bool wasAlreadyMuted = false;
       if (wParam == SCRNSAVE_START) {
-         wasAlreadyMuted = audio_->IsMuted();
-         if (!wasAlreadyMuted && muteConfig_.withRestore.onScreensaver) {
+         wsAlreadyMuted_ = audio_->IsMuted();
+         if (!wsAlreadyMuted_ && muteConfig_.withRestore.onScreensaver) {
             audio_->Mute();
          }
       } else if (wParam == SCRNSAVE_STOP) {
-         if (!wasAlreadyMuted && 
+         if (!wsAlreadyMuted_ &&
              muteConfig_.restoreAudio &&
              muteConfig_.withRestore.onScreensaver) {
             audio_->UnMute();
@@ -475,7 +497,7 @@ static int GetDiffMillseconds(const LPSYSTEMTIME t1, const LPSYSTEMTIME t2)
 
    res = ulT1.QuadPart - ulT2.QuadPart;
 
-   res /= (1000000000 / 100); // To seconds 
+   res /= (1000000000 / 100); // To seconds
    if (res < 0) { // Add 24 Hours for Wrap Around
       res += 24 * 60 * 60;
    }
@@ -489,11 +511,11 @@ VOID CALLBACK QuietHoursTimer(HWND hWnd, UINT msg, UINT_PTR id, DWORD msSinceSys
    UNREFERENCED_PARAMETER( msg );
    UNREFERENCED_PARAMETER( msSinceSysStart );
    if (id == QUIETHOURS_TIMER_START_ID) {
-      SendMessage(hWnd, WM_WINMUTE_MUTE, 1, 0);
+      SendMessage(hWnd, WM_WINMUTE_QUIETHOURS_START, 0, 0);
       SendMessage(hWnd, WM_WINMUTE_QUIETHOURS_CHANGE, 0, 0);
       KillTimer(hWnd, id);
    } else if (id == QUIETHOURS_TIMER_END_ID) {
-      SendMessage(hWnd, WM_WINMUTE_MUTE, 0, 0);
+      SendMessage(hWnd, WM_WINMUTE_QUIETHOURS_END, 0, 0);
       SendMessage(hWnd, WM_WINMUTE_QUIETHOURS_CHANGE, 0, 0);
       KillTimer(hWnd, id);
    }
index 3619d098a9f2ddf727ac8286f4dc1256aa9c2f06..e53d5a87e24105d73c6f64dcaf0b012ddfa817e3 100644 (file)
@@ -39,6 +39,8 @@ class WinAudio;
 
 static const int WM_WINMUTE_QUIETHOURS_CHANGE = WM_APP + 200;
 static const int WM_WINMUTE_MUTE = WM_APP + 201;
+static const int WM_WINMUTE_QUIETHOURS_START = WM_APP + 202;
+static const int WM_WINMUTE_QUIETHOURS_END = WM_APP + 203;
 static const int QUIETHOURS_TIMER_START_ID = 271020;
 static const int QUIETHOURS_TIMER_END_ID = 271021;
 
@@ -71,12 +73,15 @@ private:
          bool onSuspend;
       } noRestore;
       struct {
-         int enabled;
+         bool enabled;
+         bool forceUnmute;
          time_t start;
          time_t end;
       } quietHours;
    } muteConfig_;
 
+   bool wsAlreadyMuted_;
+
    TrayIcon trayIcon_;
    std::unique_ptr<WinAudio> audio_;
    ScreensaverNotifier scrnSaverNoti_;
index e9a4b44861ca359f41345f25907af58d2d3c57a4..6484b8198b8786b637c1a0cf80473b08fb37d193 100644 (file)
@@ -96,19 +96,21 @@ BEGIN
                     "SysLink",WS_TABSTOP,119,35,36,13
 END
 
-IDD_QUIETHOURS DIALOGEX 0, 0, 201, 146
+IDD_QUIETHOURS DIALOGEX 0, 0, 201, 190
 STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
 CAPTION "Configure Quiet Hours"
 FONT 8, "MS Shell Dlg", 400, 0, 0x1
 BEGIN
-    CONTROL         "Enable Quiet Hours",IDC_ENABLEQUIETHOURS,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,15,38,77,10
+    CONTROL         "Enable Quiet Hours",IDC_ENABLEQUIETHOURS,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,15,42,77,10
     CONTROL         "",IDC_QUIETHOURS_START,"SysDateTimePick32",DTS_RIGHTALIGN | DTS_UPDOWN | WS_TABSTOP | 0x8,79,58,54,15
     CONTROL         "",IDC_QUIETHOURS_END,"SysDateTimePick32",DTS_RIGHTALIGN | DTS_UPDOWN | WS_TABSTOP | 0x8,79,80,54,15
-    DEFPUSHBUTTON   "Save",IDOK,63,120,50,14
-    PUSHBUTTON      "Cancel",IDCANCEL,123,120,50,14
-    LTEXT           "During Quiet Hours WinMute automatically mutes your PC volumes and unmutes it afterwards.",IDC_STATIC,15,9,174,24
+    CONTROL         "Force unmute",IDC_FORCEUNMUTE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,31,102,60,10
+    DEFPUSHBUTTON   "Save",IDOK,63,165,50,14
+    PUSHBUTTON      "Cancel",IDCANCEL,123,165,50,14
+    LTEXT           "If force unmute is enabled, WinMute will unmute your workstation when quiet hours end and not take into account if your workstation was already muted before quiet hours started.",IDC_STATIC,32,115,162,38
     LTEXT           "Start time:",IDC_STATIC,31,60,42,12
     LTEXT           "End time:",IDC_STATIC,31,82,43,12
+    LTEXT           "During Quiet Hours WinMute automatically mutes your PC volumes and unmutes it afterwards.",IDC_STATIC,14,15,180,24
 END
 
 
@@ -134,7 +136,7 @@ BEGIN
         LEFTMARGIN, 7
         RIGHTMARGIN, 194
         TOPMARGIN, 7
-        BOTTOMMARGIN, 139
+        BOTTOMMARGIN, 183
     END
 END
 #endif    // APSTUDIO_INVOKED
index 41774399f191b1651a9d6efd7be7e01a02bbffac..cd00b1fee7a7f4ec80adefdf85898071cb46f873 100644 (file)
@@ -14,6 +14,7 @@
 #define IDC_QUIETHOURS_START            1002
 #define IDC_ENABLEQUIETHOURS            1003
 #define IDC_QUIETHOURS_END              1004
+#define IDC_FORCEUNMUTE                 1005
 #define ID_TRAYMENU_INFO                40001
 #define ID_TRAYMENU_EXIT                40002
 #define ID_TRAYMENU_MUTEONLOCK          40003