The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL2 MOUSEBUTTONDOWN & MOUSEMOTION Event Bug!
Magnet


Joined: 14 May 2014
Posts: 20
SDL2.0 (Windows)

I believe I've found a bug with mouse events which I've been able to re-produce in my app and other apps that captures MOUSEBUTTONDOWN and MOUSEMOTION events.

The Basics:
Code:

// For Simplicity
SDL_Event event;

while(SDL_PollEvent(&event) != 0)
{
    switch (event.type)
    {
     case SDL_MOUSEBUTTONUP:
     
        // If the left mouse button release, reset flag to false!
        if( event.button.button == SDL_BUTTON_LEFT )
        {
            isMouseSelection = false;

            //Debuggin: Get the mouse offsets
            std::cout << "Mouse released at: " << event.button.x << "," << event.button.y << std::endl;
        }
        break;
               
    case SDL_MOUSEBUTTONDOWN:

        // We want mouse down on left button only.
        if( event.button.button == SDL_BUTTON_LEFT )
        {
            // Mark extra flag that the mouse down has executed.
            isMouseSelection = true; 

            //Debuggin: Get the mouse offsets
            std::cout << "Mouse pressed at: " << event.button.x << "," << event.button.y << std::endl;
        }
        break;
               
    case SDL_MOUSEMOTION:

        // We only want Motion when the Left button is down!
        // And isMouseSelection is TRUE
        if( event.button.button == SDL_BUTTON_LEFT
            && isMouseSelection)
        {
            std::cout << "Mouse moved by: "
                << event.motion.xrel << ", " << event.motion.yrel
                << " : "
                << event.motion.x << "," << event.motion.y
                << std::endl;
        }
        break;
  }
}         


Pretty simple, On left button down, we set isMouseSelection = true. On release we set isMouseSelection = false.
Only when isMouseSelection == true & LEFT mouse is down, should the MOUSEMOTION be captured.

The problem:
If your in a window (not full screen) and change focus to the DESKTOP or another window

1. Left Click outside of the SDL window to change the focus to the desktop or another program.
2. Left Click back on your programs windows (But click the Window Title So your not clicking on the actual canvas, your clicking on the window border itself at the TOP).

When moving your mouse around the canvas this appears to register the active MOUSEMOTION event for the LEFT button. (meaning SDL thinks the left mouse button is still being held down).

As I move the mouse around the following is somehow showing true and displaying the MOTION output over the entire canvas until i reset it by clicking and releasing the LEFT Mouse button again.

Both the Left Mouse Button, and the Variable only set in the MouseDown event is active!

Code:

event.button.button == SDL_BUTTON_LEFT
            && isMouseSelection


I can see that clicking the window title might execute a MOUSEBUTTONDOWN event, but it should also register a MOUSEBUTTONUP on the release which it doesn't doing.
It should also not be showing event.button.button == SDL_BUTTON_LEFT in the MOUSEMOTION event becasue the button is not being held down, only the program title was clicked and released for focus.

This is leaving the mouse event active the entire time which i believe should be looked into and corrected.

I hope I've explained this clearly.
Extra Notes
Magnet


Joined: 14 May 2014
Posts: 20
Some Extra Notes:

Even if you put some extra checking on SDL_WINDOWEVENT_FOCUS_GAINED or LOST.
This only catches maybe 50% then without it. The issues are still there when clicking DESKTOP, Then Window Title, DESKTOP, then Window Title.

Also what is odd, is when you click the window title and regain focus, it never gives the output in the debug window from the MOUSEBUTTONDOWN event

ex..
Code:

case SDL_MOUSEBUTTONDOWN:

        // We want mouse down on left button only.
        if( event.button.button == SDL_BUTTON_LEFT )
        {
            // Mark extra flag that the mouse down has executed.
            isMouseSelection = true;

            //Debuggin: Get the mouse offsets
            std::cout << "Mouse pressed at: " << event.button.x << "," << event.button.y << std::endl;
        }
        break;


I don't see the std::cout printed the the screen. So something is off I believe, and when I get more time i'll have to dig through the actual event code in SDL.
Little more research
Magnet


Joined: 14 May 2014
Posts: 20
I added some more SDL Logging and found the following:

If you click on a canvas a several times, then click out of the window (desktop). Then click back on the Applications title bar you get all of the following events

INFO: Window 1 gained keyboard focus
INFO: Mouse entered window 1
Mouse pressed at: 490,0
INFO: SDL_MOUSEBUTTONDOWN 1 shown
Mouse moved by: 3, -18 : 493,0


So you get a:
1. Gained Keyboard focus
2. Window entered, which should only happen after you move over the canvas.
3. Mouse Pressed showing 1 = LEFT BUTTON
4. Extra Mouse Movement

This is from a single click and release on the Window Title with no movement over the canvas. Keep in mind there is still no MOUSEBUTTONUP for a release so this state stays in motion.
Then if you click on the canvas, you do not get another MOUSEBUTTONDOWN, only a MOUSEBUTTONUP for a release.

