From 111ec1154d8ffa9261c514ffa75832453df27287 Mon Sep 17 00:00:00 2001 From: "Geoffrey D. Bennett" Date: Sun, 31 Mar 2024 03:22:38 +1030 Subject: [PATCH] Add support for volatile buttons to widget-boolean.c Will be used by Gen 1 support. --- src/alsa-sim.c | 2 +- src/alsa.c | 17 ++++++++++++++++- src/alsa.h | 4 +++- src/widget-boolean.c | 15 +++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/alsa-sim.c b/src/alsa-sim.c index f965657..dbcdf2c 100644 --- a/src/alsa-sim.c +++ b/src/alsa-sim.c @@ -133,7 +133,7 @@ static void alsa_parse_comment_node( if (err < 0) fatal_alsa_error("snd_config_get_string error", err); if (strstr(access, "write")) - elem->writable = 1; + elem->is_writable = 1; } else if (strcmp(key, "type") == 0) { if (type != SND_CONFIG_TYPE_STRING) { printf("type type not string\n"); diff --git a/src/alsa.c b/src/alsa.c index 94bcb55..2907813 100644 --- a/src/alsa.c +++ b/src/alsa.c @@ -262,7 +262,7 @@ void alsa_set_elem_value(struct alsa_elem *elem, long value) { // return whether the element can be modified (is writable) int alsa_get_elem_writable(struct alsa_elem *elem) { if (elem->card->num == SIMULATED_CARD_NUM) - return elem->writable; + return elem->is_writable; snd_ctl_elem_info_t *elem_info; @@ -273,6 +273,21 @@ int alsa_get_elem_writable(struct alsa_elem *elem) { return snd_ctl_elem_info_is_writable(elem_info); } +// return whether the element is volatile (can change without +// notification) +int alsa_get_elem_volatile(struct alsa_elem *elem) { + if (elem->card->num == SIMULATED_CARD_NUM) + return elem->is_volatile; + + snd_ctl_elem_info_t *elem_info; + + snd_ctl_elem_info_alloca(&elem_info); + snd_ctl_elem_info_set_numid(elem_info, elem->numid); + snd_ctl_elem_info(elem->card->handle, elem_info); + + return snd_ctl_elem_info_is_volatile(elem_info); +} + // get the number of values this element has // (most are just 1; the levels element is the exception) int alsa_get_elem_count(struct alsa_elem *elem) { diff --git a/src/alsa.h b/src/alsa.h index 688311d..9ba805c 100644 --- a/src/alsa.h +++ b/src/alsa.h @@ -141,7 +141,8 @@ struct alsa_elem { GList *callbacks; // for simulated elements, the current state - int writable; + int is_writable; + int is_volatile; long value; // for simulated enumerated elements, the items @@ -219,6 +220,7 @@ long alsa_get_elem_value(struct alsa_elem *elem); int *alsa_get_elem_int_values(struct alsa_elem *elem); void alsa_set_elem_value(struct alsa_elem *elem, long value); int alsa_get_elem_writable(struct alsa_elem *elem); +int alsa_get_elem_volatile(struct alsa_elem *elem); int alsa_get_elem_count(struct alsa_elem *elem); int alsa_get_item_count(struct alsa_elem *elem); char *alsa_get_item_name(struct alsa_elem *elem, int i); diff --git a/src/widget-boolean.c b/src/widget-boolean.c index cbacd49..16ea029 100644 --- a/src/widget-boolean.c +++ b/src/widget-boolean.c @@ -6,6 +6,7 @@ struct boolean { struct alsa_elem *elem; GtkWidget *button; + guint source; const char *text[2]; }; @@ -42,7 +43,16 @@ static void toggle_button_updated( toggle_button_set_text(data->button, data->text[value]); } +static gboolean update_toggle_button(struct boolean *data) { + toggle_button_updated(data->elem, data); + + return G_SOURCE_CONTINUE; +} + static void on_destroy(struct boolean *data) { + if (data->source) + g_source_remove(data->source); + g_free(data); } @@ -82,6 +92,11 @@ GtkWidget *make_boolean_alsa_elem( toggle_button_updated(elem, data); + // periodically update volatile controls + if (alsa_get_elem_volatile(elem)) + data->source = + g_timeout_add_seconds(1, (GSourceFunc)update_toggle_button, data); + g_object_weak_ref(G_OBJECT(data->button), (GWeakNotify)on_destroy, data); return data->button;