Commit Graph

66 Commits

Author SHA1 Message Date
Sam Lantinga
cb36189692 Fixed bug 5235 - All internal sources should include SDL_assert.h
Ryan C. Gordon

We should really stick this in SDL_internal.h or something so it's always available.
2020-12-09 07:16:22 -08:00
Sam Lantinga
59f28b7f4b Fixed whitespace 2020-12-03 18:17:01 -08:00
Sam Lantinga
7a05dbf4b9 Fixed building on FreeBSD
Alex S

Looks like we have a collision with https://hg.libsdl.org/SDL/rev/cd774daff9f6. (Again, the headers in the base system are intended for drivers and should not be used for compiling non-base applications. At least that's the policy for now: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=240964#c19.)
2020-11-24 06:42:53 -08:00
Sam Lantinga
c8c818d7a4 Fixed bug 5360 - non-libudev joystick detection doesn't see controllers that were already connected
Simon McVittie

When watching for hotplug events we can poll the inotify fd, but we
still need to scan /dev/input once per process, otherwise we'll fail
to detect devices that were already connected.
2020-11-23 21:14:37 -08:00
Simon McVittie
8e2746cfb6 joystick: Don't use udev in Flatpak or pressure-vessel container
Flatpak[1] and pressure-vessel[2] are known to use user namespaces,
therefore udev event notification via netlink won't work reliably.
Both frameworks provide a filesystem API that libraries can use to
detect them. Do that, and automatically fall back from udev-based
device discovery to the inotify-based fallback introduced in Bug #5337.

[1] <https://flatpak.org/>
[2] <https://gitlab.steamos.cloud/steamrt/steam-runtime-tools/-/tree/master/pressure-vessel>

Signed-off-by: Simon McVittie <smcv@collabora.com>
2020-11-23 21:10:48 -08:00
Sam Lantinga
e9869e07bf Fixed bug 5335 - enable joystick/haptic/evdev support by default on FreeBSD
Alex S

Evdev headers aren't actually included in the base system (well, it has a private copy), they are available through the devel/evdev-proto port instead. We also have devel/libinotify and devel/libudev-devd shims, I didn't verify whether they work with SDL.
2020-11-23 21:08:19 -08:00
Ryan C. Gordon
eaa53a1979 joystick: On Linux, don't try to close an invalid inotify file descriptor. 2020-11-23 22:16:07 -05:00
Ryan C. Gordon
5c9577476f joystick: Fix up Linux joystick code to (mostly) compile on FreeBSD. 2020-11-23 22:14:22 -05:00
Sam Lantinga
fcb21aa883 Added API for sensors on game controllers
Added support for the PS4 controller gyro and accelerometer on iOS and HIDAPI drivers

Also fixed an issue with the accelerometer on iOS having inverted axes
2020-11-17 10:30:20 -08:00
Ozkan Sezer
c122e9b9ce linux/SDL_sysjoystick.c (MaybeRemoveDevice): remove SDL_USE_LIBUDEV guards
fixes bug #5349.
2020-11-12 14:11:50 +03:00
Simon McVittie
b0eba1c55d joystick: Use inotify to detect joystick unplug if not using udev
This improves SDL's ability to detect joystick hotplug in a container
environment.

We cannot reliably receive events from udev in a container, because they
are delivered as netlink events, which are authenticated by their uid
being 0. However, in a user namespace created by an unprivileged user
(for example bubblewrap, as used by Flatpak and Steam's
pressure-vessel-wrap), the kernel does not allow us to map uid 0, and
the netlink events appear to be from the kernel's overflowuid (typically
65534/nobody), meaning libudev cannot distinguish between genuine uevents
from udevd and an attack by a malicious local user.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2020-11-11 19:15:32 -08:00
Simon McVittie
fdd945f2af joystick: Use a better heuristic to guess what is a joystick
Previously we only checked for at least one button or key and at least
the X and Y absolute axes, but this has both false positives and false
negatives.

Graphics tablets, trackpads and touchscreens all have buttons and
absolute X and Y axes, but we don't want to detect those as joysticks.
On normal Linux systems ordinary users do not have access to these
device nodes, but members of the 'input' group do.

Conversely, some game controllers only have digital buttons and no
analogue axes (the Nintendo Wiimote is an example), and some have axes
and no buttons (steering wheels or flight simulator rudders might not
have buttons).

Use the more elaborate heuristic factored out from SDL's udev code path
to handle these cases.

In an ideal world we could use exactly the same heuristic as udev's
input_id builtin, but that isn't under a suitable license for inclusion
in SDL, so we have to use a parallel implementation of something
vaguely similar.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2020-11-11 19:14:52 -08:00
Simon McVittie
8db3171b98 udev: Factor out SDL_EVDEV_GuessDeviceClass
This works on capability bitfields that can either come from udev or
from ioctls, so it is equally applicable to both udev and non-udev
input device detection.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2020-11-11 19:14:34 -08:00
Simon McVittie
13e7d1a958 joystick: Allow libudev to be disabled at runtime
Device enumeration via libudev can fail in a container for two reasons:

