2
* Copyright (C) 2003-2011 The Music Player Daemon Project
3
* http://www.musicpd.org
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
#include "mixer_control.h"
22
#include "mixer_api.h"
28
#define G_LOG_DOMAIN "mixer"
31
mixer_new(const struct mixer_plugin *plugin, void *ao,
32
const struct config_param *param,
37
assert(plugin != NULL);
39
mixer = plugin->init(ao, param, error_r);
41
assert(mixer == NULL || mixer->plugin == plugin);
47
mixer_free(struct mixer *mixer)
49
assert(mixer != NULL);
50
assert(mixer->plugin != NULL);
51
assert(mixer->mutex != NULL);
53
/* mixers with the "global" flag set might still be open at
54
this point (see mixer_auto_close()) */
57
g_mutex_free(mixer->mutex);
59
mixer->plugin->finish(mixer);
63
mixer_open(struct mixer *mixer, GError **error_r)
67
assert(mixer != NULL);
68
assert(mixer->plugin != NULL);
70
g_mutex_lock(mixer->mutex);
74
else if (mixer->plugin->open == NULL)
75
success = mixer->open = true;
77
success = mixer->open = mixer->plugin->open(mixer, error_r);
79
mixer->failed = !success;
81
g_mutex_unlock(mixer->mutex);
87
mixer_close_internal(struct mixer *mixer)
89
assert(mixer != NULL);
90
assert(mixer->plugin != NULL);
93
if (mixer->plugin->close != NULL)
94
mixer->plugin->close(mixer);
100
mixer_close(struct mixer *mixer)
102
assert(mixer != NULL);
103
assert(mixer->plugin != NULL);
105
g_mutex_lock(mixer->mutex);
108
mixer_close_internal(mixer);
110
g_mutex_unlock(mixer->mutex);
114
mixer_auto_close(struct mixer *mixer)
116
if (!mixer->plugin->global)
121
* Close the mixer due to failure. The mutex must be locked before
122
* calling this function.
125
mixer_failed(struct mixer *mixer)
129
mixer_close_internal(mixer);
131
mixer->failed = true;
135
mixer_get_volume(struct mixer *mixer, GError **error_r)
139
assert(mixer != NULL);
141
if (mixer->plugin->global && !mixer->failed &&
142
!mixer_open(mixer, error_r))
145
g_mutex_lock(mixer->mutex);
148
GError *error = NULL;
150
volume = mixer->plugin->get_volume(mixer, &error);
151
if (volume < 0 && error != NULL) {
152
g_propagate_error(error_r, error);
158
g_mutex_unlock(mixer->mutex);
164
mixer_set_volume(struct mixer *mixer, unsigned volume, GError **error_r)
168
assert(mixer != NULL);
169
assert(volume <= 100);
171
if (mixer->plugin->global && !mixer->failed &&
172
!mixer_open(mixer, error_r))
175
g_mutex_lock(mixer->mutex);
178
success = mixer->plugin->set_volume(mixer, volume, error_r);
182
g_mutex_unlock(mixer->mutex);