2023-08-03 16:51:17 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2023-07-07 19:15:47 +02:00
|
|
|
// SPDX-FileCopyrightText: 2012 The glfw3-go Authors
|
|
|
|
// SPDX-FileCopyrightText: 2023 The Ebitengine Authors
|
|
|
|
|
2023-07-08 06:59:51 +02:00
|
|
|
//go:build darwin || freebsd || linux || netbsd || openbsd
|
2023-07-05 03:51:55 +02:00
|
|
|
|
2023-10-07 16:21:29 +02:00
|
|
|
package glfw
|
2023-07-05 03:51:55 +02:00
|
|
|
|
2023-07-07 16:52:56 +02:00
|
|
|
// #define GLFW_INCLUDE_NONE
|
2023-10-07 09:36:55 +02:00
|
|
|
// #include "glfw3_unix.h"
|
2023-07-07 16:52:56 +02:00
|
|
|
//
|
|
|
|
// void goErrorCB(int code, char* desc);
|
|
|
|
//
|
|
|
|
// static void glfwSetErrorCallbackCB() {
|
|
|
|
// glfwSetErrorCallback((GLFWerrorfun)goErrorCB);
|
|
|
|
// }
|
2023-07-05 03:51:55 +02:00
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2023-10-07 15:48:41 +02:00
|
|
|
"errors"
|
2023-07-05 03:51:55 +02:00
|
|
|
"fmt"
|
2023-10-07 11:36:20 +02:00
|
|
|
"os"
|
2023-07-05 03:51:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Note: There are many cryptic caveats to proper error handling here.
|
|
|
|
// See: https://github.com/go-gl/glfw3/pull/86
|
|
|
|
|
2023-10-07 15:28:15 +02:00
|
|
|
// lastError holds the value of the last error.
|
2023-10-07 15:48:41 +02:00
|
|
|
var lastError = make(chan error, 1)
|
2023-07-05 03:51:55 +02:00
|
|
|
|
|
|
|
//export goErrorCB
|
|
|
|
func goErrorCB(code C.int, desc *C.char) {
|
2023-10-07 15:48:41 +02:00
|
|
|
err := fmt.Errorf("glfw: %s: %w", C.GoString(desc), ErrorCode(code))
|
2023-07-05 03:51:55 +02:00
|
|
|
select {
|
|
|
|
case lastError <- err:
|
|
|
|
default:
|
2023-10-07 13:27:50 +02:00
|
|
|
fmt.Fprintln(os.Stderr, "GLFW: An uncaught error has occurred:", err)
|
|
|
|
fmt.Fprintln(os.Stderr, "GLFW: Please report this bug in the Go package immediately.")
|
2023-07-05 03:51:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the glfw callback internally
|
|
|
|
func init() {
|
|
|
|
C.glfwSetErrorCallbackCB()
|
|
|
|
}
|
|
|
|
|
2023-12-17 14:42:34 +01:00
|
|
|
// fetchErrorIgnoringPlatformError is fetchError ignoring platformError.
|
2023-10-07 11:05:03 +02:00
|
|
|
func fetchErrorIgnoringPlatformError() error {
|
|
|
|
select {
|
|
|
|
case err := <-lastError:
|
2023-10-07 15:48:41 +02:00
|
|
|
if errors.Is(err, PlatformError) {
|
2023-10-07 11:05:03 +02:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|