summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--switchctl.c119
1 files changed, 61 insertions, 58 deletions
diff --git a/switchctl.c b/switchctl.c
index 4896f15..d18883b 100644
--- a/switchctl.c
+++ b/switchctl.c
@@ -401,6 +401,65 @@ void process_cmd_type(const char* param, int fd, client_t* client_lst)
}
}
+int change_mode(switchctl_mode_t old_mode, int fd, cmd_t **cmd_q, options_t* opt, client_t* client_lst)
+{
+ char* ch_from = NULL;
+ if(old_mode == MODE_MASTER && state_.channel_master_ == CHAN_MAIN)
+ ch_from = "master_main";
+ else if(old_mode == MODE_MASTER && state_.channel_master_ == CHAN_MUSIC)
+ ch_from = "master_music";
+ else if(old_mode == MODE_STANDBY && state_.channel_standby_ == CHAN_MAIN)
+ ch_from = "standby_main";
+ else if(old_mode == MODE_STANDBY && state_.channel_standby_ == CHAN_MUSIC)
+ ch_from = "standby_music";
+ else {
+ state_.mode_ = old_mode;
+ log_printf(ERROR, "EEE: mode: old config is illegal?!");
+ return 0;
+ }
+
+ char* ch_to = NULL;
+ if(state_.mode_ == MODE_MASTER && state_.channel_master_ == CHAN_MAIN)
+ ch_to = "master_main";
+ else if(state_.mode_ == MODE_MASTER && state_.channel_master_ == CHAN_MUSIC)
+ ch_to = "master_music";
+ else if(state_.mode_ == MODE_STANDBY && state_.channel_standby_ == CHAN_MAIN)
+ ch_to = "standby_main";
+ else if(state_.mode_ == MODE_STANDBY && state_.channel_standby_ == CHAN_MUSIC)
+ ch_to = "standby_music";
+ else {
+ 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) {
+ state_.mode_ = old_mode;
+ if(ret > 0)
+ return 0;
+
+ return ret;
+ }
+
+ char* mode_str;
+ 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;
+ int listener_cnt = 0;
+ for(client = client_lst; client; client = client->next)
+ if(client->mode_listener && client->fd != fd) {
+ send_response(client->fd, mode_str);
+ listener_cnt++;
+ }
+ free(mode_str);
+ log_printf(DEBUG, "sent new mode to %d additional listeners", listener_cnt);
+ }
+
+ return 0;
+}
+
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 = state_.mode_;
@@ -417,70 +476,14 @@ 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 != state_.mode_) {
- char* ch_from = NULL;
- if(old_mode == MODE_MASTER && state_.channel_master_ == CHAN_MAIN)
- ch_from = "master_main";
- else if(old_mode == MODE_MASTER && state_.channel_master_ == CHAN_MUSIC)
- ch_from = "master_music";
- else if(old_mode == MODE_STANDBY && state_.channel_standby_ == CHAN_MAIN)
- ch_from = "standby_main";
- else if(old_mode == MODE_STANDBY && state_.channel_standby_ == CHAN_MUSIC)
- ch_from = "standby_music";
- else {
- state_.mode_ = old_mode;
- log_printf(ERROR, "EEE: mode: old config is illegal?!");
- return 0;
- }
-
- char* ch_to = NULL;
- if(state_.mode_ == MODE_MASTER && state_.channel_master_ == CHAN_MAIN)
- ch_to = "master_main";
- else if(state_.mode_ == MODE_MASTER && state_.channel_master_ == CHAN_MUSIC)
- ch_to = "master_music";
- else if(state_.mode_ == MODE_STANDBY && state_.channel_standby_ == CHAN_MAIN)
- ch_to = "standby_main";
- else if(state_.mode_ == MODE_STANDBY && state_.channel_standby_ == CHAN_MUSIC)
- ch_to = "standby_music";
- else {
- 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) {
- state_.mode_ = old_mode;
- if(ret > 0)
- return 0;
-
- return ret;
- }
- }
+ if(old_mode != state_.mode_)
+ return change_mode(old_mode, fd, cmd_q, opt, client_lst);
}
else {
log_printf(ERROR, "unable to set mode: empty parameter");
send_response(fd, "EEE: mode: missing parameter");
}
- if(old_mode != state_.mode_) {
- char* mode_str;
- 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;
- int listener_cnt = 0;
- for(client = client_lst; client; client = client->next)
- if(client->mode_listener && client->fd != fd) {
- send_response(client->fd, mode_str);
- listener_cnt++;
- }
- free(mode_str);
- log_printf(DEBUG, "sent new mode to %d additional listeners", listener_cnt);
- }
-
- }
-
return 0;
}