From 7bbfc8aeafb2bd75a31da8b459d6515b0af80c8c Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Sat, 11 Jan 2014 16:02:00 +0100 Subject: [WIP] Allow source command to obtain default value --- src/pjctl.c | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/src/pjctl.c b/src/pjctl.c index 3c2ab96..293bcd1 100644 --- a/src/pjctl.c +++ b/src/pjctl.c @@ -67,6 +67,7 @@ struct queue_command { char *op, char *param); char *prefix; int (*toggle)(struct pjctl *pjctl, union pjctl_param_parse *parse); + void *user_data; struct queue_command *prev, *next; }; @@ -374,17 +375,52 @@ source_response(struct pjctl *pjctl, struct queue_command *cmd, printf("OK\n"); } +static int +source(struct pjctl *pjctl, union pjctl_param_parse *p); + +static void +switch_to_first_input_type(struct pjctl *pjctl, struct queue_command *cmd, + char *op, char *param) +{ + union pjctl_param_parse *p = cmd->user_data; + int i, num, len = strlen(param); + + if (len % 3 != 2) + return; + + for (i = 0; i < len; i+=3) { + if (param[i] == ('0' + p->source.type)) { + num = param[i+1] - '0'; + if (num < 0 || num > 9) { + return; + } + p->source.num = num; + source(pjctl, p); + free(p); + } + } +} + static int source(struct pjctl *pjctl, union pjctl_param_parse *p) { struct queue_command *cmd; - uint8_t type = p->source.type, num = p->source.num; + uint8_t type = p->source.type; + int num = p->source.num; const char *switches[] = { "rgb", "video", "digital", "storage", "net" }; cmd = calloc(1, sizeof *cmd); if (!cmd) return -1; + if (num == -1) { + cmd->response_func = switch_to_first_input_type; + cmd->user_data = p; + insert_at_head(&pjctl->queue, cmd); + + return 0; + } + if (asprintf(&cmd->command, "%%1INPT %d%d\r", type, num) < 0) return -1; cmd->response_func = source_response; @@ -702,7 +738,7 @@ static struct pjctl_command { { "power", power, P{ P_SWITCH, P_END } }, { "source", source, P{ P_ATOM("rgb", "video", "digital", "storage", "net"), - P_RANGE(1,9,1), P_END } + P_RANGE(1,9,-1), P_END } }, { "mute", avmute, P{ P_ATOM("video", "audio", "av"), P_SWITCH, P_END }}, { "status", status, NULL }, -- cgit