I'm trying to move and lock a mouse cursor in place, to calculate it's delta position after it was moved by the user. I'm using X11 and the XWarpPointer()
function. I found a similar problem in another thread and I followed it, but it didn't work. I even looked into SDL source code but still nothing. The main problem is that after I set the position of the mouse and then the user moves it, the mouse moves back to the original position (before using XWarpPointer
).
void sp::Mouse::getDeltaMousePosition(int *posX, int *posY)
{
if(!m_firstEnter)
{
getMousePosition(&m_lastX, &m_lastY);
m_firstEnter = true;
}
int tempX, tempY;
getMousePosition(&tempX, &tempY);
*posX = tempX - m_lastX;
*posY = tempY - m_lastY;
Window root_window = XRootWindow(ptr_display, 0);
SelectInput(ptr_display, root_window, KeyReleaseMask);
XWarpPointer(ptr_display, None, root_window, 0, 0, 0, 0, m_screenCenterX, m_screenCenterY);
XFlush(ptr_display);
m_lastX = m_screenCenterX;
m_lastY = m_screenCenterY;
}