From c624374cc8e312071c3d4d79e321265e087482ab Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Date: Wed, 8 Mar 2023 16:53:38 +0100 Subject: [PATCH] Allow mux to specify the default value Will be written to the output when the selection function returns NULL as the selected input. --- src/emu/mux.c | 13 +++++++++++-- src/emu/mux.h | 3 +++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/emu/mux.c b/src/emu/mux.c index 2da80db..db015cf 100644 --- a/src/emu/mux.c +++ b/src/emu/mux.c @@ -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; +} diff --git a/src/emu/mux.h b/src/emu/mux.h index d9735a7..b023356 100644 --- a/src/emu/mux.h +++ b/src/emu/mux.h @@ -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 */