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
nacl
shapes
acinclude.m4
autogen.sh
axis.bmp
button.bmp
checkkeys.c
CMakeLists.txt
configure
configure.ac
controllermap.bmp
controllermap.c
COPYING
gcc-fat.sh
icon.bmp
loopwave.c
loopwavequeue.c
Makefile.in
Makefile.os2
moose.dat
picture.xbm
README
relative_mode.markdown
sample.bmp
sample.wav
testatomic.c
testaudiocapture.c
testaudiohotplug.c
testaudioinfo.c
testautomation_audio.c
testautomation_clipboard.c
testautomation_events.c
testautomation_hints.c
testautomation_keyboard.c
testautomation_main.c
testautomation_mouse.c
testautomation_pixels.c
testautomation_platform.c
testautomation_rect.c
testautomation_render.c
testautomation_rwops.c
testautomation_sdltest.c
testautomation_stdlib.c
testautomation_suites.h
testautomation_surface.c
testautomation_syswm.c
testautomation_timer.c
testautomation_video.c
testautomation.c
testbounds.c
testcustomcursor.c
testdisplayinfo.c
testdraw2.c
testdrawchessboard.c
testdropfile.c
testerror.c
testfile.c
testfilesystem.c
testgamecontroller.c Fixed bug 5028 - Virtual Joysticks (new joystick backend) 2020-03-13 19:08:45 -07:00
testgesture.c
testgl2.c
testgles2.c
testgles.c
testhaptic.c
testhittesting.c
testhotplug.c
testiconv.c
testime.c
testintersections.c
testjoystick.c
testkeys.c
testloadso.c
testlock.c
testmessage.c
testmultiaudio.c
testnative.c
testnative.h
testnativecocoa.m
testnativew32.c
testnativex11.c
testoffscreen.c
testoverlay2.c
testplatform.c
testpower.c
testqsort.c
testrelative.c
testrendercopyex.c
testrendertarget.c
testresample.c
testrumble.c
testscale.c
testsem.c
testsensor.c
testshader.c
testshape.c
testsprite2.c
testspriteminimal.c
teststreaming.c
testthread.c
testtimer.c
testver.c
testviewport.c
testvulkan.c
testwm2.c
testyuv_cvt.c
testyuv_cvt.h
testyuv.bmp
testyuv.c
torturethread.c
utf8.txt

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!