cmil::SDL::SDLControllers Class Reference

#include <SDLTranslator.h>

Inheritance diagram for cmil::SDL::SDLControllers:

Inheritance graph
[legend]
Collaboration diagram for cmil::SDL::SDLControllers:

Collaboration graph
[legend]
List of all members.

Public Member Functions

virtual ~SDLControllers ()
 SDLControllers (System *const sys)
AxisgetAxis (unsigned int which, unsigned int id) const
HatgetHat (unsigned int which, unsigned int id) const
PressgetButton (unsigned int which, unsigned int id) const
MotiongetBall (unsigned int which, unsigned int id) const
bool addController (const GameController *controller)
DeviceRegistrygetDevices ()
std::string getName () const
bool translate (const SDL_Event &input_event)
 This should return a boolean as to whether or not translation was successful.

Private Member Functions

bool translate (const SDL_JoyAxisEvent &jaxis)
bool translate (const SDL_JoyBallEvent &jball)
 TODO untested (I need to get one of these gizmos).
bool translate (const SDL_JoyHatEvent &jhat)
bool translate (const SDL_JoyButtonEvent &jbutton, bool movement)

Private Attributes

std::vector< Device * > m_sdljoysticks
DeviceRegistry m_registry

Classes

class  Device
 This class encapsulates individual joystick information. More...

Detailed Description

Definition at line 216 of file SDLTranslator.h.


Constructor & Destructor Documentation

virtual cmil::SDL::SDLControllers::~SDLControllers (  )  [inline, virtual]

Definition at line 218 of file SDLTranslator.h.

References m_sdljoysticks.

00218                                   {
00219           for(std::vector<Device* >::iterator i = m_sdljoysticks.begin(); i != m_sdljoysticks.end(); ++i)
00220             if(*i) delete (*i);
00221         } 

cmil::SDL::SDLControllers::SDLControllers ( System *const   sys  )  [inline]

Definition at line 223 of file SDLTranslator.h.

00223 : tDeviceTranslator<GameController>(sys) {}


Member Function Documentation

bool cmil::SDL::SDLControllers::addController ( const GameController controller  ) 

Axis * cmil::SDL::SDLControllers::getAxis ( unsigned int  which,
unsigned int  id 
) const

Definition at line 461 of file SDLTranslator.cpp.

References m_sdljoysticks.

Referenced by translate().

00461                                                                            {
00462       if(which <= m_sdljoysticks.size())
00463         return m_sdljoysticks[which]->getAxis(id); 
00464       return NULL;
00465     };

Here is the caller graph for this function:

Motion * cmil::SDL::SDLControllers::getBall ( unsigned int  which,
unsigned int  id 
) const

Definition at line 479 of file SDLTranslator.cpp.

References m_sdljoysticks.

Referenced by translate().

00479                                                                              {
00480       if(which <= m_sdljoysticks.size())
00481         return m_sdljoysticks[which]->getBall(id); 
00482       return NULL;
00483     };

Here is the caller graph for this function:

Press * cmil::SDL::SDLControllers::getButton ( unsigned int  which,
unsigned int  id 
) const

Definition at line 473 of file SDLTranslator.cpp.

References m_sdljoysticks.

Referenced by translate().

00473                                                                               {
00474       if(which <= m_sdljoysticks.size())
00475         return m_sdljoysticks[which]->getButton(id); 
00476       return NULL;
00477     };

Here is the caller graph for this function:

DeviceRegistry & cmil::SDL::SDLControllers::getDevices (  )  [virtual]

If no custom GameControllers were provided via addCustomDevice(), any joystick device found by SDL will be used to build a default GameController device.

Otherwise the added custom device will be checked for compatibility with the provided. A match is given when the members of the SDLControllers match the base custom GameController's

Create a generic GameController device for comparison purposes.

SDL 1.2.11 can provide the attributes of a controller at runtime.

Note that this will use the first matching custom joystick provided, and exclude all other matches.

Add a an individual joystick translator for this device type.

Since it will be translated, it's safe to register the device.

No custom device match, use the comparison GameController

Set this back to NULL so we can reuse it on the next iteration.

Implements cmil::SDL::tDeviceTranslator< DeviceBase >.

Definition at line 491 of file SDLTranslator.cpp.

References cmil::DeviceRegistry::clear(), cmil::DeviceRegistry::getSize(), Logger::log_debug(), Logger::log_error(), cmil::SDL::tDeviceTranslator< DeviceBase >::m_custom_devices, m_registry, m_sdljoysticks, and cmil::DeviceRegistry::registerDevice().

