2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2010-01-01 05:55:13 +01:00
|
|
|
* Copyright (C) 2003-2010 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"
|
2006-07-31 01:32:47 +02:00
|
|
|
#include "state_file.h"
|
2009-02-10 18:51:39 +01:00
|
|
|
#include "output_state.h"
|
2006-07-31 01:32:47 +02:00
|
|
|
#include "playlist.h"
|
2009-07-14 21:28:26 +02:00
|
|
|
#include "playlist_state.h"
|
2006-07-31 01:32:54 +02:00
|
|
|
#include "volume.h"
|
2009-11-10 21:14:22 +01:00
|
|
|
#include "glib_compat.h"
|
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
|
|
|
|
state_file_write(void)
|
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",
|
2009-01-18 18:09:50 +01:00
|
|
|
state_file_path, 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-07-14 21:28:26 +02:00
|
|
|
playlist_state_save(fp, &g_playlist);
|
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();
|
|
|
|
prev_playlist_version = playlist_state_get_hash(&g_playlist);
|
2006-07-31 01:32:47 +02:00
|
|
|
}
|
|
|
|
|
2009-01-18 18:09:50 +01:00
|
|
|
static void
|
|
|
|
state_file_read(void)
|
2006-07-31 01:32:47 +02:00
|
|
|
{
|
|
|
|
FILE *fp;
|
2009-07-15 16:57:37 +02:00
|
|
|
char line[1024];
|
|
|
|
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
|
|
|
|
2009-01-18 18:09:50 +01:00
|
|
|
fp = fopen(state_file_path, "r");
|
2008-12-02 02:22:43 +01:00
|
|
|
if (G_UNLIKELY(!fp)) {
|
2009-01-03 14:53:23 +01:00
|
|
|
g_warning("failed to open %s: %s",
|
2009-01-18 18:09:50 +01:00
|
|
|
state_file_path, 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
|
|
|
|
2009-07-15 16:57:37 +02:00
|
|
|
while (fgets(line, sizeof(line), fp) != NULL) {
|
|
|
|
g_strchomp(line);
|
|
|
|
|
|
|
|
success = read_sw_volume_state(line) ||
|
2009-10-06 10:30:10 +02:00
|
|
|
audio_output_state_read(line) ||
|
2009-07-15 16:57:37 +02:00
|
|
|
playlist_state_restore(line, fp, &g_playlist);
|
|
|
|
if (!success)
|
|
|
|
g_warning("Unrecognized line in state file: %s", line);
|
|
|
|
}
|
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();
|
|
|
|
prev_playlist_version = playlist_state_get_hash(&g_playlist);
|
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
|
|
|
|
timer_save_state_file(G_GNUC_UNUSED gpointer data)
|
|
|
|
{
|
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() &&
|
|
|
|
prev_playlist_version == playlist_state_get_hash(&g_playlist))
|
|
|
|
/* nothing has changed - don't save the state file,
|
|
|
|
don't spin up the hard disk */
|
|
|
|
return true;
|
|
|
|
|
2009-01-18 18:10:15 +01:00
|
|
|
state_file_write();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-01-18 18:09:50 +01:00
|
|
|
void
|
|
|
|
state_file_init(const char *path)
|
|
|
|
{
|
|
|
|
assert(state_file_path == NULL);
|
|
|
|
|
|
|
|
if (path == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
state_file_path = g_strdup(path);
|
|
|
|
state_file_read();
|
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,
|
|
|
|
NULL);
|
2009-01-18 18:09:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
state_file_finish(void)
|
|
|
|
{
|
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-07-15 14:32:29 +02:00
|
|
|
state_file_write();
|
2009-01-18 18:09:50 +01:00
|
|
|
|
|
|
|
g_free(state_file_path);
|
|
|
|
}
|