apposed / jaunch

Launch Programs 𝙔𝙤𝙪𝙧 Way! 🍔
The Unlicense
7 stars 2 forks source link

Console attachment logic #30

Closed ctrueden closed 1 week ago

ctrueden commented 5 months ago

The ImageJ Launcher has logic to attach to a console using freopen with channels CONIN$ (stdin) and CONOUT$ (stdout and stderr), as well as create a new console using FreeConsole() + AllocConsole(). And it has a hidden option --console/--attach-console which calls attach_win_console() to do this. It also has --new-console to create a new console. Is this ever actually necessary?

imagesc-bot commented 5 months ago

This issue has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/jaunch-a-new-java-launcher-test-fiji-with-java-21/92058/1

ctrueden commented 2 months ago

Tangentially related is this SO post, which talks about the -mwindows and -mconsole flags to gcc when building with MinGW (which Jaunch does).

ctrueden commented 2 months ago

What we really need though is to attach contingently to the console, depending on whether one is already active. And only pop a new console when --new-console is given as a command line argument.

hinerm commented 1 month ago

Just noting that in https://github.com/apposed/jaunch/commit/392c60e690fa19c4c12b9003696ad334fbad4dec I set -mwindows to disable the console. That may need to be reverted to attach to an existing console. Then just always try AttachConsole and if it fails, contingently create a new one as suggested.

If we do go back to console mode I'm curious, if the AttachConsole fails, if it will still make/show a new console. If so it looks like we can hide it (if --new-console is not present) with

// Get the handle to the console window
HWND consoleWindow = GetConsoleWindow();

// Check if the console window handle is valid
if (consoleWindow != NULL) {
    // Hide the console window
    ShowWindow(consoleWindow, SW_HIDE);
}

Also wondering if this needs to be repeated for both the C and kotlin launchers?

hinerm commented 1 month ago

That may need to be reverted to attach to an existing console.

Actually right now when I launch from an existing console Fiji just attaches to it. I'm actually not sure we need any other logic here at all.

edit: ctrl + c doesn't fully kill Fiji when running from the console. Maybe because the kotlin launcher is in console mode? Investigation is needed..

edit: adding linkerOpts("-mwindows") to the executable section of build.gradle.kts did not change this behavior, sadly.

hinerm commented 1 week ago

Since the ctrl + c is not Jaunch specific and covered in https://github.com/fiji/fiji/issues/347, and OS focus is also a separate issue covered in https://github.com/apposed/jaunch/issues/52 I think think the actual console attachment logic is working as desired and closing this issue. We can create a new one if behavior needs to be modified specifically with respect to console attachment.