From 965ec8ec817d3c244970df561130f67b8376fe5e Mon Sep 17 00:00:00 2001 From: Eladash <18193363+elad335@users.noreply.github.com> Date: Thu, 11 Apr 2024 19:25:32 +0300 Subject: [PATCH] GUI/CLI: Add setting to attach command line (Win32) --- rpcs3/main.cpp | 26 ++++++++++++++++++++++++++ rpcs3/rpcs3qt/gui_application.cpp | 18 ++++++++++++++++++ rpcs3/rpcs3qt/gui_settings.h | 1 + 3 files changed, 45 insertions(+) diff --git a/rpcs3/main.cpp b/rpcs3/main.cpp index db823de91c..c4945d5e8c 100644 --- a/rpcs3/main.cpp +++ b/rpcs3/main.cpp @@ -323,6 +323,8 @@ constexpr auto arg_timer = "high-res-timer"; constexpr auto arg_verbose_curl = "verbose-curl"; constexpr auto arg_any_location = "allow-any-location"; constexpr auto arg_codecs = "codecs"; +constexpr auto arg_stdout = "stdout"; +constexpr auto arg_stderr = "stderr"; int find_arg(std::string arg, int& argc, char* argv[]) { @@ -705,6 +707,12 @@ int main(int argc, char** argv) parser.addOption(QCommandLineOption(arg_any_location, "Allow RPCS3 to be run from any location. Dangerous")); const QCommandLineOption codec_option(arg_codecs, "List ffmpeg codecs"); parser.addOption(codec_option); + +#ifdef _WIN32 + parser.addOption(QCommandLineOption(arg_stdout, "Attach the console window and listen to standard output stream. (STDOUT)")); + parser.addOption(QCommandLineOption(arg_stderr, "Attach the console window and listen to error output stream. (STDERR)")); +#endif + parser.process(app->arguments()); // Don't start up the full rpcs3 gui if we just want the version or help. @@ -733,6 +741,24 @@ int main(int argc, char** argv) return 0; } +#ifdef _WIN32 + if (parser.isSet(arg_stdout) || parser.isSet(arg_stderr)) + { + if (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole()) + { + if (parser.isSet(arg_stdout)) + { + [[maybe_unused]] const auto con_out = freopen("CONOUT$", "w", stdout); + } + + if (parser.isSet(arg_stderr)) + { + [[maybe_unused]] const auto con_err = freopen("CONOUT$", "w", stderr); + } + } + } +#endif + // Set curl to verbose if needed rpcs3::curl::g_curl_verbose = parser.isSet(arg_verbose_curl); diff --git a/rpcs3/rpcs3qt/gui_application.cpp b/rpcs3/rpcs3qt/gui_application.cpp index 82c3c61296..a334e929bf 100644 --- a/rpcs3/rpcs3qt/gui_application.cpp +++ b/rpcs3/rpcs3qt/gui_application.cpp @@ -51,6 +51,10 @@ #include "Emu/RSX/VK/VKGSRender.h" #endif +#ifdef _WIN32 +#include "Windows.h" +#endif + LOG_CHANNEL(gui_log, "GUI"); [[noreturn]] void report_fatal_error(std::string_view text, bool is_html = false, bool include_help_text = true); @@ -112,6 +116,20 @@ bool gui_application::Init() return false; } +#ifdef _WIN32 + if (m_gui_settings->GetValue(gui::m_attachCommandLine).toBool()) + { + if (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole()) + { + [[maybe_unused]] const auto con_out = freopen("CONOUT$", "w", stderr); + } + } + else +#endif + { + m_gui_settings->SetValue(gui::m_attachCommandLine, false); + } + // The user might be set by cli arg. If not, set another user. if (m_active_user.empty()) { diff --git a/rpcs3/rpcs3qt/gui_settings.h b/rpcs3/rpcs3qt/gui_settings.h index a4bab618e5..036a6321a0 100644 --- a/rpcs3/rpcs3qt/gui_settings.h +++ b/rpcs3/rpcs3qt/gui_settings.h @@ -215,6 +215,7 @@ namespace gui const gui_save m_currentStylesheet = gui_save(meta, "currentStylesheet", DefaultStylesheet); const gui_save m_showDebugTab = gui_save(meta, "showDebugTab", false); + const gui_save m_attachCommandLine = gui_save(meta, "attachCommandLine", false); const gui_save m_enableUIColors = gui_save(meta, "enableUIColors", false); const gui_save m_richPresence = gui_save(meta, "useRichPresence", true); const gui_save m_discordState = gui_save(meta, "discordState", "");