Game Development Community

Getting Joystick/Gamepad names

by Jaybill · 03/17/2006 (7:01 pm) · 4 comments

winInputDevice.h after line 102:
U8 getDeviceID();

add:
U8 getJoystickCount();
const char* getJoystickMname();

At the end of winInputDevice.cc add:
U8 DInputDevice::getJoystickCount()
{
   return smJoystickCount;
}

const char* DInputDevice::getJoystickMname()
{
   return mName;
}

In winDirectInput.h, after line 84
const char* getJoystickAxesString( U32 deviceID );

add
const char* getJoystickName( U32 deviceID );
const char* getJoystickShortname( U32 deviceID );

at the end of winDirectInput.cc, add

//------------------------------------------------------------------------------
ConsoleFunction( getJoystickCount, S32, 1, 1, "getJoystickCount()" )
{
   argc; argv;
   DInputDevice* dptr;
   S32 jcount = static_cast<S32>( dptr->getJoystickCount() );
   return jcount;
}

And also in winDirectInput.cc after line 472, add:

//------------------------------------------------------------------------------
const char* DInputManager::getJoystickName( U32 deviceID )
{
   DInputDevice* dptr;
   for ( iterator ptr = begin(); ptr != end(); ptr++ )
   {
      dptr = dynamic_cast<DInputDevice*>( *ptr );
      if ( dptr && ( dptr->getDeviceType() == JoystickDeviceType ) && ( dptr->getDeviceID() == deviceID ) )
         return( dptr->getName() );
   }

   return( "" );
}

//------------------------------------------------------------------------------
const char* DInputManager::getJoystickShortname( U32 deviceID )
{
   DInputDevice* dptr;
   for ( iterator ptr = begin(); ptr != end(); ptr++ )
   {
      dptr = dynamic_cast<DInputDevice*>( *ptr );
      if ( dptr && ( dptr->getDeviceType() == JoystickDeviceType ) && ( dptr->getDeviceID() == deviceID ) )
         return( dptr->getJoystickMname() );
   }

   return( "" );
}

//------------------------------------------------------------------------------
ConsoleFunction( getJoystickShortname, const char*, 2, 2, "getJoystickShortname( index )" )
{
   argc;
   DInputManager* mgr = dynamic_cast<DInputManager*>( Input::getManager() );
   if ( mgr )
      return( mgr->getJoystickShortname( dAtoi( argv[1] ) ) );

   return( "" );
}

In winInput.cc after line 150 (After ConsoleFunction( getJoystickAxes ) close brace, add:

//------------------------------------------------------------------------------
ConsoleFunction( getJoystickName, const char*, 2, 2, "getJoystickName( index )" )
{
   argc;
   DInputManager* mgr = dynamic_cast<DInputManager*>( Input::getManager() );
   if ( mgr )
      return( mgr->getJoystickName( dAtoi( argv[1] ) ) );

   return( "" );
}


So now, in script, you can do the following:

function joystickDump(){
  for(%i = 0; %i < getJoystickCount(); %i++){
    echo(getJoystickShortname(%i) @ " is a " @ getJoystickName(%i) );    
    echo("Axis String for " @ getJoystickShortname(%i) @": "@getJoystickAxes(getJoystickShortname(%i)));
  } 
}

When you run this from the console, you'll get something like the following:
==>joystickdump();
joystick0 is a WingMan Cordless Gamepad
Axis String for joystick0: 5^S^V^Z^Y^X
joystick1 is a GamePad Pro USB 
Axis String for joystick1: 5^S^V^Z^Y^X

About the author

Recent Blogs


#1
03/22/2006 (12:08 pm)
Works like a charm!
#2
03/27/2006 (2:18 pm)
Thanks for this code :)
It is very usefull for our game :)
#3
12/04/2006 (1:58 pm)
Hey guys I have been trying to use this. Although it seems to work fine, I am getting a warning when I compile.

c:\torque\sdk\engine\platformwin32\windirectinput.cc(1042): warning C4700: local variable 'dptr' used without having been initialized

//------------------------------------------------------------------------------ConsoleFunction( getJoystickCount, S32, 1, 1, "getJoystickCount()" ){   argc; argv;   DInputDevice* dptr;   S32 jcount = static_cast<S32>( dptr->getJoystickCount() );   return jcount;}

Usually dptr is initialized through some sort of cast, however that is not the case here. Is there a good way to do this without causing further problems? Or should I just ignore the warning?
#4
02/20/2008 (1:02 pm)
I'm really interested in this but I'm getting the same error as Ron above, and to me this is an error that breaks compiling.