Commit Graph

442 Commits

Author SHA1 Message Date
JosJuice 1d77bddc3b ControllerInterface/Android: Add null check to AddDevice
The Google Play Console is showing some users getting a crash here,
and indeed, InputDevice.getDevice can return null.
2023-08-15 13:31:18 +02:00
get ffabb6c57b Replace std::ostringstream usage with fmt::format 2023-06-18 18:37:32 -05:00
JosJuice 71ac6ec239 InputCommon: Don't treat two analog inputs as a spurious trigger combo
I've received a report from an Android user with a gamepad (a "BSP-D3")
where one physical trigger is controlling two analog axes at the same
time. This was causing RemoveSpuriousTriggerCombinations to delete both
axes, which is clearly not a desireable outcome.

With this change, now the axis with the greatest smoothness is kept,
or both in case they have the same smoothness.
2023-06-06 22:07:14 +02:00
Admiral H. Curtiss 38b033a476
Merge pull request #11762 from jbosboom/xinput2-raw-event-query
Xinput2: use raw events and queries
2023-06-06 18:14:51 +02:00
Admiral H. Curtiss da77084d2f
Merge pull request #11819 from JosJuice/android-input-device-null
ControllerInterface/Android: Use InputEvent.getDeviceId
2023-05-29 19:27:30 +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
Admiral H. Curtiss 50fe493990
Merge pull request #11739 from OatmealDome/iokit-be-gone
InputCommon: Remove OSX (IOKit) input backend
2023-05-19 19:31:40 +02:00
Admiral H. Curtiss 279fcafdc3
Merge pull request #11758 from jbosboom/xinput2-client-pointer
XInput2: Accept input from keyboards other than the first master
2023-05-19 19:29:24 +02:00
Lioncash a9f1edeb61 StringUtil: Move IsAlpha() into Common namespace 2023-05-16 14:11:02 -04:00
R c5d2ad99d2 Support Steam Deck controls directly through hidraw
This reads Steam Deck controls bypassing Steam Input. This allows for access to
motion controls as well as independent access to thumb sticks, trackpads, and
back grip buttons.
2023-05-10 13:28:32 +01:00
JosJuice 82f70cdf94 ControllerInterface/Android: Add some missing DeleteLocalRef calls 2023-05-07 18:44:43 +02:00
JosJuice 24a697514e ControllerInterface/Android: Use InputEvent.getDeviceId
Instead of InputEvent.getDevice followed by InputDevice.getId.
Should hopefully fix https://bugs.dolphin-emu.org/issues/13237,
while also being simpler.
2023-05-07 18:40:28 +02: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
OatmealDome 72d08f6c80 InputCommon: Remove OSX (IOKit) input backend 2023-04-07 15:40:14 -04:00
Pierre Bourdon 3783bedc25
Merge pull request #11635 from Dentomologist/wiimote_fix_disconnection_deadlock
ControllerInterface: Fix deadlock when Wii Remote disconnects
2023-03-14 02:41:36 +01:00
Dentomologist d6a8e6caaf ControllerInterface: Fix deadlock when Wii Remote disconnects
In UpdateInput, lock m_devices_population_mutex before m_devices_mutex
to be consistent with other ControllerInterface functions. Normally the
former lock isn't needed in UpdateInput, but when a Wii Remote
disconnects it calls RemoveDevice which results in the mutexes being
locked in the wrong order.
2023-03-08 17:22:27 -08:00
JosJuice c2779aef06 Android: Add the advanced input mapping dialog
It's missing a lot of features from the PC version for now, like
buttons for inserting functions and the ability to see what the
expression evaluates to. I mostly just wanted to get something in
place so you can set up rumble.

Co-authored-by: Charles Lombardo <clombardo169@gmail.com>
2023-03-07 17:39:30 +01:00
JosJuice 2b1dd52750 Android: Add input device selection 2023-03-03 22:28:24 +01:00
JosJuice 8e33458f48 ControllerInterface/Android: Implement rumble 2023-03-03 22:28:23 +01:00
JosJuice 065481d989 ControllerInterface/Android: Automatically suspend sensors
This is a battery-saving measure. Whether a sensor should be suspended
is determined in the same way as whether key events and motion events
should be handled by the OS rather than consumed by Dolphin.
2023-03-03 22:28:23 +01:00
JosJuice 36acb17700 ControllerInterface/Android: Implement sensor input for InputDevices
This functionality was added in Android 12 to let apps get motion data
for gamepads.
2023-03-03 22:28:23 +01:00
JosJuice 5e51b56d72 ControllerInterface/Android: Implement sensor input 2023-03-03 22:28:23 +01:00
JosJuice 104ea09892 ControllerInterface/Android: Implement hotplug 2023-03-03 22:28:23 +01:00
JosJuice d6af294a23 ControllerInterface/Android: Return whether input was handled
When Android presents an input event to an app, it wants the app to
return true or false depending on whether the app handled the event or
not. If the event wasn't handled by the app, it will be passed on to
the system, which may decide to take an action depending on what kind
of input event it is. For instance, if a B button press is passed on to
the system, it will be turned into a Back press. But if an R1 press is
passed on to the system, nothing in particular happens.

