internal/cglfw: remove acceptError

This commit is contained in:
Hajime Hoshi 2023-10-07 22:11:51 +09:00
parent 98dc59c89f
commit 7bcefa1035
3 changed files with 25 additions and 55 deletions

View File

@ -150,45 +150,6 @@ func flushErrors() {
}
}
// acceptError fetches the next error from the error channel, it accepts only
// errors with one of the given error codes. If any other error is encountered,
// a panic will occur.
//
// Platform errors are always printed, for information why please see:
//
// https://github.com/go-gl/glfw/issues/127
func acceptError(codes ...ErrorCode) error {
// Grab the next error, if there is one.
err := fetchError()
if err == nil {
return nil
}
// Only if the error has the specific error code accepted by the caller, do
// we return the error.
for _, code := range codes {
if err.Code == code {
return err
}
}
// The error isn't accepted by the caller. If the error code is not a code
// defined in the GLFW C documentation as a programmer error, then the
// caller should have accepted it. This is effectively a bug in this
// package.
switch err.Code {
case platformError:
fmt.Fprintln(os.Stderr, err)
return nil
case notInitialized, noCurrentContext, invalidEnum, invalidValue, outOfMemory:
panic(err)
default:
fmt.Fprintln(os.Stderr, "GLFW: An invalid error was not accepted by the caller:", err)
fmt.Fprintln(os.Stderr, "GLFW: Please report this bug in the Go package immediately.")
panic(err)
}
}
// fetchError fetches the next error from the error channel, it does not block
// and returns nil if there is no error present.
func fetchError() *Error {

View File

@ -40,14 +40,10 @@ const (
// This function may only be called from the main thread.
func Init() error {
C.glfwInit()
// invalidValue can happen when specific joysticks are used. This issue
// will be fixed in GLFW 3.3.5. As a temporary fix, ignore this error.
// See go-gl/glfw#292, go-gl/glfw#324, and glfw/glfw#1763.
err := acceptError(APIUnavailable, invalidValue)
if e, ok := err.(*Error); ok && e.Code == invalidValue {
return nil
if err := fetchErrorIgnoringPlatformError(); err != nil {
return err
}
return err
return nil
}
// Terminate destroys all remaining windows, frees any allocated resources and
@ -110,13 +106,18 @@ func GetVersionString() string {
// contains or is convertible to a UTF-8 encoded string.
//
// This function may only be called from the main thread.
func GetClipboardString() string {
func GetClipboardString() (string, error) {
cs := C.glfwGetClipboardString(nil)
if cs == nil {
_ = acceptError(FormatUnavailable)
return ""
if err := fetchErrorIgnoringPlatformError(); err != nil {
if cerr, ok := err.(*Error); ok && cerr.Code == FormatUnavailable {
return "", nil
}
return "", err
}
return "", nil
}
return C.GoString(cs)
return C.GoString(cs), nil
}
// SetClipboardString sets the system clipboard to the specified UTF-8 encoded

View File

@ -395,7 +395,10 @@ func CreateWindow(width, height int, title string, monitor *Monitor, share *Wind
w := C.glfwCreateWindow(C.int(width), C.int(height), t, m, s)
if w == nil {
return nil, acceptError(APIUnavailable, VersionUnavailable)
if err := fetchErrorIgnoringPlatformError(); err != nil {
return nil, err
}
return nil, nil
}
wnd := &Window{data: w}
@ -1047,13 +1050,18 @@ func (w *Window) SetClipboardString(str string) error {
// glfw.GetClipboardString()
//
// This function may only be called from the main thread.
func (w *Window) GetClipboardString() string {
func (w *Window) GetClipboardString() (string, error) {
cs := C.glfwGetClipboardString(w.data)
if cs == nil {
_ = acceptError(FormatUnavailable)
return ""
if err := fetchErrorIgnoringPlatformError(); err != nil {
if cerr, ok := err.(*Error); ok && cerr.Code == FormatUnavailable {
return "", nil
}
return "", err
}
return "", nil
}
return C.GoString(cs)
return C.GoString(cs), nil
}
// PollEvents processes only those events that have already been received and