Debian Codegraph @

src/optional-state.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 <glib.h>
5 #include <glib/gstdio.h>
6
7 #include "optional-state.h"
8
9 // Debounce delay in milliseconds
10 #define SAVE_DEBOUNCE_MS 100
11
12 // Pending save entry with section and key
13 struct pending_entry {
14 char *section;
15 char *key;
16 char *value;
17 };
18
19 // Pending saves: serial -> GArray of pending_entry
20 static GHashTable *pending_saves = NULL;
21 static guint save_timeout_id = 0;
22
23 // Get the config directory path
24 // Returns newly allocated string that must be freed with g_free()
25 static char *get_config_dir(void) {
26 const char *config_home = g_get_user_config_dir();
27 return g_build_filename(config_home, "alsa-scarlett-gui", NULL);
28 }
29
30 // Get the state file path for a given serial number
31 static char *get_state_path(const char *serial) {
32 char *config_dir = get_config_dir();
33 char *filename = g_strdup_printf("%s.conf", serial);
34 char *path = g_build_filename(config_dir, filename, NULL);
35
36 g_free(config_dir);
37 g_free(filename);
38
39 return path;
40 }
41
42 // Create the config directory if it doesn't exist
43 // Returns 0 on success, -1 on error
44 static int ensure_config_dir(void) {
45 char *config_dir = get_config_dir();
46 int ret = 0;
47
48 if (g_mkdir_with_parents(config_dir, 0755) < 0) {
49 g_warning("Failed to create config directory: %s", config_dir);
50 ret = -1;
51 }
52
53 g_free(config_dir);
54 return ret;
55 }
56
57 // Free a pending entry
58 static void free_pending_entry(struct pending_entry *entry) {
59 g_free(entry->section);
60 g_free(entry->key);
61 g_free(entry->value);
62 g_free(entry);
63 }
64
65 // Load optional controls from state file using GKeyFile
66 GHashTable *optional_state_load(struct alsa_card *card, const char *section) {
67 if (!card || !card->serial || !*card->serial || !section)
68 return NULL;
69
70 char *path = get_state_path(card->serial);
71 GKeyFile *key_file = g_key_file_new();
72 GError *error = NULL;
73
74 // try to load the file
75 if (!g_key_file_load_from_file(key_file, path, G_KEY_FILE_NONE, &error)) {
76 // file doesn't exist or can't be read - not an error
77 g_key_file_free(key_file);
78 g_free(path);
79 if (error)
80 g_error_free(error);
81 return NULL;
82 }
83
84 g_free(path);
85
86 // create hash table for values
87 GHashTable *values = g_hash_table_new_full(
88 g_str_hash, g_str_equal, g_free, g_free
89 );
90
91 // get all keys from the specified section
92 gsize num_keys;
93 gchar **keys = g_key_file_get_keys(key_file, section, &num_keys, &error);
94
95 if (!keys) {
96 if (error)
97 g_error_free(error);
98 g_key_file_free(key_file);
99 return values;
100 }
101
102 // read each key-value pair
103 for (gsize i = 0; i < num_keys; i++) {
104 gchar *value = g_key_file_get_string(key_file, section, keys[i], NULL);
105
106 if (value)
107 g_hash_table_insert(values, g_strdup(keys[i]), value);
108 }
109
110 g_strfreev(keys);
111 g_key_file_free(key_file);
112
113 return values;
114 }
115
116 // Flush all pending saves to disk
117 static gboolean flush_pending_saves(gpointer user_data) {
118 save_timeout_id = 0;
119
120 if (!pending_saves)
121 return G_SOURCE_REMOVE;
122
123 if (ensure_config_dir() < 0)
124 return G_SOURCE_REMOVE;
125
126 // iterate over each serial with pending saves
127 GHashTableIter serial_iter;
128 gpointer serial_key, serial_value;
129
130 g_hash_table_iter_init(&serial_iter, pending_saves);
131 while (g_hash_table_iter_next(&serial_iter, &serial_key, &serial_value)) {
132 const char *serial = serial_key;
133 GArray *entries = serial_value;
134
135 char *path = get_state_path(serial);
136 GKeyFile *key_file = g_key_file_new();
137 GError *error = NULL;
138
139 // load existing file if it exists
140 g_key_file_load_from_file(key_file, path, G_KEY_FILE_NONE, NULL);
141
142 // apply all pending changes for this serial
143 for (guint i = 0; i < entries->len; i++) {
144 struct pending_entry *entry = g_array_index(entries, struct pending_entry *, i);
145
146 g_key_file_set_string(
147 key_file,
148 entry->section,
149 entry->key,
150 entry->value ? entry->value : ""
151 );
152 }
153
154 // save to file
155 if (!g_key_file_save_to_file(key_file, path, &error)) {
156 g_warning("Failed to save state file %s: %s", path, error->message);
157 g_error_free(error);
158 }
159
160 g_key_file_free(key_file);
161 g_free(path);
162 }
163
164 // clear all pending saves
165 g_hash_table_remove_all(pending_saves);
166
167 return G_SOURCE_REMOVE;
168 }
169
170 // Free array of pending entries
171 static void free_pending_entries(GArray *entries) {
172 for (guint i = 0; i < entries->len; i++) {
173 struct pending_entry *entry = g_array_index(entries, struct pending_entry *, i);
174 free_pending_entry(entry);
175 }
176 g_array_free(entries, TRUE);
177 }
178
179 // Remove the state file for a given serial
180 int optional_state_remove(const char *serial) {
181 if (!serial || !*serial)
182 return -1;
183
184 char *path = get_state_path(serial);
185 int ret = 0;
186
187 if (g_file_test(path, G_FILE_TEST_EXISTS)) {
188 if (g_unlink(path) < 0) {
189 g_warning("Failed to remove state file: %s", path);
190 ret = -1;
191 }
192 }
193
194 g_free(path);
195 return ret;
196 }
197
198 // Save optional control state to file using GKeyFile (debounced)
199 int optional_state_save(
200 struct alsa_card *card,
201 const char *section,
202 const char *key,
203 const char *value
204 ) {
205 if (!card || !card->serial || !*card->serial || !section || !key || !*key)
206 return -1;
207
208 const char *serial = card->serial;
209
210 // initialise pending_saves hash table if needed
211 if (!pending_saves) {
212 pending_saves = g_hash_table_new_full(
213 g_str_hash, g_str_equal,
214 g_free,
215 (GDestroyNotify)free_pending_entries
216 );
217 }
218
219 // get or create the array for this serial
220 GArray *entries = g_hash_table_lookup(pending_saves, serial);
221 if (!entries) {
222 entries = g_array_new(FALSE, FALSE, sizeof(struct pending_entry *));
223 g_hash_table_insert(pending_saves, g_strdup(serial), entries);
224 }
225
226 // create new pending entry
227 struct pending_entry *entry = g_malloc(sizeof(struct pending_entry));
228 entry->section = g_strdup(section);
229 entry->key = g_strdup(key);
230 entry->value = g_strdup(value ? value : "");
231
232 g_array_append_val(entries, entry);
233
234 // also ensure [device] section has serial and model
235 // (add these first time we save anything for this serial)
236 static GHashTable *device_section_written = NULL;
237 if (!device_section_written) {
238 device_section_written = g_hash_table_new_full(
239 g_str_hash, g_str_equal, g_free, NULL
240 );
241 }
242
243 if (!g_hash_table_contains(device_section_written, serial)) {
244 g_hash_table_add(device_section_written, g_strdup(serial));
245
246 struct pending_entry *serial_entry = g_malloc(sizeof(struct pending_entry));
247 serial_entry->section = g_strdup(CONFIG_SECTION_DEVICE);
248 serial_entry->key = g_strdup("serial");
249 serial_entry->value = g_strdup(serial);
250 g_array_append_val(entries, serial_entry);
251
252 if (card->name) {
253 struct pending_entry *model_entry = g_malloc(sizeof(struct pending_entry));
254 model_entry->section = g_strdup(CONFIG_SECTION_DEVICE);
255 model_entry->key = g_strdup("model");
256 model_entry->value = g_strdup(card->name);
257 g_array_append_val(entries, model_entry);
258 }
259 }
260
261 // cancel existing timeout if any
262 if (save_timeout_id)
263 g_source_remove(save_timeout_id);
264
265 // schedule new timeout
266 save_timeout_id = g_timeout_add(
267 SAVE_DEBOUNCE_MS,
268 flush_pending_saves,
269 NULL
270 );
271
272 return 0;
273 }