diff --git a/input/input.c b/input/input.c index ee37ee03d6..6ef5bab1f5 100644 --- a/input/input.c +++ b/input/input.c @@ -596,7 +596,7 @@ static void interpret_key(struct input_ctx *ictx, int code, double scale) if (mp_msg_test(ictx->log, MSGL_DEBUG)) { int noflags = code & ~(MP_KEY_STATE_DOWN | MP_KEY_STATE_UP); - char *key = mp_input_get_key_name(noflags, NULL); + char *key = mp_input_get_key_name(noflags); MP_DBG(ictx, "key code=%#x '%s'%s%s\n", code, key, (code & MP_KEY_STATE_DOWN) ? " down" : "", (code & MP_KEY_STATE_UP) ? " up" : ""); diff --git a/input/keycodes.c b/input/keycodes.c index a1e466ed09..66cb403c23 100644 --- a/input/keycodes.c +++ b/input/keycodes.c @@ -251,39 +251,49 @@ found: return -1; } -char *mp_input_get_key_name(int key, char *ret) +static void mp_input_append_key_name(bstr *buf, int key) { for (int i = 0; modifier_names[i].name; i++) { if (modifier_names[i].key & key) { - ret = talloc_asprintf_append_buffer(ret, "%s+", - modifier_names[i].name); + bstr_xappend_asprintf(NULL, buf, "%s+", modifier_names[i].name); key -= modifier_names[i].key; } } for (int i = 0; key_names[i].name != NULL; i++) { - if (key_names[i].key == key) - return talloc_asprintf_append_buffer(ret, "%s", key_names[i].name); + if (key_names[i].key == key) { + bstr_xappend_asprintf(NULL, buf, "%s", key_names[i].name); + return; + } } // printable, and valid unicode range - if (key >= 32 && key <= 0x10FFFF) - return mp_append_utf8_buffer(ret, key); + if (key >= 32 && key <= 0x10FFFF) { + mp_append_utf8_bstr(NULL, buf, key); + return; + } // Print the hex key code - return talloc_asprintf_append_buffer(ret, "%#-8x", key); + bstr_xappend_asprintf(NULL, buf, "%#-8x", key); +} + +char *mp_input_get_key_name(int key) +{ + bstr dst = {0}; + mp_input_append_key_name(&dst, key); + return dst.start; } char *mp_input_get_key_combo_name(int *keys, int max) { - char *ret = talloc_strdup(NULL, ""); + bstr dst = {0}; while (max > 0) { - ret = mp_input_get_key_name(*keys, ret); + mp_input_append_key_name(&dst, *keys); if (--max && *++keys) - ret = talloc_asprintf_append_buffer(ret, "-"); + bstr_xappend(NULL, &dst, bstr0("-")); else break; } - return ret; + return dst.start; } int mp_input_get_keys_from_string(char *name, int max_num_keys, diff --git a/input/keycodes.h b/input/keycodes.h index 827aee7ba7..e7dd10c8ab 100644 --- a/input/keycodes.h +++ b/input/keycodes.h @@ -256,8 +256,8 @@ // Get input key from its name. int mp_input_get_key_from_name(const char *name); -// Append given key by name to ret, return ret. -char *mp_input_get_key_name(int key, char *ret); +// Return given key (plus modifiers) as talloc'ed name. +char *mp_input_get_key_name(int key); // Combination of multiple keys to string. char *mp_input_get_key_combo_name(int *keys, int max);