From c958a94ca68e9b17ddc5b1b5ff2236f0d07d36bf Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Fri, 16 Jul 2021 17:56:16 +0200 Subject: [PATCH] (Windows) Hiding of console windows during game launches can now be configured via the %HIDEWINDOW% variable. --- es-app/src/FileData.cpp | 20 +++++++++++++++++++- es-core/src/Platform.cpp | 12 +++++++----- es-core/src/Platform.h | 2 +- resources/systems/windows/es_systems.xml | 8 ++++---- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/es-app/src/FileData.cpp b/es-app/src/FileData.cpp index 25874c923..834cbaae3 100644 --- a/es-app/src/FileData.cpp +++ b/es-app/src/FileData.cpp @@ -766,6 +766,10 @@ void FileData::launchGame(Window* window) const std::string romRaw = Utils::FileSystem::getPreferredPath(getPath()); const std::string esPath = Utils::FileSystem::getExePath(); +#if defined(_WIN64) + bool hideWindow = false; +#endif + std::string coreEntry; std::string coreName; size_t coreEntryPos = 0; @@ -773,6 +777,20 @@ void FileData::launchGame(Window* window) bool foundCoreFile = false; std::vector emulatorCorePaths; +#if defined(_WIN64) + // If the %HIDEWINDOW% variable is defined, we pass a flag to launchGameWindows() to + // hide the window. This is intended primarily for hiding console windows when launching + // scripts (used for example by Steam games and source ports). + if (command.substr(0, 12) == "%HIDEWINDOW%") { + hideWindow = true; + command = Utils::String::replace(command, "%HIDEWINDOW%", ""); + // Trim any leading whitespaces as they could cause the script execution to fail. + command.erase(command.begin(), std::find_if(command.begin(), command.end(), [](char c) { + return !std::isspace(static_cast(c)); + })); + } +#endif + // If there's a quotation mark before the %CORE_ variable, then remove it. // The closing quotation mark will be removed later below. command = Utils::String::replace(command, "\"%CORE_", "%CORE_"); @@ -1025,7 +1043,7 @@ void FileData::launchGame(Window* window) #if defined(_WIN64) returnValue = launchGameWindows(Utils::String::stringToWideString(command), - ViewController::get()->runInBackground(mSystem)); + ViewController::get()->runInBackground(mSystem), hideWindow); #else returnValue = launchGameUnix(command, ViewController::get()->runInBackground(mSystem)); #endif diff --git a/es-core/src/Platform.cpp b/es-core/src/Platform.cpp index e301fd4be..e0a70729c 100644 --- a/es-core/src/Platform.cpp +++ b/es-core/src/Platform.cpp @@ -136,17 +136,19 @@ int launchGameUnix(const std::string& cmd_utf8, bool runInBackground) #endif } -int launchGameWindows(const std::wstring& cmd_utf16, bool runInBackground) +int launchGameWindows(const std::wstring& cmd_utf16, bool runInBackground, bool hideWindow) { #if defined(_WIN64) STARTUPINFOW si {}; PROCESS_INFORMATION pi; si.cb = sizeof(si); - // The console window is hidden by default when compiled with MSVC, but when - // using MinGW it has to be set explicitly. - si.dwFlags = STARTF_USESHOWWINDOW; - si.wShowWindow = SW_HIDE; + if (hideWindow) { + // Optionally hide the window. This is intended primarily for hiding console windows when + // launching scripts (used for example by Steam games and source ports). + si.dwFlags = STARTF_USESHOWWINDOW; + si.wShowWindow = SW_HIDE; + } bool processReturnValue = true; DWORD errorCode = 0; diff --git a/es-core/src/Platform.h b/es-core/src/Platform.h index c32c82c3f..3cd8971ea 100644 --- a/es-core/src/Platform.h +++ b/es-core/src/Platform.h @@ -29,7 +29,7 @@ int runSystemCommand(const std::string& cmd_utf8); int runSystemCommand(const std::wstring& cmd_utf16); int launchGameUnix(const std::string& cmd_utf8, bool runInBackground); -int launchGameWindows(const std::wstring& cmd_utf16, bool runInBackground); +int launchGameWindows(const std::wstring& cmd_utf16, bool runInBackground, bool hideWindow); unsigned int getTaskbarState(); void hideTaskbar(); diff --git a/resources/systems/windows/es_systems.xml b/resources/systems/windows/es_systems.xml index 8f2424a1c..772e8b119 100644 --- a/resources/systems/windows/es_systems.xml +++ b/resources/systems/windows/es_systems.xml @@ -285,7 +285,7 @@ Desktop applications %ROMPATH%\desktop .bat - cmd.exe /C %ROM% + %HIDEWINDOW% cmd.exe /C %ROM% pc desktop @@ -447,7 +447,7 @@ Kodi home theatre software %ROMPATH%\kodi .bat - cmd.exe /C %ROM% + %HIDEWINDOW% cmd.exe /C %ROM% pc kodi @@ -816,7 +816,7 @@ Ports %ROMPATH%\ports .bat - cmd.exe /C %ROM% + %HIDEWINDOW% cmd.exe /C %ROM% pc ports @@ -1014,7 +1014,7 @@ Valve Steam %ROMPATH%\steam .bat - cmd.exe /C %ROM% + %HIDEWINDOW% cmd.exe /C %ROM% steam steam