foxygit / doom Log in
commit e0331a01741905ac665adce1da0fb04e86db4b05
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Fri May 16 01:19:14 2014 -0400
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Fri May 16 01:19:14 2014 -0400

    setup: Don't leave joystick subsystem running.

    Only init the joystick subsystem when we need to call the joystick
    API functions, and quit the subsystem when we are finished. This avoids
    conflicts with the joystick widgets that quit the subsystem while the
    main code relies on it running.

    This fixes a bug where trying to calibrate the joystick twice would
    fail on the second attempt.
---
 src/setup/joystick.c | 44 ++++++++++++++++++++++++--------------------
 1 file changed, 24 insertions(+), 20 deletions(-)

diff --git a/src/setup/joystick.c b/src/setup/joystick.c
index 41bde51c..b57667f4 100644
--- a/src/setup/joystick.c
+++ b/src/setup/joystick.c
@@ -361,12 +361,31 @@ static void LoadKnownConfiguration(void)
     LoadConfigurationSet(jstype->configs);
 }

+static void InitJoystick(void)
+{
+    if (!joystick_initted)
+    {
+        joystick_initted = SDL_Init(SDL_INIT_JOYSTICK) >= 0;
+    }
+}
+
+static void UnInitJoystick(void)
+{
+    if (joystick_initted)
+    {
+        SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
+        joystick_initted = 0;
+    }
+}
+
 // Set the label showing the name of the currently selected joystick

 static void SetJoystickButtonLabel(void)
 {
     char *name;

+    InitJoystick();
+
     name = "None set";

     if (joystick_initted
@@ -376,6 +395,8 @@ static void SetJoystickButtonLabel(void)
     }

     TXT_SetButtonLabel(joystick_button, name);
+
+    UnInitJoystick();
 }

 // Try to open all joysticks visible to SDL.
@@ -386,10 +407,7 @@ static int OpenAllJoysticks(void)
     int num_joysticks;
     int result;

-    if (!joystick_initted)
-    {
-        return 0;
-    }
+    InitJoystick();

     // SDL_JoystickOpen() all joysticks.

@@ -447,6 +465,8 @@ static void CloseAllJoysticks(void)

     free(all_joysticks);
     all_joysticks = NULL;
+
+    UnInitJoystick();
 }

 static void CalibrateXAxis(void)
@@ -544,15 +564,6 @@ static void CalibrateJoystick(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(unused))
 // GUI
 //

-static void JoystickWindowClosed(TXT_UNCAST_ARG(window), TXT_UNCAST_ARG(unused))
-{
-    if (joystick_initted)
-    {
-        SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
-        joystick_initted = 0;
-    }
-}
-
 static void AddJoystickControl(txt_table_t *table, char *label, int *var)
 {
     txt_joystick_input_t *joy_input;
@@ -569,11 +580,6 @@ void ConfigJoystick(void)
     txt_table_t *button_table, *axis_table;
     txt_table_t *joystick_table;

-    if (!joystick_initted)
-    {
-        joystick_initted = SDL_Init(SDL_INIT_JOYSTICK) >= 0;
-    }
-
     window = TXT_NewWindow("Gamepad/Joystick configuration");

     TXT_AddWidgets(window,
@@ -639,8 +645,6 @@ void ConfigJoystick(void)
     AddJoystickControl(button_table, "Activate menu", &joybmenu);

     TXT_SignalConnect(joystick_button, "pressed", CalibrateJoystick, NULL);
-    TXT_SignalConnect(window, "closed", JoystickWindowClosed, NULL);
-
     TXT_SetWindowAction(window, TXT_HORIZ_CENTER, TestConfigAction());

     SetJoystickButtonLabel();