Commit dc689548 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

ConfigKeyToString: simplify and fix potential leak

parent 09110552
...@@ -193,38 +193,24 @@ uint_fast32_t ConfigStringToKey (const char *name) ...@@ -193,38 +193,24 @@ uint_fast32_t ConfigStringToKey (const char *name)
return (vlc_towc (name, &cp) > 0) ? (mods | cp) : 0; return (vlc_towc (name, &cp) > 0) ? (mods | cp) : 0;
} }
char *ConfigKeyToString (uint_fast32_t i_key) char *ConfigKeyToString (uint_fast32_t code)
{ {
// Worst case appears to be 45 characters: char *str, buf[5];
// "Command-Meta-Ctrl-Shift-Alt-Browser Favorites" uintptr_t key = code & ~KEY_MODIFIER;
char *psz_key = malloc (64);
if (!psz_key)
return NULL;
char *p = psz_key, *psz_end = psz_key + 54;
*p = '\0';
for (size_t i = 0; i < vlc_num_modifiers; i++)
{
if (i_key & vlc_modifiers[i].i_key_code)
{
p += snprintf (p, psz_end - p, "%s-",
vlc_modifiers[i].psz_key_string);
}
}
key_descriptor_t *d; key_descriptor_t *d = bsearch ((void *)key, vlc_keys, vlc_num_keys,
char buf[5]; sizeof (vlc_keys[0]), cmpkey);
if (d == NULL && utf8_cp (key, buf) == NULL)
return NULL;
i_key &= ~KEY_MODIFIER; if (asprintf (&str, "%s%s%s%s%s%s",
d = bsearch ((void *)(uintptr_t)i_key, vlc_keys, vlc_num_keys, (code & KEY_MODIFIER_CTRL) ? "Ctrl-" : "",
sizeof (vlc_keys[0]), cmpkey); (code & KEY_MODIFIER_ALT) ? "Alt-" : "",
if (d) (code & KEY_MODIFIER_SHIFT) ? "Shift-" : "",
p += snprintf (p, psz_end - p, "%s", d->psz_key_string); (code & KEY_MODIFIER_META) ? "Meta-" : "",
else if (utf8_cp (i_key, buf)) (code & KEY_MODIFIER_COMMAND) ? "Command-" : "",
p += snprintf (p, psz_end - p, "%s", buf); (d != NULL) ? d->psz_key_string : buf) == -1)
else
return NULL; return NULL;
return psz_key; return str;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment