2017-02-07 12:44:10 +01:00
|
|
|
// Copyright 2017 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-02-06 08:08:22 +01:00
|
|
|
package ui
|
2017-02-07 12:44:10 +01:00
|
|
|
|
|
|
|
import (
|
2017-02-08 17:57:50 +01:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
2017-02-07 12:44:10 +01:00
|
|
|
)
|
|
|
|
|
2017-02-08 17:57:50 +01:00
|
|
|
var (
|
|
|
|
kernel32 = windows.NewLazySystemDLL("kernel32.dll")
|
2017-02-07 12:44:10 +01:00
|
|
|
|
2022-05-22 14:50:38 +02:00
|
|
|
procFreeConsoleWindow = kernel32.NewProc("FreeConsole")
|
|
|
|
procGetConsoleWindow = kernel32.NewProc("GetConsoleWindow")
|
2017-02-08 17:57:50 +01:00
|
|
|
)
|
2017-02-07 12:44:10 +01:00
|
|
|
|
2022-01-30 07:36:09 +01:00
|
|
|
func freeConsole() error {
|
|
|
|
r, _, e := procFreeConsoleWindow.Call()
|
2022-05-10 17:48:41 +02:00
|
|
|
if int32(r) == 0 {
|
2022-01-30 07:36:09 +01:00
|
|
|
if e != nil && e != windows.ERROR_SUCCESS {
|
2022-02-06 08:08:22 +01:00
|
|
|
return fmt.Errorf("ui: FreeConsole failed: %w", e)
|
2022-01-30 07:36:09 +01:00
|
|
|
}
|
2022-02-06 08:08:22 +01:00
|
|
|
return fmt.Errorf("ui: FreeConsole returned 0")
|
2017-02-07 12:44:10 +01:00
|
|
|
}
|
2022-01-30 07:36:09 +01:00
|
|
|
return nil
|
2017-02-08 17:57:50 +01:00
|
|
|
}
|
2017-02-07 12:44:10 +01:00
|
|
|
|
2022-01-30 07:36:09 +01:00
|
|
|
func getConsoleWindow() windows.HWND {
|
|
|
|
r, _, _ := procGetConsoleWindow.Call()
|
|
|
|
return windows.HWND(r)
|
2017-02-07 12:44:10 +01:00
|
|
|
}
|
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
// hideConsoleWindow will hide the console window that is showing when
|
2017-02-08 17:57:50 +01:00
|
|
|
// compiling on Windows without specifying the '-ldflags "-Hwindowsgui"' flag.
|
2023-10-14 17:06:07 +02:00
|
|
|
func hideConsoleWindow() {
|
2022-05-24 17:13:26 +02:00
|
|
|
// In Xbox, GetWindowThreadProcessId might not exist.
|
|
|
|
if user32.NewProc("GetWindowThreadProcessId").Find() != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-22 14:50:38 +02:00
|
|
|
pid := windows.GetCurrentProcessId()
|
|
|
|
|
2017-07-23 15:49:04 +02:00
|
|
|
// Get the process ID of the console's creator.
|
2022-05-22 14:50:38 +02:00
|
|
|
var cpid uint32
|
|
|
|
if _, err := windows.GetWindowThreadProcessId(getConsoleWindow(), &cpid); err != nil {
|
|
|
|
// Even if closing the console fails, this is not harmful.
|
|
|
|
// Ignore error.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-08 19:49:45 +01:00
|
|
|
if pid == cpid {
|
|
|
|
// The current process created its own console. Hide this.
|
2022-01-30 07:36:09 +01:00
|
|
|
// Ignore error.
|
2022-09-09 18:52:46 +02:00
|
|
|
_ = freeConsole()
|
2017-02-08 19:49:45 +01:00
|
|
|
}
|
2017-02-07 12:44:10 +01:00
|
|
|
}
|