XInput2: Use raw events and queries for buttons and keys

In X, the ButtonPress events generated when a mouse button is pressed
have a special property: if they don't activate an existing passive
grab, the X server automatically activates the "implicit passive grab"
on behalf of the client the event is delivered to.  This ensures the
ButtonRelease event is delivered to the same client even if the pointer
moves between windows, but it also causes all events from that pointer
to be delivered exclusively to that client.  As a consequence of the
implicit passive grab, for each window, only one client can listen for
ButtonPress events; any further listeners would never receive the event.

XInput 1 made the implicit grab optional and explicit by allowing
clients to listen for DeviceButtonPress events without
DeviceButtonPressGrab events.  XInput 2 does not have a separate grab
event class, but multiple clients can listen for XI_ButtonPress on the
same window.  When a button is pressed, the X server first tries to
deliver an XI_ButtonPress event; if no clients want it, then the server
tries to deliver a DeviceButtonPress event; if no clients want it, then
the server tries to deliver a ButtonPress event.  Once an event has been
delivered, event processing stops and earlier protocol levels are not
considered.  The reason for this rule is not obviously documented, but
it is probably because of the implicit passive grab; a client receiving
a ButtonPress event assumes it is the only client receiving that event,
and later protocols maintain that property for backward compatibility.

Before this commit, Dolphin listened for XI_ButtonPress events on the
root window.  This interferes with window managers that expect to
receive ButtonPress events on the root window, such as awesome and
Openbox.  In Openbox, applications are often launched from a menu
activated by clicking on the root window, and desktops are switched by
scroll wheel input on the root window.  This makes normal use of other
applications difficult when Dolphin is open (though Openbox keyboard
shortcuts still work).  Conversely, Dolphin only receives XI_ButtonPress
events for clicks on the root window or window decorations (title bars),
not on Dolphin's windows' content or the render window.  In window
managers that use a "virtual root window" covering the actual root
window, such as Mutter running in X, Dolphin and the window manager do
not conflict, but clicks delivered to other applications using XInput2
(for testing, try xinput --test-xi2) are not seen by Dolphin, which is
relevant when background input is enabled.

This commit changes Dolphin to listen for XI_RawButtonPress (and the raw
versions of other events); Dolphin was already listening to XI_RawMotion
for raw mouse movement.  Raw events are always and exclusively delivered
to the root window and are delivered to every client listening for them,
so Dolphin will not interfere with (or be interfered with by) other
applications listening for events.

As part of being raw, button numbers and keycodes in raw events have not
had mapping applied.  If a left-handed user swapped the left and right
buttons on their mouse, raw events do not reflect that.  It is possible
to query the mappings for each device and apply them manually, but that
would require a fair amount of code, including listening for mapping
changes.

Instead, Dolphin now uses the events only to set a "changed" flag, then
queries the current button and key state after processing all events.
Dolphin was already querying the pointer to get its absolute position
and querying the keyboard to filter the key bitmap it created from
events; now Dolphin also uses the button state from the pointer query
and uses the keyboard query directly.

Queries have a performance cost because they are synchronous requests to
the X server (Dolphin waits for the result).  Commit 2b640a4f made the
pointer query conditional on receiving a motion event to "cut down on
round trips", but commit bbb12a75 added an unconditional keyboard query,
and there have apparently been no performance complaints.  This commit
queries the pointer slightly more often (on button events in addition to
motion), but only queries the keyboard after key events, so the total
rate of queries should be substantially reduced.

Fixes: https://bugs.dolphin-emu.org/issues/10668
This commit is contained in:
Jeffrey Bosboom 2023-04-15 02:13:02 -07:00
parent 46540ea42b
commit b2a98c41ee

View File

