Skip to content

Commit

Permalink
Update gamecontrollerdb loading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
drhelius committed Jan 30, 2025
1 parent 5a1fd06 commit 22b9960
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
62 changes: 52 additions & 10 deletions platforms/shared/desktop/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ static Uint64 frame_time_end;

static int sdl_init(void);
static void sdl_destroy(void);
static void sdl_load_gamepad_mappings(void);
static void sdl_events(void);
static void sdl_events_emu(const SDL_Event* event);
static void sdl_shortcuts_gui(const SDL_Event* event);
Expand Down Expand Up @@ -177,16 +178,7 @@ static int sdl_init(void)

SDL_SetWindowMinimumSize(application_sdl_window, 500, 300);

application_gamepad_mappings = SDL_GameControllerAddMappingsFromRW(SDL_RWFromFile("gamecontrollerdb.txt", "rb"), 1);

if (application_gamepad_mappings > 0)
{
Log("Succesfuly loaded %d game controller mappings from gamecontrollerdb.txt", application_gamepad_mappings);
}
else
{
Log("ERROR: Game controller database not found (gamecontrollerdb.txt)!!");
}
sdl_load_gamepad_mappings();

int gamepads_found = 0;

Expand Down Expand Up @@ -252,6 +244,56 @@ static void handle_mouse_cursor(void)
ImGui::SetMouseCursor(ImGuiMouseCursor_Arrow);
}

static void sdl_load_gamepad_mappings(void)
{
std::ifstream file("gamecontrollerdb.txt");
int added_mappings = 0;
int updated_mappings = 0;
int line_number = 0;
char platform_field[64] = { };
snprintf(platform_field, 64, "platform:%s", SDL_GetPlatform());
if (file.is_open())
{
Debug("Loading gamecontrollerdb.txt file");
std::string line;
while (std::getline(file, line))
{
size_t comment = line.find_first_of('#');
if (comment != std::string::npos)
line = line.substr(0, comment);
line = line.erase(0, line.find_first_not_of(" \t\r\n"));
line = line.erase(line.find_last_not_of(" \t\r\n") + 1);
while (line[0] == ' ')
line = line.substr(1);
if (line.empty())
continue;
if ((line.find("platform:") != std::string::npos) && (line.find(platform_field) == std::string::npos))
continue;
int result = SDL_GameControllerAddMapping(line.c_str());
if (result == 1)
added_mappings++;
else if (result == 0)
updated_mappings++;
else if (result == -1)
{
Log("ERROR: Unable to load game controller mapping in line %d from gamecontrollerdb.txt", line_number);
Log("SDL: %s", SDL_GetError());
}
line_number++;
}
file.close();
}
else
{
Log("ERROR: Game controller database not found (gamecontrollerdb.txt)!!");
return;
}
Log("Added %d new game controller mappings from gamecontrollerdb.txt", added_mappings);
Log("Updated %d game controller mappings from gamecontrollerdb.txt", updated_mappings);
application_added_gamepad_mappings = added_mappings;
application_updated_gamepad_mappings = updated_mappings;
}

static void sdl_events(void)
{
SDL_Event event;
Expand Down
3 changes: 2 additions & 1 deletion platforms/shared/desktop/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@

EXTERN SDL_Window* application_sdl_window;
EXTERN SDL_GameController* application_gamepad;
EXTERN int application_gamepad_mappings;
EXTERN int application_added_gamepad_mappings;
EXTERN int application_updated_gamepad_mappings;
EXTERN float application_display_scale;
EXTERN SDL_version application_sdl_build_version;
EXTERN SDL_version application_sdl_link_version;
Expand Down
7 changes: 5 additions & 2 deletions platforms/shared/desktop/gui_popups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,11 @@ void gui_popup_modal_about(void)
else
ImGui::Text("> No gamepad detected");

if (application_gamepad_mappings > 0)
ImGui::Text("%d game controller mappings loaded from gamecontrollerdb.txt", application_gamepad_mappings);
if (application_added_gamepad_mappings || application_updated_gamepad_mappings)
{
ImGui::Text("%d game controller mappings added from gamecontrollerdb.txt", application_added_gamepad_mappings);
ImGui::Text("%d game controller mappings updated from gamecontrollerdb.txt", application_updated_gamepad_mappings);
}
else
ImGui::Text("ERROR: Game controller database not found (gamecontrollerdb.txt)!!");

Expand Down

0 comments on commit 22b9960

Please sign in to comment.