2016-06-18 22:04:38 +02:00
|
|
|
// Copyright 2016 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// +build !js
|
|
|
|
|
2019-04-07 03:42:55 +02:00
|
|
|
package glfw
|
2016-06-18 22:04:38 +02:00
|
|
|
|
2018-10-06 15:42:28 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"unsafe"
|
2016-09-11 15:34:39 +02:00
|
|
|
|
2019-01-03 21:28:27 +01:00
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
|
2018-12-29 11:20:41 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/glfw"
|
2018-10-06 10:29:40 +02:00
|
|
|
)
|
|
|
|
|
2018-10-06 15:42:28 +02:00
|
|
|
const (
|
|
|
|
smCyCaption = 4
|
|
|
|
monitorDefaultToNearest = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
type rect struct {
|
|
|
|
left int32
|
|
|
|
top int32
|
|
|
|
right int32
|
|
|
|
bottom int32
|
|
|
|
}
|
|
|
|
|
|
|
|
type monitorInfo struct {
|
|
|
|
cbSize uint32
|
|
|
|
rcMonitor rect
|
|
|
|
rcWork rect
|
|
|
|
dwFlags uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// user32 is defined at hideconsole_windows.go
|
2018-10-07 20:18:24 +02:00
|
|
|
procGetSystemMetrics = user32.NewProc("GetSystemMetrics")
|
|
|
|
procGetActiveWindow = user32.NewProc("GetActiveWindow")
|
|
|
|
procGetForegroundWindow = user32.NewProc("GetForegroundWindow")
|
|
|
|
procMonitorFromWindow = user32.NewProc("MonitorFromWindow")
|
|
|
|
procGetMonitorInfoW = user32.NewProc("GetMonitorInfoW")
|
2018-10-06 15:42:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func getSystemMetrics(nIndex int) (int, error) {
|
2019-01-03 21:28:27 +01:00
|
|
|
r, _, e := procGetSystemMetrics.Call(uintptr(nIndex))
|
|
|
|
if e != nil && e.(windows.Errno) != 0 {
|
2018-10-06 15:42:28 +02:00
|
|
|
return 0, fmt.Errorf("ui: GetSystemMetrics failed: error code: %d", e)
|
|
|
|
}
|
|
|
|
return int(r), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getActiveWindow() (uintptr, error) {
|
2019-01-03 21:28:27 +01:00
|
|
|
r, _, e := procGetActiveWindow.Call()
|
|
|
|
if e != nil && e.(windows.Errno) != 0 {
|
2018-10-06 15:42:28 +02:00
|
|
|
return 0, fmt.Errorf("ui: GetActiveWindow failed: error code: %d", e)
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2018-10-07 20:18:24 +02:00
|
|
|
func getForegroundWindow() (uintptr, error) {
|
2019-01-03 21:28:27 +01:00
|
|
|
r, _, e := procGetForegroundWindow.Call()
|
|
|
|
if e != nil && e.(windows.Errno) != 0 {
|
2018-10-07 20:18:24 +02:00
|
|
|
return 0, fmt.Errorf("ui: GetForegroundWindow failed: error code: %d", e)
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2018-10-06 15:42:28 +02:00
|
|
|
func monitorFromWindow(hwnd uintptr, dwFlags uint32) (uintptr, error) {
|
2019-01-03 21:28:27 +01:00
|
|
|
r, _, e := procMonitorFromWindow.Call(hwnd, uintptr(dwFlags))
|
|
|
|
if e != nil && e.(windows.Errno) != 0 {
|
2018-10-06 15:42:28 +02:00
|
|
|
return 0, fmt.Errorf("ui: MonitorFromWindow failed: error code: %d", e)
|
|
|
|
}
|
|
|
|
if r == 0 {
|
|
|
|
return 0, fmt.Errorf("ui: MonitorFromWindow failed: returned value: %d", r)
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMonitorInfoW(hMonitor uintptr, lpmi *monitorInfo) error {
|
2019-01-03 21:28:27 +01:00
|
|
|
r, _, e := procGetMonitorInfoW.Call(hMonitor, uintptr(unsafe.Pointer(lpmi)))
|
|
|
|
if e != nil && e.(windows.Errno) != 0 {
|
2018-10-06 15:42:28 +02:00
|
|
|
return fmt.Errorf("ui: GetMonitorInfoW failed: error code: %d", e)
|
|
|
|
}
|
|
|
|
if r == 0 {
|
|
|
|
return fmt.Errorf("ui: GetMonitorInfoW failed: returned value: %d", r)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-18 17:56:39 +02:00
|
|
|
// fromGLFWMonitorPixel must be called from the main thread.
|
|
|
|
func (u *UserInterface) fromGLFWMonitorPixel(x float64) float64 {
|
2020-09-18 17:21:08 +02:00
|
|
|
return x / u.deviceScaleFactor()
|
|
|
|
}
|
|
|
|
|
2020-09-18 17:31:34 +02:00
|
|
|
// fromGLFWPixel must be called from the main thread.
|
|
|
|
func (u *UserInterface) fromGLFWPixel(x float64) float64 {
|
|
|
|
return x / u.deviceScaleFactor()
|
|
|
|
}
|
|
|
|
|
2020-09-18 17:21:08 +02:00
|
|
|
// toGLFWPixel must be called from the main thread.
|
|
|
|
func (u *UserInterface) toGLFWPixel(x float64) float64 {
|
|
|
|
return x * u.deviceScaleFactor()
|
2016-07-04 04:37:34 +02:00
|
|
|
}
|
2017-04-18 17:51:15 +02:00
|
|
|
|
2020-03-28 13:26:57 +01:00
|
|
|
func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) {
|
2020-08-23 20:31:46 +02:00
|
|
|
mx, my := currentMonitor(u.window).GetPos()
|
2017-04-18 17:51:15 +02:00
|
|
|
// As the video width/height might be wrong,
|
|
|
|
// adjust x/y at least to enable to handle the window (#328)
|
2020-03-28 13:26:57 +01:00
|
|
|
if x < mx {
|
|
|
|
x = mx
|
2017-04-18 17:51:15 +02:00
|
|
|
}
|
2018-10-06 15:42:28 +02:00
|
|
|
t, err := getSystemMetrics(smCyCaption)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-03-28 16:09:08 +01:00
|
|
|
if y < my+t {
|
2020-03-28 13:26:57 +01:00
|
|
|
y = my + t
|
2017-04-18 17:51:15 +02:00
|
|
|
}
|
|
|
|
return x, y
|
|
|
|
}
|
2018-10-06 15:42:28 +02:00
|
|
|
|
2020-08-23 20:31:46 +02:00
|
|
|
func currentMonitorByOS(_ *glfw.Window) *glfw.Monitor {
|
2020-08-23 20:27:38 +02:00
|
|
|
// TODO: Should we return nil here?
|
2018-10-06 15:42:28 +02:00
|
|
|
w, err := getActiveWindow()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-08-23 20:27:38 +02:00
|
|
|
|
2018-10-06 15:42:28 +02:00
|
|
|
if w == 0 {
|
2020-08-23 21:07:08 +02:00
|
|
|
// The active window doesn't exist when launching, or the application is runnable on unfocused.
|
2018-10-07 20:18:24 +02:00
|
|
|
// Get the foreground window, that is common among multiple processes.
|
|
|
|
w, err = getForegroundWindow()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if w == 0 {
|
2020-08-23 16:37:58 +02:00
|
|
|
// GetForegroundWindow can return null according to the document.
|
2020-08-23 20:27:38 +02:00
|
|
|
return nil
|
2018-10-07 20:18:24 +02:00
|
|
|
}
|
2018-10-06 15:42:28 +02:00
|
|
|
}
|
|
|
|
|
2018-10-07 18:42:43 +02:00
|
|
|
// Get the current monitor by the window handle instead of the window position. It is because the window
|
|
|
|
// position is not relaiable in some cases e.g. when the window is put across multiple monitors.
|
|
|
|
|
2018-10-06 15:42:28 +02:00
|
|
|
m, err := monitorFromWindow(w, monitorDefaultToNearest)
|
|
|
|
if err != nil {
|
2020-08-22 19:31:52 +02:00
|
|
|
// monitorFromWindow can return error on Wine. Ignore this.
|
2020-08-23 20:27:38 +02:00
|
|
|
return nil
|
2018-10-06 15:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mi := monitorInfo{}
|
|
|
|
mi.cbSize = uint32(unsafe.Sizeof(mi))
|
|
|
|
if err := getMonitorInfoW(m, &mi); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
x, y := int(mi.rcMonitor.left), int(mi.rcMonitor.top)
|
|
|
|
for _, m := range glfw.GetMonitors() {
|
|
|
|
mx, my := m.GetPos()
|
|
|
|
if mx == x && my == y {
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
}
|
2020-08-23 20:27:38 +02:00
|
|
|
return nil
|
2018-10-06 15:42:28 +02:00
|
|
|
}
|
2018-11-12 16:00:10 +01:00
|
|
|
|
2020-09-03 17:43:51 +02:00
|
|
|
func (u *UserInterface) nativeWindow() uintptr {
|
2018-12-29 18:44:51 +01:00
|
|
|
return u.window.GetWin32Window()
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|