configure.ac: Move the encoders before the audio outputs.
[mpd-mk.git] / src / mixer_control.c
blob458b3abc139eda6740cd341b2c0c3e54513edc21
1 /*
2 * Copyright (C) 2003-2010 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.
20 #include "config.h"
21 #include "mixer_control.h"
22 #include "mixer_api.h"
24 #include <assert.h>
25 #include <stddef.h>
27 #undef G_LOG_DOMAIN
28 #define G_LOG_DOMAIN "mixer"
30 struct mixer *
31 mixer_new(const struct mixer_plugin *plugin, void *ao,
32 const struct config_param *param,
33 GError **error_r)
35 struct mixer *mixer;
37 assert(plugin != NULL);
39 mixer = plugin->init(ao, param, error_r);
41 assert(mixer == NULL || mixer->plugin == plugin);
43 return mixer;
46 void
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()) */
55 mixer_close(mixer);
57 g_mutex_free(mixer->mutex);
59 mixer->plugin->finish(mixer);
62 bool
63 mixer_open(struct mixer *mixer, GError **error_r)
65 bool success;
67 assert(mixer != NULL);
68 assert(mixer->plugin != NULL);
70 g_mutex_lock(mixer->mutex);
72 if (mixer->open)
73 success = true;
74 else if (mixer->plugin->open == NULL)
75 success = mixer->open = true;
76 else
77 success = mixer->open = mixer->plugin->open(mixer, error_r);
79 mixer->failed = !success;
81 g_mutex_unlock(mixer->mutex);
83 return success;
86 static void
87 mixer_close_internal(struct mixer *mixer)
89 assert(mixer != NULL);
90 assert(mixer->plugin != NULL);
91 assert(mixer->open);
93 if (mixer->plugin->close != NULL)
94 mixer->plugin->close(mixer);
96 mixer->open = false;
99 void
100 mixer_close(struct mixer *mixer)
102 assert(mixer != NULL);
103 assert(mixer->plugin != NULL);
105 g_mutex_lock(mixer->mutex);
107 if (mixer->open)
108 mixer_close_internal(mixer);
110 g_mutex_unlock(mixer->mutex);
113 void
114 mixer_auto_close(struct mixer *mixer)
116 if (!mixer->plugin->global)
117 mixer_close(mixer);
121 * Close the mixer due to failure. The mutex must be locked before
122 * calling this function.
124 static void
125 mixer_failed(struct mixer *mixer)
127 assert(mixer->open);
129 mixer_close_internal(mixer);
131 mixer->failed = true;
135 mixer_get_volume(struct mixer *mixer, GError **error_r)
137 int volume;
139 assert(mixer != NULL);
141 if (mixer->plugin->global && !mixer->failed &&
142 !mixer_open(mixer, error_r))
143 return -1;
145 g_mutex_lock(mixer->mutex);
147 if (mixer->open) {
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);
153 mixer_failed(mixer);
155 } else
156 volume = -1;
158 g_mutex_unlock(mixer->mutex);
160 return volume;
163 bool
164 mixer_set_volume(struct mixer *mixer, unsigned volume, GError **error_r)
166 bool success;
168 assert(mixer != NULL);
169 assert(volume <= 100);
171 if (mixer->plugin->global && !mixer->failed &&
172 !mixer_open(mixer, error_r))
173 return false;
175 g_mutex_lock(mixer->mutex);
177 if (mixer->open) {
178 success = mixer->plugin->set_volume(mixer, volume, error_r);
179 } else
180 success = false;
182 g_mutex_unlock(mixer->mutex);
184 return success;