From f9141fc36f18b9e315f870a39e675b31de7b2722 Mon Sep 17 00:00:00 2001 From: wm4 Date: Wed, 18 Jan 2012 02:35:40 +0100 Subject: input: simplify previous commit a bit more Less mutable state = better. --- input/input.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/input/input.c b/input/input.c index c81a9f1d16..f97487c1f7 100644 --- a/input/input.c +++ b/input/input.c @@ -571,8 +571,6 @@ struct cmd_bind_section { struct cmd_queue { struct mp_cmd *first; - int num_cmds; - int num_abort_cmds; }; struct input_ctx { @@ -692,17 +690,21 @@ static bool is_abort_cmd(int cmd_id) return false; } -// members updated by this should be replaced by functions -static void queue_internal_update(struct cmd_queue *queue) +static int queue_count_cmds(struct cmd_queue *queue) { - queue->num_cmds = 0; - queue->num_abort_cmds = 0; - struct mp_cmd *cmd = queue->first; - while (cmd) { - queue->num_cmds++; - queue->num_abort_cmds += is_abort_cmd(cmd->id); - cmd = cmd->queue_next; + int res = 0; + for (struct mp_cmd *cmd = queue->first; cmd; cmd = cmd->queue_next) + res++; + return res; +} + +static bool queue_has_abort_cmds(struct cmd_queue *queue) +{ + for (struct mp_cmd *cmd = queue->first; cmd; cmd = cmd->queue_next) { + if (is_abort_cmd(cmd->id)) + return true; } + return false; } static void queue_remove(struct cmd_queue *queue, struct mp_cmd *cmd) @@ -714,7 +716,6 @@ static void queue_remove(struct cmd_queue *queue, struct mp_cmd *cmd) // if this fails, cmd was not in the queue assert(*p_prev == cmd); *p_prev = cmd->queue_next; - queue_internal_update(queue); } static void queue_pop(struct cmd_queue *queue) @@ -735,7 +736,6 @@ static void queue_add(struct cmd_queue *queue, struct mp_cmd *cmd, *p_prev = cmd; cmd->queue_next = NULL; } - queue_internal_update(queue); } int mp_input_add_cmd_fd(struct input_ctx *ictx, int fd, int select, @@ -1296,8 +1296,8 @@ void mp_input_feed_key(struct input_ctx *ictx, int code) if (!cmd) return; struct cmd_queue *queue = &ictx->key_cmd_queue; - if (queue->num_cmds >= ictx->key_fifo_size && - (!is_abort_cmd(cmd->id) || queue->num_abort_cmds)) + if (queue_count_cmds(queue) >= ictx->key_fifo_size && + (!is_abort_cmd(cmd->id) || queue_has_abort_cmds(queue))) return; queue_add(queue, cmd, false); } @@ -1448,14 +1448,14 @@ mp_cmd_t *mp_input_get_cmd(struct input_ctx *ictx, int time, int peek_only) if (async_quit_request) return mp_input_parse_cmd("quit 1"); - if (ictx->control_cmd_queue.num_cmds || ictx->key_cmd_queue.num_cmds) + if (ictx->control_cmd_queue.first || ictx->key_cmd_queue.first) time = 0; read_all_events(ictx, time); struct mp_cmd *ret; struct cmd_queue *queue = &ictx->control_cmd_queue; - if (!queue->num_cmds) + if (!queue->first) queue = &ictx->key_cmd_queue; - if (!queue->num_cmds) { + if (!queue->first) { ret = check_autorepeat(ictx); if (!ret) return NULL; @@ -1881,8 +1881,8 @@ static int print_cmd_list(m_option_t *cfg) int mp_input_check_interrupt(struct input_ctx *ictx, int time) { for (int i = 0; ; i++) { - if (async_quit_request || ictx->key_cmd_queue.num_abort_cmds || - ictx->control_cmd_queue.num_abort_cmds) { + if (async_quit_request || queue_has_abort_cmds(&ictx->key_cmd_queue) || + queue_has_abort_cmds(&ictx->control_cmd_queue)) { mp_tmsg(MSGT_INPUT, MSGL_WARN, "Received command to move to " "another file. Aborting current processing.\n"); return true; -- cgit v1.2.3