2009-03-12 18:34:37 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-03-12 18:34:37 +01:00
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-12 18:34:37 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2009-03-12 18:34:37 +01:00
|
|
|
#include "mixer_control.h"
|
|
|
|
#include "mixer_api.h"
|
|
|
|
|
2009-03-12 18:34:38 +01:00
|
|
|
#include <assert.h>
|
2009-03-14 11:36:50 +01:00
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "mixer"
|
|
|
|
|
|
|
|
struct mixer *
|
2009-10-21 09:48:41 +02:00
|
|
|
mixer_new(const struct mixer_plugin *plugin, void *ao,
|
|
|
|
const struct config_param *param,
|
2009-10-20 22:10:56 +02:00
|
|
|
GError **error_r)
|
2009-03-14 11:36:50 +01:00
|
|
|
{
|
|
|
|
struct mixer *mixer;
|
|
|
|
|
|
|
|
assert(plugin != NULL);
|
|
|
|
|
2009-10-21 09:48:41 +02:00
|
|
|
mixer = plugin->init(ao, param, error_r);
|
2009-03-14 11:36:50 +01:00
|
|
|
|
|
|
|
assert(mixer == NULL || mixer->plugin == plugin);
|
|
|
|
|
|
|
|
return mixer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mixer_free(struct mixer *mixer)
|
|
|
|
{
|
2009-03-26 19:43:15 +01:00
|
|
|
assert(mixer != NULL);
|
2009-03-14 11:36:50 +01:00
|
|
|
assert(mixer->plugin != NULL);
|
2009-03-14 11:53:28 +01:00
|
|
|
assert(mixer->mutex != NULL);
|
|
|
|
|
2009-12-08 08:47:47 +01:00
|
|
|
/* mixers with the "global" flag set might still be open at
|
|
|
|
this point (see mixer_auto_close()) */
|
|
|
|
mixer_close(mixer);
|
|
|
|
|
2009-03-14 11:53:28 +01:00
|
|
|
g_mutex_free(mixer->mutex);
|
2009-03-14 11:36:50 +01:00
|
|
|
|
|
|
|
mixer->plugin->finish(mixer);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2009-10-20 22:10:56 +02:00
|
|
|
mixer_open(struct mixer *mixer, GError **error_r)
|
2009-03-14 11:36:50 +01:00
|
|
|
{
|
2009-03-14 11:53:28 +01:00
|
|
|
bool success;
|
|
|
|
|
2009-03-26 19:43:15 +01:00
|
|
|
assert(mixer != NULL);
|
2009-03-14 11:36:50 +01:00
|
|
|
assert(mixer->plugin != NULL);
|
2009-03-14 11:53:28 +01:00
|
|
|
|
|
|
|
g_mutex_lock(mixer->mutex);
|
2009-03-26 19:43:18 +01:00
|
|
|
|
|
|
|
if (mixer->open)
|
|
|
|
success = true;
|
2009-10-23 09:15:51 +02:00
|
|
|
else if (mixer->plugin->open == NULL)
|
|
|
|
success = mixer->open = true;
|
2009-03-26 19:43:18 +01:00
|
|
|
else
|
2009-10-20 22:10:56 +02:00
|
|
|
success = mixer->open = mixer->plugin->open(mixer, error_r);
|
2009-03-26 19:43:18 +01:00
|
|
|
|
2009-03-26 19:46:39 +01:00
|
|
|
mixer->failed = !success;
|
|
|
|
|
2009-03-14 11:53:28 +01:00
|
|
|
g_mutex_unlock(mixer->mutex);
|
|
|
|
|
|
|
|
return success;
|
2009-03-14 11:36:50 +01:00
|
|
|
}
|
|
|
|
|
2009-03-27 16:44:31 +01:00
|
|
|
static void
|
|
|
|
mixer_close_internal(struct mixer *mixer)
|
|
|
|
{
|
|
|
|
assert(mixer != NULL);
|
|
|
|
assert(mixer->plugin != NULL);
|
|
|
|
assert(mixer->open);
|
|
|
|
|
2009-10-23 09:15:51 +02:00
|
|
|
if (mixer->plugin->close != NULL)
|
|
|
|
mixer->plugin->close(mixer);
|
|
|
|
|
2009-03-27 16:44:31 +01:00
|
|
|
mixer->open = false;
|
|
|
|
}
|
|
|
|
|
2009-03-14 11:36:50 +01:00
|
|
|
void
|
|
|
|
mixer_close(struct mixer *mixer)
|
|
|
|
{
|
2009-03-26 19:43:15 +01:00
|
|
|
assert(mixer != NULL);
|
2009-03-14 11:36:50 +01:00
|
|
|
assert(mixer->plugin != NULL);
|
2009-03-14 11:53:28 +01:00
|
|
|
|
|
|
|
g_mutex_lock(mixer->mutex);
|
2009-03-26 19:43:18 +01:00
|
|
|
|
2009-03-27 16:44:31 +01:00
|
|
|
if (mixer->open)
|
|
|
|
mixer_close_internal(mixer);
|
2009-03-26 19:43:18 +01:00
|
|
|
|
2009-03-14 11:53:28 +01:00
|
|
|
g_mutex_unlock(mixer->mutex);
|
2009-03-14 11:36:50 +01:00
|
|
|
}
|
|
|
|
|
2009-03-26 19:43:18 +01:00
|
|
|
void
|
|
|
|
mixer_auto_close(struct mixer *mixer)
|
|
|
|
{
|
|
|
|
if (!mixer->plugin->global)
|
|
|
|
mixer_close(mixer);
|
|
|
|
}
|
|
|
|
|
2009-03-26 19:46:33 +01:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
|
2009-03-27 16:44:31 +01:00
|
|
|
mixer_close_internal(mixer);
|
2009-03-26 19:46:39 +01:00
|
|
|
|
|
|
|
mixer->failed = true;
|
2009-03-26 19:46:33 +01:00
|
|
|
}
|
|
|
|
|
2009-03-14 11:36:50 +01:00
|
|
|
int
|
2009-10-20 22:10:56 +02:00
|
|
|
mixer_get_volume(struct mixer *mixer, GError **error_r)
|
2009-03-14 11:36:50 +01:00
|
|
|
{
|
2009-03-14 11:53:28 +01:00
|
|
|
int volume;
|
|
|
|
|
2009-03-26 19:43:15 +01:00
|
|
|
assert(mixer != NULL);
|
|
|
|
|
2009-10-20 22:10:56 +02:00
|
|
|
if (mixer->plugin->global && !mixer->failed &&
|
|
|
|
!mixer_open(mixer, error_r))
|
2009-03-26 19:43:18 +01:00
|
|
|
return -1;
|
|
|
|
|
2009-03-14 11:53:28 +01:00
|
|
|
g_mutex_lock(mixer->mutex);
|
2009-03-26 19:43:18 +01:00
|
|
|
|
|
|
|
if (mixer->open) {
|
2009-10-23 10:32:25 +02:00
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
volume = mixer->plugin->get_volume(mixer, &error);
|
|
|
|
if (volume < 0 && error != NULL) {
|
|
|
|
g_propagate_error(error_r, error);
|
2009-03-26 19:46:33 +01:00
|
|
|
mixer_failed(mixer);
|
2009-10-23 10:32:25 +02:00
|
|
|
}
|
2009-03-26 19:43:18 +01:00
|
|
|
} else
|
|
|
|
volume = -1;
|
|
|
|
|
2009-03-14 11:53:28 +01:00
|
|
|
g_mutex_unlock(mixer->mutex);
|
|
|
|
|
|
|
|
return volume;
|
2009-03-14 11:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2009-10-20 22:10:56 +02:00
|
|
|
mixer_set_volume(struct mixer *mixer, unsigned volume, GError **error_r)
|
2009-03-14 11:36:50 +01:00
|
|
|
{
|
2009-03-14 11:53:28 +01:00
|
|
|
bool success;
|
|
|
|
|
2009-03-26 19:43:15 +01:00
|
|
|
assert(mixer != NULL);
|
2009-03-27 20:10:39 +01:00
|
|
|
assert(volume <= 100);
|
2009-03-26 19:43:15 +01:00
|
|
|
|
2009-10-20 22:10:56 +02:00
|
|
|
if (mixer->plugin->global && !mixer->failed &&
|
|
|
|
!mixer_open(mixer, error_r))
|
2009-03-26 19:43:18 +01:00
|
|
|
return false;
|
|
|
|
|
2009-03-14 11:53:28 +01:00
|
|
|
g_mutex_lock(mixer->mutex);
|
2009-03-26 19:43:18 +01:00
|
|
|
|
|
|
|
if (mixer->open) {
|
2009-10-20 22:10:56 +02:00
|
|
|
success = mixer->plugin->set_volume(mixer, volume, error_r);
|
2009-03-26 19:43:18 +01:00
|
|
|
} else
|
|
|
|
success = false;
|
|
|
|
|
2009-03-14 11:53:28 +01:00
|
|
|
g_mutex_unlock(mixer->mutex);
|
|
|
|
|
|
|
|
return success;
|
2009-03-14 11:36:50 +01:00
|
|
|
}
|