state_file.c 3.99 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21
#include "state_file.h"
22
#include "output_state.h"
23
#include "playlist.h"
24
#include "playlist_state.h"
25
#include "volume.h"
26
#include "text_file.h"
27
#include "glib_compat.h"
28

29
#include <glib.h>
30
#include <assert.h>
31
#include <string.h>
32
#include <errno.h>
33

34 35 36
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "state_file"

37
static char *state_file_path;
38

39 40 41
/** the GLib source id for the save timer */
static guint save_state_source_id;

42 43 44 45 46 47 48
/**
 * 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;

49
static void
50
state_file_write(struct player_control *pc)
51 52 53
{
	FILE *fp;

54
	assert(state_file_path != NULL);
55

56 57
	g_debug("Saving state file %s", state_file_path);

58
	fp = fopen(state_file_path, "w");
59
	if (G_UNLIKELY(!fp)) {
60
		g_warning("failed to create %s: %s",
61
			  state_file_path, strerror(errno));
62 63 64
		return;
	}

65
	save_sw_volume_state(fp);
66
	audio_output_state_save(fp);
67
	playlist_state_save(fp, &g_playlist, pc);
68

69
	fclose(fp);
70 71 72

	prev_volume_version = sw_volume_state_get_hash();
	prev_output_version = audio_output_state_get_version();
73
	prev_playlist_version = playlist_state_get_hash(&g_playlist, pc);
74 75
}

76
static void
77
state_file_read(struct player_control *pc)
78 79
{
	FILE *fp;
80
	bool success;
81

82
	assert(state_file_path != NULL);
83

84
	g_debug("Loading state file %s", state_file_path);
85

86
	fp = fopen(state_file_path, "r");
87
	if (G_UNLIKELY(!fp)) {
88
		g_warning("failed to open %s: %s",
89
			  state_file_path, strerror(errno));
90
		return;
91
	}
92

93 94 95
	GString *buffer = g_string_sized_new(1024);
	const char *line;
	while ((line = read_text_line(fp, buffer)) != NULL) {
96
		success = read_sw_volume_state(line) ||
97
			audio_output_state_read(line) ||
98 99
			playlist_state_restore(line, fp, buffer,
					       &g_playlist, pc);
100 101 102
		if (!success)
			g_warning("Unrecognized line in state file: %s", line);
	}
103

104
	fclose(fp);
105 106 107

	prev_volume_version = sw_volume_state_get_hash();
	prev_output_version = audio_output_state_get_version();
108
	prev_playlist_version = playlist_state_get_hash(&g_playlist, pc);
109 110 111


	g_string_free(buffer, true);
112
}
113

114 115 116 117 118
/**
 * This function is called every 5 minutes by the GLib main loop, and
 * saves the state file.
 */
static gboolean
119
timer_save_state_file(gpointer data)
120
{
121 122
	struct player_control *pc = data;

123 124
	if (prev_volume_version == sw_volume_state_get_hash() &&
	    prev_output_version == audio_output_state_get_version() &&
125
	    prev_playlist_version == playlist_state_get_hash(&g_playlist, pc))
126 127 128 129
		/* nothing has changed - don't save the state file,
		   don't spin up the hard disk */
		return true;

130
	state_file_write(pc);
131 132 133
	return true;
}

134
void
135
state_file_init(const char *path, struct player_control *pc)
136 137 138 139 140 141 142
{
	assert(state_file_path == NULL);

	if (path == NULL)
		return;

	state_file_path = g_strdup(path);
143
	state_file_read(pc);
144

145 146
	save_state_source_id = g_timeout_add_seconds(5 * 60,
						     timer_save_state_file,
147
						     pc);
148 149 150
}

void
151
state_file_finish(struct player_control *pc)
152
{
153 154 155 156
	if (state_file_path == NULL)
		/* no state file configured, no cleanup required */
		return;

157 158 159
	if (save_state_source_id != 0)
		g_source_remove(save_state_source_id);

160
	state_file_write(pc);
161 162 163

	g_free(state_file_path);
}