Commit Graph

66 Commits

Author SHA1 Message Date
Simon McVittie 76a7b629bf test: Add some common code to load test resources
As well as reducing duplication, this lets the tests load their resources
from the SDL_GetBasePath() on platforms that support it, which is useful
if the tests are compiled along with the rest of SDL and installed below
/usr as manual tests, similar to GNOME's installed-tests convention.

Thanks to Ozkan Sezer for the OS/2 build glue.

Co-authored-by: Ozkan Sezer <sezeroz@gmail.com>
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-05-04 18:17:55 -07:00
Sam Lantinga 0687128c96 Reduce CPU usage in testgamecontroller 2022-04-29 20:57:17 -07:00
Sam Lantinga e551384a99 Added functions to get the platform dependent name for a joystick or game controller 2022-04-26 14:54:14 -07:00
Sam Lantinga 18032979d8 Added the hint SDL_HINT_JOYSTICK_ROG_CHAKRAM to control whether ROG Chakram mice show up as joysticks
This hint defaults off, but when it is enabled the mice will have a game controller mapping set up for Profile 3 (set in the Armoury Crate software)
2022-02-15 13:07:51 -08:00
Sam Lantinga 120c76c84b Updated copyright for 2022 2022-01-03 09:40:21 -08:00
Nate River 6c25aed419
added hint for joysticks to stablize sixaxis & dualshock controllers (#5007) 2021-11-27 11:01:18 -08:00
Ozkan Sezer 3eaa80bb0b testgamecontroller.c: fixed -Wshadow warnings. 2021-11-23 14:22:02 +03:00
Misa 3bf7994fe2 Add and use `SDL_FALLTHROUGH` for fallthroughs
Case fallthrough warnings can be suppressed using the __fallthrough__
compiler attribute. Unfortunately, not all compilers have this
attribute, or even have __has_attribute to check if they have the
__fallthrough__ attribute. [[fallthrough]] is also available in C++17
and the next C2x, but not everyone uses C++17 or C2x.

So define the SDL_FALLTHROUGH macro to deal with those problems - if we
are using C++17 or C2x, it expands to [[fallthrough]]; else if the
compiler has __has_attribute and has the __fallthrough__ attribute, then
it expands to __attribute__((__fallthrough__)); else it expands to an
empty statement, with a /* fallthrough */ comment (it's a do {} while
(0) statement, because users of this macro need to use a semicolon,
because [[fallthrough]] and __attribute__((__fallthrough__)) require a
semicolon).

Clang before Clang 10 and GCC before GCC 7 have problems with using
__attribute__ as a sole statement and warn about a "declaration not
declaring anything", so fall back to using the /* fallthrough */ comment
if we are using those older compiler versions.

Applications using SDL are also free to use this macro (because it is
defined in begin_code.h).

All existing /* fallthrough */ comments have been replaced with this
macro. Some of them were unnecessary because they were the last case in
a switch; using SDL_FALLTHROUGH in those cases would result in a compile
error on compilers that support __fallthrough__, for having a
__attribute__((__fallthrough__)) statement that didn't immediately
precede a case label.
2021-11-12 07:26:14 +03:00
Sam Lantinga abc12a832c Revert "Add and use `SDL_FALLTHROUGH` for fallthroughs"
This reverts commit 66a08aa391.

This causes problems with older compilers:
https://github.com/libsdl-org/SDL/pull/4791#issuecomment-966630997
2021-11-11 15:58:44 -08:00
Cameron Gutman fe09a4930a joystick: Add APIs to query rumble support 2021-11-11 15:10:08 -08:00
Misa 66a08aa391 Add and use `SDL_FALLTHROUGH` for fallthroughs
Case fallthrough warnings can be suppressed using the __fallthrough__
compiler attribute. Unfortunately, not all compilers have this
attribute, or even have __has_attribute to check if they have the
__fallthrough__ attribute. [[fallthrough]] is also available in C++17
and the next C2x, but not everyone uses C++17 or C2x.

So define the SDL_FALLTHROUGH macro to deal with those problems - if we
are using C++17 or C2x, it expands to [[fallthrough]]; else if the
compiler has __has_attribute and has the __fallthrough__ attribute, then
it expands to __attribute__((__fallthrough__)); else it expands to an
empty statement, with a /* fallthrough */ comment (it's a do {} while
(0) statement, because users of this macro need to use a semicolon,
because [[fallthrough]] and __attribute__((__fallthrough__)) require a
semicolon).

Applications using SDL are also free to use this macro (because it is
defined in begin_code.h).

All existing /* fallthrough */ comments have been replaced with this
macro. Some of them were unnecessary because they were the last case in
a switch; using SDL_FALLTHROUGH in those cases would result in a compile
error on compilers that support __fallthrough__, for having a
__attribute__((__fallthrough__)) statement that didn't immediately
precede a case label.
2021-11-11 07:23:25 -08:00
Sam Lantinga 9fdb06450d Added support for the Joy-Con Charging Grip when SDL_HINT_JOYSTICK_HIDAPI_JOY_CONS is enabled 2021-08-04 11:34:06 -07:00
Sam Lantinga 65ff00ec1b Query the rate for the correct sensor (thanks @meyraud705) 2021-07-29 14:25:13 -07:00
Sam Lantinga a186a503e7 Added SDL_GameControllerGetSensorDataRate() to get the sensor update rate for a controller. 2021-07-29 06:43:39 -07:00
Sam Lantinga ce8261dd6d Only pump events once per frame and process all currently pending events
If you continually poll for events it's possible that new events can come in while you're still processing the last one, delaying rendering. This is more likely with high update rate sensors.
2021-07-29 06:36:20 -07:00
Sam Lantinga 94b7a87645 Added SDL_GameControllerType enumerations for the Amazon Luna and Google Stadia controllers
Fixes bug https://github.com/libsdl-org/SDL/issues/4019
2021-07-24 09:10:18 -07:00
Sam Lantinga d135c0762f Added SDL_GameControllerSendEffect() and SDL_JoystickSendEffect() to allow applications to send custom effects to the PS4 and PS5 controllers
See testgamecontroller.c for an example of a custom PS5 trigger effect
2021-07-08 13:22:41 -07:00
Ozkan Sezer 646ddfb782 minor watcom build fixes. 2021-05-15 00:22:50 +03:00
Sam Lantinga c94283d404 testgamecontroller: log which controller the event came from 2021-01-13 11:01:54 -08:00
Sam Lantinga 9130f7c377 Updated copyright for 2021 2021-01-02 10:25:38 -08:00
Sam Lantinga 390b2cf0d3 Enable PS5 enhanced functionality for testgamecontroller 2020-12-22 20:53:27 -08:00
Sam Lantinga 058a0ab47f Set the pad lights on the PS5 controller corresponding to the player index
Also allow setting the player index from testgamecontroller using the number keys
2020-12-22 14:38:32 -08:00
Sam Lantinga 3c68051eaf Allow background input when testing game controllers 2020-12-04 15:47:28 -08:00
Sam Lantinga 04f0fd13b5 Remember to close the game controller when we're done with it 2020-11-27 18:57:42 -08:00
Sam Lantinga 012471e959 Open and test all connected controllers 2020-11-27 18:57:38 -08:00
Sam Lantinga 24cc0012f0 Some controllers don't always reset their triggers to zero when they are released (e.g. Xbox One S in Bluetooth mode), so only trigger rumble if the trigger is pulled halfway or more. 2020-11-25 16:05:15 -08:00
Sam Lantinga e16afa792f Automatically switch to testing a new controller when it's plugged in 2020-11-24 16:38:49 -08:00
Sam Lantinga 04b45b5581 Allow testing a specific controller 2020-11-23 14:28:30 -08:00
Sam Lantinga 75721b191c Allow testing effects on PS4 controllers
Note, this will switch the controller into advanced report mode which breaks DirectInput on Windows
2020-11-21 18:01:23 -08: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
Sam Lantinga d140d88744 Added SDL_JoystickGetSerial() and SDL_GameControllerGetSerial() 2020-11-16 17:36:47 -08:00
Sam Lantinga fe4da4206a Improve LED color calculation, don't set LED unless left thumbstick is moved 2020-11-16 10:39:44 -08:00
Sam Lantinga 9f51fad361 Added support for the touchpad on PS4 and PS5 controllers 2020-11-13 18:01:29 -08:00
Sam Lantinga e749c1266f Fixed overflow in trigger rumble calculation 2020-11-12 07:53:01 -08:00
Sam Lantinga 1e2caac58b Added SDL_JoystickRumbleTriggers() and SDL_GameControllerRumbleTriggers() 2020-11-11 18:57:37 -08:00
Sam Lantinga 009b62f1c2 Be explicit about mapping the new game controller paddle buttons 2020-11-07 02:22:15 -08:00
Sam Lantinga 5a92edee03 Don't try to map the accelerometer as a game controller 2020-11-06 15:54:18 -08:00
Sam Lantinga 59a644fb01 iOS should use the same size window for the controller tests as other platforms
Otherwise the position of the button and axis elements won't be correct
2020-11-06 14:44:26 -08:00
Sam Lantinga 3a3aaac221 Added 4 auxiliary buttons to the game controller API
Xbox Elite controllers use AUX1-AUX4 to represent the paddle buttons when using the HIDAPI driver
PS4 and PS5 controllers use AUX1 to represent the touchpad button
Nintendo Switch Pro controllers use AUX1 to represent the capture button
2020-11-06 11:30:52 -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
Sam Lantinga aba2792896 Added a Windows Gaming Input joystick driver
This driver supports the Razer Atrox Arcade Stick

Some of the quirks of this driver, inherent in Windows Gaming Input:
* There will never appear to be controllers connected at startup. You must support hot-plugging in order to see these controllers.
* You can't read the state of the guide button
* You can't get controller events in the background
2020-04-18 21:41:37 -07:00
Sam Lantinga 3d942ccc15 Removed debug code 2020-04-08 09:02:02 -07:00
Sam Lantinga 09f552639a Fixed memory leak and removed debug code from Windows sensor implementation 2020-04-08 09:00:10 -07:00
Sam Lantinga 2be75c6a61 Fixed bug 5028 - Virtual Joysticks (new joystick backend)
David Ludwig

I have created a new driver for SDL's Joystick and Game-Controller subsystem: a Virtual driver.  This driver allows one to create a software-based joystick, which to SDL applications will look and react like a real joystick, but whose state can be set programmatically.  A primary use case for this is to help enable developers to add touch-screen joysticks to their apps.

The driver comes with a set of new, public APIs, with functions to attach and detach joysticks, set virtual-joystick state, and to determine if a joystick is a virtual-one.

Use of virtual joysticks goes as such:

1. Attach one or more virtual joysticks by calling SDL_JoystickAttachVirtual.  If successful, this returns the virtual-device's joystick-index.
2. Open the virtual joysticks (using indicies returned by SDL_JoystickAttachVirtual).
3. Call any of the SDL_JoystickSetVirtual* functions when joystick-state changes.  Please note that virtual-joystick state will only get applied on the next call to SDL_JoystickUpdate, or when pumping or polling for SDL events (via SDL_PumpEvents or SDL_PollEvent).


Here is a listing of the new, public APIs, at present and subject to change:

------------------------------------------------------------

/**
 * Attaches a new virtual joystick.
 * Returns the joystick's device index, or -1 if an error occurred.
 */
extern DECLSPEC int SDLCALL SDL_JoystickAttachVirtual(SDL_JoystickType type, int naxes, int nballs, int nbuttons, int nhats);

/**
 * Detaches a virtual joystick
 * Returns 0 on success, or -1 if an error occurred.
 */
extern DECLSPEC int SDLCALL SDL_JoystickDetachVirtual(int device_index);

/**
 * Indicates whether or not a virtual-joystick is at a given device index.
 */
extern DECLSPEC SDL_bool SDLCALL SDL_JoystickIsVirtual(int device_index);

/**
 * Set values on an opened, virtual-joystick's controls.
 * Returns 0 on success, -1 on error.
 */
extern DECLSPEC int SDLCALL SDL_JoystickSetVirtualAxis(SDL_Joystick * joystick, int axis, Sint16 value);
extern DECLSPEC int SDLCALL SDL_JoystickSetVirtualBall(SDL_Joystick * joystick, int ball, Sint16 xrel, Sint16 yrel);
extern DECLSPEC int SDLCALL SDL_JoystickSetVirtualButton(SDL_Joystick * joystick, int button, Uint8 value);
extern DECLSPEC int SDLCALL SDL_JoystickSetVirtualHat(SDL_Joystick * joystick, int hat, Uint8 value);

------------------------------------------------------------

Miscellaneous notes on the initial patch, which are also subject to change:

1. no test code is present in SDL, yet.  This should, perhaps, change.  Initial development was done with an ImGui-based app, which potentially is too thick for use in SDL-official.  If tests are to be added, what kind of tests?  Automated?  Graphical?

2. virtual game controllers can be created by calling SDL_JoystickAttachVirtual with a joystick-type of SDL_JOYSTICK_TYPE_GAME_CONTROLLER, with naxes (num axes) set to SDL_CONTROLLER_AXIS_MAX, and with nbuttons (num buttons) set to SDL_CONTROLLER_BUTTON_MAX.  When updating their state, values of type SDL_GameControllerAxis or SDL_GameControllerButton can be casted to an int and used for the control-index (in calls to SDL_JoystickSetVirtual* functions).

3. virtual joysticks' guids are mostly all-zeros with the exception of the last two bytes, the first of which is a 'v', to indicate that the guid is a virtual one, and the second of which is a SDL_JoystickType that has been converted into a Uint8.

4. virtual joysticks are ONLY turned into virtual game-controllers if and when their joystick-type is set to SDL_JOYSTICK_TYPE_GAMECONTROLLER.  This is controlled by having SDL's default list of game-controllers have a single entry for a virtual game controller (of guid, "00000000000000000000000000007601", which is subject to the guid-encoding described above).

5. regarding having to call SDL_JoystickUpdate, either directly or indirectly via SDL_PumpEvents or SDL_PollEvents, before new virtual-joystick state becomes active (as specified via SDL_JoystickSetVirtual* function-calls), this was done to match behavior found in SDL's other joystick drivers, almost all of which will only update SDL-state during SDL_JoystickUpdate.

6. the initial patch is based off of SDL 2.0.12

7. the virtual joystick subsystem is disabled by default.  It should be possible to enable it by building with SDL_JOYSTICK_VIRTUAL=1



Questions, comments, suggestions, or bug reports very welcome!
2020-03-13 19:08:45 -07:00
Sam Lantinga dd0ebfdf8b Use the triggers to test rumble for more fine grained vibration feedback 2020-02-07 11:45:32 -08:00
Sam Lantinga a8780c6a28 Updated copyright date for 2020 2020-01-16 20:49:25 -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 b5aff9d7c3 Added SDL_GameControllerTypeForIndex() and SDL_GameControllerGetType() to return the type of controller attached. 2019-11-22 13:12:12 -08:00
Ozkan Sezer 32bb8b4b40 test: replace some exit()s with returns. 2019-09-10 10:03:20 +03:00
Sam Lantinga 5e13087b0f Updated copyright for 2019 2019-01-04 22:01:14 -08:00