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

Many trackpad mouse down events are lost (MacOS+SDL2+OpenGL2) #494

Open
cesss opened this issue Sep 6, 2022 · 6 comments · May be fixed by #553
Open

Many trackpad mouse down events are lost (MacOS+SDL2+OpenGL2) #494

cesss opened this issue Sep 6, 2022 · 6 comments · May be fixed by #553

Comments

@cesss
Copy link

cesss commented Sep 6, 2022

I'm using SDL2 with OpenGL2, on Macs with a Magic Trackpad configured as "tap to click" (ie: mouse-down events are generated by just tapping the trackpad, without having to fully press it).

When I tap, most of the times the mouse-down event is lost, so togglebuttons are not toggled, buttons are not pressed, etc, etc...

A few times it works, other times doesn't. Strange. I tried to debug it, but I don't know the Nuklear internals, so I don't know where to look for.

The only things I can say for the moment are these:

  • nk_sdl_handle_event() in nuklear_sdl_gl2.h is getting an event for all the taps. No events are lost. So, the problem is not in SDL2, because the events are arriving correctly to Nuklear.
  • For every tap, I'm getting both a mouse down and a mouse up event. So, the problem is not about mouse clicks being unterminated or unstarted.
  • In every mouse up/down event, ctx->input.mouse.buttons[NK_BUTTON_LEFT].down is different from evt->type == SDL_MOUSEBUTTONDOWN. So, function nk_input_button() doesn't return prematurely at the check for both being equal.

As I said, I don't know the Nuklear internals, so I wasn't able to dig deeper. Do you have any idea on what might be happening?

@cesss
Copy link
Author

cesss commented Sep 6, 2022

I was able to find the problem. The reason is that for these Trackpad taps, MacOS generates two consecutive events. File main.c of the SDL+OpenGL2 demo has this loop, whose purpose I guess is to combine consecutive events and thus be more efficient and improve performance:

while (SDL_PollEvent(&evt)) {
            if (evt.type == SDL_QUIT) goto cleanup;
            nk_sdl_handle_event(&evt);
        }

However, some events should never be combined. I've modified the loop as follows, and now all Trackpad taps are handled correctly, and no events are lost:

while (SDL_PollEvent(&evt)) {
            if (evt.type == SDL_QUIT) goto cleanup;
            nk_sdl_handle_event(&evt);
            if((evt.type==SDL_MOUSEBUTTONUP)||(evt.type==SDL_MOUSEBUTTONDOWN)||(evt.type==SDL_KEYUP)||(evt.type==SDL_KEYDOWN)) break;
        }

As you can see, I also included key up and key down events as not-combinable. I didn't have a problem with key up/down events, but my mind says is not a good idea to try to combine them.

In fact, I believe the only ones suitable for combining are motion events. And maybe this loop should be removed and never try to combine any event.

I'm leaving this issue open, because I believe the source code in the repository should be ready to work fine on MacBooks, so I think a good solution should be found.

@cesss
Copy link
Author

cesss commented Sep 7, 2022

This issue might be the same (or not) described at vurtun/nuklear#407
(I'm mentioning that issue from here, because it described the same situation and in Macs, so future users that are affected and read that issue in the old repository, can take a look at the solution I found)

@dumblob
Copy link
Member

dumblob commented Sep 7, 2022

Before discussing this further, would you have time to try the StickyMouseButtons input mode also as suggested in the old thread you found?

Thanks!

@cesss
Copy link
Author

cesss commented Sep 9, 2022

StickyMouseButtons is GLFW-specific. I'm experiencing this in SDL. And, even then, if it worked would be a workaround, not a fix, because the problem is not in the external library used, but in the Nuklear demos: they make the assumption that two consecutive mouse events with zero time between them can be combined together and managed like if it was a single event. In the case of motion events, maybe it can be true, but in the case of mouse clicks (and key clicks) it's a wrong assumption, and it's the reason by Trackpad tapping is missing events.

@cesss
Copy link
Author

cesss commented Sep 9, 2022

Note that the bug is not in Nuklear, but in the examples. If you implement your application loop taking care of never combining events related to clicks, then it will work perfectly with Apple Trackpads.

@dumblob
Copy link
Member

dumblob commented Sep 10, 2022

StickyMouseButtons is GLFW-specific. I'm experiencing this in SDL.

Oh, my bad. Thanks for clarification!

We definitely want to have demos working cross-platform as much as we can. The examples/demos are still just a showcase what can be done and are not a comprehensive solution.

That being said I am eager to accept PRs which combines only events can be combined. IIRC the reason for combining events was to save power and computational resources if the events come in higher frequency than the desired frame rate.

For non-nuklear.h PRs we are not that strict so do not be afraid of it 😉 - see https://github.com/Immediate-Mode-UI/Nuklear#reviewers-guide .

@cblc cblc linked a pull request Apr 9, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants