2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-01-02 19:52:57 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2006-07-31 01:32:47 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2006-07-31 01:32:47 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-02 19:52:57 +01:00
|
|
|
#include "StateFile.hxx"
|
|
|
|
#include "OutputState.hxx"
|
2013-01-04 20:50:00 +01:00
|
|
|
#include "Playlist.hxx"
|
2013-01-02 19:52:57 +01:00
|
|
|
#include "PlaylistState.hxx"
|
2013-01-03 10:12:41 +01:00
|
|
|
#include "TextFile.hxx"
|
2013-01-02 19:52:57 +01:00
|
|
|
|
|
|
|
extern "C" {
|
2006-07-31 01:32:54 +02:00
|
|
|
#include "volume.h"
|
2013-01-02 19:52:57 +01:00
|
|
|
}
|
2008-10-08 10:49:29 +02:00
|
|
|
|
2008-12-02 02:22:43 +01:00
|
|
|
#include <glib.h>
|
2009-01-18 18:10:15 +01:00
|
|
|
#include <assert.h>
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <string.h>
|
2009-01-03 14:53:29 +01:00
|
|
|
#include <errno.h>
|
2006-07-31 01:32:47 +02:00
|
|
|
|
2008-12-29 17:29:23 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "state_file"
|
|
|
|
|
2009-01-18 18:09:50 +01:00
|
|
|
static char *state_file_path;
|
2006-07-31 01:32:47 +02:00
|
|
|
|
2009-01-18 18:10:15 +01:00
|
|
|
/** the GLib source id for the save timer */
|
|
|
|
static guint save_state_source_id;
|
|
|
|
|
2009-10-08 15:22:39 +02:00
|
|
|
/**
|
|
|
|
* These version numbers determine whether we need to save the state
|
|
|
|
* file. If nothing has changed, we won't let the hard drive spin up.
|
|
|
|
*/
|
|
|
|
static unsigned prev_volume_version, prev_output_version,
|
|
|
|
prev_playlist_version;
|
|
|
|
|
2009-01-18 18:10:15 +01:00
|
|
|
static void
|
2009-11-03 21:08:48 +01:00
|
|
|
state_file_write(struct player_control *pc)
|
2006-07-31 01:32:47 +02:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
2009-07-15 14:32:29 +02:00
|
|
|
assert(state_file_path != NULL);
|
2009-01-18 18:09:50 +01:00
|
|
|
|
2009-07-15 14:29:30 +02:00
|
|
|
g_debug("Saving state file %s", state_file_path);
|
|
|
|
|
2009-01-18 18:09:50 +01:00
|
|
|
fp = fopen(state_file_path, "w");
|
2008-12-02 02:22:43 +01:00
|
|
|
if (G_UNLIKELY(!fp)) {
|
2008-12-29 17:29:23 +01:00
|
|
|
g_warning("failed to create %s: %s",
|
2012-03-06 22:06:08 +01:00
|
|
|
state_file_path, g_strerror(errno));
|
2006-07-31 01:32:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-14 21:15:12 +02:00
|
|
|
save_sw_volume_state(fp);
|
2009-10-06 10:30:10 +02:00
|
|
|
audio_output_state_save(fp);
|
2009-11-03 21:08:48 +01:00
|
|
|
playlist_state_save(fp, &g_playlist, pc);
|
2006-07-31 01:32:47 +02:00
|
|
|
|
2010-07-25 11:09:13 +02:00
|
|
|
fclose(fp);
|
2009-10-08 15:22:39 +02:00
|
|
|
|
|
|
|
prev_volume_version = sw_volume_state_get_hash();
|
|
|
|
prev_output_version = audio_output_state_get_version();
|
2009-11-03 21:08:48 +01:00
|
|
|
prev_playlist_version = playlist_state_get_hash(&g_playlist, pc);
|
2006-07-31 01:32:47 +02:00
|
|
|
}
|
|
|
|
|
2009-01-18 18:09:50 +01:00
|
|
|
static void
|
2009-11-03 21:08:48 +01:00
|
|
|
state_file_read(struct player_control *pc)
|
2006-07-31 01:32:47 +02:00
|
|
|
{
|
2009-07-15 16:57:37 +02:00
|
|
|
bool success;
|
2006-07-31 01:32:47 +02:00
|
|
|
|
2009-01-18 18:09:50 +01:00
|
|
|
assert(state_file_path != NULL);
|
2006-07-31 01:32:47 +02:00
|
|
|
|
2009-07-15 14:29:30 +02:00
|
|
|
g_debug("Loading state file %s", state_file_path);
|
2009-01-18 18:10:15 +01:00
|
|
|
|
2013-01-03 10:16:05 +01:00
|
|
|
TextFile file(state_file_path);
|
|
|
|
if (file.HasFailed()) {
|
2009-01-03 14:53:23 +01:00
|
|
|
g_warning("failed to open %s: %s",
|
2012-03-06 22:06:08 +01:00
|
|
|
state_file_path, g_strerror(errno));
|
2009-01-03 14:53:23 +01:00
|
|
|
return;
|
2006-07-31 01:32:47 +02:00
|
|
|
}
|
2009-07-14 21:15:12 +02:00
|
|
|
|
2010-07-25 11:01:05 +02:00
|
|
|
const char *line;
|
2013-01-03 10:16:05 +01:00
|
|
|
while ((line = file.ReadLine()) != NULL) {
|
2009-07-15 16:57:37 +02:00
|
|
|
success = read_sw_volume_state(line) ||
|
2009-10-06 10:30:10 +02:00
|
|
|
audio_output_state_read(line) ||
|
2013-01-03 10:16:05 +01:00
|
|
|
playlist_state_restore(line, file, &g_playlist, pc);
|
2009-07-15 16:57:37 +02:00
|
|
|
if (!success)
|
|
|
|
g_warning("Unrecognized line in state file: %s", line);
|
|
|
|
}
|
2006-07-31 01:32:47 +02:00
|
|
|
|
2009-10-08 15:22:39 +02:00
|
|
|
prev_volume_version = sw_volume_state_get_hash();
|
|
|
|
prev_output_version = audio_output_state_get_version();
|
2009-11-03 21:08:48 +01:00
|
|
|
prev_playlist_version = playlist_state_get_hash(&g_playlist, pc);
|
2006-07-31 01:32:47 +02:00
|
|
|
}
|
2009-01-18 18:09:50 +01:00
|
|
|
|
2009-01-18 18:10:15 +01:00
|
|
|
/**
|
|
|
|
* This function is called every 5 minutes by the GLib main loop, and
|
|
|
|
* saves the state file.
|
|
|
|
*/
|
|
|
|
static gboolean
|
2009-11-03 21:08:48 +01:00
|
|
|
timer_save_state_file(gpointer data)
|
2009-01-18 18:10:15 +01:00
|
|
|
{
|
2013-01-02 19:52:57 +01:00
|
|
|
struct player_control *pc = (struct player_control *)data;
|
2009-11-03 21:08:48 +01:00
|
|
|
|
2009-10-08 15:22:39 +02:00
|
|
|
if (prev_volume_version == sw_volume_state_get_hash() &&
|
|
|
|
prev_output_version == audio_output_state_get_version() &&
|
2009-11-03 21:08:48 +01:00
|
|
|
prev_playlist_version == playlist_state_get_hash(&g_playlist, pc))
|
2009-10-08 15:22:39 +02:00
|
|
|
/* nothing has changed - don't save the state file,
|
|
|
|
don't spin up the hard disk */
|
|
|
|
return true;
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
state_file_write(pc);
|
2009-01-18 18:10:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-01-18 18:09:50 +01:00
|
|
|
void
|
2009-11-03 21:08:48 +01:00
|
|
|
state_file_init(const char *path, struct player_control *pc)
|
2009-01-18 18:09:50 +01:00
|
|
|
{
|
|
|
|
assert(state_file_path == NULL);
|
|
|
|
|
|
|
|
if (path == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
state_file_path = g_strdup(path);
|
2009-11-03 21:08:48 +01:00
|
|
|
state_file_read(pc);
|
2009-01-18 18:10:15 +01:00
|
|
|
|
2009-10-13 16:12:44 +02:00
|
|
|
save_state_source_id = g_timeout_add_seconds(5 * 60,
|
|
|
|
timer_save_state_file,
|
2009-11-03 21:08:48 +01:00
|
|
|
pc);
|
2009-01-18 18:09:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-11-03 21:08:48 +01:00
|
|
|
state_file_finish(struct player_control *pc)
|
2009-01-18 18:09:50 +01:00
|
|
|
{
|
2009-07-15 14:32:29 +02:00
|
|
|
if (state_file_path == NULL)
|
|
|
|
/* no state file configured, no cleanup required */
|
|
|
|
return;
|
|
|
|
|
2009-01-18 18:10:15 +01:00
|
|
|
if (save_state_source_id != 0)
|
|
|
|
g_source_remove(save_state_source_id);
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
state_file_write(pc);
|
2009-01-18 18:09:50 +01:00
|
|
|
|
|
|
|
g_free(state_file_path);
|
|
|
|
}
|