1
0
mirror of https://github.com/mpv-player/mpv synced 2025-03-20 02:09:52 +00:00

input: store number of keys, instead of using 0-termination

This commit is contained in:
wm4 2013-06-23 00:07:45 +02:00
parent 12ac3356bf
commit b24070ae5b

View File

@ -68,7 +68,8 @@
#define MP_MAX_KEY_DOWN 4 #define MP_MAX_KEY_DOWN 4
struct cmd_bind { struct cmd_bind {
int input[MP_MAX_KEY_DOWN + 1]; int keys[MP_MAX_KEY_DOWN];
int num_keys;
char *cmd; char *cmd;
char *location; // filename/line number of definition char *location; // filename/line number of definition
bool is_builtin; bool is_builtin;
@ -1097,7 +1098,7 @@ static int read_wakeup(void *ctx, int fd)
return MP_INPUT_NOTHING; return MP_INPUT_NOTHING;
} }
static bool bind_matches_key(struct cmd_bind *bind, int n, int *keys); static bool bind_matches_key(struct cmd_bind *bind, int n, const int *keys);
static void append_bind_info(char **pmsg, struct cmd_bind *bind) static void append_bind_info(char **pmsg, struct cmd_bind *bind)
{ {
@ -1148,16 +1149,15 @@ static mp_cmd_t *handle_test(struct input_ctx *ictx, int n, int *keys)
return res; return res;
} }
static bool bind_matches_key(struct cmd_bind *bind, int n, int *keys) static bool bind_matches_key(struct cmd_bind *bind, int num_keys, const int *keys)
{ {
int found = 1, s; if (bind->num_keys != num_keys)
for (s = 0; s < n && bind->input[s] != 0; s++) { return false;
if (bind->input[s] != keys[s]) { for (int i = 0; i < num_keys; i++) {
found = 0; if (bind->keys[i] != keys[i])
break; return false;
}
} }
return found && bind->input[s] == 0 && s == n; return true;
} }
static struct cmd_bind *find_bind_for_key(struct cmd_bind *binds, int n, static struct cmd_bind *find_bind_for_key(struct cmd_bind *binds, int n,
@ -1731,7 +1731,7 @@ found:
return -1; return -1;
} }
static int get_input_from_name(char *name, int *keys) static int get_input_from_name(char *name, int *out_num_keys, int *keys)
{ {
char *end, *ptr; char *end, *ptr;
int n = 0; int n = 0;
@ -1753,24 +1753,24 @@ static int get_input_from_name(char *name, int *keys)
else else
break; break;
} }
keys[n] = 0; *out_num_keys = n;
return 1; return 1;
} }
static void bind_keys(struct input_ctx *ictx, bool builtin, bstr section, static void bind_keys(struct input_ctx *ictx, bool builtin, bstr section,
const int keys[MP_MAX_KEY_DOWN + 1], bstr command, const int *keys, int num_keys, bstr command,
const char *loc) const char *loc)
{ {
int i = 0, j; int i = 0;
struct cmd_bind *bind = NULL; struct cmd_bind *bind = NULL;
struct cmd_bind_section *bind_section = get_bind_section(ictx, section); struct cmd_bind_section *bind_section = get_bind_section(ictx, section);
assert(num_keys <= MP_MAX_KEY_DOWN);
if (bind_section->cmd_binds) { if (bind_section->cmd_binds) {
for (i = 0; bind_section->cmd_binds[i].cmd != NULL; i++) { for (i = 0; bind_section->cmd_binds[i].cmd != NULL; i++) {
struct cmd_bind *b = &bind_section->cmd_binds[i]; struct cmd_bind *b = &bind_section->cmd_binds[i];
for (j = 0; b->input[j] == keys[j] && keys[j] != 0; j++) if (bind_matches_key(b, num_keys, keys) && b->is_builtin == builtin) {
/* NOTHING */;
if (keys[j] == 0 && b->input[j] == 0 && b->is_builtin == builtin) {
bind = b; bind = b;
break; break;
} }
@ -1789,7 +1789,8 @@ static void bind_keys(struct input_ctx *ictx, bool builtin, bstr section,
bind->location = talloc_strdup(bind_section->cmd_binds, loc); bind->location = talloc_strdup(bind_section->cmd_binds, loc);
bind->owner = bind_section; bind->owner = bind_section;
bind->is_builtin = builtin; bind->is_builtin = builtin;
memcpy(bind->input, keys, (MP_MAX_KEY_DOWN + 1) * sizeof(int)); bind->num_keys = num_keys;
memcpy(bind->keys, keys, num_keys * sizeof(bind->keys[0]));
} }
// restrict_section: every entry is forced to this section name // restrict_section: every entry is forced to this section name
@ -1797,7 +1798,7 @@ static void bind_keys(struct input_ctx *ictx, bool builtin, bstr section,
static int parse_config(struct input_ctx *ictx, bool builtin, bstr data, static int parse_config(struct input_ctx *ictx, bool builtin, bstr data,
const char *location, const char *restrict_section) const char *location, const char *restrict_section)
{ {
int n_binds = 0, keys[MP_MAX_KEY_DOWN + 1]; int n_binds = 0;
int line_no = 0; int line_no = 0;
char *cur_loc = NULL; char *cur_loc = NULL;
@ -1822,7 +1823,9 @@ static int parse_config(struct input_ctx *ictx, bool builtin, bstr data,
continue; continue;
} }
char *name = bstrdup0(NULL, keyname); char *name = bstrdup0(NULL, keyname);
if (!get_input_from_name(name, keys)) { int keys[MP_MAX_KEY_DOWN];
int num_keys = 0;
if (!get_input_from_name(name, &num_keys, keys)) {
talloc_free(name); talloc_free(name);
mp_tmsg(MSGT_INPUT, MSGL_ERR, mp_tmsg(MSGT_INPUT, MSGL_ERR,
"Unknown key '%.*s' at %s\n", BSTR_P(keyname), cur_loc); "Unknown key '%.*s' at %s\n", BSTR_P(keyname), cur_loc);
@ -1841,7 +1844,7 @@ static int parse_config(struct input_ctx *ictx, bool builtin, bstr data,
} }
} }
bind_keys(ictx, builtin, section, keys, command, cur_loc); bind_keys(ictx, builtin, section, keys, num_keys, command, cur_loc);
n_binds++; n_binds++;
// Print warnings if invalid commands are encountered. // Print warnings if invalid commands are encountered.