From 7b0ec038c7289a17640c31ae883d0d432d355f6e Mon Sep 17 00:00:00 2001 From: Thomas Jentzsch Date: Thu, 7 Jul 2022 20:25:30 +0200 Subject: [PATCH 1/2] fixed console output not working under Windows --- src/common/main.cxx | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/common/main.cxx b/src/common/main.cxx index e0ff11f93..a4f2dc60c 100644 --- a/src/common/main.cxx +++ b/src/common/main.cxx @@ -153,6 +153,26 @@ bool isProfilingRun(int ac, char* av[]) { return string(av[1]) == "-profile"; } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void attachConsole() +{ +#if defined(BSPF_WINDOWS) + // Attach console to allow command line output (e.g. for -help) + AttachConsole(ATTACH_PARENT_PROCESS); + FILE* fDummy; + freopen_s(&fDummy, "CONOUT$", "w", stdout); +#endif +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void freeConsole() +{ +#if defined(BSPF_WINDOWS) + cout << "Press \"Control + C\"" << std::flush; + FreeConsole(); +#endif +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #if defined(BSPF_MACOS) int stellaMain(int ac, char* av[]) @@ -219,22 +239,27 @@ int main(int ac, char* av[]) string romfile = localOpts["ROMFILE"].toString(); if(localOpts["listrominfo"].toBool()) { + attachConsole(); Logger::debug("Showing output from 'listrominfo' ..."); theOSystem->propSet().print(); + freeConsole(); return Cleanup(); } else if(localOpts["rominfo"].toBool()) { + attachConsole(); Logger::debug("Showing output from 'rominfo' ..."); FSNode romnode(romfile); Logger::error(theOSystem->getROMInfo(romnode)); - + freeConsole(); return Cleanup(); } else if(localOpts["help"].toBool()) { + attachConsole(); Logger::debug("Displaying usage"); theOSystem->settings().usage(); + freeConsole(); return Cleanup(); } From a80798e281ad1d69e94c43b4183a78475ace0d7d Mon Sep 17 00:00:00 2001 From: Thomas Jentzsch Date: Thu, 7 Jul 2022 21:29:38 +0200 Subject: [PATCH 2/2] improved Windows command line output --- src/common/main.cxx | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/common/main.cxx b/src/common/main.cxx index a4f2dc60c..1354801f7 100644 --- a/src/common/main.cxx +++ b/src/common/main.cxx @@ -161,6 +161,20 @@ void attachConsole() AttachConsole(ATTACH_PARENT_PROCESS); FILE* fDummy; freopen_s(&fDummy, "CONOUT$", "w", stdout); + + // Windows displays a new prompt immediately after starting the app. + // This code tries to hide it before the new output is generated. + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + CONSOLE_SCREEN_BUFFER_INFO csbi; + if(GetConsoleScreenBufferInfo(hConsole, &csbi)) + { + COORD pos = {0, csbi.dwCursorPosition.Y}; + SetConsoleCursorPosition(hConsole, pos); + cout << std::setw(160) << ""; // this clears the extra prompt display + SetConsoleCursorPosition(hConsole, pos); + } + else + cout << endl << endl; #endif } @@ -168,7 +182,7 @@ void attachConsole() void freeConsole() { #if defined(BSPF_WINDOWS) - cout << "Press \"Control + C\"" << std::flush; + cout << "Press \"Enter\"" << endl << std::flush; FreeConsole(); #endif }