2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
2008-12-31 16:46:41 +01:00
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-07 08:59:11 +01:00
|
|
|
#include "Volume.hxx"
|
2014-01-27 08:20:25 +01:00
|
|
|
#include "output/MultipleOutputs.hxx"
|
2013-01-09 08:36:52 +01:00
|
|
|
#include "Idle.hxx"
|
2015-11-06 09:09:02 +01:00
|
|
|
#include "util/StringCompare.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "util/Domain.hxx"
|
2013-11-24 20:20:57 +01:00
|
|
|
#include "system/PeriodClock.hxx"
|
2014-08-07 18:35:57 +02:00
|
|
|
#include "fs/io/BufferedOutputStream.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2008-04-12 06:19:26 +02:00
|
|
|
|
2009-02-28 21:12:15 +01:00
|
|
|
#include <assert.h>
|
2009-01-03 14:52:56 +01:00
|
|
|
#include <stdlib.h>
|
2008-12-29 17:28:32 +01:00
|
|
|
|
2006-07-31 01:32:54 +02:00
|
|
|
#define SW_VOLUME_STATE "sw_volume: "
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
static constexpr Domain volume_domain("volume");
|
|
|
|
|
2009-07-06 21:52:10 +02:00
|
|
|
static unsigned volume_software_set = 100;
|
2006-07-16 18:53:04 +02:00
|
|
|
|
2009-02-28 21:12:15 +01:00
|
|
|
/** the cached hardware mixer value; invalid if negative */
|
|
|
|
static int last_hardware_volume = -1;
|
|
|
|
/** the age of #last_hardware_volume */
|
2013-11-24 20:20:57 +01:00
|
|
|
static PeriodClock hardware_volume_clock;
|
2009-02-28 21:12:15 +01:00
|
|
|
|
2014-02-05 23:20:33 +01:00
|
|
|
void
|
|
|
|
InvalidateHardwareVolume()
|
2009-10-21 09:48:37 +02:00
|
|
|
{
|
|
|
|
/* flush the hardware volume cache */
|
|
|
|
last_hardware_volume = -1;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2014-01-27 08:20:25 +01:00
|
|
|
int
|
|
|
|
volume_level_get(const MultipleOutputs &outputs)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-28 21:12:15 +01:00
|
|
|
if (last_hardware_volume >= 0 &&
|
2016-12-27 22:47:20 +01:00
|
|
|
!hardware_volume_clock.CheckUpdate(std::chrono::seconds(1)))
|
2009-02-28 21:12:15 +01:00
|
|
|
/* throttle access to hardware mixers */
|
|
|
|
return last_hardware_volume;
|
|
|
|
|
2014-01-27 08:20:25 +01:00
|
|
|
last_hardware_volume = outputs.GetVolume();
|
2009-03-14 11:35:40 +01:00
|
|
|
return last_hardware_volume;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2014-01-27 08:20:25 +01:00
|
|
|
static bool
|
|
|
|
software_volume_change(MultipleOutputs &outputs, unsigned volume)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-07-06 21:52:10 +02:00
|
|
|
assert(volume <= 100);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-07-06 21:51:24 +02:00
|
|
|
volume_software_set = volume;
|
2014-01-27 08:20:25 +01:00
|
|
|
outputs.SetSoftwareVolume(volume);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-03-14 11:10:21 +01:00
|
|
|
return true;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2014-01-27 08:20:25 +01:00
|
|
|
static bool
|
|
|
|
hardware_volume_change(MultipleOutputs &outputs, unsigned volume)
|
2008-12-31 16:46:41 +01:00
|
|
|
{
|
2009-02-28 21:12:15 +01:00
|
|
|
/* reset the cache */
|
|
|
|
last_hardware_volume = -1;
|
|
|
|
|
2014-01-27 08:20:25 +01:00
|
|
|
return outputs.SetVolume(volume);
|
2008-12-31 16:46:41 +01:00
|
|
|
}
|
|
|
|
|
2014-01-27 08:20:25 +01:00
|
|
|
bool
|
|
|
|
volume_level_change(MultipleOutputs &outputs, unsigned volume)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-07-06 21:52:10 +02:00
|
|
|
assert(volume <= 100);
|
|
|
|
|
2009-07-06 22:00:50 +02:00
|
|
|
volume_software_set = volume;
|
|
|
|
|
2008-10-14 22:38:14 +02:00
|
|
|
idle_add(IDLE_MIXER);
|
|
|
|
|
2014-01-27 08:20:25 +01:00
|
|
|
return hardware_volume_change(outputs, volume);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-31 01:32:54 +02:00
|
|
|
|
2009-07-15 16:57:37 +02:00
|
|
|
bool
|
2014-01-27 08:20:25 +01:00
|
|
|
read_sw_volume_state(const char *line, MultipleOutputs &outputs)
|
2006-07-31 01:32:54 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
char *end = nullptr;
|
2006-07-31 01:32:54 +02:00
|
|
|
long int sv;
|
|
|
|
|
2015-11-11 15:34:36 +01:00
|
|
|
line = StringAfterPrefix(line, SW_VOLUME_STATE);
|
|
|
|
if (line == nullptr)
|
2009-07-15 16:57:37 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
sv = strtol(line, &end, 10);
|
|
|
|
if (*end == 0 && sv >= 0 && sv <= 100)
|
2014-01-27 08:20:25 +01:00
|
|
|
software_volume_change(outputs, sv);
|
2009-07-15 16:57:37 +02:00
|
|
|
else
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(volume_domain,
|
|
|
|
"Can't parse software volume: %s", line);
|
2009-07-15 16:57:37 +02:00
|
|
|
return true;
|
2006-07-31 01:32:54 +02:00
|
|
|
}
|
|
|
|
|
2014-07-30 20:58:14 +02:00
|
|
|
void
|
|
|
|
save_sw_volume_state(BufferedOutputStream &os)
|
2006-07-31 01:32:54 +02:00
|
|
|
{
|
2014-07-30 20:58:14 +02:00
|
|
|
os.Format(SW_VOLUME_STATE "%u\n", volume_software_set);
|
2006-07-31 01:32:54 +02:00
|
|
|
}
|
2009-10-08 15:22:39 +02:00
|
|
|
|
|
|
|
unsigned
|
|
|
|
sw_volume_state_get_hash(void)
|
|
|
|
{
|
|
|
|
return volume_software_set;
|
|
|
|
}
|