From 7e0711169683e69e6819f44c910f00e8d746abd7 Mon Sep 17 00:00:00 2001 From: folays Date: Mon, 13 Mar 2023 01:05:13 +0100 Subject: [PATCH 1/3] POC fix : Return previous callbacks (set by C) --- v3.3/glfw/input.c | 5 +++-- v3.3/glfw/input.go | 19 ++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/v3.3/glfw/input.c b/v3.3/glfw/input.c index 27e00cb4..6ec3e630 100644 --- a/v3.3/glfw/input.c +++ b/v3.3/glfw/input.c @@ -20,8 +20,9 @@ void glfwSetMouseButtonCallbackCB(GLFWwindow *window) { glfwSetMouseButtonCallback(window, (GLFWmousebuttonfun)goMouseButtonCB); } -void glfwSetCursorPosCallbackCB(GLFWwindow *window) { - glfwSetCursorPosCallback(window, (GLFWcursorposfun)goCursorPosCB); +void goCursorPosCB_call(GLFWcursorposfun cbfun, GLFWwindow *window, double xpos, double ypos) +{ + cbfun(window, xpos, ypos); } void glfwSetCursorEnterCallbackCB(GLFWwindow *window) { diff --git a/v3.3/glfw/input.go b/v3.3/glfw/input.go index 53640d3c..cb67f7ff 100644 --- a/v3.3/glfw/input.go +++ b/v3.3/glfw/input.go @@ -3,12 +3,15 @@ package glfw //#include //#define GLFW_INCLUDE_NONE //#include "glfw/include/GLFW/glfw3.h" +// +//void goCursorPosCB(GLFWwindow* window, double xpos, double ypos); +// //void glfwSetJoystickCallbackCB(); //void glfwSetKeyCallbackCB(GLFWwindow *window); //void glfwSetCharCallbackCB(GLFWwindow *window); //void glfwSetCharModsCallbackCB(GLFWwindow *window); //void glfwSetMouseButtonCallbackCB(GLFWwindow *window); -//void glfwSetCursorPosCallbackCB(GLFWwindow *window); +//void goCursorPosCB_call(GLFWcursorposfun cbfun, GLFWwindow *window, double xpos, double ypos); //void glfwSetCursorEnterCallbackCB(GLFWwindow *window); //void glfwSetScrollCallbackCB(GLFWwindow *window); //void glfwSetDropCallbackCB(GLFWwindow *window); @@ -335,8 +338,8 @@ func goMouseButtonCB(window unsafe.Pointer, button, action, mods C.int) { } //export goCursorPosCB -func goCursorPosCB(window unsafe.Pointer, xpos, ypos C.double) { - w := windows.get((*C.GLFWwindow)(window)) +func goCursorPosCB(window *C.GLFWwindow, xpos, ypos C.double) { + w := windows.get(window) w.fCursorPosHolder(w, float64(xpos), float64(ypos)) } @@ -683,15 +686,17 @@ type CursorPosCallback func(w *Window, xpos float64, ypos float64) // when the cursor is moved. The callback is provided with the position relative // to the upper-left corner of the client area of the window. func (w *Window) SetCursorPosCallback(cbfun CursorPosCallback) (previous CursorPosCallback) { - previous = w.fCursorPosHolder w.fCursorPosHolder = cbfun + var previousC C.GLFWcursorposfun if cbfun == nil { - C.glfwSetCursorPosCallback(w.data, nil) + previousC = C.glfwSetCursorPosCallback(w.data, nil) } else { - C.glfwSetCursorPosCallbackCB(w.data) + previousC = C.glfwSetCursorPosCallback(w.data, (C.GLFWcursorposfun)(C.goCursorPosCB)) } panicError() - return previous + return func(w2 *Window, xpos2 float64, ypos2 float64) { + C.goCursorPosCB_call(previousC, w2.data, C.double(xpos2), C.double(ypos2)) + } } // CursorEnterCallback is the cursor boundary crossing callback. From cd2f47c0c9fd52b8b817bd5d5c094cd3203928fa Mon Sep 17 00:00:00 2001 From: folays Date: Mon, 13 Mar 2023 01:39:56 +0100 Subject: [PATCH 2/3] previous callbacks : do not forget to simply return `nil` if previous callback was unset --- v3.3/glfw/input.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/v3.3/glfw/input.go b/v3.3/glfw/input.go index cb67f7ff..669696a1 100644 --- a/v3.3/glfw/input.go +++ b/v3.3/glfw/input.go @@ -694,6 +694,9 @@ func (w *Window) SetCursorPosCallback(cbfun CursorPosCallback) (previous CursorP previousC = C.glfwSetCursorPosCallback(w.data, (C.GLFWcursorposfun)(C.goCursorPosCB)) } panicError() + if previousC == nil { + return nil + } return func(w2 *Window, xpos2 float64, ypos2 float64) { C.goCursorPosCB_call(previousC, w2.data, C.double(xpos2), C.double(ypos2)) } From 26f41da445e3e773a01883a77ca8b2c6a2615b2a Mon Sep 17 00:00:00 2001 From: folays Date: Mon, 13 Mar 2023 07:13:10 +0100 Subject: [PATCH 3/3] fix logic --- v3.3/glfw/input.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/v3.3/glfw/input.go b/v3.3/glfw/input.go index 669696a1..21958389 100644 --- a/v3.3/glfw/input.go +++ b/v3.3/glfw/input.go @@ -686,6 +686,7 @@ type CursorPosCallback func(w *Window, xpos float64, ypos float64) // when the cursor is moved. The callback is provided with the position relative // to the upper-left corner of the client area of the window. func (w *Window) SetCursorPosCallback(cbfun CursorPosCallback) (previous CursorPosCallback) { + previous = w.fCursorPosHolder w.fCursorPosHolder = cbfun var previousC C.GLFWcursorposfun if cbfun == nil { @@ -697,6 +698,9 @@ func (w *Window) SetCursorPosCallback(cbfun CursorPosCallback) (previous CursorP if previousC == nil { return nil } + if previousC == (C.GLFWcursorposfun)(C.goCursorPosCB) { + return previous + } return func(w2 *Window, xpos2 float64, ypos2 float64) { C.goCursorPosCB_call(previousC, w2.data, C.double(xpos2), C.double(ypos2)) }