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-08-12 05:40:23 +02:00
|
|
|
//go:build !android && !darwin && !js && !windows && !nintendosdk
|
2016-06-18 22:04:38 +02:00
|
|
|
|
2022-02-06 08:08:22 +01:00
|
|
|
package ui
|
2016-06-18 22:04:38 +02:00
|
|
|
|
2018-01-02 21:22:56 +01:00
|
|
|
import (
|
2021-09-18 09:58:30 +02:00
|
|
|
"fmt"
|
2021-09-15 05:47:04 +02:00
|
|
|
"math"
|
2021-09-17 19:21:24 +02:00
|
|
|
"runtime"
|
2021-09-15 05:47:04 +02:00
|
|
|
|
|
|
|
"github.com/jezek/xgb"
|
|
|
|
"github.com/jezek/xgb/randr"
|
|
|
|
"github.com/jezek/xgb/xproto"
|
2022-03-22 15:33:21 +01:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
|
2018-01-02 21:22:56 +01:00
|
|
|
)
|
2016-07-04 04:37:34 +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) {
|
|
|
|
graphics, err := g.newOpenGL()
|
|
|
|
return graphics, GraphicsLibraryOpenGL, err
|
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 (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
|
|
|
|
return nil, nil
|
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
|
|
|
}
|
|
|
|
|
2021-09-15 05:47:04 +02:00
|
|
|
type videoModeScaleCacheKey struct{ X, Y int }
|
|
|
|
|
|
|
|
var videoModeScaleCache = map[videoModeScaleCacheKey]float64{}
|
|
|
|
|
|
|
|
// clearVideoModeScaleCache must be called from the main thread.
|
|
|
|
func clearVideoModeScaleCache() {
|
|
|
|
for k := range videoModeScaleCache {
|
|
|
|
delete(videoModeScaleCache, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// videoModeScale must be called from the main thread.
|
|
|
|
func videoModeScale(m *glfw.Monitor) float64 {
|
2022-03-31 20:51:24 +02:00
|
|
|
if m == nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-09-15 05:47:04 +02:00
|
|
|
// Caching wrapper for videoModeScaleUncached as
|
|
|
|
// videoModeScaleUncached may be expensive (uses blocking calls on X connection)
|
|
|
|
// and public ScreenSizeInFullscreen API needs the videoModeScale.
|
|
|
|
monitorX, monitorY := m.GetPos()
|
|
|
|
cacheKey := videoModeScaleCacheKey{X: monitorX, Y: monitorY}
|
|
|
|
if cached, ok := videoModeScaleCache[cacheKey]; ok {
|
|
|
|
return cached
|
|
|
|
}
|
2021-09-22 19:48:22 +02:00
|
|
|
|
|
|
|
scale := videoModeScaleUncached(m)
|
2021-09-15 05:47:04 +02:00
|
|
|
videoModeScaleCache[cacheKey] = scale
|
|
|
|
return scale
|
|
|
|
}
|
|
|
|
|
|
|
|
// videoModeScaleUncached must be called from the main thread.
|
2021-09-22 19:48:22 +02:00
|
|
|
func videoModeScaleUncached(m *glfw.Monitor) float64 {
|
2021-09-19 11:18:18 +02:00
|
|
|
// TODO: if glfw/glfw#1961 gets fixed, this function may need revising.
|
2021-09-15 05:47:04 +02:00
|
|
|
// In case GLFW decides to switch to returning logical pixels, we can just return 1.
|
|
|
|
|
|
|
|
// Note: GLFW currently returns physical pixel sizes,
|
|
|
|
// but we need to predict the window system-side size of the fullscreen window
|
2023-02-07 04:05:43 +01:00
|
|
|
// for Ebitengine's `ScreenSizeInFullscreen` public API.
|
2023-01-28 11:06:38 +01:00
|
|
|
// Also at the moment we need this prior to switching to fullscreen, but that might be replaceable.
|
2021-09-15 05:47:04 +02:00
|
|
|
// So this function computes the ratio of physical per logical pixels.
|
|
|
|
xconn, err := xgb.NewConn()
|
|
|
|
if err != nil {
|
|
|
|
// No X11 connection?
|
|
|
|
// Assume we're on pure Wayland then.
|
|
|
|
// GLFW/Wayland shouldn't be having this issue.
|
|
|
|
return 1
|
|
|
|
}
|
2021-09-19 11:18:18 +02:00
|
|
|
defer xconn.Close()
|
|
|
|
|
2021-09-22 19:03:37 +02:00
|
|
|
if err := randr.Init(xconn); err != nil {
|
2021-09-19 11:18:18 +02:00
|
|
|
// No RANDR extension? No problem.
|
2021-09-15 05:47:04 +02:00
|
|
|
return 1
|
|
|
|
}
|
2021-09-19 11:18:18 +02:00
|
|
|
|
2021-09-15 05:47:04 +02:00
|
|
|
root := xproto.Setup(xconn).DefaultScreen(xconn).Root
|
|
|
|
res, err := randr.GetScreenResourcesCurrent(xconn, root).Reply()
|
|
|
|
if err != nil {
|
2021-09-19 11:18:18 +02:00
|
|
|
// Likely means RANDR is not working. No problem.
|
2021-09-15 05:47:04 +02:00
|
|
|
return 1
|
|
|
|
}
|
2021-09-19 11:18:18 +02:00
|
|
|
|
2021-09-22 19:48:22 +02:00
|
|
|
monitorX, monitorY := m.GetPos()
|
|
|
|
|
2021-09-15 05:47:04 +02:00
|
|
|
for _, crtc := range res.Crtcs[:res.NumCrtcs] {
|
|
|
|
info, err := randr.GetCrtcInfo(xconn, crtc, res.ConfigTimestamp).Reply()
|
|
|
|
if err != nil {
|
|
|
|
// This Crtc is bad. Maybe just got disconnected?
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if info.NumOutputs == 0 {
|
|
|
|
// This Crtc is not connected to any output.
|
|
|
|
// In other words, a disabled monitor.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if int(info.X) == monitorX && int(info.Y) == monitorY {
|
|
|
|
xWidth, xHeight := info.Width, info.Height
|
|
|
|
vm := m.GetVideoMode()
|
|
|
|
physWidth, physHeight := vm.Width, vm.Height
|
|
|
|
// Return one scale, even though there may be separate X and Y scales.
|
|
|
|
// Return the _larger_ scale, as this would yield a letterboxed display on mismatch, rather than a cut-off one.
|
|
|
|
scale := math.Max(float64(physWidth)/float64(xWidth), float64(physHeight)/float64(xHeight))
|
|
|
|
return scale
|
|
|
|
}
|
|
|
|
}
|
2021-09-19 11:18:18 +02:00
|
|
|
|
2021-09-15 05:47:04 +02:00
|
|
|
// Monitor not known to XRandR. Weird.
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-10-10 08:38:13 +02:00
|
|
|
// dipFromGLFWMonitorPixel must be called from the main thread.
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) dipFromGLFWMonitorPixel(x float64, monitor *glfw.Monitor) float64 {
|
2021-10-09 09:49:47 +02:00
|
|
|
return x / (videoModeScale(monitor) * u.deviceScaleFactor(monitor))
|
2020-09-18 17:21:08 +02:00
|
|
|
}
|
|
|
|
|
2021-10-10 08:38:13 +02:00
|
|
|
// dipFromGLFWPixel must be called from the main thread.
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) dipFromGLFWPixel(x float64, monitor *glfw.Monitor) float64 {
|
2021-10-09 09:49:47 +02:00
|
|
|
return x / u.deviceScaleFactor(monitor)
|
2020-09-18 17:31:34 +02:00
|
|
|
}
|
|
|
|
|
2021-10-10 08:38:13 +02:00
|
|
|
// dipToGLFWPixel must be called from the main thread.
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) dipToGLFWPixel(x float64, monitor *glfw.Monitor) float64 {
|
2021-10-09 09:49:47 +02:00
|
|
|
return x * u.deviceScaleFactor(monitor)
|
2020-09-18 17:56:39 +02:00
|
|
|
}
|
|
|
|
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) adjustWindowPosition(x, y int, monitor *glfw.Monitor) (int, int) {
|
2017-04-18 17:51:15 +02:00
|
|
|
return x, y
|
|
|
|
}
|
2018-10-06 15:42:28 +02:00
|
|
|
|
2022-02-07 15:31:08 +01:00
|
|
|
func initialMonitorByOS() (*glfw.Monitor, error) {
|
2022-02-07 19:10:59 +01:00
|
|
|
xconn, err := xgb.NewConn()
|
|
|
|
if err != nil {
|
|
|
|
// Assume we're on pure Wayland then.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
defer xconn.Close()
|
|
|
|
|
|
|
|
root := xproto.Setup(xconn).DefaultScreen(xconn).Root
|
|
|
|
rep, err := xproto.QueryPointer(xconn, root).Reply()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
x, y := int(rep.RootX), int(rep.RootY)
|
|
|
|
|
|
|
|
// Find the monitor including the cursor.
|
|
|
|
for _, m := range ensureMonitors() {
|
|
|
|
w, h := m.vm.Width, m.vm.Height
|
|
|
|
if x >= m.x && x < m.x+w && y >= m.y && y < m.y+h {
|
|
|
|
return m.m, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 15:31:08 +01:00
|
|
|
return nil, nil
|
2021-10-09 09:49:47 +02:00
|
|
|
}
|
|
|
|
|
2022-02-08 16:26:49 +01:00
|
|
|
func monitorFromWindowByOS(_ *glfw.Window) *glfw.Monitor {
|
2020-03-28 17:12:53 +01:00
|
|
|
// TODO: Implement this correctly. (#1119).
|
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
|
|
|
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) nativeWindow() uintptr {
|
2018-12-28 06:08:44 +01:00
|
|
|
// TODO: Implement this.
|
2020-09-03 17:43:51 +02:00
|
|
|
return 0
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
2021-04-18 10:35:46 +02:00
|
|
|
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) isNativeFullscreen() bool {
|
2021-04-18 10:35:46 +02:00
|
|
|
return false
|
|
|
|
}
|
2021-05-02 07:50:50 +02:00
|
|
|
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) setNativeCursor(shape CursorShape) {
|
2021-05-02 07:50:50 +02:00
|
|
|
// TODO: Use native API in the future (#1571)
|
|
|
|
u.window.SetCursor(glfwSystemCursors[shape])
|
|
|
|
}
|
2021-09-17 19:21:24 +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
|
|
|
|
}
|
|
|
|
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) setNativeFullscreen(fullscreen bool) {
|
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
|
|
|
|
2022-09-25 15:51:09 +02:00
|
|
|
func (u *userInterfaceImpl) adjustViewSizeAfterFullscreen() {
|
2021-09-18 15:11:26 +02:00
|
|
|
}
|
2021-09-24 19:46:18 +02:00
|
|
|
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) setWindowResizingModeForOS(mode WindowResizingMode) {
|
2021-12-29 14:08:59 +01:00
|
|
|
}
|
|
|
|
|
2021-09-24 19:46:18 +02:00
|
|
|
func initializeWindowAfterCreation(w *glfw.Window) {
|
|
|
|
// Show the window once before getting the position of the window.
|
|
|
|
// On Linux/Unix, the window position is not reliable before showing.
|
|
|
|
w.Show()
|
2021-09-28 15:15:59 +02:00
|
|
|
|
|
|
|
// Hiding the window makes the position unreliable again. Do not call w.Hide() here (#1829)
|
|
|
|
// Calling Hide is problematic especially on XWayland and/or Sway.
|
|
|
|
// Apparently the window state is inconsistent just after the window is created, but we are not sure.
|
|
|
|
// For more details, see the discussion in #1829.
|
2021-09-24 19:46:18 +02:00
|
|
|
}
|
2022-12-12 17:03:19 +01:00
|
|
|
|
|
|
|
func (u *userInterfaceImpl) skipTaskbar() error {
|
|
|
|
return nil
|
|
|
|
}
|