summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2014-03-20 22:41:25 (GMT)
committerChristian Pointner <equinox@helsinki.at>2014-03-20 22:41:25 (GMT)
commitb2ee1b990397735b1b537b3d942f9815bb4ab414 (patch)
tree448fd6ade7cb4c2ebfaac714ce0b982d9dc08c10
parent270ecd189fc229375871f3f2ef76934ca4d42a56 (diff)
moved mode and channels to new state struct
-rw-r--r--switchctl.c90
1 files changed, 52 insertions, 38 deletions
diff --git a/switchctl.c b/switchctl.c
index 0f6124f..2025776 100644
--- a/switchctl.c
+++ b/switchctl.c
@@ -37,6 +37,16 @@
#include "daemon.h"
#include "utils.h"
+
+struct state_struct {
+ switchctl_mode_t mode_;
+ switchctl_channel_t channel_master_;
+ switchctl_channel_t channel_standby_;
+};
+typedef struct state_struct state_t;
+
+state_t state_;
+
int send_command(int switch_fd, cmd_t* cmd)
{
if(!cmd)
@@ -112,8 +122,8 @@ int process_cmd_request(const char* cmd, cmd_id_t cmd_id, const char* param, int
char* cmd_param = NULL;
if(cmd_id == SWITCH) {
- if((opt->mode_ == MODE_MASTER && c->type == STANDBY )||
- (opt->mode_ == MODE_STANDBY && c->type == MASTER ))
+ if((state_.mode_ == MODE_MASTER && c->type == STANDBY )||
+ (state_.mode_ == MODE_STANDBY && c->type == MASTER ))
{
log_printf(INFO, "silently ignoring request from inactive system (%s)", c->type == MASTER ? "master" : "standby");
return 0;
@@ -208,11 +218,11 @@ int process_cmd_request(const char* cmd, cmd_id_t cmd_id, const char* param, int
if(cmd_id == STATUS) {
char buf[2][30];
- snprintf(buf[0], 30, "Current Mode: %s", opt->mode_ == MODE_MASTER ? "Master" : "Standby");
+ snprintf(buf[0], 30, "Current Mode: %s", state_.mode_ == MODE_MASTER ? "Master" : "Standby");
send_response(fd, buf[0]);
- snprintf(buf[1], 30, "Master Channel: %s", opt->channel_master_ == CHAN_MAIN ? "Main" : "Music");
+ snprintf(buf[1], 30, "Master Channel: %s", state_.channel_master_ == CHAN_MAIN ? "Main" : "Music");
send_response(fd, buf[1]);
- snprintf(buf[1], 30, "Standby Channel: %s", opt->channel_standby_ == CHAN_MAIN ? "Main" : "Music");
+ snprintf(buf[1], 30, "Standby Channel: %s", state_.channel_standby_ == CHAN_MAIN ? "Main" : "Music");
send_response(fd, buf[1]);
client_t* client;
int listener_cnt = 0;
@@ -285,14 +295,14 @@ int process_cmd_channel(const char* cmd, const char* param, int fd, cmd_t **cmd_
switchctl_channel_t old_channel;
if(c->type == MASTER)
- old_channel = opt->channel_master_;
+ old_channel = state_.channel_master_;
else
- old_channel = opt->channel_standby_;
+ old_channel = state_.channel_standby_;
char* ch_from = NULL;
char* ch_to = NULL;
if(!strcmp(param, "main")) {
- if(opt->mode_ == MODE_MASTER) {
+ if(state_.mode_ == MODE_MASTER) {
ch_from = "master_music";
ch_to = "master_main";
}
@@ -301,12 +311,12 @@ int process_cmd_channel(const char* cmd, const char* param, int fd, cmd_t **cmd_
ch_to = "standby_main";
}
if(c->type == MASTER)
- opt->channel_master_ = CHAN_MAIN;
+ state_.channel_master_ = CHAN_MAIN;
else
- opt->channel_standby_ = CHAN_MAIN;
+ state_.channel_standby_ = CHAN_MAIN;
}
else if(!strcmp(param, "music")) {
- if(opt->mode_ == MODE_MASTER) {
+ if(state_.mode_ == MODE_MASTER) {
ch_from = "master_main";
ch_to = "master_music";
}
@@ -315,13 +325,13 @@ int process_cmd_channel(const char* cmd, const char* param, int fd, cmd_t **cmd_
ch_to = "standby_music";
}
if(c->type == MASTER)
- opt->channel_master_ = CHAN_MUSIC;
+ state_.channel_master_ = CHAN_MUSIC;
else
- opt->channel_standby_ = CHAN_MUSIC;
+ state_.channel_standby_ = CHAN_MUSIC;
}
- if((opt->mode_ == MODE_MASTER && c->type == STANDBY )||
- (opt->mode_ == MODE_STANDBY && c->type == MASTER ))
+ if((state_.mode_ == MODE_MASTER && c->type == STANDBY )||
+ (state_.mode_ == MODE_STANDBY && c->type == MASTER ))
{
log_printf(INFO, "no crossfade for inactive system (%s), just updated channel info", c->type == MASTER ? "master" : "standby");
return 0;
@@ -330,9 +340,9 @@ int process_cmd_channel(const char* cmd, const char* param, int fd, cmd_t **cmd_
int ret = crossfade(ch_from, ch_to, fd, cmd_q, opt);
if(ret) {
if(c->type == MASTER)
- opt->channel_master_ = old_channel;
+ state_.channel_master_ = old_channel;
else
- opt->channel_standby_ = old_channel;
+ state_.channel_standby_ = old_channel;
if(ret > 0)
return 0;
@@ -388,13 +398,13 @@ void process_cmd_type(const char* param, int fd, client_t* client_lst)
int process_cmd_mode(const char* param, int fd, cmd_t **cmd_q, options_t* opt, client_t* client_lst)
{
- switchctl_mode_t old_mode = opt->mode_;
+ switchctl_mode_t old_mode = state_.mode_;
if(param) {
if(!strncmp(param, "master", 6))
- opt->mode_ = MODE_MASTER;
+ state_.mode_ = MODE_MASTER;
else if(!strncmp(param, "standby", 7))
- opt->mode_ = MODE_STANDBY;
+ state_.mode_ = MODE_STANDBY;
else {
log_printf(DEBUG, "unkown mode '%s'", param);
send_response(fd, "EEE: mode: unknown mode");
@@ -402,40 +412,40 @@ int process_cmd_mode(const char* param, int fd, cmd_t **cmd_q, options_t* opt, c
}
// swap master with standby channels only when mode has changed
- if(old_mode != opt->mode_) {
+ if(old_mode != state_.mode_) {
char* ch_from = NULL;
- if(old_mode == MODE_MASTER && opt->channel_master_ == CHAN_MAIN)
+ if(old_mode == MODE_MASTER && state_.channel_master_ == CHAN_MAIN)
ch_from = "master_main";
- else if(old_mode == MODE_MASTER && opt->channel_master_ == CHAN_MUSIC)
+ else if(old_mode == MODE_MASTER && state_.channel_master_ == CHAN_MUSIC)
ch_from = "master_music";
- else if(old_mode == MODE_STANDBY && opt->channel_standby_ == CHAN_MAIN)
+ else if(old_mode == MODE_STANDBY && state_.channel_standby_ == CHAN_MAIN)
ch_from = "standby_main";
- else if(old_mode == MODE_STANDBY && opt->channel_standby_ == CHAN_MUSIC)
+ else if(old_mode == MODE_STANDBY && state_.channel_standby_ == CHAN_MUSIC)
ch_from = "standby_music";
else {
- opt->mode_ = old_mode;
+ state_.mode_ = old_mode;
log_printf(ERROR, "EEE: mode: old config is illegal?!");
return 0;
}
char* ch_to = NULL;
- if(opt->mode_ == MODE_MASTER && opt->channel_master_ == CHAN_MAIN)
+ if(state_.mode_ == MODE_MASTER && state_.channel_master_ == CHAN_MAIN)
ch_to = "master_main";
- else if(opt->mode_ == MODE_MASTER && opt->channel_master_ == CHAN_MUSIC)
+ else if(state_.mode_ == MODE_MASTER && state_.channel_master_ == CHAN_MUSIC)
ch_to = "master_music";
- else if(opt->mode_ == MODE_STANDBY && opt->channel_standby_ == CHAN_MAIN)
+ else if(state_.mode_ == MODE_STANDBY && state_.channel_standby_ == CHAN_MAIN)
ch_to = "standby_main";
- else if(opt->mode_ == MODE_STANDBY && opt->channel_standby_ == CHAN_MUSIC)
+ else if(state_.mode_ == MODE_STANDBY && state_.channel_standby_ == CHAN_MUSIC)
ch_to = "standby_music";
else {
- opt->mode_ = old_mode;
+ state_.mode_ = old_mode;
log_printf(ERROR, "EEE: mode: current config is illegal?!");
return 0;
}
int ret = crossfade(ch_from, ch_to, fd, cmd_q, opt);
if(ret) {
- opt->mode_ = old_mode;
+ state_.mode_ = old_mode;
if(ret > 0)
return 0;
@@ -448,9 +458,9 @@ int process_cmd_mode(const char* param, int fd, cmd_t **cmd_q, options_t* opt, c
send_response(fd, "EEE: mode: missing parameter");
}
- if(old_mode != opt->mode_) {
+ if(old_mode != state_.mode_) {
char* mode_str;
- int len = asprintf(&mode_str, "new Mode: %s", opt->mode_ == MODE_MASTER ? "master" : "standby");
+ int len = asprintf(&mode_str, "new Mode: %s", state_.mode_ == MODE_MASTER ? "master" : "standby");
if(len > 0) {
log_printf(NOTICE, "%s", mode_str);
client_t* client;
@@ -771,10 +781,10 @@ int main_loop(int switch_fd, int cmd_listen_fd, options_t* opt)
int return_value = 0;
char* channel = "unknown";
- if(opt->mode_ == MODE_MASTER && opt->channel_master_ == CHAN_MAIN) channel = "master_main";
- else if(opt->mode_ == MODE_MASTER && opt->channel_master_ == CHAN_MUSIC) channel = "master_music";
- else if(opt->mode_ == MODE_STANDBY && opt->channel_standby_ == CHAN_MAIN) channel = "standby_main";
- else if(opt->mode_ == MODE_STANDBY && opt->channel_standby_ == CHAN_MUSIC) channel = "standby_music";
+ if(state_.mode_ == MODE_MASTER && state_.channel_master_ == CHAN_MAIN) channel = "master_main";
+ else if(state_.mode_ == MODE_MASTER && state_.channel_master_ == CHAN_MUSIC) channel = "master_music";
+ else if(state_.mode_ == MODE_STANDBY && state_.channel_standby_ == CHAN_MAIN) channel = "standby_main";
+ else if(state_.mode_ == MODE_STANDBY && state_.channel_standby_ == CHAN_MUSIC) channel = "standby_music";
char* cmd_param = strdup("*0M1*0ii1");
char* ch_nr = key_value_storage_find(&opt->alias_table_, channel);
@@ -979,6 +989,10 @@ int main(int argc, char* argv[])
exit(-1);
}
+ state_.mode_ = opt.mode_;
+ state_.channel_master_ = opt.channel_master_;
+ state_.channel_standby_ = opt.channel_standby_;
+
int switch_fd = 0;
for(;;) {
switch_fd = open(opt.switch_dev_, O_RDWR | O_NOCTTY);