2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2022-07-14 17:58:12 +02:00
|
|
|
* Copyright 2003-2022 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
|
|
|
|
2022-08-08 23:15:09 +02:00
|
|
|
#include "Memento.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"
|
2021-12-03 14:02:07 +01:00
|
|
|
#include "io/BufferedOutputStream.hxx"
|
2008-04-12 06:19:26 +02:00
|
|
|
|
2022-07-13 14:06:28 +02:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
|
|
|
|
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
|
|
|
|
2014-01-27 08:20:25 +01:00
|
|
|
int
|
2022-08-08 23:15:09 +02:00
|
|
|
MixerMemento::GetVolume(const MultipleOutputs &outputs) noexcept
|
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
|
|
|
}
|
|
|
|
|
2022-08-08 23:15:09 +02:00
|
|
|
inline bool
|
|
|
|
MixerMemento::SetSoftwareVolume(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
|
|
|
}
|
|
|
|
|
2022-08-08 23:15:09 +02:00
|
|
|
inline void
|
|
|
|
MixerMemento::SetHardwareVolume(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;
|
|
|
|
|
2022-07-08 10:46:15 +02:00
|
|
|
outputs.SetVolume(volume);
|
2008-12-31 16:46:41 +01:00
|
|
|
}
|
|
|
|
|
2022-07-08 10:46:15 +02:00
|
|
|
void
|
2022-08-08 23:15:09 +02:00
|
|
|
MixerMemento::SetVolume(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;
|
|
|
|
|
2022-08-08 23:15:09 +02:00
|
|
|
SetHardwareVolume(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
|
2022-08-08 23:15:09 +02:00
|
|
|
MixerMemento::LoadSoftwareVolumeState(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)
|
2022-08-08 23:15:09 +02:00
|
|
|
SetSoftwareVolume(outputs, sv);
|
2022-08-08 23:13:03 +02:00
|
|
|
|
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
|
2022-08-08 23:15:09 +02:00
|
|
|
MixerMemento::SaveSoftwareVolumeState(BufferedOutputStream &os) const
|
2006-07-31 01:32:54 +02:00
|
|
|
{
|
2022-07-13 12:54:58 +02:00
|
|
|
os.Fmt(FMT_STRING(SW_VOLUME_STATE "{}\n"), volume_software_set);
|
2006-07-31 01:32:54 +02:00
|
|
|
}
|