@ -190,8 +190,8 @@ KeyboardMouse::KeyboardMouse(Window window, int opcode, int pointer, int keyboar
{
unsigned char mask_buf[(XI_LASTEVENT + 7) / 8] = {};
XISetMask(mask_buf, XI_ButtonPress);
XISetMask(mask_buf, XI_ButtonRelease);
XISetMask(mask_buf, XI_RawButtonPress);
XISetMask(mask_buf, XI_RawButtonRelease);
XISetMask(mask_buf, XI_RawMotion);
XIEventMask mask;
@ -203,9 +203,8 @@ KeyboardMouse::KeyboardMouse(Window window, int opcode, int pointer, int keyboar
{
unsigned char mask_buf[(XI_LASTEVENT + 7) / 8] = {};
XISetMask(mask_buf, XI_KeyPress);
XISetMask(mask_buf, XI_KeyRelease);
XISetMask(mask_buf, XI_FocusOut);
XISetMask(mask_buf, XI_RawKeyPress);
XISetMask(mask_buf, XI_RawKeyRelease);
XIEventMask mask;
mask.mask = mask_buf;
@ -272,6 +271,25 @@ void KeyboardMouse::UpdateCursor(bool should_center_mouse)
const auto win_width = std::max(win_attribs.width, 1);
const auto win_height = std::max(win_attribs.height, 1);
{
XIButtonState button_state;
XIModifierState mods;
XIGroupState group;
// Get the absolute position of the mouse pointer and the button state.
XIQueryPointer(m_display, pointer_deviceid, m_window, &root, &child, &root_x, &root_y, &win_x,
&win_y, &button_state, &mods, &group);
// X buttons are 1-indexed, so to get 32 button bits we need a larger type
// for the shift.
u64 buttons_zero_indexed = 0;
std::memcpy(&buttons_zero_indexed, button_state.mask,
std::min<size_t>(button_state.mask_len, sizeof(m_state.buttons)));
m_state.buttons = buttons_zero_indexed >> 1;
free(button_state.mask);
}
if (should_center_mouse)
{
win_x = win_width / 2;
@ -281,19 +299,6 @@ void KeyboardMouse::UpdateCursor(bool should_center_mouse)
g_controller_interface.SetMouseCenteringRequested(false);
}
else
{
// unused-- we're not interested in button presses here, as those are
// updated using events
XIButtonState button_state;
XIModifierState mods;
XIGroupState group;
XIQueryPointer(m_display, pointer_deviceid, m_window, &root, &child, &root_x, &root_y, &win_x,
&win_y, &button_state, &mods, &group);
free(button_state.mask);
}
const auto window_scale = g_controller_interface.GetWindowInputScale();
@ -309,10 +314,10 @@ void KeyboardMouse::UpdateInput()
// for the axis controls
float delta_x = 0.0f, delta_y = 0.0f, delta_z = 0.0f;
double delta_delta;
bool mouse_moved = false;
bool update_mouse = false, update_keyboard = false;
// Iterate through the event queue - update the axis controls, mouse
// button controls, and keyboard controls.
// Iterate through the event queue, processing raw pointer motion events and
// noting whether the button or key state has changed.
XEvent event;
while (XPending(m_display))
{
@ -325,28 +330,21 @@ void KeyboardMouse::UpdateInput()
if (!XGetEventData(m_display, &event.xcookie))
continue;
// only one of these will get used
XIDeviceEvent* dev_event = (XIDeviceEvent*)event.xcookie.data;
XIRawEvent* raw_event = (XIRawEvent*)event.xcookie.data;
switch (event.xcookie.evtype)
{
case XI_ButtonPress:
m_state.buttons |= 1 << (dev_event->detail - 1);
case XI_RawButtonPress:
case XI_RawButtonRelease:
update_mouse = true;
break;
case XI_ButtonRelease:
m_state.buttons &= ~(1 << (dev_event->detail - 1));
break;
case XI_KeyPress:
m_state.keyboard[dev_event->detail / 8] |= 1 << (dev_event->detail % 8);
break;
case XI_KeyRelease:
m_state.keyboard[dev_event->detail / 8] &= ~(1 << (dev_event->detail % 8));
case XI_RawKeyPress:
case XI_RawKeyRelease:
update_keyboard = true;
break;
case XI_RawMotion:
{
mouse_moved = true;
update_mouse = true;
XIRawEvent* raw_event = (XIRawEvent*)event.xcookie.data;
float values[4] = {};
size_t value_idx = 0;
@ -377,10 +375,6 @@ void KeyboardMouse::UpdateInput()
break;
}
case XI_FocusOut:
// Clear keyboard state on FocusOut as we will not be receiving KeyRelease events.
m_state.keyboard.fill(0);
break;
}
XFreeEventData(m_display, &event.xcookie);
@ -400,23 +394,13 @@ void KeyboardMouse::UpdateInput()
m_state.axis.z += delta_z;
m_state.axis.z /= SCROLL_AXIS_DECAY;
// Get the absolute position of the mouse pointer
const bool should_center_mouse =
g_controller_interface.IsMouseCenteringRequested() && Host_RendererHasFocus();
if (mouse_moved || should_center_mouse)
if (update_mouse || should_center_mouse)
UpdateCursor(should_center_mouse);
// KeyRelease and FocusOut events are sometimes not received.
// Cycling Alt-Tab and landing on the same window results in a stuck "Alt" key.
// Unpressed keys are released here.
// Because we called XISetClientPointer in the constructor, XQueryKeymap
// will return the state of the associated keyboard, even if it isn't the
// first master keyboard. (XInput2 doesn't provide a function to query
// keyboard state.)
std::array<char, 32> keyboard;
XQueryKeymap(m_display, keyboard.data());
for (size_t i = 0; i != keyboard.size(); ++i)
m_state.keyboard[i] &= keyboard[i];
if (update_keyboard)
XQueryKeymap(m_display, m_state.keyboard.data());
}
std::string KeyboardMouse::GetName() const