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.
|
|
|
|
|
2019-04-07 03:42:55 +02:00
|
|
|
package glfw
|
2017-02-07 12:44:10 +01:00
|
|
|
|
|
|
|
import (
|
2017-02-08 17:57:50 +01:00
|
|
|
"fmt"
|
2017-02-07 12:44:10 +01:00
|
|
|
"unsafe"
|
2017-02-08 17:57:50 +01:00
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
2017-02-07 12:44:10 +01:00
|
|
|
)
|
|
|
|
|
2017-02-08 17:57:50 +01:00
|
|
|
const (
|
|
|
|
processQueryLimitedInformation = 0x1000
|
|
|
|
)
|
2017-02-07 12:44:10 +01:00
|
|
|
|
2017-02-08 17:57:50 +01:00
|
|
|
var (
|
|
|
|
kernel32 = windows.NewLazySystemDLL("kernel32.dll")
|
|
|
|
user32 = windows.NewLazySystemDLL("user32.dll")
|
2017-02-07 12:44:10 +01:00
|
|
|
|
2017-03-03 16:00:04 +01:00
|
|
|
getCurrentProcessIdProc = kernel32.NewProc("GetCurrentProcessId")
|
|
|
|
getConsoleWindowProc = kernel32.NewProc("GetConsoleWindow")
|
2022-01-16 14:31:17 +01:00
|
|
|
freeConsoleWindowProc = kernel32.NewProc("FreeConsole")
|
2017-03-03 16:00:04 +01:00
|
|
|
getWindowThreadProcessIdProc = user32.NewProc("GetWindowThreadProcessId")
|
2017-02-08 17:57:50 +01:00
|
|
|
)
|
2017-02-07 12:44:10 +01:00
|
|
|
|
2017-02-08 17:57:50 +01:00
|
|
|
func getCurrentProcessId() (uint32, error) {
|
2019-01-03 21:28:27 +01:00
|
|
|
r, _, e := getCurrentProcessIdProc.Call()
|
|
|
|
if e != nil && e.(windows.Errno) != 0 {
|
2017-02-08 17:57:50 +01:00
|
|
|
return 0, fmt.Errorf("ui: GetCurrentProcessId failed: %d", e)
|
2017-02-07 12:44:10 +01:00
|
|
|
}
|
2017-02-08 17:57:50 +01:00
|
|
|
return uint32(r), nil
|
|
|
|
}
|
2017-02-07 12:44:10 +01:00
|
|
|
|
2017-02-08 19:49:45 +01:00
|
|
|
func getWindowThreadProcessId(hwnd uintptr) (uint32, error) {
|
|
|
|
pid := uint32(0)
|
2019-01-03 21:28:27 +01:00
|
|
|
r, _, e := getWindowThreadProcessIdProc.Call(hwnd, uintptr(unsafe.Pointer(&pid)))
|
2017-02-08 17:57:50 +01:00
|
|
|
if r == 0 {
|
2017-02-08 19:49:45 +01:00
|
|
|
return 0, fmt.Errorf("ui: GetWindowThreadProcessId failed: %d", e)
|
2017-02-08 17:57:50 +01:00
|
|
|
}
|
2017-02-08 19:49:45 +01:00
|
|
|
return pid, nil
|
2017-02-08 17:57:50 +01:00
|
|
|
}
|
2017-02-07 12:44:10 +01:00
|
|
|
|
2017-02-08 17:57:50 +01:00
|
|
|
func getConsoleWindow() (uintptr, error) {
|
2019-01-03 21:28:27 +01:00
|
|
|
r, _, e := getConsoleWindowProc.Call()
|
|
|
|
if e != nil && e.(windows.Errno) != 0 {
|
2017-02-08 17:57:50 +01:00
|
|
|
return 0, fmt.Errorf("ui: GetConsoleWindow failed: %d", e)
|
2017-02-07 12:44:10 +01:00
|
|
|
}
|
2017-02-08 17:57:50 +01:00
|
|
|
return r, nil
|
2017-02-07 12:44:10 +01:00
|
|
|
}
|
|
|
|
|
2022-01-16 14:31:17 +01:00
|
|
|
func freeConsole() error {
|
|
|
|
_, _, e := freeConsoleWindowProc.Call()
|
|
|
|
if e != nil && e.(windows.Errno) != 0 {
|
|
|
|
return fmt.Errorf("ui: FreeConsole failed: %d", e)
|
2017-02-08 17:57:50 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-07 12:44:10 +01:00
|
|
|
|
2017-02-08 17:57:50 +01:00
|
|
|
// hideConsoleWindowOnWindows will hide the console window that is showing when
|
|
|
|
// compiling on Windows without specifying the '-ldflags "-Hwindowsgui"' flag.
|
|
|
|
func hideConsoleWindowOnWindows() {
|
2017-02-08 19:49:45 +01:00
|
|
|
pid, err := getCurrentProcessId()
|
2017-02-08 17:57:50 +01:00
|
|
|
if err != nil {
|
|
|
|
// Ignore errors because:
|
|
|
|
// 1. It is not critical if the console can't be hid.
|
|
|
|
// 2. There is nothing to do when errors happen.
|
|
|
|
return
|
|
|
|
}
|
2017-02-08 19:49:45 +01:00
|
|
|
w, err := getConsoleWindow()
|
2017-02-08 17:57:50 +01:00
|
|
|
if err != nil {
|
|
|
|
// Ignore errors
|
|
|
|
return
|
|
|
|
}
|
2017-07-23 15:49:04 +02:00
|
|
|
// Get the process ID of the console's creator.
|
2017-02-08 19:49:45 +01:00
|
|
|
cpid, err := getWindowThreadProcessId(w)
|
2017-02-08 17:57:50 +01:00
|
|
|
if err != nil {
|
|
|
|
// Ignore errors
|
|
|
|
return
|
|
|
|
}
|
2017-02-08 19:49:45 +01:00
|
|
|
if pid == cpid {
|
|
|
|
// The current process created its own console. Hide this.
|
2022-01-16 14:31:17 +01:00
|
|
|
// Ignore error
|
|
|
|
freeConsole()
|
2017-02-08 19:49:45 +01:00
|
|
|
}
|
2017-02-07 12:44:10 +01:00
|
|
|
}
|