Commit graph

47 commits

Author SHA1 Message Date
Filoppi
e456bef163 Input: Improve Controller Interface devices threading
This specific issue was already addressed by https://github.com/dolphin-emu/dolphin/pull/11635
though I felt like there was something more we could do, and wasn't too happy with the
likelihood of devices update calls being skipped (due to `m_devices_population_mutex` being locked).
2023-12-18 21:45:22 +02:00
Jeffrey Bosboom
620955d397 XInput2: Listen to master devices only
A comment removed by this commit gives two reasons for listening to
slave devices, both of which no longer apply:

- "Only slaves emit raw motion events": perhaps this was true when the
comment was written, but now master devices provide raw motion events
along with the other raw events.

- "Selecting slave keyboards avoids dealing with key focus": we get raw
key events regardless of the focus.

Listening to both master and slave devices results in duplicate raw
events.  For button and key events, that's a tiny waste of time setting
the update flag a second time, but for raw mouse events the raw motion
will be processed twice.  That makes this commit a user-facing change.
2023-05-19 14:20:10 -07:00
Jeffrey Bosboom
b2a98c41ee 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
2023-05-19 14:20:10 -07:00
Jeffrey Bosboom
46540ea42b XInput2: Request XInput 2.1
We need XInput 2.1 to get raw events on the root window even while
another client has a grab.  We currently use raw events for relative
mouse input, and upcoming commits will use raw events for buttons and
keys.
2023-05-19 14:20:09 -07:00
Jeffrey Bosboom
a902480cb0 XInput2: Make button state a u32
Because we care how many bits it has, not its arithmetic range.  No
functional change on all supported platforms.
2023-05-19 14:20:09 -07:00
Jeffrey Bosboom
c6f9e61d38 XInput2: Accept input from keyboards other than the first master
XInput2 was created to support multiple pointer/keyboard pairs (often
called MPX for multi-pointer X).  Dolphin's XInput2 implementation has
always supported MPX by creating a KeyboardMouse object per master
pointer.  Since commit bbb12a7, Dolphin's keyboard state is filtered by
the output of XQueryKeymap.  As a core X function, XQueryKeymap queries
"the" keyboard, which by default is the first master keyboard.  As a
result, Dolphin will ignore keys pressed on other master keyboards
unless the first master is simultaneously pressing the same keys.

XInput2 doesn't provide a function to query the keyboard state.  There
is no XIQueryKeymap and the current state is not a member of the
XIKeyClassInfo returned by XIQueryDevice.  Instead, XInput2 allows a
master pointer to be nominated as "the" pointer on a per-client basis,
with "the" keyboard automatically becoming the associated master
keyboard.  The "documentation" [1] says passing None for the window is
only for debugging purposes, but it is documented in the
XISetClientPointer man page and seems to be the only way to query
keyboards beyond the first.

With this commit, Dolphin correctly reads keys from keyboards other than
the first master keyboard.  To test, use the xinput command-line utility
to create a master pointer and reattach a keyboard to the associated
master keyboard.

