From cede5027d3b84c27c225c86c6fa4136d1639f8e9 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 7 Oct 2023 23:21:29 +0900 Subject: [PATCH] internal/glfw: merge internal/cglfw into internal/glfw Closes #2703 --- internal/cglfw/glfw_unix.go | 139 ------ internal/cglfw/native_darwin.go | 41 -- internal/{cglfw => glfw}/build_bsd.go | 2 +- internal/{cglfw => glfw}/build_darwin.go | 2 +- internal/{cglfw => glfw}/build_linux.go | 2 +- internal/glfw/callback_unix.go | 88 ---- internal/glfw/callback_windows.go | 45 -- internal/{cglfw => glfw}/cocoa_init_darwin.m | 0 .../{cglfw => glfw}/cocoa_monitor_darwin.m | 0 .../{cglfw => glfw}/cocoa_platform_darwin.h | 0 internal/{cglfw => glfw}/cocoa_time_darwin.c | 0 .../{cglfw => glfw}/cocoa_window_darwin.m | 0 internal/{cglfw => glfw}/context_unix.c | 0 internal/{cglfw => glfw}/context_unix.go | 2 +- internal/{cglfw => glfw}/egl_context_unix.c | 0 internal/{cglfw => glfw}/egl_context_unix.h | 0 internal/{cglfw => glfw}/error_unix.go | 45 +- internal/glfw/{glfw3h_windows.go => glfw3.go} | 2 + internal/{cglfw => glfw}/glfw3_unix.h | 0 internal/{cglfw => glfw}/glfw3native_unix.h | 0 internal/glfw/glfw_unix.go | 466 +++++------------- internal/{cglfw => glfw}/glx_context_linbsd.c | 0 internal/{cglfw => glfw}/glx_context_linbsd.h | 0 internal/{cglfw => glfw}/init_unix.c | 0 internal/{cglfw => glfw}/input_unix.c | 0 internal/{cglfw => glfw}/input_unix.go | 207 +------- internal/{cglfw => glfw}/internal_unix.h | 0 internal/{cglfw => glfw}/monitor_unix.c | 0 internal/{cglfw => glfw}/monitor_unix.go | 30 +- internal/glfw/native_darwin.go | 60 ++- internal/{cglfw => glfw}/native_linbsd.go | 2 +- .../{cglfw => glfw}/nsgl_context_darwin.h | 0 .../{cglfw => glfw}/nsgl_context_darwin.m | 0 .../{cglfw => glfw}/osmesa_context_unix.c | 0 .../{cglfw => glfw}/osmesa_context_unix.h | 0 internal/{cglfw => glfw}/posix_thread_unix.c | 0 internal/{cglfw => glfw}/posix_thread_unix.h | 0 internal/{cglfw => glfw}/posix_time_linbsd.c | 0 internal/{cglfw => glfw}/posix_time_linbsd.h | 0 internal/glfw/type_unix.go | 40 -- internal/{cglfw => glfw}/util_unix.go | 2 +- internal/{cglfw => glfw}/window_unix.c | 0 internal/{cglfw => glfw}/window_unix.go | 131 +---- internal/{cglfw => glfw}/x11_init_linbsd.c | 0 internal/{cglfw => glfw}/x11_monitor_linbsd.c | 0 .../{cglfw => glfw}/x11_platform_linbsd.h | 0 internal/{cglfw => glfw}/x11_window_linbsd.c | 0 internal/{cglfw => glfw}/xkb_unicode_linbsd.c | 0 internal/{cglfw => glfw}/xkb_unicode_linbsd.h | 0 internal/ui/input_glfw.go | 8 +- internal/ui/ui_glfw.go | 20 +- 51 files changed, 193 insertions(+), 1141 deletions(-) delete mode 100644 internal/cglfw/glfw_unix.go delete mode 100644 internal/cglfw/native_darwin.go rename internal/{cglfw => glfw}/build_bsd.go (96%) rename internal/{cglfw => glfw}/build_darwin.go (95%) rename internal/{cglfw => glfw}/build_linux.go (95%) delete mode 100644 internal/glfw/callback_unix.go delete mode 100644 internal/glfw/callback_windows.go rename internal/{cglfw => glfw}/cocoa_init_darwin.m (100%) rename internal/{cglfw => glfw}/cocoa_monitor_darwin.m (100%) rename internal/{cglfw => glfw}/cocoa_platform_darwin.h (100%) rename internal/{cglfw => glfw}/cocoa_time_darwin.c (100%) rename internal/{cglfw => glfw}/cocoa_window_darwin.m (100%) rename internal/{cglfw => glfw}/context_unix.c (100%) rename internal/{cglfw => glfw}/context_unix.go (99%) rename internal/{cglfw => glfw}/egl_context_unix.c (100%) rename internal/{cglfw => glfw}/egl_context_unix.h (100%) rename internal/{cglfw => glfw}/error_unix.go (51%) rename internal/glfw/{glfw3h_windows.go => glfw3.go} (85%) rename internal/{cglfw => glfw}/glfw3_unix.h (100%) rename internal/{cglfw => glfw}/glfw3native_unix.h (100%) rename internal/{cglfw => glfw}/glx_context_linbsd.c (100%) rename internal/{cglfw => glfw}/glx_context_linbsd.h (100%) rename internal/{cglfw => glfw}/init_unix.c (100%) rename internal/{cglfw => glfw}/input_unix.c (100%) rename internal/{cglfw => glfw}/input_unix.go (70%) rename internal/{cglfw => glfw}/internal_unix.h (100%) rename internal/{cglfw => glfw}/monitor_unix.c (100%) rename internal/{cglfw => glfw}/monitor_unix.go (90%) rename internal/{cglfw => glfw}/native_linbsd.go (99%) rename internal/{cglfw => glfw}/nsgl_context_darwin.h (100%) rename internal/{cglfw => glfw}/nsgl_context_darwin.m (100%) rename internal/{cglfw => glfw}/osmesa_context_unix.c (100%) rename internal/{cglfw => glfw}/osmesa_context_unix.h (100%) rename internal/{cglfw => glfw}/posix_thread_unix.c (100%) rename internal/{cglfw => glfw}/posix_thread_unix.h (100%) rename internal/{cglfw => glfw}/posix_time_linbsd.c (100%) rename internal/{cglfw => glfw}/posix_time_linbsd.h (100%) delete mode 100644 internal/glfw/type_unix.go rename internal/{cglfw => glfw}/util_unix.go (96%) rename internal/{cglfw => glfw}/window_unix.c (100%) rename internal/{cglfw => glfw}/window_unix.go (80%) rename internal/{cglfw => glfw}/x11_init_linbsd.c (100%) rename internal/{cglfw => glfw}/x11_monitor_linbsd.c (100%) rename internal/{cglfw => glfw}/x11_platform_linbsd.h (100%) rename internal/{cglfw => glfw}/x11_window_linbsd.c (100%) rename internal/{cglfw => glfw}/xkb_unicode_linbsd.c (100%) rename internal/{cglfw => glfw}/xkb_unicode_linbsd.h (100%) diff --git a/internal/cglfw/glfw_unix.go b/internal/cglfw/glfw_unix.go deleted file mode 100644 index 6324e03e6..000000000 --- a/internal/cglfw/glfw_unix.go +++ /dev/null @@ -1,139 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// SPDX-FileCopyrightText: 2012 The glfw3-go Authors -// SPDX-FileCopyrightText: 2023 The Ebitengine Authors - -//go:build darwin || freebsd || linux || netbsd || openbsd - -package cglfw - -//#include -//#define GLFW_INCLUDE_NONE -//#include "glfw3_unix.h" -import "C" - -import ( - "errors" - "unsafe" -) - -// Version constants. -const ( - VersionMajor = C.GLFW_VERSION_MAJOR // This is incremented when the API is changed in non-compatible ways. - VersionMinor = C.GLFW_VERSION_MINOR // This is incremented when features are added to the API but it remains backward-compatible. - VersionRevision = C.GLFW_VERSION_REVISION // This is incremented when a bug fix release is made that does not contain any API changes. -) - -// Init initializes the GLFW library. Before most GLFW functions can be used, -// GLFW must be initialized, and before a program terminates GLFW should be -// terminated in order to free any resources allocated during or after -// initialization. -// -// If this function fails, it calls Terminate before returning. If it succeeds, -// you should call Terminate before the program exits. -// -// Additional calls to this function after successful initialization but before -// termination will succeed but will do nothing. -// -// This function may take several seconds to complete on some systems, while on -// other systems it may take only a fraction of a second to complete. -// -// On Mac OS X, this function will change the current directory of the -// application to the Contents/Resources subdirectory of the application's -// bundle, if present. -// -// This function may only be called from the main thread. -func Init() error { - C.glfwInit() - if err := fetchErrorIgnoringPlatformError(); err != nil { - return err - } - return nil -} - -// Terminate destroys all remaining windows, frees any allocated resources and -// sets the library to an uninitialized state. Once this is called, you must -// again call Init successfully before you will be able to use most GLFW -// functions. -// -// If GLFW has been successfully initialized, this function should be called -// before the program exits. If initialization fails, there is no need to call -// this function, as it is called by Init before it returns failure. -// -// This function may only be called from the main thread. -func Terminate() error { - C.glfwTerminate() - if err := fetchErrorIgnoringPlatformError(); err != nil { - return err - } - return nil -} - -// InitHint function sets hints for the next initialization of GLFW. -// -// The values you set hints to are never reset by GLFW, but they only take -// effect during initialization. Once GLFW has been initialized, any values you -// set will be ignored until the library is terminated and initialized again. -// -// Some hints are platform specific. These may be set on any platform but they -// will only affect their specific platform. Other platforms will ignore them. -// Setting these hints requires no platform specific headers or functions. -// -// This function must only be called from the main thread. -func InitHint(hint Hint, value int) { - C.glfwInitHint(C.int(hint), C.int(value)) -} - -// GetVersion retrieves the major, minor and revision numbers of the GLFW -// library. It is intended for when you are using GLFW as a shared library and -// want to ensure that you are using the minimum required version. -// -// This function may be called before Init. -func GetVersion() (major, minor, revision int) { - var ( - maj C.int - min C.int - rev C.int - ) - - C.glfwGetVersion(&maj, &min, &rev) - return int(maj), int(min), int(rev) -} - -// GetVersionString returns a static string generated at compile-time according -// to which configuration macros were defined. This is intended for use when -// submitting bug reports, to allow developers to see which code paths are -// enabled in a binary. -// -// This function may be called before Init. -func GetVersionString() string { - return C.GoString(C.glfwGetVersionString()) -} - -// GetClipboardString returns the contents of the system clipboard, if it -// contains or is convertible to a UTF-8 encoded string. -// -// This function may only be called from the main thread. -func GetClipboardString() (string, error) { - cs := C.glfwGetClipboardString(nil) - if cs == nil { - if err := fetchErrorIgnoringPlatformError(); err != nil { - if errors.Is(err, FormatUnavailable) { - return "", nil - } - return "", err - } - return "", nil - } - return C.GoString(cs), nil -} - -// SetClipboardString sets the system clipboard to the specified UTF-8 encoded -// string. -// -// This function may only be called from the main thread. -func SetClipboardString(str string) error { - cp := C.CString(str) - defer C.free(unsafe.Pointer(cp)) - C.glfwSetClipboardString(nil, cp) - return fetchErrorIgnoringPlatformError() -} diff --git a/internal/cglfw/native_darwin.go b/internal/cglfw/native_darwin.go deleted file mode 100644 index e4ae139a9..000000000 --- a/internal/cglfw/native_darwin.go +++ /dev/null @@ -1,41 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// SPDX-FileCopyrightText: 2012 The glfw3-go Authors -// SPDX-FileCopyrightText: 2023 The Ebitengine Authors - -package cglfw - -/* -#define GLFW_EXPOSE_NATIVE_COCOA -#define GLFW_EXPOSE_NATIVE_NSGL -#include "glfw3_unix.h" -#include "glfw3native_unix.h" - -// workaround wrappers needed due to a cgo and/or LLVM bug. -// See: https://github.com/go-gl/glfw/issues/136 -static void *workaround_glfwGetCocoaWindow(GLFWwindow *w) { - return (void *)glfwGetCocoaWindow(w); -} -static void *workaround_glfwGetNSGLContext(GLFWwindow *w) { - return (void *)glfwGetNSGLContext(w); -} -*/ -import "C" -import "unsafe" - -// GetCocoaMonitor returns the CGDirectDisplayID of the monitor. -func (m *Monitor) GetCocoaMonitor() (uintptr, error) { - ret := uintptr(C.glfwGetCocoaMonitor(m.data)) - return ret, fetchErrorIgnoringPlatformError() -} - -// GetCocoaWindow returns the NSWindow of the window. -func (w *Window) GetCocoaWindow() (unsafe.Pointer, error) { - ret := C.workaround_glfwGetCocoaWindow(w.data) - return ret, fetchErrorIgnoringPlatformError() -} - -// GetNSGLContext returns the NSOpenGLContext of the window. -func (w *Window) GetNSGLContext() (unsafe.Pointer, error) { - ret := C.workaround_glfwGetNSGLContext(w.data) - return ret, fetchErrorIgnoringPlatformError() -} diff --git a/internal/cglfw/build_bsd.go b/internal/glfw/build_bsd.go similarity index 96% rename from internal/cglfw/build_bsd.go rename to internal/glfw/build_bsd.go index c57b8ecc7..176ea7cd7 100644 --- a/internal/cglfw/build_bsd.go +++ b/internal/glfw/build_bsd.go @@ -4,7 +4,7 @@ //go:build freebsd || netbsd || openbsd -package cglfw +package glfw import "C" diff --git a/internal/cglfw/build_darwin.go b/internal/glfw/build_darwin.go similarity index 95% rename from internal/cglfw/build_darwin.go rename to internal/glfw/build_darwin.go index 38d79200a..309167004 100644 --- a/internal/cglfw/build_darwin.go +++ b/internal/glfw/build_darwin.go @@ -2,7 +2,7 @@ // SPDX-FileCopyrightText: 2012 The glfw3-go Authors // SPDX-FileCopyrightText: 2023 The Ebitengine Authors -package cglfw +package glfw // #cgo CFLAGS: -D_GLFW_COCOA -Wno-deprecated-declarations // #cgo LDFLAGS: -framework Cocoa -framework IOKit -framework CoreVideo diff --git a/internal/cglfw/build_linux.go b/internal/glfw/build_linux.go similarity index 95% rename from internal/cglfw/build_linux.go rename to internal/glfw/build_linux.go index 5f48d1269..2f89a0782 100644 --- a/internal/cglfw/build_linux.go +++ b/internal/glfw/build_linux.go @@ -2,7 +2,7 @@ // SPDX-FileCopyrightText: 2012 The glfw3-go Authors // SPDX-FileCopyrightText: 2023 The Ebitengine Authors -package cglfw +package glfw // #cgo CFLAGS: -D_GLFW_X11 // #cgo LDFLAGS: -lX11 -lXrandr -lXxf86vm -lXi -lXcursor -lm -lXinerama -ldl -lrt diff --git a/internal/glfw/callback_unix.go b/internal/glfw/callback_unix.go deleted file mode 100644 index f5ecd9d29..000000000 --- a/internal/glfw/callback_unix.go +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright 2021 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. - -//go:build darwin || freebsd || linux || netbsd || openbsd - -package glfw - -import ( - "github.com/hajimehoshi/ebiten/v2/internal/cglfw" -) - -func ToCharModsCallback(cb func(window *Window, char rune, mods ModifierKey)) CharModsCallback { - if cb == nil { - return nil - } - return func(window *cglfw.Window, char rune, mods cglfw.ModifierKey) { - cb(theWindows.get(window), char, ModifierKey(mods)) - } -} - -func ToCloseCallback(cb func(window *Window)) CloseCallback { - if cb == nil { - return nil - } - return func(window *cglfw.Window) { - cb(theWindows.get(window)) - } -} - -func ToDropCallback(cb func(window *Window, names []string)) DropCallback { - if cb == nil { - return nil - } - return func(window *cglfw.Window, names []string) { - cb(theWindows.get(window), names) - } -} - -func ToFramebufferSizeCallback(cb func(window *Window, width int, height int)) FramebufferSizeCallback { - if cb == nil { - return nil - } - return func(window *cglfw.Window, width int, height int) { - cb(theWindows.get(window), width, height) - } -} - -func ToMonitorCallback(cb func(monitor *Monitor, event PeripheralEvent)) MonitorCallback { - if cb == nil { - return nil - } - return func(monitor *cglfw.Monitor, event cglfw.PeripheralEvent) { - var m *Monitor - if monitor != nil { - m = &Monitor{monitor} - } - cb(m, PeripheralEvent(event)) - } -} - -func ToScrollCallback(cb func(window *Window, xoff float64, yoff float64)) ScrollCallback { - if cb == nil { - return nil - } - return func(window *cglfw.Window, xoff float64, yoff float64) { - cb(theWindows.get(window), xoff, yoff) - } -} - -func ToSizeCallback(cb func(window *Window, width int, height int)) SizeCallback { - if cb == nil { - return nil - } - return func(window *cglfw.Window, width, height int) { - cb(theWindows.get(window), width, height) - } -} diff --git a/internal/glfw/callback_windows.go b/internal/glfw/callback_windows.go deleted file mode 100644 index 717a04910..000000000 --- a/internal/glfw/callback_windows.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2021 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. - -//go:build windows - -package glfw - -func ToCharModsCallback(cb func(window *Window, char rune, mods ModifierKey)) CharModsCallback { - return cb -} - -func ToCloseCallback(cb func(window *Window)) CloseCallback { - return cb -} - -func ToDropCallback(cb func(window *Window, names []string)) DropCallback { - return cb -} - -func ToFramebufferSizeCallback(cb func(window *Window, width int, height int)) FramebufferSizeCallback { - return cb -} - -func ToMonitorCallback(cb func(monitor *Monitor, event PeripheralEvent)) MonitorCallback { - return cb -} - -func ToScrollCallback(cb func(window *Window, xoff float64, yoff float64)) ScrollCallback { - return cb -} - -func ToSizeCallback(cb func(window *Window, width int, height int)) SizeCallback { - return cb -} diff --git a/internal/cglfw/cocoa_init_darwin.m b/internal/glfw/cocoa_init_darwin.m similarity index 100% rename from internal/cglfw/cocoa_init_darwin.m rename to internal/glfw/cocoa_init_darwin.m diff --git a/internal/cglfw/cocoa_monitor_darwin.m b/internal/glfw/cocoa_monitor_darwin.m similarity index 100% rename from internal/cglfw/cocoa_monitor_darwin.m rename to internal/glfw/cocoa_monitor_darwin.m diff --git a/internal/cglfw/cocoa_platform_darwin.h b/internal/glfw/cocoa_platform_darwin.h similarity index 100% rename from internal/cglfw/cocoa_platform_darwin.h rename to internal/glfw/cocoa_platform_darwin.h diff --git a/internal/cglfw/cocoa_time_darwin.c b/internal/glfw/cocoa_time_darwin.c similarity index 100% rename from internal/cglfw/cocoa_time_darwin.c rename to internal/glfw/cocoa_time_darwin.c diff --git a/internal/cglfw/cocoa_window_darwin.m b/internal/glfw/cocoa_window_darwin.m similarity index 100% rename from internal/cglfw/cocoa_window_darwin.m rename to internal/glfw/cocoa_window_darwin.m diff --git a/internal/cglfw/context_unix.c b/internal/glfw/context_unix.c similarity index 100% rename from internal/cglfw/context_unix.c rename to internal/glfw/context_unix.c diff --git a/internal/cglfw/context_unix.go b/internal/glfw/context_unix.go similarity index 99% rename from internal/cglfw/context_unix.go rename to internal/glfw/context_unix.go index 7be253ae7..63dd7ded9 100644 --- a/internal/cglfw/context_unix.go +++ b/internal/glfw/context_unix.go @@ -4,7 +4,7 @@ //go:build darwin || freebsd || linux || netbsd || openbsd -package cglfw +package glfw //#include //#define GLFW_INCLUDE_NONE diff --git a/internal/cglfw/egl_context_unix.c b/internal/glfw/egl_context_unix.c similarity index 100% rename from internal/cglfw/egl_context_unix.c rename to internal/glfw/egl_context_unix.c diff --git a/internal/cglfw/egl_context_unix.h b/internal/glfw/egl_context_unix.h similarity index 100% rename from internal/cglfw/egl_context_unix.h rename to internal/glfw/egl_context_unix.h diff --git a/internal/cglfw/error_unix.go b/internal/glfw/error_unix.go similarity index 51% rename from internal/cglfw/error_unix.go rename to internal/glfw/error_unix.go index 196945c61..6fdf77392 100644 --- a/internal/cglfw/error_unix.go +++ b/internal/glfw/error_unix.go @@ -4,7 +4,7 @@ //go:build darwin || freebsd || linux || netbsd || openbsd -package cglfw +package glfw // #define GLFW_INCLUDE_NONE // #include "glfw3_unix.h" @@ -22,49 +22,6 @@ import ( "os" ) -// ErrorCode corresponds to an error code. -type ErrorCode int - -const ( - NotInitialized = ErrorCode(0x00010001) - NoCurrentContext = ErrorCode(0x00010002) - InvalidEnum = ErrorCode(0x00010003) - InvalidValue = ErrorCode(0x00010004) - OutOfMemory = ErrorCode(0x00010005) - APIUnavailable = ErrorCode(0x00010006) - VersionUnavailable = ErrorCode(0x00010007) - PlatformError = ErrorCode(0x00010008) - FormatUnavailable = ErrorCode(0x00010009) - NoWindowContext = ErrorCode(0x0001000A) -) - -func (e ErrorCode) Error() string { - switch e { - case NotInitialized: - return "the GLFW library is not initialized" - case NoCurrentContext: - return "there is no current context" - case InvalidEnum: - return "invalid argument for enum parameter" - case InvalidValue: - return "invalid value for parameter" - case OutOfMemory: - return "out of memory" - case APIUnavailable: - return "the requested API is unavailable" - case VersionUnavailable: - return "the requested API version is unavailable" - case PlatformError: - return "a platform-specific error occurred" - case FormatUnavailable: - return "the requested format is unavailable" - case NoWindowContext: - return "the specified window has no context" - default: - return fmt.Sprintf("GLFW error (%d)", e) - } -} - // Note: There are many cryptic caveats to proper error handling here. // See: https://github.com/go-gl/glfw3/pull/86 diff --git a/internal/glfw/glfw3h_windows.go b/internal/glfw/glfw3.go similarity index 85% rename from internal/glfw/glfw3h_windows.go rename to internal/glfw/glfw3.go index f295e1369..b4d1fa0e1 100644 --- a/internal/glfw/glfw3h_windows.go +++ b/internal/glfw/glfw3.go @@ -3,6 +3,8 @@ // SPDX-FileCopyrightText: 2006-2019 Camilla Löwy // SPDX-FileCopyrightText: 2022 The Ebitengine Authors +//go:build darwin || freebsd || linux || netbsd || openbsd || windows + package glfw type VidMode struct { diff --git a/internal/cglfw/glfw3_unix.h b/internal/glfw/glfw3_unix.h similarity index 100% rename from internal/cglfw/glfw3_unix.h rename to internal/glfw/glfw3_unix.h diff --git a/internal/cglfw/glfw3native_unix.h b/internal/glfw/glfw3native_unix.h similarity index 100% rename from internal/cglfw/glfw3native_unix.h rename to internal/glfw/glfw3native_unix.h diff --git a/internal/glfw/glfw_unix.go b/internal/glfw/glfw_unix.go index 199b21dac..6f0baa9a8 100644 --- a/internal/glfw/glfw_unix.go +++ b/internal/glfw/glfw_unix.go @@ -1,369 +1,139 @@ -// Copyright 2018 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. +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2012 The glfw3-go Authors +// SPDX-FileCopyrightText: 2023 The Ebitengine Authors //go:build darwin || freebsd || linux || netbsd || openbsd package glfw +//#include +//#define GLFW_INCLUDE_NONE +//#include "glfw3_unix.h" +import "C" + import ( - "image" - "sync" - - "github.com/hajimehoshi/ebiten/v2/internal/cglfw" + "errors" + "unsafe" ) -type windows map[*cglfw.Window]*Window - -var ( - theWindows = windows{} - windowsM sync.Mutex +// Version constants. +const ( + VersionMajor = C.GLFW_VERSION_MAJOR // This is incremented when the API is changed in non-compatible ways. + VersionMinor = C.GLFW_VERSION_MINOR // This is incremented when features are added to the API but it remains backward-compatible. + VersionRevision = C.GLFW_VERSION_REVISION // This is incremented when a bug fix release is made that does not contain any API changes. ) -func (w windows) add(win *cglfw.Window) *Window { - if win == nil { - return nil - } - ww := &Window{w: win} - windowsM.Lock() - w[win] = ww - windowsM.Unlock() - return ww -} - -func (w windows) remove(win *cglfw.Window) { - windowsM.Lock() - delete(w, win) - windowsM.Unlock() -} - -func (w windows) get(win *cglfw.Window) *Window { - if win == nil { - return nil - } - windowsM.Lock() - ww := w[win] - windowsM.Unlock() - return ww -} - -type Cursor struct { - c *cglfw.Cursor -} - -func CreateStandardCursor(shape StandardCursor) (*Cursor, error) { - c, err := cglfw.CreateStandardCursor(cglfw.StandardCursor(shape)) - if err != nil { - return nil, err - } - return &Cursor{c: c}, nil -} - -type Monitor struct { - m *cglfw.Monitor -} - -func (m *Monitor) GetContentScale() (float32, float32, error) { - x, y := m.m.GetContentScale() - return x, y, nil -} - -func (m *Monitor) GetPos() (x, y int, err error) { - return m.m.GetPos() -} - -func (m *Monitor) GetVideoMode() (*VidMode, error) { - v, err := m.m.GetVideoMode() - if err != nil { - return nil, err - } - if v == nil { - return nil, nil - } - return &VidMode{ - Width: v.Width, - Height: v.Height, - RedBits: v.RedBits, - GreenBits: v.GreenBits, - BlueBits: v.BlueBits, - RefreshRate: v.RefreshRate, - }, nil -} - -func (m *Monitor) GetName() (string, error) { - return m.m.GetName() -} - -type Window struct { - w *cglfw.Window -} - -func (w *Window) Destroy() error { - if err := w.w.Destroy(); err != nil { - return err - } - theWindows.remove(w.w) - return nil -} - -func (w *Window) Focus() error { - w.w.Focus() - return nil -} - -func (w *Window) GetAttrib(attrib Hint) (int, error) { - return w.w.GetAttrib(cglfw.Hint(attrib)) -} - -func (w *Window) GetCursorPos() (x, y float64, err error) { - return w.w.GetCursorPos() -} - -func (w *Window) GetInputMode(mode InputMode) (int, error) { - return w.w.GetInputMode(cglfw.InputMode(mode)) -} - -func (w *Window) GetKey(key Key) (Action, error) { - a, err := w.w.GetKey(cglfw.Key(key)) - if err != nil { - return 0, err - } - return Action(a), nil -} - -func (w *Window) GetMonitor() (*Monitor, error) { - m, err := w.w.GetMonitor() - if err != nil { - return nil, err - } - if m == nil { - return nil, nil - } - return &Monitor{m}, nil -} - -func (w *Window) GetMouseButton(button MouseButton) (Action, error) { - a, err := w.w.GetMouseButton(cglfw.MouseButton(button)) - if err != nil { - return 0, err - } - return Action(a), nil -} - -func (w *Window) GetPos() (x, y int, err error) { - return w.w.GetPos() -} - -func (w *Window) GetSize() (width, height int, err error) { - return w.w.GetSize() -} - -func (w *Window) Hide() error { - return w.w.Hide() -} - -func (w *Window) Iconify() error { - w.w.Iconify() - return nil -} - -func (w *Window) MakeContextCurrent() error { - return w.w.MakeContextCurrent() -} - -func (w *Window) Maximize() error { - w.w.Maximize() - return nil -} - -func (w *Window) Restore() error { - w.w.Restore() - return nil -} - -func (w *Window) SetAttrib(attrib Hint, value int) error { - w.w.SetAttrib(cglfw.Hint(attrib), value) - return nil -} - -func (w *Window) SetCharModsCallback(cbfun CharModsCallback) (previous CharModsCallback, err error) { - return w.w.SetCharModsCallback(cbfun) -} - -func (w *Window) SetCloseCallback(cbfun CloseCallback) (previous CloseCallback, err error) { - return w.w.SetCloseCallback(cbfun) -} - -func (w *Window) SetCursor(cursor *Cursor) error { - var c *cglfw.Cursor - if cursor != nil { - c = cursor.c - } - return w.w.SetCursor(c) -} - -func (w *Window) SetCursorPos(xpos, ypos float64) error { - return w.w.SetCursorPos(xpos, ypos) -} - -func (w *Window) SetDropCallback(cbfun DropCallback) (previous DropCallback, err error) { - return w.w.SetDropCallback(cbfun) -} - -func (w *Window) SetFramebufferSizeCallback(cbfun FramebufferSizeCallback) (previous FramebufferSizeCallback, err error) { - return w.w.SetFramebufferSizeCallback(cbfun) -} - -func (w *Window) SetScrollCallback(cbfun ScrollCallback) (previous ScrollCallback, err error) { - return w.w.SetScrollCallback(cbfun) -} - -func (w *Window) SetShouldClose(value bool) error { - return w.w.SetShouldClose(value) -} - -func (w *Window) SetSizeCallback(cbfun SizeCallback) (previous SizeCallback, err error) { - return w.w.SetSizeCallback(cbfun) -} - -func (w *Window) SetSizeLimits(minw, minh, maxw, maxh int) error { - return w.w.SetSizeLimits(minw, minh, maxw, maxh) -} - -func (w *Window) SetIcon(images []image.Image) error { - return w.w.SetIcon(images) -} - -func (w *Window) SetInputMode(mode InputMode, value int) error { - return w.w.SetInputMode(cglfw.InputMode(mode), value) -} - -func (w *Window) SetMonitor(monitor *Monitor, xpos, ypos, width, height, refreshRate int) error { - var m *cglfw.Monitor - if monitor != nil { - m = monitor.m - } - if err := w.w.SetMonitor(m, xpos, ypos, width, height, refreshRate); err != nil { - return err - } - return nil -} - -func (w *Window) SetPos(xpos, ypos int) error { - return w.w.SetPos(xpos, ypos) -} - -func (w *Window) SetSize(width, height int) error { - return w.w.SetSize(width, height) -} - -func (w *Window) SetTitle(title string) error { - return w.w.SetTitle(title) -} - -func (w *Window) ShouldClose() (bool, error) { - return w.w.ShouldClose() -} - -func (w *Window) Show() error { - return w.w.Show() -} - -func (w *Window) SwapBuffers() error { - return w.w.SwapBuffers() -} - -func CreateWindow(width, height int, title string, monitor *Monitor, share *Window) (*Window, error) { - var gm *cglfw.Monitor - if monitor != nil { - gm = monitor.m - } - var gw *cglfw.Window - if share != nil { - gw = share.w - } - - w, err := cglfw.CreateWindow(width, height, title, gm, gw) - if err != nil { - return nil, err - } - return theWindows.add(w), nil -} - -func GetKeyName(key Key, scancode int) (string, error) { - return cglfw.GetKeyName(cglfw.Key(key), scancode) -} - -func GetMonitors() ([]*Monitor, error) { - monitors, err := cglfw.GetMonitors() - if err != nil { - return nil, err - } - var ms []*Monitor - for _, m := range monitors { - if m != nil { - ms = append(ms, &Monitor{m}) - } else { - ms = append(ms, nil) - } - } - return ms, nil -} - -func GetPrimaryMonitor() (*Monitor, error) { - m, err := cglfw.GetPrimaryMonitor() - if err != nil { - return nil, err - } - if m == nil { - return nil, nil - } - return &Monitor{m}, nil -} - +// Init initializes the GLFW library. Before most GLFW functions can be used, +// GLFW must be initialized, and before a program terminates GLFW should be +// terminated in order to free any resources allocated during or after +// initialization. +// +// If this function fails, it calls Terminate before returning. If it succeeds, +// you should call Terminate before the program exits. +// +// Additional calls to this function after successful initialization but before +// termination will succeed but will do nothing. +// +// This function may take several seconds to complete on some systems, while on +// other systems it may take only a fraction of a second to complete. +// +// On Mac OS X, this function will change the current directory of the +// application to the Contents/Resources subdirectory of the application's +// bundle, if present. +// +// This function may only be called from the main thread. func Init() error { - return cglfw.Init() -} - -func PollEvents() error { - return cglfw.PollEvents() -} - -func PostEmptyEvent() error { - return cglfw.PostEmptyEvent() -} - -func SetMonitorCallback(cbfun MonitorCallback) (MonitorCallback, error) { - cglfw.SetMonitorCallback(cbfun) - return ToMonitorCallback(nil), nil -} - -func SwapInterval(interval int) error { - return cglfw.SwapInterval(interval) + C.glfwInit() + if err := fetchErrorIgnoringPlatformError(); err != nil { + return err + } + return nil } +// Terminate destroys all remaining windows, frees any allocated resources and +// sets the library to an uninitialized state. Once this is called, you must +// again call Init successfully before you will be able to use most GLFW +// functions. +// +// If GLFW has been successfully initialized, this function should be called +// before the program exits. If initialization fails, there is no need to call +// this function, as it is called by Init before it returns failure. +// +// This function may only be called from the main thread. func Terminate() error { - return cglfw.Terminate() + C.glfwTerminate() + if err := fetchErrorIgnoringPlatformError(); err != nil { + return err + } + return nil } -func WaitEvents() error { - return cglfw.WaitEvents() +// InitHint function sets hints for the next initialization of GLFW. +// +// The values you set hints to are never reset by GLFW, but they only take +// effect during initialization. Once GLFW has been initialized, any values you +// set will be ignored until the library is terminated and initialized again. +// +// Some hints are platform specific. These may be set on any platform but they +// will only affect their specific platform. Other platforms will ignore them. +// Setting these hints requires no platform specific headers or functions. +// +// This function must only be called from the main thread. +func InitHint(hint Hint, value int) { + C.glfwInitHint(C.int(hint), C.int(value)) } -func WaitEventsTimeout(timeout float64) error { - return cglfw.WaitEventsTimeout(timeout) +// GetVersion retrieves the major, minor and revision numbers of the GLFW +// library. It is intended for when you are using GLFW as a shared library and +// want to ensure that you are using the minimum required version. +// +// This function may be called before Init. +func GetVersion() (major, minor, revision int) { + var ( + maj C.int + min C.int + rev C.int + ) + + C.glfwGetVersion(&maj, &min, &rev) + return int(maj), int(min), int(rev) } -func WindowHint(target Hint, hint int) error { - return cglfw.WindowHint(cglfw.Hint(target), hint) +// GetVersionString returns a static string generated at compile-time according +// to which configuration macros were defined. This is intended for use when +// submitting bug reports, to allow developers to see which code paths are +// enabled in a binary. +// +// This function may be called before Init. +func GetVersionString() string { + return C.GoString(C.glfwGetVersionString()) +} + +// GetClipboardString returns the contents of the system clipboard, if it +// contains or is convertible to a UTF-8 encoded string. +// +// This function may only be called from the main thread. +func GetClipboardString() (string, error) { + cs := C.glfwGetClipboardString(nil) + if cs == nil { + if err := fetchErrorIgnoringPlatformError(); err != nil { + if errors.Is(err, FormatUnavailable) { + return "", nil + } + return "", err + } + return "", nil + } + return C.GoString(cs), nil +} + +// SetClipboardString sets the system clipboard to the specified UTF-8 encoded +// string. +// +// This function may only be called from the main thread. +func SetClipboardString(str string) error { + cp := C.CString(str) + defer C.free(unsafe.Pointer(cp)) + C.glfwSetClipboardString(nil, cp) + return fetchErrorIgnoringPlatformError() } diff --git a/internal/cglfw/glx_context_linbsd.c b/internal/glfw/glx_context_linbsd.c similarity index 100% rename from internal/cglfw/glx_context_linbsd.c rename to internal/glfw/glx_context_linbsd.c diff --git a/internal/cglfw/glx_context_linbsd.h b/internal/glfw/glx_context_linbsd.h similarity index 100% rename from internal/cglfw/glx_context_linbsd.h rename to internal/glfw/glx_context_linbsd.h diff --git a/internal/cglfw/init_unix.c b/internal/glfw/init_unix.c similarity index 100% rename from internal/cglfw/init_unix.c rename to internal/glfw/init_unix.c diff --git a/internal/cglfw/input_unix.c b/internal/glfw/input_unix.c similarity index 100% rename from internal/cglfw/input_unix.c rename to internal/glfw/input_unix.c diff --git a/internal/cglfw/input_unix.go b/internal/glfw/input_unix.go similarity index 70% rename from internal/cglfw/input_unix.go rename to internal/glfw/input_unix.go index f9d42401d..ab34a8008 100644 --- a/internal/cglfw/input_unix.go +++ b/internal/glfw/input_unix.go @@ -4,7 +4,7 @@ //go:build darwin || freebsd || linux || netbsd || openbsd -package cglfw +package glfw // #include // #define GLFW_INCLUDE_NONE @@ -58,211 +58,6 @@ import ( "unsafe" ) -// Key corresponds to a keyboard key. -type Key int - -// These key codes are inspired by the USB HID Usage Tables v1.12 (p. 53-60), -// but re-arranged to map to 7-bit ASCII for printable keys (function keys are -// put in the 256+ range). -const ( - KeyUnknown Key = C.GLFW_KEY_UNKNOWN - KeySpace Key = C.GLFW_KEY_SPACE - KeyApostrophe Key = C.GLFW_KEY_APOSTROPHE - KeyComma Key = C.GLFW_KEY_COMMA - KeyMinus Key = C.GLFW_KEY_MINUS - KeyPeriod Key = C.GLFW_KEY_PERIOD - KeySlash Key = C.GLFW_KEY_SLASH - Key0 Key = C.GLFW_KEY_0 - Key1 Key = C.GLFW_KEY_1 - Key2 Key = C.GLFW_KEY_2 - Key3 Key = C.GLFW_KEY_3 - Key4 Key = C.GLFW_KEY_4 - Key5 Key = C.GLFW_KEY_5 - Key6 Key = C.GLFW_KEY_6 - Key7 Key = C.GLFW_KEY_7 - Key8 Key = C.GLFW_KEY_8 - Key9 Key = C.GLFW_KEY_9 - KeySemicolon Key = C.GLFW_KEY_SEMICOLON - KeyEqual Key = C.GLFW_KEY_EQUAL - KeyA Key = C.GLFW_KEY_A - KeyB Key = C.GLFW_KEY_B - KeyC Key = C.GLFW_KEY_C - KeyD Key = C.GLFW_KEY_D - KeyE Key = C.GLFW_KEY_E - KeyF Key = C.GLFW_KEY_F - KeyG Key = C.GLFW_KEY_G - KeyH Key = C.GLFW_KEY_H - KeyI Key = C.GLFW_KEY_I - KeyJ Key = C.GLFW_KEY_J - KeyK Key = C.GLFW_KEY_K - KeyL Key = C.GLFW_KEY_L - KeyM Key = C.GLFW_KEY_M - KeyN Key = C.GLFW_KEY_N - KeyO Key = C.GLFW_KEY_O - KeyP Key = C.GLFW_KEY_P - KeyQ Key = C.GLFW_KEY_Q - KeyR Key = C.GLFW_KEY_R - KeyS Key = C.GLFW_KEY_S - KeyT Key = C.GLFW_KEY_T - KeyU Key = C.GLFW_KEY_U - KeyV Key = C.GLFW_KEY_V - KeyW Key = C.GLFW_KEY_W - KeyX Key = C.GLFW_KEY_X - KeyY Key = C.GLFW_KEY_Y - KeyZ Key = C.GLFW_KEY_Z - KeyLeftBracket Key = C.GLFW_KEY_LEFT_BRACKET - KeyBackslash Key = C.GLFW_KEY_BACKSLASH - KeyRightBracket Key = C.GLFW_KEY_RIGHT_BRACKET - KeyGraveAccent Key = C.GLFW_KEY_GRAVE_ACCENT - KeyWorld1 Key = C.GLFW_KEY_WORLD_1 - KeyWorld2 Key = C.GLFW_KEY_WORLD_2 - KeyEscape Key = C.GLFW_KEY_ESCAPE - KeyEnter Key = C.GLFW_KEY_ENTER - KeyTab Key = C.GLFW_KEY_TAB - KeyBackspace Key = C.GLFW_KEY_BACKSPACE - KeyInsert Key = C.GLFW_KEY_INSERT - KeyDelete Key = C.GLFW_KEY_DELETE - KeyRight Key = C.GLFW_KEY_RIGHT - KeyLeft Key = C.GLFW_KEY_LEFT - KeyDown Key = C.GLFW_KEY_DOWN - KeyUp Key = C.GLFW_KEY_UP - KeyPageUp Key = C.GLFW_KEY_PAGE_UP - KeyPageDown Key = C.GLFW_KEY_PAGE_DOWN - KeyHome Key = C.GLFW_KEY_HOME - KeyEnd Key = C.GLFW_KEY_END - KeyCapsLock Key = C.GLFW_KEY_CAPS_LOCK - KeyScrollLock Key = C.GLFW_KEY_SCROLL_LOCK - KeyNumLock Key = C.GLFW_KEY_NUM_LOCK - KeyPrintScreen Key = C.GLFW_KEY_PRINT_SCREEN - KeyPause Key = C.GLFW_KEY_PAUSE - KeyF1 Key = C.GLFW_KEY_F1 - KeyF2 Key = C.GLFW_KEY_F2 - KeyF3 Key = C.GLFW_KEY_F3 - KeyF4 Key = C.GLFW_KEY_F4 - KeyF5 Key = C.GLFW_KEY_F5 - KeyF6 Key = C.GLFW_KEY_F6 - KeyF7 Key = C.GLFW_KEY_F7 - KeyF8 Key = C.GLFW_KEY_F8 - KeyF9 Key = C.GLFW_KEY_F9 - KeyF10 Key = C.GLFW_KEY_F10 - KeyF11 Key = C.GLFW_KEY_F11 - KeyF12 Key = C.GLFW_KEY_F12 - KeyF13 Key = C.GLFW_KEY_F13 - KeyF14 Key = C.GLFW_KEY_F14 - KeyF15 Key = C.GLFW_KEY_F15 - KeyF16 Key = C.GLFW_KEY_F16 - KeyF17 Key = C.GLFW_KEY_F17 - KeyF18 Key = C.GLFW_KEY_F18 - KeyF19 Key = C.GLFW_KEY_F19 - KeyF20 Key = C.GLFW_KEY_F20 - KeyF21 Key = C.GLFW_KEY_F21 - KeyF22 Key = C.GLFW_KEY_F22 - KeyF23 Key = C.GLFW_KEY_F23 - KeyF24 Key = C.GLFW_KEY_F24 - KeyF25 Key = C.GLFW_KEY_F25 - KeyKP0 Key = C.GLFW_KEY_KP_0 - KeyKP1 Key = C.GLFW_KEY_KP_1 - KeyKP2 Key = C.GLFW_KEY_KP_2 - KeyKP3 Key = C.GLFW_KEY_KP_3 - KeyKP4 Key = C.GLFW_KEY_KP_4 - KeyKP5 Key = C.GLFW_KEY_KP_5 - KeyKP6 Key = C.GLFW_KEY_KP_6 - KeyKP7 Key = C.GLFW_KEY_KP_7 - KeyKP8 Key = C.GLFW_KEY_KP_8 - KeyKP9 Key = C.GLFW_KEY_KP_9 - KeyKPDecimal Key = C.GLFW_KEY_KP_DECIMAL - KeyKPDivide Key = C.GLFW_KEY_KP_DIVIDE - KeyKPMultiply Key = C.GLFW_KEY_KP_MULTIPLY - KeyKPSubtract Key = C.GLFW_KEY_KP_SUBTRACT - KeyKPAdd Key = C.GLFW_KEY_KP_ADD - KeyKPEnter Key = C.GLFW_KEY_KP_ENTER - KeyKPEqual Key = C.GLFW_KEY_KP_EQUAL - KeyLeftShift Key = C.GLFW_KEY_LEFT_SHIFT - KeyLeftControl Key = C.GLFW_KEY_LEFT_CONTROL - KeyLeftAlt Key = C.GLFW_KEY_LEFT_ALT - KeyLeftSuper Key = C.GLFW_KEY_LEFT_SUPER - KeyRightShift Key = C.GLFW_KEY_RIGHT_SHIFT - KeyRightControl Key = C.GLFW_KEY_RIGHT_CONTROL - KeyRightAlt Key = C.GLFW_KEY_RIGHT_ALT - KeyRightSuper Key = C.GLFW_KEY_RIGHT_SUPER - KeyMenu Key = C.GLFW_KEY_MENU - KeyLast Key = C.GLFW_KEY_LAST -) - -// ModifierKey corresponds to a modifier key. -type ModifierKey int - -// Modifier keys. -const ( - ModShift ModifierKey = C.GLFW_MOD_SHIFT - ModControl ModifierKey = C.GLFW_MOD_CONTROL - ModAlt ModifierKey = C.GLFW_MOD_ALT - ModSuper ModifierKey = C.GLFW_MOD_SUPER - ModCapsLock ModifierKey = C.GLFW_MOD_CAPS_LOCK - ModNumLock ModifierKey = C.GLFW_MOD_NUM_LOCK -) - -// MouseButton corresponds to a mouse button. -type MouseButton int - -// Mouse buttons. -const ( - MouseButton1 MouseButton = C.GLFW_MOUSE_BUTTON_1 - MouseButton2 MouseButton = C.GLFW_MOUSE_BUTTON_2 - MouseButton3 MouseButton = C.GLFW_MOUSE_BUTTON_3 - MouseButton4 MouseButton = C.GLFW_MOUSE_BUTTON_4 - MouseButton5 MouseButton = C.GLFW_MOUSE_BUTTON_5 - MouseButton6 MouseButton = C.GLFW_MOUSE_BUTTON_6 - MouseButton7 MouseButton = C.GLFW_MOUSE_BUTTON_7 - MouseButton8 MouseButton = C.GLFW_MOUSE_BUTTON_8 - MouseButtonLast MouseButton = C.GLFW_MOUSE_BUTTON_LAST - MouseButtonLeft MouseButton = C.GLFW_MOUSE_BUTTON_LEFT - MouseButtonRight MouseButton = C.GLFW_MOUSE_BUTTON_RIGHT - MouseButtonMiddle MouseButton = C.GLFW_MOUSE_BUTTON_MIDDLE -) - -// StandardCursor corresponds to a standard cursor icon. -type StandardCursor int - -// Standard cursors -const ( - ArrowCursor StandardCursor = C.GLFW_ARROW_CURSOR - IBeamCursor StandardCursor = C.GLFW_IBEAM_CURSOR - CrosshairCursor StandardCursor = C.GLFW_CROSSHAIR_CURSOR - HandCursor StandardCursor = C.GLFW_HAND_CURSOR - HResizeCursor StandardCursor = C.GLFW_HRESIZE_CURSOR - VResizeCursor StandardCursor = C.GLFW_VRESIZE_CURSOR -) - -// Action corresponds to a key or button action. -type Action int - -// Action types. -const ( - Release Action = C.GLFW_RELEASE // The key or button was released. - Press Action = C.GLFW_PRESS // The key or button was pressed. - Repeat Action = C.GLFW_REPEAT // The key was held down until it repeated. -) - -// InputMode corresponds to an input mode. -type InputMode int - -// Input modes. -const ( - CursorMode InputMode = C.GLFW_CURSOR // See Cursor mode values - StickyKeysMode InputMode = C.GLFW_STICKY_KEYS // Value can be either 1 or 0 - StickyMouseButtonsMode InputMode = C.GLFW_STICKY_MOUSE_BUTTONS // Value can be either 1 or 0 - LockKeyMods InputMode = C.GLFW_LOCK_KEY_MODS // Value can be either 1 or 0 - RawMouseMotion InputMode = C.GLFW_RAW_MOUSE_MOTION // Value can be either 1 or 0 -) - -// Cursor mode values. -const ( - CursorNormal int = C.GLFW_CURSOR_NORMAL - CursorHidden int = C.GLFW_CURSOR_HIDDEN - CursorDisabled int = C.GLFW_CURSOR_DISABLED -) - // Cursor represents a cursor. type Cursor struct { data *C.GLFWcursor diff --git a/internal/cglfw/internal_unix.h b/internal/glfw/internal_unix.h similarity index 100% rename from internal/cglfw/internal_unix.h rename to internal/glfw/internal_unix.h diff --git a/internal/cglfw/monitor_unix.c b/internal/glfw/monitor_unix.c similarity index 100% rename from internal/cglfw/monitor_unix.c rename to internal/glfw/monitor_unix.c diff --git a/internal/cglfw/monitor_unix.go b/internal/glfw/monitor_unix.go similarity index 90% rename from internal/cglfw/monitor_unix.go rename to internal/glfw/monitor_unix.go index 7176c5908..9a2e64ef0 100644 --- a/internal/cglfw/monitor_unix.go +++ b/internal/glfw/monitor_unix.go @@ -4,7 +4,7 @@ //go:build darwin || freebsd || linux || netbsd || openbsd -package cglfw +package glfw // #define GLFW_INCLUDE_NONE // #include "glfw3_unix.h" @@ -41,10 +41,6 @@ type Monitor struct { data *C.GLFWmonitor } -// PeripheralEvent corresponds to a peripheral(Monitor or Joystick) -// configuration event. -type PeripheralEvent int - // GammaRamp describes the gamma ramp for a monitor. type GammaRamp struct { Red []uint16 // A slice of value describing the response of the red channel. @@ -52,22 +48,6 @@ type GammaRamp struct { Blue []uint16 // A slice of value describing the response of the blue channel. } -// PeripheralEvent events. -const ( - Connected PeripheralEvent = C.GLFW_CONNECTED - Disconnected PeripheralEvent = C.GLFW_DISCONNECTED -) - -// VidMode describes a single video mode. -type VidMode struct { - Width int // The width, in pixels, of the video mode. - Height int // The height, in pixels, of the video mode. - RedBits int // The bit depth of the red channel of the video mode. - GreenBits int // The bit depth of the green channel of the video mode. - BlueBits int // The bit depth of the blue channel of the video mode. - RefreshRate int // The refresh rate, in Hz, of the video mode. -} - var fMonitorHolder func(monitor *Monitor, event PeripheralEvent) //export goMonitorCB @@ -142,10 +122,10 @@ func (m *Monitor) GetWorkarea() (x, y, width, height int) { // and any UI elements. // // This function must only be called from the main thread. -func (m *Monitor) GetContentScale() (float32, float32) { +func (m *Monitor) GetContentScale() (float32, float32, error) { var x, y C.float C.glfwGetMonitorContentScale(m.data, &x, &y) - return float32(x), float32(y) + return float32(x), float32(y), nil } // SetUserPointer sets the user-defined pointer of the monitor. The current value @@ -206,7 +186,7 @@ type MonitorCallback func(monitor *Monitor, event PeripheralEvent) // disconnected from the system. // // This function must only be called from the main thread. -func SetMonitorCallback(cbfun MonitorCallback) MonitorCallback { +func SetMonitorCallback(cbfun MonitorCallback) (MonitorCallback, error) { previous := fMonitorHolder fMonitorHolder = cbfun if cbfun == nil { @@ -214,7 +194,7 @@ func SetMonitorCallback(cbfun MonitorCallback) MonitorCallback { } else { C.glfwSetMonitorCallbackCB() } - return previous + return previous, nil } // GetVideoModes returns an array of all video modes supported by the monitor. diff --git a/internal/glfw/native_darwin.go b/internal/glfw/native_darwin.go index 855c50669..9b6e47e99 100644 --- a/internal/glfw/native_darwin.go +++ b/internal/glfw/native_darwin.go @@ -1,31 +1,41 @@ -// Copyright 2018 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. +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2012 The glfw3-go Authors +// SPDX-FileCopyrightText: 2023 The Ebitengine Authors package glfw -func (w *Window) GetCocoaWindow() (uintptr, error) { - ptr, err := w.w.GetCocoaWindow() - if err != nil { - return 0, err - } - return uintptr(ptr), nil +/* +#define GLFW_EXPOSE_NATIVE_COCOA +#define GLFW_EXPOSE_NATIVE_NSGL +#include "glfw3_unix.h" +#include "glfw3native_unix.h" + +// workaround wrappers needed due to a cgo and/or LLVM bug. +// See: https://github.com/go-gl/glfw/issues/136 +static void *workaround_glfwGetCocoaWindow(GLFWwindow *w) { + return (void *)glfwGetCocoaWindow(w); +} +static void *workaround_glfwGetNSGLContext(GLFWwindow *w) { + return (void *)glfwGetNSGLContext(w); +} +*/ +import "C" +import "unsafe" + +// GetCocoaMonitor returns the CGDirectDisplayID of the monitor. +func (m *Monitor) GetCocoaMonitor() (uintptr, error) { + ret := uintptr(C.glfwGetCocoaMonitor(m.data)) + return ret, fetchErrorIgnoringPlatformError() } -func (m *Monitor) GetCocoaMonitor() (uintptr, error) { - ptr, err := m.m.GetCocoaMonitor() - if err != nil { - return 0, err - } - return uintptr(ptr), nil +// GetCocoaWindow returns the NSWindow of the window. +func (w *Window) GetCocoaWindow() (uintptr, error) { + ret := uintptr(C.workaround_glfwGetCocoaWindow(w.data)) + return ret, fetchErrorIgnoringPlatformError() +} + +// GetNSGLContext returns the NSOpenGLContext of the window. +func (w *Window) GetNSGLContext() (unsafe.Pointer, error) { + ret := C.workaround_glfwGetNSGLContext(w.data) + return ret, fetchErrorIgnoringPlatformError() } diff --git a/internal/cglfw/native_linbsd.go b/internal/glfw/native_linbsd.go similarity index 99% rename from internal/cglfw/native_linbsd.go rename to internal/glfw/native_linbsd.go index 9df757bf2..4b8144aaa 100644 --- a/internal/cglfw/native_linbsd.go +++ b/internal/glfw/native_linbsd.go @@ -4,7 +4,7 @@ //go:build freebsd || linux || netbsd || openbsd -package cglfw +package glfw //#include //#define GLFW_EXPOSE_NATIVE_X11 diff --git a/internal/cglfw/nsgl_context_darwin.h b/internal/glfw/nsgl_context_darwin.h similarity index 100% rename from internal/cglfw/nsgl_context_darwin.h rename to internal/glfw/nsgl_context_darwin.h diff --git a/internal/cglfw/nsgl_context_darwin.m b/internal/glfw/nsgl_context_darwin.m similarity index 100% rename from internal/cglfw/nsgl_context_darwin.m rename to internal/glfw/nsgl_context_darwin.m diff --git a/internal/cglfw/osmesa_context_unix.c b/internal/glfw/osmesa_context_unix.c similarity index 100% rename from internal/cglfw/osmesa_context_unix.c rename to internal/glfw/osmesa_context_unix.c diff --git a/internal/cglfw/osmesa_context_unix.h b/internal/glfw/osmesa_context_unix.h similarity index 100% rename from internal/cglfw/osmesa_context_unix.h rename to internal/glfw/osmesa_context_unix.h diff --git a/internal/cglfw/posix_thread_unix.c b/internal/glfw/posix_thread_unix.c similarity index 100% rename from internal/cglfw/posix_thread_unix.c rename to internal/glfw/posix_thread_unix.c diff --git a/internal/cglfw/posix_thread_unix.h b/internal/glfw/posix_thread_unix.h similarity index 100% rename from internal/cglfw/posix_thread_unix.h rename to internal/glfw/posix_thread_unix.h diff --git a/internal/cglfw/posix_time_linbsd.c b/internal/glfw/posix_time_linbsd.c similarity index 100% rename from internal/cglfw/posix_time_linbsd.c rename to internal/glfw/posix_time_linbsd.c diff --git a/internal/cglfw/posix_time_linbsd.h b/internal/glfw/posix_time_linbsd.h similarity index 100% rename from internal/cglfw/posix_time_linbsd.h rename to internal/glfw/posix_time_linbsd.h diff --git a/internal/glfw/type_unix.go b/internal/glfw/type_unix.go deleted file mode 100644 index 148dc4980..000000000 --- a/internal/glfw/type_unix.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2018 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. - -//go:build darwin || freebsd || linux || netbsd || openbsd - -package glfw - -import ( - "github.com/hajimehoshi/ebiten/v2/internal/cglfw" -) - -type ( - CharModsCallback = cglfw.CharModsCallback - CloseCallback = cglfw.CloseCallback - DropCallback = cglfw.DropCallback - FramebufferSizeCallback = cglfw.FramebufferSizeCallback - MonitorCallback = cglfw.MonitorCallback - ScrollCallback = cglfw.ScrollCallback - SizeCallback = cglfw.SizeCallback -) - -type VidMode struct { - Width int - Height int - RedBits int - GreenBits int - BlueBits int - RefreshRate int -} diff --git a/internal/cglfw/util_unix.go b/internal/glfw/util_unix.go similarity index 96% rename from internal/cglfw/util_unix.go rename to internal/glfw/util_unix.go index 266c282cb..b1225ef07 100644 --- a/internal/cglfw/util_unix.go +++ b/internal/glfw/util_unix.go @@ -4,7 +4,7 @@ //go:build darwin || freebsd || linux || netbsd || openbsd -package cglfw +package glfw // #include import "C" diff --git a/internal/cglfw/window_unix.c b/internal/glfw/window_unix.c similarity index 100% rename from internal/cglfw/window_unix.c rename to internal/glfw/window_unix.c diff --git a/internal/cglfw/window_unix.go b/internal/glfw/window_unix.go similarity index 80% rename from internal/cglfw/window_unix.go rename to internal/glfw/window_unix.go index 2347fde64..fd4cf15ac 100644 --- a/internal/cglfw/window_unix.go +++ b/internal/glfw/window_unix.go @@ -4,7 +4,7 @@ //go:build darwin || freebsd || linux || netbsd || openbsd -package cglfw +package glfw // #include // #define GLFW_INCLUDE_NONE @@ -91,120 +91,6 @@ func (w *windowList) get(wnd *C.GLFWwindow) *Window { return w.m[wnd] } -// Hint corresponds to hints that can be set before creating a window. -// -// Hint also corresponds to the attributes of the window that can be get after -// its creation. -type Hint int - -// Init related hints. (Use with glfw.InitHint) -const ( - CocoaChdirResources Hint = C.GLFW_COCOA_CHDIR_RESOURCES // Specifies whether to set the current directory to the application to the Contents/Resources subdirectory of the application's bundle, if present. - CocoaMenubar Hint = C.GLFW_COCOA_MENUBAR // Specifies whether to create a basic menu bar, either from a nib or manually, when the first window is created, which is when AppKit is initialized. -) - -// Window related hints/attributes. -const ( - Focused Hint = C.GLFW_FOCUSED // Specifies whether the window will be given input focus when created. This hint is ignored for full screen and initially hidden windows. - Iconified Hint = C.GLFW_ICONIFIED // Specifies whether the window will be minimized. - Maximized Hint = C.GLFW_MAXIMIZED // Specifies whether the window is maximized. - Visible Hint = C.GLFW_VISIBLE // Specifies whether the window will be initially visible. - Hovered Hint = C.GLFW_HOVERED // Specifies whether the cursor is currently directly over the content area of the window, with no other windows between. See Cursor enter/leave events for details. - Resizable Hint = C.GLFW_RESIZABLE // Specifies whether the window will be resizable by the user. - Decorated Hint = C.GLFW_DECORATED // Specifies whether the window will have window decorations such as a border, a close widget, etc. - Floating Hint = C.GLFW_FLOATING // Specifies whether the window will be always-on-top. - AutoIconify Hint = C.GLFW_AUTO_ICONIFY // Specifies whether fullscreen windows automatically iconify (and restore the previous video mode) on focus loss. - CenterCursor Hint = C.GLFW_CENTER_CURSOR // Specifies whether the cursor should be centered over newly created full screen windows. This hint is ignored for windowed mode windows. - TransparentFramebuffer Hint = C.GLFW_TRANSPARENT_FRAMEBUFFER // Specifies whether the framebuffer should be transparent. - FocusOnShow Hint = C.GLFW_FOCUS_ON_SHOW // Specifies whether the window will be given input focus when glfwShowWindow is called. - ScaleToMonitor Hint = C.GLFW_SCALE_TO_MONITOR // Specified whether the window content area should be resized based on the monitor content scale of any monitor it is placed on. This includes the initial placement when the window is created. -) - -// Context related hints. -const ( - ClientAPI Hint = C.GLFW_CLIENT_API // Specifies which client API to create the context for. Hard constraint. - ContextVersionMajor Hint = C.GLFW_CONTEXT_VERSION_MAJOR // Specifies the client API version that the created context must be compatible with. - ContextVersionMinor Hint = C.GLFW_CONTEXT_VERSION_MINOR // Specifies the client API version that the created context must be compatible with. - ContextRobustness Hint = C.GLFW_CONTEXT_ROBUSTNESS // Specifies the robustness strategy to be used by the context. - ContextReleaseBehavior Hint = C.GLFW_CONTEXT_RELEASE_BEHAVIOR // Specifies the release behavior to be used by the context. - OpenGLForwardCompatible Hint = C.GLFW_OPENGL_FORWARD_COMPAT // Specifies whether the OpenGL context should be forward-compatible. Hard constraint. - OpenGLDebugContext Hint = C.GLFW_OPENGL_DEBUG_CONTEXT // Specifies whether to create a debug OpenGL context, which may have additional error and performance issue reporting functionality. If OpenGL ES is requested, this hint is ignored. - OpenGLProfile Hint = C.GLFW_OPENGL_PROFILE // Specifies which OpenGL profile to create the context for. Hard constraint. - ContextCreationAPI Hint = C.GLFW_CONTEXT_CREATION_API // Specifies which context creation API to use to create the context. -) - -// Framebuffer related hints. -const ( - ContextRevision Hint = C.GLFW_CONTEXT_REVISION - RedBits Hint = C.GLFW_RED_BITS // Specifies the desired bit depth of the default framebuffer. - GreenBits Hint = C.GLFW_GREEN_BITS // Specifies the desired bit depth of the default framebuffer. - BlueBits Hint = C.GLFW_BLUE_BITS // Specifies the desired bit depth of the default framebuffer. - AlphaBits Hint = C.GLFW_ALPHA_BITS // Specifies the desired bit depth of the default framebuffer. - DepthBits Hint = C.GLFW_DEPTH_BITS // Specifies the desired bit depth of the default framebuffer. - StencilBits Hint = C.GLFW_STENCIL_BITS // Specifies the desired bit depth of the default framebuffer. - AccumRedBits Hint = C.GLFW_ACCUM_RED_BITS // Specifies the desired bit depth of the accumulation buffer. - AccumGreenBits Hint = C.GLFW_ACCUM_GREEN_BITS // Specifies the desired bit depth of the accumulation buffer. - AccumBlueBits Hint = C.GLFW_ACCUM_BLUE_BITS // Specifies the desired bit depth of the accumulation buffer. - AccumAlphaBits Hint = C.GLFW_ACCUM_ALPHA_BITS // Specifies the desired bit depth of the accumulation buffer. - AuxBuffers Hint = C.GLFW_AUX_BUFFERS // Specifies the desired number of auxiliary buffers. - Stereo Hint = C.GLFW_STEREO // Specifies whether to use stereoscopic rendering. Hard constraint. - Samples Hint = C.GLFW_SAMPLES // Specifies the desired number of samples to use for multisampling. Zero disables multisampling. - SRGBCapable Hint = C.GLFW_SRGB_CAPABLE // Specifies whether the framebuffer should be sRGB capable. - RefreshRate Hint = C.GLFW_REFRESH_RATE // Specifies the desired refresh rate for full screen windows. If set to zero, the highest available refresh rate will be used. This hint is ignored for windowed mode windows. - DoubleBuffer Hint = C.GLFW_DOUBLEBUFFER // Specifies whether the framebuffer should be double buffered. You nearly always want to use double buffering. This is a hard constraint. - CocoaGraphicsSwitching Hint = C.GLFW_COCOA_GRAPHICS_SWITCHING // Specifies whether to in Automatic Graphics Switching, i.e. to allow the system to choose the integrated GPU for the OpenGL context and move it between GPUs if necessary or whether to force it to always run on the discrete GPU. - CocoaRetinaFramebuffer Hint = C.GLFW_COCOA_RETINA_FRAMEBUFFER // Specifies whether to use full resolution framebuffers on Retina displays. -) - -// Naming related hints. (Use with glfw.WindowHintString) -const ( - CocoaFrameNAME Hint = C.GLFW_COCOA_FRAME_NAME // Specifies the UTF-8 encoded name to use for autosaving the window frame, or if empty disables frame autosaving for the window. - X11ClassName Hint = C.GLFW_X11_CLASS_NAME // Specifies the desired ASCII encoded class parts of the ICCCM WM_CLASS window property.nd instance parts of the ICCCM WM_CLASS window property. - X11InstanceName Hint = C.GLFW_X11_INSTANCE_NAME // Specifies the desired ASCII encoded instance parts of the ICCCM WM_CLASS window property.nd instance parts of the ICCCM WM_CLASS window property. -) - -// Values for the ClientAPI hint. -const ( - OpenGLAPI int = C.GLFW_OPENGL_API - OpenGLESAPI int = C.GLFW_OPENGL_ES_API - NoAPI int = C.GLFW_NO_API -) - -// Values for ContextCreationAPI hint. -const ( - NativeContextAPI int = C.GLFW_NATIVE_CONTEXT_API - EGLContextAPI int = C.GLFW_EGL_CONTEXT_API - OSMesaContextAPI int = C.GLFW_OSMESA_CONTEXT_API -) - -// Values for the ContextRobustness hint. -const ( - NoRobustness int = C.GLFW_NO_ROBUSTNESS - NoResetNotification int = C.GLFW_NO_RESET_NOTIFICATION - LoseContextOnReset int = C.GLFW_LOSE_CONTEXT_ON_RESET -) - -// Values for ContextReleaseBehavior hint. -const ( - AnyReleaseBehavior int = C.GLFW_ANY_RELEASE_BEHAVIOR - ReleaseBehaviorFlush int = C.GLFW_RELEASE_BEHAVIOR_FLUSH - ReleaseBehaviorNone int = C.GLFW_RELEASE_BEHAVIOR_NONE -) - -// Values for the OpenGLProfile hint. -const ( - OpenGLAnyProfile int = C.GLFW_OPENGL_ANY_PROFILE - OpenGLCoreProfile int = C.GLFW_OPENGL_CORE_PROFILE - OpenGLCompatProfile int = C.GLFW_OPENGL_COMPAT_PROFILE -) - -// Other values. -const ( - True int = 1 // GL_TRUE - False int = 0 // GL_FALSE - DontCare int = C.GLFW_DONT_CARE -) - // Window represents a window. type Window struct { data *C.GLFWwindow @@ -689,8 +575,9 @@ func (w *Window) RequestAttention() { // // Do not use this function to steal focus from other applications unless you are certain that // is what the user wants. Focus stealing can be extremely disruptive. -func (w *Window) Focus() { +func (w *Window) Focus() error { C.glfwFocusWindow(w.data) + return nil } // Iconify iconifies/minimizes the window, if it was previously restored. If it @@ -699,16 +586,18 @@ func (w *Window) Focus() { // nothing. // // This function may only be called from the main thread. -func (w *Window) Iconify() { +func (w *Window) Iconify() error { C.glfwIconifyWindow(w.data) + return nil } // Maximize maximizes the specified window if it was previously not maximized. // If the window is already maximized, this function does nothing. // // If the specified window is a full screen window, this function does nothing. -func (w *Window) Maximize() { +func (w *Window) Maximize() error { C.glfwMaximizeWindow(w.data) + return nil } // Restore restores the window, if it was previously iconified/minimized. If it @@ -717,8 +606,9 @@ func (w *Window) Maximize() { // nothing. // // This function may only be called from the main thread. -func (w *Window) Restore() { +func (w *Window) Restore() error { C.glfwRestoreWindow(w.data) + return nil } // Show makes the window visible, if it was previously hidden. If the window is @@ -810,8 +700,9 @@ func (w *Window) GetAttrib(attrib Hint) (int, error) { // will take effect if the window is later made full screen. // // This function may only be called from the main thread. -func (w *Window) SetAttrib(attrib Hint, value int) { +func (w *Window) SetAttrib(attrib Hint, value int) error { C.glfwSetWindowAttrib(w.data, C.int(attrib), C.int(value)) + return nil } // SetUserPointer sets the user-defined pointer of the window. The current value diff --git a/internal/cglfw/x11_init_linbsd.c b/internal/glfw/x11_init_linbsd.c similarity index 100% rename from internal/cglfw/x11_init_linbsd.c rename to internal/glfw/x11_init_linbsd.c diff --git a/internal/cglfw/x11_monitor_linbsd.c b/internal/glfw/x11_monitor_linbsd.c similarity index 100% rename from internal/cglfw/x11_monitor_linbsd.c rename to internal/glfw/x11_monitor_linbsd.c diff --git a/internal/cglfw/x11_platform_linbsd.h b/internal/glfw/x11_platform_linbsd.h similarity index 100% rename from internal/cglfw/x11_platform_linbsd.h rename to internal/glfw/x11_platform_linbsd.h diff --git a/internal/cglfw/x11_window_linbsd.c b/internal/glfw/x11_window_linbsd.c similarity index 100% rename from internal/cglfw/x11_window_linbsd.c rename to internal/glfw/x11_window_linbsd.c diff --git a/internal/cglfw/xkb_unicode_linbsd.c b/internal/glfw/xkb_unicode_linbsd.c similarity index 100% rename from internal/cglfw/xkb_unicode_linbsd.c rename to internal/glfw/xkb_unicode_linbsd.c diff --git a/internal/cglfw/xkb_unicode_linbsd.h b/internal/glfw/xkb_unicode_linbsd.h similarity index 100% rename from internal/cglfw/xkb_unicode_linbsd.h rename to internal/glfw/xkb_unicode_linbsd.h diff --git a/internal/ui/input_glfw.go b/internal/ui/input_glfw.go index dd60d7720..551b7d979 100644 --- a/internal/ui/input_glfw.go +++ b/internal/ui/input_glfw.go @@ -32,22 +32,22 @@ var glfwMouseButtonToMouseButton = map[glfw.MouseButton]MouseButton{ } func (u *userInterfaceImpl) registerInputCallbacks() error { - if _, err := u.window.SetCharModsCallback(glfw.ToCharModsCallback(func(w *glfw.Window, char rune, mods glfw.ModifierKey) { + if _, err := u.window.SetCharModsCallback(func(w *glfw.Window, char rune, mods glfw.ModifierKey) { // As this function is called from GLFW callbacks, the current thread is main. u.m.Lock() defer u.m.Unlock() u.inputState.appendRune(char) - })); err != nil { + }); err != nil { return err } - if _, err := u.window.SetScrollCallback(glfw.ToScrollCallback(func(w *glfw.Window, xoff float64, yoff float64) { + if _, err := u.window.SetScrollCallback(func(w *glfw.Window, xoff float64, yoff float64) { // As this function is called from GLFW callbacks, the current thread is main. u.m.Lock() defer u.m.Unlock() u.inputState.WheelX += xoff u.inputState.WheelY += yoff - })); err != nil { + }); err != nil { return err } diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index d8d324b91..c0370cc57 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -148,11 +148,11 @@ func init() { if err := initialize(); err != nil { panic(err) } - if _, err := glfw.SetMonitorCallback(glfw.ToMonitorCallback(func(monitor *glfw.Monitor, event glfw.PeripheralEvent) { + if _, err := glfw.SetMonitorCallback(func(monitor *glfw.Monitor, event glfw.PeripheralEvent) { if err := theMonitors.update(); err != nil { theGlobalState.setError(err) } - })); err != nil { + }); err != nil { panic(err) } } @@ -954,7 +954,7 @@ func (u *userInterfaceImpl) endFrame() { // registerWindowCloseCallback must be called from the main thread. func (u *userInterfaceImpl) registerWindowCloseCallback() error { if u.closeCallback == nil { - u.closeCallback = glfw.ToCloseCallback(func(_ *glfw.Window) { + u.closeCallback = func(_ *glfw.Window) { u.m.Lock() u.inputState.WindowBeingClosed = true u.m.Unlock() @@ -970,7 +970,7 @@ func (u *userInterfaceImpl) registerWindowCloseCallback() error { theGlobalState.setError(err) return } - }) + } } if _, err := u.window.SetCloseCallback(u.closeCallback); err != nil { return err @@ -987,7 +987,7 @@ func (u *userInterfaceImpl) registerWindowFramebufferSizeCallback() error { // // When a decorating state changes, the callback of arguments might be an unexpected value on macOS (#2257) // Then, do not register this callback on macOS. - u.defaultFramebufferSizeCallback = glfw.ToFramebufferSizeCallback(func(_ *glfw.Window, w, h int) { + u.defaultFramebufferSizeCallback = func(_ *glfw.Window, w, h int) { f, err := u.isFullscreen() if err != nil { theGlobalState.setError(err) @@ -1019,7 +1019,7 @@ func (u *userInterfaceImpl) registerWindowFramebufferSizeCallback() error { theGlobalState.setError(err) return } - }) + } } if _, err := u.window.SetFramebufferSizeCallback(u.defaultFramebufferSizeCallback); err != nil { return err @@ -1029,11 +1029,11 @@ func (u *userInterfaceImpl) registerWindowFramebufferSizeCallback() error { func (u *userInterfaceImpl) registerDropCallback() error { if u.dropCallback == nil { - u.dropCallback = glfw.ToDropCallback(func(_ *glfw.Window, names []string) { + u.dropCallback = func(_ *glfw.Window, names []string) { u.m.Lock() defer u.m.Unlock() u.inputState.DroppedFiles = file.NewVirtualFS(names) - }) + } } if _, err := u.window.SetDropCallback(u.dropCallback); err != nil { return err @@ -1050,14 +1050,14 @@ func (u *userInterfaceImpl) waitForFramebufferSizeCallback(window *glfw.Window, u.framebufferSizeCallbackCh = make(chan struct{}, 1) if u.framebufferSizeCallback == nil { - u.framebufferSizeCallback = glfw.ToFramebufferSizeCallback(func(_ *glfw.Window, _, _ int) { + u.framebufferSizeCallback = func(_ *glfw.Window, _, _ int) { // This callback can be invoked multiple times by one PollEvents in theory (#1618). // Allow the case when the channel is full. select { case u.framebufferSizeCallbackCh <- struct{}{}: default: } - }) + } } if _, err := window.SetFramebufferSizeCallback(u.framebufferSizeCallback); err != nil { return err