Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mouse button stops working when GLFW window regains focus #4984

Closed
dpasca opened this issue Feb 5, 2022 · 7 comments
Closed

Mouse button stops working when GLFW window regains focus #4984

dpasca opened this issue Feb 5, 2022 · 7 comments

Comments

@dpasca
Copy link

dpasca commented Feb 5, 2022

Version/Branch of Dear ImGui:

Version: 1.87 WIP
Branch: master

Back-end/Renderer/Compiler/OS

Dear ImGui 1.87 WIP (18616)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=201402
define: __linux__
define: __GNUC__=10
--------------------------------
io.BackendPlatformName: imgui_impl_glfw
io.BackendRendererName: imgui_impl_opengl2
io.ConfigFlags: 0x00000000
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00000006
 HasMouseCursors
 HasSetMousePos
libglfw3-dev/stable,now 3.3.2-1 amd64 [installed]
libglfw3/stable,now 3.3.2-1 amd64 [installed,automatic]

Problem verified under IceWM in Debian 11. Not an issue with Xfce.

My Issue/Question:

The mouse button stops working when the window loses focus and gets back focus by clicking inside.

Steps for the issue:

  1. Launch example_glfw_opengl2
  2. Click on any widget to verify that the mouse button works
  3. Click outside the window, so that it loses focus
  4. Click back into the example window
  5. The mouse button no longer registers on widgets

This is not an issue when the window is minimized & restored.

I think that the problem is that when clicking back into the example window, io.AddMouseButtonEvent() in ImGui_ImplGlfw_MouseButtonCallback() is called before io.AppFocusLost is updated in https://github.com/ocornut/imgui/blob/master/imgui.cpp#L7868

Losing focus and taking it back by doing minimization & restore, is not a problem because in that case io.AppFocusLost is updated before there's any mouse clicking event, like I guess it should be.

Unfortunately I don't understand ImGui's internals, so I'm not able to easily fix this myself. Perhaps it's an obvious fix for those with more experience. Thanks.

Standalone, minimal, complete and verifiable example:

Unmodified example_glfw_opengl2

@dpasca
Copy link
Author

dpasca commented Feb 6, 2022

I tried to completely disable ImGui_ImplGlfw_WindowFocusCallback(), so that the ImGui never thinks that it has lost focus, but the problem persists. I'm not really sure what it is.

@dpasca
Copy link
Author

dpasca commented Feb 7, 2022

I found the commit that introduced the issue: 200a8f1

@ocornut
Copy link
Owner

ocornut commented Feb 7, 2022

Hello,
You'll probably want to add debug printing to ImGui_ImplGlfw_MouseButtonCallback(), ImGui_ImplGlfw_CursorPosCallback(), ImGui_ImplGlfw_CursorEnterCallback() and see what you get.

@dpasca
Copy link
Author

dpasca commented Feb 7, 2022

Hello, You'll probably want to add debug printing to ImGui_ImplGlfw_MouseButtonCallback(), ImGui_ImplGlfw_CursorPosCallback(), ImGui_ImplGlfw_CursorEnterCallback() and see what you get.

Tracking ImGui_ImplGlfw_CursorEnterCallback() helped !

After refocusing, every time the mouse button is pressed, ImGui_ImplGlfw_CursorEnterCallback() is called twice, one with 0 and the other with 1.

The problem goes deeper than glfw... because xtrace shows that additional EnterNotify and LeaveNotify X11 events are generated in a sequence. This is with the ImGui GLFW example, but the same thing happens tracing xterm !

Normal notification events, when moving the cursor in & out:

000:>:424d8: Event EnterNotify(7) detail=Nonlinear(0x03) mode=Normal(0x00) flags=focus,same-screen time=0x305480ee root=0x0000028e event=0x04a00007 child=None(0x00000000) root-x=641 root-y=460 event-x=7 event-y=436 state=0
000:>:42688: Event LeaveNotify(8) detail=Nonlinear(0x03) mode=Normal(0x00) flags=focus,same-screen time=0x30548472 root=0x0000028e event=0x04a00007 child=None(0x00000000) root-x=628 root-y=454 event-x=-6 event-y=430 state=0

Spurious notification events, generated on every mouse button down only after the window lost and regained focus by clicking into it:

000:>:20133: Event LeaveNotify(8) detail=Ancestor(0x00) mode=Grab(0x01) flags=same-screen time=0x30503ec2 root=0x0000028e event=0x04a00007 child=None(0x00000000) root-x=894 root-y=25 event-x=260 event-y=1 state=Button1
000:>:20134: Event EnterNotify(7) detail=Ancestor(0x00) mode=Ungrab(0x02) flags=focus,same-screen time=0x30503ec8 root=0x0000028e event=0x04a00007 child=None(0x00000000) root-x=894 root-y=25 event-x=260 event-y=1 state=Button1

I traced also with Xfce is on a separate computer in a VM and the spurious notifications don't appear, which makes sense, because there's no problem there.
Also, I don't know if this can make a difference, but I'm interacting via VNC.

I guess that the only thing that ImGui could do is a workaround.
I tried looking into known issues with IceWM, but I didn't seen anything about it.

@ocornut
Copy link
Owner

ocornut commented Feb 7, 2022

How does it behave with the SDL backend?

The thing is that those spurious "leave / enter" should in theory have no effect on behavior, so going to investigate that by simulating is on other backend...

ocornut added a commit that referenced this issue Feb 7, 2022
@ocornut
Copy link
Owner

ocornut commented Feb 7, 2022

I pushed a workaround to handle this: 732bc20

I think this issue might be worth reported to GLFW.

@ocornut ocornut closed this as completed Feb 7, 2022
@dpasca
Copy link
Author

dpasca commented Feb 7, 2022

The commit fixes the issue here. Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants