diff options
Diffstat (limited to 'src/state_file.c')
-rw-r--r-- | src/state_file.c | 45 |
1 files changed, 24 insertions, 21 deletions
diff --git a/src/state_file.c b/src/state_file.c index 9c6475cc8..18548d03c 100644 --- a/src/state_file.c +++ b/src/state_file.c @@ -20,6 +20,7 @@ #include "state_file.h" #include "output_state.h" #include "playlist.h" +#include "playlist_state.h" #include "volume.h" #include <glib.h> @@ -30,15 +31,6 @@ #undef G_LOG_DOMAIN #define G_LOG_DOMAIN "state_file" -static struct _sf_cb { - void (*reader)(FILE *); - void (*writer)(FILE *); -} sf_callbacks [] = { - { read_sw_volume_state, save_sw_volume_state }, - { readAudioDevicesState, saveAudioDevicesState }, - { readPlaylistState, savePlaylistState }, -}; - static char *state_file_path; /** the GLib source id for the save timer */ @@ -47,11 +39,11 @@ static guint save_state_source_id; static void state_file_write(void) { - unsigned int i; FILE *fp; - if (state_file_path == NULL) - return; + assert(state_file_path != NULL); + + g_debug("Saving state file %s", state_file_path); fp = fopen(state_file_path, "w"); if (G_UNLIKELY(!fp)) { @@ -60,8 +52,9 @@ state_file_write(void) return; } - for (i = 0; i < G_N_ELEMENTS(sf_callbacks); i++) - sf_callbacks[i].writer(fp); + save_sw_volume_state(fp); + saveAudioDevicesState(fp); + playlist_state_save(fp, &g_playlist); while(fclose(fp) && errno == EINTR) /* nothing */; } @@ -69,12 +62,13 @@ state_file_write(void) static void state_file_read(void) { - unsigned int i; FILE *fp; + char line[1024]; + bool success; assert(state_file_path != NULL); - g_debug("Saving state file"); + g_debug("Loading state file %s", state_file_path); fp = fopen(state_file_path, "r"); if (G_UNLIKELY(!fp)) { @@ -82,9 +76,15 @@ state_file_read(void) state_file_path, strerror(errno)); return; } - for (i = 0; i < G_N_ELEMENTS(sf_callbacks); i++) { - sf_callbacks[i].reader(fp); - rewind(fp); + + while (fgets(line, sizeof(line), fp) != NULL) { + g_strchomp(line); + + success = read_sw_volume_state(line) || + readAudioDevicesState(line) || + playlist_state_restore(line, fp, &g_playlist); + if (!success) + g_warning("Unrecognized line in state file: %s", line); } while(fclose(fp) && errno == EINTR) /* nothing */; @@ -119,11 +119,14 @@ state_file_init(const char *path) void state_file_finish(void) { + if (state_file_path == NULL) + /* no state file configured, no cleanup required */ + return; + if (save_state_source_id != 0) g_source_remove(save_state_source_id); - if (state_file_path != NULL) - state_file_write(); + state_file_write(); g_free(state_file_path); } |