2009-03-13 18:43:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2009 The Music Player Daemon Project
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
2006-07-31 01:32:54 +02:00
|
|
|
#include "volume.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"
|
|
|
|
|
2006-07-31 01:32:47 +02:00
|
|
|
static struct _sf_cb {
|
|
|
|
void (*reader)(FILE *);
|
|
|
|
void (*writer)(FILE *);
|
|
|
|
} sf_callbacks [] = {
|
2006-07-31 01:32:54 +02:00
|
|
|
{ read_sw_volume_state, save_sw_volume_state },
|
2006-07-31 01:32:47 +02:00
|
|
|
{ readAudioDevicesState, saveAudioDevicesState },
|
|
|
|
{ readPlaylistState, savePlaylistState },
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
static void
|
|
|
|
state_file_write(void)
|
2006-07-31 01:32:47 +02:00
|
|
|
{
|
2008-03-26 11:38:48 +01:00
|
|
|
unsigned int i;
|
2006-07-31 01:32:47 +02:00
|
|
|
FILE *fp;
|
|
|
|
|
2009-01-18 18:09:50 +01:00
|
|
|
if (state_file_path == NULL)
|
2006-07-31 01:32:47 +02:00
|
|
|
return;
|
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-01-03 14:53:29 +01:00
|
|
|
for (i = 0; i < G_N_ELEMENTS(sf_callbacks); i++)
|
2006-07-31 01:32:47 +02:00
|
|
|
sf_callbacks[i].writer(fp);
|
|
|
|
|
|
|
|
while(fclose(fp) && errno == EINTR) /* nothing */;
|
|
|
|
}
|
|
|
|
|
2009-01-18 18:09:50 +01:00
|
|
|
static void
|
|
|
|
state_file_read(void)
|
2006-07-31 01:32:47 +02:00
|
|
|
{
|
2008-03-26 11:38:48 +01:00
|
|
|
unsigned int i;
|
2006-07-31 01:32:47 +02:00
|
|
|
FILE *fp;
|
|
|
|
|
2009-01-18 18:09:50 +01:00
|
|
|
assert(state_file_path != NULL);
|
2006-07-31 01:32:47 +02:00
|
|
|
|
2009-01-18 18:10:15 +01:00
|
|
|
g_debug("Saving state file");
|
|
|
|
|
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-01-03 14:53:29 +01:00
|
|
|
for (i = 0; i < G_N_ELEMENTS(sf_callbacks); i++) {
|
2006-07-31 01:32:47 +02:00
|
|
|
sf_callbacks[i].reader(fp);
|
|
|
|
rewind(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
while(fclose(fp) && errno == EINTR) /* nothing */;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
save_state_source_id = g_timeout_add(5 * 60 * 1000,
|
|
|
|
timer_save_state_file, NULL);
|
2009-01-18 18:09:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
state_file_finish(void)
|
|
|
|
{
|
2009-01-18 18:10:15 +01:00
|
|
|
if (save_state_source_id != 0)
|
|
|
|
g_source_remove(save_state_source_id);
|
|
|
|
|
2009-01-18 18:09:50 +01:00
|
|
|
if (state_file_path != NULL)
|
2009-01-18 18:10:15 +01:00
|
|
|
state_file_write();
|
2009-01-18 18:09:50 +01:00
|
|
|
|
|
|
|
g_free(state_file_path);
|
|
|
|
}
|