mixer_control.c 3.62 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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
	assert(mixer->mutex != NULL);

53 54 55 56
	/* mixers with the "global" flag set might still be open at
	   this point (see mixer_auto_close()) */
	mixer_close(mixer);

57
	g_mutex_free(mixer->mutex);
58 59 60 61 62

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

bool
63
mixer_open(struct mixer *mixer, GError **error_r)
64
{
65 66
	bool success;

67
	assert(mixer != NULL);
68
	assert(mixer->plugin != NULL);
69 70

	g_mutex_lock(mixer->mutex);
71 72 73

	if (mixer->open)
		success = true;
74 75
	else if (mixer->plugin->open == NULL)
		success = mixer->open = true;
76
	else
77
		success = mixer->open = mixer->plugin->open(mixer, error_r);
78

79 80
	mixer->failed = !success;

81 82 83
	g_mutex_unlock(mixer->mutex);

	return success;
84 85
}

86 87 88 89 90 91 92
static void
mixer_close_internal(struct mixer *mixer)
{
	assert(mixer != NULL);
	assert(mixer->plugin != NULL);
	assert(mixer->open);

93 94 95
	if (mixer->plugin->close != NULL)
		mixer->plugin->close(mixer);

96 97 98
	mixer->open = false;
}

99 100 101
void
mixer_close(struct mixer *mixer)
{
102
	assert(mixer != NULL);
103
	assert(mixer->plugin != NULL);
104 105

	g_mutex_lock(mixer->mutex);
106

107 108
	if (mixer->open)
		mixer_close_internal(mixer);
109

110
	g_mutex_unlock(mixer->mutex);
111 112
}

113 114 115 116 117 118 119
void
mixer_auto_close(struct mixer *mixer)
{
	if (!mixer->plugin->global)
		mixer_close(mixer);
}

120 121 122 123 124 125 126 127 128
/*
 * 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);

129
	mixer_close_internal(mixer);
130 131

	mixer->failed = true;
132 133
}

134
int
135
mixer_get_volume(struct mixer *mixer, GError **error_r)
136
{
137 138
	int volume;

139 140
	assert(mixer != NULL);

141 142
	if (mixer->plugin->global && !mixer->failed &&
	    !mixer_open(mixer, error_r))
143 144
		return -1;

145
	g_mutex_lock(mixer->mutex);
146 147

	if (mixer->open) {
148 149 150 151 152
		GError *error = NULL;

		volume = mixer->plugin->get_volume(mixer, &error);
		if (volume < 0 && error != NULL) {
			g_propagate_error(error_r, error);
153
			mixer_failed(mixer);
154
		}
155 156 157
	} else
		volume = -1;

158 159 160
	g_mutex_unlock(mixer->mutex);

	return volume;
161 162 163
}

bool
164
mixer_set_volume(struct mixer *mixer, unsigned volume, GError **error_r)
165
{
166 167
	bool success;

168
	assert(mixer != NULL);
169
	assert(volume <= 100);
170

171 172
	if (mixer->plugin->global && !mixer->failed &&
	    !mixer_open(mixer, error_r))
173 174
		return false;

175
	g_mutex_lock(mixer->mutex);
176 177

	if (mixer->open) {
178
		success = mixer->plugin->set_volume(mixer, volume, error_r);
179 180 181
	} else
		success = false;

182 183 184
	g_mutex_unlock(mixer->mutex);

	return success;
185
}