* the netlink protocol between udevd and libudev is considered private,
  so there is no API guarantee that the version of libudev in a container
  will understand netlink messages from a dissimilar version of udevd
  on the host system;
* the netlink protocol between udevd and libudev relies for security on
  being able to check the uid of each message, but in a container with
  a user namespace where host uid 0 is not mapped, the libudev client
  cannot distinguish between messages from host uid 0 and messages from
  a different, malicious user on the host

To make this easier to experiment with, always compile the fallback
code path even if libudev is disabled. libudev remains the default if
enabled at compile time, but the fallback code path can be forced.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2020-11-11 19:14:11 -08:00
Sam Lantinga
1e2caac58b Added SDL_JoystickRumbleTriggers() and SDL_GameControllerRumbleTriggers() 2020-11-11 18:57:37 -08:00
Sam Lantinga
e555d45331 Added SDL_JoystickHasLED
Currently, this is only supported by the PS4 HIDAPI driver.
2020-11-05 11:07:54 -08:00
Ethan Lee
83cddd2ebc Add SDL_JoystickSetLED.
Currently, this is only supported by the PS4 HIDAPI driver.
2020-04-30 11:57:29 -04:00
Kai Krakow
c3ecf18cc4 Linux: Add hint for disabling deadzones 2020-07-21 23:38:42 +02:00
Ryan C. Gordon
0e98040d43 joystick: Linux joysticks now recover better from dropped events.
Fixes Bugzilla #4830.
2020-06-28 16:23:05 -04:00
Sam Lantinga
9fa8d6d0be Define constants not available on older kernels 2020-06-08 17:07:55 -07:00
Sam Lantinga
ae9ff11bae The zero happens at a higher level now 2020-05-29 14:54:07 -07:00
Sam Lantinga
345b4d7e14 Fixed bug 5161 - Autodetect controller mappings based on the Linux Gamepad Specification
Jan Bujak

