From 5f64b205cf146231336d770d4bb6410a554520e9 Mon Sep 17 00:00:00 2001 From: casey langen Date: Sun, 25 Dec 2016 20:44:18 -0800 Subject: [PATCH] Hide the main window immediately on exit in the Win32 build. This allows us to cross fade the audio out without appearing to be frozen. --- src/contrib/directsoundout/DirectSoundOut.cpp | 5 +- src/musikbox/Main.cpp | 9 +- src/musikbox/app/util/Win32Util.cpp | 86 +++++++++++++++++++ src/musikbox/app/util/Win32Util.h | 50 +++++++++++ src/musikbox/musikbox.vcxproj | 2 + src/musikbox/musikbox.vcxproj.filters | 6 ++ 6 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 src/musikbox/app/util/Win32Util.cpp create mode 100644 src/musikbox/app/util/Win32Util.h diff --git a/src/contrib/directsoundout/DirectSoundOut.cpp b/src/contrib/directsoundout/DirectSoundOut.cpp index 8f7b5c112..edc7cffaf 100644 --- a/src/contrib/directsoundout/DirectSoundOut.cpp +++ b/src/contrib/directsoundout/DirectSoundOut.cpp @@ -165,7 +165,7 @@ void DirectSoundOut::SetVolume(double volume) { if (this->secondaryBuffer) { double db = (volume < 0.0001f) ? DSBVOLUME_MIN - : (float) log10f(this->volume) * 6000.f; + : log10(this->volume) * 6000.f; if (db > DSBVOLUME_MAX) { db = DSBVOLUME_MAX; @@ -264,6 +264,7 @@ bool DirectSoundOut::Play(IBuffer *buffer, IBufferProvider *provider) { void DirectSoundOut::Drain() { static const int drainCount = 4; + static const int bufferResendDelayMs = 50; int channels = this->channels; int rate = this->rate; @@ -286,7 +287,7 @@ void DirectSoundOut::Drain() { int count = drainCount + 1; while (count > 0 && this->state != StateStopped) { if (!this->Play(&buffer, &buffer)) { - Sleep(250); /* eh */ + Sleep(bufferResendDelayMs); /* eh */ } else { --count; diff --git a/src/musikbox/Main.cpp b/src/musikbox/Main.cpp index de972dfc6..deef7e948 100644 --- a/src/musikbox/Main.cpp +++ b/src/musikbox/Main.cpp @@ -50,6 +50,7 @@ #include #include +#include #include #include @@ -60,6 +61,7 @@ #include #ifdef WIN32 +#include #undef MOUSE_MOVED #endif @@ -108,7 +110,8 @@ int main(int argc, char* argv[]) LibraryPtr library = LibraryFactory::Libraries().at(0); - GaplessTransport transport; + //GaplessTransport transport; + CrossfadeTransport transport; PlaybackService playback(library, transport); GlobalHotkeys globalHotkeys(playback, library); @@ -157,6 +160,10 @@ int main(int argc, char* argv[]) }); app.Run(mainLayout); + +#ifdef WIN32 + musik::box::win32::HideMainWindow(); +#endif } musik::core::audio::vis::HideSelectedVisualizer(); diff --git a/src/musikbox/app/util/Win32Util.cpp b/src/musikbox/app/util/Win32Util.cpp new file mode 100644 index 000000000..364dcdc5f --- /dev/null +++ b/src/musikbox/app/util/Win32Util.cpp @@ -0,0 +1,86 @@ +////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2007-2016 musikcube team +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// * Neither the name of the author nor the names of other contributors may +// be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +////////////////////////////////////////////////////////////////////////////// + +#pragma once + +#include "stdafx.h" +#include + +#ifdef WIN32 + +static std::basic_string className = "Curses_App"; +static HWND mainWindow = nullptr; + +static void findMainWindow() { + static TCHAR buffer[256]; + + if (mainWindow == nullptr) { + DWORD dwProcID = GetCurrentProcessId(); + HWND hWnd = GetTopWindow(GetDesktopWindow()); + while (hWnd) { + DWORD dwWndProcID = 0; + GetWindowThreadProcessId(hWnd, &dwWndProcID); + if (dwWndProcID == dwProcID) { + GetClassName(hWnd, buffer, sizeof(buffer)); + if (className == std::string(buffer)) { + mainWindow = hWnd; + return; + } + } + hWnd = GetNextWindow(hWnd, GW_HWNDNEXT); + } + } +} + +namespace musik { + namespace box { + namespace win32 { + void ShowMainWindow() { + findMainWindow(); + if (mainWindow) { + ShowWindow(mainWindow, SW_SHOWNORMAL); + } + } + + void HideMainWindow() { + findMainWindow(); + if (mainWindow) { + ShowWindow(mainWindow, SW_HIDE); + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/musikbox/app/util/Win32Util.h b/src/musikbox/app/util/Win32Util.h new file mode 100644 index 000000000..14f9e18b1 --- /dev/null +++ b/src/musikbox/app/util/Win32Util.h @@ -0,0 +1,50 @@ +////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2007-2016 musikcube team +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// * Neither the name of the author nor the names of other contributors may +// be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +////////////////////////////////////////////////////////////////////////////// + +#pragma once + +#include "stdafx.h" + +#ifdef WIN32 + +namespace musik { + namespace box { + namespace win32 { + void ShowMainWindow(); + void HideMainWindow(); + } + } +} + +#endif \ No newline at end of file diff --git a/src/musikbox/musikbox.vcxproj b/src/musikbox/musikbox.vcxproj index 83bc5d8e3..08682f30c 100755 --- a/src/musikbox/musikbox.vcxproj +++ b/src/musikbox/musikbox.vcxproj @@ -141,6 +141,7 @@ + @@ -198,6 +199,7 @@ + diff --git a/src/musikbox/musikbox.vcxproj.filters b/src/musikbox/musikbox.vcxproj.filters index bf9ffe8ed..0abdeac88 100755 --- a/src/musikbox/musikbox.vcxproj.filters +++ b/src/musikbox/musikbox.vcxproj.filters @@ -144,6 +144,9 @@ app\overlay + + app\util + @@ -336,6 +339,9 @@ cursespp + + app\util +