Joystick Bugfix
#1
Bug 
Hi,

I've found two bugs concerning the CJoystick class.
Joysticks will only be discoverd when Kodi starts and Joystick support is enabled. If support will be enabled in the gui (while kodi is running) the joystick will not be discoverd. This is because the SDL Joystick Subsystem will be first time initialized at program start and second time when support is enabled. This causes the the subsystem to never be correctly deinitialized and so there is always just a second initialization, which does not trigger joystick discovery events.

This patch fixes it:
Code:
diff --git a/xbmc/Application.cpp b/xbmc/Application.cpp
index dcb1610..281671e 100644
--- a/xbmc/Application.cpp
+++ b/xbmc/Application.cpp
@@ -717,7 +717,7 @@ bool CApplication::CreateGUI()
#endif

#if defined(HAS_SDL_JOYSTICK) && !defined(TARGET_WINDOWS)
-  sdlFlags |= SDL_INIT_JOYSTICK;
+  //sdlFlags |= SDL_INIT_JOYSTICK;
#endif

   //depending on how it's compiled, SDL periodically calls XResetScreenSaver when it's fullscreen
diff --git a/xbmc/input/SDLJoystick.cpp b/xbmc/input/SDLJoystick.cpp
index b1e71e1..0cea72c 100644
--- a/xbmc/input/SDLJoystick.cpp
+++ b/xbmc/input/SDLJoystick.cpp
@@ -54,6 +54,15 @@ void CJoystick::Initialize()
   if (!IsEnabled())
     return;

+  if (SDL_WasInit(SDL_INIT_JOYSTICK) !=  0)
+  {
+    SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
+    if (SDL_WasInit(SDL_INIT_JOYSTICK) !=  0)
+    {
+      CLog::Log(LOGERROR, "Joystick subsystem still initialized!");
+    }
+  }
+
   if(SDL_InitSubSystem(SDL_INIT_JOYSTICK) != 0)
   {
     CLog::Log(LOGERROR, "(Re)start joystick subsystem failed : %s",SDL_GetError());

The second bug occurs if you try to disable joystick support via the jsonrpc which causes kodi to crash with a segfault because the CJoystick class is not thread safe. I tried to make the class thread safe, but didn't get it working. Instead i fixed it by just setting a flag in the OnSettingChange function telling the the main thread to enable/disable joystick support.
Here is the patch:
Code:
diff --git a/xbmc/input/InputManager.cpp b/xbmc/input/InputManager.cpp
index 1eaffc2..1457182 100644
--- a/xbmc/input/InputManager.cpp
+++ b/xbmc/input/InputManager.cpp
@@ -424,6 +424,14 @@ bool CInputManager::Process(int windowId, float frameTime)
   ProcessGamepad(windowId);
   ProcessEventServer(windowId, frameTime);
   ProcessPeripherals(frameTime);
+
+#if defined(HAS_SDL_JOYSTICK)
+  if (ShallSetJoystickEnabled)
+  {
+    m_Joystick.SetEnabled(ShallSetJoystickEnabled == 1);
+    ShallSetJoystickEnabled = 0;
+  }
+#endif
  
   return true;
}
@@ -860,7 +868,12 @@ void CInputManager::OnSettingChanged(const CSetting *setting)

#if defined(HAS_SDL_JOYSTICK)
   if (settingId == CSettings::SETTING_INPUT_ENABLEJOYSTICK)
-    m_Joystick.SetEnabled(dynamic_cast<const CSettingBool*>(setting)->GetValue() &&
-    PERIPHERALS::CPeripheralImon::GetCountOfImonsConflictWithDInput() == 0);
+  {
+    ShallSetJoystickEnabled = dynamic_cast<const CSettingBool*>(setting)->GetValue() &&
+    PERIPHERALS::CPeripheralImon::GetCountOfImonsConflictWithDInput() == 0;
+    ShallSetJoystickEnabled = ShallSetJoystickEnabled ? 1 : -1;
+    //m_Joystick.SetEnabled(dynamic_cast<const CSettingBool*>(setting)->GetValue() &&
+    //PERIPHERALS::CPeripheralImon::GetCountOfImonsConflictWithDInput() == 0);
+  }
#endif
}
diff --git a/xbmc/input/InputManager.h b/xbmc/input/InputManager.h
index 423bb39..44211e6 100644
--- a/xbmc/input/InputManager.h
+++ b/xbmc/input/InputManager.h
@@ -45,7 +45,7 @@ class CKey;
class CInputManager : public ISettingCallback
{
private:
-  CInputManager() { }
+  CInputManager() : ShallSetJoystickEnabled(0) { }
   CInputManager(const CInputManager&);
   CInputManager const& operator=(CInputManager const&);
   virtual ~CInputManager() { };
@@ -268,4 +268,5 @@ private:
#if defined(HAS_SDL_JOYSTICK)
   CJoystick m_Joystick;
#endif
+  int ShallSetJoystickEnabled;
};

It would be nice, if someone could push these two bugfixes into the git. Is there a way i can do it myself?
Reply

Logout Mark Read Team Forum Stats Members Help
Joystick Bugfix0