internal/glfw: merge internal/cglfw into internal/glfw

Closes #2703
This commit is contained in:
Hajime Hoshi 2023-10-07 23:21:29 +09:00
parent ad0b61c022
commit cede5027d3
51 changed files with 193 additions and 1141 deletions

View File

@ -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 <stdlib.h>
//#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()
}

View File

@ -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()
}

View File

@ -4,7 +4,7 @@
//go:build freebsd || netbsd || openbsd //go:build freebsd || netbsd || openbsd
package cglfw package glfw
import "C" import "C"

View File

@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: 2012 The glfw3-go Authors // SPDX-FileCopyrightText: 2012 The glfw3-go Authors
// SPDX-FileCopyrightText: 2023 The Ebitengine Authors // SPDX-FileCopyrightText: 2023 The Ebitengine Authors
package cglfw package glfw
// #cgo CFLAGS: -D_GLFW_COCOA -Wno-deprecated-declarations // #cgo CFLAGS: -D_GLFW_COCOA -Wno-deprecated-declarations
// #cgo LDFLAGS: -framework Cocoa -framework IOKit -framework CoreVideo // #cgo LDFLAGS: -framework Cocoa -framework IOKit -framework CoreVideo

View File

@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: 2012 The glfw3-go Authors // SPDX-FileCopyrightText: 2012 The glfw3-go Authors
// SPDX-FileCopyrightText: 2023 The Ebitengine Authors // SPDX-FileCopyrightText: 2023 The Ebitengine Authors
package cglfw package glfw
// #cgo CFLAGS: -D_GLFW_X11 // #cgo CFLAGS: -D_GLFW_X11
// #cgo LDFLAGS: -lX11 -lXrandr -lXxf86vm -lXi -lXcursor -lm -lXinerama -ldl -lrt // #cgo LDFLAGS: -lX11 -lXrandr -lXxf86vm -lXi -lXcursor -lm -lXinerama -ldl -lrt

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -4,7 +4,7 @@
//go:build darwin || freebsd || linux || netbsd || openbsd //go:build darwin || freebsd || linux || netbsd || openbsd
package cglfw package glfw
//#include <stdlib.h> //#include <stdlib.h>
//#define GLFW_INCLUDE_NONE //#define GLFW_INCLUDE_NONE

View File

