From 569dc0fb740c90d3912f9f827b9ae793809a4279 Mon Sep 17 00:00:00 2001 From: valentecaio Date: Fri, 17 Jul 2020 21:58:43 -0300 Subject: [PATCH] handling the mapping of buttons dynamically at startup time, using files in subdir js/ --- src/port/sdl/port.cpp | 185 +++++++++++++++++++++++++++++++++++------- src/port/sdl/port.h | 20 +++-- 2 files changed, 169 insertions(+), 36 deletions(-) diff --git a/src/port/sdl/port.cpp b/src/port/sdl/port.cpp index 13d5daa..ddad895 100755 --- a/src/port/sdl/port.cpp +++ b/src/port/sdl/port.cpp @@ -71,6 +71,11 @@ static bool last_keep_aspect = false; void config_load(); void config_save(); void update_window_size(int w, int h, bool ntsc_fix); +void controller_config_load(int js_id); +void controller_config_save(int js_id); +void controller_profile_load(int js_id, int profile_id); +void controller_profile_save(int profile_id, uint8_t *but_map); +void print_controllers_info(); static const char *KEEP_ASPECT_FILENAME = "/sys/devices/platform/jz-lcd.0/keep_aspect_ratio"; @@ -132,8 +137,10 @@ static char homedir[PATH_MAX] = "./.pcsx4all"; static char memcardsdir[PATH_MAX] = "./.pcsx4all/memcards"; static char biosdir[PATH_MAX] = "./.pcsx4all/bios"; static char patchesdir[PATH_MAX] = "./.pcsx4all/patches"; -char sstatesdir[PATH_MAX] = "./.pcsx4all/sstates"; -char cheatsdir[PATH_MAX] = "./.pcsx4all/cheats"; +static char jsdir[PATH_MAX] = "./.pcsx4all/js"; +static char profilesdir[PATH_MAX] = "./.pcsx4all/js/profiles"; +char sstatesdir[PATH_MAX] = "./.pcsx4all/sstates"; +char cheatsdir[PATH_MAX] = "./.pcsx4all/cheats"; static char McdPath1[MAXPATHLEN] = ""; static char McdPath2[MAXPATHLEN] = ""; @@ -147,6 +154,8 @@ static char BiosFile[MAXPATHLEN] = ""; static void setup_paths() { + char fname[MAXPATHLEN]; + #ifndef __WIN32__ home = getenv("HOME"); #else @@ -160,14 +169,58 @@ static void setup_paths() sprintf(biosdir, "%s/bios", homedir); sprintf(patchesdir, "%s/patches", homedir); sprintf(cheatsdir, "%s/cheats", homedir); + sprintf(jsdir, "%s/js", homedir); + sprintf(profilesdir, "%s/profiles", jsdir); } + // create all necessary dirs MKDIR(homedir); MKDIR(sstatesdir); MKDIR(memcardsdir); MKDIR(biosdir); MKDIR(patchesdir); MKDIR(cheatsdir); + MKDIR(jsdir); + MKDIR(profilesdir); + + // create default controller cfg files + for(int i = 0; i < MAX_CONTROLLERS; i++) { + sprintf(fname, "%s/js%d.cfg", jsdir, i); + if(access(fname, F_OK) == -1) { + // player 1, deadzone of 30% and button profile 0 (default) + controllers[i].player = 0; + controllers[i].analog_deadzone = 30; + controllers[i].but_profile_id = 0; + controller_config_save(i); + } + } + + // create default controller profile files + for(int i = 0; i < MAX_JS_PROFILES; i++) { + sprintf(fname, "%s/profile%d.cfg", profilesdir, i); + if(access(fname, F_OK) == -1) { + // default configuration: button 0 = triangle, button 1 = circle ... + uint_least8_t but_map[] = { + DKEY_TRIANGLE, + DKEY_CIRCLE, + DKEY_CROSS, + DKEY_SQUARE, + DKEY_L1, + DKEY_R1, + DKEY_L2, + DKEY_R2, + DKEY_SELECT, + DKEY_START, + DKEY_L3, + DKEY_R3, + DKEY_UP, + DKEY_RIGHT, + DKEY_DOWN, + DKEY_LEFT + }; + controller_profile_save(i, but_map); + } + } } void probe_lastdir() @@ -191,6 +244,96 @@ void probe_lastdir() extern u32 cycle_multiplier; // in mips/recompiler.cpp #endif +void print_controllers_info() { + for(int i = 0; i #include -struct ps1_controller { +#define MAX_JS_BUTTONS 16 +#define MAX_JS_PROFILES 10 +#define MAX_CONTROLLERS 3 + +typedef struct ps1_controller { uint8_t id; uint8_t joy_right_ax0; uint8_t joy_right_ax1; @@ -24,13 +28,15 @@ struct ps1_controller { uint8_t pad_mode; uint8_t pad_controllertype; uint8_t configmode; - uint8_t player; // player-1: 0, player-2: 1 - uint_least8_t but_map[16]; // mapping of SDL buttons, the index is the SDL id of the button - SDL_Joystick* sdl_joy; // used for native analog sticks and USB joysticks -}; + uint8_t player; // player_1: 0, player_2: 1 + uint8_t analog_deadzone; // int between 0 and 100 + uint8_t but_profile_id; // 0 to MAX_JS_PROFILES + uint_least8_t but_map[MAX_JS_BUTTONS]; // mapping of SDL buttons, the index is the SDL id of the button + SDL_Joystick* sdl_joy; // used for native analog sticks and USB joysticks +} Ps1Controller; // 0 for native sticks, 1 for external js1, 2 for external js2 -extern struct ps1_controller controllers[3]; +extern struct ps1_controller controllers[MAX_CONTROLLERS]; /////////////////////////// // Windows compatibility // @@ -41,6 +47,8 @@ static inline int fsync(int f) { return 0; } #endif #define CONFIG_VERSION 0 +#define JS_CONFIG_VERSION 0 +#define JS_PROFILE_VERSION 0 unsigned get_ticks(void); void wait_ticks(unsigned s);