From 03ffd6451ac23975ed9446a6ffb4a7cbbdd39e62 Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Mon, 17 Aug 2020 19:05:48 +0300 Subject: [PATCH 1/2] Revert "[Windows] Attach to parent console instead of creating new one." This reverts commit 4f7a49db53c6aaabeca70fe8901144af708fb6b2. --- editor/editor_node.cpp | 1 + editor/editor_settings.cpp | 1 + main/main.cpp | 7 ++++++ platform/windows/detect.py | 9 +++---- platform/windows/display_server_windows.cpp | 26 ++++----------------- platform/windows/display_server_windows.h | 1 - 6 files changed, 18 insertions(+), 27 deletions(-) diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 381ff888909..ed187aa6df2 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -2624,6 +2624,7 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) { case SETTINGS_TOGGLE_CONSOLE: { bool was_visible = DisplayServer::get_singleton()->is_console_visible(); DisplayServer::get_singleton()->console_set_visible(!was_visible); + EditorSettings::get_singleton()->set_setting("interface/editor/hide_console_window", was_visible); } break; case EDITOR_SCREENSHOT: { screenshot_timer->start(); diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp index 7b24e6967ad..561ef51f0ac 100644 --- a/editor/editor_settings.cpp +++ b/editor/editor_settings.cpp @@ -334,6 +334,7 @@ void EditorSettings::_load_defaults(Ref p_extra_config) { _initial_set("interface/editor/automatically_open_screenshots", true); _initial_set("interface/editor/single_window_mode", false); hints["interface/editor/single_window_mode"] = PropertyInfo(Variant::BOOL, "interface/editor/single_window_mode", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED); + _initial_set("interface/editor/hide_console_window", false); _initial_set("interface/editor/save_each_scene_on_quit", true); // Regression _initial_set("interface/editor/quit_confirmation", true); diff --git a/main/main.cpp b/main/main.cpp index e45162c0f34..413af9017b8 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -2278,6 +2278,13 @@ bool Main::start() { } if (project_manager || editor) { + if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CONSOLE_WINDOW)) { + // Hide console window if requested (Windows-only). + bool hide_console = EditorSettings::get_singleton()->get_setting( + "interface/editor/hide_console_window"); + DisplayServer::get_singleton()->console_set_visible(!hide_console); + } + // Load SSL Certificates from Editor Settings (or builtin) Crypto::load_default_certificates(EditorSettings::get_singleton()->get_setting( "network/ssl/editor_ssl_certificates") diff --git a/platform/windows/detect.py b/platform/windows/detect.py index a9f25fa0785..271ffc88718 100644 --- a/platform/windows/detect.py +++ b/platform/windows/detect.py @@ -183,6 +183,7 @@ def configure_msvc(env, manual_msvc_config): env.Append(CCFLAGS=["/O2"]) else: # optimize for size env.Append(CCFLAGS=["/O1"]) + env.Append(LINKFLAGS=["/SUBSYSTEM:WINDOWS"]) env.Append(LINKFLAGS=["/ENTRY:mainCRTStartup"]) env.Append(LINKFLAGS=["/OPT:REF"]) @@ -192,15 +193,15 @@ def configure_msvc(env, manual_msvc_config): else: # optimize for size env.Append(CCFLAGS=["/O1"]) env.AppendUnique(CPPDEFINES=["DEBUG_ENABLED"]) + env.Append(LINKFLAGS=["/SUBSYSTEM:CONSOLE"]) env.Append(LINKFLAGS=["/OPT:REF"]) elif env["target"] == "debug": env.AppendUnique(CCFLAGS=["/Z7", "/Od", "/EHsc"]) env.AppendUnique(CPPDEFINES=["DEBUG_ENABLED"]) + env.Append(LINKFLAGS=["/SUBSYSTEM:CONSOLE"]) env.Append(LINKFLAGS=["/DEBUG"]) - env.Append(LINKFLAGS=["/SUBSYSTEM:WINDOWS"]) - if env["debug_symbols"] == "full" or env["debug_symbols"] == "yes": env.AppendUnique(CCFLAGS=["/Z7"]) env.AppendUnique(LINKFLAGS=["/DEBUG"]) @@ -313,6 +314,8 @@ def configure_mingw(env): else: # optimize for size env.Prepend(CCFLAGS=["-Os"]) + env.Append(LINKFLAGS=["-Wl,--subsystem,windows"]) + if env["debug_symbols"] == "yes": env.Prepend(CCFLAGS=["-g1"]) if env["debug_symbols"] == "full": @@ -334,8 +337,6 @@ def configure_mingw(env): env.Append(CCFLAGS=["-g3"]) env.Append(CPPDEFINES=["DEBUG_ENABLED"]) - env.Append(LINKFLAGS=["-Wl,--subsystem,windows"]) - ## Compiler configuration if os.name != "nt": diff --git a/platform/windows/display_server_windows.cpp b/platform/windows/display_server_windows.cpp index cf7bebfbdf4..2df0f80ab32 100644 --- a/platform/windows/display_server_windows.cpp +++ b/platform/windows/display_server_windows.cpp @@ -1135,17 +1135,10 @@ void DisplayServerWindows::window_set_ime_position(const Point2i &p_pos, WindowI void DisplayServerWindows::console_set_visible(bool p_enabled) { _THREAD_SAFE_METHOD_ - if (console_visible == p_enabled) { + if (console_visible == p_enabled) return; - } - if (p_enabled && GetConsoleWindow() == nullptr) { // Open new console if not attached. - own_console = true; - AllocConsole(); - } - if (own_console) { // Note: Do not hide parent console. - ShowWindow(GetConsoleWindow(), p_enabled ? SW_SHOW : SW_HIDE); - console_visible = p_enabled; - } + ShowWindow(GetConsoleWindow(), p_enabled ? SW_SHOW : SW_HIDE); + console_visible = p_enabled; } bool DisplayServerWindows::is_console_visible() const { @@ -3026,18 +3019,7 @@ DisplayServerWindows::DisplayServerWindows(const String &p_rendering_driver, Win shift_mem = false; control_mem = false; meta_mem = false; - - if (AttachConsole(ATTACH_PARENT_PROCESS)) { - FILE *_file = nullptr; - freopen_s(&_file, "CONOUT$", "w", stdout); - freopen_s(&_file, "CONOUT$", "w", stderr); - freopen_s(&_file, "CONIN$", "r", stdin); - - printf("\n"); - console_visible = true; - } else { - console_visible = false; - } + console_visible = IsWindowVisible(GetConsoleWindow()); hInstance = ((OS_Windows *)OS::get_singleton())->get_hinstance(); pressrc = 0; diff --git a/platform/windows/display_server_windows.h b/platform/windows/display_server_windows.h index 52f5b0f4a9e..7bd93a70865 100644 --- a/platform/windows/display_server_windows.h +++ b/platform/windows/display_server_windows.h @@ -405,7 +405,6 @@ private: bool drop_events = false; bool in_dispatch_input_event = false; bool console_visible = false; - bool own_console = false; WNDCLASSEXW wc; From c76fe59204e2a5b46f3f1b37d82029ae7600d7fe Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Mon, 17 Aug 2020 19:07:12 +0300 Subject: [PATCH 2/2] Add Windows Subsystem build option. --- platform/windows/detect.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/platform/windows/detect.py b/platform/windows/detect.py index 271ffc88718..4e1da22bb0a 100644 --- a/platform/windows/detect.py +++ b/platform/windows/detect.py @@ -65,6 +65,7 @@ def get_opts(): # Vista support dropped after EOL due to GH-10243 ("target_win_version", "Targeted Windows version, >= 0x0601 (Windows 7)", "0x0601"), EnumVariable("debug_symbols", "Add debugging symbols to release builds", "yes", ("yes", "no", "full")), + EnumVariable("windows_subsystem", "Windows subsystem", "gui", ("console", "gui")), BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False), ("msvc_version", "MSVC version to use. Ignored if VCINSTALLDIR is set in shell env.", None), BoolVariable("use_mingw", "Use the Mingw compiler, even if MSVC is installed. Only used on Windows.", False), @@ -177,13 +178,14 @@ def configure_msvc(env, manual_msvc_config): """Configure env to work with MSVC""" # Build type + if env["tests"]: + env["windows_subsystem"] = "console" if env["target"] == "release": if env["optimize"] == "speed": # optimize for speed (default) env.Append(CCFLAGS=["/O2"]) else: # optimize for size env.Append(CCFLAGS=["/O1"]) - env.Append(LINKFLAGS=["/SUBSYSTEM:WINDOWS"]) env.Append(LINKFLAGS=["/ENTRY:mainCRTStartup"]) env.Append(LINKFLAGS=["/OPT:REF"]) @@ -193,19 +195,22 @@ def configure_msvc(env, manual_msvc_config): else: # optimize for size env.Append(CCFLAGS=["/O1"]) env.AppendUnique(CPPDEFINES=["DEBUG_ENABLED"]) - env.Append(LINKFLAGS=["/SUBSYSTEM:CONSOLE"]) env.Append(LINKFLAGS=["/OPT:REF"]) elif env["target"] == "debug": env.AppendUnique(CCFLAGS=["/Z7", "/Od", "/EHsc"]) env.AppendUnique(CPPDEFINES=["DEBUG_ENABLED"]) - env.Append(LINKFLAGS=["/SUBSYSTEM:CONSOLE"]) env.Append(LINKFLAGS=["/DEBUG"]) if env["debug_symbols"] == "full" or env["debug_symbols"] == "yes": env.AppendUnique(CCFLAGS=["/Z7"]) env.AppendUnique(LINKFLAGS=["/DEBUG"]) + if env["windows_subsystem"] == "gui": + env.Append(LINKFLAGS=["/SUBSYSTEM:WINDOWS"]) + else: + env.Append(LINKFLAGS=["/SUBSYSTEM:CONSOLE"]) + ## Compile/link flags env.AppendUnique(CCFLAGS=["/MT", "/Gd", "/GR", "/nologo"]) @@ -303,6 +308,9 @@ def configure_mingw(env): ## Build type + if env["tests"]: + env["windows_subsystem"] = "console" + if env["target"] == "release": env.Append(CCFLAGS=["-msse2"]) @@ -314,8 +322,6 @@ def configure_mingw(env): else: # optimize for size env.Prepend(CCFLAGS=["-Os"]) - env.Append(LINKFLAGS=["-Wl,--subsystem,windows"]) - if env["debug_symbols"] == "yes": env.Prepend(CCFLAGS=["-g1"]) if env["debug_symbols"] == "full": @@ -337,6 +343,11 @@ def configure_mingw(env): env.Append(CCFLAGS=["-g3"]) env.Append(CPPDEFINES=["DEBUG_ENABLED"]) + if env["windows_subsystem"] == "gui": + env.Append(LINKFLAGS=["-Wl,--subsystem,windows"]) + else: + env.Append(LINKFLAGS=["-Wl,--subsystem,console"]) + ## Compiler configuration if os.name != "nt":