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.
|
|
|
|
|
2022-02-06 08:08:22 +01:00
|
|
|
package ui
|
2016-06-18 22:04:38 +02:00
|
|
|
|
2018-10-06 15:42:28 +02:00
|
|
|
import (
|
2022-07-04 05:55:34 +02:00
|
|
|
"errors"
|
2018-10-06 15:42:28 +02:00
|
|
|
"fmt"
|
2021-09-17 19:21:24 +02:00
|
|
|
"runtime"
|
2022-12-13 05:01:32 +01:00
|
|
|
"syscall"
|
2016-09-11 15:34:39 +02:00
|
|
|
|
2019-01-03 21:28:27 +01:00
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
2022-03-22 15:33:21 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2022-02-11 12:38:45 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/directx"
|
2022-03-22 15:33:21 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
|
2022-05-31 17:58:33 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/microsoftgdk"
|
2023-03-30 19:21:08 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/winver"
|
2018-10-06 10:29:40 +02:00
|
|
|
)
|
|
|
|
|
2022-06-16 19:10:29 +02:00
|
|
|
type graphicsDriverCreatorImpl struct {
|
2022-02-11 12:38:45 +01:00
|
|
|
transparent bool
|
|
|
|
}
|
2022-03-22 15:33:21 +01:00
|
|
|
|
2022-07-30 19:56:16 +02:00
|
|
|
func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, GraphicsLibrary, error) {
|
2023-03-30 19:21:08 +02:00
|
|
|
var dxErr error
|
2023-03-30 20:15:49 +02:00
|
|
|
var glErr error
|
2023-03-30 19:21:08 +02:00
|
|
|
if winver.IsWindows10OrGreater() {
|
|
|
|
d, err := g.newDirectX()
|
|
|
|
if err == nil {
|
|
|
|
return d, GraphicsLibraryDirectX, nil
|
|
|
|
}
|
|
|
|
dxErr = err
|
|
|
|
|
2023-03-30 20:15:49 +02:00
|
|
|
o, err := g.newOpenGL()
|
|
|
|
if err == nil {
|
|
|
|
return o, GraphicsLibraryOpenGL, nil
|
|
|
|
}
|
|
|
|
glErr = err
|
|
|
|
} else {
|
2023-03-30 20:21:48 +02:00
|
|
|
// Creating a swap chain on an older machine than Windows 10 might fail (#2613).
|
2023-03-30 20:15:49 +02:00
|
|
|
// Prefer OpenGL to DirectX.
|
|
|
|
o, err := g.newOpenGL()
|
|
|
|
if err == nil {
|
|
|
|
return o, GraphicsLibraryOpenGL, nil
|
|
|
|
}
|
|
|
|
glErr = err
|
|
|
|
|
|
|
|
// Initializing OpenGL can fail, though this is pretty rare.
|
|
|
|
d, err := g.newDirectX()
|
|
|
|
if err == nil {
|
|
|
|
return d, GraphicsLibraryDirectX, nil
|
|
|
|
}
|
|
|
|
dxErr = err
|
2022-06-17 04:39:43 +02:00
|
|
|
}
|
2023-03-30 19:21:08 +02:00
|
|
|
|
|
|
|
return nil, GraphicsLibraryUnknown, fmt.Errorf("ui: failed to choose graphics drivers: DirectX: %v, OpenGL: %v", dxErr, glErr)
|
2022-03-22 15:33:21 +01:00
|
|
|
}
|
|
|
|
|
2022-06-16 19:10:29 +02:00
|
|
|
func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
|
2022-11-13 07:25:22 +01:00
|
|
|
return opengl.NewGraphics()
|
2022-03-22 15:33:21 +01:00
|
|
|
}
|
|
|
|
|
2022-06-17 04:39:43 +02:00
|
|
|
func (g *graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
|
2022-02-11 12:38:45 +01:00
|
|
|
if g.transparent {
|
2022-06-17 04:39:43 +02:00
|
|
|
return nil, fmt.Errorf("ui: DirectX is not available with a transparent window")
|
2022-02-11 12:38:45 +01:00
|
|
|
}
|
2022-06-17 04:39:43 +02:00
|
|
|
return directx.NewGraphics()
|
2022-03-25 11:43:32 +01:00
|
|
|
}
|
|
|
|
|
2022-06-16 19:10:29 +02:00
|
|
|
func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {
|
|
|
|
return nil, nil
|
2022-03-22 15:33:21 +01:00
|
|
|
}
|
|
|
|
|
2023-09-29 17:20:59 +02:00
|
|
|
// glfwMonitorSizeInGLFWPixels must be called from the main thread.
|
2023-10-03 16:07:53 +02:00
|
|
|
func glfwMonitorSizeInGLFWPixels(m *glfw.Monitor) (int, int, error) {
|
|
|
|
vm, err := m.GetVideoMode()
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
return vm.Width, vm.Height, nil
|
2020-09-18 17:21:08 +02:00
|
|
|
}
|
|
|
|
|
2023-09-24 12:45:55 +02:00
|
|
|
func dipFromGLFWPixel(x float64, monitor *Monitor) float64 {
|
2023-09-24 12:40:43 +02:00
|
|
|
return x / monitor.deviceScaleFactor()
|
2020-09-18 17:31:34 +02:00
|
|
|
}
|
|
|
|
|
2023-09-24 12:45:55 +02:00
|
|
|
func dipToGLFWPixel(x float64, monitor *Monitor) float64 {
|
2023-09-24 12:40:43 +02:00
|
|
|
return x * monitor.deviceScaleFactor()
|
2016-07-04 04:37:34 +02:00
|
|
|
}
|
2017-04-18 17:51:15 +02:00
|
|
|
|
2023-09-23 19:05:42 +02:00
|
|
|
func (u *userInterfaceImpl) adjustWindowPosition(x, y int, monitor *Monitor) (int, int) {
|
2022-05-31 17:58:33 +02:00
|
|
|
if microsoftgdk.IsXbox() {
|
|
|
|
return x, y
|
|
|
|
}
|
|
|
|
|
2023-09-29 17:20:59 +02:00
|
|
|
mx := monitor.boundsInGLFWPixels.Min.X
|
|
|
|
my := monitor.boundsInGLFWPixels.Min.Y
|
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
|
|
|
}
|
2022-05-31 17:37:54 +02:00
|
|
|
t, err := _GetSystemMetrics(_SM_CYCAPTION)
|
2018-10-06 15:42:28 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-01-30 07:36:09 +01:00
|
|
|
if y < my+int(t) {
|
|
|
|
y = my + int(t)
|
2017-04-18 17:51:15 +02:00
|
|
|
}
|
|
|
|
return x, y
|
|
|
|
}
|
2018-10-06 15:42:28 +02:00
|
|
|
|
2023-09-23 19:05:42 +02:00
|
|
|
func initialMonitorByOS() (*Monitor, error) {
|
2022-05-31 17:58:33 +02:00
|
|
|
if microsoftgdk.IsXbox() {
|
2023-09-29 05:54:37 +02:00
|
|
|
return theMonitors.primaryMonitor(), nil
|
2022-05-31 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:37:54 +02:00
|
|
|
px, py, err := _GetCursorPos()
|
2022-07-04 07:42:17 +02:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, windows.ERROR_ACCESS_DENIED) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-02-07 15:31:08 +01:00
|
|
|
return nil, err
|
2021-04-18 13:05:37 +02:00
|
|
|
}
|
2022-02-07 15:31:08 +01:00
|
|
|
x, y := int(px), int(py)
|
|
|
|
|
|
|
|
// Find the monitor including the cursor.
|
2023-09-29 09:48:20 +02:00
|
|
|
return theMonitors.monitorFromPosition(x, y), nil
|
2021-10-09 09:49:47 +02:00
|
|
|
}
|
|
|
|
|
2023-10-03 16:07:53 +02:00
|
|
|
func monitorFromWindowByOS(w *glfw.Window) (*Monitor, error) {
|
2022-05-31 17:58:33 +02:00
|
|
|
if microsoftgdk.IsXbox() {
|
2023-10-03 16:07:53 +02:00
|
|
|
return theMonitors.primaryMonitor(), nil
|
|
|
|
}
|
|
|
|
window, err := w.GetWin32Window()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-05-31 17:58:33 +02:00
|
|
|
}
|
2023-10-03 16:07:53 +02:00
|
|
|
return monitorFromWin32Window(window), nil
|
2021-10-09 09:49:47 +02:00
|
|
|
}
|
2021-04-18 13:05:37 +02:00
|
|
|
|
2023-09-23 19:05:42 +02:00
|
|
|
func monitorFromWin32Window(w windows.HWND) *Monitor {
|
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
|
2023-01-28 11:06:38 +01:00
|
|
|
// position is not reliable in some cases e.g. when the window is put across multiple monitors.
|
2018-10-07 18:42:43 +02:00
|
|
|
|
2022-05-31 17:37:54 +02:00
|
|
|
m := _MonitorFromWindow(w, _MONITOR_DEFAULTTONEAREST)
|
2022-01-30 07:36:09 +01:00
|
|
|
if m == 0 {
|
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
|
|
|
}
|
|
|
|
|
2022-05-31 17:37:54 +02:00
|
|
|
mi, err := _GetMonitorInfoW(m)
|
|
|
|
if err != nil {
|
2018-10-06 15:42:28 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
x, y := int(mi.rcMonitor.left), int(mi.rcMonitor.top)
|
2023-09-15 17:08:33 +02:00
|
|
|
for _, m := range theMonitors.append(nil) {
|
2023-09-29 17:20:59 +02:00
|
|
|
mx := m.boundsInGLFWPixels.Min.X
|
|
|
|
my := m.boundsInGLFWPixels.Min.Y
|
|
|
|
if mx == x && my == y {
|
2023-09-23 19:05:42 +02:00
|
|
|
return m
|
2018-10-06 15:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2023-10-03 16:07:53 +02:00
|
|
|
func (u *userInterfaceImpl) nativeWindow() (uintptr, error) {
|
|
|
|
w, err := u.window.GetWin32Window()
|
|
|
|
return uintptr(w), err
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
2021-04-18 10:35:46 +02:00
|
|
|
|
2023-10-07 11:05:03 +02:00
|
|
|
func (u *userInterfaceImpl) isNativeFullscreen() (bool, error) {
|
|
|
|
return false, nil
|
2021-04-18 10:35:46 +02:00
|
|
|
}
|
2021-05-02 07:50:50 +02:00
|
|
|
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) isNativeFullscreenAvailable() bool {
|
2021-09-17 19:21:24 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-10-07 11:05:03 +02:00
|
|
|
func (u *userInterfaceImpl) setNativeFullscreen(fullscreen bool) error {
|
2022-02-06 08:08:22 +01:00
|
|
|
panic(fmt.Sprintf("ui: setNativeFullscreen is not implemented in this environment: %s", runtime.GOOS))
|
2021-09-17 19:21:24 +02:00
|
|
|
}
|
2021-09-18 15:11:26 +02:00
|
|
|
|
2023-10-07 11:05:03 +02:00
|
|
|
func (u *userInterfaceImpl) adjustViewSizeAfterFullscreen() error {
|
|
|
|
return nil
|
2021-09-18 15:11:26 +02:00
|
|
|
}
|
2021-09-24 19:46:18 +02:00
|
|
|
|
2023-10-07 11:05:03 +02:00
|
|
|
func (u *userInterfaceImpl) setWindowResizingModeForOS(mode WindowResizingMode) error {
|
|
|
|
return nil
|
2021-12-29 14:08:59 +01:00
|
|
|
}
|
|
|
|
|
2023-10-03 16:07:53 +02:00
|
|
|
func initializeWindowAfterCreation(w *glfw.Window) error {
|
|
|
|
return nil
|
2021-09-24 19:46:18 +02:00
|
|
|
}
|
2022-12-12 17:03:19 +01:00
|
|
|
|
|
|
|
func (u *userInterfaceImpl) skipTaskbar() error {
|
2022-12-13 05:01:32 +01:00
|
|
|
// S_FALSE is returned when CoInitializeEx is nested. This is a successful case.
|
|
|
|
if err := windows.CoInitializeEx(0, windows.COINIT_MULTITHREADED); err != nil && !errors.Is(err, syscall.Errno(windows.S_FALSE)) {
|
2022-12-12 17:03:19 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-12-13 05:01:32 +01:00
|
|
|
// CoUninitialize should be called even when CoInitializeEx returns S_FALSE.
|
2022-12-12 17:03:19 +01:00
|
|
|
defer windows.CoUninitialize()
|
|
|
|
|
|
|
|
ptr, err := _CoCreateInstance(&_CLSID_TaskbarList, nil, _CLSCTX_SERVER, &_IID_ITaskbarList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
t := (*_ITaskbarList)(ptr)
|
|
|
|
defer t.Release()
|
|
|
|
|
2023-10-03 16:07:53 +02:00
|
|
|
w, err := u.window.GetWin32Window()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := t.DeleteTab(w); err != nil {
|
2022-12-12 17:03:19 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|