Allow mux to specify the default value

Will be written to the output when the selection function returns NULL
as the selected input.
This commit is contained in:
Rodrigo Arias 2023-03-08 16:53:38 +01:00 committed by Rodrigo Arias Mallo
parent 920866d8af
commit c624374cc8
2 changed files with 14 additions and 2 deletions

View File

@ -97,13 +97,15 @@ cb_select(struct chan *sel_chan, void *ptr)
value_str(sel_value), input->chan->name);
}
/* Set to null by default */
struct value out_value = value_null();
struct value out_value = mux->def;
if (input != NULL && chan_read(input->chan, &out_value) != 0) {
err("chan_read() failed");
return -1;
}
dbg("setting output chan %s to %s",
mux->output->name, value_str(out_value, buf));
if (chan_set(mux->output, out_value) != 0) {
err("chan_set() failed");
return -1;
@ -180,6 +182,7 @@ mux_init(struct mux *mux,
mux->output = output;
mux->ninputs = ninputs;
mux->inputs = calloc(ninputs, sizeof(struct mux_input));
mux->def = value_null();
if (mux->inputs == NULL) {
err("calloc failed:");
@ -245,3 +248,9 @@ mux_set_input(struct mux *mux, int64_t index, struct chan *chan)
return 0;
}
void
mux_set_default(struct mux *mux, struct value def)
{
mux->def = def;
}

View File

@ -31,6 +31,7 @@ struct mux {
mux_select_func_t select_func;
struct chan *select;
struct chan *output;
struct value def;
};
void mux_input_init(struct mux_input *mux,
@ -54,4 +55,6 @@ USE_RET struct mux_input *mux_get_input(struct mux *mux,
USE_RET int mux_register(struct mux *mux,
struct bay *bay);
void mux_set_default(struct mux *mux, struct value def);
#endif /* MUX_H */