From 48a137b544bc5028959ef8a02c5f3ef1a58d1c26 Mon Sep 17 00:00:00 2001 From: lx-s Date: Wed, 4 Nov 2020 14:20:41 +0100 Subject: [PATCH] Fixed time-range validation for over-nighters. Fixed crash. --- WinMute/QuietHoursDlg.cpp | 20 +++++-------- WinMute/WinMute.cpp | 62 +++++++++++++++++---------------------- WinMute/res.rc | 4 +-- 3 files changed, 36 insertions(+), 50 deletions(-) diff --git a/WinMute/QuietHoursDlg.cpp b/WinMute/QuietHoursDlg.cpp index d9a9138..64f724a 100644 --- a/WinMute/QuietHoursDlg.cpp +++ b/WinMute/QuietHoursDlg.cpp @@ -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); diff --git a/WinMute/WinMute.cpp b/WinMute/WinMute.cpp index 544db29..c6fdf10 100644 --- a/WinMute/WinMute.cpp +++ b/WinMute/WinMute.cpp @@ -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 diff --git a/WinMute/res.rc b/WinMute/res.rc index ea20a9f..d3fff5a 100644 --- a/WinMute/res.rc +++ b/WinMute/res.rc @@ -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" -- 2.43.0