mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-01-10 16:19:28 +01:00
b2a98c41ee
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