sdl2_frt/test
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
..
emscripten Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
nacl Fixed bug 3480 - minor update to NACL common.js 2016-11-05 01:48:14 -07:00
shapes Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
acinclude.m4 Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
autogen.sh Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
axis.bmp Split controller axes into positive and negative sides so each can be bound independently. 2016-12-27 01:39:07 -08:00
button.bmp Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
checkkeys.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
CMakeLists.txt offscreen: Add new video driver backend Offscreen 2019-09-24 16:36:48 -04:00
configure test: Improved detection of OpenGL support 2020-02-08 19:34:51 +00:00
configure.ac test: Improved detection of OpenGL support 2020-02-08 19:34:51 +00:00
controllermap.bmp Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
controllermap.c Fixed binding the D-PAD on the 8BitDo M30 controller 2020-03-13 13:05:40 -07:00
COPYING Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
gcc-fat.sh Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
icon.bmp Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
loopwave.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
loopwavequeue.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
Makefile.in test: configure/make shouldn't build GL/GLES1/GLES2 programs if unsupported. 2019-05-18 23:47:57 -04:00
Makefile.os2 test: replace some exit()s with returns. 2019-09-10 10:03:20 +03:00
moose.dat Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
picture.xbm Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
README Backed out changeset e3fcdad257fc - testaudiocapture.c already does what we want 2017-03-16 16:45:12 -07:00
relative_mode.markdown Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
sample.bmp Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
sample.wav Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
testatomic.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testaudiocapture.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testaudiohotplug.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testaudioinfo.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testautomation_audio.c Fixed bug 3744 - missing SDLCALL in several functions 2017-08-13 21:06:52 -07:00
testautomation_clipboard.c Fixed a bunch of compiler warnings in the test code. 2017-01-07 22:24:45 -05:00
testautomation_events.c Fixed bug 3744 - missing SDLCALL in several functions 2017-08-13 21:06:52 -07:00
testautomation_hints.c Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
testautomation_keyboard.c Fixed a bunch of compiler warnings in the test code. 2017-01-07 22:24:45 -05:00
testautomation_main.c Fixed three source comments in tests. 2016-06-28 21:15:16 +02:00
testautomation_mouse.c Fixed bug 3741 - more compatible initializers for arrays 2017-08-13 21:15:44 -07:00
testautomation_pixels.c Added SDL_PIXELFORMAT_BGR444 2019-11-02 22:58:52 +00:00
testautomation_platform.c Fixed a bunch of compiler warnings. 2017-08-29 15:52:49 -04:00
testautomation_rect.c Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
testautomation_render.c Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
testautomation_rwops.c use SDL_zeroa at more places where the argument is an array. 2019-07-31 05:11:40 +03:00
testautomation_sdltest.c test: forgot to change a variable. 2017-08-29 18:25:55 -04:00
testautomation_stdlib.c Fixed a bunch of compiler warnings in the test code. 2017-01-07 22:24:45 -05:00
testautomation_suites.h Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
testautomation_surface.c Added SDL_PIXELFORMAT_BGR444 2019-11-02 22:58:52 +00:00
testautomation_syswm.c Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
testautomation_timer.c Fixed bug 3744 - missing SDLCALL in several functions 2017-08-13 21:06:52 -07:00
testautomation_video.c Fixed freeing uninitialized pointers on failure cleanup in tests. 2015-10-07 21:16:59 +02:00
testautomation.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testbounds.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testcustomcursor.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testdisplayinfo.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testdraw2.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testdrawchessboard.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testdropfile.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testerror.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testfile.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testfilesystem.c testfilesystem: Don't exit the application if SDL_GetBasePath isn't supported 2020-02-13 22:58:04 +00:00
testgamecontroller.c Fixed bug 5028 - Virtual Joysticks (new joystick backend) 2020-03-13 19:08:45 -07:00
testgesture.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testgl2.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testgles2.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testgles.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testhaptic.c test: replace some exit()s with returns. 2019-09-10 10:03:20 +03:00
testhittesting.c Fixed bug 3744 - missing SDLCALL in several functions 2017-08-13 21:06:52 -07:00
testhotplug.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testiconv.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testime.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testintersections.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testjoystick.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testkeys.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testloadso.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testlock.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testmessage.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testmultiaudio.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testnative.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testnative.h Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testnativecocoa.m Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00
testnativew32.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testnativex11.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testoffscreen.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testoverlay2.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testplatform.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testpower.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testqsort.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testrelative.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testrendercopyex.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testrendertarget.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testresample.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testrumble.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testscale.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testsem.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testsensor.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testshader.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testshape.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testsprite2.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testspriteminimal.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
teststreaming.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testthread.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testtimer.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testver.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testviewport.c Clarified that the clip rectangle is defined relative to the viewport, and added a clip test to testviewport.c 2020-03-08 21:02:40 -07:00
testvulkan.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testwm2.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testyuv_cvt.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testyuv_cvt.h Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
testyuv.bmp Updated SDL's YUV support, many thanks to Adrien Descamps 2017-11-12 22:51:12 -08:00
testyuv.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
torturethread.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
utf8.txt Fixed crash if initialization of EGL failed but was tried again later. 2015-06-21 17:33:46 +02:00

These are test programs for the SDL library:

	checkkeys	Watch the key events to check the keyboard
	loopwave	Audio test -- loop playing a WAV file
	loopwavequeue	Audio test -- loop playing a WAV file with SDL_QueueAudio
	testaudioinfo	Lists audio device capabilities
	testerror	Tests multi-threaded error handling
	testfile	Tests RWops layer
	testgl2		A very simple example of using OpenGL with SDL
	testiconv	Tests international string conversion
	testjoystick	List joysticks and watch joystick events
	testkeys	List the available keyboard keys
	testloadso	Tests the loadable library layer
	testlock	Hacked up test of multi-threading and locking
	testmultiaudio	Tests using several audio devices
	testoverlay2	Tests the overlay flickering/scaling during playback.
	testplatform	Tests types, endianness and cpu capabilities
	testsem		Tests SDL's semaphore implementation
	testshape	Tests shaped windows
	testsprite2	Example of fast sprite movement on the screen
	testthread	Hacked up test of multi-threading
	testtimer	Test the timer facilities
	testver		Check the version and dynamic loading and endianness
	testwm2		Test window manager -- title, icon, events
	torturethread	Simple test for thread creation/destruction
	controllermap   Useful to generate Game Controller API compatible maps



This directory contains sample.wav, which is a sample from Will Provost's
song, The Living Proof:

     From the album The Living Proof
     Publisher: 5 Guys Named Will
     Copyright 1996 Will Provost

You can get a copy of the full song (and album!) from iTunes...

    https://itunes.apple.com/us/album/the-living-proof/id4153978

or Amazon...

    http://www.amazon.com/The-Living-Proof-Will-Provost/dp/B00004R8RH

Thanks to Will for permitting us to distribute this sample with SDL!