CJoystick::CJoystick()
{
Reset(true);
+ m_joystickEnabled = false;
m_NumAxes = 0;
m_AxisId = 0;
m_JoyId = 0;
void CJoystick::Initialize()
{
+ if (!IsEnabled())
+ return;
+
+ if(SDL_InitSubSystem(SDL_INIT_JOYSTICK) != 0)
+ {
+ CLog::Log(LOGERROR, "(Re)start joystick subsystem failed : %s",SDL_GetError());
+ return;
+ }
+
// clear old joystick names
m_JoystickNames.clear();
void CJoystick::Update()
{
+ if (!IsEnabled())
+ return;
+
int buttonId = -1;
int axisId = -1;
int hatId = -1;
void CJoystick::Update(SDL_Event& joyEvent)
{
+ if (!IsEnabled())
+ return;
+
int buttonId = -1;
int axisId = -1;
int joyId = -1;
bool CJoystick::GetHat(int &id, int &position,bool consider_repeat)
{
- if (!IsHatActive())
+ if (!IsEnabled() || !IsHatActive())
{
id = position = 0;
return false;
bool CJoystick::GetButton(int &id, bool consider_repeat)
{
- if (!IsButtonActive())
+ if (!IsEnabled() || !IsButtonActive())
{
id = 0;
return false;
bool CJoystick::GetAxis (int &id)
{
- if (!IsAxisActive())
+ if (!IsEnabled() || !IsAxisActive())
{
id = 0;
return false;
return 0;
}
+void CJoystick::SetEnabled(bool enabled /*=true*/)
+{
+ if( enabled && !m_joystickEnabled )
+ {
+ m_joystickEnabled = true;
+ Initialize();
+ }
+ else if( !enabled && m_joystickEnabled )
+ {
+ ReleaseJoysticks();
+ m_joystickEnabled = false;
+ }
+}
+
float CJoystick::SetDeadzone(float val)
{
if (val<0) val=0;
return val;
}
-bool CJoystick::Reinitialize()
+bool CJoystick::ReleaseJoysticks()
{
+ m_pJoysticks.clear();
+ m_JoystickNames.clear();
+ m_HatId = 0;
+ m_ButtonId = 0;
+ m_HatState = SDL_HAT_CENTERED;
+ m_ActiveFlags = JACTIVE_NONE;
+ Reset(true);
+
// Restart SDL joystick subsystem
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
if (SDL_WasInit(SDL_INIT_JOYSTICK) != 0)
CLog::Log(LOGERROR, "Stop joystick subsystem failed");
return false;
}
- if(SDL_InitSubSystem(SDL_INIT_JOYSTICK) != 0)
- {
- CLog::Log(LOGERROR, "Restart joystick subsystem failed : %s",SDL_GetError());
- return false;
- }
+ return true;
+}
+bool CJoystick::Reinitialize()
+{
+ if( !ReleaseJoysticks() ) return false;
Initialize();
return true;
int GetAxisWithMaxAmount();
float GetAmount(int axis);
float GetAmount() { return GetAmount(m_AxisId); }
+ bool IsEnabled() const { return m_joystickEnabled; }
+ void SetEnabled(bool enabled = true);
float SetDeadzone(float val);
bool Reinitialize();
bool IsAxisActive() { return (m_ActiveFlags & JACTIVE_AXIS) == JACTIVE_AXIS; }
bool IsHatActive() { return (m_ActiveFlags & JACTIVE_HAT) == JACTIVE_HAT; }
+ bool ReleaseJoysticks();
+
int m_Amount[MAX_AXES];
int m_AxisId;
int m_ButtonId;
int m_JoyId;
int m_NumAxes;
int m_DeadzoneRange;
+ bool m_joystickEnabled;
uint32_t m_pressTicksButton;
uint32_t m_pressTicksHat;
uint8_t m_ActiveFlags;
CJoystick::CJoystick()
{
Reset(true);
+ m_joystickEnabled = false;
m_NumAxes = 0;
m_AxisId = 0;
m_JoyId = 0;
void CJoystick::Initialize()
{
+ if (!IsEnabled())
+ return;
+
HRESULT hr;
// clear old joystick names
void CJoystick::Update()
{
+ if (!IsEnabled())
+ return;
+
int buttonId = -1;
int axisId = -1;
int hatId = -1;
bool CJoystick::GetHat(int &id, int &position,bool consider_repeat)
{
- if (!IsHatActive())
+ if (!IsEnabled() || !IsHatActive())
{
id = position = 0;
return false;
bool CJoystick::GetButton(int &id, bool consider_repeat)
{
- if (!IsButtonActive())
+ if (!IsEnabled() || !IsButtonActive())
{
id = 0;
return false;
bool CJoystick::GetAxis (int &id)
{
- if (!IsAxisActive())
+ if (!IsEnabled() || !IsAxisActive())
{
id = 0;
return false;
return 0;
}
+void CJoystick::SetEnabled(bool enabled /*=true*/)
+{
+ if( enabled && !m_joystickEnabled )
+ {
+ m_joystickEnabled = true;
+ Initialize();
+ }
+ else if( !enabled && m_joystickEnabled )
+ {
+ ReleaseJoysticks();
+ m_joystickEnabled = false;
+ }
+}
+
float CJoystick::SetDeadzone(float val)
{
if (val<0) val=0;
void CJoystick::Acquire()
{
+ if (!IsEnabled())
+ return;
if(!m_pJoysticks.empty())
{
CLog::Log(LOGDEBUG, __FUNCTION__": Focus back, acquire Joysticks");
int GetAxisWithMaxAmount();
float GetAmount(int axis);
float GetAmount() { return GetAmount(m_AxisId); }
+ bool IsEnabled() const { return m_joystickEnabled; }
+ void SetEnabled(bool enabled = true);
float SetDeadzone(float val);
bool Reinitialize();
void Acquire();
int m_JoyId;
int m_NumAxes;
int m_DeadzoneRange;
+ bool m_joystickEnabled;
uint32_t m_pressTicksButton;
uint32_t m_pressTicksHat;
uint8_t m_ActiveFlags;