]> repos.lx-s.de Git - winmute.git/commitdiff
Fixed time-range validation for over-nighters. Fixed crash.
authorlx-s <hello@lx-s.de>
Wed, 4 Nov 2020 13:20:41 +0000 (14:20 +0100)
committerlx-s <hello@lx-s.de>
Wed, 4 Nov 2020 13:20:41 +0000 (14:20 +0100)
WinMute/QuietHoursDlg.cpp
WinMute/WinMute.cpp
WinMute/res.rc

index d9a9138870ab2f2029cf781f52d6e85e8bcccb4a..64f724a560bf1db555a33f6a29cb3a9a341ee48c 100644 (file)
@@ -37,17 +37,11 @@ static bool IsValidTimeRange(
    const LPSYSTEMTIME start,
    const LPSYSTEMTIME end)
 {
-   bool isValid = false;
-   if (start->wHour == end->wHour) {
-      if (start->wMinute == end->wMinute) {
-         if (start->wSecond < end->wSecond) {
-            isValid = true;
-         }
-      } else if (start->wMinute < end->wMinute) {
-         isValid = true;
-      }
-   } else if (start->wHour < end->wHour) {
-      isValid = true;
+   bool isValid = true;
+   if (start->wHour == end->wHour &&
+       start->wMinute == end->wMinute &&
+       start->wSecond == end->wSecond) {
+      isValid = false;
    }
    return isValid;
 }
@@ -67,8 +61,8 @@ static bool SaveQuietHours(
             nullptr,
             PROGRAM_NAME,
             _T("Invalid time range"),
-            _T("Quiet hours must end before they start.\n")
-            _T("Please correct your input.\n"),
+            _T("Can't start and stop at the same time.\n")
+            _T("Please correct your time range.\n"),
             TDCBF_OK_BUTTON,
             TD_WARNING_ICON,
             nullptr);
index 544db290e7e1c89c317d4c2b8de7323c79a80630..c6fdf10d06e577622b7b6abddd3fdb1c278c5907 100644 (file)
@@ -193,14 +193,15 @@ bool WinMute::Init()
       return false;
    }
 
-   ResetQuietHours();
-
    hTrayIcon_ = LoadIcon(hglobInstance, MAKEINTRESOURCE(IDI_TRAY));
    if (hTrayIcon_ == NULL) {
       PrintWindowsError(_T("LoadIcon"));
       return false;
    }
    trayIcon_.Init(hWnd_, 0, hTrayIcon_, _T("WinMute"), true);
+
+   ResetQuietHours();
+
    return true;
 }
 
@@ -476,31 +477,31 @@ LRESULT CALLBACK WinMute::WindowProc(
    return DefWindowProc(hWnd, msg, wParam, lParam);
 }
 
+static __int64 ConvertSystemTimeTo100NS(const LPSYSTEMTIME sysTime)
+{
+   FILETIME ft;
+   SystemTimeToFileTime(sysTime, &ft);
+   ULARGE_INTEGER ulInt;
+   ulInt.HighPart = ft.dwHighDateTime;
+   ulInt.LowPart = ft.dwLowDateTime;
+   return static_cast<__int64>(ulInt.QuadPart);
+}
+
 static bool QuietHoursShouldAlreadyHaveStarted(
    const LPSYSTEMTIME now,
    const LPSYSTEMTIME qhStart,
    const LPSYSTEMTIME qhEnd)
 {
-   FILETIME ftNow;
-   FILETIME ftStart;
-   FILETIME ftEnd;
-   ULARGE_INTEGER ulNow;
-   ULARGE_INTEGER ulStart;
-   ULARGE_INTEGER ulEnd;
-
-   SystemTimeToFileTime(now, &ftNow);
-   SystemTimeToFileTime(qhStart, &ftStart);
-   SystemTimeToFileTime(qhEnd, &ftEnd);
-
-   ulNow.LowPart = ftNow.dwLowDateTime;
-   ulNow.HighPart = ftNow.dwHighDateTime;
-   ulStart.LowPart = ftStart.dwLowDateTime;
-   ulStart.HighPart = ftStart.dwHighDateTime;
-   ulEnd.LowPart = ftEnd.dwLowDateTime;
-   ulEnd.HighPart = ftEnd.dwHighDateTime;
-
-   if (ulEnd.QuadPart - ulNow.QuadPart > 0 &&
-        ulStart.QuadPart - ulNow.QuadPart < 0) {
+   __int64 iStart = ConvertSystemTimeTo100NS(qhStart);
+   __int64 iEnd = ConvertSystemTimeTo100NS(qhEnd);
+   __int64 iNow = ConvertSystemTimeTo100NS(now);
+
+   if (iEnd < iStart) {
+      // Add one day
+      iEnd += 864000000000ll;
+   }
+
+   if (iEnd - iNow > 0 && iStart - iNow < 0) {
       return true;
    }
 
@@ -513,19 +514,10 @@ static bool QuietHoursShouldAlreadyHaveStarted(
  */
 static int GetDiffMillseconds(const LPSYSTEMTIME t1, const LPSYSTEMTIME t2)
 {
-   FILETIME ftT1;
-   FILETIME ftT2;
-   ULARGE_INTEGER ulT1;
-   ULARGE_INTEGER ulT2;
-   __int64 res;
-   SystemTimeToFileTime(t1, &ftT1);
-   ulT1.LowPart = ftT1.dwLowDateTime;
-   ulT1.HighPart = ftT1.dwHighDateTime;
-   SystemTimeToFileTime(t2, &ftT2);
-   ulT2.LowPart = ftT2.dwLowDateTime;
-   ulT2.HighPart = ftT2.dwHighDateTime;
-
-   res = ulT1.QuadPart - ulT2.QuadPart;
+   __int64 it1 = ConvertSystemTimeTo100NS(t1);
+   __int64 it2 = ConvertSystemTimeTo100NS(t2);
+
+   __int64 res = it1 - it2;
 
    res /= (1000000000 / 100); // To seconds
    if (res < 0) { // Add 24 Hours for Wrap Around
index ea20a9ff5e300bd3239150174fa4021e53c3d79e..d3fff5a94bce227199265f70da2ebada6e573b4c 100644 (file)
@@ -166,7 +166,7 @@ BEGIN
         BLOCK "000004b0"
         BEGIN
             VALUE "CompanyName", "lx-systems"
-            VALUE "FileDescription", "Automatic volume muting."
+            VALUE "FileDescription", "WinMute"
             VALUE "FileVersion", "1.6.0.0"
             VALUE "InternalName", "WinMute"
             VALUE "LegalCopyright", "Copyright (c) 2020, Alexander Steinhöfer"
@@ -177,7 +177,7 @@ BEGIN
         BLOCK "040704b0"
         BEGIN
             VALUE "CompanyName", "lx-systems"
-            VALUE "FileDescription", "Automatisches Stummschalten des PCs."
+            VALUE "FileDescription", "WinMute"
             VALUE "FileVersion", "1.6.0.0"
             VALUE "InternalName", "WinMute"
             VALUE "LegalCopyright", "Copyright (c) 2020, Alexander Steinhöfer"