Fix the cursor not always grabbed again correctly after a focus gain
This commit is contained in:
parent
6f3273b7a6
commit
61526628d1
@ -34,6 +34,7 @@
|
||||
#include <SFML/System/Err.hpp>
|
||||
#include <SFML/System/Mutex.hpp>
|
||||
#include <SFML/System/Lock.hpp>
|
||||
#include <SFML/System/Sleep.hpp>
|
||||
#include <xcb/xcb_image.h>
|
||||
#include <xcb/randr.h>
|
||||
#include <X11/Xlibint.h>
|
||||
@ -72,6 +73,8 @@ namespace
|
||||
XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW |
|
||||
XCB_EVENT_MASK_VISIBILITY_CHANGE;
|
||||
|
||||
static const unsigned int maxTrialsCount = 5;
|
||||
|
||||
// Filter the events received by windows (only allow those matching a specific window)
|
||||
Bool checkEvent(::Display*, XEvent* event, XPointer userData)
|
||||
{
|
||||
@ -977,6 +980,9 @@ void WindowImplX11::setMouseCursorGrabbed(bool grabbed)
|
||||
return;
|
||||
|
||||
if (grabbed)
|
||||
{
|
||||
// Try multiple times to grab the cursor
|
||||
for (unsigned int trial = 0; trial < maxTrialsCount; ++trial)
|
||||
{
|
||||
sf::priv::ScopedXcbPtr<xcb_generic_error_t> error(NULL);
|
||||
|
||||
@ -999,12 +1005,16 @@ void WindowImplX11::setMouseCursorGrabbed(bool grabbed)
|
||||
if (!error && grabPointerReply && (grabPointerReply->status == XCB_GRAB_STATUS_SUCCESS))
|
||||
{
|
||||
m_cursorGrabbed = true;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// The cursor grab failed, trying again after a small sleep
|
||||
sf::sleep(sf::milliseconds(50));
|
||||
}
|
||||
|
||||
if (!m_cursorGrabbed)
|
||||
err() << "Failed to grab mouse cursor" << std::endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ScopedXcbPtr<xcb_generic_error_t> error(xcb_request_check(
|
||||
@ -1738,6 +1748,9 @@ bool WindowImplX11::processEvent(XEvent& windowEvent)
|
||||
|
||||
// Grab cursor
|
||||
if (m_cursorGrabbed)
|
||||
{
|
||||
// Try multiple times to grab the cursor
|
||||
for (unsigned int trial = 0; trial < maxTrialsCount; ++trial)
|
||||
{
|
||||
sf::priv::ScopedXcbPtr<xcb_generic_error_t> error(NULL);
|
||||
|
||||
@ -1757,12 +1770,18 @@ bool WindowImplX11::processEvent(XEvent& windowEvent)
|
||||
&error
|
||||
));
|
||||
|
||||
if (error || !grabPointerReply || (grabPointerReply->status != XCB_GRAB_STATUS_SUCCESS))
|
||||
if (!error && grabPointerReply && (grabPointerReply->status == XCB_GRAB_STATUS_SUCCESS))
|
||||
{
|
||||
err() << "Failed to grab mouse cursor" << std::endl;
|
||||
|
||||
m_cursorGrabbed = false;
|
||||
m_cursorGrabbed = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// The cursor grab failed, trying again after a small sleep
|
||||
sf::sleep(sf::milliseconds(50));
|
||||
}
|
||||
|
||||
if (!m_cursorGrabbed)
|
||||
err() << "Failed to grab mouse cursor" << std::endl;
|
||||
}
|
||||
|
||||
Event event;
|
||||
|
Loading…
Reference in New Issue
Block a user