POC fix : Return previous callbacks (set by C) #373
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello,
Behaviour of
go-gl/glfw
hurts composability when registering input callbacks after 3rdparty C code would have already registered some callbacks.My use case is :
CGo
+ImGui
, with the (unmodified) https://github.com/ocornut/imgui/blob/master/backends/imgui_impl_glfw.cpp "backend" targeting GLFWgo-gl/glfw
ImGUI
sets some callbacks here : https://github.com/ocornut/imgui/blob/v1.89.3/backends/imgui_impl_glfw.cpp#L451-L458ImGui
plays nice and set some flags : https://github.com/ocornut/imgui/blob/v1.89.3/imgui.h#L1995-L1996If you
Go
app comes "after" ImGui (registers someGo
GLFW's callbacks AFTERImGui
) , your app is supposed to :ImGui
)WantCaptureMouse
,WantCaptureKeyboard
) which would let you known ifImGui
had an usage of those inputsraytrace
to figure out where / on what object in the 3D world you clicked on...)HOWEVER, currently
go-gl/glfw
act as if there would never had been any C callbacks previously set :https://github.com/go-gl/glfw/blob/master/v3.3/glfw/input.go#L685-L695
As seen above,
go-gl/glfw
would only return a "previous" Go function from what it "remembered" to have "PERSONALLY ITSELF" set actually inw.fCursorPosHolder
Unless I'm missing something, it would be more sensical to let GLFW (C) directly inform us what was the "previous" callback, in case some were already set from C before.
I Proof-of-Concept'ed the proposed PR fix, as a short example only on
SetCursorPosCallback
;As you can see, the main changes for
SetCursorPosCallback()
are :glfwSetCursorPosCallbackCB()
(the*CB
variant)goCursorPosCB()
(//exported
)) in the Go'simport "C"
so the above wrapper is no longer neededC.glfwSetCursorPosCallback()
with the newly-declaredgoCursorPosCB()
(instead of going trough the now-uselessglfwSetCursorPosCallbackCB()
wrapper)goCursorPosCB_call()
to be able to "call a C function pointer from Go"goCursorPosCB_call()
wrapper since Go cannot directly call C function pointer (the Go function records the C function pointer as a closure)Result : it works on my setup (macOS, Intel, Go 1.20) ;
ImGui
happily gets the events since from Go I can now just :If you agree that the fix is correct, I would update the PR for the others callbacks.
King Regards,