Skip to content

Instantly share code, notes, and snippets.

@wyatt8740
Created February 21, 2014 16:34
Show Gist options
  • Save wyatt8740/9137632 to your computer and use it in GitHub Desktop.
Save wyatt8740/9137632 to your computer and use it in GitHub Desktop.
SDL Joystick Button ID finder
/* SDLJOYTEST
Prints the ID numbers of SDL input devices when activated.
EXAMPLE: if you are trying to find the SDL button ID of the 'A' button on
an imaginary gamepad or joystick, run this program and then press
the 'A' button on your joystick. As long as it is working properly,
the program will print the IDs of whatever buttons are being held down,
allowing you to enter that ID into a config file for inputs.
So say the 'A' button prints an ID of '5'. That means that the ID
5 is what you should enter for the config file.
If you aren't pressing any buttons and it's printing something, then your
joystick(s) are slightly off-center and always active. Most games have a
'neutral zone' threshold for things like this, to prevent this from mattering.
This program does not have a 'neutral zone' yet. This was a quick-and-dirty
program I made. If things are moving too quicly for you to see what you are
looking for (because the joystick input is moving everything off screen),
you can pipe it to a text file and read it that way.
Use CTRL-C to quit the program.
I made it when I was setting up gamepad input
for the SDL port of Visual Boy Advance with a text editor.
Other emulators (and games) should work with this too.
*/
/* sdljoy-test.cpp can be compiled with:
g++ sdljoy-test.cpp -o sdljoy-test `sdl-config --libs` `sdl-config --cflags`
or on windows:
g++ sdljoy-test.cpp -o sdljoy-test `sdl-config --libs` `sdl-config --cflags` -mconsole
Requires SDL development files and binaries (can be built from source on windows).
Tested in SDL 1.2. */
// <<----START PROGRAM CODE BELOW---->>
#include <stdio.h>
#include "SDL.h"
int main ( int argc, char *argv[] )
{
// initialises SDL joysticks
if ( SDL_InitSubSystem ( SDL_INIT_JOYSTICK ) < 0 )
{
freopen("CON", "w", stdout);
freopen("CON", "w", stderr);
fprintf ( stderr, "Error initializing joystick %s\n", SDL_GetError() );
return -1;
}
freopen("CON", "w", stdout);
freopen("CON", "w", stderr);
fprintf ( stdout, "%i joysticks found\n", SDL_NumJoysticks () );
SDL_Joystick* joy1 = SDL_JoystickOpen ( 0 );
if ( joy1 == NULL )
fprintf ( stdout, "could not open joystick\n" );
fprintf ( stdout, "%i axes\n", SDL_JoystickNumAxes ( joy1 ) );
fprintf ( stdout, "%i trackballs\n", SDL_JoystickNumBalls ( joy1
) );
fprintf ( stdout, "%i hat(s)\n", SDL_JoystickNumHats ( joy1 )
);
fprintf ( stdout, "%i buttons\n", SDL_JoystickNumButtons ( joy1
) );
//SDL_JoystickEventState (SDL_ENABLE);
// this will alter the behaviour of the event queue of the sdl system
SDL_JoystickEventState ( SDL_QUERY );
while ( 1 )
{
// This is needed in the even queue
SDL_JoystickUpdate ();
// now we query for some input
for ( int i=0; i < SDL_JoystickNumButtons ( joy1 ); ++i )
{
unsigned int n = SDL_JoystickGetButton ( joy1, i );
if ( n != 0 )
fprintf ( stdout, "found you pressed button %i\n", i );
}
for ( int i=0; i < SDL_JoystickNumAxes ( joy1 ); ++i )
{
signed short a = SDL_JoystickGetAxis ( joy1, i );
if ( a != 0 )
fprintf ( stdout, "axis %i is %d\n", i,a );
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment