Clean up "ControllerInterface" a bit.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@7339 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
Jordan Woyak
2011-03-14 01:20:11 +00:00
parent 37e31f2df6
commit 8fedc3db38
16 changed files with 416 additions and 659 deletions

View File

@ -30,11 +30,11 @@ void Init( std::vector<ControllerInterface::Device*>& devices )
for(int i = 0; i < SDL_NumJoysticks(); ++i)
{
SDL_Joystick* dev = SDL_JoystickOpen(i);
if ( dev )
if (dev)
{
Joystick* js = new Joystick(dev, i, name_counts[SDL_JoystickName(i)]++);
// only add if it has some inputs/outputs
if ( js->Inputs().size() || js->Outputs().size() )
if (js->Inputs().size() || js->Outputs().size())
devices.push_back( js );
else
delete js;
@ -71,25 +71,23 @@ Joystick::Joystick(SDL_Joystick* const joystick, const int sdl_index, const unsi
#endif
// get buttons
for ( int i = 0; i < SDL_JoystickNumButtons( m_joystick ); ++i )
{
AddInput( new Button( i ) );
}
for (u8 i = 0; i != SDL_JoystickNumButtons(m_joystick); ++i)
AddInput(new Button(i, m_joystick));
// get hats
for ( int i = 0; i < SDL_JoystickNumHats( m_joystick ); ++i )
for (u8 i = 0; i != SDL_JoystickNumHats(m_joystick); ++i)
{
// each hat gets 4 input instances associated with it, (up down left right)
for ( unsigned int d = 0; d < 4; ++d )
AddInput( new Hat( i, d ) );
for (u8 d = 0; d != 4; ++d)
AddInput(new Hat(i, m_joystick, d));
}
// get axes
for ( int i = 0; i < SDL_JoystickNumAxes( m_joystick ); ++i )
for (u8 i = 0; i != SDL_JoystickNumAxes(m_joystick); ++i)
{
// each axis gets a negative and a positive input instance associated with it
AddInput( new Axis( i, -32768 ) );
AddInput( new Axis( i, 32767 ) );
AddInput(new Axis(i, m_joystick, -32768));
AddInput(new Axis(i, m_joystick, 32767));
}
#ifdef USE_SDL_HAPTIC
@ -123,22 +121,22 @@ Joystick::Joystick(SDL_Joystick* const joystick, const int sdl_index, const unsi
Joystick::~Joystick()
{
#ifdef USE_SDL_HAPTIC
if ( m_haptic )
if (m_haptic)
{
// stop/destroy all effects
SDL_HapticStopAll( m_haptic );
SDL_HapticStopAll(m_haptic);
std::vector<EffectIDState>::iterator i = m_state_out.begin(),
e = m_state_out.end();
for ( ; i!=e; ++i )
if ( i->id != -1 )
SDL_HapticDestroyEffect( m_haptic, i->id );
for ( ; i!=e; ++i)
if (i->id != -1)
SDL_HapticDestroyEffect(m_haptic, i->id);
// close haptic first
SDL_HapticClose( m_haptic );
SDL_HapticClose(m_haptic);
}
#endif
// close joystick
SDL_JoystickClose( m_joystick );
SDL_JoystickClose(m_joystick);
}
#ifdef USE_SDL_HAPTIC
@ -154,7 +152,7 @@ std::string Joystick::RampEffect::GetName() const
void Joystick::ConstantEffect::SetState( const ControlState state, Joystick::EffectIDState* const effect )
{
if ( state )
if (state)
{
effect->effect.type = SDL_HAPTIC_CONSTANT;
effect->effect.constant.length = SDL_HAPTIC_INFINITY;
@ -164,13 +162,13 @@ void Joystick::ConstantEffect::SetState( const ControlState state, Joystick::Eff
Sint16 old = effect->effect.constant.level;
effect->effect.constant.level = state * 0x7FFF;
if ( old != effect->effect.constant.level )
if (old != effect->effect.constant.level)
effect->changed = true;
}
void Joystick::RampEffect::SetState( const ControlState state, Joystick::EffectIDState* const effect )
{
if ( state )
if (state)
{
effect->effect.type = SDL_HAPTIC_RAMP;
effect->effect.ramp.length = SDL_HAPTIC_INFINITY;
@ -180,23 +178,11 @@ void Joystick::RampEffect::SetState( const ControlState state, Joystick::EffectI
Sint16 old = effect->effect.ramp.start;
effect->effect.ramp.start = state * 0x7FFF;
if ( old != effect->effect.ramp.start )
if (old != effect->effect.ramp.start)
effect->changed = true;
}
#endif
ControlState Joystick::GetInputState(const ControllerInterface::Device::Input* input) const
{
return ((Input*)input)->GetState( m_joystick );
}
void Joystick::SetOutputState(const ControllerInterface::Device::Output* output, const ControlState state)
{
#ifdef USE_SDL_HAPTIC
((Output*)output)->SetState( state, &m_state_out[ ((Output*)output)->m_index ] );
#endif
}
bool Joystick::UpdateInput()
{
// each joystick is doin this, o well
@ -210,23 +196,23 @@ bool Joystick::UpdateOutput()
#ifdef USE_SDL_HAPTIC
std::vector<EffectIDState>::iterator i = m_state_out.begin(),
e = m_state_out.end();
for ( ; i!=e; ++i )
if ( i->changed ) // if SetState was called on this output
for ( ; i!=e; ++i)
if (i->changed) // if SetState was called on this output
{
if ( -1 == i->id ) // effect isn't currently uploaded
if (-1 == i->id) // effect isn't currently uploaded
{
if ( i->effect.type ) // if outputstate is >0 this would be true
if ( (i->id = SDL_HapticNewEffect( m_haptic, &i->effect )) > -1 ) // upload the effect
SDL_HapticRunEffect( m_haptic, i->id, 1 ); // run the effect
if (i->effect.type) // if outputstate is >0 this would be true
if ((i->id = SDL_HapticNewEffect( m_haptic, &i->effect )) > -1) // upload the effect
SDL_HapticRunEffect(m_haptic, i->id, 1); // run the effect
}
else // effect is already uploaded
{
if ( i->effect.type ) // if ouputstate >0
SDL_HapticUpdateEffect( m_haptic, i->id, &i->effect ); // update the effect
if (i->effect.type) // if ouputstate >0
SDL_HapticUpdateEffect(m_haptic, i->id, &i->effect); // update the effect
else
{
SDL_HapticStopEffect( m_haptic, i->id ); // else, stop and remove the effect
SDL_HapticDestroyEffect( m_haptic, i->id );
SDL_HapticStopEffect(m_haptic, i->id); // else, stop and remove the effect
SDL_HapticDestroyEffect(m_haptic, i->id);
i->id = -1; // mark it as not uploaded
}
}
@ -275,23 +261,21 @@ std::string Joystick::Hat::GetName() const
return tmpstr;
}
ControlState Joystick::Button::GetState( SDL_Joystick* const js ) const
ControlState Joystick::Button::GetState() const
{
return SDL_JoystickGetButton( js, m_index );
return SDL_JoystickGetButton(m_js, m_index);
}
ControlState Joystick::Axis::GetState( SDL_Joystick* const js ) const
ControlState Joystick::Axis::GetState() const
{
return std::max( 0.0f, ControlState(SDL_JoystickGetAxis( js, m_index )) / m_range );
return std::max(0.0f, ControlState(SDL_JoystickGetAxis(m_js, m_index)) / m_range);
}
ControlState Joystick::Hat::GetState( SDL_Joystick* const js ) const
ControlState Joystick::Hat::GetState() const
{
return (SDL_JoystickGetHat( js, m_index ) & ( 1 << m_direction )) > 0;
return (SDL_JoystickGetHat(m_js, m_index) & (1 << m_direction)) > 0;
}
}
}

View File

@ -33,108 +33,80 @@ void Init( std::vector<ControllerInterface::Device*>& devices );
class Joystick : public ControllerInterface::Device
{
friend class ControllerInterface;
friend class ControllerInterface::ControlReference;
protected:
private:
#ifdef USE_SDL_HAPTIC
class EffectIDState
struct EffectIDState
{
friend class Joystick;
protected:
EffectIDState() : id(-1), changed(false) { memset( &effect, 0, sizeof(effect)); }
protected:
SDL_HapticEffect effect;
int id;
bool changed;
};
#endif
class Input : public ControllerInterface::Device::Input
{
friend class Joystick;
protected:
Input( const unsigned int index ) : m_index(index) {}
virtual ControlState GetState( SDL_Joystick* const js ) const = 0;
const unsigned int m_index;
};
#ifdef USE_SDL_HAPTIC
class Output : public ControllerInterface::Device::Output
{
friend class Joystick;
protected:
Output( const size_t index ) : m_index(index) {}
virtual void SetState( const ControlState state, EffectIDState* const effect ) = 0;
const size_t m_index;
};
#endif
class Button : public Input
{
friend class Joystick;
public:
std::string GetName() const;
protected:
Button( const unsigned int index ) : Input(index) {}
ControlState GetState( SDL_Joystick* const js ) const;
Button(u8 index, SDL_Joystick* js) : m_js(js), m_index(index) {}
ControlState GetState() const;
private:
SDL_Joystick* const m_js;
const u8 m_index;
};
class Axis : public Input
{
friend class Joystick;
public:
std::string GetName() const;
protected:
Axis( const unsigned int index, const Sint16 range ) : Input(index), m_range(range) {}
ControlState GetState( SDL_Joystick* const js ) const;
Axis(u8 index, SDL_Joystick* js, Sint16 range) : m_js(js), m_range(range), m_index(index) {}
ControlState GetState() const;
private:
const Sint16 m_range;
SDL_Joystick* const m_js;
const Sint16 m_range;
const u8 m_index;
};
class Hat : public Input
{
friend class Joystick;
public:
std::string GetName() const;
protected:
Hat( const unsigned int index, const unsigned int direction ) : Input(index), m_direction(direction) {}
ControlState GetState( SDL_Joystick* const js ) const;
Hat(u8 index, SDL_Joystick* js, u8 direction) : m_js(js), m_direction(direction), m_index(index) {}
ControlState GetState() const;
private:
const unsigned int m_direction;
SDL_Joystick* const m_js;
const u8 m_direction;
const u8 m_index;
};
#ifdef USE_SDL_HAPTIC
class ConstantEffect : public Output
{
friend class Joystick;
public:
std::string GetName() const;
protected:
ConstantEffect( const size_t index ) : Output(index) {}
void SetState( const ControlState state, EffectIDState* const effect );
private:
SDL_Joystick* const m_js;
};
class RampEffect : public Output
{
friend class Joystick;
public:
std::string GetName() const;
protected:
RampEffect( const size_t index ) : Output(index) {}
void SetState( const ControlState state, EffectIDState* const effect );
private:
SDL_Joystick* const m_js;
};
#endif
public:
bool UpdateInput();
bool UpdateOutput();
ControlState GetInputState( const ControllerInterface::Device::Input* const input ) const;
void SetOutputState( const ControllerInterface::Device::Output* const output, const ControlState state );
public:
Joystick(SDL_Joystick* const joystick, const int sdl_index, const unsigned int index);
~Joystick();
@ -153,7 +125,6 @@ private:
#endif
};
}
}