mixer_control.c 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 * http://www.musicpd.org
 *
 * 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 22 23
#include "mixer_control.h"
#include "mixer_api.h"

24
#include <assert.h>
25 26 27 28 29 30
#include <stddef.h>

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "mixer"

struct mixer *
31 32
mixer_new(const struct mixer_plugin *plugin, void *ao,
	  const struct config_param *param,
33
	  GError **error_r)
34 35 36 37 38
{
	struct mixer *mixer;

	assert(plugin != NULL);

39
	mixer = plugin->init(ao, param, error_r);
40 41 42 43 44 45 46 47 48

	assert(mixer == NULL || mixer->plugin == plugin);

	return mixer;
}

void
mixer_free(struct mixer *mixer)
{
49
	assert(mixer != NULL);
50
	assert(mixer->plugin != NULL);
51 52 53
	assert(mixer->mutex != NULL);

	g_mutex_free(mixer->mutex);
54 55 56 57 58

	mixer->plugin->finish(mixer);
}

bool
59
mixer_open(struct mixer *mixer, GError **error_r)
60
{
61 62
	bool success;

63
	assert(mixer != NULL);
64
	assert(mixer->plugin != NULL);
65 66

	g_mutex_lock(mixer->mutex);
67 68 69

	if (mixer->open)
		success = true;
70 71
	else if (mixer->plugin->open == NULL)
		success = mixer->open = true;
72
	else
73
		success = mixer->open = mixer->plugin->open(mixer, error_r);
74

75 76
	mixer->failed = !success;

77 78 79
	g_mutex_unlock(mixer->mutex);

	return success;
80 81
}

82 83 84 85 86 87 88
static void
mixer_close_internal(struct mixer *mixer)
{
	assert(mixer != NULL);
	assert(mixer->plugin != NULL);
	assert(mixer->open);

89 90 91
	if (mixer->plugin->close != NULL)
		mixer->plugin->close(mixer);

92 93 94
	mixer->open = false;
}

95 96 97
void
mixer_close(struct mixer *mixer)
{
98
	assert(mixer != NULL);
99
	assert(mixer->plugin != NULL);
100 101

	g_mutex_lock(mixer->mutex);
102

103 104
	if (mixer->open)
		mixer_close_internal(mixer);
105

106
	g_mutex_unlock(mixer->mutex);
107 108
}

109 110 111 112 113 114 115
void
mixer_auto_close(struct mixer *mixer)
{
	if (!mixer->plugin->global)
		mixer_close(mixer);
}

116 117 118 119 120 121 122 123 124
/*
 * Close the mixer due to failure.  The mutex must be locked before
 * calling this function.
 */
static void
mixer_failed(struct mixer *mixer)
{
	assert(mixer->open);

125
	mixer_close_internal(mixer);
126 127

	mixer->failed = true;
128 129
}

130
int
131
mixer_get_volume(struct mixer *mixer, GError **error_r)
132
{
133 134
	int volume;

135 136
	assert(mixer != NULL);

137 138
	if (mixer->plugin->global && !mixer->failed &&
	    !mixer_open(mixer, error_r))
139 140
		return -1;

141
	g_mutex_lock(mixer->mutex);
142 143

	if (mixer->open) {
144 145 146 147 148
		GError *error = NULL;

		volume = mixer->plugin->get_volume(mixer, &error);
		if (volume < 0 && error != NULL) {
			g_propagate_error(error_r, error);
149
			mixer_failed(mixer);
150
		}
151 152 153
	} else
		volume = -1;

154 155 156
	g_mutex_unlock(mixer->mutex);

	return volume;
157 158 159
}

bool
160
mixer_set_volume(struct mixer *mixer, unsigned volume, GError **error_r)
161
{
162 163
	bool success;

164
	assert(mixer != NULL);
165
	assert(volume <= 100);
166

167 168
	if (mixer->plugin->global && !mixer->failed &&
	    !mixer_open(mixer, error_r))
169 170
		return false;

171
	g_mutex_lock(mixer->mutex);
172 173

	if (mixer->open) {
174
		success = mixer->plugin->set_volume(mixer, volume, error_r);
175 176 177
	} else
		success = false;

178 179 180
	g_mutex_unlock(mixer->mutex);

	return success;
181
}