From 6c12ba1a8dd45769cda44df5a227a734bf7a75e0 Mon Sep 17 00:00:00 2001 From: lx-s Date: Fri, 8 Apr 2022 07:29:51 +0200 Subject: [PATCH] Removed std::format to reduce file size by about 130kb. --- WinMute/AboutDlg.cpp | 14 ++++++++++++-- WinMute/MuteControl.cpp | 10 +++++----- WinMute/SettingsDlg.cpp | 4 ++-- WinMute/VistaAudio.cpp | 33 +++++++++++++++++---------------- WinMute/WMLog.cpp | 18 +++++++++++++++--- WinMute/WMLog.h | 22 ++-------------------- WinMute/WinMute.cpp | 14 +++++++++----- WinMute/WinMute.vcxproj | 2 +- WinMute/common.h | 2 +- 9 files changed, 64 insertions(+), 55 deletions(-) diff --git a/WinMute/AboutDlg.cpp b/WinMute/AboutDlg.cpp index e5bef64..d2da303 100644 --- a/WinMute/AboutDlg.cpp +++ b/WinMute/AboutDlg.cpp @@ -195,11 +195,21 @@ static bool GetWinMuteVersion(tstring& versNumber) _T("\\"), reinterpret_cast(&pvi), &pviLen)) { - versNumber = std::format(_T("{}.{}.{}.{}"), + TCHAR buf[30]; + +#ifdef _UNICODE + swprintf_s( +#else + sprintf_s( +#endif + buf, + ARRAY_SIZE(buf), + _T("%d.%d.%d.%d"), pvi->dwProductVersionMS >> 16, pvi->dwFileVersionMS & 0xFFFF, pvi->dwFileVersionLS >> 16, pvi->dwFileVersionLS & 0xFFFF); + versNumber = buf; success = true; } } @@ -246,7 +256,7 @@ INT_PTR CALLBACK AboutDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) tstring progName = _T("WinMute"); tstring progVers = _T("WinMute"); if (GetWinMuteVersion(progVers)) { - progName = std::format(_T("WinMute {}"), progVers); + progName = tstring(_T("WinMute")) + progVers; Static_SetText(hTitle, progName.c_str()); } diff --git a/WinMute/MuteControl.cpp b/WinMute/MuteControl.cpp index 6c46fda..3bc0bd8 100644 --- a/WinMute/MuteControl.cpp +++ b/WinMute/MuteControl.cpp @@ -74,7 +74,7 @@ bool MuteControl::Init(HWND hParent, const TrayIcon *trayIcon) void MuteControl::SetMute(bool mute) { - WMLog::GetInstance().Write(_T("Manual muting: {}"), mute ? _T("on") : _T("off")); + WMLog::GetInstance().Write(_T("Manual muting: %s"), mute ? _T("on") : _T("off")); winAudio_->SetMute(mute); } @@ -230,21 +230,21 @@ void MuteControl::NotifyRestoreCondition(int type, bool active) void MuteControl::NotifyWorkstationLock(bool active) { - WMLog::GetInstance().Write(_T("Mute Event: Workstation Lock {}"), + WMLog::GetInstance().Write(_T("Mute Event: Workstation Lock %s"), active ? _T("start") : _T("stop")); NotifyRestoreCondition(MuteTypeWorkstationLock, active); } void MuteControl::NotifyScreensaver(bool active) { - WMLog::GetInstance().Write(_T("Mute Event: Screensaver {}"), + WMLog::GetInstance().Write(_T("Mute Event: Screensaver %s"), active ? _T("start") : _T("stop")); NotifyRestoreCondition(MuteTypeScreensaverActive, active); } void MuteControl::NotifyRemoteSession(bool active) { - WMLog::GetInstance().Write(_T("Mute Event: Remote Session {}"), + WMLog::GetInstance().Write(_T("Mute Event: Remote Session %s"), active ? _T("start") : _T("stop")); NotifyRestoreCondition(MuteTypeRemoteSession, active); } @@ -252,7 +252,7 @@ void MuteControl::NotifyRemoteSession(bool active) void MuteControl::NotifyDisplayStandby(bool active) { if (displayWasOffOnce_ || active) { - WMLog::GetInstance().Write(_T("Mute Event: Display Standby {}"), + WMLog::GetInstance().Write(_T("Mute Event: Display Standby %s"), active ? _T("start") : _T("stop")); NotifyRestoreCondition(MuteTypeDisplayStandby, active); displayWasOffOnce_ = true; diff --git a/WinMute/SettingsDlg.cpp b/WinMute/SettingsDlg.cpp index 6ccd061..a41a7b4 100644 --- a/WinMute/SettingsDlg.cpp +++ b/WinMute/SettingsDlg.cpp @@ -123,10 +123,10 @@ INT_PTR CALLBACK SettingsDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lPar ResizeTab(dlgData->hTabCtrl, hCurTab); } - HICON hIcon = LoadIcon( + /*HICON hIcon = LoadIcon( GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_SETTINGS)); - SendMessageW(hDlg, WM_SETICON, ICON_BIG, reinterpret_cast(hIcon)); + SendMessageW(hDlg, WM_SETICON, ICON_BIG, reinterpret_cast(hIcon));*/ SwitchTab(dlgData, dlgData->hTabs[SETTINGS_TAB_GENERAL]); return TRUE; diff --git a/WinMute/VistaAudio.cpp b/WinMute/VistaAudio.cpp index 8518525..0b5aa2a 100644 --- a/WinMute/VistaAudio.cpp +++ b/WinMute/VistaAudio.cpp @@ -99,13 +99,13 @@ bool VistaAudio::LoadAllEndpoints() hr = audioEndpoints->Item(i, &device); if (FAILED(hr)) { - log.Write(_T("Failed to get audio endpoint #{}"), i); + log.Write(_T("Failed to get audio endpoint #%d"), i); continue; } hr = device->OpenPropertyStore(STGM_READ, &propStore); if (FAILED(hr)) { - log.Write(_T("Failed to open property store for audio endpoint #{}"), i); + log.Write(_T("Failed to open property store for audio endpoint #%d"), i); continue; } @@ -113,10 +113,10 @@ bool VistaAudio::LoadAllEndpoints() PropVariantInit(&value); hr = propStore->GetValue(PKEY_Device_FriendlyName, &value); if (FAILED(hr)) { - log.Write(_T("Failed to get device name for audio endpoint #{}"), i); + log.Write(_T("Failed to get device name for audio endpoint #%d"), i); continue; } - log.Write(_T("Found audio endpoint \"{}\""), value.pwszVal); + log.Write(_T("Found audio endpoint \"%s\""), value.pwszVal); StringCchCopy(ep->deviceName, sizeof(ep->deviceName)/sizeof(ep->deviceName[0]), value.pwszVal); @@ -127,14 +127,14 @@ bool VistaAudio::LoadAllEndpoints() if (FAILED(device->Activate( __uuidof(IAudioSessionManager2), CLSCTX_INPROC_SERVER, nullptr, reinterpret_cast(&sessionManager2)))) { - log.Write(_T("Failed to retrieve audio session manager for {}"), + log.Write(_T("Failed to retrieve audio session manager for \"%s\""), ep->deviceName); continue; } hr = sessionManager2->GetAudioSessionControl(nullptr, 0, &ep->sessionCtrl); if (FAILED(hr)) { - log.Write(_T("Failed to retrieve audio session manager for {}"), + log.Write(_T("Failed to retrieve audio session manager for \"%s\""), ep->deviceName); continue; } @@ -149,7 +149,7 @@ bool VistaAudio::LoadAllEndpoints() __uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, nullptr, reinterpret_cast(&ep->endpointVolume)); if (FAILED(hr)) { - log.Write(_T("Failed to active endpoint volume for device {}"), + log.Write(_T("Failed to active endpoint volume for device \"%s\""), ep->deviceName); continue; } @@ -221,7 +221,7 @@ bool VistaAudio::AllEndpointsMuted() for (auto& e : endpoints_) { BOOL isMuted = FALSE; if (FAILED(e->endpointVolume->GetMute(&isMuted))) { - log.Write(_T("Failed to get mute status for \"{}\""), + log.Write(_T("Failed to get mute status for \"%s\""), e->deviceName); allMuted = false; break; @@ -243,7 +243,7 @@ bool VistaAudio::SaveMuteStatus() for (auto& e : endpoints_) { BOOL isMuted = FALSE; if (FAILED(e->endpointVolume->GetMute(&isMuted))) { - log.Write(_T("Failed to get mute status for \"{}\""), + log.Write(_T("Failed to get mute status for \"%s\""), e->deviceName); success = false; } else { @@ -260,12 +260,13 @@ bool VistaAudio::RestoreMuteStatus() WMLog& log = WMLog::GetInstance(); for (auto& e : endpoints_) { - log.Write(_T("Restoring: Mute {} for \"{}\""), - e->wasMuted, e->deviceName); + log.Write(_T("Restoring: Mute %s for \"%s\""), + (e->wasMuted) ? _T("true") : _T("false"), + e->deviceName); if (e->wasMuted != true) { if (FAILED(e->endpointVolume->SetMute(false, nullptr))) { - log.Write(_T("Failed to restore mute status to {} for \"{}\""), - e->wasMuted, + log.Write(_T("Failed to restore mute status to %s for \"%s\""), + (e->wasMuted) ? _T("true") : _T("false"), e->deviceName); success = false; } @@ -282,13 +283,13 @@ void VistaAudio::SetMute(bool mute) for (auto& e : endpoints_) { BOOL isMuted = !mute; if (FAILED(e->endpointVolume->GetMute(&isMuted))) { - log.Write(_T("Failed to get mute status for \"{}\""), + log.Write(_T("Failed to get mute status for \"%s\""), e->deviceName); } if (!!isMuted != mute) { if (FAILED(e->endpointVolume->SetMute(mute, nullptr))) { - log.Write(_T("Failed to set mute status to {} for \"{}\""), - e->wasMuted, + log.Write(_T("Failed to set mute status to %s for \"%s\""), + (e->wasMuted) ? _T("true") : _T("false"), e->deviceName); } } diff --git a/WinMute/WMLog.cpp b/WinMute/WMLog.cpp index 1a31ddc..22c2748 100644 --- a/WinMute/WMLog.cpp +++ b/WinMute/WMLog.cpp @@ -76,7 +76,7 @@ void WMLog::SetEnabled(bool enable) enabled_ = enable; } -void WMLog::WriteMessage(const tstring& msg) +void WMLog::WriteMessage(const TCHAR* msg) { struct tm tm; auto now = std::chrono::system_clock::now(); @@ -95,10 +95,22 @@ void WMLog::WriteMessage(const tstring& msg) logFile_.flush(); } -void WMLog::Write(const tstring& msg) +void WMLog::Write(const TCHAR *fmt, ...) { if (!initialized_ || !enabled_) { return; } - WriteMessage(msg); + + TCHAR buf[200]; + va_list ap; + va_start(ap, fmt); + +#ifdef _UNICODE + vswprintf_s(buf, fmt, ap); +#else + snprintf(buf, ARRAY_SIZE(buf), fmt, ap); +#endif + WriteMessage(buf); + + va_end(ap); } diff --git a/WinMute/WMLog.h b/WinMute/WMLog.h index 20016b9..ab173dc 100644 --- a/WinMute/WMLog.h +++ b/WinMute/WMLog.h @@ -39,25 +39,7 @@ class WMLog { public: static WMLog& GetInstance(); - void Write(const tstring& wmsg); - - template - void Write( - [[maybe_unused]] const tstring_view& fmt, - [[maybe_unused]] Args&&... args) - { - if (!initialized_ || !enabled_) { - return; - } -#ifdef UNICODE - const std::wstring str = std::vformat( - fmt, std::make_wformat_args(args...)); -#else - const std::wstring str = std::vformat( - fmt, std::make_format_args(args...)); -#endif - WriteMessage(str); - } + void Write(const TCHAR* fmt, ...); void SetEnabled(bool enable); std::string GetLogFilePath(); @@ -68,7 +50,7 @@ private: WMLog(const WMLog&) = delete; WMLog& operator=(const WMLog&) = delete; - void WriteMessage(const tstring& msg); + void WriteMessage(const TCHAR* msg); bool initialized_; bool enabled_; diff --git a/WinMute/WinMute.cpp b/WinMute/WinMute.cpp index 81a4882..d5371e7 100644 --- a/WinMute/WinMute.cpp +++ b/WinMute/WinMute.cpp @@ -191,11 +191,7 @@ bool WinMute::Init() hAppIcon_ = LoadIcon(hglobInstance, MAKEINTRESOURCE(IDI_APP)); - if (!InitAudio()) { - return false; - } - - if (!settings_.Init() || !LoadDefaults()) { + if (!settings_.Init()) { return false; } @@ -206,6 +202,14 @@ bool WinMute::Init() #endif log.Write(_T("Starting new session...")); + if (!InitAudio()) { + return false; + } + + if (!LoadDefaults()) { + return false; + } + if (!RegisterWindowClass() || !InitWindow() || !InitTrayMenu()) { diff --git a/WinMute/WinMute.vcxproj b/WinMute/WinMute.vcxproj index 2bc5a7b..b23e2cf 100644 --- a/WinMute/WinMute.vcxproj +++ b/WinMute/WinMute.vcxproj @@ -148,7 +148,7 @@ true _DEBUG;_WINDOWS;%(PreprocessorDefinitions) true - stdcpplatest + stdcpp20 true Caret Use diff --git a/WinMute/common.h b/WinMute/common.h index 0ab3349..3150072 100644 --- a/WinMute/common.h +++ b/WinMute/common.h @@ -54,13 +54,13 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include #include #include #include #include #include #include +#include #include #include -- 2.43.0