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 <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-03-29 08:48:47 -04:00 committed by Anna Schumaker
parent 7e00c8ed10
commit 903357395b
1 changed files with 8 additions and 4 deletions

View File

@ -4,8 +4,10 @@
#include <core/audio.h>
#include <core/idle.h>
#include <core/playlist.h>
#include <core/settings.h>
#include <core/tempq.h>
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;