It's important that we get this return value right in Dolphin. For
instance, the user generally wouldn't want a B button press to open
the EmulationActivity menu, so B button presses usually shouldn't be
passed on to the system - but volume button presses usually should be
passed on to the system, since it would be hard to adjust the volume
otherwise. What ButtonManager did was to pass on input events that are
for a button which the user has not mapped, which I think makes sense.
But exactly how to implement that is more complicated in the new input
backend than in ButtonManager, because now we have a separation between
the input backend and the code that keeps track of the user's mappings.

What I'm going with in this commit is to treat an input as mapped if
it has been polled recently. In part I chose this because it seemed
like a simple way of implementing it that wouldn't cause too many
layering violations, but it also has two useful side effects:

1. If a controller is not being polled (e.g. GameCube controllers in
   Wii games that don't use them), its mappings will not be considered.
2. Once sensor input is implemented in the Android input backend,
   we will be able to use this "polled recently" tracking to power down
   the sensors at times when the game is using a Wii Remote reporting
   mode that doesn't include motion data. (Assuming that the sensor
   inputs only are mapped to Wii Remote motion controls, that is.)
2023-03-03 22:28:23 +01:00
JosJuice ca508e4503 ControllerInterface/Android: Handle input events
Android doesn't let us poll inputs whenever we want. Instead, we
listen to input events (activities will have to forward them to the
input backend), and store the received values in atomic variables
in the Input classes. This is similar in concept to how ButtonManager
worked, but without its homegrown second input mapping system.
2023-03-03 22:28:23 +01:00
JosJuice 792cb62195 ControllerInterface/Android: Implement device population 2023-03-03 22:28:23 +01:00
JosJuice 0150f521f7 ControllerInterface/Android: Rip out ButtonManager
ButtonManager is very different from how a normal input backend works,
and is making it hard for us to improve controller support on Android.
The following commits will add a new input backend in its place.
2023-03-03 22:28:23 +01:00
Pokechu22 74a14c7d1f ControllerInterface: Fix uninitialized variables in DualShockUDPClient
Strangely, this case did not trigger a C26495 warning in Visual Studio's analyzer; instead, I spotted this when using Valgrind.
2023-02-15 19:23:47 -08:00
Pokechu22 7fafb00561 InputCommon/XInput2: Fix shadowing warning 2023-02-09 16:23:02 -08:00
Lioncash e5b91f00b0 Common: Replace StringBeginsWith/StringEndsWith with std equivalents
Obsoletes these functions in favor of the standard member functions
added in C++20.
2023-01-24 14:58:20 -05:00
Admiral H. Curtiss d949aa60bb
Merge pull request #11397 from TellowKrinkle/QuartzMousePos
InputCommon:QuartzKB&M: Fix mouse y coordinates
2023-01-02 23:04:12 +01:00
TellowKrinkle 8659de4d71 InputCommon:QuartzKB&M: Fix mouse y coordinates
Cocoa uses a different coordinate system from Carbon (Carbon's origin is the top left while Cocoa's is the bottom left)
2022-12-31 23:42:11 -06: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
TellowKrinkle 72eadc6520 InputCommon:SDL: Add SDL 2.26 left and right motion inputs 2022-11-24 15:12:07 -06:00
Jordan Woyak 168a49c87f ControllerInterface: DSU InputBackend implementation. 2022-11-01 21:59:09 -05:00
Jordan Woyak 2e5cd5d519 ControllerInterface: evdev InputBackend implementation. 2022-11-01 21:59:08 -05:00
Jordan Woyak 44a4573303 ControllerInterface: Add InputBackend interface and SDL implementation. 2022-11-01 21:59:08 -05:00
JMC47 bc4d08047d
Merge pull request #10979 from tellowkrinkle/QuartzWindowSpeed
InputCommon:QuartzKB&M: Use KVO to watch window position
2022-10-22 04:55:12 -04:00
JosJuice 51ee05cb35 Android: Use input override system for touch controls
This is the first step of getting rid of the controller indirection
on Android. (Needing a way for touch controls to provide input
to the emulator core is the reason why the controller indirection
exists to begin with as far as I understand it.)
2022-10-03 22:04:09 +02:00
Admiral H. Curtiss 26fd4ea361
WiimoteEmu: Update IR camera status from DesiredWiimoteState. 2022-10-02 23:14:35 +02:00
Admiral H. Curtiss 710210365a
Merge pull request #11038 from AdmiralCurtiss/steam-overlay-crash-fix-wgi
WGInput: Work around crash with Steam overlay.
2022-09-08 19:03:31 +02:00
Admiral H. Curtiss b8ca1b2907
InputCommon: Fix deadlock in EmulatedController::UpdateReferences(). 2022-09-07 22:37:24 +02:00
Admiral H. Curtiss 67c97a0bf2
WGInput: Work around crash with Steam overlay. 2022-09-07 21:06:26 +02:00
Shawn Hoffman cd407abe34 WGInput: implement error handling 2022-08-27 02:11:27 -07:00