Skip to content

Commit

Permalink
Restore console mode when exiting
Browse files Browse the repository at this point in the history
  • Loading branch information
dpjudas committed Feb 12, 2025
1 parent 7052457 commit 52a20bf
Showing 1 changed file with 48 additions and 15 deletions.
63 changes: 48 additions & 15 deletions SurrealEngine/MainDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,56 @@
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "Ws2_32.lib")

// Attempt to restore old console modes when process exits
struct ApplyConsoleMode
{
ApplyConsoleMode()
{
oldconsolecp = GetConsoleCP();
oldconsoleoutputcp = GetConsoleOutputCP();
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);

stdoutput = GetStdHandle(STD_OUTPUT_HANDLE);
if (stdoutput != INVALID_HANDLE_VALUE)
{
oldoutputmodeset = GetConsoleMode(stdoutput, &oldoutputmode);
SetConsoleMode(stdoutput, ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT | DISABLE_NEWLINE_AUTO_RETURN);
}

stdinput = GetStdHandle(STD_INPUT_HANDLE);
if (stdinput != INVALID_HANDLE_VALUE)
{
oldinputmodeset = GetConsoleMode(stdoutput, &oldinputmode);
SetConsoleMode(stdinput, ENABLE_VIRTUAL_TERMINAL_INPUT | ENABLE_PROCESSED_INPUT);
}
}
~ApplyConsoleMode()
{
if (oldoutputmodeset)
SetConsoleMode(stdoutput, oldoutputmode);

if (oldinputmodeset)
SetConsoleMode(stdinput, oldinputmode);

SetConsoleCP(oldconsolecp);
SetConsoleOutputCP(oldconsoleoutputcp);
}

UINT oldconsolecp = 0;
UINT oldconsoleoutputcp = 0;
BOOL oldoutputmodeset = FALSE;
BOOL oldinputmodeset = FALSE;
DWORD oldoutputmode = 0;
DWORD oldinputmode = 0;
HANDLE stdoutput = INVALID_HANDLE_VALUE;
HANDLE stdinput = INVALID_HANDLE_VALUE;
};

int wmain(int argc, wchar_t* argv[])
{
ApplyConsoleMode applyConsoleMode;

try
{
Array<std::string> args;
Expand All @@ -32,21 +80,6 @@ int wmain(int argc, wchar_t* argv[])
if (err != 0)
throw std::runtime_error("Failed to initialize winsockets");

SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);

HANDLE stdoutput = GetStdHandle(STD_OUTPUT_HANDLE);
if (stdoutput != INVALID_HANDLE_VALUE)
{
SetConsoleMode(stdoutput, ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT | DISABLE_NEWLINE_AUTO_RETURN);
}

HANDLE stdinput = GetStdHandle(STD_INPUT_HANDLE);
if (stdinput != INVALID_HANDLE_VALUE)
{
SetConsoleMode(stdinput, ENABLE_VIRTUAL_TERMINAL_INPUT | ENABLE_PROCESSED_INPUT);
}

DebuggerApp app;
return app.Main(std::move(args));
}
Expand Down

0 comments on commit 52a20bf

Please sign in to comment.