Skip to content

Commit

Permalink
Add saved file name to autoconfig profile save message
Browse files Browse the repository at this point in the history
Autoconfig profile may be useful for e.g. submitting to autoconfig
repo. Construction of the filename is moved to a separate function
and reused for message.
  • Loading branch information
zoltanvb committed Feb 20, 2024
1 parent cd1a970 commit 253a661
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 25 deletions.
86 changes: 62 additions & 24 deletions configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -4929,14 +4929,9 @@ static void input_config_save_keybinds_user_override(config_file_t *conf,
}
}

/**
* config_save_autoconf_profile:
* @device_name : Input device name
* @user : Controller number to save
* Writes a controller autoconf file to disk.
**/
bool config_save_autoconf_profile(const
char *device_name, unsigned user)
void config_get_autoconf_profile_filename(
const char *device_name, unsigned user,
char *buf, size_t len_buf)
{
static const char* invalid_filename_chars[] = {
/* https://support.microsoft.com/en-us/help/905231/information-about-the-characters-that-you-cannot-use-in-site-names--fo */
Expand All @@ -4945,12 +4940,7 @@ bool config_save_autoconf_profile(const
};
size_t len;
unsigned i;
char buf[PATH_MAX_LENGTH];
char autoconf_file[PATH_MAX_LENGTH];
config_file_t *conf = NULL;
int32_t pid_user = 0;
int32_t vid_user = 0;
bool ret = false;

settings_t *settings = config_st;
const char *autoconf_dir = settings->paths.directory_autoconfig;
const char *joypad_driver_fallback = settings->arrays.input_joypad_driver;
Expand Down Expand Up @@ -4993,15 +4983,66 @@ bool config_save_autoconf_profile(const
}

/* Generate autoconfig file path */
fill_pathname_join_special(buf, autoconf_dir, joypad_driver, sizeof(buf));
fill_pathname_join_special(buf, autoconf_dir, joypad_driver, len_buf);

if (path_is_directory(buf))
len = fill_pathname_join_special(autoconf_file, buf,
sanitised_name, sizeof(autoconf_file));
/* Driver specific autoconf dir may not exist, if autoconfs are not downloaded. */
if (!path_is_directory(buf))
{
len = strlcpy(buf, sanitised_name, len_buf);
}
else
len = fill_pathname_join_special(autoconf_file, autoconf_dir,
sanitised_name, sizeof(autoconf_file));
strlcpy(autoconf_file + len, ".cfg", sizeof(autoconf_file) - len);
{
len = fill_pathname_join_special(buf, joypad_driver, sanitised_name, len_buf);
}
strlcpy(buf + len, ".cfg", len_buf - len);

end:
if (sanitised_name)
free(sanitised_name);

}
/**
* config_save_autoconf_profile:
* @device_name : Input device name
* @user : Controller number to save
* Writes a controller autoconf file to disk.
**/
bool config_save_autoconf_profile(const
char *device_name, unsigned user)
{
size_t len;
unsigned i;
char buf[PATH_MAX_LENGTH];
char autoconf_file[PATH_MAX_LENGTH];
config_file_t *conf = NULL;
int32_t pid_user = 0;
int32_t vid_user = 0;
bool ret = false;
settings_t *settings = config_st;
const char *autoconf_dir = settings->paths.directory_autoconfig;
const char *joypad_driver_fallback = settings->arrays.input_joypad_driver;
const char *joypad_driver = NULL;

if (string_is_empty(device_name))
goto end;

/* Get currently set joypad driver */
joypad_driver = input_config_get_device_joypad_driver(user);
if (string_is_empty(joypad_driver))
{
/* This cannot happen, but if we reach this
* point without a driver being set for the
* current input device then use the value
* from the settings struct as a fallback */
joypad_driver = joypad_driver_fallback;

if (string_is_empty(joypad_driver))
goto end;
}

/* Generate autoconfig file path */
config_get_autoconf_profile_filename(device_name, user, buf, sizeof(buf));
fill_pathname_join_special(autoconf_file, autoconf_dir, buf, sizeof(autoconf_file));

/* Open config file */
if ( !(conf = config_file_new_from_path_to_string(autoconf_file))
Expand Down Expand Up @@ -5043,9 +5084,6 @@ bool config_save_autoconf_profile(const
ret = config_file_write(conf, autoconf_file, false);

end:
if (sanitised_name)
free(sanitised_name);

if (conf)
config_file_free(conf);

Expand Down
10 changes: 10 additions & 0 deletions configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,16 @@ bool config_unload_override(void);
bool config_load_remap(const char *directory_input_remapping,
void *data);

/**
* config_get_autoconf_profile_filename:
* @device_name : Input device name
* @user : Controller number to save
* Fills buf with the autoconf profile file name (including driver dir if needed).
**/

void config_get_autoconf_profile_filename(
const char *device_name, unsigned user,
char *buf, size_t len_buf);
/**
* config_save_autoconf_profile:
* @device_name : Input device name
Expand Down
4 changes: 4 additions & 0 deletions intl/msg_hash_us.h
Original file line number Diff line number Diff line change
Expand Up @@ -13997,6 +13997,10 @@ MSG_HASH(
MSG_AUTOCONFIG_FILE_SAVED_SUCCESSFULLY,
"Controller profile saved successfully."
)
MSG_HASH(
MSG_AUTOCONFIG_FILE_SAVED_SUCCESSFULLY_NAMED,
"Controller profile saved in Controller Profiles directory as\n\"%s\""
)
MSG_HASH(
MSG_AUTOSAVE_FAILED,
"Could not initialize autosave."
Expand Down
8 changes: 7 additions & 1 deletion menu/menu_setting.c
Original file line number Diff line number Diff line change
Expand Up @@ -2528,9 +2528,15 @@ static int setting_action_ok_bind_all_save_autoconfig(

if (!string_is_empty(name) &&
config_save_autoconf_profile(name, index_offset))
{
char buf[PATH_MAX_LENGTH];
char msg[PATH_MAX_LENGTH];
config_get_autoconf_profile_filename(name, index_offset, buf, sizeof(buf));
snprintf(msg, sizeof(msg),msg_hash_to_str(MSG_AUTOCONFIG_FILE_SAVED_SUCCESSFULLY_NAMED),buf);
runloop_msg_queue_push(
msg_hash_to_str(MSG_AUTOCONFIG_FILE_SAVED_SUCCESSFULLY), 1, 100, true,
msg, 1, 180, true,
NULL, MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}
else
runloop_msg_queue_push(
msg_hash_to_str(MSG_AUTOCONFIG_FILE_ERROR_SAVING), 1, 100, true,
Expand Down
1 change: 1 addition & 0 deletions msg_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ enum msg_hash_enums
MSG_OVERRIDES_ERROR_SAVING,
MSG_OVERRIDES_ERROR_REMOVING,
MSG_AUTOCONFIG_FILE_SAVED_SUCCESSFULLY,
MSG_AUTOCONFIG_FILE_SAVED_SUCCESSFULLY_NAMED,
MSG_AUTOCONFIG_FILE_ERROR_SAVING,
MSG_FILE_ALREADY_EXISTS_SAVING_TO_BACKUP_BUFFER,
MSG_AUTOLOADING_SAVESTATE_FROM,
Expand Down

0 comments on commit 253a661

Please sign in to comment.