Game Development Community

Enumerating input devices

by Jaybill · in Torque Game Builder · 02/20/2006 (5:24 pm) · 7 replies

Is there an easy way to grab all of the joystick/gamepad/whatever device names?

Once you have that, is there also a preffered way of determining what axes those devices have? I found a console command called "getJoystickAxes();" which takes a joystick instance, but I'm a little confused about what this is returning. It comes back with the string "5^S^V^Z^Y^X". It does this no matter what "instance" I pass it. (joystick0, joystick1, etc.)

#1
02/21/2006 (7:02 am)
The getJoyStickAxes command returns a tab delimited string that lists how many axes a joystick has, and what those axes are.

In the example you posted, the joystick has 5 axes,

S = Slider
V = RZAxis
X,Y,Z = Normal X, Y, Z axes

Not sure what the RZAxis is, maybe a the analog joystick on a gamepad type device?

Can't find anything where you can enumerate the input devices, at least in the TorqueScript.
#2
02/21/2006 (8:04 am)
It's odd, I've had a look at the InputManager class in the source, and of course, there's a function to enumerate devices, but it's not exposed to TorqueScript, near as I can tell.

This seems like it must be a pretty common problem - anyone else run into this?
#3
02/23/2006 (1:20 pm)
So is there any way to get a list of devices?
#4
02/23/2006 (3:23 pm)
Expose it as a console method?
#5
02/24/2006 (11:45 am)
@Steven: Good idea. The code is below. I just posted this as a resource, as well.

Look at me, making engine mods. I haven't written actual C++ since college.

(This works on beta 1.1, btw)

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
#6
02/24/2006 (11:52 am)
Very nice! Honestly, I didn't expect that much work based on what you mentioned (I thought based on the discussion flow that the functionality existed, just needed a ConsoleMethod wrapper), but that looks quite excellent!
#7
02/24/2006 (12:08 pm)
There's probably an easier way to do it, but this way seemed to make the most sense to me. The C++ knowledge occupying the deep recesses of my brain was covered with cobwebs and sitting right next to the memory of my 3rd grade teacher.