Re: [hatari-devel] Win32 console patch

[ Thread Index | Date Index | More lists.tuxfamily.org/hatari-devel Archives ]


On Sonntag, 6. November 2022 15:48:59 CET Christian Zietz wrote:
> Did you ever find time to check your patch for that?

Checked that now, revised patch attached below. It compiles, but i currently 
get a link error (undefined referenced to WinMain; i noticed that 
libSDL2main.a is linked too, maybe the SDL2.cmake is causing troubles now? I'm 
quite sure i successfully compiled it on windows not so long ago).

>It seems that some (ahem) Windows users have trouble
>providing debug traces via the separate window (-W option).

Remember that this patch only is active when you run hatari from a console. If 
you run it from the desktop, you still have to use the -W switch.
diff --git a/src/gui-win/opencon.c b/src/gui-win/opencon.c
index f245cf63..fec0a67f 100644
--- a/src/gui-win/opencon.c
+++ b/src/gui-win/opencon.c
@@ -17,14 +17,55 @@
 #include "opencon.h"
 #include "../includes/configuration.h"
 
+#ifdef __CYGWIN__
+#define CONSOLE_DEV "/dev/tty"
+#else
+#define CONSOLE_DEV "CON"
+#endif
+
 
 void Win_OpenCon(void)
 {
 	if (ConfigureParams.Log.bConsoleWindow)
 	{
-		AllocConsole();
-		freopen("CON", "w", stdout);
-		freopen("CON", "r", stdin);
-		freopen("CON", "w", stderr);
+		if (AllocConsole())
+		{
+			freopen(CONSOLE_DEV, "w", stdout);
+			freopen(CONSOLE_DEV, "r", stdin);
+			freopen(CONSOLE_DEV, "w", stderr);
+		}
+	}
+	else
+	{
+		HANDLE my_stdin;
+		HANDLE my_stdout;
+		HANDLE my_stderr;
+		DWORD filetype_in;
+		DWORD filetype_out;
+		DWORD filetype_err;
+
+		my_stdin = GetStdHandle(STD_INPUT_HANDLE);
+		my_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
+		my_stderr = GetStdHandle(STD_ERROR_HANDLE);
+		filetype_in = GetFileType(my_stdin);
+		filetype_out = GetFileType(my_stdout);
+		filetype_err = GetFileType(my_stderr);
+		if (my_stdin == INVALID_HANDLE_VALUE ||
+			filetype_in == FILE_TYPE_UNKNOWN ||
+			my_stdout == INVALID_HANDLE_VALUE ||
+			filetype_out == FILE_TYPE_UNKNOWN ||
+			my_stderr == INVALID_HANDLE_VALUE ||
+			filetype_err == FILE_TYPE_UNKNOWN)
+		{
+			if (AttachConsole(ATTACH_PARENT_PROCESS))
+			{
+				if (my_stdin == INVALID_HANDLE_VALUE || filetype_in == FILE_TYPE_UNKNOWN)
+					freopen(CONSOLE_DEV, "r", stdin);
+				if (my_stdout == INVALID_HANDLE_VALUE || filetype_out == FILE_TYPE_UNKNOWN)
+					freopen(CONSOLE_DEV, "w", stdout);
+				if (my_stderr == INVALID_HANDLE_VALUE || filetype_err == FILE_TYPE_UNKNOWN)
+					freopen(CONSOLE_DEV, "w", stderr);
+			}
+		}
 	}
 }
diff --git a/src/main.c b/src/main.c
index 4371f64f..a27e8477 100644
--- a/src/main.c
+++ b/src/main.c
@@ -948,7 +948,7 @@ int main(int argc, char *argv[])
 	/* monitor type option might require "reset" -> true */
 	Configuration_Apply(true);
 
-#ifdef WIN32
+#if defined(_WIN32) || defined(__CYGWIN__)
 	Win_OpenCon();
 #endif
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/