amo13 / Anarchy-Droid

One-Click Android Rom Installation Assistant
https://anarchy-droid.com
GNU General Public License v3.0
113 stars 15 forks source link

Windows: Get rid of the terminal window in the background #2

Closed amo13 closed 2 years ago

amo13 commented 2 years ago

Currently the app is not started as a GUI app but as a command line app. This is the standard for binaries built with go build. Therefore, a terminal is attached to the launched app. Alternatively, we can launch the app as a GUI application without attached terminal. This is the standard for apps packaged with fyne package. Additionally, the packaged app gets displayed with its application icon. This way would be ideal.

Unfortunately, if we launch the app as a GUI app without terminal attached, terminal windows pop up every second on top of the app window even stealing focus. This is because the app needs to call binaries periodically in the background (adb, fastboot and heimdall) and evalutate what they return. An ideal solution would find a way to make the calls to the binaries without making a terminals pop up.

j0le commented 2 years ago

Here are some ideas, I have, that might work:

I did not test any of this. It will most likely fail, because I forgot something. But it might be a good starting point.

Another way to reach the goal, would be to use a relatively new MS Windows feature: Pseudo Consoles. They should work like pseudo terminals (PTYs) on Unix/Linux. tmux and terminal emulators use them to play the role of a terminal. All whats left to do is to read all input comming from the stdouts of the started processes through the pty stuff and ignore it. See a post from the Windows Command Line blog Windows Command-Line: Introducing the Windows Pseudo Console (ConPTY) or Microsoft Docs. This mechanism is used by the new GUI terminal/console emulator “Windows Terminal”.

Another way to hide a console window would be, to look at the code of the terminal emulator ConEmu or the library winpty, which use a hidden console window and not the new Windows feature “pseudo console”.

j0le commented 2 years ago

I’ve written a C++ application to test all this. It works 😀. You can see it in my fork (j0le/Anarchy-Droid) in the branch no-console-wnd. The latest commit is https://github.com/j0le/Anarchy-Droid/commit/6415651371aaffe43ff1bc8d1e94d76656dda459 . Now the only thing, what’s left to do, is to port this from C++ to go. I didn’t request a pull yet, because it’s not finished yet.

These two blog posts might help porting it to go:

j0le commented 2 years ago

Here is a funny little program, that works, if the program allready has a console:

package main

import (
    "fmt"
    "syscall"
    "time"

    "golang.org/x/sys/windows"
)

func main() {
    fmt.Print("Hello World")
    var kernel32_dll = windows.NewLazySystemDLL("Kernel32.dll")
    var user32_dll = windows.NewLazySystemDLL("User32.dll")

    var proc_GetConsoleWindow = kernel32_dll.NewProc("GetConsoleWindow")
    var proc_ShowWindow = user32_dll.NewProc("ShowWindow")

    var console_hwnd uintptr
    console_hwnd, _, err := proc_GetConsoleWindow.Call()

    if err != syscall.Errno(0) {
        return
    }

    if console_hwnd == 0 {
        return
    }

    for {

        proc_ShowWindow.Call(console_hwnd, 0) // hide

        time.Sleep(1 * time.Second)

        proc_ShowWindow.Call(console_hwnd, 5) // show

        time.Sleep(1 * time.Second)
    }

}
j0le commented 2 years ago

Another update: https://github.com/j0le/Anarchy-Droid/blob/25d65ff35e14ce409c840c9da56302e5bf32d60c/cpp-no-console-wnd-test/main.go

Use this commandline to make it a GUI application (IMAGE_SUBSYSTEM_WINDOWS_GUI):

go build -ldflags="-H windowsgui"
amo13 commented 2 years ago

Amazing! Done.

j0le commented 2 years ago

nice! Just for reference: The pull request, that fixed this, is #13.

j0le commented 2 years ago

Just some random info: The python people had the same issue of a terminal window popping up on Windows. “pythonw.exe or python.exe?” – Stack Overflow