Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

[input] Add 'Enable Joystick' to GUI settings. Fix for http://trac.xbmc.org/ticket/13146 #1111

Merged
merged 5 commits into from Jul 8, 2012
@@ -9294,7 +9294,13 @@ msgctxt "#35009"
msgid "Do not use the custom keymap for this device"
msgstr ""
-#empty strings from id 35010 to 35499
+#empty strings from id 35010 to 35099
+
+msgctxt "#35100"
+msgid "Enable joystick and gamepad support"
+msgstr ""
+
+#empty strings from id 35101 to 35499
msgctxt "#35500"
msgid "Location"
View
@@ -1351,7 +1351,7 @@ bool CApplication::Initialize()
ResetScreenSaver();
#ifdef HAS_SDL_JOYSTICK
- g_Joystick.Initialize();
+ g_Joystick.SetEnabled(g_guiSettings.GetBool("input.enablejoystick"));
#endif
return true;
@@ -2813,8 +2813,9 @@ bool CApplication::ProcessGamepad(float frameTime)
#ifdef HAS_SDL_JOYSTICK
if (!m_AppFocused)
return false;
+
int iWin = GetActiveWindowID();
- int bid;
+ int bid = 0;
g_Joystick.Update();
if (g_Joystick.GetButton(bid))
{
@@ -2873,7 +2874,7 @@ bool CApplication::ProcessGamepad(float frameTime)
g_Joystick.ResetAxis(abs(bid));
}
}
- int position;
+ int position = 0;
if (g_Joystick.GetHat(bid, position))
{
// reset Idle Timer
@@ -2892,7 +2893,7 @@ bool CApplication::ProcessGamepad(float frameTime)
bid = position<<16|bid;
- if (CButtonTranslator::GetInstance().TranslateJoystickString(iWin, g_Joystick.GetJoystick().c_str(), bid, JACTIVE_HAT, actionID, actionName, fullrange))
+ if (bid && CButtonTranslator::GetInstance().TranslateJoystickString(iWin, g_Joystick.GetJoystick().c_str(), bid, JACTIVE_HAT, actionID, actionName, fullrange))
{
CAction action(actionID, 1.0f, 0.0f, actionName);
g_audioManager.PlayActionSound(action);
View
@@ -41,6 +41,13 @@
#ifdef HAS_PYTHON
#include "interfaces/python/XBPython.h"
#endif
+#if defined(TARGET_WINDOWS)
+#include "input/windows/WINJoystick.h"
+#elif defined(HAS_SDL_JOYSTICK) || defined(HAS_EVENT_SERVER)
+#include "input/SDLJoystick.h"
+#endif
+
+
CGUISettings g_guiSettings;
CSettings g_settings;
@@ -57,6 +64,9 @@
CGUITextureManager g_TextureManager;
CGUILargeTextureManager g_largeTextureManager;
CMouseStat g_Mouse;
+#if defined(HAS_SDL_JOYSTICK) || defined(HAS_EVENT_SERVER)
+ CJoystick g_Joystick;
+#endif
CGUIPassword g_passwordManager;
CGUIInfoManager g_infoManager;
View
@@ -32,25 +32,31 @@
using namespace std;
-CJoystick g_Joystick; // global
-
CJoystick::CJoystick()
{
- Reset();
+ Reset(true);
+ m_joystickEnabled = false;
m_NumAxes = 0;
m_AxisId = 0;
m_JoyId = 0;
m_ButtonId = 0;
m_HatId = 0;
m_HatState = SDL_HAT_CENTERED;
m_ActiveFlags = JACTIVE_NONE;
- for (int i = 0 ; i<MAX_AXES ; i++)
- m_Amount[i] = 0;
SetDeadzone(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();
@@ -108,7 +114,7 @@ void CJoystick::Initialize()
SDL_JoystickEventState(SDL_DISABLE);
}
-void CJoystick::Reset(bool axis)
+void CJoystick::Reset(bool axis /*=false*/)
{
if (axis)
{
@@ -122,6 +128,9 @@ void CJoystick::Reset(bool axis)
void CJoystick::Update()
{
+ if (!IsEnabled())
+ return;
+
int buttonId = -1;
int axisId = -1;
int hatId = -1;
@@ -236,6 +245,9 @@ void CJoystick::Update()
void CJoystick::Update(SDL_Event& joyEvent)
{
+ if (!IsEnabled())
+ return;
+
int buttonId = -1;
int axisId = -1;
int joyId = -1;
@@ -303,8 +315,11 @@ void CJoystick::Update(SDL_Event& joyEvent)
bool CJoystick::GetHat(int &id, int &position,bool consider_repeat)
{
- if (!IsHatActive())
+ if (!IsEnabled() || !IsHatActive())
+ {
+ id = position = 0;
return false;
+ }
position = m_HatState;
id = m_HatId;
if (!consider_repeat)
@@ -336,8 +351,11 @@ bool CJoystick::GetHat(int &id, int &position,bool consider_repeat)
bool CJoystick::GetButton(int &id, bool consider_repeat)
{
- if (!IsButtonActive())
+ if (!IsEnabled() || !IsButtonActive())
+ {
+ id = 0;
return false;
+ }
if (!consider_repeat)
{
id = m_ButtonId;
@@ -372,6 +390,17 @@ bool CJoystick::GetButton(int &id, bool consider_repeat)
return true;
}
+bool CJoystick::GetAxis (int &id)
+{
+ if (!IsEnabled() || !IsAxisActive())
+ {
+ id = 0;
+ return false;
+ }
+ id = m_AxisId;
+ return true;
+}
+
int CJoystick::GetAxisWithMaxAmount()
{
static int maxAmount;
@@ -401,6 +430,20 @@ float CJoystick::GetAmount(int axis)
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;
@@ -409,21 +452,29 @@ float CJoystick::SetDeadzone(float val)
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;
View
@@ -57,12 +57,14 @@ class CJoystick
void Update();
void Update(SDL_Event& event);
bool GetButton (int& id, bool consider_repeat=true);
- bool GetAxis (int &id) { if (!IsAxisActive()) return false; id=m_AxisId; return true; }
+ bool GetAxis (int &id);
bool GetHat (int &id, int &position, bool consider_repeat=true);
std::string GetJoystick() { return (m_JoyId>-1)?m_JoystickNames[m_JoyId]:""; }
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();
@@ -74,6 +76,8 @@ class CJoystick
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;
@@ -82,6 +86,7 @@ class CJoystick
int m_JoyId;
int m_NumAxes;
int m_DeadzoneRange;
+ bool m_joystickEnabled;
uint32_t m_pressTicksButton;
uint32_t m_pressTicksHat;
uint8_t m_ActiveFlags;
Oops, something went wrong.