I hope someone else will give this a shot and confirm my finding sometime. I believe this will be an issue for anyone using the mouse and holding down the button and dragging for tasks, and if they leave the window and click back on the title bar which a lot of people do and their button and motions are still considered active it will cause issues.

Thanks for your help.
Update
Magnet


Joined: 14 May 2014
Posts: 20
I've had another developer confirm the bug for me so I've created a bugzilla entry.

https://bugzilla.libsdl.org/show_bug.cgi?id=2842

There is also a main.cpp source file attached to the bug report giving proof of concept.

Thanks/
SDL2 MOUSEBUTTONDOWN & MOUSEMOTION Event Bug!
Jonny D


Joined: 12 Sep 2009
Posts: 932
Can you try using if(event.motion.state & SDL_BUTTON_LMASK ...) in the mouse motion event test?  The way you are testing event.button.button will generate undefined behavior (due to union type).

Jonny D


On Monday, January 12, 2015, Magnet wrote:
Quote:
I've had another developer confirm the bug for me so I've created a bugzilla entry.

https://bugzilla.libsdl.org/show_bug.cgi?id=2842

There is also a main.cpp source file attached to the bug report giving proof of concept.

Thanks/

Re: SDL2 MOUSEBUTTONDOWN & MOUSEMOTION Event Bug!
Naith


Joined: 03 Jul 2014
Posts: 158
Jonny D wrote:
Can you try using if(event.motion.state & SDL_BUTTON_LMASK ...) in the mouse motion event test?  The way you are testing event.button.button will generate undefined behavior (due to union type).

Jonny D

I am using the SDL_BUTTON mask (which the SDL_BUTTON_LMASK internally is using) whenever I check for a mouse events and I can confirm that the bug is occuring when it is used aswell.

Bug occur in this way: I hold a mouse button, the mouse pointer leaves the window (still holding the button), click on another window so that the SDL window looses focus, I release the mouse button and returns to the SDL window by clicking on the SDL window top bar. When doing this, SDL thinks I'm still holding the mouse button and I have to click inside the window to "release the button".
If I, when returning to the SDL window, clicks directly inside the window (i.e not clicking on the window top bar), SDL understands that I have released the button.
SDL2 MOUSEBUTTONDOWN & MOUSEMOTION Event Bug!
Jonny D


Joined: 12 Sep 2009
Posts: 932
My comment is not specifically about the mask usage.  It's about using event.motion.state versus using event.button.button in an SDL_MOUSEMOTIONEVENT.  event.button.button is invalid in that context, yet that is what the provided code uses.

Jonny D






On Mon, Jan 12, 2015 at 10:37 AM, Naith wrote:
Quote:
Sorry, I thought you (Jonny D) meant the regular SDL_BUTTON mask and not the SDL_BUTTON_LMASK. But yeah, the bug occur with the SDL_BUTTON mask aswell as regular SDL_MOUSEBUTTONDOWN event.


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: SDL2 MOUSEBUTTONDOWN & MOUSEMOTION Event Bug!
Magnet


Joined: 14 May 2014
Posts: 20
HI Jonny,

Thanks for the time and response. As soon as I get home I'll be happy to test your suggestion.

I still think there is a problem in general where a MouseButtonDown event is trigger with no release when clicking on the title bar. I'm still new working on my first SDL application so I appreciate the clarification.

MIke

Jonny D wrote:
My comment is not specifically about the mask usage.  It's about using event.motion.state versus using event.button.button in an SDL_MOUSEMOTIONEVENT.  event.button.button is invalid in that context, yet that is what the provided code uses.

Jonny D

On Mon, Jan 12, 2015 at 10:37 AM, Naith wrote:
Quote:
Sorry, I thought you (Jonny D) meant the regular SDL_BUTTON mask and not the SDL_BUTTON_LMASK. But yeah, the bug occur with the SDL_BUTTON mask aswell as regular SDL_MOUSEBUTTONDOWN event.


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: SDL2 MOUSEBUTTONDOWN & MOUSEMOTION Event Bug!
Magnet


Joined: 14 May 2014
Posts: 20
Jonny D wrote:
Can you try using if(event.motion.state & SDL_BUTTON_LMASK ...) in the mouse motion event test?  The way you are testing event.button.button will generate undefined behavior (due to union type).

Jonny D

On Monday, January 12, 2015, Magnet wrote:


Hi Jonny,

I just tried your suggestion and there is no change in behavior. The issue still persists with LEFT Mouse button still being active.

Code:

case SDL_MOUSEMOTION:
   // We only want Motion when the Left button is down!
   // And isMouseSelection is TRUE
   //if (event.button.button == SDL_BUTTON_LEFT
        if ((event.motion.state & SDL_BUTTON_LMASK)
            && isMouseSelection)
        {
       std::cout << "Mouse moved by: "
      << event.motion.xrel << ", " << event.motion.yrel
      << " : "
      << event.motion.x << "," << event.motion.y
      << std::endl;
   }
   break;


isMouseSelection is of course true because the click on the title bar registers the MOUSEBUTTONDOWN event.
However, the SDL_BUTTON_LMASK is still showing as active leaving no change in the behavior.