I wrote a new driver for my gamepad on Linux. I'd like SDL to support it out-of-box, as currently it just treats it as a generic joystick instead of a gamepad. From what I can see the only way to do that is to either 1) pick one of the already supported controllers' PID, VID and button layouts and have my driver send that (effectively lying that it's something else), or 2) submit a preconfigured, hardcoded mapping to SDL.

Both of those, in my opinion, are silly when we already have the Linux Gamepad Specification which standarizes this:

https://www.kernel.org/doc/html/v4.15/input/gamepad.html

Unfortunately SDL doesn't make use of it currently. So I've took it upon myself to add it; patch is in the attachments.

Basically what the patch does is that if SDL finds no built-it controller mappings for a given joystick it then asks the joystick backend to autodetect it, and that uses the relevant evdev bits to figure out which button/axis is which. (See the specs for more details.)

With this patch applied my own driver for my controller works out-of-box with SDL with no extra configuration and is correctly recognized as a gamepad; this is also going to be the case for any other driver which follows the Linux Gamepad Specification.
2020-05-29 13:37:21 -07:00
Sam Lantinga
885b3f6948 Don't check the HIDAPI driver for the virtual device created by xow 2020-03-23 14:10:25 -07:00
Sam Lantinga
c44473ba73 Unified code to standardize joystick names 2020-03-12 19:47:30 -07:00
Sam Lantinga
6efebf1768 Moved rumble expiration to the main joystick handling level, and prevent sending the driver layer duplicate rumble requests. 2020-02-04 12:48:53 -08:00
Sjoerd Simons
976eee77cc Correct joystick hat indexes on linux
The index and indices were swapped; Which is fine as long as there are
no gaps in the ABS_HAT* event availability but otherwise things do get confused.

Signed-off-by: Sjoerd Simons <sjoerd.simons@collabora.co.uk>
2020-02-04 07:23:44 -08:00
Sam Lantinga
2ae41b9c61 Fixed mapping for both versions of the Xbox One Elite Series 2 controller firmware connecting over Bluetooth 2020-01-23 12:53:43 -08:00
Sam Lantinga
6dc172d093 Turn off rumble on drivers which don't respect the replay.length value 2020-01-23 12:53:39 -08:00
Sam Lantinga
a8780c6a28 Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
Sam Lantinga
c14a59d999 Worked around an issue where the kernel would lose the force feedback effect 2020-01-11 13:38:50 -08:00
Sam Lantinga
46e1377d49 Automatically assign player indexes to game controllers, and allow changing the player index for game controllers and joysticks.
Added the functions SDL_JoystickFromPlayerIndex(), SDL_JoystickSetPlayerIndex(), SDL_GameControllerFromPlayerIndex(), and SDL_GameControllerSetPlayerIndex()
2019-12-20 20:12:03 -08:00
Sam Lantinga
0f52916027 Added custom names for some controllers 2019-12-11 17:47:01 -08:00
Sam Lantinga
2a161e7add Remove any duplicate manufacturer in the joystick name 2019-12-10 11:30:56 -08:00
Cameron Gutman
55eb76218d Use stat() to minimize input device opens when not using udev
Calling open() on input devices can generate device I/O which blocks
the main thread and causes dropped frames. Using stat() we can avoid
opening anything unless /dev/input has changed since we last polled.

We could have used something fancy like inotify, but it didn't seem
worth the added complexity for this uncommon non-udev case.
2019-11-20 20:27:45 -08:00
Sam Lantinga
9da4bfc141 Added support for the Power A Nintendo Switch Enhanced Wireless Controller 2019-10-22 10:57:07 -07:00
Sam Lantinga
c10a87429d Make sure HIDAPI is initialized whenever we call HIDAPI_IsDevicePresent() 2019-07-31 10:20:37 -07:00
Andrew Eikum
c172f36bf4 joystick: Ensure HIDAPI is initialized before calling it 2019-07-31 11:14:48 -05:00
Cameron Gutman
a4bfe2a4ae Allow hotplugging joysticks without udev 2019-06-24 21:08:26 -07:00
Sam Lantinga
747df96eb0 Better patch to make it more clear what's going on 2019-06-12 10:38:49 -07:00
Sam Lantinga
3fbaa5da05 The hat index passed to the application should be zero-based with no holes 2019-06-12 10:35:47 -07:00
Sam Lantinga
a1a2f9b9f8 Fixed bug 4486 - Segfault when pressing a trigger on the Steam Controller (Linux)
Matteo Beniamino

Pressing a trigger button on a Steam Controller causes a segmentation fault both with stable version and latest mercurial head on Linux. I'm using the recent hid_steam kernel module with lizard_mode disabled (that is no keyboard/mouse emulation). I suspect this is what's happening: the driver exposes two hats. The two hats have indices 0 and 2. Inside linux/SDL_sysjoystick.c two hats are allocated in allocate_hatdata for joystick->hwdata->hats. In HandleHat function the hat parameter (that can be 2) is directly used as the index of the array that only has two elements, causing an out of bounds access. SDL is not expecting to have "holes" between hats indices.

The index 2 is calculated in HandleInputEvents() as (ABS_HAT2X - ABS_HAT0X) / 2 where ABS_HAT2X is the value associated to the hat inside the hid_steam module.
2019-06-12 10:32:36 -07:00
Sam Lantinga
56b7f4cf31 Fixed bug 4583 - PollAllValues appears to use an incorrect index for all axes above 0x18
Noam Preil

In src/joystick/linux/SDL_sysjoystick.c:

The ConfigJoystick function's axes detection starts with a for loop using an index i for Linux's axes names. When i gets to ABS_HAT0X, it's set to ABS_HAT3Y and a continue statement appears, to skip the hats. This makes sense, as SDL handles hats separately from axes.

However, in PollAllValues, *two* indices are used: a and b. Both start out the same, and remain so until the hats are reached. At that point, a becomes identical to the i from ConfigJoystick's loop, but b is equal to a - (ABS_HAT3Y - ABS_HAT0X), or a - 8.

While all the joystick->hwdata->abs_* structures in ConfigJoystick used i - which would here be a - as both the index and the ioctl argument, PollAllValues uses b for the structure index and a as the ioctl argument.

It would appear, however, that no joystick HAS such axes, and that the b index is entirely unnecessary.

I tested three separate joysticks, and while that was far from a complete listing, I was unable to find a joystick with an axis above 0x08.
2019-06-08 14:40:27 -07:00
Sam Lantinga
5e13087b0f Updated copyright for 2019 2019-01-04 22:01:14 -08:00
Ryan C. Gordon
a7563bcd3d joystick: Removed unused variable. 2018-12-05 19:03:15 -05:00
Sam Lantinga
6ed76ae18f Fixed the ROCCAT Tyon mouse showing up as a joystick on Windows 2018-12-05 14:46:03 -08:00
Sam Lantinga
2e348c1f59 Fixed bug 3193 - Dualshock 3's motion sensors overwrite analog stick
maxxus

The Dualshock 3's motion sensors don't seem to be reported by the call to EVIOCGBIT but they still send EV_ABS events.  Because they're not reported by EVIOCGBIT they're not assigned a proper axis ids and the default of 0 is used, which is the valid id for the left analog sticks left/right axis.
2018-11-14 13:37:22 -08:00
Micha? Janiszewski
91820998fc Add and update include guards
Include guards in most changed files were missing, I added them keeping
the same style as other SDL files. In some cases I moved the include
guards around to be the first thing the header has to take advantage of
any possible improvements compiler may have for inclusion guards.
2018-10-28 21:36:48 +01:00
Sam Lantinga
14329256cb Generalized the XInput user index into a player index 2018-10-25 16:53:14 -07:00
Sam Lantinga
708ad1fd8d Fixed updating the rumble parameters on Linux 2018-10-16 14:58:07 -07:00
Sam Lantinga
63107524f6 Fixed input from the Steam Virtual Gamepad on Mac OS X 2018-08-15 19:53:34 -07:00