mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 06:09:50 -06:00
Commited my new wiimote plugin work so far. Some code was copied from the current wiimote plugin. I have cleaned up most of the functions, but there are still a bunch of unused structs and stuff that I need to clean up.
Moved ControllerInterface to InputCommon. Moved GCPadNew GUI/Config code to a new project, InputPluginCommon. It is used by both GCPadNew and WiimoteNew. I hope that I included everyone's fixes to GCPadNew and ControllerInterface. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5355 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
266
Source/Core/InputCommon/Src/ControllerInterface/SDL/SDL.cpp
Normal file
266
Source/Core/InputCommon/Src/ControllerInterface/SDL/SDL.cpp
Normal file
@ -0,0 +1,266 @@
|
||||
#include "../ControllerInterface.h"
|
||||
|
||||
#ifdef CIFACE_USE_SDL
|
||||
|
||||
#include "SDL.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#if SDL_VERSION_ATLEAST(1, 3, 0)
|
||||
#pragma comment(lib, "SDL.1.3.lib")
|
||||
#else
|
||||
#pragma comment(lib, "SDL.lib")
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace ciface
|
||||
{
|
||||
namespace SDL
|
||||
{
|
||||
|
||||
void Init( std::vector<ControllerInterface::Device*>& devices )
|
||||
{
|
||||
if ( SDL_Init( SDL_INIT_FLAGS ) >= 0 )
|
||||
{
|
||||
// joysticks
|
||||
for( int i = 0; i < SDL_NumJoysticks(); ++i )
|
||||
{
|
||||
SDL_Joystick* dev = SDL_JoystickOpen( i );
|
||||
if ( dev )
|
||||
{
|
||||
Joystick* js = new Joystick( dev, i );
|
||||
// only add if it has some inputs/outputs
|
||||
if ( js->Inputs().size() || js->Outputs().size() )
|
||||
devices.push_back( js );
|
||||
else
|
||||
delete js;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Joystick::Joystick( SDL_Joystick* const joystick, const unsigned int index ) : m_joystick(joystick), m_index(index)
|
||||
{
|
||||
// get buttons
|
||||
for ( int i = 0; i < SDL_JoystickNumButtons( m_joystick ); ++i )
|
||||
{
|
||||
inputs.push_back( new Button( i ) );
|
||||
}
|
||||
|
||||
// get hats
|
||||
for ( int 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 )
|
||||
inputs.push_back( new Hat( i, d ) );
|
||||
}
|
||||
|
||||
// get axes
|
||||
for ( int i = 0; i < SDL_JoystickNumAxes( m_joystick ); ++i )
|
||||
{
|
||||
// each axis gets a negative and a positive input instance associated with it
|
||||
inputs.push_back( new Axis( i, -32768 ) );
|
||||
inputs.push_back( new Axis( i, 32767 ) );
|
||||
}
|
||||
|
||||
#ifdef USE_SDL_HAPTIC
|
||||
// try to get supported ff effects
|
||||
m_haptic = SDL_HapticOpenFromJoystick( m_joystick );
|
||||
if ( m_haptic )
|
||||
{
|
||||
//SDL_HapticSetGain( m_haptic, 1000 );
|
||||
//SDL_HapticSetAutocenter( m_haptic, 0 );
|
||||
|
||||
const unsigned int supported_effects = SDL_HapticQuery( m_haptic );
|
||||
|
||||
// constant effect
|
||||
if ( supported_effects & SDL_HAPTIC_CONSTANT )
|
||||
{
|
||||
outputs.push_back( new ConstantEffect( m_state_out.size() ) );
|
||||
m_state_out.push_back( EffectIDState() );
|
||||
}
|
||||
|
||||
// ramp effect
|
||||
if ( supported_effects & SDL_HAPTIC_RAMP )
|
||||
{
|
||||
outputs.push_back( new RampEffect( m_state_out.size() ) );
|
||||
m_state_out.push_back( EffectIDState() );
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
Joystick::~Joystick()
|
||||
{
|
||||
#ifdef USE_SDL_HAPTIC
|
||||
if ( m_haptic )
|
||||
{
|
||||
// stop/destroy all effects
|
||||
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 );
|
||||
// close haptic first
|
||||
SDL_HapticClose( m_haptic );
|
||||
}
|
||||
#endif
|
||||
|
||||
// close joystick
|
||||
SDL_JoystickClose( m_joystick );
|
||||
}
|
||||
|
||||
#ifdef USE_SDL_HAPTIC
|
||||
std::string Joystick::ConstantEffect::GetName() const
|
||||
{
|
||||
return "Constant";
|
||||
}
|
||||
|
||||
std::string Joystick::RampEffect::GetName() const
|
||||
{
|
||||
return "Ramp";
|
||||
}
|
||||
|
||||
void Joystick::ConstantEffect::SetState( const ControlState state, Joystick::EffectIDState* const effect )
|
||||
{
|
||||
if ( state )
|
||||
{
|
||||
effect->effect.type = SDL_HAPTIC_CONSTANT;
|
||||
effect->effect.constant.length = SDL_HAPTIC_INFINITY;
|
||||
}
|
||||
else
|
||||
effect->effect.type = 0;
|
||||
|
||||
Sint16 old = effect->effect.constant.level;
|
||||
effect->effect.constant.level = state * 0x7FFF;
|
||||
if ( old != effect->effect.constant.level )
|
||||
effect->changed = true;
|
||||
}
|
||||
|
||||
void Joystick::RampEffect::SetState( const ControlState state, Joystick::EffectIDState* const effect )
|
||||
{
|
||||
if ( state )
|
||||
{
|
||||
effect->effect.type = SDL_HAPTIC_RAMP;
|
||||
effect->effect.ramp.length = SDL_HAPTIC_INFINITY;
|
||||
}
|
||||
else
|
||||
effect->effect.type = 0;
|
||||
|
||||
Sint16 old = effect->effect.ramp.start;
|
||||
effect->effect.ramp.start = state * 0x7FFF;
|
||||
if ( old != effect->effect.ramp.start )
|
||||
effect->changed = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
ControlState Joystick::GetInputState(const ControllerInterface::Device::Input* input)
|
||||
{
|
||||
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
|
||||
SDL_JoystickUpdate();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
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
|
||||
}
|
||||
else // effect is already uploaded
|
||||
{
|
||||
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 );
|
||||
i->id = -1; // mark it as not uploaded
|
||||
}
|
||||
}
|
||||
|
||||
i->changed = false;
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string Joystick::GetName() const
|
||||
{
|
||||
return SDL_JoystickName( m_index );
|
||||
}
|
||||
|
||||
std::string Joystick::GetSource() const
|
||||
{
|
||||
return "SDL";
|
||||
}
|
||||
|
||||
int Joystick::GetId() const
|
||||
{
|
||||
return m_index;
|
||||
}
|
||||
|
||||
std::string Joystick::Button::GetName() const
|
||||
{
|
||||
std::ostringstream ss;
|
||||
ss << "Button " << m_index;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string Joystick::Axis::GetName() const
|
||||
{
|
||||
std::ostringstream ss;
|
||||
ss << "Axis " << m_index << ( m_range>0 ? '+' : '-' );
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string Joystick::Hat::GetName() const
|
||||
{
|
||||
std::ostringstream ss;
|
||||
ss << "Hat " << m_index << ' ' << "NESW"[m_direction];
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
ControlState Joystick::Button::GetState( SDL_Joystick* const js ) const
|
||||
{
|
||||
return SDL_JoystickGetButton( js, m_index );
|
||||
}
|
||||
|
||||
ControlState Joystick::Axis::GetState( SDL_Joystick* const js ) const
|
||||
{
|
||||
return std::max( 0.0f, ControlState(SDL_JoystickGetAxis( js, m_index )) / m_range );
|
||||
}
|
||||
|
||||
ControlState Joystick::Hat::GetState( SDL_Joystick* const js ) const
|
||||
{
|
||||
return (SDL_JoystickGetHat( js, m_index ) & ( 1 << m_direction )) > 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
159
Source/Core/InputCommon/Src/ControllerInterface/SDL/SDL.h
Normal file
159
Source/Core/InputCommon/Src/ControllerInterface/SDL/SDL.h
Normal file
@ -0,0 +1,159 @@
|
||||
#ifndef _CIFACE_SDL_H_
|
||||
#define _CIFACE_SDL_H_
|
||||
|
||||
#include "../ControllerInterface.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <SDL.h>
|
||||
#else
|
||||
#include <SDL/SDL.h>
|
||||
#endif
|
||||
|
||||
#if SDL_VERSION_ATLEAST(1, 3, 0)
|
||||
#define USE_SDL_HAPTIC
|
||||
#endif
|
||||
|
||||
#ifdef USE_SDL_HAPTIC
|
||||
#define SDL_INIT_FLAGS SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC
|
||||
#ifdef _WIN32
|
||||
#include <SDL_haptic.h>
|
||||
#else
|
||||
#include <SDL/SDL_haptic.h>
|
||||
#endif
|
||||
#else
|
||||
#define SDL_INIT_FLAGS SDL_INIT_JOYSTICK
|
||||
#endif
|
||||
|
||||
namespace ciface
|
||||
{
|
||||
namespace SDL
|
||||
{
|
||||
|
||||
void Init( std::vector<ControllerInterface::Device*>& devices );
|
||||
|
||||
class Joystick : public ControllerInterface::Device
|
||||
{
|
||||
friend class ControllerInterface;
|
||||
friend class ControllerInterface::ControlReference;
|
||||
|
||||
protected:
|
||||
|
||||
#ifdef USE_SDL_HAPTIC
|
||||
class 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;
|
||||
};
|
||||
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;
|
||||
private:
|
||||
const Sint16 m_range;
|
||||
};
|
||||
|
||||
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;
|
||||
private:
|
||||
const unsigned int m_direction;
|
||||
};
|
||||
|
||||
#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 );
|
||||
};
|
||||
|
||||
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 );
|
||||
};
|
||||
#endif
|
||||
|
||||
bool UpdateInput();
|
||||
bool UpdateOutput();
|
||||
|
||||
ControlState GetInputState( const ControllerInterface::Device::Input* const input );
|
||||
void SetOutputState( const ControllerInterface::Device::Output* const output, const ControlState state );
|
||||
|
||||
public:
|
||||
Joystick( SDL_Joystick* const joystick, const unsigned int index );
|
||||
~Joystick();
|
||||
|
||||
std::string GetName() const;
|
||||
int GetId() const;
|
||||
std::string GetSource() const;
|
||||
|
||||
private:
|
||||
SDL_Joystick* const m_joystick;
|
||||
const unsigned int m_index;
|
||||
|
||||
#ifdef USE_SDL_HAPTIC
|
||||
std::vector<EffectIDState> m_state_out;
|
||||
SDL_Haptic* m_haptic;
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user