summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2013-06-23 00:07:45 +0200
committerwm4 <wm4@nowhere>2013-06-29 22:58:14 +0200
commitb24070ae5b914b05356efd913e299fcdc489a6ed (patch)
tree10c7c34704f1c0da29316ba8b1e2b1ccb551ee0a
parent12ac3356bf82aa84f8caefecea9dd24be0e66ebb (diff)
downloadmpv-b24070ae5b914b05356efd913e299fcdc489a6ed.tar.bz2
mpv-b24070ae5b914b05356efd913e299fcdc489a6ed.tar.xz
input: store number of keys, instead of using 0-termination
-rw-r--r--core/input/input.c45
1 files changed, 24 insertions, 21 deletions
diff --git a/core/input/input.c b/core/input/input.c
index bec4331eec..7aeea74ce7 100644
--- a/core/input/input.c
+++ b/core/input/input.c
@@ -68,7 +68,8 @@
#define MP_MAX_KEY_DOWN 4
struct cmd_bind {
- int input[MP_MAX_KEY_DOWN + 1];
+ int keys[MP_MAX_KEY_DOWN];
+ int num_keys;
char *cmd;
char *location; // filename/line number of definition
bool is_builtin;
@@ -1097,7 +1098,7 @@ static int read_wakeup(void *ctx, int fd)
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)
{
@@ -1148,16 +1149,15 @@ static mp_cmd_t *handle_test(struct input_ctx *ictx, int n, int *keys)
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;
- for (s = 0; s < n && bind->input[s] != 0; s++) {
- if (bind->input[s] != keys[s]) {
- found = 0;
- break;
- }
+ if (bind->num_keys != num_keys)
+ return false;
+ for (int i = 0; i < num_keys; i++) {
+ if (bind->keys[i] != keys[i])
+ 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,
@@ -1731,7 +1731,7 @@ found:
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;
int n = 0;
@@ -1753,24 +1753,24 @@ static int get_input_from_name(char *name, int *keys)
else
break;
}
- keys[n] = 0;
+ *out_num_keys = n;
return 1;
}
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)
{
- int i = 0, j;
+ int i = 0;
struct cmd_bind *bind = NULL;
struct cmd_bind_section *bind_section = get_bind_section(ictx, section);
+ assert(num_keys <= MP_MAX_KEY_DOWN);
+
if (bind_section->cmd_binds) {
for (i = 0; bind_section->cmd_binds[i].cmd != NULL; i++) {
struct cmd_bind *b = &bind_section->cmd_binds[i];
- for (j = 0; b->input[j] == keys[j] && keys[j] != 0; j++)
- /* NOTHING */;
- if (keys[j] == 0 && b->input[j] == 0 && b->is_builtin == builtin) {
+ if (bind_matches_key(b, num_keys, keys) && b->is_builtin == builtin) {
bind = b;
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->owner = bind_section;
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
@@ -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,
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;
char *cur_loc = NULL;
@@ -1822,7 +1823,9 @@ static int parse_config(struct input_ctx *ictx, bool builtin, bstr data,
continue;
}
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);
mp_tmsg(MSGT_INPUT, MSGL_ERR,
"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++;
// Print warnings if invalid commands are encountered.