input_config_get_bind_string - do proper character

counting now, avoid strlcats, and have functions return
size_t value indicating how many chars were written
This commit is contained in:
libretroadmin 2024-06-16 18:07:30 +02:00
parent a303b7dcc6
commit 54a24dc9d5
4 changed files with 104 additions and 114 deletions

View File

@ -3600,38 +3600,39 @@ unsigned input_config_translate_str_to_bind_id(const char *str)
return RARCH_BIND_LIST_END;
}
void input_config_get_bind_string(
size_t input_config_get_bind_string(
void *settings_data,
char *buf,
char *s,
const struct retro_keybind *bind,
const struct retro_keybind *auto_bind,
size_t size)
size_t len)
{
settings_t *settings = (settings_t*)settings_data;
size_t _len = 0;
int delim = 0;
bool input_descriptor_label_show =
settings->bools.input_descriptor_label_show;
*buf = '\0';
*s = '\0';
if (bind && bind->joykey != NO_BTN)
input_config_get_bind_string_joykey(
_len = input_config_get_bind_string_joykey(
input_descriptor_label_show,
buf, "", bind, size);
s, "", bind, len);
else if (bind && bind->joyaxis != AXIS_NONE)
input_config_get_bind_string_joyaxis(
_len = input_config_get_bind_string_joyaxis(
input_descriptor_label_show,
buf, "", bind, size);
s, "", bind, len);
else if (auto_bind && auto_bind->joykey != NO_BTN)
input_config_get_bind_string_joykey(
_len = input_config_get_bind_string_joykey(
input_descriptor_label_show,
buf, "(Auto)", auto_bind, size);
s, "(Auto)", auto_bind, len);
else if (auto_bind && auto_bind->joyaxis != AXIS_NONE)
input_config_get_bind_string_joyaxis(
_len = input_config_get_bind_string_joyaxis(
input_descriptor_label_show,
buf, "(Auto)", auto_bind, size);
s, "(Auto)", auto_bind, len);
if (*buf)
if (*s)
delim = 1;
#ifndef RARCH_CONSOLE
@ -3649,15 +3650,10 @@ void input_config_get_bind_string(
/*empty?*/
else if (*key != '\0')
{
char keybuf[64];
keybuf[0] = '\0';
if (delim)
strlcat(buf, ", ", size);
snprintf(keybuf, sizeof(keybuf),
_len += strlcpy(s + _len, ", ", len - _len);
_len += snprintf(s + _len, len - _len,
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_INPUT_KEY), key);
strlcat(buf, keybuf, size);
delim = 1;
}
}
@ -3700,51 +3696,49 @@ void input_config_get_bind_string(
if (tag != 0)
{
if (delim)
strlcat(buf, ", ", size);
strlcat(buf, msg_hash_to_str((enum msg_hash_enums)tag), size);
_len += strlcpy(s + _len, ", ", len - _len);
_len += strlcpy(s + _len, msg_hash_to_str((enum msg_hash_enums)tag), len - _len);
}
}
/*completely empty?*/
if (*buf == '\0')
strlcat(buf, "---", size);
if (*s == '\0')
_len += strlcpy(s + _len, "---", len - _len);
return _len;
}
void input_config_get_bind_string_joykey(
size_t input_config_get_bind_string_joykey(
bool input_descriptor_label_show,
char *buf, const char *suffix,
const struct retro_keybind *bind, size_t size)
char *s, const char *suffix,
const struct retro_keybind *bind, size_t len)
{
size_t _len = 0;
if (GET_HAT_DIR(bind->joykey))
{
if ( bind->joykey_label
&& !string_is_empty(bind->joykey_label)
&& input_descriptor_label_show)
fill_pathname_join_delim(buf,
bind->joykey_label, suffix, ' ', size);
else
return fill_pathname_join_delim(s,
bind->joykey_label, suffix, ' ', len);
_len = snprintf(s, len,
"Hat #%u ", (unsigned)GET_HAT(bind->joykey));
switch (GET_HAT_DIR(bind->joykey))
{
size_t len = snprintf(buf, size,
"Hat #%u ", (unsigned)GET_HAT(bind->joykey));
switch (GET_HAT_DIR(bind->joykey))
{
case HAT_UP_MASK:
strlcpy(buf + len, "Up", size - len);
break;
case HAT_DOWN_MASK:
strlcpy(buf + len, "Down", size - len);
break;
case HAT_LEFT_MASK:
strlcpy(buf + len, "Left", size - len);
break;
case HAT_RIGHT_MASK:
strlcpy(buf + len, "Right", size - len);
break;
default:
strlcpy(buf + len, "?", size - len);
break;
}
case HAT_UP_MASK:
_len += strlcpy(s + _len, "Up", len - _len);
break;
case HAT_DOWN_MASK:
_len += strlcpy(s + _len, "Down", len - _len);
break;
case HAT_LEFT_MASK:
_len += strlcpy(s + _len, "Left", len - _len);
break;
case HAT_RIGHT_MASK:
_len += strlcpy(s + _len, "Right", len - _len);
break;
default:
_len += strlcpy(s + _len, "?", len - _len);
break;
}
}
else
@ -3752,37 +3746,34 @@ void input_config_get_bind_string_joykey(
if ( bind->joykey_label
&& !string_is_empty(bind->joykey_label)
&& input_descriptor_label_show)
fill_pathname_join_delim(buf,
bind->joykey_label, suffix, ' ', size);
else
{
size_t _len = strlcpy(buf, "Button ", size);
snprintf(buf + _len, size - _len, "%u",
(unsigned)bind->joykey);
}
return fill_pathname_join_delim(s,
bind->joykey_label, suffix, ' ', len);
_len = strlcpy(s, "Button ", len);
_len += snprintf(s + _len, len - _len, "%u",
(unsigned)bind->joykey);
}
return _len;
}
void input_config_get_bind_string_joyaxis(
size_t input_config_get_bind_string_joyaxis(
bool input_descriptor_label_show,
char *buf, const char *suffix,
const struct retro_keybind *bind, size_t size)
char *s, const char *suffix,
const struct retro_keybind *bind, size_t len)
{
size_t _len = 0;
if ( bind->joyaxis_label
&& !string_is_empty(bind->joyaxis_label)
&& input_descriptor_label_show)
fill_pathname_join_delim(buf,
bind->joyaxis_label, suffix, ' ', size);
else
{
size_t _len = strlcpy(buf, "Axis ", size);
if (AXIS_NEG_GET(bind->joyaxis) != AXIS_DIR_NONE)
snprintf(buf + _len, size - _len, "-%u",
(unsigned)AXIS_NEG_GET(bind->joyaxis));
else if (AXIS_POS_GET(bind->joyaxis) != AXIS_DIR_NONE)
snprintf(buf + _len, size - _len, "+%u",
(unsigned)AXIS_POS_GET(bind->joyaxis));
}
return fill_pathname_join_delim(s,
bind->joyaxis_label, suffix, ' ', len);
_len = strlcpy(s, "Axis ", len);
if (AXIS_NEG_GET(bind->joyaxis) != AXIS_DIR_NONE)
_len += snprintf(s + _len, len - _len, "-%u",
(unsigned)AXIS_NEG_GET(bind->joyaxis));
else if (AXIS_POS_GET(bind->joyaxis) != AXIS_DIR_NONE)
_len += snprintf(s + _len, len - _len, "+%u",
(unsigned)AXIS_POS_GET(bind->joyaxis));
return _len;
}
void osk_update_last_codepoint(

View File

@ -949,15 +949,28 @@ void input_remote_free(input_remote_t *handle, unsigned max_users);
void input_game_focus_free(void);
void input_config_get_bind_string_joyaxis(
bool input_descriptor_label_show,
char *buf, const char *prefix,
const struct retro_keybind *bind, size_t size);
/**
* Converts a retro_keybind to a human-readable string, optionally allowing a
* fallback auto_bind to be used as the source for the string.
*
* @param buf A string which will be overwritten with the returned value
* @param bind A binding to convert to a string
* @param auto_bind A default binding which will be used after `bind`. Can be NULL.
* @param size The maximum length that will be written to `buf`
*/
size_t input_config_get_bind_string(void *settings_data,
char *s, const struct retro_keybind *bind,
const struct retro_keybind *auto_bind, size_t len);
void input_config_get_bind_string_joykey(
size_t input_config_get_bind_string_joyaxis(
bool input_descriptor_label_show,
char *buf, const char *prefix,
const struct retro_keybind *bind, size_t size);
char *s, const char *prefix,
const struct retro_keybind *bind, size_t len);
size_t input_config_get_bind_string_joykey(
bool input_descriptor_label_show,
char *s, const char *prefix,
const struct retro_keybind *bind, size_t len);
bool input_key_pressed(int key, bool keyboard_pressed);

View File

@ -30,7 +30,7 @@ RETRO_BEGIN_DECLS
/**
* Loads a remap file from disk to memory.
*
*
* @param data Path to config file.
*
* @return true (1) if successful, otherwise false (0).
@ -49,15 +49,15 @@ bool input_remapping_save_file(const char *path);
/**
* Caches any global configuration settings that should not be overwritten by
* input remap changes made while content is running. Must be called on each
* core init.
* core init.
*/
void input_remapping_cache_global_config(void);
/**
* Restores any global configuration settings that were cached on the last core
* init if INP_FLAG_REMAPPING_CACHE_ACTIVE is set.
* Must be called on core deinitialization.
*
* Must be called on core deinitialization.
*
* @param clear_cache If true, function becomes a NOOP until the next time
* `input_remapping_cache_global_config()` is called, and
* INP_FLAG_REMAPPING_CACHE_ACTIVE is set.
@ -75,7 +75,7 @@ void input_remapping_restore_global_config(bool clear_cache, bool restore_analog
void input_remapping_update_port_map(void);
/**
* Frees runloop_st->name.remapfile and sets these runloop_state flags to false:
* Frees runloop_st->name.remapfile and sets these runloop_state flags to false:
* remaps_core_active, remaps_content_dir_active, and remaps_game_active.
*
* @param save_remap If true, current remap settings will be saved to
@ -86,7 +86,7 @@ void input_remapping_deinit(bool save_remap);
/**
* Used to set the default mapping values within the `settings` struct
* @param clear_cache This value is passed to
* @param clear_cache This value is passed to
* `input_remapping_restore_global_config()`. Please see
* the documentation for that function for details.
*/
@ -95,9 +95,9 @@ void input_remapping_set_defaults(bool clear_cache);
/**
* Checks `input_config_bind_map` for the requested `input_bind_map`, and if
* the bind has been registered, returns its base.
*
*
* @param index
*
*
* @return the contents of the meta field, or NULL if there is no matching bind
*/
const char *input_config_bind_map_get_base(unsigned bind_index);
@ -115,9 +115,9 @@ unsigned input_config_bind_map_get_meta(unsigned bind_index);
/**
* Checks `input_config_bind_map` for the requested `input_bind_map`, and if
* the bind has been registered, returns a pointer to its description field.
*
*
* @param index
*
*
* @return the contents of the description field, or NULL if there is no
* matching bind
*/
@ -126,29 +126,16 @@ const char *input_config_bind_map_get_desc(unsigned index);
/**
* Checks `input_config_bind_map` for the requested `input_bind_map`, and if
* the bind has been registered, returns the value of its retro_key field.
*
*
* @param index
*
*
* @return the value of the retro_key field, or 0 if there is no matching bind
*/
uint8_t input_config_bind_map_get_retro_key(unsigned index);
/**
* Converts a retro_keybind to a human-readable string, optionally allowing a
* fallback auto_bind to be used as the source for the string.
*
* @param buf A string which will be overwritten with the returned value
* @param bind A binding to convert to a string
* @param auto_bind A default binding which will be used after `bind`. Can be NULL.
* @param size The maximum length that will be written to `buf`
*/
void input_config_get_bind_string(void *settings_data,
char *buf, const struct retro_keybind *bind,
const struct retro_keybind *auto_bind, size_t size);
/**
* Parses the string representation of a retro_key struct
*
*
* @param str String to parse.
*
* @return Key identifier.
@ -157,7 +144,7 @@ enum retro_key input_config_translate_str_to_rk(const char *str, size_t len);
/**
* Searches for a string among the "base" fields of the list of binds.
*
*
* @param str String to search for among the binds
*
* @return Bind index value on success or RARCH_BIND_LIST_END if not found.
@ -174,7 +161,7 @@ void config_read_keybinds_conf(void *data);
/**
* Apply autoconfig binds to the indicated control port.
*
*
* @param port
* @param data An object of type config_file_t. We assume it is passed as a
* void pointer like this to avoid including config_file.h.

View File

@ -5949,9 +5949,9 @@ static int action_ok_add_entry_to_playlist(const char *path,
static void action_input_add_entry_to_new_playlist(void *userdata, const char *line)
{
settings_t *settings = config_get_ptr();
size_t path_length = 0;
char path[PATH_MAX_LENGTH];
settings_t *settings = config_get_ptr();
size_t _len = 0;
menu_input_dialog_end();
@ -5959,9 +5959,8 @@ static void action_input_add_entry_to_new_playlist(void *userdata, const char *l
return;
/* Create path for new file */
path_length = fill_pathname_join_special(path, settings->paths.directory_playlist, line, sizeof(path));
strlcat(path, ".lpl", sizeof(path) - path_length);
_len = fill_pathname_join_special(path, settings->paths.directory_playlist, line, sizeof(path));
strlcpy(path + _len, ".lpl", sizeof(path) - _len);
action_ok_add_entry_to_playlist(NULL, path, 0, 0, 0);
}