00491                                                {
00492       SDL_Joystick* opened_sdl_joy = NULL; 
00493       GameController*  custom_device = NULL; 
00494       std::list<GameController*>::iterator i; 
00495 
00496       unsigned long int njoy; 
00497       unsigned long int axes, buttons, balls, hats;
00498 
00499       std::ostringstream joy_ss;
00500 
00501       if(m_registry.getSize())  
00502         m_registry.clear();
00503 
00504       if(SDL_WasInit(SDL_INIT_JOYSTICK)) {
00505 
00506         if( (njoy = (unsigned)SDL_NumJoysticks()) ) {
00507 
00508           for(unsigned int n = 0 ; n < njoy ; n++) {
00509 
00510             if( (opened_sdl_joy = SDL_JoystickOpen(n)) ) {
00512               joy_ss << "Joystick" << n;  
00513 
00514               log_debug(joy_ss.str());
00515 
00517               axes =    (unsigned)SDL_JoystickNumAxes(    opened_sdl_joy );
00518               buttons = (unsigned)SDL_JoystickNumButtons( opened_sdl_joy );
00519               balls =   (unsigned)SDL_JoystickNumBalls(   opened_sdl_joy );
00520               hats =    (unsigned)SDL_JoystickNumHats(    opened_sdl_joy );
00521 
00522               GameController comparison_joy(joy_ss.str(), axes, buttons, balls, hats);
00523 
00525               for(i = m_custom_devices.begin() ; i != m_custom_devices.end(); ++i) {
00526                 if(*(*i) == comparison_joy) {
00527                   InputDevice* clone = (*i)->clone();
00528                   if(!(custom_device = dynamic_cast<GameController*>(clone) )) {
00529                     log_error("getDevices() casting failure of cloned InputDevice");
00530                     delete clone;
00531                   }
00532                   else {
00534                     m_sdljoysticks.push_back( new Device(*custom_device, opened_sdl_joy) );
00535       
00537                     m_registry.registerDevice(*clone);
00538                     continue;
00539                   }
00540                 }
00541               }
00542 
00544               if(!custom_device) {
00545                   log_debug("getDevices() Adding generic joystick due to no usable custom ones found.");
00546                   m_sdljoysticks.push_back( new Device(comparison_joy, opened_sdl_joy) );
00547                   m_registry.registerDevice(comparison_joy);
00548               }
00549             }
00550             else
00551               log_error("getDevices() SDL provided a joystick with a null structure");
00552 
00554             custom_device = NULL;
00555           }
00556 
00557           joy_ss.str("");
00558         }
00559         else 
00560           log_error("getDevices() SDL error, no joysticks available. thus no inputs will be provided by this translator.");
00561       }
00562       else
00563         log_error("getDevices() SDL joystick subsystem not inititialized, providing no inputs.");
00564 
00565       log_debug("getDevices() returning registry");
00566       return m_registry;
00567     }

Here is the call graph for this function:

Hat * cmil::SDL::SDLControllers::getHat ( unsigned int  which,
unsigned int  id 
) const

Definition at line 467 of file SDLTranslator.cpp.

References m_sdljoysticks.

Referenced by translate().

00467                                                                          {
00468       if(which <= m_sdljoysticks.size())
00469         return m_sdljoysticks[which]->getHat(id); 
00470       return NULL;
00471     };

Here is the caller graph for this function:

std::string cmil::SDL::SDLControllers::getName (  )  const [inline, virtual]

Reimplemented from cmil::SDL::tDeviceTranslator< DeviceBase >.

Definition at line 271 of file SDLTranslator.h.

00271 { return "SDLControllers"; }

bool cmil::SDL::SDLControllers::translate ( const SDL_JoyButtonEvent &  jbutton,
bool  movement 
) [private]

Definition at line 638 of file SDLTranslator.cpp.

References getButton(), Logger::log_debug(), cmil::ModData::setState(), and cmil::SDL::Relay::UpdateSystem().

00638                                                                                    {
00639       Press* button = getButton(jbutton.which, jbutton.button);
00640 
00641       if(button) { 
00642         if(movement)
00643           log_debug("translate setting state to true");
00644         else
00645           log_debug("translate setting state to false");
00646 
00647         button->setState(movement);
00648         UpdateSystem(*button);
00649         return true;
00650       }
00651 
00652       return false;
00653     };

Here is the call graph for this function:

bool cmil::SDL::SDLControllers::translate ( const SDL_JoyHatEvent &  jhat  )  [private]

Definition at line 592 of file SDLTranslator.cpp.

References cmil::HatAction::Centered, cmil::HatAction::Down, getHat(), cmil::HatAction::Left, cmil::HatAction::LeftDown, cmil::HatAction::LeftUp, Logger::log_error(), cmil::HatAction::Right, cmil::HatAction::RightDown, cmil::HatAction::RightUp, cmil::Hat::setPosition(), cmil::HatAction::Up, and cmil::SDL::Relay::UpdateSystem().

