Fix the cursor not always grabbed again correctly after a focus gain

This commit is contained in:
Victor Levasseur 2016-06-21 17:55:00 +02:00 committed by Lukas Dürrenberger
parent 6f3273b7a6
commit 61526628d1

View File

@ -34,6 +34,7 @@
#include <SFML/System/Err.hpp> #include <SFML/System/Err.hpp>
#include <SFML/System/Mutex.hpp> #include <SFML/System/Mutex.hpp>
#include <SFML/System/Lock.hpp> #include <SFML/System/Lock.hpp>
#include <SFML/System/Sleep.hpp>
#include <xcb/xcb_image.h> #include <xcb/xcb_image.h>
#include <xcb/randr.h> #include <xcb/randr.h>
#include <X11/Xlibint.h> #include <X11/Xlibint.h>
@ -72,6 +73,8 @@ namespace
XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW |
XCB_EVENT_MASK_VISIBILITY_CHANGE; XCB_EVENT_MASK_VISIBILITY_CHANGE;
static const unsigned int maxTrialsCount = 5;
// Filter the events received by windows (only allow those matching a specific window) // Filter the events received by windows (only allow those matching a specific window)
Bool checkEvent(::Display*, XEvent* event, XPointer userData) Bool checkEvent(::Display*, XEvent* event, XPointer userData)
{ {
@ -977,6 +980,9 @@ void WindowImplX11::setMouseCursorGrabbed(bool grabbed)
return; return;
if (grabbed) 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); 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)) if (!error && grabPointerReply && (grabPointerReply->status == XCB_GRAB_STATUS_SUCCESS))
{ {
m_cursorGrabbed = true; 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; err() << "Failed to grab mouse cursor" << std::endl;
} }
}
else else
{ {
ScopedXcbPtr<xcb_generic_error_t> error(xcb_request_check( ScopedXcbPtr<xcb_generic_error_t> error(xcb_request_check(
@ -1738,6 +1748,9 @@ bool WindowImplX11::processEvent(XEvent& windowEvent)
// Grab cursor // Grab cursor
if (m_cursorGrabbed) 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); sf::priv::ScopedXcbPtr<xcb_generic_error_t> error(NULL);
@ -1757,12 +1770,18 @@ bool WindowImplX11::processEvent(XEvent& windowEvent)
&error &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 = true;
break;
m_cursorGrabbed = false;
} }
// 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; Event event;