From 48fcb850deeb434b36c4ab249a61e7658533fba4 Mon Sep 17 00:00:00 2001 From: zeromus Date: Sun, 11 May 2025 17:40:24 -0400 Subject: [PATCH] winport: fiddle with stdio redirection buffering --- desmume/src/frontend/windows/console.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/desmume/src/frontend/windows/console.cpp b/desmume/src/frontend/windows/console.cpp index cce950201..18774e132 100644 --- a/desmume/src/frontend/windows/console.cpp +++ b/desmume/src/frontend/windows/console.cpp @@ -118,6 +118,8 @@ void OpenConsole() //stdout is already connected to something. keep using it and dont let the console interfere bool shouldRedirectStdout = fileType == FILE_TYPE_UNKNOWN; + bool shouldLineBufferStdOut = fileType == FILE_TYPE_CHAR; + bool shouldLineBufferStdIn = GetFileType(hConsoleIn) == FILE_TYPE_CHAR; bool attached = false; #if 0 @@ -156,6 +158,18 @@ void OpenConsole() freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stderr); freopen("CONIN$", "r", stdin); + + //not sure how this would happen.. but in case it's specifically sent to CON somehow... + //otherwise it's full buffered by default + if(shouldLineBufferStdOut) + setvbuf(stdout, nullptr, _IOLBF, 0); + + //Same for stdin, I guess + if(shouldLineBufferStdIn) + setvbuf(stdout, nullptr, _IOLBF, 0); + + //stderr is always unbuffered + setvbuf(stderr, nullptr, _IONBF, 0); } SetConsoleCtrlHandler(HandlerRoutine, TRUE);