From 903357395b231f2a76da0cdafc5eb0855c5b8145 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 29 Mar 2016 08:48:47 -0400 Subject: [PATCH] core/audio: Save current track in settings database The settings code is designed to map strings to unsigned integers, which is exactly what we do here. This lets us cut out an extra file access, which is always a plus. We remove the audio file after upgrading to prevent reading it multiple times. Signed-off-by: Anna Schumaker --- core/audio.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/core/audio.c b/core/audio.c index 4ec6cc3a..d8966efd 100644 --- a/core/audio.c +++ b/core/audio.c @@ -4,8 +4,10 @@ #include #include #include +#include #include +static const char *SETTINGS_TRACK = "core.audio.cur"; static struct file audio_file = FILE_INIT("cur_track", 0, 0); static struct track *audio_track = NULL; static GstElement *audio_player = NULL; @@ -16,9 +18,7 @@ static guint audio_bus = 0; static void __audio_save() { - file_open(&audio_file, OPEN_WRITE); - file_writef(&audio_file, "%u\n", track_index(audio_track)); - file_close(&audio_file); + settings_set(SETTINGS_TRACK, track_index(audio_track)); } static bool __audio_change_state(GstState state) @@ -96,9 +96,13 @@ static bool __audio_init_idle(void *data) { unsigned int track; - if (file_open(&audio_file, OPEN_READ)) { + if (settings_has(SETTINGS_TRACK)) { + track = settings_get(SETTINGS_TRACK); + __audio_load(track_get(track), GST_STATE_PAUSED); + } else if (file_open(&audio_file, OPEN_READ)) { file_readf(&audio_file, "%u", &track); file_close(&audio_file); + file_remove(&audio_file); __audio_load(track_get(track), GST_STATE_PAUSED); } return true;