1
0
mirror of https://github.com/mpv-player/mpv synced 2025-03-25 04:38:01 +00:00

input: simplify previous commit a bit more

Less mutable state = better.
This commit is contained in:
wm4 2012-01-18 02:35:40 +01:00
parent 98203198b6
commit f9141fc36f

View File

@ -571,8 +571,6 @@ struct cmd_bind_section {
struct cmd_queue { struct cmd_queue {
struct mp_cmd *first; struct mp_cmd *first;
int num_cmds;
int num_abort_cmds;
}; };
struct input_ctx { struct input_ctx {
@ -692,17 +690,21 @@ static bool is_abort_cmd(int cmd_id)
return false; return false;
} }
// members updated by this should be replaced by functions static int queue_count_cmds(struct cmd_queue *queue)
static void queue_internal_update(struct cmd_queue *queue)
{ {
queue->num_cmds = 0; int res = 0;
queue->num_abort_cmds = 0; for (struct mp_cmd *cmd = queue->first; cmd; cmd = cmd->queue_next)
struct mp_cmd *cmd = queue->first; res++;
while (cmd) { return res;
queue->num_cmds++; }
queue->num_abort_cmds += is_abort_cmd(cmd->id);
cmd = cmd->queue_next; 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) 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 // if this fails, cmd was not in the queue
assert(*p_prev == cmd); assert(*p_prev == cmd);
*p_prev = cmd->queue_next; *p_prev = cmd->queue_next;
queue_internal_update(queue);
} }
static void queue_pop(struct cmd_queue *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; *p_prev = cmd;
cmd->queue_next = NULL; cmd->queue_next = NULL;
} }
queue_internal_update(queue);
} }
int mp_input_add_cmd_fd(struct input_ctx *ictx, int fd, int select, 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) if (!cmd)
return; return;
struct cmd_queue *queue = &ictx->key_cmd_queue; struct cmd_queue *queue = &ictx->key_cmd_queue;
if (queue->num_cmds >= ictx->key_fifo_size && if (queue_count_cmds(queue) >= ictx->key_fifo_size &&
(!is_abort_cmd(cmd->id) || queue->num_abort_cmds)) (!is_abort_cmd(cmd->id) || queue_has_abort_cmds(queue)))
return; return;
queue_add(queue, cmd, false); 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) if (async_quit_request)
return mp_input_parse_cmd("quit 1"); 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; time = 0;
read_all_events(ictx, time); read_all_events(ictx, time);
struct mp_cmd *ret; struct mp_cmd *ret;
struct cmd_queue *queue = &ictx->control_cmd_queue; struct cmd_queue *queue = &ictx->control_cmd_queue;
if (!queue->num_cmds) if (!queue->first)
queue = &ictx->key_cmd_queue; queue = &ictx->key_cmd_queue;
if (!queue->num_cmds) { if (!queue->first) {
ret = check_autorepeat(ictx); ret = check_autorepeat(ictx);
if (!ret) if (!ret)
return NULL; 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) int mp_input_check_interrupt(struct input_ctx *ictx, int time)
{ {
for (int i = 0; ; i++) { for (int i = 0; ; i++) {
if (async_quit_request || ictx->key_cmd_queue.num_abort_cmds || if (async_quit_request || queue_has_abort_cmds(&ictx->key_cmd_queue) ||
ictx->control_cmd_queue.num_abort_cmds) { queue_has_abort_cmds(&ictx->control_cmd_queue)) {
mp_tmsg(MSGT_INPUT, MSGL_WARN, "Received command to move to " mp_tmsg(MSGT_INPUT, MSGL_WARN, "Received command to move to "
"another file. Aborting current processing.\n"); "another file. Aborting current processing.\n");
return true; return true;