[1]: https://who-t.blogspot.com/2009/07/xi2-recipes-part-6.html
     (the XInput2 developer's blog)
2023-04-13 20:10:00 -07:00
Minty-Meeo
f29019180f Pragma diagnostic ignore [-Wregister] is no longer needed 2023-04-12 03:59:57 -05:00
Pokechu22
7fafb00561 InputCommon/XInput2: Fix shadowing warning 2023-02-09 16:23:02 -08:00
TheConfuZzledDude
288fa635f9 InputCommon/XInput2: Changed axis value reading to be more correct 2022-12-26 17:27:48 +00:00
TheConfuZzledDude
27d596b89c InputCommon/Xinput: Cleaned up a bit 2022-12-23 22:35:09 +00:00
TheConfuZzledDude
c3018fdc3b InputCommon/XInput2: Added an axis output for the scroll wheel 2022-12-23 22:10:28 +00:00
Admiral H. Curtiss
e0870166ef
XInput2: Handle mouse centering hotkey. 2022-07-25 01:26:56 +02:00
Pokechu22
2025763420 Treewide: Adjust order of includes 2021-12-10 14:49:57 -08:00
Pierre Bourdon
e149ad4f0a
treewide: convert GPLv2+ license info to SPDX tags
SPDX standardizes how source code conveys its copyright and licensing
information. See https://spdx.github.io/spdx-spec/1-rationale/ . SPDX
tags are adopted in many large projects, including things like the Linux
kernel.
2021-07-05 04:35:56 +02:00
iwubcode
db4b4e40cb InputCommon / DolphinQt / Core: Add a "RelativeMouse" input which provides the raw delta mouse input
Co-authored-by: Jordan Woyak <jordan.woyak@gmail.com>
2021-03-17 20:58:33 -05:00
Filippo Tarpini
75f35393c3
Fix XInput2 cursor going to +infinite if the window size was 0 2021-01-02 18:30:14 +02:00
Jordan Woyak
bbb12a7560 Linux/XInput2: Fix keys being stuck pressed on focus loss. 2020-10-19 11:46:01 -05:00
Jordan Woyak
f015c99a51 ControllerInterface: Add platform consistent names for modifier keys. 2020-09-25 20:29:18 -05:00
Jun Su
997cfa49fc InputCommon: cleanup warnings of -Wclass-memaccess
Initialize m_state with the default constructor.
2020-03-23 14:26:36 +08:00
Jordan Woyak
da12f3eebc InputCommon: Constify Device::Input::IsDetectable function. 2020-02-22 10:27:43 -06:00
Jordan Woyak
b92f6480a0 InputCommon: Make "Cursor" inputs aware of the rendered aspect ratio. 2020-01-24 09:20:41 -06:00
Lioncash
e8edc49bbe InputCommon: Make use of fmt where applicable
Continues the migration over to fmt
2019-11-22 14:38:26 -05:00
Lioncash
ec60027f56 InputCommon: Use nested namespace specifiers where applicable 2019-06-17 16:51:41 -04:00
Techjar
ff972e3673 Reformat repo to clang-format 7.0 rules 2019-05-06 18:48:04 +00:00
Techjar
311d0442de InputCommon/XInput2: Increase mouse buttons to 32
Xlib supports many mouse buttons, though there are 9 standard buttons, and they aren't arranged like other mouse APIs. Using only 5 buttons was preventing the use of buttons besides left/right/middle click and the scroll wheel. Here's what all the standard buttons are:
1. left button
2. middle button (pressing the scroll wheel)
3. right button
4. turn scroll wheel up
5. turn scroll wheel down
6. push scroll wheel left
7. push scroll wheel right
8. 4th button (aka browser backward button)
9. 5th button (aka browser forward button)

The remaining button indices are non-standard and device-specific, and technically far more than 32 are supported, but this seems like a reasonable limit to avoid cluttering the list with tons of useless mouse buttons. What mouse has more than 32 buttons anyways?
2018-08-27 08:47:32 -04:00
Greg V
9b201815f2 Ignore -Wregister to calm down recent clang and GCC (C++17 mode)
The 'register' keyword is used by a header included from Xlib (X11/XKBlib.h).
2018-02-05 14:43:07 +03:00
Michael Maltese
3e69d066f5 ControllerInterface: replace Reinitialize with RefreshDevices
The SDL backend crashes when you close a joystick after SDL_Quit has
been called. Some backends don't need to be shutdown and
re-initialized everytime, we can just ask to enumerate devices again.
2016-11-30 16:07:55 -08:00
Jasper St. Pierre
928d05ec47 InputCommon: Remove the Xlib backend
The XInput2 backend is more performant, so let's default to it and
remove the old, core-only backend.
2016-09-04 10:44:22 -07:00
Jasper St. Pierre
2b640a4f7d XInput2: Only do a round trip when the mouse has moved
This should cut down on round trips and blocking calls dramatically
during gameplay.
2016-09-04 10:44:22 -07:00
Léo Lam
788e19f54d ControllerInterface: Make the ID assigning code common
This makes the device ID assigning code common to all backends, by
moving it to AddDevice() instead of copy-pasting or replicating
the logic in the backends.

Also, to prepare for hotplugging, instead of relying on a name usage
count, the new ID assigning system always starts from ID 0 and tries
to assign the first ID that is not used.
2016-07-14 10:50:53 +02:00
Léo Lam
8678133e87 ControllerInterface: Switch to std::shared_ptr
Small cleanup by using std::shared_ptr and getting rid of
ciface.Devices() which just returned the m_devices (which defeats the
point of making m_devices protected).

Incidentally, this should make the code safer when we have
different threads accessing devices in the future (for hotplug?).

A lot of code use Device references directly so there is
no easy way to remove FindDevice() and make those unique_ptrs.
2016-06-25 21:46:39 +02:00
Léo Lam
fd29e5c4cc ControllerInterface: Don't pass m_devices to the backends
Previously, the devices vector would be passed to all backends. They
would then manually push_back to it to add new devices. This was fine
but caused issues when trying to add synchronisation.

Instead, backends now call AddDevice() to fill m_devices so that it is
not accessible from the outside.
2016-06-25 13:46:53 +02:00
Pierre Bourdon
3570c7f03a Reformat all the things. Have fun with merge conflicts. 2016-06-24 10:43:46 +02:00
catzilla4
867a1beb9f Added some headers to allow Dolphin to compile in my environment. 2015-10-06 19:09:57 -05:00
Tillmann Karras
268f52e054 Add missing license headers 2015-05-25 13:11:47 +02:00
Tillmann Karras
6d9986846c Simplify some more license headers 2015-05-25 13:11:41 +02:00
Jasper St. Pierre
f2787f620e ControllerInterface: Make UpdateInput / UpdateOutput return void
The return values here have never been checked, so it doesn't make sense
to return a value to begin with.
2014-11-28 10:50:45 -08:00
Jasper St. Pierre
367a42dcfd ControllerInterface: Implement dummy UpdateInput / UpdateOutputs
Make the implementation here a bit easier.
2014-11-28 10:50:45 -08:00
comex
7f6284c2fc Change a bunch of reference function arguments to pointers.
Per the coding style and sanity.
2014-10-02 03:00:33 -04:00
Rohit Nirmal
46057db37d Fix build failing when disabling precompiled headers. 2014-09-19 18:17:51 -04:00
Tillmann Karras
d802d39281 clang-modernize -use-nullptr
and s/\bNULL\b/nullptr/g for *.cpp/h/mm files not compiled on my machine
2014-03-09 21:14:26 +01:00
Tillmann Karras
f28116b7da clang-modernize -add-override 2014-03-09 21:12:01 +01:00
Pierre Bourdon
6847a0fc0c Fix more header sorting issues in InputCommon/ (now check-includes clean). 2014-02-20 01:01:10 +01:00
Lioncash
2afe215271 Convert all includes to relative paths. 2014-02-18 02:19:10 -05:00
Lioncash
6c4ee1753a Fix some vertical alignments
ie. uses spaces for alignment.
2014-02-16 20:12:05 -05:00
lioncash
d2038049f5 Replace all include guard ifdefs with "#pragma once" 2014-02-10 18:07:16 -05:00
Jasper St. Pierre
34692ab826 Remove unnecessary Src/ folders 2013-12-31 14:03:19 -05:00