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
1 changed files with 20 additions and 20 deletions

View File

@ -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;