shithub: m8c

Download patch

ref: f7cf5e28c1d666609cc01445ce5433d18e5fa207
parent: de383da3999938633f3c28c50d5b9793dbb03b30
author: smootalicious <smootalicious@gmail.com>
date: Mon Jun 13 14:31:17 EDT 2022

Initialize the game controllers if they are plugged in before m8c starts. Read the database BEFORE trying to read the game controller. Check for the user's game controller db if found, or fall back to the one included in the m8c directory.

--- a/input.c
+++ b/input.c
@@ -3,6 +3,7 @@
 
 #include <SDL.h>
 #include <stdio.h>
+#include <sys/stat.h>
 
 #include "config.h"
 #include "input.h"
@@ -49,22 +50,14 @@
   SDL_Delay(
       10); // Some controllers like XBone wired need a little while to get ready
 
-  // Open all available game controllers
-  for (int i = 0; i < num_joysticks; i++) {
-    if (!SDL_IsGameController(i))
-      continue;
-    if (controller_index >= MAX_CONTROLLERS)
-      break;
-    game_controllers[controller_index] = SDL_GameControllerOpen(i);
-    SDL_Log("Controller %d: %s", controller_index + 1,
-            SDL_GameControllerName(game_controllers[controller_index]));
-    controller_index++;
-  }
-
   // Try to load the game controller database file
   char db_filename[1024] = {0};
   snprintf(db_filename, sizeof(db_filename), "%sgamecontrollerdb.txt",
            SDL_GetPrefPath("", "m8c"));
+  SDL_Log("Looking for game controller database at %s", db_filename);
+  struct stat buffer;   
+  if ( (stat (db_filename, &buffer) != 0))   snprintf(db_filename, sizeof(db_filename), "%sgamecontrollerdb.txt",
+           SDL_GetBasePath());
   SDL_Log("Trying to open game controller database from %s", db_filename);
   SDL_RWops *db_rw = SDL_RWFromFile(db_filename, "rb");
 
@@ -78,6 +71,18 @@
   } else {
     SDL_LogError(SDL_LOG_CATEGORY_INPUT,
                  "Unable to open game controller database file.");
+  }
+
+  // Open all available game controllers
+  for (int i = 0; i < num_joysticks; i++) {
+    if (!SDL_IsGameController(i))
+      continue;
+    if (controller_index >= MAX_CONTROLLERS)
+      break;
+    game_controllers[controller_index] = SDL_GameControllerOpen(i);
+    SDL_Log("Controller %d: %s", controller_index + 1,
+            SDL_GameControllerName(game_controllers[controller_index]));
+    controller_index++;
   }
 
   return controller_index;
--- a/main.c
+++ b/main.c
@@ -82,6 +82,9 @@
   if (initialize_sdl(conf.init_fullscreen, conf.init_use_gpu) == -1)
     run = QUIT;
 
+  // initial scan for (existing) game controllers
+  initialize_game_controllers();
+
 #ifdef DEBUG_MSG
   SDL_LogSetAllPriority(SDL_LOG_PRIORITY_DEBUG);
 #endif
--