diff --git a/launcher/Application.cpp b/launcher/Application.cpp index 356f48f3b..c2519c634 100644 --- a/launcher/Application.cpp +++ b/launcher/Application.cpp @@ -158,6 +158,7 @@ #endif #include #include +#include "console/WindowsConsole.h" #endif #include "console/Console.h" @@ -291,9 +292,21 @@ std::tuple read_lock_File(const Q Application::Application(int& argc, char** argv) : QApplication(argc, argv) { +#if defined Q_OS_WIN32 + // attach the parent console if stdout not already captured + if (AttachWindowsConsole()) { + consoleAttached = true; + if (auto err = EnableAnsiSupport(); !err) { + isANSIColorConsole = true; + } else { + std::cout << "Error setting up ansi console" << err.message() << std::endl; + } + } +#else if (console::isConsole()) { isANSIColorConsole = true; } +#endif setOrganizationName(BuildConfig.LAUNCHER_NAME); setOrganizationDomain(BuildConfig.LAUNCHER_DOMAIN); @@ -1405,6 +1418,16 @@ Application::~Application() { // Shut down logger by setting the logger function to nothing qInstallMessageHandler(nullptr); + +#if defined Q_OS_WIN32 + // Detach from Windows console + if (consoleAttached) { + fclose(stdout); + fclose(stdin); + fclose(stderr); + FreeConsole(); + } +#endif } void Application::messageReceived(const QByteArray& message) diff --git a/launcher/Application.h b/launcher/Application.h index febe3c554..0fd733b50 100644 --- a/launcher/Application.h +++ b/launcher/Application.h @@ -271,6 +271,11 @@ class Application : public QApplication { Qt::ApplicationState m_prevAppState = Qt::ApplicationInactive; #endif +#if defined Q_OS_WIN32 + // used on Windows to attach the standard IO streams + bool consoleAttached = false; +#endif + // FIXME: attach to instances instead. struct InstanceXtras { InstanceWindow* window = nullptr; diff --git a/launcher/console/WindowsConsole.cpp b/launcher/console/WindowsConsole.cpp index e12183624..4a0eb3d3d 100644 --- a/launcher/console/WindowsConsole.cpp +++ b/launcher/console/WindowsConsole.cpp @@ -24,16 +24,12 @@ #endif #include -#include #include -#include #include #include #include #include -namespace console { - void RedirectHandle(DWORD handle, FILE* stream, const char* mode) { HANDLE stdHandle = GetStdHandle(handle); @@ -161,31 +157,3 @@ std::error_code EnableAnsiSupport() return {}; } - -void FreeWindowsConsole() -{ - fclose(stdout); - fclose(stdin); - fclose(stderr); - FreeConsole(); -} - -WindowsConsoleGuard::WindowsConsoleGuard() : m_consoleAttached(false) -{ - if (console::AttachWindowsConsole()) { - m_consoleAttached = true; - if (auto err = console::EnableAnsiSupport(); err) { - std::cout << "Error setting up ansi console" << err.message() << std::endl; - } - } -} - -WindowsConsoleGuard::~WindowsConsoleGuard() -{ - // Detach from Windows console - if (m_consoleAttached) { - console::FreeWindowsConsole(); - } -} - -} // namespace console diff --git a/launcher/console/WindowsConsole.h b/launcher/console/WindowsConsole.h index 52102217d..4c1f3ee28 100644 --- a/launcher/console/WindowsConsole.h +++ b/launcher/console/WindowsConsole.h @@ -21,24 +21,8 @@ #pragma once -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif - #include -namespace console { + void BindCrtHandlesToStdHandles(bool bindStdIn, bool bindStdOut, bool bindStdErr); bool AttachWindowsConsole(); std::error_code EnableAnsiSupport(); -void FreeWindowsConsole(); - -class WindowsConsoleGuard { - public: - WindowsConsoleGuard(); - ~WindowsConsoleGuard(); - - private: - bool m_consoleAttached; -}; - -} // namespace console diff --git a/launcher/filelink/FileLink.cpp b/launcher/filelink/FileLink.cpp index d87a1078b..343f31eaa 100644 --- a/launcher/filelink/FileLink.cpp +++ b/launcher/filelink/FileLink.cpp @@ -34,11 +34,26 @@ #include +#include + +#if defined Q_OS_WIN32 +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include "console/WindowsConsole.h" +#endif + #include namespace fs = std::filesystem; FileLinkApp::FileLinkApp(int& argc, char** argv) : QCoreApplication(argc, argv), socket(new QLocalSocket(this)) { +#if defined Q_OS_WIN32 + // attach the parent console + if (AttachWindowsConsole()) { + consoleAttached = true; + } +#endif setOrganizationName(BuildConfig.LAUNCHER_NAME); setOrganizationDomain(BuildConfig.LAUNCHER_DOMAIN); setApplicationName(BuildConfig.LAUNCHER_NAME + "FileLink"); @@ -221,4 +236,13 @@ FileLinkApp::~FileLinkApp() qDebug() << "link program shutting down"; // Shut down logger by setting the logger function to nothing qInstallMessageHandler(nullptr); + +#if defined Q_OS_WIN32 + // Detach from Windows console + if (consoleAttached) { + fclose(stdout); + fclose(stdin); + fclose(stderr); + } +#endif } diff --git a/launcher/filelink/FileLink.h b/launcher/filelink/FileLink.h index b767f7f74..583d0d43a 100644 --- a/launcher/filelink/FileLink.h +++ b/launcher/filelink/FileLink.h @@ -64,4 +64,8 @@ class FileLinkApp : public QCoreApplication { QList m_links_to_make; QList m_path_results; +#if defined Q_OS_WIN32 + // used on Windows to attach the standard IO streams + bool consoleAttached = false; +#endif }; diff --git a/launcher/filelink/filelink_main.cpp b/launcher/filelink/filelink_main.cpp index d34844370..2a8bcb703 100644 --- a/launcher/filelink/filelink_main.cpp +++ b/launcher/filelink/filelink_main.cpp @@ -22,17 +22,8 @@ #include "FileLink.h" -#if defined Q_OS_WIN32 -#include "console/WindowsConsole.h" -#endif - int main(int argc, char* argv[]) { -#if defined Q_OS_WIN32 - // attach the parent console - console::WindowsConsoleGuard _consoleGuard; -#endif - FileLinkApp ldh(argc, argv); switch (ldh.status()) { diff --git a/launcher/main.cpp b/launcher/main.cpp index 9378387bb..2bce655d2 100644 --- a/launcher/main.cpp +++ b/launcher/main.cpp @@ -33,23 +33,13 @@ * limitations under the License. */ -#include - #include "Application.h" -#if defined Q_OS_WIN32 -#include "console/WindowsConsole.h" -#endif - int main(int argc, char* argv[]) { -#if defined Q_OS_WIN32 - // used on Windows to attach the standard IO streams - console::WindowsConsoleGuard _consoleGuard; -#endif - // initialize Qt Application app(argc, argv); + switch (app.status()) { case Application::StartingUp: case Application::Initialized: { diff --git a/launcher/updater/prismupdater/PrismUpdater.cpp b/launcher/updater/prismupdater/PrismUpdater.cpp index 00e59bfb6..d8e6a2469 100644 --- a/launcher/updater/prismupdater/PrismUpdater.cpp +++ b/launcher/updater/prismupdater/PrismUpdater.cpp @@ -40,6 +40,16 @@ #include #include +#include + +#if defined Q_OS_WIN32 +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include +#include "console/WindowsConsole.h" +#endif + #include namespace fs = std::filesystem; @@ -76,6 +86,12 @@ void appDebugOutput(QtMsgType type, const QMessageLogContext& context, const QSt PrismUpdaterApp::PrismUpdaterApp(int& argc, char** argv) : QApplication(argc, argv) { +#if defined Q_OS_WIN32 + // attach the parent console if stdout not already captured + if (AttachWindowsConsole()) { + consoleAttached = true; + } +#endif setOrganizationName(BuildConfig.LAUNCHER_NAME); setOrganizationDomain(BuildConfig.LAUNCHER_DOMAIN); setApplicationName(BuildConfig.LAUNCHER_NAME + "Updater"); @@ -366,6 +382,16 @@ PrismUpdaterApp::~PrismUpdaterApp() qDebug() << "updater shutting down"; // Shut down logger by setting the logger function to nothing qInstallMessageHandler(nullptr); + +#if defined Q_OS_WIN32 + // Detach from Windows console + if (consoleAttached) { + fclose(stdout); + fclose(stdin); + fclose(stderr); + FreeConsole(); + } +#endif } void PrismUpdaterApp::fail(const QString& reason) diff --git a/launcher/updater/prismupdater/updater_main.cpp b/launcher/updater/prismupdater/updater_main.cpp index ddc38d5cd..89c1d1198 100644 --- a/launcher/updater/prismupdater/updater_main.cpp +++ b/launcher/updater/prismupdater/updater_main.cpp @@ -21,18 +21,8 @@ */ #include "PrismUpdater.h" - -#if defined Q_OS_WIN32 -#include "console/WindowsConsole.h" -#endif - int main(int argc, char* argv[]) { -#if defined Q_OS_WIN32 - // attach the parent console if stdout not already captured - console::WindowsConsoleGuard _consoleGuard; -#endif - PrismUpdaterApp wUpApp(argc, argv); switch (wUpApp.status()) {