From 223172ac0babc7dc272a5489ac2e91062a12a0c2 Mon Sep 17 00:00:00 2001 From: merry Date: Tue, 15 Mar 2022 01:35:41 +0000 Subject: [PATCH] Ui: Add option to show/hide console window (Windows-only) (#3170) * Ui: Add option to show/hide console window (Windows-only) * Ui: Only display Show Console menu item on Windows * ConsoleHelper: Handle NULL case This will never happen * Address nits * Address comments * Address comments 2 --- .../Configuration/ConfigurationFileFormat.cs | 7 ++- Ryujinx/Configuration/ConfigurationState.cs | 24 ++++++++- Ryujinx/Ui/Helper/ConsoleHelper.cs | 49 +++++++++++++++++++ Ryujinx/Ui/MainWindow.cs | 11 +++++ Ryujinx/Ui/MainWindow.glade | 9 ++++ 5 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 Ryujinx/Ui/Helper/ConsoleHelper.cs diff --git a/Ryujinx/Configuration/ConfigurationFileFormat.cs b/Ryujinx/Configuration/ConfigurationFileFormat.cs index 8e64aa081f..27b0c36296 100644 --- a/Ryujinx/Configuration/ConfigurationFileFormat.cs +++ b/Ryujinx/Configuration/ConfigurationFileFormat.cs @@ -14,7 +14,7 @@ namespace Ryujinx.Configuration /// /// The current version of the file format /// - public const int CurrentVersion = 36; + public const int CurrentVersion = 37; /// /// Version of the configuration file format @@ -236,6 +236,11 @@ namespace Ryujinx.Configuration /// public bool StartFullscreen { get; set; } + /// + /// Show console window + /// + public bool ShowConsole { get; set; } + /// /// Enable or disable keyboard support (Independent from controllers binding) /// diff --git a/Ryujinx/Configuration/ConfigurationState.cs b/Ryujinx/Configuration/ConfigurationState.cs index 0b495ab766..1200fb528c 100644 --- a/Ryujinx/Configuration/ConfigurationState.cs +++ b/Ryujinx/Configuration/ConfigurationState.cs @@ -6,6 +6,7 @@ using Ryujinx.Common.Configuration.Hid.Keyboard; using Ryujinx.Common.Logging; using Ryujinx.Configuration.System; using Ryujinx.Configuration.Ui; +using Ryujinx.Ui.Helper; using System; using System.Collections.Generic; @@ -88,6 +89,11 @@ namespace Ryujinx.Configuration /// public ReactiveObject StartFullscreen { get; private set; } + /// + /// Hide / Show Console Window + /// + public ReactiveObject ShowConsole { get; private set; } + public UiSection() { GuiColumns = new Columns(); @@ -96,6 +102,8 @@ namespace Ryujinx.Configuration EnableCustomTheme = new ReactiveObject(); CustomThemePath = new ReactiveObject(); StartFullscreen = new ReactiveObject(); + ShowConsole = new ReactiveObject(); + ShowConsole.Event += static (s, e) => { ConsoleHelper.SetConsoleWindowState(e.NewValue); }; } } @@ -508,6 +516,7 @@ namespace Ryujinx.Configuration EnableCustomTheme = Ui.EnableCustomTheme, CustomThemePath = Ui.CustomThemePath, StartFullscreen = Ui.StartFullscreen, + ShowConsole = Ui.ShowConsole, EnableKeyboard = Hid.EnableKeyboard, EnableMouse = Hid.EnableMouse, Hotkeys = Hid.Hotkeys, @@ -574,6 +583,7 @@ namespace Ryujinx.Configuration Ui.EnableCustomTheme.Value = false; Ui.CustomThemePath.Value = ""; Ui.StartFullscreen.Value = false; + Ui.ShowConsole.Value = true; Hid.EnableKeyboard.Value = false; Hid.EnableMouse.Value = false; Hid.Hotkeys.Value = new KeyboardHotkeys @@ -995,7 +1005,7 @@ namespace Ryujinx.Configuration controllerConfig.RangeRight = 1.0f; } } - + configurationFileUpdated = true; } @@ -1007,7 +1017,16 @@ namespace Ryujinx.Configuration configurationFileUpdated = true; } - + + if (configurationFileFormat.Version < 37) + { + Common.Logging.Logger.Warning?.Print(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 37."); + + configurationFileFormat.ShowConsole = true; + + configurationFileUpdated = true; + } + Logger.EnableFileLog.Value = configurationFileFormat.EnableFileLog; Graphics.BackendThreading.Value = configurationFileFormat.BackendThreading; Graphics.ResScale.Value = configurationFileFormat.ResScale; @@ -1061,6 +1080,7 @@ namespace Ryujinx.Configuration Ui.EnableCustomTheme.Value = configurationFileFormat.EnableCustomTheme; Ui.CustomThemePath.Value = configurationFileFormat.CustomThemePath; Ui.StartFullscreen.Value = configurationFileFormat.StartFullscreen; + Ui.ShowConsole.Value = configurationFileFormat.ShowConsole; Hid.EnableKeyboard.Value = configurationFileFormat.EnableKeyboard; Hid.EnableMouse.Value = configurationFileFormat.EnableMouse; Hid.Hotkeys.Value = configurationFileFormat.Hotkeys; diff --git a/Ryujinx/Ui/Helper/ConsoleHelper.cs b/Ryujinx/Ui/Helper/ConsoleHelper.cs new file mode 100644 index 0000000000..6f89df02e9 --- /dev/null +++ b/Ryujinx/Ui/Helper/ConsoleHelper.cs @@ -0,0 +1,49 @@ +using Ryujinx.Common.Logging; +using System; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; + +namespace Ryujinx.Ui.Helper +{ + public static class ConsoleHelper + { + public static bool SetConsoleWindowStateSupported => OperatingSystem.IsWindows(); + + public static void SetConsoleWindowState(bool show) + { + if (OperatingSystem.IsWindows()) + { + SetConsoleWindowStateWindows(show); + } + else if (show == false) + { + Logger.Warning?.Print(LogClass.Application, "OS doesn't support hiding console window"); + } + } + + [SupportedOSPlatform("windows")] + private static void SetConsoleWindowStateWindows(bool show) + { + const int SW_HIDE = 0; + const int SW_SHOW = 5; + + IntPtr hWnd = GetConsoleWindow(); + + if (hWnd == IntPtr.Zero) + { + Logger.Warning?.Print(LogClass.Application, "Attempted to show/hide console window but console window does not exist"); + return; + } + + ShowWindow(hWnd, show ? SW_SHOW : SW_HIDE); + } + + [SupportedOSPlatform("windows")] + [DllImport("kernel32")] + static extern IntPtr GetConsoleWindow(); + + [SupportedOSPlatform("windows")] + [DllImport("user32")] + static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); + } +} \ No newline at end of file diff --git a/Ryujinx/Ui/MainWindow.cs b/Ryujinx/Ui/MainWindow.cs index 5ab3cf61a1..32e66ee4e3 100644 --- a/Ryujinx/Ui/MainWindow.cs +++ b/Ryujinx/Ui/MainWindow.cs @@ -107,6 +107,7 @@ namespace Ryujinx.Ui [GUI] MenuItem _hideUi; [GUI] MenuItem _fullScreen; [GUI] CheckMenuItem _startFullScreen; + [GUI] CheckMenuItem _showConsole; [GUI] CheckMenuItem _favToggle; [GUI] MenuItem _firmwareInstallDirectory; [GUI] MenuItem _firmwareInstallFile; @@ -213,6 +214,9 @@ namespace Ryujinx.Ui _startFullScreen.Active = true; } + _showConsole.Active = ConfigurationState.Instance.Ui.ShowConsole.Value; + _showConsole.Visible = ConsoleHelper.SetConsoleWindowStateSupported; + _actionMenu.Sensitive = false; _pauseEmulation.Sensitive = false; _resumeEmulation.Sensitive = false; @@ -1535,6 +1539,13 @@ namespace Ryujinx.Ui SaveConfig(); } + private void ShowConsole_Toggled(object sender, EventArgs args) + { + ConfigurationState.Instance.Ui.ShowConsole.Value = _showConsole.Active; + + SaveConfig(); + } + private void OptionMenu_StateChanged(object o, StateChangedArgs args) { _manageUserProfiles.Sensitive = _emulationContext == null; diff --git a/Ryujinx/Ui/MainWindow.glade b/Ryujinx/Ui/MainWindow.glade index 595786a325..b7d93e9bda 100644 --- a/Ryujinx/Ui/MainWindow.glade +++ b/Ryujinx/Ui/MainWindow.glade @@ -142,6 +142,15 @@ + + + True + False + Show Log Console + True + + + True