@ -4,7 +4,7 @@
//go:build darwin || freebsd || linux || netbsd || openbsd //go:build darwin || freebsd || linux || netbsd || openbsd
package cglfw package glfw
// #define GLFW_INCLUDE_NONE // #define GLFW_INCLUDE_NONE
// #include "glfw3_unix.h" // #include "glfw3_unix.h"
@ -22,49 +22,6 @@ import (
"os" "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. // Note: There are many cryptic caveats to proper error handling here.
// See: https://github.com/go-gl/glfw3/pull/86 // See: https://github.com/go-gl/glfw3/pull/86

View File

@ -3,6 +3,8 @@
// SPDX-FileCopyrightText: 2006-2019 Camilla Löwy <elmindreda@glfw.org> // SPDX-FileCopyrightText: 2006-2019 Camilla Löwy <elmindreda@glfw.org>
// SPDX-FileCopyrightText: 2022 The Ebitengine Authors // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
//go:build darwin || freebsd || linux || netbsd || openbsd || windows
package glfw package glfw
type VidMode struct { type VidMode struct {

View File

@ -1,369 +1,139 @@
// Copyright 2018 The Ebiten Authors // SPDX-License-Identifier: Apache-2.0
// // SPDX-FileCopyrightText: 2012 The glfw3-go Authors
// Licensed under the Apache License, Version 2.0 (the "License"); // SPDX-FileCopyrightText: 2023 The Ebitengine Authors
// 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 //go:build darwin || freebsd || linux || netbsd || openbsd
package glfw package glfw
//#include <stdlib.h>
//#define GLFW_INCLUDE_NONE
//#include "glfw3_unix.h"
import "C"
import ( import (
"image" "errors"
"sync" "unsafe"
"github.com/hajimehoshi/ebiten/v2/internal/cglfw"
) )
type windows map[*cglfw.Window]*Window // Version constants.
const (
var ( VersionMajor = C.GLFW_VERSION_MAJOR // This is incremented when the API is changed in non-compatible ways.
theWindows = windows{} VersionMinor = C.GLFW_VERSION_MINOR // This is incremented when features are added to the API but it remains backward-compatible.
windowsM sync.Mutex 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 { // Init initializes the GLFW library. Before most GLFW functions can be used,
if win == nil { // GLFW must be initialized, and before a program terminates GLFW should be
return nil // terminated in order to free any resources allocated during or after
} // initialization.
ww := &Window{w: win} //
windowsM.Lock() // If this function fails, it calls Terminate before returning. If it succeeds,
w[win] = ww // you should call Terminate before the program exits.
windowsM.Unlock() //
return ww // Additional calls to this function after successful initialization but before
} // termination will succeed but will do nothing.
//
func (w windows) remove(win *cglfw.Window) { // This function may take several seconds to complete on some systems, while on
windowsM.Lock() // other systems it may take only a fraction of a second to complete.
delete(w, win) //
windowsM.Unlock() // 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.
func (w windows) get(win *cglfw.Window) *Window { //
if win == nil { // This function may only be called from the main thread.
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
}
func Init() error { func Init() error {
return cglfw.Init() C.glfwInit()
} if err := fetchErrorIgnoringPlatformError(); err != nil {
return err
func PollEvents() error { }
return cglfw.PollEvents() return nil
}
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)
} }
// 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 { func Terminate() error {
return cglfw.Terminate() C.glfwTerminate()
if err := fetchErrorIgnoringPlatformError(); err != nil {
return err
}
return nil
} }
func WaitEvents() error { // InitHint function sets hints for the next initialization of GLFW.
return cglfw.WaitEvents() //
// 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 { // GetVersion retrieves the major, minor and revision numbers of the GLFW
return cglfw.WaitEventsTimeout(timeout) // 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 { // GetVersionString returns a static string generated at compile-time according
return cglfw.WindowHint(cglfw.Hint(target), hint) // 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()
} }

View File

@ -4,7 +4,7 @@
//go:build darwin || freebsd || linux || netbsd || openbsd //go:build darwin || freebsd || linux || netbsd || openbsd
package cglfw package glfw
// #include <stdlib.h> // #include <stdlib.h>
// #define GLFW_INCLUDE_NONE // #define GLFW_INCLUDE_NONE
@ -58,211 +58,6 @@ import (
"unsafe" "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. // Cursor represents a cursor.
type Cursor struct { type Cursor struct {
data *C.GLFWcursor data *C.GLFWcursor

View File

@ -4,7 +4,7 @@
//go:build darwin || freebsd || linux || netbsd || openbsd //go:build darwin || freebsd || linux || netbsd || openbsd
package cglfw package glfw
// #define GLFW_INCLUDE_NONE // #define GLFW_INCLUDE_NONE
// #include "glfw3_unix.h" // #include "glfw3_unix.h"
@ -41,10 +41,6 @@ type Monitor struct {
data *C.GLFWmonitor data *C.GLFWmonitor
} }
// PeripheralEvent corresponds to a peripheral(Monitor or Joystick)
// configuration event.
type PeripheralEvent int
// GammaRamp describes the gamma ramp for a monitor. // GammaRamp describes the gamma ramp for a monitor.
type GammaRamp struct { type GammaRamp struct {
Red []uint16 // A slice of value describing the response of the red channel. 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. 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) var fMonitorHolder func(monitor *Monitor, event PeripheralEvent)
//export goMonitorCB //export goMonitorCB
@ -142,10 +122,10 @@ func (m *Monitor) GetWorkarea() (x, y, width, height int) {
// and any UI elements. // and any UI elements.
// //
// This function must only be called from the main thread. // 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 var x, y C.float
C.glfwGetMonitorContentScale(m.data, &x, &y) 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 // 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. // disconnected from the system.
// //
// This function must only be called from the main thread. // This function must only be called from the main thread.
func SetMonitorCallback(cbfun MonitorCallback) MonitorCallback { func SetMonitorCallback(cbfun MonitorCallback) (MonitorCallback, error) {
previous := fMonitorHolder previous := fMonitorHolder
fMonitorHolder = cbfun fMonitorHolder = cbfun
if cbfun == nil { if cbfun == nil {
@ -214,7 +194,7 @@ func SetMonitorCallback(cbfun MonitorCallback) MonitorCallback {
} else { } else {
C.glfwSetMonitorCallbackCB() C.glfwSetMonitorCallbackCB()
} }
return previous return previous, nil
} }
// GetVideoModes returns an array of all video modes supported by the monitor. // GetVideoModes returns an array of all video modes supported by the monitor.

View File

@ -1,31 +1,41 @@
// Copyright 2018 The Ebiten Authors // SPDX-License-Identifier: Apache-2.0
// // SPDX-FileCopyrightText: 2012 The glfw3-go Authors
// Licensed under the Apache License, Version 2.0 (the "License"); // SPDX-FileCopyrightText: 2023 The Ebitengine Authors
// 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.
package glfw package glfw
func (w *Window) GetCocoaWindow() (uintptr, error) { /*
ptr, err := w.w.GetCocoaWindow() #define GLFW_EXPOSE_NATIVE_COCOA
if err != nil { #define GLFW_EXPOSE_NATIVE_NSGL
return 0, err #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);
} }
return uintptr(ptr), nil 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) { // GetCocoaWindow returns the NSWindow of the window.
ptr, err := m.m.GetCocoaMonitor() func (w *Window) GetCocoaWindow() (uintptr, error) {
if err != nil { ret := uintptr(C.workaround_glfwGetCocoaWindow(w.data))
return 0, err return ret, fetchErrorIgnoringPlatformError()
} }
return uintptr(ptr), nil
// GetNSGLContext returns the NSOpenGLContext of the window.
func (w *Window) GetNSGLContext() (unsafe.Pointer, error) {
ret := C.workaround_glfwGetNSGLContext(w.data)
return ret, fetchErrorIgnoringPlatformError()
} }

View File

@ -4,7 +4,7 @@
//go:build freebsd || linux || netbsd || openbsd //go:build freebsd || linux || netbsd || openbsd
package cglfw package glfw
//#include <stdlib.h> //#include <stdlib.h>
//#define GLFW_EXPOSE_NATIVE_X11 //#define GLFW_EXPOSE_NATIVE_X11

View File

@ -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
}

View File

@ -4,7 +4,7 @@
//go:build darwin || freebsd || linux || netbsd || openbsd //go:build darwin || freebsd || linux || netbsd || openbsd
package cglfw package glfw
// #include <stdlib.h> // #include <stdlib.h>
import "C" import "C"

View File

@ -4,7 +4,7 @@
//go:build darwin || freebsd || linux || netbsd || openbsd //go:build darwin || freebsd || linux || netbsd || openbsd
package cglfw package glfw
// #include <stdlib.h> // #include <stdlib.h>
// #define GLFW_INCLUDE_NONE // #define GLFW_INCLUDE_NONE
@ -91,120 +91,6 @@ func (w *windowList) get(wnd *C.GLFWwindow) *Window {
return w.m[wnd] 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. // Window represents a window.
type Window struct { type Window struct {
data *C.GLFWwindow 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 // 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. // is what the user wants. Focus stealing can be extremely disruptive.
func (w *Window) Focus() { func (w *Window) Focus() error {
C.glfwFocusWindow(w.data) C.glfwFocusWindow(w.data)
return nil
} }
// Iconify iconifies/minimizes the window, if it was previously restored. If it // Iconify iconifies/minimizes the window, if it was previously restored. If it
@ -699,16 +586,18 @@ func (w *Window) Focus() {
// nothing. // nothing.
// //
// This function may only be called from the main thread. // This function may only be called from the main thread.
func (w *Window) Iconify() { func (w *Window) Iconify() error {
C.glfwIconifyWindow(w.data) C.glfwIconifyWindow(w.data)
return nil
} }
// Maximize maximizes the specified window if it was previously not maximized. // Maximize maximizes the specified window if it was previously not maximized.
// If the window is already maximized, this function does nothing. // If the window is already maximized, this function does nothing.
// //
// If the specified window is a full screen window, 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) C.glfwMaximizeWindow(w.data)
return nil
} }
// Restore restores the window, if it was previously iconified/minimized. If it // Restore restores the window, if it was previously iconified/minimized. If it
@ -717,8 +606,9 @@ func (w *Window) Maximize() {
// nothing. // nothing.
// //
// This function may only be called from the main thread. // This function may only be called from the main thread.
func (w *Window) Restore() { func (w *Window) Restore() error {
C.glfwRestoreWindow(w.data) C.glfwRestoreWindow(w.data)
return nil
} }
// Show makes the window visible, if it was previously hidden. If the window is // 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. // will take effect if the window is later made full screen.
// //
// This function may only be called from the main thread. // 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)) C.glfwSetWindowAttrib(w.data, C.int(attrib), C.int(value))
return nil
} }
// SetUserPointer sets the user-defined pointer of the window. The current value // SetUserPointer sets the user-defined pointer of the window. The current value

View File

@ -32,22 +32,22 @@ var glfwMouseButtonToMouseButton = map[glfw.MouseButton]MouseButton{
} }
func (u *userInterfaceImpl) registerInputCallbacks() error { 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. // As this function is called from GLFW callbacks, the current thread is main.
u.m.Lock() u.m.Lock()
defer u.m.Unlock() defer u.m.Unlock()
u.inputState.appendRune(char) u.inputState.appendRune(char)
})); err != nil { }); err != nil {
return err 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. // As this function is called from GLFW callbacks, the current thread is main.
u.m.Lock() u.m.Lock()
defer u.m.Unlock() defer u.m.Unlock()
u.inputState.WheelX += xoff u.inputState.WheelX += xoff
u.inputState.WheelY += yoff u.inputState.WheelY += yoff
})); err != nil { }); err != nil {
return err return err
} }

View File

@ -148,11 +148,11 @@ func init() {
if err := initialize(); err != nil { if err := initialize(); err != nil {
panic(err) 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 { if err := theMonitors.update(); err != nil {
theGlobalState.setError(err) theGlobalState.setError(err)
} }
})); err != nil { }); err != nil {
panic(err) panic(err)
} }
} }
@ -954,7 +954,7 @@ func (u *userInterfaceImpl) endFrame() {
// registerWindowCloseCallback must be called from the main thread. // registerWindowCloseCallback must be called from the main thread.
func (u *userInterfaceImpl) registerWindowCloseCallback() error { func (u *userInterfaceImpl) registerWindowCloseCallback() error {
if u.closeCallback == nil { if u.closeCallback == nil {
u.closeCallback = glfw.ToCloseCallback(func(_ *glfw.Window) { u.closeCallback = func(_ *glfw.Window) {
u.m.Lock() u.m.Lock()
u.inputState.WindowBeingClosed = true u.inputState.WindowBeingClosed = true
u.m.Unlock() u.m.Unlock()
@ -970,7 +970,7 @@ func (u *userInterfaceImpl) registerWindowCloseCallback() error {
theGlobalState.setError(err) theGlobalState.setError(err)
return return
} }
}) }
} }
if _, err := u.window.SetCloseCallback(u.closeCallback); err != nil { if _, err := u.window.SetCloseCallback(u.closeCallback); err != nil {
return err 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) // 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. // 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() f, err := u.isFullscreen()
if err != nil { if err != nil {
theGlobalState.setError(err) theGlobalState.setError(err)
@ -1019,7 +1019,7 @@ func (u *userInterfaceImpl) registerWindowFramebufferSizeCallback() error {
theGlobalState.setError(err) theGlobalState.setError(err)
return return
} }
}) }
} }
if _, err := u.window.SetFramebufferSizeCallback(u.defaultFramebufferSizeCallback); err != nil { if _, err := u.window.SetFramebufferSizeCallback(u.defaultFramebufferSizeCallback); err != nil {
return err return err
@ -1029,11 +1029,11 @@ func (u *userInterfaceImpl) registerWindowFramebufferSizeCallback() error {
func (u *userInterfaceImpl) registerDropCallback() error { func (u *userInterfaceImpl) registerDropCallback() error {
if u.dropCallback == nil { if u.dropCallback == nil {
u.dropCallback = glfw.ToDropCallback(func(_ *glfw.Window, names []string) { u.dropCallback = func(_ *glfw.Window, names []string) {
u.m.Lock() u.m.Lock()
defer u.m.Unlock() defer u.m.Unlock()
u.inputState.DroppedFiles = file.NewVirtualFS(names) u.inputState.DroppedFiles = file.NewVirtualFS(names)
}) }
} }
if _, err := u.window.SetDropCallback(u.dropCallback); err != nil { if _, err := u.window.SetDropCallback(u.dropCallback); err != nil {
return err return err
@ -1050,14 +1050,14 @@ func (u *userInterfaceImpl) waitForFramebufferSizeCallback(window *glfw.Window,
u.framebufferSizeCallbackCh = make(chan struct{}, 1) u.framebufferSizeCallbackCh = make(chan struct{}, 1)
if u.framebufferSizeCallback == nil { 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). // This callback can be invoked multiple times by one PollEvents in theory (#1618).
// Allow the case when the channel is full. // Allow the case when the channel is full.
select { select {
case u.framebufferSizeCallbackCh <- struct{}{}: case u.framebufferSizeCallbackCh <- struct{}{}:
default: default:
} }
}) }
} }
if _, err := window.SetFramebufferSizeCallback(u.framebufferSizeCallback); err != nil { if _, err := window.SetFramebufferSizeCallback(u.framebufferSizeCallback); err != nil {
return err return err