Maelstrom: Open exactly the joysticks we are going to use. Also, open more than one joystick.

https://github.com/libsdl-org/Maelstrom/commit/f68011ede9b560e35d62f9fae9241f94fb0fc3dc

From f68011ede9b560e35d62f9fae9241f94fb0fc3dc Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 23 Nov 2011 23:31:21 -0500
Subject: [PATCH] Open exactly the joysticks we are going to use.  Also, open
 more than one joystick. I can play three way multiplayer locally with three
 joysticks attached.  Yay! :)

---
 game/controls.cpp | 39 +++++++++++++++++++++++++++++++++++++++
 game/controls.h   |  4 ++--
 game/game.cpp     |  3 +++
 game/init.cpp     | 10 ----------
 4 files changed, 44 insertions(+), 12 deletions(-)

diff --git a/game/controls.cpp b/game/controls.cpp
index c7d4ede4..675bac3c 100644
--- a/game/controls.cpp
+++ b/game/controls.cpp
@@ -397,6 +397,45 @@ static void HandleEvent(SDL_Event *event)
 	}
 }
 
+#define MAX_JOYSTICKS	MAX_PLAYERS
+
+static Uint8 joystickMasks[MAX_JOYSTICKS] = {
+	CONTROL_JOYSTICK1,
+	CONTROL_JOYSTICK2,
+	CONTROL_JOYSTICK3
+};
+static SDL_Joystick *joysticks[MAX_JOYSTICKS];
+	
+void InitPlayerControls(void)
+{
+	Uint8 controlMask = 0;
+	unsigned i;
+
+	for (i = 0; i < MAX_PLAYERS; ++i) {
+		controlMask |= gPlayers[i]->GetControlType();
+	}
+
+	for (i = 0; i < MAX_JOYSTICKS; ++i) {
+		if (!(controlMask & joystickMasks[i])) {
+			continue;
+		}
+		joysticks[i] = SDL_JoystickOpen(i);
+		if (joysticks[i] == NULL) {
+			error("Warning: Couldn't open joystick '%s' : %s\n",
+				SDL_JoystickName(i), SDL_GetError());
+		}
+	}
+}
+
+void QuitPlayerControls(void)
+{
+	for (int i = 0; i < MAX_JOYSTICKS; ++i) {
+		if (joysticks[i]) {
+			SDL_JoystickClose(joysticks[i]);
+			joysticks[i] = NULL;
+		}
+	}
+}
 
 /* This function gives a good way to delay a specified amount of time
    while handling keyboard/joystick events, or just to poll for events.
diff --git a/game/controls.h b/game/controls.h
index a9d050d4..7ab35e0e 100644
--- a/game/controls.h
+++ b/game/controls.h
@@ -35,10 +35,10 @@ extern void	CalibrateJoystick(char *joystick);
 #endif
 extern void	LoadControls(void);
 extern void	SaveControls(void);
-extern int	PollEvent(SDL_Event *event, int timeout);
+extern void	InitPlayerControls(void);
+extern void	QuitPlayerControls(void);
 extern void	HandleEvents(int timeout);
 extern int	DropEvents(void);
-extern void	ShowDawn(void);
 
 /* Generic key control definitions */
 #define THRUST_KEY	0x01
diff --git a/game/game.cpp b/game/game.cpp
index aafea5fa..ad83cd38 100644
--- a/game/game.cpp
+++ b/game/game.cpp
@@ -102,6 +102,7 @@ void NewGame(void)
 	if ( !SetupPlayers() ) {
 		return;
 	}
+	InitPlayerControls();
 
 	/* Send a "NEW_GAME" packet onto the network */
 	if ( gGameInfo.IsMultiplayer() && gGameInfo.IsHosting() ) {
@@ -137,6 +138,8 @@ void NewGame(void)
 
 	DoGameOver();
 
+	QuitPlayerControls();
+
 	ui->ShowPanel(PANEL_MAIN);
 }	/* -- NewGame */
 
diff --git a/game/init.cpp b/game/init.cpp
index 5db8eb82..bab1057b 100644
--- a/game/init.cpp
+++ b/game/init.cpp
@@ -704,16 +704,6 @@ int DoInitializations(Uint32 window_flags, Uint32 render_flags)
 		}
 	}
 
-#ifdef SDL_INIT_JOYSTICK
-	/* Initialize the first joystick */
-	if ( SDL_NumJoysticks() > 0 ) {
-		if ( SDL_JoystickOpen(0) == NULL ) {
-			error("Warning: Couldn't open joystick '%s' : %s\n",
-				SDL_JoystickName(0), SDL_GetError());
-		}
-	}
-#endif
-
 	/* Load the Maelstrom icon */
 	icon = SDL_LoadBMP_RW(PHYSFSRWOPS_openRead("icon.bmp"), 1);
 	if ( icon == NULL ) {