Debian Codegraph @

src/optional-controls.c

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2022-2025 Geoffrey D. Bennett <[email protected]>
2 // SPDX-License-Identifier: GPL-3.0-or-later
3
4 #include <alsa/asoundlib.h>
5 #include <string.h>
6
7 #include "optional-controls.h"
8 #include "optional-state.h"
9 #include "alsa.h"
10
11 // Table of optional control definitions
12 // These are controls that may or may not exist on the device
13 // If they don't exist, we create simulated elements and persist values
14 // Config key uses element name (ALSA-style naming)
15 const struct optional_control_def optional_controls[] = {
16 {
17 .alsa_name = "Name",
18 .alsa_type = SND_CTL_ELEM_TYPE_BYTES,
19 .max_size = 32,
20 .default_value = NULL,
21 .type = OPTIONAL_CONTROL_NAME
22 },
23 { NULL }
24 };
25
26 // Callback structure to pass data to save callback
27 struct optional_control_save_data {
28 struct alsa_card *card;
29 char *config_key;
30 };
31
32 // Callback when an optional control value changes
33 // Saves the new value to the state file
34 static void optional_control_changed(
35 struct alsa_elem *elem,
36 void *private
37 ) {
38 struct optional_control_save_data *data = private;
39
40 // for BYTES elements, get the string value
41 if (elem->type == SND_CTL_ELEM_TYPE_BYTES) {
42 size_t size;
43 const void *bytes = alsa_get_elem_bytes(elem, &size);
44
45 if (bytes && size > 0) {
46 // find actual string length (up to first null byte)
47 size_t str_len = strnlen((const char *)bytes, size);
48
49 // only save if valid UTF-8
50 if (str_len > 0 && g_utf8_validate((const char *)bytes, str_len, NULL)) {
51 char *str = g_strndup((const char *)bytes, str_len);
52 optional_state_save(
53 data->card, CONFIG_SECTION_CONTROLS, data->config_key, str
54 );
55 g_free(str);
56 } else {
57 optional_state_save(
58 data->card, CONFIG_SECTION_CONTROLS, data->config_key, ""
59 );
60 }
61 } else {
62 optional_state_save(
63 data->card, CONFIG_SECTION_CONTROLS, data->config_key, ""
64 );
65 }
66 }
67 }
68
69 // Get the Name element for a card
70 struct alsa_elem *optional_controls_get_name_elem(struct alsa_card *card) {
71 return get_elem_by_name(card->elems, "Name");
72 }
73
74 // Free optional control callback data
75 void optional_controls_free_callback_data(void *data) {
76 if (!data)
77 return;
78
79 struct optional_control_save_data *save_data = data;
80 g_free(save_data->config_key);
81 g_free(save_data);
82 }
83
84 // Initialise optional controls for a card
85 void optional_controls_init(struct alsa_card *card) {
86 if (!card->serial || !*card->serial) {
87 // no serial number, can't persist state
88 return;
89 }
90
91 // load existing state from [controls] section
92 GHashTable *state = optional_state_load(card, CONFIG_SECTION_CONTROLS);
93 if (!state) {
94 state = g_hash_table_new_full(
95 g_str_hash, g_str_equal, g_free, g_free
96 );
97 }
98
99 // process each optional control definition
100 for (int i = 0; optional_controls[i].alsa_name; i++) {
101 const struct optional_control_def *def = &optional_controls[i];
102
103 // check if the element already exists (real ALSA control)
104 struct alsa_elem *elem = get_elem_by_name(card->elems, def->alsa_name);
105
106 if (elem) {
107 // real element exists, use it directly without state file
108 continue;
109 }
110
111 // element doesn't exist, create simulated element
112 elem = alsa_create_optional_elem(
113 card,
114 def->alsa_name,
115 def->alsa_type,
116 def->max_size
117 );
118
119 if (!elem) {
120 fprintf(stderr, "Failed to create optional element %s\n", def->alsa_name);
121 continue;
122 }
123
124 // load value from state file (use element name as config key)
125 const char *value = g_hash_table_lookup(state, def->alsa_name);
126
127 if (!value && def->default_value)
128 value = def->default_value;
129
130 // set the initial value
131 if (value && *value) {
132 if (def->alsa_type == SND_CTL_ELEM_TYPE_BYTES)
133 alsa_set_elem_bytes(elem, value, strlen(value));
134 }
135
136 // register callback to save state on changes
137 struct optional_control_save_data *callback_data =
138 g_malloc0(sizeof(struct optional_control_save_data));
139 callback_data->card = card;
140 callback_data->config_key = g_strdup(def->alsa_name);
141
142 alsa_elem_add_callback(
143 elem, optional_control_changed, callback_data,
144 optional_controls_free_callback_data
145 );
146 }
147
148 g_hash_table_destroy(state);
149 }