2023-09-15 17:08:33 +02:00
|
|
|
// Copyright 2023 The Ebitengine 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.
|
|
|
|
|
2023-10-01 16:14:47 +02:00
|
|
|
//go:build !android && !ios && !js && !nintendosdk && !playstation5
|
2023-09-15 17:08:33 +02:00
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2023-09-29 17:20:59 +02:00
|
|
|
"image"
|
2023-09-15 17:08:33 +02:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Monitor is a wrapper around glfw.Monitor.
|
|
|
|
type Monitor struct {
|
2023-09-23 19:05:42 +02:00
|
|
|
m *glfw.Monitor
|
|
|
|
videoMode *glfw.VidMode
|
|
|
|
|
2023-09-29 17:20:59 +02:00
|
|
|
id int
|
|
|
|
name string
|
|
|
|
boundsInGLFWPixels image.Rectangle
|
|
|
|
contentScale float64
|
2023-09-15 17:08:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the monitor's name.
|
|
|
|
func (m *Monitor) Name() string {
|
|
|
|
return m.name
|
|
|
|
}
|
|
|
|
|
2024-02-12 07:03:07 +01:00
|
|
|
// DeviceScaleFactor is concurrent-safe as contentScale is immutable.
|
|
|
|
func (m *Monitor) DeviceScaleFactor() float64 {
|
2023-09-24 12:40:43 +02:00
|
|
|
// It is rare, but monitor can be nil when glfw.GetPrimaryMonitor returns nil.
|
|
|
|
// In this case, return 1 as a tentative scale (#1878).
|
|
|
|
if m == nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return m.contentScale
|
|
|
|
}
|
|
|
|
|
2023-09-27 17:52:12 +02:00
|
|
|
func (m *Monitor) sizeInDIP() (float64, float64) {
|
2023-09-29 17:20:59 +02:00
|
|
|
w, h := m.boundsInGLFWPixels.Dx(), m.boundsInGLFWPixels.Dy()
|
|
|
|
return dipFromGLFWPixel(float64(w), m), dipFromGLFWPixel(float64(h), m)
|
2023-09-25 11:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-09-15 17:08:33 +02:00
|
|
|
type monitors struct {
|
|
|
|
// monitors is the monitor list cache for desktop glfw compile targets.
|
|
|
|
// populated by 'updateMonitors' which is called on init and every
|
|
|
|
// monitor config change event.
|
|
|
|
monitors []*Monitor
|
|
|
|
|
|
|
|
m sync.Mutex
|
|
|
|
|
|
|
|
updateCalled int32
|
|
|
|
}
|
|
|
|
|
|
|
|
var theMonitors monitors
|
|
|
|
|
|
|
|
func (m *monitors) append(ms []*Monitor) []*Monitor {
|
|
|
|
if atomic.LoadInt32(&m.updateCalled) == 0 {
|
2023-09-23 18:31:32 +02:00
|
|
|
panic("ui: (*monitors).update must be called before (*monitors).append is called")
|
2023-09-15 17:08:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m.m.Lock()
|
|
|
|
defer m.m.Unlock()
|
|
|
|
|
|
|
|
return append(ms, m.monitors...)
|
|
|
|
}
|
|
|
|
|
2023-09-29 05:20:07 +02:00
|
|
|
func (m *monitors) primaryMonitor() *Monitor {
|
|
|
|
if atomic.LoadInt32(&m.updateCalled) == 0 {
|
2023-09-29 08:36:12 +02:00
|
|
|
panic("ui: (*monitors).update must be called before (*monitors).primaryMonitor is called")
|
2023-09-29 05:20:07 +02:00
|
|
|
}
|
|
|
|
|
2023-09-15 17:08:33 +02:00
|
|
|
m.m.Lock()
|
|
|
|
defer m.m.Unlock()
|
|
|
|
|
2023-09-29 05:20:07 +02:00
|
|
|
return m.monitors[0]
|
2023-09-15 17:08:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *monitors) monitorFromID(id int) *Monitor {
|
|
|
|
m.m.Lock()
|
|
|
|
defer m.m.Unlock()
|
|
|
|
|
|
|
|
return m.monitors[id]
|
|
|
|
}
|
|
|
|
|
2023-09-24 12:30:07 +02:00
|
|
|
// monitorFromPosition returns a monitor for the given position (x, y),
|
|
|
|
// or returns nil if monitor is not found.
|
2023-09-29 09:48:20 +02:00
|
|
|
// The position is in GLFW pixels.
|
2023-09-24 12:30:07 +02:00
|
|
|
func (m *monitors) monitorFromPosition(x, y int) *Monitor {
|
|
|
|
m.m.Lock()
|
|
|
|
defer m.m.Unlock()
|
|
|
|
|
|
|
|
for _, m := range m.monitors {
|
2023-09-29 09:48:20 +02:00
|
|
|
// Use an inclusive range. On macOS, the cursor position can take this range (#2794).
|
2023-09-29 17:20:59 +02:00
|
|
|
b := m.boundsInGLFWPixels
|
|
|
|
if b.Min.X <= x && x <= b.Max.X && b.Min.Y <= y && y <= b.Max.Y {
|
2023-09-24 12:30:07 +02:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-23 18:31:32 +02:00
|
|
|
// update must be called from the main thread.
|
2023-10-03 16:07:53 +02:00
|
|
|
func (m *monitors) update() error {
|
|
|
|
glfwMonitors, err := glfw.GetMonitors()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-15 17:08:33 +02:00
|
|
|
newMonitors := make([]*Monitor, 0, len(glfwMonitors))
|
|
|
|
for i, m := range glfwMonitors {
|
2023-10-03 16:07:53 +02:00
|
|
|
x, y, err := m.GetPos()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-24 08:41:54 +02:00
|
|
|
|
|
|
|
// TODO: Detect the update of the content scale by SetContentScaleCallback (#2343).
|
|
|
|
contentScale := 1.0
|
|
|
|
|
|
|
|
// Keep calling GetContentScale until the returned scale is 0 (#2051).
|
|
|
|
// Retry this at most 5 times to avoid an infinite loop.
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
// An error can happen e.g. when entering a screensaver on Windows (#2488).
|
|
|
|
sx, _, err := m.GetContentScale()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if sx == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
contentScale = float64(sx)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2023-10-03 16:07:53 +02:00
|
|
|
videoMode, err := m.GetVideoMode()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name, err := m.GetName()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w, h, err := glfwMonitorSizeInGLFWPixels(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-29 17:20:59 +02:00
|
|
|
b := image.Rect(x, y, x+w, y+h)
|
2023-09-15 17:08:33 +02:00
|
|
|
newMonitors = append(newMonitors, &Monitor{
|
2023-09-29 17:20:59 +02:00
|
|
|
m: m,
|
2023-10-03 16:07:53 +02:00
|
|
|
videoMode: videoMode,
|
2023-09-29 17:20:59 +02:00
|
|
|
id: i,
|
2023-10-03 16:07:53 +02:00
|
|
|
name: name,
|
2023-09-29 17:20:59 +02:00
|
|
|
boundsInGLFWPixels: b,
|
|
|
|
contentScale: contentScale,
|
2023-09-15 17:08:33 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
m.m.Lock()
|
|
|
|
m.monitors = newMonitors
|
|
|
|
m.m.Unlock()
|
|
|
|
|
|
|
|
atomic.StoreInt32(&m.updateCalled, 1)
|
2023-10-03 16:07:53 +02:00
|
|
|
return nil
|
2023-09-15 17:08:33 +02:00
|
|
|
}
|