| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <glib.h> |
| 5 |
#include <glib/gstdio.h> |
| 6 |
|
| 7 |
#include "optional-state.h" |
| 8 |
|
| 9 |
|
| 10 |
#define SAVE_DEBOUNCE_MS 100 |
| 11 |
|
| 12 |
|
| 13 |
struct pending_entry { |
| 14 |
char *section; |
| 15 |
char *key; |
| 16 |
char *value; |
| 17 |
}; |
| 18 |
|
| 19 |
|
| 20 |
static GHashTable *pending_saves = NULL; |
| 21 |
static guint save_timeout_id = 0; |
| 22 |
|
| 23 |
|
| 24 |
|
| 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 |
|
| 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 |
|
| 43 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 75 |
if (!g_key_file_load_from_file(key_file, path, G_KEY_FILE_NONE, &error)) { |
| 76 |
|
| 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 |
|
| 87 |
GHashTable *values = g_hash_table_new_full( |
| 88 |
g_str_hash, g_str_equal, g_free, g_free |
| 89 |
); |
| 90 |
|
| 91 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 140 |
g_key_file_load_from_file(key_file, path, G_KEY_FILE_NONE, NULL); |
| 141 |
|
| 142 |
|
| 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 |
|
| 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 |
|
| 165 |
g_hash_table_remove_all(pending_saves); |
| 166 |
|
| 167 |
return G_SOURCE_REMOVE; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 235 |
|
| 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 |
|
| 262 |
if (save_timeout_id) |
| 263 |
g_source_remove(save_timeout_id); |
| 264 |
|
| 265 |
|
| 266 |
save_timeout_id = g_timeout_add( |
| 267 |
SAVE_DEBOUNCE_MS, |
| 268 |
flush_pending_saves, |
| 269 |
NULL |
| 270 |
); |
| 271 |
|
| 272 |
return 0; |
| 273 |
} |