00592                                                               { 
00593       Hat* hat = getHat(jhat.which, jhat.hat);
00594 
00595       if(hat) {
00596         switch(jhat.value) {
00597           case SDL_HAT_CENTERED:
00598             hat->setPosition(HatAction::Centered);
00599           break;
00600           case SDL_HAT_UP:
00601             hat->setPosition(HatAction::Up);
00602           break;
00603           case SDL_HAT_RIGHT:
00604             hat->setPosition(HatAction::Right);
00605           break;
00606           case SDL_HAT_DOWN:
00607             hat->setPosition(HatAction::Down);
00608           break;
00609           case SDL_HAT_LEFT:
00610             hat->setPosition(HatAction::Left);
00611           break;
00612           case SDL_HAT_RIGHTUP:
00613             hat->setPosition(HatAction::RightUp);
00614           break;
00615           case SDL_HAT_RIGHTDOWN:
00616             hat->setPosition(HatAction::RightDown);
00617           break;
00618           case SDL_HAT_LEFTUP:
00619             hat->setPosition(HatAction::LeftUp);
00620           break;
00621           case SDL_HAT_LEFTDOWN:
00622             hat->setPosition(HatAction::LeftDown);
00623           break;
00624 
00625           default:
00626             log_error("Convert( Hat ) position gone unhandled");
00627             return false;
00628           break;
00629         }
00630       }
00631       else 
00632         return false;
00633 
00634       UpdateSystem(*hat);
00635       return true; 
00636     };

Here is the call graph for this function:

bool cmil::SDL::SDLControllers::translate ( const SDL_JoyBallEvent &  jball  )  [private]

TODO untested (I need to get one of these gizmos).

Definition at line 581 of file SDLTranslator.cpp.

References getBall(), cmil::Motion::setMotion(), and cmil::SDL::Relay::UpdateSystem().

00581                                                                 { 
00582       Motion* motion= getBall(jball.which, jball.ball);
00583       if(motion)
00584         motion->setMotion(jball.xrel, jball.yrel);
00585       else
00586         return false;
00587 
00588       UpdateSystem(*motion);
00589       return true;
00590     };

Here is the call graph for this function:

bool cmil::SDL::SDLControllers::translate ( const SDL_JoyAxisEvent &  jaxis  )  [private]

Definition at line 569 of file SDLTranslator.cpp.

References getAxis(), cmil::Axis::setAxis(), and cmil::SDL::Relay::UpdateSystem().

00569                                                                 { 
00570       Axis* axis = getAxis(jaxis.which, jaxis.axis);
00571       if(axis)
00572         axis->setAxis(jaxis.value);
00573       else
00574         return false;
00575 
00576       UpdateSystem(*axis);
00577       return true;
00578     };

Here is the call graph for this function:

bool cmil::SDL::SDLControllers::translate ( const SDL_Event &  input_event  )  [virtual]

This should return a boolean as to whether or not translation was successful.

Implements cmil::SDL::tDeviceTranslator< DeviceBase >.

Definition at line 655 of file SDLTranslator.cpp.

References Logger::log_debug(), cmil::pressed, and cmil::released.

00655                                                                {
00656       bool translated = false;
00657 
00658       switch(input_event.type) {
00659         case SDL_JOYAXISMOTION:
00660           translated = translate(input_event.jaxis); 
00661         break;
00662         case SDL_JOYHATMOTION:  
00663           translated = translate(input_event.jhat); 
00664         break;
00665         case SDL_JOYBALLMOTION: 
00666           translated = translate(input_event.jball); 
00667         break;
00668         case SDL_JOYBUTTONDOWN: 
00669           log_debug("translate joy event, calling button press");
00670           translated = translate(input_event.jbutton, pressed); 
00671         break;
00672         case SDL_JOYBUTTONUP: 
00673           log_debug("translate joy event, calling button release");
00674           translated = translate(input_event.jbutton, released); 
00675         break;
00676         default:
00677           translated = false; 
00678         break;
00679       }
00680 
00681       return translated;
00682     };

Here is the call graph for this function:


Member Data Documentation

DeviceRegistry cmil::SDL::SDLControllers::m_registry [private]

Definition at line 284 of file SDLTranslator.h.

Referenced by getDevices().

std::vector<Device* > cmil::SDL::SDLControllers::m_sdljoysticks [private]

This is handled by getDevices() These have been made ptrs given the nature of their destructor.

Definition at line 283 of file SDLTranslator.h.

Referenced by getAxis(), getBall(), getButton(), getDevices(), getHat(), and ~SDLControllers().


The documentation for this class was generated from the following files:
(c) 2006-2007 John P. Feltz
Generated on Wed Jul 25 16:08:03 2007 for Common Media Input Layer by doxygen 1.4.7

SourceForge.net Logo