2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01:00
|
|
|
* This project's homepage is: 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.
|
|
|
|
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
#include "volume.h"
|
|
|
|
|
|
|
|
#include "conf.h"
|
|
|
|
#include "log.h"
|
2008-08-26 08:44:38 +02:00
|
|
|
#include "player_control.h"
|
2006-07-31 01:32:54 +02:00
|
|
|
#include "utils.h"
|
2008-10-14 22:38:14 +02:00
|
|
|
#include "idle.h"
|
2008-01-03 08:29:49 +01:00
|
|
|
#include "os_compat.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-04-12 06:19:26 +02:00
|
|
|
#include "../config.h"
|
|
|
|
|
2004-10-30 06:19:11 +02:00
|
|
|
#ifdef HAVE_OSS
|
2004-02-24 00:41:20 +01:00
|
|
|
#include <sys/soundcard.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ALSA
|
|
|
|
#include <alsa/asoundlib.h>
|
|
|
|
#endif
|
|
|
|
|
2004-04-02 00:11:29 +02:00
|
|
|
#define VOLUME_MIXER_TYPE_SOFTWARE 0
|
|
|
|
#define VOLUME_MIXER_TYPE_OSS 1
|
|
|
|
#define VOLUME_MIXER_TYPE_ALSA 2
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-04-02 00:11:29 +02:00
|
|
|
#define VOLUME_MIXER_SOFTWARE_DEFAULT ""
|
|
|
|
#define VOLUME_MIXER_OSS_DEFAULT "/dev/mixer"
|
|
|
|
#define VOLUME_MIXER_ALSA_DEFAULT "default"
|
2005-03-08 01:54:37 +01:00
|
|
|
#define VOLUME_MIXER_ALSA_CONTROL_DEFAULT "PCM"
|
2006-07-31 01:32:54 +02:00
|
|
|
#define SW_VOLUME_STATE "sw_volume: "
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-10-30 06:19:11 +02:00
|
|
|
#ifdef HAVE_OSS
|
2004-10-28 07:14:55 +02:00
|
|
|
#define VOLUME_MIXER_TYPE_DEFAULT VOLUME_MIXER_TYPE_OSS
|
|
|
|
#define VOLUME_MIXER_DEVICE_DEFAULT VOLUME_MIXER_OSS_DEFAULT
|
|
|
|
#else
|
|
|
|
#ifdef HAVE_ALSA
|
|
|
|
#define VOLUME_MIXER_TYPE_DEFAULT VOLUME_MIXER_TYPE_ALSA
|
|
|
|
#define VOLUME_MIXER_DEVICE_DEFAULT VOLUME_MIXER_ALSA_DEFAULT
|
|
|
|
#else
|
|
|
|
#define VOLUME_MIXER_TYPE_DEFAULT VOLUME_MIXER_TYPE_SOFTWARE
|
|
|
|
#define VOLUME_MIXER_DEVICE_DEFAULT VOLUME_MIXER_SOFTWARE_DEFAULT
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2006-07-15 05:34:48 +02:00
|
|
|
static int volume_mixerType = VOLUME_MIXER_TYPE_DEFAULT;
|
2008-02-05 11:17:33 +01:00
|
|
|
static const char *volume_mixerDevice = VOLUME_MIXER_DEVICE_DEFAULT;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-15 05:34:48 +02:00
|
|
|
static int volume_softwareSet = 100;
|
2004-03-23 21:34:12 +01:00
|
|
|
|
2004-10-30 06:19:11 +02:00
|
|
|
#ifdef HAVE_OSS
|
2006-07-16 18:53:04 +02:00
|
|
|
static int volume_ossFd = -1;
|
2006-07-15 05:34:48 +02:00
|
|
|
static int volume_ossControl = SOUND_MIXER_PCM;
|
2004-02-24 00:41:20 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_ALSA
|
2007-01-14 04:07:53 +01:00
|
|
|
static snd_mixer_t *volume_alsaMixerHandle;
|
2006-07-20 18:02:40 +02:00
|
|
|
static snd_mixer_elem_t *volume_alsaElem;
|
2006-07-15 05:34:48 +02:00
|
|
|
static long volume_alsaMin;
|
|
|
|
static long volume_alsaMax;
|
|
|
|
static int volume_alsaSet = -1;
|
2004-02-24 00:41:20 +01:00
|
|
|
#endif
|
|
|
|
|
2004-10-30 06:19:11 +02:00
|
|
|
#ifdef HAVE_OSS
|
2006-07-16 18:53:04 +02:00
|
|
|
|
2008-09-23 20:47:53 +02:00
|
|
|
#include <alloca.h> /* only alloca user in mpd atm, may change ... */
|
|
|
|
|
2006-07-16 18:53:04 +02:00
|
|
|
static void closeOssMixer(void)
|
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
while (close(volume_ossFd) && errno == EINTR) ;
|
2006-07-16 18:53:04 +02:00
|
|
|
volume_ossFd = -1;
|
|
|
|
}
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static int prepOssMixer(const char *device)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
ConfigParam *param;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((volume_ossFd = open(device, O_RDONLY)) < 0) {
|
|
|
|
WARNING("unable to open oss mixer \"%s\"\n", device);
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-09-23 20:47:53 +02:00
|
|
|
if ((param = getConfigParam(CONF_MIXER_CONTROL))) {
|
2008-02-05 11:17:33 +01:00
|
|
|
const char *labels[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_LABELS;
|
2008-09-23 20:47:53 +02:00
|
|
|
int i;
|
2006-07-16 18:53:04 +02:00
|
|
|
int devmask = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (ioctl(volume_ossFd, SOUND_MIXER_READ_DEVMASK, &devmask) < 0) {
|
2004-06-12 04:06:16 +02:00
|
|
|
WARNING("errors getting read_devmask for oss mixer\n");
|
2006-07-16 18:53:04 +02:00
|
|
|
closeOssMixer();
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
|
2008-09-23 20:47:53 +02:00
|
|
|
ssize_t len = strlen(labels[i]);
|
|
|
|
char *duplicated = alloca(len + 1);
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
/* eliminate spaces at the end */
|
2008-09-23 20:47:53 +02:00
|
|
|
memcpy(duplicated, labels[i], len + 1);
|
|
|
|
len -= 2;
|
|
|
|
while (len >= 0 && duplicated[len] == ' ')
|
|
|
|
duplicated[len--] = '\0';
|
|
|
|
if (strcasecmp(duplicated, param->value) == 0)
|
2004-02-24 00:41:20 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (i >= SOUND_MIXER_NRDEVICES) {
|
2004-10-28 07:14:55 +02:00
|
|
|
WARNING("mixer control \"%s\" not found at line %i\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
param->value, param->line);
|
2006-07-16 18:53:04 +02:00
|
|
|
closeOssMixer();
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (!((1 << i) & devmask)) {
|
2004-10-28 07:14:55 +02:00
|
|
|
WARNING("mixer control \"%s\" not usable at line %i\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
param->value, param->line);
|
2006-07-16 18:53:04 +02:00
|
|
|
closeOssMixer();
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
volume_ossControl = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-16 18:53:04 +02:00
|
|
|
static int ensure_oss_open(void)
|
|
|
|
{
|
|
|
|
if ((volume_ossFd < 0 && prepOssMixer(volume_mixerDevice) < 0))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int getOssVolumeLevel(void)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int left, right, level;
|
|
|
|
|
2006-07-16 18:53:04 +02:00
|
|
|
if (ensure_oss_open() < 0)
|
|
|
|
return -1;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (ioctl(volume_ossFd, MIXER_READ(volume_ossControl), &level) < 0) {
|
2006-07-16 18:53:04 +02:00
|
|
|
closeOssMixer();
|
2004-06-12 04:06:16 +02:00
|
|
|
WARNING("unable to read volume\n");
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
left = level & 0xff;
|
|
|
|
right = (level & 0xff00) >> 8;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (left != right) {
|
2004-06-12 04:06:16 +02:00
|
|
|
WARNING("volume for left and right is not the same, \"%i\" and "
|
2006-07-20 18:02:40 +02:00
|
|
|
"\"%i\"\n", left, right);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
2008-09-07 13:50:16 +02:00
|
|
|
static int changeOssVolumeLevel(int change, int rel)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int current;
|
|
|
|
int new;
|
|
|
|
int level;
|
|
|
|
|
|
|
|
if (rel) {
|
2008-09-07 13:50:16 +02:00
|
|
|
if ((current = getOssVolumeLevel()) < 0)
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
new = current + change;
|
2006-07-16 18:53:04 +02:00
|
|
|
} else {
|
|
|
|
if (ensure_oss_open() < 0)
|
|
|
|
return -1;
|
|
|
|
new = change;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (new < 0)
|
|
|
|
new = 0;
|
|
|
|
else if (new > 100)
|
|
|
|
new = 100;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
level = (new << 8) + new;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (ioctl(volume_ossFd, MIXER_WRITE(volume_ossControl), &level) < 0) {
|
2006-07-16 18:53:04 +02:00
|
|
|
closeOssMixer();
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_ALSA
|
2006-07-20 18:02:40 +02:00
|
|
|
static void closeAlsaMixer(void)
|
|
|
|
{
|
2006-07-16 18:52:49 +02:00
|
|
|
snd_mixer_close(volume_alsaMixerHandle);
|
|
|
|
volume_alsaMixerHandle = NULL;
|
|
|
|
}
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static int prepAlsaMixer(const char *card)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int err;
|
2006-07-20 18:02:40 +02:00
|
|
|
snd_mixer_elem_t *elem;
|
2008-02-05 11:17:33 +01:00
|
|
|
const char *controlName = VOLUME_MIXER_ALSA_CONTROL_DEFAULT;
|
2006-07-20 18:02:40 +02:00
|
|
|
ConfigParam *param;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
err = snd_mixer_open(&volume_alsaMixerHandle, 0);
|
2006-07-17 03:28:38 +02:00
|
|
|
snd_config_update_free_global();
|
|
|
|
if (err < 0) {
|
2006-07-20 18:02:40 +02:00
|
|
|
WARNING("problems opening alsa mixer: %s\n", snd_strerror(err));
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2006-07-15 05:34:48 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((err = snd_mixer_attach(volume_alsaMixerHandle, card)) < 0) {
|
2006-07-16 18:52:49 +02:00
|
|
|
closeAlsaMixer();
|
2006-08-11 23:50:56 +02:00
|
|
|
WARNING("problems attaching alsa mixer: %s\n",
|
2004-02-24 00:41:20 +01:00
|
|
|
snd_strerror(err));
|
|
|
|
return -1;
|
|
|
|
}
|
2006-07-15 05:34:48 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((err =
|
|
|
|
snd_mixer_selem_register(volume_alsaMixerHandle, NULL,
|
|
|
|
NULL)) < 0) {
|
2006-07-16 18:52:49 +02:00
|
|
|
closeAlsaMixer();
|
2004-06-12 04:06:16 +02:00
|
|
|
WARNING("problems snd_mixer_selem_register'ing: %s\n",
|
2004-02-24 00:41:20 +01:00
|
|
|
snd_strerror(err));
|
|
|
|
return -1;
|
|
|
|
}
|
2006-07-16 18:52:49 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((err = snd_mixer_load(volume_alsaMixerHandle)) < 0) {
|
2006-07-16 18:52:49 +02:00
|
|
|
closeAlsaMixer();
|
2004-06-12 04:06:16 +02:00
|
|
|
WARNING("problems snd_mixer_selem_register'ing: %s\n",
|
2004-02-24 00:41:20 +01:00
|
|
|
snd_strerror(err));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
elem = snd_mixer_first_elem(volume_alsaMixerHandle);
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-15 05:34:48 +02:00
|
|
|
param = getConfigParam(CONF_MIXER_CONTROL);
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (param) {
|
2004-10-28 07:14:55 +02:00
|
|
|
controlName = param->value;
|
2004-04-02 00:11:29 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (elem) {
|
|
|
|
if (snd_mixer_elem_get_type(elem) == SND_MIXER_ELEM_SIMPLE) {
|
|
|
|
if (strcasecmp(controlName,
|
|
|
|
snd_mixer_selem_get_name(elem)) == 0) {
|
2004-04-02 00:11:29 +02:00
|
|
|
break;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
2004-04-02 00:11:29 +02:00
|
|
|
elem = snd_mixer_elem_next(elem);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-04-02 00:11:29 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (elem) {
|
2004-02-24 00:41:20 +01:00
|
|
|
volume_alsaElem = elem;
|
2006-07-20 18:02:40 +02:00
|
|
|
snd_mixer_selem_get_playback_volume_range(volume_alsaElem,
|
|
|
|
&volume_alsaMin,
|
|
|
|
&volume_alsaMax);
|
2004-04-02 00:11:29 +02:00
|
|
|
return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
WARNING("can't find alsa mixer_control \"%s\"\n", controlName);
|
2004-04-02 00:11:29 +02:00
|
|
|
|
2006-07-16 18:52:49 +02:00
|
|
|
closeAlsaMixer();
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-16 18:52:49 +02:00
|
|
|
static int prep_alsa_get_level(long *level)
|
|
|
|
{
|
|
|
|
const char *cmd;
|
|
|
|
int err;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!volume_alsaMixerHandle && prepAlsaMixer(volume_mixerDevice) < 0)
|
2006-07-16 18:52:49 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((err = snd_mixer_handle_events(volume_alsaMixerHandle)) < 0) {
|
|
|
|
cmd = "handle_events";
|
|
|
|
goto error;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((err = snd_mixer_selem_get_playback_volume(volume_alsaElem,
|
|
|
|
SND_MIXER_SCHN_FRONT_LEFT,
|
|
|
|
level)) < 0) {
|
2006-07-16 18:52:49 +02:00
|
|
|
cmd = "selem_get_playback_volume";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
return 0;
|
2006-08-10 00:18:06 +02:00
|
|
|
|
|
|
|
error:
|
2006-07-16 18:52:49 +02:00
|
|
|
WARNING("problems getting alsa volume: %s (snd_mixer_%s)\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
snd_strerror(err), cmd);
|
2006-07-16 18:52:49 +02:00
|
|
|
closeAlsaMixer();
|
|
|
|
return -1;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int getAlsaVolumeLevel(void)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int ret;
|
|
|
|
long level;
|
|
|
|
long max = volume_alsaMax;
|
|
|
|
long min = volume_alsaMin;
|
2004-03-23 22:18:19 +01:00
|
|
|
|
2006-07-16 18:52:49 +02:00
|
|
|
if (prep_alsa_get_level(&level) < 0)
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
ret = ((volume_alsaSet / 100.0) * (max - min) + min) + 0.5;
|
|
|
|
if (volume_alsaSet > 0 && ret == level) {
|
2004-03-23 22:18:19 +01:00
|
|
|
ret = volume_alsaSet;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
|
|
|
ret = (int)(100 * (((float)(level - min)) / (max - min)) + 0.5);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-07 13:50:16 +02:00
|
|
|
static int changeAlsaVolumeLevel(int change, int rel)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
float vol;
|
|
|
|
long level;
|
2004-03-23 21:34:12 +01:00
|
|
|
long test;
|
2004-02-24 00:41:20 +01:00
|
|
|
long max = volume_alsaMax;
|
|
|
|
long min = volume_alsaMin;
|
|
|
|
int err;
|
|
|
|
|
2006-07-16 18:52:49 +02:00
|
|
|
if (prep_alsa_get_level(&level) < 0)
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (rel) {
|
2006-07-20 18:02:40 +02:00
|
|
|
test = ((volume_alsaSet / 100.0) * (max - min) + min) + 0.5;
|
|
|
|
if (volume_alsaSet >= 0 && level == test) {
|
2004-03-23 21:34:12 +01:00
|
|
|
vol = volume_alsaSet;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
|
|
|
vol = 100.0 * (((float)(level - min)) / (max - min));
|
|
|
|
vol += change;
|
|
|
|
} else
|
2004-02-24 00:41:20 +01:00
|
|
|
vol = change;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
volume_alsaSet = vol + 0.5;
|
|
|
|
volume_alsaSet = volume_alsaSet > 100 ? 100 :
|
|
|
|
(volume_alsaSet < 0 ? 0 : volume_alsaSet);
|
2004-03-23 21:34:12 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
level = (long)(((vol / 100.0) * (max - min) + min) + 0.5);
|
|
|
|
level = level > max ? max : level;
|
|
|
|
level = level < min ? min : level;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((err =
|
|
|
|
snd_mixer_selem_set_playback_volume_all(volume_alsaElem,
|
|
|
|
level)) < 0) {
|
|
|
|
WARNING("problems setting alsa volume: %s\n",
|
|
|
|
snd_strerror(err));
|
2006-07-16 18:52:49 +02:00
|
|
|
closeAlsaMixer();
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static int prepMixer(const char *device)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
switch (volume_mixerType) {
|
2004-02-24 00:41:20 +01:00
|
|
|
#ifdef HAVE_ALSA
|
|
|
|
case VOLUME_MIXER_TYPE_ALSA:
|
|
|
|
return prepAlsaMixer(device);
|
|
|
|
#endif
|
2004-10-30 06:19:11 +02:00
|
|
|
#ifdef HAVE_OSS
|
2004-02-24 00:41:20 +01:00
|
|
|
case VOLUME_MIXER_TYPE_OSS:
|
|
|
|
return prepOssMixer(device);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void finishVolume(void)
|
|
|
|
{
|
|
|
|
switch (volume_mixerType) {
|
2004-02-24 00:41:20 +01:00
|
|
|
#ifdef HAVE_ALSA
|
|
|
|
case VOLUME_MIXER_TYPE_ALSA:
|
|
|
|
closeAlsaMixer();
|
|
|
|
break;
|
|
|
|
#endif
|
2004-10-31 05:34:54 +01:00
|
|
|
#ifdef HAVE_OSS
|
2004-02-24 00:41:20 +01:00
|
|
|
case VOLUME_MIXER_TYPE_OSS:
|
|
|
|
closeOssMixer();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void initVolume(void)
|
|
|
|
{
|
|
|
|
ConfigParam *param = getConfigParam(CONF_MIXER_TYPE);
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (param) {
|
|
|
|
if (0) ;
|
2004-02-24 00:41:20 +01:00
|
|
|
#ifdef HAVE_ALSA
|
2006-07-20 18:02:40 +02:00
|
|
|
else if (strcmp(param->value, VOLUME_MIXER_ALSA) == 0) {
|
2004-10-28 07:14:55 +02:00
|
|
|
volume_mixerType = VOLUME_MIXER_TYPE_ALSA;
|
|
|
|
volume_mixerDevice = VOLUME_MIXER_ALSA_DEFAULT;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
#endif
|
2004-10-31 05:34:54 +01:00
|
|
|
#ifdef HAVE_OSS
|
2006-07-20 18:02:40 +02:00
|
|
|
else if (strcmp(param->value, VOLUME_MIXER_OSS) == 0) {
|
2004-10-28 07:14:55 +02:00
|
|
|
volume_mixerType = VOLUME_MIXER_TYPE_OSS;
|
|
|
|
volume_mixerDevice = VOLUME_MIXER_OSS_DEFAULT;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
#endif
|
2006-07-20 18:02:40 +02:00
|
|
|
else if (strcmp(param->value, VOLUME_MIXER_SOFTWARE) == 0) {
|
2004-10-28 07:14:55 +02:00
|
|
|
volume_mixerType = VOLUME_MIXER_TYPE_SOFTWARE;
|
|
|
|
volume_mixerDevice = VOLUME_MIXER_SOFTWARE_DEFAULT;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("unknown mixer type %s at line %i\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
param->value, param->line);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
|
|
|
|
param = getConfigParam(CONF_MIXER_DEVICE);
|
2006-07-15 05:34:48 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (param) {
|
2004-10-28 07:14:55 +02:00
|
|
|
volume_mixerDevice = param->value;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void openVolumeDevice(void)
|
|
|
|
{
|
|
|
|
if (prepMixer(volume_mixerDevice) < 0) {
|
2004-06-12 04:06:16 +02:00
|
|
|
WARNING("using software volume\n");
|
2004-02-24 00:41:20 +01:00
|
|
|
volume_mixerType = VOLUME_MIXER_TYPE_SOFTWARE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int getSoftwareVolume(void)
|
|
|
|
{
|
2004-04-11 13:48:04 +02:00
|
|
|
return volume_softwareSet;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int getVolumeLevel(void)
|
|
|
|
{
|
|
|
|
switch (volume_mixerType) {
|
2004-02-24 00:41:20 +01:00
|
|
|
#ifdef HAVE_ALSA
|
|
|
|
case VOLUME_MIXER_TYPE_ALSA:
|
|
|
|
return getAlsaVolumeLevel();
|
|
|
|
#endif
|
2004-10-31 05:34:54 +01:00
|
|
|
#ifdef HAVE_OSS
|
2004-02-24 00:41:20 +01:00
|
|
|
case VOLUME_MIXER_TYPE_OSS:
|
|
|
|
return getOssVolumeLevel();
|
|
|
|
#endif
|
|
|
|
case VOLUME_MIXER_TYPE_SOFTWARE:
|
|
|
|
return getSoftwareVolume();
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-07 13:50:16 +02:00
|
|
|
static int changeSoftwareVolume(int change, int rel)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int new = change;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (rel)
|
|
|
|
new += volume_softwareSet;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (new > 100)
|
|
|
|
new = 100;
|
|
|
|
else if (new < 0)
|
|
|
|
new = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-03-23 21:34:12 +01:00
|
|
|
volume_softwareSet = new;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
/*new = 100.0*(exp(new/50.0)-1)/(M_E*M_E-1)+0.5; */
|
|
|
|
if (new >= 100)
|
|
|
|
new = 1000;
|
|
|
|
else if (new <= 0)
|
|
|
|
new = 0;
|
|
|
|
else
|
|
|
|
new =
|
|
|
|
1000.0 * (exp(new / 25.0) - 1) / (54.5981500331F - 1) + 0.5;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
setPlayerSoftwareVolume(new);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-07 13:50:16 +02:00
|
|
|
int changeVolumeLevel(int change, int rel)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-14 22:38:14 +02:00
|
|
|
idle_add(IDLE_MIXER);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (volume_mixerType) {
|
2004-02-24 00:41:20 +01:00
|
|
|
#ifdef HAVE_ALSA
|
|
|
|
case VOLUME_MIXER_TYPE_ALSA:
|
2008-09-07 13:50:16 +02:00
|
|
|
return changeAlsaVolumeLevel(change, rel);
|
2004-02-24 00:41:20 +01:00
|
|
|
#endif
|
2004-10-31 05:34:54 +01:00
|
|
|
#ifdef HAVE_OSS
|
2004-02-24 00:41:20 +01:00
|
|
|
case VOLUME_MIXER_TYPE_OSS:
|
2008-09-07 13:50:16 +02:00
|
|
|
return changeOssVolumeLevel(change, rel);
|
2004-02-24 00:41:20 +01:00
|
|
|
#endif
|
|
|
|
case VOLUME_MIXER_TYPE_SOFTWARE:
|
2008-09-07 13:50:16 +02:00
|
|
|
return changeSoftwareVolume(change, rel);
|
2004-02-24 00:41:20 +01:00
|
|
|
default:
|
2006-07-15 05:34:48 +02:00
|
|
|
return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
2006-07-31 01:32:54 +02:00
|
|
|
|
|
|
|
void read_sw_volume_state(FILE *fp)
|
|
|
|
{
|
2008-09-23 20:48:12 +02:00
|
|
|
char buf[sizeof(SW_VOLUME_STATE) + sizeof("100") - 1];
|
2006-07-31 01:32:54 +02:00
|
|
|
char *end = NULL;
|
|
|
|
long int sv;
|
|
|
|
|
|
|
|
if (volume_mixerType != VOLUME_MIXER_TYPE_SOFTWARE)
|
|
|
|
return;
|
2008-09-23 20:48:12 +02:00
|
|
|
while (myFgets(buf, sizeof(buf), fp)) {
|
|
|
|
if (prefixcmp(buf, SW_VOLUME_STATE))
|
2006-07-31 01:32:54 +02:00
|
|
|
continue;
|
2008-09-23 20:48:12 +02:00
|
|
|
sv = strtol(buf + strlen(SW_VOLUME_STATE), &end, 10);
|
2006-07-31 01:32:54 +02:00
|
|
|
if (mpd_likely(!*end))
|
2008-09-07 13:50:16 +02:00
|
|
|
changeSoftwareVolume(sv, 0);
|
2006-07-31 01:32:54 +02:00
|
|
|
else
|
|
|
|
ERROR("Can't parse software volume: %s\n", buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void save_sw_volume_state(FILE *fp)
|
|
|
|
{
|
|
|
|
if (volume_mixerType == VOLUME_MIXER_TYPE_SOFTWARE)
|
|
|
|
fprintf(fp, SW_VOLUME_STATE "%d\n", volume_softwareSet);
|
|
|
|
}
|
|
|
|
|