From 0c4af737dd18e6a4be08786a5507025afe90980e Mon Sep 17 00:00:00 2001 From: Matthew Budd Date: Sat, 15 Aug 2020 13:18:16 -0400 Subject: [PATCH] Added logic to GTK GUI to generate default mapping files for new game controllers. --- src/CMakeLists.txt | 1 + src/drivers/common/os_utils.cpp | 87 ++++++++++++++++++++++++++++++++ src/drivers/common/os_utils.h | 9 ++++ src/drivers/sdl/sdl-joystick.cpp | 38 +++++++++++++- 4 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 src/drivers/common/os_utils.cpp create mode 100644 src/drivers/common/os_utils.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e0252c99..6a13bd2b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -395,6 +395,7 @@ set(SRC_DRIVERS_COMMON ${CMAKE_CURRENT_SOURCE_DIR}/drivers/common/scale3x.cpp ${CMAKE_CURRENT_SOURCE_DIR}/drivers/common/scalebit.cpp ${CMAKE_CURRENT_SOURCE_DIR}/drivers/common/vidblit.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/drivers/common/os_utils.cpp ${CMAKE_CURRENT_SOURCE_DIR}/drivers/common/nes_ntsc.c ${CMAKE_CURRENT_SOURCE_DIR}/drivers/videolog/nesvideos-piece.cpp ${CMAKE_CURRENT_SOURCE_DIR}/drivers/videolog/rgbtorgb.cpp diff --git a/src/drivers/common/os_utils.cpp b/src/drivers/common/os_utils.cpp new file mode 100644 index 00000000..90460584 --- /dev/null +++ b/src/drivers/common/os_utils.cpp @@ -0,0 +1,87 @@ +// os_util.cpp +#include +#include + +#if defined(WIN32) +#include +#else +#include +#include +#endif + +#include "common/os_utils.h" +//************************************************************ +int fceu_mkdir( const char *path ) +{ + int retval; +#if defined(WIN32) + retval = mkdir(path); + chmod(path, 755); +#else + retval = mkdir(path, S_IRWXU); + + if ( retval != 0 ) + { + if ( errno == EEXIST ) + { + //printf("Path Exists: '%s'\n", path); + retval = 0; + } + } +#endif + return retval; +} +//************************************************************ +int fceu_mkpath( const char *path ) +{ + int i, retval = 0; + char p[512]; + + i=0; + while ( path[i] != 0 ) + { + if ( path[i] == '/' ) + { + if ( i > 0 ) + { + p[i] = 0; + + retval = fceu_mkdir( p ); + + if ( retval ) + { + return retval; + } + } + } + p[i] = path[i]; i++; + } + p[i] = 0; + + retval = fceu_mkdir( p ); + + return retval; +} +//************************************************************ +bool fceu_file_exists( const char *filepath ) +{ +#ifdef WIN32 + FILE *fp; + fp = ::fopen( filename, "r" ); + + if ( fp != NULL ) + { + ::fclose(fp); + return true; + } +#else + struct stat sb; + + if ( stat( filepath, &sb ) == 0 ) + { + return true; + } +#endif + return false; +} +//************************************************************ diff --git a/src/drivers/common/os_utils.h b/src/drivers/common/os_utils.h new file mode 100644 index 00000000..4ebd7e06 --- /dev/null +++ b/src/drivers/common/os_utils.h @@ -0,0 +1,9 @@ +// os_utils.h +// + +int fceu_mkdir( const char *path ); + +int fceu_mkpath( const char *path ); + +bool fceu_file_exists( const char *filepath ); + diff --git a/src/drivers/sdl/sdl-joystick.cpp b/src/drivers/sdl/sdl-joystick.cpp index 0c599d82..a5e52623 100644 --- a/src/drivers/sdl/sdl-joystick.cpp +++ b/src/drivers/sdl/sdl-joystick.cpp @@ -25,6 +25,7 @@ //#include #include "sdl/sdl.h" #include "sdl/sdl-joystick.h" +#include "common/os_utils.h" #include #include @@ -151,6 +152,39 @@ void jsDev_t::init( int idx ) guidStr.assign( stmp ); + // If game controller, save default mapping if it does not already exist. + if ( gc ) + { + std::string path; + const char *baseDir = FCEUI_GetBaseDirectory(); + + path = std::string(baseDir) + "/input/" + guidStr; + + fceu_mkpath( path.c_str() ); + + path += "/default.txt"; + + if ( !fceu_file_exists( path.c_str() ) ) + { + FILE *fp; + + fp = ::fopen( path.c_str(), "w" ); + + if ( fp != NULL ) + { + const char *defaultMap; + + defaultMap = SDL_GameControllerMapping(gc); + + if ( defaultMap ) + { + //printf("GameController Mapping: %s\n", defaultMap ); + fprintf( fp, "%s", defaultMap ); + } + ::fclose(fp); + } + } + } } void jsDev_t::print(void) @@ -679,7 +713,7 @@ int GamePad_t::saveCurrentMapToFile( const char *name ) } path = std::string(baseDir) + "/input/" + std::string(guid); - //dir.mkpath( QString::fromStdString(path) ); + fceu_mkpath( path.c_str() ); path += "/" + std::string(name) + ".txt"; @@ -767,7 +801,7 @@ int GamePad_t::createProfile( const char *name ) } path = std::string(baseDir) + "/input/" + std::string(guid); - //dir.mkpath( QString::fromStdString(path) ); + fceu_mkpath( path.c_str() ); //printf("DIR: '%s'\n", path.c_str() ); //path += "/" + std::string(name) + ".txt";